@@ -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.
2224mkdir 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 --
2839module 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
70111func 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)
0 commit comments