1 //===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Define the main class fuzzer::Fuzzer and most functions.
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_FUZZER_INTERNAL_H
13 #define LLVM_FUZZER_INTERNAL_H
22 #include <unordered_set>
24 #include "FuzzerInterface.h"
27 typedef std::vector<uint8_t> Unit;
28 using namespace std::chrono;
30 std::string FileToString(const std::string &Path);
31 Unit FileToVector(const std::string &Path);
32 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
34 void WriteToFile(const Unit &U, const std::string &Path);
35 void CopyFileToErr(const std::string &Path);
36 // Returns "Dir/FileName" or equivalent for the current OS.
37 std::string DirPlusFile(const std::string &DirPath,
38 const std::string &FileName);
40 void Printf(const char *Fmt, ...);
41 void Print(const Unit &U, const char *PrintAfter = "");
42 void PrintASCII(const Unit &U, const char *PrintAfter = "");
43 std::string Hash(const Unit &U);
44 void SetTimer(int Seconds);
45 void PrintFileAsBase64(const std::string &Path);
46 void ExecuteCommand(const std::string &Command);
48 // Private copy of SHA1 implementation.
49 static const int kSHA1NumBytes = 20;
50 // Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
51 void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
53 // Changes U to contain only ASCII (isprint+isspace) characters.
54 // Returns true iff U has been changed.
55 bool ToASCII(Unit &U);
57 int NumberOfCpuCores();
61 struct FuzzingOptions {
64 int UnitTimeoutSec = 300;
65 bool DoCrossOver = true;
67 bool ExitOnFirst = false;
68 bool UseCounters = false;
69 bool UseTraces = false;
70 bool UseFullCoverageSet = false;
72 int PreferSmallDuringInitialShuffle = -1;
73 size_t MaxNumberOfRuns = ULONG_MAX;
74 int SyncTimeout = 600;
75 int ReportSlowUnits = 10;
76 bool OnlyASCII = false;
79 std::string OutputCorpus;
80 std::string SyncCommand;
81 std::vector<std::string> Tokens;
83 Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
84 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
85 void Loop(size_t NumIterations);
86 void ShuffleAndMinimize();
87 void InitializeTraceState();
88 size_t CorpusSize() const { return Corpus.size(); }
89 void ReadDir(const std::string &Path, long *Epoch) {
90 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
92 void RereadOutputCorpus();
93 // Save the current corpus to OutputCorpus.
96 size_t secondsSinceProcessStartUp() {
97 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
101 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
103 static void StaticAlarmCallback();
105 Unit SubstituteTokens(const Unit &U) const;
108 void AlarmCallback();
109 void ExecuteCallback(const Unit &U);
110 void MutateAndTestOne(Unit *U);
111 void ReportNewCoverage(size_t NewCoverage, const Unit &U);
112 size_t RunOne(const Unit &U);
113 void RunOneAndUpdateCorpus(Unit &U);
114 size_t RunOneMaximizeTotalCoverage(const Unit &U);
115 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
116 size_t RunOneMaximizeCoveragePairs(const Unit &U);
117 void WriteToOutputCorpus(const Unit &U);
118 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
119 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
120 void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
124 // Trace-based fuzzing: we run a unit with some kind of tracing
125 // enabled and record potentially useful mutations. Then
126 // We apply these mutations one by one to the unit and run it again.
128 // Start tracing; forget all previously proposed mutations.
129 void StartTraceRecording();
130 // Stop tracing and return the number of proposed mutations.
131 size_t StopTraceRecording();
132 // Apply Idx-th trace-based mutation to U.
133 void ApplyTraceBasedMutation(size_t Idx, Unit *U);
135 void SetDeathCallback();
136 static void StaticDeathCallback();
137 void DeathCallback();
140 size_t TotalNumberOfRuns = 0;
141 size_t TotalNumberOfExecutedTraceBasedMutations = 0;
143 std::vector<Unit> Corpus;
144 std::unordered_set<std::string> UnitHashesAddedToCorpus;
145 std::unordered_set<uintptr_t> FullCoverageSets;
148 std::vector<uint8_t> CounterBitmap;
149 size_t TotalBits() { // Slow. Call it only for printing stats.
151 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
155 UserSuppliedFuzzer &USF;
156 FuzzingOptions Options;
157 system_clock::time_point ProcessStartTime = system_clock::now();
158 system_clock::time_point LastExternalSync = system_clock::now();
159 system_clock::time_point UnitStartTime;
160 long TimeOfLongestUnitInSeconds = 0;
161 long EpochOfLastReadOfOutputCorpus = 0;
164 class SimpleUserSuppliedFuzzer: public UserSuppliedFuzzer {
166 SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
167 : UserSuppliedFuzzer(Rand), Callback(Callback) {}
168 virtual void TargetFunction(const uint8_t *Data, size_t Size) {
169 return Callback(Data, Size);
173 UserCallback Callback;
176 }; // namespace fuzzer
178 #endif // LLVM_FUZZER_INTERNAL_H