From 48a7e1e1844a9be227073bba9ec7a3ed2a0ce360 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 24 Aug 2025 21:13:08 -0700 Subject: [PATCH] bring back fuzzing --- build.zig | 17 ++++++----------- fuzz/main.zig | 9 +++------ shell.nix | 9 +++++++++ 3 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 shell.nix diff --git a/build.zig b/build.zig index 16c37cc0583b44946bd67513846c4c53a53d1ecc..b8ca82dd0a42e72d4cedfc462f0493b3b124c4a7 100644 --- a/build.zig +++ b/build.zig @@ -44,33 +44,28 @@ pub fn build(b: *std.Build) void { } fn addFuzzer(b: *std.Build, target: std.Build.ResolvedTarget, comptime name: []const u8, afl_clang_args: []const []const u8) *std.Build.Step.InstallFile { - // The library const fuzz_lib = b.addStaticLibrary(.{ - .name = "fuzz-" ++ name, + .name = "fuzz-" ++ name ++ "-lib", .root_source_file = b.path("fuzz/main.zig"), .target = target, .optimize = .Debug, }); fuzz_lib.want_lto = true; fuzz_lib.bundle_compiler_rt = true; - // Seems to be necessary for LLVM >= 15 + fuzz_lib.use_llvm = true; + fuzz_lib.use_lld = true; fuzz_lib.root_module.pic = true; deps.addAllTo(fuzz_lib); - // Setup the output name const fuzz_executable_name = "fuzz-" ++ name; - const fuzz_exe_path = std.fs.path.join(b.allocator, &.{ b.cache_root.path.?, fuzz_executable_name }) catch @panic("OOM"); - // We want `afl-clang-lto -o path/to/output path/to/library` - const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o", fuzz_exe_path }); - // Add the path to the library file to afl-clang-lto's args + const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o" }); + const output_path = fuzz_compile.addOutputFileArg(fuzz_executable_name); fuzz_compile.addArtifactArg(fuzz_lib); - // Custom args fuzz_compile.addArgs(afl_clang_args); - // Install the cached output to the install 'bin' path - const fuzz_install = b.addInstallBinFile(.{ .cwd_relative = fuzz_exe_path }, fuzz_executable_name); + const fuzz_install = b.addInstallBinFile(output_path, fuzz_executable_name); fuzz_install.step.dependOn(&fuzz_compile.step); return fuzz_install; diff --git a/fuzz/main.zig b/fuzz/main.zig index 524b7ece4915e910959a9106fc3f3daf786b0717..cff0280651585a5cde7d5943389265ec5beeee64 100644 --- a/fuzz/main.zig +++ b/fuzz/main.zig @@ -1,17 +1,14 @@ const std = @import("std"); const json = @import("json"); +const nfs = @import("nfs"); pub export fn main() void { - // Setup an allocator that will detect leaks/use-after-free/etc var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - // this will check for leaks and crash the program if it finds any defer std.debug.assert(gpa.deinit() == .ok); const allocator = gpa.allocator(); - // Read the data from stdin - const stdin = std.io.getStdIn(); + const stdin = nfs.stdin(); - // Try to parse the data - var parsed = json.parse(allocator, "[stdin]", stdin.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 }) catch return; + var parsed = json.parse(allocator, "[stdin]", stdin, .{ .support_trailing_commas = true, .maximum_depth = 100 }) catch return; defer parsed.deinit(allocator); } diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000000000000000000000000000000000000..31a182f82828bd1762e1b12eacaf09bb2fa4dfa9 --- /dev/null +++ b/shell.nix @@ -0,0 +1,9 @@ +with import {}; + +pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + aflplusplus + ]; + + hardeningDisable = [ "all" ]; +} -- 2.54.0