authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 21:13:08 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 21:13:08 -07:00
log48a7e1e1844a9be227073bba9ec7a3ed2a0ce360
tree5441b4abab406d5fbadf7db98a1e22500f2bb711
parent82c1bb5d1ec46745d0e1982b906b51f130d1f2e9

bring back fuzzing


3 files changed, 18 insertions(+), 17 deletions(-)

build.zig+6-11
...@@ -44,33 +44,28 @@ pub fn build(b: *std.Build) void {...@@ -44,33 +44,28 @@ pub fn build(b: *std.Build) void {
44}44}
4545
46fn addFuzzer(b: *std.Build, target: std.Build.ResolvedTarget, comptime name: []const u8, afl_clang_args: []const []const u8) *std.Build.Step.InstallFile {46fn addFuzzer(b: *std.Build, target: std.Build.ResolvedTarget, comptime name: []const u8, afl_clang_args: []const []const u8) *std.Build.Step.InstallFile {
47 // The library
48 const fuzz_lib = b.addStaticLibrary(.{47 const fuzz_lib = b.addStaticLibrary(.{
49 .name = "fuzz-" ++ name,48 .name = "fuzz-" ++ name ++ "-lib",
50 .root_source_file = b.path("fuzz/main.zig"),49 .root_source_file = b.path("fuzz/main.zig"),
51 .target = target,50 .target = target,
52 .optimize = .Debug,51 .optimize = .Debug,
53 });52 });
54 fuzz_lib.want_lto = true;53 fuzz_lib.want_lto = true;
55 fuzz_lib.bundle_compiler_rt = true;54 fuzz_lib.bundle_compiler_rt = true;
56 // Seems to be necessary for LLVM >= 1555 fuzz_lib.use_llvm = true;
56 fuzz_lib.use_lld = true;
57 fuzz_lib.root_module.pic = true;57 fuzz_lib.root_module.pic = true;
5858
59 deps.addAllTo(fuzz_lib);59 deps.addAllTo(fuzz_lib);
6060
61 // Setup the output name
62 const fuzz_executable_name = "fuzz-" ++ name;61 const fuzz_executable_name = "fuzz-" ++ name;
63 const fuzz_exe_path = std.fs.path.join(b.allocator, &.{ b.cache_root.path.?, fuzz_executable_name }) catch @panic("OOM");
6462
65 // We want `afl-clang-lto -o path/to/output path/to/library`63 const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o" });
66 const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o", fuzz_exe_path });64 const output_path = fuzz_compile.addOutputFileArg(fuzz_executable_name);
67 // Add the path to the library file to afl-clang-lto's args
68 fuzz_compile.addArtifactArg(fuzz_lib);65 fuzz_compile.addArtifactArg(fuzz_lib);
69 // Custom args
70 fuzz_compile.addArgs(afl_clang_args);66 fuzz_compile.addArgs(afl_clang_args);
7167
72 // Install the cached output to the install 'bin' path68 const fuzz_install = b.addInstallBinFile(output_path, fuzz_executable_name);
73 const fuzz_install = b.addInstallBinFile(.{ .cwd_relative = fuzz_exe_path }, fuzz_executable_name);
74 fuzz_install.step.dependOn(&fuzz_compile.step);69 fuzz_install.step.dependOn(&fuzz_compile.step);
7570
76 return fuzz_install;71 return fuzz_install;
fuzz/main.zig+3-6
...@@ -1,17 +1,14 @@...@@ -1,17 +1,14 @@
1const std = @import("std");1const std = @import("std");
2const json = @import("json");2const json = @import("json");
3const nfs = @import("nfs");
34
4pub export fn main() void {5pub export fn main() void {
5 // Setup an allocator that will detect leaks/use-after-free/etc
6 var gpa = std.heap.GeneralPurposeAllocator(.{}){};6 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
7 // this will check for leaks and crash the program if it finds any
8 defer std.debug.assert(gpa.deinit() == .ok);7 defer std.debug.assert(gpa.deinit() == .ok);
9 const allocator = gpa.allocator();8 const allocator = gpa.allocator();
109
11 // Read the data from stdin10 const stdin = nfs.stdin();
12 const stdin = std.io.getStdIn();
1311
14 // Try to parse the data12 var parsed = json.parse(allocator, "[stdin]", stdin, .{ .support_trailing_commas = true, .maximum_depth = 100 }) catch return;
15 var parsed = json.parse(allocator, "[stdin]", stdin.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 }) catch return;
16 defer parsed.deinit(allocator);13 defer parsed.deinit(allocator);
17}14}
shell.nix created+9
...@@ -0,0 +1,9 @@
1with import <nixpkgs> {};
2
3pkgs.mkShell {
4 nativeBuildInputs = with pkgs; [
5 aflplusplus
6 ];
7
8 hardeningDisable = [ "all" ];
9}