libfuzzer: add standalone test for libfuzzer initialization

This commit is contained in:
tjog 2025-05-03 17:15:59 +02:00
parent bf9b15ee67
commit 3ed159964a
No known key found for this signature in database
3 changed files with 51 additions and 0 deletions

View File

@ -108,6 +108,9 @@
.libcxx = .{
.path = "libcxx",
},
.libfuzzer = .{
.path = "libfuzzer",
},
.load_dynamic_library = .{
.path = "load_dynamic_library",
},

View File

@ -0,0 +1,26 @@
const std = @import("std");
const builtin = @import("builtin");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
if (builtin.os.tag == .windows) return; // TODO: libfuzzer support for windows
const run_step = b.step("run", "Run executables");
const exe = b.addExecutable(.{
.name = "main",
.root_module = b.createModule(.{
.root_source_file = b.path("main.zig"),
.target = target,
.optimize = optimize,
.fuzz = true,
}),
});
b.installArtifact(exe);
b.default_step = run_step;
const run_artifact = b.addRunArtifact(exe);
run_step.dependOn(&run_artifact.step);
}

View File

@ -0,0 +1,22 @@
const std = @import("std");
const FuzzerSlice = extern struct {
ptr: [*]const u8,
len: usize,
fn fromSlice(s: []const u8) FuzzerSlice {
return .{ .ptr = s.ptr, .len = s.len };
}
};
extern fn fuzzer_set_name(name_ptr: [*]const u8, name_len: usize) void;
extern fn fuzzer_init(cache_dir: FuzzerSlice) void;
extern fn fuzzer_init_corpus_elem(input_ptr: [*]const u8, input_len: usize) void;
extern fn fuzzer_coverage_id() u64;
pub fn main() !void {
fuzzer_init(FuzzerSlice.fromSlice(""));
fuzzer_init_corpus_elem("hello".ptr, "hello".len);
fuzzer_set_name("test".ptr, "test".len);
_ = fuzzer_coverage_id();
}