blob: b220c9859007ff03f865399edd02a2b536c5b1b2 [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- FuzzerLoop.cpp - Fuzzer's main loop --------------------------------===//
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// Fuzzer's main loop.
10//===----------------------------------------------------------------------===//
11
12#include "FuzzerInternal.h"
Kostya Serebryany4b96ce92015-02-03 19:42:05 +000013#include <sanitizer/coverage_interface.h>
Aaron Ballmanef116982015-01-29 16:58:29 +000014#include <algorithm>
Aaron Ballmanef116982015-01-29 16:58:29 +000015#include <iostream>
Aaron Ballmanef116982015-01-29 16:58:29 +000016
Aaron Ballmanef116982015-01-29 16:58:29 +000017namespace fuzzer {
18
19// static
20Unit Fuzzer::CurrentUnit;
21system_clock::time_point Fuzzer::UnitStartTime;
22
23void Fuzzer::SetDeathCallback() {
24 __sanitizer_set_death_callback(DeathCallback);
25}
26
27void Fuzzer::DeathCallback() {
28 std::cerr << "DEATH: " << std::endl;
29 Print(CurrentUnit, "\n");
30 PrintASCII(CurrentUnit, "\n");
31 WriteToCrash(CurrentUnit, "crash-");
32}
33
34void Fuzzer::AlarmCallback() {
35 size_t Seconds =
36 duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
37 std::cerr << "ALARM: working on the last Unit for " << Seconds << " seconds"
38 << std::endl;
39 if (Seconds >= 3) {
40 Print(CurrentUnit, "\n");
41 PrintASCII(CurrentUnit, "\n");
42 WriteToCrash(CurrentUnit, "timeout-");
43 }
44 exit(1);
45}
46
Kostya Serebryany03db8b92015-03-30 22:44:03 +000047void Fuzzer::PrintStats(const char *Where, size_t Cov, const char *End) {
48 if (!Options.Verbosity) return;
49 size_t Seconds = secondsSinceProcessStartUp();
50 size_t ExecPerSec = (Seconds ? TotalNumberOfRuns / Seconds : 0);
51 std::cerr
52 << "#" << TotalNumberOfRuns
53 << "\t" << Where
54 << " cov " << Cov
55 << " bits " << TotalBits()
56 << " units " << Corpus.size()
57 << " exec/s " << ExecPerSec
58 << End;
59}
60
Aaron Ballmanef116982015-01-29 16:58:29 +000061void Fuzzer::ShuffleAndMinimize() {
Kostya Serebryany03db8b92015-03-30 22:44:03 +000062 size_t MaxCov = 0;
Kostya Serebryany92e04762015-02-04 23:42:42 +000063 bool PreferSmall =
64 (Options.PreferSmallDuringInitialShuffle == 1 ||
65 (Options.PreferSmallDuringInitialShuffle == -1 && rand() % 2));
Aaron Ballmanef116982015-01-29 16:58:29 +000066 if (Options.Verbosity)
Kostya Serebryany03db8b92015-03-30 22:44:03 +000067 std::cerr << "PreferSmall: " << PreferSmall << "\n";
68 PrintStats("READ ", 0);
Aaron Ballmanef116982015-01-29 16:58:29 +000069 std::vector<Unit> NewCorpus;
Kostya Serebryany92e04762015-02-04 23:42:42 +000070 std::random_shuffle(Corpus.begin(), Corpus.end());
71 if (PreferSmall)
72 std::stable_sort(
73 Corpus.begin(), Corpus.end(),
74 [](const Unit &A, const Unit &B) { return A.size() < B.size(); });
Aaron Ballmanef116982015-01-29 16:58:29 +000075 Unit &U = CurrentUnit;
76 for (const auto &C : Corpus) {
77 for (size_t First = 0; First < 1; First++) {
78 U.clear();
79 size_t Last = std::min(First + Options.MaxLen, C.size());
80 U.insert(U.begin(), C.begin() + First, C.begin() + Last);
81 size_t NewCoverage = RunOne(U);
82 if (NewCoverage) {
83 MaxCov = NewCoverage;
84 NewCorpus.push_back(U);
85 if (Options.Verbosity >= 2)
Kostya Serebryany92e04762015-02-04 23:42:42 +000086 std::cerr << "NEW0: " << NewCoverage
87 << " L " << U.size()
88 << "\n";
Aaron Ballmanef116982015-01-29 16:58:29 +000089 }
90 }
91 }
92 Corpus = NewCorpus;
Kostya Serebryany03db8b92015-03-30 22:44:03 +000093 PrintStats("INITED", MaxCov);
Aaron Ballmanef116982015-01-29 16:58:29 +000094}
95
96size_t Fuzzer::RunOne(const Unit &U) {
97 UnitStartTime = system_clock::now();
98 TotalNumberOfRuns++;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000099 if (Options.UseFullCoverageSet)
100 return RunOneMaximizeFullCoverageSet(U);
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000101 if (Options.UseCoveragePairs)
102 return RunOneMaximizeCoveragePairs(U);
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000103 return RunOneMaximizeTotalCoverage(U);
104}
105
106static uintptr_t HashOfArrayOfPCs(uintptr_t *PCs, uintptr_t NumPCs) {
107 uintptr_t Res = 0;
108 for (uintptr_t i = 0; i < NumPCs; i++) {
109 Res = (Res + PCs[i]) * 7;
110 }
111 return Res;
112}
113
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000114// Experimental. Does not yet scale.
115// Fuly reset the current coverage state, run a single unit,
116// collect all coverage pairs and return non-zero if a new pair is observed.
117size_t Fuzzer::RunOneMaximizeCoveragePairs(const Unit &U) {
118 __sanitizer_reset_coverage();
119 Callback(U.data(), U.size());
120 uintptr_t *PCs;
121 uintptr_t NumPCs = __sanitizer_get_coverage_guards(&PCs);
122 bool HasNewPairs = false;
123 for (uintptr_t i = 0; i < NumPCs; i++) {
124 if (!PCs[i]) continue;
125 for (uintptr_t j = 0; j < NumPCs; j++) {
126 if (!PCs[j]) continue;
127 uint64_t Pair = (i << 32) | j;
128 HasNewPairs |= CoveragePairs.insert(Pair).second;
129 }
130 }
131 if (HasNewPairs)
132 return CoveragePairs.size();
133 return 0;
134}
135
136// Experimental.
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000137// Fuly reset the current coverage state, run a single unit,
138// compute a hash function from the full coverage set,
139// return non-zero if the hash value is new.
140// This produces tons of new units and as is it's only suitable for small tests,
141// e.g. test/FullCoverageSetTest.cpp. FIXME: make it scale.
142size_t Fuzzer::RunOneMaximizeFullCoverageSet(const Unit &U) {
143 __sanitizer_reset_coverage();
Kostya Serebryany016852c2015-02-19 18:45:37 +0000144 Callback(U.data(), U.size());
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000145 uintptr_t *PCs;
146 uintptr_t NumPCs =__sanitizer_get_coverage_guards(&PCs);
147 if (FullCoverageSets.insert(HashOfArrayOfPCs(PCs, NumPCs)).second)
148 return FullCoverageSets.size();
149 return 0;
150}
151
152size_t Fuzzer::RunOneMaximizeTotalCoverage(const Unit &U) {
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000153 size_t NumCounters = __sanitizer_get_number_of_counters();
154 if (Options.UseCounters) {
155 CounterBitmap.resize(NumCounters);
156 __sanitizer_update_counter_bitset_and_clear_counters(0);
157 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000158 size_t OldCoverage = __sanitizer_get_total_unique_coverage();
Kostya Serebryany016852c2015-02-19 18:45:37 +0000159 Callback(U.data(), U.size());
Aaron Ballmanef116982015-01-29 16:58:29 +0000160 size_t NewCoverage = __sanitizer_get_total_unique_coverage();
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000161 size_t NumNewBits = 0;
162 if (Options.UseCounters)
163 NumNewBits = __sanitizer_update_counter_bitset_and_clear_counters(
164 CounterBitmap.data());
165
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000166 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity)
167 PrintStats("pulse ", NewCoverage);
168
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000169 if (NewCoverage > OldCoverage || NumNewBits)
Aaron Ballmanef116982015-01-29 16:58:29 +0000170 return NewCoverage;
171 return 0;
172}
173
174void Fuzzer::WriteToOutputCorpus(const Unit &U) {
175 if (Options.OutputCorpus.empty()) return;
176 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
177 WriteToFile(U, Path);
178 if (Options.Verbosity >= 2)
179 std::cerr << "Written to " << Path << std::endl;
180}
181
182void Fuzzer::WriteToCrash(const Unit &U, const char *Prefix) {
183 std::string Path = Prefix + Hash(U);
184 WriteToFile(U, Path);
185 std::cerr << "CRASHED; file written to " << Path << std::endl;
186}
187
188void Fuzzer::SaveCorpus() {
189 if (Options.OutputCorpus.empty()) return;
190 for (const auto &U : Corpus)
191 WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U)));
192 if (Options.Verbosity)
193 std::cerr << "Written corpus of " << Corpus.size() << " files to "
194 << Options.OutputCorpus << "\n";
195}
196
197size_t Fuzzer::MutateAndTestOne(Unit *U) {
198 size_t NewUnits = 0;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000199 for (int i = 0; i < Options.MutateDepth; i++) {
Kostya Serebryany33f86692015-02-04 22:20:09 +0000200 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
201 return NewUnits;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000202 MutateWithDFSan(U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000203 Mutate(U, Options.MaxLen);
Aaron Ballmanef116982015-01-29 16:58:29 +0000204 size_t NewCoverage = RunOne(*U);
205 if (NewCoverage) {
206 Corpus.push_back(*U);
207 NewUnits++;
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000208 PrintStats("NEW ", NewCoverage, "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000209 if (Options.Verbosity) {
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000210 std::cerr << " L: " << U->size();
Aaron Ballmanef116982015-01-29 16:58:29 +0000211 if (U->size() < 30) {
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000212 std::cerr << " ";
Aaron Ballmanef116982015-01-29 16:58:29 +0000213 PrintASCII(*U);
214 std::cerr << "\t";
215 Print(*U);
216 }
217 std::cerr << "\n";
218 }
219 WriteToOutputCorpus(*U);
220 if (Options.ExitOnFirst)
221 exit(0);
222 }
223 }
224 return NewUnits;
225}
226
227size_t Fuzzer::Loop(size_t NumIterations) {
228 size_t NewUnits = 0;
229 for (size_t i = 1; i <= NumIterations; i++) {
Kostya Serebryany33f86692015-02-04 22:20:09 +0000230 for (size_t J1 = 0; J1 < Corpus.size(); J1++) {
231 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
232 return NewUnits;
233 // First, simply mutate the unit w/o doing crosses.
234 CurrentUnit = Corpus[J1];
235 NewUnits += MutateAndTestOne(&CurrentUnit);
236 // Now, cross with others.
237 if (Options.DoCrossOver) {
Aaron Ballmanef116982015-01-29 16:58:29 +0000238 for (size_t J2 = 0; J2 < Corpus.size(); J2++) {
239 CurrentUnit.clear();
240 CrossOver(Corpus[J1], Corpus[J2], &CurrentUnit, Options.MaxLen);
241 NewUnits += MutateAndTestOne(&CurrentUnit);
242 }
243 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000244 }
245 }
246 return NewUnits;
247}
248
249} // namespace fuzzer