blob: 7d8247327f6bd07787610bce947482183eefcdec [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
Dan Liew3868e462016-05-19 22:00:33 +000030// Platform detection.
31#ifdef __linux__
32#define LIBFUZZER_LINUX 1
33#define LIBFUZZER_APPLE 0
34#elif __APPLE__
35#define LIBFUZZER_LINUX 0
36#define LIBFUZZER_APPLE 1
37#else
38#error "Support for your platform has not been implemented"
39#endif
40
Aaron Ballmanef116982015-01-29 16:58:29 +000041namespace fuzzer {
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +000042
43typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
44int FuzzerDriver(int argc, char **argv, UserCallback Callback);
45
Aaron Ballmanef116982015-01-29 16:58:29 +000046using namespace std::chrono;
Kostya Serebryanyaca76962016-01-16 01:23:12 +000047typedef std::vector<uint8_t> Unit;
Kostya Serebryany945761b2016-03-18 00:23:29 +000048typedef std::vector<Unit> UnitVector;
Aaron Ballmanef116982015-01-29 16:58:29 +000049
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000050// A simple POD sized array of bytes.
Ivan Krasindf919102016-01-22 22:28:27 +000051template <size_t kMaxSize> class FixedWord {
52public:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000053 FixedWord() {}
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000054 FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
55
56 void Set(const uint8_t *B, uint8_t S) {
57 assert(S <= kMaxSize);
58 memcpy(Data, B, S);
59 Size = S;
60 }
61
Ivan Krasindf919102016-01-22 22:28:27 +000062 bool operator==(const FixedWord<kMaxSize> &w) const {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000063 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
64 }
65
Ivan Krasindf919102016-01-22 22:28:27 +000066 bool operator<(const FixedWord<kMaxSize> &w) const {
67 if (Size != w.Size)
68 return Size < w.Size;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000069 return memcmp(Data, w.Data, Size) < 0;
70 }
71
72 static size_t GetMaxSize() { return kMaxSize; }
73 const uint8_t *data() const { return Data; }
74 uint8_t size() const { return Size; }
75
Ivan Krasindf919102016-01-22 22:28:27 +000076private:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000077 uint8_t Size = 0;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000078 uint8_t Data[kMaxSize];
79};
80
Ivan Krasindf919102016-01-22 22:28:27 +000081typedef FixedWord<27> Word; // 28 bytes.
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000082
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000083bool IsFile(const std::string &Path);
Kostya Serebryany52a788e2015-03-31 20:13:20 +000084std::string FileToString(const std::string &Path);
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000085Unit FileToVector(const std::string &Path, size_t MaxSize = 0);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000086void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000087 long *Epoch, size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +000088void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000089void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000090// Returns "Dir/FileName" or equivalent for the current OS.
91std::string DirPlusFile(const std::string &DirPath,
92 const std::string &FileName);
93
Kostya Serebryany49e40902016-03-18 20:58:29 +000094void DupAndCloseStderr();
95void CloseStdout();
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000096void Printf(const char *Fmt, ...);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000097void PrintHexArray(const Unit &U, const char *PrintAfter = "");
98void PrintHexArray(const uint8_t *Data, size_t Size,
99 const char *PrintAfter = "");
Kostya Serebryany41740052016-01-12 02:36:59 +0000100void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000101void PrintASCII(const Unit &U, const char *PrintAfter = "");
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000102void PrintASCII(const Word &W, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000103std::string Hash(const Unit &U);
104void SetTimer(int Seconds);
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000105void SetSigSegvHandler();
106void SetSigBusHandler();
107void SetSigAbrtHandler();
108void SetSigIllHandler();
109void SetSigFpeHandler();
110void SetSigIntHandler();
Kostya Serebryanyf389ae12016-03-24 21:03:58 +0000111void SetSigTermHandler();
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000112std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000113int ExecuteCommand(const std::string &Command);
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000114size_t GetPeakRSSMb();
Aaron Ballmanef116982015-01-29 16:58:29 +0000115
Kostya Serebryany96eab652015-05-14 22:41:49 +0000116// Private copy of SHA1 implementation.
117static const int kSHA1NumBytes = 20;
118// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
119void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
120
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000121// Changes U to contain only ASCII (isprint+isspace) characters.
122// Returns true iff U has been changed.
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000123bool ToASCII(uint8_t *Data, size_t Size);
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000124bool IsASCII(const Unit &U);
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000125bool IsASCII(const uint8_t *Data, size_t Size);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000126
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000127int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000128int GetPid();
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000129int SignalToMainThread();
130void SleepSeconds(int Seconds);
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000131
Kostya Serebryanya3992212016-02-13 03:00:53 +0000132class Random {
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000133 public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000134 Random(unsigned int seed) : R(seed) {}
135 size_t Rand() { return R(); }
136 size_t RandBool() { return Rand() % 2; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000137 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000138 std::mt19937 &Get_mt19937() { return R; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000139 private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000140 std::mt19937 R;
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000141};
142
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000143// Dictionary.
144
145// Parses one dictionary entry.
146// If successfull, write the enty to Unit and returns true,
147// otherwise returns false.
148bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
149// Parses the dictionary file, fills Units, returns true iff all lines
150// were parsed succesfully.
151bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
152
Kostya Serebryany292cf032016-02-13 03:37:24 +0000153class DictionaryEntry {
154 public:
155 DictionaryEntry() {}
156 DictionaryEntry(Word W) : W(W) {}
157 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
158 const Word &GetW() const { return W; }
159
160 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
161 size_t GetPositionHint() const {
162 assert(HasPositionHint());
163 return PositionHint;
164 }
165 void IncUseCount() { UseCount++; }
166 void IncSuccessCount() { SuccessCount++; }
167 size_t GetUseCount() const { return UseCount; }
168 size_t GetSuccessCount() const {return SuccessCount; }
169
170private:
171 Word W;
172 size_t PositionHint = std::numeric_limits<size_t>::max();
173 size_t UseCount = 0;
174 size_t SuccessCount = 0;
175};
176
177class Dictionary {
178 public:
179 static const size_t kMaxDictSize = 1 << 14;
180
181 bool ContainsWord(const Word &W) const {
182 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
183 return DE.GetW() == W;
184 });
185 }
186 const DictionaryEntry *begin() const { return &DE[0]; }
187 const DictionaryEntry *end() const { return begin() + Size; }
188 DictionaryEntry & operator[] (size_t Idx) {
189 assert(Idx < Size);
190 return DE[Idx];
191 }
192 void push_back(DictionaryEntry DE) {
193 if (Size < kMaxDictSize)
194 this->DE[Size++] = DE;
195 }
196 void clear() { Size = 0; }
197 bool empty() const { return Size == 0; }
198 size_t size() const { return Size; }
199
200private:
201 DictionaryEntry DE[kMaxDictSize];
202 size_t Size = 0;
203};
204
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000205class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000206public:
Kostya Serebryany23194962016-02-13 03:46:26 +0000207 MutationDispatcher(Random &Rand) : Rand(Rand) {}
Kostya Serebryany292cf032016-02-13 03:37:24 +0000208 ~MutationDispatcher() {}
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000209 /// Indicate that we are about to start a new sequence of mutations.
210 void StartMutationSequence();
211 /// Print the current sequence of mutations.
212 void PrintMutationSequence();
213 /// Indicate that the current sequence of mutations was successfull.
214 void RecordSuccessfulMutationSequence();
215 /// Mutates data by shuffling bytes.
216 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
217 /// Mutates data by erasing a byte.
218 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
219 /// Mutates data by inserting a byte.
220 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
221 /// Mutates data by chanding one byte.
222 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
223 /// Mutates data by chanding one bit.
224 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
225
226 /// Mutates data by adding a word from the manual dictionary.
227 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
228 size_t MaxSize);
229
230 /// Mutates data by adding a word from the temporary automatic dictionary.
231 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
232 size_t MaxSize);
233
234 /// Mutates data by adding a word from the persistent automatic dictionary.
235 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
236 size_t MaxSize);
237
238 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
239 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
240
241 /// CrossOver Data with some other element of the corpus.
242 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
243
244 /// Applies one of the above mutations.
245 /// Returns the new size of data which could be up to MaxSize.
246 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
247
248 /// Creates a cross-over of two pieces of Data, returns its size.
249 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
250 size_t Size2, uint8_t *Out, size_t MaxOutSize);
251
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000252 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000253
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000254 void AddWordToAutoDictionary(const Word &W, size_t PositionHint);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000255 void ClearAutoDictionary();
256 void PrintRecommendedDictionary();
257
Kostya Serebryany292cf032016-02-13 03:37:24 +0000258 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000259
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000260 Random &GetRand() { return Rand; }
261
Ivan Krasindf919102016-01-22 22:28:27 +0000262private:
Kostya Serebryany292cf032016-02-13 03:37:24 +0000263
264 struct Mutator {
265 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
266 const char *Name;
267 };
268
Kostya Serebryany292cf032016-02-13 03:37:24 +0000269 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
270 size_t MaxSize);
271
Kostya Serebryanya3992212016-02-13 03:00:53 +0000272 Random &Rand;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000273 // Dictionary provided by the user via -dict=DICT_FILE.
274 Dictionary ManualDictionary;
275 // Temporary dictionary modified by the fuzzer itself,
276 // recreated periodically.
277 Dictionary TempAutoDictionary;
278 // Persistent dictionary modified by the fuzzer, consists of
279 // entries that led to successfull discoveries in the past mutations.
280 Dictionary PersistentAutoDictionary;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000281 std::vector<Mutator> CurrentMutatorSequence;
282 std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
283 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000284 std::vector<uint8_t> MutateInPlaceHere;
Kostya Serebryany23194962016-02-13 03:46:26 +0000285
286 static Mutator Mutators[];
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000287};
288
Aaron Ballmanef116982015-01-29 16:58:29 +0000289class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000290public:
Aaron Ballmanef116982015-01-29 16:58:29 +0000291 struct FuzzingOptions {
292 int Verbosity = 1;
Kostya Serebryany64d24572016-03-12 01:57:04 +0000293 size_t MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000294 int UnitTimeoutSec = 300;
Kostya Serebryany54a63632016-01-29 23:30:07 +0000295 int TimeoutExitCode = 77;
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000296 int ErrorExitCode = 77;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000297 int MaxTotalTimeSec = 0;
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000298 int RssLimitMb = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000299 bool DoCrossOver = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000300 int MutateDepth = 5;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000301 bool UseCounters = false;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000302 bool UseIndirCalls = true;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000303 bool UseTraces = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000304 bool UseMemcmp = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000305 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000306 bool Reload = true;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000307 bool ShuffleAtStartUp = true;
Kostya Serebryany945761b2016-03-18 00:23:29 +0000308 bool PreferSmall = true;
Kostya Serebryany33f86692015-02-04 22:20:09 +0000309 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000310 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000311 bool OnlyASCII = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000312 std::string OutputCorpus;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000313 std::string ArtifactPrefix = "./";
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000314 std::string ExactArtifactPath;
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000315 bool SaveArtifacts = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000316 bool PrintNEW = true; // Print a status line when new units are found;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000317 bool OutputCSV = false;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000318 bool PrintNewCovPcs = false;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000319 bool PrintFinalStats = false;
Kostya Serebryany2fe93042016-04-29 18:49:55 +0000320 bool DetectLeaks = true;
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000321 bool TruncateUnits = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000322 };
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000323
324 // Aggregates all available coverage measurements.
325 struct Coverage {
326 Coverage() { Reset(); }
327
328 void Reset() {
329 BlockCoverage = 0;
330 CallerCalleeCoverage = 0;
331 PcMapBits = 0;
332 CounterBitmapBits = 0;
333 PcBufferLen = 0;
334 CounterBitmap.clear();
335 PCMap.Reset();
336 }
337
338 std::string DebugString() const;
339
340 size_t BlockCoverage;
341 size_t CallerCalleeCoverage;
342
343 size_t PcBufferLen;
344 // Precalculated number of bits in CounterBitmap.
345 size_t CounterBitmapBits;
346 std::vector<uint8_t> CounterBitmap;
347 // Precalculated number of bits in PCMap.
348 size_t PcMapBits;
349 PcCoverageMap PCMap;
350 };
351
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000352 Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
Ivan Krasindf919102016-01-22 22:28:27 +0000353 void AddToCorpus(const Unit &U) {
354 Corpus.push_back(U);
355 UpdateCorpusDistribution();
356 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000357 size_t ChooseUnitIdxToMutate();
358 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000359 void TruncateUnits(std::vector<Unit> *NewCorpus);
Kostya Serebryany468ed782015-09-08 17:30:35 +0000360 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000361 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000362 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000363 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000364 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000365 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000366 size_t MaxUnitSizeInCorpus() const;
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000367 void ReadDir(const std::string &Path, long *Epoch, size_t MaxSize) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000368 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000369 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch, MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000370 }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000371 void RereadOutputCorpus(size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000372 // Save the current corpus to OutputCorpus.
373 void SaveCorpus();
374
Kostya Serebryany92e04762015-02-04 23:42:42 +0000375 size_t secondsSinceProcessStartUp() {
376 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
377 .count();
378 }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000379 size_t execPerSec() {
380 size_t Seconds = secondsSinceProcessStartUp();
381 return Seconds ? TotalNumberOfRuns / Seconds : 0;
382 }
Kostya Serebryany92e04762015-02-04 23:42:42 +0000383
384 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
385
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000386 static void StaticAlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000387 static void StaticCrashSignalCallback();
388 static void StaticInterruptCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000389
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000390 void ExecuteCallback(const uint8_t *Data, size_t Size);
Kostya Serebryanybaf7fd02016-05-04 20:44:50 +0000391 bool RunOne(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000392
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000393 // Merge Corpora[1:] into Corpora[0].
394 void Merge(const std::vector<std::string> &Corpora);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000395 // Returns a subset of 'Extra' that adds coverage to 'Initial'.
396 UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
Kostya Serebryany1deb0492016-02-13 06:24:18 +0000397 MutationDispatcher &GetMD() { return MD; }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000398 void PrintFinalStats();
Kostya Serebryany64d24572016-03-12 01:57:04 +0000399 void SetMaxLen(size_t MaxLen);
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000400 void RssLimitCallback();
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000401
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000402 // Public for tests.
403 void ResetCoverage();
404
Ivan Krasindf919102016-01-22 22:28:27 +0000405private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000406 void AlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000407 void CrashCallback();
408 void InterruptCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000409 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000410 void ReportNewCoverage(const Unit &U);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000411 bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000412 void RunOneAndUpdateCorpus(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000413 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000414 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000415 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000416 void PrintStatusForNewUnit(const Unit &U);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000417 void ShuffleCorpus(UnitVector *V);
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000418 void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size);
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000419 void CheckForMemoryLeaks();
Kostya Serebryany945761b2016-03-18 00:23:29 +0000420
Ivan Krasindf919102016-01-22 22:28:27 +0000421 // Updates the probability distribution for the units in the corpus.
422 // Must be called whenever the corpus or unit weights are changed.
423 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000424
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000425 bool UpdateMaxCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000426
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000427 // Trace-based fuzzing: we run a unit with some kind of tracing
428 // enabled and record potentially useful mutations. Then
429 // We apply these mutations one by one to the unit and run it again.
430
431 // Start tracing; forget all previously proposed mutations.
432 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000433 // Stop tracing.
434 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000435
Aaron Ballmanef116982015-01-29 16:58:29 +0000436 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000437 static void StaticDeathCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000438 void DumpCurrentUnit(const char *Prefix);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000439 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000440
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000441 const uint8_t *CurrentUnitData = nullptr;
Kostya Serebryanyebb932d2016-04-18 22:50:39 +0000442 size_t CurrentUnitSize = 0;
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000443 bool InOOMState = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000444
445 size_t TotalNumberOfRuns = 0;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000446 size_t NumberOfNewUnitsAdded = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000447
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000448 bool HasMoreMallocsThanFrees = false;
Kostya Serebryany7018a1a2016-04-27 19:52:34 +0000449 size_t NumberOfLeakDetectionAttempts = 0;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000450
Aaron Ballmanef116982015-01-29 16:58:29 +0000451 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000452 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000453 std::vector<uint8_t> MutateInPlaceHere;
454
Ivan Krasindf919102016-01-22 22:28:27 +0000455 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000456 UserCallback CB;
457 MutationDispatcher &MD;
Aaron Ballmanef116982015-01-29 16:58:29 +0000458 FuzzingOptions Options;
459 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000460 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000461 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000462 long EpochOfLastReadOfOutputCorpus = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000463
464 // Maximum recorded coverage.
465 Coverage MaxCoverage;
Aaron Ballmanef116982015-01-29 16:58:29 +0000466};
467
Ivan Krasindf919102016-01-22 22:28:27 +0000468}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000469
470#endif // LLVM_FUZZER_INTERNAL_H