Skip to content

Commit f0feda8

Browse files
authored
Merge pull request ziglang#23727 from tjog/add-libfuzz-standalone-test
add standalone test for libfuzzer initialization
2 parents bb79c85 + 0ba77ec commit f0feda8

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

lib/std/os/linux.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ pub const getauxval = if (extern_getauxval) struct {
523523
}.getauxval else getauxvalImpl;
524524

525525
fn getauxvalImpl(index: usize) callconv(.c) usize {
526+
@disableInstrumentation();
526527
const auxv = elf_aux_maybe orelse return 0;
527528
var i: usize = 0;
528529
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {

src/link/MachO.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ pub fn flushModule(
416416
}
417417

418418
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));
420420
}
421421

422422
if (comp.ubsan_rt_lib) |crt_file| {

test/standalone/build.zig.zon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
.libcxx = .{
109109
.path = "libcxx",
110110
},
111+
.libfuzzer = .{
112+
.path = "libfuzzer",
113+
},
111114
.load_dynamic_library = .{
112115
.path = "load_dynamic_library",
113116
},
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
4+
pub 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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const std = @import("std");
2+
3+
const 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+
12+
extern fn fuzzer_set_name(name_ptr: [*]const u8, name_len: usize) void;
13+
extern fn fuzzer_init(cache_dir: FuzzerSlice) void;
14+
extern fn fuzzer_init_corpus_elem(input_ptr: [*]const u8, input_len: usize) void;
15+
extern fn fuzzer_coverage_id() u64;
16+
17+
pub 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+
}

0 commit comments

Comments
 (0)