<div dir="ltr">Can we reuse lib/fuzzer/standalone/StandaloneFuzzTargetMain.c for this purpose? <div>Also, it's possible to link against libFuzzer w/o using the coverage instrumentation. <br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 1, 2017 at 10:02 AM, Justin Bogner via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: bogner<br>
Date: Fri Sep  1 10:02:22 2017<br>
New Revision: 312338<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=312338&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=312338&view=rev</a><br>
Log:<br>
llvm-isel-fuzzer: Make buildable and testable without libFuzzer<br>
<br>
This adds a dummy main so we can build and run the llvm-isel-fuzzer<br>
functionality when we aren't building LLVM with coverage. The approach<br>
here should serve as a template to stop in-tree fuzzers from<br>
bitrotting (See <a href="http://llvm.org/pr34314" rel="noreferrer" target="_blank">llvm.org/pr34314</a>).<br>
<br>
Note that I'll probably move most of the logic in DummyISelFuzzer's<br>
`main` to a library so it's easy to reuse it in other fuzz targets,<br>
but I'm planning on doing that in a follow up that also consolidates<br>
argument handling in our LLVMFuzzerInitialize implementations.<br>
<br>
Added:<br>
    llvm/trunk/tools/llvm-isel-<wbr>fuzzer/DummyISelFuzzer.cpp<br>
Modified:<br>
    llvm/trunk/cmake/modules/<wbr>AddLLVM.cmake<br>
    llvm/trunk/tools/llvm-isel-<wbr>fuzzer/CMakeLists.txt<br>
<br>
Modified: llvm/trunk/cmake/modules/<wbr>AddLLVM.cmake<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/cmake/modules/AddLLVM.cmake?rev=312338&r1=312337&r2=312338&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/cmake/<wbr>modules/AddLLVM.cmake?rev=<wbr>312338&r1=312337&r2=312338&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/cmake/modules/<wbr>AddLLVM.cmake (original)<br>
+++ llvm/trunk/cmake/modules/<wbr>AddLLVM.cmake Fri Sep  1 10:02:22 2017<br>
@@ -893,11 +893,15 @@ macro(add_llvm_utility name)<br>
 endmacro(add_llvm_utility name)<br>
<br>
 macro(add_llvm_fuzzer name)<br>
+  cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})<br>
   if( LLVM_USE_SANITIZE_COVERAGE )<br>
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")<br>
-    add_llvm_executable(${name} ${ARGN})<br>
+    add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})<br>
     set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")<br>
-  endif()<br>
+  elseif( ARG_DUMMY_MAIN )<br>
+    add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})<br>
+    set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")<br>
+endif()<br>
 endmacro()<br>
<br>
 macro(add_llvm_target target_name)<br>
<br>
Modified: llvm/trunk/tools/llvm-isel-<wbr>fuzzer/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-isel-fuzzer/CMakeLists.txt?rev=312338&r1=312337&r2=312338&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/tools/llvm-<wbr>isel-fuzzer/CMakeLists.txt?<wbr>rev=312338&r1=312337&r2=<wbr>312338&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/tools/llvm-isel-<wbr>fuzzer/CMakeLists.txt (original)<br>
+++ llvm/trunk/tools/llvm-isel-<wbr>fuzzer/CMakeLists.txt Fri Sep  1 10:02:22 2017<br>
@@ -12,4 +12,5 @@ set(LLVM_LINK_COMPONENTS<br>
     Support<br>
     Target<br>
 )<br>
-add_llvm_fuzzer(llvm-isel-<wbr>fuzzer llvm-isel-fuzzer.cpp)<br>
+add_llvm_fuzzer(llvm-isel-<wbr>fuzzer llvm-isel-fuzzer.cpp<br>
+  DUMMY_MAIN DummyISelFuzzer.cpp)<br>
<br>
Added: llvm/trunk/tools/llvm-isel-<wbr>fuzzer/DummyISelFuzzer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-isel-fuzzer/DummyISelFuzzer.cpp?rev=312338&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/tools/llvm-<wbr>isel-fuzzer/DummyISelFuzzer.<wbr>cpp?rev=312338&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/tools/llvm-isel-<wbr>fuzzer/DummyISelFuzzer.cpp (added)<br>
+++ llvm/trunk/tools/llvm-isel-<wbr>fuzzer/DummyISelFuzzer.cpp Fri Sep  1 10:02:22 2017<br>
@@ -0,0 +1,56 @@<br>
+//===--- DummyFuzzerMain.cpp - Entry point to sanity check the fuzzer -----===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===------------------------<wbr>------------------------------<wbr>----------------===//<br>
+//<br>
+// Implementation of main so we can build and test without linking libFuzzer.<br>
+//<br>
+//===------------------------<wbr>------------------------------<wbr>----------------===//<br>
+<br>
+#include "llvm/ADT/StringRef.h"<br>
+#include "llvm/Support/Compiler.h"<br>
+#include "llvm/Support/Error.h"<br>
+#include "llvm/Support/MemoryBuffer.h"<br>
+#include "llvm/Support/raw_ostream.h"<br>
+<br>
+using namespace llvm;<br>
+<br>
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);<br>
+extern "C" LLVM_ATTRIBUTE_WEAK int LLVMFuzzerInitialize(int *argc,<br>
+                                                        char ***argv) {<br>
+  return 0;<br>
+}<br>
+<br>
+int main(int argc, char *argv[]) {<br>
+  errs() << "*** This tool was not linked to libFuzzer.\n"<br>
+         << "*** No fuzzing will be performed.\n";<br>
+  if (int RC = LLVMFuzzerInitialize(&argc, &argv)) {<br>
+    errs() << "Initialization failed\n";<br>
+    return RC;<br>
+  }<br>
+<br>
+  for (int I = 1; I < argc; ++I) {<br>
+    StringRef Arg(argv[I]);<br>
+    if (Arg.startswith("-")) {<br>
+      if (Arg.equals("-ignore_<wbr>remaining_args=1"))<br>
+        break;<br>
+      continue;<br>
+    }<br>
+<br>
+    auto BufOrErr = MemoryBuffer::getFile(Arg, /*FileSize-*/ -1,<br>
+                                          /*RequiresNullTerminator=*/<wbr>false);<br>
+    if (std::error_code EC = BufOrErr.getError()) {<br>
+      errs() << "Error reading file: " << Arg << ": " << EC.message() << "\n";<br>
+      return 1;<br>
+    }<br>
+    std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());<br>
+    errs() << "Running: " << Arg << " (" << Buf->getBufferSize() << " bytes)\n";<br>
+    LLVMFuzzerTestOneInput(<br>
+        reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),<br>
+        Buf->getBufferSize());<br>
+  }<br>
+}<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>