forked from kristoff-it/zig-afl-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
91 lines (79 loc) · 3.01 KB
/
build.zig
File metadata and controls
91 lines (79 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const std = @import("std");
pub fn addInstrumentedExe(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
/// Pass null if llvm-config is in PATH
llvm_config_path: ?[]const []const u8,
/// If true will search the path for afl++ instead of compiling from source.
/// This is a workaround for issues with zig compiled afl++ and C++11 abi on ubuntu.
use_system_afl: bool,
obj: *std.Build.Step.Compile,
) ?std.Build.LazyPath {
const afl_kit = b.dependencyFromBuildZig(@This(), .{});
// TODO: validate obj
// std.debug.assert(obj.root_module.stack_check == false); // not linking with compiler-rt
// std.debug.assert(obj.root_module.link_libc == true); // afl runtime depends on libc
if (false) {
const exe = b.addExecutable(.{
.name = obj.name,
.target = target,
.optimize = optimize,
});
// exe.root_module.fuzz = false;
exe.root_module.link_libc = true;
exe.addCSourceFile(.{
.file = afl_kit.path("afl.c"),
.flags = &.{},
});
obj.root_module.fuzz = true;
obj.root_module.link_libc = true;
obj.sanitize_coverage_trace_pc_guard = true;
exe.addObject(obj);
// exe.addObject(afl_kit.path("afl-compiler-rt.o"));
exe.addCSourceFile(.{
.file = afl_kit.path("afl-compiler-rt.o"),
.flags = &.{},
});
return exe;
}
var run_afl_cc: *std.Build.Step.Run = undefined;
if (!use_system_afl) {
const afl = afl_kit.builder.lazyDependency("AFLplusplus", .{
.target = target,
.optimize = optimize,
.@"llvm-config-path" = llvm_config_path orelse &[_][]const u8{},
}) orelse return null;
const install_tools = b.addInstallDirectory(.{
.source_dir = std.Build.LazyPath{
.cwd_relative = afl.builder.install_path,
},
.install_dir = .prefix,
.install_subdir = "AFLplusplus",
});
install_tools.step.dependOn(afl.builder.getInstallStep());
run_afl_cc = b.addSystemCommand(&.{
b.pathJoin(&.{ afl.builder.exe_dir, "afl-cc" }),
"-O3",
"-o",
});
run_afl_cc.step.dependOn(&afl.builder.top_level_steps.get("llvm_exes").?.step);
run_afl_cc.step.dependOn(&install_tools.step);
} else {
run_afl_cc = b.addSystemCommand(&.{
b.findProgram(&.{"afl-cc"}, &.{}) catch @panic("Could not find 'afl-cc', which is required to build"),
"-O3",
"-o",
});
}
_ = obj.getEmittedBin(); // hack around build system bug
const fuzz_exe = run_afl_cc.addOutputFileArg(obj.name);
run_afl_cc.addFileArg(afl_kit.path("afl.c"));
run_afl_cc.addFileArg(obj.getEmittedLlvmBc());
return fuzz_exe;
}
pub fn build(b: *std.Build) !void {
_ = b;
// const target = b.standardTargetOptions(.{});
// const optimize = b.standardOptimizeOption(.{});
}