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 //===----------------------------------------------------------------------===//
18 #include <unordered_set>
20 #include "FuzzerInterface.h"
23 typedef std::vector<uint8_t> Unit;
24 using namespace std::chrono;
26 std::string FileToString(const std::string &Path);
27 Unit FileToVector(const std::string &Path);
28 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
30 void WriteToFile(const Unit &U, const std::string &Path);
31 void CopyFileToErr(const std::string &Path);
32 // Returns "Dir/FileName" or equivalent for the current OS.
33 std::string DirPlusFile(const std::string &DirPath,
34 const std::string &FileName);
36 void Mutate(Unit *U, size_t MaxLen);
38 void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
40 void Print(const Unit &U, const char *PrintAfter = "");
41 void PrintASCII(const Unit &U, const char *PrintAfter = "");
42 std::string Hash(const Unit &U);
43 void SetTimer(int Seconds);
44 void PrintFileAsBase64(const std::string &Path);
45 void ExecuteCommand(const std::string &Command);
47 // Private copy of SHA1 implementation.
48 static const int kSHA1NumBytes = 20;
49 // Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
50 void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
52 int NumberOfCpuCores();
56 struct FuzzingOptions {
59 int UnitTimeoutSec = 300;
60 bool DoCrossOver = true;
62 bool ExitOnFirst = false;
63 bool UseCounters = false;
64 bool UseTraces = false;
65 bool UseFullCoverageSet = false;
66 bool UseCoveragePairs = false;
68 int PreferSmallDuringInitialShuffle = -1;
69 size_t MaxNumberOfRuns = ULONG_MAX;
70 int SyncTimeout = 600;
71 std::string OutputCorpus;
72 std::string SyncCommand;
73 std::vector<std::string> Tokens;
75 Fuzzer(UserCallback Callback, FuzzingOptions Options);
76 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
77 void Loop(size_t NumIterations);
78 void ShuffleAndMinimize();
79 void InitializeTraceState();
80 size_t CorpusSize() const { return Corpus.size(); }
81 void ReadDir(const std::string &Path, long *Epoch) {
82 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
84 void RereadOutputCorpus();
85 // Save the current corpus to OutputCorpus.
88 size_t secondsSinceProcessStartUp() {
89 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
93 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
95 static void StaticAlarmCallback();
97 Unit SubstituteTokens(const Unit &U) const;
100 void AlarmCallback();
101 void ExecuteCallback(const Unit &U);
102 void MutateAndTestOne(Unit *U);
103 void ReportNewCoverage(size_t NewCoverage, const Unit &U);
104 size_t RunOne(const Unit &U);
105 void RunOneAndUpdateCorpus(const Unit &U);
106 size_t RunOneMaximizeTotalCoverage(const Unit &U);
107 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
108 size_t RunOneMaximizeCoveragePairs(const Unit &U);
109 void WriteToOutputCorpus(const Unit &U);
110 void WriteToCrash(const Unit &U, const char *Prefix);
111 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
112 void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
116 // Trace-based fuzzing: we run a unit with some kind of tracing
117 // enabled and record potentially useful mutations. Then
118 // We apply these mutations one by one to the unit and run it again.
120 // Start tracing; forget all previously proposed mutations.
121 void StartTraceRecording();
122 // Stop tracing and return the number of proposed mutations.
123 size_t StopTraceRecording();
124 // Apply Idx-th trace-based mutation to U.
125 void ApplyTraceBasedMutation(size_t Idx, Unit *U);
127 void SetDeathCallback();
128 static void StaticDeathCallback();
129 void DeathCallback();
132 size_t TotalNumberOfRuns = 0;
134 std::vector<Unit> Corpus;
135 std::unordered_set<std::string> UnitHashesAddedToCorpus;
136 std::unordered_set<uintptr_t> FullCoverageSets;
137 std::unordered_set<uint64_t> CoveragePairs;
140 std::vector<uint8_t> CounterBitmap;
141 size_t TotalBits() { // Slow. Call it only for printing stats.
143 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
147 UserCallback Callback;
148 FuzzingOptions Options;
149 system_clock::time_point ProcessStartTime = system_clock::now();
150 system_clock::time_point LastExternalSync = system_clock::now();
151 system_clock::time_point UnitStartTime;
152 long TimeOfLongestUnitInSeconds = 0;
153 long EpochOfLastReadOfOutputCorpus = 0;
156 }; // namespace fuzzer