github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/internal/fuzz/fuzz.go (about) 1 // Package fuzz is a shim to allow compilation against Go 1.18. 2 // It only defines a single type to work with testing/internal/testdeps, 3 // and hide all the other new dependencies from this package. 4 package fuzz 5 6 import ( 7 "context" 8 "errors" 9 "io" 10 "reflect" 11 "time" 12 ) 13 14 // CorpusEntry represents an individual input for fuzzing. 15 // 16 // We must use an equivalent type in the testing and testing/internal/testdeps 17 // packages, but testing can't import this package directly, and we don't want 18 // to export this type from testing. Instead, we use the same struct type and 19 // use a type alias (not a defined type) for convenience. 20 type CorpusEntry = struct { 21 Parent string 22 23 // Path is the path of the corpus file, if the entry was loaded from disk. 24 // For other entries, including seed values provided by f.Add, Path is the 25 // name of the test, e.g. seed#0 or its hash. 26 Path string 27 28 // Data is the raw input data. Data should only be populated for seed 29 // values. For on-disk corpus files, Data will be nil, as it will be loaded 30 // from disk using Path. 31 Data []byte 32 33 // Values is the unmarshaled values from a corpus file. 34 Values []any 35 36 Generation int 37 38 // IsSeed indicates whether this entry is part of the seed corpus. 39 IsSeed bool 40 } 41 42 // CoordinateFuzzingOpts is a set of arguments for CoordinateFuzzing. 43 // The zero value is valid for each field unless specified otherwise. 44 type CoordinateFuzzingOpts struct { 45 // Log is a writer for logging progress messages and warnings. 46 // If nil, io.Discard will be used instead. 47 Log io.Writer 48 49 // Timeout is the amount of wall clock time to spend fuzzing after the corpus 50 // has loaded. If zero, there will be no time limit. 51 Timeout time.Duration 52 53 // Limit is the number of random values to generate and test. If zero, 54 // there will be no limit on the number of generated values. 55 Limit int64 56 57 // MinimizeTimeout is the amount of wall clock time to spend minimizing 58 // after discovering a crasher. If zero, there will be no time limit. If 59 // MinimizeTimeout and MinimizeLimit are both zero, then minimization will 60 // be disabled. 61 MinimizeTimeout time.Duration 62 63 // MinimizeLimit is the maximum number of calls to the fuzz function to be 64 // made while minimizing after finding a crash. If zero, there will be no 65 // limit. Calls to the fuzz function made when minimizing also count toward 66 // Limit. If MinimizeTimeout and MinimizeLimit are both zero, then 67 // minimization will be disabled. 68 MinimizeLimit int64 69 70 // parallel is the number of worker processes to run in parallel. If zero, 71 // CoordinateFuzzing will run GOMAXPROCS workers. 72 Parallel int 73 74 // Seed is a list of seed values added by the fuzz target with testing.F.Add 75 // and in testdata. 76 Seed []CorpusEntry 77 78 // Types is the list of types which make up a corpus entry. 79 // Types must be set and must match values in Seed. 80 Types []reflect.Type 81 82 // CorpusDir is a directory where files containing values that crash the 83 // code being tested may be written. CorpusDir must be set. 84 CorpusDir string 85 86 // CacheDir is a directory containing additional "interesting" values. 87 // The fuzzer may derive new values from these, and may write new values here. 88 CacheDir string 89 } 90 91 // CoordinateFuzzing creates several worker processes and communicates with 92 // them to test random inputs that could trigger crashes and expose bugs. 93 // The worker processes run the same binary in the same directory with the 94 // same environment variables as the coordinator process. Workers also run 95 // with the same arguments as the coordinator, except with the -test.fuzzworker 96 // flag prepended to the argument list. 97 // 98 // If a crash occurs, the function will return an error containing information 99 // about the crash, which can be reported to the user. 100 func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err error) { 101 return errors.New("not implemented") 102 } 103 104 // ReadCorpus reads the corpus from the provided dir. The returned corpus 105 // entries are guaranteed to match the given types. Any malformed files will 106 // be saved in a MalformedCorpusError and returned, along with the most recent 107 // error. 108 func ReadCorpus(dir string, types []reflect.Type) ([]CorpusEntry, error) { 109 return nil, errors.New("not implemented") 110 } 111 112 // CheckCorpus verifies that the types in vals match the expected types 113 // provided. 114 func CheckCorpus(vals []any, types []reflect.Type) error { 115 return errors.New("not implemented") 116 } 117 118 func ResetCoverage() {} 119 func SnapshotCoverage() {} 120 121 // RunFuzzWorker is called in a worker process to communicate with the 122 // coordinator process in order to fuzz random inputs. RunFuzzWorker loops 123 // until the coordinator tells it to stop. 124 // 125 // fn is a wrapper on the fuzz function. It may return an error to indicate 126 // a given input "crashed". The coordinator will also record a crasher if 127 // the function times out or terminates the process. 128 // 129 // RunFuzzWorker returns an error if it could not communicate with the 130 // coordinator process. 131 func RunFuzzWorker(ctx context.Context, fn func(CorpusEntry) error) error { 132 return errors.New("not implemented") 133 }