Merge pull request #23727 from tjog/add-libfuzz-standalone-test

add standalone test for libfuzzer initialization
This commit is contained in:
Alex Rønne Petersen 2025-05-05 07:23:18 +02:00 committed by GitHub
commit f0feda820e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 53 additions and 1 deletions

View file

@ -523,6 +523,7 @@ pub const getauxval = if (extern_getauxval) struct {
}.getauxval else getauxvalImpl;
fn getauxvalImpl(index: usize) callconv(.c) usize {
@disableInstrumentation();
const auxv = elf_aux_maybe orelse return 0;
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {

View file

@ -416,7 +416,7 @@ pub fn flushModule(
}
if (comp.config.any_fuzz) {
try positionals.append(try link.openObjectInput(diags, comp.fuzzer_lib.?.full_object_path));
try positionals.append(try link.openArchiveInput(diags, comp.fuzzer_lib.?.full_object_path, false, false));
}
if (comp.ubsan_rt_lib) |crt_file| {

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();
}