Skip to content

Commit afddfe2

Browse files
authored
Merge pull request #20773 from ziglang/fuzz
integrate fuzz testing into the build system
2 parents 1c35e73 + 688c2df commit afddfe2

15 files changed

+663
-76
lines changed

lib/compiler/build_runner.zig

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ const ArrayList = std.ArrayList;
99
const File = std.fs.File;
1010
const Step = std.Build.Step;
1111
const Watch = std.Build.Watch;
12+
const Fuzz = std.Build.Fuzz;
1213
const Allocator = std.mem.Allocator;
13-
const fatal = std.zig.fatal;
14+
const fatal = std.process.fatal;
15+
const runner = @This();
1416

1517
pub const root = @import("@build");
1618
pub const dependencies = @import("@dependencies");
@@ -102,6 +104,7 @@ pub fn main() !void {
102104
var steps_menu = false;
103105
var output_tmp_nonce: ?[16]u8 = null;
104106
var watch = false;
107+
var fuzz = false;
105108
var debounce_interval_ms: u16 = 50;
106109

107110
while (nextArg(args, &arg_idx)) |arg| {
@@ -205,6 +208,8 @@ pub fn main() !void {
205208
try debug_log_scopes.append(next_arg);
206209
} else if (mem.eql(u8, arg, "--debug-pkg-config")) {
207210
builder.debug_pkg_config = true;
211+
} else if (mem.eql(u8, arg, "--debug-rt")) {
212+
graph.debug_compiler_runtime_libs = true;
208213
} else if (mem.eql(u8, arg, "--debug-compile-errors")) {
209214
builder.debug_compile_errors = true;
210215
} else if (mem.eql(u8, arg, "--system")) {
@@ -234,6 +239,8 @@ pub fn main() !void {
234239
prominent_compile_errors = true;
235240
} else if (mem.eql(u8, arg, "--watch")) {
236241
watch = true;
242+
} else if (mem.eql(u8, arg, "--fuzz")) {
243+
fuzz = true;
237244
} else if (mem.eql(u8, arg, "-fincremental")) {
238245
graph.incremental = true;
239246
} else if (mem.eql(u8, arg, "-fno-incremental")) {
@@ -353,6 +360,7 @@ pub fn main() !void {
353360
.max_rss_mutex = .{},
354361
.skip_oom_steps = skip_oom_steps,
355362
.watch = watch,
363+
.fuzz = fuzz,
356364
.memory_blocked_steps = std.ArrayList(*Step).init(arena),
357365
.step_stack = .{},
358366
.prominent_compile_errors = prominent_compile_errors,
@@ -394,6 +402,10 @@ pub fn main() !void {
394402
},
395403
else => return err,
396404
};
405+
if (fuzz) {
406+
Fuzz.start(&run.thread_pool, run.step_stack.keys(), run.ttyconf, main_progress_node);
407+
}
408+
397409
if (!watch) return cleanExit();
398410

399411
switch (builtin.os.tag) {
@@ -457,6 +469,7 @@ const Run = struct {
457469
max_rss_mutex: std.Thread.Mutex,
458470
skip_oom_steps: bool,
459471
watch: bool,
472+
fuzz: bool,
460473
memory_blocked_steps: std.ArrayList(*Step),
461474
step_stack: std.AutoArrayHashMapUnmanaged(*Step, void),
462475
prominent_compile_errors: bool,
@@ -466,6 +479,11 @@ const Run = struct {
466479
summary: Summary,
467480
ttyconf: std.io.tty.Config,
468481
stderr: File,
482+
483+
fn cleanExit(run: Run) void {
484+
if (run.watch or run.fuzz) return;
485+
return runner.cleanExit();
486+
}
469487
};
470488

471489
fn prepare(
@@ -614,8 +632,7 @@ fn runStepNames(
614632
else => false,
615633
};
616634
if (failure_count == 0 and failures_only) {
617-
if (!run.watch) cleanExit();
618-
return;
635+
return run.cleanExit();
619636
}
620637

621638
const ttyconf = run.ttyconf;
@@ -672,8 +689,7 @@ fn runStepNames(
672689
}
673690

674691
if (failure_count == 0) {
675-
if (!run.watch) cleanExit();
676-
return;
692+
return run.cleanExit();
677693
}
678694

679695
// Finally, render compile errors at the bottom of the terminal.
@@ -1058,7 +1074,8 @@ fn workerMakeOneStep(
10581074
std.debug.lockStdErr();
10591075
defer std.debug.unlockStdErr();
10601076

1061-
printErrorMessages(b, s, run) catch {};
1077+
const gpa = b.allocator;
1078+
printErrorMessages(gpa, s, run.ttyconf, run.stderr, run.prominent_compile_errors) catch {};
10621079
}
10631080

10641081
handle_result: {
@@ -1111,11 +1128,13 @@ fn workerMakeOneStep(
11111128
}
11121129
}
11131130

1114-
fn printErrorMessages(b: *std.Build, failing_step: *Step, run: *const Run) !void {
1115-
const gpa = b.allocator;
1116-
const stderr = run.stderr;
1117-
const ttyconf = run.ttyconf;
1118-
1131+
pub fn printErrorMessages(
1132+
gpa: Allocator,
1133+
failing_step: *Step,
1134+
ttyconf: std.io.tty.Config,
1135+
stderr: File,
1136+
prominent_compile_errors: bool,
1137+
) !void {
11191138
// Provide context for where these error messages are coming from by
11201139
// printing the corresponding Step subtree.
11211140

@@ -1152,7 +1171,7 @@ fn printErrorMessages(b: *std.Build, failing_step: *Step, run: *const Run) !void
11521171
}
11531172
}
11541173

1155-
if (!run.prominent_compile_errors and failing_step.result_error_bundle.errorMessageCount() > 0)
1174+
if (!prominent_compile_errors and failing_step.result_error_bundle.errorMessageCount() > 0)
11561175
try failing_step.result_error_bundle.renderToWriter(renderOptions(ttyconf), stderr.writer());
11571176

11581177
for (failing_step.result_error_msgs.items) |msg| {
@@ -1226,6 +1245,7 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
12261245
\\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss
12271246
\\ --fetch Exit after fetching dependency tree
12281247
\\ --watch Continuously rebuild when source files are modified
1248+
\\ --fuzz Continuously search for unit test failures
12291249
\\ --debounce <ms> Delay before rebuilding after changed file detected
12301250
\\ -fincremental Enable incremental compilation
12311251
\\ -fno-incremental Disable incremental compilation
@@ -1294,6 +1314,7 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
12941314
\\ --seed [integer] For shuffling dependency traversal order (default: random)
12951315
\\ --debug-log [scope] Enable debugging the compiler
12961316
\\ --debug-pkg-config Fail if unknown pkg-config flags encountered
1317+
\\ --debug-rt Debug compiler runtime libraries
12971318
\\ --verbose-link Enable compiler debug output for linking
12981319
\\ --verbose-air Enable compiler debug output for Zig AIR
12991320
\\ --verbose-llvm-ir[=file] Enable compiler debug output for LLVM IR

0 commit comments

Comments
 (0)