blob: 66548350e97ff97d4c852ce5ef22de6b9ff63dde [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
26Unit ReadFile(const char *Path);
27void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V);
28void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000029void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000030// Returns "Dir/FileName" or equivalent for the current OS.
31std::string DirPlusFile(const std::string &DirPath,
32 const std::string &FileName);
33
34void Mutate(Unit *U, size_t MaxLen);
35
36void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
37
38void Print(const Unit &U, const char *PrintAfter = "");
39void PrintASCII(const Unit &U, const char *PrintAfter = "");
40std::string Hash(const Unit &U);
41void SetTimer(int Seconds);
42
43class Fuzzer {
44 public:
45 struct FuzzingOptions {
46 int Verbosity = 1;
47 int MaxLen = 0;
48 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +000049 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +000050 bool ExitOnFirst = false;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +000051 bool UseCounters = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000052 bool UseFullCoverageSet = false;
Kostya Serebryany2e3622b2015-02-20 03:02:37 +000053 bool UseCoveragePairs = false;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000054 bool UseDFSan = false;
Kostya Serebryany92e04762015-02-04 23:42:42 +000055 int PreferSmallDuringInitialShuffle = -1;
Kostya Serebryany33f86692015-02-04 22:20:09 +000056 size_t MaxNumberOfRuns = ULONG_MAX;
Aaron Ballmanef116982015-01-29 16:58:29 +000057 std::string OutputCorpus;
58 };
Kostya Serebryany016852c2015-02-19 18:45:37 +000059 Fuzzer(UserCallback Callback, FuzzingOptions Options)
60 : Callback(Callback), Options(Options) {
Aaron Ballmanef116982015-01-29 16:58:29 +000061 SetDeathCallback();
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000062 InitializeDFSan();
Aaron Ballmanef116982015-01-29 16:58:29 +000063 }
64 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
65 size_t Loop(size_t NumIterations);
66 void ShuffleAndMinimize();
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000067 void InitializeDFSan();
Aaron Ballmanef116982015-01-29 16:58:29 +000068 size_t CorpusSize() const { return Corpus.size(); }
69 void ReadDir(const std::string &Path) {
70 ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
71 }
72 // Save the current corpus to OutputCorpus.
73 void SaveCorpus();
74
Kostya Serebryany92e04762015-02-04 23:42:42 +000075 size_t secondsSinceProcessStartUp() {
76 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
77 .count();
78 }
79
80 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
81
Aaron Ballmanef116982015-01-29 16:58:29 +000082 static void AlarmCallback();
83
84 private:
85 size_t MutateAndTestOne(Unit *U);
86 size_t RunOne(const Unit &U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000087 size_t RunOneMaximizeTotalCoverage(const Unit &U);
88 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
Kostya Serebryany2e3622b2015-02-20 03:02:37 +000089 size_t RunOneMaximizeCoveragePairs(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +000090 void WriteToOutputCorpus(const Unit &U);
91 static void WriteToCrash(const Unit &U, const char *Prefix);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000092 bool MutateWithDFSan(Unit *U);
Kostya Serebryany03db8b92015-03-30 22:44:03 +000093 void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
Aaron Ballmanef116982015-01-29 16:58:29 +000094
95 void SetDeathCallback();
96 static void DeathCallback();
97 static Unit CurrentUnit;
98
99 size_t TotalNumberOfRuns = 0;
100
101 std::vector<Unit> Corpus;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000102 std::unordered_set<uintptr_t> FullCoverageSets;
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000103 std::unordered_set<uint64_t> CoveragePairs;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000104
105 // For UseCounters
106 std::vector<uint8_t> CounterBitmap;
107 size_t TotalBits() { // Slow. Call it only for printing stats.
108 size_t Res = 0;
109 for (auto x : CounterBitmap) Res += __builtin_popcount(x);
110 return Res;
111 }
112
Kostya Serebryany016852c2015-02-19 18:45:37 +0000113 UserCallback Callback;
Aaron Ballmanef116982015-01-29 16:58:29 +0000114 FuzzingOptions Options;
115 system_clock::time_point ProcessStartTime = system_clock::now();
116 static system_clock::time_point UnitStartTime;
Kostya Serebryany16901a92015-03-30 23:04:35 +0000117 long TimeOfLongestUnitInSeconds = 0;
Aaron Ballmanef116982015-01-29 16:58:29 +0000118};
119
120}; // namespace fuzzer