Delete trailing whitespace; NFC
[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
12 #ifndef LLVM_FUZZER_INTERNAL_H
13 #define LLVM_FUZZER_INTERNAL_H
14
15 #include <cassert>
16 #include <climits>
17 #include <chrono>
18 #include <cstddef>
19 #include <cstdlib>
20 #include <string>
21 #include <vector>
22 #include <unordered_set>
23
24 #include "FuzzerInterface.h"
25
26 namespace fuzzer {
27 using namespace std::chrono;
28
29 std::string FileToString(const std::string &Path);
30 Unit FileToVector(const std::string &Path);
31 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
32                             long *Epoch);
33 void WriteToFile(const Unit &U, const std::string &Path);
34 void CopyFileToErr(const std::string &Path);
35 // Returns "Dir/FileName" or equivalent for the current OS.
36 std::string DirPlusFile(const std::string &DirPath,
37                         const std::string &FileName);
38
39 void Printf(const char *Fmt, ...);
40 void Print(const Unit &U, const char *PrintAfter = "");
41 void PrintASCII(const Unit &U, const char *PrintAfter = "");
42 std::string Hash(const Unit &U);
43 void SetTimer(int Seconds);
44 std::string Base64(const Unit &U);
45 int ExecuteCommand(const std::string &Command);
46
47 // Private copy of SHA1 implementation.
48 static const int kSHA1NumBytes = 20;
49 // Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
50 void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
51
52 // Changes U to contain only ASCII (isprint+isspace) characters.
53 // Returns true iff U has been changed.
54 bool ToASCII(Unit &U);
55 bool IsASCII(const Unit &U);
56
57 int NumberOfCpuCores();
58 int GetPid();
59
60 // Dictionary.
61
62 // Parses one dictionary entry.
63 // If successfull, write the enty to Unit and returns true,
64 // otherwise returns false.
65 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
66 // Parses the dictionary file, fills Units, returns true iff all lines
67 // were parsed succesfully.
68 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
69
70 class Fuzzer {
71  public:
72   struct FuzzingOptions {
73     int Verbosity = 1;
74     int MaxLen = 0;
75     int UnitTimeoutSec = 300;
76     int MaxTotalTimeSec = 0;
77     bool DoCrossOver = true;
78     int  MutateDepth = 5;
79     bool ExitOnFirst = false;
80     bool UseCounters = false;
81     bool UseIndirCalls = true;
82     bool UseTraces = false;
83     bool UseFullCoverageSet  = false;
84     bool Reload = true;
85     bool ShuffleAtStartUp = true;
86     int PreferSmallDuringInitialShuffle = -1;
87     size_t MaxNumberOfRuns = ULONG_MAX;
88     int SyncTimeout = 600;
89     int ReportSlowUnits = 10;
90     bool OnlyASCII = false;
91     int TBMDepth = 10;
92     int TBMWidth = 10;
93     std::string OutputCorpus;
94     std::string SyncCommand;
95     std::string ArtifactPrefix = "./";
96     std::string ExactArtifactPath;
97     bool SaveArtifacts = true;
98     bool PrintNEW = true;  // Print a status line when new units are found;
99     bool OutputCSV = false;
100     bool PrintNewCovPcs = false;
101   };
102   Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
103   void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
104   size_t ChooseUnitIdxToMutate();
105   const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
106   void Loop();
107   void Drill();
108   void ShuffleAndMinimize();
109   void InitializeTraceState();
110   size_t CorpusSize() const { return Corpus.size(); }
111   void ReadDir(const std::string &Path, long *Epoch) {
112     Printf("Loading corpus: %s\n", Path.c_str());
113     ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
114   }
115   void RereadOutputCorpus();
116   // Save the current corpus to OutputCorpus.
117   void SaveCorpus();
118
119   size_t secondsSinceProcessStartUp() {
120     return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
121         .count();
122   }
123
124   size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
125
126   static void StaticAlarmCallback();
127
128   void ExecuteCallback(const Unit &U);
129
130   // Merge Corpora[1:] into Corpora[0].
131   void Merge(const std::vector<std::string> &Corpora);
132
133  private:
134   void AlarmCallback();
135   void MutateAndTestOne();
136   void ReportNewCoverage(const Unit &U);
137   bool RunOne(const Unit &U);
138   void RunOneAndUpdateCorpus(Unit &U);
139   void WriteToOutputCorpus(const Unit &U);
140   void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
141   void PrintStats(const char *Where, const char *End = "\n");
142   void PrintStatusForNewUnit(const Unit &U);
143   void PrintUnitInASCII(const Unit &U, const char *PrintAfter = "");
144
145   void SyncCorpus();
146
147   size_t RecordBlockCoverage();
148   size_t RecordCallerCalleeCoverage();
149   void PrepareCoverageBeforeRun();
150   bool CheckCoverageAfterRun();
151
152
153   // Trace-based fuzzing: we run a unit with some kind of tracing
154   // enabled and record potentially useful mutations. Then
155   // We apply these mutations one by one to the unit and run it again.
156
157   // Start tracing; forget all previously proposed mutations.
158   void StartTraceRecording();
159   // Stop tracing and return the number of proposed mutations.
160   size_t StopTraceRecording();
161   // Apply Idx-th trace-based mutation to U.
162   void ApplyTraceBasedMutation(size_t Idx, Unit *U);
163
164   void SetDeathCallback();
165   static void StaticDeathCallback();
166   void DeathCallback();
167   Unit CurrentUnit;
168
169   size_t TotalNumberOfRuns = 0;
170   size_t TotalNumberOfExecutedTraceBasedMutations = 0;
171
172   std::vector<Unit> Corpus;
173   std::unordered_set<std::string> UnitHashesAddedToCorpus;
174
175   // For UseCounters
176   std::vector<uint8_t> CounterBitmap;
177   size_t TotalBits() {  // Slow. Call it only for printing stats.
178     size_t Res = 0;
179     for (auto x : CounterBitmap) Res += __builtin_popcount(x);
180     return Res;
181   }
182
183   UserSuppliedFuzzer &USF;
184   FuzzingOptions Options;
185   system_clock::time_point ProcessStartTime = system_clock::now();
186   system_clock::time_point LastExternalSync = system_clock::now();
187   system_clock::time_point UnitStartTime;
188   long TimeOfLongestUnitInSeconds = 0;
189   long EpochOfLastReadOfOutputCorpus = 0;
190   size_t LastRecordedBlockCoverage = 0;
191   size_t LastRecordedCallerCalleeCoverage = 0;
192   size_t LastCoveragePcBufferLen = 0;
193 };
194
195 class SimpleUserSuppliedFuzzer: public UserSuppliedFuzzer {
196  public:
197   SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
198       : UserSuppliedFuzzer(Rand), Callback(Callback) {}
199
200   virtual int TargetFunction(const uint8_t *Data, size_t Size) override {
201     return Callback(Data, Size);
202   }
203
204  private:
205   UserCallback Callback = nullptr;
206 };
207
208 };  // namespace fuzzer
209
210 #endif // LLVM_FUZZER_INTERNAL_H