blob: 61f48662f609b1d8accb9fd369e485cd3c6423f2 [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"
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000031#include "FuzzerTracePC.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 Serebryanya3992212016-02-13 03:00:53 +0000134class Random {
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000135 public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000136 Random(unsigned int seed) : R(seed) {}
137 size_t Rand() { return R(); }
138 size_t RandBool() { return Rand() % 2; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000139 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000140 std::mt19937 &Get_mt19937() { return R; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000141 private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000142 std::mt19937 R;
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000143};
144
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000145// Dictionary.
146
147// Parses one dictionary entry.
148// If successfull, write the enty to Unit and returns true,
149// otherwise returns false.
150bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
151// Parses the dictionary file, fills Units, returns true iff all lines
152// were parsed succesfully.
153bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
154
Kostya Serebryany292cf032016-02-13 03:37:24 +0000155class DictionaryEntry {
156 public:
157 DictionaryEntry() {}
158 DictionaryEntry(Word W) : W(W) {}
159 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
160 const Word &GetW() const { return W; }
161
162 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
163 size_t GetPositionHint() const {
164 assert(HasPositionHint());
165 return PositionHint;
166 }
167 void IncUseCount() { UseCount++; }
168 void IncSuccessCount() { SuccessCount++; }
169 size_t GetUseCount() const { return UseCount; }
170 size_t GetSuccessCount() const {return SuccessCount; }
171
172private:
173 Word W;
174 size_t PositionHint = std::numeric_limits<size_t>::max();
175 size_t UseCount = 0;
176 size_t SuccessCount = 0;
177};
178
179class Dictionary {
180 public:
181 static const size_t kMaxDictSize = 1 << 14;
182
183 bool ContainsWord(const Word &W) const {
184 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
185 return DE.GetW() == W;
186 });
187 }
188 const DictionaryEntry *begin() const { return &DE[0]; }
189 const DictionaryEntry *end() const { return begin() + Size; }
190 DictionaryEntry & operator[] (size_t Idx) {
191 assert(Idx < Size);
192 return DE[Idx];
193 }
194 void push_back(DictionaryEntry DE) {
195 if (Size < kMaxDictSize)
196 this->DE[Size++] = DE;
197 }
198 void clear() { Size = 0; }
199 bool empty() const { return Size == 0; }
200 size_t size() const { return Size; }
201
202private:
203 DictionaryEntry DE[kMaxDictSize];
204 size_t Size = 0;
205};
206
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000207struct FuzzingOptions {
208 int Verbosity = 1;
209 size_t MaxLen = 0;
210 int UnitTimeoutSec = 300;
211 int TimeoutExitCode = 77;
212 int ErrorExitCode = 77;
213 int MaxTotalTimeSec = 0;
214 int RssLimitMb = 0;
215 bool DoCrossOver = true;
216 int MutateDepth = 5;
217 bool UseCounters = false;
218 bool UseIndirCalls = true;
219 bool UseTraces = false;
220 bool UseMemcmp = true;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000221 bool UseMemmem = true;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000222 bool UseFullCoverageSet = false;
223 bool Reload = true;
224 bool ShuffleAtStartUp = true;
225 bool PreferSmall = true;
226 size_t MaxNumberOfRuns = ULONG_MAX;
227 int ReportSlowUnits = 10;
228 bool OnlyASCII = false;
229 std::string OutputCorpus;
230 std::string ArtifactPrefix = "./";
231 std::string ExactArtifactPath;
232 bool SaveArtifacts = true;
233 bool PrintNEW = true; // Print a status line when new units are found;
234 bool OutputCSV = false;
235 bool PrintNewCovPcs = false;
236 bool PrintFinalStats = false;
237 bool DetectLeaks = true;
238 bool TruncateUnits = false;
239 bool PruneCorpus = true;
240};
241
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000242class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000243public:
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000244 MutationDispatcher(Random &Rand, const FuzzingOptions &Options);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000245 ~MutationDispatcher() {}
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000246 /// Indicate that we are about to start a new sequence of mutations.
247 void StartMutationSequence();
248 /// Print the current sequence of mutations.
249 void PrintMutationSequence();
250 /// Indicate that the current sequence of mutations was successfull.
251 void RecordSuccessfulMutationSequence();
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000252 /// Mutates data by invoking user-provided mutator.
253 size_t Mutate_Custom(uint8_t *Data, size_t Size, size_t MaxSize);
Mike Aizatsky41d66832016-06-07 20:22:15 +0000254 /// Mutates data by invoking user-provided crossover.
255 size_t Mutate_CustomCrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000256 /// Mutates data by shuffling bytes.
257 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000258 /// Mutates data by erasing bytes.
259 size_t Mutate_EraseBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000260 /// Mutates data by inserting a byte.
261 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000262 /// Mutates data by inserting several repeated bytes.
263 size_t Mutate_InsertRepeatedBytes(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000264 /// Mutates data by chanding one byte.
265 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
266 /// Mutates data by chanding one bit.
267 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
268
269 /// Mutates data by adding a word from the manual dictionary.
270 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
271 size_t MaxSize);
272
273 /// Mutates data by adding a word from the temporary automatic dictionary.
274 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
275 size_t MaxSize);
276
277 /// Mutates data by adding a word from the persistent automatic dictionary.
278 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
279 size_t MaxSize);
280
281 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
282 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
283
284 /// CrossOver Data with some other element of the corpus.
285 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
286
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000287 /// Applies one of the configured mutations.
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000288 /// Returns the new size of data which could be up to MaxSize.
289 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000290 /// Applies one of the default mutations. Provided as a service
291 /// to mutation authors.
292 size_t DefaultMutate(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000293
294 /// Creates a cross-over of two pieces of Data, returns its size.
295 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
296 size_t Size2, uint8_t *Out, size_t MaxOutSize);
297
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000298 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000299
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000300 void AddWordToAutoDictionary(DictionaryEntry DE);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000301 void ClearAutoDictionary();
302 void PrintRecommendedDictionary();
303
Kostya Serebryany292cf032016-02-13 03:37:24 +0000304 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000305
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000306 Random &GetRand() { return Rand; }
307
Ivan Krasindf919102016-01-22 22:28:27 +0000308private:
Kostya Serebryany292cf032016-02-13 03:37:24 +0000309
310 struct Mutator {
311 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
312 const char *Name;
313 };
314
Kostya Serebryany292cf032016-02-13 03:37:24 +0000315 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
316 size_t MaxSize);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000317 size_t MutateImpl(uint8_t *Data, size_t Size, size_t MaxSize,
318 const std::vector<Mutator> &Mutators);
319
Kostya Serebryanya3992212016-02-13 03:00:53 +0000320 Random &Rand;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000321 const FuzzingOptions Options;
322
Kostya Serebryany292cf032016-02-13 03:37:24 +0000323 // Dictionary provided by the user via -dict=DICT_FILE.
324 Dictionary ManualDictionary;
325 // Temporary dictionary modified by the fuzzer itself,
326 // recreated periodically.
327 Dictionary TempAutoDictionary;
328 // Persistent dictionary modified by the fuzzer, consists of
329 // entries that led to successfull discoveries in the past mutations.
330 Dictionary PersistentAutoDictionary;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000331 std::vector<Mutator> CurrentMutatorSequence;
332 std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
333 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000334 std::vector<uint8_t> MutateInPlaceHere;
Kostya Serebryany23194962016-02-13 03:46:26 +0000335
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000336 std::vector<Mutator> Mutators;
337 std::vector<Mutator> DefaultMutators;
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000338};
339
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000340class CoverageController;
341
Aaron Ballmanef116982015-01-29 16:58:29 +0000342class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000343public:
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000344
345 // Aggregates all available coverage measurements.
346 struct Coverage {
347 Coverage() { Reset(); }
348
349 void Reset() {
350 BlockCoverage = 0;
351 CallerCalleeCoverage = 0;
352 PcMapBits = 0;
353 CounterBitmapBits = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000354 CounterBitmap.clear();
355 PCMap.Reset();
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000356 PcBufferPos = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000357 }
358
359 std::string DebugString() const;
360
361 size_t BlockCoverage;
362 size_t CallerCalleeCoverage;
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000363 size_t PcBufferPos;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000364 // Precalculated number of bits in CounterBitmap.
365 size_t CounterBitmapBits;
366 std::vector<uint8_t> CounterBitmap;
367 // Precalculated number of bits in PCMap.
368 size_t PcMapBits;
369 PcCoverageMap PCMap;
370 };
371
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000372 Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000373 ~Fuzzer();
Ivan Krasindf919102016-01-22 22:28:27 +0000374 void AddToCorpus(const Unit &U) {
375 Corpus.push_back(U);
376 UpdateCorpusDistribution();
377 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000378 size_t ChooseUnitIdxToMutate();
379 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000380 void TruncateUnits(std::vector<Unit> *NewCorpus);
Kostya Serebryany468ed782015-09-08 17:30:35 +0000381 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000382 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000383 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000384 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000385 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000386 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000387 size_t MaxUnitSizeInCorpus() const;
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000388 void ReadDir(const std::string &Path, long *Epoch, size_t MaxSize) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000389 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000390 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch, MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000391 }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000392 void RereadOutputCorpus(size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000393 // Save the current corpus to OutputCorpus.
394 void SaveCorpus();
395
Kostya Serebryany92e04762015-02-04 23:42:42 +0000396 size_t secondsSinceProcessStartUp() {
397 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
398 .count();
399 }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000400 size_t execPerSec() {
401 size_t Seconds = secondsSinceProcessStartUp();
402 return Seconds ? TotalNumberOfRuns / Seconds : 0;
403 }
Kostya Serebryany92e04762015-02-04 23:42:42 +0000404
405 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
406
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000407 static void StaticAlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000408 static void StaticCrashSignalCallback();
409 static void StaticInterruptCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000410
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000411 void ExecuteCallback(const uint8_t *Data, size_t Size);
Kostya Serebryanybaf7fd02016-05-04 20:44:50 +0000412 bool RunOne(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000413
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000414 // Merge Corpora[1:] into Corpora[0].
415 void Merge(const std::vector<std::string> &Corpora);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000416 // Returns a subset of 'Extra' that adds coverage to 'Initial'.
417 UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
Kostya Serebryany1deb0492016-02-13 06:24:18 +0000418 MutationDispatcher &GetMD() { return MD; }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000419 void PrintFinalStats();
Kostya Serebryany64d24572016-03-12 01:57:04 +0000420 void SetMaxLen(size_t MaxLen);
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000421 void RssLimitCallback();
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000422
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000423 // Public for tests.
424 void ResetCoverage();
425
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000426 bool InFuzzingThread() const { return IsMyThread; }
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000427 size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000428
Ivan Krasindf919102016-01-22 22:28:27 +0000429private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000430 void AlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000431 void CrashCallback();
432 void InterruptCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000433 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000434 void ReportNewCoverage(const Unit &U);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000435 bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000436 void RunOneAndUpdateCorpus(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000437 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000438 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000439 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000440 void PrintStatusForNewUnit(const Unit &U);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000441 void ShuffleCorpus(UnitVector *V);
Kostya Serebryany4b923262016-05-26 20:25:49 +0000442 void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
443 bool DuringInitialCorpusExecution);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000444
Ivan Krasindf919102016-01-22 22:28:27 +0000445 // Updates the probability distribution for the units in the corpus.
446 // Must be called whenever the corpus or unit weights are changed.
447 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000448
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000449 bool UpdateMaxCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000450
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000451 // Trace-based fuzzing: we run a unit with some kind of tracing
452 // enabled and record potentially useful mutations. Then
453 // We apply these mutations one by one to the unit and run it again.
454
455 // Start tracing; forget all previously proposed mutations.
456 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000457 // Stop tracing.
458 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000459
Aaron Ballmanef116982015-01-29 16:58:29 +0000460 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000461 static void StaticDeathCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000462 void DumpCurrentUnit(const char *Prefix);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000463 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000464
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000465 void LazyAllocateCurrentUnitData();
466 uint8_t *CurrentUnitData = nullptr;
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000467 std::atomic<size_t> CurrentUnitSize;
Aaron Ballmanef116982015-01-29 16:58:29 +0000468
469 size_t TotalNumberOfRuns = 0;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000470 size_t NumberOfNewUnitsAdded = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000471
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000472 bool HasMoreMallocsThanFrees = false;
Kostya Serebryany7018a1a2016-04-27 19:52:34 +0000473 size_t NumberOfLeakDetectionAttempts = 0;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000474
Aaron Ballmanef116982015-01-29 16:58:29 +0000475 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000476 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000477
Ivan Krasindf919102016-01-22 22:28:27 +0000478 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000479 UserCallback CB;
480 MutationDispatcher &MD;
Aaron Ballmanef116982015-01-29 16:58:29 +0000481 FuzzingOptions Options;
482 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000483 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000484 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000485 long EpochOfLastReadOfOutputCorpus = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000486
487 // Maximum recorded coverage.
488 Coverage MaxCoverage;
Mike Aizatskyb4bbc3b2016-08-05 20:09:53 +0000489 std::unique_ptr<CoverageController> CController;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000490
491 // Need to know our own thread.
492 static thread_local bool IsMyThread;
Aaron Ballmanef116982015-01-29 16:58:29 +0000493};
494
Dan Liew1873a492016-06-07 23:32:50 +0000495// Global interface to functions that may or may not be available.
496extern ExternalFunctions *EF;
497
Ivan Krasindf919102016-01-22 22:28:27 +0000498}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000499
500#endif // LLVM_FUZZER_INTERNAL_H