blob: acf07ee6166d67379fd7d92e2184f92f2d19c6ca [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
20namespace fuzzer {
21typedef std::vector<uint8_t> Unit;
22using namespace std::chrono;
23
24Unit ReadFile(const char *Path);
25void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V);
26void WriteToFile(const Unit &U, const std::string &Path);
Kostya Serebryany5b266a82015-02-04 19:10:20 +000027void CopyFileToErr(const std::string &Path);
Aaron Ballmanef116982015-01-29 16:58:29 +000028// Returns "Dir/FileName" or equivalent for the current OS.
29std::string DirPlusFile(const std::string &DirPath,
30 const std::string &FileName);
31
32void Mutate(Unit *U, size_t MaxLen);
33
34void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
35
36void Print(const Unit &U, const char *PrintAfter = "");
37void PrintASCII(const Unit &U, const char *PrintAfter = "");
38std::string Hash(const Unit &U);
39void SetTimer(int Seconds);
40
41class Fuzzer {
42 public:
43 struct FuzzingOptions {
44 int Verbosity = 1;
45 int MaxLen = 0;
46 bool DoCrossOver = true;
Kostya Serebryany5b266a82015-02-04 19:10:20 +000047 int MutateDepth = 5;
Aaron Ballmanef116982015-01-29 16:58:29 +000048 bool ExitOnFirst = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000049 bool UseFullCoverageSet = false;
Kostya Serebryany33f86692015-02-04 22:20:09 +000050 size_t MaxNumberOfRuns = ULONG_MAX;
Aaron Ballmanef116982015-01-29 16:58:29 +000051 std::string OutputCorpus;
52 };
53 Fuzzer(FuzzingOptions Options) : Options(Options) {
54 SetDeathCallback();
55 }
56 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
57 size_t Loop(size_t NumIterations);
58 void ShuffleAndMinimize();
59 size_t CorpusSize() const { return Corpus.size(); }
60 void ReadDir(const std::string &Path) {
61 ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
62 }
63 // Save the current corpus to OutputCorpus.
64 void SaveCorpus();
65
66 static void AlarmCallback();
67
68 private:
69 size_t MutateAndTestOne(Unit *U);
70 size_t RunOne(const Unit &U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000071 size_t RunOneMaximizeTotalCoverage(const Unit &U);
72 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +000073 void WriteToOutputCorpus(const Unit &U);
74 static void WriteToCrash(const Unit &U, const char *Prefix);
75
76 void SetDeathCallback();
77 static void DeathCallback();
78 static Unit CurrentUnit;
79
80 size_t TotalNumberOfRuns = 0;
81
82 std::vector<Unit> Corpus;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000083 std::unordered_set<uintptr_t> FullCoverageSets;
Aaron Ballmanef116982015-01-29 16:58:29 +000084 FuzzingOptions Options;
85 system_clock::time_point ProcessStartTime = system_clock::now();
86 static system_clock::time_point UnitStartTime;
87};
88
89}; // namespace fuzzer