Skip to content

Commit 1ba7341

Browse files
committed
cmd/link, runtime: use a different section for Go libfuzzer counters
Currently in libfuzzer mode, we put our counters in section __sancov_cntrs. When linking with C/C++ code that also has fuzzer counters, apparently the C linker combines our counters and their counters and registers them together. But in the Go runtime we also have code to register our counters. So the Go counters ended up registered twice, causing problems. Since we already have code to register our counters, put them in a Go-specific section so it won't be combined with the C counters. Fixes #57449. Change-Id: If3d41735124e7e301572d4b7aecf7d057ac134c0 Reviewed-on: https://go-review.googlesource.com/c/go/+/459055 Reviewed-by: Nicolas Hillegeer <[email protected]> Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Cherry Mui <[email protected]>
1 parent c61d322 commit 1ba7341

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

src/cmd/link/internal/ld/data.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,9 +1860,9 @@ func (state *dodataState) allocateDataSections(ctxt *Link) {
18601860

18611861
// Coverage instrumentation counters for libfuzzer.
18621862
if len(state.data[sym.SLIBFUZZER_8BIT_COUNTER]) > 0 {
1863-
sect := state.allocateNamedSectionAndAssignSyms(&Segdata, "__sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, sym.Sxxx, 06)
1864-
ldr.SetSymSect(ldr.LookupOrCreateSym("__start___sancov_cntrs", 0), sect)
1865-
ldr.SetSymSect(ldr.LookupOrCreateSym("__stop___sancov_cntrs", 0), sect)
1863+
sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".go.fuzzcntrs", sym.SLIBFUZZER_8BIT_COUNTER, sym.Sxxx, 06)
1864+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.__start___sancov_cntrs", 0), sect)
1865+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.__stop___sancov_cntrs", 0), sect)
18661866
ldr.SetSymSect(ldr.LookupOrCreateSym("internal/fuzz._counters", 0), sect)
18671867
ldr.SetSymSect(ldr.LookupOrCreateSym("internal/fuzz._ecounters", 0), sect)
18681868
}
@@ -2643,7 +2643,7 @@ func (ctxt *Link) address() []*sym.Segment {
26432643
bss = s
26442644
case ".noptrbss":
26452645
noptrbss = s
2646-
case "__sancov_cntrs":
2646+
case ".go.fuzzcntrs":
26472647
fuzzCounters = s
26482648
}
26492649
}
@@ -2764,8 +2764,8 @@ func (ctxt *Link) address() []*sym.Segment {
27642764
ctxt.xdefine("runtime.end", sym.SBSS, int64(Segdata.Vaddr+Segdata.Length))
27652765

27662766
if fuzzCounters != nil {
2767-
ctxt.xdefine("__start___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr))
2768-
ctxt.xdefine("__stop___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length))
2767+
ctxt.xdefine("runtime.__start___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr))
2768+
ctxt.xdefine("runtime.__stop___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length))
27692769
ctxt.xdefine("internal/fuzz._counters", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr))
27702770
ctxt.xdefine("internal/fuzz._ecounters", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length))
27712771
}

src/cmd/link/internal/ld/elf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ func (ctxt *Link) doelf() {
13711371
shstrtab.Addstring(".data")
13721372
shstrtab.Addstring(".bss")
13731373
shstrtab.Addstring(".noptrbss")
1374-
shstrtab.Addstring("__sancov_cntrs")
1374+
shstrtab.Addstring(".go.fuzzcntrs")
13751375
shstrtab.Addstring(".go.buildinfo")
13761376
if ctxt.IsMIPS() {
13771377
shstrtab.Addstring(".MIPS.abiflags")

src/runtime/libfuzzer.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,8 @@ var __sanitizer_cov_trace_const_cmp8 byte
148148
//go:cgo_import_static __sanitizer_cov_8bit_counters_init
149149
var __sanitizer_cov_8bit_counters_init byte
150150

151-
//go:linkname __start___sancov_cntrs __start___sancov_cntrs
152-
//go:cgo_import_static __start___sancov_cntrs
153-
var __start___sancov_cntrs byte
154-
155-
//go:linkname __stop___sancov_cntrs __stop___sancov_cntrs
156-
//go:cgo_import_static __stop___sancov_cntrs
157-
var __stop___sancov_cntrs byte
151+
// start, stop markers of counters, set by the linker
152+
var __start___sancov_cntrs, __stop___sancov_cntrs byte
158153

159154
//go:linkname __sanitizer_cov_pcs_init __sanitizer_cov_pcs_init
160155
//go:cgo_import_static __sanitizer_cov_pcs_init

0 commit comments

Comments
 (0)