blob: f43f99f838a56a1be9cfde9ae2731ad7316c603e [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>
Aaron Ballmanef116982015-01-29 16:58:29 +000016#include <cassert>
17#include <chrono>
Ivan Krasindf919102016-01-22 22:28:27 +000018#include <climits>
Aaron Ballmanef116982015-01-29 16:58:29 +000019#include <cstddef>
20#include <cstdlib>
Ivan Krasindf919102016-01-22 22:28:27 +000021#include <random>
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000022#include <string.h>
Ivan Krasindf919102016-01-22 22:28:27 +000023#include <string>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000024#include <unordered_set>
Ivan Krasindf919102016-01-22 22:28:27 +000025#include <vector>
Aaron Ballmanef116982015-01-29 16:58:29 +000026
Kostya Serebryany016852c2015-02-19 18:45:37 +000027#include "FuzzerInterface.h"
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000028#include "FuzzerTracePC.h"
Kostya Serebryany016852c2015-02-19 18:45:37 +000029
Aaron Ballmanef116982015-01-29 16:58:29 +000030namespace fuzzer {
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +000031
32typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
33int FuzzerDriver(int argc, char **argv, UserCallback Callback);
34
Aaron Ballmanef116982015-01-29 16:58:29 +000035using namespace std::chrono;
Kostya Serebryanyaca76962016-01-16 01:23:12 +000036typedef std::vector<uint8_t> Unit;
Kostya Serebryany945761b2016-03-18 00:23:29 +000037typedef std::vector<Unit> UnitVector;
Aaron Ballmanef116982015-01-29 16:58:29 +000038
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000039// A simple POD sized array of bytes.
Ivan Krasindf919102016-01-22 22:28:27 +000040template <size_t kMaxSize> class FixedWord {
41public:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000042 FixedWord() {}
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000043 FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
44
45 void Set(const uint8_t *B, uint8_t S) {
46 assert(S <= kMaxSize);
47 memcpy(Data, B, S);
48 Size = S;
49 }
50
Ivan Krasindf919102016-01-22 22:28:27 +000051 bool operator==(const FixedWord<kMaxSize> &w) const {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000052 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
53 }
54
Ivan Krasindf919102016-01-22 22:28:27 +000055 bool operator<(const FixedWord<kMaxSize> &w) const {
56 if (Size != w.Size)
57 return Size < w.Size;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000058 return memcmp(Data, w.Data, Size) < 0;
59 }
60
61 static size_t GetMaxSize() { return kMaxSize; }
62 const uint8_t *data() const { return Data; }
63 uint8_t size() const { return Size; }
64
Ivan Krasindf919102016-01-22 22:28:27 +000065private:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000066 uint8_t Size = 0;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000067 uint8_t Data[kMaxSize];
68};
69
Ivan Krasindf919102016-01-22 22:28:27 +000070typedef FixedWord<27> Word; // 28 bytes.
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000071
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000072bool IsFile(const std::string &Path);
Kostya Serebryany52a788e2015-03-31 20:13:20 +000073std::string FileToString(const std::string &Path);
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000074Unit FileToVector(const std::string &Path, size_t MaxSize = 0);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000075void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000076 long *Epoch, size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +000077void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000078void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000079// Returns "Dir/FileName" or equivalent for the current OS.
80std::string DirPlusFile(const std::string &DirPath,
81 const std::string &FileName);
82
Kostya Serebryany49e40902016-03-18 20:58:29 +000083void DupAndCloseStderr();
84void CloseStdout();
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000085void Printf(const char *Fmt, ...);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000086void PrintHexArray(const Unit &U, const char *PrintAfter = "");
87void PrintHexArray(const uint8_t *Data, size_t Size,
88 const char *PrintAfter = "");
Kostya Serebryany41740052016-01-12 02:36:59 +000089void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000090void PrintASCII(const Unit &U, const char *PrintAfter = "");
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000091void PrintASCII(const Word &W, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000092std::string Hash(const Unit &U);
93void SetTimer(int Seconds);
Kostya Serebryany228d5b12016-03-01 22:19:21 +000094void SetSigSegvHandler();
95void SetSigBusHandler();
96void SetSigAbrtHandler();
97void SetSigIllHandler();
98void SetSigFpeHandler();
99void SetSigIntHandler();
Kostya Serebryanyf389ae12016-03-24 21:03:58 +0000100void SetSigTermHandler();
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000101std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000102int ExecuteCommand(const std::string &Command);
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000103size_t GetPeakRSSMb();
Aaron Ballmanef116982015-01-29 16:58:29 +0000104
Kostya Serebryany96eab652015-05-14 22:41:49 +0000105// Private copy of SHA1 implementation.
106static const int kSHA1NumBytes = 20;
107// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
108void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
109
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000110// Changes U to contain only ASCII (isprint+isspace) characters.
111// Returns true iff U has been changed.
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000112bool ToASCII(uint8_t *Data, size_t Size);
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000113bool IsASCII(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000114
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000115int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000116int GetPid();
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000117int SignalToMainThread();
118void SleepSeconds(int Seconds);
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000119
Kostya Serebryanya3992212016-02-13 03:00:53 +0000120class Random {
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000121 public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000122 Random(unsigned int seed) : R(seed) {}
123 size_t Rand() { return R(); }
124 size_t RandBool() { return Rand() % 2; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000125 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000126 std::mt19937 &Get_mt19937() { return R; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000127 private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000128 std::mt19937 R;
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000129};
130
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000131// Dictionary.
132
133// Parses one dictionary entry.
134// If successfull, write the enty to Unit and returns true,
135// otherwise returns false.
136bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
137// Parses the dictionary file, fills Units, returns true iff all lines
138// were parsed succesfully.
139bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
140
Kostya Serebryany292cf032016-02-13 03:37:24 +0000141class DictionaryEntry {
142 public:
143 DictionaryEntry() {}
144 DictionaryEntry(Word W) : W(W) {}
145 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
146 const Word &GetW() const { return W; }
147
148 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
149 size_t GetPositionHint() const {
150 assert(HasPositionHint());
151 return PositionHint;
152 }
153 void IncUseCount() { UseCount++; }
154 void IncSuccessCount() { SuccessCount++; }
155 size_t GetUseCount() const { return UseCount; }
156 size_t GetSuccessCount() const {return SuccessCount; }
157
158private:
159 Word W;
160 size_t PositionHint = std::numeric_limits<size_t>::max();
161 size_t UseCount = 0;
162 size_t SuccessCount = 0;
163};
164
165class Dictionary {
166 public:
167 static const size_t kMaxDictSize = 1 << 14;
168
169 bool ContainsWord(const Word &W) const {
170 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
171 return DE.GetW() == W;
172 });
173 }
174 const DictionaryEntry *begin() const { return &DE[0]; }
175 const DictionaryEntry *end() const { return begin() + Size; }
176 DictionaryEntry & operator[] (size_t Idx) {
177 assert(Idx < Size);
178 return DE[Idx];
179 }
180 void push_back(DictionaryEntry DE) {
181 if (Size < kMaxDictSize)
182 this->DE[Size++] = DE;
183 }
184 void clear() { Size = 0; }
185 bool empty() const { return Size == 0; }
186 size_t size() const { return Size; }
187
188private:
189 DictionaryEntry DE[kMaxDictSize];
190 size_t Size = 0;
191};
192
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000193class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000194public:
Kostya Serebryany23194962016-02-13 03:46:26 +0000195 MutationDispatcher(Random &Rand) : Rand(Rand) {}
Kostya Serebryany292cf032016-02-13 03:37:24 +0000196 ~MutationDispatcher() {}
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000197 /// Indicate that we are about to start a new sequence of mutations.
198 void StartMutationSequence();
199 /// Print the current sequence of mutations.
200 void PrintMutationSequence();
201 /// Indicate that the current sequence of mutations was successfull.
202 void RecordSuccessfulMutationSequence();
203 /// Mutates data by shuffling bytes.
204 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
205 /// Mutates data by erasing a byte.
206 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
207 /// Mutates data by inserting a byte.
208 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
209 /// Mutates data by chanding one byte.
210 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
211 /// Mutates data by chanding one bit.
212 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
213
214 /// Mutates data by adding a word from the manual dictionary.
215 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
216 size_t MaxSize);
217
218 /// Mutates data by adding a word from the temporary automatic dictionary.
219 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
220 size_t MaxSize);
221
222 /// Mutates data by adding a word from the persistent automatic dictionary.
223 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
224 size_t MaxSize);
225
226 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
227 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
228
229 /// CrossOver Data with some other element of the corpus.
230 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
231
232 /// Applies one of the above mutations.
233 /// Returns the new size of data which could be up to MaxSize.
234 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
235
236 /// Creates a cross-over of two pieces of Data, returns its size.
237 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
238 size_t Size2, uint8_t *Out, size_t MaxOutSize);
239
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000240 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000241
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000242 void AddWordToAutoDictionary(const Word &W, size_t PositionHint);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000243 void ClearAutoDictionary();
244 void PrintRecommendedDictionary();
245
Kostya Serebryany292cf032016-02-13 03:37:24 +0000246 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000247
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000248 Random &GetRand() { return Rand; }
249
Ivan Krasindf919102016-01-22 22:28:27 +0000250private:
Kostya Serebryany292cf032016-02-13 03:37:24 +0000251
252 struct Mutator {
253 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
254 const char *Name;
255 };
256
Kostya Serebryany292cf032016-02-13 03:37:24 +0000257 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
258 size_t MaxSize);
259
Kostya Serebryanya3992212016-02-13 03:00:53 +0000260 Random &Rand;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000261 // Dictionary provided by the user via -dict=DICT_FILE.
262 Dictionary ManualDictionary;
263 // Temporary dictionary modified by the fuzzer itself,
264 // recreated periodically.
265 Dictionary TempAutoDictionary;
266 // Persistent dictionary modified by the fuzzer, consists of
267 // entries that led to successfull discoveries in the past mutations.
268 Dictionary PersistentAutoDictionary;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000269 std::vector<Mutator> CurrentMutatorSequence;
270 std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
271 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000272 std::vector<uint8_t> MutateInPlaceHere;
Kostya Serebryany23194962016-02-13 03:46:26 +0000273
274 static Mutator Mutators[];
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000275};
276
Aaron Ballmanef116982015-01-29 16:58:29 +0000277class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000278public:
Aaron Ballmanef116982015-01-29 16:58:29 +0000279 struct FuzzingOptions {
280 int Verbosity = 1;
Kostya Serebryany64d24572016-03-12 01:57:04 +0000281 size_t MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000282 int UnitTimeoutSec = 300;
Kostya Serebryany54a63632016-01-29 23:30:07 +0000283 int TimeoutExitCode = 77;
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000284 int ErrorExitCode = 77;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000285 int MaxTotalTimeSec = 0;
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000286 int RssLimitMb = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000287 bool DoCrossOver = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000288 int MutateDepth = 5;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000289 bool UseCounters = false;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000290 bool UseIndirCalls = true;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000291 bool UseTraces = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000292 bool UseMemcmp = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000293 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000294 bool Reload = true;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000295 bool ShuffleAtStartUp = true;
Kostya Serebryany945761b2016-03-18 00:23:29 +0000296 bool PreferSmall = true;
Kostya Serebryany33f86692015-02-04 22:20:09 +0000297 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000298 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000299 bool OnlyASCII = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000300 std::string OutputCorpus;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000301 std::string ArtifactPrefix = "./";
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000302 std::string ExactArtifactPath;
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000303 bool SaveArtifacts = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000304 bool PrintNEW = true; // Print a status line when new units are found;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000305 bool OutputCSV = false;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000306 bool PrintNewCovPcs = false;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000307 bool PrintFinalStats = false;
Kostya Serebryany2fe93042016-04-29 18:49:55 +0000308 bool DetectLeaks = true;
Aaron Ballmanef116982015-01-29 16:58:29 +0000309 };
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000310
311 // Aggregates all available coverage measurements.
312 struct Coverage {
313 Coverage() { Reset(); }
314
315 void Reset() {
316 BlockCoverage = 0;
317 CallerCalleeCoverage = 0;
318 PcMapBits = 0;
319 CounterBitmapBits = 0;
320 PcBufferLen = 0;
321 CounterBitmap.clear();
322 PCMap.Reset();
323 }
324
325 std::string DebugString() const;
326
327 size_t BlockCoverage;
328 size_t CallerCalleeCoverage;
329
330 size_t PcBufferLen;
331 // Precalculated number of bits in CounterBitmap.
332 size_t CounterBitmapBits;
333 std::vector<uint8_t> CounterBitmap;
334 // Precalculated number of bits in PCMap.
335 size_t PcMapBits;
336 PcCoverageMap PCMap;
337 };
338
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000339 Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
Ivan Krasindf919102016-01-22 22:28:27 +0000340 void AddToCorpus(const Unit &U) {
341 Corpus.push_back(U);
342 UpdateCorpusDistribution();
343 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000344 size_t ChooseUnitIdxToMutate();
345 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Kostya Serebryany468ed782015-09-08 17:30:35 +0000346 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000347 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000348 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000349 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000350 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000351 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000352 size_t MaxUnitSizeInCorpus() const;
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000353 void ReadDir(const std::string &Path, long *Epoch, size_t MaxSize) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000354 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000355 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch, MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000356 }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000357 void RereadOutputCorpus(size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000358 // Save the current corpus to OutputCorpus.
359 void SaveCorpus();
360
Kostya Serebryany92e04762015-02-04 23:42:42 +0000361 size_t secondsSinceProcessStartUp() {
362 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
363 .count();
364 }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000365 size_t execPerSec() {
366 size_t Seconds = secondsSinceProcessStartUp();
367 return Seconds ? TotalNumberOfRuns / Seconds : 0;
368 }
Kostya Serebryany92e04762015-02-04 23:42:42 +0000369
370 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
371
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000372 static void StaticAlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000373 static void StaticCrashSignalCallback();
374 static void StaticInterruptCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000375
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000376 void ExecuteCallback(const uint8_t *Data, size_t Size);
Kostya Serebryanybaf7fd02016-05-04 20:44:50 +0000377 bool RunOne(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000378
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000379 // Merge Corpora[1:] into Corpora[0].
380 void Merge(const std::vector<std::string> &Corpora);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000381 // Returns a subset of 'Extra' that adds coverage to 'Initial'.
382 UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
Kostya Serebryany1deb0492016-02-13 06:24:18 +0000383 MutationDispatcher &GetMD() { return MD; }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000384 void PrintFinalStats();
Kostya Serebryany64d24572016-03-12 01:57:04 +0000385 void SetMaxLen(size_t MaxLen);
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000386 void RssLimitCallback();
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000387
Ivan Krasindf919102016-01-22 22:28:27 +0000388private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000389 void AlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000390 void CrashCallback();
391 void InterruptCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000392 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000393 void ReportNewCoverage(const Unit &U);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000394 bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
395 void RunOneAndUpdateCorpus(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000396 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000397 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000398 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000399 void PrintStatusForNewUnit(const Unit &U);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000400 void ShuffleCorpus(UnitVector *V);
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000401 void TryDetectingAMemoryLeak(uint8_t *Data, size_t Size);
402 void CheckForMemoryLeaks();
Kostya Serebryany945761b2016-03-18 00:23:29 +0000403
Ivan Krasindf919102016-01-22 22:28:27 +0000404 // Updates the probability distribution for the units in the corpus.
405 // Must be called whenever the corpus or unit weights are changed.
406 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000407
Kostya Serebryany945761b2016-03-18 00:23:29 +0000408 void ResetCoverage();
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000409 bool UpdateMaxCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000410
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000411 // Trace-based fuzzing: we run a unit with some kind of tracing
412 // enabled and record potentially useful mutations. Then
413 // We apply these mutations one by one to the unit and run it again.
414
415 // Start tracing; forget all previously proposed mutations.
416 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000417 // Stop tracing.
418 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000419
Aaron Ballmanef116982015-01-29 16:58:29 +0000420 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000421 static void StaticDeathCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000422 void DumpCurrentUnit(const char *Prefix);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000423 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000424
Kostya Serebryanyebb932d2016-04-18 22:50:39 +0000425 uint8_t *CurrentUnitData = nullptr;
426 size_t CurrentUnitSize = 0;
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000427 bool InOOMState = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000428
429 size_t TotalNumberOfRuns = 0;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000430 size_t NumberOfNewUnitsAdded = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000431
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000432 bool HasMoreMallocsThanFrees = false;
Kostya Serebryany7018a1a2016-04-27 19:52:34 +0000433 size_t NumberOfLeakDetectionAttempts = 0;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000434
Aaron Ballmanef116982015-01-29 16:58:29 +0000435 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000436 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000437 std::vector<uint8_t> MutateInPlaceHere;
438
Ivan Krasindf919102016-01-22 22:28:27 +0000439 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000440 UserCallback CB;
441 MutationDispatcher &MD;
Aaron Ballmanef116982015-01-29 16:58:29 +0000442 FuzzingOptions Options;
443 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000444 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000445 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000446 long EpochOfLastReadOfOutputCorpus = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000447
448 // Maximum recorded coverage.
449 Coverage MaxCoverage;
Aaron Ballmanef116982015-01-29 16:58:29 +0000450};
451
Ivan Krasindf919102016-01-22 22:28:27 +0000452}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000453
454#endif // LLVM_FUZZER_INTERNAL_H