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 Unit ReadFile(const char *Path);
27 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V);
28 void WriteToFile(const Unit &U, const std::string &Path);
29 void CopyFileToErr(const std::string &Path);
30 // Returns "Dir/FileName" or equivalent for the current OS.
31 std::string DirPlusFile(const std::string &DirPath,
32 const std::string &FileName);
34 void Mutate(Unit *U, size_t MaxLen);
36 void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
38 void Print(const Unit &U, const char *PrintAfter = "");
39 void PrintASCII(const Unit &U, const char *PrintAfter = "");
40 std::string Hash(const Unit &U);
41 void SetTimer(int Seconds);
45 struct FuzzingOptions {
48 bool DoCrossOver = true;
50 bool ExitOnFirst = false;
51 bool UseCounters = false;
52 bool UseFullCoverageSet = false;
53 bool UseCoveragePairs = false;
54 bool UseDFSan = false;
55 int PreferSmallDuringInitialShuffle = -1;
56 size_t MaxNumberOfRuns = ULONG_MAX;
57 std::string OutputCorpus;
59 Fuzzer(UserCallback Callback, FuzzingOptions Options)
60 : Callback(Callback), Options(Options) {
64 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
65 size_t Loop(size_t NumIterations);
66 void ShuffleAndMinimize();
67 void InitializeDFSan();
68 size_t CorpusSize() const { return Corpus.size(); }
69 void ReadDir(const std::string &Path) {
70 ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
72 // Save the current corpus to OutputCorpus.
75 size_t secondsSinceProcessStartUp() {
76 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
80 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
82 static void AlarmCallback();
85 size_t MutateAndTestOne(Unit *U);
86 size_t RunOne(const Unit &U);
87 size_t RunOneMaximizeTotalCoverage(const Unit &U);
88 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
89 size_t RunOneMaximizeCoveragePairs(const Unit &U);
90 void WriteToOutputCorpus(const Unit &U);
91 static void WriteToCrash(const Unit &U, const char *Prefix);
92 bool MutateWithDFSan(Unit *U);
94 void SetDeathCallback();
95 static void DeathCallback();
96 static Unit CurrentUnit;
98 size_t TotalNumberOfRuns = 0;
100 std::vector<Unit> Corpus;
101 std::unordered_set<uintptr_t> FullCoverageSets;
102 std::unordered_set<uint64_t> CoveragePairs;
105 std::vector<uint8_t> CounterBitmap;
106 size_t TotalBits() { // Slow. Call it only for printing stats.
108 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
112 UserCallback Callback;
113 FuzzingOptions Options;
114 system_clock::time_point ProcessStartTime = system_clock::now();
115 static system_clock::time_point UnitStartTime;
118 }; // namespace fuzzer