blob: f085f94d612e9d60d9298e8a908a7e75dc79693f [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
47class Fuzzer {
48 public:
49 struct FuzzingOptions {
50 int Verbosity = 1;
51 int MaxLen = 0;
52 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +000053 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +000054 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +000055 bool UseCounters = false;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000056 bool UseTraces = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000057 bool UseFullCoverageSet = false;
Kostya Serebryany2e3622b2015-02-20 03:02:37 +000058 bool UseCoveragePairs = false;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000059 bool UseDFSan = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000060 bool Reload = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +000061 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +000062 size_t MaxNumberOfRuns = ULONG_MAX;
Aaron Ballmanef116982015-01-29 16:58:29 +000063 std::string OutputCorpus;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000064 std::vector<std::string> Tokens;
Aaron Ballmanef116982015-01-29 16:58:29 +000065 };
Kostya Serebryany52a788e2015-03-31 20:13:20 +000066 Fuzzer(UserCallback Callback, FuzzingOptions Options);
Aaron Ballmanef116982015-01-29 16:58:29 +000067 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
Kostya Serebryany7d470cf2015-05-07 18:32:29 +000068 void Loop(size_t NumIterations);
Aaron Ballmanef116982015-01-29 16:58:29 +000069 void ShuffleAndMinimize();
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000070 void InitializeDFSan();
Aaron Ballmanef116982015-01-29 16:58:29 +000071 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000072 void ReadDir(const std::string &Path, long *Epoch) {
73 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000074 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000075 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +000076 // Save the current corpus to OutputCorpus.
77 void SaveCorpus();
78
Kostya Serebryany92e04762015-02-04 23:42:42 +000079 size_t secondsSinceProcessStartUp() {
80 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
81 .count();
82 }
83
84 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
85
Kostya Serebryany52a788e2015-03-31 20:13:20 +000086 static void StaticAlarmCallback();
87
88 Unit SubstituteTokens(const Unit &U) const;
Aaron Ballmanef116982015-01-29 16:58:29 +000089
90 private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +000091 void AlarmCallback();
92 void ExecuteCallback(const Unit &U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +000093 void MutateAndTestOne(Unit *U);
94 void ReportNewCoverage(size_t NewCoverage, const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +000095 size_t RunOne(const Unit &U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +000096 void RunOneAndUpdateCorpus(const Unit &U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000097 size_t RunOneMaximizeTotalCoverage(const Unit &U);
98 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
Kostya Serebryany2e3622b2015-02-20 03:02:37 +000099 size_t RunOneMaximizeCoveragePairs(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000100 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000101 void WriteToCrash(const Unit &U, const char *Prefix);
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000102 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000103 void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000104
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000105 // Trace-based fuzzing: we run a unit with some kind of tracing
106 // enabled and record potentially useful mutations. Then
107 // We apply these mutations one by one to the unit and run it again.
108
109 // Start tracing; forget all previously proposed mutations.
110 void StartTraceRecording();
111 // Stop tracing and return the number of proposed mutations.
112 size_t StopTraceRecording();
113 // Apply Idx-th trace-based mutation to U.
114 void ApplyTraceBasedMutation(size_t Idx, Unit *U);
115
Aaron Ballmanef116982015-01-29 16:58:29 +0000116 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000117 static void StaticDeathCallback();
118 void DeathCallback();
119 Unit CurrentUnit;
Aaron Ballmanef116982015-01-29 16:58:29 +0000120
121 size_t TotalNumberOfRuns = 0;
122
123 std::vector<Unit> Corpus;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000124 std::set<Unit> UnitsAddedAfterInitialLoad;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000125 std::unordered_set<uintptr_t> FullCoverageSets;
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000126 std::unordered_set<uint64_t> CoveragePairs;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000127
128 // For UseCounters
129 std::vector<uint8_t> CounterBitmap;
130 size_t TotalBits() { // Slow. Call it only for printing stats.
131 size_t Res = 0;
132 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
133 return Res;
134 }
135
Kostya Serebryany016852c2015-02-19 18:45:37 +0000136 UserCallback Callback;
Aaron Ballmanef116982015-01-29 16:58:29 +0000137 FuzzingOptions Options;
138 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000139 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000140 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000141 long EpochOfLastReadOfOutputCorpus = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000142};
143
144}; // namespace fuzzer