FuzzerDriver.cpp 31 KB

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