xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerLoop.cpp (revision 85868e8a1daeaae7a0e48effb2ea2310ae3b02c6)
1 //===- FuzzerLoop.cpp - Fuzzer's main loop --------------------------------===//
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 // Fuzzer's main loop.
9 //===----------------------------------------------------------------------===//
10 
11 #include "FuzzerCorpus.h"
12 #include "FuzzerIO.h"
13 #include "FuzzerInternal.h"
14 #include "FuzzerMutate.h"
15 #include "FuzzerRandom.h"
16 #include "FuzzerTracePC.h"
17 #include <algorithm>
18 #include <cstring>
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 
23 #if defined(__has_include)
24 #if __has_include(<sanitizer / lsan_interface.h>)
25 #include <sanitizer/lsan_interface.h>
26 #endif
27 #endif
28 
29 #define NO_SANITIZE_MEMORY
30 #if defined(__has_feature)
31 #if __has_feature(memory_sanitizer)
32 #undef NO_SANITIZE_MEMORY
33 #define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
34 #endif
35 #endif
36 
37 namespace fuzzer {
38 static const size_t kMaxUnitSizeToPrint = 256;
39 
40 thread_local bool Fuzzer::IsMyThread;
41 
42 bool RunningUserCallback = false;
43 
44 // Only one Fuzzer per process.
45 static Fuzzer *F;
46 
47 // Leak detection is expensive, so we first check if there were more mallocs
48 // than frees (using the sanitizer malloc hooks) and only then try to call lsan.
49 struct MallocFreeTracer {
50   void Start(int TraceLevel) {
51     this->TraceLevel = TraceLevel;
52     if (TraceLevel)
53       Printf("MallocFreeTracer: START\n");
54     Mallocs = 0;
55     Frees = 0;
56   }
57   // Returns true if there were more mallocs than frees.
58   bool Stop() {
59     if (TraceLevel)
60       Printf("MallocFreeTracer: STOP %zd %zd (%s)\n", Mallocs.load(),
61              Frees.load(), Mallocs == Frees ? "same" : "DIFFERENT");
62     bool Result = Mallocs > Frees;
63     Mallocs = 0;
64     Frees = 0;
65     TraceLevel = 0;
66     return Result;
67   }
68   std::atomic<size_t> Mallocs;
69   std::atomic<size_t> Frees;
70   int TraceLevel = 0;
71 
72   std::recursive_mutex TraceMutex;
73   bool TraceDisabled = false;
74 };
75 
76 static MallocFreeTracer AllocTracer;
77 
78 // Locks printing and avoids nested hooks triggered from mallocs/frees in
79 // sanitizer.
80 class TraceLock {
81 public:
82   TraceLock() : Lock(AllocTracer.TraceMutex) {
83     AllocTracer.TraceDisabled = !AllocTracer.TraceDisabled;
84   }
85   ~TraceLock() { AllocTracer.TraceDisabled = !AllocTracer.TraceDisabled; }
86 
87   bool IsDisabled() const {
88     // This is already inverted value.
89     return !AllocTracer.TraceDisabled;
90   }
91 
92 private:
93   std::lock_guard<std::recursive_mutex> Lock;
94 };
95 
96 ATTRIBUTE_NO_SANITIZE_MEMORY
97 void MallocHook(const volatile void *ptr, size_t size) {
98   size_t N = AllocTracer.Mallocs++;
99   F->HandleMalloc(size);
100   if (int TraceLevel = AllocTracer.TraceLevel) {
101     TraceLock Lock;
102     if (Lock.IsDisabled())
103       return;
104     Printf("MALLOC[%zd] %p %zd\n", N, ptr, size);
105     if (TraceLevel >= 2 && EF)
106       PrintStackTrace();
107   }
108 }
109 
110 ATTRIBUTE_NO_SANITIZE_MEMORY
111 void FreeHook(const volatile void *ptr) {
112   size_t N = AllocTracer.Frees++;
113   if (int TraceLevel = AllocTracer.TraceLevel) {
114     TraceLock Lock;
115     if (Lock.IsDisabled())
116       return;
117     Printf("FREE[%zd]   %p\n", N, ptr);
118     if (TraceLevel >= 2 && EF)
119       PrintStackTrace();
120   }
121 }
122 
123 // Crash on a single malloc that exceeds the rss limit.
124 void Fuzzer::HandleMalloc(size_t Size) {
125   if (!Options.MallocLimitMb || (Size >> 20) < (size_t)Options.MallocLimitMb)
126     return;
127   Printf("==%d== ERROR: libFuzzer: out-of-memory (malloc(%zd))\n", GetPid(),
128          Size);
129   Printf("   To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
130   PrintStackTrace();
131   DumpCurrentUnit("oom-");
132   Printf("SUMMARY: libFuzzer: out-of-memory\n");
133   PrintFinalStats();
134   _Exit(Options.OOMExitCode); // Stop right now.
135 }
136 
137 Fuzzer::Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
138                FuzzingOptions Options)
139     : CB(CB), Corpus(Corpus), MD(MD), Options(Options) {
140   if (EF->__sanitizer_set_death_callback)
141     EF->__sanitizer_set_death_callback(StaticDeathCallback);
142   assert(!F);
143   F = this;
144   TPC.ResetMaps();
145   IsMyThread = true;
146   if (Options.DetectLeaks && EF->__sanitizer_install_malloc_and_free_hooks)
147     EF->__sanitizer_install_malloc_and_free_hooks(MallocHook, FreeHook);
148   TPC.SetUseCounters(Options.UseCounters);
149   TPC.SetUseValueProfileMask(Options.UseValueProfile);
150 
151   if (Options.Verbosity)
152     TPC.PrintModuleInfo();
153   if (!Options.OutputCorpus.empty() && Options.ReloadIntervalSec)
154     EpochOfLastReadOfOutputCorpus = GetEpoch(Options.OutputCorpus);
155   MaxInputLen = MaxMutationLen = Options.MaxLen;
156   TmpMaxMutationLen = 0;  // Will be set once we load the corpus.
157   AllocateCurrentUnitData();
158   CurrentUnitSize = 0;
159   memset(BaseSha1, 0, sizeof(BaseSha1));
160 }
161 
162 Fuzzer::~Fuzzer() {}
163 
164 void Fuzzer::AllocateCurrentUnitData() {
165   if (CurrentUnitData || MaxInputLen == 0)
166     return;
167   CurrentUnitData = new uint8_t[MaxInputLen];
168 }
169 
170 void Fuzzer::StaticDeathCallback() {
171   assert(F);
172   F->DeathCallback();
173 }
174 
175 void Fuzzer::DumpCurrentUnit(const char *Prefix) {
176   if (!CurrentUnitData)
177     return; // Happens when running individual inputs.
178   ScopedDisableMsanInterceptorChecks S;
179   MD.PrintMutationSequence();
180   Printf("; base unit: %s\n", Sha1ToString(BaseSha1).c_str());
181   size_t UnitSize = CurrentUnitSize;
182   if (UnitSize <= kMaxUnitSizeToPrint) {
183     PrintHexArray(CurrentUnitData, UnitSize, "\n");
184     PrintASCII(CurrentUnitData, UnitSize, "\n");
185   }
186   WriteUnitToFileWithPrefix({CurrentUnitData, CurrentUnitData + UnitSize},
187                             Prefix);
188 }
189 
190 NO_SANITIZE_MEMORY
191 void Fuzzer::DeathCallback() {
192   DumpCurrentUnit("crash-");
193   PrintFinalStats();
194 }
195 
196 void Fuzzer::StaticAlarmCallback() {
197   assert(F);
198   F->AlarmCallback();
199 }
200 
201 void Fuzzer::StaticCrashSignalCallback() {
202   assert(F);
203   F->CrashCallback();
204 }
205 
206 void Fuzzer::StaticExitCallback() {
207   assert(F);
208   F->ExitCallback();
209 }
210 
211 void Fuzzer::StaticInterruptCallback() {
212   assert(F);
213   F->InterruptCallback();
214 }
215 
216 void Fuzzer::StaticGracefulExitCallback() {
217   assert(F);
218   F->GracefulExitRequested = true;
219   Printf("INFO: signal received, trying to exit gracefully\n");
220 }
221 
222 void Fuzzer::StaticFileSizeExceedCallback() {
223   Printf("==%lu== ERROR: libFuzzer: file size exceeded\n", GetPid());
224   exit(1);
225 }
226 
227 void Fuzzer::CrashCallback() {
228   if (EF->__sanitizer_acquire_crash_state &&
229       !EF->__sanitizer_acquire_crash_state())
230     return;
231   Printf("==%lu== ERROR: libFuzzer: deadly signal\n", GetPid());
232   PrintStackTrace();
233   Printf("NOTE: libFuzzer has rudimentary signal handlers.\n"
234          "      Combine libFuzzer with AddressSanitizer or similar for better "
235          "crash reports.\n");
236   Printf("SUMMARY: libFuzzer: deadly signal\n");
237   DumpCurrentUnit("crash-");
238   PrintFinalStats();
239   _Exit(Options.ErrorExitCode); // Stop right now.
240 }
241 
242 void Fuzzer::ExitCallback() {
243   if (!RunningUserCallback)
244     return; // This exit did not come from the user callback
245   if (EF->__sanitizer_acquire_crash_state &&
246       !EF->__sanitizer_acquire_crash_state())
247     return;
248   Printf("==%lu== ERROR: libFuzzer: fuzz target exited\n", GetPid());
249   PrintStackTrace();
250   Printf("SUMMARY: libFuzzer: fuzz target exited\n");
251   DumpCurrentUnit("crash-");
252   PrintFinalStats();
253   _Exit(Options.ErrorExitCode);
254 }
255 
256 void Fuzzer::MaybeExitGracefully() {
257   if (!F->GracefulExitRequested) return;
258   Printf("==%lu== INFO: libFuzzer: exiting as requested\n", GetPid());
259   RmDirRecursive(TempPath(".dir"));
260   F->PrintFinalStats();
261   _Exit(0);
262 }
263 
264 void Fuzzer::InterruptCallback() {
265   Printf("==%lu== libFuzzer: run interrupted; exiting\n", GetPid());
266   PrintFinalStats();
267   ScopedDisableMsanInterceptorChecks S; // RmDirRecursive may call opendir().
268   RmDirRecursive(TempPath(".dir"));
269   // Stop right now, don't perform any at-exit actions.
270   _Exit(Options.InterruptExitCode);
271 }
272 
273 NO_SANITIZE_MEMORY
274 void Fuzzer::AlarmCallback() {
275   assert(Options.UnitTimeoutSec > 0);
276   // In Windows and Fuchsia, Alarm callback is executed by a different thread.
277   // NetBSD's current behavior needs this change too.
278 #if !LIBFUZZER_WINDOWS && !LIBFUZZER_NETBSD && !LIBFUZZER_FUCHSIA
279   if (!InFuzzingThread())
280     return;
281 #endif
282   if (!RunningUserCallback)
283     return; // We have not started running units yet.
284   size_t Seconds =
285       duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
286   if (Seconds == 0)
287     return;
288   if (Options.Verbosity >= 2)
289     Printf("AlarmCallback %zd\n", Seconds);
290   if (Seconds >= (size_t)Options.UnitTimeoutSec) {
291     if (EF->__sanitizer_acquire_crash_state &&
292         !EF->__sanitizer_acquire_crash_state())
293       return;
294     Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds);
295     Printf("       and the timeout value is %d (use -timeout=N to change)\n",
296            Options.UnitTimeoutSec);
297     DumpCurrentUnit("timeout-");
298     Printf("==%lu== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(),
299            Seconds);
300     PrintStackTrace();
301     Printf("SUMMARY: libFuzzer: timeout\n");
302     PrintFinalStats();
303     _Exit(Options.TimeoutExitCode); // Stop right now.
304   }
305 }
306 
307 void Fuzzer::RssLimitCallback() {
308   if (EF->__sanitizer_acquire_crash_state &&
309       !EF->__sanitizer_acquire_crash_state())
310     return;
311   Printf(
312       "==%lu== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n",
313       GetPid(), GetPeakRSSMb(), Options.RssLimitMb);
314   Printf("   To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
315   PrintMemoryProfile();
316   DumpCurrentUnit("oom-");
317   Printf("SUMMARY: libFuzzer: out-of-memory\n");
318   PrintFinalStats();
319   _Exit(Options.OOMExitCode); // Stop right now.
320 }
321 
322 void Fuzzer::PrintStats(const char *Where, const char *End, size_t Units,
323                         size_t Features) {
324   size_t ExecPerSec = execPerSec();
325   if (!Options.Verbosity)
326     return;
327   Printf("#%zd\t%s", TotalNumberOfRuns, Where);
328   if (size_t N = TPC.GetTotalPCCoverage())
329     Printf(" cov: %zd", N);
330   if (size_t N = Features ? Features : Corpus.NumFeatures())
331     Printf(" ft: %zd", N);
332   if (!Corpus.empty()) {
333     Printf(" corp: %zd", Corpus.NumActiveUnits());
334     if (size_t N = Corpus.SizeInBytes()) {
335       if (N < (1 << 14))
336         Printf("/%zdb", N);
337       else if (N < (1 << 24))
338         Printf("/%zdKb", N >> 10);
339       else
340         Printf("/%zdMb", N >> 20);
341     }
342     if (size_t FF = Corpus.NumInputsThatTouchFocusFunction())
343       Printf(" focus: %zd", FF);
344   }
345   if (TmpMaxMutationLen)
346     Printf(" lim: %zd", TmpMaxMutationLen);
347   if (Units)
348     Printf(" units: %zd", Units);
349 
350   Printf(" exec/s: %zd", ExecPerSec);
351   Printf(" rss: %zdMb", GetPeakRSSMb());
352   Printf("%s", End);
353 }
354 
355 void Fuzzer::PrintFinalStats() {
356   if (Options.PrintCoverage)
357     TPC.PrintCoverage();
358   if (Options.PrintCorpusStats)
359     Corpus.PrintStats();
360   if (!Options.PrintFinalStats)
361     return;
362   size_t ExecPerSec = execPerSec();
363   Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns);
364   Printf("stat::average_exec_per_sec:     %zd\n", ExecPerSec);
365   Printf("stat::new_units_added:          %zd\n", NumberOfNewUnitsAdded);
366   Printf("stat::slowest_unit_time_sec:    %zd\n", TimeOfLongestUnitInSeconds);
367   Printf("stat::peak_rss_mb:              %zd\n", GetPeakRSSMb());
368 }
369 
370 void Fuzzer::SetMaxInputLen(size_t MaxInputLen) {
371   assert(this->MaxInputLen == 0); // Can only reset MaxInputLen from 0 to non-0.
372   assert(MaxInputLen);
373   this->MaxInputLen = MaxInputLen;
374   this->MaxMutationLen = MaxInputLen;
375   AllocateCurrentUnitData();
376   Printf("INFO: -max_len is not provided; "
377          "libFuzzer will not generate inputs larger than %zd bytes\n",
378          MaxInputLen);
379 }
380 
381 void Fuzzer::SetMaxMutationLen(size_t MaxMutationLen) {
382   assert(MaxMutationLen && MaxMutationLen <= MaxInputLen);
383   this->MaxMutationLen = MaxMutationLen;
384 }
385 
386 void Fuzzer::CheckExitOnSrcPosOrItem() {
387   if (!Options.ExitOnSrcPos.empty()) {
388     static auto *PCsSet = new Set<uintptr_t>;
389     auto HandlePC = [&](const TracePC::PCTableEntry *TE) {
390       if (!PCsSet->insert(TE->PC).second)
391         return;
392       std::string Descr = DescribePC("%F %L", TE->PC + 1);
393       if (Descr.find(Options.ExitOnSrcPos) != std::string::npos) {
394         Printf("INFO: found line matching '%s', exiting.\n",
395                Options.ExitOnSrcPos.c_str());
396         _Exit(0);
397       }
398     };
399     TPC.ForEachObservedPC(HandlePC);
400   }
401   if (!Options.ExitOnItem.empty()) {
402     if (Corpus.HasUnit(Options.ExitOnItem)) {
403       Printf("INFO: found item with checksum '%s', exiting.\n",
404              Options.ExitOnItem.c_str());
405       _Exit(0);
406     }
407   }
408 }
409 
410 void Fuzzer::RereadOutputCorpus(size_t MaxSize) {
411   if (Options.OutputCorpus.empty() || !Options.ReloadIntervalSec)
412     return;
413   Vector<Unit> AdditionalCorpus;
414   ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus,
415                          &EpochOfLastReadOfOutputCorpus, MaxSize,
416                          /*ExitOnError*/ false);
417   if (Options.Verbosity >= 2)
418     Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
419   bool Reloaded = false;
420   for (auto &U : AdditionalCorpus) {
421     if (U.size() > MaxSize)
422       U.resize(MaxSize);
423     if (!Corpus.HasUnit(U)) {
424       if (RunOne(U.data(), U.size())) {
425         CheckExitOnSrcPosOrItem();
426         Reloaded = true;
427       }
428     }
429   }
430   if (Reloaded)
431     PrintStats("RELOAD");
432 }
433 
434 void Fuzzer::PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size) {
435   auto TimeOfUnit =
436       duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
437   if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) &&
438       secondsSinceProcessStartUp() >= 2)
439     PrintStats("pulse ");
440   if (TimeOfUnit > TimeOfLongestUnitInSeconds * 1.1 &&
441       TimeOfUnit >= Options.ReportSlowUnits) {
442     TimeOfLongestUnitInSeconds = TimeOfUnit;
443     Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds);
444     WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-");
445   }
446 }
447 
448 static void WriteFeatureSetToFile(const std::string &FeaturesDir,
449                                   const std::string &FileName,
450                                   const Vector<uint32_t> &FeatureSet) {
451   if (FeaturesDir.empty() || FeatureSet.empty()) return;
452   WriteToFile(reinterpret_cast<const uint8_t *>(FeatureSet.data()),
453               FeatureSet.size() * sizeof(FeatureSet[0]),
454               DirPlusFile(FeaturesDir, FileName));
455 }
456 
457 static void RenameFeatureSetFile(const std::string &FeaturesDir,
458                                  const std::string &OldFile,
459                                  const std::string &NewFile) {
460   if (FeaturesDir.empty()) return;
461   RenameFile(DirPlusFile(FeaturesDir, OldFile),
462              DirPlusFile(FeaturesDir, NewFile));
463 }
464 
465 bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile,
466                     InputInfo *II, bool *FoundUniqFeatures) {
467   if (!Size)
468     return false;
469 
470   ExecuteCallback(Data, Size);
471 
472   UniqFeatureSetTmp.clear();
473   size_t FoundUniqFeaturesOfII = 0;
474   size_t NumUpdatesBefore = Corpus.NumFeatureUpdates();
475   TPC.CollectFeatures([&](size_t Feature) {
476     if (Corpus.AddFeature(Feature, Size, Options.Shrink))
477       UniqFeatureSetTmp.push_back(Feature);
478     if (Options.ReduceInputs && II)
479       if (std::binary_search(II->UniqFeatureSet.begin(),
480                              II->UniqFeatureSet.end(), Feature))
481         FoundUniqFeaturesOfII++;
482   });
483   if (FoundUniqFeatures)
484     *FoundUniqFeatures = FoundUniqFeaturesOfII;
485   PrintPulseAndReportSlowInput(Data, Size);
486   size_t NumNewFeatures = Corpus.NumFeatureUpdates() - NumUpdatesBefore;
487   if (NumNewFeatures) {
488     TPC.UpdateObservedPCs();
489     auto NewII = Corpus.AddToCorpus({Data, Data + Size}, NumNewFeatures,
490                                     MayDeleteFile, TPC.ObservedFocusFunction(),
491                                     UniqFeatureSetTmp, DFT, II);
492     WriteFeatureSetToFile(Options.FeaturesDir, Sha1ToString(NewII->Sha1),
493                           NewII->UniqFeatureSet);
494     return true;
495   }
496   if (II && FoundUniqFeaturesOfII &&
497       II->DataFlowTraceForFocusFunction.empty() &&
498       FoundUniqFeaturesOfII == II->UniqFeatureSet.size() &&
499       II->U.size() > Size) {
500     auto OldFeaturesFile = Sha1ToString(II->Sha1);
501     Corpus.Replace(II, {Data, Data + Size});
502     RenameFeatureSetFile(Options.FeaturesDir, OldFeaturesFile,
503                          Sha1ToString(II->Sha1));
504     return true;
505   }
506   return false;
507 }
508 
509 size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const {
510   assert(InFuzzingThread());
511   *Data = CurrentUnitData;
512   return CurrentUnitSize;
513 }
514 
515 void Fuzzer::CrashOnOverwrittenData() {
516   Printf("==%d== ERROR: libFuzzer: fuzz target overwrites its const input\n",
517          GetPid());
518   PrintStackTrace();
519   Printf("SUMMARY: libFuzzer: overwrites-const-input\n");
520   DumpCurrentUnit("crash-");
521   PrintFinalStats();
522   _Exit(Options.ErrorExitCode); // Stop right now.
523 }
524 
525 // Compare two arrays, but not all bytes if the arrays are large.
526 static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) {
527   const size_t Limit = 64;
528   if (Size <= 64)
529     return !memcmp(A, B, Size);
530   // Compare first and last Limit/2 bytes.
531   return !memcmp(A, B, Limit / 2) &&
532          !memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2);
533 }
534 
535 void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
536   TPC.RecordInitialStack();
537   TotalNumberOfRuns++;
538   assert(InFuzzingThread());
539   // We copy the contents of Unit into a separate heap buffer
540   // so that we reliably find buffer overflows in it.
541   uint8_t *DataCopy = new uint8_t[Size];
542   memcpy(DataCopy, Data, Size);
543   if (EF->__msan_unpoison)
544     EF->__msan_unpoison(DataCopy, Size);
545   if (EF->__msan_unpoison_param)
546     EF->__msan_unpoison_param(2);
547   if (CurrentUnitData && CurrentUnitData != Data)
548     memcpy(CurrentUnitData, Data, Size);
549   CurrentUnitSize = Size;
550   {
551     ScopedEnableMsanInterceptorChecks S;
552     AllocTracer.Start(Options.TraceMalloc);
553     UnitStartTime = system_clock::now();
554     TPC.ResetMaps();
555     RunningUserCallback = true;
556     int Res = CB(DataCopy, Size);
557     RunningUserCallback = false;
558     UnitStopTime = system_clock::now();
559     (void)Res;
560     assert(Res == 0);
561     HasMoreMallocsThanFrees = AllocTracer.Stop();
562   }
563   if (!LooseMemeq(DataCopy, Data, Size))
564     CrashOnOverwrittenData();
565   CurrentUnitSize = 0;
566   delete[] DataCopy;
567 }
568 
569 std::string Fuzzer::WriteToOutputCorpus(const Unit &U) {
570   if (Options.OnlyASCII)
571     assert(IsASCII(U));
572   if (Options.OutputCorpus.empty())
573     return "";
574   std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
575   WriteToFile(U, Path);
576   if (Options.Verbosity >= 2)
577     Printf("Written %zd bytes to %s\n", U.size(), Path.c_str());
578   return Path;
579 }
580 
581 void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
582   if (!Options.SaveArtifacts)
583     return;
584   std::string Path = Options.ArtifactPrefix + Prefix + Hash(U);
585   if (!Options.ExactArtifactPath.empty())
586     Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix.
587   WriteToFile(U, Path);
588   Printf("artifact_prefix='%s'; Test unit written to %s\n",
589          Options.ArtifactPrefix.c_str(), Path.c_str());
590   if (U.size() <= kMaxUnitSizeToPrint)
591     Printf("Base64: %s\n", Base64(U).c_str());
592 }
593 
594 void Fuzzer::PrintStatusForNewUnit(const Unit &U, const char *Text) {
595   if (!Options.PrintNEW)
596     return;
597   PrintStats(Text, "");
598   if (Options.Verbosity) {
599     Printf(" L: %zd/%zd ", U.size(), Corpus.MaxInputSize());
600     MD.PrintMutationSequence();
601     Printf("\n");
602   }
603 }
604 
605 void Fuzzer::ReportNewCoverage(InputInfo *II, const Unit &U) {
606   II->NumSuccessfullMutations++;
607   MD.RecordSuccessfulMutationSequence();
608   PrintStatusForNewUnit(U, II->Reduced ? "REDUCE" : "NEW   ");
609   WriteToOutputCorpus(U);
610   NumberOfNewUnitsAdded++;
611   CheckExitOnSrcPosOrItem(); // Check only after the unit is saved to corpus.
612   LastCorpusUpdateRun = TotalNumberOfRuns;
613 }
614 
615 // Tries detecting a memory leak on the particular input that we have just
616 // executed before calling this function.
617 void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
618                                      bool DuringInitialCorpusExecution) {
619   if (!HasMoreMallocsThanFrees)
620     return; // mallocs==frees, a leak is unlikely.
621   if (!Options.DetectLeaks)
622     return;
623   if (!DuringInitialCorpusExecution &&
624       TotalNumberOfRuns >= Options.MaxNumberOfRuns)
625     return;
626   if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) ||
627       !(EF->__lsan_do_recoverable_leak_check))
628     return; // No lsan.
629   // Run the target once again, but with lsan disabled so that if there is
630   // a real leak we do not report it twice.
631   EF->__lsan_disable();
632   ExecuteCallback(Data, Size);
633   EF->__lsan_enable();
634   if (!HasMoreMallocsThanFrees)
635     return; // a leak is unlikely.
636   if (NumberOfLeakDetectionAttempts++ > 1000) {
637     Options.DetectLeaks = false;
638     Printf("INFO: libFuzzer disabled leak detection after every mutation.\n"
639            "      Most likely the target function accumulates allocated\n"
640            "      memory in a global state w/o actually leaking it.\n"
641            "      You may try running this binary with -trace_malloc=[12]"
642            "      to get a trace of mallocs and frees.\n"
643            "      If LeakSanitizer is enabled in this process it will still\n"
644            "      run on the process shutdown.\n");
645     return;
646   }
647   // Now perform the actual lsan pass. This is expensive and we must ensure
648   // we don't call it too often.
649   if (EF->__lsan_do_recoverable_leak_check()) { // Leak is found, report it.
650     if (DuringInitialCorpusExecution)
651       Printf("\nINFO: a leak has been found in the initial corpus.\n\n");
652     Printf("INFO: to ignore leaks on libFuzzer side use -detect_leaks=0.\n\n");
653     CurrentUnitSize = Size;
654     DumpCurrentUnit("leak-");
655     PrintFinalStats();
656     _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on.
657   }
658 }
659 
660 void Fuzzer::MutateAndTestOne() {
661   MD.StartMutationSequence();
662 
663   auto &II = Corpus.ChooseUnitToMutate(MD.GetRand());
664   if (Options.DoCrossOver)
665     MD.SetCrossOverWith(&Corpus.ChooseUnitToMutate(MD.GetRand()).U);
666   const auto &U = II.U;
667   memcpy(BaseSha1, II.Sha1, sizeof(BaseSha1));
668   assert(CurrentUnitData);
669   size_t Size = U.size();
670   assert(Size <= MaxInputLen && "Oversized Unit");
671   memcpy(CurrentUnitData, U.data(), Size);
672 
673   assert(MaxMutationLen > 0);
674 
675   size_t CurrentMaxMutationLen =
676       Min(MaxMutationLen, Max(U.size(), TmpMaxMutationLen));
677   assert(CurrentMaxMutationLen > 0);
678 
679   for (int i = 0; i < Options.MutateDepth; i++) {
680     if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
681       break;
682     MaybeExitGracefully();
683     size_t NewSize = 0;
684     if (II.HasFocusFunction && !II.DataFlowTraceForFocusFunction.empty() &&
685         Size <= CurrentMaxMutationLen)
686       NewSize = MD.MutateWithMask(CurrentUnitData, Size, Size,
687                                   II.DataFlowTraceForFocusFunction);
688 
689     // If MutateWithMask either failed or wasn't called, call default Mutate.
690     if (!NewSize)
691       NewSize = MD.Mutate(CurrentUnitData, Size, CurrentMaxMutationLen);
692     assert(NewSize > 0 && "Mutator returned empty unit");
693     assert(NewSize <= CurrentMaxMutationLen && "Mutator return oversized unit");
694     Size = NewSize;
695     II.NumExecutedMutations++;
696 
697     bool FoundUniqFeatures = false;
698     bool NewCov = RunOne(CurrentUnitData, Size, /*MayDeleteFile=*/true, &II,
699                          &FoundUniqFeatures);
700     TryDetectingAMemoryLeak(CurrentUnitData, Size,
701                             /*DuringInitialCorpusExecution*/ false);
702     if (NewCov) {
703       ReportNewCoverage(&II, {CurrentUnitData, CurrentUnitData + Size});
704       break;  // We will mutate this input more in the next rounds.
705     }
706     if (Options.ReduceDepth && !FoundUniqFeatures)
707       break;
708   }
709 }
710 
711 void Fuzzer::PurgeAllocator() {
712   if (Options.PurgeAllocatorIntervalSec < 0 || !EF->__sanitizer_purge_allocator)
713     return;
714   if (duration_cast<seconds>(system_clock::now() -
715                              LastAllocatorPurgeAttemptTime)
716           .count() < Options.PurgeAllocatorIntervalSec)
717     return;
718 
719   if (Options.RssLimitMb <= 0 ||
720       GetPeakRSSMb() > static_cast<size_t>(Options.RssLimitMb) / 2)
721     EF->__sanitizer_purge_allocator();
722 
723   LastAllocatorPurgeAttemptTime = system_clock::now();
724 }
725 
726 void Fuzzer::ReadAndExecuteSeedCorpora(Vector<SizedFile> &CorporaFiles) {
727   const size_t kMaxSaneLen = 1 << 20;
728   const size_t kMinDefaultLen = 4096;
729   size_t MaxSize = 0;
730   size_t MinSize = -1;
731   size_t TotalSize = 0;
732   for (auto &File : CorporaFiles) {
733     MaxSize = Max(File.Size, MaxSize);
734     MinSize = Min(File.Size, MinSize);
735     TotalSize += File.Size;
736   }
737   if (Options.MaxLen == 0)
738     SetMaxInputLen(std::min(std::max(kMinDefaultLen, MaxSize), kMaxSaneLen));
739   assert(MaxInputLen > 0);
740 
741   // Test the callback with empty input and never try it again.
742   uint8_t dummy = 0;
743   ExecuteCallback(&dummy, 0);
744 
745   if (CorporaFiles.empty()) {
746     Printf("INFO: A corpus is not provided, starting from an empty corpus\n");
747     Unit U({'\n'}); // Valid ASCII input.
748     RunOne(U.data(), U.size());
749   } else {
750     Printf("INFO: seed corpus: files: %zd min: %zdb max: %zdb total: %zdb"
751            " rss: %zdMb\n",
752            CorporaFiles.size(), MinSize, MaxSize, TotalSize, GetPeakRSSMb());
753     if (Options.ShuffleAtStartUp)
754       std::shuffle(CorporaFiles.begin(), CorporaFiles.end(), MD.GetRand());
755 
756     if (Options.PreferSmall) {
757       std::stable_sort(CorporaFiles.begin(), CorporaFiles.end());
758       assert(CorporaFiles.front().Size <= CorporaFiles.back().Size);
759     }
760 
761     // Load and execute inputs one by one.
762     for (auto &SF : CorporaFiles) {
763       auto U = FileToVector(SF.File, MaxInputLen, /*ExitOnError=*/false);
764       assert(U.size() <= MaxInputLen);
765       RunOne(U.data(), U.size());
766       CheckExitOnSrcPosOrItem();
767       TryDetectingAMemoryLeak(U.data(), U.size(),
768                               /*DuringInitialCorpusExecution*/ true);
769     }
770   }
771 
772   PrintStats("INITED");
773   if (!Options.FocusFunction.empty())
774     Printf("INFO: %zd/%zd inputs touch the focus function\n",
775            Corpus.NumInputsThatTouchFocusFunction(), Corpus.size());
776   if (!Options.DataFlowTrace.empty())
777     Printf("INFO: %zd/%zd inputs have the Data Flow Trace\n",
778            Corpus.NumInputsWithDataFlowTrace(), Corpus.size());
779 
780   if (Corpus.empty() && Options.MaxNumberOfRuns) {
781     Printf("ERROR: no interesting inputs were found. "
782            "Is the code instrumented for coverage? Exiting.\n");
783     exit(1);
784   }
785 }
786 
787 void Fuzzer::Loop(Vector<SizedFile> &CorporaFiles) {
788   auto FocusFunctionOrAuto = Options.FocusFunction;
789   DFT.Init(Options.DataFlowTrace, &FocusFunctionOrAuto, CorporaFiles,
790            MD.GetRand());
791   TPC.SetFocusFunction(FocusFunctionOrAuto);
792   ReadAndExecuteSeedCorpora(CorporaFiles);
793   DFT.Clear();  // No need for DFT any more.
794   TPC.SetPrintNewPCs(Options.PrintNewCovPcs);
795   TPC.SetPrintNewFuncs(Options.PrintNewCovFuncs);
796   system_clock::time_point LastCorpusReload = system_clock::now();
797 
798   TmpMaxMutationLen =
799       Min(MaxMutationLen, Max(size_t(4), Corpus.MaxInputSize()));
800 
801   while (true) {
802     auto Now = system_clock::now();
803     if (!Options.StopFile.empty() &&
804         !FileToVector(Options.StopFile, 1, false).empty())
805       break;
806     if (duration_cast<seconds>(Now - LastCorpusReload).count() >=
807         Options.ReloadIntervalSec) {
808       RereadOutputCorpus(MaxInputLen);
809       LastCorpusReload = system_clock::now();
810     }
811     if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
812       break;
813     if (TimedOut())
814       break;
815 
816     // Update TmpMaxMutationLen
817     if (Options.LenControl) {
818       if (TmpMaxMutationLen < MaxMutationLen &&
819           TotalNumberOfRuns - LastCorpusUpdateRun >
820               Options.LenControl * Log(TmpMaxMutationLen)) {
821         TmpMaxMutationLen =
822             Min(MaxMutationLen, TmpMaxMutationLen + Log(TmpMaxMutationLen));
823         LastCorpusUpdateRun = TotalNumberOfRuns;
824       }
825     } else {
826       TmpMaxMutationLen = MaxMutationLen;
827     }
828 
829     // Perform several mutations and runs.
830     MutateAndTestOne();
831 
832     PurgeAllocator();
833   }
834 
835   PrintStats("DONE  ", "\n");
836   MD.PrintRecommendedDictionary();
837 }
838 
839 void Fuzzer::MinimizeCrashLoop(const Unit &U) {
840   if (U.size() <= 1)
841     return;
842   while (!TimedOut() && TotalNumberOfRuns < Options.MaxNumberOfRuns) {
843     MD.StartMutationSequence();
844     memcpy(CurrentUnitData, U.data(), U.size());
845     for (int i = 0; i < Options.MutateDepth; i++) {
846       size_t NewSize = MD.Mutate(CurrentUnitData, U.size(), MaxMutationLen);
847       assert(NewSize > 0 && NewSize <= MaxMutationLen);
848       ExecuteCallback(CurrentUnitData, NewSize);
849       PrintPulseAndReportSlowInput(CurrentUnitData, NewSize);
850       TryDetectingAMemoryLeak(CurrentUnitData, NewSize,
851                               /*DuringInitialCorpusExecution*/ false);
852     }
853   }
854 }
855 
856 } // namespace fuzzer
857 
858 extern "C" {
859 
860 ATTRIBUTE_INTERFACE size_t
861 LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
862   assert(fuzzer::F);
863   return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize);
864 }
865 
866 } // extern "C"
867