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>
|
2016-01-14 07:02:30 +08:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2015-11-13 09:54:40 +08:00
|
|
|
#if defined(__has_include)
|
2016-01-23 06:28:27 +08:00
|
|
|
#if __has_include(<sanitizer / coverage_interface.h>)
|
|
|
|
#include <sanitizer/coverage_interface.h>
|
|
|
|
#endif
|
2016-04-20 08:24:21 +08:00
|
|
|
#if __has_include(<sanitizer / lsan_interface.h>)
|
|
|
|
#include <sanitizer/lsan_interface.h>
|
|
|
|
#endif
|
2015-11-13 09:54:40 +08:00
|
|
|
#endif
|
|
|
|
|
2016-03-18 22:19:19 +08:00
|
|
|
#define NO_SANITIZE_MEMORY
|
|
|
|
#if defined(__has_feature)
|
|
|
|
#if __has_feature(memory_sanitizer)
|
|
|
|
#undef NO_SANITIZE_MEMORY
|
|
|
|
#define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
2015-10-17 06:47:20 +08:00
|
|
|
static const size_t kMaxUnitSizeToPrint = 256;
|
2016-05-25 07:14:29 +08:00
|
|
|
static const size_t TruncateMaxRuns = 1000;
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2016-05-27 05:32:30 +08:00
|
|
|
thread_local bool Fuzzer::IsMyThread;
|
|
|
|
|
2016-06-08 07:32:50 +08:00
|
|
|
static void MissingExternalApiFunction(const char *FnName) {
|
2015-11-10 07:17:45 +08:00
|
|
|
Printf("ERROR: %s is not defined. Exiting.\n"
|
2016-01-23 06:28:27 +08:00
|
|
|
"Did you use -fsanitize-coverage=... to build your code?\n",
|
|
|
|
FnName);
|
2015-11-10 07:17:45 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:32:50 +08:00
|
|
|
#define CHECK_EXTERNAL_FUNCTION(fn) \
|
2015-11-10 07:17:45 +08:00
|
|
|
do { \
|
2016-06-08 07:32:50 +08:00
|
|
|
if (!(EF->fn)) \
|
|
|
|
MissingExternalApiFunction(#fn); \
|
2015-11-10 07:17:45 +08:00
|
|
|
} while (false)
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
// Only one Fuzzer per process.
|
|
|
|
static Fuzzer *F;
|
|
|
|
|
2016-08-06 04:09:53 +08:00
|
|
|
// Only one CoverageController per process should be created.
|
|
|
|
class CoverageController {
|
|
|
|
public:
|
2016-08-17 01:37:13 +08:00
|
|
|
explicit CoverageController(const FuzzingOptions &Options)
|
2016-08-06 04:09:53 +08:00
|
|
|
: Options(Options) {
|
|
|
|
if (Options.PrintNewCovPcs) {
|
|
|
|
PcBufferLen = 1 << 24;
|
|
|
|
PcBuffer = new uintptr_t[PcBufferLen];
|
|
|
|
EF->__sanitizer_set_coverage_pc_buffer(PcBuffer, PcBufferLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uintptr_t* pc_buffer() const { return PcBuffer; }
|
|
|
|
|
|
|
|
void Reset() {
|
2016-06-08 07:32:50 +08:00
|
|
|
CHECK_EXTERNAL_FUNCTION(__sanitizer_reset_coverage);
|
|
|
|
EF->__sanitizer_reset_coverage();
|
2016-05-11 07:43:15 +08:00
|
|
|
}
|
|
|
|
|
2016-08-06 04:09:53 +08:00
|
|
|
void ResetCounters() {
|
2016-05-11 07:43:15 +08:00
|
|
|
if (Options.UseCounters) {
|
2016-06-08 07:32:50 +08:00
|
|
|
EF->__sanitizer_update_counter_bitset_and_clear_counters(0);
|
2016-05-11 07:43:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 04:09:53 +08:00
|
|
|
void Prepare(Fuzzer::Coverage *C) {
|
2016-05-11 07:43:15 +08:00
|
|
|
if (Options.UseCounters) {
|
2016-06-08 07:32:50 +08:00
|
|
|
size_t NumCounters = EF->__sanitizer_get_number_of_counters();
|
2016-05-11 07:43:15 +08:00
|
|
|
C->CounterBitmap.resize(NumCounters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Records data to a maximum coverage tracker. Returns true if additional
|
|
|
|
// coverage was discovered.
|
2016-08-06 04:09:53 +08:00
|
|
|
bool RecordMax(Fuzzer::Coverage *C) {
|
2016-05-11 07:43:15 +08:00
|
|
|
bool Res = false;
|
|
|
|
|
2016-06-08 07:32:50 +08:00
|
|
|
uint64_t NewBlockCoverage = EF->__sanitizer_get_total_unique_coverage();
|
2016-05-11 07:43:15 +08:00
|
|
|
if (NewBlockCoverage > C->BlockCoverage) {
|
|
|
|
Res = true;
|
|
|
|
C->BlockCoverage = NewBlockCoverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Options.UseIndirCalls &&
|
2016-06-08 07:32:50 +08:00
|
|
|
EF->__sanitizer_get_total_unique_caller_callee_pairs) {
|
2016-05-11 07:43:15 +08:00
|
|
|
uint64_t NewCallerCalleeCoverage =
|
2016-06-08 07:32:50 +08:00
|
|
|
EF->__sanitizer_get_total_unique_caller_callee_pairs();
|
2016-05-11 07:43:15 +08:00
|
|
|
if (NewCallerCalleeCoverage > C->CallerCalleeCoverage) {
|
|
|
|
Res = true;
|
|
|
|
C->CallerCalleeCoverage = NewCallerCalleeCoverage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Options.UseCounters) {
|
|
|
|
uint64_t CounterDelta =
|
2016-06-08 07:32:50 +08:00
|
|
|
EF->__sanitizer_update_counter_bitset_and_clear_counters(
|
2016-05-11 07:43:15 +08:00
|
|
|
C->CounterBitmap.data());
|
|
|
|
if (CounterDelta > 0) {
|
|
|
|
Res = true;
|
|
|
|
C->CounterBitmapBits += CounterDelta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 01:37:13 +08:00
|
|
|
size_t NewPCMapBits = PCMapMergeFromCurrent(C->PCMap);
|
|
|
|
if (NewPCMapBits > C->PCMapBits) {
|
2016-05-11 07:43:15 +08:00
|
|
|
Res = true;
|
2016-08-17 01:37:13 +08:00
|
|
|
C->PCMapBits = NewPCMapBits;
|
2016-05-11 07:43:15 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 03:33:51 +08:00
|
|
|
size_t NewVPMapBits = VPMapMergeFromCurrent(C->VPMap);
|
|
|
|
if (NewVPMapBits > C->VPMapBits) {
|
|
|
|
Res = true;
|
|
|
|
C->VPMapBits = NewVPMapBits;
|
|
|
|
}
|
|
|
|
|
2016-08-07 05:28:56 +08:00
|
|
|
if (EF->__sanitizer_get_coverage_pc_buffer_pos) {
|
|
|
|
uint64_t NewPcBufferPos = EF->__sanitizer_get_coverage_pc_buffer_pos();
|
|
|
|
if (NewPcBufferPos > C->PcBufferPos) {
|
|
|
|
Res = true;
|
|
|
|
C->PcBufferPos = NewPcBufferPos;
|
|
|
|
}
|
2016-08-06 04:09:53 +08:00
|
|
|
|
2016-08-07 05:28:56 +08:00
|
|
|
if (PcBufferLen && NewPcBufferPos >= PcBufferLen) {
|
|
|
|
Printf("ERROR: PC buffer overflow\n");
|
|
|
|
_Exit(1);
|
|
|
|
}
|
2016-05-11 07:43:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
2016-08-06 04:09:53 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const FuzzingOptions Options;
|
|
|
|
uintptr_t* PcBuffer = nullptr;
|
|
|
|
size_t PcBufferLen = 0;
|
2016-05-11 07:43:15 +08:00
|
|
|
};
|
|
|
|
|
2016-06-17 04:17:41 +08:00
|
|
|
// Leak detection is expensive, so we first check if there were more mallocs
|
|
|
|
// than frees (using the sanitizer malloc hooks) and only then try to call lsan.
|
|
|
|
struct MallocFreeTracer {
|
|
|
|
void Start() {
|
|
|
|
Mallocs = 0;
|
|
|
|
Frees = 0;
|
|
|
|
}
|
|
|
|
// Returns true if there were more mallocs than frees.
|
|
|
|
bool Stop() { return Mallocs > Frees; }
|
|
|
|
std::atomic<size_t> Mallocs;
|
|
|
|
std::atomic<size_t> Frees;
|
|
|
|
};
|
|
|
|
|
|
|
|
static MallocFreeTracer AllocTracer;
|
|
|
|
|
|
|
|
void MallocHook(const volatile void *ptr, size_t size) {
|
|
|
|
AllocTracer.Mallocs++;
|
|
|
|
}
|
|
|
|
void FreeHook(const volatile void *ptr) {
|
|
|
|
AllocTracer.Frees++;
|
|
|
|
}
|
|
|
|
|
2016-02-13 11:25:16 +08:00
|
|
|
Fuzzer::Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options)
|
2016-08-06 04:09:53 +08:00
|
|
|
: CB(CB), MD(MD), Options(Options),
|
|
|
|
CController(new CoverageController(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;
|
2016-05-11 07:43:15 +08:00
|
|
|
ResetCoverage();
|
2016-05-27 05:32:30 +08:00
|
|
|
IsMyThread = true;
|
2016-06-17 04:17:41 +08:00
|
|
|
if (Options.DetectLeaks && EF->__sanitizer_install_malloc_and_free_hooks)
|
|
|
|
EF->__sanitizer_install_malloc_and_free_hooks(MallocHook, FreeHook);
|
2015-04-01 04:13:20 +08:00
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2016-08-06 04:09:53 +08:00
|
|
|
Fuzzer::~Fuzzer() { }
|
|
|
|
|
2016-05-27 08:21:33 +08:00
|
|
|
void Fuzzer::LazyAllocateCurrentUnitData() {
|
|
|
|
if (CurrentUnitData || Options.MaxLen == 0) return;
|
|
|
|
CurrentUnitData = new uint8_t[Options.MaxLen];
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
void Fuzzer::SetDeathCallback() {
|
2016-06-08 07:32:50 +08:00
|
|
|
CHECK_EXTERNAL_FUNCTION(__sanitizer_set_death_callback);
|
|
|
|
EF->__sanitizer_set_death_callback(StaticDeathCallback);
|
2015-04-01 04:13:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::StaticDeathCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->DeathCallback();
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-03-02 06:19:21 +08:00
|
|
|
void Fuzzer::DumpCurrentUnit(const char *Prefix) {
|
2016-05-28 12:19:46 +08:00
|
|
|
if (!CurrentUnitData) return; // Happens when running individual inputs.
|
2016-08-18 04:45:23 +08:00
|
|
|
MD.PrintMutationSequence();
|
|
|
|
Printf("; base unit: %s\n", Sha1ToString(BaseSha1).c_str());
|
2016-05-27 08:54:15 +08:00
|
|
|
size_t UnitSize = CurrentUnitSize;
|
2016-05-27 06:17:32 +08:00
|
|
|
if (UnitSize <= kMaxUnitSizeToPrint) {
|
2016-05-27 08:54:15 +08:00
|
|
|
PrintHexArray(CurrentUnitData, UnitSize, "\n");
|
|
|
|
PrintASCII(CurrentUnitData, UnitSize, "\n");
|
2015-10-09 12:03:14 +08:00
|
|
|
}
|
2016-05-27 08:54:15 +08:00
|
|
|
WriteUnitToFileWithPrefix({CurrentUnitData, CurrentUnitData + UnitSize},
|
|
|
|
Prefix);
|
2016-03-02 06:19:21 +08:00
|
|
|
}
|
|
|
|
|
2016-03-18 22:19:19 +08:00
|
|
|
NO_SANITIZE_MEMORY
|
2016-03-02 06:19:21 +08:00
|
|
|
void Fuzzer::DeathCallback() {
|
2016-05-27 06:17:32 +08:00
|
|
|
DumpCurrentUnit("crash-");
|
2016-02-27 06:42:23 +08:00
|
|
|
PrintFinalStats();
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
void Fuzzer::StaticAlarmCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->AlarmCallback();
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:19:21 +08:00
|
|
|
void Fuzzer::StaticCrashSignalCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->CrashCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::StaticInterruptCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->InterruptCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::CrashCallback() {
|
|
|
|
Printf("==%d== ERROR: libFuzzer: deadly signal\n", GetPid());
|
2016-06-08 07:32:50 +08:00
|
|
|
if (EF->__sanitizer_print_stack_trace)
|
|
|
|
EF->__sanitizer_print_stack_trace();
|
2016-03-02 06:19:21 +08:00
|
|
|
Printf("NOTE: libFuzzer has rudimentary signal handlers.\n"
|
|
|
|
" Combine libFuzzer with AddressSanitizer or similar for better "
|
|
|
|
"crash reports.\n");
|
|
|
|
Printf("SUMMARY: libFuzzer: deadly signal\n");
|
|
|
|
DumpCurrentUnit("crash-");
|
|
|
|
PrintFinalStats();
|
|
|
|
exit(Options.ErrorExitCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::InterruptCallback() {
|
|
|
|
Printf("==%d== libFuzzer: run interrupted; exiting\n", GetPid());
|
|
|
|
PrintFinalStats();
|
2016-03-04 06:36:37 +08:00
|
|
|
_Exit(0); // Stop right now, don't perform any at-exit actions.
|
2016-03-02 06:19:21 +08:00
|
|
|
}
|
|
|
|
|
2016-03-18 22:19:19 +08:00
|
|
|
NO_SANITIZE_MEMORY
|
2015-01-30 00:58:29 +08:00
|
|
|
void Fuzzer::AlarmCallback() {
|
2015-05-20 06:12:57 +08:00
|
|
|
assert(Options.UnitTimeoutSec > 0);
|
2016-05-27 06:17:32 +08:00
|
|
|
if (!InFuzzingThread()) return;
|
2016-05-27 08:54:15 +08:00
|
|
|
if (!CurrentUnitSize)
|
2016-02-18 03:42:34 +08:00
|
|
|
return; // We have not started running units yet.
|
2015-01-30 00:58:29 +08:00
|
|
|
size_t Seconds =
|
|
|
|
duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
|
2016-01-23 06:28:27 +08:00
|
|
|
if (Seconds == 0)
|
|
|
|
return;
|
2015-05-20 06:12:57 +08:00
|
|
|
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);
|
2016-03-02 06:19:21 +08:00
|
|
|
DumpCurrentUnit("timeout-");
|
2015-10-17 07:04:31 +08:00
|
|
|
Printf("==%d== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(),
|
|
|
|
Seconds);
|
2016-06-08 07:32:50 +08:00
|
|
|
if (EF->__sanitizer_print_stack_trace)
|
|
|
|
EF->__sanitizer_print_stack_trace();
|
2015-10-17 07:04:31 +08:00
|
|
|
Printf("SUMMARY: libFuzzer: timeout\n");
|
2016-02-27 06:42:23 +08:00
|
|
|
PrintFinalStats();
|
2016-03-24 09:32:08 +08:00
|
|
|
_Exit(Options.TimeoutExitCode); // Stop right now.
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 07:38:07 +08:00
|
|
|
void Fuzzer::RssLimitCallback() {
|
2016-05-27 08:54:15 +08:00
|
|
|
Printf(
|
|
|
|
"==%d== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n",
|
|
|
|
GetPid(), GetPeakRSSMb(), Options.RssLimitMb);
|
2016-06-02 09:33:11 +08:00
|
|
|
Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
|
2016-06-08 07:32:50 +08:00
|
|
|
if (EF->__sanitizer_print_memory_profile)
|
|
|
|
EF->__sanitizer_print_memory_profile(50);
|
2016-05-27 08:54:15 +08:00
|
|
|
DumpCurrentUnit("oom-");
|
|
|
|
Printf("SUMMARY: libFuzzer: out-of-memory\n");
|
|
|
|
PrintFinalStats();
|
|
|
|
_Exit(Options.ErrorExitCode); // Stop right now.
|
2016-05-07 05:58:35 +08:00
|
|
|
}
|
|
|
|
|
2015-10-23 06:56:45 +08:00
|
|
|
void Fuzzer::PrintStats(const char *Where, const char *End) {
|
2016-02-27 06:42:23 +08:00
|
|
|
size_t ExecPerSec = execPerSec();
|
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");
|
|
|
|
}
|
2016-04-26 03:41:45 +08:00
|
|
|
Printf("%zd,%zd,%zd,%zd,%zd,%zd,%s\n", TotalNumberOfRuns,
|
2016-05-11 07:43:15 +08:00
|
|
|
MaxCoverage.BlockCoverage, MaxCoverage.CounterBitmapBits,
|
|
|
|
MaxCoverage.CallerCalleeCoverage, Corpus.size(), ExecPerSec, Where);
|
2015-11-12 12:38:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Options.Verbosity)
|
|
|
|
return;
|
2015-10-23 06:50:47 +08:00
|
|
|
Printf("#%zd\t%s", TotalNumberOfRuns, Where);
|
2016-05-11 07:43:15 +08:00
|
|
|
if (MaxCoverage.BlockCoverage)
|
|
|
|
Printf(" cov: %zd", MaxCoverage.BlockCoverage);
|
2016-08-17 01:37:13 +08:00
|
|
|
if (MaxCoverage.PCMapBits)
|
|
|
|
Printf(" path: %zd", MaxCoverage.PCMapBits);
|
2016-08-17 03:33:51 +08:00
|
|
|
if (MaxCoverage.VPMapBits)
|
|
|
|
Printf(" vp: %zd", MaxCoverage.VPMapBits);
|
2016-05-11 07:43:15 +08:00
|
|
|
if (auto TB = MaxCoverage.CounterBitmapBits)
|
2015-10-23 06:50:47 +08:00
|
|
|
Printf(" bits: %zd", TB);
|
2016-05-11 07:43:15 +08:00
|
|
|
if (MaxCoverage.CallerCalleeCoverage)
|
|
|
|
Printf(" indir: %zd", MaxCoverage.CallerCalleeCoverage);
|
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
|
|
|
Printf("%s", End);
|
2015-03-31 06:44:03 +08:00
|
|
|
}
|
|
|
|
|
2016-02-27 06:42:23 +08:00
|
|
|
void Fuzzer::PrintFinalStats() {
|
|
|
|
if (!Options.PrintFinalStats) return;
|
|
|
|
size_t ExecPerSec = execPerSec();
|
|
|
|
Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns);
|
|
|
|
Printf("stat::average_exec_per_sec: %zd\n", ExecPerSec);
|
|
|
|
Printf("stat::new_units_added: %zd\n", NumberOfNewUnitsAdded);
|
|
|
|
Printf("stat::slowest_unit_time_sec: %zd\n", TimeOfLongestUnitInSeconds);
|
|
|
|
Printf("stat::peak_rss_mb: %zd\n", GetPeakRSSMb());
|
|
|
|
}
|
|
|
|
|
2016-03-12 09:57:04 +08:00
|
|
|
size_t Fuzzer::MaxUnitSizeInCorpus() const {
|
|
|
|
size_t Res = 0;
|
|
|
|
for (auto &X : Corpus)
|
|
|
|
Res = std::max(Res, X.size());
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::SetMaxLen(size_t MaxLen) {
|
|
|
|
assert(Options.MaxLen == 0); // Can only reset MaxLen from 0 to non-0.
|
|
|
|
assert(MaxLen);
|
|
|
|
Options.MaxLen = MaxLen;
|
|
|
|
Printf("INFO: -max_len is not provided, using %zd\n", Options.MaxLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Fuzzer::RereadOutputCorpus(size_t MaxSize) {
|
2016-01-23 06:28:27 +08:00
|
|
|
if (Options.OutputCorpus.empty())
|
|
|
|
return;
|
2015-05-09 05:30:55 +08:00
|
|
|
std::vector<Unit> AdditionalCorpus;
|
|
|
|
ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus,
|
2016-03-12 09:57:04 +08:00
|
|
|
&EpochOfLastReadOfOutputCorpus, MaxSize);
|
2015-05-09 05:30:55 +08:00
|
|
|
if (Corpus.empty()) {
|
|
|
|
Corpus = AdditionalCorpus;
|
|
|
|
return;
|
|
|
|
}
|
2016-01-23 06:28:27 +08:00
|
|
|
if (!Options.Reload)
|
|
|
|
return;
|
2015-05-20 06:12:57 +08:00
|
|
|
if (Options.Verbosity >= 2)
|
2016-01-23 06:28:27 +08:00
|
|
|
Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
|
2015-05-09 05:30:55 +08:00
|
|
|
for (auto &X : AdditionalCorpus) {
|
2016-03-12 09:57:04 +08:00
|
|
|
if (X.size() > MaxSize)
|
|
|
|
X.resize(MaxSize);
|
2015-05-19 09:06:07 +08:00
|
|
|
if (UnitHashesAddedToCorpus.insert(Hash(X)).second) {
|
2016-01-14 07:46:01 +08:00
|
|
|
if (RunOne(X)) {
|
2015-05-19 09:06:07 +08:00
|
|
|
Corpus.push_back(X);
|
2016-01-23 06:28:27 +08:00
|
|
|
UpdateCorpusDistribution();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 08:23:29 +08:00
|
|
|
void Fuzzer::ShuffleCorpus(UnitVector *V) {
|
|
|
|
std::random_shuffle(V->begin(), V->end(), MD.GetRand());
|
|
|
|
if (Options.PreferSmall)
|
|
|
|
std::stable_sort(V->begin(), V->end(), [](const Unit &A, const Unit &B) {
|
|
|
|
return A.size() < B.size();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:14:29 +08:00
|
|
|
// Tries random prefixes of corpus items.
|
|
|
|
// Prefix length is chosen according to exponential distribution
|
|
|
|
// to sample short lengths much more heavily.
|
|
|
|
void Fuzzer::TruncateUnits(std::vector<Unit> *NewCorpus) {
|
|
|
|
size_t MaxCorpusLen = 0;
|
|
|
|
for (const auto &U : Corpus)
|
|
|
|
MaxCorpusLen = std::max(MaxCorpusLen, U.size());
|
|
|
|
|
|
|
|
if (MaxCorpusLen <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 50% of exponential distribution is Log[2]/lambda.
|
|
|
|
// Choose lambda so that median is MaxCorpusLen / 2.
|
|
|
|
double Lambda = 2.0 * log(2.0) / static_cast<double>(MaxCorpusLen);
|
|
|
|
std::exponential_distribution<> Dist(Lambda);
|
|
|
|
std::vector<double> Sizes;
|
|
|
|
size_t TruncatePoints = std::max(1ul, TruncateMaxRuns / Corpus.size());
|
|
|
|
Sizes.reserve(TruncatePoints);
|
|
|
|
for (size_t I = 0; I < TruncatePoints; ++I) {
|
|
|
|
Sizes.push_back(Dist(MD.GetRand().Get_mt19937()) + 1);
|
|
|
|
}
|
|
|
|
std::sort(Sizes.begin(), Sizes.end());
|
|
|
|
|
|
|
|
for (size_t S : Sizes) {
|
|
|
|
for (const auto &U : Corpus) {
|
|
|
|
if (S < U.size() && RunOne(U.data(), S)) {
|
|
|
|
Unit U1(U.begin(), U.begin() + S);
|
|
|
|
NewCorpus->push_back(U1);
|
|
|
|
WriteToOutputCorpus(U1);
|
|
|
|
PrintStatusForNewUnit(U1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PrintStats("TRUNC ");
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
void Fuzzer::ShuffleAndMinimize() {
|
2015-10-23 06:56:45 +08:00
|
|
|
PrintStats("READ ");
|
2015-01-30 00:58:29 +08:00
|
|
|
std::vector<Unit> NewCorpus;
|
2016-03-18 08:23:29 +08:00
|
|
|
if (Options.ShuffleAtStartUp)
|
|
|
|
ShuffleCorpus(&Corpus);
|
|
|
|
|
2016-05-25 07:14:29 +08:00
|
|
|
if (Options.TruncateUnits) {
|
|
|
|
ResetCoverage();
|
|
|
|
TruncateUnits(&NewCorpus);
|
|
|
|
ResetCoverage();
|
|
|
|
}
|
|
|
|
|
2016-03-18 08:23:29 +08:00
|
|
|
for (const auto &U : Corpus) {
|
2016-06-08 02:16:32 +08:00
|
|
|
bool NewCoverage = RunOne(U);
|
|
|
|
if (!Options.PruneCorpus || NewCoverage) {
|
2016-03-18 08:23:29 +08:00
|
|
|
NewCorpus.push_back(U);
|
|
|
|
if (Options.Verbosity >= 2)
|
2016-05-11 07:43:15 +08:00
|
|
|
Printf("NEW0: %zd L %zd\n", MaxCoverage.BlockCoverage, U.size());
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
2016-05-27 04:25:49 +08:00
|
|
|
TryDetectingAMemoryLeak(U.data(), U.size(),
|
|
|
|
/*DuringInitialCorpusExecution*/ true);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
Corpus = NewCorpus;
|
2016-01-23 06:28:27 +08:00
|
|
|
UpdateCorpusDistribution();
|
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");
|
2016-06-08 09:46:13 +08:00
|
|
|
if (Corpus.empty()) {
|
|
|
|
Printf("ERROR: no interesting inputs were found. "
|
|
|
|
"Is the code instrumented for coverage? Exiting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-05-11 07:43:15 +08:00
|
|
|
bool Fuzzer::UpdateMaxCoverage() {
|
2016-08-06 04:09:53 +08:00
|
|
|
uintptr_t PrevPcBufferPos = MaxCoverage.PcBufferPos;
|
|
|
|
bool Res = CController->RecordMax(&MaxCoverage);
|
|
|
|
|
|
|
|
if (Options.PrintNewCovPcs && PrevPcBufferPos != MaxCoverage.PcBufferPos) {
|
|
|
|
uintptr_t* PcBuffer = CController->pc_buffer();
|
|
|
|
for (size_t I = PrevPcBufferPos; I < MaxCoverage.PcBufferPos; ++I) {
|
|
|
|
Printf("%p\n", PcBuffer[I]);
|
2016-05-11 07:43:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2016-02-14 01:56:51 +08:00
|
|
|
bool Fuzzer::RunOne(const uint8_t *Data, size_t Size) {
|
2015-01-30 00:58:29 +08:00
|
|
|
TotalNumberOfRuns++;
|
2015-10-23 06:50:47 +08:00
|
|
|
|
2016-05-11 07:43:15 +08:00
|
|
|
// TODO(aizatsky): this Reset call seems to be not needed.
|
2016-08-06 04:09:53 +08:00
|
|
|
CController->ResetCounters();
|
2016-02-14 01:56:51 +08:00
|
|
|
ExecuteCallback(Data, Size);
|
2016-05-11 07:43:15 +08:00
|
|
|
bool Res = UpdateMaxCoverage();
|
2015-10-23 06:50:47 +08:00
|
|
|
|
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);
|
2016-02-14 01:56:51 +08:00
|
|
|
WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-");
|
2015-03-31 07:04:35 +08:00
|
|
|
}
|
|
|
|
return Res;
|
2015-01-30 07:01:07 +08:00
|
|
|
}
|
|
|
|
|
2016-05-27 04:03:02 +08:00
|
|
|
void Fuzzer::RunOneAndUpdateCorpus(const uint8_t *Data, size_t Size) {
|
2015-05-08 02:32:29 +08:00
|
|
|
if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
|
|
|
|
return;
|
2016-02-14 01:56:51 +08:00
|
|
|
if (RunOne(Data, Size))
|
|
|
|
ReportNewCoverage({Data, Data + Size});
|
2015-05-08 02:32:29 +08:00
|
|
|
}
|
|
|
|
|
2016-05-27 08:54:15 +08:00
|
|
|
size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const {
|
2016-05-27 06:17:32 +08:00
|
|
|
assert(InFuzzingThread());
|
|
|
|
*Data = CurrentUnitData;
|
|
|
|
return CurrentUnitSize;
|
|
|
|
}
|
|
|
|
|
2016-02-14 01:56:51 +08:00
|
|
|
void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
|
2016-05-27 08:54:15 +08:00
|
|
|
assert(InFuzzingThread());
|
2016-05-27 08:21:33 +08:00
|
|
|
LazyAllocateCurrentUnitData();
|
2016-04-19 06:50:39 +08:00
|
|
|
UnitStartTime = system_clock::now();
|
2016-01-14 07:02:30 +08:00
|
|
|
// We copy the contents of Unit into a separate heap buffer
|
|
|
|
// so that we reliably find buffer overflows in it.
|
2016-02-14 01:56:51 +08:00
|
|
|
std::unique_ptr<uint8_t[]> DataCopy(new uint8_t[Size]);
|
|
|
|
memcpy(DataCopy.get(), Data, Size);
|
2016-05-27 08:21:33 +08:00
|
|
|
if (CurrentUnitData && CurrentUnitData != Data)
|
|
|
|
memcpy(CurrentUnitData, Data, Size);
|
2016-02-14 01:56:51 +08:00
|
|
|
AssignTaintLabels(DataCopy.get(), Size);
|
2016-05-27 08:54:15 +08:00
|
|
|
CurrentUnitSize = Size;
|
2016-04-20 08:24:21 +08:00
|
|
|
AllocTracer.Start();
|
2016-02-14 01:56:51 +08:00
|
|
|
int Res = CB(DataCopy.get(), Size);
|
2016-04-20 08:24:21 +08:00
|
|
|
(void)Res;
|
|
|
|
HasMoreMallocsThanFrees = AllocTracer.Stop();
|
2016-05-27 08:54:15 +08:00
|
|
|
CurrentUnitSize = 0;
|
2015-10-03 07:34:06 +08:00
|
|
|
assert(Res == 0);
|
2015-04-01 04:13:20 +08:00
|
|
|
}
|
|
|
|
|
2016-05-11 07:43:15 +08:00
|
|
|
std::string Fuzzer::Coverage::DebugString() const {
|
|
|
|
std::string Result =
|
|
|
|
std::string("Coverage{") + "BlockCoverage=" +
|
|
|
|
std::to_string(BlockCoverage) + " CallerCalleeCoverage=" +
|
|
|
|
std::to_string(CallerCalleeCoverage) + " CounterBitmapBits=" +
|
2016-08-17 03:33:51 +08:00
|
|
|
std::to_string(CounterBitmapBits) + " PCMapBits=" +
|
|
|
|
std::to_string(PCMapBits) + " VPMapBits " +
|
|
|
|
std::to_string(VPMapBits) + "}";
|
2016-05-11 07:43:15 +08:00
|
|
|
return Result;
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::WriteToOutputCorpus(const Unit &U) {
|
2016-05-27 04:03:02 +08:00
|
|
|
if (Options.OnlyASCII)
|
|
|
|
assert(IsASCII(U));
|
2016-01-23 06:28:27 +08:00
|
|
|
if (Options.OutputCorpus.empty())
|
|
|
|
return;
|
2015-01-30 00:58:29 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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())
|
2016-01-23 06:28:27 +08:00
|
|
|
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() {
|
2016-01-23 06:28:27 +08:00
|
|
|
if (Options.OutputCorpus.empty())
|
|
|
|
return;
|
2015-01-30 00:58:29 +08:00
|
|
|
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());
|
2016-02-13 11:25:16 +08:00
|
|
|
MD.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);
|
2016-01-23 06:28:27 +08:00
|
|
|
UpdateCorpusDistribution();
|
2015-11-12 09:02:01 +08:00
|
|
|
UnitHashesAddedToCorpus.insert(Hash(U));
|
2016-02-13 11:25:16 +08:00
|
|
|
MD.RecordSuccessfulMutationSequence();
|
2015-11-12 09:02:01 +08:00
|
|
|
PrintStatusForNewUnit(U);
|
2015-05-08 02:32:29 +08:00
|
|
|
WriteToOutputCorpus(U);
|
2016-02-27 06:42:23 +08:00
|
|
|
NumberOfNewUnitsAdded++;
|
2015-05-08 02:32:29 +08:00
|
|
|
}
|
|
|
|
|
2016-03-18 08:23:29 +08:00
|
|
|
// Finds minimal number of units in 'Extra' that add coverage to 'Initial'.
|
|
|
|
// We do it by actually executing the units, sometimes more than once,
|
|
|
|
// because we may be using different coverage-like signals and the only
|
|
|
|
// common thing between them is that we can say "this unit found new stuff".
|
|
|
|
UnitVector Fuzzer::FindExtraUnits(const UnitVector &Initial,
|
|
|
|
const UnitVector &Extra) {
|
|
|
|
UnitVector Res = Extra;
|
|
|
|
size_t OldSize = Res.size();
|
|
|
|
for (int Iter = 0; Iter < 10; Iter++) {
|
|
|
|
ShuffleCorpus(&Res);
|
|
|
|
ResetCoverage();
|
|
|
|
|
|
|
|
for (auto &U : Initial)
|
|
|
|
RunOne(U);
|
|
|
|
|
|
|
|
Corpus.clear();
|
|
|
|
for (auto &U : Res)
|
|
|
|
if (RunOne(U))
|
|
|
|
Corpus.push_back(U);
|
|
|
|
|
|
|
|
char Stat[7] = "MIN ";
|
|
|
|
Stat[3] = '0' + Iter;
|
|
|
|
PrintStats(Stat);
|
|
|
|
|
|
|
|
size_t NewSize = Corpus.size();
|
2016-05-14 06:11:23 +08:00
|
|
|
assert(NewSize <= OldSize);
|
2016-03-18 08:23:29 +08:00
|
|
|
Res.swap(Corpus);
|
|
|
|
|
2016-05-14 06:11:23 +08:00
|
|
|
if (NewSize + 5 >= OldSize)
|
2016-03-18 08:23:29 +08:00
|
|
|
break;
|
|
|
|
OldSize = NewSize;
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
std::vector<std::string> ExtraCorpora(Corpora.begin() + 1, Corpora.end());
|
|
|
|
|
2016-03-18 08:23:29 +08:00
|
|
|
assert(Options.MaxLen > 0);
|
|
|
|
UnitVector Initial, Extra;
|
|
|
|
ReadDirToVectorOfUnits(Corpora[0].c_str(), &Initial, nullptr, Options.MaxLen);
|
|
|
|
for (auto &C : ExtraCorpora)
|
|
|
|
ReadDirToVectorOfUnits(C.c_str(), &Extra, nullptr, Options.MaxLen);
|
|
|
|
|
|
|
|
if (!Initial.empty()) {
|
|
|
|
Printf("=== Minimizing the initial corpus of %zd units\n", Initial.size());
|
|
|
|
Initial = FindExtraUnits({}, Initial);
|
2015-10-24 09:16:40 +08:00
|
|
|
}
|
2016-03-18 08:23:29 +08:00
|
|
|
|
|
|
|
Printf("=== Merging extra %zd units\n", Extra.size());
|
|
|
|
auto Res = FindExtraUnits(Initial, Extra);
|
|
|
|
|
|
|
|
for (auto &U: Res)
|
|
|
|
WriteToOutputCorpus(U);
|
|
|
|
|
|
|
|
Printf("=== Merge: written %zd units\n", Res.size());
|
2015-10-24 09:16:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 08:24:21 +08:00
|
|
|
// Tries detecting a memory leak on the particular input that we have just
|
|
|
|
// executed before calling this function.
|
2016-05-27 04:25:49 +08:00
|
|
|
void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
|
|
|
|
bool DuringInitialCorpusExecution) {
|
2016-04-20 08:24:21 +08:00
|
|
|
if (!HasMoreMallocsThanFrees) return; // mallocs==frees, a leak is unlikely.
|
|
|
|
if (!Options.DetectLeaks) return;
|
2016-06-08 07:32:50 +08:00
|
|
|
if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) ||
|
|
|
|
!(EF->__lsan_do_recoverable_leak_check))
|
2016-04-20 08:24:21 +08:00
|
|
|
return; // No lsan.
|
|
|
|
// Run the target once again, but with lsan disabled so that if there is
|
|
|
|
// a real leak we do not report it twice.
|
2016-06-08 07:32:50 +08:00
|
|
|
EF->__lsan_disable();
|
2016-05-29 23:58:57 +08:00
|
|
|
RunOne(Data, Size);
|
2016-06-08 07:32:50 +08:00
|
|
|
EF->__lsan_enable();
|
2016-04-20 08:24:21 +08:00
|
|
|
if (!HasMoreMallocsThanFrees) return; // a leak is unlikely.
|
2016-04-28 03:52:34 +08:00
|
|
|
if (NumberOfLeakDetectionAttempts++ > 1000) {
|
|
|
|
Options.DetectLeaks = false;
|
|
|
|
Printf("INFO: libFuzzer disabled leak detection after every mutation.\n"
|
|
|
|
" Most likely the target function accumulates allocated\n"
|
|
|
|
" memory in a global state w/o actually leaking it.\n"
|
|
|
|
" If LeakSanitizer is enabled in this process it will still\n"
|
|
|
|
" run on the process shutdown.\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-20 08:24:21 +08:00
|
|
|
// Now perform the actual lsan pass. This is expensive and we must ensure
|
|
|
|
// we don't call it too often.
|
2016-06-08 07:32:50 +08:00
|
|
|
if (EF->__lsan_do_recoverable_leak_check()) { // Leak is found, report it.
|
2016-05-27 04:25:49 +08:00
|
|
|
if (DuringInitialCorpusExecution)
|
|
|
|
Printf("\nINFO: a leak has been found in the initial corpus.\n\n");
|
|
|
|
Printf("INFO: to ignore leaks on libFuzzer side use -detect_leaks=0.\n\n");
|
2016-05-27 08:54:15 +08:00
|
|
|
CurrentUnitSize = Size;
|
2016-04-20 08:24:21 +08:00
|
|
|
DumpCurrentUnit("leak-");
|
|
|
|
PrintFinalStats();
|
|
|
|
_Exit(Options.ErrorExitCode); // not exit() to disable lsan further on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 10:49:09 +08:00
|
|
|
void Fuzzer::MutateAndTestOne() {
|
2016-05-27 08:21:33 +08:00
|
|
|
LazyAllocateCurrentUnitData();
|
2016-02-13 11:25:16 +08:00
|
|
|
MD.StartMutationSequence();
|
2015-12-19 10:49:09 +08:00
|
|
|
|
2016-02-14 01:56:51 +08:00
|
|
|
auto &U = ChooseUnitToMutate();
|
2016-08-18 04:45:23 +08:00
|
|
|
ComputeSHA1(U.data(), U.size(), BaseSha1); // Remember where we started.
|
2016-05-27 08:21:33 +08:00
|
|
|
assert(CurrentUnitData);
|
2016-02-14 01:56:51 +08:00
|
|
|
size_t Size = U.size();
|
2016-04-09 07:32:24 +08:00
|
|
|
assert(Size <= Options.MaxLen && "Oversized Unit");
|
2016-05-27 08:21:33 +08:00
|
|
|
memcpy(CurrentUnitData, U.data(), Size);
|
2015-12-19 10:49:09 +08:00
|
|
|
|
2015-02-05 03:10:20 +08:00
|
|
|
for (int i = 0; i < Options.MutateDepth; i++) {
|
2016-02-13 10:29:38 +08:00
|
|
|
size_t NewSize = 0;
|
2016-06-04 05:34:29 +08:00
|
|
|
NewSize = MD.Mutate(CurrentUnitData, Size, Options.MaxLen);
|
2015-05-31 01:33:13 +08:00
|
|
|
assert(NewSize > 0 && "Mutator returned empty unit");
|
2016-03-12 09:57:04 +08:00
|
|
|
assert(NewSize <= Options.MaxLen &&
|
2015-05-31 01:33:13 +08:00
|
|
|
"Mutator return overisized unit");
|
2016-02-14 01:56:51 +08:00
|
|
|
Size = NewSize;
|
2016-01-09 11:08:58 +08:00
|
|
|
if (i == 0)
|
|
|
|
StartTraceRecording();
|
2016-05-27 08:21:33 +08:00
|
|
|
RunOneAndUpdateCorpus(CurrentUnitData, Size);
|
2016-01-09 11:08:58 +08:00
|
|
|
StopTraceRecording();
|
2016-05-27 08:21:33 +08:00
|
|
|
TryDetectingAMemoryLeak(CurrentUnitData, Size,
|
2016-05-27 04:25:49 +08:00
|
|
|
/*DuringInitialCorpusExecution*/ false);
|
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.
|
2016-01-23 06:28:27 +08:00
|
|
|
// This function gives more weight to the more recent units.
|
2015-11-12 09:02:01 +08:00
|
|
|
size_t Fuzzer::ChooseUnitIdxToMutate() {
|
2016-02-13 11:25:16 +08:00
|
|
|
size_t Idx =
|
|
|
|
static_cast<size_t>(CorpusDistribution(MD.GetRand().Get_mt19937()));
|
2016-01-23 06:28:27 +08:00
|
|
|
assert(Idx < Corpus.size());
|
|
|
|
return Idx;
|
2015-11-05 07:22:25 +08:00
|
|
|
}
|
|
|
|
|
2016-03-18 08:23:29 +08:00
|
|
|
void Fuzzer::ResetCoverage() {
|
2016-08-06 04:09:53 +08:00
|
|
|
CController->Reset();
|
2016-05-11 07:43:15 +08:00
|
|
|
MaxCoverage.Reset();
|
2016-08-06 04:09:53 +08:00
|
|
|
CController->Prepare(&MaxCoverage);
|
2016-03-18 08:23:29 +08:00
|
|
|
}
|
|
|
|
|
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());
|
2016-01-23 06:28:27 +08:00
|
|
|
Options.PrintNEW = false; // Don't print NEW status lines when drilling.
|
2015-11-12 09:02:01 +08:00
|
|
|
|
|
|
|
Unit U = ChooseUnitToMutate();
|
|
|
|
|
2016-03-18 08:23:29 +08:00
|
|
|
ResetCoverage();
|
2015-11-12 09:02:01 +08:00
|
|
|
|
|
|
|
std::vector<Unit> SavedCorpus;
|
|
|
|
SavedCorpus.swap(Corpus);
|
|
|
|
Corpus.push_back(U);
|
2016-01-23 06:28:27 +08:00
|
|
|
UpdateCorpusDistribution();
|
2015-11-12 09:02:01 +08:00
|
|
|
assert(Corpus.size() == 1);
|
|
|
|
RunOne(U);
|
|
|
|
PrintStats("DRILL ");
|
|
|
|
std::string SavedOutputCorpusPath; // Don't write new units while drilling.
|
|
|
|
SavedOutputCorpusPath.swap(Options.OutputCorpus);
|
|
|
|
Loop();
|
|
|
|
|
2016-03-18 08:23:29 +08:00
|
|
|
ResetCoverage();
|
2015-11-12 09:02:01 +08:00
|
|
|
|
|
|
|
PrintStats("REINIT");
|
|
|
|
SavedOutputCorpusPath.swap(Options.OutputCorpus);
|
2016-01-14 07:46:01 +08:00
|
|
|
for (auto &U : SavedCorpus)
|
2015-11-12 09:02:01 +08:00
|
|
|
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)
|
2016-02-13 11:25:16 +08:00
|
|
|
MD.SetCorpus(&Corpus);
|
2015-09-09 01:30:35 +08:00
|
|
|
while (true) {
|
2015-12-05 10:09:22 +08:00
|
|
|
auto Now = system_clock::now();
|
|
|
|
if (duration_cast<seconds>(Now - LastCorpusReload).count()) {
|
2016-03-12 09:57:04 +08:00
|
|
|
RereadOutputCorpus(Options.MaxLen);
|
2015-12-05 10:09:22 +08:00
|
|
|
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() >
|
2016-01-23 06:28:27 +08:00
|
|
|
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");
|
2016-02-13 11:25:16 +08:00
|
|
|
MD.PrintRecommendedDictionary();
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
void Fuzzer::UpdateCorpusDistribution() {
|
|
|
|
size_t N = Corpus.size();
|
|
|
|
std::vector<double> Intervals(N + 1);
|
|
|
|
std::vector<double> Weights(N);
|
|
|
|
std::iota(Intervals.begin(), Intervals.end(), 0);
|
|
|
|
std::iota(Weights.begin(), Weights.end(), 1);
|
|
|
|
CorpusDistribution = std::piecewise_constant_distribution<double>(
|
|
|
|
Intervals.begin(), Intervals.end(), Weights.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
2016-05-14 02:04:35 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
|
|
|
|
assert(fuzzer::F);
|
2016-06-04 05:34:29 +08:00
|
|
|
return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize);
|
2016-05-14 02:04:35 +08:00
|
|
|
}
|
|
|
|
} // extern "C"
|