blob: 09c57c3f6b76daf63b5c0bb170ea5c6c6c9cc550 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerLoop.cpp - Fuzzer's main loop --------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// Fuzzer's main loop.
10//===----------------------------------------------------------------------===//
11
12#include "FuzzerCorpus.h"
13#include "FuzzerIO.h"
14#include "FuzzerInternal.h"
15#include "FuzzerMutate.h"
16#include "FuzzerRandom.h"
17#include "FuzzerShmem.h"
18#include "FuzzerTracePC.h"
19#include <algorithm>
20#include <cstring>
21#include <memory>
vitalybukab3e5a2c2017-11-01 03:02:59 +000022#include <mutex>
george.karpenkov29efa6d2017-08-21 23:25:50 +000023#include <set>
24
25#if defined(__has_include)
26#if __has_include(<sanitizer / lsan_interface.h>)
27#include <sanitizer/lsan_interface.h>
28#endif
29#endif
30
31#define NO_SANITIZE_MEMORY
32#if defined(__has_feature)
33#if __has_feature(memory_sanitizer)
34#undef NO_SANITIZE_MEMORY
35#define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
36#endif
37#endif
38
39namespace fuzzer {
40static const size_t kMaxUnitSizeToPrint = 256;
41
42thread_local bool Fuzzer::IsMyThread;
43
44SharedMemoryRegion SMR;
45
morehousec6ee8752018-07-17 16:12:00 +000046bool RunningUserCallback = false;
47
george.karpenkov29efa6d2017-08-21 23:25:50 +000048// Only one Fuzzer per process.
49static Fuzzer *F;
50
51// Leak detection is expensive, so we first check if there were more mallocs
52// than frees (using the sanitizer malloc hooks) and only then try to call lsan.
53struct MallocFreeTracer {
54 void Start(int TraceLevel) {
55 this->TraceLevel = TraceLevel;
56 if (TraceLevel)
57 Printf("MallocFreeTracer: START\n");
58 Mallocs = 0;
59 Frees = 0;
60 }
61 // Returns true if there were more mallocs than frees.
62 bool Stop() {
63 if (TraceLevel)
64 Printf("MallocFreeTracer: STOP %zd %zd (%s)\n", Mallocs.load(),
65 Frees.load(), Mallocs == Frees ? "same" : "DIFFERENT");
66 bool Result = Mallocs > Frees;
67 Mallocs = 0;
68 Frees = 0;
69 TraceLevel = 0;
70 return Result;
71 }
72 std::atomic<size_t> Mallocs;
73 std::atomic<size_t> Frees;
74 int TraceLevel = 0;
vitalybukae6504cf2017-11-02 04:12:10 +000075
76 std::recursive_mutex TraceMutex;
77 bool TraceDisabled = false;
george.karpenkov29efa6d2017-08-21 23:25:50 +000078};
79
80static MallocFreeTracer AllocTracer;
81
vitalybukae6504cf2017-11-02 04:12:10 +000082// Locks printing and avoids nested hooks triggered from mallocs/frees in
83// sanitizer.
84class TraceLock {
85public:
86 TraceLock() : Lock(AllocTracer.TraceMutex) {
87 AllocTracer.TraceDisabled = !AllocTracer.TraceDisabled;
88 }
89 ~TraceLock() { AllocTracer.TraceDisabled = !AllocTracer.TraceDisabled; }
90
91 bool IsDisabled() const {
92 // This is already inverted value.
93 return !AllocTracer.TraceDisabled;
94 }
95
96private:
97 std::lock_guard<std::recursive_mutex> Lock;
98};
vitalybukab3e5a2c2017-11-01 03:02:59 +000099
george.karpenkov29efa6d2017-08-21 23:25:50 +0000100ATTRIBUTE_NO_SANITIZE_MEMORY
101void MallocHook(const volatile void *ptr, size_t size) {
102 size_t N = AllocTracer.Mallocs++;
103 F->HandleMalloc(size);
104 if (int TraceLevel = AllocTracer.TraceLevel) {
vitalybukae6504cf2017-11-02 04:12:10 +0000105 TraceLock Lock;
106 if (Lock.IsDisabled())
107 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000108 Printf("MALLOC[%zd] %p %zd\n", N, ptr, size);
109 if (TraceLevel >= 2 && EF)
morehousebd67cc22018-05-08 23:45:05 +0000110 PrintStackTrace();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000111 }
112}
113
114ATTRIBUTE_NO_SANITIZE_MEMORY
115void FreeHook(const volatile void *ptr) {
116 size_t N = AllocTracer.Frees++;
117 if (int TraceLevel = AllocTracer.TraceLevel) {
vitalybukae6504cf2017-11-02 04:12:10 +0000118 TraceLock Lock;
119 if (Lock.IsDisabled())
120 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000121 Printf("FREE[%zd] %p\n", N, ptr);
122 if (TraceLevel >= 2 && EF)
morehousebd67cc22018-05-08 23:45:05 +0000123 PrintStackTrace();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000124 }
125}
126
127// Crash on a single malloc that exceeds the rss limit.
128void Fuzzer::HandleMalloc(size_t Size) {
kcc120e40b2017-12-01 22:12:04 +0000129 if (!Options.MallocLimitMb || (Size >> 20) < (size_t)Options.MallocLimitMb)
george.karpenkov29efa6d2017-08-21 23:25:50 +0000130 return;
131 Printf("==%d== ERROR: libFuzzer: out-of-memory (malloc(%zd))\n", GetPid(),
132 Size);
133 Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
morehousebd67cc22018-05-08 23:45:05 +0000134 PrintStackTrace();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000135 DumpCurrentUnit("oom-");
136 Printf("SUMMARY: libFuzzer: out-of-memory\n");
137 PrintFinalStats();
138 _Exit(Options.ErrorExitCode); // Stop right now.
139}
140
141Fuzzer::Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
142 FuzzingOptions Options)
143 : CB(CB), Corpus(Corpus), MD(MD), Options(Options) {
144 if (EF->__sanitizer_set_death_callback)
145 EF->__sanitizer_set_death_callback(StaticDeathCallback);
146 assert(!F);
147 F = this;
148 TPC.ResetMaps();
149 IsMyThread = true;
150 if (Options.DetectLeaks && EF->__sanitizer_install_malloc_and_free_hooks)
151 EF->__sanitizer_install_malloc_and_free_hooks(MallocHook, FreeHook);
152 TPC.SetUseCounters(Options.UseCounters);
kcc3850d062018-07-03 22:33:09 +0000153 TPC.SetUseValueProfileMask(Options.UseValueProfile);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000154
155 if (Options.Verbosity)
156 TPC.PrintModuleInfo();
157 if (!Options.OutputCorpus.empty() && Options.ReloadIntervalSec)
158 EpochOfLastReadOfOutputCorpus = GetEpoch(Options.OutputCorpus);
159 MaxInputLen = MaxMutationLen = Options.MaxLen;
160 TmpMaxMutationLen = Max(size_t(4), Corpus.MaxInputSize());
161 AllocateCurrentUnitData();
162 CurrentUnitSize = 0;
163 memset(BaseSha1, 0, sizeof(BaseSha1));
kcc3acbe072018-05-16 23:26:37 +0000164 TPC.SetFocusFunction(Options.FocusFunction);
kcc86e43882018-06-06 01:23:29 +0000165 DFT.Init(Options.DataFlowTrace, Options.FocusFunction);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000166}
167
alekseyshl9f6a9f22017-10-23 23:24:33 +0000168Fuzzer::~Fuzzer() {}
george.karpenkov29efa6d2017-08-21 23:25:50 +0000169
170void Fuzzer::AllocateCurrentUnitData() {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000171 if (CurrentUnitData || MaxInputLen == 0)
172 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000173 CurrentUnitData = new uint8_t[MaxInputLen];
174}
175
176void Fuzzer::StaticDeathCallback() {
177 assert(F);
178 F->DeathCallback();
179}
180
181void Fuzzer::DumpCurrentUnit(const char *Prefix) {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000182 if (!CurrentUnitData)
183 return; // Happens when running individual inputs.
morehouse1467b792018-07-09 23:51:08 +0000184 ScopedDisableMsanInterceptorChecks S;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000185 MD.PrintMutationSequence();
186 Printf("; base unit: %s\n", Sha1ToString(BaseSha1).c_str());
187 size_t UnitSize = CurrentUnitSize;
188 if (UnitSize <= kMaxUnitSizeToPrint) {
189 PrintHexArray(CurrentUnitData, UnitSize, "\n");
190 PrintASCII(CurrentUnitData, UnitSize, "\n");
191 }
192 WriteUnitToFileWithPrefix({CurrentUnitData, CurrentUnitData + UnitSize},
193 Prefix);
194}
195
196NO_SANITIZE_MEMORY
197void Fuzzer::DeathCallback() {
198 DumpCurrentUnit("crash-");
199 PrintFinalStats();
200}
201
202void Fuzzer::StaticAlarmCallback() {
203 assert(F);
204 F->AlarmCallback();
205}
206
207void Fuzzer::StaticCrashSignalCallback() {
208 assert(F);
209 F->CrashCallback();
210}
211
212void Fuzzer::StaticExitCallback() {
213 assert(F);
214 F->ExitCallback();
215}
216
217void Fuzzer::StaticInterruptCallback() {
218 assert(F);
219 F->InterruptCallback();
220}
221
kcc1239a992017-11-09 20:30:19 +0000222void Fuzzer::StaticGracefulExitCallback() {
223 assert(F);
224 F->GracefulExitRequested = true;
225 Printf("INFO: signal received, trying to exit gracefully\n");
226}
227
george.karpenkov29efa6d2017-08-21 23:25:50 +0000228void Fuzzer::StaticFileSizeExceedCallback() {
229 Printf("==%lu== ERROR: libFuzzer: file size exceeded\n", GetPid());
230 exit(1);
231}
232
233void Fuzzer::CrashCallback() {
morehousef7b44452018-05-02 02:55:28 +0000234 if (EF->__sanitizer_acquire_crash_state)
235 EF->__sanitizer_acquire_crash_state();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000236 Printf("==%lu== ERROR: libFuzzer: deadly signal\n", GetPid());
morehousebd67cc22018-05-08 23:45:05 +0000237 PrintStackTrace();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000238 Printf("NOTE: libFuzzer has rudimentary signal handlers.\n"
239 " Combine libFuzzer with AddressSanitizer or similar for better "
240 "crash reports.\n");
241 Printf("SUMMARY: libFuzzer: deadly signal\n");
242 DumpCurrentUnit("crash-");
243 PrintFinalStats();
alekseyshl9f6a9f22017-10-23 23:24:33 +0000244 _Exit(Options.ErrorExitCode); // Stop right now.
george.karpenkov29efa6d2017-08-21 23:25:50 +0000245}
246
247void Fuzzer::ExitCallback() {
morehousec6ee8752018-07-17 16:12:00 +0000248 if (!RunningUserCallback)
george.karpenkov29efa6d2017-08-21 23:25:50 +0000249 return; // This exit did not come from the user callback
morehouse5a4566a2018-05-01 21:01:53 +0000250 if (EF->__sanitizer_acquire_crash_state &&
251 !EF->__sanitizer_acquire_crash_state())
252 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000253 Printf("==%lu== ERROR: libFuzzer: fuzz target exited\n", GetPid());
morehousebd67cc22018-05-08 23:45:05 +0000254 PrintStackTrace();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000255 Printf("SUMMARY: libFuzzer: fuzz target exited\n");
256 DumpCurrentUnit("crash-");
257 PrintFinalStats();
258 _Exit(Options.ErrorExitCode);
259}
260
kcc1239a992017-11-09 20:30:19 +0000261void Fuzzer::MaybeExitGracefully() {
262 if (!GracefulExitRequested) return;
263 Printf("==%lu== INFO: libFuzzer: exiting as requested\n", GetPid());
264 PrintFinalStats();
265 _Exit(0);
266}
267
george.karpenkov29efa6d2017-08-21 23:25:50 +0000268void Fuzzer::InterruptCallback() {
269 Printf("==%lu== libFuzzer: run interrupted; exiting\n", GetPid());
270 PrintFinalStats();
alekseyshl9f6a9f22017-10-23 23:24:33 +0000271 _Exit(0); // Stop right now, don't perform any at-exit actions.
george.karpenkov29efa6d2017-08-21 23:25:50 +0000272}
273
274NO_SANITIZE_MEMORY
275void Fuzzer::AlarmCallback() {
276 assert(Options.UnitTimeoutSec > 0);
277 // In Windows Alarm callback is executed by a different thread.
kamil3849ee02018-11-06 01:28:01 +0000278 // NetBSD's current behavior needs this change too.
279#if !LIBFUZZER_WINDOWS && !LIBFUZZER_NETBSD
alekseyshl9f6a9f22017-10-23 23:24:33 +0000280 if (!InFuzzingThread())
281 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000282#endif
morehousec6ee8752018-07-17 16:12:00 +0000283 if (!RunningUserCallback)
george.karpenkov29efa6d2017-08-21 23:25:50 +0000284 return; // We have not started running units yet.
285 size_t Seconds =
286 duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
287 if (Seconds == 0)
288 return;
289 if (Options.Verbosity >= 2)
290 Printf("AlarmCallback %zd\n", Seconds);
291 if (Seconds >= (size_t)Options.UnitTimeoutSec) {
morehouse5a4566a2018-05-01 21:01:53 +0000292 if (EF->__sanitizer_acquire_crash_state &&
293 !EF->__sanitizer_acquire_crash_state())
294 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000295 Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds);
296 Printf(" and the timeout value is %d (use -timeout=N to change)\n",
297 Options.UnitTimeoutSec);
298 DumpCurrentUnit("timeout-");
299 Printf("==%lu== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(),
300 Seconds);
morehousebd67cc22018-05-08 23:45:05 +0000301 PrintStackTrace();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000302 Printf("SUMMARY: libFuzzer: timeout\n");
303 PrintFinalStats();
304 _Exit(Options.TimeoutExitCode); // Stop right now.
305 }
306}
307
308void Fuzzer::RssLimitCallback() {
morehouse5a4566a2018-05-01 21:01:53 +0000309 if (EF->__sanitizer_acquire_crash_state &&
310 !EF->__sanitizer_acquire_crash_state())
311 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000312 Printf(
313 "==%lu== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n",
314 GetPid(), GetPeakRSSMb(), Options.RssLimitMb);
315 Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
morehousebd67cc22018-05-08 23:45:05 +0000316 PrintMemoryProfile();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000317 DumpCurrentUnit("oom-");
318 Printf("SUMMARY: libFuzzer: out-of-memory\n");
319 PrintFinalStats();
320 _Exit(Options.ErrorExitCode); // Stop right now.
321}
322
323void Fuzzer::PrintStats(const char *Where, const char *End, size_t Units) {
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 = Corpus.NumFeatures())
alekseyshl9f6a9f22017-10-23 23:24:33 +0000331 Printf(" ft: %zd", N);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000332 if (!Corpus.empty()) {
333 Printf(" corp: %zd", Corpus.NumActiveUnits());
334 if (size_t N = Corpus.SizeInBytes()) {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000335 if (N < (1 << 14))
george.karpenkov29efa6d2017-08-21 23:25:50 +0000336 Printf("/%zdb", N);
337 else if (N < (1 << 24))
338 Printf("/%zdKb", N >> 10);
339 else
340 Printf("/%zdMb", N >> 20);
341 }
kcc3acbe072018-05-16 23:26:37 +0000342 if (size_t FF = Corpus.NumInputsThatTouchFocusFunction())
343 Printf(" focus: %zd", FF);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000344 }
morehousea6c692c2018-02-22 19:00:17 +0000345 if (TmpMaxMutationLen)
346 Printf(" lim: %zd", TmpMaxMutationLen);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000347 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
355void Fuzzer::PrintFinalStats() {
356 if (Options.PrintCoverage)
357 TPC.PrintCoverage();
dor1sbb933292018-07-16 16:01:31 +0000358 if (Options.PrintUnstableStats)
359 TPC.PrintUnstableStats();
kcca7dd2a92018-05-21 19:47:00 +0000360 if (Options.DumpCoverage)
361 TPC.DumpCoverage();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000362 if (Options.PrintCorpusStats)
363 Corpus.PrintStats();
alekseyshl9f6a9f22017-10-23 23:24:33 +0000364 if (!Options.PrintFinalStats)
365 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000366 size_t ExecPerSec = execPerSec();
367 Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns);
368 Printf("stat::average_exec_per_sec: %zd\n", ExecPerSec);
369 Printf("stat::new_units_added: %zd\n", NumberOfNewUnitsAdded);
370 Printf("stat::slowest_unit_time_sec: %zd\n", TimeOfLongestUnitInSeconds);
371 Printf("stat::peak_rss_mb: %zd\n", GetPeakRSSMb());
372}
373
374void Fuzzer::SetMaxInputLen(size_t MaxInputLen) {
375 assert(this->MaxInputLen == 0); // Can only reset MaxInputLen from 0 to non-0.
376 assert(MaxInputLen);
377 this->MaxInputLen = MaxInputLen;
378 this->MaxMutationLen = MaxInputLen;
379 AllocateCurrentUnitData();
380 Printf("INFO: -max_len is not provided; "
381 "libFuzzer will not generate inputs larger than %zd bytes\n",
382 MaxInputLen);
383}
384
385void Fuzzer::SetMaxMutationLen(size_t MaxMutationLen) {
386 assert(MaxMutationLen && MaxMutationLen <= MaxInputLen);
387 this->MaxMutationLen = MaxMutationLen;
388}
389
390void Fuzzer::CheckExitOnSrcPosOrItem() {
391 if (!Options.ExitOnSrcPos.empty()) {
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000392 static auto *PCsSet = new Set<uintptr_t>;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000393 auto HandlePC = [&](uintptr_t PC) {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000394 if (!PCsSet->insert(PC).second)
395 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000396 std::string Descr = DescribePC("%F %L", PC + 1);
397 if (Descr.find(Options.ExitOnSrcPos) != std::string::npos) {
398 Printf("INFO: found line matching '%s', exiting.\n",
399 Options.ExitOnSrcPos.c_str());
400 _Exit(0);
401 }
402 };
403 TPC.ForEachObservedPC(HandlePC);
404 }
405 if (!Options.ExitOnItem.empty()) {
406 if (Corpus.HasUnit(Options.ExitOnItem)) {
407 Printf("INFO: found item with checksum '%s', exiting.\n",
408 Options.ExitOnItem.c_str());
409 _Exit(0);
410 }
411 }
412}
413
414void Fuzzer::RereadOutputCorpus(size_t MaxSize) {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000415 if (Options.OutputCorpus.empty() || !Options.ReloadIntervalSec)
416 return;
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000417 Vector<Unit> AdditionalCorpus;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000418 ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus,
419 &EpochOfLastReadOfOutputCorpus, MaxSize,
420 /*ExitOnError*/ false);
421 if (Options.Verbosity >= 2)
422 Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
423 bool Reloaded = false;
424 for (auto &U : AdditionalCorpus) {
425 if (U.size() > MaxSize)
426 U.resize(MaxSize);
427 if (!Corpus.HasUnit(U)) {
428 if (RunOne(U.data(), U.size())) {
429 CheckExitOnSrcPosOrItem();
430 Reloaded = true;
431 }
432 }
433 }
434 if (Reloaded)
435 PrintStats("RELOAD");
436}
437
george.karpenkov29efa6d2017-08-21 23:25:50 +0000438void Fuzzer::PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size) {
439 auto TimeOfUnit =
440 duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
441 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) &&
442 secondsSinceProcessStartUp() >= 2)
443 PrintStats("pulse ");
444 if (TimeOfUnit > TimeOfLongestUnitInSeconds * 1.1 &&
445 TimeOfUnit >= Options.ReportSlowUnits) {
446 TimeOfLongestUnitInSeconds = TimeOfUnit;
447 Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds);
448 WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-");
449 }
450}
451
dor1sbb933292018-07-16 16:01:31 +0000452void Fuzzer::CheckForUnstableCounters(const uint8_t *Data, size_t Size) {
453 auto CBSetupAndRun = [&]() {
454 ScopedEnableMsanInterceptorChecks S;
455 UnitStartTime = system_clock::now();
456 TPC.ResetMaps();
morehousec6ee8752018-07-17 16:12:00 +0000457 RunningUserCallback = true;
dor1sbb933292018-07-16 16:01:31 +0000458 CB(Data, Size);
morehousec6ee8752018-07-17 16:12:00 +0000459 RunningUserCallback = false;
dor1sbb933292018-07-16 16:01:31 +0000460 UnitStopTime = system_clock::now();
461 };
462
463 // Copy original run counters into our unstable counters
464 TPC.InitializeUnstableCounters();
465
466 // First Rerun
467 CBSetupAndRun();
dor1s658ff782018-08-08 14:32:46 +0000468 if (TPC.UpdateUnstableCounters(Options.HandleUnstable)) {
469 // Second Rerun
470 CBSetupAndRun();
471 TPC.UpdateAndApplyUnstableCounters(Options.HandleUnstable);
472 }
dor1sbb933292018-07-16 16:01:31 +0000473}
474
george.karpenkov29efa6d2017-08-21 23:25:50 +0000475bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile,
kccb6836be2017-12-01 19:18:38 +0000476 InputInfo *II, bool *FoundUniqFeatures) {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000477 if (!Size)
478 return false;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000479
480 ExecuteCallback(Data, Size);
481
482 UniqFeatureSetTmp.clear();
483 size_t FoundUniqFeaturesOfII = 0;
484 size_t NumUpdatesBefore = Corpus.NumFeatureUpdates();
dor1sf50b3bb2018-07-23 14:20:52 +0000485 bool NewFeaturesUnstable = false;
486
487 if (Options.HandleUnstable || Options.PrintUnstableStats) {
488 TPC.CollectFeatures([&](size_t Feature) {
489 if (Corpus.IsFeatureNew(Feature, Size, Options.Shrink))
490 NewFeaturesUnstable = true;
491 });
492 if (NewFeaturesUnstable)
493 CheckForUnstableCounters(Data, Size);
494 }
495
george.karpenkov29efa6d2017-08-21 23:25:50 +0000496 TPC.CollectFeatures([&](size_t Feature) {
497 if (Corpus.AddFeature(Feature, Size, Options.Shrink))
498 UniqFeatureSetTmp.push_back(Feature);
499 if (Options.ReduceInputs && II)
500 if (std::binary_search(II->UniqFeatureSet.begin(),
501 II->UniqFeatureSet.end(), Feature))
502 FoundUniqFeaturesOfII++;
503 });
dor1sf50b3bb2018-07-23 14:20:52 +0000504
kccb6836be2017-12-01 19:18:38 +0000505 if (FoundUniqFeatures)
506 *FoundUniqFeatures = FoundUniqFeaturesOfII;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000507 PrintPulseAndReportSlowInput(Data, Size);
508 size_t NumNewFeatures = Corpus.NumFeatureUpdates() - NumUpdatesBefore;
dor1sbb933292018-07-16 16:01:31 +0000509
george.karpenkov29efa6d2017-08-21 23:25:50 +0000510 if (NumNewFeatures) {
511 TPC.UpdateObservedPCs();
512 Corpus.AddToCorpus({Data, Data + Size}, NumNewFeatures, MayDeleteFile,
kcc0cab3f02018-07-19 01:23:32 +0000513 TPC.ObservedFocusFunction(), UniqFeatureSetTmp, DFT, II);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000514 return true;
515 }
516 if (II && FoundUniqFeaturesOfII &&
kccadf188b2018-06-07 01:40:20 +0000517 II->DataFlowTraceForFocusFunction.empty() &&
george.karpenkov29efa6d2017-08-21 23:25:50 +0000518 FoundUniqFeaturesOfII == II->UniqFeatureSet.size() &&
519 II->U.size() > Size) {
520 Corpus.Replace(II, {Data, Data + Size});
521 return true;
522 }
523 return false;
524}
525
526size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const {
527 assert(InFuzzingThread());
528 *Data = CurrentUnitData;
529 return CurrentUnitSize;
530}
531
532void Fuzzer::CrashOnOverwrittenData() {
533 Printf("==%d== ERROR: libFuzzer: fuzz target overwrites it's const input\n",
534 GetPid());
535 DumpCurrentUnit("crash-");
536 Printf("SUMMARY: libFuzzer: out-of-memory\n");
537 _Exit(Options.ErrorExitCode); // Stop right now.
538}
539
540// Compare two arrays, but not all bytes if the arrays are large.
541static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) {
542 const size_t Limit = 64;
543 if (Size <= 64)
544 return !memcmp(A, B, Size);
545 // Compare first and last Limit/2 bytes.
546 return !memcmp(A, B, Limit / 2) &&
547 !memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2);
548}
549
550void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
551 TPC.RecordInitialStack();
552 TotalNumberOfRuns++;
553 assert(InFuzzingThread());
554 if (SMR.IsClient())
555 SMR.WriteByteArray(Data, Size);
556 // We copy the contents of Unit into a separate heap buffer
557 // so that we reliably find buffer overflows in it.
558 uint8_t *DataCopy = new uint8_t[Size];
559 memcpy(DataCopy, Data, Size);
morehouse1467b792018-07-09 23:51:08 +0000560 if (EF->__msan_unpoison)
561 EF->__msan_unpoison(DataCopy, Size);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000562 if (CurrentUnitData && CurrentUnitData != Data)
563 memcpy(CurrentUnitData, Data, Size);
564 CurrentUnitSize = Size;
morehouse1467b792018-07-09 23:51:08 +0000565 {
566 ScopedEnableMsanInterceptorChecks S;
567 AllocTracer.Start(Options.TraceMalloc);
568 UnitStartTime = system_clock::now();
569 TPC.ResetMaps();
morehousec6ee8752018-07-17 16:12:00 +0000570 RunningUserCallback = true;
morehouse1467b792018-07-09 23:51:08 +0000571 int Res = CB(DataCopy, Size);
morehousec6ee8752018-07-17 16:12:00 +0000572 RunningUserCallback = false;
morehouse1467b792018-07-09 23:51:08 +0000573 UnitStopTime = system_clock::now();
574 (void)Res;
575 assert(Res == 0);
576 HasMoreMallocsThanFrees = AllocTracer.Stop();
577 }
george.karpenkov29efa6d2017-08-21 23:25:50 +0000578 if (!LooseMemeq(DataCopy, Data, Size))
579 CrashOnOverwrittenData();
580 CurrentUnitSize = 0;
581 delete[] DataCopy;
582}
583
584void Fuzzer::WriteToOutputCorpus(const Unit &U) {
585 if (Options.OnlyASCII)
586 assert(IsASCII(U));
587 if (Options.OutputCorpus.empty())
588 return;
589 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
590 WriteToFile(U, Path);
591 if (Options.Verbosity >= 2)
592 Printf("Written %zd bytes to %s\n", U.size(), Path.c_str());
593}
594
595void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
596 if (!Options.SaveArtifacts)
597 return;
598 std::string Path = Options.ArtifactPrefix + Prefix + Hash(U);
599 if (!Options.ExactArtifactPath.empty())
600 Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix.
601 WriteToFile(U, Path);
602 Printf("artifact_prefix='%s'; Test unit written to %s\n",
603 Options.ArtifactPrefix.c_str(), Path.c_str());
604 if (U.size() <= kMaxUnitSizeToPrint)
605 Printf("Base64: %s\n", Base64(U).c_str());
606}
607
608void Fuzzer::PrintStatusForNewUnit(const Unit &U, const char *Text) {
609 if (!Options.PrintNEW)
610 return;
611 PrintStats(Text, "");
612 if (Options.Verbosity) {
613 Printf(" L: %zd/%zd ", U.size(), Corpus.MaxInputSize());
614 MD.PrintMutationSequence();
615 Printf("\n");
616 }
617}
618
619void Fuzzer::ReportNewCoverage(InputInfo *II, const Unit &U) {
620 II->NumSuccessfullMutations++;
621 MD.RecordSuccessfulMutationSequence();
alekseyshl9f6a9f22017-10-23 23:24:33 +0000622 PrintStatusForNewUnit(U, II->Reduced ? "REDUCE" : "NEW ");
george.karpenkov29efa6d2017-08-21 23:25:50 +0000623 WriteToOutputCorpus(U);
624 NumberOfNewUnitsAdded++;
alekseyshl9f6a9f22017-10-23 23:24:33 +0000625 CheckExitOnSrcPosOrItem(); // Check only after the unit is saved to corpus.
george.karpenkov29efa6d2017-08-21 23:25:50 +0000626 LastCorpusUpdateRun = TotalNumberOfRuns;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000627}
628
629// Tries detecting a memory leak on the particular input that we have just
630// executed before calling this function.
631void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
632 bool DuringInitialCorpusExecution) {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000633 if (!HasMoreMallocsThanFrees)
634 return; // mallocs==frees, a leak is unlikely.
635 if (!Options.DetectLeaks)
636 return;
dor1s38279cb2017-09-12 02:01:54 +0000637 if (!DuringInitialCorpusExecution &&
alekseyshl9f6a9f22017-10-23 23:24:33 +0000638 TotalNumberOfRuns >= Options.MaxNumberOfRuns)
639 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000640 if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) ||
641 !(EF->__lsan_do_recoverable_leak_check))
alekseyshl9f6a9f22017-10-23 23:24:33 +0000642 return; // No lsan.
george.karpenkov29efa6d2017-08-21 23:25:50 +0000643 // Run the target once again, but with lsan disabled so that if there is
644 // a real leak we do not report it twice.
645 EF->__lsan_disable();
646 ExecuteCallback(Data, Size);
647 EF->__lsan_enable();
alekseyshl9f6a9f22017-10-23 23:24:33 +0000648 if (!HasMoreMallocsThanFrees)
649 return; // a leak is unlikely.
george.karpenkov29efa6d2017-08-21 23:25:50 +0000650 if (NumberOfLeakDetectionAttempts++ > 1000) {
651 Options.DetectLeaks = false;
652 Printf("INFO: libFuzzer disabled leak detection after every mutation.\n"
653 " Most likely the target function accumulates allocated\n"
654 " memory in a global state w/o actually leaking it.\n"
655 " You may try running this binary with -trace_malloc=[12]"
656 " to get a trace of mallocs and frees.\n"
657 " If LeakSanitizer is enabled in this process it will still\n"
658 " run on the process shutdown.\n");
659 return;
660 }
661 // Now perform the actual lsan pass. This is expensive and we must ensure
662 // we don't call it too often.
663 if (EF->__lsan_do_recoverable_leak_check()) { // Leak is found, report it.
664 if (DuringInitialCorpusExecution)
665 Printf("\nINFO: a leak has been found in the initial corpus.\n\n");
666 Printf("INFO: to ignore leaks on libFuzzer side use -detect_leaks=0.\n\n");
667 CurrentUnitSize = Size;
668 DumpCurrentUnit("leak-");
669 PrintFinalStats();
alekseyshl9f6a9f22017-10-23 23:24:33 +0000670 _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on.
george.karpenkov29efa6d2017-08-21 23:25:50 +0000671 }
672}
673
674void Fuzzer::MutateAndTestOne() {
675 MD.StartMutationSequence();
676
677 auto &II = Corpus.ChooseUnitToMutate(MD.GetRand());
678 const auto &U = II.U;
679 memcpy(BaseSha1, II.Sha1, sizeof(BaseSha1));
680 assert(CurrentUnitData);
681 size_t Size = U.size();
682 assert(Size <= MaxInputLen && "Oversized Unit");
683 memcpy(CurrentUnitData, U.data(), Size);
684
685 assert(MaxMutationLen > 0);
686
687 size_t CurrentMaxMutationLen =
688 Min(MaxMutationLen, Max(U.size(), TmpMaxMutationLen));
689 assert(CurrentMaxMutationLen > 0);
690
691 for (int i = 0; i < Options.MutateDepth; i++) {
692 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
693 break;
kcc1239a992017-11-09 20:30:19 +0000694 MaybeExitGracefully();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000695 size_t NewSize = 0;
kcc0cab3f02018-07-19 01:23:32 +0000696 if (II.HasFocusFunction && !II.DataFlowTraceForFocusFunction.empty() &&
697 Size <= CurrentMaxMutationLen)
698 NewSize = MD.MutateWithMask(CurrentUnitData, Size, Size,
699 II.DataFlowTraceForFocusFunction);
700 else
701 NewSize = MD.Mutate(CurrentUnitData, Size, CurrentMaxMutationLen);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000702 assert(NewSize > 0 && "Mutator returned empty unit");
alekseyshld995b552017-10-23 22:04:30 +0000703 assert(NewSize <= CurrentMaxMutationLen && "Mutator return oversized unit");
george.karpenkov29efa6d2017-08-21 23:25:50 +0000704 Size = NewSize;
705 II.NumExecutedMutations++;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000706
kccb6836be2017-12-01 19:18:38 +0000707 bool FoundUniqFeatures = false;
708 bool NewCov = RunOne(CurrentUnitData, Size, /*MayDeleteFile=*/true, &II,
709 &FoundUniqFeatures);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000710 TryDetectingAMemoryLeak(CurrentUnitData, Size,
711 /*DuringInitialCorpusExecution*/ false);
kccb6836be2017-12-01 19:18:38 +0000712 if (NewCov) {
morehouse553f00b2017-11-09 20:44:08 +0000713 ReportNewCoverage(&II, {CurrentUnitData, CurrentUnitData + Size});
kccb6836be2017-12-01 19:18:38 +0000714 break; // We will mutate this input more in the next rounds.
715 }
716 if (Options.ReduceDepth && !FoundUniqFeatures)
dor1se6729cb2018-07-16 15:15:34 +0000717 break;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000718 }
719}
720
alekseyshld995b552017-10-23 22:04:30 +0000721void Fuzzer::PurgeAllocator() {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000722 if (Options.PurgeAllocatorIntervalSec < 0 || !EF->__sanitizer_purge_allocator)
alekseyshld995b552017-10-23 22:04:30 +0000723 return;
alekseyshld995b552017-10-23 22:04:30 +0000724 if (duration_cast<seconds>(system_clock::now() -
alekseyshl9f6a9f22017-10-23 23:24:33 +0000725 LastAllocatorPurgeAttemptTime)
726 .count() < Options.PurgeAllocatorIntervalSec)
alekseyshld995b552017-10-23 22:04:30 +0000727 return;
alekseyshld995b552017-10-23 22:04:30 +0000728
729 if (Options.RssLimitMb <= 0 ||
alekseyshl9f6a9f22017-10-23 23:24:33 +0000730 GetPeakRSSMb() > static_cast<size_t>(Options.RssLimitMb) / 2)
alekseyshld995b552017-10-23 22:04:30 +0000731 EF->__sanitizer_purge_allocator();
alekseyshld995b552017-10-23 22:04:30 +0000732
733 LastAllocatorPurgeAttemptTime = system_clock::now();
734}
735
kcc2e93b3f2017-08-29 02:05:01 +0000736void Fuzzer::ReadAndExecuteSeedCorpora(const Vector<std::string> &CorpusDirs) {
737 const size_t kMaxSaneLen = 1 << 20;
738 const size_t kMinDefaultLen = 4096;
kccdc00cd32017-08-29 20:51:24 +0000739 Vector<SizedFile> SizedFiles;
740 size_t MaxSize = 0;
741 size_t MinSize = -1;
742 size_t TotalSize = 0;
kcc7f5f2222017-09-12 21:58:07 +0000743 size_t LastNumFiles = 0;
kccdc00cd32017-08-29 20:51:24 +0000744 for (auto &Dir : CorpusDirs) {
kcc7f5f2222017-09-12 21:58:07 +0000745 GetSizedFilesFromDir(Dir, &SizedFiles);
746 Printf("INFO: % 8zd files found in %s\n", SizedFiles.size() - LastNumFiles,
747 Dir.c_str());
748 LastNumFiles = SizedFiles.size();
749 }
750 for (auto &File : SizedFiles) {
751 MaxSize = Max(File.Size, MaxSize);
752 MinSize = Min(File.Size, MinSize);
753 TotalSize += File.Size;
kcc2e93b3f2017-08-29 02:05:01 +0000754 }
kccdc00cd32017-08-29 20:51:24 +0000755 if (Options.MaxLen == 0)
756 SetMaxInputLen(std::min(std::max(kMinDefaultLen, MaxSize), kMaxSaneLen));
757 assert(MaxInputLen > 0);
758
kcc2cd9f092017-10-13 01:12:23 +0000759 // Test the callback with empty input and never try it again.
760 uint8_t dummy = 0;
761 ExecuteCallback(&dummy, 0);
762
kccdc00cd32017-08-29 20:51:24 +0000763 if (SizedFiles.empty()) {
764 Printf("INFO: A corpus is not provided, starting from an empty corpus\n");
765 Unit U({'\n'}); // Valid ASCII input.
766 RunOne(U.data(), U.size());
767 } else {
768 Printf("INFO: seed corpus: files: %zd min: %zdb max: %zdb total: %zdb"
769 " rss: %zdMb\n",
770 SizedFiles.size(), MinSize, MaxSize, TotalSize, GetPeakRSSMb());
771 if (Options.ShuffleAtStartUp)
772 std::shuffle(SizedFiles.begin(), SizedFiles.end(), MD.GetRand());
773
kcc7f5f2222017-09-12 21:58:07 +0000774 if (Options.PreferSmall) {
775 std::stable_sort(SizedFiles.begin(), SizedFiles.end());
776 assert(SizedFiles.front().Size <= SizedFiles.back().Size);
777 }
kccdc00cd32017-08-29 20:51:24 +0000778
779 // Load and execute inputs one by one.
780 for (auto &SF : SizedFiles) {
kccae85e292017-08-31 19:17:15 +0000781 auto U = FileToVector(SF.File, MaxInputLen, /*ExitOnError=*/false);
kccdc00cd32017-08-29 20:51:24 +0000782 assert(U.size() <= MaxInputLen);
783 RunOne(U.data(), U.size());
784 CheckExitOnSrcPosOrItem();
785 TryDetectingAMemoryLeak(U.data(), U.size(),
786 /*DuringInitialCorpusExecution*/ true);
787 }
kcc2e93b3f2017-08-29 02:05:01 +0000788 }
789
kccdc00cd32017-08-29 20:51:24 +0000790 PrintStats("INITED");
kcc3acbe072018-05-16 23:26:37 +0000791 if (!Options.FocusFunction.empty())
792 Printf("INFO: %zd/%zd inputs touch the focus function\n",
793 Corpus.NumInputsThatTouchFocusFunction(), Corpus.size());
kccadf188b2018-06-07 01:40:20 +0000794 if (!Options.DataFlowTrace.empty())
795 Printf("INFO: %zd/%zd inputs have the Data Flow Trace\n",
796 Corpus.NumInputsWithDataFlowTrace(), Corpus.size());
kcc3acbe072018-05-16 23:26:37 +0000797
dor1s770a9bc2018-05-23 19:42:30 +0000798 if (Corpus.empty() && Options.MaxNumberOfRuns) {
kccdc00cd32017-08-29 20:51:24 +0000799 Printf("ERROR: no interesting inputs were found. "
800 "Is the code instrumented for coverage? Exiting.\n");
801 exit(1);
kcc2e93b3f2017-08-29 02:05:01 +0000802 }
kcc2e93b3f2017-08-29 02:05:01 +0000803}
804
805void Fuzzer::Loop(const Vector<std::string> &CorpusDirs) {
806 ReadAndExecuteSeedCorpora(CorpusDirs);
kccadf188b2018-06-07 01:40:20 +0000807 DFT.Clear(); // No need for DFT any more.
george.karpenkov29efa6d2017-08-21 23:25:50 +0000808 TPC.SetPrintNewPCs(Options.PrintNewCovPcs);
kcc00da6482017-08-25 20:09:25 +0000809 TPC.SetPrintNewFuncs(Options.PrintNewCovFuncs);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000810 system_clock::time_point LastCorpusReload = system_clock::now();
811 if (Options.DoCrossOver)
812 MD.SetCorpus(&Corpus);
813 while (true) {
814 auto Now = system_clock::now();
815 if (duration_cast<seconds>(Now - LastCorpusReload).count() >=
816 Options.ReloadIntervalSec) {
817 RereadOutputCorpus(MaxInputLen);
818 LastCorpusReload = system_clock::now();
819 }
820 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
821 break;
alekseyshl9f6a9f22017-10-23 23:24:33 +0000822 if (TimedOut())
823 break;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000824
825 // Update TmpMaxMutationLen
morehouse8c42ada2018-02-13 20:52:15 +0000826 if (Options.LenControl) {
george.karpenkov29efa6d2017-08-21 23:25:50 +0000827 if (TmpMaxMutationLen < MaxMutationLen &&
kcce29d7e32017-12-12 23:11:28 +0000828 TotalNumberOfRuns - LastCorpusUpdateRun >
morehouse8c42ada2018-02-13 20:52:15 +0000829 Options.LenControl * Log(TmpMaxMutationLen)) {
george.karpenkov29efa6d2017-08-21 23:25:50 +0000830 TmpMaxMutationLen =
kcce29d7e32017-12-12 23:11:28 +0000831 Min(MaxMutationLen, TmpMaxMutationLen + Log(TmpMaxMutationLen));
kcce29d7e32017-12-12 23:11:28 +0000832 LastCorpusUpdateRun = TotalNumberOfRuns;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000833 }
834 } else {
835 TmpMaxMutationLen = MaxMutationLen;
836 }
837
838 // Perform several mutations and runs.
839 MutateAndTestOne();
alekseyshld995b552017-10-23 22:04:30 +0000840
841 PurgeAllocator();
george.karpenkov29efa6d2017-08-21 23:25:50 +0000842 }
843
844 PrintStats("DONE ", "\n");
845 MD.PrintRecommendedDictionary();
846}
847
848void Fuzzer::MinimizeCrashLoop(const Unit &U) {
alekseyshl9f6a9f22017-10-23 23:24:33 +0000849 if (U.size() <= 1)
850 return;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000851 while (!TimedOut() && TotalNumberOfRuns < Options.MaxNumberOfRuns) {
852 MD.StartMutationSequence();
853 memcpy(CurrentUnitData, U.data(), U.size());
854 for (int i = 0; i < Options.MutateDepth; i++) {
855 size_t NewSize = MD.Mutate(CurrentUnitData, U.size(), MaxMutationLen);
856 assert(NewSize > 0 && NewSize <= MaxMutationLen);
857 ExecuteCallback(CurrentUnitData, NewSize);
858 PrintPulseAndReportSlowInput(CurrentUnitData, NewSize);
859 TryDetectingAMemoryLeak(CurrentUnitData, NewSize,
860 /*DuringInitialCorpusExecution*/ false);
861 }
862 }
863}
864
865void Fuzzer::AnnounceOutput(const uint8_t *Data, size_t Size) {
866 if (SMR.IsServer()) {
867 SMR.WriteByteArray(Data, Size);
868 } else if (SMR.IsClient()) {
869 SMR.PostClient();
870 SMR.WaitServer();
871 size_t OtherSize = SMR.ReadByteArraySize();
872 uint8_t *OtherData = SMR.GetByteArray();
873 if (Size != OtherSize || memcmp(Data, OtherData, Size) != 0) {
874 size_t i = 0;
875 for (i = 0; i < Min(Size, OtherSize); i++)
876 if (Data[i] != OtherData[i])
877 break;
878 Printf("==%lu== ERROR: libFuzzer: equivalence-mismatch. Sizes: %zd %zd; "
alekseyshl9f6a9f22017-10-23 23:24:33 +0000879 "offset %zd\n",
880 GetPid(), Size, OtherSize, i);
george.karpenkov29efa6d2017-08-21 23:25:50 +0000881 DumpCurrentUnit("mismatch-");
882 Printf("SUMMARY: libFuzzer: equivalence-mismatch\n");
883 PrintFinalStats();
884 _Exit(Options.ErrorExitCode);
885 }
886 }
887}
888
889} // namespace fuzzer
890
891extern "C" {
892
phosek966475e2018-01-17 20:39:14 +0000893__attribute__((visibility("default"))) size_t
894LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
george.karpenkov29efa6d2017-08-21 23:25:50 +0000895 assert(fuzzer::F);
896 return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize);
897}
898
899// Experimental
phosek966475e2018-01-17 20:39:14 +0000900__attribute__((visibility("default"))) void
901LLVMFuzzerAnnounceOutput(const uint8_t *Data, size_t Size) {
george.karpenkov29efa6d2017-08-21 23:25:50 +0000902 assert(fuzzer::F);
903 fuzzer::F->AnnounceOutput(Data, Size);
904}
alekseyshl9f6a9f22017-10-23 23:24:33 +0000905} // extern "C"