blob: b2a62dd7932795081d02ae80e28b7a63646e80e2 [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
Aaron Ballmanef116982015-01-29 16:58:29 +000015#include <cassert>
16#include <chrono>
Ivan Krasindf919102016-01-22 22:28:27 +000017#include <climits>
Aaron Ballmanef116982015-01-29 16:58:29 +000018#include <cstddef>
19#include <cstdlib>
Ivan Krasindf919102016-01-22 22:28:27 +000020#include <random>
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000021#include <string.h>
Ivan Krasindf919102016-01-22 22:28:27 +000022#include <string>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000023#include <unordered_set>
Ivan Krasindf919102016-01-22 22:28:27 +000024#include <vector>
Aaron Ballmanef116982015-01-29 16:58:29 +000025
Kostya Serebryany016852c2015-02-19 18:45:37 +000026#include "FuzzerInterface.h"
27
Aaron Ballmanef116982015-01-29 16:58:29 +000028namespace fuzzer {
Aaron Ballmanef116982015-01-29 16:58:29 +000029using namespace std::chrono;
Kostya Serebryanyaca76962016-01-16 01:23:12 +000030typedef std::vector<uint8_t> Unit;
Aaron Ballmanef116982015-01-29 16:58:29 +000031
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000032// A simple POD sized array of bytes.
Ivan Krasindf919102016-01-22 22:28:27 +000033template <size_t kMaxSize> class FixedWord {
34public:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000035 FixedWord() {}
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000036 FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
37
38 void Set(const uint8_t *B, uint8_t S) {
39 assert(S <= kMaxSize);
40 memcpy(Data, B, S);
41 Size = S;
42 }
43
Ivan Krasindf919102016-01-22 22:28:27 +000044 bool operator==(const FixedWord<kMaxSize> &w) const {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000045 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
46 }
47
Ivan Krasindf919102016-01-22 22:28:27 +000048 bool operator<(const FixedWord<kMaxSize> &w) const {
49 if (Size != w.Size)
50 return Size < w.Size;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000051 return memcmp(Data, w.Data, Size) < 0;
52 }
53
54 static size_t GetMaxSize() { return kMaxSize; }
55 const uint8_t *data() const { return Data; }
56 uint8_t size() const { return Size; }
57
Ivan Krasindf919102016-01-22 22:28:27 +000058private:
Kostya Serebryany160dcba2016-01-22 23:55:14 +000059 uint8_t Size = 0;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000060 uint8_t Data[kMaxSize];
61};
62
Ivan Krasindf919102016-01-22 22:28:27 +000063typedef FixedWord<27> Word; // 28 bytes.
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000064
Kostya Serebryany52a788e2015-03-31 20:13:20 +000065std::string FileToString(const std::string &Path);
66Unit FileToVector(const std::string &Path);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000067void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
68 long *Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000069void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000070void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000071// Returns "Dir/FileName" or equivalent for the current OS.
72std::string DirPlusFile(const std::string &DirPath,
73 const std::string &FileName);
74
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000075void Printf(const char *Fmt, ...);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000076void PrintHexArray(const Unit &U, const char *PrintAfter = "");
77void PrintHexArray(const uint8_t *Data, size_t Size,
78 const char *PrintAfter = "");
Kostya Serebryany41740052016-01-12 02:36:59 +000079void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000080void PrintASCII(const Unit &U, const char *PrintAfter = "");
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000081void PrintASCII(const Word &W, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000082std::string Hash(const Unit &U);
83void SetTimer(int Seconds);
Kostya Serebryany9e48cda2015-12-04 22:29:39 +000084std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000085int ExecuteCommand(const std::string &Command);
Aaron Ballmanef116982015-01-29 16:58:29 +000086
Kostya Serebryany96eab652015-05-14 22:41:49 +000087// Private copy of SHA1 implementation.
88static const int kSHA1NumBytes = 20;
89// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
90void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
91
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000092// Changes U to contain only ASCII (isprint+isspace) characters.
93// Returns true iff U has been changed.
94bool ToASCII(Unit &U);
Kostya Serebryanya9346c22015-09-02 19:08:08 +000095bool IsASCII(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000096
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000097int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +000098int GetPid();
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000099
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000100// Dictionary.
101
102// Parses one dictionary entry.
103// If successfull, write the enty to Unit and returns true,
104// otherwise returns false.
105bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
106// Parses the dictionary file, fills Units, returns true iff all lines
107// were parsed succesfully.
108bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
109
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000110class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000111public:
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000112 MutationDispatcher(FuzzerRandomBase &Rand);
113 ~MutationDispatcher();
114 /// Indicate that we are about to start a new sequence of mutations.
115 void StartMutationSequence();
116 /// Print the current sequence of mutations.
117 void PrintMutationSequence();
118 /// Indicate that the current sequence of mutations was successfull.
119 void RecordSuccessfulMutationSequence();
120 /// Mutates data by shuffling bytes.
121 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
122 /// Mutates data by erasing a byte.
123 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
124 /// Mutates data by inserting a byte.
125 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
126 /// Mutates data by chanding one byte.
127 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
128 /// Mutates data by chanding one bit.
129 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
130
131 /// Mutates data by adding a word from the manual dictionary.
132 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
133 size_t MaxSize);
134
135 /// Mutates data by adding a word from the temporary automatic dictionary.
136 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
137 size_t MaxSize);
138
139 /// Mutates data by adding a word from the persistent automatic dictionary.
140 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
141 size_t MaxSize);
142
143 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
144 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
145
146 /// CrossOver Data with some other element of the corpus.
147 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
148
149 /// Applies one of the above mutations.
150 /// Returns the new size of data which could be up to MaxSize.
151 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
152
153 /// Creates a cross-over of two pieces of Data, returns its size.
154 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
155 size_t Size2, uint8_t *Out, size_t MaxOutSize);
156
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000157 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000158
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000159 void AddWordToAutoDictionary(const Word &W, size_t PositionHint);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000160 void ClearAutoDictionary();
161 void PrintRecommendedDictionary();
162
163 void SetCorpus(const std::vector<Unit> *Corpus);
164
Ivan Krasindf919102016-01-22 22:28:27 +0000165private:
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000166 FuzzerRandomBase &Rand;
167 struct Impl;
168 Impl *MDImpl;
169};
170
Aaron Ballmanef116982015-01-29 16:58:29 +0000171class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000172public:
Aaron Ballmanef116982015-01-29 16:58:29 +0000173 struct FuzzingOptions {
174 int Verbosity = 1;
175 int MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000176 int UnitTimeoutSec = 300;
Kostya Serebryany9768e7f2016-01-23 19:34:19 +0000177 bool AbortOnTimeout = false;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000178 int MaxTotalTimeSec = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000179 bool DoCrossOver = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000180 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +0000181 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000182 bool UseCounters = false;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000183 bool UseIndirCalls = true;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000184 bool UseTraces = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000185 bool UseMemcmp = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000186 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000187 bool Reload = true;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000188 bool ShuffleAtStartUp = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +0000189 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +0000190 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000191 int SyncTimeout = 600;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000192 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000193 bool OnlyASCII = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000194 std::string OutputCorpus;
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000195 std::string SyncCommand;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000196 std::string ArtifactPrefix = "./";
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000197 std::string ExactArtifactPath;
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000198 bool SaveArtifacts = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000199 bool PrintNEW = true; // Print a status line when new units are found;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000200 bool OutputCSV = false;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000201 bool PrintNewCovPcs = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000202 };
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000203 Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
Ivan Krasindf919102016-01-22 22:28:27 +0000204 void AddToCorpus(const Unit &U) {
205 Corpus.push_back(U);
206 UpdateCorpusDistribution();
207 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000208 size_t ChooseUnitIdxToMutate();
209 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Kostya Serebryany468ed782015-09-08 17:30:35 +0000210 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000211 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000212 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000213 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000214 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000215 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000216 void ReadDir(const std::string &Path, long *Epoch) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000217 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000218 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +0000219 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000220 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +0000221 // Save the current corpus to OutputCorpus.
222 void SaveCorpus();
223
Kostya Serebryany92e04762015-02-04 23:42:42 +0000224 size_t secondsSinceProcessStartUp() {
225 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
226 .count();
227 }
228
229 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
230
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000231 static void StaticAlarmCallback();
232
Ivan Krasin95e82d52015-10-01 23:23:06 +0000233 void ExecuteCallback(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000234
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000235 // Merge Corpora[1:] into Corpora[0].
236 void Merge(const std::vector<std::string> &Corpora);
237
Ivan Krasindf919102016-01-22 22:28:27 +0000238private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000239 void AlarmCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000240 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000241 void ReportNewCoverage(const Unit &U);
242 bool RunOne(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000243 void RunOneAndUpdateCorpus(Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000244 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000245 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000246 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000247 void PrintStatusForNewUnit(const Unit &U);
Ivan Krasindf919102016-01-22 22:28:27 +0000248 // Updates the probability distribution for the units in the corpus.
249 // Must be called whenever the corpus or unit weights are changed.
250 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000251
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000252 void SyncCorpus();
253
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000254 size_t RecordBlockCoverage();
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000255 size_t RecordCallerCalleeCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000256 void PrepareCoverageBeforeRun();
257 bool CheckCoverageAfterRun();
258
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000259 // Trace-based fuzzing: we run a unit with some kind of tracing
260 // enabled and record potentially useful mutations. Then
261 // We apply these mutations one by one to the unit and run it again.
262
263 // Start tracing; forget all previously proposed mutations.
264 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000265 // Stop tracing.
266 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000267
Aaron Ballmanef116982015-01-29 16:58:29 +0000268 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000269 static void StaticDeathCallback();
270 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000271
272 uint8_t *CurrentUnitData;
273 size_t CurrentUnitSize;
Aaron Ballmanef116982015-01-29 16:58:29 +0000274
275 size_t TotalNumberOfRuns = 0;
Kostya Serebryany12c78372015-08-12 01:55:37 +0000276 size_t TotalNumberOfExecutedTraceBasedMutations = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000277
278 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000279 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000280
281 // For UseCounters
282 std::vector<uint8_t> CounterBitmap;
Ivan Krasindf919102016-01-22 22:28:27 +0000283 size_t TotalBits() { // Slow. Call it only for printing stats.
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000284 size_t Res = 0;
Ivan Krasindf919102016-01-22 22:28:27 +0000285 for (auto x : CounterBitmap)
286 Res += __builtin_popcount(x);
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000287 return Res;
288 }
289
Ivan Krasindf919102016-01-22 22:28:27 +0000290 // TODO(krasin): remove GetRand from UserSuppliedFuzzer,
291 // and fully rely on the generator and the seed.
292 // The user supplied fuzzer will have a way to access the
293 // generator for its own purposes (like seeding the custom
294 // PRNG).
295 std::mt19937 Generator;
296 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000297 UserSuppliedFuzzer &USF;
Aaron Ballmanef116982015-01-29 16:58:29 +0000298 FuzzingOptions Options;
299 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000300 system_clock::time_point LastExternalSync = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000301 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000302 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000303 long EpochOfLastReadOfOutputCorpus = 0;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000304 size_t LastRecordedBlockCoverage = 0;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000305 size_t LastRecordedCallerCalleeCoverage = 0;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000306 size_t LastCoveragePcBufferLen = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000307};
308
Ivan Krasindf919102016-01-22 22:28:27 +0000309class SimpleUserSuppliedFuzzer : public UserSuppliedFuzzer {
310public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000311 SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
312 : UserSuppliedFuzzer(Rand), Callback(Callback) {}
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000313
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000314 virtual int TargetFunction(const uint8_t *Data, size_t Size) override {
Kostya Serebryany94660b32015-10-23 18:37:58 +0000315 return Callback(Data, Size);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000316 }
317
Ivan Krasindf919102016-01-22 22:28:27 +0000318private:
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000319 UserCallback Callback = nullptr;
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000320};
321
Ivan Krasindf919102016-01-22 22:28:27 +0000322}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000323
324#endif // LLVM_FUZZER_INTERNAL_H