2015-02-19 18:45:37 +00: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 21:31:51 +00:00
|
|
|
#include <chrono>
|
2015-02-19 18:45:37 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
2015-03-31 20:13:20 +00:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2015-02-19 18:45:37 +00:00
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
// Program arguments.
|
|
|
|
struct FlagDescription {
|
|
|
|
const char *Name;
|
|
|
|
const char *Description;
|
|
|
|
int Default;
|
2015-03-31 20:13:20 +00:00
|
|
|
int *IntFlag;
|
|
|
|
const char **StrFlag;
|
2015-02-19 18:45:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
2015-03-31 20:13:20 +00:00
|
|
|
#define FUZZER_FLAG_INT(Name, Default, Description) int Name;
|
|
|
|
#define FUZZER_FLAG_STRING(Name, Description) const char *Name;
|
2015-02-19 18:45:37 +00:00
|
|
|
#include "FuzzerFlags.def"
|
2015-03-31 20:13:20 +00:00
|
|
|
#undef FUZZER_FLAG_INT
|
|
|
|
#undef FUZZER_FLAG_STRING
|
2015-02-19 18:45:37 +00:00
|
|
|
} Flags;
|
|
|
|
|
2015-10-18 05:15:34 +00:00
|
|
|
static const FlagDescription FlagDescriptions [] {
|
2015-03-31 20:13:20 +00:00
|
|
|
#define FUZZER_FLAG_INT(Name, Default, Description) \
|
|
|
|
{ #Name, Description, Default, &Flags.Name, nullptr},
|
|
|
|
#define FUZZER_FLAG_STRING(Name, Description) \
|
|
|
|
{ #Name, Description, 0, nullptr, &Flags.Name },
|
2015-02-19 18:45:37 +00:00
|
|
|
#include "FuzzerFlags.def"
|
2015-03-31 20:13:20 +00:00
|
|
|
#undef FUZZER_FLAG_INT
|
|
|
|
#undef FUZZER_FLAG_STRING
|
2015-02-19 18:45:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const size_t kNumFlags =
|
|
|
|
sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
|
|
|
|
|
2015-09-10 16:57:57 +00:00
|
|
|
static std::vector<std::string> *Inputs;
|
|
|
|
static std::string *ProgName;
|
2015-02-19 18:45:37 +00:00
|
|
|
|
|
|
|
static void PrintHelp() {
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("Usage: %s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n",
|
2015-09-10 16:57:57 +00:00
|
|
|
ProgName->c_str());
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("\nFlags: (strictly in form -flag=value)\n");
|
2015-02-19 18:45:37 +00: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 01:07:46 +00:00
|
|
|
Printf(" %s", D.Name);
|
2015-02-19 18:45:37 +00:00
|
|
|
for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf(" ");
|
|
|
|
Printf("\t");
|
|
|
|
Printf("%d\t%s\n", D.Default, D.Description);
|
2015-02-19 18:45:37 +00:00
|
|
|
}
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("\nFlags starting with '--' will be ignored and "
|
|
|
|
"will be passed verbatim to subprocesses.\n");
|
2015-02-19 18:45:37 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ParseOneFlag(const char *Param) {
|
|
|
|
if (Param[0] != '-') return false;
|
2015-05-21 20:39:13 +00:00
|
|
|
if (Param[1] == '-') {
|
|
|
|
static bool PrintedWarning = false;
|
|
|
|
if (!PrintedWarning) {
|
|
|
|
PrintedWarning = true;
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("WARNING: libFuzzer ignores flags that start with '--'\n");
|
2015-05-21 20:39:13 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-19 18:45:37 +00: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 20:13:20 +00:00
|
|
|
if (FlagDescriptions[F].IntFlag) {
|
|
|
|
int Val = std::stol(Str);
|
|
|
|
*FlagDescriptions[F].IntFlag = Val;
|
|
|
|
if (Flags.verbosity >= 2)
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("Flag: %s %d\n", Name, Val);;
|
2015-03-31 20:13:20 +00:00
|
|
|
return true;
|
|
|
|
} else if (FlagDescriptions[F].StrFlag) {
|
|
|
|
*FlagDescriptions[F].StrFlag = Str;
|
|
|
|
if (Flags.verbosity >= 2)
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("Flag: %s %s\n", Name, Str);
|
2015-03-31 20:13:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-19 18:45:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PrintHelp();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't use any library to minimize dependencies.
|
2015-09-10 16:57:57 +00:00
|
|
|
static void ParseFlags(const std::vector<std::string> &Args) {
|
2015-03-31 20:13:20 +00:00
|
|
|
for (size_t F = 0; F < kNumFlags; F++) {
|
|
|
|
if (FlagDescriptions[F].IntFlag)
|
|
|
|
*FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
|
|
|
|
if (FlagDescriptions[F].StrFlag)
|
|
|
|
*FlagDescriptions[F].StrFlag = nullptr;
|
|
|
|
}
|
2015-09-10 16:57:57 +00: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 18:45:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:31:51 +00: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 01:07:46 +00:00
|
|
|
Printf("pulse...\n");
|
2015-05-11 21:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 18:45:37 +00: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 01:07:46 +00:00
|
|
|
Printf("%s", ToRun.c_str());
|
2015-11-12 01:02:01 +00:00
|
|
|
int ExitCode = ExecuteCommand(ToRun.c_str());
|
2015-02-19 18:45:37 +00:00
|
|
|
if (ExitCode != 0)
|
|
|
|
*HasErrors = true;
|
2015-05-11 21:31:51 +00:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("================== Job %d exited with exit code %d ============\n",
|
|
|
|
C, ExitCode);
|
2015-02-19 18:45:37 +00:00
|
|
|
fuzzer::CopyFileToErr(Log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:57:57 +00:00
|
|
|
static int RunInMultipleProcesses(const std::vector<std::string> &Args,
|
|
|
|
int NumWorkers, int NumJobs) {
|
2015-02-19 18:45:37 +00:00
|
|
|
std::atomic<int> Counter(0);
|
|
|
|
std::atomic<bool> HasErrors(false);
|
|
|
|
std::string Cmd;
|
2015-09-10 16:57:57 +00:00
|
|
|
for (auto &S : Args) {
|
|
|
|
if (FlagValue(S.c_str(), "jobs") || FlagValue(S.c_str(), "workers"))
|
|
|
|
continue;
|
|
|
|
Cmd += S + " ";
|
2015-02-19 18:45:37 +00:00
|
|
|
}
|
|
|
|
std::vector<std::thread> V;
|
2015-05-11 21:31:51 +00:00
|
|
|
std::thread Pulse(PulseThread);
|
2015-05-12 01:43:20 +00:00
|
|
|
Pulse.detach();
|
2015-02-19 18:45:37 +00: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-01 23:23:06 +00:00
|
|
|
int RunOneTest(Fuzzer *F, const char *InputFilePath) {
|
|
|
|
Unit U = FileToVector(InputFilePath);
|
2015-11-03 18:57:25 +00:00
|
|
|
Unit PreciseSizedU(U);
|
|
|
|
assert(PreciseSizedU.size() == PreciseSizedU.capacity());
|
|
|
|
F->ExecuteCallback(PreciseSizedU);
|
2015-10-01 23:23:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-19 18:45:37 +00:00
|
|
|
int FuzzerDriver(int argc, char **argv, UserCallback Callback) {
|
2015-07-24 01:06:40 +00:00
|
|
|
FuzzerRandomLibc Rand(0);
|
|
|
|
SimpleUserSuppliedFuzzer SUSF(&Rand, Callback);
|
2015-05-22 22:35:31 +00:00
|
|
|
return FuzzerDriver(argc, argv, SUSF);
|
|
|
|
}
|
|
|
|
|
|
|
|
int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF) {
|
2015-09-10 16:57:57 +00:00
|
|
|
std::vector<std::string> Args(argv, argv + argc);
|
|
|
|
return FuzzerDriver(Args, USF);
|
|
|
|
}
|
2015-02-19 18:45:37 +00:00
|
|
|
|
2015-09-10 16:57:57 +00:00
|
|
|
int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback) {
|
|
|
|
FuzzerRandomLibc Rand(0);
|
|
|
|
SimpleUserSuppliedFuzzer SUSF(&Rand, Callback);
|
|
|
|
return FuzzerDriver(Args, SUSF);
|
|
|
|
}
|
|
|
|
|
|
|
|
int FuzzerDriver(const std::vector<std::string> &Args,
|
|
|
|
UserSuppliedFuzzer &USF) {
|
|
|
|
using namespace fuzzer;
|
|
|
|
assert(!Args.empty());
|
|
|
|
ProgName = new std::string(Args[0]);
|
|
|
|
ParseFlags(Args);
|
2015-02-19 18:45:37 +00:00
|
|
|
if (Flags.help) {
|
|
|
|
PrintHelp();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-12 18:51:57 +00:00
|
|
|
if (Flags.jobs > 0 && Flags.workers == 0) {
|
|
|
|
Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
|
|
|
|
if (Flags.workers > 1)
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("Running %d workers\n", Flags.workers);
|
2015-05-12 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 18:45:37 +00:00
|
|
|
if (Flags.workers > 0 && Flags.jobs > 0)
|
2015-09-10 16:57:57 +00:00
|
|
|
return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
|
2015-02-19 18:45:37 +00:00
|
|
|
|
|
|
|
Fuzzer::FuzzingOptions Options;
|
|
|
|
Options.Verbosity = Flags.verbosity;
|
|
|
|
Options.MaxLen = Flags.max_len;
|
2015-05-19 22:12:57 +00:00
|
|
|
Options.UnitTimeoutSec = Flags.timeout;
|
2015-10-02 20:47:55 +00:00
|
|
|
Options.MaxTotalTimeSec = Flags.max_total_time;
|
2015-02-19 18:45:37 +00:00
|
|
|
Options.DoCrossOver = Flags.cross_over;
|
|
|
|
Options.MutateDepth = Flags.mutate_depth;
|
|
|
|
Options.ExitOnFirst = Flags.exit_on_first;
|
[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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231166 91177308-0d34-0410-b5e6-96231b3b80d8
2015-03-03 23:27:02 +00:00
|
|
|
Options.UseCounters = Flags.use_counters;
|
2015-10-22 23:55:39 +00:00
|
|
|
Options.UseIndirCalls = Flags.use_indir_calls;
|
2015-05-11 20:51:19 +00:00
|
|
|
Options.UseTraces = Flags.use_traces;
|
2015-10-17 04:38:26 +00:00
|
|
|
Options.ShuffleAtStartUp = Flags.shuffle;
|
2015-02-19 18:45:37 +00:00
|
|
|
Options.PreferSmallDuringInitialShuffle =
|
|
|
|
Flags.prefer_small_during_initial_shuffle;
|
2015-05-08 21:30:55 +00:00
|
|
|
Options.Reload = Flags.reload;
|
2015-08-11 01:44:42 +00:00
|
|
|
Options.OnlyASCII = Flags.only_ascii;
|
2015-08-12 01:55:37 +00:00
|
|
|
Options.TBMDepth = Flags.tbm_depth;
|
|
|
|
Options.TBMWidth = Flags.tbm_width;
|
2015-11-12 04:38:40 +00:00
|
|
|
Options.OutputCSV = Flags.output_csv;
|
2015-02-19 18:45:37 +00:00
|
|
|
if (Flags.runs >= 0)
|
|
|
|
Options.MaxNumberOfRuns = Flags.runs;
|
2015-09-10 16:57:57 +00:00
|
|
|
if (!Inputs->empty())
|
|
|
|
Options.OutputCorpus = (*Inputs)[0];
|
2015-05-18 21:34:20 +00:00
|
|
|
if (Flags.sync_command)
|
|
|
|
Options.SyncCommand = Flags.sync_command;
|
|
|
|
Options.SyncTimeout = Flags.sync_timeout;
|
2015-08-05 21:43:48 +00:00
|
|
|
Options.ReportSlowUnits = Flags.report_slow_units;
|
2015-10-09 03:57:59 +00:00
|
|
|
if (Flags.artifact_prefix)
|
|
|
|
Options.ArtifactPrefix = Flags.artifact_prefix;
|
2015-11-25 21:40:46 +00:00
|
|
|
if (Flags.exact_artifact_path)
|
|
|
|
Options.ExactArtifactPath = Flags.exact_artifact_path;
|
2015-11-12 01:02:01 +00:00
|
|
|
std::vector<Unit> Dictionary;
|
2015-09-03 20:23:46 +00:00
|
|
|
if (Flags.dict)
|
2015-11-12 01:02:01 +00:00
|
|
|
if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
|
2015-09-03 20:23:46 +00:00
|
|
|
return 1;
|
2015-11-12 01:02:01 +00:00
|
|
|
if (Flags.verbosity > 0 && !Dictionary.empty())
|
|
|
|
Printf("Dictionary: %zd entries\n", Dictionary.size());
|
2015-10-16 22:41:47 +00:00
|
|
|
Options.SaveArtifacts = !Flags.test_single_input;
|
2015-09-03 20:23:46 +00:00
|
|
|
|
2015-09-04 00:12:11 +00:00
|
|
|
Fuzzer F(USF, Options);
|
|
|
|
|
2015-11-12 01:02:01 +00:00
|
|
|
for (auto &U: Dictionary)
|
|
|
|
USF.GetMD().AddWordToDictionary(U.data(), U.size());
|
|
|
|
|
2015-10-16 22:41:47 +00:00
|
|
|
// Timer
|
|
|
|
if (Flags.timeout > 0)
|
|
|
|
SetTimer(Flags.timeout / 2 + 1);
|
|
|
|
|
2015-11-21 03:46:43 +00:00
|
|
|
if (Flags.test_single_input) {
|
|
|
|
RunOneTest(&F, Flags.test_single_input);
|
|
|
|
exit(0);
|
|
|
|
}
|
2015-10-01 23:23:06 +00:00
|
|
|
|
2015-10-24 01:16:40 +00:00
|
|
|
if (Flags.merge) {
|
|
|
|
F.Merge(*Inputs);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-05-23 01:07:46 +00:00
|
|
|
unsigned Seed = Flags.seed;
|
|
|
|
// Initialize Seed.
|
|
|
|
if (Seed == 0)
|
|
|
|
Seed = time(0) * 10000 + getpid();
|
2015-02-19 18:45:37 +00:00
|
|
|
if (Flags.verbosity)
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("Seed: %u\n", Seed);
|
2015-07-24 01:06:40 +00:00
|
|
|
USF.GetRand().ResetSeed(Seed);
|
2015-02-19 18:45:37 +00:00
|
|
|
|
2015-05-08 21:30:55 +00:00
|
|
|
F.RereadOutputCorpus();
|
2015-09-10 16:57:57 +00:00
|
|
|
for (auto &inp : *Inputs)
|
2015-05-08 21:30:55 +00:00
|
|
|
if (inp != Options.OutputCorpus)
|
|
|
|
F.ReadDir(inp, nullptr);
|
2015-02-19 18:45:37 +00:00
|
|
|
|
|
|
|
if (F.CorpusSize() == 0)
|
|
|
|
F.AddToCorpus(Unit()); // Can't fuzz empty corpus, so add an empty input.
|
|
|
|
F.ShuffleAndMinimize();
|
|
|
|
if (Flags.save_minimized_corpus)
|
|
|
|
F.SaveCorpus();
|
2015-11-12 01:02:01 +00:00
|
|
|
else if (Flags.drill)
|
|
|
|
F.Drill();
|
|
|
|
else
|
|
|
|
F.Loop();
|
|
|
|
|
2015-02-19 18:45:37 +00:00
|
|
|
if (Flags.verbosity)
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("Done %d runs in %zd second(s)\n", F.getTotalNumberOfRuns(),
|
|
|
|
F.secondsSinceProcessStartUp());
|
|
|
|
|
2015-10-03 07:02:05 +00:00
|
|
|
exit(0); // Don't let F destroy itself.
|
2015-02-19 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzzer
|