1 //===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Define the main class fuzzer::Fuzzer and most functions.
10 //===----------------------------------------------------------------------===//
19 typedef std::vector<uint8_t> Unit;
20 using namespace std::chrono;
22 Unit ReadFile(const char *Path);
23 std::vector<std::string> ListFilesInDir(const std::string &Dir);
24 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V);
25 void WriteToFile(const Unit &U, const std::string &Path);
27 void Mutate(Unit *U, size_t MaxLen);
29 void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
31 void Print(const Unit &U, const char *PrintAfter = "");
32 void PrintASCII(const Unit &U, const char *PrintAfter = "");
33 std::string Hash(const Unit &U);
34 void SetTimer(int Seconds);
38 struct FuzzingOptions {
41 bool DoCrossOver = true;
42 bool MutateDepth = 10;
43 bool ExitOnFirst = false;
44 std::string OutputCorpus;
46 Fuzzer(FuzzingOptions Options) : Options(Options) {
49 void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
50 size_t Loop(size_t NumIterations);
51 void ShuffleAndMinimize();
52 size_t CorpusSize() const { return Corpus.size(); }
53 void ReadDir(const std::string &Path) {
54 ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
57 static void AlarmCallback();
60 size_t MutateAndTestOne(Unit *U);
61 size_t RunOne(const Unit &U);
62 void WriteToOutputCorpus(const Unit &U);
63 static void WriteToCrash(const Unit &U, const char *Prefix);
65 void SetDeathCallback();
66 static void DeathCallback();
67 static Unit CurrentUnit;
69 size_t TotalNumberOfRuns = 0;
71 std::vector<Unit> Corpus;
72 FuzzingOptions Options;
73 system_clock::time_point ProcessStartTime = system_clock::now();
74 static system_clock::time_point UnitStartTime;
77 }; // namespace fuzzer