blob: 7d152f19eaf89d77ede7751cf82d00497e49bac3 [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 Serebryany16901a92015-03-30 23:04:35 +000099 size_t Res = 0;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000100 if (Options.UseFullCoverageSet)
Kostya Serebryany16901a92015-03-30 23:04:35 +0000101 Res = RunOneMaximizeFullCoverageSet(U);
102 else if (Options.UseCoveragePairs)
103 Res = RunOneMaximizeCoveragePairs(U);
104 else
105 Res = RunOneMaximizeTotalCoverage(U);
106 auto UnitStopTime = system_clock::now();
107 auto TimeOfUnit =
108 duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
109 if (TimeOfUnit > TimeOfLongestUnitInSeconds) {
110 TimeOfLongestUnitInSeconds = TimeOfUnit;
111 std::cerr << "Longest unit: " << TimeOfLongestUnitInSeconds
112 << " s:\n";
113 Print(U, "\n");
114 }
115 return Res;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000116}
117
118static uintptr_t HashOfArrayOfPCs(uintptr_t *PCs, uintptr_t NumPCs) {
119 uintptr_t Res = 0;
120 for (uintptr_t i = 0; i < NumPCs; i++) {
121 Res = (Res + PCs[i]) * 7;
122 }
123 return Res;
124}
125
Kostya Serebryany2e3622b2015-02-20 03:02:37 +0000126// Experimental. Does not yet scale.
127// Fuly reset the current coverage state, run a single unit,
128// collect all coverage pairs and return non-zero if a new pair is observed.
129size_t Fuzzer::RunOneMaximizeCoveragePairs(const Unit &U) {
130 __sanitizer_reset_coverage();
131 Callback(U.data(), U.size());
132 uintptr_t *PCs;
133 uintptr_t NumPCs = __sanitizer_get_coverage_guards(&PCs);
134 bool HasNewPairs = false;
135 for (uintptr_t i = 0; i < NumPCs; i++) {
136 if (!PCs[i]) continue;
137 for (uintptr_t j = 0; j < NumPCs; j++) {
138 if (!PCs[j]) continue;
139 uint64_t Pair = (i << 32) | j;
140 HasNewPairs |= CoveragePairs.insert(Pair).second;
141 }
142 }
143 if (HasNewPairs)
144 return CoveragePairs.size();
145 return 0;
146}
147
148// Experimental.
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000149// Fuly reset the current coverage state, run a single unit,
150// compute a hash function from the full coverage set,
151// return non-zero if the hash value is new.
152// This produces tons of new units and as is it's only suitable for small tests,
153// e.g. test/FullCoverageSetTest.cpp. FIXME: make it scale.
154size_t Fuzzer::RunOneMaximizeFullCoverageSet(const Unit &U) {
155 __sanitizer_reset_coverage();
Kostya Serebryany016852c2015-02-19 18:45:37 +0000156 Callback(U.data(), U.size());
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +0000157 uintptr_t *PCs;
158 uintptr_t NumPCs =__sanitizer_get_coverage_guards(&PCs);
159 if (FullCoverageSets.insert(HashOfArrayOfPCs(PCs, NumPCs)).second)
160 return FullCoverageSets.size();
161 return 0;
162}
163
164size_t Fuzzer::RunOneMaximizeTotalCoverage(const Unit &U) {
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000165 size_t NumCounters = __sanitizer_get_number_of_counters();
166 if (Options.UseCounters) {
167 CounterBitmap.resize(NumCounters);
168 __sanitizer_update_counter_bitset_and_clear_counters(0);
169 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000170 size_t OldCoverage = __sanitizer_get_total_unique_coverage();
Kostya Serebryany016852c2015-02-19 18:45:37 +0000171 Callback(U.data(), U.size());
Aaron Ballmanef116982015-01-29 16:58:29 +0000172 size_t NewCoverage = __sanitizer_get_total_unique_coverage();
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000173 size_t NumNewBits = 0;
174 if (Options.UseCounters)
175 NumNewBits = __sanitizer_update_counter_bitset_and_clear_counters(
176 CounterBitmap.data());
177
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000178 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity)
179 PrintStats("pulse ", NewCoverage);
180
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000181 if (NewCoverage > OldCoverage || NumNewBits)
Aaron Ballmanef116982015-01-29 16:58:29 +0000182 return NewCoverage;
183 return 0;
184}
185
186void Fuzzer::WriteToOutputCorpus(const Unit &U) {
187 if (Options.OutputCorpus.empty()) return;
188 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
189 WriteToFile(U, Path);
190 if (Options.Verbosity >= 2)
191 std::cerr << "Written to " << Path << std::endl;
192}
193
194void Fuzzer::WriteToCrash(const Unit &U, const char *Prefix) {
195 std::string Path = Prefix + Hash(U);
196 WriteToFile(U, Path);
197 std::cerr << "CRASHED; file written to " << Path << std::endl;
198}
199
200void Fuzzer::SaveCorpus() {
201 if (Options.OutputCorpus.empty()) return;
202 for (const auto &U : Corpus)
203 WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U)));
204 if (Options.Verbosity)
205 std::cerr << "Written corpus of " << Corpus.size() << " files to "
206 << Options.OutputCorpus << "\n";
207}
208
209size_t Fuzzer::MutateAndTestOne(Unit *U) {
210 size_t NewUnits = 0;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000211 for (int i = 0; i < Options.MutateDepth; i++) {
Kostya Serebryany33f86692015-02-04 22:20:09 +0000212 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
213 return NewUnits;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000214 MutateWithDFSan(U);
Aaron Ballmanef116982015-01-29 16:58:29 +0000215 Mutate(U, Options.MaxLen);
Aaron Ballmanef116982015-01-29 16:58:29 +0000216 size_t NewCoverage = RunOne(*U);
217 if (NewCoverage) {
218 Corpus.push_back(*U);
219 NewUnits++;
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000220 PrintStats("NEW ", NewCoverage, "");
Aaron Ballmanef116982015-01-29 16:58:29 +0000221 if (Options.Verbosity) {
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000222 std::cerr << " L: " << U->size();
Aaron Ballmanef116982015-01-29 16:58:29 +0000223 if (U->size() < 30) {
Kostya Serebryany03db8b92015-03-30 22:44:03 +0000224 std::cerr << " ";
Aaron Ballmanef116982015-01-29 16:58:29 +0000225 PrintASCII(*U);
226 std::cerr << "\t";
227 Print(*U);
228 }
229 std::cerr << "\n";
230 }
231 WriteToOutputCorpus(*U);
232 if (Options.ExitOnFirst)
233 exit(0);
234 }
235 }
236 return NewUnits;
237}
238
239size_t Fuzzer::Loop(size_t NumIterations) {
240 size_t NewUnits = 0;
241 for (size_t i = 1; i <= NumIterations; i++) {
Kostya Serebryany33f86692015-02-04 22:20:09 +0000242 for (size_t J1 = 0; J1 < Corpus.size(); J1++) {
243 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
244 return NewUnits;
245 // First, simply mutate the unit w/o doing crosses.
246 CurrentUnit = Corpus[J1];
247 NewUnits += MutateAndTestOne(&CurrentUnit);
248 // Now, cross with others.
249 if (Options.DoCrossOver) {
Aaron Ballmanef116982015-01-29 16:58:29 +0000250 for (size_t J2 = 0; J2 < Corpus.size(); J2++) {
251 CurrentUnit.clear();
252 CrossOver(Corpus[J1], Corpus[J2], &CurrentUnit, Options.MaxLen);
253 NewUnits += MutateAndTestOne(&CurrentUnit);
254 }
255 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000256 }
257 }
258 return NewUnits;
259}
260
261} // namespace fuzzer