blob: 89261895579aca2dc5a735d44730768f0e02e757 [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>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000019#include <set>
Aaron Ballmanef116982015-01-29 16:58:29 +000020
Kostya Serebryany016852c2015-02-19 18:45:37 +000021#include "FuzzerInterface.h"
22
Aaron Ballmanef116982015-01-29 16:58:29 +000023namespace fuzzer {
24typedef std::vector<uint8_t> Unit;
25using namespace std::chrono;
26
Kostya Serebryany52a788e2015-03-31 20:13:20 +000027std::string FileToString(const std::string &Path);
28Unit FileToVector(const std::string &Path);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000029void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
30 long *Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000031void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000032void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000033// Returns "Dir/FileName" or equivalent for the current OS.
34std::string DirPlusFile(const std::string &DirPath,
35 const std::string &FileName);
36
37void Mutate(Unit *U, size_t MaxLen);
38
39void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
40
41void 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 Serebryanyca6a2a22015-05-05 21:59:51 +000045void PrintFileAsBase64(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000046
Kostya Serebryany96eab652015-05-14 22:41:49 +000047// Private copy of SHA1 implementation.
48static const int kSHA1NumBytes = 20;
49// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
50void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
51
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000052int NumberOfCpuCores();
53
Aaron Ballmanef116982015-01-29 16:58:29 +000054class Fuzzer {
55 public:
56 struct FuzzingOptions {
57 int Verbosity = 1;
58 int MaxLen = 0;
59 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +000060 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +000061 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +000062 bool UseCounters = false;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000063 bool UseTraces = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000064 bool UseFullCoverageSet = false;
Kostya Serebryany2e3622b2015-02-20 03:02:37 +000065 bool UseCoveragePairs = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000066 bool Reload = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +000067 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +000068 size_t MaxNumberOfRuns = ULONG_MAX;
Aaron Ballmanef116982015-01-29 16:58:29 +000069 std::string OutputCorpus;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000070 std::vector<std::string> Tokens;
Aaron Ballmanef116982015-01-29 16:58:29 +000071 };
Kostya Serebryany52a788e2015-03-31 20:13:20 +000072 Fuzzer(UserCallback Callback, FuzzingOptions Options);
Aaron Ballmanef116982015-01-29 16:58:29 +000073 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
Kostya Serebryany7d470cf2015-05-07 18:32:29 +000074 void Loop(size_t NumIterations);
Aaron Ballmanef116982015-01-29 16:58:29 +000075 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +000076 void InitializeTraceState();
Aaron Ballmanef116982015-01-29 16:58:29 +000077 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000078 void ReadDir(const std::string &Path, long *Epoch) {
79 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000080 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000081 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +000082 // Save the current corpus to OutputCorpus.
83 void SaveCorpus();
84
Kostya Serebryany92e04762015-02-04 23:42:42 +000085 size_t secondsSinceProcessStartUp() {
86 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
87 .count();
88 }
89
90 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
91
Kostya Serebryany52a788e2015-03-31 20:13:20 +000092 static void StaticAlarmCallback();
93
94 Unit SubstituteTokens(const Unit &U) const;
Aaron Ballmanef116982015-01-29 16:58:29 +000095
96 private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +000097 void AlarmCallback();
98 void ExecuteCallback(const Unit &U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +000099 void MutateAndTestOne(Unit *U);
100 void ReportNewCoverage(size_t NewCoverage, const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000101 size_t RunOne(const Unit &U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000102 void RunOneAndUpdateCorpus(const Unit &U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000103 size_t RunOneMaximizeTotalCoverage(const Unit &U);
104 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000105 size_t RunOneMaximizeCoveragePairs(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000106 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000107 void WriteToCrash(const Unit &U, const char *Prefix);
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000108 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000109 void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000110
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000111 // Trace-based fuzzing: we run a unit with some kind of tracing
112 // enabled and record potentially useful mutations. Then
113 // We apply these mutations one by one to the unit and run it again.
114
115 // Start tracing; forget all previously proposed mutations.
116 void StartTraceRecording();
117 // Stop tracing and return the number of proposed mutations.
118 size_t StopTraceRecording();
119 // Apply Idx-th trace-based mutation to U.
120 void ApplyTraceBasedMutation(size_t Idx, Unit *U);
121
Aaron Ballmanef116982015-01-29 16:58:29 +0000122 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000123 static void StaticDeathCallback();
124 void DeathCallback();
125 Unit CurrentUnit;
Aaron Ballmanef116982015-01-29 16:58:29 +0000126
127 size_t TotalNumberOfRuns = 0;
128
129 std::vector<Unit> Corpus;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000130 std::set<Unit> UnitsAddedAfterInitialLoad;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000131 std::unordered_set<uintptr_t> FullCoverageSets;
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000132 std::unordered_set<uint64_t> CoveragePairs;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000133
134 // For UseCounters
135 std::vector<uint8_t> CounterBitmap;
136 size_t TotalBits() { // Slow. Call it only for printing stats.
137 size_t Res = 0;
138 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
139 return Res;
140 }
141
Kostya Serebryany016852c2015-02-19 18:45:37 +0000142 UserCallback Callback;
Aaron Ballmanef116982015-01-29 16:58:29 +0000143 FuzzingOptions Options;
144 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000145 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000146 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000147 long EpochOfLastReadOfOutputCorpus = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000148};
149
150}; // namespace fuzzer