blob: 6ffaaf292361c5ad2b63942a6a5ab6ddb254843d [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 Serebryanyd46a59f2016-08-16 19:33:51 +0000137// See FuzzerTraceState.cpp
138void EnableValueProfile();
139size_t VPMapMergeFromCurrent(ValueBitMap &M);
140
Kostya Serebryanya3992212016-02-13 03:00:53 +0000141class Random {
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000142 public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000143 Random(unsigned int seed) : R(seed) {}
144 size_t Rand() { return R(); }
145 size_t RandBool() { return Rand() % 2; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000146 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000147 std::mt19937 &Get_mt19937() { return R; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000148 private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000149 std::mt19937 R;
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000150};
151
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000152// Dictionary.
153
154// Parses one dictionary entry.
155// If successfull, write the enty to Unit and returns true,
156// otherwise returns false.
157bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
158// Parses the dictionary file, fills Units, returns true iff all lines
159// were parsed succesfully.
160bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
161
Kostya Serebryany292cf032016-02-13 03:37:24 +0000162class DictionaryEntry {
163 public:
164 DictionaryEntry() {}
165 DictionaryEntry(Word W) : W(W) {}
166 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
167 const Word &GetW() const { return W; }
168
169 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
170 size_t GetPositionHint() const {
171 assert(HasPositionHint());
172 return PositionHint;
173 }
174 void IncUseCount() { UseCount++; }
175 void IncSuccessCount() { SuccessCount++; }
176 size_t GetUseCount() const { return UseCount; }
177 size_t GetSuccessCount() const {return SuccessCount; }
178
179private:
180 Word W;
181 size_t PositionHint = std::numeric_limits<size_t>::max();
182 size_t UseCount = 0;
183 size_t SuccessCount = 0;
184};
185
186class Dictionary {
187 public:
188 static const size_t kMaxDictSize = 1 << 14;
189
190 bool ContainsWord(const Word &W) const {
191 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
192 return DE.GetW() == W;
193 });
194 }
195 const DictionaryEntry *begin() const { return &DE[0]; }
196 const DictionaryEntry *end() const { return begin() + Size; }
197 DictionaryEntry & operator[] (size_t Idx) {
198 assert(Idx < Size);
199 return DE[Idx];
200 }
201 void push_back(DictionaryEntry DE) {
202 if (Size < kMaxDictSize)
203 this->DE[Size++] = DE;
204 }
205 void clear() { Size = 0; }
206 bool empty() const { return Size == 0; }
207 size_t size() const { return Size; }
208
209private:
210 DictionaryEntry DE[kMaxDictSize];
211 size_t Size = 0;
212};
213
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000214struct FuzzingOptions {
215 int Verbosity = 1;
216 size_t MaxLen = 0;
217 int UnitTimeoutSec = 300;
218 int TimeoutExitCode = 77;
219 int ErrorExitCode = 77;
220 int MaxTotalTimeSec = 0;
221 int RssLimitMb = 0;
222 bool DoCrossOver = true;
223 int MutateDepth = 5;
224 bool UseCounters = false;
225 bool UseIndirCalls = true;
226 bool UseTraces = false;
227 bool UseMemcmp = true;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000228 bool UseMemmem = true;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000229 bool UseFullCoverageSet = false;
230 bool Reload = true;
231 bool ShuffleAtStartUp = true;
232 bool PreferSmall = true;
233 size_t MaxNumberOfRuns = ULONG_MAX;
234 int ReportSlowUnits = 10;
235 bool OnlyASCII = false;
236 std::string OutputCorpus;
237 std::string ArtifactPrefix = "./";
238 std::string ExactArtifactPath;
239 bool SaveArtifacts = true;
240 bool PrintNEW = true; // Print a status line when new units are found;
241 bool OutputCSV = false;
242 bool PrintNewCovPcs = false;
243 bool PrintFinalStats = false;
244 bool DetectLeaks = true;
245 bool TruncateUnits = false;
246 bool PruneCorpus = true;
247};
248
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000249class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000250public:
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000251 MutationDispatcher(Random &Rand, const FuzzingOptions &Options);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000252 ~MutationDispatcher() {}
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000253 /// Indicate that we are about to start a new sequence of mutations.
254 void StartMutationSequence();
255 /// Print the current sequence of mutations.
256 void PrintMutationSequence();
257 /// Indicate that the current sequence of mutations was successfull.
258 void RecordSuccessfulMutationSequence();
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000259 /// Mutates data by invoking user-provided mutator.
260 size_t Mutate_Custom(uint8_t *Data, size_t Size, size_t MaxSize);
Mike Aizatsky41d66832016-06-07 20:22:15 +0000261 /// Mutates data by invoking user-provided crossover.
262 size_t Mutate_CustomCrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000263 /// Mutates data by shuffling bytes.
264 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000265 /// Mutates data by erasing bytes.
266 size_t Mutate_EraseBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000267 /// Mutates data by inserting a byte.
268 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000269 /// Mutates data by inserting several repeated bytes.
270 size_t Mutate_InsertRepeatedBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000271 /// Mutates data by chanding one byte.
272 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
273 /// Mutates data by chanding one bit.
274 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000275 /// Mutates data by copying/inserting a part of data into a different place.
276 size_t Mutate_CopyPart(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000277
278 /// Mutates data by adding a word from the manual dictionary.
279 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
280 size_t MaxSize);
281
282 /// Mutates data by adding a word from the temporary automatic dictionary.
283 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
284 size_t MaxSize);
285
286 /// Mutates data by adding a word from the persistent automatic dictionary.
287 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
288 size_t MaxSize);
289
290 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
291 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
292
293 /// CrossOver Data with some other element of the corpus.
294 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
295
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000296 /// Applies one of the configured mutations.
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000297 /// Returns the new size of data which could be up to MaxSize.
298 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000299 /// Applies one of the default mutations. Provided as a service
300 /// to mutation authors.
301 size_t DefaultMutate(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000302
303 /// Creates a cross-over of two pieces of Data, returns its size.
304 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
305 size_t Size2, uint8_t *Out, size_t MaxOutSize);
306
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000307 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000308
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000309 void AddWordToAutoDictionary(DictionaryEntry DE);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000310 void ClearAutoDictionary();
311 void PrintRecommendedDictionary();
312
Kostya Serebryany292cf032016-02-13 03:37:24 +0000313 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000314
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000315 Random &GetRand() { return Rand; }
316
Ivan Krasindf919102016-01-22 22:28:27 +0000317private:
Kostya Serebryany292cf032016-02-13 03:37:24 +0000318
319 struct Mutator {
320 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
321 const char *Name;
322 };
323
Kostya Serebryany292cf032016-02-13 03:37:24 +0000324 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
325 size_t MaxSize);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000326 size_t MutateImpl(uint8_t *Data, size_t Size, size_t MaxSize,
327 const std::vector<Mutator> &Mutators);
328
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000329 size_t InsertPartOf(const uint8_t *From, size_t FromSize, uint8_t *To,
330 size_t ToSize, size_t MaxToSize);
331 size_t CopyPartOf(const uint8_t *From, size_t FromSize, uint8_t *To,
332 size_t ToSize);
333
Kostya Serebryanya3992212016-02-13 03:00:53 +0000334 Random &Rand;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000335 const FuzzingOptions Options;
336
Kostya Serebryany292cf032016-02-13 03:37:24 +0000337 // Dictionary provided by the user via -dict=DICT_FILE.
338 Dictionary ManualDictionary;
339 // Temporary dictionary modified by the fuzzer itself,
340 // recreated periodically.
341 Dictionary TempAutoDictionary;
342 // Persistent dictionary modified by the fuzzer, consists of
343 // entries that led to successfull discoveries in the past mutations.
344 Dictionary PersistentAutoDictionary;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000345 std::vector<Mutator> CurrentMutatorSequence;
346 std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
347 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000348 std::vector<uint8_t> MutateInPlaceHere;
Kostya Serebryany23194962016-02-13 03:46:26 +0000349
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000350 std::vector<Mutator> Mutators;
351 std::vector<Mutator> DefaultMutators;
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000352};
353
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000354class CoverageController;
355
Aaron Ballmanef116982015-01-29 16:58:29 +0000356class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000357public:
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000358
359 // Aggregates all available coverage measurements.
360 struct Coverage {
361 Coverage() { Reset(); }
362
363 void Reset() {
364 BlockCoverage = 0;
365 CallerCalleeCoverage = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000366 CounterBitmapBits = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000367 CounterBitmap.clear();
368 PCMap.Reset();
Kostya Serebryanyc98ef712016-08-16 17:37:13 +0000369 PCMapBits = 0;
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000370 VPMap.Reset();
371 VPMapBits = 0;
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000372 PcBufferPos = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000373 }
374
375 std::string DebugString() const;
376
377 size_t BlockCoverage;
378 size_t CallerCalleeCoverage;
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000379 size_t PcBufferPos;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000380 // Precalculated number of bits in CounterBitmap.
381 size_t CounterBitmapBits;
382 std::vector<uint8_t> CounterBitmap;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +0000383 ValueBitMap PCMap;
384 size_t PCMapBits;
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000385 ValueBitMap VPMap;
386 size_t VPMapBits;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000387 };
388
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000389 Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000390 ~Fuzzer();
Ivan Krasindf919102016-01-22 22:28:27 +0000391 void AddToCorpus(const Unit &U) {
392 Corpus.push_back(U);
393 UpdateCorpusDistribution();
394 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000395 size_t ChooseUnitIdxToMutate();
396 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000397 void TruncateUnits(std::vector<Unit> *NewCorpus);
Kostya Serebryany468ed782015-09-08 17:30:35 +0000398 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000399 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000400 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000401 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000402 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000403 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000404 size_t MaxUnitSizeInCorpus() const;
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000405 void ReadDir(const std::string &Path, long *Epoch, size_t MaxSize) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000406 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000407 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch, MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000408 }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000409 void RereadOutputCorpus(size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000410 // Save the current corpus to OutputCorpus.
411 void SaveCorpus();
412
Kostya Serebryany92e04762015-02-04 23:42:42 +0000413 size_t secondsSinceProcessStartUp() {
414 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
415 .count();
416 }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000417 size_t execPerSec() {
418 size_t Seconds = secondsSinceProcessStartUp();
419 return Seconds ? TotalNumberOfRuns / Seconds : 0;
420 }
Kostya Serebryany92e04762015-02-04 23:42:42 +0000421
422 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
423
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000424 static void StaticAlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000425 static void StaticCrashSignalCallback();
426 static void StaticInterruptCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000427
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000428 void ExecuteCallback(const uint8_t *Data, size_t Size);
Kostya Serebryanybaf7fd02016-05-04 20:44:50 +0000429 bool RunOne(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000430
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000431 // Merge Corpora[1:] into Corpora[0].
432 void Merge(const std::vector<std::string> &Corpora);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000433 // Returns a subset of 'Extra' that adds coverage to 'Initial'.
434 UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
Kostya Serebryany1deb0492016-02-13 06:24:18 +0000435 MutationDispatcher &GetMD() { return MD; }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000436 void PrintFinalStats();
Kostya Serebryany64d24572016-03-12 01:57:04 +0000437 void SetMaxLen(size_t MaxLen);
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000438 void RssLimitCallback();
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000439
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000440 // Public for tests.
441 void ResetCoverage();
442
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000443 bool InFuzzingThread() const { return IsMyThread; }
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000444 size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000445
Ivan Krasindf919102016-01-22 22:28:27 +0000446private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000447 void AlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000448 void CrashCallback();
449 void InterruptCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000450 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000451 void ReportNewCoverage(const Unit &U);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000452 bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000453 void RunOneAndUpdateCorpus(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000454 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000455 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000456 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000457 void PrintStatusForNewUnit(const Unit &U);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000458 void ShuffleCorpus(UnitVector *V);
Kostya Serebryany4b923262016-05-26 20:25:49 +0000459 void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
460 bool DuringInitialCorpusExecution);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000461
Ivan Krasindf919102016-01-22 22:28:27 +0000462 // Updates the probability distribution for the units in the corpus.
463 // Must be called whenever the corpus or unit weights are changed.
464 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000465
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000466 bool UpdateMaxCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000467
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000468 // Trace-based fuzzing: we run a unit with some kind of tracing
469 // enabled and record potentially useful mutations. Then
470 // We apply these mutations one by one to the unit and run it again.
471
472 // Start tracing; forget all previously proposed mutations.
473 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000474 // Stop tracing.
475 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000476
Aaron Ballmanef116982015-01-29 16:58:29 +0000477 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000478 static void StaticDeathCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000479 void DumpCurrentUnit(const char *Prefix);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000480 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000481
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000482 void LazyAllocateCurrentUnitData();
483 uint8_t *CurrentUnitData = nullptr;
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000484 std::atomic<size_t> CurrentUnitSize;
Aaron Ballmanef116982015-01-29 16:58:29 +0000485
486 size_t TotalNumberOfRuns = 0;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000487 size_t NumberOfNewUnitsAdded = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000488
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000489 bool HasMoreMallocsThanFrees = false;
Kostya Serebryany7018a1a2016-04-27 19:52:34 +0000490 size_t NumberOfLeakDetectionAttempts = 0;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000491
Aaron Ballmanef116982015-01-29 16:58:29 +0000492 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000493 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000494
Ivan Krasindf919102016-01-22 22:28:27 +0000495 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000496 UserCallback CB;
497 MutationDispatcher &MD;
Aaron Ballmanef116982015-01-29 16:58:29 +0000498 FuzzingOptions Options;
499 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000500 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000501 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000502 long EpochOfLastReadOfOutputCorpus = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000503
504 // Maximum recorded coverage.
505 Coverage MaxCoverage;
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000506 std::unique_ptr<CoverageController> CController;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000507
508 // Need to know our own thread.
509 static thread_local bool IsMyThread;
Aaron Ballmanef116982015-01-29 16:58:29 +0000510};
511
Dan Liew1873a492016-06-07 23:32:50 +0000512// Global interface to functions that may or may not be available.
513extern ExternalFunctions *EF;
514
Ivan Krasindf919102016-01-22 22:28:27 +0000515}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000516
517#endif // LLVM_FUZZER_INTERNAL_H