|
| 1 | +// Copyright 2020 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package fuzz provides common fuzzing functionality for tests built with |
| 6 | +// "go test" and for programs that use fuzzing functionality in the testing |
| 7 | +// package. |
| 8 | +package fuzz |
| 9 | + |
| 10 | +import ( |
| 11 | + "os" |
| 12 | + "runtime" |
| 13 | + "sync" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +// CoordinateFuzzing creates several worker processes and communicates with |
| 18 | +// them to test random inputs that could trigger crashes and expose bugs. |
| 19 | +// The worker processes run the same binary in the same directory with the |
| 20 | +// same environment variables as the coordinator process. Workers also run |
| 21 | +// with the same arguments as the coordinator, except with the -test.fuzzworker |
| 22 | +// flag prepended to the argument list. |
| 23 | +// |
| 24 | +// parallel is the number of worker processes to run in parallel. If parallel |
| 25 | +// is 0, CoordinateFuzzing will run GOMAXPROCS workers. |
| 26 | +// |
| 27 | +// seed is a list of seed values added by the fuzz target with testing.F.Add. |
| 28 | +// Seed values from testdata and GOFUZZCACHE should not be included in this |
| 29 | +// list; this function loads them separately. |
| 30 | +func CoordinateFuzzing(parallel int, seed [][]byte) error { |
| 31 | + if parallel == 0 { |
| 32 | + parallel = runtime.GOMAXPROCS(0) |
| 33 | + } |
| 34 | + // TODO(jayconrod): support fuzzing indefinitely or with a given duration. |
| 35 | + // The value below is just a placeholder until we figure out how to handle |
| 36 | + // interrupts. |
| 37 | + duration := 5 * time.Second |
| 38 | + |
| 39 | + // TODO(jayconrod): do we want to support fuzzing different binaries? |
| 40 | + dir := "" // same as self |
| 41 | + binPath := os.Args[0] |
| 42 | + args := append([]string{"-test.fuzzworker"}, os.Args[1:]...) |
| 43 | + env := os.Environ() // same as self |
| 44 | + |
| 45 | + c := &coordinator{ |
| 46 | + doneC: make(chan struct{}), |
| 47 | + inputC: make(chan corpusEntry), |
| 48 | + } |
| 49 | + |
| 50 | + newWorker := func() *worker { |
| 51 | + return &worker{ |
| 52 | + dir: dir, |
| 53 | + binPath: binPath, |
| 54 | + args: args, |
| 55 | + env: env, |
| 56 | + coordinator: c, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + corpus := corpus{entries: make([]corpusEntry, len(seed))} |
| 61 | + for i, v := range seed { |
| 62 | + corpus.entries[i].b = v |
| 63 | + } |
| 64 | + if len(corpus.entries) == 0 { |
| 65 | + // TODO(jayconrod,katiehockman): pick a good starting corpus when one is |
| 66 | + // missing or very small. |
| 67 | + corpus.entries = append(corpus.entries, corpusEntry{b: []byte{0}}) |
| 68 | + } |
| 69 | + |
| 70 | + // TODO(jayconrod,katiehockman): read corpus from testdata. |
| 71 | + // TODO(jayconrod,katiehockman): read corpus from GOFUZZCACHE. |
| 72 | + |
| 73 | + // Start workers. |
| 74 | + workers := make([]*worker, parallel) |
| 75 | + runErrs := make([]error, parallel) |
| 76 | + var wg sync.WaitGroup |
| 77 | + wg.Add(parallel) |
| 78 | + for i := 0; i < parallel; i++ { |
| 79 | + go func(i int) { |
| 80 | + defer wg.Done() |
| 81 | + workers[i] = newWorker() |
| 82 | + runErrs[i] = workers[i].runFuzzing() |
| 83 | + }(i) |
| 84 | + } |
| 85 | + |
| 86 | + // Main event loop. |
| 87 | + stopC := time.After(duration) |
| 88 | + i := 0 |
| 89 | + for { |
| 90 | + select { |
| 91 | + // TODO(jayconrod): handle interruptions like SIGINT. |
| 92 | + // TODO(jayconrod,katiehockman): receive crashers and new corpus values |
| 93 | + // from workers. |
| 94 | + |
| 95 | + case <-stopC: |
| 96 | + // Time's up. |
| 97 | + close(c.doneC) |
| 98 | + |
| 99 | + case <-c.doneC: |
| 100 | + // Wait for workers to stop and return. |
| 101 | + wg.Wait() |
| 102 | + for _, err := range runErrs { |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + } |
| 107 | + return nil |
| 108 | + |
| 109 | + case c.inputC <- corpus.entries[i]: |
| 110 | + // Sent the next input to any worker. |
| 111 | + // TODO(jayconrod,katiehockman): need a scheduling algorithm that chooses |
| 112 | + // which corpus value to send next (or generates something new). |
| 113 | + i = (i + 1) % len(corpus.entries) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // TODO(jayconrod,katiehockman): write crashers to testdata and other inputs |
| 118 | + // to GOFUZZCACHE. If the testdata directory is outside the current module, |
| 119 | + // always write to GOFUZZCACHE, since the testdata is likely read-only. |
| 120 | +} |
| 121 | + |
| 122 | +type corpus struct { |
| 123 | + entries []corpusEntry |
| 124 | +} |
| 125 | + |
| 126 | +// TODO(jayconrod,katiehockman): decide whether and how to unify this type |
| 127 | +// with the equivalent in testing. |
| 128 | +type corpusEntry struct { |
| 129 | + b []byte |
| 130 | +} |
| 131 | + |
| 132 | +// coordinator holds channels that workers can use to communicate with |
| 133 | +// the coordinator. |
| 134 | +type coordinator struct { |
| 135 | + // doneC is closed to indicate fuzzing is done and workers should stop. |
| 136 | + // doneC may be closed due to a time limit expiring or a fatal error in |
| 137 | + // a worker. |
| 138 | + doneC chan struct{} |
| 139 | + |
| 140 | + // inputC is sent values to fuzz by the coordinator. Any worker may receive |
| 141 | + // values from this channel. |
| 142 | + inputC chan corpusEntry |
| 143 | +} |
0 commit comments