1 //===- FuzzerLoop.cpp - Fuzzer's main loop --------------------------------===//
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 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #include "FuzzerInternal.h"
13 #include <sanitizer/coverage_interface.h>
17 // This function should be defined by the user.
18 extern "C" void TestOneInput(const uint8_t *Data, size_t Size);
23 Unit Fuzzer::CurrentUnit;
24 system_clock::time_point Fuzzer::UnitStartTime;
26 void Fuzzer::SetDeathCallback() {
27 __sanitizer_set_death_callback(DeathCallback);
30 void Fuzzer::DeathCallback() {
31 std::cerr << "DEATH: " << std::endl;
32 Print(CurrentUnit, "\n");
33 PrintASCII(CurrentUnit, "\n");
34 WriteToCrash(CurrentUnit, "crash-");
37 void Fuzzer::AlarmCallback() {
39 duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
40 std::cerr << "ALARM: working on the last Unit for " << Seconds << " seconds"
43 Print(CurrentUnit, "\n");
44 PrintASCII(CurrentUnit, "\n");
45 WriteToCrash(CurrentUnit, "timeout-");
50 void Fuzzer::ShuffleAndMinimize() {
52 (Options.PreferSmallDuringInitialShuffle == 1 ||
53 (Options.PreferSmallDuringInitialShuffle == -1 && rand() % 2));
54 if (Options.Verbosity)
55 std::cerr << "Shuffle: Size: " << Corpus.size()
56 << " prefer small: " << PreferSmall
58 std::vector<Unit> NewCorpus;
59 std::random_shuffle(Corpus.begin(), Corpus.end());
62 Corpus.begin(), Corpus.end(),
63 [](const Unit &A, const Unit &B) { return A.size() < B.size(); });
65 Unit &U = CurrentUnit;
66 for (const auto &C : Corpus) {
67 for (size_t First = 0; First < 1; First++) {
69 size_t Last = std::min(First + Options.MaxLen, C.size());
70 U.insert(U.begin(), C.begin() + First, C.begin() + Last);
71 size_t NewCoverage = RunOne(U);
74 NewCorpus.push_back(U);
75 if (Options.Verbosity >= 2)
76 std::cerr << "NEW0: " << NewCoverage
83 if (Options.Verbosity)
84 std::cerr << "Shuffle done: " << Corpus.size() << " IC: " << MaxCov << "\n";
87 size_t Fuzzer::RunOne(const Unit &U) {
88 UnitStartTime = system_clock::now();
90 if (Options.UseFullCoverageSet)
91 return RunOneMaximizeFullCoverageSet(U);
92 return RunOneMaximizeTotalCoverage(U);
95 static uintptr_t HashOfArrayOfPCs(uintptr_t *PCs, uintptr_t NumPCs) {
97 for (uintptr_t i = 0; i < NumPCs; i++) {
98 Res = (Res + PCs[i]) * 7;
103 // Fuly reset the current coverage state, run a single unit,
104 // compute a hash function from the full coverage set,
105 // return non-zero if the hash value is new.
106 // This produces tons of new units and as is it's only suitable for small tests,
107 // e.g. test/FullCoverageSetTest.cpp. FIXME: make it scale.
108 size_t Fuzzer::RunOneMaximizeFullCoverageSet(const Unit &U) {
109 __sanitizer_reset_coverage();
110 TestOneInput(U.data(), U.size());
112 uintptr_t NumPCs =__sanitizer_get_coverage_guards(&PCs);
113 if (FullCoverageSets.insert(HashOfArrayOfPCs(PCs, NumPCs)).second)
114 return FullCoverageSets.size();
118 size_t Fuzzer::RunOneMaximizeTotalCoverage(const Unit &U) {
119 size_t OldCoverage = __sanitizer_get_total_unique_coverage();
120 TestOneInput(U.data(), U.size());
121 size_t NewCoverage = __sanitizer_get_total_unique_coverage();
122 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity) {
123 size_t Seconds = secondsSinceProcessStartUp();
125 << "#" << TotalNumberOfRuns
126 << "\tcov: " << NewCoverage
127 << "\texec/s: " << (Seconds ? TotalNumberOfRuns / Seconds : 0) << "\n";
129 if (NewCoverage > OldCoverage)
134 void Fuzzer::WriteToOutputCorpus(const Unit &U) {
135 if (Options.OutputCorpus.empty()) return;
136 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
137 WriteToFile(U, Path);
138 if (Options.Verbosity >= 2)
139 std::cerr << "Written to " << Path << std::endl;
142 void Fuzzer::WriteToCrash(const Unit &U, const char *Prefix) {
143 std::string Path = Prefix + Hash(U);
144 WriteToFile(U, Path);
145 std::cerr << "CRASHED; file written to " << Path << std::endl;
148 void Fuzzer::SaveCorpus() {
149 if (Options.OutputCorpus.empty()) return;
150 for (const auto &U : Corpus)
151 WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U)));
152 if (Options.Verbosity)
153 std::cerr << "Written corpus of " << Corpus.size() << " files to "
154 << Options.OutputCorpus << "\n";
157 size_t Fuzzer::MutateAndTestOne(Unit *U) {
159 for (int i = 0; i < Options.MutateDepth; i++) {
160 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
162 Mutate(U, Options.MaxLen);
163 size_t NewCoverage = RunOne(*U);
165 Corpus.push_back(*U);
167 if (Options.Verbosity) {
168 std::cerr << "#" << TotalNumberOfRuns
169 << "\tNEW: " << NewCoverage
170 << " L: " << U->size()
171 << " S: " << Corpus.size()
174 if (U->size() < 30) {
181 WriteToOutputCorpus(*U);
182 if (Options.ExitOnFirst)
189 size_t Fuzzer::Loop(size_t NumIterations) {
191 for (size_t i = 1; i <= NumIterations; i++) {
192 for (size_t J1 = 0; J1 < Corpus.size(); J1++) {
193 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
195 // First, simply mutate the unit w/o doing crosses.
196 CurrentUnit = Corpus[J1];
197 NewUnits += MutateAndTestOne(&CurrentUnit);
198 // Now, cross with others.
199 if (Options.DoCrossOver) {
200 for (size_t J2 = 0; J2 < Corpus.size(); J2++) {
202 CrossOver(Corpus[J1], Corpus[J2], &CurrentUnit, Options.MaxLen);
203 NewUnits += MutateAndTestOne(&CurrentUnit);
211 } // namespace fuzzer