blob: 378178e064d60430d2fdc88b1e2303f46083f322 [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +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 "FuzzerInternal.h"
Aaron Ballmanef116982015-01-29 16:58:29 +000013#include <algorithm>
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +000014#include <cstring>
15#include <memory>
Aaron Ballmanef116982015-01-29 16:58:29 +000016
Kostya Serebryany2a48c242015-11-13 01:54:40 +000017#if defined(__has_include)
Ivan Krasindf919102016-01-22 22:28:27 +000018#if __has_include(<sanitizer / coverage_interface.h>)
19#include <sanitizer/coverage_interface.h>
20#endif
Kostya Serebryany1bfd5832016-04-20 00:24:21 +000021#if __has_include(<sanitizer / lsan_interface.h>)
22#include <sanitizer/lsan_interface.h>
23#endif
Kostya Serebryany2a48c242015-11-13 01:54:40 +000024#endif
25
Benjamin Kramerd96b0c12016-03-18 14:19:19 +000026#define NO_SANITIZE_MEMORY
27#if defined(__has_feature)
28#if __has_feature(memory_sanitizer)
29#undef NO_SANITIZE_MEMORY
30#define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
31#endif
32#endif
33
Kostya Serebryanyd6edce92015-10-16 23:04:31 +000034extern "C" {
Kostya Serebryany5eab74e2015-11-09 23:17:45 +000035// Re-declare some of the sanitizer functions as "weak" so that
Kostya Serebryany2a48c242015-11-13 01:54:40 +000036// libFuzzer can be linked w/o the sanitizers and sanitizer-coverage
Kostya Serebryany5eab74e2015-11-09 23:17:45 +000037// (in which case it will complain at start-up time).
Kostya Serebryanyd6edce92015-10-16 23:04:31 +000038__attribute__((weak)) void __sanitizer_print_stack_trace();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000039__attribute__((weak)) void __sanitizer_reset_coverage();
Kostya Serebryany94660b32015-10-23 18:37:58 +000040__attribute__((weak)) size_t __sanitizer_get_total_unique_caller_callee_pairs();
Kostya Serebryany5eab74e2015-11-09 23:17:45 +000041__attribute__((weak)) size_t __sanitizer_get_total_unique_coverage();
Ivan Krasindf919102016-01-22 22:28:27 +000042__attribute__((weak)) void
43__sanitizer_set_death_callback(void (*callback)(void));
Kostya Serebryany5eab74e2015-11-09 23:17:45 +000044__attribute__((weak)) size_t __sanitizer_get_number_of_counters();
Ivan Krasindf919102016-01-22 22:28:27 +000045__attribute__((weak)) uintptr_t
46__sanitizer_update_counter_bitset_and_clear_counters(uint8_t *bitset);
Mike Aizatsky8b11f872016-01-06 00:21:22 +000047__attribute__((weak)) uintptr_t
48__sanitizer_get_coverage_pc_buffer(uintptr_t **data);
Kostya Serebryany22cc5e22016-02-13 02:29:38 +000049
Kostya Serebryany1bfd5832016-04-20 00:24:21 +000050__attribute__((weak)) void __sanitizer_malloc_hook(void *ptr, size_t size);
51__attribute__((weak)) void __sanitizer_free_hook(void *ptr);
52__attribute__((weak)) void __lsan_enable();
53__attribute__((weak)) void __lsan_disable();
54__attribute__((weak)) int __lsan_do_recoverable_leak_check();
Kostya Serebryanyf6414422016-06-02 01:33:11 +000055__attribute__((weak)) int __sanitizer_print_memory_profile(size_t);
Kostya Serebryanyd6edce92015-10-16 23:04:31 +000056}
57
Aaron Ballmanef116982015-01-29 16:58:29 +000058namespace fuzzer {
Kostya Serebryanya9da9b42015-10-16 22:47:20 +000059static const size_t kMaxUnitSizeToPrint = 256;
Mike Aizatskyaf432a42016-05-24 23:14:29 +000060static const size_t TruncateMaxRuns = 1000;
Aaron Ballmanef116982015-01-29 16:58:29 +000061
Kostya Serebryanyf26017b2016-05-26 21:32:30 +000062thread_local bool Fuzzer::IsMyThread;
63
Kostya Serebryany5eab74e2015-11-09 23:17:45 +000064static void MissingWeakApiFunction(const char *FnName) {
65 Printf("ERROR: %s is not defined. Exiting.\n"
Ivan Krasindf919102016-01-22 22:28:27 +000066 "Did you use -fsanitize-coverage=... to build your code?\n",
67 FnName);
Kostya Serebryany5eab74e2015-11-09 23:17:45 +000068 exit(1);
69}
70
71#define CHECK_WEAK_API_FUNCTION(fn) \
72 do { \
73 if (!fn) \
74 MissingWeakApiFunction(#fn); \
75 } while (false)
76
Kostya Serebryany52a788e2015-03-31 20:13:20 +000077// Only one Fuzzer per process.
78static Fuzzer *F;
79
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000080struct CoverageController {
81 static void Reset() {
82 CHECK_WEAK_API_FUNCTION(__sanitizer_reset_coverage);
83 __sanitizer_reset_coverage();
84 PcMapResetCurrent();
85 }
86
87 static void ResetCounters(const Fuzzer::FuzzingOptions &Options) {
88 if (Options.UseCounters) {
89 __sanitizer_update_counter_bitset_and_clear_counters(0);
90 }
91 }
92
93 static void Prepare(const Fuzzer::FuzzingOptions &Options,
94 Fuzzer::Coverage *C) {
95 if (Options.UseCounters) {
96 size_t NumCounters = __sanitizer_get_number_of_counters();
97 C->CounterBitmap.resize(NumCounters);
98 }
99 }
100
101 // Records data to a maximum coverage tracker. Returns true if additional
102 // coverage was discovered.
103 static bool RecordMax(const Fuzzer::FuzzingOptions &Options,
104 Fuzzer::Coverage *C) {
105 bool Res = false;
106
107 uint64_t NewBlockCoverage = __sanitizer_get_total_unique_coverage();
108 if (NewBlockCoverage > C->BlockCoverage) {
109 Res = true;
110 C->BlockCoverage = NewBlockCoverage;
111 }
112
113 if (Options.UseIndirCalls &&
114 __sanitizer_get_total_unique_caller_callee_pairs) {
115 uint64_t NewCallerCalleeCoverage =
116 __sanitizer_get_total_unique_caller_callee_pairs();
117 if (NewCallerCalleeCoverage > C->CallerCalleeCoverage) {
118 Res = true;
119 C->CallerCalleeCoverage = NewCallerCalleeCoverage;
120 }
121 }
122
123 if (Options.UseCounters) {
124 uint64_t CounterDelta =
125 __sanitizer_update_counter_bitset_and_clear_counters(
126 C->CounterBitmap.data());
127 if (CounterDelta > 0) {
128 Res = true;
129 C->CounterBitmapBits += CounterDelta;
130 }
131 }
132
133 uint64_t NewPcMapBits = PcMapMergeInto(&C->PCMap);
134 if (NewPcMapBits > C->PcMapBits) {
135 Res = true;
136 C->PcMapBits = NewPcMapBits;
137 }
138
139 uintptr_t *CoverageBuf;
140 uint64_t NewPcBufferLen = __sanitizer_get_coverage_pc_buffer(&CoverageBuf);
141 if (NewPcBufferLen > C->PcBufferLen) {
142 Res = true;
143 C->PcBufferLen = NewPcBufferLen;
144 }
145
146 return Res;
147 }
148};
149
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000150Fuzzer::Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options)
151 : CB(CB), MD(MD), Options(Options) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000152 SetDeathCallback();
Kostya Serebryany22526252015-05-11 21:16:27 +0000153 InitializeTraceState();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000154 assert(!F);
155 F = this;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000156 ResetCoverage();
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000157 IsMyThread = true;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000158}
Aaron Ballmanef116982015-01-29 16:58:29 +0000159
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000160void Fuzzer::LazyAllocateCurrentUnitData() {
161 if (CurrentUnitData || Options.MaxLen == 0) return;
162 CurrentUnitData = new uint8_t[Options.MaxLen];
163}
164
Aaron Ballmanef116982015-01-29 16:58:29 +0000165void Fuzzer::SetDeathCallback() {
Kostya Serebryany5eab74e2015-11-09 23:17:45 +0000166 CHECK_WEAK_API_FUNCTION(__sanitizer_set_death_callback);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000167 __sanitizer_set_death_callback(StaticDeathCallback);
168}
169
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000170void Fuzzer::StaticDeathCallback() {
171 assert(F);
172 F->DeathCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +0000173}
174
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000175void Fuzzer::DumpCurrentUnit(const char *Prefix) {
Kostya Serebryany311cc832016-05-28 04:19:46 +0000176 if (!CurrentUnitData) return; // Happens when running individual inputs.
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000177 size_t UnitSize = CurrentUnitSize;
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000178 if (UnitSize <= kMaxUnitSizeToPrint) {
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000179 PrintHexArray(CurrentUnitData, UnitSize, "\n");
180 PrintASCII(CurrentUnitData, UnitSize, "\n");
Kostya Serebryanye95022a2015-10-09 04:03:14 +0000181 }
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000182 WriteUnitToFileWithPrefix({CurrentUnitData, CurrentUnitData + UnitSize},
183 Prefix);
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000184}
185
Benjamin Kramerd96b0c12016-03-18 14:19:19 +0000186NO_SANITIZE_MEMORY
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000187void Fuzzer::DeathCallback() {
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000188 DumpCurrentUnit("crash-");
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000189 PrintFinalStats();
Aaron Ballmanef116982015-01-29 16:58:29 +0000190}
191
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000192void Fuzzer::StaticAlarmCallback() {
193 assert(F);
194 F->AlarmCallback();
195}
196
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000197void Fuzzer::StaticCrashSignalCallback() {
198 assert(F);
199 F->CrashCallback();
200}
201
202void Fuzzer::StaticInterruptCallback() {
203 assert(F);
204 F->InterruptCallback();
205}
206
207void Fuzzer::CrashCallback() {
208 Printf("==%d== ERROR: libFuzzer: deadly signal\n", GetPid());
209 if (__sanitizer_print_stack_trace)
210 __sanitizer_print_stack_trace();
211 Printf("NOTE: libFuzzer has rudimentary signal handlers.\n"
212 " Combine libFuzzer with AddressSanitizer or similar for better "
213 "crash reports.\n");
214 Printf("SUMMARY: libFuzzer: deadly signal\n");
215 DumpCurrentUnit("crash-");
216 PrintFinalStats();
217 exit(Options.ErrorExitCode);
218}
219
220void Fuzzer::InterruptCallback() {
221 Printf("==%d== libFuzzer: run interrupted; exiting\n", GetPid());
222 PrintFinalStats();
Kostya Serebryanye483ed22016-03-03 22:36:37 +0000223 _Exit(0); // Stop right now, don't perform any at-exit actions.
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000224}
225
Benjamin Kramerd96b0c12016-03-18 14:19:19 +0000226NO_SANITIZE_MEMORY
Aaron Ballmanef116982015-01-29 16:58:29 +0000227void Fuzzer::AlarmCallback() {
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000228 assert(Options.UnitTimeoutSec > 0);
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000229 if (!InFuzzingThread()) return;
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000230 if (!CurrentUnitSize)
Kostya Serebryanycfbcf902016-02-17 19:42:34 +0000231 return; // We have not started running units yet.
Aaron Ballmanef116982015-01-29 16:58:29 +0000232 size_t Seconds =
233 duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
Ivan Krasindf919102016-01-22 22:28:27 +0000234 if (Seconds == 0)
235 return;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000236 if (Options.Verbosity >= 2)
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000237 Printf("AlarmCallback %zd\n", Seconds);
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000238 if (Seconds >= (size_t)Options.UnitTimeoutSec) {
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000239 Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds);
Kostya Serebryany316b5712015-05-26 20:57:47 +0000240 Printf(" and the timeout value is %d (use -timeout=N to change)\n",
241 Options.UnitTimeoutSec);
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000242 DumpCurrentUnit("timeout-");
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000243 Printf("==%d== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(),
244 Seconds);
245 if (__sanitizer_print_stack_trace)
246 __sanitizer_print_stack_trace();
247 Printf("SUMMARY: libFuzzer: timeout\n");
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000248 PrintFinalStats();
Kostya Serebryany31516732016-03-24 01:32:08 +0000249 _Exit(Options.TimeoutExitCode); // Stop right now.
Aaron Ballmanef116982015-01-29 16:58:29 +0000250 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000251}
252
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000253void Fuzzer::RssLimitCallback() {
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000254 Printf(
255 "==%d== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n",
256 GetPid(), GetPeakRSSMb(), Options.RssLimitMb);
Kostya Serebryanyf6414422016-06-02 01:33:11 +0000257 Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
258 if (__sanitizer_print_memory_profile)
259 __sanitizer_print_memory_profile(50);
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000260 DumpCurrentUnit("oom-");
261 Printf("SUMMARY: libFuzzer: out-of-memory\n");
262 PrintFinalStats();
263 _Exit(Options.ErrorExitCode); // Stop right now.
Kostya Serebryany52b394e2016-05-06 21:58:35 +0000264}
265
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000266void Fuzzer::PrintStats(const char *Where, const char *End) {
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000267 size_t ExecPerSec = execPerSec();
Mike Aizatskya9c23872015-11-12 04:38:40 +0000268 if (Options.OutputCSV) {
269 static bool csvHeaderPrinted = false;
270 if (!csvHeaderPrinted) {
271 csvHeaderPrinted = true;
272 Printf("runs,block_cov,bits,cc_cov,corpus,execs_per_sec,tbms,reason\n");
273 }
Kostya Serebryany9ba19182016-04-25 19:41:45 +0000274 Printf("%zd,%zd,%zd,%zd,%zd,%zd,%s\n", TotalNumberOfRuns,
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000275 MaxCoverage.BlockCoverage, MaxCoverage.CounterBitmapBits,
276 MaxCoverage.CallerCalleeCoverage, Corpus.size(), ExecPerSec, Where);
Mike Aizatskya9c23872015-11-12 04:38:40 +0000277 }
278
279 if (!Options.Verbosity)
280 return;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000281 Printf("#%zd\t%s", TotalNumberOfRuns, Where);
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000282 if (MaxCoverage.BlockCoverage)
283 Printf(" cov: %zd", MaxCoverage.BlockCoverage);
284 if (MaxCoverage.PcMapBits)
285 Printf(" path: %zd", MaxCoverage.PcMapBits);
286 if (auto TB = MaxCoverage.CounterBitmapBits)
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000287 Printf(" bits: %zd", TB);
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000288 if (MaxCoverage.CallerCalleeCoverage)
289 Printf(" indir: %zd", MaxCoverage.CallerCalleeCoverage);
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000290 Printf(" units: %zd exec/s: %zd", Corpus.size(), ExecPerSec);
Kostya Serebryany12c78372015-08-12 01:55:37 +0000291 Printf("%s", End);
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000292}
293
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000294void Fuzzer::PrintFinalStats() {
295 if (!Options.PrintFinalStats) return;
296 size_t ExecPerSec = execPerSec();
297 Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns);
298 Printf("stat::average_exec_per_sec: %zd\n", ExecPerSec);
299 Printf("stat::new_units_added: %zd\n", NumberOfNewUnitsAdded);
300 Printf("stat::slowest_unit_time_sec: %zd\n", TimeOfLongestUnitInSeconds);
301 Printf("stat::peak_rss_mb: %zd\n", GetPeakRSSMb());
302}
303
Kostya Serebryany64d24572016-03-12 01:57:04 +0000304size_t Fuzzer::MaxUnitSizeInCorpus() const {
305 size_t Res = 0;
306 for (auto &X : Corpus)
307 Res = std::max(Res, X.size());
308 return Res;
309}
310
311void Fuzzer::SetMaxLen(size_t MaxLen) {
312 assert(Options.MaxLen == 0); // Can only reset MaxLen from 0 to non-0.
313 assert(MaxLen);
314 Options.MaxLen = MaxLen;
315 Printf("INFO: -max_len is not provided, using %zd\n", Options.MaxLen);
316}
317
318
319void Fuzzer::RereadOutputCorpus(size_t MaxSize) {
Ivan Krasindf919102016-01-22 22:28:27 +0000320 if (Options.OutputCorpus.empty())
321 return;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000322 std::vector<Unit> AdditionalCorpus;
323 ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus,
Kostya Serebryany64d24572016-03-12 01:57:04 +0000324 &EpochOfLastReadOfOutputCorpus, MaxSize);
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000325 if (Corpus.empty()) {
326 Corpus = AdditionalCorpus;
327 return;
328 }
Ivan Krasindf919102016-01-22 22:28:27 +0000329 if (!Options.Reload)
330 return;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000331 if (Options.Verbosity >= 2)
Ivan Krasindf919102016-01-22 22:28:27 +0000332 Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000333 for (auto &X : AdditionalCorpus) {
Kostya Serebryany64d24572016-03-12 01:57:04 +0000334 if (X.size() > MaxSize)
335 X.resize(MaxSize);
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000336 if (UnitHashesAddedToCorpus.insert(Hash(X)).second) {
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000337 if (RunOne(X)) {
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000338 Corpus.push_back(X);
Ivan Krasindf919102016-01-22 22:28:27 +0000339 UpdateCorpusDistribution();
Mike Aizatskya9c23872015-11-12 04:38:40 +0000340 PrintStats("RELOAD");
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000341 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000342 }
343 }
344}
345
Kostya Serebryany945761b2016-03-18 00:23:29 +0000346void Fuzzer::ShuffleCorpus(UnitVector *V) {
347 std::random_shuffle(V->begin(), V->end(), MD.GetRand());
348 if (Options.PreferSmall)
349 std::stable_sort(V->begin(), V->end(), [](const Unit &A, const Unit &B) {
350 return A.size() < B.size();
351 });
352}
353
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000354// Tries random prefixes of corpus items.
355// Prefix length is chosen according to exponential distribution
356// to sample short lengths much more heavily.
357void Fuzzer::TruncateUnits(std::vector<Unit> *NewCorpus) {
358 size_t MaxCorpusLen = 0;
359 for (const auto &U : Corpus)
360 MaxCorpusLen = std::max(MaxCorpusLen, U.size());
361
362 if (MaxCorpusLen <= 1)
363 return;
364
365 // 50% of exponential distribution is Log[2]/lambda.
366 // Choose lambda so that median is MaxCorpusLen / 2.
367 double Lambda = 2.0 * log(2.0) / static_cast<double>(MaxCorpusLen);
368 std::exponential_distribution<> Dist(Lambda);
369 std::vector<double> Sizes;
370 size_t TruncatePoints = std::max(1ul, TruncateMaxRuns / Corpus.size());
371 Sizes.reserve(TruncatePoints);
372 for (size_t I = 0; I < TruncatePoints; ++I) {
373 Sizes.push_back(Dist(MD.GetRand().Get_mt19937()) + 1);
374 }
375 std::sort(Sizes.begin(), Sizes.end());
376
377 for (size_t S : Sizes) {
378 for (const auto &U : Corpus) {
379 if (S < U.size() && RunOne(U.data(), S)) {
380 Unit U1(U.begin(), U.begin() + S);
381 NewCorpus->push_back(U1);
382 WriteToOutputCorpus(U1);
383 PrintStatusForNewUnit(U1);
384 }
385 }
386 }
387 PrintStats("TRUNC ");
388}
389
Aaron Ballmanef116982015-01-29 16:58:29 +0000390void Fuzzer::ShuffleAndMinimize() {
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000391 PrintStats("READ ");
Aaron Ballmanef116982015-01-29 16:58:29 +0000392 std::vector<Unit> NewCorpus;
Kostya Serebryany945761b2016-03-18 00:23:29 +0000393 if (Options.ShuffleAtStartUp)
394 ShuffleCorpus(&Corpus);
395
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000396 if (Options.TruncateUnits) {
397 ResetCoverage();
398 TruncateUnits(&NewCorpus);
399 ResetCoverage();
400 }
401
Kostya Serebryany945761b2016-03-18 00:23:29 +0000402 for (const auto &U : Corpus) {
403 if (RunOne(U)) {
404 NewCorpus.push_back(U);
405 if (Options.Verbosity >= 2)
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000406 Printf("NEW0: %zd L %zd\n", MaxCoverage.BlockCoverage, U.size());
Aaron Ballmanef116982015-01-29 16:58:29 +0000407 }
Kostya Serebryany4b923262016-05-26 20:25:49 +0000408 TryDetectingAMemoryLeak(U.data(), U.size(),
409 /*DuringInitialCorpusExecution*/ true);
Aaron Ballmanef116982015-01-29 16:58:29 +0000410 }
411 Corpus = NewCorpus;
Ivan Krasindf919102016-01-22 22:28:27 +0000412 UpdateCorpusDistribution();
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000413 for (auto &X : Corpus)
414 UnitHashesAddedToCorpus.insert(Hash(X));
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000415 PrintStats("INITED");
Aaron Ballmanef116982015-01-29 16:58:29 +0000416}
417
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000418bool Fuzzer::UpdateMaxCoverage() {
419 uintptr_t PrevBufferLen = MaxCoverage.PcBufferLen;
420 bool Res = CoverageController::RecordMax(Options, &MaxCoverage);
421
422 if (Options.PrintNewCovPcs && PrevBufferLen != MaxCoverage.PcBufferLen) {
423 uintptr_t *CoverageBuf;
424 __sanitizer_get_coverage_pc_buffer(&CoverageBuf);
425 assert(CoverageBuf);
426 for (size_t I = PrevBufferLen; I < MaxCoverage.PcBufferLen; ++I) {
427 Printf("%p\n", CoverageBuf[I]);
428 }
429 }
430
431 return Res;
432}
433
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000434bool Fuzzer::RunOne(const uint8_t *Data, size_t Size) {
Aaron Ballmanef116982015-01-29 16:58:29 +0000435 TotalNumberOfRuns++;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000436
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000437 // TODO(aizatsky): this Reset call seems to be not needed.
438 CoverageController::ResetCounters(Options);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000439 ExecuteCallback(Data, Size);
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000440 bool Res = UpdateMaxCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000441
Kostya Serebryany16901a92015-03-30 23:04:35 +0000442 auto UnitStopTime = system_clock::now();
443 auto TimeOfUnit =
444 duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
Mike Aizatskya9c23872015-11-12 04:38:40 +0000445 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) &&
446 secondsSinceProcessStartUp() >= 2)
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000447 PrintStats("pulse ");
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000448 if (TimeOfUnit > TimeOfLongestUnitInSeconds &&
449 TimeOfUnit >= Options.ReportSlowUnits) {
Kostya Serebryany16901a92015-03-30 23:04:35 +0000450 TimeOfLongestUnitInSeconds = TimeOfUnit;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000451 Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000452 WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-");
Kostya Serebryany16901a92015-03-30 23:04:35 +0000453 }
454 return Res;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000455}
456
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000457void Fuzzer::RunOneAndUpdateCorpus(const uint8_t *Data, size_t Size) {
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000458 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
459 return;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000460 if (RunOne(Data, Size))
461 ReportNewCoverage({Data, Data + Size});
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000462}
463
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000464// Leak detection is expensive, so we first check if there were more mallocs
465// than frees (using the sanitizer malloc hooks) and only then try to call lsan.
466struct MallocFreeTracer {
467 void Start() {
468 Mallocs = 0;
469 Frees = 0;
470 }
471 // Returns true if there were more mallocs than frees.
472 bool Stop() { return Mallocs > Frees; }
473 size_t Mallocs;
474 size_t Frees;
475};
476
477static thread_local MallocFreeTracer AllocTracer;
478
Dan Liew3868e462016-05-19 22:00:33 +0000479// FIXME: The hooks only count on Linux because
480// on Mac OSX calls to malloc are intercepted before
481// thread local storage is initialised leading to
482// crashes when accessing ``AllocTracer``.
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000483extern "C" {
Dan Liew3868e462016-05-19 22:00:33 +0000484void __sanitizer_malloc_hook(void *ptr, size_t size) {
485 if (!LIBFUZZER_APPLE)
486 AllocTracer.Mallocs++;
487}
488void __sanitizer_free_hook(void *ptr) {
489 if (!LIBFUZZER_APPLE)
490 AllocTracer.Frees++;
491}
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000492} // extern "C"
493
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000494size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const {
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000495 assert(InFuzzingThread());
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000496 *Data = CurrentUnitData;
497 return CurrentUnitSize;
498}
499
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000500void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000501 assert(InFuzzingThread());
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000502 LazyAllocateCurrentUnitData();
Kostya Serebryanyebb932d2016-04-18 22:50:39 +0000503 UnitStartTime = system_clock::now();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000504 // We copy the contents of Unit into a separate heap buffer
505 // so that we reliably find buffer overflows in it.
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000506 std::unique_ptr<uint8_t[]> DataCopy(new uint8_t[Size]);
507 memcpy(DataCopy.get(), Data, Size);
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000508 if (CurrentUnitData && CurrentUnitData != Data)
509 memcpy(CurrentUnitData, Data, Size);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000510 AssignTaintLabels(DataCopy.get(), Size);
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000511 CurrentUnitSize = Size;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000512 AllocTracer.Start();
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000513 int Res = CB(DataCopy.get(), Size);
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000514 (void)Res;
515 HasMoreMallocsThanFrees = AllocTracer.Stop();
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000516 CurrentUnitSize = 0;
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000517 assert(Res == 0);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000518}
519
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000520std::string Fuzzer::Coverage::DebugString() const {
521 std::string Result =
522 std::string("Coverage{") + "BlockCoverage=" +
523 std::to_string(BlockCoverage) + " CallerCalleeCoverage=" +
524 std::to_string(CallerCalleeCoverage) + " CounterBitmapBits=" +
525 std::to_string(CounterBitmapBits) + " PcMapBits=" +
526 std::to_string(PcMapBits) + "}";
527 return Result;
Aaron Ballmanef116982015-01-29 16:58:29 +0000528}
529
530void Fuzzer::WriteToOutputCorpus(const Unit &U) {
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000531 if (Options.OnlyASCII)
532 assert(IsASCII(U));
Ivan Krasindf919102016-01-22 22:28:27 +0000533 if (Options.OutputCorpus.empty())
534 return;
Aaron Ballmanef116982015-01-29 16:58:29 +0000535 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
536 WriteToFile(U, Path);
537 if (Options.Verbosity >= 2)
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000538 Printf("Written to %s\n", Path.c_str());
Aaron Ballmanef116982015-01-29 16:58:29 +0000539}
540
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000541void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000542 if (!Options.SaveArtifacts)
543 return;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000544 std::string Path = Options.ArtifactPrefix + Prefix + Hash(U);
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000545 if (!Options.ExactArtifactPath.empty())
Ivan Krasindf919102016-01-22 22:28:27 +0000546 Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix.
Aaron Ballmanef116982015-01-29 16:58:29 +0000547 WriteToFile(U, Path);
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000548 Printf("artifact_prefix='%s'; Test unit written to %s\n",
549 Options.ArtifactPrefix.c_str(), Path.c_str());
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000550 if (U.size() <= kMaxUnitSizeToPrint)
551 Printf("Base64: %s\n", Base64(U).c_str());
Aaron Ballmanef116982015-01-29 16:58:29 +0000552}
553
554void Fuzzer::SaveCorpus() {
Ivan Krasindf919102016-01-22 22:28:27 +0000555 if (Options.OutputCorpus.empty())
556 return;
Aaron Ballmanef116982015-01-29 16:58:29 +0000557 for (const auto &U : Corpus)
558 WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U)));
559 if (Options.Verbosity)
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000560 Printf("Written corpus of %zd files to %s\n", Corpus.size(),
561 Options.OutputCorpus.c_str());
Aaron Ballmanef116982015-01-29 16:58:29 +0000562}
563
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000564void Fuzzer::PrintStatusForNewUnit(const Unit &U) {
565 if (!Options.PrintNEW)
566 return;
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000567 PrintStats("NEW ", "");
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000568 if (Options.Verbosity) {
Kostya Serebryany14c50282015-12-19 01:09:49 +0000569 Printf(" L: %zd ", U.size());
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000570 MD.PrintMutationSequence();
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000571 Printf("\n");
Aaron Ballmanef116982015-01-29 16:58:29 +0000572 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000573}
574
575void Fuzzer::ReportNewCoverage(const Unit &U) {
576 Corpus.push_back(U);
Ivan Krasindf919102016-01-22 22:28:27 +0000577 UpdateCorpusDistribution();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000578 UnitHashesAddedToCorpus.insert(Hash(U));
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000579 MD.RecordSuccessfulMutationSequence();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000580 PrintStatusForNewUnit(U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000581 WriteToOutputCorpus(U);
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000582 NumberOfNewUnitsAdded++;
Aaron Ballmanef116982015-01-29 16:58:29 +0000583}
584
Kostya Serebryany945761b2016-03-18 00:23:29 +0000585// Finds minimal number of units in 'Extra' that add coverage to 'Initial'.
586// We do it by actually executing the units, sometimes more than once,
587// because we may be using different coverage-like signals and the only
588// common thing between them is that we can say "this unit found new stuff".
589UnitVector Fuzzer::FindExtraUnits(const UnitVector &Initial,
590 const UnitVector &Extra) {
591 UnitVector Res = Extra;
592 size_t OldSize = Res.size();
593 for (int Iter = 0; Iter < 10; Iter++) {
594 ShuffleCorpus(&Res);
595 ResetCoverage();
596
597 for (auto &U : Initial)
598 RunOne(U);
599
600 Corpus.clear();
601 for (auto &U : Res)
602 if (RunOne(U))
603 Corpus.push_back(U);
604
605 char Stat[7] = "MIN ";
606 Stat[3] = '0' + Iter;
607 PrintStats(Stat);
608
609 size_t NewSize = Corpus.size();
Kostya Serebryanya0788e72016-05-13 22:11:23 +0000610 assert(NewSize <= OldSize);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000611 Res.swap(Corpus);
612
Kostya Serebryanya0788e72016-05-13 22:11:23 +0000613 if (NewSize + 5 >= OldSize)
Kostya Serebryany945761b2016-03-18 00:23:29 +0000614 break;
615 OldSize = NewSize;
616 }
617 return Res;
618}
619
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000620void Fuzzer::Merge(const std::vector<std::string> &Corpora) {
621 if (Corpora.size() <= 1) {
622 Printf("Merge requires two or more corpus dirs\n");
623 return;
624 }
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000625 std::vector<std::string> ExtraCorpora(Corpora.begin() + 1, Corpora.end());
626
Kostya Serebryany945761b2016-03-18 00:23:29 +0000627 assert(Options.MaxLen > 0);
628 UnitVector Initial, Extra;
629 ReadDirToVectorOfUnits(Corpora[0].c_str(), &Initial, nullptr, Options.MaxLen);
630 for (auto &C : ExtraCorpora)
631 ReadDirToVectorOfUnits(C.c_str(), &Extra, nullptr, Options.MaxLen);
632
633 if (!Initial.empty()) {
634 Printf("=== Minimizing the initial corpus of %zd units\n", Initial.size());
635 Initial = FindExtraUnits({}, Initial);
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000636 }
Kostya Serebryany945761b2016-03-18 00:23:29 +0000637
638 Printf("=== Merging extra %zd units\n", Extra.size());
639 auto Res = FindExtraUnits(Initial, Extra);
640
641 for (auto &U: Res)
642 WriteToOutputCorpus(U);
643
644 Printf("=== Merge: written %zd units\n", Res.size());
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000645}
646
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000647// Tries detecting a memory leak on the particular input that we have just
648// executed before calling this function.
Kostya Serebryany4b923262016-05-26 20:25:49 +0000649void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
650 bool DuringInitialCorpusExecution) {
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000651 if (!HasMoreMallocsThanFrees) return; // mallocs==frees, a leak is unlikely.
652 if (!Options.DetectLeaks) return;
653 if (!&__lsan_enable || !&__lsan_disable || !__lsan_do_recoverable_leak_check)
654 return; // No lsan.
655 // Run the target once again, but with lsan disabled so that if there is
656 // a real leak we do not report it twice.
657 __lsan_disable();
Kostya Serebryany47952102016-05-29 15:58:57 +0000658 RunOne(Data, Size);
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000659 __lsan_enable();
660 if (!HasMoreMallocsThanFrees) return; // a leak is unlikely.
Kostya Serebryany7018a1a2016-04-27 19:52:34 +0000661 if (NumberOfLeakDetectionAttempts++ > 1000) {
662 Options.DetectLeaks = false;
663 Printf("INFO: libFuzzer disabled leak detection after every mutation.\n"
664 " Most likely the target function accumulates allocated\n"
665 " memory in a global state w/o actually leaking it.\n"
666 " If LeakSanitizer is enabled in this process it will still\n"
667 " run on the process shutdown.\n");
668 return;
669 }
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000670 // Now perform the actual lsan pass. This is expensive and we must ensure
671 // we don't call it too often.
672 if (__lsan_do_recoverable_leak_check()) { // Leak is found, report it.
Kostya Serebryany4b923262016-05-26 20:25:49 +0000673 if (DuringInitialCorpusExecution)
674 Printf("\nINFO: a leak has been found in the initial corpus.\n\n");
675 Printf("INFO: to ignore leaks on libFuzzer side use -detect_leaks=0.\n\n");
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000676 CurrentUnitSize = Size;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000677 DumpCurrentUnit("leak-");
678 PrintFinalStats();
679 _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on.
680 }
681}
682
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000683void Fuzzer::MutateAndTestOne() {
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000684 LazyAllocateCurrentUnitData();
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000685 MD.StartMutationSequence();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000686
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000687 auto &U = ChooseUnitToMutate();
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000688 assert(CurrentUnitData);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000689 size_t Size = U.size();
Mike Aizatsky94e29662016-04-08 23:32:24 +0000690 assert(Size <= Options.MaxLen && "Oversized Unit");
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000691 memcpy(CurrentUnitData, U.data(), Size);
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000692
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000693 for (int i = 0; i < Options.MutateDepth; i++) {
Kostya Serebryany22cc5e22016-02-13 02:29:38 +0000694 size_t NewSize = 0;
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000695 NewSize = MD.Mutate(CurrentUnitData, Size, Options.MaxLen);
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000696 assert(NewSize > 0 && "Mutator returned empty unit");
Kostya Serebryany64d24572016-03-12 01:57:04 +0000697 assert(NewSize <= Options.MaxLen &&
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000698 "Mutator return overisized unit");
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000699 Size = NewSize;
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000700 if (Options.OnlyASCII)
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000701 ToASCII(CurrentUnitData, Size);
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000702 if (i == 0)
703 StartTraceRecording();
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000704 RunOneAndUpdateCorpus(CurrentUnitData, Size);
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000705 StopTraceRecording();
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000706 TryDetectingAMemoryLeak(CurrentUnitData, Size,
Kostya Serebryany4b923262016-05-26 20:25:49 +0000707 /*DuringInitialCorpusExecution*/ false);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000708 }
709}
710
Kostya Serebryanye6926212015-11-04 23:22:25 +0000711// Returns an index of random unit from the corpus to mutate.
712// Hypothesis: units added to the corpus last are more likely to be interesting.
Ivan Krasindf919102016-01-22 22:28:27 +0000713// This function gives more weight to the more recent units.
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000714size_t Fuzzer::ChooseUnitIdxToMutate() {
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000715 size_t Idx =
716 static_cast<size_t>(CorpusDistribution(MD.GetRand().Get_mt19937()));
Ivan Krasindf919102016-01-22 22:28:27 +0000717 assert(Idx < Corpus.size());
718 return Idx;
Kostya Serebryanye6926212015-11-04 23:22:25 +0000719}
720
Kostya Serebryany945761b2016-03-18 00:23:29 +0000721void Fuzzer::ResetCoverage() {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000722 CoverageController::Reset();
723 MaxCoverage.Reset();
724 CoverageController::Prepare(Options, &MaxCoverage);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000725}
726
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000727// Experimental search heuristic: drilling.
728// - Read, shuffle, execute and minimize the corpus.
729// - Choose one random unit.
730// - Reset the coverage.
731// - Start fuzzing as if the chosen unit was the only element of the corpus.
732// - When done, reset the coverage again.
733// - Merge the newly created corpus into the original one.
734void Fuzzer::Drill() {
735 // The corpus is already read, shuffled, and minimized.
736 assert(!Corpus.empty());
Ivan Krasindf919102016-01-22 22:28:27 +0000737 Options.PrintNEW = false; // Don't print NEW status lines when drilling.
Kostya Serebryany7d211662015-09-04 00:12:11 +0000738
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000739 Unit U = ChooseUnitToMutate();
740
Kostya Serebryany945761b2016-03-18 00:23:29 +0000741 ResetCoverage();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000742
743 std::vector<Unit> SavedCorpus;
744 SavedCorpus.swap(Corpus);
745 Corpus.push_back(U);
Ivan Krasindf919102016-01-22 22:28:27 +0000746 UpdateCorpusDistribution();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000747 assert(Corpus.size() == 1);
748 RunOne(U);
749 PrintStats("DRILL ");
750 std::string SavedOutputCorpusPath; // Don't write new units while drilling.
751 SavedOutputCorpusPath.swap(Options.OutputCorpus);
752 Loop();
753
Kostya Serebryany945761b2016-03-18 00:23:29 +0000754 ResetCoverage();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000755
756 PrintStats("REINIT");
757 SavedOutputCorpusPath.swap(Options.OutputCorpus);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000758 for (auto &U : SavedCorpus)
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000759 RunOne(U);
760 PrintStats("MERGE ");
761 Options.PrintNEW = true;
762 size_t NumMerged = 0;
763 for (auto &U : Corpus) {
764 if (RunOne(U)) {
765 PrintStatusForNewUnit(U);
766 NumMerged++;
767 WriteToOutputCorpus(U);
768 }
769 }
770 PrintStats("MERGED");
771 if (NumMerged && Options.Verbosity)
772 Printf("Drilling discovered %zd new units\n", NumMerged);
773}
774
775void Fuzzer::Loop() {
Kostya Serebryany8617aaa2015-12-05 02:09:22 +0000776 system_clock::time_point LastCorpusReload = system_clock::now();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000777 if (Options.DoCrossOver)
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000778 MD.SetCorpus(&Corpus);
Kostya Serebryany468ed782015-09-08 17:30:35 +0000779 while (true) {
Kostya Serebryany8617aaa2015-12-05 02:09:22 +0000780 auto Now = system_clock::now();
781 if (duration_cast<seconds>(Now - LastCorpusReload).count()) {
Kostya Serebryany64d24572016-03-12 01:57:04 +0000782 RereadOutputCorpus(Options.MaxLen);
Kostya Serebryany8617aaa2015-12-05 02:09:22 +0000783 LastCorpusReload = Now;
784 }
Kostya Serebryanye6926212015-11-04 23:22:25 +0000785 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
Mike Aizatskya9c23872015-11-12 04:38:40 +0000786 break;
Kostya Serebryanye6926212015-11-04 23:22:25 +0000787 if (Options.MaxTotalTimeSec > 0 &&
788 secondsSinceProcessStartUp() >
Ivan Krasindf919102016-01-22 22:28:27 +0000789 static_cast<size_t>(Options.MaxTotalTimeSec))
Mike Aizatskya9c23872015-11-12 04:38:40 +0000790 break;
Kostya Serebryanye6926212015-11-04 23:22:25 +0000791 // Perform several mutations and runs.
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000792 MutateAndTestOne();
Aaron Ballmanef116982015-01-29 16:58:29 +0000793 }
Mike Aizatskya9c23872015-11-12 04:38:40 +0000794
795 PrintStats("DONE ", "\n");
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000796 MD.PrintRecommendedDictionary();
Aaron Ballmanef116982015-01-29 16:58:29 +0000797}
798
Ivan Krasindf919102016-01-22 22:28:27 +0000799void Fuzzer::UpdateCorpusDistribution() {
800 size_t N = Corpus.size();
801 std::vector<double> Intervals(N + 1);
802 std::vector<double> Weights(N);
803 std::iota(Intervals.begin(), Intervals.end(), 0);
804 std::iota(Weights.begin(), Weights.end(), 1);
805 CorpusDistribution = std::piecewise_constant_distribution<double>(
806 Intervals.begin(), Intervals.end(), Weights.begin());
807}
808
809} // namespace fuzzer
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +0000810
811extern "C" {
812
813size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
814 assert(fuzzer::F);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000815 return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize);
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +0000816}
817} // extern "C"