~jamii/imp

ref: 506a516200d479706f31b17ba422aa5e4de52493 imp/test/fuzz.zig -rw-r--r-- 22.9 KiB
506a5162Jamie Brandon Bump zig version 3 months ago
                                                                                
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
const imp = @import("../lib/imp.zig");
usingnamespace imp.common;
const meta = imp.meta;

fn fuzz_panic() noreturn {
    imp_panic("Internal error in fuzzer", .{});
}

const Input = struct {
    allocator: *Allocator,
    arena: *ArenaAllocator,
    bytes: []const u8,
    i: usize,
    closed_ranges: ArrayList([2]usize), // [lo, hi)
    open_ranges: ArrayList(usize),

    fn init(allocator: *Allocator, bytes: []const u8) Input {
        var arena = allocator.create(ArenaAllocator) catch fuzz_panic();
        arena.* = ArenaAllocator.init(allocator);
        return Input{
            .allocator = allocator,
            .arena = arena,
            .bytes = bytes,
            .i = 0,
            .closed_ranges = ArrayList([2]usize).init(allocator),
            .open_ranges = ArrayList(usize).init(allocator),
        };
    }

    fn deinit(self: *Input) void {
        self.arena.deinit();
        self.allocator.destroy(self.arena);
        self.closed_ranges.deinit();
        self.open_ranges.deinit();
    }

    fn getBytes(self: *Input, comptime len: usize) [len]u8 {
        var bytes: [len]u8 = undefined;
        var i: usize = 0;
        while (i < len) : ({
            i += 1;
            self.i += 1;
        }) {
            if (self.i < self.bytes.len) {
                bytes[i] = self.bytes[self.i];
            } else {
                bytes[i] = 0;
            }
        }
        return bytes;
    }

    fn startRange(self: *Input) void {
        self.open_ranges.append(self.i) catch fuzz_panic();
    }

    fn endRange(self: *Input) void {
        const lo = self.open_ranges.pop();
        const hi = min(self.i, self.bytes.len);
        if (lo < self.bytes.len) {
            self.closed_ranges.append(.{ lo, hi }) catch fuzz_panic();
        }
    }
};

fn generatorReturnType(comptime gen: type) type {
    return std.meta.declarationInfo(gen, "generate").data.Fn.return_type;
}

const Void = struct {
    fn generate(input: *Input) void {
        return;
    }
};

fn Int(comptime T: type) type {
    return struct {
        fn generate(input: *Input) T {
            input.startRange();
            // this is copied from std.rand.Random.int
            const UnsignedT = std.meta.IntType(false, T.bit_count);
            const ByteAlignedT = std.meta.IntType(false, @divTrunc(T.bit_count + 7, 8) * 8);
            const bytes = input.getBytes(@sizeOf(ByteAlignedT));
            const byte_aligned_result = std.mem.readIntSliceLittle(ByteAlignedT, &bytes);
            const unsigned_result = @truncate(UnsignedT, byte_aligned_result);
            const output = @bitCast(T, unsigned_result);
            input.endRange();
            return output;
        }
    };
}

fn Float(comptime T: type) type {
    return struct {
        fn generate(input: *Input) T {
            input.startRange();
            // this is copied from std.rand.Random.float
            const output = output: {
                switch (T) {
                    f32 => {
                        const s = Int(u32).generate(input);
                        const repr = (0x7f << 23) | (s >> 9);
                        break :output @bitCast(f32, repr) - 1.0;
                    },
                    f64 => {
                        const s = Int(u64).generate(input);
                        const repr = (0x3ff << 52) | (s >> 12);
                        break :output @bitCast(f64, repr) - 1.0;
                    },
                    else => @compileError("unknown floating point type"),
                }
            };
            input.endRange();
            return output;
        }
    };
}

const Bool = struct {
    fn generate(input: *Input) bool {
        return Int(u1).generate(input) == 1;
    }
};

fn Slice(comptime elem_gen: type, comptime min_len: usize) type {
    const T = generatorReturnType(elem_gen);
    return struct {
        fn generate(input: *Input) []T {
            var output = ArrayList(T).init(&input.arena.allocator);
            input.startRange();
            while (true) {
                input.startRange();
                defer input.endRange();
                if (output.items.len > min_len and !Bool.generate(input)) break;
                output.append(elem_gen.generate(input)) catch fuzz_panic();
            }
            input.endRange();
            return output.items;
        }
    };
}

fn Ascii(comptime min_len: usize) type {
    return struct {
        fn generate(input: *Input) []u8 {
            var output = ArrayList(u8).init(&input.arena.allocator);
            const bytes = Slice(Int(u7), min_len).generate(input);
            for (bytes) |byte| {
                output.append(byte) catch fuzz_panic();
            }
            return output.items;
        }
    };
}

fn Utf8(comptime min_len: usize) type {
    return struct {
        fn generate(input: *Input) []u8 {
            var output = ArrayList(u8).init(&input.arena.allocator);
            input.startRange();
            while (true) {
                input.startRange();
                defer input.endRange();
                if (output.items.len > min_len and !Bool.generate(input)) break;
                while (true) {
                    // 0 is a valid char so this won't loop forever
                    const char = Int(u21).generate(input);
                    var bytes = [4]u8{ 0, 0, 0, 0 };
                    if (std.unicode.utf8Encode(char, &bytes)) |len| {
                        output.appendSlice(bytes[0..len]) catch fuzz_panic();
                        break;
                    } else |_| {
                        // ignore this char
                    }
                }
            }
            input.endRange();
            return output.items;
        }
    };
}

fn Struct(comptime T: type, comptime field_gens: anytype) type {
    return struct {
        fn generate(input: *Input) T {
            input.startRange();
            var output: T = undefined;
            inline for (@typeInfo(T).Struct.fields) |field| {
                @field(output, field.name) = @field(field_gens, field.name).generate(input);
            }
            input.endRange();
            return output;
        }
    };
}

fn Enum(comptime T: type) type {
    return struct {
        fn generate(input: *Input) T {
            input.startRange();
            const field_ix = Int(@typeInfo(T).Enum.tag_type).generate(input) % @typeInfo(T).Enum.fields.len;
            var output: T = undefined;
            inline for (@typeInfo(T).Enum.fields) |field, i| {
                if (field_ix == i) {
                    output = @intToEnum(T, field.value);
                }
            }
            input.endRange();
            return output;
        }
    };
}

fn Union(comptime T: type, comptime field_gens: anytype) type {
    return struct {
        fn generate(input: *Input) T {
            input.startRange();
            var output: T = undefined;
            const field_ix = min(Int(u8).generate(input), @typeInfo(T).Union.fields.len - 1);
            inline for (@typeInfo(T).Union.fields) |field, i| {
                if (field_ix == i) {
                    output = @unionInit(T, field.name, @field(field_gens, field.name).generate(input));
                }
            }
            input.endRange();
            return output;
        }
    };
}

fn Ptr(comptime gen: anytype) type {
    const T = generatorReturnType(gen);
    return struct {
        fn generate(input: *Input) T {
            var output = input.arena.allocator.create(T);
            const value = gen.generate(input);
            output.* = value;
        }
    };
}

const Scalar = struct {
    fn generate(input: *Input) imp.lang.repr.value.Scalar {
        if (Bool.generate(input)) {
            return .{ .Text = Utf8(0).generate(input) };
        } else {
            return .{ .Number = Float(f64).generate(input) };
        }
        // never generate Box - it's not a valid syntax literal
    }
};

const Name = Ascii(1);

const Syntax = struct {
    const Pair = Struct(imp.lang.repr.syntax.Pair, .{
        .left = PtrExpr,
        .right = PtrExpr,
    });

    const Fix = Struct(imp.lang.repr.syntax.Fix, .{
        .init = PtrExpr,
        .next = PtrExpr,
    });

    const Reduce = Struct(imp.lang.repr.syntax.Reduce, .{
        .input = PtrExpr,
        .init = PtrExpr,
        .next = PtrExpr,
    });

    const When = Struct(imp.lang.repr.syntax.When, .{
        .condition = PtrExpr,
        .true_branch = PtrExpr,
    });

    const Arg = Struct(imp.lang.repr.syntax.Arg, .{
        .name = Name,
        .unbox = Bool,
    });

    const Annotate = Struct(imp.lang.repr.syntax.Annotate, .{
        .annotation = Name,
        .body = PtrExpr,
    });

    const If = Struct(imp.lang.repr.syntax.If, .{
        .condition = PtrExpr,
        .true_branch = PtrExpr,
        .false_branch = PtrExpr,
    });

    const Let = Struct(imp.lang.repr.syntax.Let, .{
        .name = Name,
        .value = PtrExpr,
        .body = PtrExpr,
    });

    const Lookup = Struct(imp.lang.repr.syntax.Lookup, .{
        .value = PtrExpr,
        .name = Name,
    });

    const Expr = Union(imp.lang.repr.syntax.Expr, .{
        .None = Void,
        .Some = Void,
        .Scalar = Scalar,
        .Union = Pair,
        .Intersect = Pair,
        .Product = Pair,
        .Equal = Pair,
        .Name = Name,
        .When = When,
        .Arg = Arg,
        .Apply = Pair,
        .Box = PtrExpr,
        .Fix = Fix,
        .Reduce = Reduce,
        .Enumerate = PtrExpr,
        .Annotate = Annotate,

        .Negate = PtrExpr,
        .If = If,
        .Let = Let,
        .Lookup = Lookup,
    });

    const PtrExpr = struct {
        fn generate(input: *Input) *const imp.lang.repr.syntax.Expr {
            var output = input.arena.allocator.create(imp.lang.repr.syntax.Expr) catch fuzz_panic();
            output.* = Expr.generate(input);
            return output;
        }
    };
};

const Core = struct {
    const Pair = Struct(imp.lang.repr.core.Pair, .{
        .left = PtrExpr,
        .right = PtrExpr,
    });

    // will fix these up in makeValid
    const NameIx = Int(usize);

    const When = Struct(imp.lang.repr.core.When, .{
        .condition = PtrExpr,
        .true_branch = PtrExpr,
    });

    const Box = Struct(imp.lang.repr.core.Box, .{
        .body = PtrExpr,
        // will fix this up in makeValid
        .scope = Slice(NameIx, 0),
    });

    const Fix = Struct(imp.lang.repr.core.Fix, .{
        .init = PtrExpr,
        .next = PtrExpr,
    });

    const Reduce = Struct(imp.lang.repr.core.Reduce, .{
        .input = PtrExpr,
        .init = PtrExpr,
        .next = PtrExpr,
    });

    const Annotate = Struct(imp.lang.repr.core.Annotate, .{
        .annotation = Name,
        .body = PtrExpr,
    });

    const Native = Enum(imp.lang.repr.core.Native);

    const Expr = Union(imp.lang.repr.core.Expr, .{
        .None = Void,
        .Some = Void,
        .Scalar = Scalar,
        .Union = Pair,
        .Intersect = Pair,
        .Product = Pair,
        .Equal = Pair,
        .Name = NameIx,
        .UnboxName = NameIx,
        .Negate = PtrExpr,
        .When = When,
        .Abstract = PtrExpr,
        .Apply = Pair,
        .Box = Box,
        .Fix = Fix,
        .Reduce = Reduce,
        .Enumerate = PtrExpr,
        .Annotate = Annotate,
        .Native = Native,
    });

    const PtrExpr = struct {
        fn generate(input: *Input) *const imp.lang.repr.core.Expr {
            var output = input.arena.allocator.create(imp.lang.repr.core.Expr) catch fuzz_panic();
            output.* = Expr.generate(input);
            return output;
        }
    };

    fn makeValid(store: *imp.lang.Store, invalid_expr: *const imp.lang.repr.core.Expr, scope_size: usize) *const imp.lang.repr.core.Expr {
        var valid_expr = invalid_expr.*;
        switch (valid_expr) {
            .Name, .UnboxName => |*name_ix| {
                // pick a valid name_ix
                if (scope_size == 0) {
                    valid_expr = .None;
                } else {
                    name_ix.* %= scope_size;
                }
            },
            .Box => |*box| {
                // set the scope that would be produced by Desugarer.desugarBox
                var scope = store.arena.allocator.alloc(imp.lang.repr.core.NameIx, scope_size) catch fuzz_panic();
                for (scope) |*name_ix, i| {
                    name_ix.* = i;
                }
                box.scope = scope;
            },
            else => {},
        }
        const child_scope_size = if (valid_expr == .Abstract) scope_size + 1 else scope_size;
        for (valid_expr.getChildrenMut().slice()) |child| {
            child.* = makeValid(store, child.*, child_scope_size);
        }
        return store.putCore(valid_expr, undefined) catch fuzz_panic();
    }

    const StoreAndExpr = struct {
        store: imp.lang.Store,
        expr: *const imp.lang.repr.core.Expr,
    };

    const StoreAndValidExpr = struct {
        fn generate(input: *Input) StoreAndExpr {
            var store = imp.lang.Store.init(input.arena);
            const invalid_expr = PtrExpr.generate(input);
            const valid_expr = makeValid(&store, invalid_expr, 0);
            return .{
                .store = store,
                .expr = valid_expr,
            };
        }
    };
};

const Options = struct {
    seed: u64 = 42,
    max_bytes: usize = 10_000,
    fuzz_iterations: usize = 100_000,
    shrink_iterations: usize = 100_000,
};

fn fuzz(allocator: *Allocator, options: Options, generator: anytype, tester: anytype) error{FuzzFailed}!void {
    var rng = std.rand.DefaultPrng.init(options.seed);
    var random = &rng.random;
    var fuzz_iteration: usize = 0;
    while (fuzz_iteration < options.fuzz_iterations) : (fuzz_iteration += 1) {
        // generate some random bytes
        var bytes = ArrayList(u8).init(allocator);
        var num_bytes = random.uintLessThan(usize, @divTrunc(options.max_bytes * fuzz_iteration, options.fuzz_iterations) + 1);
        while (num_bytes > 0) : (num_bytes -= 1) {
            bytes.append(random.int(u8)) catch fuzz_panic();
        }

        // test
        // TODO catch panic somehow?
        var input = Input.init(allocator, bytes.items);
        const value = generator.generate(&input);
        expect(input.open_ranges.items.len == 0);
        const result = tester(input.arena, value);
        assert(input.open_ranges.items.len == 0);
        if (result) |_| {
            bytes.deinit();
            input.deinit();
        } else |err| {
            warn("\nFound {} after {} fuzzes:\n", .{ err, fuzz_iteration });
            dump(value);

            // try to shrink input
            var most_shrunk_bytes = bytes;
            var most_shrunk_input = input;
            var most_shrunk_value = value;
            var most_shrunk_err = err;
            var shrink_iteration: usize = 0;
            while (shrink_iteration < options.shrink_iterations and most_shrunk_bytes.items.len > 0) : (shrink_iteration += 1) {
                // copy most_shrunk_bytes
                var shrunk_bytes = ArrayList(u8).initCapacity(allocator, most_shrunk_bytes.items.len) catch fuzz_panic();
                shrunk_bytes.appendSlice(most_shrunk_bytes.items) catch fuzz_panic();

                // decide how to shrink
                if (random.boolean()) {
                    // shrink a byte
                    const i = random.uintLessThan(usize, shrunk_bytes.items.len);
                    shrunk_bytes.items[i] = random.uintLessThan(u8, max(shrunk_bytes.items[i], 1));
                } else {
                    // remove a range
                    const range_ix = random.uintLessThan(usize, most_shrunk_input.closed_ranges.items.len);
                    const range = most_shrunk_input.closed_ranges.items[range_ix];
                    var i = range[1];
                    while (i < shrunk_bytes.items.len) : (i += 1) {
                        shrunk_bytes.items[i - (range[1] - range[0])] = shrunk_bytes.items[i];
                    }
                    shrunk_bytes.shrink(shrunk_bytes.items.len - (range[1] - range[0]));
                }

                // test again
                var shrunk_input = Input.init(allocator, shrunk_bytes.items);
                expect(shrunk_input.open_ranges.items.len == 0);
                const shrunk_value = generator.generate(&shrunk_input);
                const shrunk_result = tester(shrunk_input.arena, shrunk_value);
                assert(input.open_ranges.items.len == 0);
                if (shrunk_result) |_| {
                    shrunk_bytes.deinit();
                    shrunk_input.deinit();
                } else |shrunk_err| {
                    most_shrunk_bytes.deinit();
                    most_shrunk_input.deinit();
                    most_shrunk_bytes = shrunk_bytes;
                    most_shrunk_input = shrunk_input;
                    most_shrunk_value = shrunk_value;
                    most_shrunk_err = shrunk_err;
                }
            }

            warn("\nFound {} after {} shrinks:\n", .{ most_shrunk_err, shrink_iteration });
            dump(most_shrunk_value);
            most_shrunk_bytes.deinit();
            most_shrunk_input.deinit();
            return error.FuzzFailed;
        }
    }
}

test "fuzz parse" {
    try fuzz(std.heap.c_allocator, .{}, Utf8(0), fuzz_parse);
}
fn fuzz_parse(arena: *ArenaAllocator, source: []const u8) !void {
    var store = imp.lang.Store.init(arena);
    var error_info: ?imp.lang.pass.parse.ErrorInfo = null;
    // should never panic on any input
    _ = imp.lang.pass.parse.parse(&store, source, &error_info) catch |err| {
        switch (err) {
            error.ParseError => return,
            // shouldn't oom unless you're running this on a potato
            error.OutOfMemory => return err,
            // should only return utf8 errors if input is invalid utf8
            error.Utf8InvalidStartByte, error.InvalidUtf8, error.InvalidCharacter, error.Utf8ExpectedContinuation, error.Utf8OverlongEncoding, error.Utf8EncodesSurrogateHalf, error.Utf8CodepointTooLarge => {
                if (std.unicode.utf8ValidateSlice(source)) {
                    return err;
                }
            },
        }
    };
}

test "fuzz desugar" {
    try fuzz(std.heap.c_allocator, .{}, Syntax.PtrExpr, fuzz_desugar);
}
fn fuzz_desugar(arena: *ArenaAllocator, expr: *const imp.lang.repr.syntax.Expr) !void {
    var store = imp.lang.Store.init(arena);
    var error_info: ?imp.lang.pass.desugar.ErrorInfo = null;
    // should never panic on any input
    _ = imp.lang.pass.desugar.desugar(&store, expr, &error_info) catch |err| {
        switch (err) {
            error.DesugarError => {},
            // shouldn't oom unless you're running this on a potato
            error.OutOfMemory => return err,
        }
    };
}

test "fuzz analyze" {
    try fuzz(std.heap.c_allocator, .{}, Core.StoreAndValidExpr, fuzz_analyze);
}
fn fuzz_analyze(arena: *ArenaAllocator, store_and_expr: Core.StoreAndExpr) !void {
    var store = store_and_expr.store;
    const expr = store_and_expr.expr;
    var error_info: ?imp.lang.pass.analyze.ErrorInfo = null;
    // should never panic on any input
    _ = imp.lang.pass.analyze.analyze(&store, expr, &error_info) catch |err| {
        switch (err) {
            error.AnalyzeError => {},
            // shouldn't oom unless you're running this on a potato
            // TODO might be able to write programs that require exponential specialization
            error.OutOfMemory => return err,
        }
    };
}

test "fuzz analyze deterministic" {
    try fuzz(std.heap.c_allocator, .{}, Core.StoreAndValidExpr, fuzz_analyze_deterministic);
}
fn fuzz_analyze_deterministic(arena: *ArenaAllocator, store_and_expr: Core.StoreAndExpr) !void {
    var store = store_and_expr.store;
    const expr = store_and_expr.expr;
    var error_info: ?imp.lang.pass.analyze.ErrorInfo = null;
    const result1 = imp.lang.pass.analyze.analyze(&store, expr, &error_info);
    const result2 = imp.lang.pass.analyze.analyze(&store, expr, &error_info);
    // sanity check
    expect(meta.deepEqual(result1, result1));
    if (!meta.deepEqual(result1, result2)) {
        // dump(.{result1, result2});
        return error.Nondeterministic;
    }
}

test "fuzz interpret" {
    try fuzz(std.heap.c_allocator, .{}, Core.StoreAndValidExpr, fuzz_interpret);
}
fn fuzz_interpret(arena: *ArenaAllocator, store_and_expr: Core.StoreAndExpr) !void {
    var store = store_and_expr.store;
    const expr = store_and_expr.expr;
    var analyze_error_info: ?imp.lang.pass.analyze.ErrorInfo = null;
    const analyzed = imp.lang.pass.analyze.analyze(&store, expr, &analyze_error_info);
    var error_info: ?imp.lang.pass.interpret.ErrorInfo = null;
    // should never panic on any valid input
    if (imp.lang.pass.interpret.interpret(&store_and_expr.store, arena, expr, &error_info)) |set| {
        // if type is finite, should return finite set
        if (analyzed) |set_type| {
            if (set_type.isFinite() and set != .Finite) {
                return error.InterpretTooLazy;
            }
            // TODO check type agrees with value
        } else |_| {}
    } else |err| {
        switch (err) {
            error.InterpretError => {
                // should never return InterpretError on programs which typecheck
                if (analyzed) |set_type| {
                    dump(set_type);
                    return error.InvalidInterpretError;
                } else |_| {}
            },
            // easy to trigger eg divide by zero
            error.NativeError => {},
            // can easily oom with small programs
            error.OutOfMemory => {},
        }
    }
}

test "fuzz interpret deterministic" {
    try fuzz(std.heap.c_allocator, .{}, Core.StoreAndValidExpr, fuzz_interpret_deterministic);
}
fn fuzz_interpret_deterministic(arena: *ArenaAllocator, store_and_expr: Core.StoreAndExpr) !void {
    var store = store_and_expr.store;
    const expr = store_and_expr.expr;
    var error_info: ?imp.lang.pass.interpret.ErrorInfo = null;
    const result1 = imp.lang.pass.interpret.interpret(&store_and_expr.store, arena, expr, &error_info);
    const result2 = imp.lang.pass.interpret.interpret(&store_and_expr.store, arena, expr, &error_info);
    // sanity check
    expect(meta.deepEqual(result1, result1));
    if (!meta.deepEqual(result1, result2)) {
        // dump(.{result1, result2});
        return error.Nondeterministic;
    }
}

// TODO for some reason the fuzzer OOMs when using std.testing.allocator

// TODO need to track how often we're actually generating valid inputs

// TODO seems to be hard to shrink by emitting a tree node - maybe need a shrink option that replaces a range with an inner range?

// fn no_fo(arena: *ArenaAllocator, ) !void {
//     const string = Ascii(1);
//     if (std.mem.indexOf(u8, string, "fo")) |_| {
//         return error.Fail;
//     }
// }

// test "no_fo" {
//     try fuzz(std.heap.page_allocator, .{}, no_fo);
// }

// export fn LLVMFuzzerTestOneInput(data: [*c]u8, len: size_t) c_int {
//     const bytes = data[0..len];
//     var input = Input.init(std.heap.c_allocator, bytes);
//     no_fo(&input) catch |err| {
//         imp_panic("Test failed with {}", err); // TODO trace
//     };
// }