]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerLoop.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / compiler-rt / lib / fuzzer / FuzzerLoop.cpp
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 Alarm callback is executed by a different thread.
277   // NetBSD's current behavior needs this change too.
278 #if !LIBFUZZER_WINDOWS && !LIBFUZZER_NETBSD
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 ExecPerSec = execPerSec();
324   if (!Options.Verbosity)
325     return;
326   Printf("#%zd\t%s", TotalNumberOfRuns, Where);
327   if (size_t N = TPC.GetTotalPCCoverage())
328     Printf(" cov: %zd", N);
329   if (size_t N = Corpus.NumFeatures())
330     Printf(" ft: %zd", N);
331   if (!Corpus.empty()) {
332     Printf(" corp: %zd", Corpus.NumActiveUnits());
333     if (size_t N = Corpus.SizeInBytes()) {
334       if (N < (1 << 14))
335         Printf("/%zdb", N);
336       else if (N < (1 << 24))
337         Printf("/%zdKb", N >> 10);
338       else
339         Printf("/%zdMb", N >> 20);
340     }
341     if (size_t FF = Corpus.NumInputsThatTouchFocusFunction())
342       Printf(" focus: %zd", FF);
343   }
344   if (TmpMaxMutationLen)
345     Printf(" lim: %zd", TmpMaxMutationLen);
346   if (Units)
347     Printf(" units: %zd", Units);
348
349   Printf(" exec/s: %zd", ExecPerSec);
350   Printf(" rss: %zdMb", GetPeakRSSMb());
351   Printf("%s", End);
352 }
353
354 void Fuzzer::PrintFinalStats() {
355   if (Options.PrintCoverage)
356     TPC.PrintCoverage();
357   if (Options.PrintCorpusStats)
358     Corpus.PrintStats();
359   if (!Options.PrintFinalStats)
360     return;
361   size_t ExecPerSec = execPerSec();
362   Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns);
363   Printf("stat::average_exec_per_sec:     %zd\n", ExecPerSec);
364   Printf("stat::new_units_added:          %zd\n", NumberOfNewUnitsAdded);
365   Printf("stat::slowest_unit_time_sec:    %zd\n", TimeOfLongestUnitInSeconds);
366   Printf("stat::peak_rss_mb:              %zd\n", GetPeakRSSMb());
367 }
368
369 void Fuzzer::SetMaxInputLen(size_t MaxInputLen) {
370   assert(this->MaxInputLen == 0); // Can only reset MaxInputLen from 0 to non-0.
371   assert(MaxInputLen);
372   this->MaxInputLen = MaxInputLen;
373   this->MaxMutationLen = MaxInputLen;
374   AllocateCurrentUnitData();
375   Printf("INFO: -max_len is not provided; "
376          "libFuzzer will not generate inputs larger than %zd bytes\n",
377          MaxInputLen);
378 }
379
380 void Fuzzer::SetMaxMutationLen(size_t MaxMutationLen) {
381   assert(MaxMutationLen && MaxMutationLen <= MaxInputLen);
382   this->MaxMutationLen = MaxMutationLen;
383 }
384
385 void Fuzzer::CheckExitOnSrcPosOrItem() {
386   if (!Options.ExitOnSrcPos.empty()) {
387     static auto *PCsSet = new Set<uintptr_t>;
388     auto HandlePC = [&](const TracePC::PCTableEntry *TE) {
389       if (!PCsSet->insert(TE->PC).second)
390         return;
391       std::string Descr = DescribePC("%F %L", TE->PC + 1);
392       if (Descr.find(Options.ExitOnSrcPos) != std::string::npos) {
393         Printf("INFO: found line matching '%s', exiting.\n",
394                Options.ExitOnSrcPos.c_str());
395         _Exit(0);
396       }
397     };
398     TPC.ForEachObservedPC(HandlePC);
399   }
400   if (!Options.ExitOnItem.empty()) {
401     if (Corpus.HasUnit(Options.ExitOnItem)) {
402       Printf("INFO: found item with checksum '%s', exiting.\n",
403              Options.ExitOnItem.c_str());
404       _Exit(0);
405     }
406   }
407 }
408
409 void Fuzzer::RereadOutputCorpus(size_t MaxSize) {
410   if (Options.OutputCorpus.empty() || !Options.ReloadIntervalSec)
411     return;
412   Vector<Unit> AdditionalCorpus;
413   ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus,
414                          &EpochOfLastReadOfOutputCorpus, MaxSize,
415                          /*ExitOnError*/ false);
416   if (Options.Verbosity >= 2)
417     Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
418   bool Reloaded = false;
419   for (auto &U : AdditionalCorpus) {
420     if (U.size() > MaxSize)
421       U.resize(MaxSize);
422     if (!Corpus.HasUnit(U)) {
423       if (RunOne(U.data(), U.size())) {
424         CheckExitOnSrcPosOrItem();
425         Reloaded = true;
426       }
427     }
428   }
429   if (Reloaded)
430     PrintStats("RELOAD");
431 }
432
433 void Fuzzer::PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size) {
434   auto TimeOfUnit =
435       duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
436   if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) &&
437       secondsSinceProcessStartUp() >= 2)
438     PrintStats("pulse ");
439   if (TimeOfUnit > TimeOfLongestUnitInSeconds * 1.1 &&
440       TimeOfUnit >= Options.ReportSlowUnits) {
441     TimeOfLongestUnitInSeconds = TimeOfUnit;
442     Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds);
443     WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-");
444   }
445 }
446
447 static void WriteFeatureSetToFile(const std::string &FeaturesDir,
448                                   const std::string &FileName,
449                                   const Vector<uint32_t> &FeatureSet) {
450   if (FeaturesDir.empty() || FeatureSet.empty()) return;
451   WriteToFile(reinterpret_cast<const uint8_t *>(FeatureSet.data()),
452               FeatureSet.size() * sizeof(FeatureSet[0]),
453               DirPlusFile(FeaturesDir, FileName));
454 }
455
456 static void RenameFeatureSetFile(const std::string &FeaturesDir,
457                                  const std::string &OldFile,
458                                  const std::string &NewFile) {
459   if (FeaturesDir.empty()) return;
460   RenameFile(DirPlusFile(FeaturesDir, OldFile),
461              DirPlusFile(FeaturesDir, NewFile));
462 }
463
464 bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile,
465                     InputInfo *II, bool *FoundUniqFeatures) {
466   if (!Size)
467     return false;
468
469   ExecuteCallback(Data, Size);
470
471   UniqFeatureSetTmp.clear();
472   size_t FoundUniqFeaturesOfII = 0;
473   size_t NumUpdatesBefore = Corpus.NumFeatureUpdates();
474   TPC.CollectFeatures([&](size_t Feature) {
475     if (Corpus.AddFeature(Feature, Size, Options.Shrink))
476       UniqFeatureSetTmp.push_back(Feature);
477     if (Options.ReduceInputs && II)
478       if (std::binary_search(II->UniqFeatureSet.begin(),
479                              II->UniqFeatureSet.end(), Feature))
480         FoundUniqFeaturesOfII++;
481   });
482   if (FoundUniqFeatures)
483     *FoundUniqFeatures = FoundUniqFeaturesOfII;
484   PrintPulseAndReportSlowInput(Data, Size);
485   size_t NumNewFeatures = Corpus.NumFeatureUpdates() - NumUpdatesBefore;
486   if (NumNewFeatures) {
487     TPC.UpdateObservedPCs();
488     auto NewII = Corpus.AddToCorpus({Data, Data + Size}, NumNewFeatures,
489                                     MayDeleteFile, TPC.ObservedFocusFunction(),
490                                     UniqFeatureSetTmp, DFT, II);
491     WriteFeatureSetToFile(Options.FeaturesDir, Sha1ToString(NewII->Sha1),
492                           NewII->UniqFeatureSet);
493     return true;
494   }
495   if (II && FoundUniqFeaturesOfII &&
496       II->DataFlowTraceForFocusFunction.empty() &&
497       FoundUniqFeaturesOfII == II->UniqFeatureSet.size() &&
498       II->U.size() > Size) {
499     auto OldFeaturesFile = Sha1ToString(II->Sha1);
500     Corpus.Replace(II, {Data, Data + Size});
501     RenameFeatureSetFile(Options.FeaturesDir, OldFeaturesFile,
502                          Sha1ToString(II->Sha1));
503     return true;
504   }
505   return false;
506 }
507
508 size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const {
509   assert(InFuzzingThread());
510   *Data = CurrentUnitData;
511   return CurrentUnitSize;
512 }
513
514 void Fuzzer::CrashOnOverwrittenData() {
515   Printf("==%d== ERROR: libFuzzer: fuzz target overwrites it's const input\n",
516          GetPid());
517   DumpCurrentUnit("crash-");
518   Printf("SUMMARY: libFuzzer: out-of-memory\n");
519   _Exit(Options.ErrorExitCode); // Stop right now.
520 }
521
522 // Compare two arrays, but not all bytes if the arrays are large.
523 static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) {
524   const size_t Limit = 64;
525   if (Size <= 64)
526     return !memcmp(A, B, Size);
527   // Compare first and last Limit/2 bytes.
528   return !memcmp(A, B, Limit / 2) &&
529          !memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2);
530 }
531
532 void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
533   TPC.RecordInitialStack();
534   TotalNumberOfRuns++;
535   assert(InFuzzingThread());
536   // We copy the contents of Unit into a separate heap buffer
537   // so that we reliably find buffer overflows in it.
538   uint8_t *DataCopy = new uint8_t[Size];
539   memcpy(DataCopy, Data, Size);
540   if (EF->__msan_unpoison)
541     EF->__msan_unpoison(DataCopy, Size);
542   if (EF->__msan_unpoison_param)
543     EF->__msan_unpoison_param(2);
544   if (CurrentUnitData && CurrentUnitData != Data)
545     memcpy(CurrentUnitData, Data, Size);
546   CurrentUnitSize = Size;
547   {
548     ScopedEnableMsanInterceptorChecks S;
549     AllocTracer.Start(Options.TraceMalloc);
550     UnitStartTime = system_clock::now();
551     TPC.ResetMaps();
552     RunningUserCallback = true;
553     int Res = CB(DataCopy, Size);
554     RunningUserCallback = false;
555     UnitStopTime = system_clock::now();
556     (void)Res;
557     assert(Res == 0);
558     HasMoreMallocsThanFrees = AllocTracer.Stop();
559   }
560   if (!LooseMemeq(DataCopy, Data, Size))
561     CrashOnOverwrittenData();
562   CurrentUnitSize = 0;
563   delete[] DataCopy;
564 }
565
566 std::string Fuzzer::WriteToOutputCorpus(const Unit &U) {
567   if (Options.OnlyASCII)
568     assert(IsASCII(U));
569   if (Options.OutputCorpus.empty())
570     return "";
571   std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
572   WriteToFile(U, Path);
573   if (Options.Verbosity >= 2)
574     Printf("Written %zd bytes to %s\n", U.size(), Path.c_str());
575   return Path;
576 }
577
578 void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
579   if (!Options.SaveArtifacts)
580     return;
581   std::string Path = Options.ArtifactPrefix + Prefix + Hash(U);
582   if (!Options.ExactArtifactPath.empty())
583     Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix.
584   WriteToFile(U, Path);
585   Printf("artifact_prefix='%s'; Test unit written to %s\n",
586          Options.ArtifactPrefix.c_str(), Path.c_str());
587   if (U.size() <= kMaxUnitSizeToPrint)
588     Printf("Base64: %s\n", Base64(U).c_str());
589 }
590
591 void Fuzzer::PrintStatusForNewUnit(const Unit &U, const char *Text) {
592   if (!Options.PrintNEW)
593     return;
594   PrintStats(Text, "");
595   if (Options.Verbosity) {
596     Printf(" L: %zd/%zd ", U.size(), Corpus.MaxInputSize());
597     MD.PrintMutationSequence();
598     Printf("\n");
599   }
600 }
601
602 void Fuzzer::ReportNewCoverage(InputInfo *II, const Unit &U) {
603   II->NumSuccessfullMutations++;
604   MD.RecordSuccessfulMutationSequence();
605   PrintStatusForNewUnit(U, II->Reduced ? "REDUCE" : "NEW   ");
606   WriteToOutputCorpus(U);
607   NumberOfNewUnitsAdded++;
608   CheckExitOnSrcPosOrItem(); // Check only after the unit is saved to corpus.
609   LastCorpusUpdateRun = TotalNumberOfRuns;
610 }
611
612 // Tries detecting a memory leak on the particular input that we have just
613 // executed before calling this function.
614 void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
615                                      bool DuringInitialCorpusExecution) {
616   if (!HasMoreMallocsThanFrees)
617     return; // mallocs==frees, a leak is unlikely.
618   if (!Options.DetectLeaks)
619     return;
620   if (!DuringInitialCorpusExecution &&
621       TotalNumberOfRuns >= Options.MaxNumberOfRuns)
622     return;
623   if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) ||
624       !(EF->__lsan_do_recoverable_leak_check))
625     return; // No lsan.
626   // Run the target once again, but with lsan disabled so that if there is
627   // a real leak we do not report it twice.
628   EF->__lsan_disable();
629   ExecuteCallback(Data, Size);
630   EF->__lsan_enable();
631   if (!HasMoreMallocsThanFrees)
632     return; // a leak is unlikely.
633   if (NumberOfLeakDetectionAttempts++ > 1000) {
634     Options.DetectLeaks = false;
635     Printf("INFO: libFuzzer disabled leak detection after every mutation.\n"
636            "      Most likely the target function accumulates allocated\n"
637            "      memory in a global state w/o actually leaking it.\n"
638            "      You may try running this binary with -trace_malloc=[12]"
639            "      to get a trace of mallocs and frees.\n"
640            "      If LeakSanitizer is enabled in this process it will still\n"
641            "      run on the process shutdown.\n");
642     return;
643   }
644   // Now perform the actual lsan pass. This is expensive and we must ensure
645   // we don't call it too often.
646   if (EF->__lsan_do_recoverable_leak_check()) { // Leak is found, report it.
647     if (DuringInitialCorpusExecution)
648       Printf("\nINFO: a leak has been found in the initial corpus.\n\n");
649     Printf("INFO: to ignore leaks on libFuzzer side use -detect_leaks=0.\n\n");
650     CurrentUnitSize = Size;
651     DumpCurrentUnit("leak-");
652     PrintFinalStats();
653     _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on.
654   }
655 }
656
657 void Fuzzer::MutateAndTestOne() {
658   MD.StartMutationSequence();
659
660   auto &II = Corpus.ChooseUnitToMutate(MD.GetRand());
661   if (Options.DoCrossOver)
662     MD.SetCrossOverWith(&Corpus.ChooseUnitToMutate(MD.GetRand()).U);
663   const auto &U = II.U;
664   memcpy(BaseSha1, II.Sha1, sizeof(BaseSha1));
665   assert(CurrentUnitData);
666   size_t Size = U.size();
667   assert(Size <= MaxInputLen && "Oversized Unit");
668   memcpy(CurrentUnitData, U.data(), Size);
669
670   assert(MaxMutationLen > 0);
671
672   size_t CurrentMaxMutationLen =
673       Min(MaxMutationLen, Max(U.size(), TmpMaxMutationLen));
674   assert(CurrentMaxMutationLen > 0);
675
676   for (int i = 0; i < Options.MutateDepth; i++) {
677     if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
678       break;
679     MaybeExitGracefully();
680     size_t NewSize = 0;
681     if (II.HasFocusFunction && !II.DataFlowTraceForFocusFunction.empty() &&
682         Size <= CurrentMaxMutationLen)
683       NewSize = MD.MutateWithMask(CurrentUnitData, Size, Size,
684                                   II.DataFlowTraceForFocusFunction);
685
686     // If MutateWithMask either failed or wasn't called, call default Mutate.
687     if (!NewSize)
688       NewSize = MD.Mutate(CurrentUnitData, Size, CurrentMaxMutationLen);
689     assert(NewSize > 0 && "Mutator returned empty unit");
690     assert(NewSize <= CurrentMaxMutationLen && "Mutator return oversized unit");
691     Size = NewSize;
692     II.NumExecutedMutations++;
693
694     bool FoundUniqFeatures = false;
695     bool NewCov = RunOne(CurrentUnitData, Size, /*MayDeleteFile=*/true, &II,
696                          &FoundUniqFeatures);
697     TryDetectingAMemoryLeak(CurrentUnitData, Size,
698                             /*DuringInitialCorpusExecution*/ false);
699     if (NewCov) {
700       ReportNewCoverage(&II, {CurrentUnitData, CurrentUnitData + Size});
701       break;  // We will mutate this input more in the next rounds.
702     }
703     if (Options.ReduceDepth && !FoundUniqFeatures)
704       break;
705   }
706 }
707
708 void Fuzzer::PurgeAllocator() {
709   if (Options.PurgeAllocatorIntervalSec < 0 || !EF->__sanitizer_purge_allocator)
710     return;
711   if (duration_cast<seconds>(system_clock::now() -
712                              LastAllocatorPurgeAttemptTime)
713           .count() < Options.PurgeAllocatorIntervalSec)
714     return;
715
716   if (Options.RssLimitMb <= 0 ||
717       GetPeakRSSMb() > static_cast<size_t>(Options.RssLimitMb) / 2)
718     EF->__sanitizer_purge_allocator();
719
720   LastAllocatorPurgeAttemptTime = system_clock::now();
721 }
722
723 void Fuzzer::ReadAndExecuteSeedCorpora(Vector<SizedFile> &CorporaFiles) {
724   const size_t kMaxSaneLen = 1 << 20;
725   const size_t kMinDefaultLen = 4096;
726   size_t MaxSize = 0;
727   size_t MinSize = -1;
728   size_t TotalSize = 0;
729   for (auto &File : CorporaFiles) {
730     MaxSize = Max(File.Size, MaxSize);
731     MinSize = Min(File.Size, MinSize);
732     TotalSize += File.Size;
733   }
734   if (Options.MaxLen == 0)
735     SetMaxInputLen(std::min(std::max(kMinDefaultLen, MaxSize), kMaxSaneLen));
736   assert(MaxInputLen > 0);
737
738   // Test the callback with empty input and never try it again.
739   uint8_t dummy = 0;
740   ExecuteCallback(&dummy, 0);
741
742   // Protect lazy counters here, after the once-init code has been executed.
743   if (Options.LazyCounters)
744     TPC.ProtectLazyCounters();
745
746   if (CorporaFiles.empty()) {
747     Printf("INFO: A corpus is not provided, starting from an empty corpus\n");
748     Unit U({'\n'}); // Valid ASCII input.
749     RunOne(U.data(), U.size());
750   } else {
751     Printf("INFO: seed corpus: files: %zd min: %zdb max: %zdb total: %zdb"
752            " rss: %zdMb\n",
753            CorporaFiles.size(), MinSize, MaxSize, TotalSize, GetPeakRSSMb());
754     if (Options.ShuffleAtStartUp)
755       std::shuffle(CorporaFiles.begin(), CorporaFiles.end(), MD.GetRand());
756
757     if (Options.PreferSmall) {
758       std::stable_sort(CorporaFiles.begin(), CorporaFiles.end());
759       assert(CorporaFiles.front().Size <= CorporaFiles.back().Size);
760     }
761
762     // Load and execute inputs one by one.
763     for (auto &SF : CorporaFiles) {
764       auto U = FileToVector(SF.File, MaxInputLen, /*ExitOnError=*/false);
765       assert(U.size() <= MaxInputLen);
766       RunOne(U.data(), U.size());
767       CheckExitOnSrcPosOrItem();
768       TryDetectingAMemoryLeak(U.data(), U.size(),
769                               /*DuringInitialCorpusExecution*/ true);
770     }
771   }
772
773   PrintStats("INITED");
774   if (!Options.FocusFunction.empty())
775     Printf("INFO: %zd/%zd inputs touch the focus function\n",
776            Corpus.NumInputsThatTouchFocusFunction(), Corpus.size());
777   if (!Options.DataFlowTrace.empty())
778     Printf("INFO: %zd/%zd inputs have the Data Flow Trace\n",
779            Corpus.NumInputsWithDataFlowTrace(), Corpus.size());
780
781   if (Corpus.empty() && Options.MaxNumberOfRuns) {
782     Printf("ERROR: no interesting inputs were found. "
783            "Is the code instrumented for coverage? Exiting.\n");
784     exit(1);
785   }
786 }
787
788 void Fuzzer::Loop(Vector<SizedFile> &CorporaFiles) {
789   auto FocusFunctionOrAuto = Options.FocusFunction;
790   DFT.Init(Options.DataFlowTrace, &FocusFunctionOrAuto, CorporaFiles,
791            MD.GetRand());
792   TPC.SetFocusFunction(FocusFunctionOrAuto);
793   ReadAndExecuteSeedCorpora(CorporaFiles);
794   DFT.Clear();  // No need for DFT any more.
795   TPC.SetPrintNewPCs(Options.PrintNewCovPcs);
796   TPC.SetPrintNewFuncs(Options.PrintNewCovFuncs);
797   system_clock::time_point LastCorpusReload = system_clock::now();
798
799   TmpMaxMutationLen =
800       Min(MaxMutationLen, Max(size_t(4), Corpus.MaxInputSize()));
801
802   while (true) {
803     auto Now = system_clock::now();
804     if (!Options.StopFile.empty() &&
805         !FileToVector(Options.StopFile, 1, false).empty())
806       break;
807     if (duration_cast<seconds>(Now - LastCorpusReload).count() >=
808         Options.ReloadIntervalSec) {
809       RereadOutputCorpus(MaxInputLen);
810       LastCorpusReload = system_clock::now();
811     }
812     if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
813       break;
814     if (TimedOut())
815       break;
816
817     // Update TmpMaxMutationLen
818     if (Options.LenControl) {
819       if (TmpMaxMutationLen < MaxMutationLen &&
820           TotalNumberOfRuns - LastCorpusUpdateRun >
821               Options.LenControl * Log(TmpMaxMutationLen)) {
822         TmpMaxMutationLen =
823             Min(MaxMutationLen, TmpMaxMutationLen + Log(TmpMaxMutationLen));
824         LastCorpusUpdateRun = TotalNumberOfRuns;
825       }
826     } else {
827       TmpMaxMutationLen = MaxMutationLen;
828     }
829
830     // Perform several mutations and runs.
831     MutateAndTestOne();
832
833     PurgeAllocator();
834   }
835
836   PrintStats("DONE  ", "\n");
837   MD.PrintRecommendedDictionary();
838 }
839
840 void Fuzzer::MinimizeCrashLoop(const Unit &U) {
841   if (U.size() <= 1)
842     return;
843   while (!TimedOut() && TotalNumberOfRuns < Options.MaxNumberOfRuns) {
844     MD.StartMutationSequence();
845     memcpy(CurrentUnitData, U.data(), U.size());
846     for (int i = 0; i < Options.MutateDepth; i++) {
847       size_t NewSize = MD.Mutate(CurrentUnitData, U.size(), MaxMutationLen);
848       assert(NewSize > 0 && NewSize <= MaxMutationLen);
849       ExecuteCallback(CurrentUnitData, NewSize);
850       PrintPulseAndReportSlowInput(CurrentUnitData, NewSize);
851       TryDetectingAMemoryLeak(CurrentUnitData, NewSize,
852                               /*DuringInitialCorpusExecution*/ false);
853     }
854   }
855 }
856
857 } // namespace fuzzer
858
859 extern "C" {
860
861 ATTRIBUTE_INTERFACE size_t
862 LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
863   assert(fuzzer::F);
864   return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize);
865 }
866
867 } // extern "C"