blob: 610b4375f24266f6a3d0ec62caca6b8386ff676b [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//===----------------------------------------------------------------------===//
11#include <cassert>
Kostya Serebryany33f86692015-02-04 22:20:09 +000012#include <climits>
Aaron Ballmanef116982015-01-29 16:58:29 +000013#include <chrono>
14#include <cstddef>
15#include <cstdlib>
16#include <string>
17#include <vector>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000018#include <unordered_set>
Aaron Ballmanef116982015-01-29 16:58:29 +000019
Kostya Serebryany016852c2015-02-19 18:45:37 +000020#include "FuzzerInterface.h"
21
Aaron Ballmanef116982015-01-29 16:58:29 +000022namespace fuzzer {
23typedef std::vector<uint8_t> Unit;
24using namespace std::chrono;
25
Kostya Serebryany52a788e2015-03-31 20:13:20 +000026std::string FileToString(const std::string &Path);
27Unit FileToVector(const std::string &Path);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000028void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
29 long *Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000030void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000031void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000032// Returns "Dir/FileName" or equivalent for the current OS.
33std::string DirPlusFile(const std::string &DirPath,
34 const std::string &FileName);
35
Kostya Serebryany86a5fba2015-08-01 02:23:06 +000036size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize,
Kostya Serebryany8ce74242015-08-01 01:42:51 +000037 FuzzerRandomBase &Rand);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +000038size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize,
39 FuzzerRandomBase &Rand);
40size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize,
41 FuzzerRandomBase &Rand);
42size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize,
43 FuzzerRandomBase &Rand);
Kostya Serebryany404c69f2015-07-24 01:06:40 +000044size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
45 FuzzerRandomBase &Rand);
Aaron Ballmanef116982015-01-29 16:58:29 +000046
Kostya Serebryanyf3424592015-05-22 22:35:31 +000047size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
Kostya Serebryany404c69f2015-07-24 01:06:40 +000048 size_t Size2, uint8_t *Out, size_t MaxOutSize,
49 FuzzerRandomBase &Rand);
Aaron Ballmanef116982015-01-29 16:58:29 +000050
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000051void Printf(const char *Fmt, ...);
Aaron Ballmanef116982015-01-29 16:58:29 +000052void Print(const Unit &U, const char *PrintAfter = "");
53void PrintASCII(const Unit &U, const char *PrintAfter = "");
54std::string Hash(const Unit &U);
55void SetTimer(int Seconds);
Kostya Serebryanyca6a2a22015-05-05 21:59:51 +000056void PrintFileAsBase64(const std::string &Path);
Kostya Serebryany2da7b842015-05-18 21:34:20 +000057void ExecuteCommand(const std::string &Command);
Aaron Ballmanef116982015-01-29 16:58:29 +000058
Kostya Serebryany96eab652015-05-14 22:41:49 +000059// Private copy of SHA1 implementation.
60static const int kSHA1NumBytes = 20;
61// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
62void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
63
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000064int NumberOfCpuCores();
65
Aaron Ballmanef116982015-01-29 16:58:29 +000066class Fuzzer {
67 public:
68 struct FuzzingOptions {
69 int Verbosity = 1;
70 int MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +000071 int UnitTimeoutSec = 300;
Aaron Ballmanef116982015-01-29 16:58:29 +000072 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +000073 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +000074 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +000075 bool UseCounters = false;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000076 bool UseTraces = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000077 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000078 bool Reload = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +000079 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +000080 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany2da7b842015-05-18 21:34:20 +000081 int SyncTimeout = 600;
Kostya Serebryany70926ae2015-08-05 21:43:48 +000082 int ReportSlowUnits = 10;
Aaron Ballmanef116982015-01-29 16:58:29 +000083 std::string OutputCorpus;
Kostya Serebryany2da7b842015-05-18 21:34:20 +000084 std::string SyncCommand;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000085 std::vector<std::string> Tokens;
Aaron Ballmanef116982015-01-29 16:58:29 +000086 };
Kostya Serebryanyf3424592015-05-22 22:35:31 +000087 Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
Aaron Ballmanef116982015-01-29 16:58:29 +000088 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
Kostya Serebryany7d470cf2015-05-07 18:32:29 +000089 void Loop(size_t NumIterations);
Aaron Ballmanef116982015-01-29 16:58:29 +000090 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +000091 void InitializeTraceState();
Aaron Ballmanef116982015-01-29 16:58:29 +000092 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000093 void ReadDir(const std::string &Path, long *Epoch) {
94 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000095 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000096 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +000097 // Save the current corpus to OutputCorpus.
98 void SaveCorpus();
99
Kostya Serebryany92e04762015-02-04 23:42:42 +0000100 size_t secondsSinceProcessStartUp() {
101 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
102 .count();
103 }
104
105 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
106
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000107 static void StaticAlarmCallback();
108
109 Unit SubstituteTokens(const Unit &U) const;
Aaron Ballmanef116982015-01-29 16:58:29 +0000110
111 private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000112 void AlarmCallback();
113 void ExecuteCallback(const Unit &U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000114 void MutateAndTestOne(Unit *U);
115 void ReportNewCoverage(size_t NewCoverage, const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000116 size_t RunOne(const Unit &U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000117 void RunOneAndUpdateCorpus(const Unit &U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000118 size_t RunOneMaximizeTotalCoverage(const Unit &U);
119 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000120 size_t RunOneMaximizeCoveragePairs(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000121 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000122 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000123 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000124 void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000125
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000126 void SyncCorpus();
127
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000128 // Trace-based fuzzing: we run a unit with some kind of tracing
129 // enabled and record potentially useful mutations. Then
130 // We apply these mutations one by one to the unit and run it again.
131
132 // Start tracing; forget all previously proposed mutations.
133 void StartTraceRecording();
134 // Stop tracing and return the number of proposed mutations.
135 size_t StopTraceRecording();
136 // Apply Idx-th trace-based mutation to U.
137 void ApplyTraceBasedMutation(size_t Idx, Unit *U);
138
Aaron Ballmanef116982015-01-29 16:58:29 +0000139 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000140 static void StaticDeathCallback();
141 void DeathCallback();
142 Unit CurrentUnit;
Aaron Ballmanef116982015-01-29 16:58:29 +0000143
144 size_t TotalNumberOfRuns = 0;
145
146 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000147 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000148 std::unordered_set<uintptr_t> FullCoverageSets;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000149
150 // For UseCounters
151 std::vector<uint8_t> CounterBitmap;
152 size_t TotalBits() { // Slow. Call it only for printing stats.
153 size_t Res = 0;
154 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
155 return Res;
156 }
157
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000158 UserSuppliedFuzzer &USF;
Aaron Ballmanef116982015-01-29 16:58:29 +0000159 FuzzingOptions Options;
160 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000161 system_clock::time_point LastExternalSync = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000162 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000163 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000164 long EpochOfLastReadOfOutputCorpus = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000165};
166
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000167class SimpleUserSuppliedFuzzer: public UserSuppliedFuzzer {
168 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000169 SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
170 : UserSuppliedFuzzer(Rand), Callback(Callback) {}
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000171 virtual void TargetFunction(const uint8_t *Data, size_t Size) {
172 return Callback(Data, Size);
173 }
174
175 private:
176 UserCallback Callback;
177};
178
Aaron Ballmanef116982015-01-29 16:58:29 +0000179}; // namespace fuzzer