blob: db1e505ac3842aa659a37c2db88802d8699eeefb [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===//
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// Define the main class fuzzer::Fuzzer and most functions.
10//===----------------------------------------------------------------------===//
Yaron Keren347663b2015-08-10 16:37:40 +000011
12#ifndef LLVM_FUZZER_INTERNAL_H
13#define LLVM_FUZZER_INTERNAL_H
14
Kostya Serebryany292cf032016-02-13 03:37:24 +000015#include <algorithm>
Kostya Serebryany0edb5632016-05-27 00:54:15 +000016#include <atomic>
Aaron Ballmanef116982015-01-29 16:58:29 +000017#include <cassert>
18#include <chrono>
Ivan Krasindf919102016-01-22 22:28:27 +000019#include <climits>
Aaron Ballmanef116982015-01-29 16:58:29 +000020#include <cstddef>
21#include <cstdlib>
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +000022#include <memory>
Ivan Krasindf919102016-01-22 22:28:27 +000023#include <random>
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000024#include <string.h>
Ivan Krasindf919102016-01-22 22:28:27 +000025#include <string>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000026#include <unordered_set>
Ivan Krasindf919102016-01-22 22:28:27 +000027#include <vector>
Aaron Ballmanef116982015-01-29 16:58:29 +000028
Dan Liewd3c33112016-06-02 05:48:02 +000029#include "FuzzerExtFunctions.h"
Kostya Serebryany016852c2015-02-19 18:45:37 +000030#include "FuzzerInterface.h"
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000031#include "FuzzerValueBitMap.h"
Kostya Serebryany016852c2015-02-19 18:45:37 +000032
Dan Liew3868e462016-05-19 22:00:33 +000033// Platform detection.
34#ifdef __linux__
35#define LIBFUZZER_LINUX 1
36#define LIBFUZZER_APPLE 0
37#elif __APPLE__
38#define LIBFUZZER_LINUX 0
39#define LIBFUZZER_APPLE 1
40#else
41#error "Support for your platform has not been implemented"
42#endif
43
Aaron Ballmanef116982015-01-29 16:58:29 +000044namespace fuzzer {
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +000045
46typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
Dan Liewd3c33112016-06-02 05:48:02 +000047int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +000048
Aaron Ballmanef116982015-01-29 16:58:29 +000049using namespace std::chrono;
Kostya Serebryanyaca76962016-01-16 01:23:12 +000050typedef std::vector<uint8_t> Unit;
Kostya Serebryany945761b2016-03-18 00:23:29 +000051typedef std::vector<Unit> UnitVector;
Aaron Ballmanef116982015-01-29 16:58:29 +000052
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000053// A simple POD sized array of bytes.
Ivan Krasindf919102016-01-22 22:28:27 +000054template <size_t kMaxSize> class FixedWord {
55public:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000056 FixedWord() {}
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000057 FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
58
59 void Set(const uint8_t *B, uint8_t S) {
60 assert(S <= kMaxSize);
61 memcpy(Data, B, S);
62 Size = S;
63 }
64
Ivan Krasindf919102016-01-22 22:28:27 +000065 bool operator==(const FixedWord<kMaxSize> &w) const {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000066 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
67 }
68
Ivan Krasindf919102016-01-22 22:28:27 +000069 bool operator<(const FixedWord<kMaxSize> &w) const {
70 if (Size != w.Size)
71 return Size < w.Size;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000072 return memcmp(Data, w.Data, Size) < 0;
73 }
74
75 static size_t GetMaxSize() { return kMaxSize; }
76 const uint8_t *data() const { return Data; }
77 uint8_t size() const { return Size; }
78
Ivan Krasindf919102016-01-22 22:28:27 +000079private:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000080 uint8_t Size = 0;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000081 uint8_t Data[kMaxSize];
82};
83
Ivan Krasindf919102016-01-22 22:28:27 +000084typedef FixedWord<27> Word; // 28 bytes.
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000085
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000086bool IsFile(const std::string &Path);
Kostya Serebryany52a788e2015-03-31 20:13:20 +000087std::string FileToString(const std::string &Path);
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000088Unit FileToVector(const std::string &Path, size_t MaxSize = 0);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000089void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000090 long *Epoch, size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +000091void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000092void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000093// Returns "Dir/FileName" or equivalent for the current OS.
94std::string DirPlusFile(const std::string &DirPath,
95 const std::string &FileName);
96
Kostya Serebryany49e40902016-03-18 20:58:29 +000097void DupAndCloseStderr();
98void CloseStdout();
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000099void Printf(const char *Fmt, ...);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000100void PrintHexArray(const Unit &U, const char *PrintAfter = "");
101void PrintHexArray(const uint8_t *Data, size_t Size,
102 const char *PrintAfter = "");
Kostya Serebryany41740052016-01-12 02:36:59 +0000103void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000104void PrintASCII(const Unit &U, const char *PrintAfter = "");
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000105void PrintASCII(const Word &W, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000106std::string Hash(const Unit &U);
107void SetTimer(int Seconds);
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000108void SetSigSegvHandler();
109void SetSigBusHandler();
110void SetSigAbrtHandler();
111void SetSigIllHandler();
112void SetSigFpeHandler();
113void SetSigIntHandler();
Kostya Serebryanyf389ae12016-03-24 21:03:58 +0000114void SetSigTermHandler();
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000115std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000116int ExecuteCommand(const std::string &Command);
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000117size_t GetPeakRSSMb();
Aaron Ballmanef116982015-01-29 16:58:29 +0000118
Kostya Serebryany96eab652015-05-14 22:41:49 +0000119// Private copy of SHA1 implementation.
120static const int kSHA1NumBytes = 20;
121// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
122void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
123
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000124// Changes U to contain only ASCII (isprint+isspace) characters.
125// Returns true iff U has been changed.
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000126bool ToASCII(uint8_t *Data, size_t Size);
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000127bool IsASCII(const Unit &U);
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000128bool IsASCII(const uint8_t *Data, size_t Size);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000129
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000130int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000131int GetPid();
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000132void SleepSeconds(int Seconds);
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000133
Kostya Serebryanyc98ef712016-08-16 17:37:13 +0000134// See FuzzerTracePC.cpp
135size_t PCMapMergeFromCurrent(ValueBitMap &M);
136
Kostya Serebryanya3992212016-02-13 03:00:53 +0000137class Random {
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000138 public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000139 Random(unsigned int seed) : R(seed) {}
140 size_t Rand() { return R(); }
141 size_t RandBool() { return Rand() % 2; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000142 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000143 std::mt19937 &Get_mt19937() { return R; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000144 private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000145 std::mt19937 R;
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000146};
147
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000148// Dictionary.
149
150// Parses one dictionary entry.
151// If successfull, write the enty to Unit and returns true,
152// otherwise returns false.
153bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
154// Parses the dictionary file, fills Units, returns true iff all lines
155// were parsed succesfully.
156bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
157
Kostya Serebryany292cf032016-02-13 03:37:24 +0000158class DictionaryEntry {
159 public:
160 DictionaryEntry() {}
161 DictionaryEntry(Word W) : W(W) {}
162 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
163 const Word &GetW() const { return W; }
164
165 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
166 size_t GetPositionHint() const {
167 assert(HasPositionHint());
168 return PositionHint;
169 }
170 void IncUseCount() { UseCount++; }
171 void IncSuccessCount() { SuccessCount++; }
172 size_t GetUseCount() const { return UseCount; }
173 size_t GetSuccessCount() const {return SuccessCount; }
174
175private:
176 Word W;
177 size_t PositionHint = std::numeric_limits<size_t>::max();
178 size_t UseCount = 0;
179 size_t SuccessCount = 0;
180};
181
182class Dictionary {
183 public:
184 static const size_t kMaxDictSize = 1 << 14;
185
186 bool ContainsWord(const Word &W) const {
187 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
188 return DE.GetW() == W;
189 });
190 }
191 const DictionaryEntry *begin() const { return &DE[0]; }
192 const DictionaryEntry *end() const { return begin() + Size; }
193 DictionaryEntry & operator[] (size_t Idx) {
194 assert(Idx < Size);
195 return DE[Idx];
196 }
197 void push_back(DictionaryEntry DE) {
198 if (Size < kMaxDictSize)
199 this->DE[Size++] = DE;
200 }
201 void clear() { Size = 0; }
202 bool empty() const { return Size == 0; }
203 size_t size() const { return Size; }
204
205private:
206 DictionaryEntry DE[kMaxDictSize];
207 size_t Size = 0;
208};
209
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000210struct FuzzingOptions {
211 int Verbosity = 1;
212 size_t MaxLen = 0;
213 int UnitTimeoutSec = 300;
214 int TimeoutExitCode = 77;
215 int ErrorExitCode = 77;
216 int MaxTotalTimeSec = 0;
217 int RssLimitMb = 0;
218 bool DoCrossOver = true;
219 int MutateDepth = 5;
220 bool UseCounters = false;
221 bool UseIndirCalls = true;
222 bool UseTraces = false;
223 bool UseMemcmp = true;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000224 bool UseMemmem = true;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000225 bool UseFullCoverageSet = false;
226 bool Reload = true;
227 bool ShuffleAtStartUp = true;
228 bool PreferSmall = true;
229 size_t MaxNumberOfRuns = ULONG_MAX;
230 int ReportSlowUnits = 10;
231 bool OnlyASCII = false;
232 std::string OutputCorpus;
233 std::string ArtifactPrefix = "./";
234 std::string ExactArtifactPath;
235 bool SaveArtifacts = true;
236 bool PrintNEW = true; // Print a status line when new units are found;
237 bool OutputCSV = false;
238 bool PrintNewCovPcs = false;
239 bool PrintFinalStats = false;
240 bool DetectLeaks = true;
241 bool TruncateUnits = false;
242 bool PruneCorpus = true;
243};
244
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000245class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000246public:
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000247 MutationDispatcher(Random &Rand, const FuzzingOptions &Options);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000248 ~MutationDispatcher() {}
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000249 /// Indicate that we are about to start a new sequence of mutations.
250 void StartMutationSequence();
251 /// Print the current sequence of mutations.
252 void PrintMutationSequence();
253 /// Indicate that the current sequence of mutations was successfull.
254 void RecordSuccessfulMutationSequence();
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000255 /// Mutates data by invoking user-provided mutator.
256 size_t Mutate_Custom(uint8_t *Data, size_t Size, size_t MaxSize);
Mike Aizatsky41d66832016-06-07 20:22:15 +0000257 /// Mutates data by invoking user-provided crossover.
258 size_t Mutate_CustomCrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000259 /// Mutates data by shuffling bytes.
260 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000261 /// Mutates data by erasing bytes.
262 size_t Mutate_EraseBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000263 /// Mutates data by inserting a byte.
264 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000265 /// Mutates data by inserting several repeated bytes.
266 size_t Mutate_InsertRepeatedBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000267 /// Mutates data by chanding one byte.
268 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
269 /// Mutates data by chanding one bit.
270 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
271
272 /// Mutates data by adding a word from the manual dictionary.
273 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
274 size_t MaxSize);
275
276 /// Mutates data by adding a word from the temporary automatic dictionary.
277 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
278 size_t MaxSize);
279
280 /// Mutates data by adding a word from the persistent automatic dictionary.
281 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
282 size_t MaxSize);
283
284 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
285 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
286
287 /// CrossOver Data with some other element of the corpus.
288 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
289
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000290 /// Applies one of the configured mutations.
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000291 /// Returns the new size of data which could be up to MaxSize.
292 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000293 /// Applies one of the default mutations. Provided as a service
294 /// to mutation authors.
295 size_t DefaultMutate(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000296
297 /// Creates a cross-over of two pieces of Data, returns its size.
298 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
299 size_t Size2, uint8_t *Out, size_t MaxOutSize);
300
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000301 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000302
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000303 void AddWordToAutoDictionary(DictionaryEntry DE);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000304 void ClearAutoDictionary();
305 void PrintRecommendedDictionary();
306
Kostya Serebryany292cf032016-02-13 03:37:24 +0000307 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000308
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000309 Random &GetRand() { return Rand; }
310
Ivan Krasindf919102016-01-22 22:28:27 +0000311private:
Kostya Serebryany292cf032016-02-13 03:37:24 +0000312
313 struct Mutator {
314 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
315 const char *Name;
316 };
317
Kostya Serebryany292cf032016-02-13 03:37:24 +0000318 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
319 size_t MaxSize);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000320 size_t MutateImpl(uint8_t *Data, size_t Size, size_t MaxSize,
321 const std::vector<Mutator> &Mutators);
322
Kostya Serebryanya3992212016-02-13 03:00:53 +0000323 Random &Rand;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000324 const FuzzingOptions Options;
325
Kostya Serebryany292cf032016-02-13 03:37:24 +0000326 // Dictionary provided by the user via -dict=DICT_FILE.
327 Dictionary ManualDictionary;
328 // Temporary dictionary modified by the fuzzer itself,
329 // recreated periodically.
330 Dictionary TempAutoDictionary;
331 // Persistent dictionary modified by the fuzzer, consists of
332 // entries that led to successfull discoveries in the past mutations.
333 Dictionary PersistentAutoDictionary;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000334 std::vector<Mutator> CurrentMutatorSequence;
335 std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
336 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000337 std::vector<uint8_t> MutateInPlaceHere;
Kostya Serebryany23194962016-02-13 03:46:26 +0000338
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000339 std::vector<Mutator> Mutators;
340 std::vector<Mutator> DefaultMutators;
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000341};
342
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000343class CoverageController;
344
Aaron Ballmanef116982015-01-29 16:58:29 +0000345class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000346public:
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000347
348 // Aggregates all available coverage measurements.
349 struct Coverage {
350 Coverage() { Reset(); }
351
352 void Reset() {
353 BlockCoverage = 0;
354 CallerCalleeCoverage = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000355 CounterBitmapBits = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000356 CounterBitmap.clear();
357 PCMap.Reset();
Kostya Serebryanyc98ef712016-08-16 17:37:13 +0000358 PCMapBits = 0;
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000359 PcBufferPos = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000360 }
361
362 std::string DebugString() const;
363
364 size_t BlockCoverage;
365 size_t CallerCalleeCoverage;
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000366 size_t PcBufferPos;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000367 // Precalculated number of bits in CounterBitmap.
368 size_t CounterBitmapBits;
369 std::vector<uint8_t> CounterBitmap;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +0000370 ValueBitMap PCMap;
371 size_t PCMapBits;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000372 };
373
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000374 Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000375 ~Fuzzer();
Ivan Krasindf919102016-01-22 22:28:27 +0000376 void AddToCorpus(const Unit &U) {
377 Corpus.push_back(U);
378 UpdateCorpusDistribution();
379 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000380 size_t ChooseUnitIdxToMutate();
381 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000382 void TruncateUnits(std::vector<Unit> *NewCorpus);
Kostya Serebryany468ed782015-09-08 17:30:35 +0000383 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000384 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000385 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000386 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000387 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000388 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000389 size_t MaxUnitSizeInCorpus() const;
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000390 void ReadDir(const std::string &Path, long *Epoch, size_t MaxSize) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000391 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000392 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch, MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000393 }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000394 void RereadOutputCorpus(size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000395 // Save the current corpus to OutputCorpus.
396 void SaveCorpus();
397
Kostya Serebryany92e04762015-02-04 23:42:42 +0000398 size_t secondsSinceProcessStartUp() {
399 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
400 .count();
401 }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000402 size_t execPerSec() {
403 size_t Seconds = secondsSinceProcessStartUp();
404 return Seconds ? TotalNumberOfRuns / Seconds : 0;
405 }
Kostya Serebryany92e04762015-02-04 23:42:42 +0000406
407 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
408
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000409 static void StaticAlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000410 static void StaticCrashSignalCallback();
411 static void StaticInterruptCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000412
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000413 void ExecuteCallback(const uint8_t *Data, size_t Size);
Kostya Serebryanybaf7fd02016-05-04 20:44:50 +0000414 bool RunOne(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000415
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000416 // Merge Corpora[1:] into Corpora[0].
417 void Merge(const std::vector<std::string> &Corpora);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000418 // Returns a subset of 'Extra' that adds coverage to 'Initial'.
419 UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
Kostya Serebryany1deb0492016-02-13 06:24:18 +0000420 MutationDispatcher &GetMD() { return MD; }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000421 void PrintFinalStats();
Kostya Serebryany64d24572016-03-12 01:57:04 +0000422 void SetMaxLen(size_t MaxLen);
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000423 void RssLimitCallback();
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000424
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000425 // Public for tests.
426 void ResetCoverage();
427
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000428 bool InFuzzingThread() const { return IsMyThread; }
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000429 size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000430
Ivan Krasindf919102016-01-22 22:28:27 +0000431private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000432 void AlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000433 void CrashCallback();
434 void InterruptCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000435 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000436 void ReportNewCoverage(const Unit &U);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000437 bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000438 void RunOneAndUpdateCorpus(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000439 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000440 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000441 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000442 void PrintStatusForNewUnit(const Unit &U);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000443 void ShuffleCorpus(UnitVector *V);
Kostya Serebryany4b923262016-05-26 20:25:49 +0000444 void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
445 bool DuringInitialCorpusExecution);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000446
Ivan Krasindf919102016-01-22 22:28:27 +0000447 // Updates the probability distribution for the units in the corpus.
448 // Must be called whenever the corpus or unit weights are changed.
449 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000450
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000451 bool UpdateMaxCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000452
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000453 // Trace-based fuzzing: we run a unit with some kind of tracing
454 // enabled and record potentially useful mutations. Then
455 // We apply these mutations one by one to the unit and run it again.
456
457 // Start tracing; forget all previously proposed mutations.
458 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000459 // Stop tracing.
460 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000461
Aaron Ballmanef116982015-01-29 16:58:29 +0000462 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000463 static void StaticDeathCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000464 void DumpCurrentUnit(const char *Prefix);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000465 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000466
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000467 void LazyAllocateCurrentUnitData();
468 uint8_t *CurrentUnitData = nullptr;
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000469 std::atomic<size_t> CurrentUnitSize;
Aaron Ballmanef116982015-01-29 16:58:29 +0000470
471 size_t TotalNumberOfRuns = 0;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000472 size_t NumberOfNewUnitsAdded = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000473
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000474 bool HasMoreMallocsThanFrees = false;
Kostya Serebryany7018a1a2016-04-27 19:52:34 +0000475 size_t NumberOfLeakDetectionAttempts = 0;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000476
Aaron Ballmanef116982015-01-29 16:58:29 +0000477 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000478 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000479
Ivan Krasindf919102016-01-22 22:28:27 +0000480 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000481 UserCallback CB;
482 MutationDispatcher &MD;
Aaron Ballmanef116982015-01-29 16:58:29 +0000483 FuzzingOptions Options;
484 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000485 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000486 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000487 long EpochOfLastReadOfOutputCorpus = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000488
489 // Maximum recorded coverage.
490 Coverage MaxCoverage;
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000491 std::unique_ptr<CoverageController> CController;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000492
493 // Need to know our own thread.
494 static thread_local bool IsMyThread;
Aaron Ballmanef116982015-01-29 16:58:29 +0000495};
496
Dan Liew1873a492016-06-07 23:32:50 +0000497// Global interface to functions that may or may not be available.
498extern ExternalFunctions *EF;
499
Ivan Krasindf919102016-01-22 22:28:27 +0000500}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000501
502#endif // LLVM_FUZZER_INTERNAL_H