blob: 45379711db44e7a9cd6fdbb83d64e767510732f5 [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>
12#include <chrono>
13#include <cstddef>
14#include <cstdlib>
15#include <string>
16#include <vector>
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000017#include <unordered_set>
Aaron Ballmanef116982015-01-29 16:58:29 +000018
19namespace fuzzer {
20typedef std::vector<uint8_t> Unit;
21using namespace std::chrono;
22
23Unit ReadFile(const char *Path);
24void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V);
25void WriteToFile(const Unit &U, const std::string &Path);
26// Returns "Dir/FileName" or equivalent for the current OS.
27std::string DirPlusFile(const std::string &DirPath,
28 const std::string &FileName);
29
30void Mutate(Unit *U, size_t MaxLen);
31
32void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
33
34void Print(const Unit &U, const char *PrintAfter = "");
35void PrintASCII(const Unit &U, const char *PrintAfter = "");
36std::string Hash(const Unit &U);
37void SetTimer(int Seconds);
38
39class Fuzzer {
40 public:
41 struct FuzzingOptions {
42 int Verbosity = 1;
43 int MaxLen = 0;
44 bool DoCrossOver = true;
45 bool MutateDepth = 10;
46 bool ExitOnFirst = false;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000047 bool UseFullCoverageSet = false;
Aaron Ballmanef116982015-01-29 16:58:29 +000048 std::string OutputCorpus;
49 };
50 Fuzzer(FuzzingOptions Options) : Options(Options) {
51 SetDeathCallback();
52 }
53 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
54 size_t Loop(size_t NumIterations);
55 void ShuffleAndMinimize();
56 size_t CorpusSize() const { return Corpus.size(); }
57 void ReadDir(const std::string &Path) {
58 ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
59 }
60 // Save the current corpus to OutputCorpus.
61 void SaveCorpus();
62
63 static void AlarmCallback();
64
65 private:
66 size_t MutateAndTestOne(Unit *U);
67 size_t RunOne(const Unit &U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000068 size_t RunOneMaximizeTotalCoverage(const Unit &U);
69 size_t RunOneMaximizeFullCoverageSet(const Unit &U);
Aaron Ballmanef116982015-01-29 16:58:29 +000070 void WriteToOutputCorpus(const Unit &U);
71 static void WriteToCrash(const Unit &U, const char *Prefix);
72
73 void SetDeathCallback();
74 static void DeathCallback();
75 static Unit CurrentUnit;
76
77 size_t TotalNumberOfRuns = 0;
78
79 std::vector<Unit> Corpus;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000080 std::unordered_set<uintptr_t> FullCoverageSets;
Aaron Ballmanef116982015-01-29 16:58:29 +000081 FuzzingOptions Options;
82 system_clock::time_point ProcessStartTime = system_clock::now();
83 static system_clock::time_point UnitStartTime;
84};
85
86}; // namespace fuzzer