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"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "FuzzerIO.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"
|
2017-01-21 04:57:07 +08:00
|
|
|
#include "FuzzerShmem.h"
|
2016-12-15 14:21:21 +08:00
|
|
|
#include "FuzzerTracePC.h"
|
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>
|
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)
|
2017-06-06 13:08:36 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 01:45:53 +08:00
|
|
|
static void WorkerThread(const std::string &Cmd, std::atomic<unsigned> *Counter,
|
|
|
|
unsigned NumJobs, std::atomic<bool> *HasErrors) {
|
2015-02-20 02:45:37 +08:00
|
|
|
while (true) {
|
2016-12-14 01:45:53 +08:00
|
|
|
unsigned C = (*Counter)++;
|
2015-02-20 02:45:37 +08:00
|
|
|
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);
|
2016-12-14 01:45:53 +08:00
|
|
|
Printf("================== Job %u exited with exit code %d ============\n",
|
2015-05-23 09:07:46 +08:00
|
|
|
C, ExitCode);
|
2015-02-20 02:45:37 +08:00
|
|
|
fuzzer::CopyFileToErr(Log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:24 +08:00
|
|
|
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 int RunInMultipleProcesses(const std::vector<std::string> &Args,
|
2016-12-14 01:45:53 +08:00
|
|
|
unsigned NumWorkers, unsigned NumJobs) {
|
|
|
|
std::atomic<unsigned> Counter(0);
|
2016-09-01 09:22:27 +08:00
|
|
|
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();
|
2016-12-14 01:45:53 +08:00
|
|
|
for (unsigned i = 0; i < NumWorkers; i++)
|
2015-02-20 02:45:37 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-25 08:56:08 +08:00
|
|
|
static std::string GetDedupTokenFromFile(const std::string &Path) {
|
|
|
|
auto S = FileToString(Path);
|
|
|
|
auto Beg = S.find("DEDUP_TOKEN:");
|
|
|
|
if (Beg == std::string::npos)
|
|
|
|
return "";
|
|
|
|
auto End = S.find('\n', Beg);
|
|
|
|
if (End == std::string::npos)
|
|
|
|
return "";
|
|
|
|
return S.substr(Beg, End - Beg);
|
|
|
|
}
|
|
|
|
|
2017-04-18 04:58:21 +08:00
|
|
|
int CleanseCrashInput(const std::vector<std::string> &Args,
|
|
|
|
const FuzzingOptions &Options) {
|
|
|
|
if (Inputs->size() != 1 || !Flags.exact_artifact_path) {
|
|
|
|
Printf("ERROR: -cleanse_crash should be given one input file and"
|
|
|
|
" -exact_artifact_path\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
std::string InputFilePath = Inputs->at(0);
|
|
|
|
std::string OutputFilePath = Flags.exact_artifact_path;
|
|
|
|
std::string BaseCmd =
|
|
|
|
CloneArgsWithoutX(Args, "cleanse_crash", "cleanse_crash");
|
|
|
|
|
|
|
|
auto InputPos = BaseCmd.find(" " + InputFilePath + " ");
|
|
|
|
assert(InputPos != std::string::npos);
|
|
|
|
BaseCmd.erase(InputPos, InputFilePath.size() + 1);
|
|
|
|
|
|
|
|
auto LogFilePath = DirPlusFile(
|
|
|
|
TmpDir(), "libFuzzerTemp." + std::to_string(GetPid()) + ".txt");
|
|
|
|
auto TmpFilePath = DirPlusFile(
|
|
|
|
TmpDir(), "libFuzzerTemp." + std::to_string(GetPid()) + ".repro");
|
|
|
|
auto LogFileRedirect = " > " + LogFilePath + " 2>&1 ";
|
|
|
|
|
|
|
|
auto Cmd = BaseCmd + " " + TmpFilePath + LogFileRedirect;
|
|
|
|
|
|
|
|
std::string CurrentFilePath = InputFilePath;
|
|
|
|
auto U = FileToVector(CurrentFilePath);
|
|
|
|
size_t Size = U.size();
|
|
|
|
|
|
|
|
const std::vector<uint8_t> ReplacementBytes = {' ', 0xff};
|
|
|
|
for (int NumAttempts = 0; NumAttempts < 5; NumAttempts++) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (size_t Idx = 0; Idx < Size; Idx++) {
|
|
|
|
Printf("CLEANSE[%d]: Trying to replace byte %zd of %zd\n", NumAttempts,
|
|
|
|
Idx, Size);
|
|
|
|
uint8_t OriginalByte = U[Idx];
|
|
|
|
if (ReplacementBytes.end() != std::find(ReplacementBytes.begin(),
|
|
|
|
ReplacementBytes.end(),
|
|
|
|
OriginalByte))
|
|
|
|
continue;
|
|
|
|
for (auto NewByte : ReplacementBytes) {
|
|
|
|
U[Idx] = NewByte;
|
|
|
|
WriteToFile(U, TmpFilePath);
|
|
|
|
auto ExitCode = ExecuteCommand(Cmd);
|
|
|
|
RemoveFile(TmpFilePath);
|
|
|
|
if (!ExitCode) {
|
|
|
|
U[Idx] = OriginalByte;
|
|
|
|
} else {
|
|
|
|
Changed = true;
|
|
|
|
Printf("CLEANSE: Replaced byte %zd with 0x%x\n", Idx, NewByte);
|
|
|
|
WriteToFile(U, OutputFilePath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!Changed) break;
|
|
|
|
}
|
|
|
|
RemoveFile(LogFilePath);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:38:12 +08:00
|
|
|
int MinimizeCrashInput(const std::vector<std::string> &Args,
|
|
|
|
const FuzzingOptions &Options) {
|
2016-09-01 09:22:27 +08:00
|
|
|
if (Inputs->size() != 1) {
|
|
|
|
Printf("ERROR: -minimize_crash should be given one input file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
std::string InputFilePath = Inputs->at(0);
|
2016-12-13 08:40:47 +08:00
|
|
|
std::string BaseCmd =
|
|
|
|
CloneArgsWithoutX(Args, "minimize_crash", "exact_artifact_path");
|
2016-09-01 09:22:27 +08:00
|
|
|
auto InputPos = BaseCmd.find(" " + InputFilePath + " ");
|
|
|
|
assert(InputPos != std::string::npos);
|
|
|
|
BaseCmd.erase(InputPos, InputFilePath.size() + 1);
|
2016-12-13 08:40:47 +08:00
|
|
|
if (Flags.runs <= 0 && Flags.max_total_time == 0) {
|
|
|
|
Printf("INFO: you need to specify -runs=N or "
|
|
|
|
"-max_total_time=N with -minimize_crash=1\n"
|
|
|
|
"INFO: defaulting to -max_total_time=600\n");
|
|
|
|
BaseCmd += " -max_total_time=600";
|
|
|
|
}
|
2017-03-25 08:56:08 +08:00
|
|
|
|
|
|
|
auto LogFilePath = DirPlusFile(
|
|
|
|
TmpDir(), "libFuzzerTemp." + std::to_string(GetPid()) + ".txt");
|
|
|
|
auto LogFileRedirect = " > " + LogFilePath + " 2>&1 ";
|
2016-09-01 09:22:27 +08:00
|
|
|
|
|
|
|
std::string CurrentFilePath = InputFilePath;
|
|
|
|
while (true) {
|
|
|
|
Unit U = FileToVector(CurrentFilePath);
|
|
|
|
Printf("CRASH_MIN: minimizing crash input: '%s' (%zd bytes)\n",
|
|
|
|
CurrentFilePath.c_str(), U.size());
|
|
|
|
|
2017-03-25 08:56:08 +08:00
|
|
|
auto Cmd = BaseCmd + " " + CurrentFilePath + LogFileRedirect;
|
2016-09-01 09:22:27 +08:00
|
|
|
|
|
|
|
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());
|
2017-03-25 08:56:08 +08:00
|
|
|
auto DedupToken1 = GetDedupTokenFromFile(LogFilePath);
|
|
|
|
if (!DedupToken1.empty())
|
|
|
|
Printf("CRASH_MIN: DedupToken1: %s\n", DedupToken1.c_str());
|
2016-09-01 09:22:27 +08:00
|
|
|
|
2017-01-20 03:38:12 +08:00
|
|
|
std::string ArtifactPath =
|
2017-03-25 05:09:16 +08:00
|
|
|
Flags.exact_artifact_path
|
|
|
|
? Flags.exact_artifact_path
|
|
|
|
: Options.ArtifactPrefix + "minimized-from-" + Hash(U);
|
2016-09-01 09:22:27 +08:00
|
|
|
Cmd += " -minimize_crash_internal_step=1 -exact_artifact_path=" +
|
|
|
|
ArtifactPath;
|
|
|
|
Printf("CRASH_MIN: executing: %s\n", Cmd.c_str());
|
|
|
|
ExitCode = ExecuteCommand(Cmd);
|
2017-03-25 08:56:08 +08:00
|
|
|
CopyFileToErr(LogFilePath);
|
2016-09-01 09:22:27 +08:00
|
|
|
if (ExitCode == 0) {
|
2016-12-13 08:40:47 +08:00
|
|
|
if (Flags.exact_artifact_path) {
|
|
|
|
CurrentFilePath = Flags.exact_artifact_path;
|
|
|
|
WriteToFile(U, CurrentFilePath);
|
|
|
|
}
|
2016-09-01 09:22:27 +08:00
|
|
|
Printf("CRASH_MIN: failed to minimize beyond %s (%d bytes), exiting\n",
|
|
|
|
CurrentFilePath.c_str(), U.size());
|
2017-03-25 08:56:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto DedupToken2 = GetDedupTokenFromFile(LogFilePath);
|
|
|
|
if (!DedupToken2.empty())
|
|
|
|
Printf("CRASH_MIN: DedupToken2: %s\n", DedupToken2.c_str());
|
|
|
|
|
|
|
|
if (DedupToken1 != DedupToken2) {
|
|
|
|
if (Flags.exact_artifact_path) {
|
|
|
|
CurrentFilePath = Flags.exact_artifact_path;
|
|
|
|
WriteToFile(U, CurrentFilePath);
|
|
|
|
}
|
|
|
|
Printf("CRASH_MIN: mismatch in dedup tokens"
|
|
|
|
" (looks like a different bug). Won't minimize further\n");
|
|
|
|
break;
|
2016-09-01 09:22:27 +08:00
|
|
|
}
|
2017-03-25 08:56:08 +08:00
|
|
|
|
2016-09-01 09:22:27 +08:00
|
|
|
CurrentFilePath = ArtifactPath;
|
2017-03-25 08:56:08 +08:00
|
|
|
Printf("*********************************\n");
|
2016-09-01 09:22:27 +08:00
|
|
|
}
|
2017-03-25 08:56:08 +08:00
|
|
|
RemoveFile(LogFilePath);
|
2016-09-01 09:22:27 +08:00
|
|
|
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);
|
|
|
|
Printf("INFO: Starting MinimizeCrashInputInternalStep: %zd\n", U.size());
|
2017-01-20 03:38:12 +08:00
|
|
|
if (U.size() < 2) {
|
|
|
|
Printf("INFO: The input is small enough, exiting\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-17 09:40:09 +08:00
|
|
|
int AnalyzeDictionary(Fuzzer *F, const std::vector<Unit>& Dict,
|
|
|
|
UnitVector& Corpus) {
|
|
|
|
Printf("Started dictionary minimization (up to %d tests)\n",
|
|
|
|
Dict.size() * Corpus.size() * 2);
|
|
|
|
|
|
|
|
// Scores and usage count for each dictionary unit.
|
|
|
|
std::vector<int> Scores(Dict.size());
|
|
|
|
std::vector<int> Usages(Dict.size());
|
|
|
|
|
|
|
|
std::vector<size_t> InitialFeatures;
|
|
|
|
std::vector<size_t> ModifiedFeatures;
|
|
|
|
for (auto &C : Corpus) {
|
|
|
|
// Get coverage for the testcase without modifications.
|
|
|
|
F->ExecuteCallback(C.data(), C.size());
|
|
|
|
InitialFeatures.clear();
|
|
|
|
TPC.CollectFeatures([&](size_t Feature) -> bool {
|
|
|
|
InitialFeatures.push_back(Feature);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (size_t i = 0; i < Dict.size(); ++i) {
|
|
|
|
auto Data = C;
|
|
|
|
auto StartPos = std::search(Data.begin(), Data.end(),
|
|
|
|
Dict[i].begin(), Dict[i].end());
|
|
|
|
// Skip dictionary unit, if the testcase does not contain it.
|
|
|
|
if (StartPos == Data.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
++Usages[i];
|
|
|
|
while (StartPos != Data.end()) {
|
|
|
|
// Replace all occurrences of dictionary unit in the testcase.
|
|
|
|
auto EndPos = StartPos + Dict[i].size();
|
|
|
|
for (auto It = StartPos; It != EndPos; ++It)
|
|
|
|
*It ^= 0xFF;
|
|
|
|
|
|
|
|
StartPos = std::search(EndPos, Data.end(),
|
|
|
|
Dict[i].begin(), Dict[i].end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get coverage for testcase with masked occurrences of dictionary unit.
|
|
|
|
F->ExecuteCallback(Data.data(), Data.size());
|
|
|
|
ModifiedFeatures.clear();
|
|
|
|
TPC.CollectFeatures([&](size_t Feature) -> bool {
|
|
|
|
ModifiedFeatures.push_back(Feature);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (InitialFeatures == ModifiedFeatures)
|
|
|
|
--Scores[i];
|
|
|
|
else
|
|
|
|
Scores[i] += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Printf("###### Useless dictionary elements. ######\n");
|
|
|
|
for (size_t i = 0; i < Dict.size(); ++i) {
|
|
|
|
// Dictionary units with positive score are treated as useful ones.
|
|
|
|
if (Scores[i] > 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Printf("\"");
|
|
|
|
PrintASCII(Dict[i].data(), Dict[i].size(), "\"");
|
|
|
|
Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i]);
|
|
|
|
}
|
|
|
|
Printf("###### End of useless dictionary elements. ######\n");
|
|
|
|
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");
|
2017-01-21 05:34:24 +08:00
|
|
|
std::string Argv0((*argv)[0]);
|
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]);
|
2017-01-21 08:13:50 +08:00
|
|
|
if (Argv0 != *ProgName) {
|
|
|
|
Printf("ERROR: argv[0] has been modified in LLVMFuzzerInitialize\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-09-11 00:57:57 +08:00
|
|
|
ParseFlags(Args);
|
2015-02-20 02:45:37 +08:00
|
|
|
if (Flags.help) {
|
|
|
|
PrintHelp();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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)
|
2016-12-14 01:45:53 +08:00
|
|
|
Printf("Running %u 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;
|
2017-06-16 06:43:40 +08:00
|
|
|
const size_t kMinDefaultLen = 4096;
|
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;
|
2016-12-28 07:24:55 +08:00
|
|
|
Options.ExperimentalLenControl = Flags.experimental_len_control;
|
2017-06-16 06:43:40 +08:00
|
|
|
if (Flags.experimental_len_control && Flags.max_len == kMinDefaultLen)
|
2016-12-28 07:24:55 +08:00
|
|
|
Options.MaxLen = 1 << 20;
|
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-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;
|
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-12-20 06:18:08 +08:00
|
|
|
Options.DumpCoverage = Flags.dump_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)
|
2016-12-28 03:51:34 +08:00
|
|
|
Seed =
|
|
|
|
std::chrono::system_clock::now().time_since_epoch().count() + GetPid();
|
2016-02-13 11:00:53 +08:00
|
|
|
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
|
|
|
|
2016-12-14 01:45:20 +08:00
|
|
|
Options.HandleAbrt = Flags.handle_abrt;
|
|
|
|
Options.HandleBus = Flags.handle_bus;
|
|
|
|
Options.HandleFpe = Flags.handle_fpe;
|
|
|
|
Options.HandleIll = Flags.handle_ill;
|
|
|
|
Options.HandleInt = Flags.handle_int;
|
|
|
|
Options.HandleSegv = Flags.handle_segv;
|
|
|
|
Options.HandleTerm = Flags.handle_term;
|
2017-01-06 06:05:47 +08:00
|
|
|
Options.HandleXfsz = Flags.handle_xfsz;
|
2016-12-14 01:45:20 +08:00
|
|
|
SetSignalHandler(Options);
|
2015-10-17 06:41:47 +08:00
|
|
|
|
2017-01-20 03:38:12 +08:00
|
|
|
if (Flags.minimize_crash)
|
|
|
|
return MinimizeCrashInput(Args, Options);
|
|
|
|
|
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
|
|
|
|
2017-04-18 04:58:21 +08:00
|
|
|
if (Flags.cleanse_crash)
|
|
|
|
return CleanseCrashInput(Args, Options);
|
|
|
|
|
2017-01-21 04:57:07 +08:00
|
|
|
if (auto Name = Flags.run_equivalence_server) {
|
|
|
|
SMR.Destroy(Name);
|
2017-02-01 08:07:47 +08:00
|
|
|
if (!SMR.Create(Name)) {
|
|
|
|
Printf("ERROR: can't create shared memory region\n");
|
2017-01-21 04:57:07 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Printf("INFO: EQUIVALENCE SERVER UP\n");
|
|
|
|
while (true) {
|
|
|
|
SMR.WaitClient();
|
|
|
|
size_t Size = SMR.ReadByteArraySize();
|
|
|
|
SMR.WriteByteArray(nullptr, 0);
|
2017-05-09 09:17:29 +08:00
|
|
|
const Unit tmp(SMR.GetByteArray(), SMR.GetByteArray() + Size);
|
|
|
|
F->RunOne(tmp.data(), tmp.size());
|
2017-01-21 04:57:07 +08:00
|
|
|
SMR.PostServer();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto Name = Flags.use_equivalence_server) {
|
|
|
|
if (!SMR.Open(Name)) {
|
|
|
|
Printf("ERROR: can't open shared memory region\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Printf("INFO: EQUIVALENCE CLIENT UP\n");
|
|
|
|
}
|
|
|
|
|
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);
|
2017-03-15 05:47:52 +08:00
|
|
|
if (Flags.merge_control_file)
|
|
|
|
F->CrashResistantMergeInternalStep(Flags.merge_control_file);
|
|
|
|
else
|
2017-03-23 04:32:44 +08:00
|
|
|
F->CrashResistantMerge(Args, *Inputs,
|
|
|
|
Flags.load_coverage_summary,
|
|
|
|
Flags.save_coverage_summary);
|
2016-12-09 09:17:24 +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
|
|
|
|
2017-03-17 09:40:09 +08:00
|
|
|
if (Flags.analyze_dict) {
|
|
|
|
if (Dictionary.empty() || Inputs->empty()) {
|
|
|
|
Printf("ERROR: can't analyze dict without dict and corpus provided\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (AnalyzeDictionary(F, Dictionary, InitialCorpus)) {
|
|
|
|
Printf("Dictionary analysis failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Printf("Dictionary analysis suceeded\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
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)
|
2017-02-15 06:14:36 +08:00
|
|
|
Printf("Done %zd runs in %zd second(s)\n", F->getTotalNumberOfRuns(),
|
2016-11-12 08:24:35 +08:00
|
|
|
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
|