Index: tools/clang-fuzzer/CMakeLists.txt =================================================================== --- tools/clang-fuzzer/CMakeLists.txt +++ tools/clang-fuzzer/CMakeLists.txt @@ -14,6 +14,7 @@ ClangFuzzer.cpp DummyClangFuzzer.cpp ExampleClangProtoFuzzer.cpp + FuzzerInitialize.cpp ) if(CLANG_ENABLE_PROTO_FUZZER) @@ -44,6 +45,7 @@ add_clang_executable(clang-proto-fuzzer ${DUMMY_MAIN} ExampleClangProtoFuzzer.cpp + FuzzerInitialize.cpp ) target_link_libraries(clang-proto-fuzzer Index: tools/clang-fuzzer/ExampleClangProtoFuzzer.cpp =================================================================== --- tools/clang-fuzzer/ExampleClangProtoFuzzer.cpp +++ tools/clang-fuzzer/ExampleClangProtoFuzzer.cpp @@ -18,27 +18,14 @@ #include "handle-cxx/handle_cxx.h" #include "proto-to-cxx/proto_to_cxx.h" +#include "FuzzerInitialize.h" #include "src/libfuzzer/libfuzzer_macro.h" #include using namespace clang_fuzzer; -static std::vector CLArgs; - -extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { - CLArgs.push_back("-O2"); - for (int I = 1; I < *argc; I++) { - if (strcmp((*argv)[I], "-ignore_remaining_args=1") == 0) { - for (I++; I < *argc; I++) - CLArgs.push_back((*argv)[I]); - break; - } - } - return 0; -} - DEFINE_BINARY_PROTO_FUZZER(const Function& input) { auto S = FunctionToString(input); - HandleCXX(S, CLArgs); + HandleCXX(S, GetCLArgs()); } Index: tools/clang-fuzzer/FuzzerInitialize.h =================================================================== --- /dev/null +++ tools/clang-fuzzer/FuzzerInitialize.h @@ -0,0 +1,25 @@ +//==-- FuzzerInitialize.h - Protobuf-C++ conversion ----------------------------==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Defines a function that returns the command line arguments for a specific +// call to the fuzz target. +// +//===----------------------------------------------------------------------===// + +#include "handle-cxx/handle_cxx.h" +#include "proto-to-cxx/proto_to_cxx.h" + +#include "src/libfuzzer/libfuzzer_macro.h" + +#include + +namespace clang_fuzzer { +const std::vector& GetCLArgs(); +} + Index: tools/clang-fuzzer/FuzzerInitialize.cpp =================================================================== --- tools/clang-fuzzer/FuzzerInitialize.cpp +++ tools/clang-fuzzer/FuzzerInitialize.cpp @@ -1,4 +1,4 @@ -//===-- ExampleClangProtoFuzzer.cpp - Fuzz Clang --------------------------===// +//===-- FuzzerInitialize.cpp - Fuzz Clang ---------------------------------===// // // The LLVM Compiler Infrastructure // @@ -8,24 +8,29 @@ //===----------------------------------------------------------------------===// /// /// \file -/// This file implements a function that runs Clang on a single -/// input and uses libprotobuf-mutator to find new inputs. This function is -/// then linked into the Fuzzer library. +/// This file implements two functions: one that returns the command line +/// arguments for a given call to the fuzz target and one that initializes +/// the fuzzer with the correct command line arguments. /// //===----------------------------------------------------------------------===// #include "cxx_proto.pb.h" -#include "handle-cxx/handle_cxx.h" -#include "proto-to-cxx/proto_to_cxx.h" -#include "src/libfuzzer/libfuzzer_macro.h" - -#include +#include "FuzzerInitialize.h" using namespace clang_fuzzer; + +namespace clang_fuzzer { + static std::vector CLArgs; +const std::vector& GetCLArgs() { + return CLArgs; +} + +} + extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { CLArgs.push_back("-O2"); for (int I = 1; I < *argc; I++) { @@ -38,7 +43,3 @@ return 0; } -DEFINE_BINARY_PROTO_FUZZER(const Function& input) { - auto S = FunctionToString(input); - HandleCXX(S, CLArgs); -}