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