]> Cypherpunks.ru repositories - gostls13.git/commitdiff
[dev.fuzz] internal/fuzz: don't count time spent loading corpus
authorJay Conrod <jayconrod@google.com>
Mon, 5 Apr 2021 15:40:12 +0000 (11:40 -0400)
committerJay Conrod <jayconrod@google.com>
Fri, 9 Apr 2021 17:53:57 +0000 (17:53 +0000)
The -fuzztime flag tells us how much time to spend fuzzing, not
counting time spent running the seed corpus. We shouldn't count time
spent loading the cache either. If the cache is large, the time limit
may be exceeded before the coordinator starts the workers.

Change-Id: If00435faa5d24aabdb9003ebb9337fa2e47f22b6
Reviewed-on: https://go-review.googlesource.com/c/go/+/307310
Trust: Jay Conrod <jayconrod@google.com>
Trust: Katie Hockman <katie@golang.org>
Run-TryBot: Jay Conrod <jayconrod@google.com>
Run-TryBot: Katie Hockman <katie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Katie Hockman <katie@golang.org>
src/internal/fuzz/fuzz.go
src/testing/internal/testdeps/deps.go

index 8e0425c0c425d81e12835dbf35b5665b12760964..293cb48d4db637748a00190ab11de79ec605ebd1 100644 (file)
@@ -18,6 +18,7 @@ import (
        "reflect"
        "runtime"
        "strings"
+       "time"
 )
 
 // CoordinateFuzzing creates several worker processes and communicates with
@@ -27,6 +28,9 @@ import (
 // with the same arguments as the coordinator, except with the -test.fuzzworker
 // flag prepended to the argument list.
 //
+// timeout is the amount of wall clock time to spend fuzzing after the corpus
+// has loaded.
+//
 // parallel is the number of worker processes to run in parallel. If parallel
 // is 0, CoordinateFuzzing will run GOMAXPROCS workers.
 //
@@ -43,7 +47,7 @@ import (
 //
 // If a crash occurs, the function will return an error containing information
 // about the crash, which can be reported to the user.
-func CoordinateFuzzing(ctx context.Context, parallel int, seed []CorpusEntry, types []reflect.Type, corpusDir, cacheDir string) (err error) {
+func CoordinateFuzzing(ctx context.Context, timeout time.Duration, parallel int, seed []CorpusEntry, types []reflect.Type, corpusDir, cacheDir string) (err error) {
        if err := ctx.Err(); err != nil {
                return err
        }
@@ -69,6 +73,12 @@ func CoordinateFuzzing(ctx context.Context, parallel int, seed []CorpusEntry, ty
                corpus.entries = append(corpus.entries, CorpusEntry{Data: marshalCorpusFile(vals...), Values: vals})
        }
 
+       if timeout > 0 {
+               var cancel func()
+               ctx, cancel = context.WithTimeout(ctx, timeout)
+               defer cancel()
+       }
+
        // TODO(jayconrod): do we want to support fuzzing different binaries?
        dir := "" // same as self
        binPath := os.Args[0]
index 8f587b2e1d366d2ecf5443f915d10b752b24d9e1..c77aca3da8e5b18c9155b151310b517071543184 100644 (file)
@@ -137,14 +137,9 @@ func (TestDeps) CoordinateFuzzing(timeout time.Duration, parallel int, seed []fu
        // Fuzzing may be interrupted with a timeout or if the user presses ^C.
        // In either case, we'll stop worker processes gracefully and save
        // crashers and interesting values.
-       ctx, cancel := context.WithCancel(context.Background())
-       if timeout > 0 {
-               ctx, cancel = context.WithTimeout(ctx, timeout)
-       }
-       ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
-       defer stop()
+       ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
        defer cancel()
-       err = fuzz.CoordinateFuzzing(ctx, parallel, seed, types, corpusDir, cacheDir)
+       err = fuzz.CoordinateFuzzing(ctx, timeout, parallel, seed, types, corpusDir, cacheDir)
        if err == ctx.Err() {
                return nil
        }
@@ -158,9 +153,7 @@ func (TestDeps) RunFuzzWorker(fn func(fuzz.CorpusEntry) error) error {
        // If the worker is interrupted, return quickly and without error.
        // If only the coordinator process is interrupted, it tells each worker
        // process to stop by closing its "fuzz_in" pipe.
-       ctx, cancel := context.WithCancel(context.Background())
-       ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
-       defer stop()
+       ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
        defer cancel()
        err := fuzz.RunFuzzWorker(ctx, fn)
        if err == ctx.Err() {