blob: 042858951815b8f2f3de7e643ec193f4586bf35f [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
17// This function should be defined by the user.
18extern "C" void TestOneInput(const uint8_t *Data, size_t Size);
19
20namespace fuzzer {
21
22// static
23Unit Fuzzer::CurrentUnit;
24system_clock::time_point Fuzzer::UnitStartTime;
25
26void Fuzzer::SetDeathCallback() {
27 __sanitizer_set_death_callback(DeathCallback);
28}
29
30void Fuzzer::DeathCallback() {
31 std::cerr << "DEATH: " << std::endl;
32 Print(CurrentUnit, "\n");
33 PrintASCII(CurrentUnit, "\n");
34 WriteToCrash(CurrentUnit, "crash-");
35}
36
37void Fuzzer::AlarmCallback() {
38 size_t Seconds =
39 duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
40 std::cerr << "ALARM: working on the last Unit for " << Seconds << " seconds"
41 << std::endl;
42 if (Seconds >= 3) {
43 Print(CurrentUnit, "\n");
44 PrintASCII(CurrentUnit, "\n");
45 WriteToCrash(CurrentUnit, "timeout-");
46 }
47 exit(1);
48}
49
50void Fuzzer::ShuffleAndMinimize() {
51 if (Options.Verbosity)
52 std::cerr << "Shuffle: " << Corpus.size() << "\n";
53 std::vector<Unit> NewCorpus;
54 random_shuffle(Corpus.begin(), Corpus.end());
55 size_t MaxCov = 0;
56 Unit &U = CurrentUnit;
57 for (const auto &C : Corpus) {
58 for (size_t First = 0; First < 1; First++) {
59 U.clear();
60 size_t Last = std::min(First + Options.MaxLen, C.size());
61 U.insert(U.begin(), C.begin() + First, C.begin() + Last);
62 size_t NewCoverage = RunOne(U);
63 if (NewCoverage) {
64 MaxCov = NewCoverage;
65 NewCorpus.push_back(U);
66 if (Options.Verbosity >= 2)
67 std::cerr << "NEW0: " << NewCoverage << "\n";
68 }
69 }
70 }
71 Corpus = NewCorpus;
72 if (Options.Verbosity)
73 std::cerr << "Shuffle done: " << Corpus.size() << " IC: " << MaxCov << "\n";
74}
75
76size_t Fuzzer::RunOne(const Unit &U) {
77 UnitStartTime = system_clock::now();
78 TotalNumberOfRuns++;
Kostya Serebryany2c1b33b2015-01-29 23:01:07 +000079 if (Options.UseFullCoverageSet)
80 return RunOneMaximizeFullCoverageSet(U);
81 return RunOneMaximizeTotalCoverage(U);
82}
83
84static uintptr_t HashOfArrayOfPCs(uintptr_t *PCs, uintptr_t NumPCs) {
85 uintptr_t Res = 0;
86 for (uintptr_t i = 0; i < NumPCs; i++) {
87 Res = (Res + PCs[i]) * 7;
88 }
89 return Res;
90}
91
92// Fuly reset the current coverage state, run a single unit,
93// compute a hash function from the full coverage set,
94// return non-zero if the hash value is new.
95// This produces tons of new units and as is it's only suitable for small tests,
96// e.g. test/FullCoverageSetTest.cpp. FIXME: make it scale.
97size_t Fuzzer::RunOneMaximizeFullCoverageSet(const Unit &U) {
98 __sanitizer_reset_coverage();
99 TestOneInput(U.data(), U.size());
100 uintptr_t *PCs;
101 uintptr_t NumPCs =__sanitizer_get_coverage_guards(&PCs);
102 if (FullCoverageSets.insert(HashOfArrayOfPCs(PCs, NumPCs)).second)
103 return FullCoverageSets.size();
104 return 0;
105}
106
107size_t Fuzzer::RunOneMaximizeTotalCoverage(const Unit &U) {
Aaron Ballmanef116982015-01-29 16:58:29 +0000108 size_t OldCoverage = __sanitizer_get_total_unique_coverage();
109 TestOneInput(U.data(), U.size());
110 size_t NewCoverage = __sanitizer_get_total_unique_coverage();
111 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity) {
112 size_t Seconds =
113 duration_cast<seconds>(system_clock::now() - ProcessStartTime).count();
114 std::cerr
115 << "#" << TotalNumberOfRuns
116 << "\tcov: " << NewCoverage
117 << "\texec/s: " << (Seconds ? TotalNumberOfRuns / Seconds : 0) << "\n";
118 }
119 if (NewCoverage > OldCoverage)
120 return NewCoverage;
121 return 0;
122}
123
124void Fuzzer::WriteToOutputCorpus(const Unit &U) {
125 if (Options.OutputCorpus.empty()) return;
126 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
127 WriteToFile(U, Path);
128 if (Options.Verbosity >= 2)
129 std::cerr << "Written to " << Path << std::endl;
130}
131
132void Fuzzer::WriteToCrash(const Unit &U, const char *Prefix) {
133 std::string Path = Prefix + Hash(U);
134 WriteToFile(U, Path);
135 std::cerr << "CRASHED; file written to " << Path << std::endl;
136}
137
138void Fuzzer::SaveCorpus() {
139 if (Options.OutputCorpus.empty()) return;
140 for (const auto &U : Corpus)
141 WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U)));
142 if (Options.Verbosity)
143 std::cerr << "Written corpus of " << Corpus.size() << " files to "
144 << Options.OutputCorpus << "\n";
145}
146
147size_t Fuzzer::MutateAndTestOne(Unit *U) {
148 size_t NewUnits = 0;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000149 for (int i = 0; i < Options.MutateDepth; i++) {
Aaron Ballmanef116982015-01-29 16:58:29 +0000150 Mutate(U, Options.MaxLen);
Aaron Ballmanef116982015-01-29 16:58:29 +0000151 size_t NewCoverage = RunOne(*U);
152 if (NewCoverage) {
153 Corpus.push_back(*U);
154 NewUnits++;
155 if (Options.Verbosity) {
156 std::cerr << "#" << TotalNumberOfRuns
157 << "\tNEW: " << NewCoverage
158 << " L: " << U->size()
Kostya Serebryanyfe43aa82015-02-04 01:22:57 +0000159 << " S: " << Corpus.size()
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000160 << " I: " << i
Aaron Ballmanef116982015-01-29 16:58:29 +0000161 << "\t";
162 if (U->size() < 30) {
163 PrintASCII(*U);
164 std::cerr << "\t";
165 Print(*U);
166 }
167 std::cerr << "\n";
168 }
169 WriteToOutputCorpus(*U);
170 if (Options.ExitOnFirst)
171 exit(0);
172 }
173 }
174 return NewUnits;
175}
176
177size_t Fuzzer::Loop(size_t NumIterations) {
178 size_t NewUnits = 0;
179 for (size_t i = 1; i <= NumIterations; i++) {
180 if (Options.DoCrossOver) {
181 for (size_t J1 = 0; J1 < Corpus.size(); J1++) {
182 for (size_t J2 = 0; J2 < Corpus.size(); J2++) {
183 CurrentUnit.clear();
184 CrossOver(Corpus[J1], Corpus[J2], &CurrentUnit, Options.MaxLen);
185 NewUnits += MutateAndTestOne(&CurrentUnit);
186 }
187 }
188 } else { // No CrossOver
189 for (size_t J = 0; J < Corpus.size(); J++) {
190 CurrentUnit = Corpus[J];
191 NewUnits += MutateAndTestOne(&CurrentUnit);
192 }
193 }
194 }
195 return NewUnits;
196}
197
198} // namespace fuzzer