blob: 6fb429e705e579620401100efb1e4b524a5eac8a [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>
Ivan Krasindf919102016-01-22 22:28:27 +000022#include <random>
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000023#include <string.h>
Ivan Krasindf919102016-01-22 22:28:27 +000024#include <string>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000025#include <unordered_set>
Ivan Krasindf919102016-01-22 22:28:27 +000026#include <vector>
Aaron Ballmanef116982015-01-29 16:58:29 +000027
Dan Liewd3c33112016-06-02 05:48:02 +000028#include "FuzzerExtFunctions.h"
Kostya Serebryany016852c2015-02-19 18:45:37 +000029#include "FuzzerInterface.h"
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000030#include "FuzzerTracePC.h"
Kostya Serebryany016852c2015-02-19 18:45:37 +000031
Dan Liew3868e462016-05-19 22:00:33 +000032// Platform detection.
33#ifdef __linux__
34#define LIBFUZZER_LINUX 1
35#define LIBFUZZER_APPLE 0
36#elif __APPLE__
37#define LIBFUZZER_LINUX 0
38#define LIBFUZZER_APPLE 1
39#else
40#error "Support for your platform has not been implemented"
41#endif
42
Aaron Ballmanef116982015-01-29 16:58:29 +000043namespace fuzzer {
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +000044
45typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
Dan Liewd3c33112016-06-02 05:48:02 +000046int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
Kostya Serebryany8b0d90a2016-05-13 18:04:35 +000047
Aaron Ballmanef116982015-01-29 16:58:29 +000048using namespace std::chrono;
Kostya Serebryanyaca76962016-01-16 01:23:12 +000049typedef std::vector<uint8_t> Unit;
Kostya Serebryany945761b2016-03-18 00:23:29 +000050typedef std::vector<Unit> UnitVector;
Aaron Ballmanef116982015-01-29 16:58:29 +000051
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000052// A simple POD sized array of bytes.
Ivan Krasindf919102016-01-22 22:28:27 +000053template <size_t kMaxSize> class FixedWord {
54public:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000055 FixedWord() {}
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000056 FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
57
58 void Set(const uint8_t *B, uint8_t S) {
59 assert(S <= kMaxSize);
60 memcpy(Data, B, S);
61 Size = S;
62 }
63
Ivan Krasindf919102016-01-22 22:28:27 +000064 bool operator==(const FixedWord<kMaxSize> &w) const {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000065 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
66 }
67
Ivan Krasindf919102016-01-22 22:28:27 +000068 bool operator<(const FixedWord<kMaxSize> &w) const {
69 if (Size != w.Size)
70 return Size < w.Size;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000071 return memcmp(Data, w.Data, Size) < 0;
72 }
73
74 static size_t GetMaxSize() { return kMaxSize; }
75 const uint8_t *data() const { return Data; }
76 uint8_t size() const { return Size; }
77
Ivan Krasindf919102016-01-22 22:28:27 +000078private:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000079 uint8_t Size = 0;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000080 uint8_t Data[kMaxSize];
81};
82
Ivan Krasindf919102016-01-22 22:28:27 +000083typedef FixedWord<27> Word; // 28 bytes.
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000084
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000085bool IsFile(const std::string &Path);
Kostya Serebryany52a788e2015-03-31 20:13:20 +000086std::string FileToString(const std::string &Path);
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000087Unit FileToVector(const std::string &Path, size_t MaxSize = 0);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000088void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000089 long *Epoch, size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +000090void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000091void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000092// Returns "Dir/FileName" or equivalent for the current OS.
93std::string DirPlusFile(const std::string &DirPath,
94 const std::string &FileName);
95
Kostya Serebryany49e40902016-03-18 20:58:29 +000096void DupAndCloseStderr();
97void CloseStdout();
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000098void Printf(const char *Fmt, ...);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000099void PrintHexArray(const Unit &U, const char *PrintAfter = "");
100void PrintHexArray(const uint8_t *Data, size_t Size,
101 const char *PrintAfter = "");
Kostya Serebryany41740052016-01-12 02:36:59 +0000102void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000103void PrintASCII(const Unit &U, const char *PrintAfter = "");
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000104void PrintASCII(const Word &W, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000105std::string Hash(const Unit &U);
106void SetTimer(int Seconds);
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000107void SetSigSegvHandler();
108void SetSigBusHandler();
109void SetSigAbrtHandler();
110void SetSigIllHandler();
111void SetSigFpeHandler();
112void SetSigIntHandler();
Kostya Serebryanyf389ae12016-03-24 21:03:58 +0000113void SetSigTermHandler();
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000114std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000115int ExecuteCommand(const std::string &Command);
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000116size_t GetPeakRSSMb();
Aaron Ballmanef116982015-01-29 16:58:29 +0000117
Kostya Serebryany96eab652015-05-14 22:41:49 +0000118// Private copy of SHA1 implementation.
119static const int kSHA1NumBytes = 20;
120// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
121void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
122
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000123// Changes U to contain only ASCII (isprint+isspace) characters.
124// Returns true iff U has been changed.
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000125bool ToASCII(uint8_t *Data, size_t Size);
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000126bool IsASCII(const Unit &U);
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000127bool IsASCII(const uint8_t *Data, size_t Size);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000128
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000129int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000130int GetPid();
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000131void SleepSeconds(int Seconds);
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000132
Kostya Serebryanya3992212016-02-13 03:00:53 +0000133class Random {
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000134 public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000135 Random(unsigned int seed) : R(seed) {}
136 size_t Rand() { return R(); }
137 size_t RandBool() { return Rand() % 2; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000138 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000139 std::mt19937 &Get_mt19937() { return R; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000140 private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000141 std::mt19937 R;
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000142};
143
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000144// Dictionary.
145
146// Parses one dictionary entry.
147// If successfull, write the enty to Unit and returns true,
148// otherwise returns false.
149bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
150// Parses the dictionary file, fills Units, returns true iff all lines
151// were parsed succesfully.
152bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
153
Kostya Serebryany292cf032016-02-13 03:37:24 +0000154class DictionaryEntry {
155 public:
156 DictionaryEntry() {}
157 DictionaryEntry(Word W) : W(W) {}
158 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
159 const Word &GetW() const { return W; }
160
161 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
162 size_t GetPositionHint() const {
163 assert(HasPositionHint());
164 return PositionHint;
165 }
166 void IncUseCount() { UseCount++; }
167 void IncSuccessCount() { SuccessCount++; }
168 size_t GetUseCount() const { return UseCount; }
169 size_t GetSuccessCount() const {return SuccessCount; }
170
171private:
172 Word W;
173 size_t PositionHint = std::numeric_limits<size_t>::max();
174 size_t UseCount = 0;
175 size_t SuccessCount = 0;
176};
177
178class Dictionary {
179 public:
180 static const size_t kMaxDictSize = 1 << 14;
181
182 bool ContainsWord(const Word &W) const {
183 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
184 return DE.GetW() == W;
185 });
186 }
187 const DictionaryEntry *begin() const { return &DE[0]; }
188 const DictionaryEntry *end() const { return begin() + Size; }
189 DictionaryEntry & operator[] (size_t Idx) {
190 assert(Idx < Size);
191 return DE[Idx];
192 }
193 void push_back(DictionaryEntry DE) {
194 if (Size < kMaxDictSize)
195 this->DE[Size++] = DE;
196 }
197 void clear() { Size = 0; }
198 bool empty() const { return Size == 0; }
199 size_t size() const { return Size; }
200
201private:
202 DictionaryEntry DE[kMaxDictSize];
203 size_t Size = 0;
204};
205
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000206class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000207public:
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000208 MutationDispatcher(Random &Rand);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000209 ~MutationDispatcher() {}
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000210 /// Indicate that we are about to start a new sequence of mutations.
211 void StartMutationSequence();
212 /// Print the current sequence of mutations.
213 void PrintMutationSequence();
214 /// Indicate that the current sequence of mutations was successfull.
215 void RecordSuccessfulMutationSequence();
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000216 /// Mutates data by invoking user-provided mutator.
217 size_t Mutate_Custom(uint8_t *Data, size_t Size, size_t MaxSize);
Mike Aizatsky41d66832016-06-07 20:22:15 +0000218 /// Mutates data by invoking user-provided crossover.
219 size_t Mutate_CustomCrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000220 /// Mutates data by shuffling bytes.
221 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
222 /// Mutates data by erasing a byte.
223 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
224 /// Mutates data by inserting a byte.
225 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
226 /// Mutates data by chanding one byte.
227 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
228 /// Mutates data by chanding one bit.
229 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
230
231 /// Mutates data by adding a word from the manual dictionary.
232 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
233 size_t MaxSize);
234
235 /// Mutates data by adding a word from the temporary automatic dictionary.
236 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
237 size_t MaxSize);
238
239 /// Mutates data by adding a word from the persistent automatic dictionary.
240 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
241 size_t MaxSize);
242
243 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
244 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
245
246 /// CrossOver Data with some other element of the corpus.
247 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
248
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000249 /// Applies one of the configured mutations.
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000250 /// Returns the new size of data which could be up to MaxSize.
251 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000252 /// Applies one of the default mutations. Provided as a service
253 /// to mutation authors.
254 size_t DefaultMutate(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000255
256 /// Creates a cross-over of two pieces of Data, returns its size.
257 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
258 size_t Size2, uint8_t *Out, size_t MaxOutSize);
259
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000260 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000261
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000262 void AddWordToAutoDictionary(const Word &W, size_t PositionHint);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000263 void ClearAutoDictionary();
264 void PrintRecommendedDictionary();
265
Kostya Serebryany292cf032016-02-13 03:37:24 +0000266 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000267
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000268 Random &GetRand() { return Rand; }
269
Ivan Krasindf919102016-01-22 22:28:27 +0000270private:
Kostya Serebryany292cf032016-02-13 03:37:24 +0000271
272 struct Mutator {
273 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
274 const char *Name;
275 };
276
Kostya Serebryany292cf032016-02-13 03:37:24 +0000277 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
278 size_t MaxSize);
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000279 size_t MutateImpl(uint8_t *Data, size_t Size, size_t MaxSize,
280 const std::vector<Mutator> &Mutators);
281
Kostya Serebryanya3992212016-02-13 03:00:53 +0000282 Random &Rand;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000283 // Dictionary provided by the user via -dict=DICT_FILE.
284 Dictionary ManualDictionary;
285 // Temporary dictionary modified by the fuzzer itself,
286 // recreated periodically.
287 Dictionary TempAutoDictionary;
288 // Persistent dictionary modified by the fuzzer, consists of
289 // entries that led to successfull discoveries in the past mutations.
290 Dictionary PersistentAutoDictionary;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000291 std::vector<Mutator> CurrentMutatorSequence;
292 std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
293 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000294 std::vector<uint8_t> MutateInPlaceHere;
Kostya Serebryany23194962016-02-13 03:46:26 +0000295
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000296 std::vector<Mutator> Mutators;
297 std::vector<Mutator> DefaultMutators;
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000298};
299
Aaron Ballmanef116982015-01-29 16:58:29 +0000300class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000301public:
Aaron Ballmanef116982015-01-29 16:58:29 +0000302 struct FuzzingOptions {
303 int Verbosity = 1;
Kostya Serebryany64d24572016-03-12 01:57:04 +0000304 size_t MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000305 int UnitTimeoutSec = 300;
Kostya Serebryany54a63632016-01-29 23:30:07 +0000306 int TimeoutExitCode = 77;
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000307 int ErrorExitCode = 77;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000308 int MaxTotalTimeSec = 0;
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000309 int RssLimitMb = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000310 bool DoCrossOver = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000311 int MutateDepth = 5;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000312 bool UseCounters = false;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000313 bool UseIndirCalls = true;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000314 bool UseTraces = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000315 bool UseMemcmp = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000316 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000317 bool Reload = true;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000318 bool ShuffleAtStartUp = true;
Kostya Serebryany945761b2016-03-18 00:23:29 +0000319 bool PreferSmall = true;
Kostya Serebryany33f86692015-02-04 22:20:09 +0000320 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000321 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000322 bool OnlyASCII = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000323 std::string OutputCorpus;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000324 std::string ArtifactPrefix = "./";
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000325 std::string ExactArtifactPath;
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000326 bool SaveArtifacts = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000327 bool PrintNEW = true; // Print a status line when new units are found;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000328 bool OutputCSV = false;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000329 bool PrintNewCovPcs = false;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000330 bool PrintFinalStats = false;
Kostya Serebryany2fe93042016-04-29 18:49:55 +0000331 bool DetectLeaks = true;
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000332 bool TruncateUnits = false;
Mike Aizatsky1f88b122016-06-07 18:16:32 +0000333 bool PruneCorpus = true;
Aaron Ballmanef116982015-01-29 16:58:29 +0000334 };
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000335
336 // Aggregates all available coverage measurements.
337 struct Coverage {
338 Coverage() { Reset(); }
339
340 void Reset() {
341 BlockCoverage = 0;
342 CallerCalleeCoverage = 0;
343 PcMapBits = 0;
344 CounterBitmapBits = 0;
345 PcBufferLen = 0;
346 CounterBitmap.clear();
347 PCMap.Reset();
348 }
349
350 std::string DebugString() const;
351
352 size_t BlockCoverage;
353 size_t CallerCalleeCoverage;
354
355 size_t PcBufferLen;
356 // Precalculated number of bits in CounterBitmap.
357 size_t CounterBitmapBits;
358 std::vector<uint8_t> CounterBitmap;
359 // Precalculated number of bits in PCMap.
360 size_t PcMapBits;
361 PcCoverageMap PCMap;
362 };
363
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000364 Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
Ivan Krasindf919102016-01-22 22:28:27 +0000365 void AddToCorpus(const Unit &U) {
366 Corpus.push_back(U);
367 UpdateCorpusDistribution();
368 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000369 size_t ChooseUnitIdxToMutate();
370 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000371 void TruncateUnits(std::vector<Unit> *NewCorpus);
Kostya Serebryany468ed782015-09-08 17:30:35 +0000372 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000373 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000374 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000375 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000376 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000377 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000378 size_t MaxUnitSizeInCorpus() const;
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000379 void ReadDir(const std::string &Path, long *Epoch, size_t MaxSize) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000380 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000381 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch, MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000382 }
Kostya Serebryany64d24572016-03-12 01:57:04 +0000383 void RereadOutputCorpus(size_t MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000384 // Save the current corpus to OutputCorpus.
385 void SaveCorpus();
386
Kostya Serebryany92e04762015-02-04 23:42:42 +0000387 size_t secondsSinceProcessStartUp() {
388 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
389 .count();
390 }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000391 size_t execPerSec() {
392 size_t Seconds = secondsSinceProcessStartUp();
393 return Seconds ? TotalNumberOfRuns / Seconds : 0;
394 }
Kostya Serebryany92e04762015-02-04 23:42:42 +0000395
396 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
397
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000398 static void StaticAlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000399 static void StaticCrashSignalCallback();
400 static void StaticInterruptCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000401
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000402 void ExecuteCallback(const uint8_t *Data, size_t Size);
Kostya Serebryanybaf7fd02016-05-04 20:44:50 +0000403 bool RunOne(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000404
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000405 // Merge Corpora[1:] into Corpora[0].
406 void Merge(const std::vector<std::string> &Corpora);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000407 // Returns a subset of 'Extra' that adds coverage to 'Initial'.
408 UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
Kostya Serebryany1deb0492016-02-13 06:24:18 +0000409 MutationDispatcher &GetMD() { return MD; }
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000410 void PrintFinalStats();
Kostya Serebryany64d24572016-03-12 01:57:04 +0000411 void SetMaxLen(size_t MaxLen);
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000412 void RssLimitCallback();
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000413
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000414 // Public for tests.
415 void ResetCoverage();
416
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000417 bool InFuzzingThread() const { return IsMyThread; }
Kostya Serebryanyd8384122016-05-26 22:17:32 +0000418 size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000419
Ivan Krasindf919102016-01-22 22:28:27 +0000420private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000421 void AlarmCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000422 void CrashCallback();
423 void InterruptCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000424 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000425 void ReportNewCoverage(const Unit &U);
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000426 bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000427 void RunOneAndUpdateCorpus(const uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000428 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000429 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000430 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000431 void PrintStatusForNewUnit(const Unit &U);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000432 void ShuffleCorpus(UnitVector *V);
Kostya Serebryany4b923262016-05-26 20:25:49 +0000433 void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
434 bool DuringInitialCorpusExecution);
Kostya Serebryany945761b2016-03-18 00:23:29 +0000435
Ivan Krasindf919102016-01-22 22:28:27 +0000436 // Updates the probability distribution for the units in the corpus.
437 // Must be called whenever the corpus or unit weights are changed.
438 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000439
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000440 bool UpdateMaxCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000441
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000442 // Trace-based fuzzing: we run a unit with some kind of tracing
443 // enabled and record potentially useful mutations. Then
444 // We apply these mutations one by one to the unit and run it again.
445
446 // Start tracing; forget all previously proposed mutations.
447 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000448 // Stop tracing.
449 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000450
Aaron Ballmanef116982015-01-29 16:58:29 +0000451 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000452 static void StaticDeathCallback();
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000453 void DumpCurrentUnit(const char *Prefix);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000454 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000455
Kostya Serebryany8fc3a272016-05-27 00:21:33 +0000456 void LazyAllocateCurrentUnitData();
457 uint8_t *CurrentUnitData = nullptr;
Kostya Serebryany0edb5632016-05-27 00:54:15 +0000458 std::atomic<size_t> CurrentUnitSize;
Aaron Ballmanef116982015-01-29 16:58:29 +0000459
460 size_t TotalNumberOfRuns = 0;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000461 size_t NumberOfNewUnitsAdded = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000462
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000463 bool HasMoreMallocsThanFrees = false;
Kostya Serebryany7018a1a2016-04-27 19:52:34 +0000464 size_t NumberOfLeakDetectionAttempts = 0;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000465
Aaron Ballmanef116982015-01-29 16:58:29 +0000466 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000467 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000468
Ivan Krasindf919102016-01-22 22:28:27 +0000469 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000470 UserCallback CB;
471 MutationDispatcher &MD;
Aaron Ballmanef116982015-01-29 16:58:29 +0000472 FuzzingOptions Options;
473 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000474 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000475 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000476 long EpochOfLastReadOfOutputCorpus = 0;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +0000477
478 // Maximum recorded coverage.
479 Coverage MaxCoverage;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000480
481 // Need to know our own thread.
482 static thread_local bool IsMyThread;
Aaron Ballmanef116982015-01-29 16:58:29 +0000483};
484
Dan Liew1873a492016-06-07 23:32:50 +0000485// Global interface to functions that may or may not be available.
486extern ExternalFunctions *EF;
487
Ivan Krasindf919102016-01-22 22:28:27 +0000488}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000489
490#endif // LLVM_FUZZER_INTERNAL_H