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>
20 Unit Fuzzer::CurrentUnit;
21 system_clock::time_point Fuzzer::UnitStartTime;
23 void Fuzzer::SetDeathCallback() {
24 __sanitizer_set_death_callback(DeathCallback);
27 void Fuzzer::DeathCallback() {
28 std::cerr << "DEATH: " << std::endl;
29 Print(CurrentUnit, "\n");
30 PrintASCII(CurrentUnit, "\n");
31 WriteToCrash(CurrentUnit, "crash-");
34 void Fuzzer::AlarmCallback() {
36 duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
37 std::cerr << "ALARM: working on the last Unit for " << Seconds << " seconds"
40 Print(CurrentUnit, "\n");
41 PrintASCII(CurrentUnit, "\n");
42 WriteToCrash(CurrentUnit, "timeout-");
47 void Fuzzer::ShuffleAndMinimize() {
49 (Options.PreferSmallDuringInitialShuffle == 1 ||
50 (Options.PreferSmallDuringInitialShuffle == -1 && rand() % 2));
51 if (Options.Verbosity)
52 std::cerr << "Shuffle: Size: " << Corpus.size()
53 << " prefer small: " << PreferSmall
55 std::vector<Unit> NewCorpus;
56 std::random_shuffle(Corpus.begin(), Corpus.end());
59 Corpus.begin(), Corpus.end(),
60 [](const Unit &A, const Unit &B) { return A.size() < B.size(); });
62 Unit &U = CurrentUnit;
63 for (const auto &C : Corpus) {
64 for (size_t First = 0; First < 1; First++) {
66 size_t Last = std::min(First + Options.MaxLen, C.size());
67 U.insert(U.begin(), C.begin() + First, C.begin() + Last);
68 size_t NewCoverage = RunOne(U);
71 NewCorpus.push_back(U);
72 if (Options.Verbosity >= 2)
73 std::cerr << "NEW0: " << NewCoverage
80 if (Options.Verbosity)
81 std::cerr << "Shuffle done: " << Corpus.size() << " IC: " << MaxCov << "\n";
84 size_t Fuzzer::RunOne(const Unit &U) {
85 UnitStartTime = system_clock::now();
87 if (Options.UseFullCoverageSet)
88 return RunOneMaximizeFullCoverageSet(U);
89 if (Options.UseCoveragePairs)
90 return RunOneMaximizeCoveragePairs(U);
91 return RunOneMaximizeTotalCoverage(U);
94 static uintptr_t HashOfArrayOfPCs(uintptr_t *PCs, uintptr_t NumPCs) {
96 for (uintptr_t i = 0; i < NumPCs; i++) {
97 Res = (Res + PCs[i]) * 7;
102 // Experimental. Does not yet scale.
103 // Fuly reset the current coverage state, run a single unit,
104 // collect all coverage pairs and return non-zero if a new pair is observed.
105 size_t Fuzzer::RunOneMaximizeCoveragePairs(const Unit &U) {
106 __sanitizer_reset_coverage();
107 Callback(U.data(), U.size());
109 uintptr_t NumPCs = __sanitizer_get_coverage_guards(&PCs);
110 bool HasNewPairs = false;
111 for (uintptr_t i = 0; i < NumPCs; i++) {
112 if (!PCs[i]) continue;
113 for (uintptr_t j = 0; j < NumPCs; j++) {
114 if (!PCs[j]) continue;
115 uint64_t Pair = (i << 32) | j;
116 HasNewPairs |= CoveragePairs.insert(Pair).second;
120 return CoveragePairs.size();
125 // Fuly reset the current coverage state, run a single unit,
126 // compute a hash function from the full coverage set,
127 // return non-zero if the hash value is new.
128 // This produces tons of new units and as is it's only suitable for small tests,
129 // e.g. test/FullCoverageSetTest.cpp. FIXME: make it scale.
130 size_t Fuzzer::RunOneMaximizeFullCoverageSet(const Unit &U) {
131 __sanitizer_reset_coverage();
132 Callback(U.data(), U.size());
134 uintptr_t NumPCs =__sanitizer_get_coverage_guards(&PCs);
135 if (FullCoverageSets.insert(HashOfArrayOfPCs(PCs, NumPCs)).second)
136 return FullCoverageSets.size();
140 size_t Fuzzer::RunOneMaximizeTotalCoverage(const Unit &U) {
141 size_t OldCoverage = __sanitizer_get_total_unique_coverage();
142 Callback(U.data(), U.size());
143 size_t NewCoverage = __sanitizer_get_total_unique_coverage();
144 if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity) {
145 size_t Seconds = secondsSinceProcessStartUp();
147 << "#" << TotalNumberOfRuns
148 << "\tcov: " << NewCoverage
149 << "\texec/s: " << (Seconds ? TotalNumberOfRuns / Seconds : 0) << "\n";
151 if (NewCoverage > OldCoverage)
156 void Fuzzer::WriteToOutputCorpus(const Unit &U) {
157 if (Options.OutputCorpus.empty()) return;
158 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
159 WriteToFile(U, Path);
160 if (Options.Verbosity >= 2)
161 std::cerr << "Written to " << Path << std::endl;
164 void Fuzzer::WriteToCrash(const Unit &U, const char *Prefix) {
165 std::string Path = Prefix + Hash(U);
166 WriteToFile(U, Path);
167 std::cerr << "CRASHED; file written to " << Path << std::endl;
170 void Fuzzer::SaveCorpus() {
171 if (Options.OutputCorpus.empty()) return;
172 for (const auto &U : Corpus)
173 WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U)));
174 if (Options.Verbosity)
175 std::cerr << "Written corpus of " << Corpus.size() << " files to "
176 << Options.OutputCorpus << "\n";
179 size_t Fuzzer::MutateAndTestOne(Unit *U) {
181 for (int i = 0; i < Options.MutateDepth; i++) {
182 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
184 Mutate(U, Options.MaxLen);
185 size_t NewCoverage = RunOne(*U);
187 Corpus.push_back(*U);
189 if (Options.Verbosity) {
190 std::cerr << "#" << TotalNumberOfRuns
191 << "\tNEW: " << NewCoverage
192 << " L: " << U->size()
193 << " S: " << Corpus.size()
196 if (U->size() < 30) {
203 WriteToOutputCorpus(*U);
204 if (Options.ExitOnFirst)
211 size_t Fuzzer::Loop(size_t NumIterations) {
213 for (size_t i = 1; i <= NumIterations; i++) {
214 for (size_t J1 = 0; J1 < Corpus.size(); J1++) {
215 if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
217 // First, simply mutate the unit w/o doing crosses.
218 CurrentUnit = Corpus[J1];
219 NewUnits += MutateAndTestOne(&CurrentUnit);
220 // Now, cross with others.
221 if (Options.DoCrossOver) {
222 for (size_t J2 = 0; J2 < Corpus.size(); J2++) {
224 CrossOver(Corpus[J1], Corpus[J2], &CurrentUnit, Options.MaxLen);
225 NewUnits += MutateAndTestOne(&CurrentUnit);
233 } // namespace fuzzer