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         auto Val = MyStol(Str);
163         *FlagDescriptions[F].IntFlag = static_cast<int>(Val);
164         if (Flags.verbosity >= 2)
165           Printf("Flag: %s %d\n", Name, Val);
166         return true;
167       } else if (FlagDescriptions[F].UIntFlag) {
168         auto Val = std::stoul(Str);
169         *FlagDescriptions[F].UIntFlag = static_cast<unsigned int>(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                                     bool CreateDirectory) {
255   if (Path.empty()) {
256     Printf("ERROR: Provided directory path is an empty string\n");
257     exit(1);
258   }
259 
260   if (IsDirectory(Path))
261     return;
262 
263   if (CreateDirectory) {
264     if (!MkDirRecursive(Path)) {
265       Printf("ERROR: Failed to create directory \"%s\"\n", Path.c_str());
266       exit(1);
267     }
268     return;
269   }
270 
271   Printf("ERROR: The required directory \"%s\" does not exist\n", Path.c_str());
272   exit(1);
273 }
274 
275 std::string CloneArgsWithoutX(const Vector<std::string> &Args,
276                               const char *X1, const char *X2) {
277   std::string Cmd;
278   for (auto &S : Args) {
279     if (FlagValue(S.c_str(), X1) || FlagValue(S.c_str(), X2))
280       continue;
281     Cmd += S + " ";
282   }
283   return Cmd;
284 }
285 
286 static int RunInMultipleProcesses(const Vector<std::string> &Args,
287                                   unsigned NumWorkers, unsigned NumJobs) {
288   std::atomic<unsigned> Counter(0);
289   std::atomic<bool> HasErrors(false);
290   Command Cmd(Args);
291   Cmd.removeFlag("jobs");
292   Cmd.removeFlag("workers");
293   Vector<std::thread> V;
294   std::thread Pulse(PulseThread);
295   Pulse.detach();
296   for (unsigned i = 0; i < NumWorkers; i++)
297     V.push_back(std::thread(WorkerThread, std::ref(Cmd), &Counter, NumJobs, &HasErrors));
298   for (auto &T : V)
299     T.join();
300   return HasErrors ? 1 : 0;
301 }
302 
303 static void RssThread(Fuzzer *F, size_t RssLimitMb) {
304   while (true) {
305     SleepSeconds(1);
306     size_t Peak = GetPeakRSSMb();
307     if (Peak > RssLimitMb)
308       F->RssLimitCallback();
309   }
310 }
311 
312 static void StartRssThread(Fuzzer *F, size_t RssLimitMb) {
313   if (!RssLimitMb)
314     return;
315   std::thread T(RssThread, F, RssLimitMb);
316   T.detach();
317 }
318 
319 int RunOneTest(Fuzzer *F, const char *InputFilePath, size_t MaxLen) {
320   Unit U = FileToVector(InputFilePath);
321   if (MaxLen && MaxLen < U.size())
322     U.resize(MaxLen);
323   F->ExecuteCallback(U.data(), U.size());
324   if (Flags.print_full_coverage) {
325     // Leak detection is not needed when collecting full coverage data.
326     F->TPCUpdateObservedPCs();
327   } else {
328     F->TryDetectingAMemoryLeak(U.data(), U.size(), true);
329   }
330   return 0;
331 }
332 
333 static bool AllInputsAreFiles() {
334   if (Inputs->empty()) return false;
335   for (auto &Path : *Inputs)
336     if (!IsFile(Path))
337       return false;
338   return true;
339 }
340 
341 static std::string GetDedupTokenFromCmdOutput(const std::string &S) {
342   auto Beg = S.find("DEDUP_TOKEN:");
343   if (Beg == std::string::npos)
344     return "";
345   auto End = S.find('\n', Beg);
346   if (End == std::string::npos)
347     return "";
348   return S.substr(Beg, End - Beg);
349 }
350 
351 int CleanseCrashInput(const Vector<std::string> &Args,
352                        const FuzzingOptions &Options) {
353   if (Inputs->size() != 1 || !Flags.exact_artifact_path) {
354     Printf("ERROR: -cleanse_crash should be given one input file and"
355           " -exact_artifact_path\n");
356     exit(1);
357   }
358   std::string InputFilePath = Inputs->at(0);
359   std::string OutputFilePath = Flags.exact_artifact_path;
360   Command Cmd(Args);
361   Cmd.removeFlag("cleanse_crash");
362 
363   assert(Cmd.hasArgument(InputFilePath));
364   Cmd.removeArgument(InputFilePath);
365 
366   auto TmpFilePath = TempPath("CleanseCrashInput", ".repro");
367   Cmd.addArgument(TmpFilePath);
368   Cmd.setOutputFile(getDevNull());
369   Cmd.combineOutAndErr();
370 
371   std::string CurrentFilePath = InputFilePath;
372   auto U = FileToVector(CurrentFilePath);
373   size_t Size = U.size();
374 
375   const Vector<uint8_t> ReplacementBytes = {' ', 0xff};
376   for (int NumAttempts = 0; NumAttempts < 5; NumAttempts++) {
377     bool Changed = false;
378     for (size_t Idx = 0; Idx < Size; Idx++) {
379       Printf("CLEANSE[%d]: Trying to replace byte %zd of %zd\n", NumAttempts,
380              Idx, Size);
381       uint8_t OriginalByte = U[Idx];
382       if (ReplacementBytes.end() != std::find(ReplacementBytes.begin(),
383                                               ReplacementBytes.end(),
384                                               OriginalByte))
385         continue;
386       for (auto NewByte : ReplacementBytes) {
387         U[Idx] = NewByte;
388         WriteToFile(U, TmpFilePath);
389         auto ExitCode = ExecuteCommand(Cmd);
390         RemoveFile(TmpFilePath);
391         if (!ExitCode) {
392           U[Idx] = OriginalByte;
393         } else {
394           Changed = true;
395           Printf("CLEANSE: Replaced byte %zd with 0x%x\n", Idx, NewByte);
396           WriteToFile(U, OutputFilePath);
397           break;
398         }
399       }
400     }
401     if (!Changed) break;
402   }
403   return 0;
404 }
405 
406 int MinimizeCrashInput(const Vector<std::string> &Args,
407                        const FuzzingOptions &Options) {
408   if (Inputs->size() != 1) {
409     Printf("ERROR: -minimize_crash should be given one input file\n");
410     exit(1);
411   }
412   std::string InputFilePath = Inputs->at(0);
413   Command BaseCmd(Args);
414   BaseCmd.removeFlag("minimize_crash");
415   BaseCmd.removeFlag("exact_artifact_path");
416   assert(BaseCmd.hasArgument(InputFilePath));
417   BaseCmd.removeArgument(InputFilePath);
418   if (Flags.runs <= 0 && Flags.max_total_time == 0) {
419     Printf("INFO: you need to specify -runs=N or "
420            "-max_total_time=N with -minimize_crash=1\n"
421            "INFO: defaulting to -max_total_time=600\n");
422     BaseCmd.addFlag("max_total_time", "600");
423   }
424 
425   BaseCmd.combineOutAndErr();
426 
427   std::string CurrentFilePath = InputFilePath;
428   while (true) {
429     Unit U = FileToVector(CurrentFilePath);
430     Printf("CRASH_MIN: minimizing crash input: '%s' (%zd bytes)\n",
431            CurrentFilePath.c_str(), U.size());
432 
433     Command Cmd(BaseCmd);
434     Cmd.addArgument(CurrentFilePath);
435 
436     Printf("CRASH_MIN: executing: %s\n", Cmd.toString().c_str());
437     std::string CmdOutput;
438     bool Success = ExecuteCommand(Cmd, &CmdOutput);
439     if (Success) {
440       Printf("ERROR: the input %s did not crash\n", CurrentFilePath.c_str());
441       exit(1);
442     }
443     Printf("CRASH_MIN: '%s' (%zd bytes) caused a crash. Will try to minimize "
444            "it further\n",
445            CurrentFilePath.c_str(), U.size());
446     auto DedupToken1 = GetDedupTokenFromCmdOutput(CmdOutput);
447     if (!DedupToken1.empty())
448       Printf("CRASH_MIN: DedupToken1: %s\n", DedupToken1.c_str());
449 
450     std::string ArtifactPath =
451         Flags.exact_artifact_path
452             ? Flags.exact_artifact_path
453             : Options.ArtifactPrefix + "minimized-from-" + Hash(U);
454     Cmd.addFlag("minimize_crash_internal_step", "1");
455     Cmd.addFlag("exact_artifact_path", ArtifactPath);
456     Printf("CRASH_MIN: executing: %s\n", Cmd.toString().c_str());
457     CmdOutput.clear();
458     Success = ExecuteCommand(Cmd, &CmdOutput);
459     Printf("%s", CmdOutput.c_str());
460     if (Success) {
461       if (Flags.exact_artifact_path) {
462         CurrentFilePath = Flags.exact_artifact_path;
463         WriteToFile(U, CurrentFilePath);
464       }
465       Printf("CRASH_MIN: failed to minimize beyond %s (%d bytes), exiting\n",
466              CurrentFilePath.c_str(), U.size());
467       break;
468     }
469     auto DedupToken2 = GetDedupTokenFromCmdOutput(CmdOutput);
470     if (!DedupToken2.empty())
471       Printf("CRASH_MIN: DedupToken2: %s\n", DedupToken2.c_str());
472 
473     if (DedupToken1 != DedupToken2) {
474       if (Flags.exact_artifact_path) {
475         CurrentFilePath = Flags.exact_artifact_path;
476         WriteToFile(U, CurrentFilePath);
477       }
478       Printf("CRASH_MIN: mismatch in dedup tokens"
479              " (looks like a different bug). Won't minimize further\n");
480       break;
481     }
482 
483     CurrentFilePath = ArtifactPath;
484     Printf("*********************************\n");
485   }
486   return 0;
487 }
488 
489 int MinimizeCrashInputInternalStep(Fuzzer *F, InputCorpus *Corpus) {
490   assert(Inputs->size() == 1);
491   std::string InputFilePath = Inputs->at(0);
492   Unit U = FileToVector(InputFilePath);
493   Printf("INFO: Starting MinimizeCrashInputInternalStep: %zd\n", U.size());
494   if (U.size() < 2) {
495     Printf("INFO: The input is small enough, exiting\n");
496     exit(0);
497   }
498   F->SetMaxInputLen(U.size());
499   F->SetMaxMutationLen(U.size() - 1);
500   F->MinimizeCrashLoop(U);
501   Printf("INFO: Done MinimizeCrashInputInternalStep, no crashes found\n");
502   exit(0);
503   return 0;
504 }
505 
506 void Merge(Fuzzer *F, FuzzingOptions &Options, const Vector<std::string> &Args,
507            const Vector<std::string> &Corpora, const char *CFPathOrNull) {
508   if (Corpora.size() < 2) {
509     Printf("INFO: Merge requires two or more corpus dirs\n");
510     exit(0);
511   }
512 
513   Vector<SizedFile> OldCorpus, NewCorpus;
514   GetSizedFilesFromDir(Corpora[0], &OldCorpus);
515   for (size_t i = 1; i < Corpora.size(); i++)
516     GetSizedFilesFromDir(Corpora[i], &NewCorpus);
517   std::sort(OldCorpus.begin(), OldCorpus.end());
518   std::sort(NewCorpus.begin(), NewCorpus.end());
519 
520   std::string CFPath = CFPathOrNull ? CFPathOrNull : TempPath("Merge", ".txt");
521   Vector<std::string> NewFiles;
522   Set<uint32_t> NewFeatures, NewCov;
523   CrashResistantMerge(Args, OldCorpus, NewCorpus, &NewFiles, {}, &NewFeatures,
524                       {}, &NewCov, CFPath, true);
525   for (auto &Path : NewFiles)
526     F->WriteToOutputCorpus(FileToVector(Path, Options.MaxLen));
527   // We are done, delete the control file if it was a temporary one.
528   if (!Flags.merge_control_file)
529     RemoveFile(CFPath);
530 
531   exit(0);
532 }
533 
534 int AnalyzeDictionary(Fuzzer *F, const Vector<Unit>& Dict,
535                       UnitVector& Corpus) {
536   Printf("Started dictionary minimization (up to %d tests)\n",
537          Dict.size() * Corpus.size() * 2);
538 
539   // Scores and usage count for each dictionary unit.
540   Vector<int> Scores(Dict.size());
541   Vector<int> Usages(Dict.size());
542 
543   Vector<size_t> InitialFeatures;
544   Vector<size_t> ModifiedFeatures;
545   for (auto &C : Corpus) {
546     // Get coverage for the testcase without modifications.
547     F->ExecuteCallback(C.data(), C.size());
548     InitialFeatures.clear();
549     TPC.CollectFeatures([&](size_t Feature) {
550       InitialFeatures.push_back(Feature);
551     });
552 
553     for (size_t i = 0; i < Dict.size(); ++i) {
554       Vector<uint8_t> Data = C;
555       auto StartPos = std::search(Data.begin(), Data.end(),
556                                   Dict[i].begin(), Dict[i].end());
557       // Skip dictionary unit, if the testcase does not contain it.
558       if (StartPos == Data.end())
559         continue;
560 
561       ++Usages[i];
562       while (StartPos != Data.end()) {
563         // Replace all occurrences of dictionary unit in the testcase.
564         auto EndPos = StartPos + Dict[i].size();
565         for (auto It = StartPos; It != EndPos; ++It)
566           *It ^= 0xFF;
567 
568         StartPos = std::search(EndPos, Data.end(),
569                                Dict[i].begin(), Dict[i].end());
570       }
571 
572       // Get coverage for testcase with masked occurrences of dictionary unit.
573       F->ExecuteCallback(Data.data(), Data.size());
574       ModifiedFeatures.clear();
575       TPC.CollectFeatures([&](size_t Feature) {
576         ModifiedFeatures.push_back(Feature);
577       });
578 
579       if (InitialFeatures == ModifiedFeatures)
580         --Scores[i];
581       else
582         Scores[i] += 2;
583     }
584   }
585 
586   Printf("###### Useless dictionary elements. ######\n");
587   for (size_t i = 0; i < Dict.size(); ++i) {
588     // Dictionary units with positive score are treated as useful ones.
589     if (Scores[i] > 0)
590        continue;
591 
592     Printf("\"");
593     PrintASCII(Dict[i].data(), Dict[i].size(), "\"");
594     Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i]);
595   }
596   Printf("###### End of useless dictionary elements. ######\n");
597   return 0;
598 }
599 
600 Vector<std::string> ParseSeedInuts(const char *seed_inputs) {
601   // Parse -seed_inputs=file1,file2,... or -seed_inputs=@seed_inputs_file
602   Vector<std::string> Files;
603   if (!seed_inputs) return Files;
604   std::string SeedInputs;
605   if (Flags.seed_inputs[0] == '@')
606     SeedInputs = FileToString(Flags.seed_inputs + 1); // File contains list.
607   else
608     SeedInputs = Flags.seed_inputs; // seed_inputs contains the list.
609   if (SeedInputs.empty()) {
610     Printf("seed_inputs is empty or @file does not exist.\n");
611     exit(1);
612   }
613   // Parse SeedInputs.
614   size_t comma_pos = 0;
615   while ((comma_pos = SeedInputs.find_last_of(',')) != std::string::npos) {
616     Files.push_back(SeedInputs.substr(comma_pos + 1));
617     SeedInputs = SeedInputs.substr(0, comma_pos);
618   }
619   Files.push_back(SeedInputs);
620   return Files;
621 }
622 
623 static Vector<SizedFile> ReadCorpora(const Vector<std::string> &CorpusDirs,
624     const Vector<std::string> &ExtraSeedFiles) {
625   Vector<SizedFile> SizedFiles;
626   size_t LastNumFiles = 0;
627   for (auto &Dir : CorpusDirs) {
628     GetSizedFilesFromDir(Dir, &SizedFiles);
629     Printf("INFO: % 8zd files found in %s\n", SizedFiles.size() - LastNumFiles,
630            Dir.c_str());
631     LastNumFiles = SizedFiles.size();
632   }
633   for (auto &File : ExtraSeedFiles)
634     if (auto Size = FileSize(File))
635       SizedFiles.push_back({File, Size});
636   return SizedFiles;
637 }
638 
639 int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
640   using namespace fuzzer;
641   assert(argc && argv && "Argument pointers cannot be nullptr");
642   std::string Argv0((*argv)[0]);
643   EF = new ExternalFunctions();
644   if (EF->LLVMFuzzerInitialize)
645     EF->LLVMFuzzerInitialize(argc, argv);
646   if (EF->__msan_scoped_disable_interceptor_checks)
647     EF->__msan_scoped_disable_interceptor_checks();
648   const Vector<std::string> Args(*argv, *argv + *argc);
649   assert(!Args.empty());
650   ProgName = new std::string(Args[0]);
651   if (Argv0 != *ProgName) {
652     Printf("ERROR: argv[0] has been modified in LLVMFuzzerInitialize\n");
653     exit(1);
654   }
655   ParseFlags(Args, EF);
656   if (Flags.help) {
657     PrintHelp();
658     return 0;
659   }
660 
661   if (Flags.close_fd_mask & 2)
662     DupAndCloseStderr();
663   if (Flags.close_fd_mask & 1)
664     CloseStdout();
665 
666   if (Flags.jobs > 0 && Flags.workers == 0) {
667     Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
668     if (Flags.workers > 1)
669       Printf("Running %u workers\n", Flags.workers);
670   }
671 
672   if (Flags.workers > 0 && Flags.jobs > 0)
673     return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
674 
675   FuzzingOptions Options;
676   Options.Verbosity = Flags.verbosity;
677   Options.MaxLen = Flags.max_len;
678   Options.LenControl = Flags.len_control;
679   Options.KeepSeed = Flags.keep_seed;
680   Options.UnitTimeoutSec = Flags.timeout;
681   Options.ErrorExitCode = Flags.error_exitcode;
682   Options.TimeoutExitCode = Flags.timeout_exitcode;
683   Options.IgnoreTimeouts = Flags.ignore_timeouts;
684   Options.IgnoreOOMs = Flags.ignore_ooms;
685   Options.IgnoreCrashes = Flags.ignore_crashes;
686   Options.MaxTotalTimeSec = Flags.max_total_time;
687   Options.DoCrossOver = Flags.cross_over;
688   Options.CrossOverUniformDist = Flags.cross_over_uniform_dist;
689   Options.MutateDepth = Flags.mutate_depth;
690   Options.ReduceDepth = Flags.reduce_depth;
691   Options.UseCounters = Flags.use_counters;
692   Options.UseMemmem = Flags.use_memmem;
693   Options.UseCmp = Flags.use_cmp;
694   Options.UseValueProfile = Flags.use_value_profile;
695   Options.Shrink = Flags.shrink;
696   Options.ReduceInputs = Flags.reduce_inputs;
697   Options.ShuffleAtStartUp = Flags.shuffle;
698   Options.PreferSmall = Flags.prefer_small;
699   Options.ReloadIntervalSec = Flags.reload;
700   Options.OnlyASCII = Flags.only_ascii;
701   Options.DetectLeaks = Flags.detect_leaks;
702   Options.PurgeAllocatorIntervalSec = Flags.purge_allocator_interval;
703   Options.TraceMalloc = Flags.trace_malloc;
704   Options.RssLimitMb = Flags.rss_limit_mb;
705   Options.MallocLimitMb = Flags.malloc_limit_mb;
706   if (!Options.MallocLimitMb)
707     Options.MallocLimitMb = Options.RssLimitMb;
708   if (Flags.runs >= 0)
709     Options.MaxNumberOfRuns = Flags.runs;
710   if (!Inputs->empty() && !Flags.minimize_crash_internal_step) {
711     // Ensure output corpus assumed to be the first arbitrary argument input
712     // is not a path to an existing file.
713     std::string OutputCorpusDir = (*Inputs)[0];
714     if (!IsFile(OutputCorpusDir)) {
715       Options.OutputCorpus = OutputCorpusDir;
716       ValidateDirectoryExists(Options.OutputCorpus, Flags.create_missing_dirs);
717     }
718   }
719   Options.ReportSlowUnits = Flags.report_slow_units;
720   if (Flags.artifact_prefix) {
721     Options.ArtifactPrefix = Flags.artifact_prefix;
722 
723     // Since the prefix could be a full path to a file name prefix, assume
724     // that if the path ends with the platform's separator that a directory
725     // is desired
726     std::string ArtifactPathDir = Options.ArtifactPrefix;
727     if (!IsSeparator(ArtifactPathDir[ArtifactPathDir.length() - 1])) {
728       ArtifactPathDir = DirName(ArtifactPathDir);
729     }
730     ValidateDirectoryExists(ArtifactPathDir, Flags.create_missing_dirs);
731   }
732   if (Flags.exact_artifact_path) {
733     Options.ExactArtifactPath = Flags.exact_artifact_path;
734     ValidateDirectoryExists(DirName(Options.ExactArtifactPath),
735                             Flags.create_missing_dirs);
736   }
737   Vector<Unit> Dictionary;
738   if (Flags.dict)
739     if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
740       return 1;
741   if (Flags.verbosity > 0 && !Dictionary.empty())
742     Printf("Dictionary: %zd entries\n", Dictionary.size());
743   bool RunIndividualFiles = AllInputsAreFiles();
744   Options.SaveArtifacts =
745       !RunIndividualFiles || Flags.minimize_crash_internal_step;
746   Options.PrintNewCovPcs = Flags.print_pcs;
747   Options.PrintNewCovFuncs = Flags.print_funcs;
748   Options.PrintFinalStats = Flags.print_final_stats;
749   Options.PrintCorpusStats = Flags.print_corpus_stats;
750   Options.PrintCoverage = Flags.print_coverage;
751   Options.PrintFullCoverage = Flags.print_full_coverage;
752   if (Flags.exit_on_src_pos)
753     Options.ExitOnSrcPos = Flags.exit_on_src_pos;
754   if (Flags.exit_on_item)
755     Options.ExitOnItem = Flags.exit_on_item;
756   if (Flags.focus_function)
757     Options.FocusFunction = Flags.focus_function;
758   if (Flags.data_flow_trace)
759     Options.DataFlowTrace = Flags.data_flow_trace;
760   if (Flags.features_dir) {
761     Options.FeaturesDir = Flags.features_dir;
762     ValidateDirectoryExists(Options.FeaturesDir, Flags.create_missing_dirs);
763   }
764   if (Flags.mutation_graph_file)
765     Options.MutationGraphFile = Flags.mutation_graph_file;
766   if (Flags.collect_data_flow)
767     Options.CollectDataFlow = Flags.collect_data_flow;
768   if (Flags.stop_file)
769     Options.StopFile = Flags.stop_file;
770   Options.Entropic = Flags.entropic;
771   Options.EntropicFeatureFrequencyThreshold =
772       (size_t)Flags.entropic_feature_frequency_threshold;
773   Options.EntropicNumberOfRarestFeatures =
774       (size_t)Flags.entropic_number_of_rarest_features;
775   Options.EntropicScalePerExecTime = Flags.entropic_scale_per_exec_time;
776   if (!Options.FocusFunction.empty())
777     Options.Entropic = false; // FocusFunction overrides entropic scheduling.
778   if (Options.Entropic)
779     Printf("INFO: Running with entropic power schedule (0x%X, %d).\n",
780            Options.EntropicFeatureFrequencyThreshold,
781            Options.EntropicNumberOfRarestFeatures);
782   struct EntropicOptions Entropic;
783   Entropic.Enabled = Options.Entropic;
784   Entropic.FeatureFrequencyThreshold =
785       Options.EntropicFeatureFrequencyThreshold;
786   Entropic.NumberOfRarestFeatures = Options.EntropicNumberOfRarestFeatures;
787   Entropic.ScalePerExecTime = Options.EntropicScalePerExecTime;
788 
789   unsigned Seed = Flags.seed;
790   // Initialize Seed.
791   if (Seed == 0)
792     Seed = static_cast<unsigned>(
793         std::chrono::system_clock::now().time_since_epoch().count() + GetPid());
794   if (Flags.verbosity)
795     Printf("INFO: Seed: %u\n", Seed);
796 
797   if (Flags.collect_data_flow && !Flags.fork && !Flags.merge) {
798     if (RunIndividualFiles)
799       return CollectDataFlow(Flags.collect_data_flow, Flags.data_flow_trace,
800                         ReadCorpora({}, *Inputs));
801     else
802       return CollectDataFlow(Flags.collect_data_flow, Flags.data_flow_trace,
803                         ReadCorpora(*Inputs, {}));
804   }
805 
806   Random Rand(Seed);
807   auto *MD = new MutationDispatcher(Rand, Options);
808   auto *Corpus = new InputCorpus(Options.OutputCorpus, Entropic);
809   auto *F = new Fuzzer(Callback, *Corpus, *MD, Options);
810 
811   for (auto &U: Dictionary)
812     if (U.size() <= Word::GetMaxSize())
813       MD->AddWordToManualDictionary(Word(U.data(), U.size()));
814 
815       // Threads are only supported by Chrome. Don't use them with emscripten
816       // for now.
817 #if !LIBFUZZER_EMSCRIPTEN
818   StartRssThread(F, Flags.rss_limit_mb);
819 #endif // LIBFUZZER_EMSCRIPTEN
820 
821   Options.HandleAbrt = Flags.handle_abrt;
822   Options.HandleAlrm = !Flags.minimize_crash;
823   Options.HandleBus = Flags.handle_bus;
824   Options.HandleFpe = Flags.handle_fpe;
825   Options.HandleIll = Flags.handle_ill;
826   Options.HandleInt = Flags.handle_int;
827   Options.HandleSegv = Flags.handle_segv;
828   Options.HandleTerm = Flags.handle_term;
829   Options.HandleXfsz = Flags.handle_xfsz;
830   Options.HandleUsr1 = Flags.handle_usr1;
831   Options.HandleUsr2 = Flags.handle_usr2;
832   Options.HandleWinExcept = Flags.handle_winexcept;
833 
834   SetSignalHandler(Options);
835 
836   std::atexit(Fuzzer::StaticExitCallback);
837 
838   if (Flags.minimize_crash)
839     return MinimizeCrashInput(Args, Options);
840 
841   if (Flags.minimize_crash_internal_step)
842     return MinimizeCrashInputInternalStep(F, Corpus);
843 
844   if (Flags.cleanse_crash)
845     return CleanseCrashInput(Args, Options);
846 
847   if (RunIndividualFiles) {
848     Options.SaveArtifacts = false;
849     int Runs = std::max(1, Flags.runs);
850     Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
851            Inputs->size(), Runs);
852     for (auto &Path : *Inputs) {
853       auto StartTime = system_clock::now();
854       Printf("Running: %s\n", Path.c_str());
855       for (int Iter = 0; Iter < Runs; Iter++)
856         RunOneTest(F, Path.c_str(), Options.MaxLen);
857       auto StopTime = system_clock::now();
858       auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
859       Printf("Executed %s in %zd ms\n", Path.c_str(), (long)MS);
860     }
861     Printf("***\n"
862            "*** NOTE: fuzzing was not performed, you have only\n"
863            "***       executed the target code on a fixed set of inputs.\n"
864            "***\n");
865     F->PrintFinalStats();
866     exit(0);
867   }
868 
869   if (Flags.fork)
870     FuzzWithFork(F->GetMD().GetRand(), Options, Args, *Inputs, Flags.fork);
871 
872   if (Flags.merge)
873     Merge(F, Options, Args, *Inputs, Flags.merge_control_file);
874 
875   if (Flags.merge_inner) {
876     const size_t kDefaultMaxMergeLen = 1 << 20;
877     if (Options.MaxLen == 0)
878       F->SetMaxInputLen(kDefaultMaxMergeLen);
879     assert(Flags.merge_control_file);
880     F->CrashResistantMergeInternalStep(Flags.merge_control_file);
881     exit(0);
882   }
883 
884   if (Flags.analyze_dict) {
885     size_t MaxLen = INT_MAX;  // Large max length.
886     UnitVector InitialCorpus;
887     for (auto &Inp : *Inputs) {
888       Printf("Loading corpus dir: %s\n", Inp.c_str());
889       ReadDirToVectorOfUnits(Inp.c_str(), &InitialCorpus, nullptr,
890                              MaxLen, /*ExitOnError=*/false);
891     }
892 
893     if (Dictionary.empty() || Inputs->empty()) {
894       Printf("ERROR: can't analyze dict without dict and corpus provided\n");
895       return 1;
896     }
897     if (AnalyzeDictionary(F, Dictionary, InitialCorpus)) {
898       Printf("Dictionary analysis failed\n");
899       exit(1);
900     }
901     Printf("Dictionary analysis succeeded\n");
902     exit(0);
903   }
904 
905   auto CorporaFiles = ReadCorpora(*Inputs, ParseSeedInuts(Flags.seed_inputs));
906   F->Loop(CorporaFiles);
907 
908   if (Flags.verbosity)
909     Printf("Done %zd runs in %zd second(s)\n", F->getTotalNumberOfRuns(),
910            F->secondsSinceProcessStartUp());
911   F->PrintFinalStats();
912 
913   exit(0);  // Don't let F destroy itself.
914 }
915 
916 extern "C" ATTRIBUTE_INTERFACE int
917 LLVMFuzzerRunDriver(int *argc, char ***argv,
918                     int (*UserCb)(const uint8_t *Data, size_t Size)) {
919   return FuzzerDriver(argc, argv, UserCb);
920 }
921 
922 // Storage for global ExternalFunctions object.
923 ExternalFunctions *EF = nullptr;
924 
925 }  // namespace fuzzer
926