2015-01-30 00:58:29 +08:00
|
|
|
//===- FuzzerLoop.cpp - Fuzzer's main loop --------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Fuzzer's main loop.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FuzzerInternal.h"
|
2015-02-04 03:42:05 +08:00
|
|
|
#include <sanitizer/coverage_interface.h>
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
// Only one Fuzzer per process.
|
|
|
|
static Fuzzer *F;
|
|
|
|
|
2015-05-23 06:35:31 +08:00
|
|
|
Fuzzer::Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options)
|
|
|
|
: USF(USF), Options(Options) {
|
2015-04-01 04:13:20 +08:00
|
|
|
SetDeathCallback();
|
2015-05-12 05:16:27 +08:00
|
|
|
InitializeTraceState();
|
2015-04-01 04:13:20 +08:00
|
|
|
assert(!F);
|
|
|
|
F = this;
|
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
|
|
|
|
void Fuzzer::SetDeathCallback() {
|
2015-04-01 04:13:20 +08:00
|
|
|
__sanitizer_set_death_callback(StaticDeathCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter) {
|
|
|
|
if (Options.Tokens.empty()) {
|
|
|
|
PrintASCII(U, PrintAfter);
|
|
|
|
} else {
|
|
|
|
auto T = SubstituteTokens(U);
|
|
|
|
T.push_back(0);
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("%s%s", T.data(), PrintAfter);
|
2015-04-01 04:13:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::StaticDeathCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->DeathCallback();
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::DeathCallback() {
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("DEATH:\n");
|
2015-01-30 00:58:29 +08:00
|
|
|
Print(CurrentUnit, "\n");
|
2015-04-01 04:13:20 +08:00
|
|
|
PrintUnitInASCIIOrTokens(CurrentUnit, "\n");
|
2015-01-30 00:58:29 +08:00
|
|
|
WriteToCrash(CurrentUnit, "crash-");
|
|
|
|
}
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
void Fuzzer::StaticAlarmCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->AlarmCallback();
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
void Fuzzer::AlarmCallback() {
|
2015-05-20 06:12:57 +08:00
|
|
|
assert(Options.UnitTimeoutSec > 0);
|
2015-01-30 00:58:29 +08:00
|
|
|
size_t Seconds =
|
|
|
|
duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
|
2015-05-20 06:12:57 +08:00
|
|
|
if (Seconds == 0) return;
|
|
|
|
if (Options.Verbosity >= 2)
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("AlarmCallback %zd\n", Seconds);
|
2015-05-20 06:12:57 +08:00
|
|
|
if (Seconds >= (size_t)Options.UnitTimeoutSec) {
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds);
|
2015-05-27 04:57:47 +08:00
|
|
|
Printf(" and the timeout value is %d (use -timeout=N to change)\n",
|
|
|
|
Options.UnitTimeoutSec);
|
2015-01-30 00:58:29 +08:00
|
|
|
Print(CurrentUnit, "\n");
|
2015-04-01 04:13:20 +08:00
|
|
|
PrintUnitInASCIIOrTokens(CurrentUnit, "\n");
|
2015-01-30 00:58:29 +08:00
|
|
|
WriteToCrash(CurrentUnit, "timeout-");
|
2015-05-20 06:12:57 +08:00
|
|
|
exit(1);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 06:44:03 +08:00
|
|
|
void Fuzzer::PrintStats(const char *Where, size_t Cov, const char *End) {
|
|
|
|
if (!Options.Verbosity) return;
|
|
|
|
size_t Seconds = secondsSinceProcessStartUp();
|
|
|
|
size_t ExecPerSec = (Seconds ? TotalNumberOfRuns / Seconds : 0);
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("#%zd\t%s cov %zd bits %zd units %zd exec/s %zd %s", TotalNumberOfRuns,
|
|
|
|
Where, Cov, TotalBits(), Corpus.size(), ExecPerSec, End);
|
2015-03-31 06:44:03 +08:00
|
|
|
}
|
|
|
|
|
2015-05-09 05:30:55 +08:00
|
|
|
void Fuzzer::RereadOutputCorpus() {
|
|
|
|
if (Options.OutputCorpus.empty()) return;
|
|
|
|
std::vector<Unit> AdditionalCorpus;
|
|
|
|
ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus,
|
|
|
|
&EpochOfLastReadOfOutputCorpus);
|
|
|
|
if (Corpus.empty()) {
|
|
|
|
Corpus = AdditionalCorpus;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!Options.Reload) return;
|
2015-05-20 06:12:57 +08:00
|
|
|
if (Options.Verbosity >= 2)
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
|
2015-05-09 05:30:55 +08:00
|
|
|
for (auto &X : AdditionalCorpus) {
|
|
|
|
if (X.size() > (size_t)Options.MaxLen)
|
|
|
|
X.resize(Options.MaxLen);
|
2015-05-19 09:06:07 +08:00
|
|
|
if (UnitHashesAddedToCorpus.insert(Hash(X)).second) {
|
2015-05-09 05:30:55 +08:00
|
|
|
CurrentUnit.clear();
|
|
|
|
CurrentUnit.insert(CurrentUnit.begin(), X.begin(), X.end());
|
|
|
|
size_t NewCoverage = RunOne(CurrentUnit);
|
2015-05-19 09:06:07 +08:00
|
|
|
if (NewCoverage) {
|
|
|
|
Corpus.push_back(X);
|
|
|
|
if (Options.Verbosity >= 1)
|
|
|
|
PrintStats("RELOAD", NewCoverage);
|
|
|
|
}
|
2015-05-09 05:30:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
void Fuzzer::ShuffleAndMinimize() {
|
2015-03-31 06:44:03 +08:00
|
|
|
size_t MaxCov = 0;
|
2015-02-05 07:42:42 +08:00
|
|
|
bool PreferSmall =
|
|
|
|
(Options.PreferSmallDuringInitialShuffle == 1 ||
|
|
|
|
(Options.PreferSmallDuringInitialShuffle == -1 && rand() % 2));
|
2015-01-30 00:58:29 +08:00
|
|
|
if (Options.Verbosity)
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("PreferSmall: %d\n", PreferSmall);
|
2015-03-31 06:44:03 +08:00
|
|
|
PrintStats("READ ", 0);
|
2015-01-30 00:58:29 +08:00
|
|
|
std::vector<Unit> NewCorpus;
|
2015-02-05 07:42:42 +08:00
|
|
|
std::random_shuffle(Corpus.begin(), Corpus.end());
|
|
|
|
if (PreferSmall)
|
|
|
|
std::stable_sort(
|
|
|
|
Corpus.begin(), Corpus.end(),
|
|
|
|
[](const Unit &A, const Unit &B) { return A.size() < B.size(); });
|
2015-01-30 00:58:29 +08:00
|
|
|
Unit &U = CurrentUnit;
|
|
|
|
for (const auto &C : Corpus) {
|
|
|
|
for (size_t First = 0; First < 1; First++) {
|
|
|
|
U.clear();
|
|
|
|
size_t Last = std::min(First + Options.MaxLen, C.size());
|
|
|
|
U.insert(U.begin(), C.begin() + First, C.begin() + Last);
|
|
|
|
size_t NewCoverage = RunOne(U);
|
|
|
|
if (NewCoverage) {
|
|
|
|
MaxCov = NewCoverage;
|
|
|
|
NewCorpus.push_back(U);
|
|
|
|
if (Options.Verbosity >= 2)
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("NEW0: %zd L %zd\n", NewCoverage, U.size());
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Corpus = NewCorpus;
|
2015-05-19 09:06:07 +08:00
|
|
|
for (auto &X : Corpus)
|
|
|
|
UnitHashesAddedToCorpus.insert(Hash(X));
|
2015-03-31 06:44:03 +08:00
|
|
|
PrintStats("INITED", MaxCov);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Fuzzer::RunOne(const Unit &U) {
|
|
|
|
UnitStartTime = system_clock::now();
|
|
|
|
TotalNumberOfRuns++;
|
2015-03-31 07:04:35 +08:00
|
|
|
size_t Res = 0;
|
2015-01-30 07:01:07 +08:00
|
|
|
if (Options.UseFullCoverageSet)
|
2015-03-31 07:04:35 +08:00
|
|
|
Res = RunOneMaximizeFullCoverageSet(U);
|
|
|
|
else
|
|
|
|
Res = RunOneMaximizeTotalCoverage(U);
|
|
|
|
auto UnitStopTime = system_clock::now();
|
|
|
|
auto TimeOfUnit =
|
|
|
|
duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
|
|
|
|
if (TimeOfUnit > TimeOfLongestUnitInSeconds) {
|
|
|
|
TimeOfLongestUnitInSeconds = TimeOfUnit;
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("Longest unit: %zd s:\n", TimeOfLongestUnitInSeconds);
|
2015-03-31 07:04:35 +08:00
|
|
|
Print(U, "\n");
|
|
|
|
}
|
|
|
|
return Res;
|
2015-01-30 07:01:07 +08:00
|
|
|
}
|
|
|
|
|
2015-05-08 02:32:29 +08:00
|
|
|
void Fuzzer::RunOneAndUpdateCorpus(const Unit &U) {
|
|
|
|
if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
|
|
|
|
return;
|
|
|
|
ReportNewCoverage(RunOne(U), U);
|
|
|
|
}
|
|
|
|
|
2015-01-30 07:01:07 +08:00
|
|
|
static uintptr_t HashOfArrayOfPCs(uintptr_t *PCs, uintptr_t NumPCs) {
|
|
|
|
uintptr_t Res = 0;
|
|
|
|
for (uintptr_t i = 0; i < NumPCs; i++) {
|
|
|
|
Res = (Res + PCs[i]) * 7;
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
Unit Fuzzer::SubstituteTokens(const Unit &U) const {
|
|
|
|
Unit Res;
|
|
|
|
for (auto Idx : U) {
|
|
|
|
if (Idx < Options.Tokens.size()) {
|
|
|
|
std::string Token = Options.Tokens[Idx];
|
|
|
|
Res.insert(Res.end(), Token.begin(), Token.end());
|
|
|
|
} else {
|
|
|
|
Res.push_back(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME: Apply DFSan labels.
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::ExecuteCallback(const Unit &U) {
|
|
|
|
if (Options.Tokens.empty()) {
|
2015-05-23 06:35:31 +08:00
|
|
|
USF.TargetFunction(U.data(), U.size());
|
2015-04-01 04:13:20 +08:00
|
|
|
} else {
|
|
|
|
auto T = SubstituteTokens(U);
|
2015-05-23 06:35:31 +08:00
|
|
|
USF.TargetFunction(T.data(), T.size());
|
2015-04-01 04:13:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 11:02:37 +08:00
|
|
|
// Experimental.
|
2015-01-30 07:01:07 +08:00
|
|
|
// Fuly reset the current coverage state, run a single unit,
|
|
|
|
// compute a hash function from the full coverage set,
|
|
|
|
// return non-zero if the hash value is new.
|
|
|
|
// This produces tons of new units and as is it's only suitable for small tests,
|
|
|
|
// e.g. test/FullCoverageSetTest.cpp. FIXME: make it scale.
|
|
|
|
size_t Fuzzer::RunOneMaximizeFullCoverageSet(const Unit &U) {
|
|
|
|
__sanitizer_reset_coverage();
|
2015-04-01 04:13:20 +08:00
|
|
|
ExecuteCallback(U);
|
2015-01-30 07:01:07 +08:00
|
|
|
uintptr_t *PCs;
|
|
|
|
uintptr_t NumPCs =__sanitizer_get_coverage_guards(&PCs);
|
|
|
|
if (FullCoverageSets.insert(HashOfArrayOfPCs(PCs, NumPCs)).second)
|
|
|
|
return FullCoverageSets.size();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Fuzzer::RunOneMaximizeTotalCoverage(const Unit &U) {
|
[sanitizer/coverage] Add AFL-style coverage counters (search heuristic for fuzzing).
Introduce -mllvm -sanitizer-coverage-8bit-counters=1
which adds imprecise thread-unfriendly 8-bit coverage counters.
The run-time library maps these 8-bit counters to 8-bit bitsets in the same way
AFL (http://lcamtuf.coredump.cx/afl/technical_details.txt) does:
counter values are divided into 8 ranges and based on the counter
value one of the bits in the bitset is set.
The AFL ranges are used here: 1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+.
These counters provide a search heuristic for single-threaded
coverage-guided fuzzers, we do not expect them to be useful for other purposes.
Depending on the value of -fsanitize-coverage=[123] flag,
these counters will be added to the function entry blocks (=1),
every basic block (=2), or every edge (=3).
Use these counters as an optional search heuristic in the Fuzzer library.
Add a test where this heuristic is critical.
llvm-svn: 231166
2015-03-04 07:27:02 +08:00
|
|
|
size_t NumCounters = __sanitizer_get_number_of_counters();
|
|
|
|
if (Options.UseCounters) {
|
|
|
|
CounterBitmap.resize(NumCounters);
|
|
|
|
__sanitizer_update_counter_bitset_and_clear_counters(0);
|
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
size_t OldCoverage = __sanitizer_get_total_unique_coverage();
|
2015-04-01 04:13:20 +08:00
|
|
|
ExecuteCallback(U);
|
2015-01-30 00:58:29 +08:00
|
|
|
size_t NewCoverage = __sanitizer_get_total_unique_coverage();
|
[sanitizer/coverage] Add AFL-style coverage counters (search heuristic for fuzzing).
Introduce -mllvm -sanitizer-coverage-8bit-counters=1
which adds imprecise thread-unfriendly 8-bit coverage counters.
The run-time library maps these 8-bit counters to 8-bit bitsets in the same way
AFL (http://lcamtuf.coredump.cx/afl/technical_details.txt) does:
counter values are divided into 8 ranges and based on the counter
value one of the bits in the bitset is set.
The AFL ranges are used here: 1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+.
These counters provide a search heuristic for single-threaded
coverage-guided fuzzers, we do not expect them to be useful for other purposes.
Depending on the value of -fsanitize-coverage=[123] flag,
these counters will be added to the function entry blocks (=1),
every basic block (=2), or every edge (=3).
Use these counters as an optional search heuristic in the Fuzzer library.
Add a test where this heuristic is critical.
llvm-svn: 231166
2015-03-04 07:27:02 +08:00
|
|
|
size_t NumNewBits = 0;
|
|
|
|
if (Options.UseCounters)
|
|
|
|
NumNewBits = __sanitizer_update_counter_bitset_and_clear_counters(
|
|
|
|
CounterBitmap.data());
|
|
|
|
|
2015-03-31 06:44:03 +08:00
|
|
|
if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity)
|
|
|
|
PrintStats("pulse ", NewCoverage);
|
|
|
|
|
[sanitizer/coverage] Add AFL-style coverage counters (search heuristic for fuzzing).
Introduce -mllvm -sanitizer-coverage-8bit-counters=1
which adds imprecise thread-unfriendly 8-bit coverage counters.
The run-time library maps these 8-bit counters to 8-bit bitsets in the same way
AFL (http://lcamtuf.coredump.cx/afl/technical_details.txt) does:
counter values are divided into 8 ranges and based on the counter
value one of the bits in the bitset is set.
The AFL ranges are used here: 1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+.
These counters provide a search heuristic for single-threaded
coverage-guided fuzzers, we do not expect them to be useful for other purposes.
Depending on the value of -fsanitize-coverage=[123] flag,
these counters will be added to the function entry blocks (=1),
every basic block (=2), or every edge (=3).
Use these counters as an optional search heuristic in the Fuzzer library.
Add a test where this heuristic is critical.
llvm-svn: 231166
2015-03-04 07:27:02 +08:00
|
|
|
if (NewCoverage > OldCoverage || NumNewBits)
|
2015-01-30 00:58:29 +08:00
|
|
|
return NewCoverage;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::WriteToOutputCorpus(const Unit &U) {
|
|
|
|
if (Options.OutputCorpus.empty()) return;
|
|
|
|
std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
|
|
|
|
WriteToFile(U, Path);
|
|
|
|
if (Options.Verbosity >= 2)
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("Written to %s\n", Path.c_str());
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::WriteToCrash(const Unit &U, const char *Prefix) {
|
|
|
|
std::string Path = Prefix + Hash(U);
|
|
|
|
WriteToFile(U, Path);
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("CRASHED; file written to %s\nBase64: ", Path.c_str());
|
2015-05-06 05:59:51 +08:00
|
|
|
PrintFileAsBase64(Path);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::SaveCorpus() {
|
|
|
|
if (Options.OutputCorpus.empty()) return;
|
|
|
|
for (const auto &U : Corpus)
|
|
|
|
WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U)));
|
|
|
|
if (Options.Verbosity)
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("Written corpus of %zd files to %s\n", Corpus.size(),
|
|
|
|
Options.OutputCorpus.c_str());
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-08 02:32:29 +08:00
|
|
|
void Fuzzer::ReportNewCoverage(size_t NewCoverage, const Unit &U) {
|
|
|
|
if (!NewCoverage) return;
|
|
|
|
Corpus.push_back(U);
|
2015-05-19 09:06:07 +08:00
|
|
|
UnitHashesAddedToCorpus.insert(Hash(U));
|
2015-05-08 02:32:29 +08:00
|
|
|
PrintStats("NEW ", NewCoverage, "");
|
|
|
|
if (Options.Verbosity) {
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf(" L: %zd", U.size());
|
2015-05-08 02:32:29 +08:00
|
|
|
if (U.size() < 30) {
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf(" ");
|
2015-05-08 02:32:29 +08:00
|
|
|
PrintUnitInASCIIOrTokens(U, "\t");
|
|
|
|
Print(U);
|
|
|
|
}
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("\n");
|
2015-05-08 02:32:29 +08:00
|
|
|
}
|
|
|
|
WriteToOutputCorpus(U);
|
|
|
|
if (Options.ExitOnFirst)
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::MutateAndTestOne(Unit *U) {
|
2015-02-05 03:10:20 +08:00
|
|
|
for (int i = 0; i < Options.MutateDepth; i++) {
|
2015-05-08 05:02:11 +08:00
|
|
|
StartTraceRecording();
|
2015-05-23 06:35:31 +08:00
|
|
|
size_t Size = U->size();
|
|
|
|
U->resize(Options.MaxLen);
|
|
|
|
size_t NewSize = USF.Mutate(U->data(), Size, U->size());
|
2015-05-27 03:29:33 +08:00
|
|
|
assert(NewSize > 0 && NewSize <= (size_t)Options.MaxLen);
|
2015-05-23 06:35:31 +08:00
|
|
|
U->resize(NewSize);
|
2015-05-08 02:32:29 +08:00
|
|
|
RunOneAndUpdateCorpus(*U);
|
2015-05-08 05:02:11 +08:00
|
|
|
size_t NumTraceBasedMutations = StopTraceRecording();
|
|
|
|
for (size_t j = 0; j < NumTraceBasedMutations; j++) {
|
2015-05-17 10:44:31 +08:00
|
|
|
ApplyTraceBasedMutation(j, U);
|
|
|
|
RunOneAndUpdateCorpus(*U);
|
2015-05-08 05:02:11 +08:00
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 02:32:29 +08:00
|
|
|
void Fuzzer::Loop(size_t NumIterations) {
|
2015-01-30 00:58:29 +08:00
|
|
|
for (size_t i = 1; i <= NumIterations; i++) {
|
2015-02-05 06:20:09 +08:00
|
|
|
for (size_t J1 = 0; J1 < Corpus.size(); J1++) {
|
2015-05-19 05:34:20 +08:00
|
|
|
SyncCorpus();
|
2015-05-09 05:30:55 +08:00
|
|
|
RereadOutputCorpus();
|
2015-02-05 06:20:09 +08:00
|
|
|
if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
|
2015-05-08 02:32:29 +08:00
|
|
|
return;
|
2015-02-05 06:20:09 +08:00
|
|
|
// First, simply mutate the unit w/o doing crosses.
|
|
|
|
CurrentUnit = Corpus[J1];
|
2015-05-08 02:32:29 +08:00
|
|
|
MutateAndTestOne(&CurrentUnit);
|
2015-02-05 06:20:09 +08:00
|
|
|
// Now, cross with others.
|
2015-05-27 03:29:33 +08:00
|
|
|
if (Options.DoCrossOver && !Corpus[J1].empty()) {
|
2015-01-30 00:58:29 +08:00
|
|
|
for (size_t J2 = 0; J2 < Corpus.size(); J2++) {
|
2015-05-23 06:35:31 +08:00
|
|
|
CurrentUnit.resize(Options.MaxLen);
|
|
|
|
size_t NewSize = USF.CrossOver(
|
|
|
|
Corpus[J1].data(), Corpus[J1].size(), Corpus[J2].data(),
|
|
|
|
Corpus[J2].size(), CurrentUnit.data(), CurrentUnit.size());
|
2015-05-27 03:29:33 +08:00
|
|
|
assert(NewSize > 0 && NewSize <= (size_t)Options.MaxLen);
|
2015-05-23 06:35:31 +08:00
|
|
|
CurrentUnit.resize(NewSize);
|
2015-05-08 02:32:29 +08:00
|
|
|
MutateAndTestOne(&CurrentUnit);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 05:34:20 +08:00
|
|
|
void Fuzzer::SyncCorpus() {
|
|
|
|
if (Options.SyncCommand.empty() || Options.OutputCorpus.empty()) return;
|
|
|
|
auto Now = system_clock::now();
|
|
|
|
if (duration_cast<seconds>(Now - LastExternalSync).count() <
|
|
|
|
Options.SyncTimeout)
|
|
|
|
return;
|
|
|
|
LastExternalSync = Now;
|
|
|
|
ExecuteCommand(Options.SyncCommand + " " + Options.OutputCorpus);
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
} // namespace fuzzer
|