@@ -4627,14 +4627,58 @@ fn add_fuzz_target(
46274627 const fuzz_step = b .step (b .fmt ("build-fuzz-{s}" , .{name }), b .fmt ("Build fuzz executable for {s}" , .{name }));
46284628 b .default_step .dependOn (fuzz_step );
46294629
4630- const afl = b .lazyImport (@This (), "afl_kit" ) orelse return ;
4631- const fuzz_exe = afl .addInstrumentedExe (b , target , .ReleaseSafe , &.{}, use_system_afl , fuzz_obj , &.{"-lm" }) orelse return ;
4630+ const fuzz_exe = if (target .result .os .tag == .macos )
4631+ addMacosAflFuzzExe (b , target , .ReleaseSafe , use_system_afl , fuzz_obj ) orelse return
4632+ else blk : {
4633+ const afl = b .lazyImport (@This (), "afl_kit" ) orelse return ;
4634+ break :blk afl .addInstrumentedExe (b , target , .ReleaseSafe , &.{}, use_system_afl , fuzz_obj , &.{"-lm" }) orelse return ;
4635+ };
46324636 const install_fuzz = b .addInstallBinFile (fuzz_exe , name_exe );
46334637 fuzz_step .dependOn (& install_fuzz .step );
46344638 b .getInstallStep ().dependOn (& install_fuzz .step );
46354639 }
46364640}
46374641
4642+ fn addMacosAflFuzzExe (
4643+ b : * std.Build ,
4644+ target : ResolvedTarget ,
4645+ optimize : OptimizeMode ,
4646+ use_system_afl : bool ,
4647+ fuzz_obj : * Step.Compile ,
4648+ ) ? std.Build.LazyPath {
4649+ if (! use_system_afl ) {
4650+ std .log .warn ("Vendored AFL++ does not currently support macOS fuzz executable linking; use system AFL++" , .{});
4651+ return null ;
4652+ }
4653+
4654+ const afl_kit = b .lazyDependency ("afl_kit" , .{}) orelse return null ;
4655+ const afl_cc = b .findProgram (&.{"afl-cc" }, &.{}) catch @panic ("Could not find 'afl-cc', which is required to build" );
4656+ const afl_bin_dir = std .fs .path .dirname (afl_cc ) orelse @panic ("Could not determine afl-cc directory" );
4657+ const afl_compiler_rt = std.Build.LazyPath { .cwd_relative = b .pathJoin (&.{ afl_bin_dir , ".." , "lib" , "afl" , "afl-compiler-rt.o" }) };
4658+
4659+ fuzz_obj .root_module .fuzz = true ;
4660+ fuzz_obj .root_module .link_libc = true ;
4661+ fuzz_obj .sanitize_coverage_trace_pc_guard = true ;
4662+
4663+ const exe = b .addExecutable (.{
4664+ .name = fuzz_obj .name ,
4665+ .root_module = b .createModule (.{
4666+ .target = target ,
4667+ .optimize = optimize ,
4668+ .link_libc = true ,
4669+ }),
4670+ });
4671+ configureBackend (exe , target );
4672+ exe .root_module .addCSourceFile (.{
4673+ .file = afl_kit .path ("afl.c" ),
4674+ .flags = &.{},
4675+ });
4676+ exe .root_module .addObject (fuzz_obj );
4677+ exe .root_module .addObjectFile (afl_compiler_rt );
4678+
4679+ return exe .getEmittedBin ();
4680+ }
4681+
46384682fn addMainExe (
46394683 b : * std.Build ,
46404684 roc_modules : modules.RocModules ,
0 commit comments