@@ -472,46 +472,76 @@ int MinimizeCrashInputInternalStep(Fuzzer *F, InputCorpus *Corpus) {
472
472
}
473
473
474
474
// This is just a skeleton of an experimental -fork=1 feature.
475
- void FuzzWithFork (const FuzzingOptions &Options,
475
+ void FuzzWithFork (Fuzzer *F, const FuzzingOptions &Options,
476
476
const Vector<std::string> &Args,
477
477
const Vector<std::string> &Corpora) {
478
478
Printf (" INFO: -fork=1: doing fuzzing in a separate process in order to "
479
479
" be more resistant to crashes, timeouts, and OOMs\n " );
480
+ auto Rand = F->GetMD ().GetRand ();
480
481
481
482
Vector<SizedFile> Corpus;
482
483
for (auto &Dir : Corpora)
483
484
GetSizedFilesFromDir (Dir, &Corpus);
484
485
std::sort (Corpus.begin (), Corpus.end ());
486
+ auto CFPath = TempPath (" .fork" );
485
487
486
488
Vector<std::string> Files;
487
489
Set<uint32_t > Features;
488
490
if (!Corpus.empty ()) {
489
- auto CFPath = TempPath (" .fork" );
490
491
CrashResistantMerge (Args, {}, Corpus, &Files, {}, &Features, CFPath);
491
492
RemoveFile (CFPath);
492
493
}
493
- Printf (" INFO: -fork=1: %zd seeds, starting to fuzz\n " , Files.size ());
494
+ auto TempDir = TempPath (" Dir" );
495
+ MkDir (TempDir);
496
+ Printf (" INFO: -fork=1: %zd seeds, starting to fuzz; scratch: %s\n " ,
497
+ Files.size (), TempDir.c_str ());
494
498
495
- Command Cmd (Args);
496
- Cmd .removeFlag (" fork" );
499
+ Command BaseCmd (Args);
500
+ BaseCmd .removeFlag (" fork" );
497
501
for (auto &C : Corpora) // Remove all corpora from the args.
498
- Cmd.removeArgument (C);
499
- if (Files.size () >= 2 )
500
- Cmd.addFlag (" seed_inputs" ,
501
- Files.back () + " ," + Files[Files.size () - 2 ]);
502
- Cmd.addFlag (" runs" , " 1000000" );
503
- Cmd.addFlag (" max_total_time" , " 30" );
504
- for (size_t i = 0 ; i < 1000 ; i++) {
502
+ BaseCmd.removeArgument (C);
503
+ BaseCmd.addFlag (" runs" , " 1000000" );
504
+ BaseCmd.addFlag (" max_total_time" , " 30" );
505
+ BaseCmd.addArgument (TempDir);
506
+ int ExitCode = 0 ;
507
+ for (size_t i = 0 ; i < 1000000 ; i++) {
508
+ // TODO: take new files from disk e.g. those generated by another process.
509
+ Command Cmd (BaseCmd);
510
+ if (Files.size () >= 2 )
511
+ Cmd.addFlag (" seed_inputs" ,
512
+ Files[Rand.SkewTowardsLast (Files.size ())] + " ," +
513
+ Files[Rand.SkewTowardsLast (Files.size ())]);
505
514
Printf (" RUN %s\n " , Cmd.toString ().c_str ());
506
- int ExitCode = ExecuteCommand (Cmd);
515
+ RmFilesInDir (TempDir);
516
+ ExitCode = ExecuteCommand (Cmd);
517
+ Printf (" Exit code: %d\n " , ExitCode);
507
518
if (ExitCode == Options.InterruptExitCode )
508
- exit (0 );
509
- if (ExitCode == Options.TimeoutExitCode || ExitCode == Options.OOMExitCode )
510
- continue ;
519
+ break ;
520
+ Vector<SizedFile> TempFiles;
521
+ Vector<std::string>FilesToAdd;
522
+ Set<uint32_t > NewFeatures;
523
+ GetSizedFilesFromDir (TempDir, &TempFiles);
524
+ CrashResistantMerge (Args, {}, TempFiles, &FilesToAdd, Features,
525
+ &NewFeatures, CFPath);
526
+ RemoveFile (CFPath);
527
+ for (auto &Path : FilesToAdd) {
528
+ auto NewPath = F->WriteToOutputCorpus (FileToVector (Path, Options.MaxLen ));
529
+ if (!NewPath.empty ())
530
+ Files.push_back (NewPath);
531
+ }
532
+ Features.insert (NewFeatures.begin (), NewFeatures.end ());
533
+ Printf (" INFO: temp_files: %zd files_added: %zd newft: %zd ft: %zd\n " ,
534
+ TempFiles.size (), FilesToAdd.size (), NewFeatures.size (),
535
+ Features.size ());
511
536
if (ExitCode != 0 ) break ;
512
537
}
513
538
514
- exit (0 );
539
+ RmFilesInDir (TempDir);
540
+ RmDir (TempDir);
541
+
542
+ // Use the exit code from the last child process.
543
+ Printf (" Fork: exiting: %d\n " , ExitCode);
544
+ exit (ExitCode);
515
545
}
516
546
517
547
void Merge (Fuzzer *F, FuzzingOptions &Options, const Vector<std::string> &Args,
@@ -770,7 +800,7 @@ int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
770
800
}
771
801
772
802
if (Flags.fork )
773
- FuzzWithFork (Options, Args, *Inputs);
803
+ FuzzWithFork (F, Options, Args, *Inputs);
774
804
775
805
if (Flags.merge )
776
806
Merge (F, Options, Args, *Inputs, Flags.merge_control_file );
0 commit comments