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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-22 00:42:17 +02:00
|
|
|
#include "FuzzerCorpus.h"
|
2015-02-19 19:45:37 +01:00
|
|
|
#include "FuzzerInterface.h"
|
|
|
|
#include "FuzzerInternal.h"
|
2016-11-30 20:06:14 +01:00
|
|
|
#include "FuzzerIO.h"
|
2016-09-21 03:50:50 +02:00
|
|
|
#include "FuzzerMutate.h"
|
|
|
|
#include "FuzzerRandom.h"
|
2017-01-20 21:57:07 +01:00
|
|
|
#include "FuzzerShmem.h"
|
2016-12-15 07:21:21 +01:00
|
|
|
#include "FuzzerTracePC.h"
|
2016-04-18 11:17:29 +02:00
|
|
|
#include <algorithm>
|
2015-02-19 19:45:37 +01:00
|
|
|
#include <atomic>
|
2016-04-18 11:17:29 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstring>
|
2015-02-19 19:45:37 +01:00
|
|
|
#include <mutex>
|
2015-03-31 22:13:20 +02:00
|
|
|
#include <string>
|
2016-04-18 11:17:29 +02:00
|
|
|
#include <thread>
|
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];
|
2016-09-10 02:35:30 +02:00
|
|
|
if (strstr(D.Description, "internal flag") == D.Description) continue;
|
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
|
|
|
}
|
2016-04-15 23:56:29 +02: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-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
|
|
|
}
|
|
|
|
}
|
2016-06-01 18:41:12 +02:00
|
|
|
Printf("\n\nWARNING: unrecognized flag '%s'; "
|
|
|
|
"use -help=1 to list all flags\n\n", Param);
|
|
|
|
return true;
|
2015-02-19 19:45:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2016-05-07 01:38:07 +02:00
|
|
|
SleepSeconds(600);
|
2015-05-11 23:31:51 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:45:53 +01:00
|
|
|
static void WorkerThread(const std::string &Cmd, std::atomic<unsigned> *Counter,
|
|
|
|
unsigned NumJobs, std::atomic<bool> *HasErrors) {
|
2015-02-19 19:45:37 +01:00
|
|
|
while (true) {
|
2016-12-13 18:45:53 +01:00
|
|
|
unsigned C = (*Counter)++;
|
2015-02-19 19:45:37 +01: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 03:07:46 +02:00
|
|
|
Printf("%s", ToRun.c_str());
|
2016-04-11 22:35:17 +02:00
|
|
|
int ExitCode = ExecuteCommand(ToRun);
|
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);
|
2016-12-13 18:45:53 +01:00
|
|
|
Printf("================== Job %u exited with exit code %d ============\n",
|
2015-05-23 03:07:46 +02:00
|
|
|
C, ExitCode);
|
2015-02-19 19:45:37 +01:00
|
|
|
fuzzer::CopyFileToErr(Log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 02:17:24 +01:00
|
|
|
std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
|
|
|
|
const char *X1, const char *X2) {
|
2015-02-19 19:45:37 +01:00
|
|
|
std::string Cmd;
|
2015-09-10 18:57:57 +02:00
|
|
|
for (auto &S : Args) {
|
2016-09-01 03:22:27 +02:00
|
|
|
if (FlagValue(S.c_str(), X1) || FlagValue(S.c_str(), X2))
|
2015-09-10 18:57:57 +02:00
|
|
|
continue;
|
|
|
|
Cmd += S + " ";
|
2015-02-19 19:45:37 +01:00
|
|
|
}
|
2016-09-01 03:22:27 +02:00
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int RunInMultipleProcesses(const std::vector<std::string> &Args,
|
2016-12-13 18:45:53 +01:00
|
|
|
unsigned NumWorkers, unsigned NumJobs) {
|
|
|
|
std::atomic<unsigned> Counter(0);
|
2016-09-01 03:22:27 +02:00
|
|
|
std::atomic<bool> HasErrors(false);
|
|
|
|
std::string Cmd = CloneArgsWithoutX(Args, "jobs", "workers");
|
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();
|
2016-12-13 18:45:53 +01:00
|
|
|
for (unsigned i = 0; i < NumWorkers; i++)
|
2015-02-19 19:45:37 +01:00
|
|
|
V.push_back(std::thread(WorkerThread, Cmd, &Counter, NumJobs, &HasErrors));
|
|
|
|
for (auto &T : V)
|
|
|
|
T.join();
|
|
|
|
return HasErrors ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2016-05-06 23:58:35 +02:00
|
|
|
static void RssThread(Fuzzer *F, size_t RssLimitMb) {
|
|
|
|
while (true) {
|
2016-05-07 01:38:07 +02:00
|
|
|
SleepSeconds(1);
|
2016-05-06 23:58:35 +02:00
|
|
|
size_t Peak = GetPeakRSSMb();
|
|
|
|
if (Peak > RssLimitMb)
|
2016-05-07 01:38:07 +02:00
|
|
|
F->RssLimitCallback();
|
2016-05-06 23:58:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void StartRssThread(Fuzzer *F, size_t RssLimitMb) {
|
|
|
|
if (!RssLimitMb) return;
|
|
|
|
std::thread T(RssThread, F, RssLimitMb);
|
|
|
|
T.detach();
|
|
|
|
}
|
|
|
|
|
2016-08-30 16:52:05 +02:00
|
|
|
int RunOneTest(Fuzzer *F, const char *InputFilePath, size_t MaxLen) {
|
2015-10-02 01:23:06 +02:00
|
|
|
Unit U = FileToVector(InputFilePath);
|
2016-08-30 16:52:05 +02:00
|
|
|
if (MaxLen && MaxLen < U.size())
|
|
|
|
U.resize(MaxLen);
|
|
|
|
F->RunOne(U.data(), U.size());
|
2016-10-18 20:38:08 +02:00
|
|
|
F->TryDetectingAMemoryLeak(U.data(), U.size(), true);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-25 01:56:08 +01: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-17 22:58:21 +02: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-19 20:38:12 +01:00
|
|
|
int MinimizeCrashInput(const std::vector<std::string> &Args,
|
|
|
|
const FuzzingOptions &Options) {
|
2016-09-01 03:22:27 +02: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 01:40:47 +01:00
|
|
|
std::string BaseCmd =
|
|
|
|
CloneArgsWithoutX(Args, "minimize_crash", "exact_artifact_path");
|
2016-09-01 03:22:27 +02:00
|
|
|
auto InputPos = BaseCmd.find(" " + InputFilePath + " ");
|
|
|
|
assert(InputPos != std::string::npos);
|
|
|
|
BaseCmd.erase(InputPos, InputFilePath.size() + 1);
|
2016-12-13 01:40:47 +01: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 01:56:08 +01:00
|
|
|
|
|
|
|
auto LogFilePath = DirPlusFile(
|
|
|
|
TmpDir(), "libFuzzerTemp." + std::to_string(GetPid()) + ".txt");
|
|
|
|
auto LogFileRedirect = " > " + LogFilePath + " 2>&1 ";
|
2016-09-01 03:22:27 +02: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 01:56:08 +01:00
|
|
|
auto Cmd = BaseCmd + " " + CurrentFilePath + LogFileRedirect;
|
2016-09-01 03:22:27 +02: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 01:56:08 +01:00
|
|
|
auto DedupToken1 = GetDedupTokenFromFile(LogFilePath);
|
|
|
|
if (!DedupToken1.empty())
|
|
|
|
Printf("CRASH_MIN: DedupToken1: %s\n", DedupToken1.c_str());
|
2016-09-01 03:22:27 +02:00
|
|
|
|
2017-01-19 20:38:12 +01:00
|
|
|
std::string ArtifactPath =
|
2017-03-24 22:09:16 +01:00
|
|
|
Flags.exact_artifact_path
|
|
|
|
? Flags.exact_artifact_path
|
|
|
|
: Options.ArtifactPrefix + "minimized-from-" + Hash(U);
|
2016-09-01 03:22:27 +02: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 01:56:08 +01:00
|
|
|
CopyFileToErr(LogFilePath);
|
2016-09-01 03:22:27 +02:00
|
|
|
if (ExitCode == 0) {
|
2016-12-13 01:40:47 +01:00
|
|
|
if (Flags.exact_artifact_path) {
|
|
|
|
CurrentFilePath = Flags.exact_artifact_path;
|
|
|
|
WriteToFile(U, CurrentFilePath);
|
|
|
|
}
|
2016-09-01 03:22:27 +02:00
|
|
|
Printf("CRASH_MIN: failed to minimize beyond %s (%d bytes), exiting\n",
|
|
|
|
CurrentFilePath.c_str(), U.size());
|
2017-03-25 01:56:08 +01: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 03:22:27 +02:00
|
|
|
}
|
2017-03-25 01:56:08 +01:00
|
|
|
|
2016-09-01 03:22:27 +02:00
|
|
|
CurrentFilePath = ArtifactPath;
|
2017-03-25 01:56:08 +01:00
|
|
|
Printf("*********************************\n");
|
2016-09-01 03:22:27 +02:00
|
|
|
}
|
2017-03-25 01:56:08 +01:00
|
|
|
RemoveFile(LogFilePath);
|
2016-09-01 03:22:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-22 00:42:17 +02:00
|
|
|
int MinimizeCrashInputInternalStep(Fuzzer *F, InputCorpus *Corpus) {
|
2016-09-01 03:22:27 +02: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-19 20:38:12 +01:00
|
|
|
if (U.size() < 2) {
|
|
|
|
Printf("INFO: The input is small enough, exiting\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-10-06 00:56:21 +02:00
|
|
|
Corpus->AddToCorpus(U, 0);
|
2016-09-23 01:16:36 +02:00
|
|
|
F->SetMaxInputLen(U.size());
|
|
|
|
F->SetMaxMutationLen(U.size() - 1);
|
2016-10-15 03:00:24 +02:00
|
|
|
F->MinimizeCrashLoop(U);
|
2016-09-01 03:22:27 +02:00
|
|
|
Printf("INFO: Done MinimizeCrashInputInternalStep, no crashes found\n");
|
|
|
|
exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-17 02:40:09 +01: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 07:48:02 +02:00
|
|
|
int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
|
2015-09-10 18:57:57 +02:00
|
|
|
using namespace fuzzer;
|
2016-06-02 07:48:02 +02:00
|
|
|
assert(argc && argv && "Argument pointers cannot be nullptr");
|
2017-01-20 22:34:24 +01:00
|
|
|
std::string Argv0((*argv)[0]);
|
2016-06-08 01:32:50 +02:00
|
|
|
EF = new ExternalFunctions();
|
|
|
|
if (EF->LLVMFuzzerInitialize)
|
|
|
|
EF->LLVMFuzzerInitialize(argc, argv);
|
2016-06-02 07:48:02 +02:00
|
|
|
const std::vector<std::string> Args(*argv, *argv + *argc);
|
2015-09-10 18:57:57 +02:00
|
|
|
assert(!Args.empty());
|
|
|
|
ProgName = new std::string(Args[0]);
|
2017-01-21 01:13:50 +01:00
|
|
|
if (Argv0 != *ProgName) {
|
|
|
|
Printf("ERROR: argv[0] has been modified in LLVMFuzzerInitialize\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-09-10 18:57:57 +02:00
|
|
|
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)
|
2016-12-13 18:45:53 +01:00
|
|
|
Printf("Running %u 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;
|
2016-06-23 22:44:48 +02:00
|
|
|
FuzzingOptions Options;
|
2015-02-19 19:45:37 +01:00
|
|
|
Options.Verbosity = Flags.verbosity;
|
|
|
|
Options.MaxLen = Flags.max_len;
|
2016-12-28 00:24:55 +01:00
|
|
|
Options.ExperimentalLenControl = Flags.experimental_len_control;
|
|
|
|
if (Flags.experimental_len_control && Flags.max_len == 64)
|
|
|
|
Options.MaxLen = 1 << 20;
|
2015-05-20 00:12:57 +02:00
|
|
|
Options.UnitTimeoutSec = Flags.timeout;
|
2016-11-03 20:31:18 +01:00
|
|
|
Options.ErrorExitCode = Flags.error_exitcode;
|
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;
|
2016-07-16 01:27:19 +02:00
|
|
|
Options.UseMemmem = Flags.use_memmem;
|
2016-10-14 22:20:33 +02:00
|
|
|
Options.UseCmp = Flags.use_cmp;
|
2016-09-23 02:46:18 +02:00
|
|
|
Options.UseValueProfile = Flags.use_value_profile;
|
2016-10-01 03:04:29 +02:00
|
|
|
Options.Shrink = Flags.shrink;
|
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;
|
2016-10-09 00:12:14 +02:00
|
|
|
Options.ReloadIntervalSec = Flags.reload;
|
2015-08-11 03:44:42 +02:00
|
|
|
Options.OnlyASCII = Flags.only_ascii;
|
2016-04-20 02:24:21 +02:00
|
|
|
Options.DetectLeaks = Flags.detect_leaks;
|
2016-10-13 21:06:46 +02:00
|
|
|
Options.TraceMalloc = Flags.trace_malloc;
|
2016-05-07 01:38:07 +02:00
|
|
|
Options.RssLimitMb = Flags.rss_limit_mb;
|
2015-02-19 19:45:37 +01:00
|
|
|
if (Flags.runs >= 0)
|
|
|
|
Options.MaxNumberOfRuns = Flags.runs;
|
2016-09-01 03:22:27 +02:00
|
|
|
if (!Inputs->empty() && !Flags.minimize_crash_internal_step)
|
2015-09-10 18:57:57 +02:00
|
|
|
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();
|
2016-09-01 03:22:27 +02:00
|
|
|
Options.SaveArtifacts =
|
|
|
|
!DoPlainRun || Flags.minimize_crash_internal_step;
|
2016-08-26 00:35:08 +02:00
|
|
|
Options.PrintNewCovPcs = Flags.print_pcs;
|
2016-02-26 23:42:23 +01:00
|
|
|
Options.PrintFinalStats = Flags.print_final_stats;
|
2016-09-22 00:42:17 +02:00
|
|
|
Options.PrintCorpusStats = Flags.print_corpus_stats;
|
2016-09-18 23:47:08 +02:00
|
|
|
Options.PrintCoverage = Flags.print_coverage;
|
2016-12-19 23:18:08 +01:00
|
|
|
Options.DumpCoverage = Flags.dump_coverage;
|
2016-09-27 02:10:20 +02:00
|
|
|
if (Flags.exit_on_src_pos)
|
|
|
|
Options.ExitOnSrcPos = Flags.exit_on_src_pos;
|
2016-10-01 03:04:29 +02:00
|
|
|
if (Flags.exit_on_item)
|
|
|
|
Options.ExitOnItem = Flags.exit_on_item;
|
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)
|
2016-12-27 20:51:34 +01:00
|
|
|
Seed =
|
|
|
|
std::chrono::system_clock::now().time_since_epoch().count() + GetPid();
|
2016-02-13 04:00:53 +01:00
|
|
|
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-11-14 20:21:38 +01:00
|
|
|
auto *MD = new MutationDispatcher(Rand, Options);
|
|
|
|
auto *Corpus = new InputCorpus(Options.OutputCorpus);
|
|
|
|
auto *F = new Fuzzer(Callback, *Corpus, *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-11-12 01:24:35 +01:00
|
|
|
MD->AddWordToManualDictionary(Word(U.data(), U.size()));
|
2015-11-12 02:02:01 +01:00
|
|
|
|
2016-11-12 01:24:35 +01:00
|
|
|
StartRssThread(F, Flags.rss_limit_mb);
|
2016-05-06 23:58:35 +02:00
|
|
|
|
2016-12-13 18:45:20 +01: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-05 23:05:47 +01:00
|
|
|
Options.HandleXfsz = Flags.handle_xfsz;
|
2016-12-13 18:45:20 +01:00
|
|
|
SetSignalHandler(Options);
|
2015-10-17 00:41:47 +02:00
|
|
|
|
2017-01-19 20:38:12 +01:00
|
|
|
if (Flags.minimize_crash)
|
|
|
|
return MinimizeCrashInput(Args, Options);
|
|
|
|
|
2016-09-01 03:22:27 +02:00
|
|
|
if (Flags.minimize_crash_internal_step)
|
2016-11-12 01:24:35 +01:00
|
|
|
return MinimizeCrashInputInternalStep(F, Corpus);
|
2016-09-01 03:22:27 +02:00
|
|
|
|
2017-04-17 22:58:21 +02:00
|
|
|
if (Flags.cleanse_crash)
|
|
|
|
return CleanseCrashInput(Args, Options);
|
|
|
|
|
2017-01-20 21:57:07 +01:00
|
|
|
if (auto Name = Flags.run_equivalence_server) {
|
|
|
|
SMR.Destroy(Name);
|
2017-02-01 01:07:47 +01:00
|
|
|
if (!SMR.Create(Name)) {
|
|
|
|
Printf("ERROR: can't create shared memory region\n");
|
2017-01-20 21:57:07 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Printf("INFO: EQUIVALENCE SERVER UP\n");
|
|
|
|
while (true) {
|
|
|
|
SMR.WaitClient();
|
|
|
|
size_t Size = SMR.ReadByteArraySize();
|
|
|
|
SMR.WriteByteArray(nullptr, 0);
|
|
|
|
F->RunOne(SMR.GetByteArray(), Size);
|
|
|
|
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-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-06-17 15:07:06 +02:00
|
|
|
Printf("Running: %s\n", Path.c_str());
|
2016-03-04 23:35:40 +01:00
|
|
|
for (int Iter = 0; Iter < Runs; Iter++)
|
2016-11-12 01:24:35 +01:00
|
|
|
RunOneTest(F, Path.c_str(), Options.MaxLen);
|
2016-02-02 04:03:47 +01:00
|
|
|
auto StopTime = system_clock::now();
|
|
|
|
auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
|
2016-06-17 15:07:06 +02:00
|
|
|
Printf("Executed %s in %zd ms\n", Path.c_str(), (long)MS);
|
2016-02-02 04:03:47 +01:00
|
|
|
}
|
2016-08-15 21:44:04 +02: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 01:24:35 +01:00
|
|
|
F->PrintFinalStats();
|
2016-02-02 04:03:47 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-10-24 03:16:40 +02:00
|
|
|
if (Flags.merge) {
|
2016-03-12 02:57:04 +01:00
|
|
|
if (Options.MaxLen == 0)
|
2016-11-12 01:24:35 +01:00
|
|
|
F->SetMaxInputLen(kMaxSaneLen);
|
2017-03-14 22:47:52 +01:00
|
|
|
if (Flags.merge_control_file)
|
|
|
|
F->CrashResistantMergeInternalStep(Flags.merge_control_file);
|
|
|
|
else
|
2017-03-22 21:32:44 +01:00
|
|
|
F->CrashResistantMerge(Args, *Inputs,
|
|
|
|
Flags.load_coverage_summary,
|
|
|
|
Flags.save_coverage_summary);
|
2016-12-09 02:17:24 +01:00
|
|
|
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-09-21 03:04:43 +02:00
|
|
|
UnitVector InitialCorpus;
|
|
|
|
for (auto &Inp : *Inputs) {
|
|
|
|
Printf("Loading corpus dir: %s\n", Inp.c_str());
|
2016-10-09 01:24:45 +02:00
|
|
|
ReadDirToVectorOfUnits(Inp.c_str(), &InitialCorpus, nullptr,
|
|
|
|
TemporaryMaxLen, /*ExitOnError=*/false);
|
2016-09-21 03:04:43 +02:00
|
|
|
}
|
2016-03-12 02:57:04 +01:00
|
|
|
|
2017-03-17 02:40:09 +01: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 03:04:43 +02:00
|
|
|
if (Options.MaxLen == 0) {
|
|
|
|
size_t MaxLen = 0;
|
|
|
|
for (auto &U : InitialCorpus)
|
|
|
|
MaxLen = std::max(U.size(), MaxLen);
|
2016-11-12 01:24:35 +01:00
|
|
|
F->SetMaxInputLen(std::min(std::max(kMinDefaultLen, MaxLen), kMaxSaneLen));
|
2016-09-21 03:04:43 +02:00
|
|
|
}
|
2015-02-19 19:45:37 +01:00
|
|
|
|
2016-09-21 03:04:43 +02:00
|
|
|
if (InitialCorpus.empty()) {
|
2016-11-12 03:27:21 +01:00
|
|
|
InitialCorpus.push_back(Unit({'\n'})); // Valid ASCII input.
|
2016-04-18 23:14:11 +02:00
|
|
|
if (Options.Verbosity)
|
|
|
|
Printf("INFO: A corpus is not provided, starting from an empty corpus\n");
|
|
|
|
}
|
2016-11-12 01:24:35 +01:00
|
|
|
F->ShuffleAndMinimize(&InitialCorpus);
|
2016-10-06 07:14:00 +02:00
|
|
|
InitialCorpus.clear(); // Don't need this memory any more.
|
2016-11-12 01:24:35 +01:00
|
|
|
F->Loop();
|
2015-11-12 02:02:01 +01:00
|
|
|
|
2015-02-19 19:45:37 +01:00
|
|
|
if (Flags.verbosity)
|
2017-02-14 23:14:36 +01:00
|
|
|
Printf("Done %zd runs in %zd second(s)\n", F->getTotalNumberOfRuns(),
|
2016-11-12 01:24:35 +01:00
|
|
|
F->secondsSinceProcessStartUp());
|
|
|
|
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-06-08 01:32:50 +02:00
|
|
|
|
|
|
|
// Storage for global ExternalFunctions object.
|
|
|
|
ExternalFunctions *EF = nullptr;
|
|
|
|
|
2015-02-19 19:45:37 +01:00
|
|
|
} // namespace fuzzer
|