2015-02-19 19:45:37 +01:00
|
|
|
//===- FuzzerDriver.cpp - FuzzerDriver function and flags -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FuzzerDriver and flag parsing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FuzzerInterface.h"
|
|
|
|
#include "FuzzerInternal.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
2015-05-11 23:31:51 +02:00
|
|
|
#include <chrono>
|
2015-02-19 19:45:37 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
2015-03-31 22:13:20 +02:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2015-02-19 19:45:37 +01:00
|
|
|
|
2016-01-15 01:17:37 +01:00
|
|
|
// This function should be present in the libFuzzer so that the client
|
|
|
|
// binary can test for its existence.
|
|
|
|
extern "C" __attribute__((used)) void __libfuzzer_is_present() {}
|
|
|
|
|
2015-02-19 19:45:37 +01:00
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
// Program arguments.
|
|
|
|
struct FlagDescription {
|
|
|
|
const char *Name;
|
|
|
|
const char *Description;
|
|
|
|
int Default;
|
2015-03-31 22:13:20 +02:00
|
|
|
int *IntFlag;
|
|
|
|
const char **StrFlag;
|
2015-12-10 21:41:53 +01:00
|
|
|
unsigned int *UIntFlag;
|
2015-02-19 19:45:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
2016-03-01 23:33:14 +01:00
|
|
|
#define FUZZER_DEPRECATED_FLAG(Name)
|
2015-03-31 22:13:20 +02:00
|
|
|
#define FUZZER_FLAG_INT(Name, Default, Description) int Name;
|
2015-12-10 21:41:53 +01:00
|
|
|
#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
|
2015-03-31 22:13:20 +02:00
|
|
|
#define FUZZER_FLAG_STRING(Name, Description) const char *Name;
|
2015-02-19 19:45:37 +01:00
|
|
|
#include "FuzzerFlags.def"
|
2016-03-01 23:33:14 +01:00
|
|
|
#undef FUZZER_DEPRECATED_FLAG
|
2015-03-31 22:13:20 +02:00
|
|
|
#undef FUZZER_FLAG_INT
|
2015-12-10 21:41:53 +01:00
|
|
|
#undef FUZZER_FLAG_UNSIGNED
|
2015-03-31 22:13:20 +02:00
|
|
|
#undef FUZZER_FLAG_STRING
|
2015-02-19 19:45:37 +01:00
|
|
|
} Flags;
|
|
|
|
|
2015-10-18 07:15:34 +02:00
|
|
|
static const FlagDescription FlagDescriptions [] {
|
2016-03-01 23:33:14 +01:00
|
|
|
#define FUZZER_DEPRECATED_FLAG(Name) \
|
|
|
|
{#Name, "Deprecated; don't use", 0, nullptr, nullptr, nullptr},
|
2015-03-31 22:13:20 +02:00
|
|
|
#define FUZZER_FLAG_INT(Name, Default, Description) \
|
2015-12-10 21:41:53 +01:00
|
|
|
{#Name, Description, Default, &Flags.Name, nullptr, nullptr},
|
|
|
|
#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) \
|
|
|
|
{#Name, Description, static_cast<int>(Default), \
|
|
|
|
nullptr, nullptr, &Flags.Name},
|
2015-03-31 22:13:20 +02:00
|
|
|
#define FUZZER_FLAG_STRING(Name, Description) \
|
2015-12-10 21:41:53 +01:00
|
|
|
{#Name, Description, 0, nullptr, &Flags.Name, nullptr},
|
2015-02-19 19:45:37 +01:00
|
|
|
#include "FuzzerFlags.def"
|
2016-03-01 23:33:14 +01:00
|
|
|
#undef FUZZER_DEPRECATED_FLAG
|
2015-03-31 22:13:20 +02:00
|
|
|
#undef FUZZER_FLAG_INT
|
2015-12-10 21:41:53 +01:00
|
|
|
#undef FUZZER_FLAG_UNSIGNED
|
2015-03-31 22:13:20 +02:00
|
|
|
#undef FUZZER_FLAG_STRING
|
2015-02-19 19:45:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const size_t kNumFlags =
|
|
|
|
sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
|
|
|
|
|
2015-09-10 18:57:57 +02:00
|
|
|
static std::vector<std::string> *Inputs;
|
|
|
|
static std::string *ProgName;
|
2015-02-19 19:45:37 +01:00
|
|
|
|
|
|
|
static void PrintHelp() {
|
2016-02-02 04:03:47 +01:00
|
|
|
Printf("Usage:\n");
|
|
|
|
auto Prog = ProgName->c_str();
|
|
|
|
Printf("\nTo run fuzzing pass 0 or more directories.\n");
|
|
|
|
Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog);
|
|
|
|
|
|
|
|
Printf("\nTo run individual tests without fuzzing pass 1 or more files:\n");
|
|
|
|
Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog);
|
|
|
|
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("\nFlags: (strictly in form -flag=value)\n");
|
2015-02-19 19:45:37 +01:00
|
|
|
size_t MaxFlagLen = 0;
|
|
|
|
for (size_t F = 0; F < kNumFlags; F++)
|
|
|
|
MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen);
|
|
|
|
|
|
|
|
for (size_t F = 0; F < kNumFlags; F++) {
|
|
|
|
const auto &D = FlagDescriptions[F];
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf(" %s", D.Name);
|
2015-02-19 19:45:37 +01:00
|
|
|
for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf(" ");
|
|
|
|
Printf("\t");
|
|
|
|
Printf("%d\t%s\n", D.Default, D.Description);
|
2015-02-19 19:45:37 +01:00
|
|
|
}
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("\nFlags starting with '--' will be ignored and "
|
|
|
|
"will be passed verbatim to subprocesses.\n");
|
2015-02-19 19:45:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *FlagValue(const char *Param, const char *Name) {
|
|
|
|
size_t Len = strlen(Name);
|
|
|
|
if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 &&
|
|
|
|
Param[Len + 1] == '=')
|
|
|
|
return &Param[Len + 2];
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-15 01:17:37 +01:00
|
|
|
// Avoid calling stol as it triggers a bug in clang/glibc build.
|
|
|
|
static long MyStol(const char *Str) {
|
|
|
|
long Res = 0;
|
2016-01-19 21:33:57 +01:00
|
|
|
long Sign = 1;
|
|
|
|
if (*Str == '-') {
|
|
|
|
Str++;
|
|
|
|
Sign = -1;
|
|
|
|
}
|
2016-01-15 01:17:37 +01:00
|
|
|
for (size_t i = 0; Str[i]; i++) {
|
|
|
|
char Ch = Str[i];
|
|
|
|
if (Ch < '0' || Ch > '9')
|
|
|
|
return Res;
|
|
|
|
Res = Res * 10 + (Ch - '0');
|
|
|
|
}
|
2016-01-19 21:33:57 +01:00
|
|
|
return Res * Sign;
|
2016-01-15 01:17:37 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 19:45:37 +01:00
|
|
|
static bool ParseOneFlag(const char *Param) {
|
|
|
|
if (Param[0] != '-') return false;
|
2015-05-21 22:39:13 +02:00
|
|
|
if (Param[1] == '-') {
|
|
|
|
static bool PrintedWarning = false;
|
|
|
|
if (!PrintedWarning) {
|
|
|
|
PrintedWarning = true;
|
2016-03-12 02:57:04 +01:00
|
|
|
Printf("INFO: libFuzzer ignores flags that start with '--'\n");
|
2015-05-21 22:39:13 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-19 19:45:37 +01:00
|
|
|
for (size_t F = 0; F < kNumFlags; F++) {
|
|
|
|
const char *Name = FlagDescriptions[F].Name;
|
|
|
|
const char *Str = FlagValue(Param, Name);
|
|
|
|
if (Str) {
|
2015-03-31 22:13:20 +02:00
|
|
|
if (FlagDescriptions[F].IntFlag) {
|
2016-01-15 01:17:37 +01:00
|
|
|
int Val = MyStol(Str);
|
2015-03-31 22:13:20 +02:00
|
|
|
*FlagDescriptions[F].IntFlag = Val;
|
|
|
|
if (Flags.verbosity >= 2)
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("Flag: %s %d\n", Name, Val);;
|
2015-03-31 22:13:20 +02:00
|
|
|
return true;
|
2015-12-10 21:41:53 +01:00
|
|
|
} else if (FlagDescriptions[F].UIntFlag) {
|
|
|
|
unsigned int Val = std::stoul(Str);
|
|
|
|
*FlagDescriptions[F].UIntFlag = Val;
|
|
|
|
if (Flags.verbosity >= 2)
|
|
|
|
Printf("Flag: %s %u\n", Name, Val);
|
|
|
|
return true;
|
2015-03-31 22:13:20 +02:00
|
|
|
} else if (FlagDescriptions[F].StrFlag) {
|
|
|
|
*FlagDescriptions[F].StrFlag = Str;
|
|
|
|
if (Flags.verbosity >= 2)
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("Flag: %s %s\n", Name, Str);
|
2015-03-31 22:13:20 +02:00
|
|
|
return true;
|
2016-03-01 23:33:14 +01:00
|
|
|
} else { // Deprecated flag.
|
|
|
|
Printf("Flag: %s: deprecated, don't use\n", Name);
|
|
|
|
return true;
|
2015-03-31 22:13:20 +02:00
|
|
|
}
|
2015-02-19 19:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PrintHelp();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't use any library to minimize dependencies.
|
2015-09-10 18:57:57 +02:00
|
|
|
static void ParseFlags(const std::vector<std::string> &Args) {
|
2015-03-31 22:13:20 +02:00
|
|
|
for (size_t F = 0; F < kNumFlags; F++) {
|
|
|
|
if (FlagDescriptions[F].IntFlag)
|
|
|
|
*FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
|
2015-12-10 21:41:53 +01:00
|
|
|
if (FlagDescriptions[F].UIntFlag)
|
|
|
|
*FlagDescriptions[F].UIntFlag =
|
|
|
|
static_cast<unsigned int>(FlagDescriptions[F].Default);
|
2015-03-31 22:13:20 +02:00
|
|
|
if (FlagDescriptions[F].StrFlag)
|
|
|
|
*FlagDescriptions[F].StrFlag = nullptr;
|
|
|
|
}
|
2015-09-10 18:57:57 +02:00
|
|
|
Inputs = new std::vector<std::string>;
|
|
|
|
for (size_t A = 1; A < Args.size(); A++) {
|
|
|
|
if (ParseOneFlag(Args[A].c_str())) continue;
|
|
|
|
Inputs->push_back(Args[A]);
|
2015-02-19 19:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:31:51 +02:00
|
|
|
static std::mutex Mu;
|
|
|
|
|
|
|
|
static void PulseThread() {
|
|
|
|
while (true) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(600));
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("pulse...\n");
|
2015-05-11 23:31:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 19:45:37 +01:00
|
|
|
static void WorkerThread(const std::string &Cmd, std::atomic<int> *Counter,
|
|
|
|
int NumJobs, std::atomic<bool> *HasErrors) {
|
|
|
|
while (true) {
|
|
|
|
int C = (*Counter)++;
|
|
|
|
if (C >= NumJobs) break;
|
|
|
|
std::string Log = "fuzz-" + std::to_string(C) + ".log";
|
|
|
|
std::string ToRun = Cmd + " > " + Log + " 2>&1\n";
|
|
|
|
if (Flags.verbosity)
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("%s", ToRun.c_str());
|
2015-11-12 02:02:01 +01:00
|
|
|
int ExitCode = ExecuteCommand(ToRun.c_str());
|
2015-02-19 19:45:37 +01:00
|
|
|
if (ExitCode != 0)
|
|
|
|
*HasErrors = true;
|
2015-05-11 23:31:51 +02:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("================== Job %d exited with exit code %d ============\n",
|
|
|
|
C, ExitCode);
|
2015-02-19 19:45:37 +01:00
|
|
|
fuzzer::CopyFileToErr(Log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:57:57 +02:00
|
|
|
static int RunInMultipleProcesses(const std::vector<std::string> &Args,
|
|
|
|
int NumWorkers, int NumJobs) {
|
2015-02-19 19:45:37 +01:00
|
|
|
std::atomic<int> Counter(0);
|
|
|
|
std::atomic<bool> HasErrors(false);
|
|
|
|
std::string Cmd;
|
2015-09-10 18:57:57 +02:00
|
|
|
for (auto &S : Args) {
|
|
|
|
if (FlagValue(S.c_str(), "jobs") || FlagValue(S.c_str(), "workers"))
|
|
|
|
continue;
|
|
|
|
Cmd += S + " ";
|
2015-02-19 19:45:37 +01:00
|
|
|
}
|
|
|
|
std::vector<std::thread> V;
|
2015-05-11 23:31:51 +02:00
|
|
|
std::thread Pulse(PulseThread);
|
2015-05-12 03:43:20 +02:00
|
|
|
Pulse.detach();
|
2015-02-19 19:45:37 +01:00
|
|
|
for (int i = 0; i < NumWorkers; i++)
|
|
|
|
V.push_back(std::thread(WorkerThread, Cmd, &Counter, NumJobs, &HasErrors));
|
|
|
|
for (auto &T : V)
|
|
|
|
T.join();
|
|
|
|
return HasErrors ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2015-10-02 01:23:06 +02:00
|
|
|
int RunOneTest(Fuzzer *F, const char *InputFilePath) {
|
|
|
|
Unit U = FileToVector(InputFilePath);
|
2015-11-03 19:57:25 +01:00
|
|
|
Unit PreciseSizedU(U);
|
|
|
|
assert(PreciseSizedU.size() == PreciseSizedU.capacity());
|
2016-02-13 18:56:51 +01:00
|
|
|
F->ExecuteCallback(PreciseSizedU.data(), PreciseSizedU.size());
|
2015-10-02 01:23:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-02 04:03:47 +01:00
|
|
|
static bool AllInputsAreFiles() {
|
|
|
|
if (Inputs->empty()) return false;
|
|
|
|
for (auto &Path : *Inputs)
|
|
|
|
if (!IsFile(Path))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-13 04:59:26 +01:00
|
|
|
static int FuzzerDriver(const std::vector<std::string> &Args,
|
|
|
|
UserCallback Callback) {
|
2015-09-10 18:57:57 +02:00
|
|
|
using namespace fuzzer;
|
|
|
|
assert(!Args.empty());
|
|
|
|
ProgName = new std::string(Args[0]);
|
|
|
|
ParseFlags(Args);
|
2015-02-19 19:45:37 +01:00
|
|
|
if (Flags.help) {
|
|
|
|
PrintHelp();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-18 21:58:29 +01:00
|
|
|
if (Flags.close_fd_mask & 2)
|
|
|
|
DupAndCloseStderr();
|
|
|
|
if (Flags.close_fd_mask & 1)
|
|
|
|
CloseStdout();
|
|
|
|
|
2015-05-12 20:51:57 +02:00
|
|
|
if (Flags.jobs > 0 && Flags.workers == 0) {
|
|
|
|
Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
|
|
|
|
if (Flags.workers > 1)
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("Running %d workers\n", Flags.workers);
|
2015-05-12 20:51:57 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 19:45:37 +01:00
|
|
|
if (Flags.workers > 0 && Flags.jobs > 0)
|
2015-09-10 18:57:57 +02:00
|
|
|
return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
|
2015-02-19 19:45:37 +01:00
|
|
|
|
2016-03-12 02:57:04 +01:00
|
|
|
const size_t kMaxSaneLen = 1 << 20;
|
2016-03-15 02:28:00 +01:00
|
|
|
const size_t kMinDefaultLen = 64;
|
2015-02-19 19:45:37 +01:00
|
|
|
Fuzzer::FuzzingOptions Options;
|
|
|
|
Options.Verbosity = Flags.verbosity;
|
|
|
|
Options.MaxLen = Flags.max_len;
|
2015-05-20 00:12:57 +02:00
|
|
|
Options.UnitTimeoutSec = Flags.timeout;
|
2016-01-30 00:30:07 +01:00
|
|
|
Options.TimeoutExitCode = Flags.timeout_exitcode;
|
2015-10-02 22:47:55 +02:00
|
|
|
Options.MaxTotalTimeSec = Flags.max_total_time;
|
2015-02-19 19:45:37 +01:00
|
|
|
Options.DoCrossOver = Flags.cross_over;
|
|
|
|
Options.MutateDepth = Flags.mutate_depth;
|
[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 00:27:02 +01:00
|
|
|
Options.UseCounters = Flags.use_counters;
|
2015-10-23 01:55:39 +02:00
|
|
|
Options.UseIndirCalls = Flags.use_indir_calls;
|
2015-05-11 22:51:19 +02:00
|
|
|
Options.UseTraces = Flags.use_traces;
|
2016-01-15 07:24:05 +01:00
|
|
|
Options.UseMemcmp = Flags.use_memcmp;
|
2015-10-17 06:38:26 +02:00
|
|
|
Options.ShuffleAtStartUp = Flags.shuffle;
|
2016-03-18 01:23:29 +01:00
|
|
|
Options.PreferSmall = Flags.prefer_small;
|
2015-05-08 23:30:55 +02:00
|
|
|
Options.Reload = Flags.reload;
|
2015-08-11 03:44:42 +02:00
|
|
|
Options.OnlyASCII = Flags.only_ascii;
|
2015-11-12 05:38:40 +01:00
|
|
|
Options.OutputCSV = Flags.output_csv;
|
2015-02-19 19:45:37 +01:00
|
|
|
if (Flags.runs >= 0)
|
|
|
|
Options.MaxNumberOfRuns = Flags.runs;
|
2015-09-10 18:57:57 +02:00
|
|
|
if (!Inputs->empty())
|
|
|
|
Options.OutputCorpus = (*Inputs)[0];
|
2015-08-05 23:43:48 +02:00
|
|
|
Options.ReportSlowUnits = Flags.report_slow_units;
|
2015-10-09 05:57:59 +02:00
|
|
|
if (Flags.artifact_prefix)
|
|
|
|
Options.ArtifactPrefix = Flags.artifact_prefix;
|
2015-11-25 22:40:46 +01:00
|
|
|
if (Flags.exact_artifact_path)
|
|
|
|
Options.ExactArtifactPath = Flags.exact_artifact_path;
|
2015-11-12 02:02:01 +01:00
|
|
|
std::vector<Unit> Dictionary;
|
2015-09-03 22:23:46 +02:00
|
|
|
if (Flags.dict)
|
2015-11-12 02:02:01 +01:00
|
|
|
if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
|
2015-09-03 22:23:46 +02:00
|
|
|
return 1;
|
2015-11-12 02:02:01 +01:00
|
|
|
if (Flags.verbosity > 0 && !Dictionary.empty())
|
|
|
|
Printf("Dictionary: %zd entries\n", Dictionary.size());
|
2016-03-17 20:59:39 +01:00
|
|
|
bool DoPlainRun = AllInputsAreFiles();
|
|
|
|
Options.SaveArtifacts = !DoPlainRun;
|
2016-01-06 01:21:22 +01:00
|
|
|
Options.PrintNewCovPcs = Flags.print_new_cov_pcs;
|
2016-02-26 23:42:23 +01:00
|
|
|
Options.PrintFinalStats = Flags.print_final_stats;
|
2015-09-03 22:23:46 +02:00
|
|
|
|
2016-02-13 04:00:53 +01:00
|
|
|
unsigned Seed = Flags.seed;
|
|
|
|
// Initialize Seed.
|
|
|
|
if (Seed == 0)
|
|
|
|
Seed = (std::chrono::system_clock::now().time_since_epoch().count() << 10) +
|
|
|
|
getpid();
|
|
|
|
if (Flags.verbosity)
|
2016-03-12 02:57:04 +01:00
|
|
|
Printf("INFO: Seed: %u\n", Seed);
|
2016-02-13 04:00:53 +01:00
|
|
|
|
|
|
|
Random Rand(Seed);
|
2016-02-13 04:25:16 +01:00
|
|
|
MutationDispatcher MD(Rand);
|
|
|
|
Fuzzer F(Callback, MD, Options);
|
2015-09-04 02:12:11 +02:00
|
|
|
|
2015-11-12 02:02:01 +01:00
|
|
|
for (auto &U: Dictionary)
|
2016-01-16 04:53:32 +01:00
|
|
|
if (U.size() <= Word::GetMaxSize())
|
2016-02-13 04:25:16 +01:00
|
|
|
MD.AddWordToManualDictionary(Word(U.data(), U.size()));
|
2015-11-12 02:02:01 +01:00
|
|
|
|
2015-10-17 00:41:47 +02:00
|
|
|
// Timer
|
|
|
|
if (Flags.timeout > 0)
|
|
|
|
SetTimer(Flags.timeout / 2 + 1);
|
2016-03-01 23:19:21 +01:00
|
|
|
if (Flags.handle_segv) SetSigSegvHandler();
|
|
|
|
if (Flags.handle_bus) SetSigBusHandler();
|
|
|
|
if (Flags.handle_abrt) SetSigAbrtHandler();
|
|
|
|
if (Flags.handle_ill) SetSigIllHandler();
|
|
|
|
if (Flags.handle_fpe) SetSigFpeHandler();
|
|
|
|
if (Flags.handle_int) SetSigIntHandler();
|
2016-03-24 22:03:58 +01:00
|
|
|
if (Flags.handle_term) SetSigTermHandler();
|
2015-10-17 00:41:47 +02:00
|
|
|
|
2016-03-17 20:59:39 +01:00
|
|
|
if (DoPlainRun) {
|
|
|
|
Options.SaveArtifacts = false;
|
2016-02-12 03:32:03 +01:00
|
|
|
int Runs = std::max(1, Flags.runs);
|
|
|
|
Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
|
|
|
|
Inputs->size(), Runs);
|
2016-02-02 04:03:47 +01:00
|
|
|
for (auto &Path : *Inputs) {
|
|
|
|
auto StartTime = system_clock::now();
|
2016-03-04 23:35:40 +01:00
|
|
|
for (int Iter = 0; Iter < Runs; Iter++)
|
2016-02-12 03:32:03 +01:00
|
|
|
RunOneTest(&F, Path.c_str());
|
2016-02-02 04:03:47 +01:00
|
|
|
auto StopTime = system_clock::now();
|
|
|
|
auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
|
|
|
|
Printf("%s: %zd ms\n", Path.c_str(), (long)MS);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2016-03-12 02:57:04 +01:00
|
|
|
|
2015-10-24 03:16:40 +02:00
|
|
|
if (Flags.merge) {
|
2016-03-12 02:57:04 +01:00
|
|
|
if (Options.MaxLen == 0)
|
|
|
|
F.SetMaxLen(kMaxSaneLen);
|
2015-10-24 03:16:40 +02:00
|
|
|
F.Merge(*Inputs);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2016-03-12 02:57:04 +01:00
|
|
|
size_t TemporaryMaxLen = Options.MaxLen ? Options.MaxLen : kMaxSaneLen;
|
2015-02-19 19:45:37 +01:00
|
|
|
|
2016-03-12 02:57:04 +01:00
|
|
|
F.RereadOutputCorpus(TemporaryMaxLen);
|
2015-09-10 18:57:57 +02:00
|
|
|
for (auto &inp : *Inputs)
|
2015-05-08 23:30:55 +02:00
|
|
|
if (inp != Options.OutputCorpus)
|
2016-03-12 02:57:04 +01:00
|
|
|
F.ReadDir(inp, nullptr, TemporaryMaxLen);
|
|
|
|
|
|
|
|
if (Options.MaxLen == 0)
|
|
|
|
F.SetMaxLen(
|
2016-03-15 02:28:00 +01:00
|
|
|
std::min(std::max(kMinDefaultLen, F.MaxUnitSizeInCorpus()), kMaxSaneLen));
|
2015-02-19 19:45:37 +01:00
|
|
|
|
|
|
|
if (F.CorpusSize() == 0)
|
|
|
|
F.AddToCorpus(Unit()); // Can't fuzz empty corpus, so add an empty input.
|
|
|
|
F.ShuffleAndMinimize();
|
2015-12-19 04:42:16 +01:00
|
|
|
if (Flags.drill)
|
2015-11-12 02:02:01 +01:00
|
|
|
F.Drill();
|
|
|
|
else
|
|
|
|
F.Loop();
|
|
|
|
|
2015-02-19 19:45:37 +01:00
|
|
|
if (Flags.verbosity)
|
2015-05-23 03:07:46 +02:00
|
|
|
Printf("Done %d runs in %zd second(s)\n", F.getTotalNumberOfRuns(),
|
|
|
|
F.secondsSinceProcessStartUp());
|
2016-02-26 23:42:23 +01:00
|
|
|
F.PrintFinalStats();
|
2015-05-23 03:07:46 +02:00
|
|
|
|
2015-10-03 09:02:05 +02:00
|
|
|
exit(0); // Don't let F destroy itself.
|
2015-02-19 19:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-02-13 04:59:26 +01:00
|
|
|
int FuzzerDriver(int argc, char **argv, UserCallback Callback) {
|
|
|
|
std::vector<std::string> Args(argv, argv + argc);
|
|
|
|
return FuzzerDriver(Args, Callback);
|
|
|
|
}
|
|
|
|
|
2015-02-19 19:45:37 +01:00
|
|
|
} // namespace fuzzer
|