blob: 5db60598eef4efe8fbfff1b64936aa6b1bc2579c [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 Serebryanybfbe7fc2016-02-02 03:03:47 +000065bool IsFile(const std::string &Path);
Kostya Serebryany52a788e2015-03-31 20:13:20 +000066std::string FileToString(const std::string &Path);
67Unit FileToVector(const std::string &Path);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000068void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
69 long *Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000070void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000071void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000072// Returns "Dir/FileName" or equivalent for the current OS.
73std::string DirPlusFile(const std::string &DirPath,
74 const std::string &FileName);
75
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000076void Printf(const char *Fmt, ...);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000077void PrintHexArray(const Unit &U, const char *PrintAfter = "");
78void PrintHexArray(const uint8_t *Data, size_t Size,
79 const char *PrintAfter = "");
Kostya Serebryany41740052016-01-12 02:36:59 +000080void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000081void PrintASCII(const Unit &U, const char *PrintAfter = "");
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000082void PrintASCII(const Word &W, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000083std::string Hash(const Unit &U);
84void SetTimer(int Seconds);
Kostya Serebryany9e48cda2015-12-04 22:29:39 +000085std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000086int ExecuteCommand(const std::string &Command);
Aaron Ballmanef116982015-01-29 16:58:29 +000087
Kostya Serebryany96eab652015-05-14 22:41:49 +000088// Private copy of SHA1 implementation.
89static const int kSHA1NumBytes = 20;
90// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
91void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
92
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000093// Changes U to contain only ASCII (isprint+isspace) characters.
94// Returns true iff U has been changed.
95bool ToASCII(Unit &U);
Kostya Serebryanya9346c22015-09-02 19:08:08 +000096bool IsASCII(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000097
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000098int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +000099int GetPid();
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000100
Kostya Serebryanya3992212016-02-13 03:00:53 +0000101class Random {
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000102 public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000103 Random(unsigned int seed) : R(seed) {}
104 size_t Rand() { return R(); }
105 size_t RandBool() { return Rand() % 2; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000106 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000107 std::mt19937 &Get_mt19937() { return R; }
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000108 private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000109 std::mt19937 R;
Kostya Serebryanyecab57b2016-02-13 02:39:30 +0000110};
111
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000112// Dictionary.
113
114// Parses one dictionary entry.
115// If successfull, write the enty to Unit and returns true,
116// otherwise returns false.
117bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
118// Parses the dictionary file, fills Units, returns true iff all lines
119// were parsed succesfully.
120bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
121
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000122class MutationDispatcher {
Ivan Krasindf919102016-01-22 22:28:27 +0000123public:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000124 MutationDispatcher(Random &Rand);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000125 ~MutationDispatcher();
126 /// Indicate that we are about to start a new sequence of mutations.
127 void StartMutationSequence();
128 /// Print the current sequence of mutations.
129 void PrintMutationSequence();
130 /// Indicate that the current sequence of mutations was successfull.
131 void RecordSuccessfulMutationSequence();
132 /// Mutates data by shuffling bytes.
133 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
134 /// Mutates data by erasing a byte.
135 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
136 /// Mutates data by inserting a byte.
137 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
138 /// Mutates data by chanding one byte.
139 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
140 /// Mutates data by chanding one bit.
141 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
142
143 /// Mutates data by adding a word from the manual dictionary.
144 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
145 size_t MaxSize);
146
147 /// Mutates data by adding a word from the temporary automatic dictionary.
148 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
149 size_t MaxSize);
150
151 /// Mutates data by adding a word from the persistent automatic dictionary.
152 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
153 size_t MaxSize);
154
155 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
156 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
157
158 /// CrossOver Data with some other element of the corpus.
159 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
160
161 /// Applies one of the above mutations.
162 /// Returns the new size of data which could be up to MaxSize.
163 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
164
165 /// Creates a cross-over of two pieces of Data, returns its size.
166 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
167 size_t Size2, uint8_t *Out, size_t MaxOutSize);
168
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000169 void AddWordToManualDictionary(const Word &W);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000170
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000171 void AddWordToAutoDictionary(const Word &W, size_t PositionHint);
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000172 void ClearAutoDictionary();
173 void PrintRecommendedDictionary();
174
175 void SetCorpus(const std::vector<Unit> *Corpus);
176
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000177 Random &GetRand() { return Rand; }
178
Ivan Krasindf919102016-01-22 22:28:27 +0000179private:
Kostya Serebryanya3992212016-02-13 03:00:53 +0000180 Random &Rand;
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000181 struct Impl;
182 Impl *MDImpl;
183};
184
Aaron Ballmanef116982015-01-29 16:58:29 +0000185class Fuzzer {
Ivan Krasindf919102016-01-22 22:28:27 +0000186public:
Aaron Ballmanef116982015-01-29 16:58:29 +0000187 struct FuzzingOptions {
188 int Verbosity = 1;
189 int MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000190 int UnitTimeoutSec = 300;
Kostya Serebryany9768e7f2016-01-23 19:34:19 +0000191 bool AbortOnTimeout = false;
Kostya Serebryany54a63632016-01-29 23:30:07 +0000192 int TimeoutExitCode = 77;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000193 int MaxTotalTimeSec = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000194 bool DoCrossOver = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000195 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +0000196 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000197 bool UseCounters = false;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000198 bool UseIndirCalls = true;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000199 bool UseTraces = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000200 bool UseMemcmp = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000201 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000202 bool Reload = true;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000203 bool ShuffleAtStartUp = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +0000204 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +0000205 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000206 int SyncTimeout = 600;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000207 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000208 bool OnlyASCII = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000209 std::string OutputCorpus;
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000210 std::string SyncCommand;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000211 std::string ArtifactPrefix = "./";
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000212 std::string ExactArtifactPath;
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000213 bool SaveArtifacts = true;
Ivan Krasindf919102016-01-22 22:28:27 +0000214 bool PrintNEW = true; // Print a status line when new units are found;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000215 bool OutputCSV = false;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000216 bool PrintNewCovPcs = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000217 };
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000218 Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
Ivan Krasindf919102016-01-22 22:28:27 +0000219 void AddToCorpus(const Unit &U) {
220 Corpus.push_back(U);
221 UpdateCorpusDistribution();
222 }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000223 size_t ChooseUnitIdxToMutate();
224 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Kostya Serebryany468ed782015-09-08 17:30:35 +0000225 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000226 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000227 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000228 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000229 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000230 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000231 void ReadDir(const std::string &Path, long *Epoch) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000232 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000233 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +0000234 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000235 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +0000236 // Save the current corpus to OutputCorpus.
237 void SaveCorpus();
238
Kostya Serebryany92e04762015-02-04 23:42:42 +0000239 size_t secondsSinceProcessStartUp() {
240 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
241 .count();
242 }
243
244 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
245
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000246 static void StaticAlarmCallback();
247
Ivan Krasin95e82d52015-10-01 23:23:06 +0000248 void ExecuteCallback(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000249
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000250 // Merge Corpora[1:] into Corpora[0].
251 void Merge(const std::vector<std::string> &Corpora);
252
Ivan Krasindf919102016-01-22 22:28:27 +0000253private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000254 void AlarmCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000255 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000256 void ReportNewCoverage(const Unit &U);
257 bool RunOne(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000258 void RunOneAndUpdateCorpus(Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000259 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000260 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000261 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000262 void PrintStatusForNewUnit(const Unit &U);
Ivan Krasindf919102016-01-22 22:28:27 +0000263 // Updates the probability distribution for the units in the corpus.
264 // Must be called whenever the corpus or unit weights are changed.
265 void UpdateCorpusDistribution();
Aaron Ballmanef116982015-01-29 16:58:29 +0000266
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000267 void SyncCorpus();
268
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000269 size_t RecordBlockCoverage();
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000270 size_t RecordCallerCalleeCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000271 void PrepareCoverageBeforeRun();
272 bool CheckCoverageAfterRun();
273
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000274 // Trace-based fuzzing: we run a unit with some kind of tracing
275 // enabled and record potentially useful mutations. Then
276 // We apply these mutations one by one to the unit and run it again.
277
278 // Start tracing; forget all previously proposed mutations.
279 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000280 // Stop tracing.
281 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000282
Aaron Ballmanef116982015-01-29 16:58:29 +0000283 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000284 static void StaticDeathCallback();
285 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000286
287 uint8_t *CurrentUnitData;
288 size_t CurrentUnitSize;
Aaron Ballmanef116982015-01-29 16:58:29 +0000289
290 size_t TotalNumberOfRuns = 0;
Kostya Serebryany12c78372015-08-12 01:55:37 +0000291 size_t TotalNumberOfExecutedTraceBasedMutations = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000292
293 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000294 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000295
296 // For UseCounters
297 std::vector<uint8_t> CounterBitmap;
Ivan Krasindf919102016-01-22 22:28:27 +0000298 size_t TotalBits() { // Slow. Call it only for printing stats.
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000299 size_t Res = 0;
Ivan Krasindf919102016-01-22 22:28:27 +0000300 for (auto x : CounterBitmap)
301 Res += __builtin_popcount(x);
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000302 return Res;
303 }
304
Ivan Krasindf919102016-01-22 22:28:27 +0000305 std::piecewise_constant_distribution<double> CorpusDistribution;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000306 UserCallback CB;
307 MutationDispatcher &MD;
Aaron Ballmanef116982015-01-29 16:58:29 +0000308 FuzzingOptions Options;
309 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000310 system_clock::time_point LastExternalSync = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000311 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000312 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000313 long EpochOfLastReadOfOutputCorpus = 0;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000314 size_t LastRecordedBlockCoverage = 0;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000315 size_t LastRecordedCallerCalleeCoverage = 0;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000316 size_t LastCoveragePcBufferLen = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000317};
318
Ivan Krasindf919102016-01-22 22:28:27 +0000319}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000320
321#endif // LLVM_FUZZER_INTERNAL_H