[llvm] r247300 - [libFuzzer] add two more variants of FuzzerDriver for convenience
Kostya Serebryany via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 09:57:57 PDT 2015
Author: kcc
Date: Thu Sep 10 11:57:57 2015
New Revision: 247300
URL: http://llvm.org/viewvc/llvm-project?rev=247300&view=rev
Log:
[libFuzzer] add two more variants of FuzzerDriver for convenience
Modified:
llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp
llvm/trunk/lib/Fuzzer/FuzzerInterface.h
Modified: llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp?rev=247300&r1=247299&r2=247300&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp Thu Sep 10 11:57:57 2015
@@ -55,12 +55,12 @@ static FlagDescription FlagDescriptions
static const size_t kNumFlags =
sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
-static std::vector<std::string> inputs;
-static const char *ProgName;
+static std::vector<std::string> *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 *Par
}
// We don't use any library to minimize dependencies.
-static void ParseFlags(int argc, char **argv) {
+static void ParseFlags(const std::vector<std::string> &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<std::string>;
+ 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::stri
}
}
-static int RunInMultipleProcesses(int argc, char **argv, int NumWorkers,
- int NumJobs) {
+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;
- 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<std::thread> V;
std::thread Pulse(PulseThread);
@@ -208,10 +209,22 @@ int FuzzerDriver(int argc, char **argv,
}
int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF) {
- using namespace fuzzer;
+ std::vector<std::string> Args(argv, argv + argc);
+ return FuzzerDriver(Args, USF);
+}
- ProgName = argv[0];
- ParseFlags(argc, argv);
+int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback) {
+ FuzzerRandomLibc Rand(0);
+ SimpleUserSuppliedFuzzer SUSF(&Rand, Callback);
+ return FuzzerDriver(Args, SUSF);
+}
+
+int FuzzerDriver(const std::vector<std::string> &Args,
+ UserSuppliedFuzzer &USF) {
+ using namespace fuzzer;
+ assert(!Args.empty());
+ ProgName = new std::string(Args[0]);
+ ParseFlags(Args);
if (Flags.help) {
PrintHelp();
return 0;
@@ -224,7 +237,7 @@ int FuzzerDriver(int argc, char **argv,
}
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,
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,
}
F.RereadOutputCorpus();
- for (auto &inp : inputs)
+ for (auto &inp : *Inputs)
if (inp != Options.OutputCorpus)
F.ReadDir(inp, nullptr);
Modified: llvm/trunk/lib/Fuzzer/FuzzerInterface.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerInterface.h?rev=247300&r1=247299&r2=247300&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerInterface.h (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerInterface.h Thu Sep 10 11:57:57 2015
@@ -18,6 +18,8 @@
#include <cstddef>
#include <cstdint>
+#include <vector>
+#include <string>
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<std::string> &Args, UserSuppliedFuzzer &USF);
+int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
+
} // namespace fuzzer
#endif // LLVM_FUZZER_INTERFACE_H
More information about the llvm-commits
mailing list