blob: 6078867661189bba53928c90e959ee4aa0bec2f5 [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>
Kostya Serebryany33f86692015-02-04 22:20:09 +000016#include <climits>
Aaron Ballmanef116982015-01-29 16:58:29 +000017#include <chrono>
18#include <cstddef>
19#include <cstdlib>
20#include <string>
21#include <vector>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000022#include <unordered_set>
Aaron Ballmanef116982015-01-29 16:58:29 +000023
Kostya Serebryany016852c2015-02-19 18:45:37 +000024#include "FuzzerInterface.h"
25
Aaron Ballmanef116982015-01-29 16:58:29 +000026namespace fuzzer {
Aaron Ballmanef116982015-01-29 16:58:29 +000027using namespace std::chrono;
28
Kostya Serebryany52a788e2015-03-31 20:13:20 +000029std::string FileToString(const std::string &Path);
30Unit FileToVector(const std::string &Path);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000031void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
32 long *Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000033void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000034void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000035// Returns "Dir/FileName" or equivalent for the current OS.
36std::string DirPlusFile(const std::string &DirPath,
37 const std::string &FileName);
38
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000039void Printf(const char *Fmt, ...);
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000040void PrintHexArray(const Unit &U, const char *PrintAfter = "");
41void PrintHexArray(const uint8_t *Data, size_t Size,
42 const char *PrintAfter = "");
Kostya Serebryany41740052016-01-12 02:36:59 +000043void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000044void PrintASCII(const Unit &U, const char *PrintAfter = "");
45std::string Hash(const Unit &U);
46void SetTimer(int Seconds);
Kostya Serebryany9e48cda2015-12-04 22:29:39 +000047std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000048int ExecuteCommand(const std::string &Command);
Aaron Ballmanef116982015-01-29 16:58:29 +000049
Kostya Serebryany96eab652015-05-14 22:41:49 +000050// Private copy of SHA1 implementation.
51static const int kSHA1NumBytes = 20;
52// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
53void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
54
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000055// Changes U to contain only ASCII (isprint+isspace) characters.
56// Returns true iff U has been changed.
57bool ToASCII(Unit &U);
Kostya Serebryanya9346c22015-09-02 19:08:08 +000058bool IsASCII(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000059
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000060int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +000061int GetPid();
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000062
Kostya Serebryany9838b2b2015-09-03 20:23:46 +000063// Dictionary.
64
65// Parses one dictionary entry.
66// If successfull, write the enty to Unit and returns true,
67// otherwise returns false.
68bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
69// Parses the dictionary file, fills Units, returns true iff all lines
70// were parsed succesfully.
71bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
72
Kostya Serebryany628bc3e2016-01-16 00:04:36 +000073class MutationDispatcher {
74 public:
75 MutationDispatcher(FuzzerRandomBase &Rand);
76 ~MutationDispatcher();
77 /// Indicate that we are about to start a new sequence of mutations.
78 void StartMutationSequence();
79 /// Print the current sequence of mutations.
80 void PrintMutationSequence();
81 /// Indicate that the current sequence of mutations was successfull.
82 void RecordSuccessfulMutationSequence();
83 /// Mutates data by shuffling bytes.
84 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
85 /// Mutates data by erasing a byte.
86 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
87 /// Mutates data by inserting a byte.
88 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
89 /// Mutates data by chanding one byte.
90 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
91 /// Mutates data by chanding one bit.
92 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
93
94 /// Mutates data by adding a word from the manual dictionary.
95 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
96 size_t MaxSize);
97
98 /// Mutates data by adding a word from the temporary automatic dictionary.
99 size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
100 size_t MaxSize);
101
102 /// Mutates data by adding a word from the persistent automatic dictionary.
103 size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
104 size_t MaxSize);
105
106 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
107 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
108
109 /// CrossOver Data with some other element of the corpus.
110 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
111
112 /// Applies one of the above mutations.
113 /// Returns the new size of data which could be up to MaxSize.
114 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
115
116 /// Creates a cross-over of two pieces of Data, returns its size.
117 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
118 size_t Size2, uint8_t *Out, size_t MaxOutSize);
119
120 void AddWordToManualDictionary(const Unit &Word);
121
122 void AddWordToAutoDictionary(const Unit &Word, size_t PositionHint);
123 void ClearAutoDictionary();
124 void PrintRecommendedDictionary();
125
126 void SetCorpus(const std::vector<Unit> *Corpus);
127
128 private:
129 FuzzerRandomBase &Rand;
130 struct Impl;
131 Impl *MDImpl;
132};
133
Aaron Ballmanef116982015-01-29 16:58:29 +0000134class Fuzzer {
135 public:
136 struct FuzzingOptions {
137 int Verbosity = 1;
138 int MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000139 int UnitTimeoutSec = 300;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000140 int MaxTotalTimeSec = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000141 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000142 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +0000143 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000144 bool UseCounters = false;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000145 bool UseIndirCalls = true;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000146 bool UseTraces = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000147 bool UseMemcmp = true;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000148 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000149 bool Reload = true;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000150 bool ShuffleAtStartUp = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +0000151 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +0000152 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000153 int SyncTimeout = 600;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000154 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000155 bool OnlyASCII = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000156 std::string OutputCorpus;
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000157 std::string SyncCommand;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000158 std::string ArtifactPrefix = "./";
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000159 std::string ExactArtifactPath;
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000160 bool SaveArtifacts = true;
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000161 bool PrintNEW = true; // Print a status line when new units are found;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000162 bool OutputCSV = false;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000163 bool PrintNewCovPcs = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000164 };
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000165 Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
Aaron Ballmanef116982015-01-29 16:58:29 +0000166 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000167 size_t ChooseUnitIdxToMutate();
168 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Kostya Serebryany468ed782015-09-08 17:30:35 +0000169 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000170 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000171 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000172 void InitializeTraceState();
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000173 void AssignTaintLabels(uint8_t *Data, size_t Size);
Aaron Ballmanef116982015-01-29 16:58:29 +0000174 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000175 void ReadDir(const std::string &Path, long *Epoch) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000176 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000177 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +0000178 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000179 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +0000180 // Save the current corpus to OutputCorpus.
181 void SaveCorpus();
182
Kostya Serebryany92e04762015-02-04 23:42:42 +0000183 size_t secondsSinceProcessStartUp() {
184 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
185 .count();
186 }
187
188 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
189
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000190 static void StaticAlarmCallback();
191
Ivan Krasin95e82d52015-10-01 23:23:06 +0000192 void ExecuteCallback(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000193
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000194 // Merge Corpora[1:] into Corpora[0].
195 void Merge(const std::vector<std::string> &Corpora);
196
Aaron Ballmanef116982015-01-29 16:58:29 +0000197 private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000198 void AlarmCallback();
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000199 void MutateAndTestOne();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000200 void ReportNewCoverage(const Unit &U);
201 bool RunOne(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000202 void RunOneAndUpdateCorpus(Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000203 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000204 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000205 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000206 void PrintStatusForNewUnit(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000207
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000208 void SyncCorpus();
209
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000210 size_t RecordBlockCoverage();
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000211 size_t RecordCallerCalleeCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000212 void PrepareCoverageBeforeRun();
213 bool CheckCoverageAfterRun();
214
215
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000216 // Trace-based fuzzing: we run a unit with some kind of tracing
217 // enabled and record potentially useful mutations. Then
218 // We apply these mutations one by one to the unit and run it again.
219
220 // Start tracing; forget all previously proposed mutations.
221 void StartTraceRecording();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000222 // Stop tracing.
223 void StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000224
Aaron Ballmanef116982015-01-29 16:58:29 +0000225 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000226 static void StaticDeathCallback();
227 void DeathCallback();
Kostya Serebryany98abb2c2016-01-13 23:46:01 +0000228
229 uint8_t *CurrentUnitData;
230 size_t CurrentUnitSize;
Aaron Ballmanef116982015-01-29 16:58:29 +0000231
232 size_t TotalNumberOfRuns = 0;
Kostya Serebryany12c78372015-08-12 01:55:37 +0000233 size_t TotalNumberOfExecutedTraceBasedMutations = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000234
235 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000236 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000237
238 // For UseCounters
239 std::vector<uint8_t> CounterBitmap;
240 size_t TotalBits() { // Slow. Call it only for printing stats.
241 size_t Res = 0;
242 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
243 return Res;
244 }
245
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000246 UserSuppliedFuzzer &USF;
Aaron Ballmanef116982015-01-29 16:58:29 +0000247 FuzzingOptions Options;
248 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000249 system_clock::time_point LastExternalSync = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000250 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000251 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000252 long EpochOfLastReadOfOutputCorpus = 0;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000253 size_t LastRecordedBlockCoverage = 0;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000254 size_t LastRecordedCallerCalleeCoverage = 0;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000255 size_t LastCoveragePcBufferLen = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000256};
257
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000258class SimpleUserSuppliedFuzzer: public UserSuppliedFuzzer {
259 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000260 SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
261 : UserSuppliedFuzzer(Rand), Callback(Callback) {}
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000262
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000263 virtual int TargetFunction(const uint8_t *Data, size_t Size) override {
Kostya Serebryany94660b32015-10-23 18:37:58 +0000264 return Callback(Data, Size);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000265 }
266
267 private:
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000268 UserCallback Callback = nullptr;
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000269};
270
Aaron Ballmanef116982015-01-29 16:58:29 +0000271}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000272
273#endif // LLVM_FUZZER_INTERNAL_H