Skip to content

Commit 1f411e9

Browse files
testing: only snapshot coverage during fuzzing
Only snapshot/reset coverage counters when we are actually fuzzing. This prevents a race when running corpus/seed values during the testing phase. Fixes #50488 Change-Id: I7dd5a0353a296c0b13eede29ad9af7c78814fa2d Reviewed-on: https://go-review.googlesource.com/c/go/+/376554 Trust: Katie Hockman <[email protected]> Reviewed-by: Katie Hockman <[email protected]> Trust: Roland Shoemaker <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 8b9b365 commit 1f411e9

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Test that when both race detection and coverage instrumentation are enabled,
2+
# and seed values are being executed, the race detector isn't mistakenly
3+
# triggered.
4+
5+
[short] skip
6+
[!fuzz] skip
7+
8+
# Test with coverage instrumentation enbaled (-fuzz) and race instrumentation
9+
# but without actually fuzzing the target (by using a non-matching pattern)
10+
go test -fuzz=xxx -race -v
11+
! stderr 'race detected during execution of test'
12+
13+
# Test with just race instrumentation enabled
14+
go test -race -v
15+
! stderr 'race detected during execution of test'
16+
17+
# Test with coverage and race instrumentation enabled, and a matching fuzz
18+
# pattern
19+
go test -fuzz=FuzzRace -race -v -fuzztime=200x
20+
! stderr 'race detected during execution of test'
21+
22+
-- go.mod --
23+
module test
24+
25+
-- race_test.go --
26+
package race
27+
28+
import "testing"
29+
30+
func FuzzRace(f *testing.F) {
31+
for i := 0; i < 100; i++ {
32+
f.Add(i)
33+
}
34+
35+
f.Fuzz(func(t *testing.T, i int) {
36+
t.Parallel()
37+
})
38+
}

src/testing/fuzz.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,10 @@ func (f *F) Fuzz(ff any) {
327327
// we make sure it is called right before the tRunner function
328328
// exits, regardless of whether it was executed cleanly, panicked,
329329
// or if the fuzzFn called t.Fatal.
330-
defer f.fuzzContext.deps.SnapshotCoverage()
331-
f.fuzzContext.deps.ResetCoverage()
330+
if f.testContext.isFuzzing {
331+
defer f.fuzzContext.deps.SnapshotCoverage()
332+
f.fuzzContext.deps.ResetCoverage()
333+
}
332334
fn.Call(args)
333335
})
334336
<-t.signal

0 commit comments

Comments
 (0)