[fuzzer] split main() into FuzzerDriver() that takes a callback as a parameter and...
[oota-llvm.git] / lib / Fuzzer / FuzzerInternal.h
1 //===- 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>
12 #include <climits>
13 #include <chrono>
14 #include <cstddef>
15 #include <cstdlib>
16 #include <string>
17 #include <vector>
18 #include <unordered_set>
19
20 #include "FuzzerInterface.h"
21
22 namespace fuzzer {
23 typedef std::vector<uint8_t> Unit;
24 using namespace std::chrono;
25
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);
33
34 void Mutate(Unit *U, size_t MaxLen);
35
36 void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
37
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);
42
43 class Fuzzer {
44  public:
45   struct FuzzingOptions {
46     int Verbosity = 1;
47     int MaxLen = 0;
48     bool DoCrossOver = true;
49     int  MutateDepth = 5;
50     bool ExitOnFirst = false;
51     bool UseFullCoverageSet  = false;
52     int PreferSmallDuringInitialShuffle = -1;
53     size_t MaxNumberOfRuns = ULONG_MAX;
54     std::string OutputCorpus;
55   };
56   Fuzzer(UserCallback Callback, FuzzingOptions Options)
57       : Callback(Callback), Options(Options) {
58     SetDeathCallback();
59   }
60   void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
61   size_t Loop(size_t NumIterations);
62   void ShuffleAndMinimize();
63   size_t CorpusSize() const { return Corpus.size(); }
64   void ReadDir(const std::string &Path) {
65     ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
66   }
67   // Save the current corpus to OutputCorpus.
68   void SaveCorpus();
69
70   size_t secondsSinceProcessStartUp() {
71     return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
72         .count();
73   }
74
75   size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
76
77   static void AlarmCallback();
78
79  private:
80   size_t MutateAndTestOne(Unit *U);
81   size_t RunOne(const Unit &U);
82   size_t RunOneMaximizeTotalCoverage(const Unit &U);
83   size_t RunOneMaximizeFullCoverageSet(const Unit &U);
84   void WriteToOutputCorpus(const Unit &U);
85   static void WriteToCrash(const Unit &U, const char *Prefix);
86
87   void SetDeathCallback();
88   static void DeathCallback();
89   static Unit CurrentUnit;
90
91   size_t TotalNumberOfRuns = 0;
92
93   std::vector<Unit> Corpus;
94   std::unordered_set<uintptr_t> FullCoverageSets;
95   UserCallback Callback;
96   FuzzingOptions Options;
97   system_clock::time_point ProcessStartTime = system_clock::now();
98   static system_clock::time_point UnitStartTime;
99 };
100
101 };  // namespace fuzzer