blob: 77871097bd72df20add3da785e335cb092ad3833 [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//===----------------------------------------------------------------------===//
11#include <cassert>
Kostya Serebryany33f86692015-02-04 22:20:09 +000012#include <climits>
Aaron Ballmanef116982015-01-29 16:58:29 +000013#include <chrono>
14#include <cstddef>
15#include <cstdlib>
16#include <string>
17#include <vector>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000018#include <unordered_set>
Aaron Ballmanef116982015-01-29 16:58:29 +000019
Kostya Serebryany016852c2015-02-19 18:45:37 +000020#include "FuzzerInterface.h"
21
Aaron Ballmanef116982015-01-29 16:58:29 +000022namespace fuzzer {
23typedef std::vector<uint8_t> Unit;
24using namespace std::chrono;
25
Kostya Serebryany52a788e2015-03-31 20:13:20 +000026std::string FileToString(const std::string &Path);
27Unit FileToVector(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000028void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V);
29void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000030void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000031// Returns "Dir/FileName" or equivalent for the current OS.
32std::string DirPlusFile(const std::string &DirPath,
33 const std::string &FileName);
34
35void Mutate(Unit *U, size_t MaxLen);
36
37void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
38
39void Print(const Unit &U, const char *PrintAfter = "");
40void PrintASCII(const Unit &U, const char *PrintAfter = "");
41std::string Hash(const Unit &U);
42void SetTimer(int Seconds);
43
44class Fuzzer {
45 public:
46 struct FuzzingOptions {
47 int Verbosity = 1;
48 int MaxLen = 0;
49 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +000050 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +000051 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +000052 bool UseCounters = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000053 bool UseFullCoverageSet = false;
Kostya Serebryany2e3622b2015-02-20 03:02:37 +000054 bool UseCoveragePairs = false;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000055 bool UseDFSan = false;
Kostya Serebryany92e04762015-02-04 23:42:42 +000056 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +000057 size_t MaxNumberOfRuns = ULONG_MAX;
Aaron Ballmanef116982015-01-29 16:58:29 +000058 std::string OutputCorpus;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000059 std::vector<std::string> Tokens;
Aaron Ballmanef116982015-01-29 16:58:29 +000060 };
Kostya Serebryany52a788e2015-03-31 20:13:20 +000061 Fuzzer(UserCallback Callback, FuzzingOptions Options);
Aaron Ballmanef116982015-01-29 16:58:29 +000062 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
63 size_t Loop(size_t NumIterations);
64 void ShuffleAndMinimize();
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000065 void InitializeDFSan();
Aaron Ballmanef116982015-01-29 16:58:29 +000066 size_t CorpusSize() const { return Corpus.size(); }
67 void ReadDir(const std::string &Path) {
68 ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
69 }
70 // Save the current corpus to OutputCorpus.
71 void SaveCorpus();
72
Kostya Serebryany92e04762015-02-04 23:42:42 +000073 size_t secondsSinceProcessStartUp() {
74 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
75 .count();
76 }
77
78 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
79
Kostya Serebryany52a788e2015-03-31 20:13:20 +000080 static void StaticAlarmCallback();
81
82 Unit SubstituteTokens(const Unit &U) const;
Aaron Ballmanef116982015-01-29 16:58:29 +000083
84 private:
Kostya Serebryany52a788e2015-03-31 20:13:20 +000085 void AlarmCallback();
86 void ExecuteCallback(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +000087 size_t MutateAndTestOne(Unit *U);
88 size_t RunOne(const Unit &U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000089 size_t RunOneMaximizeTotalCoverage(const Unit &U);
90 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
Kostya Serebryany2e3622b2015-02-20 03:02:37 +000091 size_t RunOneMaximizeCoveragePairs(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +000092 void WriteToOutputCorpus(const Unit &U);
Kostya Serebryany52a788e2015-03-31 20:13:20 +000093 void WriteToCrash(const Unit &U, const char *Prefix);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000094 bool MutateWithDFSan(Unit *U);
Kostya Serebryany03db8b92015-03-30 22:44:03 +000095 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
Kostya Serebryany52a788e2015-03-31 20:13:20 +000096 void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
Aaron Ballmanef116982015-01-29 16:58:29 +000097
98 void SetDeathCallback();
Kostya Serebryany52a788e2015-03-31 20:13:20 +000099 static void StaticDeathCallback();
100 void DeathCallback();
101 Unit CurrentUnit;
Aaron Ballmanef116982015-01-29 16:58:29 +0000102
103 size_t TotalNumberOfRuns = 0;
104
105 std::vector<Unit> Corpus;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000106 std::unordered_set<uintptr_t> FullCoverageSets;
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000107 std::unordered_set<uint64_t> CoveragePairs;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000108
109 // For UseCounters
110 std::vector<uint8_t> CounterBitmap;
111 size_t TotalBits() { // Slow. Call it only for printing stats.
112 size_t Res = 0;
113 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
114 return Res;
115 }
116
Kostya Serebryany016852c2015-02-19 18:45:37 +0000117 UserCallback Callback;
Aaron Ballmanef116982015-01-29 16:58:29 +0000118 FuzzingOptions Options;
119 system_clock::time_point ProcessStartTime = system_clock::now();
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000120 system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000121 long TimeOfLongestUnitInSeconds = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000122};
123
124}; // namespace fuzzer