Skip to content

Commit 8ed2ea8

Browse files
committed
Fix macOS AFL fuzz executable linking
1 parent e8338ea commit 8ed2ea8

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

build.zig

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
46384682
fn addMainExe(
46394683
b: *std.Build,
46404684
roc_modules: modules.RocModules,

test/fuzzing/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ generated program is deliberately exercising builtin behavior.
1414
Generated platform/app fuzzers must also generate app-provided names, platform
1515
aliases, and platform wrapper names. External ABI symbol strings and target names
1616
are platform data, not userspace Roc identifiers.
17+
18+
On macOS, AFL++ may fail with `shmget() failed` under the default System V
19+
shared-memory limits. Prefix AFL commands with `AFL_MAP_SIZE=2097152` unless the
20+
machine has already been configured with `afl-system-config`.

0 commit comments

Comments
 (0)