2015-01-29 17:58:29 +01: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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-22 00:42:17 +02:00
|
|
|
#include "FuzzerCorpus.h"
|
2016-11-30 20:06:14 +01:00
|
|
|
#include "FuzzerIO.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "FuzzerInternal.h"
|
2016-09-21 03:50:50 +02:00
|
|
|
#include "FuzzerMutate.h"
|
|
|
|
#include "FuzzerRandom.h"
|
2017-01-20 21:57:07 +01:00
|
|
|
#include "FuzzerShmem.h"
|
2016-11-30 20:06:14 +01:00
|
|
|
#include "FuzzerTracePC.h"
|
2015-01-29 17:58:29 +01:00
|
|
|
#include <algorithm>
|
2016-01-14 00:02:30 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
2016-11-30 20:06:14 +01:00
|
|
|
#include <set>
|
2015-01-29 17:58:29 +01:00
|
|
|
|
2015-11-13 02:54:40 +01:00
|
|
|
#if defined(__has_include)
|
2016-04-20 02:24:21 +02:00
|
|
|
#if __has_include(<sanitizer / lsan_interface.h>)
|
|
|
|
#include <sanitizer/lsan_interface.h>
|
|
|
|
#endif
|
2015-11-13 02:54:40 +01:00
|
|
|
#endif
|
|
|
|
|
2016-03-18 15:19:19 +01: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-29 17:58:29 +01:00
|
|
|
namespace fuzzer {
|
2015-10-17 00:47:20 +02:00
|
|
|
static const size_t kMaxUnitSizeToPrint = 256;
|
2015-01-29 17:58:29 +01:00
|
|
|
|
2016-05-26 23:32:30 +02:00
|
|
|
thread_local bool Fuzzer::IsMyThread;
|
|
|
|
|
2017-01-20 21:57:07 +01:00
|
|
|
SharedMemoryRegion SMR;
|
|
|
|
|
2015-03-31 22:13:20 +02:00
|
|
|
// Only one Fuzzer per process.
|
|
|
|
static Fuzzer *F;
|
|
|
|
|
2016-06-16 22:17:41 +02: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 {
|
2016-10-13 21:06:46 +02:00
|
|
|
void Start(int TraceLevel) {
|
|
|
|
this->TraceLevel = TraceLevel;
|
|
|
|
if (TraceLevel)
|
|
|
|
Printf("MallocFreeTracer: START\n");
|
2016-06-16 22:17:41 +02:00
|
|
|
Mallocs = 0;
|
|
|
|
Frees = 0;
|
|
|
|
}
|
|
|
|
// Returns true if there were more mallocs than frees.
|
2016-10-14 00:24:10 +02:00
|
|
|
bool Stop() {
|
2016-10-13 21:06:46 +02:00
|
|
|
if (TraceLevel)
|
|
|
|
Printf("MallocFreeTracer: STOP %zd %zd (%s)\n", Mallocs.load(),
|
|
|
|
Frees.load(), Mallocs == Frees ? "same" : "DIFFERENT");
|
|
|
|
bool Result = Mallocs > Frees;
|
|
|
|
Mallocs = 0;
|
|
|
|
Frees = 0;
|
|
|
|
TraceLevel = 0;
|
|
|
|
return Result;
|
|
|
|
}
|
2016-06-16 22:17:41 +02:00
|
|
|
std::atomic<size_t> Mallocs;
|
|
|
|
std::atomic<size_t> Frees;
|
2016-10-13 21:06:46 +02:00
|
|
|
int TraceLevel = 0;
|
2016-06-16 22:17:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static MallocFreeTracer AllocTracer;
|
|
|
|
|
2016-12-16 23:45:25 +01:00
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2016-06-16 22:17:41 +02:00
|
|
|
void MallocHook(const volatile void *ptr, size_t size) {
|
2016-10-13 21:06:46 +02:00
|
|
|
size_t N = AllocTracer.Mallocs++;
|
2016-11-30 23:39:35 +01:00
|
|
|
F->HandleMalloc(size);
|
2016-10-13 21:06:46 +02:00
|
|
|
if (int TraceLevel = AllocTracer.TraceLevel) {
|
|
|
|
Printf("MALLOC[%zd] %p %zd\n", N, ptr, size);
|
|
|
|
if (TraceLevel >= 2 && EF)
|
|
|
|
EF->__sanitizer_print_stack_trace();
|
|
|
|
}
|
2016-06-16 22:17:41 +02:00
|
|
|
}
|
2016-12-16 23:45:25 +01:00
|
|
|
|
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2016-06-16 22:17:41 +02:00
|
|
|
void FreeHook(const volatile void *ptr) {
|
2016-10-13 21:06:46 +02:00
|
|
|
size_t N = AllocTracer.Frees++;
|
|
|
|
if (int TraceLevel = AllocTracer.TraceLevel) {
|
2016-10-14 00:24:10 +02:00
|
|
|
Printf("FREE[%zd] %p\n", N, ptr);
|
2016-10-13 21:06:46 +02:00
|
|
|
if (TraceLevel >= 2 && EF)
|
|
|
|
EF->__sanitizer_print_stack_trace();
|
|
|
|
}
|
2016-06-16 22:17:41 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 23:39:35 +01:00
|
|
|
// Crash on a single malloc that exceeds the rss limit.
|
|
|
|
void Fuzzer::HandleMalloc(size_t Size) {
|
2016-12-01 18:56:15 +01:00
|
|
|
if (!Options.RssLimitMb || (Size >> 20) < (size_t)Options.RssLimitMb)
|
2016-11-30 23:39:35 +01:00
|
|
|
return;
|
|
|
|
Printf("==%d== ERROR: libFuzzer: out-of-memory (malloc(%zd))\n", GetPid(),
|
|
|
|
Size);
|
|
|
|
Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
|
|
|
|
if (EF->__sanitizer_print_stack_trace)
|
|
|
|
EF->__sanitizer_print_stack_trace();
|
|
|
|
DumpCurrentUnit("oom-");
|
|
|
|
Printf("SUMMARY: libFuzzer: out-of-memory\n");
|
|
|
|
PrintFinalStats();
|
|
|
|
_Exit(Options.ErrorExitCode); // Stop right now.
|
|
|
|
}
|
|
|
|
|
2016-09-22 00:42:17 +02:00
|
|
|
Fuzzer::Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
|
|
|
|
FuzzingOptions Options)
|
|
|
|
: CB(CB), Corpus(Corpus), MD(MD), Options(Options) {
|
2017-03-15 01:34:25 +01:00
|
|
|
if (EF->__sanitizer_set_death_callback)
|
|
|
|
EF->__sanitizer_set_death_callback(StaticDeathCallback);
|
2015-03-31 22:13:20 +02:00
|
|
|
assert(!F);
|
|
|
|
F = this;
|
2016-09-23 04:18:59 +02:00
|
|
|
TPC.ResetMaps();
|
2016-05-26 23:32:30 +02:00
|
|
|
IsMyThread = true;
|
2016-06-16 22:17:41 +02:00
|
|
|
if (Options.DetectLeaks && EF->__sanitizer_install_malloc_and_free_hooks)
|
|
|
|
EF->__sanitizer_install_malloc_and_free_hooks(MallocHook, FreeHook);
|
2016-09-15 03:30:18 +02:00
|
|
|
TPC.SetUseCounters(Options.UseCounters);
|
2016-09-23 02:46:18 +02:00
|
|
|
TPC.SetUseValueProfile(Options.UseValueProfile);
|
2016-10-26 02:20:51 +02:00
|
|
|
TPC.SetPrintNewPCs(Options.PrintNewCovPcs);
|
2016-08-25 03:25:03 +02:00
|
|
|
|
2016-09-17 07:04:47 +02:00
|
|
|
if (Options.Verbosity)
|
|
|
|
TPC.PrintModuleInfo();
|
2016-10-09 00:12:14 +02:00
|
|
|
if (!Options.OutputCorpus.empty() && Options.ReloadIntervalSec)
|
2016-09-21 03:04:43 +02:00
|
|
|
EpochOfLastReadOfOutputCorpus = GetEpoch(Options.OutputCorpus);
|
2016-09-23 01:16:36 +02:00
|
|
|
MaxInputLen = MaxMutationLen = Options.MaxLen;
|
|
|
|
AllocateCurrentUnitData();
|
2016-12-06 00:35:22 +01:00
|
|
|
CurrentUnitSize = 0;
|
|
|
|
memset(BaseSha1, 0, sizeof(BaseSha1));
|
2015-03-31 22:13:20 +02:00
|
|
|
}
|
2015-01-29 17:58:29 +01:00
|
|
|
|
2016-08-05 22:09:53 +02:00
|
|
|
Fuzzer::~Fuzzer() { }
|
|
|
|
|
2016-09-23 01:16:36 +02:00
|
|
|
void Fuzzer::AllocateCurrentUnitData() {
|
|
|
|
if (CurrentUnitData || MaxInputLen == 0) return;
|
|
|
|
CurrentUnitData = new uint8_t[MaxInputLen];
|
2016-05-27 02:21:33 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 22:13:20 +02:00
|
|
|
void Fuzzer::StaticDeathCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->DeathCallback();
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 23:19:21 +01:00
|
|
|
void Fuzzer::DumpCurrentUnit(const char *Prefix) {
|
2016-05-28 06:19:46 +02:00
|
|
|
if (!CurrentUnitData) return; // Happens when running individual inputs.
|
2016-08-17 22:45:23 +02:00
|
|
|
MD.PrintMutationSequence();
|
|
|
|
Printf("; base unit: %s\n", Sha1ToString(BaseSha1).c_str());
|
2016-05-27 02:54:15 +02:00
|
|
|
size_t UnitSize = CurrentUnitSize;
|
2016-05-27 00:17:32 +02:00
|
|
|
if (UnitSize <= kMaxUnitSizeToPrint) {
|
2016-05-27 02:54:15 +02:00
|
|
|
PrintHexArray(CurrentUnitData, UnitSize, "\n");
|
|
|
|
PrintASCII(CurrentUnitData, UnitSize, "\n");
|
2015-10-09 06:03:14 +02:00
|
|
|
}
|
2016-05-27 02:54:15 +02:00
|
|
|
WriteUnitToFileWithPrefix({CurrentUnitData, CurrentUnitData + UnitSize},
|
|
|
|
Prefix);
|
2016-03-01 23:19:21 +01:00
|
|
|
}
|
|
|
|
|
2016-03-18 15:19:19 +01:00
|
|
|
NO_SANITIZE_MEMORY
|
2016-03-01 23:19:21 +01:00
|
|
|
void Fuzzer::DeathCallback() {
|
2016-05-27 00:17:32 +02:00
|
|
|
DumpCurrentUnit("crash-");
|
2016-02-26 23:42:23 +01:00
|
|
|
PrintFinalStats();
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 22:13:20 +02:00
|
|
|
void Fuzzer::StaticAlarmCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->AlarmCallback();
|
|
|
|
}
|
|
|
|
|
2016-03-01 23:19:21 +01:00
|
|
|
void Fuzzer::StaticCrashSignalCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->CrashCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::StaticInterruptCallback() {
|
|
|
|
assert(F);
|
|
|
|
F->InterruptCallback();
|
|
|
|
}
|
|
|
|
|
2017-01-05 23:05:47 +01:00
|
|
|
void Fuzzer::StaticFileSizeExceedCallback() {
|
|
|
|
Printf("==%lu== ERROR: libFuzzer: file size exceeded\n", GetPid());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-03-01 23:19:21 +01:00
|
|
|
void Fuzzer::CrashCallback() {
|
2016-12-13 18:45:44 +01:00
|
|
|
Printf("==%lu== ERROR: libFuzzer: deadly signal\n", GetPid());
|
2016-06-08 01:32:50 +02:00
|
|
|
if (EF->__sanitizer_print_stack_trace)
|
|
|
|
EF->__sanitizer_print_stack_trace();
|
2016-03-01 23:19:21 +01: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();
|
2017-05-03 22:31:19 +02:00
|
|
|
_Exit(Options.ErrorExitCode); // Stop right now.
|
2016-03-01 23:19:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::InterruptCallback() {
|
2016-12-13 18:45:44 +01:00
|
|
|
Printf("==%lu== libFuzzer: run interrupted; exiting\n", GetPid());
|
2016-03-01 23:19:21 +01:00
|
|
|
PrintFinalStats();
|
2016-03-03 23:36:37 +01:00
|
|
|
_Exit(0); // Stop right now, don't perform any at-exit actions.
|
2016-03-01 23:19:21 +01:00
|
|
|
}
|
|
|
|
|
2016-03-18 15:19:19 +01:00
|
|
|
NO_SANITIZE_MEMORY
|
2015-01-29 17:58:29 +01:00
|
|
|
void Fuzzer::AlarmCallback() {
|
2015-05-20 00:12:57 +02:00
|
|
|
assert(Options.UnitTimeoutSec > 0);
|
2017-01-22 02:58:59 +01:00
|
|
|
// In Windows Alarm callback is executed by a different thread.
|
|
|
|
#if !LIBFUZZER_WINDOWS
|
2016-05-27 00:17:32 +02:00
|
|
|
if (!InFuzzingThread()) return;
|
2017-01-22 02:58:59 +01:00
|
|
|
#endif
|
2016-12-13 18:46:25 +01:00
|
|
|
if (!RunningCB)
|
2016-02-17 20:42:34 +01:00
|
|
|
return; // We have not started running units yet.
|
2015-01-29 17:58:29 +01:00
|
|
|
size_t Seconds =
|
|
|
|
duration_cast<seconds>(system_clock::now() - UnitStartTime).count();
|
2016-01-22 23:28:27 +01:00
|
|
|
if (Seconds == 0)
|
|
|
|
return;
|
2015-05-20 00:12:57 +02:00
|
|
|
if (Options.Verbosity >= 2)
|
2015-05-23 03:22:35 +02:00
|
|
|
Printf("AlarmCallback %zd\n", Seconds);
|
2015-05-20 00:12:57 +02:00
|
|
|
if (Seconds >= (size_t)Options.UnitTimeoutSec) {
|
2015-05-23 03:22:35 +02:00
|
|
|
Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds);
|
2015-05-26 22:57:47 +02:00
|
|
|
Printf(" and the timeout value is %d (use -timeout=N to change)\n",
|
|
|
|
Options.UnitTimeoutSec);
|
2016-03-01 23:19:21 +01:00
|
|
|
DumpCurrentUnit("timeout-");
|
2016-12-13 18:45:44 +01:00
|
|
|
Printf("==%lu== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(),
|
2015-10-17 01:04:31 +02:00
|
|
|
Seconds);
|
2016-06-08 01:32:50 +02:00
|
|
|
if (EF->__sanitizer_print_stack_trace)
|
|
|
|
EF->__sanitizer_print_stack_trace();
|
2015-10-17 01:04:31 +02:00
|
|
|
Printf("SUMMARY: libFuzzer: timeout\n");
|
2016-02-26 23:42:23 +01:00
|
|
|
PrintFinalStats();
|
2016-03-24 02:32:08 +01:00
|
|
|
_Exit(Options.TimeoutExitCode); // Stop right now.
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 01:38:07 +02:00
|
|
|
void Fuzzer::RssLimitCallback() {
|
2016-05-27 02:54:15 +02:00
|
|
|
Printf(
|
2016-12-13 18:45:44 +01:00
|
|
|
"==%lu== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n",
|
2016-05-27 02:54:15 +02:00
|
|
|
GetPid(), GetPeakRSSMb(), Options.RssLimitMb);
|
2016-06-02 03:33:11 +02:00
|
|
|
Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n");
|
2016-06-08 01:32:50 +02:00
|
|
|
if (EF->__sanitizer_print_memory_profile)
|
2017-03-16 00:27:53 +01:00
|
|
|
EF->__sanitizer_print_memory_profile(95, 8);
|
2016-05-27 02:54:15 +02:00
|
|
|
DumpCurrentUnit("oom-");
|
|
|
|
Printf("SUMMARY: libFuzzer: out-of-memory\n");
|
|
|
|
PrintFinalStats();
|
|
|
|
_Exit(Options.ErrorExitCode); // Stop right now.
|
2016-05-06 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 03:58:51 +02:00
|
|
|
void Fuzzer::PrintStats(const char *Where, const char *End, size_t Units) {
|
2016-02-26 23:42:23 +01:00
|
|
|
size_t ExecPerSec = execPerSec();
|
2015-11-12 05:38:40 +01:00
|
|
|
if (!Options.Verbosity)
|
|
|
|
return;
|
2015-10-23 00:50:47 +02:00
|
|
|
Printf("#%zd\t%s", TotalNumberOfRuns, Where);
|
2016-09-23 03:20:07 +02:00
|
|
|
if (size_t N = TPC.GetTotalPCCoverage())
|
|
|
|
Printf(" cov: %zd", N);
|
2016-10-06 00:56:21 +02:00
|
|
|
if (size_t N = Corpus.NumFeatures())
|
|
|
|
Printf( " ft: %zd", N);
|
2016-10-21 18:26:27 +02:00
|
|
|
if (!Corpus.empty()) {
|
2016-10-06 00:56:21 +02:00
|
|
|
Printf(" corp: %zd", Corpus.NumActiveUnits());
|
2016-10-05 02:25:17 +02:00
|
|
|
if (size_t N = Corpus.SizeInBytes()) {
|
|
|
|
if (N < (1<<14))
|
|
|
|
Printf("/%zdb", N);
|
|
|
|
else if (N < (1 << 24))
|
|
|
|
Printf("/%zdKb", N >> 10);
|
|
|
|
else
|
|
|
|
Printf("/%zdMb", N >> 20);
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 03:58:51 +02:00
|
|
|
if (Units)
|
|
|
|
Printf(" units: %zd", Units);
|
2016-10-05 02:25:17 +02:00
|
|
|
|
2016-09-23 03:58:51 +02:00
|
|
|
Printf(" exec/s: %zd", ExecPerSec);
|
2016-10-06 07:14:00 +02:00
|
|
|
Printf(" rss: %zdMb", GetPeakRSSMb());
|
2015-08-12 03:55:37 +02:00
|
|
|
Printf("%s", End);
|
2015-03-31 00:44:03 +02:00
|
|
|
}
|
|
|
|
|
2016-02-26 23:42:23 +01:00
|
|
|
void Fuzzer::PrintFinalStats() {
|
2016-09-18 23:47:08 +02:00
|
|
|
if (Options.PrintCoverage)
|
|
|
|
TPC.PrintCoverage();
|
2016-12-19 23:18:08 +01:00
|
|
|
if (Options.DumpCoverage)
|
|
|
|
TPC.DumpCoverage();
|
2016-09-22 00:42:17 +02:00
|
|
|
if (Options.PrintCorpusStats)
|
|
|
|
Corpus.PrintStats();
|
2016-02-26 23:42:23 +01:00
|
|
|
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-09-23 01:16:36 +02:00
|
|
|
void Fuzzer::SetMaxInputLen(size_t MaxInputLen) {
|
|
|
|
assert(this->MaxInputLen == 0); // Can only reset MaxInputLen from 0 to non-0.
|
|
|
|
assert(MaxInputLen);
|
|
|
|
this->MaxInputLen = MaxInputLen;
|
|
|
|
this->MaxMutationLen = MaxInputLen;
|
|
|
|
AllocateCurrentUnitData();
|
2017-06-16 00:43:40 +02:00
|
|
|
Printf("INFO: -max_len is not provided; "
|
|
|
|
"libFuzzer will not generate inputs larger than %zd bytes\n",
|
|
|
|
MaxInputLen);
|
2016-09-23 01:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fuzzer::SetMaxMutationLen(size_t MaxMutationLen) {
|
|
|
|
assert(MaxMutationLen && MaxMutationLen <= MaxInputLen);
|
|
|
|
this->MaxMutationLen = MaxMutationLen;
|
2016-03-12 02:57:04 +01:00
|
|
|
}
|
|
|
|
|
2016-10-18 20:06:05 +02:00
|
|
|
void Fuzzer::CheckExitOnSrcPosOrItem() {
|
2016-09-27 02:10:20 +02:00
|
|
|
if (!Options.ExitOnSrcPos.empty()) {
|
2016-10-26 20:52:04 +02:00
|
|
|
static auto *PCsSet = new std::set<uintptr_t>;
|
2016-10-26 02:42:52 +02:00
|
|
|
for (size_t i = 1, N = TPC.GetNumPCs(); i < N; i++) {
|
|
|
|
uintptr_t PC = TPC.GetPC(i);
|
|
|
|
if (!PC) continue;
|
2016-10-26 20:52:04 +02:00
|
|
|
if (!PCsSet->insert(PC).second) continue;
|
2016-10-26 02:42:52 +02:00
|
|
|
std::string Descr = DescribePC("%L", PC);
|
|
|
|
if (Descr.find(Options.ExitOnSrcPos) != std::string::npos) {
|
|
|
|
Printf("INFO: found line matching '%s', exiting.\n",
|
|
|
|
Options.ExitOnSrcPos.c_str());
|
|
|
|
_Exit(0);
|
2016-09-27 02:10:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-18 20:06:05 +02:00
|
|
|
if (!Options.ExitOnItem.empty()) {
|
|
|
|
if (Corpus.HasUnit(Options.ExitOnItem)) {
|
|
|
|
Printf("INFO: found item with checksum '%s', exiting.\n",
|
|
|
|
Options.ExitOnItem.c_str());
|
|
|
|
_Exit(0);
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 02:10:20 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 02:57:04 +01:00
|
|
|
void Fuzzer::RereadOutputCorpus(size_t MaxSize) {
|
2016-10-09 00:12:14 +02:00
|
|
|
if (Options.OutputCorpus.empty() || !Options.ReloadIntervalSec) return;
|
2015-05-08 23:30:55 +02:00
|
|
|
std::vector<Unit> AdditionalCorpus;
|
|
|
|
ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus,
|
2016-10-09 01:24:45 +02:00
|
|
|
&EpochOfLastReadOfOutputCorpus, MaxSize,
|
|
|
|
/*ExitOnError*/ false);
|
2015-05-20 00:12:57 +02:00
|
|
|
if (Options.Verbosity >= 2)
|
2016-01-22 23:28:27 +01:00
|
|
|
Printf("Reload: read %zd new units.\n", AdditionalCorpus.size());
|
2016-10-09 00:12:14 +02:00
|
|
|
bool Reloaded = false;
|
2016-10-01 01:29:27 +02:00
|
|
|
for (auto &U : AdditionalCorpus) {
|
|
|
|
if (U.size() > MaxSize)
|
|
|
|
U.resize(MaxSize);
|
|
|
|
if (!Corpus.HasUnit(U)) {
|
2017-07-13 00:20:04 +02:00
|
|
|
if (RunOne(U.data(), U.size()))
|
2016-10-09 00:12:14 +02:00
|
|
|
Reloaded = true;
|
2015-05-08 23:30:55 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-09 00:12:14 +02:00
|
|
|
if (Reloaded)
|
|
|
|
PrintStats("RELOAD");
|
2015-05-08 23:30:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-18 01:23:29 +01:00
|
|
|
void Fuzzer::ShuffleCorpus(UnitVector *V) {
|
2017-02-07 23:37:34 +01:00
|
|
|
std::shuffle(V->begin(), V->end(), MD.GetRand());
|
2016-03-18 01:23:29 +01:00
|
|
|
if (Options.PreferSmall)
|
|
|
|
std::stable_sort(V->begin(), V->end(), [](const Unit &A, const Unit &B) {
|
|
|
|
return A.size() < B.size();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-21 03:04:43 +02:00
|
|
|
void Fuzzer::ShuffleAndMinimize(UnitVector *InitialCorpus) {
|
|
|
|
Printf("#0\tREAD units: %zd\n", InitialCorpus->size());
|
2016-03-18 01:23:29 +01:00
|
|
|
if (Options.ShuffleAtStartUp)
|
2016-09-21 03:04:43 +02:00
|
|
|
ShuffleCorpus(InitialCorpus);
|
2016-05-25 01:14:29 +02:00
|
|
|
|
2016-10-05 02:25:17 +02:00
|
|
|
// Test the callback with empty input and never try it again.
|
|
|
|
uint8_t dummy;
|
|
|
|
ExecuteCallback(&dummy, 0);
|
|
|
|
|
2016-09-21 03:04:43 +02:00
|
|
|
for (const auto &U : *InitialCorpus) {
|
2017-07-13 00:20:04 +02:00
|
|
|
RunOne(U.data(), U.size());
|
2016-05-26 22:25:49 +02:00
|
|
|
TryDetectingAMemoryLeak(U.data(), U.size(),
|
|
|
|
/*DuringInitialCorpusExecution*/ true);
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
2015-10-23 00:56:45 +02:00
|
|
|
PrintStats("INITED");
|
2016-06-08 03:46:13 +02:00
|
|
|
if (Corpus.empty()) {
|
|
|
|
Printf("ERROR: no interesting inputs were found. "
|
|
|
|
"Is the code instrumented for coverage? Exiting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
|
2017-07-13 00:20:04 +02:00
|
|
|
void Fuzzer::PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size) {
|
2015-03-31 01:04:35 +02:00
|
|
|
auto TimeOfUnit =
|
|
|
|
duration_cast<seconds>(UnitStopTime - UnitStartTime).count();
|
2015-11-12 05:38:40 +01:00
|
|
|
if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) &&
|
|
|
|
secondsSinceProcessStartUp() >= 2)
|
2015-10-23 00:56:45 +02:00
|
|
|
PrintStats("pulse ");
|
2016-09-22 03:34:58 +02:00
|
|
|
if (TimeOfUnit > TimeOfLongestUnitInSeconds * 1.1 &&
|
2015-08-05 23:43:48 +02:00
|
|
|
TimeOfUnit >= Options.ReportSlowUnits) {
|
2015-03-31 01:04:35 +02:00
|
|
|
TimeOfLongestUnitInSeconds = TimeOfUnit;
|
2015-08-05 23:43:48 +02:00
|
|
|
Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds);
|
2016-02-13 18:56:51 +01:00
|
|
|
WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-");
|
2015-03-31 01:04:35 +02:00
|
|
|
}
|
2017-07-13 00:20:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 03:08:53 +02:00
|
|
|
bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile,
|
|
|
|
InputInfo *II) {
|
2017-07-13 00:20:04 +02:00
|
|
|
if (!Size) return false;
|
|
|
|
|
|
|
|
ExecuteCallback(Data, Size);
|
|
|
|
|
|
|
|
FeatureSetTmp.clear();
|
|
|
|
size_t NumUpdatesBefore = Corpus.NumFeatureUpdates();
|
|
|
|
TPC.CollectFeatures([&](size_t Feature) {
|
|
|
|
Corpus.AddFeature(Feature, Size, Options.Shrink);
|
|
|
|
if (Options.ReduceInputs)
|
|
|
|
FeatureSetTmp.push_back(Feature);
|
|
|
|
});
|
|
|
|
PrintPulseAndReportSlowInput(Data, Size);
|
|
|
|
size_t NumNewFeatures = Corpus.NumFeatureUpdates() - NumUpdatesBefore;
|
|
|
|
if (NumNewFeatures) {
|
2017-07-13 03:08:53 +02:00
|
|
|
Corpus.AddToCorpus({Data, Data + Size}, NumNewFeatures, MayDeleteFile,
|
|
|
|
FeatureSetTmp);
|
2017-07-13 00:20:04 +02:00
|
|
|
CheckExitOnSrcPosOrItem();
|
2017-07-13 03:08:53 +02:00
|
|
|
return true;
|
2017-07-13 00:20:04 +02:00
|
|
|
}
|
2017-07-13 03:08:53 +02:00
|
|
|
if (II && Corpus.TryToReplace(II, Data, Size, FeatureSetTmp)) {
|
|
|
|
CheckExitOnSrcPosOrItem();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-01-30 00:01:07 +01:00
|
|
|
}
|
|
|
|
|
2016-05-27 02:54:15 +02:00
|
|
|
size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const {
|
2016-05-27 00:17:32 +02:00
|
|
|
assert(InFuzzingThread());
|
|
|
|
*Data = CurrentUnitData;
|
|
|
|
return CurrentUnitSize;
|
|
|
|
}
|
|
|
|
|
2017-05-09 03:17:29 +02:00
|
|
|
void Fuzzer::CrashOnOverwrittenData() {
|
|
|
|
Printf("==%d== ERROR: libFuzzer: fuzz target overwrites it's const input\n",
|
|
|
|
GetPid());
|
|
|
|
DumpCurrentUnit("crash-");
|
|
|
|
Printf("SUMMARY: libFuzzer: out-of-memory\n");
|
|
|
|
_Exit(Options.ErrorExitCode); // Stop right now.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare two arrays, but not all bytes if the arrays are large.
|
|
|
|
static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) {
|
|
|
|
const size_t Limit = 64;
|
|
|
|
if (Size <= 64)
|
|
|
|
return !memcmp(A, B, Size);
|
|
|
|
// Compare first and last Limit/2 bytes.
|
|
|
|
return !memcmp(A, B, Limit / 2) &&
|
|
|
|
!memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2);
|
|
|
|
}
|
|
|
|
|
2016-02-13 18:56:51 +01:00
|
|
|
void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
|
2017-07-13 00:20:04 +02:00
|
|
|
TotalNumberOfRuns++;
|
2016-05-27 02:54:15 +02:00
|
|
|
assert(InFuzzingThread());
|
2017-01-20 21:57:07 +01:00
|
|
|
if (SMR.IsClient())
|
|
|
|
SMR.WriteByteArray(Data, Size);
|
2016-01-14 00:02:30 +01:00
|
|
|
// We copy the contents of Unit into a separate heap buffer
|
|
|
|
// so that we reliably find buffer overflows in it.
|
2016-08-25 03:25:03 +02:00
|
|
|
uint8_t *DataCopy = new uint8_t[Size];
|
|
|
|
memcpy(DataCopy, Data, Size);
|
2016-05-27 02:21:33 +02:00
|
|
|
if (CurrentUnitData && CurrentUnitData != Data)
|
|
|
|
memcpy(CurrentUnitData, Data, Size);
|
2016-05-27 02:54:15 +02:00
|
|
|
CurrentUnitSize = Size;
|
2016-10-13 21:06:46 +02:00
|
|
|
AllocTracer.Start(Options.TraceMalloc);
|
2016-09-22 03:34:58 +02:00
|
|
|
UnitStartTime = system_clock::now();
|
2016-09-23 04:18:59 +02:00
|
|
|
TPC.ResetMaps();
|
2016-12-13 18:46:25 +01:00
|
|
|
RunningCB = true;
|
2016-08-25 03:25:03 +02:00
|
|
|
int Res = CB(DataCopy, Size);
|
2016-12-13 18:46:25 +01:00
|
|
|
RunningCB = false;
|
2016-09-22 03:34:58 +02:00
|
|
|
UnitStopTime = system_clock::now();
|
2016-04-20 02:24:21 +02:00
|
|
|
(void)Res;
|
2016-09-22 03:34:58 +02:00
|
|
|
assert(Res == 0);
|
2016-04-20 02:24:21 +02:00
|
|
|
HasMoreMallocsThanFrees = AllocTracer.Stop();
|
2017-05-09 03:17:29 +02:00
|
|
|
if (!LooseMemeq(DataCopy, Data, Size))
|
|
|
|
CrashOnOverwrittenData();
|
2016-05-27 02:54:15 +02:00
|
|
|
CurrentUnitSize = 0;
|
2016-08-25 03:25:03 +02:00
|
|
|
delete[] DataCopy;
|
2015-03-31 22:13:20 +02:00
|
|
|
}
|
|
|
|
|
2015-01-29 17:58:29 +01:00
|
|
|
void Fuzzer::WriteToOutputCorpus(const Unit &U) {
|
2016-05-26 22:03:02 +02:00
|
|
|
if (Options.OnlyASCII)
|
|
|
|
assert(IsASCII(U));
|
2016-01-22 23:28:27 +01:00
|
|
|
if (Options.OutputCorpus.empty())
|
|
|
|
return;
|
2015-01-29 17:58:29 +01:00
|
|
|
std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
|
|
|
|
WriteToFile(U, Path);
|
|
|
|
if (Options.Verbosity >= 2)
|
2015-05-23 03:22:35 +02:00
|
|
|
Printf("Written to %s\n", Path.c_str());
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
|
2015-07-23 20:37:22 +02:00
|
|
|
void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
|
2015-10-17 00:41:47 +02:00
|
|
|
if (!Options.SaveArtifacts)
|
|
|
|
return;
|
2015-10-09 05:57:59 +02:00
|
|
|
std::string Path = Options.ArtifactPrefix + Prefix + Hash(U);
|
2015-11-25 22:40:46 +01:00
|
|
|
if (!Options.ExactArtifactPath.empty())
|
2016-01-22 23:28:27 +01:00
|
|
|
Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix.
|
2015-01-29 17:58:29 +01:00
|
|
|
WriteToFile(U, Path);
|
2015-10-09 05:57:59 +02:00
|
|
|
Printf("artifact_prefix='%s'; Test unit written to %s\n",
|
|
|
|
Options.ArtifactPrefix.c_str(), Path.c_str());
|
2015-12-04 23:29:39 +01:00
|
|
|
if (U.size() <= kMaxUnitSizeToPrint)
|
|
|
|
Printf("Base64: %s\n", Base64(U).c_str());
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
|
2015-11-12 02:02:01 +01:00
|
|
|
void Fuzzer::PrintStatusForNewUnit(const Unit &U) {
|
|
|
|
if (!Options.PrintNEW)
|
|
|
|
return;
|
2015-10-23 00:56:45 +02:00
|
|
|
PrintStats("NEW ", "");
|
2015-05-07 20:32:29 +02:00
|
|
|
if (Options.Verbosity) {
|
2015-12-19 02:09:49 +01:00
|
|
|
Printf(" L: %zd ", U.size());
|
2016-02-13 04:25:16 +01:00
|
|
|
MD.PrintMutationSequence();
|
2015-05-23 03:22:35 +02:00
|
|
|
Printf("\n");
|
2015-05-07 20:32:29 +02:00
|
|
|
}
|
2015-11-12 02:02:01 +01:00
|
|
|
}
|
|
|
|
|
2016-09-22 00:42:17 +02:00
|
|
|
void Fuzzer::ReportNewCoverage(InputInfo *II, const Unit &U) {
|
|
|
|
II->NumSuccessfullMutations++;
|
2016-02-13 04:25:16 +01:00
|
|
|
MD.RecordSuccessfulMutationSequence();
|
2015-11-12 02:02:01 +01:00
|
|
|
PrintStatusForNewUnit(U);
|
2015-05-07 20:32:29 +02:00
|
|
|
WriteToOutputCorpus(U);
|
2016-02-26 23:42:23 +01:00
|
|
|
NumberOfNewUnitsAdded++;
|
2016-10-26 02:20:51 +02:00
|
|
|
TPC.PrintNewPCs();
|
2015-05-07 20:32:29 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 02:24:21 +02:00
|
|
|
// Tries detecting a memory leak on the particular input that we have just
|
|
|
|
// executed before calling this function.
|
2016-05-26 22:25:49 +02:00
|
|
|
void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
|
|
|
|
bool DuringInitialCorpusExecution) {
|
2016-04-20 02:24:21 +02:00
|
|
|
if (!HasMoreMallocsThanFrees) return; // mallocs==frees, a leak is unlikely.
|
|
|
|
if (!Options.DetectLeaks) return;
|
2016-06-08 01:32:50 +02:00
|
|
|
if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) ||
|
|
|
|
!(EF->__lsan_do_recoverable_leak_check))
|
2016-04-20 02:24:21 +02: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 01:32:50 +02:00
|
|
|
EF->__lsan_disable();
|
2016-10-06 01:31:01 +02:00
|
|
|
ExecuteCallback(Data, Size);
|
2016-06-08 01:32:50 +02:00
|
|
|
EF->__lsan_enable();
|
2016-04-20 02:24:21 +02:00
|
|
|
if (!HasMoreMallocsThanFrees) return; // a leak is unlikely.
|
2016-04-27 21:52:34 +02: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"
|
2016-10-14 00:24:10 +02:00
|
|
|
" You may try running this binary with -trace_malloc=[12]"
|
|
|
|
" to get a trace of mallocs and frees.\n"
|
2016-04-27 21:52:34 +02:00
|
|
|
" If LeakSanitizer is enabled in this process it will still\n"
|
|
|
|
" run on the process shutdown.\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-20 02:24:21 +02:00
|
|
|
// Now perform the actual lsan pass. This is expensive and we must ensure
|
|
|
|
// we don't call it too often.
|
2016-06-08 01:32:50 +02:00
|
|
|
if (EF->__lsan_do_recoverable_leak_check()) { // Leak is found, report it.
|
2016-05-26 22:25:49 +02: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 02:54:15 +02:00
|
|
|
CurrentUnitSize = Size;
|
2016-04-20 02:24:21 +02:00
|
|
|
DumpCurrentUnit("leak-");
|
|
|
|
PrintFinalStats();
|
|
|
|
_Exit(Options.ErrorExitCode); // not exit() to disable lsan further on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 00:24:55 +01:00
|
|
|
static size_t ComputeMutationLen(size_t MaxInputSize, size_t MaxMutationLen,
|
|
|
|
Random &Rand) {
|
|
|
|
assert(MaxInputSize <= MaxMutationLen);
|
|
|
|
if (MaxInputSize == MaxMutationLen) return MaxMutationLen;
|
|
|
|
size_t Result = MaxInputSize;
|
|
|
|
size_t R = Rand.Rand();
|
|
|
|
if ((R % (1U << 7)) == 0)
|
|
|
|
Result++;
|
|
|
|
if ((R % (1U << 15)) == 0)
|
|
|
|
Result += 10 + Result / 2;
|
|
|
|
return Min(Result, MaxMutationLen);
|
|
|
|
}
|
|
|
|
|
2015-12-19 03:49:09 +01:00
|
|
|
void Fuzzer::MutateAndTestOne() {
|
2016-02-13 04:25:16 +01:00
|
|
|
MD.StartMutationSequence();
|
2015-12-19 03:49:09 +01:00
|
|
|
|
2016-09-22 00:42:17 +02:00
|
|
|
auto &II = Corpus.ChooseUnitToMutate(MD.GetRand());
|
2016-09-21 23:41:48 +02:00
|
|
|
const auto &U = II.U;
|
|
|
|
memcpy(BaseSha1, II.Sha1, sizeof(BaseSha1));
|
2016-05-27 02:21:33 +02:00
|
|
|
assert(CurrentUnitData);
|
2016-02-13 18:56:51 +01:00
|
|
|
size_t Size = U.size();
|
2016-09-23 01:16:36 +02:00
|
|
|
assert(Size <= MaxInputLen && "Oversized Unit");
|
2016-05-27 02:21:33 +02:00
|
|
|
memcpy(CurrentUnitData, U.data(), Size);
|
2015-12-19 03:49:09 +01:00
|
|
|
|
2016-09-23 01:16:36 +02:00
|
|
|
assert(MaxMutationLen > 0);
|
2016-09-22 03:34:58 +02:00
|
|
|
|
2016-12-28 00:24:55 +01:00
|
|
|
size_t CurrentMaxMutationLen =
|
|
|
|
Options.ExperimentalLenControl
|
|
|
|
? ComputeMutationLen(Corpus.MaxInputSize(), MaxMutationLen,
|
|
|
|
MD.GetRand())
|
|
|
|
: MaxMutationLen;
|
|
|
|
|
2015-02-04 20:10:20 +01:00
|
|
|
for (int i = 0; i < Options.MutateDepth; i++) {
|
2016-09-22 00:42:17 +02:00
|
|
|
if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
|
|
|
|
break;
|
2016-02-13 03:29:38 +01:00
|
|
|
size_t NewSize = 0;
|
2016-12-28 00:24:55 +01:00
|
|
|
NewSize = MD.Mutate(CurrentUnitData, Size, CurrentMaxMutationLen);
|
2015-05-30 19:33:13 +02:00
|
|
|
assert(NewSize > 0 && "Mutator returned empty unit");
|
2016-12-28 00:24:55 +01:00
|
|
|
assert(NewSize <= CurrentMaxMutationLen && "Mutator return overisized unit");
|
2016-02-13 18:56:51 +01:00
|
|
|
Size = NewSize;
|
2016-09-22 00:42:17 +02:00
|
|
|
II.NumExecutedMutations++;
|
2017-07-13 03:08:53 +02:00
|
|
|
if (RunOne(CurrentUnitData, Size, /*MayDeleteFile=*/true, &II))
|
2016-09-22 00:42:17 +02:00
|
|
|
ReportNewCoverage(&II, {CurrentUnitData, CurrentUnitData + Size});
|
2017-07-13 00:20:04 +02:00
|
|
|
|
2016-05-27 02:21:33 +02:00
|
|
|
TryDetectingAMemoryLeak(CurrentUnitData, Size,
|
2016-05-26 22:25:49 +02:00
|
|
|
/*DuringInitialCorpusExecution*/ false);
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 02:02:01 +01:00
|
|
|
void Fuzzer::Loop() {
|
2016-12-30 02:13:07 +01:00
|
|
|
TPC.InitializePrintNewPCs();
|
2015-12-05 03:09:22 +01:00
|
|
|
system_clock::time_point LastCorpusReload = system_clock::now();
|
2015-12-19 03:49:09 +01:00
|
|
|
if (Options.DoCrossOver)
|
2016-02-13 04:25:16 +01:00
|
|
|
MD.SetCorpus(&Corpus);
|
2015-09-08 19:30:35 +02:00
|
|
|
while (true) {
|
2015-12-05 03:09:22 +01:00
|
|
|
auto Now = system_clock::now();
|
2016-10-09 00:12:14 +02:00
|
|
|
if (duration_cast<seconds>(Now - LastCorpusReload).count() >=
|
|
|
|
Options.ReloadIntervalSec) {
|
2016-09-23 01:16:36 +02:00
|
|
|
RereadOutputCorpus(MaxInputLen);
|
2016-10-09 01:24:45 +02:00
|
|
|
LastCorpusReload = system_clock::now();
|
2015-12-05 03:09:22 +01:00
|
|
|
}
|
2015-11-05 00:22:25 +01:00
|
|
|
if (TotalNumberOfRuns >= Options.MaxNumberOfRuns)
|
2015-11-12 05:38:40 +01:00
|
|
|
break;
|
2016-10-15 03:00:24 +02:00
|
|
|
if (TimedOut()) break;
|
2015-11-05 00:22:25 +01:00
|
|
|
// Perform several mutations and runs.
|
2015-12-19 03:49:09 +01:00
|
|
|
MutateAndTestOne();
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
2015-11-12 05:38:40 +01:00
|
|
|
|
|
|
|
PrintStats("DONE ", "\n");
|
2016-02-13 04:25:16 +01:00
|
|
|
MD.PrintRecommendedDictionary();
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
|
2016-10-15 03:00:24 +02:00
|
|
|
void Fuzzer::MinimizeCrashLoop(const Unit &U) {
|
2017-01-19 20:38:12 +01:00
|
|
|
if (U.size() <= 1) return;
|
2016-10-15 03:00:24 +02:00
|
|
|
while (!TimedOut() && TotalNumberOfRuns < Options.MaxNumberOfRuns) {
|
|
|
|
MD.StartMutationSequence();
|
|
|
|
memcpy(CurrentUnitData, U.data(), U.size());
|
|
|
|
for (int i = 0; i < Options.MutateDepth; i++) {
|
|
|
|
size_t NewSize = MD.Mutate(CurrentUnitData, U.size(), MaxMutationLen);
|
|
|
|
assert(NewSize > 0 && NewSize <= MaxMutationLen);
|
2017-07-13 00:20:04 +02:00
|
|
|
ExecuteCallback(CurrentUnitData, NewSize);
|
|
|
|
PrintPulseAndReportSlowInput(CurrentUnitData, NewSize);
|
2016-10-15 03:00:24 +02:00
|
|
|
TryDetectingAMemoryLeak(CurrentUnitData, NewSize,
|
|
|
|
/*DuringInitialCorpusExecution*/ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 21:57:07 +01:00
|
|
|
void Fuzzer::AnnounceOutput(const uint8_t *Data, size_t Size) {
|
|
|
|
if (SMR.IsServer()) {
|
|
|
|
SMR.WriteByteArray(Data, Size);
|
|
|
|
} else if (SMR.IsClient()) {
|
|
|
|
SMR.PostClient();
|
|
|
|
SMR.WaitServer();
|
|
|
|
size_t OtherSize = SMR.ReadByteArraySize();
|
|
|
|
uint8_t *OtherData = SMR.GetByteArray();
|
|
|
|
if (Size != OtherSize || memcmp(Data, OtherData, Size) != 0) {
|
|
|
|
size_t i = 0;
|
|
|
|
for (i = 0; i < Min(Size, OtherSize); i++)
|
|
|
|
if (Data[i] != OtherData[i])
|
|
|
|
break;
|
|
|
|
Printf("==%lu== ERROR: libFuzzer: equivalence-mismatch. Sizes: %zd %zd; "
|
|
|
|
"offset %zd\n", GetPid(), Size, OtherSize, i);
|
|
|
|
DumpCurrentUnit("mismatch-");
|
|
|
|
Printf("SUMMARY: libFuzzer: equivalence-mismatch\n");
|
|
|
|
PrintFinalStats();
|
|
|
|
_Exit(Options.ErrorExitCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 23:28:27 +01:00
|
|
|
} // namespace fuzzer
|
2016-05-13 20:04:35 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
|
|
|
|
assert(fuzzer::F);
|
2016-06-03 23:34:29 +02:00
|
|
|
return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize);
|
2016-05-13 20:04:35 +02:00
|
|
|
}
|
2017-01-19 20:07:26 +01:00
|
|
|
|
|
|
|
// Experimental
|
2017-01-20 21:57:07 +01:00
|
|
|
void LLVMFuzzerAnnounceOutput(const uint8_t *Data, size_t Size) {
|
|
|
|
assert(fuzzer::F);
|
|
|
|
fuzzer::F->AnnounceOutput(Data, Size);
|
|
|
|
}
|
2016-05-13 20:04:35 +02:00
|
|
|
} // extern "C"
|