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);
29 void WriteToFile(const Unit &U, const std::string &Path);
30 void CopyFileToErr(const std::string &Path);
31 // Returns "Dir/FileName" or equivalent for the current OS.
32 std::string DirPlusFile(const std::string &DirPath,
33 const std::string &FileName);
35 void Mutate(Unit *U, size_t MaxLen);
37 void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
39 void Print(const Unit &U, const char *PrintAfter = "");
40 void PrintASCII(const Unit &U, const char *PrintAfter = "");
41 std::string Hash(const Unit &U);
42 void SetTimer(int Seconds);
46 struct FuzzingOptions {
49 bool DoCrossOver = true;
51 bool ExitOnFirst = false;
52 bool UseCounters = false;
53 bool UseFullCoverageSet = false;
54 bool UseCoveragePairs = false;
55 bool UseDFSan = false;
56 int PreferSmallDuringInitialShuffle = -1;
57 size_t MaxNumberOfRuns = ULONG_MAX;
58 std::string OutputCorpus;
59 std::vector<std::string> Tokens;
61 Fuzzer(UserCallback Callback, FuzzingOptions Options);
62 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
63 size_t Loop(size_t NumIterations);
64 void ShuffleAndMinimize();
65 void InitializeDFSan();
66 size_t CorpusSize() const { return Corpus.size(); }
67 void ReadDir(const std::string &Path) {
68 ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
70 // Save the current corpus to OutputCorpus.
73 size_t secondsSinceProcessStartUp() {
74 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
78 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
80 static void StaticAlarmCallback();
82 Unit SubstituteTokens(const Unit &U) const;
86 void ExecuteCallback(const Unit &U);
87 size_t MutateAndTestOne(Unit *U);
88 size_t RunOne(const Unit &U);
89 size_t RunOneMaximizeTotalCoverage(const Unit &U);
90 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
91 size_t RunOneMaximizeCoveragePairs(const Unit &U);
92 void WriteToOutputCorpus(const Unit &U);
93 void WriteToCrash(const Unit &U, const char *Prefix);
94 bool MutateWithDFSan(Unit *U);
95 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
96 void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
98 void SetDeathCallback();
99 static void StaticDeathCallback();
100 void DeathCallback();
103 size_t TotalNumberOfRuns = 0;
105 std::vector<Unit> Corpus;
106 std::unordered_set<uintptr_t> FullCoverageSets;
107 std::unordered_set<uint64_t> CoveragePairs;
110 std::vector<uint8_t> CounterBitmap;
111 size_t TotalBits() { // Slow. Call it only for printing stats.
113 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
117 UserCallback Callback;
118 FuzzingOptions Options;
119 system_clock::time_point ProcessStartTime = system_clock::now();
120 system_clock::time_point UnitStartTime;
121 long TimeOfLongestUnitInSeconds = 0;
124 }; // namespace fuzzer