1 //===- FuzzerDriver.cpp - FuzzerDriver function and flags -----------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 // FuzzerDriver and flag parsing.
9 //===----------------------------------------------------------------------===//
10 
11 #include "FuzzerCommand.h"
12 #include "FuzzerCorpus.h"
13 #include "FuzzerFork.h"
14 #include "FuzzerIO.h"
15 #include "FuzzerInterface.h"
16 #include "FuzzerInternal.h"
17 #include "FuzzerMerge.h"
18 #include "FuzzerMutate.h"
19 #include "FuzzerPlatform.h"
20 #include "FuzzerRandom.h"
21 #include "FuzzerTracePC.h"
22 #include <algorithm>
23 #include <atomic>
24 #include <chrono>
25 #include <cstdlib>
26 #include <cstring>
27 #include <mutex>
28 #include <string>
29 #include <thread>
30 #include <fstream>
31 
32 // This function should be present in the libFuzzer so that the client
33 // binary can test for its existence.
34 #if LIBFUZZER_MSVC
35 extern "C" void __libfuzzer_is_present() {}
36 #if defined(_M_IX86) || defined(__i386__)
37 #pragma comment(linker, "/include:___libfuzzer_is_present")
38 #else
39 #pragma comment(linker, "/include:__libfuzzer_is_present")
40 #endif
41 #else
42 extern "C" __attribute__((used)) void __libfuzzer_is_present() {}
43 #endif  // LIBFUZZER_MSVC
44 
45 namespace fuzzer {
46 
47 // Program arguments.
48 struct FlagDescription {
49   const char *Name;
50   const char *Description;
51   int   Default;
52   int   *IntFlag;
53   const char **StrFlag;
54   unsigned int *UIntFlag;
55 };
56 
57 struct {
58 #define FUZZER_DEPRECATED_FLAG(Name)
59 #define FUZZER_FLAG_INT(Name, Default, Description) int Name;
60 #define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
61 #define FUZZER_FLAG_STRING(Name, Description) const char *Name;
62 #include "FuzzerFlags.def"
63 #undef FUZZER_DEPRECATED_FLAG
64 #undef FUZZER_FLAG_INT
65 #undef FUZZER_FLAG_UNSIGNED
66 #undef FUZZER_FLAG_STRING
67 } Flags;
68 
69 static const FlagDescription FlagDescriptions [] {
70 #define FUZZER_DEPRECATED_FLAG(Name)                                           \
71   {#Name, "Deprecated; don't use", 0, nullptr, nullptr, nullptr},
72 #define FUZZER_FLAG_INT(Name, Default, Description)                            \
73   {#Name, Description, Default, &Flags.Name, nullptr, nullptr},
74 #define FUZZER_FLAG_UNSIGNED(Name, Default, Description)                       \
75   {#Name,   Description, static_cast<int>(Default),                            \
76    nullptr, nullptr, &Flags.Name},
77 #define FUZZER_FLAG_STRING(Name, Description)                                  \
78   {#Name, Description, 0, nullptr, &Flags.Name, nullptr},
79 #include "FuzzerFlags.def"
80 #undef FUZZER_DEPRECATED_FLAG
81 #undef FUZZER_FLAG_INT
82 #undef FUZZER_FLAG_UNSIGNED
83 #undef FUZZER_FLAG_STRING
84 };
85 
86 static const size_t kNumFlags =
87     sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
88 
89 static Vector<std::string> *Inputs;
90 static std::string *ProgName;
91 
92 static void PrintHelp() {
93   Printf("Usage:\n");
94   auto Prog = ProgName->c_str();
95   Printf("\nTo run fuzzing pass 0 or more directories.\n");
96   Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog);
97 
98   Printf("\nTo run individual tests without fuzzing pass 1 or more files:\n");
99   Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog);
100 
101   Printf("\nFlags: (strictly in form -flag=value)\n");
102   size_t MaxFlagLen = 0;
103   for (size_t F = 0; F < kNumFlags; F++)
104     MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen);
105 
106   for (size_t F = 0; F < kNumFlags; F++) {
107     const auto &D = FlagDescriptions[F];
108     if (strstr(D.Description, "internal flag") == D.Description) continue;
109     Printf(" %s", D.Name);
110     for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
111       Printf(" ");
112     Printf("\t");
113     Printf("%d\t%s\n", D.Default, D.Description);
114   }
115   Printf("\nFlags starting with '--' will be ignored and "
116             "will be passed verbatim to subprocesses.\n");
117 }
118 
119 static const char *FlagValue(const char *Param, const char *Name) {
120   size_t Len = strlen(Name);
121   if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 &&
122       Param[Len + 1] == '=')
123       return &Param[Len + 2];
124   return nullptr;
125 }
126 
127 // Avoid calling stol as it triggers a bug in clang/glibc build.
128 static long MyStol(const char *Str) {
129   long Res = 0;
130   long Sign = 1;
131   if (*Str == '-') {
132     Str++;
133     Sign = -1;
134   }
135   for (size_t i = 0; Str[i]; i++) {
136     char Ch = Str[i];
137     if (Ch < '0' || Ch > '9')
138       return Res;
139     Res = Res * 10 + (Ch - '0');
140   }
141   return Res * Sign;
142 }
143 
144 static bool ParseOneFlag(const char *Param) {
145   if (Param[0] != '-') return false;
146   if (Param[1] == '-') {
147     static bool PrintedWarning = false;
148     if (!PrintedWarning) {
149       PrintedWarning = true;
150       Printf("INFO: libFuzzer ignores flags that start with '--'\n");
151     }
152     for (size_t F = 0; F < kNumFlags; F++)
153       if (FlagValue(Param + 1, FlagDescriptions[F].Name))
154         Printf("WARNING: did you mean '%s' (single dash)?\n", Param + 1);
155     return true;
156   }
157   for (size_t F = 0; F < kNumFlags; F++) {
158     const char *Name = FlagDescriptions[F].Name;
159     const char *Str = FlagValue(Param, Name);
160     if (Str)  {
161       if (FlagDescriptions[F].IntFlag) {
162         int Val = MyStol(Str);
163         *FlagDescriptions[F].IntFlag = Val;
164         if (Flags.verbosity >= 2)
165           Printf("Flag: %s %d\n", Name, Val);
166         return true;
167       } else if (FlagDescriptions[F].UIntFlag) {
168         unsigned int Val = std::stoul(Str);
169         *FlagDescriptions[F].UIntFlag = Val;
170         if (Flags.verbosity >= 2)
171           Printf("Flag: %s %u\n", Name, Val);
172         return true;
173       } else if (FlagDescriptions[F].StrFlag) {
174         *FlagDescriptions[F].StrFlag = Str;
175         if (Flags.verbosity >= 2)
176           Printf("Flag: %s %s\n", Name, Str);
177         return true;
178       } else {  // Deprecated flag.
179         Printf("Flag: %s: deprecated, don't use\n", Name);
180         return true;
181       }
182     }
183   }
184   Printf("\n\nWARNING: unrecognized flag '%s'; "
185          "use -help=1 to list all flags\n\n", Param);
186   return true;
187 }
188 
189 // We don't use any library to minimize dependencies.
190 static void ParseFlags(const Vector<std::string> &Args,
191                        const ExternalFunctions *EF) {
192   for (size_t F = 0; F < kNumFlags; F++) {
193     if (FlagDescriptions[F].IntFlag)
194       *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
195     if (FlagDescriptions[F].UIntFlag)
196       *FlagDescriptions[F].UIntFlag =
197           static_cast<unsigned int>(FlagDescriptions[F].Default);
198     if (FlagDescriptions[F].StrFlag)
199       *FlagDescriptions[F].StrFlag = nullptr;
200   }
201 
202   // Disable len_control by default, if LLVMFuzzerCustomMutator is used.
203   if (EF->LLVMFuzzerCustomMutator) {
204     Flags.len_control = 0;
205     Printf("INFO: found LLVMFuzzerCustomMutator (%p). "
206            "Disabling -len_control by default.\n", EF->LLVMFuzzerCustomMutator);
207   }
208 
209   Inputs = new Vector<std::string>;
210   for (size_t A = 1; A < Args.size(); A++) {
211     if (ParseOneFlag(Args[A].c_str())) {
212       if (Flags.ignore_remaining_args)
213         break;
214       continue;
215     }
216     Inputs->push_back(Args[A]);
217   }
218 }
219 
220 static std::mutex Mu;
221 
222 static void PulseThread() {
223   while (true) {
224     SleepSeconds(600);
225     std::lock_guard<std::mutex> Lock(Mu);
226     Printf("pulse...\n");
227   }
228 }
229 
230 static void WorkerThread(const Command &BaseCmd, std::atomic<unsigned> *Counter,
231                          unsigned NumJobs, std::atomic<bool> *HasErrors) {
232   while (true) {
233     unsigned C = (*Counter)++;
234     if (C >= NumJobs) break;
235     std::string Log = "fuzz-" + std::to_string(C) + ".log";
236     Command Cmd(BaseCmd);
237     Cmd.setOutputFile(Log);
238     Cmd.combineOutAndErr();
239     if (Flags.verbosity) {
240       std::string CommandLine = Cmd.toString();
241       Printf("%s\n", CommandLine.c_str());
242     }
243     int ExitCode = ExecuteCommand(Cmd);
244     if (ExitCode != 0)
245       *HasErrors = true;
246     std::lock_guard<std::mutex> Lock(Mu);
247     Printf("================== Job %u exited with exit code %d ============\n",
248            C, ExitCode);
249     fuzzer::CopyFileToErr(Log);
250   }
251 }
252 
253 static void ValidateDirectoryExists(const std::string &Path) {
254   if (!Path.empty() && !IsDirectory(Path)) {
255     Printf("ERROR: The required directory \"%s\" does not exist\n", Path.c_str());
256     exit(1);
257   }
258 }
259 
260 std::string CloneArgsWithoutX(const Vector<std::string> &Args,
261                               const char *X1, const char *X2) {
262   std::string Cmd;
263   for (auto &S : Args) {
264     if (FlagValue(S.c_str(), X1) || FlagValue(S.c_str(), X2))
265       continue;
266     Cmd += S + " ";
267   }
268   return Cmd;
269 }
270 
271 static int RunInMultipleProcesses(const Vector<std::string> &Args,
272                                   unsigned NumWorkers, unsigned NumJobs) {
273   std::atomic<unsigned> Counter(0);
274   std::atomic<bool> HasErrors(false);
275   Command Cmd(Args);
276   Cmd.removeFlag("jobs");
277   Cmd.removeFlag("workers");
278   Vector<std::thread> V;
279   std::thread Pulse(PulseThread);
280   Pulse.detach();
281   for (unsigned i = 0; i < NumWorkers; i++)
282     V.push_back(std::thread(WorkerThread, std::ref(Cmd), &Counter, NumJobs, &HasErrors));
283   for (auto &T : V)
284     T.join();
285   return HasErrors ? 1 : 0;
286 }
287 
288 static void RssThread(Fuzzer *F, size_t RssLimitMb) {
289   while (true) {
290     SleepSeconds(1);
291     size_t Peak = GetPeakRSSMb();
292     if (Peak > RssLimitMb)
293       F->RssLimitCallback();
294   }
295 }
296 
297 static void StartRssThread(Fuzzer *F, size_t RssLimitMb) {
298   if (!RssLimitMb)
299     return;
300   std::thread T(RssThread, F, RssLimitMb);
301   T.detach();
302 }
303 
304 int RunOneTest(Fuzzer *F, const char *InputFilePath, size_t MaxLen) {
305   Unit U = FileToVector(InputFilePath);
306   if (MaxLen && MaxLen < U.size())
307     U.resize(MaxLen);
308   F->ExecuteCallback(U.data(), U.size());
309   F->TryDetectingAMemoryLeak(U.data(), U.size(), true);
310   return 0;
311 }
312 
313 static bool AllInputsAreFiles() {
314   if (Inputs->empty()) return false;
315   for (auto &Path : *Inputs)
316     if (!IsFile(Path))
317       return false;
318   return true;
319 }
320 
321 static std::string GetDedupTokenFromCmdOutput(const std::string &S) {
322   auto Beg = S.find("DEDUP_TOKEN:");
323   if (Beg == std::string::npos)
324     return "";
325   auto End = S.find('\n', Beg);
326   if (End == std::string::npos)
327     return "";
328   return S.substr(Beg, End - Beg);
329 }
330 
331 int CleanseCrashInput(const Vector<std::string> &Args,
332                        const FuzzingOptions &Options) {
333   if (Inputs->size() != 1 || !Flags.exact_artifact_path) {
334     Printf("ERROR: -cleanse_crash should be given one input file and"
335           " -exact_artifact_path\n");
336     exit(1);
337   }
338   std::string InputFilePath = Inputs->at(0);
339   std::string OutputFilePath = Flags.exact_artifact_path;
340   Command Cmd(Args);
341   Cmd.removeFlag("cleanse_crash");
342 
343   assert(Cmd.hasArgument(InputFilePath));
344   Cmd.removeArgument(InputFilePath);
345 
346   auto TmpFilePath = TempPath("CleanseCrashInput", ".repro");
347   Cmd.addArgument(TmpFilePath);
348   Cmd.setOutputFile(getDevNull());
349   Cmd.combineOutAndErr();
350 
351   std::string CurrentFilePath = InputFilePath;
352   auto U = FileToVector(CurrentFilePath);
353   size_t Size = U.size();
354 
355   const Vector<uint8_t> ReplacementBytes = {' ', 0xff};
356   for (int NumAttempts = 0; NumAttempts < 5; NumAttempts++) {
357     bool Changed = false;
358     for (size_t Idx = 0; Idx < Size; Idx++) {
359       Printf("CLEANSE[%d]: Trying to replace byte %zd of %zd\n", NumAttempts,
360              Idx, Size);
361       uint8_t OriginalByte = U[Idx];
362       if (ReplacementBytes.end() != std::find(ReplacementBytes.begin(),
363                                               ReplacementBytes.end(),
364                                               OriginalByte))
365         continue;
366       for (auto NewByte : ReplacementBytes) {
367         U[Idx] = NewByte;
368         WriteToFile(U, TmpFilePath);
369         auto ExitCode = ExecuteCommand(Cmd);
370         RemoveFile(TmpFilePath);
371         if (!ExitCode) {
372           U[Idx] = OriginalByte;
373         } else {
374           Changed = true;
375           Printf("CLEANSE: Replaced byte %zd with 0x%x\n", Idx, NewByte);
376           WriteToFile(U, OutputFilePath);
377           break;
378         }
379       }
380     }
381     if (!Changed) break;
382   }
383   return 0;
384 }
385 
386 int MinimizeCrashInput(const Vector<std::string> &Args,
387                        const FuzzingOptions &Options) {
388   if (Inputs->size() != 1) {
389     Printf("ERROR: -minimize_crash should be given one input file\n");
390     exit(1);
391   }
392   std::string InputFilePath = Inputs->at(0);
393   Command BaseCmd(Args);
394   BaseCmd.removeFlag("minimize_crash");
395   BaseCmd.removeFlag("exact_artifact_path");
396   assert(BaseCmd.hasArgument(InputFilePath));
397   BaseCmd.removeArgument(InputFilePath);
398   if (Flags.runs <= 0 && Flags.max_total_time == 0) {
399     Printf("INFO: you need to specify -runs=N or "
400            "-max_total_time=N with -minimize_crash=1\n"
401            "INFO: defaulting to -max_total_time=600\n");
402     BaseCmd.addFlag("max_total_time", "600");
403   }
404 
405   BaseCmd.combineOutAndErr();
406 
407   std::string CurrentFilePath = InputFilePath;
408   while (true) {
409     Unit U = FileToVector(CurrentFilePath);
410     Printf("CRASH_MIN: minimizing crash input: '%s' (%zd bytes)\n",
411            CurrentFilePath.c_str(), U.size());
412 
413     Command Cmd(BaseCmd);
414     Cmd.addArgument(CurrentFilePath);
415 
416     Printf("CRASH_MIN: executing: %s\n", Cmd.toString().c_str());
417     std::string CmdOutput;
418     bool Success = ExecuteCommand(Cmd, &CmdOutput);
419     if (Success) {
420       Printf("ERROR: the input %s did not crash\n", CurrentFilePath.c_str());
421       exit(1);
422     }
423     Printf("CRASH_MIN: '%s' (%zd bytes) caused a crash. Will try to minimize "
424            "it further\n",
425            CurrentFilePath.c_str(), U.size());
426     auto DedupToken1 = GetDedupTokenFromCmdOutput(CmdOutput);
427     if (!DedupToken1.empty())
428       Printf("CRASH_MIN: DedupToken1: %s\n", DedupToken1.c_str());
429 
430     std::string ArtifactPath =
431         Flags.exact_artifact_path
432             ? Flags.exact_artifact_path
433             : Options.ArtifactPrefix + "minimized-from-" + Hash(U);
434     Cmd.addFlag("minimize_crash_internal_step", "1");
435     Cmd.addFlag("exact_artifact_path", ArtifactPath);
436     Printf("CRASH_MIN: executing: %s\n", Cmd.toString().c_str());
437     CmdOutput.clear();
438     Success = ExecuteCommand(Cmd, &CmdOutput);
439     Printf("%s", CmdOutput.c_str());
440     if (Success) {
441       if (Flags.exact_artifact_path) {
442         CurrentFilePath = Flags.exact_artifact_path;
443         WriteToFile(U, CurrentFilePath);
444       }
445       Printf("CRASH_MIN: failed to minimize beyond %s (%d bytes), exiting\n",
446              CurrentFilePath.c_str(), U.size());
447       break;
448     }
449     auto DedupToken2 = GetDedupTokenFromCmdOutput(CmdOutput);
450     if (!DedupToken2.empty())
451       Printf("CRASH_MIN: DedupToken2: %s\n", DedupToken2.c_str());
452 
453     if (DedupToken1 != DedupToken2) {
454       if (Flags.exact_artifact_path) {
455         CurrentFilePath = Flags.exact_artifact_path;
456         WriteToFile(U, CurrentFilePath);
457       }
458       Printf("CRASH_MIN: mismatch in dedup tokens"
459              " (looks like a different bug). Won't minimize further\n");
460       break;
461     }
462 
463     CurrentFilePath = ArtifactPath;
464     Printf("*********************************\n");
465   }
466   return 0;
467 }
468 
469 int MinimizeCrashInputInternalStep(Fuzzer *F, InputCorpus *Corpus) {
470   assert(Inputs->size() == 1);
471   std::string InputFilePath = Inputs->at(0);
472   Unit U = FileToVector(InputFilePath);
473   Printf("INFO: Starting MinimizeCrashInputInternalStep: %zd\n", U.size());
474   if (U.size() < 2) {
475     Printf("INFO: The input is small enough, exiting\n");
476     exit(0);
477   }
478   F->SetMaxInputLen(U.size());
479   F->SetMaxMutationLen(U.size() - 1);
480   F->MinimizeCrashLoop(U);
481   Printf("INFO: Done MinimizeCrashInputInternalStep, no crashes found\n");
482   exit(0);
483   return 0;
484 }
485 
486 void Merge(Fuzzer *F, FuzzingOptions &Options, const Vector<std::string> &Args,
487            const Vector<std::string> &Corpora, const char *CFPathOrNull) {
488   if (Corpora.size() < 2) {
489     Printf("INFO: Merge requires two or more corpus dirs\n");
490     exit(0);
491   }
492 
493   Vector<SizedFile> OldCorpus, NewCorpus;
494   GetSizedFilesFromDir(Corpora[0], &OldCorpus);
495   for (size_t i = 1; i < Corpora.size(); i++)
496     GetSizedFilesFromDir(Corpora[i], &NewCorpus);
497   std::sort(OldCorpus.begin(), OldCorpus.end());
498   std::sort(NewCorpus.begin(), NewCorpus.end());
499 
500   std::string CFPath = CFPathOrNull ? CFPathOrNull : TempPath("Merge", ".txt");
501   Vector<std::string> NewFiles;
502   Set<uint32_t> NewFeatures, NewCov;
503   CrashResistantMerge(Args, OldCorpus, NewCorpus, &NewFiles, {}, &NewFeatures,
504                       {}, &NewCov, CFPath, true);
505   for (auto &Path : NewFiles)
506     F->WriteToOutputCorpus(FileToVector(Path, Options.MaxLen));
507   // We are done, delete the control file if it was a temporary one.
508   if (!Flags.merge_control_file)
509     RemoveFile(CFPath);
510 
511   exit(0);
512 }
513 
514 int AnalyzeDictionary(Fuzzer *F, const Vector<Unit>& Dict,
515                       UnitVector& Corpus) {
516   Printf("Started dictionary minimization (up to %d tests)\n",
517          Dict.size() * Corpus.size() * 2);
518 
519   // Scores and usage count for each dictionary unit.
520   Vector<int> Scores(Dict.size());
521   Vector<int> Usages(Dict.size());
522 
523   Vector<size_t> InitialFeatures;
524   Vector<size_t> ModifiedFeatures;
525   for (auto &C : Corpus) {
526     // Get coverage for the testcase without modifications.
527     F->ExecuteCallback(C.data(), C.size());
528     InitialFeatures.clear();
529     TPC.CollectFeatures([&](size_t Feature) {
530       InitialFeatures.push_back(Feature);
531     });
532 
533     for (size_t i = 0; i < Dict.size(); ++i) {
534       Vector<uint8_t> Data = C;
535       auto StartPos = std::search(Data.begin(), Data.end(),
536                                   Dict[i].begin(), Dict[i].end());
537       // Skip dictionary unit, if the testcase does not contain it.
538       if (StartPos == Data.end())
539         continue;
540 
541       ++Usages[i];
542       while (StartPos != Data.end()) {
543         // Replace all occurrences of dictionary unit in the testcase.
544         auto EndPos = StartPos + Dict[i].size();
545         for (auto It = StartPos; It != EndPos; ++It)
546           *It ^= 0xFF;
547 
548         StartPos = std::search(EndPos, Data.end(),
549                                Dict[i].begin(), Dict[i].end());
550       }
551 
552       // Get coverage for testcase with masked occurrences of dictionary unit.
553       F->ExecuteCallback(Data.data(), Data.size());
554       ModifiedFeatures.clear();
555       TPC.CollectFeatures([&](size_t Feature) {
556         ModifiedFeatures.push_back(Feature);
557       });
558 
559       if (InitialFeatures == ModifiedFeatures)
560         --Scores[i];
561       else
562         Scores[i] += 2;
563     }
564   }
565 
566   Printf("###### Useless dictionary elements. ######\n");
567   for (size_t i = 0; i < Dict.size(); ++i) {
568     // Dictionary units with positive score are treated as useful ones.
569     if (Scores[i] > 0)
570        continue;
571 
572     Printf("\"");
573     PrintASCII(Dict[i].data(), Dict[i].size(), "\"");
574     Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i]);
575   }
576   Printf("###### End of useless dictionary elements. ######\n");
577   return 0;
578 }
579 
580 Vector<std::string> ParseSeedInuts(const char *seed_inputs) {
581   // Parse -seed_inputs=file1,file2,... or -seed_inputs=@seed_inputs_file
582   Vector<std::string> Files;
583   if (!seed_inputs) return Files;
584   std::string SeedInputs;
585   if (Flags.seed_inputs[0] == '@')
586     SeedInputs = FileToString(Flags.seed_inputs + 1); // File contains list.
587   else
588     SeedInputs = Flags.seed_inputs; // seed_inputs contains the list.
589   if (SeedInputs.empty()) {
590     Printf("seed_inputs is empty or @file does not exist.\n");
591     exit(1);
592   }
593   // Parse SeedInputs.
594   size_t comma_pos = 0;
595   while ((comma_pos = SeedInputs.find_last_of(',')) != std::string::npos) {
596     Files.push_back(SeedInputs.substr(comma_pos + 1));
597     SeedInputs = SeedInputs.substr(0, comma_pos);
598   }
599   Files.push_back(SeedInputs);
600   return Files;
601 }
602 
603 static Vector<SizedFile> ReadCorpora(const Vector<std::string> &CorpusDirs,
604     const Vector<std::string> &ExtraSeedFiles) {
605   Vector<SizedFile> SizedFiles;
606   size_t LastNumFiles = 0;
607   for (auto &Dir : CorpusDirs) {
608     GetSizedFilesFromDir(Dir, &SizedFiles);
609     Printf("INFO: % 8zd files found in %s\n", SizedFiles.size() - LastNumFiles,
610            Dir.c_str());
611     LastNumFiles = SizedFiles.size();
612   }
613   for (auto &File : ExtraSeedFiles)
614     if (auto Size = FileSize(File))
615       SizedFiles.push_back({File, Size});
616   return SizedFiles;
617 }
618 
619 int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
620   using namespace fuzzer;
621   assert(argc && argv && "Argument pointers cannot be nullptr");
622   std::string Argv0((*argv)[0]);
623   EF = new ExternalFunctions();
624   if (EF->LLVMFuzzerInitialize)
625     EF->LLVMFuzzerInitialize(argc, argv);
626   if (EF->__msan_scoped_disable_interceptor_checks)
627     EF->__msan_scoped_disable_interceptor_checks();
628   const Vector<std::string> Args(*argv, *argv + *argc);
629   assert(!Args.empty());
630   ProgName = new std::string(Args[0]);
631   if (Argv0 != *ProgName) {
632     Printf("ERROR: argv[0] has been modified in LLVMFuzzerInitialize\n");
633     exit(1);
634   }
635   ParseFlags(Args, EF);
636   if (Flags.help) {
637     PrintHelp();
638     return 0;
639   }
640 
641   if (Flags.close_fd_mask & 2)
642     DupAndCloseStderr();
643   if (Flags.close_fd_mask & 1)
644     CloseStdout();
645 
646   if (Flags.jobs > 0 && Flags.workers == 0) {
647     Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
648     if (Flags.workers > 1)
649       Printf("Running %u workers\n", Flags.workers);
650   }
651 
652   if (Flags.workers > 0 && Flags.jobs > 0)
653     return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
654 
655   FuzzingOptions Options;
656   Options.Verbosity = Flags.verbosity;
657   Options.MaxLen = Flags.max_len;
658   Options.LenControl = Flags.len_control;
659   Options.UnitTimeoutSec = Flags.timeout;
660   Options.ErrorExitCode = Flags.error_exitcode;
661   Options.TimeoutExitCode = Flags.timeout_exitcode;
662   Options.IgnoreTimeouts = Flags.ignore_timeouts;
663   Options.IgnoreOOMs = Flags.ignore_ooms;
664   Options.IgnoreCrashes = Flags.ignore_crashes;
665   Options.MaxTotalTimeSec = Flags.max_total_time;
666   Options.DoCrossOver = Flags.cross_over;
667   Options.MutateDepth = Flags.mutate_depth;
668   Options.ReduceDepth = Flags.reduce_depth;
669   Options.UseCounters = Flags.use_counters;
670   Options.UseMemmem = Flags.use_memmem;
671   Options.UseCmp = Flags.use_cmp;
672   Options.UseValueProfile = Flags.use_value_profile;
673   Options.Shrink = Flags.shrink;
674   Options.ReduceInputs = Flags.reduce_inputs;
675   Options.ShuffleAtStartUp = Flags.shuffle;
676   Options.PreferSmall = Flags.prefer_small;
677   Options.ReloadIntervalSec = Flags.reload;
678   Options.OnlyASCII = Flags.only_ascii;
679   Options.DetectLeaks = Flags.detect_leaks;
680   Options.PurgeAllocatorIntervalSec = Flags.purge_allocator_interval;
681   Options.TraceMalloc = Flags.trace_malloc;
682   Options.RssLimitMb = Flags.rss_limit_mb;
683   Options.MallocLimitMb = Flags.malloc_limit_mb;
684   if (!Options.MallocLimitMb)
685     Options.MallocLimitMb = Options.RssLimitMb;
686   if (Flags.runs >= 0)
687     Options.MaxNumberOfRuns = Flags.runs;
688   if (!Inputs->empty() && !Flags.minimize_crash_internal_step) {
689     // Ensure output corpus assumed to be the first arbitrary argument input
690     // is not a path to an existing file.
691     std::string OutputCorpusDir = (*Inputs)[0];
692     if (!IsFile(OutputCorpusDir)) {
693       Options.OutputCorpus = OutputCorpusDir;
694       ValidateDirectoryExists(Options.OutputCorpus);
695     }
696   }
697   Options.ReportSlowUnits = Flags.report_slow_units;
698   if (Flags.artifact_prefix) {
699     Options.ArtifactPrefix = Flags.artifact_prefix;
700 
701     // Since the prefix could be a full path to a file name prefix, assume
702     // that if the path ends with the platform's separator that a directory
703     // is desired
704     std::string ArtifactPathDir = Options.ArtifactPrefix;
705     if (!IsSeparator(ArtifactPathDir[ArtifactPathDir.length() - 1])) {
706       ArtifactPathDir = DirName(ArtifactPathDir);
707     }
708     ValidateDirectoryExists(ArtifactPathDir);
709   }
710   if (Flags.exact_artifact_path) {
711     Options.ExactArtifactPath = Flags.exact_artifact_path;
712     ValidateDirectoryExists(DirName(Options.ExactArtifactPath));
713   }
714   Vector<Unit> Dictionary;
715   if (Flags.dict)
716     if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
717       return 1;
718   if (Flags.verbosity > 0 && !Dictionary.empty())
719     Printf("Dictionary: %zd entries\n", Dictionary.size());
720   bool RunIndividualFiles = AllInputsAreFiles();
721   Options.SaveArtifacts =
722       !RunIndividualFiles || Flags.minimize_crash_internal_step;
723   Options.PrintNewCovPcs = Flags.print_pcs;
724   Options.PrintNewCovFuncs = Flags.print_funcs;
725   Options.PrintFinalStats = Flags.print_final_stats;
726   Options.PrintCorpusStats = Flags.print_corpus_stats;
727   Options.PrintCoverage = Flags.print_coverage;
728   if (Flags.exit_on_src_pos)
729     Options.ExitOnSrcPos = Flags.exit_on_src_pos;
730   if (Flags.exit_on_item)
731     Options.ExitOnItem = Flags.exit_on_item;
732   if (Flags.focus_function)
733     Options.FocusFunction = Flags.focus_function;
734   if (Flags.data_flow_trace)
735     Options.DataFlowTrace = Flags.data_flow_trace;
736   if (Flags.features_dir) {
737     Options.FeaturesDir = Flags.features_dir;
738     ValidateDirectoryExists(Options.FeaturesDir);
739   }
740   if (Flags.collect_data_flow)
741     Options.CollectDataFlow = Flags.collect_data_flow;
742   if (Flags.stop_file)
743     Options.StopFile = Flags.stop_file;
744   Options.Entropic = Flags.entropic;
745   Options.EntropicFeatureFrequencyThreshold =
746       (size_t)Flags.entropic_feature_frequency_threshold;
747   Options.EntropicNumberOfRarestFeatures =
748       (size_t)Flags.entropic_number_of_rarest_features;
749   if (Options.Entropic) {
750     if (!Options.FocusFunction.empty()) {
751       Printf("ERROR: The parameters `--entropic` and `--focus_function` cannot "
752              "be used together.\n");
753       exit(1);
754     }
755     Printf("INFO: Running with entropic power schedule (0x%X, %d).\n",
756            Options.EntropicFeatureFrequencyThreshold,
757            Options.EntropicNumberOfRarestFeatures);
758   }
759   struct EntropicOptions Entropic;
760   Entropic.Enabled = Options.Entropic;
761   Entropic.FeatureFrequencyThreshold =
762       Options.EntropicFeatureFrequencyThreshold;
763   Entropic.NumberOfRarestFeatures = Options.EntropicNumberOfRarestFeatures;
764 
765   unsigned Seed = Flags.seed;
766   // Initialize Seed.
767   if (Seed == 0)
768     Seed =
769         std::chrono::system_clock::now().time_since_epoch().count() + GetPid();
770   if (Flags.verbosity)
771     Printf("INFO: Seed: %u\n", Seed);
772 
773   if (Flags.collect_data_flow && !Flags.fork && !Flags.merge) {
774     if (RunIndividualFiles)
775       return CollectDataFlow(Flags.collect_data_flow, Flags.data_flow_trace,
776                         ReadCorpora({}, *Inputs));
777     else
778       return CollectDataFlow(Flags.collect_data_flow, Flags.data_flow_trace,
779                         ReadCorpora(*Inputs, {}));
780   }
781 
782   Random Rand(Seed);
783   auto *MD = new MutationDispatcher(Rand, Options);
784   auto *Corpus = new InputCorpus(Options.OutputCorpus, Entropic);
785   auto *F = new Fuzzer(Callback, *Corpus, *MD, Options);
786 
787   for (auto &U: Dictionary)
788     if (U.size() <= Word::GetMaxSize())
789       MD->AddWordToManualDictionary(Word(U.data(), U.size()));
790 
791       // Threads are only supported by Chrome. Don't use them with emscripten
792       // for now.
793 #if !LIBFUZZER_EMSCRIPTEN
794   StartRssThread(F, Flags.rss_limit_mb);
795 #endif // LIBFUZZER_EMSCRIPTEN
796 
797   Options.HandleAbrt = Flags.handle_abrt;
798   Options.HandleAlrm = !Flags.minimize_crash;
799   Options.HandleBus = Flags.handle_bus;
800   Options.HandleFpe = Flags.handle_fpe;
801   Options.HandleIll = Flags.handle_ill;
802   Options.HandleInt = Flags.handle_int;
803   Options.HandleSegv = Flags.handle_segv;
804   Options.HandleTerm = Flags.handle_term;
805   Options.HandleXfsz = Flags.handle_xfsz;
806   Options.HandleUsr1 = Flags.handle_usr1;
807   Options.HandleUsr2 = Flags.handle_usr2;
808   SetSignalHandler(Options);
809 
810   std::atexit(Fuzzer::StaticExitCallback);
811 
812   if (Flags.minimize_crash)
813     return MinimizeCrashInput(Args, Options);
814 
815   if (Flags.minimize_crash_internal_step)
816     return MinimizeCrashInputInternalStep(F, Corpus);
817 
818   if (Flags.cleanse_crash)
819     return CleanseCrashInput(Args, Options);
820 
821   if (RunIndividualFiles) {
822     Options.SaveArtifacts = false;
823     int Runs = std::max(1, Flags.runs);
824     Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
825            Inputs->size(), Runs);
826     for (auto &Path : *Inputs) {
827       auto StartTime = system_clock::now();
828       Printf("Running: %s\n", Path.c_str());
829       for (int Iter = 0; Iter < Runs; Iter++)
830         RunOneTest(F, Path.c_str(), Options.MaxLen);
831       auto StopTime = system_clock::now();
832       auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
833       Printf("Executed %s in %zd ms\n", Path.c_str(), (long)MS);
834     }
835     Printf("***\n"
836            "*** NOTE: fuzzing was not performed, you have only\n"
837            "***       executed the target code on a fixed set of inputs.\n"
838            "***\n");
839     F->PrintFinalStats();
840     exit(0);
841   }
842 
843   if (Flags.fork)
844     FuzzWithFork(F->GetMD().GetRand(), Options, Args, *Inputs, Flags.fork);
845 
846   if (Flags.merge)
847     Merge(F, Options, Args, *Inputs, Flags.merge_control_file);
848 
849   if (Flags.merge_inner) {
850     const size_t kDefaultMaxMergeLen = 1 << 20;
851     if (Options.MaxLen == 0)
852       F->SetMaxInputLen(kDefaultMaxMergeLen);
853     assert(Flags.merge_control_file);
854     F->CrashResistantMergeInternalStep(Flags.merge_control_file);
855     exit(0);
856   }
857 
858   if (Flags.analyze_dict) {
859     size_t MaxLen = INT_MAX;  // Large max length.
860     UnitVector InitialCorpus;
861     for (auto &Inp : *Inputs) {
862       Printf("Loading corpus dir: %s\n", Inp.c_str());
863       ReadDirToVectorOfUnits(Inp.c_str(), &InitialCorpus, nullptr,
864                              MaxLen, /*ExitOnError=*/false);
865     }
866 
867     if (Dictionary.empty() || Inputs->empty()) {
868       Printf("ERROR: can't analyze dict without dict and corpus provided\n");
869       return 1;
870     }
871     if (AnalyzeDictionary(F, Dictionary, InitialCorpus)) {
872       Printf("Dictionary analysis failed\n");
873       exit(1);
874     }
875     Printf("Dictionary analysis succeeded\n");
876     exit(0);
877   }
878 
879   auto CorporaFiles = ReadCorpora(*Inputs, ParseSeedInuts(Flags.seed_inputs));
880   F->Loop(CorporaFiles);
881 
882   if (Flags.verbosity)
883     Printf("Done %zd runs in %zd second(s)\n", F->getTotalNumberOfRuns(),
884            F->secondsSinceProcessStartUp());
885   F->PrintFinalStats();
886 
887   exit(0);  // Don't let F destroy itself.
888 }
889 
890 extern "C" ATTRIBUTE_INTERFACE int
891 LLVMFuzzerRunDriver(int *argc, char ***argv,
892                     int (*UserCb)(const uint8_t *Data, size_t Size)) {
893   return FuzzerDriver(argc, argv, UserCb);
894 }
895 
896 // Storage for global ExternalFunctions object.
897 ExternalFunctions *EF = nullptr;
898 
899 }  // namespace fuzzer
900