Skip to content

Commit 61890fb

Browse files
committed
internal/fuzz: fix -fuzzminimizetime with 'x' bug
Fixes golang#48928 Change-Id: I3825ec615ab5fc19389ef4c10ad1042005a3761c Reviewed-on: https://go-review.googlesource.com/c/go/+/355450 Trust: Katie Hockman <katie@golang.org> Run-TryBot: Katie Hockman <katie@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com> TryBot-Result: Go Bot <gobot@golang.org>
1 parent 3d051ba commit 61890fb

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/cmd/go/testdata/script/test_fuzz_fuzztime.txt

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@ exec ./fuzz.test$GOEXE -test.timeout=10ms -test.fuzz=FuzzFast -test.fuzztime=5s
1616
# Timeout should not cause inputs to be written as crashers.
1717
! exists testdata/fuzz
1818

19+
env GOCACHE=$WORK/tmp
20+
1921
# When we use fuzztime with an "x" suffix, it runs a specific number of times.
20-
# This fuzz function creates a file with a unique name ($pid.$count) on each run.
21-
# We count the files to find the number of runs.
22+
# This fuzz function creates a file with a unique name ($pid.$count) on each
23+
# run. We count the files to find the number of runs.
2224
mkdir count
23-
env GOCACHE=$WORK/tmp
24-
go test -fuzz=FuzzCount -fuzztime=1000x -fuzzminimizetime=1x
25-
go run check_file_count.go 1000
25+
go test -fuzz=FuzzTestCount -fuzztime=1000x -fuzzminimizetime=1x
26+
go run check_file_count.go count 1000
27+
28+
# When we use fuzzminimizetime with an "x" suffix, it runs a specific number of
29+
# times while minimizing. This fuzz function creates a file with a unique name
30+
# ($pid.$count) on each run once the first crash has been found. That means that
31+
# there should be one file for each execution of the fuzz function during
32+
# minimization, so we count these to determine how many times minimization was
33+
# run.
34+
mkdir minimizecount
35+
! go test -fuzz=FuzzMinimizeCount -fuzzminimizetime=3x -parallel=1
36+
go run check_file_count.go minimizecount 3
2637

2738
-- go.mod --
2839
module fuzz
@@ -45,7 +56,7 @@ import (
4556
"testing"
4657
)
4758

48-
func FuzzCount(f *testing.F) {
59+
func FuzzTestCount(f *testing.F) {
4960
pid := os.Getpid()
5061
n := 0
5162
f.Fuzz(func(t *testing.T, _ []byte) {
@@ -56,6 +67,36 @@ func FuzzCount(f *testing.F) {
5667
n++
5768
})
5869
}
70+
-- fuzz_minimize_count_test.go --
71+
package fuzz
72+
73+
import (
74+
"bytes"
75+
"fmt"
76+
"os"
77+
"testing"
78+
)
79+
80+
func FuzzMinimizeCount(f *testing.F) {
81+
pid := os.Getpid()
82+
n := 0
83+
seed := bytes.Repeat([]byte("a"), 357)
84+
f.Add(seed)
85+
crashFound := false
86+
f.Fuzz(func(t *testing.T, b []byte) {
87+
if crashFound {
88+
name := fmt.Sprintf("minimizecount/%v.%d", pid, n)
89+
if err := os.WriteFile(name, nil, 0666); err != nil {
90+
t.Fatal(err)
91+
}
92+
n++
93+
}
94+
if !bytes.Equal(b, seed) { // this should happen right away
95+
crashFound = true
96+
t.Error("minimize this!")
97+
}
98+
})
99+
}
59100
-- check_file_count.go --
60101
// +build ignore
61102

@@ -68,13 +109,13 @@ import (
68109
)
69110

70111
func main() {
71-
dir, err := os.ReadDir("count")
112+
dir, err := os.ReadDir(os.Args[1])
72113
if err != nil {
73114
fmt.Fprintln(os.Stderr, err)
74115
os.Exit(1)
75116
}
76117
got := len(dir)
77-
want, _ := strconv.Atoi(os.Args[1])
118+
want, _ := strconv.Atoi(os.Args[2])
78119
if got != want {
79120
fmt.Fprintf(os.Stderr, "got %d files; want %d\n", got, want)
80121
os.Exit(1)

src/internal/fuzz/fuzz.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,11 @@ func (c *coordinator) peekMinimizeInput() (fuzzMinimizeInput, bool) {
825825
}
826826
}
827827
}
828-
remaining := c.opts.Limit - c.count - c.countWaiting
829-
if input.limit > remaining {
830-
input.limit = remaining
828+
if c.opts.Limit > 0 {
829+
remaining := c.opts.Limit - c.count - c.countWaiting
830+
if input.limit > remaining {
831+
input.limit = remaining
832+
}
831833
}
832834
return input, true
833835
}

0 commit comments

Comments
 (0)