authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-05 07:23:18+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-05-05 07:23:18+02:00
logf0feda820e678a7a8f7c2716b515145f4b9a5303
tree4623cb6ce4c3f2933c0248d66879026a180ad74a
parentbb79c85cb7ad591e0d8d4fe94b3c32883173c5fa
parent0ba77eca7430ecb7c9852df175f3fc175bf50bbf
signaturebadge-check Signed by PGP key B5690EEEBB952194

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

add standalone test for libfuzzer initialization

5 files changed, 53 insertions(+), 1 deletions(-)

lib/std/os/linux.zig+1
...@@ -523,6 +523,7 @@ pub const getauxval = if (extern_getauxval) struct {...@@ -523,6 +523,7 @@ pub const getauxval = if (extern_getauxval) struct {
523}.getauxval else getauxvalImpl;523}.getauxval else getauxvalImpl;
524524
525fn getauxvalImpl(index: usize) callconv(.c) usize {525fn getauxvalImpl(index: usize) callconv(.c) usize {
526 @disableInstrumentation();
526 const auxv = elf_aux_maybe orelse return 0;527 const auxv = elf_aux_maybe orelse return 0;
527 var i: usize = 0;528 var i: usize = 0;
528 while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {529 while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
src/link/MachO.zig+1-1
...@@ -416,7 +416,7 @@ pub fn flushModule(...@@ -416,7 +416,7 @@ pub fn flushModule(
416 }416 }
417417
418 if (comp.config.any_fuzz) {418 if (comp.config.any_fuzz) {
419 try positionals.append(try link.openObjectInput(diags, comp.fuzzer_lib.?.full_object_path));419 try positionals.append(try link.openArchiveInput(diags, comp.fuzzer_lib.?.full_object_path, false, false));
420 }420 }
421421
422 if (comp.ubsan_rt_lib) |crt_file| {422 if (comp.ubsan_rt_lib) |crt_file| {
test/standalone/build.zig.zon+3
...@@ -108,6 +108,9 @@...@@ -108,6 +108,9 @@
108 .libcxx = .{108 .libcxx = .{
109 .path = "libcxx",109 .path = "libcxx",
110 },110 },
111 .libfuzzer = .{
112 .path = "libfuzzer",
113 },
111 .load_dynamic_library = .{114 .load_dynamic_library = .{
112 .path = "load_dynamic_library",115 .path = "load_dynamic_library",
113 },116 },
test/standalone/libfuzzer/build.zig created+26
...@@ -0,0 +1,26 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4pub fn build(b: *std.Build) void {
5 const target = b.standardTargetOptions(.{});
6 const optimize = b.standardOptimizeOption(.{});
7
8 if (builtin.os.tag == .windows) return; // TODO: libfuzzer support for windows
9
10 const run_step = b.step("run", "Run executables");
11 const exe = b.addExecutable(.{
12 .name = "main",
13 .root_module = b.createModule(.{
14 .root_source_file = b.path("main.zig"),
15 .target = target,
16 .optimize = optimize,
17 .fuzz = true,
18 }),
19 });
20
21 b.installArtifact(exe);
22 b.default_step = run_step;
23
24 const run_artifact = b.addRunArtifact(exe);
25 run_step.dependOn(&run_artifact.step);
26}
test/standalone/libfuzzer/main.zig created+22
...@@ -0,0 +1,22 @@
1const std = @import("std");
2
3const FuzzerSlice = extern struct {
4 ptr: [*]const u8,
5 len: usize,
6
7 fn fromSlice(s: []const u8) FuzzerSlice {
8 return .{ .ptr = s.ptr, .len = s.len };
9 }
10};
11
12extern fn fuzzer_set_name(name_ptr: [*]const u8, name_len: usize) void;
13extern fn fuzzer_init(cache_dir: FuzzerSlice) void;
14extern fn fuzzer_init_corpus_elem(input_ptr: [*]const u8, input_len: usize) void;
15extern fn fuzzer_coverage_id() u64;
16
17pub fn main() !void {
18 fuzzer_init(FuzzerSlice.fromSlice(""));
19 fuzzer_init_corpus_elem("hello".ptr, "hello".len);
20 fuzzer_set_name("test".ptr, "test".len);
21 _ = fuzzer_coverage_id();
22}