| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const deps = @import("./deps.zig"); |
| 4 | |
| 5 | pub fn build(b: *std.Build) void { |
| 6 | const target = b.standardTargetOptions(.{}); |
| 7 | const mode = b.option(std.builtin.OptimizeMode, "mode", "") orelse .Debug; |
| 8 | const disable_llvm = b.option(bool, "disable_llvm", "use the non-llvm zig codegen") orelse false; |
| 9 | |
| 10 | const test_exe = b.addTest(.{ |
| 11 | .root_module = b.createModule(.{ |
| 12 | .root_source_file = b.path("test.zig"), |
| 13 | .target = target, |
| 14 | .optimize = mode, |
| 15 | }), |
| 16 | }); |
| 17 | deps.addAllTo(test_exe); |
| 18 | test_exe.root_module.link_libc = true; |
| 19 | test_exe.use_llvm = !disable_llvm; |
| 20 | test_exe.use_lld = !disable_llvm; |
| 21 | b.getInstallStep().dependOn(&test_exe.step); |
| 22 | |
| 23 | const build_options = b.addOptions(); |
| 24 | build_options.addOption(string, "JSONTestSuite_root", deps.dirs._bebdygynna6k ++ "/test_parsing"); |
| 25 | test_exe.root_module.addImport("build_options", build_options.createModule()); |
| 26 | |
| 27 | const test_cmd = b.addRunArtifact(test_exe); |
| 28 | test_cmd.setCwd(b.path(".")); |
| 29 | test_cmd.has_side_effects = true; |
| 30 | test_cmd.step.dependOn(b.getInstallStep()); |
| 31 | |
| 32 | const test_step = b.step("test", "Run all library tests"); |
| 33 | test_step.dependOn(&test_cmd.step); |
| 34 | |
| 35 | // |
| 36 | |
| 37 | const fuzz_exe = addFuzzer(b, target, "json", &.{}); |
| 38 | b.getInstallStep().dependOn(fuzz_exe.step.dependencies.items[0]); |
| 39 | |
| 40 | const fuzz_run = b.addSystemCommand(&.{"afl-fuzz"}); |
| 41 | fuzz_run.step.dependOn(&fuzz_exe.step); |
| 42 | fuzz_run.addArgs(&.{ "-i", "fuzz/input" }); |
| 43 | fuzz_run.addArgs(&.{ "-o", "fuzz/output" }); |
| 44 | fuzz_run.addArgs(&.{ "-x", "fuzz/json.dict" }); |
| 45 | fuzz_run.addArg("--"); |
| 46 | fuzz_run.addFileArg(fuzz_exe.source); |
| 47 | |
| 48 | const fuzz_step = b.step("fuzz", "Run AFL++"); |
| 49 | fuzz_step.dependOn(&fuzz_run.step); |
| 50 | } |
| 51 | |
| 52 | fn addFuzzer(b: *std.Build, target: std.Build.ResolvedTarget, comptime name: []const u8, afl_clang_args: []const []const u8) *std.Build.Step.InstallFile { |
| 53 | const fuzz_lib = b.addLibrary(.{ |
| 54 | .linkage = .static, |
| 55 | .name = "fuzz-" ++ name ++ "-lib", |
| 56 | .root_module = b.createModule(.{ |
| 57 | .root_source_file = b.path("fuzz/main.zig"), |
| 58 | .target = target, |
| 59 | .optimize = .Debug, |
| 60 | }), |
| 61 | }); |
| 62 | fuzz_lib.lto = .full; |
| 63 | fuzz_lib.bundle_compiler_rt = true; |
| 64 | fuzz_lib.use_llvm = true; |
| 65 | fuzz_lib.use_lld = true; |
| 66 | fuzz_lib.root_module.pic = true; |
| 67 | |
| 68 | deps.addAllTo(fuzz_lib); |
| 69 | |
| 70 | const fuzz_executable_name = "fuzz-" ++ name; |
| 71 | |
| 72 | const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o" }); |
| 73 | const output_path = fuzz_compile.addOutputFileArg(fuzz_executable_name); |
| 74 | fuzz_compile.addArtifactArg(fuzz_lib); |
| 75 | fuzz_compile.addArgs(afl_clang_args); |
| 76 | |
| 77 | const fuzz_install = b.addInstallBinFile(output_path, fuzz_executable_name); |
| 78 | fuzz_install.step.dependOn(&fuzz_compile.step); |
| 79 | |
| 80 | return fuzz_install; |
| 81 | } |