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 {
4444}
4545
4646fn 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
4847 const fuzz_lib = b.addStaticLibrary(.{
49 .name = "fuzz-" ++ name,
48 .name = "fuzz-" ++ name ++ "-lib",
5049 .root_source_file = b.path("fuzz/main.zig"),
5150 .target = target,
5251 .optimize = .Debug,
5352 });
5453 fuzz_lib.want_lto = true;
5554 fuzz_lib.bundle_compiler_rt = true;
56 // Seems to be necessary for LLVM >= 15
55 fuzz_lib.use_llvm = true;
56 fuzz_lib.use_lld = true;
5757 fuzz_lib.root_module.pic = true;
5858
5959 deps.addAllTo(fuzz_lib);
6060
61 // Setup the output name
6261 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`
66 const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o", fuzz_exe_path });
67 // Add the path to the library file to afl-clang-lto's args
63 const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o" });
64 const output_path = fuzz_compile.addOutputFileArg(fuzz_executable_name);
6865 fuzz_compile.addArtifactArg(fuzz_lib);
69 // Custom args
7066 fuzz_compile.addArgs(afl_clang_args);
7167
72 // Install the cached output to the install 'bin' path
73 const fuzz_install = b.addInstallBinFile(.{ .cwd_relative = fuzz_exe_path }, fuzz_executable_name);
68 const fuzz_install = b.addInstallBinFile(output_path, fuzz_executable_name);
7469 fuzz_install.step.dependOn(&fuzz_compile.step);
7570
7671 return fuzz_install;
fuzz/main.zig+3-6
......@@ -1,17 +1,14 @@
11const std = @import("std");
22const json = @import("json");
3const nfs = @import("nfs");
34
45pub export fn main() void {
5 // Setup an allocator that will detect leaks/use-after-free/etc
66 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
7 // this will check for leaks and crash the program if it finds any
87 defer std.debug.assert(gpa.deinit() == .ok);
98 const allocator = gpa.allocator();
109
11 // Read the data from stdin
12 const stdin = std.io.getStdIn();
10 const stdin = nfs.stdin();
1311
14 // Try to parse the data
15 var parsed = json.parse(allocator, "[stdin]", stdin.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 }) catch return;
12 var parsed = json.parse(allocator, "[stdin]", stdin, .{ .support_trailing_commas = true, .maximum_depth = 100 }) catch return;
1613 defer parsed.deinit(allocator);
1714}
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}