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"
|
|
|
|
#include <algorithm>
|
|
|
|
|
2015-11-13 09:54:40 +08:00
|
|
|
#if defined(__has_include)
|
|
|
|
# if __has_include(<sanitizer/coverage_interface.h>)
|
|
|
|
# include <sanitizer/coverage_interface.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-10-17 07:04:31 +08:00
|
|
|
extern "C" {
|
2015-11-10 07:17:45 +08:00
|
|
|
// Re-declare some of the sanitizer functions as "weak" so that
|
2015-11-13 09:54:40 +08:00
|
|
|
// libFuzzer can be linked w/o the sanitizers and sanitizer-coverage
|
2015-11-10 07:17:45 +08:00
|
|
|
// (in which case it will complain at start-up time).
|
2015-10-17 07:04:31 +08:00
|
|
|
__attribute__((weak)) void __sanitizer_print_stack_trace();
|
2015-11-12 09:02:01 +08:00
|
|
|
__attribute__((weak)) void __sanitizer_reset_coverage();
|
2015-10-24 02:37:58 +08:00
|
|
|
__attribute__((weak)) size_t __sanitizer_get_total_unique_caller_callee_pairs();
|
2015-11-10 07:17:45 +08:00
|
|
|
__attribute__((weak)) size_t __sanitizer_get_total_unique_coverage();
|
|
|
|
__attribute__((weak))
|
|
|
|
void __sanitizer_set_death_callback(void (*callback)(void));
|
|
|
|
__attribute__((weak)) size_t __sanitizer_get_number_of_counters();
|
|
|
|
__attribute__((weak))
|
|
|
|
uintptr_t __sanitizer_update_counter_bitset_and_clear_counters(uint8_t *bitset);
|
2015-10-17 07:04:31 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
2015-10-17 06:47:20 +08:00
|
|
|
static const size_t kMaxUnitSizeToPrint = 256;
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2015-11-10 07:17:45 +08:00
|
|
|
static void MissingWeakApiFunction(const char *FnName) {
|
|
|
|
Printf("ERROR: %s is not defined. Exiting.\n"
|
|
|
|
"Did you use -fsanitize-coverage=... to build your code?\n", FnName);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_WEAK_API_FUNCTION(fn) \
|
|
|
|
do { \
|
|
|
|
if (!fn) \
|
|
|
|
MissingWeakApiFunction(#fn); \
|
|
|
|
} while (false)
|
|
|
|
|
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-11-10 07:17:45 +08:00
|
|
|
CHECK_WEAK_API_FUNCTION(__sanitizer_set_death_callback);
|
2015-04-01 04:13:20 +08:00
|
|
|
__sanitizer_set_death_callback(StaticDeathCallback);
|
|
|
|
}
|
|
|
|
|
2015-10-23 05:48:09 +08:00
|
|
|
void Fuzzer::PrintUnitInASCII(const Unit &U, const char *PrintAfter) {
|
|
|
|
PrintASCII(U, 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-10-09 12:03:14 +08:00
|
|
|
if (CurrentUnit.size() <= kMaxUnitSizeToPrint) {
|
|
|
|
Print(CurrentUnit, "\n");
|
2015-10-23 05:48:09 +08:00
|
|
|
PrintUnitInASCII(CurrentUnit, "\n");
|
2015-10-09 12:03:14 +08:00
|
|
|
}
|
2015-07-24 02:37:22 +08:00
|
|
|
WriteUnitToFileWithPrefix(CurrentUnit, "crash-");
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
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-10-09 12:03:14 +08:00
|
|
|
if (CurrentUnit.size() <= kMaxUnitSizeToPrint) {
|
2015-08-01 06:07:17 +08:00
|
|
|
Print(CurrentUnit, "\n");
|
2015-10-23 05:48:09 +08:00
|
|
|
PrintUnitInASCII(CurrentUnit, "\n");
|
2015-10-09 12:03:14 +08:00
|
|
|
}
|
2015-07-24 02:37:22 +08:00
|
|
|
WriteUnitToFileWithPrefix(CurrentUnit, "timeout-");
|
2015-10-17 07:04:31 +08:00
|
|
|
Printf("==%d== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(),
|
|
|
|
Seconds);
|
|
|
|
if (__sanitizer_print_stack_trace)
|
|
|
|
__sanitizer_print_stack_trace();
|
|
|
|
Printf("SUMMARY: libFuzzer: timeout\n");
|
2015-05-20 06:12:57 +08:00
|
|
|
exit(1);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 06:56:45 +08:00
|
|
|
void Fuzzer::PrintStats(const char *Where, const char *End) {
|
2015-03-31 06:44:03 +08:00
|
|
|
size_t Seconds = secondsSinceProcessStartUp();
|
|
|
|
size_t ExecPerSec = (Seconds ? TotalNumberOfRuns / Seconds : 0);
|
2015-11-12 12:38:40 +08:00
|
|
|
|
|
|
|
if (Options.OutputCSV) {
|
|
|
|
static bool csvHeaderPrinted = false;
|
|
|
|
if (!csvHeaderPrinted) {
|
|
|
|
csvHeaderPrinted = true;
|
|
|
|
Printf("runs,block_cov,bits,cc_cov,corpus,execs_per_sec,tbms,reason\n");
|
|
|
|
}
|
|
|
|
Printf("%zd,%zd,%zd,%zd,%zd,%zd,%zd,%s\n", TotalNumberOfRuns,
|
|
|
|
LastRecordedBlockCoverage, TotalBits(),
|
|
|
|
LastRecordedCallerCalleeCoverage, Corpus.size(), ExecPerSec,
|
|
|
|
TotalNumberOfExecutedTraceBasedMutations, Where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Options.Verbosity)
|
|
|
|
return;
|
2015-10-23 06:50:47 +08:00
|
|
|
Printf("#%zd\t%s", TotalNumberOfRuns, Where);
|
2015-10-23 06:56:45 +08:00
|
|
|
if (LastRecordedBlockCoverage)
|
|
|
|
Printf(" cov: %zd", LastRecordedBlockCoverage);
|
2015-10-23 06:50:47 +08:00
|
|
|
if (auto TB = TotalBits())
|
|
|
|
Printf(" bits: %zd", TB);
|
2015-10-23 07:55:39 +08:00
|
|
|
if (LastRecordedCallerCalleeCoverage)
|
|
|
|
Printf(" indir: %zd", LastRecordedCallerCalleeCoverage);
|
2015-10-23 06:50:47 +08:00
|
|
|
Printf(" units: %zd exec/s: %zd", Corpus.size(), ExecPerSec);
|
2015-08-12 09:55:37 +08:00
|
|
|
if (TotalNumberOfExecutedTraceBasedMutations)
|
|
|
|
Printf(" tbm: %zd", TotalNumberOfExecutedTraceBasedMutations);
|
|
|
|
Printf("%s", 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());
|
2015-10-23 06:50:47 +08:00
|
|
|
if (RunOne(CurrentUnit)) {
|
2015-05-19 09:06:07 +08:00
|
|
|
Corpus.push_back(X);
|
2015-11-12 12:38:40 +08:00
|
|
|
PrintStats("RELOAD");
|
2015-05-19 09:06:07 +08:00
|
|
|
}
|
2015-05-09 05:30:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
void Fuzzer::ShuffleAndMinimize() {
|
2015-07-24 09:06:40 +08:00
|
|
|
bool PreferSmall = (Options.PreferSmallDuringInitialShuffle == 1 ||
|
|
|
|
(Options.PreferSmallDuringInitialShuffle == -1 &&
|
|
|
|
USF.GetRand().RandBool()));
|
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-10-23 06:56:45 +08:00
|
|
|
PrintStats("READ ");
|
2015-01-30 00:58:29 +08:00
|
|
|
std::vector<Unit> NewCorpus;
|
2015-10-17 12:38:26 +08:00
|
|
|
if (Options.ShuffleAtStartUp) {
|
|
|
|
std::random_shuffle(Corpus.begin(), Corpus.end(), USF.GetRand());
|
|
|
|
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);
|
2015-09-03 03:08:08 +08:00
|
|
|
if (Options.OnlyASCII)
|
|
|
|
ToASCII(U);
|
2015-10-23 06:50:47 +08:00
|
|
|
if (RunOne(U)) {
|
2015-01-30 00:58:29 +08:00
|
|
|
NewCorpus.push_back(U);
|
|
|
|
if (Options.Verbosity >= 2)
|
2015-10-23 06:50:47 +08:00
|
|
|
Printf("NEW0: %zd L %zd\n", LastRecordedBlockCoverage, 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-10-23 06:56:45 +08:00
|
|
|
PrintStats("INITED");
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2015-10-23 06:50:47 +08:00
|
|
|
bool Fuzzer::RunOne(const Unit &U) {
|
2015-01-30 00:58:29 +08:00
|
|
|
UnitStartTime = system_clock::now();
|
|
|
|
TotalNumberOfRuns++;
|
2015-10-23 06:50:47 +08:00
|
|
|
|
|
|
|
PrepareCoverageBeforeRun();
|
|
|
|
ExecuteCallback(U);
|
|
|
|
bool Res = CheckCoverageAfterRun();
|
|
|
|
|
2015-03-31 07:04:35 +08:00
|
|
|
auto UnitStopTime = system_clock::now();
|
|
|
|
auto TimeOfUnit =
|
|
|
|
duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
|
2015-11-12 12:38:40 +08:00
|
|
|
if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) &&
|
|
|
|
secondsSinceProcessStartUp() >= 2)
|
2015-10-23 06:56:45 +08:00
|
|
|
PrintStats("pulse ");
|
2015-08-06 05:43:48 +08:00
|
|
|
if (TimeOfUnit > TimeOfLongestUnitInSeconds &&
|
|
|
|
TimeOfUnit >= Options.ReportSlowUnits) {
|
2015-03-31 07:04:35 +08:00
|
|
|
TimeOfLongestUnitInSeconds = TimeOfUnit;
|
2015-08-06 05:43:48 +08:00
|
|
|
Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds);
|
|
|
|
WriteUnitToFileWithPrefix(U, "slow-unit-");
|
2015-03-31 07:04:35 +08:00
|
|
|
}
|
|
|
|
return Res;
|
2015-01-30 07:01:07 +08:00
|
|
|
}
|
|
|
|
|
2015-08-11 09:44:42 +08:00
|
|
|
void Fuzzer::RunOneAndUpdateCorpus(Unit &U) {
|
2015-05-08 02:32:29 +08:00
|
|
|
if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
|
|
|
|
return;
|
2015-08-11 09:44:42 +08:00
|
|
|
if (Options.OnlyASCII)
|
|
|
|
ToASCII(U);
|
2015-10-23 06:50:47 +08:00
|
|
|
if (RunOne(U))
|
|
|
|
ReportNewCoverage(U);
|
2015-05-08 02:32:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
void Fuzzer::ExecuteCallback(const Unit &U) {
|
2015-12-03 06:43:53 +08:00
|
|
|
const uint8_t *Data = U.data();
|
|
|
|
uint8_t EmptyData;
|
|
|
|
if (!Data)
|
|
|
|
Data = &EmptyData;
|
|
|
|
int Res = USF.TargetFunction(Data, U.size());
|
2015-10-23 05:48:09 +08:00
|
|
|
(void)Res;
|
2015-10-03 07:34:06 +08:00
|
|
|
assert(Res == 0);
|
2015-04-01 04:13:20 +08:00
|
|
|
}
|
|
|
|
|
2015-10-23 06:50:47 +08:00
|
|
|
size_t Fuzzer::RecordBlockCoverage() {
|
2015-11-10 07:17:45 +08:00
|
|
|
CHECK_WEAK_API_FUNCTION(__sanitizer_get_total_unique_coverage);
|
2015-10-23 06:50:47 +08:00
|
|
|
return LastRecordedBlockCoverage = __sanitizer_get_total_unique_coverage();
|
|
|
|
}
|
|
|
|
|
2015-10-23 07:55:39 +08:00
|
|
|
size_t Fuzzer::RecordCallerCalleeCoverage() {
|
|
|
|
if (!Options.UseIndirCalls)
|
|
|
|
return 0;
|
2015-10-24 02:37:58 +08:00
|
|
|
if (!__sanitizer_get_total_unique_caller_callee_pairs)
|
|
|
|
return 0;
|
2015-10-23 07:55:39 +08:00
|
|
|
return LastRecordedCallerCalleeCoverage =
|
|
|
|
__sanitizer_get_total_unique_caller_callee_pairs();
|
|
|
|
}
|
|
|
|
|
2015-10-23 06:50:47 +08:00
|
|
|
void Fuzzer::PrepareCoverageBeforeRun() {
|
[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 (Options.UseCounters) {
|
2015-10-23 06:50:47 +08:00
|
|
|
size_t NumCounters = __sanitizer_get_number_of_counters();
|
[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
|
|
|
CounterBitmap.resize(NumCounters);
|
|
|
|
__sanitizer_update_counter_bitset_and_clear_counters(0);
|
|
|
|
}
|
2015-10-23 06:50:47 +08:00
|
|
|
RecordBlockCoverage();
|
2015-10-23 07:55:39 +08:00
|
|
|
RecordCallerCalleeCoverage();
|
2015-10-23 06:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Fuzzer::CheckCoverageAfterRun() {
|
|
|
|
size_t OldCoverage = LastRecordedBlockCoverage;
|
|
|
|
size_t NewCoverage = RecordBlockCoverage();
|
2015-10-23 07:55:39 +08:00
|
|
|
size_t OldCallerCalleeCoverage = LastRecordedCallerCalleeCoverage;
|
|
|
|
size_t NewCallerCalleeCoverage = RecordCallerCalleeCoverage();
|
[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-10-23 07:55:39 +08:00
|
|
|
return NewCoverage > OldCoverage ||
|
|
|
|
NewCallerCalleeCoverage > OldCallerCalleeCoverage || NumNewBits;
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-03 03:08:08 +08:00
|
|
|
assert(!Options.OnlyASCII || IsASCII(U));
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 02:37:22 +08:00
|
|
|
void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
|
2015-10-17 06:41:47 +08:00
|
|
|
if (!Options.SaveArtifacts)
|
|
|
|
return;
|
2015-10-09 11:57:59 +08:00
|
|
|
std::string Path = Options.ArtifactPrefix + Prefix + Hash(U);
|
2015-11-26 05:40:46 +08:00
|
|
|
if (!Options.ExactArtifactPath.empty())
|
|
|
|
Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix.
|
2015-01-30 00:58:29 +08:00
|
|
|
WriteToFile(U, Path);
|
2015-10-09 11:57:59 +08:00
|
|
|
Printf("artifact_prefix='%s'; Test unit written to %s\n",
|
|
|
|
Options.ArtifactPrefix.c_str(), Path.c_str());
|
2015-12-05 06:29:39 +08:00
|
|
|
if (U.size() <= kMaxUnitSizeToPrint)
|
|
|
|
Printf("Base64: %s\n", Base64(U).c_str());
|
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-11-12 09:02:01 +08:00
|
|
|
void Fuzzer::PrintStatusForNewUnit(const Unit &U) {
|
|
|
|
if (!Options.PrintNEW)
|
|
|
|
return;
|
2015-10-23 06:56:45 +08:00
|
|
|
PrintStats("NEW ", "");
|
2015-05-08 02:32:29 +08:00
|
|
|
if (Options.Verbosity) {
|
2015-12-19 09:09:49 +08:00
|
|
|
Printf(" L: %zd ", U.size());
|
|
|
|
USF.PrintMutationSequence();
|
2015-05-23 09:22:35 +08:00
|
|
|
Printf("\n");
|
2015-05-08 02:32:29 +08:00
|
|
|
}
|
2015-11-12 09:02:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::ReportNewCoverage(const Unit &U) {
|
|
|
|
Corpus.push_back(U);
|
|
|
|
UnitHashesAddedToCorpus.insert(Hash(U));
|
|
|
|
PrintStatusForNewUnit(U);
|
2015-05-08 02:32:29 +08:00
|
|
|
WriteToOutputCorpus(U);
|
|
|
|
if (Options.ExitOnFirst)
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-10-24 09:16:40 +08:00
|
|
|
void Fuzzer::Merge(const std::vector<std::string> &Corpora) {
|
|
|
|
if (Corpora.size() <= 1) {
|
|
|
|
Printf("Merge requires two or more corpus dirs\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto InitialCorpusDir = Corpora[0];
|
|
|
|
ReadDir(InitialCorpusDir, nullptr);
|
|
|
|
Printf("Merge: running the initial corpus '%s' of %d units\n",
|
|
|
|
InitialCorpusDir.c_str(), Corpus.size());
|
|
|
|
for (auto &U : Corpus)
|
|
|
|
RunOne(U);
|
|
|
|
|
|
|
|
std::vector<std::string> ExtraCorpora(Corpora.begin() + 1, Corpora.end());
|
|
|
|
|
|
|
|
size_t NumTried = 0;
|
|
|
|
size_t NumMerged = 0;
|
|
|
|
for (auto &C : ExtraCorpora) {
|
|
|
|
Corpus.clear();
|
|
|
|
ReadDir(C, nullptr);
|
|
|
|
Printf("Merge: merging the extra corpus '%s' of %zd units\n", C.c_str(),
|
|
|
|
Corpus.size());
|
|
|
|
for (auto &U : Corpus) {
|
|
|
|
NumTried++;
|
|
|
|
if (RunOne(U)) {
|
|
|
|
WriteToOutputCorpus(U);
|
|
|
|
NumMerged++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Printf("Merge: written %zd out of %zd units\n", NumMerged, NumTried);
|
|
|
|
}
|
|
|
|
|
2015-12-19 10:49:09 +08:00
|
|
|
void Fuzzer::MutateAndTestOne() {
|
|
|
|
auto &U = CurrentUnit;
|
|
|
|
USF.StartMutationSequence();
|
|
|
|
|
|
|
|
U = ChooseUnitToMutate();
|
|
|
|
|
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-12-19 10:49:09 +08:00
|
|
|
size_t Size = U.size();
|
|
|
|
U.resize(Options.MaxLen);
|
|
|
|
size_t NewSize = USF.Mutate(U.data(), Size, U.size());
|
2015-05-31 01:33:13 +08:00
|
|
|
assert(NewSize > 0 && "Mutator returned empty unit");
|
|
|
|
assert(NewSize <= (size_t)Options.MaxLen &&
|
|
|
|
"Mutator return overisized unit");
|
2015-12-19 10:49:09 +08:00
|
|
|
U.resize(NewSize);
|
|
|
|
RunOneAndUpdateCorpus(U);
|
2015-05-08 05:02:11 +08:00
|
|
|
size_t NumTraceBasedMutations = StopTraceRecording();
|
2015-08-12 09:55:37 +08:00
|
|
|
size_t TBMWidth =
|
|
|
|
std::min((size_t)Options.TBMWidth, NumTraceBasedMutations);
|
|
|
|
size_t TBMDepth =
|
|
|
|
std::min((size_t)Options.TBMDepth, NumTraceBasedMutations);
|
2015-12-19 10:49:09 +08:00
|
|
|
Unit BackUp = U;
|
2015-08-12 09:55:37 +08:00
|
|
|
for (size_t w = 0; w < TBMWidth; w++) {
|
2015-12-19 10:49:09 +08:00
|
|
|
U = BackUp;
|
2015-08-12 09:55:37 +08:00
|
|
|
for (size_t d = 0; d < TBMDepth; d++) {
|
|
|
|
TotalNumberOfExecutedTraceBasedMutations++;
|
2015-12-19 10:49:09 +08:00
|
|
|
ApplyTraceBasedMutation(USF.GetRand()(NumTraceBasedMutations), &U);
|
|
|
|
RunOneAndUpdateCorpus(U);
|
2015-08-12 09:55:37 +08:00
|
|
|
}
|
2015-05-08 05:02:11 +08:00
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:22:25 +08:00
|
|
|
// Returns an index of random unit from the corpus to mutate.
|
|
|
|
// Hypothesis: units added to the corpus last are more likely to be interesting.
|
|
|
|
// This function gives more wieght to the more recent units.
|
2015-11-12 09:02:01 +08:00
|
|
|
size_t Fuzzer::ChooseUnitIdxToMutate() {
|
2015-11-05 07:22:25 +08:00
|
|
|
size_t N = Corpus.size();
|
|
|
|
size_t Total = (N + 1) * N / 2;
|
|
|
|
size_t R = USF.GetRand()(Total);
|
|
|
|
size_t IdxBeg = 0, IdxEnd = N;
|
|
|
|
// Binary search.
|
|
|
|
while (IdxEnd - IdxBeg >= 2) {
|
|
|
|
size_t Idx = IdxBeg + (IdxEnd - IdxBeg) / 2;
|
|
|
|
if (R > (Idx + 1) * Idx / 2)
|
|
|
|
IdxBeg = Idx;
|
|
|
|
else
|
|
|
|
IdxEnd = Idx;
|
|
|
|
}
|
|
|
|
assert(IdxBeg < N);
|
|
|
|
return IdxBeg;
|
|
|
|
}
|
|
|
|
|
2015-11-12 09:02:01 +08:00
|
|
|
// Experimental search heuristic: drilling.
|
|
|
|
// - Read, shuffle, execute and minimize the corpus.
|
|
|
|
// - Choose one random unit.
|
|
|
|
// - Reset the coverage.
|
|
|
|
// - Start fuzzing as if the chosen unit was the only element of the corpus.
|
|
|
|
// - When done, reset the coverage again.
|
|
|
|
// - Merge the newly created corpus into the original one.
|
|
|
|
void Fuzzer::Drill() {
|
|
|
|
// The corpus is already read, shuffled, and minimized.
|
|
|
|
assert(!Corpus.empty());
|
|
|
|
Options.PrintNEW = false; // Don't print NEW status lines when drilling.
|
|
|
|
|
|
|
|
Unit U = ChooseUnitToMutate();
|
|
|
|
|
|
|
|
CHECK_WEAK_API_FUNCTION(__sanitizer_reset_coverage);
|
|
|
|
__sanitizer_reset_coverage();
|
|
|
|
|
|
|
|
std::vector<Unit> SavedCorpus;
|
|
|
|
SavedCorpus.swap(Corpus);
|
|
|
|
Corpus.push_back(U);
|
|
|
|
assert(Corpus.size() == 1);
|
|
|
|
RunOne(U);
|
|
|
|
PrintStats("DRILL ");
|
|
|
|
std::string SavedOutputCorpusPath; // Don't write new units while drilling.
|
|
|
|
SavedOutputCorpusPath.swap(Options.OutputCorpus);
|
|
|
|
Loop();
|
|
|
|
|
|
|
|
__sanitizer_reset_coverage();
|
|
|
|
|
|
|
|
PrintStats("REINIT");
|
|
|
|
SavedOutputCorpusPath.swap(Options.OutputCorpus);
|
|
|
|
for (auto &U : SavedCorpus)
|
|
|
|
RunOne(U);
|
|
|
|
PrintStats("MERGE ");
|
|
|
|
Options.PrintNEW = true;
|
|
|
|
size_t NumMerged = 0;
|
|
|
|
for (auto &U : Corpus) {
|
|
|
|
if (RunOne(U)) {
|
|
|
|
PrintStatusForNewUnit(U);
|
|
|
|
NumMerged++;
|
|
|
|
WriteToOutputCorpus(U);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PrintStats("MERGED");
|
|
|
|
if (NumMerged && Options.Verbosity)
|
|
|
|
Printf("Drilling discovered %zd new units\n", NumMerged);
|
|
|
|
}
|
2015-09-04 08:12:11 +08:00
|
|
|
|
2015-11-12 09:02:01 +08:00
|
|
|
void Fuzzer::Loop() {
|
2015-12-05 10:09:22 +08:00
|
|
|
system_clock::time_point LastCorpusReload = system_clock::now();
|
2015-12-19 10:49:09 +08:00
|
|
|
if (Options.DoCrossOver)
|
|
|
|
USF.SetCorpus(&Corpus);
|
2015-09-09 01:30:35 +08:00
|
|
|
while (true) {
|
2015-11-05 07:22:25 +08:00
|
|
|
SyncCorpus();
|
2015-12-05 10:09:22 +08:00
|
|
|
auto Now = system_clock::now();
|
|
|
|
if (duration_cast<seconds>(Now - LastCorpusReload).count()) {
|
|
|
|
RereadOutputCorpus();
|
|
|
|
LastCorpusReload = Now;
|
|
|
|
}
|
2015-11-05 07:22:25 +08:00
|
|
|
if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
|
2015-11-12 12:38:40 +08:00
|
|
|
break;
|
2015-11-05 07:22:25 +08:00
|
|
|
if (Options.MaxTotalTimeSec > 0 &&
|
|
|
|
secondsSinceProcessStartUp() >
|
|
|
|
static_cast<size_t>(Options.MaxTotalTimeSec))
|
2015-11-12 12:38:40 +08:00
|
|
|
break;
|
2015-11-05 07:22:25 +08:00
|
|
|
// Perform several mutations and runs.
|
2015-12-19 10:49:09 +08:00
|
|
|
MutateAndTestOne();
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
2015-11-12 12:38:40 +08:00
|
|
|
|
|
|
|
PrintStats("DONE ", "\n");
|
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
|