[llvm] r312338 - llvm-isel-fuzzer: Make buildable and testable without libFuzzer

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 1 12:58:20 PDT 2017


On Fri, Sep 1, 2017 at 12:50 PM, Justin Bogner <mail at justinbogner.com>
wrote:

> Kostya Serebryany <kcc at google.com> writes:
> > Can we reuse lib/fuzzer/standalone/StandaloneFuzzTargetMain.c for this
> > purpose?
>
> Given that this was moved to compiler-rt, I don't think so. We really
> want to be able to test these things when we only have an LLVM checkout.
>
> If we did find a way to make this practical we'd have to make
> StandaloneFuzzTargetMain's argument handling a bit more robust.
>
> > Also, it's possible to link against libFuzzer w/o using the coverage
> > instrumentation.
>
> Fair enough, but does that give us any real advantage over wiring up a
> dummy main?


Less code. :)


> This way we can build these targets with any compiler
> regardless of whether it supports libFuzzer, so anyone building anywhere
> should see problems before it gets to the bots.
>

That's a valid point.


>
> > On Fri, Sep 1, 2017 at 10:02 AM, Justin Bogner via llvm-commits <
> > llvm-commits at lists.llvm.org> wrote:
> >
> >> Author: bogner
> >> Date: Fri Sep  1 10:02:22 2017
> >> New Revision: 312338
> >>
> >> URL: http://llvm.org/viewvc/llvm-project?rev=312338&view=rev
> >> Log:
> >> llvm-isel-fuzzer: Make buildable and testable without libFuzzer
> >>
> >> This adds a dummy main so we can build and run the llvm-isel-fuzzer
> >> functionality when we aren't building LLVM with coverage. The approach
> >> here should serve as a template to stop in-tree fuzzers from
> >> bitrotting (See llvm.org/pr34314).
> >>
> >> Note that I'll probably move most of the logic in DummyISelFuzzer's
> >> `main` to a library so it's easy to reuse it in other fuzz targets,
> >> but I'm planning on doing that in a follow up that also consolidates
> >> argument handling in our LLVMFuzzerInitialize implementations.
> >>
> >> Added:
> >>     llvm/trunk/tools/llvm-isel-fuzzer/DummyISelFuzzer.cpp
> >> Modified:
> >>     llvm/trunk/cmake/modules/AddLLVM.cmake
> >>     llvm/trunk/tools/llvm-isel-fuzzer/CMakeLists.txt
> >>
> >> Modified: llvm/trunk/cmake/modules/AddLLVM.cmake
> >> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/cmake/
> >> modules/AddLLVM.cmake?rev=312338&r1=312337&r2=312338&view=diff
> >> ============================================================
> >> ==================
> >> --- llvm/trunk/cmake/modules/AddLLVM.cmake (original)
> >> +++ llvm/trunk/cmake/modules/AddLLVM.cmake Fri Sep  1 10:02:22 2017
> >> @@ -893,11 +893,15 @@ macro(add_llvm_utility name)
> >>  endmacro(add_llvm_utility name)
> >>
> >>  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")
> >> -    add_llvm_executable(${name} ${ARGN})
> >> +    add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
> >>      set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
> >> -  endif()
> >> +  elseif( ARG_DUMMY_MAIN )
> >> +    add_llvm_executable(${name} ${ARG_DUMMY_MAIN}
> >> ${ARG_UNPARSED_ARGUMENTS})
> >> +    set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
> >> +endif()
> >>  endmacro()
> >>
> >>  macro(add_llvm_target target_name)
> >>
> >> Modified: llvm/trunk/tools/llvm-isel-fuzzer/CMakeLists.txt
> >> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-
> >> isel-fuzzer/CMakeLists.txt?rev=312338&r1=312337&r2=312338&view=diff
> >> ============================================================
> >> ==================
> >> --- llvm/trunk/tools/llvm-isel-fuzzer/CMakeLists.txt (original)
> >> +++ llvm/trunk/tools/llvm-isel-fuzzer/CMakeLists.txt Fri Sep  1
> 10:02:22
> >> 2017
> >> @@ -12,4 +12,5 @@ set(LLVM_LINK_COMPONENTS
> >>      Support
> >>      Target
> >>  )
> >> -add_llvm_fuzzer(llvm-isel-fuzzer llvm-isel-fuzzer.cpp)
> >> +add_llvm_fuzzer(llvm-isel-fuzzer llvm-isel-fuzzer.cpp
> >> +  DUMMY_MAIN DummyISelFuzzer.cpp)
> >>
> >> Added: llvm/trunk/tools/llvm-isel-fuzzer/DummyISelFuzzer.cpp
> >> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-
> >> isel-fuzzer/DummyISelFuzzer.cpp?rev=312338&view=auto
> >> ============================================================
> >> ==================
> >> --- llvm/trunk/tools/llvm-isel-fuzzer/DummyISelFuzzer.cpp (added)
> >> +++ llvm/trunk/tools/llvm-isel-fuzzer/DummyISelFuzzer.cpp Fri Sep  1
> >> 10:02:22 2017
> >> @@ -0,0 +1,56 @@
> >> +//===--- DummyFuzzerMain.cpp - Entry point to sanity check the fuzzer
> >> -----===//
> >> +//
> >> +//                     The LLVM Compiler Infrastructure
> >> +//
> >> +// This file is distributed under the University of Illinois Open
> Source
> >> +// License. See LICENSE.TXT for details.
> >> +//
> >> +//===------------------------------------------------------
> >> ----------------===//
> >> +//
> >> +// Implementation of main so we can build and test without linking
> >> libFuzzer.
> >> +//
> >> +//===------------------------------------------------------
> >> ----------------===//
> >> +
> >> +#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;
> >> +
> >> +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t
> Size);
> >> +extern "C" LLVM_ATTRIBUTE_WEAK int LLVMFuzzerInitialize(int *argc,
> >> +                                                        char ***argv) {
> >> +  return 0;
> >> +}
> >> +
> >> +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());
> >> +  }
> >> +}
> >>
> >>
> >> _______________________________________________
> >> llvm-commits mailing list
> >> llvm-commits at lists.llvm.org
> >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
> >>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170901/be8833ad/attachment.html>


More information about the llvm-commits mailing list