blob: 03905c2ec3a142ab630a094e6c85837c37f5a39a [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"
28
Aaron Ballmanef116982015-01-29 16:58:29 +000029namespace fuzzer {
Aaron Ballmanef116982015-01-29 16:58:29 +000030using namespace std::chrono;
Kostya Serebryanyaca76962016-01-16 01:23:12 +000031typedef std::vector<uint8_t> Unit;
Aaron Ballmanef116982015-01-29 16:58:29 +000032
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000033// A simple POD sized array of bytes.
Ivan Krasindf919102016-01-22 22:28:27 +000034template <size_t kMaxSize> class FixedWord {
35public:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000036 FixedWord() {}
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000037 FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
38
39 void Set(const uint8_t *B, uint8_t S) {
40 assert(S <= kMaxSize);
41 memcpy(Data, B, S);
42 Size = S;
43 }
44
Ivan Krasindf919102016-01-22 22:28:27 +000045 bool operator==(const FixedWord<kMaxSize> &w) const {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000046 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
47 }
48
Ivan Krasindf919102016-01-22 22:28:27 +000049 bool operator<(const FixedWord<kMaxSize> &w) const {
50 if (Size != w.Size)
51 return Size < w.Size;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000052 return memcmp(Data, w.Data, Size) < 0;
53 }
54
55 static size_t GetMaxSize() { return kMaxSize; }
56 const uint8_t *data() const { return Data; }
57 uint8_t size() const { return Size; }
58
Ivan Krasindf919102016-01-22 22:28:27 +000059private:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000060 uint8_t Size = 0;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000061 uint8_t Data[kMaxSize];
62};
63
Ivan Krasindf919102016-01-22 22:28:27 +000064typedef FixedWord<27> Word; // 28 bytes.
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000065
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000066bool IsFile(const std::string &Path);
Kostya Serebryany52a788e2015-03-31 20:13:20 +000067std::string FileToString(const std::string &Path);
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000068Unit FileToVector(const std::string &Path, size_t MaxSize = 0);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000069void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000070 long *Epoch, size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +000071void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000072void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000073// Returns "Dir/FileName" or equivalent for the current OS.
74std::string DirPlusFile(const std::string &DirPath,
75 const std::string &FileName);
76
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000077void Printf(const char *Fmt, ...);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000078void PrintHexArray(const Unit &U, const char *PrintAfter = "");
79void PrintHexArray(const uint8_t *Data, size_t Size,
80 const char *PrintAfter = "");
Kostya Serebryany41740052016-01-12 02:36:59 +000081void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000082void PrintASCII(const Unit &U, const char *PrintAfter = "");
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000083void PrintASCII(const Word &W, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000084std::string Hash(const Unit &U);
85void SetTimer(int Seconds);
Kostya Serebryany9e48cda2015-12-04 22:29:39 +000086std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000087int ExecuteCommand(const std::string &Command);
Aaron Ballmanef116982015-01-29 16:58:29 +000088
Kostya Serebryany96eab652015-05-14 22:41:49 +000089// Private copy of SHA1 implementation.
90static const int kSHA1NumBytes = 20;
91// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
92void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
93
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000094// Changes U to contain only ASCII (isprint+isspace) characters.
95// Returns true iff U has been changed.
Kostya Serebryany8a5bef02016-02-13 17:56:51 +000096bool ToASCII(uint8_t *Data, size_t Size);
Kostya Serebryanya9346c22015-09-02 19:08:08 +000097bool IsASCII(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000098
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000099int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000100int GetPid();
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000101
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000102// Clears the current PC Map.
103void PcMapResetCurrent();
104// Merges the current PC Map into the combined one, and clears the former.
105void PcMapMergeCurrentToCombined();
106// Returns the size of the combined PC Map.
107size_t PcMapCombinedSize();
108
Kostya Serebryanya3992212016-02-13 03:00:53 +0000109class Random {
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000110 public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000111 Random(unsigned int seed) : R(seed) {}
112 size_t Rand() { return R(); }
113 size_t RandBool() { return Rand() % 2; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000114 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000115 std::mt19937 &Get_mt19937() { return R; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000116 private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000117 std::mt19937 R;
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000118};
119
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000120// Dictionary.
121
122// Parses one dictionary entry.
123// If successfull, write the enty to Unit and returns true,
124// otherwise returns false.
125bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
126// Parses the dictionary file, fills Units, returns true iff all lines
127// were parsed succesfully.
128bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
129
Kostya Serebryany292cf032016-02-13 03:37:24 +0000130class DictionaryEntry {
131 public:
132 DictionaryEntry() {}
133 DictionaryEntry(Word W) : W(W) {}
134 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
135 const Word &GetW() const { return W; }
136
137 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
138 size_t GetPositionHint() const {
139 assert(HasPositionHint());
140 return PositionHint;
141 }
142 void IncUseCount() { UseCount++; }
143 void IncSuccessCount() { SuccessCount++; }
144 size_t GetUseCount() const { return UseCount; }
145 size_t GetSuccessCount() const {return SuccessCount; }
146
147private:
148 Word W;
149 size_t PositionHint = std::numeric_limits<size_t>::max();
150 size_t UseCount = 0;
151 size_t SuccessCount = 0;
152};
153
154class Dictionary {
155 public:
156 static const size_t kMaxDictSize = 1 << 14;
157
158 bool ContainsWord(const Word &W) const {
159 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
160 return DE.GetW() == W;
161 });
162 }
163 const DictionaryEntry *begin() const { return &DE[0]; }
164 const DictionaryEntry *end() const { return begin() + Size; }
165 DictionaryEntry & operator[] (size_t Idx) {
166 assert(Idx < Size);
167 return DE[Idx];
168 }
169 void push_back(DictionaryEntry DE) {
170 if (Size < kMaxDictSize)
171 this->DE[Size++] = DE;
172 }
173 void clear() { Size = 0; }
174 bool empty() const { return Size == 0; }
175 size_t size() const { return Size; }
176
177private:
178 DictionaryEntry DE[kMaxDictSize];
179 size_t Size = 0;
180};
181
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000182class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000183public:
Kostya Serebryany23194962016-02-13 03:46:26 +0000184 MutationDispatcher(Random &Rand) : Rand(Rand) {}
Kostya Serebryany292cf032016-02-13 03:37:24 +0000185 ~MutationDispatcher() {}
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000186 /// Indicate that we are about to start a new sequence of mutations.
187 void StartMutationSequence();
188 /// Print the current sequence of mutations.
189 void PrintMutationSequence();
190 /// Indicate that the current sequence of mutations was successfull.
191 void RecordSuccessfulMutationSequence();
192 /// Mutates data by shuffling bytes.
193 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
194 /// Mutates data by erasing a byte.
195 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
196 /// Mutates data by inserting a byte.
197 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
198 /// Mutates data by chanding one byte.
199 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
200 /// Mutates data by chanding one bit.
201 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
202
203 /// Mutates data by adding a word from the manual dictionary.
204 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
205 size_t MaxSize);
206
207 /// Mutates data by adding a word from the temporary automatic dictionary.
208 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
209 size_t MaxSize);
210
211 /// Mutates data by adding a word from the persistent automatic dictionary.
212 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
213 size_t MaxSize);
214
215 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
216 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
217
218 /// CrossOver Data with some other element of the corpus.
219 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
220
221 /// Applies one of the above mutations.
222 /// Returns the new size of data which could be up to MaxSize.
223 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
224
225 /// Creates a cross-over of two pieces of Data, returns its size.
226 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
227 size_t Size2, uint8_t *Out, size_t MaxOutSize);
228
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000229 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000230
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000231 void AddWordToAutoDictionary(const Word &W, size_t PositionHint);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000232 void ClearAutoDictionary();
233 void PrintRecommendedDictionary();
234
Kostya Serebryany292cf032016-02-13 03:37:24 +0000235 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000236
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000237 Random &GetRand() { return Rand; }
238
Ivan Krasindf919102016-01-22 22:28:27 +0000239private:
Kostya Serebryany292cf032016-02-13 03:37:24 +0000240
241 struct Mutator {
242 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
243 const char *Name;
244 };
245
Kostya Serebryany292cf032016-02-13 03:37:24 +0000246 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
247 size_t MaxSize);
248
Kostya Serebryanya3992212016-02-13 03:00:53 +0000249 Random &Rand;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000250 // Dictionary provided by the user via -dict=DICT_FILE.
251 Dictionary ManualDictionary;
252 // Temporary dictionary modified by the fuzzer itself,
253 // recreated periodically.
254 Dictionary TempAutoDictionary;
255 // Persistent dictionary modified by the fuzzer, consists of
256 // entries that led to successfull discoveries in the past mutations.
257 Dictionary PersistentAutoDictionary;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000258 std::vector<Mutator> CurrentMutatorSequence;
259 std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
260 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000261 std::vector<uint8_t> MutateInPlaceHere;
Kostya Serebryany23194962016-02-13 03:46:26 +0000262
263 static Mutator Mutators[];
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000264};
265
Aaron Ballmanef116982015-01-29 16:58:29 +0000266class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000267public:
Aaron Ballmanef116982015-01-29 16:58:29 +0000268 struct FuzzingOptions {
269 int Verbosity = 1;
270 int MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000271 int UnitTimeoutSec = 300;
Kostya Serebryany9768e7f2016-01-23 19:34:19 +0000272 bool AbortOnTimeout = false;
Kostya Serebryany54a63632016-01-29 23:30:07 +0000273 int TimeoutExitCode = 77;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000274 int MaxTotalTimeSec = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000275 bool DoCrossOver = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000276 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +0000277 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000278 bool UseCounters = false;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000279 bool UseIndirCalls = true;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000280 bool UseTraces = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000281 bool UseMemcmp = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000282 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000283 bool Reload = true;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000284 bool ShuffleAtStartUp = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +0000285 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +0000286 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000287 int SyncTimeout = 600;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000288 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000289 bool OnlyASCII = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000290 std::string OutputCorpus;
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000291 std::string SyncCommand;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000292 std::string ArtifactPrefix = "./";
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000293 std::string ExactArtifactPath;
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000294 bool SaveArtifacts = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000295 bool PrintNEW = true; // Print a status line when new units are found;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000296 bool OutputCSV = false;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000297 bool PrintNewCovPcs = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000298 };
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000299 Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
Ivan Krasindf919102016-01-22 22:28:27 +0000300 void AddToCorpus(const Unit &U) {
301 Corpus.push_back(U);
302 UpdateCorpusDistribution();
303 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000304 size_t ChooseUnitIdxToMutate();
305 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Kostya Serebryany468ed782015-09-08 17:30:35 +0000306 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000307 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000308 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000309 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000310 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000311 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000312 void ReadDir(const std::string &Path, long *Epoch, size_t MaxSize) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000313 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000314 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch, MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000315 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000316 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +0000317 // Save the current corpus to OutputCorpus.
318 void SaveCorpus();
319
Kostya Serebryany92e04762015-02-04 23:42:42 +0000320 size_t secondsSinceProcessStartUp() {
321 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
322 .count();
323 }
324
325 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
326
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000327 static void StaticAlarmCallback();
328
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000329 void ExecuteCallback(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000330
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000331 // Merge Corpora[1:] into Corpora[0].
332 void Merge(const std::vector<std::string> &Corpora);
Kostya Serebryany1deb0492016-02-13 06:24:18 +0000333 MutationDispatcher &GetMD() { return MD; }
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000334
Ivan Krasindf919102016-01-22 22:28:27 +0000335private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000336 void AlarmCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000337 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000338 void ReportNewCoverage(const Unit &U);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000339 bool RunOne(const uint8_t *Data, size_t Size);
340 bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
341 void RunOneAndUpdateCorpus(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000342 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000343 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000344 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000345 void PrintStatusForNewUnit(const Unit &U);
Ivan Krasindf919102016-01-22 22:28:27 +0000346 // Updates the probability distribution for the units in the corpus.
347 // Must be called whenever the corpus or unit weights are changed.
348 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000349
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000350 void SyncCorpus();
351
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000352 size_t RecordBlockCoverage();
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000353 size_t RecordCallerCalleeCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000354 void PrepareCoverageBeforeRun();
355 bool CheckCoverageAfterRun();
356
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000357 // Trace-based fuzzing: we run a unit with some kind of tracing
358 // enabled and record potentially useful mutations. Then
359 // We apply these mutations one by one to the unit and run it again.
360
361 // Start tracing; forget all previously proposed mutations.
362 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000363 // Stop tracing.
364 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000365
Aaron Ballmanef116982015-01-29 16:58:29 +0000366 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000367 static void StaticDeathCallback();
368 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000369
370 uint8_t *CurrentUnitData;
371 size_t CurrentUnitSize;
Aaron Ballmanef116982015-01-29 16:58:29 +0000372
373 size_t TotalNumberOfRuns = 0;
Kostya Serebryany12c78372015-08-12 01:55:37 +0000374 size_t TotalNumberOfExecutedTraceBasedMutations = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000375
376 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000377 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000378
379 // For UseCounters
380 std::vector<uint8_t> CounterBitmap;
Ivan Krasindf919102016-01-22 22:28:27 +0000381 size_t TotalBits() { // Slow. Call it only for printing stats.
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000382 size_t Res = 0;
Ivan Krasindf919102016-01-22 22:28:27 +0000383 for (auto x : CounterBitmap)
384 Res += __builtin_popcount(x);
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000385 return Res;
386 }
387
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000388 std::vector<uint8_t> MutateInPlaceHere;
389
Ivan Krasindf919102016-01-22 22:28:27 +0000390 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000391 UserCallback CB;
392 MutationDispatcher &MD;
Aaron Ballmanef116982015-01-29 16:58:29 +0000393 FuzzingOptions Options;
394 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000395 system_clock::time_point LastExternalSync = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000396 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000397 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000398 long EpochOfLastReadOfOutputCorpus = 0;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000399 size_t LastRecordedBlockCoverage = 0;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000400 size_t LastRecordedPcMapSize = 0;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000401 size_t LastRecordedCallerCalleeCoverage = 0;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000402 size_t LastCoveragePcBufferLen = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000403};
404
Ivan Krasindf919102016-01-22 22:28:27 +0000405}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000406
407#endif // LLVM_FUZZER_INTERNAL_H