[lib/Fuzzer] on crash print the contents of the crashy input as base64
[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 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);
34
35 void Mutate(Unit *U, size_t MaxLen);
36
37 void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
38
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);
43 void PrintFileAsBase64(const std::string &Path);
44
45 class Fuzzer {
46  public:
47   struct FuzzingOptions {
48     int Verbosity = 1;
49     int MaxLen = 0;
50     bool DoCrossOver = true;
51     int  MutateDepth = 5;
52     bool ExitOnFirst = false;
53     bool UseCounters = false;
54     bool UseFullCoverageSet  = false;
55     bool UseCoveragePairs = false;
56     bool UseDFSan = false;
57     int PreferSmallDuringInitialShuffle = -1;
58     size_t MaxNumberOfRuns = ULONG_MAX;
59     std::string OutputCorpus;
60     std::vector<std::string> Tokens;
61   };
62   Fuzzer(UserCallback Callback, FuzzingOptions Options);
63   void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
64   size_t Loop(size_t NumIterations);
65   void ShuffleAndMinimize();
66   void InitializeDFSan();
67   size_t CorpusSize() const { return Corpus.size(); }
68   void ReadDir(const std::string &Path) {
69     ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
70   }
71   // Save the current corpus to OutputCorpus.
72   void SaveCorpus();
73
74   size_t secondsSinceProcessStartUp() {
75     return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
76         .count();
77   }
78
79   size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
80
81   static void StaticAlarmCallback();
82
83   Unit SubstituteTokens(const Unit &U) const;
84
85  private:
86   void AlarmCallback();
87   void ExecuteCallback(const Unit &U);
88   size_t MutateAndTestOne(Unit *U);
89   size_t RunOne(const Unit &U);
90   size_t RunOneMaximizeTotalCoverage(const Unit &U);
91   size_t RunOneMaximizeFullCoverageSet(const Unit &U);
92   size_t RunOneMaximizeCoveragePairs(const Unit &U);
93   void WriteToOutputCorpus(const Unit &U);
94   void WriteToCrash(const Unit &U, const char *Prefix);
95   bool MutateWithDFSan(Unit *U);
96   void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
97   void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
98
99   void SetDeathCallback();
100   static void StaticDeathCallback();
101   void DeathCallback();
102   Unit CurrentUnit;
103
104   size_t TotalNumberOfRuns = 0;
105
106   std::vector<Unit> Corpus;
107   std::unordered_set<uintptr_t> FullCoverageSets;
108   std::unordered_set<uint64_t>  CoveragePairs;
109
110   // For UseCounters
111   std::vector<uint8_t> CounterBitmap;
112   size_t TotalBits() {  // Slow. Call it only for printing stats.
113     size_t Res = 0;
114     for (auto x : CounterBitmap) Res += __builtin_popcount(x);
115     return Res;
116   }
117
118   UserCallback Callback;
119   FuzzingOptions Options;
120   system_clock::time_point ProcessStartTime = system_clock::now();
121   system_clock::time_point UnitStartTime;
122   long TimeOfLongestUnitInSeconds = 0;
123 };
124
125 };  // namespace fuzzer