blob: 8b430c5428d880d244f40ede2db98a9519ef4efb [file] [log] [blame]
George Karpenkovcccae792017-08-21 23:25:50 +00001//===- FuzzerLoop.cpp - Fuzzer's main loop --------------------------------===//
2//
Chandler Carrutha291af62019-01-19 08:50:56 +00003// 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
George Karpenkovcccae792017-08-21 23:25:50 +00006//
7//===----------------------------------------------------------------------===//
8// Fuzzer's main loop.
9//===----------------------------------------------------------------------===//
10
11#include "FuzzerCorpus.h"
12#include "FuzzerIO.h"
13#include "FuzzerInternal.h"
14#include "FuzzerMutate.h"
Dokyung Songfa6011b2020-07-14 22:25:51 +000015#include "FuzzerPlatform.h"
George Karpenkovcccae792017-08-21 23:25:50 +000016#include "FuzzerRandom.h"
George Karpenkovcccae792017-08-21 23:25:50 +000017#include "FuzzerTracePC.h"
18#include <algorithm>
19#include <cstring>
20#include <memory>
Vitaly Bukab039e5d2017-11-01 03:02:59 +000021#include <mutex>
George Karpenkovcccae792017-08-21 23:25:50 +000022#include <set>
23
24#if defined(__has_include)
25#if __has_include(<sanitizer / lsan_interface.h>)
26#include <sanitizer/lsan_interface.h>
27#endif
28#endif
29
30#define NO_SANITIZE_MEMORY
31#if defined(__has_feature)
32#if __has_feature(memory_sanitizer)
33#undef NO_SANITIZE_MEMORY
34#define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
35#endif
36#endif
37
38namespace fuzzer {
39static const size_t kMaxUnitSizeToPrint = 256;
40
41thread_local bool Fuzzer::IsMyThread;
42
Matt Morehouse38259192018-07-17 16:12:00 +000043bool RunningUserCallback = false;
44
George Karpenkovcccae792017-08-21 23:25:50 +000045// Only one Fuzzer per process.
46static Fuzzer *F;
47
48// Leak detection is expensive, so we first check if there were more mallocs
49// than frees (using the sanitizer malloc hooks) and only then try to call lsan.
50struct MallocFreeTracer {
51 void Start(int TraceLevel) {
52 this->TraceLevel = TraceLevel;
53 if (TraceLevel)
54 Printf("MallocFreeTracer: START\n");
55 Mallocs = 0;
56 Frees = 0;
57 }
58 // Returns true if there were more mallocs than frees.
59 bool Stop() {
60 if (TraceLevel)
61 Printf("MallocFreeTracer: STOP %zd %zd (%s)\n", Mallocs.load(),
62 Frees.load(), Mallocs == Frees ? "same" : "DIFFERENT");
63 bool Result = Mallocs > Frees;
64 Mallocs = 0;
65 Frees = 0;
66 TraceLevel = 0;
67 return Result;
68 }
69 std::atomic<size_t> Mallocs;
70 std::atomic<size_t> Frees;
71 int TraceLevel = 0;
Vitaly Buka7ee7f0f2017-11-02 04:12:10 +000072
73 std::recursive_mutex TraceMutex;
74 bool TraceDisabled = false;
George Karpenkovcccae792017-08-21 23:25:50 +000075};
76
77static MallocFreeTracer AllocTracer;
78
Vitaly Buka7ee7f0f2017-11-02 04:12:10 +000079// Locks printing and avoids nested hooks triggered from mallocs/frees in
80// sanitizer.
81class TraceLock {
82public:
83 TraceLock() : Lock(AllocTracer.TraceMutex) {
84 AllocTracer.TraceDisabled = !AllocTracer.TraceDisabled;
85 }
86 ~TraceLock() { AllocTracer.TraceDisabled = !AllocTracer.TraceDisabled; }
87
88 bool IsDisabled() const {
89 // This is already inverted value.
90 return !AllocTracer.TraceDisabled;
91 }
92
93private:
94 std::lock_guard<std::recursive_mutex> Lock;
95};
Vitaly Bukab039e5d2017-11-01 03:02:59 +000096
George Karpenkovcccae792017-08-21 23:25:50 +000097ATTRIBUTE_NO_SANITIZE_MEMORY
98void MallocHook(const volatile void *ptr, size_t size) {
99 size_t N = AllocTracer.Mallocs++;
100 F->HandleMalloc(size);
101 if (int TraceLevel = AllocTracer.TraceLevel) {
Vitaly Buka7ee7f0f2017-11-02 04:12:10 +0000102 TraceLock Lock;
103 if (Lock.IsDisabled())
104 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000105 Printf("MALLOC[%zd] %p %zd\n", N, ptr, size);
106 if (TraceLevel >= 2 && EF)
Matt Morehouse3711b102018-05-08 23:45:05 +0000107 PrintStackTrace();
George Karpenkovcccae792017-08-21 23:25:50 +0000108 }
109}
110
111ATTRIBUTE_NO_SANITIZE_MEMORY
112void FreeHook(const volatile void *ptr) {
113 size_t N = AllocTracer.Frees++;
114 if (int TraceLevel = AllocTracer.TraceLevel) {
Vitaly Buka7ee7f0f2017-11-02 04:12:10 +0000115 TraceLock Lock;
116 if (Lock.IsDisabled())
117 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000118 Printf("FREE[%zd] %p\n", N, ptr);
119 if (TraceLevel >= 2 && EF)
Matt Morehouse3711b102018-05-08 23:45:05 +0000120 PrintStackTrace();
George Karpenkovcccae792017-08-21 23:25:50 +0000121 }
122}
123
124// Crash on a single malloc that exceeds the rss limit.
125void Fuzzer::HandleMalloc(size_t Size) {
Kostya Serebryany37c260c2017-12-01 22:12:04 +0000126 if (!Options.MallocLimitMb || (Size >> 20) < (size_t)Options.MallocLimitMb)
George Karpenkovcccae792017-08-21 23:25:50 +0000127 return;
128 Printf("==%d== ERROR: libFuzzer: out-of-memory (malloc(%zd))\n", GetPid(),
129 Size);
130 Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
Matt Morehouse3711b102018-05-08 23:45:05 +0000131 PrintStackTrace();
George Karpenkovcccae792017-08-21 23:25:50 +0000132 DumpCurrentUnit("oom-");
133 Printf("SUMMARY: libFuzzer: out-of-memory\n");
134 PrintFinalStats();
Kostya Serebryany9eefe0a2019-02-09 00:16:21 +0000135 _Exit(Options.OOMExitCode); // Stop right now.
George Karpenkovcccae792017-08-21 23:25:50 +0000136}
137
138Fuzzer::Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
Wu, Yingcongd8ed2102023-03-09 22:10:07 -0800139 const FuzzingOptions &Options)
George Karpenkovcccae792017-08-21 23:25:50 +0000140 : CB(CB), Corpus(Corpus), MD(MD), Options(Options) {
141 if (EF->__sanitizer_set_death_callback)
142 EF->__sanitizer_set_death_callback(StaticDeathCallback);
143 assert(!F);
144 F = this;
145 TPC.ResetMaps();
146 IsMyThread = true;
147 if (Options.DetectLeaks && EF->__sanitizer_install_malloc_and_free_hooks)
148 EF->__sanitizer_install_malloc_and_free_hooks(MallocHook, FreeHook);
149 TPC.SetUseCounters(Options.UseCounters);
Kostya Serebryany8ec21332018-07-03 22:33:09 +0000150 TPC.SetUseValueProfileMask(Options.UseValueProfile);
George Karpenkovcccae792017-08-21 23:25:50 +0000151
152 if (Options.Verbosity)
153 TPC.PrintModuleInfo();
154 if (!Options.OutputCorpus.empty() && Options.ReloadIntervalSec)
155 EpochOfLastReadOfOutputCorpus = GetEpoch(Options.OutputCorpus);
156 MaxInputLen = MaxMutationLen = Options.MaxLen;
Kostya Serebryany8bb19a92019-02-16 01:23:41 +0000157 TmpMaxMutationLen = 0; // Will be set once we load the corpus.
George Karpenkovcccae792017-08-21 23:25:50 +0000158 AllocateCurrentUnitData();
159 CurrentUnitSize = 0;
160 memset(BaseSha1, 0, sizeof(BaseSha1));
161}
162
George Karpenkovcccae792017-08-21 23:25:50 +0000163void Fuzzer::AllocateCurrentUnitData() {
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000164 if (CurrentUnitData || MaxInputLen == 0)
165 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000166 CurrentUnitData = new uint8_t[MaxInputLen];
167}
168
169void Fuzzer::StaticDeathCallback() {
170 assert(F);
171 F->DeathCallback();
172}
173
174void Fuzzer::DumpCurrentUnit(const char *Prefix) {
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000175 if (!CurrentUnitData)
176 return; // Happens when running individual inputs.
Matt Morehouse13675a62018-07-09 23:51:08 +0000177 ScopedDisableMsanInterceptorChecks S;
Marco Vanottie1d706b2021-07-02 09:44:54 -0700178 MD.PrintMutationSequence();
George Karpenkovcccae792017-08-21 23:25:50 +0000179 Printf("; base unit: %s\n", Sha1ToString(BaseSha1).c_str());
180 size_t UnitSize = CurrentUnitSize;
181 if (UnitSize <= kMaxUnitSizeToPrint) {
182 PrintHexArray(CurrentUnitData, UnitSize, "\n");
183 PrintASCII(CurrentUnitData, UnitSize, "\n");
184 }
185 WriteUnitToFileWithPrefix({CurrentUnitData, CurrentUnitData + UnitSize},
186 Prefix);
187}
188
189NO_SANITIZE_MEMORY
190void Fuzzer::DeathCallback() {
191 DumpCurrentUnit("crash-");
192 PrintFinalStats();
193}
194
195void Fuzzer::StaticAlarmCallback() {
196 assert(F);
197 F->AlarmCallback();
198}
199
200void Fuzzer::StaticCrashSignalCallback() {
201 assert(F);
202 F->CrashCallback();
203}
204
205void Fuzzer::StaticExitCallback() {
206 assert(F);
207 F->ExitCallback();
208}
209
210void Fuzzer::StaticInterruptCallback() {
211 assert(F);
212 F->InterruptCallback();
213}
214
Kostya Serebryanyae41c212017-11-09 20:30:19 +0000215void Fuzzer::StaticGracefulExitCallback() {
216 assert(F);
217 F->GracefulExitRequested = true;
218 Printf("INFO: signal received, trying to exit gracefully\n");
219}
220
George Karpenkovcccae792017-08-21 23:25:50 +0000221void Fuzzer::StaticFileSizeExceedCallback() {
222 Printf("==%lu== ERROR: libFuzzer: file size exceeded\n", GetPid());
223 exit(1);
224}
225
226void Fuzzer::CrashCallback() {
Julian Lettner50e7a4b2019-01-31 01:24:01 +0000227 if (EF->__sanitizer_acquire_crash_state &&
228 !EF->__sanitizer_acquire_crash_state())
229 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000230 Printf("==%lu== ERROR: libFuzzer: deadly signal\n", GetPid());
Matt Morehouse3711b102018-05-08 23:45:05 +0000231 PrintStackTrace();
George Karpenkovcccae792017-08-21 23:25:50 +0000232 Printf("NOTE: libFuzzer has rudimentary signal handlers.\n"
233 " Combine libFuzzer with AddressSanitizer or similar for better "
234 "crash reports.\n");
235 Printf("SUMMARY: libFuzzer: deadly signal\n");
236 DumpCurrentUnit("crash-");
237 PrintFinalStats();
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000238 _Exit(Options.ErrorExitCode); // Stop right now.
George Karpenkovcccae792017-08-21 23:25:50 +0000239}
240
241void Fuzzer::ExitCallback() {
Matt Morehouse38259192018-07-17 16:12:00 +0000242 if (!RunningUserCallback)
George Karpenkovcccae792017-08-21 23:25:50 +0000243 return; // This exit did not come from the user callback
Matt Morehouse53bf7e72018-05-01 21:01:53 +0000244 if (EF->__sanitizer_acquire_crash_state &&
245 !EF->__sanitizer_acquire_crash_state())
246 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000247 Printf("==%lu== ERROR: libFuzzer: fuzz target exited\n", GetPid());
Matt Morehouse3711b102018-05-08 23:45:05 +0000248 PrintStackTrace();
George Karpenkovcccae792017-08-21 23:25:50 +0000249 Printf("SUMMARY: libFuzzer: fuzz target exited\n");
250 DumpCurrentUnit("crash-");
251 PrintFinalStats();
252 _Exit(Options.ErrorExitCode);
253}
254
Kostya Serebryanyae41c212017-11-09 20:30:19 +0000255void Fuzzer::MaybeExitGracefully() {
Kostya Serebryany8c09d8e2019-02-08 21:27:23 +0000256 if (!F->GracefulExitRequested) return;
Kostya Serebryanyae41c212017-11-09 20:30:19 +0000257 Printf("==%lu== INFO: libFuzzer: exiting as requested\n", GetPid());
Yuanfang Chen7b418fb2020-02-10 18:22:09 -0800258 RmDirRecursive(TempPath("FuzzWithFork", ".dir"));
Kostya Serebryany8c09d8e2019-02-08 21:27:23 +0000259 F->PrintFinalStats();
Kostya Serebryanyae41c212017-11-09 20:30:19 +0000260 _Exit(0);
261}
262
Maxim Schesslerb4538f82022-08-15 11:44:06 -0700263int Fuzzer::InterruptExitCode() {
264 assert(F);
265 return F->Options.InterruptExitCode;
266}
267
George Karpenkovcccae792017-08-21 23:25:50 +0000268void Fuzzer::InterruptCallback() {
269 Printf("==%lu== libFuzzer: run interrupted; exiting\n", GetPid());
270 PrintFinalStats();
Matt Morehousefe9268e2019-04-26 00:17:41 +0000271 ScopedDisableMsanInterceptorChecks S; // RmDirRecursive may call opendir().
Yuanfang Chen7b418fb2020-02-10 18:22:09 -0800272 RmDirRecursive(TempPath("FuzzWithFork", ".dir"));
Kostya Serebryany9eefe0a2019-02-09 00:16:21 +0000273 // Stop right now, don't perform any at-exit actions.
274 _Exit(Options.InterruptExitCode);
George Karpenkovcccae792017-08-21 23:25:50 +0000275}
276
277NO_SANITIZE_MEMORY
278void Fuzzer::AlarmCallback() {
279 assert(Options.UnitTimeoutSec > 0);
Jake Ehrlich8f3f4e02019-10-09 21:01:50 +0000280 // In Windows and Fuchsia, Alarm callback is executed by a different thread.
Kamil Rytarowskid8f18de2018-11-06 01:28:01 +0000281 // NetBSD's current behavior needs this change too.
Jake Ehrlich8f3f4e02019-10-09 21:01:50 +0000282#if !LIBFUZZER_WINDOWS && !LIBFUZZER_NETBSD && !LIBFUZZER_FUCHSIA
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000283 if (!InFuzzingThread())
284 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000285#endif
Matt Morehouse38259192018-07-17 16:12:00 +0000286 if (!RunningUserCallback)
George Karpenkovcccae792017-08-21 23:25:50 +0000287 return; // We have not started running units yet.
288 size_t Seconds =
289 duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
290 if (Seconds == 0)
291 return;
292 if (Options.Verbosity >= 2)
293 Printf("AlarmCallback %zd\n", Seconds);
294 if (Seconds >= (size_t)Options.UnitTimeoutSec) {
Matt Morehouse53bf7e72018-05-01 21:01:53 +0000295 if (EF->__sanitizer_acquire_crash_state &&
296 !EF->__sanitizer_acquire_crash_state())
297 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000298 Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds);
299 Printf(" and the timeout value is %d (use -timeout=N to change)\n",
300 Options.UnitTimeoutSec);
301 DumpCurrentUnit("timeout-");
Wu, Yingcongb71f3652023-03-07 21:55:33 -0800302 Printf("==%lu== ERROR: libFuzzer: timeout after %zu seconds\n", GetPid(),
George Karpenkovcccae792017-08-21 23:25:50 +0000303 Seconds);
Matt Morehouse3711b102018-05-08 23:45:05 +0000304 PrintStackTrace();
George Karpenkovcccae792017-08-21 23:25:50 +0000305 Printf("SUMMARY: libFuzzer: timeout\n");
306 PrintFinalStats();
307 _Exit(Options.TimeoutExitCode); // Stop right now.
308 }
309}
310
311void Fuzzer::RssLimitCallback() {
Matt Morehouse53bf7e72018-05-01 21:01:53 +0000312 if (EF->__sanitizer_acquire_crash_state &&
313 !EF->__sanitizer_acquire_crash_state())
314 return;
Wu, Yingcongb71f3652023-03-07 21:55:33 -0800315 Printf("==%lu== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %dMb)\n",
316 GetPid(), GetPeakRSSMb(), Options.RssLimitMb);
George Karpenkovcccae792017-08-21 23:25:50 +0000317 Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
Matt Morehouse3711b102018-05-08 23:45:05 +0000318 PrintMemoryProfile();
George Karpenkovcccae792017-08-21 23:25:50 +0000319 DumpCurrentUnit("oom-");
320 Printf("SUMMARY: libFuzzer: out-of-memory\n");
321 PrintFinalStats();
Kostya Serebryany9eefe0a2019-02-09 00:16:21 +0000322 _Exit(Options.OOMExitCode); // Stop right now.
George Karpenkovcccae792017-08-21 23:25:50 +0000323}
324
Max Morozba74fad2019-08-12 20:21:27 +0000325void Fuzzer::PrintStats(const char *Where, const char *End, size_t Units,
326 size_t Features) {
George Karpenkovcccae792017-08-21 23:25:50 +0000327 size_t ExecPerSec = execPerSec();
328 if (!Options.Verbosity)
329 return;
330 Printf("#%zd\t%s", TotalNumberOfRuns, Where);
331 if (size_t N = TPC.GetTotalPCCoverage())
332 Printf(" cov: %zd", N);
Max Morozba74fad2019-08-12 20:21:27 +0000333 if (size_t N = Features ? Features : Corpus.NumFeatures())
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000334 Printf(" ft: %zd", N);
George Karpenkovcccae792017-08-21 23:25:50 +0000335 if (!Corpus.empty()) {
336 Printf(" corp: %zd", Corpus.NumActiveUnits());
337 if (size_t N = Corpus.SizeInBytes()) {
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000338 if (N < (1 << 14))
George Karpenkovcccae792017-08-21 23:25:50 +0000339 Printf("/%zdb", N);
340 else if (N < (1 << 24))
341 Printf("/%zdKb", N >> 10);
342 else
343 Printf("/%zdMb", N >> 20);
344 }
Kostya Serebryany59587be2018-05-16 23:26:37 +0000345 if (size_t FF = Corpus.NumInputsThatTouchFocusFunction())
346 Printf(" focus: %zd", FF);
George Karpenkovcccae792017-08-21 23:25:50 +0000347 }
Matt Morehouse426cabb2018-02-22 19:00:17 +0000348 if (TmpMaxMutationLen)
349 Printf(" lim: %zd", TmpMaxMutationLen);
George Karpenkovcccae792017-08-21 23:25:50 +0000350 if (Units)
351 Printf(" units: %zd", Units);
352
353 Printf(" exec/s: %zd", ExecPerSec);
354 Printf(" rss: %zdMb", GetPeakRSSMb());
355 Printf("%s", End);
356}
357
358void Fuzzer::PrintFinalStats() {
Max Morozef43aac2020-10-23 11:07:30 -0700359 if (Options.PrintFullCoverage)
360 TPC.PrintCoverage(/*PrintAllCounters=*/true);
George Karpenkovcccae792017-08-21 23:25:50 +0000361 if (Options.PrintCoverage)
Max Morozef43aac2020-10-23 11:07:30 -0700362 TPC.PrintCoverage(/*PrintAllCounters=*/false);
George Karpenkovcccae792017-08-21 23:25:50 +0000363 if (Options.PrintCorpusStats)
364 Corpus.PrintStats();
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000365 if (!Options.PrintFinalStats)
366 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000367 size_t ExecPerSec = execPerSec();
368 Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns);
369 Printf("stat::average_exec_per_sec: %zd\n", ExecPerSec);
370 Printf("stat::new_units_added: %zd\n", NumberOfNewUnitsAdded);
Wu, Yingcongb71f3652023-03-07 21:55:33 -0800371 Printf("stat::slowest_unit_time_sec: %ld\n", TimeOfLongestUnitInSeconds);
George Karpenkovcccae792017-08-21 23:25:50 +0000372 Printf("stat::peak_rss_mb: %zd\n", GetPeakRSSMb());
373}
374
375void Fuzzer::SetMaxInputLen(size_t MaxInputLen) {
376 assert(this->MaxInputLen == 0); // Can only reset MaxInputLen from 0 to non-0.
377 assert(MaxInputLen);
378 this->MaxInputLen = MaxInputLen;
379 this->MaxMutationLen = MaxInputLen;
380 AllocateCurrentUnitData();
381 Printf("INFO: -max_len is not provided; "
382 "libFuzzer will not generate inputs larger than %zd bytes\n",
383 MaxInputLen);
384}
385
386void Fuzzer::SetMaxMutationLen(size_t MaxMutationLen) {
387 assert(MaxMutationLen && MaxMutationLen <= MaxInputLen);
388 this->MaxMutationLen = MaxMutationLen;
389}
390
391void Fuzzer::CheckExitOnSrcPosOrItem() {
392 if (!Options.ExitOnSrcPos.empty()) {
Kostya Serebryany6276d3d2021-08-03 10:04:16 -0700393 static auto *PCsSet = new std::set<uintptr_t>;
Kostya Serebryany1024ccc2019-02-14 23:12:33 +0000394 auto HandlePC = [&](const TracePC::PCTableEntry *TE) {
395 if (!PCsSet->insert(TE->PC).second)
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000396 return;
Kostya Serebryany1024ccc2019-02-14 23:12:33 +0000397 std::string Descr = DescribePC("%F %L", TE->PC + 1);
George Karpenkovcccae792017-08-21 23:25:50 +0000398 if (Descr.find(Options.ExitOnSrcPos) != std::string::npos) {
399 Printf("INFO: found line matching '%s', exiting.\n",
400 Options.ExitOnSrcPos.c_str());
401 _Exit(0);
402 }
403 };
404 TPC.ForEachObservedPC(HandlePC);
405 }
406 if (!Options.ExitOnItem.empty()) {
407 if (Corpus.HasUnit(Options.ExitOnItem)) {
408 Printf("INFO: found item with checksum '%s', exiting.\n",
409 Options.ExitOnItem.c_str());
410 _Exit(0);
411 }
412 }
413}
414
415void Fuzzer::RereadOutputCorpus(size_t MaxSize) {
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000416 if (Options.OutputCorpus.empty() || !Options.ReloadIntervalSec)
417 return;
Kostya Serebryany6276d3d2021-08-03 10:04:16 -0700418 std::vector<Unit> AdditionalCorpus;
419 std::vector<std::string> AdditionalCorpusPaths;
Alexey Vishnyakov978a3f62021-04-16 09:21:49 -0700420 ReadDirToVectorOfUnits(
421 Options.OutputCorpus.c_str(), &AdditionalCorpus,
422 &EpochOfLastReadOfOutputCorpus, MaxSize,
423 /*ExitOnError*/ false,
424 (Options.Verbosity >= 2 ? &AdditionalCorpusPaths : nullptr));
George Karpenkovcccae792017-08-21 23:25:50 +0000425 if (Options.Verbosity >= 2)
426 Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
427 bool Reloaded = false;
Alexey Vishnyakov978a3f62021-04-16 09:21:49 -0700428 for (size_t i = 0; i != AdditionalCorpus.size(); ++i) {
429 auto &U = AdditionalCorpus[i];
George Karpenkovcccae792017-08-21 23:25:50 +0000430 if (U.size() > MaxSize)
431 U.resize(MaxSize);
432 if (!Corpus.HasUnit(U)) {
433 if (RunOne(U.data(), U.size())) {
434 CheckExitOnSrcPosOrItem();
435 Reloaded = true;
Alexey Vishnyakov978a3f62021-04-16 09:21:49 -0700436 if (Options.Verbosity >= 2)
437 Printf("Reloaded %s\n", AdditionalCorpusPaths[i].c_str());
George Karpenkovcccae792017-08-21 23:25:50 +0000438 }
439 }
440 }
441 if (Reloaded)
442 PrintStats("RELOAD");
443}
444
George Karpenkovcccae792017-08-21 23:25:50 +0000445void Fuzzer::PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size) {
446 auto TimeOfUnit =
447 duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
448 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) &&
449 secondsSinceProcessStartUp() >= 2)
450 PrintStats("pulse ");
Aaron Greenb25f4812021-03-11 16:00:53 -0800451 auto Threshhold =
452 static_cast<long>(static_cast<double>(TimeOfLongestUnitInSeconds) * 1.1);
453 if (TimeOfUnit > Threshhold && TimeOfUnit >= Options.ReportSlowUnits) {
George Karpenkovcccae792017-08-21 23:25:50 +0000454 TimeOfLongestUnitInSeconds = TimeOfUnit;
Wu, Yingcongb71f3652023-03-07 21:55:33 -0800455 Printf("Slowest unit: %ld s:\n", TimeOfLongestUnitInSeconds);
George Karpenkovcccae792017-08-21 23:25:50 +0000456 WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-");
457 }
458}
459
Kostya Serebryany99abd8a2019-04-13 00:20:31 +0000460static void WriteFeatureSetToFile(const std::string &FeaturesDir,
Kostya Serebryanye00923c2019-04-13 01:57:33 +0000461 const std::string &FileName,
Kostya Serebryany6276d3d2021-08-03 10:04:16 -0700462 const std::vector<uint32_t> &FeatureSet) {
Kostya Serebryany99abd8a2019-04-13 00:20:31 +0000463 if (FeaturesDir.empty() || FeatureSet.empty()) return;
464 WriteToFile(reinterpret_cast<const uint8_t *>(FeatureSet.data()),
465 FeatureSet.size() * sizeof(FeatureSet[0]),
Kostya Serebryanye00923c2019-04-13 01:57:33 +0000466 DirPlusFile(FeaturesDir, FileName));
Kostya Serebryany99abd8a2019-04-13 00:20:31 +0000467}
468
469static void RenameFeatureSetFile(const std::string &FeaturesDir,
470 const std::string &OldFile,
471 const std::string &NewFile) {
472 if (FeaturesDir.empty()) return;
473 RenameFile(DirPlusFile(FeaturesDir, OldFile),
474 DirPlusFile(FeaturesDir, NewFile));
475}
476
Dokyung Song44ae51e2020-07-08 19:30:53 +0000477static void WriteEdgeToMutationGraphFile(const std::string &MutationGraphFile,
478 const InputInfo *II,
479 const InputInfo *BaseII,
480 const std::string &MS) {
481 if (MutationGraphFile.empty())
482 return;
483
484 std::string Sha1 = Sha1ToString(II->Sha1);
485
486 std::string OutputString;
487
488 // Add a new vertex.
489 OutputString.append("\"");
490 OutputString.append(Sha1);
491 OutputString.append("\"\n");
492
493 // Add a new edge if there is base input.
494 if (BaseII) {
495 std::string BaseSha1 = Sha1ToString(BaseII->Sha1);
496 OutputString.append("\"");
497 OutputString.append(BaseSha1);
498 OutputString.append("\" -> \"");
499 OutputString.append(Sha1);
500 OutputString.append("\" [label=\"");
501 OutputString.append(MS);
502 OutputString.append("\"];\n");
503 }
504
505 AppendToFile(OutputString, MutationGraphFile);
506}
507
George Karpenkovcccae792017-08-21 23:25:50 +0000508bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile,
Dokyung Song5f46ebc2020-07-31 00:07:20 +0000509 InputInfo *II, bool ForceAddToCorpus,
510 bool *FoundUniqFeatures) {
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000511 if (!Size)
512 return false;
Aaron Greenb25f4812021-03-11 16:00:53 -0800513 // Largest input length should be INT_MAX.
514 assert(Size < std::numeric_limits<uint32_t>::max());
George Karpenkovcccae792017-08-21 23:25:50 +0000515
Kostya Serebryany5763e452022-06-28 11:36:30 -0700516 if(!ExecuteCallback(Data, Size)) return false;
Dokyung Songa9708642020-08-17 16:59:59 +0000517 auto TimeOfUnit = duration_cast<microseconds>(UnitStopTime - UnitStartTime);
George Karpenkovcccae792017-08-21 23:25:50 +0000518
519 UniqFeatureSetTmp.clear();
520 size_t FoundUniqFeaturesOfII = 0;
521 size_t NumUpdatesBefore = Corpus.NumFeatureUpdates();
Aaron Greenb25f4812021-03-11 16:00:53 -0800522 TPC.CollectFeatures([&](uint32_t Feature) {
523 if (Corpus.AddFeature(Feature, static_cast<uint32_t>(Size), Options.Shrink))
George Karpenkovcccae792017-08-21 23:25:50 +0000524 UniqFeatureSetTmp.push_back(Feature);
Matt Morehouse1fbdd5d2020-05-19 10:28:18 -0700525 if (Options.Entropic)
526 Corpus.UpdateFeatureFrequency(II, Feature);
Dokyung Song5f46ebc2020-07-31 00:07:20 +0000527 if (Options.ReduceInputs && II && !II->NeverReduce)
George Karpenkovcccae792017-08-21 23:25:50 +0000528 if (std::binary_search(II->UniqFeatureSet.begin(),
529 II->UniqFeatureSet.end(), Feature))
530 FoundUniqFeaturesOfII++;
531 });
Kostya Serebryany4dc76f42017-12-01 19:18:38 +0000532 if (FoundUniqFeatures)
533 *FoundUniqFeatures = FoundUniqFeaturesOfII;
George Karpenkovcccae792017-08-21 23:25:50 +0000534 PrintPulseAndReportSlowInput(Data, Size);
535 size_t NumNewFeatures = Corpus.NumFeatureUpdates() - NumUpdatesBefore;
Dokyung Song5f46ebc2020-07-31 00:07:20 +0000536 if (NumNewFeatures || ForceAddToCorpus) {
George Karpenkovcccae792017-08-21 23:25:50 +0000537 TPC.UpdateObservedPCs();
Dokyung Song5f46ebc2020-07-31 00:07:20 +0000538 auto NewII =
539 Corpus.AddToCorpus({Data, Data + Size}, NumNewFeatures, MayDeleteFile,
540 TPC.ObservedFocusFunction(), ForceAddToCorpus,
Dokyung Songa9708642020-08-17 16:59:59 +0000541 TimeOfUnit, UniqFeatureSetTmp, DFT, II);
Kostya Serebryanye00923c2019-04-13 01:57:33 +0000542 WriteFeatureSetToFile(Options.FeaturesDir, Sha1ToString(NewII->Sha1),
Kostya Serebryany99abd8a2019-04-13 00:20:31 +0000543 NewII->UniqFeatureSet);
Dokyung Song44ae51e2020-07-08 19:30:53 +0000544 WriteEdgeToMutationGraphFile(Options.MutationGraphFile, NewII, II,
Marco Vanottie1d706b2021-07-02 09:44:54 -0700545 MD.MutationSequence());
George Karpenkovcccae792017-08-21 23:25:50 +0000546 return true;
547 }
548 if (II && FoundUniqFeaturesOfII &&
Kostya Serebryany4c9290b2018-06-07 01:40:20 +0000549 II->DataFlowTraceForFocusFunction.empty() &&
George Karpenkovcccae792017-08-21 23:25:50 +0000550 FoundUniqFeaturesOfII == II->UniqFeatureSet.size() &&
551 II->U.size() > Size) {
Kostya Serebryany99abd8a2019-04-13 00:20:31 +0000552 auto OldFeaturesFile = Sha1ToString(II->Sha1);
PZ Reada925f492021-10-20 06:14:22 -0700553 Corpus.Replace(II, {Data, Data + Size}, TimeOfUnit);
Kostya Serebryany99abd8a2019-04-13 00:20:31 +0000554 RenameFeatureSetFile(Options.FeaturesDir, OldFeaturesFile,
555 Sha1ToString(II->Sha1));
George Karpenkovcccae792017-08-21 23:25:50 +0000556 return true;
557 }
558 return false;
559}
560
Max Morozef43aac2020-10-23 11:07:30 -0700561void Fuzzer::TPCUpdateObservedPCs() { TPC.UpdateObservedPCs(); }
562
George Karpenkovcccae792017-08-21 23:25:50 +0000563size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const {
564 assert(InFuzzingThread());
565 *Data = CurrentUnitData;
566 return CurrentUnitSize;
567}
568
569void Fuzzer::CrashOnOverwrittenData() {
Mitch Phillips99e26f82019-09-26 00:54:30 +0000570 Printf("==%d== ERROR: libFuzzer: fuzz target overwrites its const input\n",
George Karpenkovcccae792017-08-21 23:25:50 +0000571 GetPid());
Mitch Phillips66b77512019-09-27 22:04:36 +0000572 PrintStackTrace();
573 Printf("SUMMARY: libFuzzer: overwrites-const-input\n");
George Karpenkovcccae792017-08-21 23:25:50 +0000574 DumpCurrentUnit("crash-");
Mitch Phillips66b77512019-09-27 22:04:36 +0000575 PrintFinalStats();
George Karpenkovcccae792017-08-21 23:25:50 +0000576 _Exit(Options.ErrorExitCode); // Stop right now.
577}
578
579// Compare two arrays, but not all bytes if the arrays are large.
580static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) {
581 const size_t Limit = 64;
582 if (Size <= 64)
583 return !memcmp(A, B, Size);
584 // Compare first and last Limit/2 bytes.
585 return !memcmp(A, B, Limit / 2) &&
586 !memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2);
587}
588
Jonas Paulsson9337e092021-03-04 14:55:41 -0600589// This method is not inlined because it would cause a test to fail where it
590// is part of the stack unwinding. See D97975 for details.
Kostya Serebryany5763e452022-06-28 11:36:30 -0700591ATTRIBUTE_NOINLINE bool Fuzzer::ExecuteCallback(const uint8_t *Data,
Matt Morehouse1dbbff12021-03-12 14:36:57 -0800592 size_t Size) {
George Karpenkovcccae792017-08-21 23:25:50 +0000593 TPC.RecordInitialStack();
594 TotalNumberOfRuns++;
595 assert(InFuzzingThread());
George Karpenkovcccae792017-08-21 23:25:50 +0000596 // We copy the contents of Unit into a separate heap buffer
597 // so that we reliably find buffer overflows in it.
598 uint8_t *DataCopy = new uint8_t[Size];
599 memcpy(DataCopy, Data, Size);
Matt Morehouse13675a62018-07-09 23:51:08 +0000600 if (EF->__msan_unpoison)
601 EF->__msan_unpoison(DataCopy, Size);
Matt Morehouse56d39e72019-05-09 22:48:46 +0000602 if (EF->__msan_unpoison_param)
603 EF->__msan_unpoison_param(2);
George Karpenkovcccae792017-08-21 23:25:50 +0000604 if (CurrentUnitData && CurrentUnitData != Data)
605 memcpy(CurrentUnitData, Data, Size);
606 CurrentUnitSize = Size;
Kostya Serebryany5763e452022-06-28 11:36:30 -0700607 int CBRes = 0;
Matt Morehouse13675a62018-07-09 23:51:08 +0000608 {
609 ScopedEnableMsanInterceptorChecks S;
610 AllocTracer.Start(Options.TraceMalloc);
611 UnitStartTime = system_clock::now();
612 TPC.ResetMaps();
Matt Morehouse38259192018-07-17 16:12:00 +0000613 RunningUserCallback = true;
Kostya Serebryany5763e452022-06-28 11:36:30 -0700614 CBRes = CB(DataCopy, Size);
Matt Morehouse38259192018-07-17 16:12:00 +0000615 RunningUserCallback = false;
Matt Morehouse13675a62018-07-09 23:51:08 +0000616 UnitStopTime = system_clock::now();
Kostya Serebryany5763e452022-06-28 11:36:30 -0700617 assert(CBRes == 0 || CBRes == -1);
Matt Morehouse13675a62018-07-09 23:51:08 +0000618 HasMoreMallocsThanFrees = AllocTracer.Stop();
619 }
George Karpenkovcccae792017-08-21 23:25:50 +0000620 if (!LooseMemeq(DataCopy, Data, Size))
621 CrashOnOverwrittenData();
622 CurrentUnitSize = 0;
623 delete[] DataCopy;
Kostya Serebryany5763e452022-06-28 11:36:30 -0700624 return CBRes == 0;
George Karpenkovcccae792017-08-21 23:25:50 +0000625}
626
Kostya Serebryany5c6dd422019-02-12 00:12:33 +0000627std::string Fuzzer::WriteToOutputCorpus(const Unit &U) {
George Karpenkovcccae792017-08-21 23:25:50 +0000628 if (Options.OnlyASCII)
629 assert(IsASCII(U));
630 if (Options.OutputCorpus.empty())
Kostya Serebryany5c6dd422019-02-12 00:12:33 +0000631 return "";
George Karpenkovcccae792017-08-21 23:25:50 +0000632 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
633 WriteToFile(U, Path);
634 if (Options.Verbosity >= 2)
635 Printf("Written %zd bytes to %s\n", U.size(), Path.c_str());
Kostya Serebryany5c6dd422019-02-12 00:12:33 +0000636 return Path;
George Karpenkovcccae792017-08-21 23:25:50 +0000637}
638
639void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
640 if (!Options.SaveArtifacts)
641 return;
642 std::string Path = Options.ArtifactPrefix + Prefix + Hash(U);
643 if (!Options.ExactArtifactPath.empty())
644 Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix.
645 WriteToFile(U, Path);
646 Printf("artifact_prefix='%s'; Test unit written to %s\n",
647 Options.ArtifactPrefix.c_str(), Path.c_str());
648 if (U.size() <= kMaxUnitSizeToPrint)
649 Printf("Base64: %s\n", Base64(U).c_str());
650}
651
652void Fuzzer::PrintStatusForNewUnit(const Unit &U, const char *Text) {
653 if (!Options.PrintNEW)
654 return;
655 PrintStats(Text, "");
656 if (Options.Verbosity) {
657 Printf(" L: %zd/%zd ", U.size(), Corpus.MaxInputSize());
Marco Vanottie1d706b2021-07-02 09:44:54 -0700658 MD.PrintMutationSequence(Options.Verbosity >= 2);
George Karpenkovcccae792017-08-21 23:25:50 +0000659 Printf("\n");
660 }
661}
662
663void Fuzzer::ReportNewCoverage(InputInfo *II, const Unit &U) {
664 II->NumSuccessfullMutations++;
665 MD.RecordSuccessfulMutationSequence();
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000666 PrintStatusForNewUnit(U, II->Reduced ? "REDUCE" : "NEW ");
George Karpenkovcccae792017-08-21 23:25:50 +0000667 WriteToOutputCorpus(U);
668 NumberOfNewUnitsAdded++;
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000669 CheckExitOnSrcPosOrItem(); // Check only after the unit is saved to corpus.
George Karpenkovcccae792017-08-21 23:25:50 +0000670 LastCorpusUpdateRun = TotalNumberOfRuns;
George Karpenkovcccae792017-08-21 23:25:50 +0000671}
672
673// Tries detecting a memory leak on the particular input that we have just
674// executed before calling this function.
675void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
676 bool DuringInitialCorpusExecution) {
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000677 if (!HasMoreMallocsThanFrees)
678 return; // mallocs==frees, a leak is unlikely.
679 if (!Options.DetectLeaks)
680 return;
Max Morozff406c92017-09-12 02:01:54 +0000681 if (!DuringInitialCorpusExecution &&
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000682 TotalNumberOfRuns >= Options.MaxNumberOfRuns)
683 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000684 if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) ||
685 !(EF->__lsan_do_recoverable_leak_check))
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000686 return; // No lsan.
George Karpenkovcccae792017-08-21 23:25:50 +0000687 // Run the target once again, but with lsan disabled so that if there is
688 // a real leak we do not report it twice.
689 EF->__lsan_disable();
690 ExecuteCallback(Data, Size);
691 EF->__lsan_enable();
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000692 if (!HasMoreMallocsThanFrees)
693 return; // a leak is unlikely.
George Karpenkovcccae792017-08-21 23:25:50 +0000694 if (NumberOfLeakDetectionAttempts++ > 1000) {
695 Options.DetectLeaks = false;
696 Printf("INFO: libFuzzer disabled leak detection after every mutation.\n"
697 " Most likely the target function accumulates allocated\n"
698 " memory in a global state w/o actually leaking it.\n"
699 " You may try running this binary with -trace_malloc=[12]"
700 " to get a trace of mallocs and frees.\n"
701 " If LeakSanitizer is enabled in this process it will still\n"
702 " run on the process shutdown.\n");
703 return;
704 }
705 // Now perform the actual lsan pass. This is expensive and we must ensure
706 // we don't call it too often.
707 if (EF->__lsan_do_recoverable_leak_check()) { // Leak is found, report it.
708 if (DuringInitialCorpusExecution)
709 Printf("\nINFO: a leak has been found in the initial corpus.\n\n");
710 Printf("INFO: to ignore leaks on libFuzzer side use -detect_leaks=0.\n\n");
711 CurrentUnitSize = Size;
712 DumpCurrentUnit("leak-");
713 PrintFinalStats();
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000714 _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on.
George Karpenkovcccae792017-08-21 23:25:50 +0000715 }
716}
717
718void Fuzzer::MutateAndTestOne() {
719 MD.StartMutationSequence();
720
721 auto &II = Corpus.ChooseUnitToMutate(MD.GetRand());
Dokyung Song5eeb08b2020-09-01 16:22:59 +0000722 if (Options.DoCrossOver) {
723 auto &CrossOverII = Corpus.ChooseUnitToCrossOverWith(
724 MD.GetRand(), Options.CrossOverUniformDist);
725 MD.SetCrossOverWith(&CrossOverII.U);
726 }
George Karpenkovcccae792017-08-21 23:25:50 +0000727 const auto &U = II.U;
728 memcpy(BaseSha1, II.Sha1, sizeof(BaseSha1));
729 assert(CurrentUnitData);
730 size_t Size = U.size();
731 assert(Size <= MaxInputLen && "Oversized Unit");
732 memcpy(CurrentUnitData, U.data(), Size);
733
734 assert(MaxMutationLen > 0);
735
736 size_t CurrentMaxMutationLen =
737 Min(MaxMutationLen, Max(U.size(), TmpMaxMutationLen));
738 assert(CurrentMaxMutationLen > 0);
739
740 for (int i = 0; i < Options.MutateDepth; i++) {
741 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
742 break;
Kostya Serebryanyae41c212017-11-09 20:30:19 +0000743 MaybeExitGracefully();
George Karpenkovcccae792017-08-21 23:25:50 +0000744 size_t NewSize = 0;
Kostya Serebryany6d2ecdf2018-07-19 01:23:32 +0000745 if (II.HasFocusFunction && !II.DataFlowTraceForFocusFunction.empty() &&
746 Size <= CurrentMaxMutationLen)
747 NewSize = MD.MutateWithMask(CurrentUnitData, Size, Size,
748 II.DataFlowTraceForFocusFunction);
Matt Morehousefe9268e2019-04-26 00:17:41 +0000749
Max Moroz952f2862019-04-11 16:24:53 +0000750 // If MutateWithMask either failed or wasn't called, call default Mutate.
751 if (!NewSize)
Kostya Serebryany6d2ecdf2018-07-19 01:23:32 +0000752 NewSize = MD.Mutate(CurrentUnitData, Size, CurrentMaxMutationLen);
George Karpenkovcccae792017-08-21 23:25:50 +0000753 assert(NewSize > 0 && "Mutator returned empty unit");
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000754 assert(NewSize <= CurrentMaxMutationLen && "Mutator return oversized unit");
George Karpenkovcccae792017-08-21 23:25:50 +0000755 Size = NewSize;
756 II.NumExecutedMutations++;
Matt Morehouse1fbdd5d2020-05-19 10:28:18 -0700757 Corpus.IncrementNumExecutedMutations();
George Karpenkovcccae792017-08-21 23:25:50 +0000758
Kostya Serebryany4dc76f42017-12-01 19:18:38 +0000759 bool FoundUniqFeatures = false;
760 bool NewCov = RunOne(CurrentUnitData, Size, /*MayDeleteFile=*/true, &II,
Dokyung Song5f46ebc2020-07-31 00:07:20 +0000761 /*ForceAddToCorpus*/ false, &FoundUniqFeatures);
George Karpenkovcccae792017-08-21 23:25:50 +0000762 TryDetectingAMemoryLeak(CurrentUnitData, Size,
763 /*DuringInitialCorpusExecution*/ false);
Kostya Serebryany4dc76f42017-12-01 19:18:38 +0000764 if (NewCov) {
Matt Morehousef8ef8242017-11-09 20:44:08 +0000765 ReportNewCoverage(&II, {CurrentUnitData, CurrentUnitData + Size});
Kostya Serebryany4dc76f42017-12-01 19:18:38 +0000766 break; // We will mutate this input more in the next rounds.
767 }
768 if (Options.ReduceDepth && !FoundUniqFeatures)
Matt Morehouse56d39e72019-05-09 22:48:46 +0000769 break;
George Karpenkovcccae792017-08-21 23:25:50 +0000770 }
Matt Morehouse1fbdd5d2020-05-19 10:28:18 -0700771
772 II.NeedsEnergyUpdate = true;
George Karpenkovcccae792017-08-21 23:25:50 +0000773}
774
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000775void Fuzzer::PurgeAllocator() {
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000776 if (Options.PurgeAllocatorIntervalSec < 0 || !EF->__sanitizer_purge_allocator)
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000777 return;
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000778 if (duration_cast<seconds>(system_clock::now() -
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000779 LastAllocatorPurgeAttemptTime)
780 .count() < Options.PurgeAllocatorIntervalSec)
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000781 return;
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000782
783 if (Options.RssLimitMb <= 0 ||
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000784 GetPeakRSSMb() > static_cast<size_t>(Options.RssLimitMb) / 2)
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000785 EF->__sanitizer_purge_allocator();
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000786
787 LastAllocatorPurgeAttemptTime = system_clock::now();
788}
789
Kostya Serebryany6276d3d2021-08-03 10:04:16 -0700790void Fuzzer::ReadAndExecuteSeedCorpora(std::vector<SizedFile> &CorporaFiles) {
Kostya Serebryany3784e6d2017-08-29 02:05:01 +0000791 const size_t kMaxSaneLen = 1 << 20;
792 const size_t kMinDefaultLen = 4096;
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000793 size_t MaxSize = 0;
794 size_t MinSize = -1;
795 size_t TotalSize = 0;
Kostya Serebryany7e8a7102019-05-10 01:34:26 +0000796 for (auto &File : CorporaFiles) {
Kostya Serebryany079154e2017-09-12 21:58:07 +0000797 MaxSize = Max(File.Size, MaxSize);
798 MinSize = Min(File.Size, MinSize);
799 TotalSize += File.Size;
Kostya Serebryany3784e6d2017-08-29 02:05:01 +0000800 }
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000801 if (Options.MaxLen == 0)
802 SetMaxInputLen(std::min(std::max(kMinDefaultLen, MaxSize), kMaxSaneLen));
803 assert(MaxInputLen > 0);
804
Kostya Serebryany541fc292017-10-13 01:12:23 +0000805 // Test the callback with empty input and never try it again.
806 uint8_t dummy = 0;
807 ExecuteCallback(&dummy, 0);
808
Kostya Serebryany7e8a7102019-05-10 01:34:26 +0000809 if (CorporaFiles.empty()) {
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000810 Printf("INFO: A corpus is not provided, starting from an empty corpus\n");
811 Unit U({'\n'}); // Valid ASCII input.
812 RunOne(U.data(), U.size());
813 } else {
814 Printf("INFO: seed corpus: files: %zd min: %zdb max: %zdb total: %zdb"
815 " rss: %zdMb\n",
Kostya Serebryany7e8a7102019-05-10 01:34:26 +0000816 CorporaFiles.size(), MinSize, MaxSize, TotalSize, GetPeakRSSMb());
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000817 if (Options.ShuffleAtStartUp)
Kostya Serebryany7e8a7102019-05-10 01:34:26 +0000818 std::shuffle(CorporaFiles.begin(), CorporaFiles.end(), MD.GetRand());
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000819
Kostya Serebryany079154e2017-09-12 21:58:07 +0000820 if (Options.PreferSmall) {
Kostya Serebryany7e8a7102019-05-10 01:34:26 +0000821 std::stable_sort(CorporaFiles.begin(), CorporaFiles.end());
822 assert(CorporaFiles.front().Size <= CorporaFiles.back().Size);
Kostya Serebryany079154e2017-09-12 21:58:07 +0000823 }
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000824
825 // Load and execute inputs one by one.
Kostya Serebryany7e8a7102019-05-10 01:34:26 +0000826 for (auto &SF : CorporaFiles) {
Kostya Serebryany751b6c52017-08-31 19:17:15 +0000827 auto U = FileToVector(SF.File, MaxInputLen, /*ExitOnError=*/false);
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000828 assert(U.size() <= MaxInputLen);
Dokyung Song5f46ebc2020-07-31 00:07:20 +0000829 RunOne(U.data(), U.size(), /*MayDeleteFile*/ false, /*II*/ nullptr,
830 /*ForceAddToCorpus*/ Options.KeepSeed,
831 /*FoundUniqFeatures*/ nullptr);
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000832 CheckExitOnSrcPosOrItem();
833 TryDetectingAMemoryLeak(U.data(), U.size(),
834 /*DuringInitialCorpusExecution*/ true);
835 }
Kostya Serebryany3784e6d2017-08-29 02:05:01 +0000836 }
837
Kostya Serebryany16dba4a2017-08-29 20:51:24 +0000838 PrintStats("INITED");
Max Morozced22322020-02-18 09:57:16 -0800839 if (!Options.FocusFunction.empty()) {
Kostya Serebryany59587be2018-05-16 23:26:37 +0000840 Printf("INFO: %zd/%zd inputs touch the focus function\n",
841 Corpus.NumInputsThatTouchFocusFunction(), Corpus.size());
Max Morozced22322020-02-18 09:57:16 -0800842 if (!Options.DataFlowTrace.empty())
843 Printf("INFO: %zd/%zd inputs have the Data Flow Trace\n",
844 Corpus.NumInputsWithDataFlowTrace(),
845 Corpus.NumInputsThatTouchFocusFunction());
846 }
Kostya Serebryany59587be2018-05-16 23:26:37 +0000847
Max Morozffef3b72018-05-23 19:42:30 +0000848 if (Corpus.empty() && Options.MaxNumberOfRuns) {
Kostya Serebryany5763e452022-06-28 11:36:30 -0700849 Printf("WARNING: no interesting inputs were found so far. "
850 "Is the code instrumented for coverage?\n"
851 "This may also happen if the target rejected all inputs we tried so "
852 "far\n");
853 // The remaining logic requires that the corpus is not empty,
854 // so we add one fake input to the in-memory corpus.
855 Corpus.AddToCorpus({'\n'}, /*NumFeatures=*/1, /*MayDeleteFile=*/true,
856 /*HasFocusFunction=*/false, /*NeverReduce=*/false,
857 /*TimeOfUnit=*/duration_cast<microseconds>(0s), {0}, DFT,
858 /*BaseII*/ nullptr);
Kostya Serebryany3784e6d2017-08-29 02:05:01 +0000859 }
Kostya Serebryany3784e6d2017-08-29 02:05:01 +0000860}
861
Kostya Serebryany6276d3d2021-08-03 10:04:16 -0700862void Fuzzer::Loop(std::vector<SizedFile> &CorporaFiles) {
Kostya Serebryany32da9772019-05-24 00:43:52 +0000863 auto FocusFunctionOrAuto = Options.FocusFunction;
864 DFT.Init(Options.DataFlowTrace, &FocusFunctionOrAuto, CorporaFiles,
865 MD.GetRand());
866 TPC.SetFocusFunction(FocusFunctionOrAuto);
Kostya Serebryany7e8a7102019-05-10 01:34:26 +0000867 ReadAndExecuteSeedCorpora(CorporaFiles);
Kostya Serebryany4c9290b2018-06-07 01:40:20 +0000868 DFT.Clear(); // No need for DFT any more.
George Karpenkovcccae792017-08-21 23:25:50 +0000869 TPC.SetPrintNewPCs(Options.PrintNewCovPcs);
Kostya Serebryany20ce1412017-08-25 20:09:25 +0000870 TPC.SetPrintNewFuncs(Options.PrintNewCovFuncs);
George Karpenkovcccae792017-08-21 23:25:50 +0000871 system_clock::time_point LastCorpusReload = system_clock::now();
Kostya Serebryany8bb19a92019-02-16 01:23:41 +0000872
873 TmpMaxMutationLen =
874 Min(MaxMutationLen, Max(size_t(4), Corpus.MaxInputSize()));
875
George Karpenkovcccae792017-08-21 23:25:50 +0000876 while (true) {
877 auto Now = system_clock::now();
Kostya Serebryany90a52bd2019-06-14 22:56:50 +0000878 if (!Options.StopFile.empty() &&
879 !FileToVector(Options.StopFile, 1, false).empty())
880 break;
George Karpenkovcccae792017-08-21 23:25:50 +0000881 if (duration_cast<seconds>(Now - LastCorpusReload).count() >=
882 Options.ReloadIntervalSec) {
883 RereadOutputCorpus(MaxInputLen);
884 LastCorpusReload = system_clock::now();
885 }
886 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
887 break;
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000888 if (TimedOut())
889 break;
George Karpenkovcccae792017-08-21 23:25:50 +0000890
891 // Update TmpMaxMutationLen
Matt Morehouse422907c2018-02-13 20:52:15 +0000892 if (Options.LenControl) {
George Karpenkovcccae792017-08-21 23:25:50 +0000893 if (TmpMaxMutationLen < MaxMutationLen &&
Kostya Serebryanycea27ca2017-12-12 23:11:28 +0000894 TotalNumberOfRuns - LastCorpusUpdateRun >
Matt Morehouse422907c2018-02-13 20:52:15 +0000895 Options.LenControl * Log(TmpMaxMutationLen)) {
George Karpenkovcccae792017-08-21 23:25:50 +0000896 TmpMaxMutationLen =
Kostya Serebryanycea27ca2017-12-12 23:11:28 +0000897 Min(MaxMutationLen, TmpMaxMutationLen + Log(TmpMaxMutationLen));
Kostya Serebryanycea27ca2017-12-12 23:11:28 +0000898 LastCorpusUpdateRun = TotalNumberOfRuns;
George Karpenkovcccae792017-08-21 23:25:50 +0000899 }
900 } else {
901 TmpMaxMutationLen = MaxMutationLen;
902 }
903
904 // Perform several mutations and runs.
905 MutateAndTestOne();
Alex Shlyapnikov95283d52017-10-23 22:04:30 +0000906
907 PurgeAllocator();
George Karpenkovcccae792017-08-21 23:25:50 +0000908 }
909
910 PrintStats("DONE ", "\n");
Marco Vanottie1d706b2021-07-02 09:44:54 -0700911 MD.PrintRecommendedDictionary();
George Karpenkovcccae792017-08-21 23:25:50 +0000912}
913
914void Fuzzer::MinimizeCrashLoop(const Unit &U) {
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000915 if (U.size() <= 1)
916 return;
George Karpenkovcccae792017-08-21 23:25:50 +0000917 while (!TimedOut() && TotalNumberOfRuns < Options.MaxNumberOfRuns) {
918 MD.StartMutationSequence();
919 memcpy(CurrentUnitData, U.data(), U.size());
920 for (int i = 0; i < Options.MutateDepth; i++) {
921 size_t NewSize = MD.Mutate(CurrentUnitData, U.size(), MaxMutationLen);
922 assert(NewSize > 0 && NewSize <= MaxMutationLen);
923 ExecuteCallback(CurrentUnitData, NewSize);
924 PrintPulseAndReportSlowInput(CurrentUnitData, NewSize);
925 TryDetectingAMemoryLeak(CurrentUnitData, NewSize,
926 /*DuringInitialCorpusExecution*/ false);
927 }
928 }
929}
930
George Karpenkovcccae792017-08-21 23:25:50 +0000931} // namespace fuzzer
932
933extern "C" {
934
Jonathan Metzmanb6711772019-01-17 16:36:05 +0000935ATTRIBUTE_INTERFACE size_t
Petr Hosek89c217f2018-01-17 20:39:14 +0000936LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
George Karpenkovcccae792017-08-21 23:25:50 +0000937 assert(fuzzer::F);
938 return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize);
939}
940
Alex Shlyapnikovaf314ed2017-10-23 23:24:33 +0000941} // extern "C"