blob: bc6bec7473d31e2ae9cf8b3b401d07f15fdb1cd0 [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 {
27typedef std::vector<uint8_t> Unit;
28using namespace std::chrono;
29
Kostya Serebryany52a788e2015-03-31 20:13:20 +000030std::string FileToString(const std::string &Path);
31Unit FileToVector(const std::string &Path);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000032void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
33 long *Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000034void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000035void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000036// Returns "Dir/FileName" or equivalent for the current OS.
37std::string DirPlusFile(const std::string &DirPath,
38 const std::string &FileName);
39
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000040void Printf(const char *Fmt, ...);
Aaron Ballmanef116982015-01-29 16:58:29 +000041void Print(const Unit &U, const char *PrintAfter = "");
42void PrintASCII(const Unit &U, const char *PrintAfter = "");
43std::string Hash(const Unit &U);
44void SetTimer(int Seconds);
Kostya Serebryany9e48cda2015-12-04 22:29:39 +000045std::string Base64(const Unit &U);
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000046int ExecuteCommand(const std::string &Command);
Aaron Ballmanef116982015-01-29 16:58:29 +000047
Kostya Serebryany96eab652015-05-14 22:41:49 +000048// Private copy of SHA1 implementation.
49static const int kSHA1NumBytes = 20;
50// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
51void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
52
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000053// Changes U to contain only ASCII (isprint+isspace) characters.
54// Returns true iff U has been changed.
55bool ToASCII(Unit &U);
Kostya Serebryanya9346c22015-09-02 19:08:08 +000056bool IsASCII(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000057
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000058int NumberOfCpuCores();
Kostya Serebryanyd6edce92015-10-16 23:04:31 +000059int GetPid();
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000060
Kostya Serebryany9838b2b2015-09-03 20:23:46 +000061// Dictionary.
62
63// Parses one dictionary entry.
64// If successfull, write the enty to Unit and returns true,
65// otherwise returns false.
66bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
67// Parses the dictionary file, fills Units, returns true iff all lines
68// were parsed succesfully.
69bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
70
Aaron Ballmanef116982015-01-29 16:58:29 +000071class Fuzzer {
72 public:
73 struct FuzzingOptions {
74 int Verbosity = 1;
75 int MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +000076 int UnitTimeoutSec = 300;
Kostya Serebryanyb85db172015-10-02 20:47:55 +000077 int MaxTotalTimeSec = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +000078 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +000079 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +000080 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +000081 bool UseCounters = false;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +000082 bool UseIndirCalls = true;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000083 bool UseTraces = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000084 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000085 bool Reload = true;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +000086 bool ShuffleAtStartUp = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +000087 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +000088 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany2da7b842015-05-18 21:34:20 +000089 int SyncTimeout = 600;
Kostya Serebryany70926ae2015-08-05 21:43:48 +000090 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000091 bool OnlyASCII = false;
Kostya Serebryany12c78372015-08-12 01:55:37 +000092 int TBMDepth = 10;
93 int TBMWidth = 10;
Aaron Ballmanef116982015-01-29 16:58:29 +000094 std::string OutputCorpus;
Kostya Serebryany2da7b842015-05-18 21:34:20 +000095 std::string SyncCommand;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +000096 std::string ArtifactPrefix = "./";
Kostya Serebryany2d0ef142015-11-25 21:40:46 +000097 std::string ExactArtifactPath;
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000098 bool SaveArtifacts = true;
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000099 bool PrintNEW = true; // Print a status line when new units are found;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000100 bool OutputCSV = false;
Aaron Ballmanef116982015-01-29 16:58:29 +0000101 };
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000102 Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
Aaron Ballmanef116982015-01-29 16:58:29 +0000103 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000104 size_t ChooseUnitIdxToMutate();
105 const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
Kostya Serebryany468ed782015-09-08 17:30:35 +0000106 void Loop();
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000107 void Drill();
Aaron Ballmanef116982015-01-29 16:58:29 +0000108 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +0000109 void InitializeTraceState();
Aaron Ballmanef116982015-01-29 16:58:29 +0000110 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000111 void ReadDir(const std::string &Path, long *Epoch) {
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000112 Printf("Loading corpus: %s\n", Path.c_str());
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000113 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +0000114 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000115 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +0000116 // Save the current corpus to OutputCorpus.
117 void SaveCorpus();
118
Kostya Serebryany92e04762015-02-04 23:42:42 +0000119 size_t secondsSinceProcessStartUp() {
120 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
121 .count();
122 }
123
124 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
125
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000126 static void StaticAlarmCallback();
127
Ivan Krasin95e82d52015-10-01 23:23:06 +0000128 void ExecuteCallback(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000129
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000130 // Merge Corpora[1:] into Corpora[0].
131 void Merge(const std::vector<std::string> &Corpora);
132
Aaron Ballmanef116982015-01-29 16:58:29 +0000133 private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000134 void AlarmCallback();
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000135 void MutateAndTestOne(Unit *U);
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000136 void ReportNewCoverage(const Unit &U);
137 bool RunOne(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000138 void RunOneAndUpdateCorpus(Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000139 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000140 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany09d2a5f2015-10-22 22:56:45 +0000141 void PrintStats(const char *Where, const char *End = "\n");
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000142 void PrintStatusForNewUnit(const Unit &U);
Kostya Serebryanyb3602562015-10-22 21:48:09 +0000143 void PrintUnitInASCII(const Unit &U, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000144
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000145 void SyncCorpus();
146
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000147 size_t RecordBlockCoverage();
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000148 size_t RecordCallerCalleeCoverage();
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000149 void PrepareCoverageBeforeRun();
150 bool CheckCoverageAfterRun();
151
152
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000153 // Trace-based fuzzing: we run a unit with some kind of tracing
154 // enabled and record potentially useful mutations. Then
155 // We apply these mutations one by one to the unit and run it again.
156
157 // Start tracing; forget all previously proposed mutations.
158 void StartTraceRecording();
159 // Stop tracing and return the number of proposed mutations.
160 size_t StopTraceRecording();
161 // Apply Idx-th trace-based mutation to U.
162 void ApplyTraceBasedMutation(size_t Idx, Unit *U);
163
Aaron Ballmanef116982015-01-29 16:58:29 +0000164 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000165 static void StaticDeathCallback();
166 void DeathCallback();
167 Unit CurrentUnit;
Aaron Ballmanef116982015-01-29 16:58:29 +0000168
169 size_t TotalNumberOfRuns = 0;
Kostya Serebryany12c78372015-08-12 01:55:37 +0000170 size_t TotalNumberOfExecutedTraceBasedMutations = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000171
172 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000173 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000174
175 // For UseCounters
176 std::vector<uint8_t> CounterBitmap;
177 size_t TotalBits() { // Slow. Call it only for printing stats.
178 size_t Res = 0;
179 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
180 return Res;
181 }
182
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000183 UserSuppliedFuzzer &USF;
Aaron Ballmanef116982015-01-29 16:58:29 +0000184 FuzzingOptions Options;
185 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000186 system_clock::time_point LastExternalSync = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000187 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000188 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000189 long EpochOfLastReadOfOutputCorpus = 0;
Kostya Serebryany007c9b22015-10-22 22:50:47 +0000190 size_t LastRecordedBlockCoverage = 0;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000191 size_t LastRecordedCallerCalleeCoverage = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000192};
193
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000194class SimpleUserSuppliedFuzzer: public UserSuppliedFuzzer {
195 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000196 SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
197 : UserSuppliedFuzzer(Rand), Callback(Callback) {}
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000198
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000199 virtual int TargetFunction(const uint8_t *Data, size_t Size) override {
Kostya Serebryany94660b32015-10-23 18:37:58 +0000200 return Callback(Data, Size);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000201 }
202
203 private:
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000204 UserCallback Callback = nullptr;
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000205};
206
Aaron Ballmanef116982015-01-29 16:58:29 +0000207}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000208
209#endif // LLVM_FUZZER_INTERNAL_H