1 //===- FuzzerDriver.cpp - FuzzerDriver function and flags -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 // FuzzerDriver and flag parsing.
9 //===----------------------------------------------------------------------===//
11 #include "FuzzerCommand.h"
12 #include "FuzzerCorpus.h"
13 #include "FuzzerFork.h"
15 #include "FuzzerInterface.h"
16 #include "FuzzerInternal.h"
17 #include "FuzzerMerge.h"
18 #include "FuzzerMutate.h"
19 #include "FuzzerRandom.h"
20 #include "FuzzerTracePC.h"
31 // This function should be present in the libFuzzer so that the client
32 // binary can test for its existence.
34 extern "C" void __libfuzzer_is_present() {}
35 #pragma comment(linker, "/include:__libfuzzer_is_present")
37 extern "C" __attribute__((used)) void __libfuzzer_is_present() {}
38 #endif // LIBFUZZER_MSVC
43 struct FlagDescription {
45 const char *Description;
49 unsigned int *UIntFlag;
53 #define FUZZER_DEPRECATED_FLAG(Name)
54 #define FUZZER_FLAG_INT(Name, Default, Description) int Name;
55 #define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
56 #define FUZZER_FLAG_STRING(Name, Description) const char *Name;
57 #include "FuzzerFlags.def"
58 #undef FUZZER_DEPRECATED_FLAG
59 #undef FUZZER_FLAG_INT
60 #undef FUZZER_FLAG_UNSIGNED
61 #undef FUZZER_FLAG_STRING
64 static const FlagDescription FlagDescriptions [] {
65 #define FUZZER_DEPRECATED_FLAG(Name) \
66 {#Name, "Deprecated; don't use", 0, nullptr, nullptr, nullptr},
67 #define FUZZER_FLAG_INT(Name, Default, Description) \
68 {#Name, Description, Default, &Flags.Name, nullptr, nullptr},
69 #define FUZZER_FLAG_UNSIGNED(Name, Default, Description) \
70 {#Name, Description, static_cast<int>(Default), \
71 nullptr, nullptr, &Flags.Name},
72 #define FUZZER_FLAG_STRING(Name, Description) \
73 {#Name, Description, 0, nullptr, &Flags.Name, nullptr},
74 #include "FuzzerFlags.def"
75 #undef FUZZER_DEPRECATED_FLAG
76 #undef FUZZER_FLAG_INT
77 #undef FUZZER_FLAG_UNSIGNED
78 #undef FUZZER_FLAG_STRING
81 static const size_t kNumFlags =
82 sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
84 static Vector<std::string> *Inputs;
85 static std::string *ProgName;
87 static void PrintHelp() {
89 auto Prog = ProgName->c_str();
90 Printf("\nTo run fuzzing pass 0 or more directories.\n");
91 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog);
93 Printf("\nTo run individual tests without fuzzing pass 1 or more files:\n");
94 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog);
96 Printf("\nFlags: (strictly in form -flag=value)\n");
97 size_t MaxFlagLen = 0;
98 for (size_t F = 0; F < kNumFlags; F++)
99 MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen);
101 for (size_t F = 0; F < kNumFlags; F++) {
102 const auto &D = FlagDescriptions[F];
103 if (strstr(D.Description, "internal flag") == D.Description) continue;
104 Printf(" %s", D.Name);
105 for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
108 Printf("%d\t%s\n", D.Default, D.Description);
110 Printf("\nFlags starting with '--' will be ignored and "
111 "will be passed verbatim to subprocesses.\n");
114 static const char *FlagValue(const char *Param, const char *Name) {
115 size_t Len = strlen(Name);
116 if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 &&
117 Param[Len + 1] == '=')
118 return &Param[Len + 2];
122 // Avoid calling stol as it triggers a bug in clang/glibc build.
123 static long MyStol(const char *Str) {
130 for (size_t i = 0; Str[i]; i++) {
132 if (Ch < '0' || Ch > '9')
134 Res = Res * 10 + (Ch - '0');
139 static bool ParseOneFlag(const char *Param) {
140 if (Param[0] != '-') return false;
141 if (Param[1] == '-') {
142 static bool PrintedWarning = false;
143 if (!PrintedWarning) {
144 PrintedWarning = true;
145 Printf("INFO: libFuzzer ignores flags that start with '--'\n");
147 for (size_t F = 0; F < kNumFlags; F++)
148 if (FlagValue(Param + 1, FlagDescriptions[F].Name))
149 Printf("WARNING: did you mean '%s' (single dash)?\n", Param + 1);
152 for (size_t F = 0; F < kNumFlags; F++) {
153 const char *Name = FlagDescriptions[F].Name;
154 const char *Str = FlagValue(Param, Name);
156 if (FlagDescriptions[F].IntFlag) {
157 int Val = MyStol(Str);
158 *FlagDescriptions[F].IntFlag = Val;
159 if (Flags.verbosity >= 2)
160 Printf("Flag: %s %d\n", Name, Val);
162 } else if (FlagDescriptions[F].UIntFlag) {
163 unsigned int Val = std::stoul(Str);
164 *FlagDescriptions[F].UIntFlag = Val;
165 if (Flags.verbosity >= 2)
166 Printf("Flag: %s %u\n", Name, Val);
168 } else if (FlagDescriptions[F].StrFlag) {
169 *FlagDescriptions[F].StrFlag = Str;
170 if (Flags.verbosity >= 2)
171 Printf("Flag: %s %s\n", Name, Str);
173 } else { // Deprecated flag.
174 Printf("Flag: %s: deprecated, don't use\n", Name);
179 Printf("\n\nWARNING: unrecognized flag '%s'; "
180 "use -help=1 to list all flags\n\n", Param);
184 // We don't use any library to minimize dependencies.
185 static void ParseFlags(const Vector<std::string> &Args,
186 const ExternalFunctions *EF) {
187 for (size_t F = 0; F < kNumFlags; F++) {
188 if (FlagDescriptions[F].IntFlag)
189 *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
190 if (FlagDescriptions[F].UIntFlag)
191 *FlagDescriptions[F].UIntFlag =
192 static_cast<unsigned int>(FlagDescriptions[F].Default);
193 if (FlagDescriptions[F].StrFlag)
194 *FlagDescriptions[F].StrFlag = nullptr;
197 // Disable len_control by default, if LLVMFuzzerCustomMutator is used.
198 if (EF->LLVMFuzzerCustomMutator)
199 Flags.len_control = 0;
201 Inputs = new Vector<std::string>;
202 for (size_t A = 1; A < Args.size(); A++) {
203 if (ParseOneFlag(Args[A].c_str())) {
204 if (Flags.ignore_remaining_args)
208 Inputs->push_back(Args[A]);
212 static std::mutex Mu;
214 static void PulseThread() {
217 std::lock_guard<std::mutex> Lock(Mu);
218 Printf("pulse...\n");
222 static void WorkerThread(const Command &BaseCmd, std::atomic<unsigned> *Counter,
223 unsigned NumJobs, std::atomic<bool> *HasErrors) {
225 unsigned C = (*Counter)++;
226 if (C >= NumJobs) break;
227 std::string Log = "fuzz-" + std::to_string(C) + ".log";
228 Command Cmd(BaseCmd);
229 Cmd.setOutputFile(Log);
230 Cmd.combineOutAndErr();
231 if (Flags.verbosity) {
232 std::string CommandLine = Cmd.toString();
233 Printf("%s\n", CommandLine.c_str());
235 int ExitCode = ExecuteCommand(Cmd);
238 std::lock_guard<std::mutex> Lock(Mu);
239 Printf("================== Job %u exited with exit code %d ============\n",
241 fuzzer::CopyFileToErr(Log);
245 std::string CloneArgsWithoutX(const Vector<std::string> &Args,
246 const char *X1, const char *X2) {
248 for (auto &S : Args) {
249 if (FlagValue(S.c_str(), X1) || FlagValue(S.c_str(), X2))
256 static int RunInMultipleProcesses(const Vector<std::string> &Args,
257 unsigned NumWorkers, unsigned NumJobs) {
258 std::atomic<unsigned> Counter(0);
259 std::atomic<bool> HasErrors(false);
261 Cmd.removeFlag("jobs");
262 Cmd.removeFlag("workers");
263 Vector<std::thread> V;
264 std::thread Pulse(PulseThread);
266 for (unsigned i = 0; i < NumWorkers; i++)
267 V.push_back(std::thread(WorkerThread, std::ref(Cmd), &Counter, NumJobs, &HasErrors));
270 return HasErrors ? 1 : 0;
273 static void RssThread(Fuzzer *F, size_t RssLimitMb) {
276 size_t Peak = GetPeakRSSMb();
277 if (Peak > RssLimitMb)
278 F->RssLimitCallback();
282 static void StartRssThread(Fuzzer *F, size_t RssLimitMb) {
283 if (!RssLimitMb) return;
284 std::thread T(RssThread, F, RssLimitMb);
288 int RunOneTest(Fuzzer *F, const char *InputFilePath, size_t MaxLen) {
289 Unit U = FileToVector(InputFilePath);
290 if (MaxLen && MaxLen < U.size())
292 F->ExecuteCallback(U.data(), U.size());
293 F->TryDetectingAMemoryLeak(U.data(), U.size(), true);
297 static bool AllInputsAreFiles() {
298 if (Inputs->empty()) return false;
299 for (auto &Path : *Inputs)
305 static std::string GetDedupTokenFromFile(const std::string &Path) {
306 auto S = FileToString(Path);
307 auto Beg = S.find("DEDUP_TOKEN:");
308 if (Beg == std::string::npos)
310 auto End = S.find('\n', Beg);
311 if (End == std::string::npos)
313 return S.substr(Beg, End - Beg);
316 int CleanseCrashInput(const Vector<std::string> &Args,
317 const FuzzingOptions &Options) {
318 if (Inputs->size() != 1 || !Flags.exact_artifact_path) {
319 Printf("ERROR: -cleanse_crash should be given one input file and"
320 " -exact_artifact_path\n");
323 std::string InputFilePath = Inputs->at(0);
324 std::string OutputFilePath = Flags.exact_artifact_path;
326 Cmd.removeFlag("cleanse_crash");
328 assert(Cmd.hasArgument(InputFilePath));
329 Cmd.removeArgument(InputFilePath);
331 auto LogFilePath = TempPath(".txt");
332 auto TmpFilePath = TempPath(".repro");
333 Cmd.addArgument(TmpFilePath);
334 Cmd.setOutputFile(LogFilePath);
335 Cmd.combineOutAndErr();
337 std::string CurrentFilePath = InputFilePath;
338 auto U = FileToVector(CurrentFilePath);
339 size_t Size = U.size();
341 const Vector<uint8_t> ReplacementBytes = {' ', 0xff};
342 for (int NumAttempts = 0; NumAttempts < 5; NumAttempts++) {
343 bool Changed = false;
344 for (size_t Idx = 0; Idx < Size; Idx++) {
345 Printf("CLEANSE[%d]: Trying to replace byte %zd of %zd\n", NumAttempts,
347 uint8_t OriginalByte = U[Idx];
348 if (ReplacementBytes.end() != std::find(ReplacementBytes.begin(),
349 ReplacementBytes.end(),
352 for (auto NewByte : ReplacementBytes) {
354 WriteToFile(U, TmpFilePath);
355 auto ExitCode = ExecuteCommand(Cmd);
356 RemoveFile(TmpFilePath);
358 U[Idx] = OriginalByte;
361 Printf("CLEANSE: Replaced byte %zd with 0x%x\n", Idx, NewByte);
362 WriteToFile(U, OutputFilePath);
369 RemoveFile(LogFilePath);
373 int MinimizeCrashInput(const Vector<std::string> &Args,
374 const FuzzingOptions &Options) {
375 if (Inputs->size() != 1) {
376 Printf("ERROR: -minimize_crash should be given one input file\n");
379 std::string InputFilePath = Inputs->at(0);
380 Command BaseCmd(Args);
381 BaseCmd.removeFlag("minimize_crash");
382 BaseCmd.removeFlag("exact_artifact_path");
383 assert(BaseCmd.hasArgument(InputFilePath));
384 BaseCmd.removeArgument(InputFilePath);
385 if (Flags.runs <= 0 && Flags.max_total_time == 0) {
386 Printf("INFO: you need to specify -runs=N or "
387 "-max_total_time=N with -minimize_crash=1\n"
388 "INFO: defaulting to -max_total_time=600\n");
389 BaseCmd.addFlag("max_total_time", "600");
392 auto LogFilePath = TempPath(".txt");
393 BaseCmd.setOutputFile(LogFilePath);
394 BaseCmd.combineOutAndErr();
396 std::string CurrentFilePath = InputFilePath;
398 Unit U = FileToVector(CurrentFilePath);
399 Printf("CRASH_MIN: minimizing crash input: '%s' (%zd bytes)\n",
400 CurrentFilePath.c_str(), U.size());
402 Command Cmd(BaseCmd);
403 Cmd.addArgument(CurrentFilePath);
405 std::string CommandLine = Cmd.toString();
406 Printf("CRASH_MIN: executing: %s\n", CommandLine.c_str());
407 int ExitCode = ExecuteCommand(Cmd);
409 Printf("ERROR: the input %s did not crash\n", CurrentFilePath.c_str());
412 Printf("CRASH_MIN: '%s' (%zd bytes) caused a crash. Will try to minimize "
414 CurrentFilePath.c_str(), U.size());
415 auto DedupToken1 = GetDedupTokenFromFile(LogFilePath);
416 if (!DedupToken1.empty())
417 Printf("CRASH_MIN: DedupToken1: %s\n", DedupToken1.c_str());
419 std::string ArtifactPath =
420 Flags.exact_artifact_path
421 ? Flags.exact_artifact_path
422 : Options.ArtifactPrefix + "minimized-from-" + Hash(U);
423 Cmd.addFlag("minimize_crash_internal_step", "1");
424 Cmd.addFlag("exact_artifact_path", ArtifactPath);
425 CommandLine = Cmd.toString();
426 Printf("CRASH_MIN: executing: %s\n", CommandLine.c_str());
427 ExitCode = ExecuteCommand(Cmd);
428 CopyFileToErr(LogFilePath);
430 if (Flags.exact_artifact_path) {
431 CurrentFilePath = Flags.exact_artifact_path;
432 WriteToFile(U, CurrentFilePath);
434 Printf("CRASH_MIN: failed to minimize beyond %s (%d bytes), exiting\n",
435 CurrentFilePath.c_str(), U.size());
438 auto DedupToken2 = GetDedupTokenFromFile(LogFilePath);
439 if (!DedupToken2.empty())
440 Printf("CRASH_MIN: DedupToken2: %s\n", DedupToken2.c_str());
442 if (DedupToken1 != DedupToken2) {
443 if (Flags.exact_artifact_path) {
444 CurrentFilePath = Flags.exact_artifact_path;
445 WriteToFile(U, CurrentFilePath);
447 Printf("CRASH_MIN: mismatch in dedup tokens"
448 " (looks like a different bug). Won't minimize further\n");
452 CurrentFilePath = ArtifactPath;
453 Printf("*********************************\n");
455 RemoveFile(LogFilePath);
459 int MinimizeCrashInputInternalStep(Fuzzer *F, InputCorpus *Corpus) {
460 assert(Inputs->size() == 1);
461 std::string InputFilePath = Inputs->at(0);
462 Unit U = FileToVector(InputFilePath);
463 Printf("INFO: Starting MinimizeCrashInputInternalStep: %zd\n", U.size());
465 Printf("INFO: The input is small enough, exiting\n");
468 F->SetMaxInputLen(U.size());
469 F->SetMaxMutationLen(U.size() - 1);
470 F->MinimizeCrashLoop(U);
471 Printf("INFO: Done MinimizeCrashInputInternalStep, no crashes found\n");
476 void Merge(Fuzzer *F, FuzzingOptions &Options, const Vector<std::string> &Args,
477 const Vector<std::string> &Corpora, const char *CFPathOrNull) {
478 if (Corpora.size() < 2) {
479 Printf("INFO: Merge requires two or more corpus dirs\n");
483 Vector<SizedFile> OldCorpus, NewCorpus;
484 GetSizedFilesFromDir(Corpora[0], &OldCorpus);
485 for (size_t i = 1; i < Corpora.size(); i++)
486 GetSizedFilesFromDir(Corpora[i], &NewCorpus);
487 std::sort(OldCorpus.begin(), OldCorpus.end());
488 std::sort(NewCorpus.begin(), NewCorpus.end());
490 std::string CFPath = CFPathOrNull ? CFPathOrNull : TempPath(".txt");
491 Vector<std::string> NewFiles;
492 Set<uint32_t> NewFeatures, NewCov;
493 CrashResistantMerge(Args, OldCorpus, NewCorpus, &NewFiles, {}, &NewFeatures,
494 {}, &NewCov, CFPath, true);
495 for (auto &Path : NewFiles)
496 F->WriteToOutputCorpus(FileToVector(Path, Options.MaxLen));
497 // We are done, delete the control file if it was a temporary one.
498 if (!Flags.merge_control_file)
504 int AnalyzeDictionary(Fuzzer *F, const Vector<Unit>& Dict,
505 UnitVector& Corpus) {
506 Printf("Started dictionary minimization (up to %d tests)\n",
507 Dict.size() * Corpus.size() * 2);
509 // Scores and usage count for each dictionary unit.
510 Vector<int> Scores(Dict.size());
511 Vector<int> Usages(Dict.size());
513 Vector<size_t> InitialFeatures;
514 Vector<size_t> ModifiedFeatures;
515 for (auto &C : Corpus) {
516 // Get coverage for the testcase without modifications.
517 F->ExecuteCallback(C.data(), C.size());
518 InitialFeatures.clear();
519 TPC.CollectFeatures([&](size_t Feature) {
520 InitialFeatures.push_back(Feature);
523 for (size_t i = 0; i < Dict.size(); ++i) {
524 Vector<uint8_t> Data = C;
525 auto StartPos = std::search(Data.begin(), Data.end(),
526 Dict[i].begin(), Dict[i].end());
527 // Skip dictionary unit, if the testcase does not contain it.
528 if (StartPos == Data.end())
532 while (StartPos != Data.end()) {
533 // Replace all occurrences of dictionary unit in the testcase.
534 auto EndPos = StartPos + Dict[i].size();
535 for (auto It = StartPos; It != EndPos; ++It)
538 StartPos = std::search(EndPos, Data.end(),
539 Dict[i].begin(), Dict[i].end());
542 // Get coverage for testcase with masked occurrences of dictionary unit.
543 F->ExecuteCallback(Data.data(), Data.size());
544 ModifiedFeatures.clear();
545 TPC.CollectFeatures([&](size_t Feature) {
546 ModifiedFeatures.push_back(Feature);
549 if (InitialFeatures == ModifiedFeatures)
556 Printf("###### Useless dictionary elements. ######\n");
557 for (size_t i = 0; i < Dict.size(); ++i) {
558 // Dictionary units with positive score are treated as useful ones.
563 PrintASCII(Dict[i].data(), Dict[i].size(), "\"");
564 Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i]);
566 Printf("###### End of useless dictionary elements. ######\n");
570 Vector<std::string> ParseSeedInuts(const char *seed_inputs) {
571 // Parse -seed_inputs=file1,file2,... or -seed_inputs=@seed_inputs_file
572 Vector<std::string> Files;
573 if (!seed_inputs) return Files;
574 std::string SeedInputs;
575 if (Flags.seed_inputs[0] == '@')
576 SeedInputs = FileToString(Flags.seed_inputs + 1); // File contains list.
578 SeedInputs = Flags.seed_inputs; // seed_inputs contains the list.
579 if (SeedInputs.empty()) {
580 Printf("seed_inputs is empty or @file does not exist.\n");
584 size_t comma_pos = 0;
585 while ((comma_pos = SeedInputs.find_last_of(',')) != std::string::npos) {
586 Files.push_back(SeedInputs.substr(comma_pos + 1));
587 SeedInputs = SeedInputs.substr(0, comma_pos);
589 Files.push_back(SeedInputs);
593 static Vector<SizedFile> ReadCorpora(const Vector<std::string> &CorpusDirs,
594 const Vector<std::string> &ExtraSeedFiles) {
595 Vector<SizedFile> SizedFiles;
596 size_t LastNumFiles = 0;
597 for (auto &Dir : CorpusDirs) {
598 GetSizedFilesFromDir(Dir, &SizedFiles);
599 Printf("INFO: % 8zd files found in %s\n", SizedFiles.size() - LastNumFiles,
601 LastNumFiles = SizedFiles.size();
603 for (auto &File : ExtraSeedFiles)
604 if (auto Size = FileSize(File))
605 SizedFiles.push_back({File, Size});
609 int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
610 using namespace fuzzer;
611 assert(argc && argv && "Argument pointers cannot be nullptr");
612 std::string Argv0((*argv)[0]);
613 EF = new ExternalFunctions();
614 if (EF->LLVMFuzzerInitialize)
615 EF->LLVMFuzzerInitialize(argc, argv);
616 if (EF->__msan_scoped_disable_interceptor_checks)
617 EF->__msan_scoped_disable_interceptor_checks();
618 const Vector<std::string> Args(*argv, *argv + *argc);
619 assert(!Args.empty());
620 ProgName = new std::string(Args[0]);
621 if (Argv0 != *ProgName) {
622 Printf("ERROR: argv[0] has been modified in LLVMFuzzerInitialize\n");
625 ParseFlags(Args, EF);
631 if (Flags.close_fd_mask & 2)
633 if (Flags.close_fd_mask & 1)
636 if (Flags.jobs > 0 && Flags.workers == 0) {
637 Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
638 if (Flags.workers > 1)
639 Printf("Running %u workers\n", Flags.workers);
642 if (Flags.workers > 0 && Flags.jobs > 0)
643 return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
645 FuzzingOptions Options;
646 Options.Verbosity = Flags.verbosity;
647 Options.MaxLen = Flags.max_len;
648 Options.LenControl = Flags.len_control;
649 Options.UnitTimeoutSec = Flags.timeout;
650 Options.ErrorExitCode = Flags.error_exitcode;
651 Options.TimeoutExitCode = Flags.timeout_exitcode;
652 Options.IgnoreTimeouts = Flags.ignore_timeouts;
653 Options.IgnoreOOMs = Flags.ignore_ooms;
654 Options.IgnoreCrashes = Flags.ignore_crashes;
655 Options.MaxTotalTimeSec = Flags.max_total_time;
656 Options.DoCrossOver = Flags.cross_over;
657 Options.MutateDepth = Flags.mutate_depth;
658 Options.ReduceDepth = Flags.reduce_depth;
659 Options.UseCounters = Flags.use_counters;
660 Options.UseMemmem = Flags.use_memmem;
661 Options.UseCmp = Flags.use_cmp;
662 Options.UseValueProfile = Flags.use_value_profile;
663 Options.Shrink = Flags.shrink;
664 Options.ReduceInputs = Flags.reduce_inputs;
665 Options.ShuffleAtStartUp = Flags.shuffle;
666 Options.PreferSmall = Flags.prefer_small;
667 Options.ReloadIntervalSec = Flags.reload;
668 Options.OnlyASCII = Flags.only_ascii;
669 Options.DetectLeaks = Flags.detect_leaks;
670 Options.PurgeAllocatorIntervalSec = Flags.purge_allocator_interval;
671 Options.TraceMalloc = Flags.trace_malloc;
672 Options.RssLimitMb = Flags.rss_limit_mb;
673 Options.MallocLimitMb = Flags.malloc_limit_mb;
674 if (!Options.MallocLimitMb)
675 Options.MallocLimitMb = Options.RssLimitMb;
677 Options.MaxNumberOfRuns = Flags.runs;
678 if (!Inputs->empty() && !Flags.minimize_crash_internal_step)
679 Options.OutputCorpus = (*Inputs)[0];
680 Options.ReportSlowUnits = Flags.report_slow_units;
681 if (Flags.artifact_prefix)
682 Options.ArtifactPrefix = Flags.artifact_prefix;
683 if (Flags.exact_artifact_path)
684 Options.ExactArtifactPath = Flags.exact_artifact_path;
685 Vector<Unit> Dictionary;
687 if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
689 if (Flags.verbosity > 0 && !Dictionary.empty())
690 Printf("Dictionary: %zd entries\n", Dictionary.size());
691 bool RunIndividualFiles = AllInputsAreFiles();
692 Options.SaveArtifacts =
693 !RunIndividualFiles || Flags.minimize_crash_internal_step;
694 Options.PrintNewCovPcs = Flags.print_pcs;
695 Options.PrintNewCovFuncs = Flags.print_funcs;
696 Options.PrintFinalStats = Flags.print_final_stats;
697 Options.PrintCorpusStats = Flags.print_corpus_stats;
698 Options.PrintCoverage = Flags.print_coverage;
699 if (Flags.exit_on_src_pos)
700 Options.ExitOnSrcPos = Flags.exit_on_src_pos;
701 if (Flags.exit_on_item)
702 Options.ExitOnItem = Flags.exit_on_item;
703 if (Flags.focus_function)
704 Options.FocusFunction = Flags.focus_function;
705 if (Flags.data_flow_trace)
706 Options.DataFlowTrace = Flags.data_flow_trace;
707 if (Flags.features_dir)
708 Options.FeaturesDir = Flags.features_dir;
709 if (Flags.collect_data_flow)
710 Options.CollectDataFlow = Flags.collect_data_flow;
711 Options.LazyCounters = Flags.lazy_counters;
713 Options.StopFile = Flags.stop_file;
715 unsigned Seed = Flags.seed;
719 std::chrono::system_clock::now().time_since_epoch().count() + GetPid();
721 Printf("INFO: Seed: %u\n", Seed);
723 if (Flags.collect_data_flow && !Flags.fork && !Flags.merge) {
724 if (RunIndividualFiles)
725 return CollectDataFlow(Flags.collect_data_flow, Flags.data_flow_trace,
726 ReadCorpora({}, *Inputs));
728 return CollectDataFlow(Flags.collect_data_flow, Flags.data_flow_trace,
729 ReadCorpora(*Inputs, {}));
733 auto *MD = new MutationDispatcher(Rand, Options);
734 auto *Corpus = new InputCorpus(Options.OutputCorpus);
735 auto *F = new Fuzzer(Callback, *Corpus, *MD, Options);
737 for (auto &U: Dictionary)
738 if (U.size() <= Word::GetMaxSize())
739 MD->AddWordToManualDictionary(Word(U.data(), U.size()));
741 StartRssThread(F, Flags.rss_limit_mb);
743 Options.HandleAbrt = Flags.handle_abrt;
744 Options.HandleBus = Flags.handle_bus;
745 Options.HandleFpe = Flags.handle_fpe;
746 Options.HandleIll = Flags.handle_ill;
747 Options.HandleInt = Flags.handle_int;
748 Options.HandleSegv = Flags.handle_segv;
749 Options.HandleTerm = Flags.handle_term;
750 Options.HandleXfsz = Flags.handle_xfsz;
751 Options.HandleUsr1 = Flags.handle_usr1;
752 Options.HandleUsr2 = Flags.handle_usr2;
753 SetSignalHandler(Options);
755 std::atexit(Fuzzer::StaticExitCallback);
757 if (Flags.minimize_crash)
758 return MinimizeCrashInput(Args, Options);
760 if (Flags.minimize_crash_internal_step)
761 return MinimizeCrashInputInternalStep(F, Corpus);
763 if (Flags.cleanse_crash)
764 return CleanseCrashInput(Args, Options);
766 if (RunIndividualFiles) {
767 Options.SaveArtifacts = false;
768 int Runs = std::max(1, Flags.runs);
769 Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
770 Inputs->size(), Runs);
771 for (auto &Path : *Inputs) {
772 auto StartTime = system_clock::now();
773 Printf("Running: %s\n", Path.c_str());
774 for (int Iter = 0; Iter < Runs; Iter++)
775 RunOneTest(F, Path.c_str(), Options.MaxLen);
776 auto StopTime = system_clock::now();
777 auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
778 Printf("Executed %s in %zd ms\n", Path.c_str(), (long)MS);
781 "*** NOTE: fuzzing was not performed, you have only\n"
782 "*** executed the target code on a fixed set of inputs.\n"
784 F->PrintFinalStats();
789 FuzzWithFork(F->GetMD().GetRand(), Options, Args, *Inputs, Flags.fork);
792 Merge(F, Options, Args, *Inputs, Flags.merge_control_file);
794 if (Flags.merge_inner) {
795 const size_t kDefaultMaxMergeLen = 1 << 20;
796 if (Options.MaxLen == 0)
797 F->SetMaxInputLen(kDefaultMaxMergeLen);
798 assert(Flags.merge_control_file);
799 F->CrashResistantMergeInternalStep(Flags.merge_control_file);
803 if (Flags.analyze_dict) {
804 size_t MaxLen = INT_MAX; // Large max length.
805 UnitVector InitialCorpus;
806 for (auto &Inp : *Inputs) {
807 Printf("Loading corpus dir: %s\n", Inp.c_str());
808 ReadDirToVectorOfUnits(Inp.c_str(), &InitialCorpus, nullptr,
809 MaxLen, /*ExitOnError=*/false);
812 if (Dictionary.empty() || Inputs->empty()) {
813 Printf("ERROR: can't analyze dict without dict and corpus provided\n");
816 if (AnalyzeDictionary(F, Dictionary, InitialCorpus)) {
817 Printf("Dictionary analysis failed\n");
820 Printf("Dictionary analysis succeeded\n");
824 auto CorporaFiles = ReadCorpora(*Inputs, ParseSeedInuts(Flags.seed_inputs));
825 F->Loop(CorporaFiles);
828 Printf("Done %zd runs in %zd second(s)\n", F->getTotalNumberOfRuns(),
829 F->secondsSinceProcessStartUp());
830 F->PrintFinalStats();
832 exit(0); // Don't let F destroy itself.
835 // Storage for global ExternalFunctions object.
836 ExternalFunctions *EF = nullptr;
838 } // namespace fuzzer