@@ -141,14 +141,14 @@ func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err err
141
141
if c .crashMinimizing == nil || crashWritten {
142
142
return
143
143
}
144
- fileName , werr := writeToCorpus (c .crashMinimizing .entry . Data , opts .CorpusDir )
144
+ werr := writeToCorpus (& c .crashMinimizing .entry , opts .CorpusDir )
145
145
if werr != nil {
146
146
err = fmt .Errorf ("%w\n %v" , err , werr )
147
147
return
148
148
}
149
149
if err == nil {
150
150
err = & crashError {
151
- name : filepath . Base ( fileName ) ,
151
+ path : c . crashMinimizing . entry . Path ,
152
152
err : errors .New (c .crashMinimizing .crasherMsg ),
153
153
}
154
154
}
@@ -230,7 +230,8 @@ func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err err
230
230
231
231
if result .crasherMsg != "" {
232
232
if c .warmupRun () && result .entry .IsSeed {
233
- fmt .Fprintf (c .opts .Log , "found a crash while testing seed corpus entry: %q\n " , result .entry .Parent )
233
+ target := filepath .Base (c .opts .CorpusDir )
234
+ fmt .Fprintf (c .opts .Log , "found a crash while testing seed corpus entry: %s/%s\n " , target , testName (result .entry .Parent ))
234
235
stop (errors .New (result .crasherMsg ))
235
236
break
236
237
}
@@ -249,11 +250,11 @@ func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err err
249
250
} else if ! crashWritten {
250
251
// Found a crasher that's either minimized or not minimizable.
251
252
// Write to corpus and stop.
252
- fileName , err := writeToCorpus (result .entry . Data , opts .CorpusDir )
253
+ err := writeToCorpus (& result .entry , opts .CorpusDir )
253
254
if err == nil {
254
255
crashWritten = true
255
256
err = & crashError {
256
- name : filepath . Base ( fileName ) ,
257
+ path : result . entry . Path ,
257
258
err : errors .New (result .crasherMsg ),
258
259
}
259
260
}
@@ -262,7 +263,7 @@ func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err err
262
263
c .opts .Log ,
263
264
"DEBUG new crasher, elapsed: %s, id: %s, parent: %s, gen: %d, size: %d, exec time: %s\n " ,
264
265
c .elapsed (),
265
- fileName ,
266
+ result . entry . Path ,
266
267
result .entry .Parent ,
267
268
result .entry .Generation ,
268
269
len (result .entry .Data ),
@@ -315,12 +316,11 @@ func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err err
315
316
// Update the coordinator's coverage mask and save the value.
316
317
inputSize := len (result .entry .Data )
317
318
if opts .CacheDir != "" {
318
- filename , err := writeToCorpus (result .entry . Data , opts .CacheDir )
319
+ err := writeToCorpus (& result .entry , opts .CacheDir )
319
320
if err != nil {
320
321
stop (err )
321
322
}
322
323
result .entry .Data = nil
323
- result .entry .Name = filename
324
324
}
325
325
c .updateCoverage (keepCoverage )
326
326
c .corpus .entries = append (c .corpus .entries , result .entry )
@@ -331,7 +331,7 @@ func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err err
331
331
c .opts .Log ,
332
332
"DEBUG new interesting input, elapsed: %s, id: %s, parent: %s, gen: %d, new bits: %d, total bits: %d, size: %d, exec time: %s\n " ,
333
333
c .elapsed (),
334
- result .entry .Name ,
334
+ result .entry .Path ,
335
335
result .entry .Parent ,
336
336
result .entry .Generation ,
337
337
countBits (keepCoverage ),
@@ -347,7 +347,7 @@ func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err err
347
347
c .opts .Log ,
348
348
"DEBUG worker reported interesting input that doesn't expand coverage, elapsed: %s, id: %s, parent: %s, canMinimize: %t\n " ,
349
349
c .elapsed (),
350
- result .entry .Name ,
350
+ result .entry .Path ,
351
351
result .entry .Parent ,
352
352
result .canMinimize ,
353
353
)
@@ -397,7 +397,7 @@ func CoordinateFuzzing(ctx context.Context, opts CoordinateFuzzingOpts) (err err
397
397
// of the file where the input causing the crasher was saved. The testing
398
398
// framework uses this to report a command to re-run that specific input.
399
399
type crashError struct {
400
- name string
400
+ path string
401
401
err error
402
402
}
403
403
@@ -409,8 +409,8 @@ func (e *crashError) Unwrap() error {
409
409
return e .err
410
410
}
411
411
412
- func (e * crashError ) CrashName () string {
413
- return e .name
412
+ func (e * crashError ) CrashPath () string {
413
+ return e .path
414
414
}
415
415
416
416
type corpus struct {
@@ -426,14 +426,14 @@ type corpus struct {
426
426
type CorpusEntry = struct {
427
427
Parent string
428
428
429
- // Name is the name of the corpus file, if the entry was loaded from the
430
- // seed corpus. It can be used with -run. For entries added with f.Add and
431
- // entries generated by the mutator, Name is empty and Data is populated .
432
- Name string
429
+ // Path is the path of the corpus file, if the entry was loaded from disk.
430
+ // For other entries, including seed values provided by f.Add, Path is the
431
+ // name of the test, e.g. seed#0 or its hash .
432
+ Path string
433
433
434
- // Data is the raw input data. Data should only be populated for initial
435
- // seed values added with f.Add. For on-disk corpus files, Data will
436
- // be nil .
434
+ // Data is the raw input data. Data should only be populated for seed
435
+ // values. For on-disk corpus files, Data will be nil, as it will be loaded
436
+ // from disk using Path .
437
437
Data []byte
438
438
439
439
// Values is the unmarshaled values from a corpus file.
@@ -452,7 +452,7 @@ func CorpusEntryData(ce CorpusEntry) ([]byte, error) {
452
452
return ce .Data , nil
453
453
}
454
454
455
- return os .ReadFile (ce .Name )
455
+ return os .ReadFile (ce .Path )
456
456
}
457
457
458
458
type fuzzInput struct {
@@ -616,7 +616,7 @@ type coordinator struct {
616
616
}
617
617
618
618
func newCoordinator (opts CoordinateFuzzingOpts ) (* coordinator , error ) {
619
- // Make sure all of the seed corpus given by f.Add has marshalled data.
619
+ // Make sure all of the seed corpus has marshalled data.
620
620
for i := range opts .Seed {
621
621
if opts .Seed [i ].Data == nil && opts .Seed [i ].Values != nil {
622
622
opts .Seed [i ].Data = marshalCorpusFile (opts .Seed [i ].Values ... )
@@ -673,7 +673,7 @@ func newCoordinator(opts CoordinateFuzzingOpts) (*coordinator, error) {
673
673
data := marshalCorpusFile (vals ... )
674
674
h := sha256 .Sum256 (data )
675
675
name := fmt .Sprintf ("%x" , h [:4 ])
676
- c .corpus .entries = append (c .corpus .entries , CorpusEntry {Name : name , Data : data })
676
+ c .corpus .entries = append (c .corpus .entries , CorpusEntry {Path : name , Data : data })
677
677
}
678
678
679
679
return c , nil
@@ -956,7 +956,7 @@ func ReadCorpus(dir string, types []reflect.Type) ([]CorpusEntry, error) {
956
956
errs = append (errs , fmt .Errorf ("%q: %v" , filename , err ))
957
957
continue
958
958
}
959
- corpus = append (corpus , CorpusEntry {Name : filename , Values : vals })
959
+ corpus = append (corpus , CorpusEntry {Path : filename , Values : vals })
960
960
}
961
961
if len (errs ) > 0 {
962
962
return corpus , & MalformedCorpusError {errs : errs }
@@ -979,7 +979,7 @@ func readCorpusData(data []byte, types []reflect.Type) ([]interface{}, error) {
979
979
// provided.
980
980
func CheckCorpus (vals []interface {}, types []reflect.Type ) error {
981
981
if len (vals ) != len (types ) {
982
- return fmt .Errorf ("wrong number of values in corpus entry: %d, want %d " , len ( vals ), len ( types ) )
982
+ return fmt .Errorf ("wrong number of values in corpus entry %v: want %v " , vals , types )
983
983
}
984
984
for i := range types {
985
985
if reflect .TypeOf (vals [i ]) != types [i ] {
@@ -989,21 +989,25 @@ func CheckCorpus(vals []interface{}, types []reflect.Type) error {
989
989
return nil
990
990
}
991
991
992
- // writeToCorpus atomically writes the given bytes to a new file in testdata.
993
- // If the directory does not exist, it will create one. If the file already
994
- // exists, writeToCorpus will not rewrite it. writeToCorpus returns the
995
- // file's name, or an error if it failed.
996
- func writeToCorpus (b [] byte , dir string ) (name string , err error ) {
997
- sum := fmt .Sprintf ("%x" , sha256 .Sum256 (b ))
998
- name = filepath .Join (dir , sum )
992
+ // writeToCorpus atomically writes the given bytes to a new file in testdata. If
993
+ // the directory does not exist, it will create one. If the file already exists,
994
+ // writeToCorpus will not rewrite it. writeToCorpus sets entry.Path to the new
995
+ // file that was just written or an error if it failed.
996
+ func writeToCorpus (entry * CorpusEntry , dir string ) (err error ) {
997
+ sum := fmt .Sprintf ("%x" , sha256 .Sum256 (entry . Data ))
998
+ entry . Path = filepath .Join (dir , sum )
999
999
if err := os .MkdirAll (dir , 0777 ); err != nil {
1000
- return "" , err
1000
+ return err
1001
1001
}
1002
- if err := ioutil .WriteFile (name , b , 0666 ); err != nil {
1003
- os .Remove (name ) // remove partially written file
1004
- return "" , err
1002
+ if err := ioutil .WriteFile (entry . Path , entry . Data , 0666 ); err != nil {
1003
+ os .Remove (entry . Path ) // remove partially written file
1004
+ return err
1005
1005
}
1006
- return name , nil
1006
+ return nil
1007
+ }
1008
+
1009
+ func testName (path string ) string {
1010
+ return filepath .Base (path )
1007
1011
}
1008
1012
1009
1013
func zeroValue (t reflect.Type ) interface {} {
0 commit comments