[lib/Fuzzer] change the way we use taint information for fuzzing. Now, we run a singl...
[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   void 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   void MutateAndTestOne(Unit *U);
89   void ReportNewCoverage(size_t NewCoverage, const Unit &U);
90   size_t RunOne(const Unit &U);
91   void RunOneAndUpdateCorpus(const Unit &U);
92   size_t RunOneMaximizeTotalCoverage(const Unit &U);
93   size_t RunOneMaximizeFullCoverageSet(const Unit &U);
94   size_t RunOneMaximizeCoveragePairs(const Unit &U);
95   void WriteToOutputCorpus(const Unit &U);
96   void WriteToCrash(const Unit &U, const char *Prefix);
97   void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
98   void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
99
100   // Trace-based fuzzing: we run a unit with some kind of tracing
101   // enabled and record potentially useful mutations. Then
102   // We apply these mutations one by one to the unit and run it again.
103
104   // Start tracing; forget all previously proposed mutations.
105   void StartTraceRecording();
106   // Stop tracing and return the number of proposed mutations.
107   size_t StopTraceRecording();
108   // Apply Idx-th trace-based mutation to U.
109   void ApplyTraceBasedMutation(size_t Idx, Unit *U);
110
111   void SetDeathCallback();
112   static void StaticDeathCallback();
113   void DeathCallback();
114   Unit CurrentUnit;
115
116   size_t TotalNumberOfRuns = 0;
117
118   std::vector<Unit> Corpus;
119   std::unordered_set<uintptr_t> FullCoverageSets;
120   std::unordered_set<uint64_t>  CoveragePairs;
121
122   // For UseCounters
123   std::vector<uint8_t> CounterBitmap;
124   size_t TotalBits() {  // Slow. Call it only for printing stats.
125     size_t Res = 0;
126     for (auto x : CounterBitmap) Res += __builtin_popcount(x);
127     return Res;
128   }
129
130   UserCallback Callback;
131   FuzzingOptions Options;
132   system_clock::time_point ProcessStartTime = system_clock::now();
133   system_clock::time_point UnitStartTime;
134   long TimeOfLongestUnitInSeconds = 0;
135 };
136
137 };  // namespace fuzzer