File: msan_aottest.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (538 lines) | stat: -rw-r--r-- 21,540 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#ifdef _WIN32
#include <stdio.h>
int main(int argc, char **argv) {
    printf("[SKIP] MSAN isn't supported for any Windows variant.\n");
    return 0;
}
#else
#include "HalideBuffer.h"
#include "HalideRuntime.h"

#include <iostream>
#include <limits>
#include <type_traits>
#include <vector>

#include "msan.h"

using namespace std;
using namespace Halide::Runtime;
using MsanBuffer = Halide::Runtime::Buffer<uint8_t, 3>;

enum {
    AnnotateBoundsInferenceBuffer,
    AnnotateBoundsInferenceShape,
    AnnotateIntermediateBuffer,
    AnnotateIntermediateShape,
    AnnotateOutputBuffer,
    AnnotateOutputShape,
    AnnotateIntermediateContents,
    AnnotateOutputContents,
} annotate_stage = AnnotateBoundsInferenceBuffer;

enum {
    CheckInputBuffer,
    CheckInputShape,
    CheckInputContents,
    CheckExternResultBuffer,
    CheckExternResultShape,
    CheckExternResultContents,
} check_stage = CheckInputBuffer;

const void *output_base = nullptr;
const void *output_previous = nullptr;
int bounds_inference_count = 0;
bool expect_intermediate_buffer_error = false;
bool skip_extern_copy = false;
uint64_t input_contents_checked = 0;
uint64_t input_contents_uninitialized = 0;
uint64_t externresult_contents_checked = 0;
uint64_t externresult_contents_uninitialized = 0;
uint64_t output_contents_annotated = 0;

void reset_state(const MsanBuffer &in, const MsanBuffer &out) {
    annotate_stage = AnnotateBoundsInferenceBuffer;
    check_stage = CheckInputBuffer;
    output_base = out.data();
    output_previous = nullptr;
    bounds_inference_count = 0;
    expect_intermediate_buffer_error = false;
    skip_extern_copy = false;
    input_contents_uninitialized = 0;
    input_contents_checked = 0;
    externresult_contents_uninitialized = 0;
    externresult_contents_checked = 0;
    output_contents_annotated = 0;
    // printf("IN-DATA:  %p:%p:%p\n", (void *)in.raw_buffer(), (void *)in.raw_buffer()->dim, (void *)in.data());
    // printf("OUT-DATA: %p:%p:%p\n", (void *)out.raw_buffer(), (void *)out.raw_buffer()->dim, (void *)out.data());
}

// Just copies in -> out.
extern "C" int msan_extern_stage(halide_buffer_t *in, halide_buffer_t *out) {
    if (in->is_bounds_query() || out->is_bounds_query()) {
        if (in->is_bounds_query()) {
            assert(in->dimensions == 3);
            in->dim[0].extent = 4;
            in->dim[1].extent = 4;
            in->dim[2].extent = 3;
            in->dim[0].min = 0;
            in->dim[1].min = 0;
            in->dim[2].min = 0;
        }
        if (out->is_bounds_query()) {
            assert(out->dimensions == 3);
            out->dim[0].extent = 4;
            out->dim[1].extent = 4;
            out->dim[2].extent = 3;
            out->dim[0].min = 0;
            out->dim[1].min = 0;
            out->dim[2].min = 0;
        }
        return 0;
    }

    if (in->type != out->type) {
        fprintf(stderr, "type mismatch\n");
        return halide_error_code_generic_error;
    }
    if (skip_extern_copy) {
        // Fill it with zero to mimic msan "poison".
        MsanBuffer(*out).fill(0);
    } else {
        MsanBuffer(*out).copy_from(MsanBuffer(*in));
    }
    out->set_host_dirty();
    return halide_error_code_success;
}

extern "C" void halide_error(void *user_context, const char *msg) {
    // Emitting "error.*:" to stdout or stderr will cause CMake to report the
    // test as a failure on Windows, regardless of error code returned,
    // hence the abbreviation to "err".
    // fprintf(stderr, "Saw err: %s\n", msg);
    // Do not exit.
}

// Must provide a stub for these since we aren't compiling with LLVM MSAN
// enabled, and the default implementation of our msan-specific runtime needs them.
extern "C" void __msan_check_mem_is_initialized(const void *mem, size_t size) {
    fprintf(stderr, "Impossible\n");
    exit(1);
}

extern "C" void __msan_unpoison(const void *mem, size_t size) {
    fprintf(stderr, "Impossible\n");
    exit(1);
}

extern "C" long __msan_test_shadow(const void *mem, size_t size) {
    fprintf(stderr, "Impossible\n");
    exit(1);
}

extern "C" int halide_msan_check_memory_is_initialized(void *user_context, const void *ptr, uint64_t len, const char *name) {
    // printf("CHECK-MEM: %d:%p:%08x for buf %s\n", (int)check_stage, ptr, (unsigned int)len, name);
    if (check_stage == CheckInputBuffer) {
        if (len != sizeof(halide_buffer_t)) {
            fprintf(stderr, "Failure: Expected sizeof(halide_buffer_t), saw %d\n", (unsigned int)len);
            exit(1);
        }
        check_stage = CheckInputShape;
    } else if (check_stage == CheckInputShape) {
        if (len != sizeof(halide_dimension_t) * 3) {
            fprintf(stderr, "Failure: Expected sizeof(halide_dimension_t) * 3, saw %d\n", (unsigned int)len);
            exit(1);
        }
        check_stage = CheckInputContents;
    } else if (check_stage == CheckInputContents) {
        for (uint64_t i = 0; i < len; ++i) {
            input_contents_uninitialized += (((const uint8_t *)ptr)[i] == 0);
        }
        input_contents_checked += len;
        check_stage = CheckExternResultBuffer;
    } else if (check_stage == CheckExternResultBuffer) {
        if (len != sizeof(halide_buffer_t)) {
            fprintf(stderr, "Failure: Expected sizeof(halide_buffer_t), saw %d\n", (unsigned int)len);
            exit(1);
        }
        check_stage = CheckExternResultShape;
    } else if (check_stage == CheckExternResultShape) {
        if (len != sizeof(halide_dimension_t) * 3) {
            fprintf(stderr, "Failure: Expected sizeof(halide_dimension_t) * 3, saw %d\n", (unsigned int)len);
            exit(1);
        }
        check_stage = CheckExternResultContents;
    } else if (check_stage == CheckExternResultContents) {
        for (uint64_t i = 0; i < len; ++i) {
            externresult_contents_uninitialized += (((const uint8_t *)ptr)[i] == 0);
        }
        externresult_contents_checked += len;
    } else {
        fprintf(stderr, "Failure: bad enum\n");
        exit(1);
    }
    return 0;
}

extern "C" int halide_msan_annotate_memory_is_initialized(void *user_context, const void *ptr, uint64_t len) {
    // printf("ANNOTATE: %d:%p:%08x\n", (int)annotate_stage, ptr, (unsigned int)len);
    if (annotate_stage == AnnotateBoundsInferenceBuffer) {
        if (output_previous != nullptr || len != sizeof(halide_buffer_t)) {
            fprintf(stderr, "Failure: Expected sizeof(halide_buffer_t), saw %d\n", (unsigned int)len);
            exit(1);
        }
        annotate_stage = AnnotateBoundsInferenceShape;
    } else if (annotate_stage == AnnotateBoundsInferenceShape) {
        if (output_previous != nullptr || len != sizeof(halide_dimension_t) * 3) {
            fprintf(stderr, "Failure: Expected sizeof(halide_dimension_t) * 3, saw %d\n", (unsigned int)len);
            exit(1);
        }
        bounds_inference_count += 1;
        if (bounds_inference_count == 4) {
            annotate_stage = AnnotateIntermediateBuffer;
        } else {
            annotate_stage = AnnotateBoundsInferenceBuffer;
        }
    } else if (annotate_stage == AnnotateIntermediateBuffer) {
        if (expect_intermediate_buffer_error) {
            if (len != 80) {
                fprintf(stderr, "Failure: Expected error message of len=80, saw %d bytes\n", (unsigned int)len);
                exit(1);
            }
            return 0;  // stay in this state
        }
        if (output_previous != nullptr || len != sizeof(halide_buffer_t)) {
            fprintf(stderr, "Failure: Expected sizeof(halide_buffer_t), saw %d\n", (unsigned int)len);
            exit(1);
        }
        annotate_stage = AnnotateIntermediateShape;
    } else if (annotate_stage == AnnotateIntermediateShape) {
        if (output_previous != nullptr || len != sizeof(halide_dimension_t) * 3) {
            fprintf(stderr, "Failure: Expected sizeof(halide_dimension_t) * 3, saw %d\n", (unsigned int)len);
            exit(1);
        }
        annotate_stage = AnnotateOutputBuffer;
    } else if (annotate_stage == AnnotateOutputBuffer) {
        if (output_previous != nullptr || len != sizeof(halide_buffer_t)) {
            fprintf(stderr, "Failure: Expected sizeof(halide_buffer_t), saw %d\n", (unsigned int)len);
            exit(1);
        }
        annotate_stage = AnnotateOutputShape;
    } else if (annotate_stage == AnnotateOutputShape) {
        if (output_previous != nullptr || len != sizeof(halide_dimension_t) * 3) {
            fprintf(stderr, "Failure: Expected sizeof(halide_dimension_t) * 3, saw %d\n", (unsigned int)len);
            exit(1);
        }
        annotate_stage = AnnotateIntermediateContents;
    } else if (annotate_stage == AnnotateIntermediateContents) {
        if (output_previous != nullptr || len != 4 * 4 * 3) {
            fprintf(stderr, "Failure: Expected %d, saw %d\n", 4 * 4 * 3, (unsigned int)len);
            exit(1);
        }
        annotate_stage = AnnotateOutputContents;
    } else if (annotate_stage == AnnotateOutputContents) {
        if (output_previous == nullptr) {
            if (ptr != output_base) {
                fprintf(stderr, "Failure: Expected base p %p but saw %p\n", output_base, ptr);
                exit(1);
            }
            if (ptr <= output_previous) {
                fprintf(stderr, "Failure: Expected monotonic increase but saw %p -> %p\n", output_previous, ptr);
                exit(1);
            }
            output_previous = ptr;
        }
        output_contents_annotated += len;
    } else {
        fprintf(stderr, "Failure: bad enum\n");
        exit(1);
    }
    return 0;
}

template<typename T>
void verify(const T &image) {
    image.for_each_element([&](int x, int y, int c) {
        int expected = 7;
        for (int i = 0; i < 4; ++i) {
            expected += (uint8_t)(i + y + c) | 0x01;
        }
        int actual = image(x, y, c);
        if (actual != expected) {
            fprintf(stderr, "Failure @ %d %d %d: expected %d, got %d\n", x, y, c, expected, actual);
            exit(1);
        }
    });
}

MsanBuffer make_input_for(const MsanBuffer &output) {
    auto input = MsanBuffer::make_with_shape_of(output);
    // Ensure that no 'valid' inputs are all-zero
    input.for_each_element([&](int x, int y, int c) { input(x, y, c) = (uint8_t)(x + y + c) | 0x01; });
    return input;
}

//-----------------------------------------------------------------------------

int main() {
    printf("Testing interleaved...\n");
    {
        auto out = MsanBuffer::make_interleaved(4, 4, 3);
        auto in = make_input_for(out);
        reset_state(in, out);
        if (msan(in, out) != 0) {
            fprintf(stderr, "Failure!\n");
            exit(1);
        }
        verify(out);
        if (input_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: input_contents_uninitialized is wrong (%d).\n", (int)input_contents_uninitialized);
            exit(1);
        }
        if (input_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: input_contents_checked is wrong (%d).\n", (int)input_contents_checked);
            exit(1);
        }
        if (output_contents_annotated != 4 * 4 * 3) {
            fprintf(stderr, "Failure: output_contents_annotated is wrong (%d).\n", (int)output_contents_annotated);
            exit(1);
        }
        if (output_previous == nullptr) {
            fprintf(stderr, "Failure: Expected to see annotations.\n");
            exit(1);
        }
    }

    printf("Testing sparse interleaved...\n");
    {
        const int kPad = 1;
        halide_dimension_t shape[3] = {
            {0, 4, 3},
            {0, 4, (4 * 3) + kPad},
            {0, 3, 1},
        };
        std::vector<uint8_t> data(((4 * 3) + kPad) * 4);
        auto out = MsanBuffer(data.data(), 3, shape);
        auto in = make_input_for(out);
        reset_state(in, out);
        if (msan(in, out) != 0) {
            fprintf(stderr, "Failure!\n");
            exit(1);
        }
        if (input_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: input_contents_uninitialized is wrong (%d).\n", (int)input_contents_uninitialized);
            exit(1);
        }
        if (input_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: input_contents_checked is wrong (%d).\n", (int)input_contents_checked);
            exit(1);
        }
        if (externresult_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: externresult_contents_uninitialized is wrong (%d).\n", (int)externresult_contents_uninitialized);
            exit(1);
        }
        if (externresult_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: externresult_contents_checked is wrong (%d).\n", (int)externresult_contents_checked);
            exit(1);
        }
        if (output_contents_annotated != 4 * 4 * 3) {
            fprintf(stderr, "Failure: output_contents_annotated is wrong (%d).\n", (int)output_contents_annotated);
            exit(1);
        }
        if (output_previous == nullptr) {
            fprintf(stderr, "Failure: Expected to see annotations.\n");
            exit(1);
        }
    }

    printf("Testing planar...\n");
    {
        auto out = MsanBuffer(4, 4, 3);
        auto in = make_input_for(out);
        reset_state(in, out);
        if (msan(in, out) != 0) {
            fprintf(stderr, "Failure!\n");
            exit(1);
        }
        if (input_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: input_contents_uninitialized is wrong (%d).\n", (int)input_contents_uninitialized);
            exit(1);
        }
        if (input_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: input_contents_checked is wrong (%d).\n", (int)input_contents_checked);
            exit(1);
        }
        if (externresult_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: externresult_contents_uninitialized is wrong (%d).\n", (int)externresult_contents_uninitialized);
            exit(1);
        }
        if (externresult_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: externresult_contents_checked is wrong (%d).\n", (int)externresult_contents_checked);
            exit(1);
        }
        if (output_contents_annotated != 4 * 4 * 3) {
            fprintf(stderr, "Failure: output_contents_annotated is wrong (%d).\n", (int)output_contents_annotated);
            exit(1);
        }
        if (output_previous == nullptr) {
            fprintf(stderr, "Failure: Expected to see annotations.\n");
            exit(1);
        }
    }

    printf("Testing sparse planar...\n");
    {
        const int kPad = 1;
        halide_dimension_t shape[3] = {
            {0, 4, 1},
            {0, 4, 4 + kPad},
            {0, 3, (4 + kPad) * 4},
        };
        std::vector<uint8_t> data((4 + kPad) * 4 * 3);
        auto out = MsanBuffer(data.data(), 3, shape);
        auto in = make_input_for(out);
        reset_state(in, out);
        if (msan(in, out) != 0) {
            fprintf(stderr, "Failure!\n");
            exit(1);
        }
        if (input_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: input_contents_uninitialized is wrong (%d).\n", (int)input_contents_uninitialized);
            exit(1);
        }
        if (input_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: input_contents_checked is wrong (%d).\n", (int)input_contents_checked);
            exit(1);
        }
        if (externresult_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: externresult_contents_uninitialized is wrong (%d).\n", (int)externresult_contents_uninitialized);
            exit(1);
        }
        if (externresult_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: externresult_contents_checked is wrong (%d).\n", (int)externresult_contents_checked);
            exit(1);
        }
        if (output_contents_annotated != 4 * 4 * 3) {
            fprintf(stderr, "Failure: output_contents_annotated is wrong (%d).\n", (int)output_contents_annotated);
            exit(1);
        }
        if (output_previous == nullptr) {
            fprintf(stderr, "Failure: Expected to see annotations.\n");
            exit(1);
        }
    }

    // Buffers should not be marked as "initialized" if the filter fails with an error.
    printf("Verifying that output is not marked when error occurs...\n");
    {
        auto out = MsanBuffer(1, 1, 1);
        auto in = make_input_for(out);
        reset_state(in, out);
        expect_intermediate_buffer_error = true;
        if (msan(in, out) == 0) {
            fprintf(stderr, "Failure (expected failure but did not)!\n");
            exit(1);
        }
        if (input_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: input_contents_uninitialized is wrong (%d).\n", (int)input_contents_uninitialized);
            exit(1);
        }
        if (input_contents_checked != 1) {
            fprintf(stderr, "Failure: input_contents_checked is wrong (%d).\n", (int)input_contents_checked);
            exit(1);
        }
        if (externresult_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: externresult_contents_uninitialized is wrong (%d).\n", (int)externresult_contents_uninitialized);
            exit(1);
        }
        if (externresult_contents_checked != 0) {
            fprintf(stderr, "Failure: externresult_contents_checked is wrong (%d).\n", (int)externresult_contents_checked);
            exit(1);
        }
        if (output_contents_annotated != 0) {
            fprintf(stderr, "Failure: expected no output contents to be annotated.\n");
            exit(1);
        }
        if (output_previous != nullptr) {
            fprintf(stderr, "Failure: Expected NOT to see annotations.\n");
            exit(1);
        }
    }

    // Buffers should not be marked as "initialized" if the filter fails with an error.
    // We'll test the mechanism by ensuring that our valid input buffer never has
    // only nonzero elements, and then checking for those.
    printf("Verifying that input is checked for initialization...\n");
    {
        auto out = MsanBuffer::make_interleaved(4, 4, 3);
        auto in = make_input_for(out);
        // Make exactly one element "uninitialized"
        in(3, 2, 1) = 0;
        reset_state(in, out);
        // Note that with "real" msan in place, we would expect this to never return;
        // halide_msan_check_memory_is_initialized() would abort if it encounters
        // uninitialized memory. It's hard to simulate that in our test harness, so
        // we'll actually let it "complete" successfully and check the uninitialized state at the end.
        if (msan(in, out) != 0) {
            fprintf(stderr, "Failure!\n");
            exit(1);
        }
        if (input_contents_uninitialized != sizeof(uint8_t)) {
            fprintf(stderr, "Failure: input_contents_uninitialized is wrong (%d).\n", (int)input_contents_uninitialized);
            exit(1);
        }
        if (input_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: input_contents_checked is wrong (%d).\n", (int)input_contents_checked);
            exit(1);
        }
        if (externresult_contents_uninitialized != 0) {
            fprintf(stderr, "Failure: externresult_contents_uninitialized is wrong (%d).\n", (int)externresult_contents_uninitialized);
            exit(1);
        }
        if (externresult_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: externresult_contents_checked is wrong (%d).\n", (int)externresult_contents_checked);
            exit(1);
        }
        // Don't bother checking outputs here.
    }

    printf("Verifying that result of define_extern is checked for initialization...\n");
    {
        auto out = MsanBuffer::make_interleaved(4, 4, 3);
        auto in = make_input_for(out);
        // Make exactly one element "uninitialized"
        in(3, 2, 1) = 0;
        reset_state(in, out);
        skip_extern_copy = true;
        // Note that with "real" msan in place, we would expect this to never return;
        // halide_msan_check_memory_is_initialized() would abort if it encounters
        // uninitialized memory. It's hard to simulate that in our test harness, so
        // we'll actually let it "complete" successfully and check the uninitialized state at the end.
        if (msan(in, out) != 0) {
            fprintf(stderr, "Failure!\n");
            exit(1);
        }
        if (input_contents_uninitialized != sizeof(uint8_t)) {
            fprintf(stderr, "Failure: input_contents_uninitialized is wrong (%d).\n", (int)input_contents_uninitialized);
            exit(1);
        }
        if (input_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: input_contents_checked is wrong (%d).\n", (int)input_contents_checked);
            exit(1);
        }
        if (externresult_contents_uninitialized != 4 * 4 * 3) {
            fprintf(stderr, "Failure: externresult_contents_uninitialized is wrong (%d).\n", (int)externresult_contents_uninitialized);
            exit(1);
        }
        if (externresult_contents_checked != 4 * 4 * 3) {
            fprintf(stderr, "Failure: externresult_contents_checked is wrong (%d).\n", (int)externresult_contents_checked);
            exit(1);
        }
        // Don't bother checking outputs here.
    }

    printf("Success!\n");
    return 0;
}

#endif