authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-09-09 20:32:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-09-11 13:41:29-07:00
log9bc731b30a0be771a8128bab25d873f9212643a9
treebe83f471fe0569427e82478c23937c6b8a98970e
parent2b76221a468d1d4556b8f512a069b703f621cc2c

fuzzing: better std.testing.allocator lifetime management


1 files changed, 11 insertions(+), 9 deletions(-)

lib/compiler/test_runner.zig+11-9
......@@ -148,11 +148,7 @@ fn mainServer() !void {
148148 const test_fn = builtin.test_functions[index];
149149 const entry_addr = @intFromPtr(test_fn.func);
150150 try server.serveU64Message(.fuzz_start_addr, entry_addr);
151 const prev_allocator_state = testing.allocator_instance;
152 defer {
153 testing.allocator_instance = prev_allocator_state;
154 if (testing.allocator_instance.deinit() == .leak) std.process.exit(1);
155 }
151 defer if (testing.allocator_instance.deinit() == .leak) std.process.exit(1);
156152 is_fuzz_test = false;
157153 test_fn.func() catch |err| switch (err) {
158154 error.SkipZigTest => return,
......@@ -383,18 +379,24 @@ pub fn fuzz(
383379 testOne(input_ptr[0..input_len]) catch |err| switch (err) {
384380 error.SkipZigTest => return,
385381 else => {
386 if (@errorReturnTrace()) |trace| {
387 std.debug.dumpStackTrace(trace.*);
388 }
382 std.debug.lockStdErr();
383 if (@errorReturnTrace()) |trace| std.debug.dumpStackTrace(trace.*);
389384 std.debug.print("failed with error.{s}\n", .{@errorName(err)});
390385 std.process.exit(1);
391386 },
392387 };
393 if (log_err_count != 0) @panic("error logs detected");
388 if (log_err_count != 0) {
389 std.debug.lockStdErr();
390 std.debug.print("error logs detected\n", .{});
391 std.process.exit(1);
392 }
394393 }
395394 };
396395 if (builtin.fuzz) {
396 const prev_allocator_state = testing.allocator_instance;
397 testing.allocator_instance = .{};
397398 fuzzer_start(&global.fuzzer_one);
399 testing.allocator_instance = prev_allocator_state;
398400 return;
399401 }
400402