blob: 0adbc891bd0ac6134ed41895aa8ae421ebe23982 [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
50__attribute__((weak)) size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size,
51 size_t MaxSize,
52 unsigned int Seed);
Kostya Serebryany1bfd5832016-04-20 00:24:21 +000053__attribute__((weak)) void __sanitizer_malloc_hook(void *ptr, size_t size);
54__attribute__((weak)) void __sanitizer_free_hook(void *ptr);
55__attribute__((weak)) void __lsan_enable();
56__attribute__((weak)) void __lsan_disable();
57__attribute__((weak)) int __lsan_do_recoverable_leak_check();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +000058}
59
Aaron Ballmanef116982015-01-29 16:58:29 +000060namespace fuzzer {
Kostya Serebryanya9da9b42015-10-16 22:47:20 +000061static const size_t kMaxUnitSizeToPrint = 256;
Aaron Ballmanef116982015-01-29 16:58:29 +000062
Kostya Serebryany5eab74e2015-11-09 23:17:45 +000063static void MissingWeakApiFunction(const char *FnName) {
64 Printf("ERROR: %s is not defined. Exiting.\n"
Ivan Krasindf919102016-01-22 22:28:27 +000065 "Did you use -fsanitize-coverage=... to build your code?\n",
66 FnName);
Kostya Serebryany5eab74e2015-11-09 23:17:45 +000067 exit(1);
68}
69
70#define CHECK_WEAK_API_FUNCTION(fn) \
71 do { \
72 if (!fn) \
73 MissingWeakApiFunction(#fn); \
74 } while (false)
75
Kostya Serebryany52a788e2015-03-31 20:13:20 +000076// Only one Fuzzer per process.
77static Fuzzer *F;
78
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000079struct CoverageController {
80 static void Reset() {
81 CHECK_WEAK_API_FUNCTION(__sanitizer_reset_coverage);
82 __sanitizer_reset_coverage();
83 PcMapResetCurrent();
84 }
85
86 static void ResetCounters(const Fuzzer::FuzzingOptions &Options) {
87 if (Options.UseCounters) {
88 __sanitizer_update_counter_bitset_and_clear_counters(0);
89 }
90 }
91
92 static void Prepare(const Fuzzer::FuzzingOptions &Options,
93 Fuzzer::Coverage *C) {
94 if (Options.UseCounters) {
95 size_t NumCounters = __sanitizer_get_number_of_counters();
96 C->CounterBitmap.resize(NumCounters);
97 }
98 }
99
100 // Records data to a maximum coverage tracker. Returns true if additional
101 // coverage was discovered.
102 static bool RecordMax(const Fuzzer::FuzzingOptions &Options,
103 Fuzzer::Coverage *C) {
104 bool Res = false;
105
106 uint64_t NewBlockCoverage = __sanitizer_get_total_unique_coverage();
107 if (NewBlockCoverage > C->BlockCoverage) {
108 Res = true;
109 C->BlockCoverage = NewBlockCoverage;
110 }
111
112 if (Options.UseIndirCalls &&
113 __sanitizer_get_total_unique_caller_callee_pairs) {
114 uint64_t NewCallerCalleeCoverage =
115 __sanitizer_get_total_unique_caller_callee_pairs();
116 if (NewCallerCalleeCoverage > C->CallerCalleeCoverage) {
117 Res = true;
118 C->CallerCalleeCoverage = NewCallerCalleeCoverage;
119 }
120 }
121
122 if (Options.UseCounters) {
123 uint64_t CounterDelta =
124 __sanitizer_update_counter_bitset_and_clear_counters(
125 C->CounterBitmap.data());
126 if (CounterDelta > 0) {
127 Res = true;
128 C->CounterBitmapBits += CounterDelta;
129 }
130 }
131
132 uint64_t NewPcMapBits = PcMapMergeInto(&C->PCMap);
133 if (NewPcMapBits > C->PcMapBits) {
134 Res = true;
135 C->PcMapBits = NewPcMapBits;
136 }
137
138 uintptr_t *CoverageBuf;
139 uint64_t NewPcBufferLen = __sanitizer_get_coverage_pc_buffer(&CoverageBuf);
140 if (NewPcBufferLen > C->PcBufferLen) {
141 Res = true;
142 C->PcBufferLen = NewPcBufferLen;
143 }
144
145 return Res;
146 }
147};
148
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000149Fuzzer::Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options)
150 : CB(CB), MD(MD), Options(Options) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000151 SetDeathCallback();
Kostya Serebryany22526252015-05-11 21:16:27 +0000152 InitializeTraceState();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000153 assert(!F);
154 F = this;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000155 ResetCoverage();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000156}
Aaron Ballmanef116982015-01-29 16:58:29 +0000157
158void Fuzzer::SetDeathCallback() {
Kostya Serebryany5eab74e2015-11-09 23:17:45 +0000159 CHECK_WEAK_API_FUNCTION(__sanitizer_set_death_callback);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000160 __sanitizer_set_death_callback(StaticDeathCallback);
161}
162
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000163void Fuzzer::StaticDeathCallback() {
164 assert(F);
165 F->DeathCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +0000166}
167
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000168void Fuzzer::DumpCurrentUnit(const char *Prefix) {
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000169 if (CurrentUnitSize <= kMaxUnitSizeToPrint) {
170 PrintHexArray(CurrentUnitData, CurrentUnitSize, "\n");
171 PrintASCII(CurrentUnitData, CurrentUnitSize, "\n");
Kostya Serebryanye95022a2015-10-09 04:03:14 +0000172 }
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000173 WriteUnitToFileWithPrefix(
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000174 {CurrentUnitData, CurrentUnitData + CurrentUnitSize}, Prefix);
175}
176
Benjamin Kramerd96b0c12016-03-18 14:19:19 +0000177NO_SANITIZE_MEMORY
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000178void Fuzzer::DeathCallback() {
179 if (!CurrentUnitSize) return;
180 Printf("DEATH:\n");
181 DumpCurrentUnit("crash-");
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000182 PrintFinalStats();
Aaron Ballmanef116982015-01-29 16:58:29 +0000183}
184
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000185void Fuzzer::StaticAlarmCallback() {
186 assert(F);
187 F->AlarmCallback();
188}
189
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000190void Fuzzer::StaticCrashSignalCallback() {
191 assert(F);
192 F->CrashCallback();
193}
194
195void Fuzzer::StaticInterruptCallback() {
196 assert(F);
197 F->InterruptCallback();
198}
199
200void Fuzzer::CrashCallback() {
201 Printf("==%d== ERROR: libFuzzer: deadly signal\n", GetPid());
202 if (__sanitizer_print_stack_trace)
203 __sanitizer_print_stack_trace();
204 Printf("NOTE: libFuzzer has rudimentary signal handlers.\n"
205 " Combine libFuzzer with AddressSanitizer or similar for better "
206 "crash reports.\n");
207 Printf("SUMMARY: libFuzzer: deadly signal\n");
208 DumpCurrentUnit("crash-");
209 PrintFinalStats();
210 exit(Options.ErrorExitCode);
211}
212
213void Fuzzer::InterruptCallback() {
214 Printf("==%d== libFuzzer: run interrupted; exiting\n", GetPid());
215 PrintFinalStats();
Kostya Serebryanye483ed22016-03-03 22:36:37 +0000216 _Exit(0); // Stop right now, don't perform any at-exit actions.
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000217}
218
Benjamin Kramerd96b0c12016-03-18 14:19:19 +0000219NO_SANITIZE_MEMORY
Aaron Ballmanef116982015-01-29 16:58:29 +0000220void Fuzzer::AlarmCallback() {
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000221 assert(Options.UnitTimeoutSec > 0);
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000222 if (InOOMState) {
223 Printf("==%d== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n",
224 GetPid(), GetPeakRSSMb(), Options.RssLimitMb);
225 Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n");
226 if (CurrentUnitSize && CurrentUnitData) {
227 DumpCurrentUnit("oom-");
228 if (__sanitizer_print_stack_trace)
229 __sanitizer_print_stack_trace();
230 }
231 Printf("SUMMARY: libFuzzer: out-of-memory\n");
232 PrintFinalStats();
233 _Exit(Options.ErrorExitCode); // Stop right now.
234 }
235
Kostya Serebryanycfbcf902016-02-17 19:42:34 +0000236 if (!CurrentUnitSize)
237 return; // We have not started running units yet.
Aaron Ballmanef116982015-01-29 16:58:29 +0000238 size_t Seconds =
239 duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
Ivan Krasindf919102016-01-22 22:28:27 +0000240 if (Seconds == 0)
241 return;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000242 if (Options.Verbosity >= 2)
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000243 Printf("AlarmCallback %zd\n", Seconds);
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000244 if (Seconds >= (size_t)Options.UnitTimeoutSec) {
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000245 Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds);
Kostya Serebryany316b5712015-05-26 20:57:47 +0000246 Printf(" and the timeout value is %d (use -timeout=N to change)\n",
247 Options.UnitTimeoutSec);
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000248 DumpCurrentUnit("timeout-");
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000249 Printf("==%d== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(),
250 Seconds);
251 if (__sanitizer_print_stack_trace)
252 __sanitizer_print_stack_trace();
253 Printf("SUMMARY: libFuzzer: timeout\n");
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000254 PrintFinalStats();
Kostya Serebryany31516732016-03-24 01:32:08 +0000255 _Exit(Options.TimeoutExitCode); // Stop right now.
Aaron Ballmanef116982015-01-29 16:58:29 +0000256 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000257}
258
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000259void Fuzzer::RssLimitCallback() {
260 InOOMState = true;
261 SignalToMainThread();
262 SleepSeconds(5);
263 Printf("Signal to main thread failed (non-linux?). Exiting.\n");
264 _Exit(Options.ErrorExitCode);
265 return;
Kostya Serebryany52b394e2016-05-06 21:58:35 +0000266}
267
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000268void Fuzzer::PrintStats(const char *Where, const char *End) {
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000269 size_t ExecPerSec = execPerSec();
Mike Aizatskya9c23872015-11-12 04:38:40 +0000270 if (Options.OutputCSV) {
271 static bool csvHeaderPrinted = false;
272 if (!csvHeaderPrinted) {
273 csvHeaderPrinted = true;
274 Printf("runs,block_cov,bits,cc_cov,corpus,execs_per_sec,tbms,reason\n");
275 }
Kostya Serebryany9ba19182016-04-25 19:41:45 +0000276 Printf("%zd,%zd,%zd,%zd,%zd,%zd,%s\n", TotalNumberOfRuns,
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000277 MaxCoverage.BlockCoverage, MaxCoverage.CounterBitmapBits,
278 MaxCoverage.CallerCalleeCoverage, Corpus.size(), ExecPerSec, Where);
Mike Aizatskya9c23872015-11-12 04:38:40 +0000279 }
280
281 if (!Options.Verbosity)
282 return;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000283 Printf("#%zd\t%s", TotalNumberOfRuns, Where);
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000284 if (MaxCoverage.BlockCoverage)
285 Printf(" cov: %zd", MaxCoverage.BlockCoverage);
286 if (MaxCoverage.PcMapBits)
287 Printf(" path: %zd", MaxCoverage.PcMapBits);
288 if (auto TB = MaxCoverage.CounterBitmapBits)
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000289 Printf(" bits: %zd", TB);
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000290 if (MaxCoverage.CallerCalleeCoverage)
291 Printf(" indir: %zd", MaxCoverage.CallerCalleeCoverage);
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000292 Printf(" units: %zd exec/s: %zd", Corpus.size(), ExecPerSec);
Kostya Serebryany12c78372015-08-12 01:55:37 +0000293 Printf("%s", End);
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000294}
295
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000296void Fuzzer::PrintFinalStats() {
297 if (!Options.PrintFinalStats) return;
298 size_t ExecPerSec = execPerSec();
299 Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns);
300 Printf("stat::average_exec_per_sec: %zd\n", ExecPerSec);
301 Printf("stat::new_units_added: %zd\n", NumberOfNewUnitsAdded);
302 Printf("stat::slowest_unit_time_sec: %zd\n", TimeOfLongestUnitInSeconds);
303 Printf("stat::peak_rss_mb: %zd\n", GetPeakRSSMb());
304}
305
Kostya Serebryany64d24572016-03-12 01:57:04 +0000306size_t Fuzzer::MaxUnitSizeInCorpus() const {
307 size_t Res = 0;
308 for (auto &X : Corpus)
309 Res = std::max(Res, X.size());
310 return Res;
311}
312
313void Fuzzer::SetMaxLen(size_t MaxLen) {
314 assert(Options.MaxLen == 0); // Can only reset MaxLen from 0 to non-0.
315 assert(MaxLen);
316 Options.MaxLen = MaxLen;
317 Printf("INFO: -max_len is not provided, using %zd\n", Options.MaxLen);
318}
319
320
321void Fuzzer::RereadOutputCorpus(size_t MaxSize) {
Ivan Krasindf919102016-01-22 22:28:27 +0000322 if (Options.OutputCorpus.empty())
323 return;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000324 std::vector<Unit> AdditionalCorpus;
325 ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus,
Kostya Serebryany64d24572016-03-12 01:57:04 +0000326 &EpochOfLastReadOfOutputCorpus, MaxSize);
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000327 if (Corpus.empty()) {
328 Corpus = AdditionalCorpus;
329 return;
330 }
Ivan Krasindf919102016-01-22 22:28:27 +0000331 if (!Options.Reload)
332 return;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000333 if (Options.Verbosity >= 2)
Ivan Krasindf919102016-01-22 22:28:27 +0000334 Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000335 for (auto &X : AdditionalCorpus) {
Kostya Serebryany64d24572016-03-12 01:57:04 +0000336 if (X.size() > MaxSize)
337 X.resize(MaxSize);
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000338 if (UnitHashesAddedToCorpus.insert(Hash(X)).second) {
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000339 if (RunOne(X)) {
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000340 Corpus.push_back(X);
Ivan Krasindf919102016-01-22 22:28:27 +0000341 UpdateCorpusDistribution();
Mike Aizatskya9c23872015-11-12 04:38:40 +0000342 PrintStats("RELOAD");
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000343 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000344 }
345 }
346}
347
Kostya Serebryany945761b2016-03-18 00:23:29 +0000348void Fuzzer::ShuffleCorpus(UnitVector *V) {
349 std::random_shuffle(V->begin(), V->end(), MD.GetRand());
350 if (Options.PreferSmall)
351 std::stable_sort(V->begin(), V->end(), [](const Unit &A, const Unit &B) {
352 return A.size() < B.size();
353 });
354}
355
Aaron Ballmanef116982015-01-29 16:58:29 +0000356void Fuzzer::ShuffleAndMinimize() {
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000357 PrintStats("READ ");
Aaron Ballmanef116982015-01-29 16:58:29 +0000358 std::vector<Unit> NewCorpus;
Kostya Serebryany945761b2016-03-18 00:23:29 +0000359 if (Options.ShuffleAtStartUp)
360 ShuffleCorpus(&Corpus);
361
362 for (const auto &U : Corpus) {
363 if (RunOne(U)) {
364 NewCorpus.push_back(U);
365 if (Options.Verbosity >= 2)
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000366 Printf("NEW0: %zd L %zd\n", MaxCoverage.BlockCoverage, U.size());
Aaron Ballmanef116982015-01-29 16:58:29 +0000367 }
368 }
369 Corpus = NewCorpus;
Ivan Krasindf919102016-01-22 22:28:27 +0000370 UpdateCorpusDistribution();
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000371 for (auto &X : Corpus)
372 UnitHashesAddedToCorpus.insert(Hash(X));
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000373 PrintStats("INITED");
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000374 CheckForMemoryLeaks();
Aaron Ballmanef116982015-01-29 16:58:29 +0000375}
376
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000377bool Fuzzer::UpdateMaxCoverage() {
378 uintptr_t PrevBufferLen = MaxCoverage.PcBufferLen;
379 bool Res = CoverageController::RecordMax(Options, &MaxCoverage);
380
381 if (Options.PrintNewCovPcs && PrevBufferLen != MaxCoverage.PcBufferLen) {
382 uintptr_t *CoverageBuf;
383 __sanitizer_get_coverage_pc_buffer(&CoverageBuf);
384 assert(CoverageBuf);
385 for (size_t I = PrevBufferLen; I < MaxCoverage.PcBufferLen; ++I) {
386 Printf("%p\n", CoverageBuf[I]);
387 }
388 }
389
390 return Res;
391}
392
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000393bool Fuzzer::RunOne(const uint8_t *Data, size_t Size) {
Aaron Ballmanef116982015-01-29 16:58:29 +0000394 TotalNumberOfRuns++;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000395
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000396 // TODO(aizatsky): this Reset call seems to be not needed.
397 CoverageController::ResetCounters(Options);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000398 ExecuteCallback(Data, Size);
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000399 bool Res = UpdateMaxCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000400
Kostya Serebryany16901a92015-03-30 23:04:35 +0000401 auto UnitStopTime = system_clock::now();
402 auto TimeOfUnit =
403 duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
Mike Aizatskya9c23872015-11-12 04:38:40 +0000404 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) &&
405 secondsSinceProcessStartUp() >= 2)
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000406 PrintStats("pulse ");
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000407 if (TimeOfUnit > TimeOfLongestUnitInSeconds &&
408 TimeOfUnit >= Options.ReportSlowUnits) {
Kostya Serebryany16901a92015-03-30 23:04:35 +0000409 TimeOfLongestUnitInSeconds = TimeOfUnit;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000410 Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000411 WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-");
Kostya Serebryany16901a92015-03-30 23:04:35 +0000412 }
413 return Res;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000414}
415
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000416void Fuzzer::RunOneAndUpdateCorpus(uint8_t *Data, size_t Size) {
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000417 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
418 return;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000419 if (Options.OnlyASCII)
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000420 ToASCII(Data, Size);
421 if (RunOne(Data, Size))
422 ReportNewCoverage({Data, Data + Size});
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000423}
424
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000425// Leak detection is expensive, so we first check if there were more mallocs
426// than frees (using the sanitizer malloc hooks) and only then try to call lsan.
427struct MallocFreeTracer {
428 void Start() {
429 Mallocs = 0;
430 Frees = 0;
431 }
432 // Returns true if there were more mallocs than frees.
433 bool Stop() { return Mallocs > Frees; }
434 size_t Mallocs;
435 size_t Frees;
436};
437
438static thread_local MallocFreeTracer AllocTracer;
439
Dan Liew3868e462016-05-19 22:00:33 +0000440// FIXME: The hooks only count on Linux because
441// on Mac OSX calls to malloc are intercepted before
442// thread local storage is initialised leading to
443// crashes when accessing ``AllocTracer``.
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000444extern "C" {
Dan Liew3868e462016-05-19 22:00:33 +0000445void __sanitizer_malloc_hook(void *ptr, size_t size) {
446 if (!LIBFUZZER_APPLE)
447 AllocTracer.Mallocs++;
448}
449void __sanitizer_free_hook(void *ptr) {
450 if (!LIBFUZZER_APPLE)
451 AllocTracer.Frees++;
452}
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000453} // extern "C"
454
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000455void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
Kostya Serebryanyebb932d2016-04-18 22:50:39 +0000456 UnitStartTime = system_clock::now();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000457 // We copy the contents of Unit into a separate heap buffer
458 // so that we reliably find buffer overflows in it.
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000459 std::unique_ptr<uint8_t[]> DataCopy(new uint8_t[Size]);
460 memcpy(DataCopy.get(), Data, Size);
461 AssignTaintLabels(DataCopy.get(), Size);
462 CurrentUnitData = DataCopy.get();
463 CurrentUnitSize = Size;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000464 AllocTracer.Start();
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000465 int Res = CB(DataCopy.get(), Size);
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000466 (void)Res;
467 HasMoreMallocsThanFrees = AllocTracer.Stop();
Kostya Serebryanyebb932d2016-04-18 22:50:39 +0000468 CurrentUnitSize = 0;
469 CurrentUnitData = nullptr;
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000470 assert(Res == 0);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000471}
472
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000473std::string Fuzzer::Coverage::DebugString() const {
474 std::string Result =
475 std::string("Coverage{") + "BlockCoverage=" +
476 std::to_string(BlockCoverage) + " CallerCalleeCoverage=" +
477 std::to_string(CallerCalleeCoverage) + " CounterBitmapBits=" +
478 std::to_string(CounterBitmapBits) + " PcMapBits=" +
479 std::to_string(PcMapBits) + "}";
480 return Result;
Aaron Ballmanef116982015-01-29 16:58:29 +0000481}
482
483void Fuzzer::WriteToOutputCorpus(const Unit &U) {
Ivan Krasindf919102016-01-22 22:28:27 +0000484 if (Options.OutputCorpus.empty())
485 return;
Aaron Ballmanef116982015-01-29 16:58:29 +0000486 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
487 WriteToFile(U, Path);
488 if (Options.Verbosity >= 2)
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000489 Printf("Written to %s\n", Path.c_str());
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000490 assert(!Options.OnlyASCII || IsASCII(U));
Aaron Ballmanef116982015-01-29 16:58:29 +0000491}
492
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000493void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000494 if (!Options.SaveArtifacts)
495 return;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000496 std::string Path = Options.ArtifactPrefix + Prefix + Hash(U);
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000497 if (!Options.ExactArtifactPath.empty())
Ivan Krasindf919102016-01-22 22:28:27 +0000498 Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix.
Aaron Ballmanef116982015-01-29 16:58:29 +0000499 WriteToFile(U, Path);
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000500 Printf("artifact_prefix='%s'; Test unit written to %s\n",
501 Options.ArtifactPrefix.c_str(), Path.c_str());
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000502 if (U.size() <= kMaxUnitSizeToPrint)
503 Printf("Base64: %s\n", Base64(U).c_str());
Aaron Ballmanef116982015-01-29 16:58:29 +0000504}
505
506void Fuzzer::SaveCorpus() {
Ivan Krasindf919102016-01-22 22:28:27 +0000507 if (Options.OutputCorpus.empty())
508 return;
Aaron Ballmanef116982015-01-29 16:58:29 +0000509 for (const auto &U : Corpus)
510 WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U)));
511 if (Options.Verbosity)
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000512 Printf("Written corpus of %zd files to %s\n", Corpus.size(),
513 Options.OutputCorpus.c_str());
Aaron Ballmanef116982015-01-29 16:58:29 +0000514}
515
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000516void Fuzzer::PrintStatusForNewUnit(const Unit &U) {
517 if (!Options.PrintNEW)
518 return;
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000519 PrintStats("NEW ", "");
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000520 if (Options.Verbosity) {
Kostya Serebryany14c50282015-12-19 01:09:49 +0000521 Printf(" L: %zd ", U.size());
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000522 MD.PrintMutationSequence();
Kostya Serebryany7c180ea2015-05-23 01:22:35 +0000523 Printf("\n");
Aaron Ballmanef116982015-01-29 16:58:29 +0000524 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000525}
526
527void Fuzzer::ReportNewCoverage(const Unit &U) {
528 Corpus.push_back(U);
Ivan Krasindf919102016-01-22 22:28:27 +0000529 UpdateCorpusDistribution();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000530 UnitHashesAddedToCorpus.insert(Hash(U));
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000531 MD.RecordSuccessfulMutationSequence();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000532 PrintStatusForNewUnit(U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000533 WriteToOutputCorpus(U);
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000534 NumberOfNewUnitsAdded++;
Aaron Ballmanef116982015-01-29 16:58:29 +0000535}
536
Kostya Serebryany945761b2016-03-18 00:23:29 +0000537// Finds minimal number of units in 'Extra' that add coverage to 'Initial'.
538// We do it by actually executing the units, sometimes more than once,
539// because we may be using different coverage-like signals and the only
540// common thing between them is that we can say "this unit found new stuff".
541UnitVector Fuzzer::FindExtraUnits(const UnitVector &Initial,
542 const UnitVector &Extra) {
543 UnitVector Res = Extra;
544 size_t OldSize = Res.size();
545 for (int Iter = 0; Iter < 10; Iter++) {
546 ShuffleCorpus(&Res);
547 ResetCoverage();
548
549 for (auto &U : Initial)
550 RunOne(U);
551
552 Corpus.clear();
553 for (auto &U : Res)
554 if (RunOne(U))
555 Corpus.push_back(U);
556
557 char Stat[7] = "MIN ";
558 Stat[3] = '0' + Iter;
559 PrintStats(Stat);
560
561 size_t NewSize = Corpus.size();
Kostya Serebryanya0788e72016-05-13 22:11:23 +0000562 assert(NewSize <= OldSize);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000563 Res.swap(Corpus);
564
Kostya Serebryanya0788e72016-05-13 22:11:23 +0000565 if (NewSize + 5 >= OldSize)
Kostya Serebryany945761b2016-03-18 00:23:29 +0000566 break;
567 OldSize = NewSize;
568 }
569 return Res;
570}
571
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000572void Fuzzer::Merge(const std::vector<std::string> &Corpora) {
573 if (Corpora.size() <= 1) {
574 Printf("Merge requires two or more corpus dirs\n");
575 return;
576 }
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000577 std::vector<std::string> ExtraCorpora(Corpora.begin() + 1, Corpora.end());
578
Kostya Serebryany945761b2016-03-18 00:23:29 +0000579 assert(Options.MaxLen > 0);
580 UnitVector Initial, Extra;
581 ReadDirToVectorOfUnits(Corpora[0].c_str(), &Initial, nullptr, Options.MaxLen);
582 for (auto &C : ExtraCorpora)
583 ReadDirToVectorOfUnits(C.c_str(), &Extra, nullptr, Options.MaxLen);
584
585 if (!Initial.empty()) {
586 Printf("=== Minimizing the initial corpus of %zd units\n", Initial.size());
587 Initial = FindExtraUnits({}, Initial);
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000588 }
Kostya Serebryany945761b2016-03-18 00:23:29 +0000589
590 Printf("=== Merging extra %zd units\n", Extra.size());
591 auto Res = FindExtraUnits(Initial, Extra);
592
593 for (auto &U: Res)
594 WriteToOutputCorpus(U);
595
596 Printf("=== Merge: written %zd units\n", Res.size());
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000597}
598
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000599// Tries to call lsan, and if there are leaks exits. We call this right after
600// the initial corpus was read because if there are leaky inputs in the corpus
601// further fuzzing will likely hit OOMs.
602void Fuzzer::CheckForMemoryLeaks() {
603 if (!Options.DetectLeaks) return;
604 if (!__lsan_do_recoverable_leak_check)
605 return;
606 if (__lsan_do_recoverable_leak_check()) {
607 Printf("==%d== ERROR: libFuzzer: initial corpus triggers memory leaks.\n"
608 "Exiting now. Use -detect_leaks=0 to disable leak detection here.\n"
609 "LeakSanitizer will still check for leaks at the process exit.\n",
610 GetPid());
611 PrintFinalStats();
612 _Exit(Options.ErrorExitCode);
613 }
614}
615
616// Tries detecting a memory leak on the particular input that we have just
617// executed before calling this function.
618void Fuzzer::TryDetectingAMemoryLeak(uint8_t *Data, size_t Size) {
619 if (!HasMoreMallocsThanFrees) return; // mallocs==frees, a leak is unlikely.
620 if (!Options.DetectLeaks) return;
621 if (!&__lsan_enable || !&__lsan_disable || !__lsan_do_recoverable_leak_check)
622 return; // No lsan.
623 // Run the target once again, but with lsan disabled so that if there is
624 // a real leak we do not report it twice.
625 __lsan_disable();
626 RunOneAndUpdateCorpus(Data, Size);
627 __lsan_enable();
628 if (!HasMoreMallocsThanFrees) return; // a leak is unlikely.
Kostya Serebryany7018a1a2016-04-27 19:52:34 +0000629 if (NumberOfLeakDetectionAttempts++ > 1000) {
630 Options.DetectLeaks = false;
631 Printf("INFO: libFuzzer disabled leak detection after every mutation.\n"
632 " Most likely the target function accumulates allocated\n"
633 " memory in a global state w/o actually leaking it.\n"
634 " If LeakSanitizer is enabled in this process it will still\n"
635 " run on the process shutdown.\n");
636 return;
637 }
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000638 // Now perform the actual lsan pass. This is expensive and we must ensure
639 // we don't call it too often.
640 if (__lsan_do_recoverable_leak_check()) { // Leak is found, report it.
641 CurrentUnitData = Data;
642 CurrentUnitSize = Size;
643 DumpCurrentUnit("leak-");
644 PrintFinalStats();
645 _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on.
646 }
647}
648
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000649void Fuzzer::MutateAndTestOne() {
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000650 MD.StartMutationSequence();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000651
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000652 auto &U = ChooseUnitToMutate();
653 MutateInPlaceHere.resize(Options.MaxLen);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000654 size_t Size = U.size();
Mike Aizatsky94e29662016-04-08 23:32:24 +0000655 assert(Size <= Options.MaxLen && "Oversized Unit");
656 memcpy(MutateInPlaceHere.data(), U.data(), Size);
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000657
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000658 for (int i = 0; i < Options.MutateDepth; i++) {
Kostya Serebryany22cc5e22016-02-13 02:29:38 +0000659 size_t NewSize = 0;
660 if (LLVMFuzzerCustomMutator)
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000661 NewSize = LLVMFuzzerCustomMutator(MutateInPlaceHere.data(), Size,
662 Options.MaxLen, MD.GetRand().Rand());
Kostya Serebryany22cc5e22016-02-13 02:29:38 +0000663 else
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000664 NewSize = MD.Mutate(MutateInPlaceHere.data(), Size, Options.MaxLen);
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000665 assert(NewSize > 0 && "Mutator returned empty unit");
Kostya Serebryany64d24572016-03-12 01:57:04 +0000666 assert(NewSize <= Options.MaxLen &&
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000667 "Mutator return overisized unit");
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000668 Size = NewSize;
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000669 if (i == 0)
670 StartTraceRecording();
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000671 RunOneAndUpdateCorpus(MutateInPlaceHere.data(), Size);
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000672 StopTraceRecording();
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000673 TryDetectingAMemoryLeak(MutateInPlaceHere.data(), Size);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000674 }
675}
676
Kostya Serebryanye6926212015-11-04 23:22:25 +0000677// Returns an index of random unit from the corpus to mutate.
678// Hypothesis: units added to the corpus last are more likely to be interesting.
Ivan Krasindf919102016-01-22 22:28:27 +0000679// This function gives more weight to the more recent units.
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000680size_t Fuzzer::ChooseUnitIdxToMutate() {
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000681 size_t Idx =
682 static_cast<size_t>(CorpusDistribution(MD.GetRand().Get_mt19937()));
Ivan Krasindf919102016-01-22 22:28:27 +0000683 assert(Idx < Corpus.size());
684 return Idx;
Kostya Serebryanye6926212015-11-04 23:22:25 +0000685}
686
Kostya Serebryany945761b2016-03-18 00:23:29 +0000687void Fuzzer::ResetCoverage() {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000688 CoverageController::Reset();
689 MaxCoverage.Reset();
690 CoverageController::Prepare(Options, &MaxCoverage);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000691}
692
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000693// Experimental search heuristic: drilling.
694// - Read, shuffle, execute and minimize the corpus.
695// - Choose one random unit.
696// - Reset the coverage.
697// - Start fuzzing as if the chosen unit was the only element of the corpus.
698// - When done, reset the coverage again.
699// - Merge the newly created corpus into the original one.
700void Fuzzer::Drill() {
701 // The corpus is already read, shuffled, and minimized.
702 assert(!Corpus.empty());
Ivan Krasindf919102016-01-22 22:28:27 +0000703 Options.PrintNEW = false; // Don't print NEW status lines when drilling.
Kostya Serebryany7d211662015-09-04 00:12:11 +0000704
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000705 Unit U = ChooseUnitToMutate();
706
Kostya Serebryany945761b2016-03-18 00:23:29 +0000707 ResetCoverage();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000708
709 std::vector<Unit> SavedCorpus;
710 SavedCorpus.swap(Corpus);
711 Corpus.push_back(U);
Ivan Krasindf919102016-01-22 22:28:27 +0000712 UpdateCorpusDistribution();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000713 assert(Corpus.size() == 1);
714 RunOne(U);
715 PrintStats("DRILL ");
716 std::string SavedOutputCorpusPath; // Don't write new units while drilling.
717 SavedOutputCorpusPath.swap(Options.OutputCorpus);
718 Loop();
719
Kostya Serebryany945761b2016-03-18 00:23:29 +0000720 ResetCoverage();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000721
722 PrintStats("REINIT");
723 SavedOutputCorpusPath.swap(Options.OutputCorpus);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000724 for (auto &U : SavedCorpus)
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000725 RunOne(U);
726 PrintStats("MERGE ");
727 Options.PrintNEW = true;
728 size_t NumMerged = 0;
729 for (auto &U : Corpus) {
730 if (RunOne(U)) {
731 PrintStatusForNewUnit(U);
732 NumMerged++;
733 WriteToOutputCorpus(U);
734 }
735 }
736 PrintStats("MERGED");
737 if (NumMerged && Options.Verbosity)
738 Printf("Drilling discovered %zd new units\n", NumMerged);
739}
740
741void Fuzzer::Loop() {
Kostya Serebryany8617aaa2015-12-05 02:09:22 +0000742 system_clock::time_point LastCorpusReload = system_clock::now();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000743 if (Options.DoCrossOver)
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000744 MD.SetCorpus(&Corpus);
Kostya Serebryany468ed782015-09-08 17:30:35 +0000745 while (true) {
Kostya Serebryany8617aaa2015-12-05 02:09:22 +0000746 auto Now = system_clock::now();
747 if (duration_cast<seconds>(Now - LastCorpusReload).count()) {
Kostya Serebryany64d24572016-03-12 01:57:04 +0000748 RereadOutputCorpus(Options.MaxLen);
Kostya Serebryany8617aaa2015-12-05 02:09:22 +0000749 LastCorpusReload = Now;
750 }
Kostya Serebryanye6926212015-11-04 23:22:25 +0000751 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
Mike Aizatskya9c23872015-11-12 04:38:40 +0000752 break;
Kostya Serebryanye6926212015-11-04 23:22:25 +0000753 if (Options.MaxTotalTimeSec > 0 &&
754 secondsSinceProcessStartUp() >
Ivan Krasindf919102016-01-22 22:28:27 +0000755 static_cast<size_t>(Options.MaxTotalTimeSec))
Mike Aizatskya9c23872015-11-12 04:38:40 +0000756 break;
Kostya Serebryanye6926212015-11-04 23:22:25 +0000757 // Perform several mutations and runs.
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000758 MutateAndTestOne();
Aaron Ballmanef116982015-01-29 16:58:29 +0000759 }
Mike Aizatskya9c23872015-11-12 04:38:40 +0000760
761 PrintStats("DONE ", "\n");
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000762 MD.PrintRecommendedDictionary();
Aaron Ballmanef116982015-01-29 16:58:29 +0000763}
764
Ivan Krasindf919102016-01-22 22:28:27 +0000765void Fuzzer::UpdateCorpusDistribution() {
766 size_t N = Corpus.size();
767 std::vector<double> Intervals(N + 1);
768 std::vector<double> Weights(N);
769 std::iota(Intervals.begin(), Intervals.end(), 0);
770 std::iota(Weights.begin(), Weights.end(), 1);
771 CorpusDistribution = std::piecewise_constant_distribution<double>(
772 Intervals.begin(), Intervals.end(), Weights.begin());
773}
774
775} // namespace fuzzer
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +0000776
777extern "C" {
778
779size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
780 assert(fuzzer::F);
781 return fuzzer::F->GetMD().Mutate(Data, Size, MaxSize);
782}
783} // extern "C"