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 {...@@ -148,11 +148,7 @@ fn mainServer() !void {
148 const test_fn = builtin.test_functions[index];148 const test_fn = builtin.test_functions[index];
149 const entry_addr = @intFromPtr(test_fn.func);149 const entry_addr = @intFromPtr(test_fn.func);
150 try server.serveU64Message(.fuzz_start_addr, entry_addr);150 try server.serveU64Message(.fuzz_start_addr, entry_addr);
151 const prev_allocator_state = testing.allocator_instance;151 defer if (testing.allocator_instance.deinit() == .leak) std.process.exit(1);
152 defer {
153 testing.allocator_instance = prev_allocator_state;
154 if (testing.allocator_instance.deinit() == .leak) std.process.exit(1);
155 }
156 is_fuzz_test = false;152 is_fuzz_test = false;
157 test_fn.func() catch |err| switch (err) {153 test_fn.func() catch |err| switch (err) {
158 error.SkipZigTest => return,154 error.SkipZigTest => return,
...@@ -383,18 +379,24 @@ pub fn fuzz(...@@ -383,18 +379,24 @@ pub fn fuzz(
383 testOne(input_ptr[0..input_len]) catch |err| switch (err) {379 testOne(input_ptr[0..input_len]) catch |err| switch (err) {
384 error.SkipZigTest => return,380 error.SkipZigTest => return,
385 else => {381 else => {
386 if (@errorReturnTrace()) |trace| {382 std.debug.lockStdErr();
387 std.debug.dumpStackTrace(trace.*);383 if (@errorReturnTrace()) |trace| std.debug.dumpStackTrace(trace.*);
388 }
389 std.debug.print("failed with error.{s}\n", .{@errorName(err)});384 std.debug.print("failed with error.{s}\n", .{@errorName(err)});
390 std.process.exit(1);385 std.process.exit(1);
391 },386 },
392 };387 };
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 }
394 }393 }
395 };394 };
396 if (builtin.fuzz) {395 if (builtin.fuzz) {
396 const prev_allocator_state = testing.allocator_instance;
397 testing.allocator_instance = .{};
397 fuzzer_start(&global.fuzzer_one);398 fuzzer_start(&global.fuzzer_one);
399 testing.allocator_instance = prev_allocator_state;
398 return;400 return;
399 }401 }
400402