Skip to content

Commit 17f62c0

Browse files
committed
[dev.fuzz] internal/fuzz: fix bug for -fuzzminimizetime of zero
Updates #48321 Change-Id: Ib35388f17580f1244a6eae4e5879f8329b6b44ce Reviewed-on: https://go-review.googlesource.com/c/go/+/349090 Trust: Katie Hockman <[email protected]> Run-TryBot: Katie Hockman <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent d106089 commit 17f62c0

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

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

+30-12
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@
66
# We clean the fuzz cache during this test. Don't clean the user's cache.
77
env GOCACHE=$WORK/gocache
88

9-
# Test that fuzzminimizetime can be zero seconds
10-
! go test -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=10000x -fuzzminimizetime=0s minimizer_test.go
11-
! stdout '^ok'
12-
stdout 'contains a non-zero byte'
13-
stdout FAIL
14-
15-
# Test that fuzzminimizetime can be zero times
16-
! go test -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=10000x -fuzzminimizetime=0x minimizer_test.go
17-
! stdout '^ok'
18-
stdout 'contains a non-zero byte'
19-
stdout FAIL
20-
219
# Test that fuzzminimizetime cannot be negative seconds
2210
! go test -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=10000x -fuzzminimizetime=-1ms minimizer_test.go
2311
! stdout '^ok'
@@ -32,6 +20,20 @@ stdout FAIL
3220
stdout 'invalid count'
3321
stdout FAIL
3422

23+
# Test that fuzzminimizetime can be zero seconds, and minimization is disabled
24+
! go test -fuzz=FuzzMinimizeZeroDurationSet -run=FuzzMinimizeZeroDurationSet -fuzztime=10000x -fuzzminimizetime=0s minimizer_test.go
25+
! stdout '^ok'
26+
! stdout 'found a crash, minimizing...'
27+
stdout 'there was an Error'
28+
stdout FAIL
29+
30+
# Test that fuzzminimizetime can be zero times, and minimization is disabled
31+
! go test -fuzz=FuzzMinimizeZeroLimitSet -run=FuzzMinimizeZeroLimitSet -fuzztime=10000x -fuzzminimizetime=0x minimizer_test.go
32+
! stdout '^ok'
33+
! stdout 'found a crash, minimizing...'
34+
stdout 'there was an Error'
35+
stdout FAIL
36+
3537
# Test that minimization is working for recoverable errors.
3638
! go test -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=10000x minimizer_test.go
3739
! stdout '^ok'
@@ -87,6 +89,22 @@ import (
8789
"testing"
8890
)
8991

92+
func FuzzMinimizeZeroDurationSet(f *testing.F) {
93+
f.Fuzz(func(t *testing.T, b []byte) {
94+
if len(b) > 5 {
95+
t.Errorf("there was an Error")
96+
}
97+
})
98+
}
99+
100+
func FuzzMinimizeZeroLimitSet(f *testing.F) {
101+
f.Fuzz(func(t *testing.T, b []byte) {
102+
if len(b) > 5 {
103+
t.Errorf("there was an Error")
104+
}
105+
})
106+
}
107+
90108
func FuzzMinimizerRecoverable(f *testing.F) {
91109
f.Add(make([]byte, 100))
92110
f.Fuzz(func(t *testing.T, b []byte) {

src/internal/fuzz/fuzz.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ type CoordinateFuzzingOpts struct {
4040
Limit int64
4141

4242
// MinimizeTimeout is the amount of wall clock time to spend minimizing
43-
// after discovering a crasher. If zero, there will be no time limit.
43+
// after discovering a crasher. If zero, there will be no time limit. If
44+
// MinimizeTimeout and MinimizeLimit are both zero, then minimization will
45+
// be disabled.
4446
MinimizeTimeout time.Duration
4547

4648
// MinimizeLimit is the maximum number of calls to the fuzz function to be
47-
// made while minimizing after finding a crash. If zero, there will be
48-
// no limit. Calls to the fuzz function made when minimizing also count
49-
// toward Limit.
49+
// made while minimizing after finding a crash. If zero, there will be no
50+
// limit. Calls to the fuzz function made when minimizing also count toward
51+
// Limit. If MinimizeTimeout and MinimizeLimit are both zero, then
52+
// minimization will be disabled.
5053
MinimizeLimit int64
5154

5255
// parallel is the number of worker processes to run in parallel. If zero,
@@ -552,9 +555,10 @@ type coordinator struct {
552555
// generated values that workers reported as interesting.
553556
corpus corpus
554557

555-
// typesAreMinimizable is true if one or more of the types of fuzz function's
556-
// parameters can be minimized.
557-
typesAreMinimizable bool
558+
// minimizationAllowed is true if one or more of the types of fuzz
559+
// function's parameters can be minimized, and either the limit or duration
560+
// for minimization is non-zero.
561+
minimizationAllowed bool
558562

559563
// inputQueue is a queue of inputs that workers should try fuzzing. This is
560564
// initially populated from the seed corpus and cached inputs. More inputs
@@ -604,10 +608,12 @@ func newCoordinator(opts CoordinateFuzzingOpts) (*coordinator, error) {
604608
resultC: make(chan fuzzResult),
605609
corpus: corpus,
606610
}
607-
for _, t := range opts.Types {
608-
if isMinimizable(t) {
609-
c.typesAreMinimizable = true
610-
break
611+
if opts.MinimizeLimit > 0 || opts.MinimizeTimeout > 0 {
612+
for _, t := range opts.Types {
613+
if isMinimizable(t) {
614+
c.minimizationAllowed = true
615+
break
616+
}
611617
}
612618
}
613619

@@ -736,7 +742,7 @@ func (c *coordinator) queueForMinimization(result fuzzResult, keepCoverage []byt
736742
// peekMinimizeInput returns the next input that should be sent to workers for
737743
// minimization.
738744
func (c *coordinator) peekMinimizeInput() (fuzzMinimizeInput, bool) {
739-
if c.opts.Limit > 0 && c.count+c.countWaiting >= c.opts.Limit {
745+
if !c.canMinimize() {
740746
// Already making the maximum number of calls to the fuzz function.
741747
// Don't send more inputs right now.
742748
return fuzzMinimizeInput{}, false
@@ -810,7 +816,7 @@ func (c *coordinator) updateCoverage(newCoverage []byte) int {
810816
// canMinimize returns whether the coordinator should attempt to find smaller
811817
// inputs that reproduce a crash or new coverage.
812818
func (c *coordinator) canMinimize() bool {
813-
return c.typesAreMinimizable &&
819+
return c.minimizationAllowed &&
814820
(c.opts.Limit == 0 || c.count+c.countWaiting < c.opts.Limit)
815821
}
816822

0 commit comments

Comments
 (0)