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