From 0001c18d8c75b1265fc5adb105b77fad7581e172 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Thu, 10 Sep 2015 16:57:57 +0000 Subject: [PATCH] [libFuzzer] add two more variants of FuzzerDriver for convenience llvm-svn: 247300 --- lib/Fuzzer/FuzzerDriver.cpp | 53 ++++++++++++++++++++++-------------- lib/Fuzzer/FuzzerInterface.h | 6 ++++ 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/lib/Fuzzer/FuzzerDriver.cpp b/lib/Fuzzer/FuzzerDriver.cpp index 55d1632a9a7..e79c21e8bdb 100644 --- a/lib/Fuzzer/FuzzerDriver.cpp +++ b/lib/Fuzzer/FuzzerDriver.cpp @@ -55,12 +55,12 @@ static FlagDescription FlagDescriptions [] { static const size_t kNumFlags = sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]); -static std::vector inputs; -static const char *ProgName; +static std::vector *Inputs; +static std::string *ProgName; static void PrintHelp() { Printf("Usage: %s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", - ProgName); + ProgName->c_str()); Printf("\nFlags: (strictly in form -flag=value)\n"); size_t MaxFlagLen = 0; for (size_t F = 0; F < kNumFlags; F++) @@ -119,16 +119,17 @@ static bool ParseOneFlag(const char *Param) { } // We don't use any library to minimize dependencies. -static void ParseFlags(int argc, char **argv) { +static void ParseFlags(const std::vector &Args) { for (size_t F = 0; F < kNumFlags; F++) { if (FlagDescriptions[F].IntFlag) *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default; if (FlagDescriptions[F].StrFlag) *FlagDescriptions[F].StrFlag = nullptr; } - for (int A = 1; A < argc; A++) { - if (ParseOneFlag(argv[A])) continue; - inputs.push_back(argv[A]); + Inputs = new std::vector; + for (size_t A = 1; A < Args.size(); A++) { + if (ParseOneFlag(Args[A].c_str())) continue; + Inputs->push_back(Args[A]); } } @@ -161,15 +162,15 @@ static void WorkerThread(const std::string &Cmd, std::atomic *Counter, } } -static int RunInMultipleProcesses(int argc, char **argv, int NumWorkers, - int NumJobs) { +static int RunInMultipleProcesses(const std::vector &Args, + int NumWorkers, int NumJobs) { std::atomic Counter(0); std::atomic HasErrors(false); std::string Cmd; - for (int i = 0; i < argc; i++) { - if (FlagValue(argv[i], "jobs") || FlagValue(argv[i], "workers")) continue; - Cmd += argv[i]; - Cmd += " "; + for (auto &S : Args) { + if (FlagValue(S.c_str(), "jobs") || FlagValue(S.c_str(), "workers")) + continue; + Cmd += S + " "; } std::vector V; std::thread Pulse(PulseThread); @@ -208,10 +209,22 @@ int FuzzerDriver(int argc, char **argv, UserCallback Callback) { } int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF) { - using namespace fuzzer; + std::vector Args(argv, argv + argc); + return FuzzerDriver(Args, USF); +} - ProgName = argv[0]; - ParseFlags(argc, argv); +int FuzzerDriver(const std::vector &Args, UserCallback Callback) { + FuzzerRandomLibc Rand(0); + SimpleUserSuppliedFuzzer SUSF(&Rand, Callback); + return FuzzerDriver(Args, SUSF); +} + +int FuzzerDriver(const std::vector &Args, + UserSuppliedFuzzer &USF) { + using namespace fuzzer; + assert(!Args.empty()); + ProgName = new std::string(Args[0]); + ParseFlags(Args); if (Flags.help) { PrintHelp(); return 0; @@ -224,7 +237,7 @@ int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF) { } if (Flags.workers > 0 && Flags.jobs > 0) - return RunInMultipleProcesses(argc, argv, Flags.workers, Flags.jobs); + return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs); Fuzzer::FuzzingOptions Options; Options.Verbosity = Flags.verbosity; @@ -245,8 +258,8 @@ int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF) { Options.TBMWidth = Flags.tbm_width; if (Flags.runs >= 0) Options.MaxNumberOfRuns = Flags.runs; - if (!inputs.empty()) - Options.OutputCorpus = inputs[0]; + if (!Inputs->empty()) + Options.OutputCorpus = (*Inputs)[0]; if (Flags.sync_command) Options.SyncCommand = Flags.sync_command; Options.SyncTimeout = Flags.sync_timeout; @@ -282,7 +295,7 @@ int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF) { } F.RereadOutputCorpus(); - for (auto &inp : inputs) + for (auto &inp : *Inputs) if (inp != Options.OutputCorpus) F.ReadDir(inp, nullptr); diff --git a/lib/Fuzzer/FuzzerInterface.h b/lib/Fuzzer/FuzzerInterface.h index d5af308be76..7f7d608800d 100644 --- a/lib/Fuzzer/FuzzerInterface.h +++ b/lib/Fuzzer/FuzzerInterface.h @@ -18,6 +18,8 @@ #include #include +#include +#include namespace fuzzer { @@ -162,6 +164,10 @@ class UserSuppliedFuzzer { /// Runs the fuzzing with the UserSuppliedFuzzer. int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF); +/// More C++-ish interface. +int FuzzerDriver(const std::vector &Args, UserSuppliedFuzzer &USF); +int FuzzerDriver(const std::vector &Args, UserCallback Callback); + } // namespace fuzzer #endif // LLVM_FUZZER_INTERFACE_H