]> granicus.if.org Git - llvm/commitdiff
Move some CLI utils out of llvm-isel-fuzzer and into the library
authorJustin Bogner <mail@justinbogner.com>
Sat, 2 Sep 2017 23:43:04 +0000 (23:43 +0000)
committerJustin Bogner <mail@justinbogner.com>
Sat, 2 Sep 2017 23:43:04 +0000 (23:43 +0000)
FuzzMutate might not be the best place for these, but it makes more
sense than an entirely new library for now. This will make setting up
fuzz targets with consistent CLI handling easier.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312425 91177308-0d34-0410-b5e6-96231b3b80d8

cmake/modules/AddLLVM.cmake
include/llvm/FuzzMutate/FuzzerCLI.h [new file with mode: 0644]
lib/FuzzMutate/CMakeLists.txt
lib/FuzzMutate/FuzzerCLI.cpp [new file with mode: 0644]
tools/llvm-isel-fuzzer/DummyISelFuzzer.cpp
tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp

index 93a55257e3884b73c53444bca2056c84a03b6ad1..66abb2486d9421aac8b5304f9da583d7f7102fbc 100644 (file)
@@ -896,6 +896,7 @@ macro(add_llvm_fuzzer name)
   cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
   if( LLVM_USE_SANITIZE_COVERAGE )
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
+    set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
     add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
     set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
   elseif( ARG_DUMMY_MAIN )
diff --git a/include/llvm/FuzzMutate/FuzzerCLI.h b/include/llvm/FuzzMutate/FuzzerCLI.h
new file mode 100644 (file)
index 0000000..83c8356
--- /dev/null
@@ -0,0 +1,39 @@
+//===-- FuzzerCLI.h - Common logic for CLIs of fuzzers ----------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Common logic needed to implement LLVM's fuzz targets' CLIs - including LLVM
+// concepts like cl::opt and libFuzzer concepts like -ignore_remaining_args=1.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_FUZZMUTATE_FUZZER_CLI_H
+#define LLVM_FUZZMUTATE_FUZZER_CLI_H
+
+#include "llvm/Support/DataTypes.h"
+
+namespace llvm {
+
+/// Parse cl::opts from a fuzz target commandline.
+///
+/// This handles all arguments after -ignore_remaining_args=1 as cl::opts.
+void parseFuzzerCLOpts(int ArgC, char *ArgV[]);
+
+using FuzzerTestFun = int (*)(const uint8_t *Data, size_t Size);
+using FuzzerInitFun = int (*)(int *argc, char ***argv);
+
+/// Runs a fuzz target on the inputs specified on the command line.
+///
+/// Useful for testing fuzz targets without linking to libFuzzer. Finds inputs
+/// in the argument list in a libFuzzer compatible way.
+int runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne,
+                      FuzzerInitFun Init = [](int *, char ***) { return 0; });
+
+} // end llvm namespace
+
+#endif // LLVM_FUZZMUTATE_FUZZER_CLI_H
index 4aec682b01fb6eb98323425f569972565acc5d55..1a1a9abb1c28fe3d287690c5b10bb6292e825786 100644 (file)
@@ -1,4 +1,5 @@
 add_llvm_library(LLVMFuzzMutate
+  FuzzerCLI.cpp
   IRMutator.cpp
   OpDescriptor.cpp
   Operations.cpp
diff --git a/lib/FuzzMutate/FuzzerCLI.cpp b/lib/FuzzMutate/FuzzerCLI.cpp
new file mode 100644 (file)
index 0000000..3b71cde
--- /dev/null
@@ -0,0 +1,63 @@
+//===-- FuzzerCLI.cpp -----------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/FuzzMutate/FuzzerCLI.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+
+void llvm::parseFuzzerCLOpts(int ArgC, char *ArgV[]) {
+  std::vector<const char *> CLArgs;
+  CLArgs.push_back(ArgV[0]);
+
+  int I = 1;
+  while (I < ArgC)
+    if (StringRef(ArgV[I++]).equals("-ignore_remaining_args=1"))
+      break;
+  while (I < ArgC)
+    CLArgs.push_back(ArgV[I++]);
+
+  cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
+}
+
+int llvm::runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne,
+                            FuzzerInitFun Init) {
+  errs() << "*** This tool was not linked to libFuzzer.\n"
+         << "*** No fuzzing will be performed.\n";
+  if (int RC = Init(&ArgC, &ArgV)) {
+    errs() << "Initialization failed\n";
+    return RC;
+  }
+
+  for (int I = 1; I < ArgC; ++I) {
+    StringRef Arg(ArgV[I]);
+    if (Arg.startswith("-")) {
+      if (Arg.equals("-ignore_remaining_args=1"))
+        break;
+      continue;
+    }
+
+    auto BufOrErr = MemoryBuffer::getFile(Arg, /*FileSize-*/ -1,
+                                          /*RequiresNullTerminator=*/false);
+    if (std::error_code EC = BufOrErr.getError()) {
+      errs() << "Error reading file: " << Arg << ": " << EC.message() << "\n";
+      return 1;
+    }
+    std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
+    errs() << "Running: " << Arg << " (" << Buf->getBufferSize() << " bytes)\n";
+    TestOne(reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
+            Buf->getBufferSize());
+  }
+  return 0;
+}
index 89c1c81cdbe47496fdba4b0606ae312727b10280..ec9c74e3040dd830b0f096904063672fc3700c06 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/Compiler.h"
-#include "llvm/Support/Error.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Support/raw_ostream.h"
-
-using namespace llvm;
+#include "llvm/FuzzMutate/FuzzerCLI.h"
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv);
-
 int main(int argc, char *argv[]) {
-  errs() << "*** This tool was not linked to libFuzzer.\n"
-         << "*** No fuzzing will be performed.\n";
-  if (int RC = LLVMFuzzerInitialize(&argc, &argv)) {
-    errs() << "Initialization failed\n";
-    return RC;
-  }
-
-  for (int I = 1; I < argc; ++I) {
-    StringRef Arg(argv[I]);
-    if (Arg.startswith("-")) {
-      if (Arg.equals("-ignore_remaining_args=1"))
-        break;
-      continue;
-    }
-
-    auto BufOrErr = MemoryBuffer::getFile(Arg, /*FileSize-*/ -1,
-                                          /*RequiresNullTerminator=*/false);
-    if (std::error_code EC = BufOrErr.getError()) {
-      errs() << "Error reading file: " << Arg << ": " << EC.message() << "\n";
-      return 1;
-    }
-    std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
-    errs() << "Running: " << Arg << " (" << Buf->getBufferSize() << " bytes)\n";
-    LLVMFuzzerTestOneInput(
-        reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
-        Buf->getBufferSize());
-  }
+  return llvm::runFuzzerOnInputs(argc, argv, LLVMFuzzerTestOneInput,
+                                 LLVMFuzzerInitialize);
 }
index 1d16376bb874688806407cc2db54b5e251de2c16..314acdb5087d141471da404ab82d3f14fa1880ed 100644 (file)
@@ -16,6 +16,7 @@
 #include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/FuzzMutate/FuzzerCLI.h"
 #include "llvm/FuzzMutate/IRMutator.h"
 #include "llvm/FuzzMutate/Operations.h"
 #include "llvm/FuzzMutate/Random.h"
@@ -133,21 +134,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
   return 0;
 }
 
-/// Parse command line options, but ignore anything before '--'.
-static void parseCLOptsAfterDashDash(int argc, char *argv[]) {
-  std::vector<const char *> CLArgs;
-  CLArgs.push_back(argv[0]);
-
-  int I = 1;
-  while (I < argc)
-    if (StringRef(argv[I++]).equals("-ignore_remaining_args=1"))
-      break;
-  while (I < argc)
-    CLArgs.push_back(argv[I++]);
-
-  cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
-}
-
 static void handleLLVMFatalError(void *, const std::string &Message, bool) {
   // TODO: Would it be better to call into the fuzzer internals directly?
   dbgs() << "LLVM ERROR: " << Message << "\n"
@@ -164,7 +150,7 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
   InitializeAllAsmPrinters();
   InitializeAllAsmParsers();
 
-  parseCLOptsAfterDashDash(*argc, *argv);
+  parseFuzzerCLOpts(*argc, *argv);
 
   if (TargetTriple.empty()) {
     errs() << *argv[0] << ": -mtriple must be specified\n";