Skip to content

Commit aecf4b1

Browse files
committed
internal/fuzz: log average execs/sec since last log
This change also fixes a bug with calculating the total interesting count. When fuzzing with an empty corpus, the fuzzing engine adds an starting corpus value in that run in order to start fuzzing. That meant that the interesting total count was off by one: it would start at 1, even though the cache was empty. Added some tests for this as well. Fixes #48787 Change-Id: I47acf96f0a0797214ebb24a95366d8460bf303bc Reviewed-on: https://go-review.googlesource.com/c/go/+/354150 Trust: Katie Hockman <katie@golang.org> Run-TryBot: Katie Hockman <katie@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org>
1 parent 4d8db00 commit aecf4b1

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ stdout ok
184184
! stdout ^ok
185185
stdout FAIL
186186

187+
188+
# Fuzzing indicates that one new interesting value was found with an empty
189+
# corpus, and the total size of the cache is now 1.
190+
go clean -fuzzcache
191+
go test -fuzz=Fuzz -fuzztime=10000x success_fuzz_test.go
192+
stdout 'new interesting: 1'
193+
stdout 'total: 1'
194+
195+
# Fuzzing again with a small fuzztime does not find any other interesting
196+
# values but still indicates that the cache size is 1.
197+
go test -fuzz=Fuzz -fuzztime=2x success_fuzz_test.go
198+
stdout 'new interesting: 0'
199+
stdout 'total: 1'
200+
187201
-- noop_fuzz_test.go --
188202
package noop_fuzz
189203

src/internal/fuzz/fuzz.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,13 @@ type coordinator struct {
556556
// count is the number of values fuzzed so far.
557557
count int64
558558

559+
// countLastLog is the number of values fuzzed when the output was last
560+
// logged.
561+
countLastLog int64
562+
563+
// timeLastLog is the time at which the output was last logged.
564+
timeLastLog time.Time
565+
559566
// interestingCount is the number of unique interesting values which have
560567
// been found this execution.
561568
interestingCount int64
@@ -618,12 +625,13 @@ func newCoordinator(opts CoordinateFuzzingOpts) (*coordinator, error) {
618625
return nil, err
619626
}
620627
c := &coordinator{
621-
opts: opts,
622-
startTime: time.Now(),
623-
inputC: make(chan fuzzInput),
624-
minimizeC: make(chan fuzzMinimizeInput),
625-
resultC: make(chan fuzzResult),
626-
corpus: corpus,
628+
opts: opts,
629+
startTime: time.Now(),
630+
inputC: make(chan fuzzInput),
631+
minimizeC: make(chan fuzzMinimizeInput),
632+
resultC: make(chan fuzzResult),
633+
corpus: corpus,
634+
timeLastLog: time.Now(),
627635
}
628636
if opts.MinimizeLimit > 0 || opts.MinimizeTimeout > 0 {
629637
for _, t := range opts.Types {
@@ -676,6 +684,7 @@ func (c *coordinator) updateStats(result fuzzResult) {
676684
}
677685

678686
func (c *coordinator) logStats() {
687+
now := time.Now()
679688
if c.warmupRun() {
680689
runSoFar := c.warmupInputCount - c.warmupInputLeft
681690
if coverageEnabled {
@@ -684,14 +693,16 @@ func (c *coordinator) logStats() {
684693
fmt.Fprintf(c.opts.Log, "fuzz: elapsed: %s, testing seed corpus: %d/%d completed\n", c.elapsed(), runSoFar, c.warmupInputCount)
685694
}
686695
} else {
687-
rate := float64(c.count) / time.Since(c.startTime).Seconds() // be more precise here
696+
rate := float64(c.count-c.countLastLog) / now.Sub(c.timeLastLog).Seconds()
688697
if coverageEnabled {
689-
interestingTotalCount := len(c.corpus.entries) - len(c.opts.Seed)
698+
interestingTotalCount := int64(c.warmupInputCount-len(c.opts.Seed)) + c.interestingCount
690699
fmt.Fprintf(c.opts.Log, "fuzz: elapsed: %s, execs: %d (%.0f/sec), new interesting: %d (total: %d)\n", c.elapsed(), c.count, rate, c.interestingCount, interestingTotalCount)
691700
} else {
692701
fmt.Fprintf(c.opts.Log, "fuzz: elapsed: %s, execs: %d (%.0f/sec)", c.elapsed(), c.count, rate)
693702
}
694703
}
704+
c.countLastLog = c.count
705+
c.timeLastLog = now
695706
}
696707

697708
// peekInput returns the next value that should be sent to workers.

0 commit comments

Comments
 (0)