diff --git a/pkg/afl++/build.zig b/pkg/afl++/build.zig index 5df2216da..c8b17254e 100644 --- a/pkg/afl++/build.zig +++ b/pkg/afl++/build.zig @@ -30,6 +30,30 @@ pub fn addInstrumentedExe( return fuzz_exe; } +/// Creates a run step that invokes `afl-fuzz` with the given instrumented +/// executable, input corpus directory, and output directory. +/// +/// Returns the `Run` step so callers can wire it into a build step. +pub fn addFuzzerRun( + b: *std.Build, + exe: std.Build.LazyPath, + corpus_dir: std.Build.LazyPath, + output_dir: std.Build.LazyPath, +) *std.Build.Step.Run { + const run = b.addSystemCommand(&.{ + b.findProgram(&.{"afl-fuzz"}, &.{}) catch + @panic("Could not find 'afl-fuzz', which is required to run"), + "-i", + }); + run.addDirectoryArg(corpus_dir); + run.addArgs(&.{"-o"}); + run.addDirectoryArg(output_dir); + run.addArgs(&.{"--"}); + run.addFileArg(exe); + run.addArgs(&.{"@@"}); + return run; +} + // Required so `zig build` works although it does nothing. pub fn build(b: *std.Build) !void { _ = b; diff --git a/test/fuzz-libghostty/build.zig b/test/fuzz-libghostty/build.zig index da6cd6cfb..cc1ec60ce 100644 --- a/test/fuzz-libghostty/build.zig +++ b/test/fuzz-libghostty/build.zig @@ -44,20 +44,7 @@ pub fn build(b: *std.Build) void { const exe = afl.addInstrumentedExe(b, lib); // Runner to simplify running afl-fuzz - const run = run: { - const run = b.addSystemCommand(&.{ - b.findProgram(&.{"afl-fuzz"}, &.{}) catch - @panic("Could not find 'afl-fuzz', which is required to run"), - "-i", - }); - run.addDirectoryArg(b.path("corpus/initial")); - run.addArgs(&.{"-o"}); - run.addDirectoryArg(b.path("afl-out")); - run.addArgs(&.{"--"}); - run.addFileArg(exe); - run.addArgs(&.{"@@"}); - break :run run; - }; + const run = afl.addFuzzerRun(b, exe, b.path("corpus/initial"), b.path("afl-out")); // Install b.installArtifact(lib);