blob: c44f0ea34d5be7f2ac368d0df5b89317213fbf3c [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- 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//===----------------------------------------------------------------------===//
Yaron Keren347663b2015-08-10 16:37:40 +000011
12#ifndef LLVM_FUZZER_INTERNAL_H
13#define LLVM_FUZZER_INTERNAL_H
14
Aaron Ballmanef116982015-01-29 16:58:29 +000015#include <cassert>
Kostya Serebryany33f86692015-02-04 22:20:09 +000016#include <climits>
Aaron Ballmanef116982015-01-29 16:58:29 +000017#include <chrono>
18#include <cstddef>
19#include <cstdlib>
20#include <string>
21#include <vector>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000022#include <unordered_set>
Aaron Ballmanef116982015-01-29 16:58:29 +000023
Kostya Serebryany016852c2015-02-19 18:45:37 +000024#include "FuzzerInterface.h"
25
Aaron Ballmanef116982015-01-29 16:58:29 +000026namespace fuzzer {
27typedef std::vector<uint8_t> Unit;
28using namespace std::chrono;
29
Kostya Serebryany52a788e2015-03-31 20:13:20 +000030std::string FileToString(const std::string &Path);
31Unit FileToVector(const std::string &Path);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000032void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
33 long *Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000034void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000035void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000036// Returns "Dir/FileName" or equivalent for the current OS.
37std::string DirPlusFile(const std::string &DirPath,
38 const std::string &FileName);
39
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000040void Printf(const char *Fmt, ...);
Aaron Ballmanef116982015-01-29 16:58:29 +000041void Print(const Unit &U, const char *PrintAfter = "");
42void PrintASCII(const Unit &U, const char *PrintAfter = "");
43std::string Hash(const Unit &U);
44void SetTimer(int Seconds);
Kostya Serebryanyca6a2a22015-05-05 21:59:51 +000045void PrintFileAsBase64(const std::string &Path);
Kostya Serebryany2da7b842015-05-18 21:34:20 +000046void ExecuteCommand(const std::string &Command);
Aaron Ballmanef116982015-01-29 16:58:29 +000047
Kostya Serebryany96eab652015-05-14 22:41:49 +000048// Private copy of SHA1 implementation.
49static const int kSHA1NumBytes = 20;
50// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
51void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
52
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000053// Changes U to contain only ASCII (isprint+isspace) characters.
54// Returns true iff U has been changed.
55bool ToASCII(Unit &U);
Kostya Serebryanya9346c22015-09-02 19:08:08 +000056bool IsASCII(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000057
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000058int NumberOfCpuCores();
59
Aaron Ballmanef116982015-01-29 16:58:29 +000060class Fuzzer {
61 public:
62 struct FuzzingOptions {
63 int Verbosity = 1;
64 int MaxLen = 0;
Kostya Serebryany490bbd62015-05-19 22:12:57 +000065 int UnitTimeoutSec = 300;
Aaron Ballmanef116982015-01-29 16:58:29 +000066 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +000067 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +000068 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +000069 bool UseCounters = false;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000070 bool UseTraces = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000071 bool UseFullCoverageSet = false;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000072 bool Reload = true;
Kostya Serebryany92e04762015-02-04 23:42:42 +000073 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +000074 size_t MaxNumberOfRuns = ULONG_MAX;
Kostya Serebryany2da7b842015-05-18 21:34:20 +000075 int SyncTimeout = 600;
Kostya Serebryany70926ae2015-08-05 21:43:48 +000076 int ReportSlowUnits = 10;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000077 bool OnlyASCII = false;
Kostya Serebryany12c78372015-08-12 01:55:37 +000078 int TBMDepth = 10;
79 int TBMWidth = 10;
Aaron Ballmanef116982015-01-29 16:58:29 +000080 std::string OutputCorpus;
Kostya Serebryany2da7b842015-05-18 21:34:20 +000081 std::string SyncCommand;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000082 std::vector<std::string> Tokens;
Aaron Ballmanef116982015-01-29 16:58:29 +000083 };
Kostya Serebryanyf3424592015-05-22 22:35:31 +000084 Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
Aaron Ballmanef116982015-01-29 16:58:29 +000085 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
Kostya Serebryany7d470cf2015-05-07 18:32:29 +000086 void Loop(size_t NumIterations);
Aaron Ballmanef116982015-01-29 16:58:29 +000087 void ShuffleAndMinimize();
Kostya Serebryany22526252015-05-11 21:16:27 +000088 void InitializeTraceState();
Aaron Ballmanef116982015-01-29 16:58:29 +000089 size_t CorpusSize() const { return Corpus.size(); }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000090 void ReadDir(const std::string &Path, long *Epoch) {
91 ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
Aaron Ballmanef116982015-01-29 16:58:29 +000092 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000093 void RereadOutputCorpus();
Aaron Ballmanef116982015-01-29 16:58:29 +000094 // Save the current corpus to OutputCorpus.
95 void SaveCorpus();
96
Kostya Serebryany92e04762015-02-04 23:42:42 +000097 size_t secondsSinceProcessStartUp() {
98 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
99 .count();
100 }
101
102 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
103
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000104 static void StaticAlarmCallback();
105
106 Unit SubstituteTokens(const Unit &U) const;
Aaron Ballmanef116982015-01-29 16:58:29 +0000107
108 private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000109 void AlarmCallback();
110 void ExecuteCallback(const Unit &U);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000111 void MutateAndTestOne(Unit *U);
112 void ReportNewCoverage(size_t NewCoverage, const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000113 size_t RunOne(const Unit &U);
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000114 void RunOneAndUpdateCorpus(Unit &U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000115 size_t RunOneMaximizeTotalCoverage(const Unit &U);
116 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000117 size_t RunOneMaximizeCoveragePairs(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000118 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany2b7d2e92015-07-23 18:37:22 +0000119 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000120 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000121 void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000122
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000123 void SyncCorpus();
124
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000125 // Trace-based fuzzing: we run a unit with some kind of tracing
126 // enabled and record potentially useful mutations. Then
127 // We apply these mutations one by one to the unit and run it again.
128
129 // Start tracing; forget all previously proposed mutations.
130 void StartTraceRecording();
131 // Stop tracing and return the number of proposed mutations.
132 size_t StopTraceRecording();
133 // Apply Idx-th trace-based mutation to U.
134 void ApplyTraceBasedMutation(size_t Idx, Unit *U);
135
Aaron Ballmanef116982015-01-29 16:58:29 +0000136 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000137 static void StaticDeathCallback();
138 void DeathCallback();
139 Unit CurrentUnit;
Aaron Ballmanef116982015-01-29 16:58:29 +0000140
141 size_t TotalNumberOfRuns = 0;
Kostya Serebryany12c78372015-08-12 01:55:37 +0000142 size_t TotalNumberOfExecutedTraceBasedMutations = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000143
144 std::vector<Unit> Corpus;
Kostya Serebryanycbb23342015-05-19 01:06:07 +0000145 std::unordered_set<std::string> UnitHashesAddedToCorpus;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000146 std::unordered_set<uintptr_t> FullCoverageSets;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000147
148 // For UseCounters
149 std::vector<uint8_t> CounterBitmap;
150 size_t TotalBits() { // Slow. Call it only for printing stats.
151 size_t Res = 0;
152 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
153 return Res;
154 }
155
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000156 UserSuppliedFuzzer &USF;
Aaron Ballmanef116982015-01-29 16:58:29 +0000157 FuzzingOptions Options;
158 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000159 system_clock::time_point LastExternalSync = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000160 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000161 long TimeOfLongestUnitInSeconds = 0;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000162 long EpochOfLastReadOfOutputCorpus = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000163};
164
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000165class SimpleUserSuppliedFuzzer: public UserSuppliedFuzzer {
166 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000167 SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
168 : UserSuppliedFuzzer(Rand), Callback(Callback) {}
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000169 virtual void TargetFunction(const uint8_t *Data, size_t Size) {
170 return Callback(Data, Size);
171 }
172
173 private:
174 UserCallback Callback;
175};
176
Aaron Ballmanef116982015-01-29 16:58:29 +0000177}; // namespace fuzzer
Yaron Keren347663b2015-08-10 16:37:40 +0000178
179#endif // LLVM_FUZZER_INTERNAL_H