2015-02-20 02:45:37 +08: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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-22 06:42:17 +08:00
|
|
|
#include "FuzzerCorpus.h"
|
2015-02-20 02:45:37 +08:00
|
|
|
#include "FuzzerInterface.h"
|
|
|
|
#include "FuzzerInternal.h"
|
2016-09-21 09:50:50 +08:00
|
|
|
#include "FuzzerMutate.h"
|
|
|
|
#include "FuzzerRandom.h"
|
2015-02-20 02:45:37 +08:00
|
|
|
|
2016-04-18 17:17:29 +08:00
|
|
|
#include <algorithm>
|
2015-02-20 02:45:37 +08:00
|
|
|
#include <atomic>
|
2016-04-18 17:17:29 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstring>
|
2015-02-20 02:45:37 +08:00
|
|
|
#include <mutex>
|
2015-04-01 04:13:20 +08:00
|
|
|
#include <string>
|
2016-04-18 17:17:29 +08:00
|
|
|
#include <thread>
|
|
|
|
#include <unistd.h>
|
2015-02-20 02:45:37 +08:00
|
|
|
|
2016-01-15 08:17:37 +08: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-20 02:45:37 +08:00
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
// Program arguments.
|
|
|
|
struct FlagDescription {
|
|
|
|
const char *Name;
|
|
|
|
const char *Description;
|
|
|
|
int Default;
|
2015-04-01 04:13:20 +08:00
|
|
|
int *IntFlag;
|
|
|
|
const char **StrFlag;
|
2015-12-11 04:41:53 +08:00
|
|
|
unsigned int *UIntFlag;
|
2015-02-20 02:45:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
2016-03-02 06:33:14 +08:00
|
|
|
#define FUZZER_DEPRECATED_FLAG(Name)
|
2015-04-01 04:13:20 +08:00
|
|
|
#define FUZZER_FLAG_INT(Name, Default, Description) int Name;
|
2015-12-11 04:41:53 +08:00
|
|
|
#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
|
2015-04-01 04:13:20 +08:00
|
|
|
#define FUZZER_FLAG_STRING(Name, Description) const char *Name;
|
2015-02-20 02:45:37 +08:00
|
|
|
#include "FuzzerFlags.def"
|
2016-03-02 06:33:14 +08:00
|
|
|
#undef FUZZER_DEPRECATED_FLAG
|
2015-04-01 04:13:20 +08:00
|
|
|
#undef FUZZER_FLAG_INT
|
2015-12-11 04:41:53 +08:00
|
|
|
#undef FUZZER_FLAG_UNSIGNED
|
2015-04-01 04:13:20 +08:00
|
|
|
#undef FUZZER_FLAG_STRING
|
2015-02-20 02:45:37 +08:00
|
|
|
} Flags;
|
|
|
|
|
2015-10-18 13:15:34 +08:00
|
|
|
static const FlagDescription FlagDescriptions [] {
|
2016-03-02 06:33:14 +08:00
|
|
|
#define FUZZER_DEPRECATED_FLAG(Name) \
|
|
|
|
{#Name, "Deprecated; don't use", 0, nullptr, nullptr, nullptr},
|
2015-04-01 04:13:20 +08:00
|
|
|
#define FUZZER_FLAG_INT(Name, Default, Description) \
|
2015-12-11 04:41:53 +08: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-04-01 04:13:20 +08:00
|
|
|
#define FUZZER_FLAG_STRING(Name, Description) \
|
2015-12-11 04:41:53 +08:00
|
|
|
{#Name, Description, 0, nullptr, &Flags.Name, nullptr},
|
2015-02-20 02:45:37 +08:00
|
|
|
#include "FuzzerFlags.def"
|
2016-03-02 06:33:14 +08:00
|
|
|
#undef FUZZER_DEPRECATED_FLAG
|
2015-04-01 04:13:20 +08:00
|
|
|
#undef FUZZER_FLAG_INT
|
2015-12-11 04:41:53 +08:00
|
|
|
#undef FUZZER_FLAG_UNSIGNED
|
2015-04-01 04:13:20 +08:00
|
|
|
#undef FUZZER_FLAG_STRING
|
2015-02-20 02:45:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const size_t kNumFlags =
|
|
|
|
sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
|
|
|
|
|
2015-09-11 00:57:57 +08:00
|
|
|
static std::vector<std::string> *Inputs;
|
|
|
|
static std::string *ProgName;
|
2015-02-20 02:45:37 +08:00
|
|
|
|
|
|
|
static void PrintHelp() {
|
2016-02-02 11:03:47 +08: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 09:07:46 +08:00
|
|
|
Printf("\nFlags: (strictly in form -flag=value)\n");
|
2015-02-20 02:45:37 +08: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];
|
2016-09-10 08:35:30 +08:00
|
|
|
if (strstr(D.Description, "internal flag") == D.Description) continue;
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf(" %s", D.Name);
|
2015-02-20 02:45:37 +08:00
|
|
|
for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf(" ");
|
|
|
|
Printf("\t");
|
|
|
|
Printf("%d\t%s\n", D.Default, D.Description);
|
2015-02-20 02:45:37 +08:00
|
|
|
}
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf("\nFlags starting with '--' will be ignored and "
|
|
|
|
"will be passed verbatim to subprocesses.\n");
|
2015-02-20 02:45:37 +08: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 08:17:37 +08:00
|
|
|
// Avoid calling stol as it triggers a bug in clang/glibc build.
|
|
|
|
static long MyStol(const char *Str) {
|
|
|
|
long Res = 0;
|
2016-01-20 04:33:57 +08:00
|
|
|
long Sign = 1;
|
|
|
|
if (*Str == '-') {
|
|
|
|
Str++;
|
|
|
|
Sign = -1;
|
|
|
|
}
|
2016-01-15 08:17:37 +08: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-20 04:33:57 +08:00
|
|
|
return Res * Sign;
|
2016-01-15 08:17:37 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 02:45:37 +08:00
|
|
|
static bool ParseOneFlag(const char *Param) {
|
|
|
|
if (Param[0] != '-') return false;
|
2015-05-22 04:39:13 +08:00
|
|
|
if (Param[1] == '-') {
|
|
|
|
static bool PrintedWarning = false;
|
|
|
|
if (!PrintedWarning) {
|
|
|
|
PrintedWarning = true;
|
2016-03-12 09:57:04 +08:00
|
|
|
Printf("INFO: libFuzzer ignores flags that start with '--'\n");
|
2015-05-22 04:39:13 +08:00
|
|
|
}
|
2016-04-16 05:56:29 +08:00
|
|
|
for (size_t F = 0; F < kNumFlags; F++)
|
|
|
|
if (FlagValue(Param + 1, FlagDescriptions[F].Name))
|
|
|
|
Printf("WARNING: did you mean '%s' (single dash)?\n", Param + 1);
|
2015-05-22 04:39:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-20 02:45:37 +08:00
|
|
|
for (size_t F = 0; F < kNumFlags; F++) {
|
|
|
|
const char *Name = FlagDescriptions[F].Name;
|
|
|
|
const char *Str = FlagValue(Param, Name);
|
|
|
|
if (Str) {
|
2015-04-01 04:13:20 +08:00
|
|
|
if (FlagDescriptions[F].IntFlag) {
|
2016-01-15 08:17:37 +08:00
|
|
|
int Val = MyStol(Str);
|
2015-04-01 04:13:20 +08:00
|
|
|
*FlagDescriptions[F].IntFlag = Val;
|
|
|
|
if (Flags.verbosity >= 2)
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf("Flag: %s %d\n", Name, Val);;
|
2015-04-01 04:13:20 +08:00
|
|
|
return true;
|
2015-12-11 04:41:53 +08: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-04-01 04:13:20 +08:00
|
|
|
} else if (FlagDescriptions[F].StrFlag) {
|
|
|
|
*FlagDescriptions[F].StrFlag = Str;
|
|
|
|
if (Flags.verbosity >= 2)
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf("Flag: %s %s\n", Name, Str);
|
2015-04-01 04:13:20 +08:00
|
|
|
return true;
|
2016-03-02 06:33:14 +08:00
|
|
|
} else { // Deprecated flag.
|
|
|
|
Printf("Flag: %s: deprecated, don't use\n", Name);
|
|
|
|
return true;
|
2015-04-01 04:13:20 +08:00
|
|
|
}
|
2015-02-20 02:45:37 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 00:41:12 +08:00
|
|
|
Printf("\n\nWARNING: unrecognized flag '%s'; "
|
|
|
|
"use -help=1 to list all flags\n\n", Param);
|
|
|
|
return true;
|
2015-02-20 02:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't use any library to minimize dependencies.
|
2015-09-11 00:57:57 +08:00
|
|
|
static void ParseFlags(const std::vector<std::string> &Args) {
|
2015-04-01 04:13:20 +08:00
|
|
|
for (size_t F = 0; F < kNumFlags; F++) {
|
|
|
|
if (FlagDescriptions[F].IntFlag)
|
|
|
|
*FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
|
2015-12-11 04:41:53 +08:00
|
|
|
if (FlagDescriptions[F].UIntFlag)
|
|
|
|
*FlagDescriptions[F].UIntFlag =
|
|
|
|
static_cast<unsigned int>(FlagDescriptions[F].Default);
|
2015-04-01 04:13:20 +08:00
|
|
|
if (FlagDescriptions[F].StrFlag)
|
|
|
|
*FlagDescriptions[F].StrFlag = nullptr;
|
|
|
|
}
|
2015-09-11 00:57:57 +08: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-20 02:45:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 05:31:51 +08:00
|
|
|
static std::mutex Mu;
|
|
|
|
|
|
|
|
static void PulseThread() {
|
|
|
|
while (true) {
|
2016-05-07 07:38:07 +08:00
|
|
|
SleepSeconds(600);
|
2015-05-12 05:31:51 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf("pulse...\n");
|
2015-05-12 05:31:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 02:45:37 +08: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 09:07:46 +08:00
|
|
|
Printf("%s", ToRun.c_str());
|
2016-04-12 04:35:17 +08:00
|
|
|
int ExitCode = ExecuteCommand(ToRun);
|
2015-02-20 02:45:37 +08:00
|
|
|
if (ExitCode != 0)
|
|
|
|
*HasErrors = true;
|
2015-05-12 05:31:51 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf("================== Job %d exited with exit code %d ============\n",
|
|
|
|
C, ExitCode);
|
2015-02-20 02:45:37 +08:00
|
|
|
fuzzer::CopyFileToErr(Log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 09:22:27 +08:00
|
|
|
static std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
|
|
|
|
const char *X1, const char *X2) {
|
2015-02-20 02:45:37 +08:00
|
|
|
std::string Cmd;
|
2015-09-11 00:57:57 +08:00
|
|
|
for (auto &S : Args) {
|
2016-09-01 09:22:27 +08:00
|
|
|
if (FlagValue(S.c_str(), X1) || FlagValue(S.c_str(), X2))
|
2015-09-11 00:57:57 +08:00
|
|
|
continue;
|
|
|
|
Cmd += S + " ";
|
2015-02-20 02:45:37 +08:00
|
|
|
}
|
2016-09-01 09:22:27 +08:00
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
|
|
|
|
const char *X) {
|
|
|
|
return CloneArgsWithoutX(Args, X, X);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int RunInMultipleProcesses(const std::vector<std::string> &Args,
|
|
|
|
int NumWorkers, int NumJobs) {
|
|
|
|
std::atomic<int> Counter(0);
|
|
|
|
std::atomic<bool> HasErrors(false);
|
|
|
|
std::string Cmd = CloneArgsWithoutX(Args, "jobs", "workers");
|
2015-02-20 02:45:37 +08:00
|
|
|
std::vector<std::thread> V;
|
2015-05-12 05:31:51 +08:00
|
|
|
std::thread Pulse(PulseThread);
|
2015-05-12 09:43:20 +08:00
|
|
|
Pulse.detach();
|
2015-02-20 02:45:37 +08: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;
|
|
|
|
}
|
|
|
|
|
2016-05-07 05:58:35 +08:00
|
|
|
static void RssThread(Fuzzer *F, size_t RssLimitMb) {
|
|
|
|
while (true) {
|
2016-05-07 07:38:07 +08:00
|
|
|
SleepSeconds(1);
|
2016-05-07 05:58:35 +08:00
|
|
|
size_t Peak = GetPeakRSSMb();
|
|
|
|
if (Peak > RssLimitMb)
|
2016-05-07 07:38:07 +08:00
|
|
|
F->RssLimitCallback();
|
2016-05-07 05:58:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void StartRssThread(Fuzzer *F, size_t RssLimitMb) {
|
|
|
|
if (!RssLimitMb) return;
|
|
|
|
std::thread T(RssThread, F, RssLimitMb);
|
|
|
|
T.detach();
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:52:05 +08:00
|
|
|
int RunOneTest(Fuzzer *F, const char *InputFilePath, size_t MaxLen) {
|
2015-10-02 07:23:06 +08:00
|
|
|
Unit U = FileToVector(InputFilePath);
|
2016-08-30 22:52:05 +08:00
|
|
|
if (MaxLen && MaxLen < U.size())
|
|
|
|
U.resize(MaxLen);
|
|
|
|
F->RunOne(U.data(), U.size());
|
2016-10-19 02:38:08 +08:00
|
|
|
F->TryDetectingAMemoryLeak(U.data(), U.size(), true);
|
2015-10-02 07:23:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-02 11:03:47 +08:00
|
|
|
static bool AllInputsAreFiles() {
|
|
|
|
if (Inputs->empty()) return false;
|
|
|
|
for (auto &Path : *Inputs)
|
|
|
|
if (!IsFile(Path))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-01 09:22:27 +08:00
|
|
|
int MinimizeCrashInput(const std::vector<std::string> &Args) {
|
|
|
|
if (Inputs->size() != 1) {
|
|
|
|
Printf("ERROR: -minimize_crash should be given one input file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (Flags.runs <= 0 && Flags.max_total_time == 0) {
|
|
|
|
Printf("ERROR: you need to use -runs=N or "
|
|
|
|
"-max_total_time=N with -minimize_crash=1\n" );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
std::string InputFilePath = Inputs->at(0);
|
|
|
|
std::string BaseCmd = CloneArgsWithoutX(Args, "minimize_crash");
|
|
|
|
auto InputPos = BaseCmd.find(" " + InputFilePath + " ");
|
|
|
|
assert(InputPos != std::string::npos);
|
|
|
|
BaseCmd.erase(InputPos, InputFilePath.size() + 1);
|
|
|
|
// BaseCmd += " > /dev/null 2>&1 ";
|
|
|
|
|
|
|
|
std::string CurrentFilePath = InputFilePath;
|
|
|
|
while (true) {
|
|
|
|
Unit U = FileToVector(CurrentFilePath);
|
|
|
|
if (U.size() < 2) {
|
|
|
|
Printf("CRASH_MIN: '%s' is small enough\n", CurrentFilePath.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Printf("CRASH_MIN: minimizing crash input: '%s' (%zd bytes)\n",
|
|
|
|
CurrentFilePath.c_str(), U.size());
|
|
|
|
|
|
|
|
auto Cmd = BaseCmd + " " + CurrentFilePath;
|
|
|
|
|
|
|
|
Printf("CRASH_MIN: executing: %s\n", Cmd.c_str());
|
|
|
|
int ExitCode = ExecuteCommand(Cmd);
|
|
|
|
if (ExitCode == 0) {
|
|
|
|
Printf("ERROR: the input %s did not crash\n", CurrentFilePath.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Printf("CRASH_MIN: '%s' (%zd bytes) caused a crash. Will try to minimize "
|
|
|
|
"it further\n",
|
|
|
|
CurrentFilePath.c_str(), U.size());
|
|
|
|
|
|
|
|
std::string ArtifactPath = "minimized-from-" + Hash(U);
|
|
|
|
Cmd += " -minimize_crash_internal_step=1 -exact_artifact_path=" +
|
|
|
|
ArtifactPath;
|
|
|
|
Printf("CRASH_MIN: executing: %s\n", Cmd.c_str());
|
|
|
|
ExitCode = ExecuteCommand(Cmd);
|
|
|
|
if (ExitCode == 0) {
|
|
|
|
Printf("CRASH_MIN: failed to minimize beyond %s (%d bytes), exiting\n",
|
|
|
|
CurrentFilePath.c_str(), U.size());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
CurrentFilePath = ArtifactPath;
|
|
|
|
Printf("\n\n\n\n\n\n*********************************\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-22 06:42:17 +08:00
|
|
|
int MinimizeCrashInputInternalStep(Fuzzer *F, InputCorpus *Corpus) {
|
2016-09-01 09:22:27 +08:00
|
|
|
assert(Inputs->size() == 1);
|
|
|
|
std::string InputFilePath = Inputs->at(0);
|
|
|
|
Unit U = FileToVector(InputFilePath);
|
|
|
|
assert(U.size() > 2);
|
|
|
|
Printf("INFO: Starting MinimizeCrashInputInternalStep: %zd\n", U.size());
|
2016-10-06 06:56:21 +08:00
|
|
|
Corpus->AddToCorpus(U, 0);
|
2016-09-23 07:16:36 +08:00
|
|
|
F->SetMaxInputLen(U.size());
|
|
|
|
F->SetMaxMutationLen(U.size() - 1);
|
2016-10-15 09:00:24 +08:00
|
|
|
F->MinimizeCrashLoop(U);
|
2016-09-01 09:22:27 +08:00
|
|
|
Printf("INFO: Done MinimizeCrashInputInternalStep, no crashes found\n");
|
|
|
|
exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-02 13:48:02 +08:00
|
|
|
int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
|
2015-09-11 00:57:57 +08:00
|
|
|
using namespace fuzzer;
|
2016-06-02 13:48:02 +08:00
|
|
|
assert(argc && argv && "Argument pointers cannot be nullptr");
|
2016-06-08 07:32:50 +08:00
|
|
|
EF = new ExternalFunctions();
|
|
|
|
if (EF->LLVMFuzzerInitialize)
|
|
|
|
EF->LLVMFuzzerInitialize(argc, argv);
|
2016-06-02 13:48:02 +08:00
|
|
|
const std::vector<std::string> Args(*argv, *argv + *argc);
|
2015-09-11 00:57:57 +08:00
|
|
|
assert(!Args.empty());
|
|
|
|
ProgName = new std::string(Args[0]);
|
|
|
|
ParseFlags(Args);
|
2015-02-20 02:45:37 +08:00
|
|
|
if (Flags.help) {
|
|
|
|
PrintHelp();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-01 09:22:27 +08:00
|
|
|
if (Flags.minimize_crash)
|
|
|
|
return MinimizeCrashInput(Args);
|
|
|
|
|
2016-03-19 04:58:29 +08:00
|
|
|
if (Flags.close_fd_mask & 2)
|
|
|
|
DupAndCloseStderr();
|
|
|
|
if (Flags.close_fd_mask & 1)
|
|
|
|
CloseStdout();
|
|
|
|
|
2015-05-13 02:51:57 +08:00
|
|
|
if (Flags.jobs > 0 && Flags.workers == 0) {
|
|
|
|
Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
|
|
|
|
if (Flags.workers > 1)
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf("Running %d workers\n", Flags.workers);
|
2015-05-13 02:51:57 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 02:45:37 +08:00
|
|
|
if (Flags.workers > 0 && Flags.jobs > 0)
|
2015-09-11 00:57:57 +08:00
|
|
|
return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
|
2015-02-20 02:45:37 +08:00
|
|
|
|
2016-03-12 09:57:04 +08:00
|
|
|
const size_t kMaxSaneLen = 1 << 20;
|
2016-03-15 09:28:00 +08:00
|
|
|
const size_t kMinDefaultLen = 64;
|
2016-06-24 04:44:48 +08:00
|
|
|
FuzzingOptions Options;
|
2015-02-20 02:45:37 +08:00
|
|
|
Options.Verbosity = Flags.verbosity;
|
|
|
|
Options.MaxLen = Flags.max_len;
|
2015-05-20 06:12:57 +08:00
|
|
|
Options.UnitTimeoutSec = Flags.timeout;
|
2016-11-04 03:31:18 +08:00
|
|
|
Options.ErrorExitCode = Flags.error_exitcode;
|
2016-01-30 07:30:07 +08:00
|
|
|
Options.TimeoutExitCode = Flags.timeout_exitcode;
|
2015-10-03 04:47:55 +08:00
|
|
|
Options.MaxTotalTimeSec = Flags.max_total_time;
|
2015-02-20 02:45:37 +08: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 07:27:02 +08:00
|
|
|
Options.UseCounters = Flags.use_counters;
|
2015-10-23 07:55:39 +08:00
|
|
|
Options.UseIndirCalls = Flags.use_indir_calls;
|
2016-01-15 14:24:05 +08:00
|
|
|
Options.UseMemcmp = Flags.use_memcmp;
|
2016-07-16 07:27:19 +08:00
|
|
|
Options.UseMemmem = Flags.use_memmem;
|
2016-10-15 04:20:33 +08:00
|
|
|
Options.UseCmp = Flags.use_cmp;
|
2016-09-23 08:46:18 +08:00
|
|
|
Options.UseValueProfile = Flags.use_value_profile;
|
2016-10-01 09:04:29 +08:00
|
|
|
Options.Shrink = Flags.shrink;
|
2015-10-17 12:38:26 +08:00
|
|
|
Options.ShuffleAtStartUp = Flags.shuffle;
|
2016-03-18 08:23:29 +08:00
|
|
|
Options.PreferSmall = Flags.prefer_small;
|
2016-10-09 06:12:14 +08:00
|
|
|
Options.ReloadIntervalSec = Flags.reload;
|
2015-08-11 09:44:42 +08:00
|
|
|
Options.OnlyASCII = Flags.only_ascii;
|
2015-11-12 12:38:40 +08:00
|
|
|
Options.OutputCSV = Flags.output_csv;
|
2016-04-20 08:24:21 +08:00
|
|
|
Options.DetectLeaks = Flags.detect_leaks;
|
2016-10-14 03:06:46 +08:00
|
|
|
Options.TraceMalloc = Flags.trace_malloc;
|
2016-05-07 07:38:07 +08:00
|
|
|
Options.RssLimitMb = Flags.rss_limit_mb;
|
2015-02-20 02:45:37 +08:00
|
|
|
if (Flags.runs >= 0)
|
|
|
|
Options.MaxNumberOfRuns = Flags.runs;
|
2016-09-01 09:22:27 +08:00
|
|
|
if (!Inputs->empty() && !Flags.minimize_crash_internal_step)
|
2015-09-11 00:57:57 +08:00
|
|
|
Options.OutputCorpus = (*Inputs)[0];
|
2015-08-06 05:43:48 +08:00
|
|
|
Options.ReportSlowUnits = Flags.report_slow_units;
|
2015-10-09 11:57:59 +08:00
|
|
|
if (Flags.artifact_prefix)
|
|
|
|
Options.ArtifactPrefix = Flags.artifact_prefix;
|
2015-11-26 05:40:46 +08:00
|
|
|
if (Flags.exact_artifact_path)
|
|
|
|
Options.ExactArtifactPath = Flags.exact_artifact_path;
|
2015-11-12 09:02:01 +08:00
|
|
|
std::vector<Unit> Dictionary;
|
2015-09-04 04:23:46 +08:00
|
|
|
if (Flags.dict)
|
2015-11-12 09:02:01 +08:00
|
|
|
if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
|
2015-09-04 04:23:46 +08:00
|
|
|
return 1;
|
2015-11-12 09:02:01 +08:00
|
|
|
if (Flags.verbosity > 0 && !Dictionary.empty())
|
|
|
|
Printf("Dictionary: %zd entries\n", Dictionary.size());
|
2016-03-18 03:59:39 +08:00
|
|
|
bool DoPlainRun = AllInputsAreFiles();
|
2016-09-01 09:22:27 +08:00
|
|
|
Options.SaveArtifacts =
|
|
|
|
!DoPlainRun || Flags.minimize_crash_internal_step;
|
2016-08-26 06:35:08 +08:00
|
|
|
Options.PrintNewCovPcs = Flags.print_pcs;
|
2016-02-27 06:42:23 +08:00
|
|
|
Options.PrintFinalStats = Flags.print_final_stats;
|
2016-09-22 06:42:17 +08:00
|
|
|
Options.PrintCorpusStats = Flags.print_corpus_stats;
|
2016-09-19 05:47:08 +08:00
|
|
|
Options.PrintCoverage = Flags.print_coverage;
|
2016-09-27 08:10:20 +08:00
|
|
|
if (Flags.exit_on_src_pos)
|
|
|
|
Options.ExitOnSrcPos = Flags.exit_on_src_pos;
|
2016-10-01 09:04:29 +08:00
|
|
|
if (Flags.exit_on_item)
|
|
|
|
Options.ExitOnItem = Flags.exit_on_item;
|
2015-09-04 04:23:46 +08:00
|
|
|
|
2016-02-13 11:00:53 +08: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 09:57:04 +08:00
|
|
|
Printf("INFO: Seed: %u\n", Seed);
|
2016-02-13 11:00:53 +08:00
|
|
|
|
|
|
|
Random Rand(Seed);
|
2016-11-15 03:21:38 +08:00
|
|
|
auto *MD = new MutationDispatcher(Rand, Options);
|
|
|
|
auto *Corpus = new InputCorpus(Options.OutputCorpus);
|
|
|
|
auto *F = new Fuzzer(Callback, *Corpus, *MD, Options);
|
2015-09-04 08:12:11 +08:00
|
|
|
|
2015-11-12 09:02:01 +08:00
|
|
|
for (auto &U: Dictionary)
|
2016-01-16 11:53:32 +08:00
|
|
|
if (U.size() <= Word::GetMaxSize())
|
2016-11-12 08:24:35 +08:00
|
|
|
MD->AddWordToManualDictionary(Word(U.data(), U.size()));
|
2015-11-12 09:02:01 +08:00
|
|
|
|
2016-11-12 08:24:35 +08:00
|
|
|
StartRssThread(F, Flags.rss_limit_mb);
|
2016-05-07 05:58:35 +08:00
|
|
|
|
2015-10-17 06:41:47 +08:00
|
|
|
// Timer
|
|
|
|
if (Flags.timeout > 0)
|
|
|
|
SetTimer(Flags.timeout / 2 + 1);
|
2016-03-02 06:19:21 +08: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-25 05:03:58 +08:00
|
|
|
if (Flags.handle_term) SetSigTermHandler();
|
2015-10-17 06:41:47 +08:00
|
|
|
|
2016-09-01 09:22:27 +08:00
|
|
|
if (Flags.minimize_crash_internal_step)
|
2016-11-12 08:24:35 +08:00
|
|
|
return MinimizeCrashInputInternalStep(F, Corpus);
|
2016-09-01 09:22:27 +08:00
|
|
|
|
2016-03-18 03:59:39 +08:00
|
|
|
if (DoPlainRun) {
|
|
|
|
Options.SaveArtifacts = false;
|
2016-02-12 10:32:03 +08: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 11:03:47 +08:00
|
|
|
for (auto &Path : *Inputs) {
|
|
|
|
auto StartTime = system_clock::now();
|
2016-06-17 21:07:06 +08:00
|
|
|
Printf("Running: %s\n", Path.c_str());
|
2016-03-05 06:35:40 +08:00
|
|
|
for (int Iter = 0; Iter < Runs; Iter++)
|
2016-11-12 08:24:35 +08:00
|
|
|
RunOneTest(F, Path.c_str(), Options.MaxLen);
|
2016-02-02 11:03:47 +08:00
|
|
|
auto StopTime = system_clock::now();
|
|
|
|
auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
|
2016-06-17 21:07:06 +08:00
|
|
|
Printf("Executed %s in %zd ms\n", Path.c_str(), (long)MS);
|
2016-02-02 11:03:47 +08:00
|
|
|
}
|
2016-08-16 03:44:04 +08:00
|
|
|
Printf("***\n"
|
|
|
|
"*** NOTE: fuzzing was not performed, you have only\n"
|
|
|
|
"*** executed the target code on a fixed set of inputs.\n"
|
|
|
|
"***\n");
|
2016-11-12 08:24:35 +08:00
|
|
|
F->PrintFinalStats();
|
2016-02-02 11:03:47 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-10-24 09:16:40 +08:00
|
|
|
if (Flags.merge) {
|
2016-03-12 09:57:04 +08:00
|
|
|
if (Options.MaxLen == 0)
|
2016-11-12 08:24:35 +08:00
|
|
|
F->SetMaxInputLen(kMaxSaneLen);
|
|
|
|
F->Merge(*Inputs);
|
2015-10-24 09:16:40 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2016-03-12 09:57:04 +08:00
|
|
|
size_t TemporaryMaxLen = Options.MaxLen ? Options.MaxLen : kMaxSaneLen;
|
2015-02-20 02:45:37 +08:00
|
|
|
|
2016-09-21 09:04:43 +08:00
|
|
|
UnitVector InitialCorpus;
|
|
|
|
for (auto &Inp : *Inputs) {
|
|
|
|
Printf("Loading corpus dir: %s\n", Inp.c_str());
|
2016-10-09 07:24:45 +08:00
|
|
|
ReadDirToVectorOfUnits(Inp.c_str(), &InitialCorpus, nullptr,
|
|
|
|
TemporaryMaxLen, /*ExitOnError=*/false);
|
2016-09-21 09:04:43 +08:00
|
|
|
}
|
2016-03-12 09:57:04 +08:00
|
|
|
|
2016-09-21 09:04:43 +08:00
|
|
|
if (Options.MaxLen == 0) {
|
|
|
|
size_t MaxLen = 0;
|
|
|
|
for (auto &U : InitialCorpus)
|
|
|
|
MaxLen = std::max(U.size(), MaxLen);
|
2016-11-12 08:24:35 +08:00
|
|
|
F->SetMaxInputLen(std::min(std::max(kMinDefaultLen, MaxLen), kMaxSaneLen));
|
2016-09-21 09:04:43 +08:00
|
|
|
}
|
2015-02-20 02:45:37 +08:00
|
|
|
|
2016-09-21 09:04:43 +08:00
|
|
|
if (InitialCorpus.empty()) {
|
2016-11-12 10:27:21 +08:00
|
|
|
InitialCorpus.push_back(Unit({'\n'})); // Valid ASCII input.
|
2016-04-19 05:14:11 +08:00
|
|
|
if (Options.Verbosity)
|
|
|
|
Printf("INFO: A corpus is not provided, starting from an empty corpus\n");
|
|
|
|
}
|
2016-11-12 08:24:35 +08:00
|
|
|
F->ShuffleAndMinimize(&InitialCorpus);
|
2016-10-06 13:14:00 +08:00
|
|
|
InitialCorpus.clear(); // Don't need this memory any more.
|
2016-11-12 08:24:35 +08:00
|
|
|
F->Loop();
|
2015-11-12 09:02:01 +08:00
|
|
|
|
2015-02-20 02:45:37 +08:00
|
|
|
if (Flags.verbosity)
|
2016-11-12 08:24:35 +08:00
|
|
|
Printf("Done %d runs in %zd second(s)\n", F->getTotalNumberOfRuns(),
|
|
|
|
F->secondsSinceProcessStartUp());
|
|
|
|
F->PrintFinalStats();
|
2015-05-23 09:07:46 +08:00
|
|
|
|
2015-10-03 15:02:05 +08:00
|
|
|
exit(0); // Don't let F destroy itself.
|
2015-02-20 02:45:37 +08:00
|
|
|
}
|
2016-06-08 07:32:50 +08:00
|
|
|
|
|
|
|
// Storage for global ExternalFunctions object.
|
|
|
|
ExternalFunctions *EF = nullptr;
|
|
|
|
|
2015-02-20 02:45:37 +08:00
|
|
|
} // namespace fuzzer
|