Skip to content

Commit 84ece56

Browse files
xxxbxxxalexrp
authored andcommitted
fix -fsanitize-coverage-trace-pc-guard and fuzzer support for C compile units
- allow `-fsanitize-coverage-trace-pc-guard` to be used on its own without enabling the fuzzer. (note that previouly, while the flag was only active when fuzzing, the fuzzer itself doesn't use it, and the code will not link as is.) - add stub functions in the fuzzer to link with instrumented C code (previously fuzzed tests failed to link if they were calling into C): while the zig compile unit uses a custom `EmitOptions.Coverage` with features disabled, the C code is built calling into the clang driver with "-fsanitize=fuzzer-no-link" that automatically enables the default features. (see https://github.com/llvm/llvm-project/blob/de06978ebcff5f75913067b019d2d522d0be0872/clang/lib/Driver/SanitizerArgs.cpp#L587) - emit `-fsanitize-coverage=trace-pc-guard` instead of `-Xclang -fsanitize-coverage-trace-pc-guard` so that edge coverrage is enabled by clang driver. (previously, it was enabled only because the fuzzer was)
1 parent aa5c6c0 commit 84ece56

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

lib/fuzzer.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ export fn __sanitizer_cov_trace_pc_indir(callee: usize) void {
8383
//fuzzer.traceValue(pc ^ callee);
8484
//std.log.debug("0x{x}: indirect call to 0x{x}", .{ pc, callee });
8585
}
86+
export fn __sanitizer_cov_8bit_counters_init(start: usize, end: usize) void {
87+
// clang will emit a call to this function when compiling with code coverage instrumentation.
88+
// however fuzzer_init() does not need this information, since it directly reads from the symbol table.
89+
_ = start;
90+
_ = end;
91+
}
92+
export fn __sanitizer_cov_pcs_init(start: usize, end: usize) void {
93+
// clang will emit a call to this function when compiling with code coverage instrumentation.
94+
// however fuzzer_init() does not need this information, since it directly reads from the symbol table.
95+
_ = start;
96+
_ = end;
97+
}
8698

8799
fn handleCmp(pc: usize, arg1: u64, arg2: u64) void {
88100
fuzzer.traceValue(pc ^ arg1 ^ arg2);

src/Compilation.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5922,10 +5922,10 @@ pub fn addCCArgs(
59225922
// function was called.
59235923
try argv.append("-fno-sanitize=function");
59245924
}
5925+
}
59255926

5926-
if (comp.config.san_cov_trace_pc_guard) {
5927-
try argv.appendSlice(&.{ "-Xclang", "-fsanitize-coverage-trace-pc-guard" });
5928-
}
5927+
if (comp.config.san_cov_trace_pc_guard) {
5928+
try argv.append("-fsanitize-coverage=trace-pc-guard");
59295929
}
59305930
}
59315931

src/codegen/llvm.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,22 +1333,24 @@ pub const Object = struct {
13331333
.is_small = options.is_small,
13341334
.time_report = options.time_report,
13351335
.tsan = options.sanitize_thread,
1336-
.sancov = options.fuzz,
13371336
.lto = options.lto != .none,
13381337
// https://github.com/ziglang/zig/issues/21215
13391338
.allow_fast_isel = !comp.root_mod.resolved_target.result.cpu.arch.isMIPS(),
13401339
.asm_filename = null,
13411340
.bin_filename = options.bin_path,
13421341
.llvm_ir_filename = options.post_ir_path,
13431342
.bitcode_filename = null,
1343+
1344+
// `.coverage` value is only used when `.sancov` is enabled.
1345+
.sancov = options.fuzz or comp.config.san_cov_trace_pc_guard,
13441346
.coverage = .{
13451347
.CoverageType = .Edge,
13461348
// Works in tandem with Inline8bitCounters or InlineBoolFlag.
13471349
// Zig does not yet implement its own version of this but it
13481350
// needs to for better fuzzing logic.
13491351
.IndirectCalls = false,
13501352
.TraceBB = false,
1351-
.TraceCmp = true,
1353+
.TraceCmp = options.fuzz,
13521354
.TraceDiv = false,
13531355
.TraceGep = false,
13541356
.Use8bitCounters = false,

0 commit comments

Comments
 (0)