summaryrefslogtreecommitdiffstats
path: root/tools/clang-fuzzer
diff options
context:
space:
mode:
authorMatt Morehouse <mascasa@google.com>2017-10-10 17:41:43 +0000
committerMatt Morehouse <mascasa@google.com>2017-10-10 17:41:43 +0000
commitc3893ecadc67a73263c5501c6215839a8c258735 (patch)
treef3967ee0560bbb6723dd8c7fb13cc1f241ec2bf9 /tools/clang-fuzzer
parenta92cdd5042367c446fbbd2b5453e7a01ef71c737 (diff)
[clang-fuzzer] Allow building without coverage instrumentation.
Summary: Compile with DummyClangFuzzer.cpp as entry point rather than libFuzzer's main when coverage instrumentation is missing. https://llvm.org/pr34314 Reviewers: kcc, bogner, vitalybuka Reviewed By: vitalybuka Subscribers: cfe-commits, mgorny Differential Revision: https://reviews.llvm.org/D38642 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@315336 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/clang-fuzzer')
-rw-r--r--tools/clang-fuzzer/CMakeLists.txt111
-rw-r--r--tools/clang-fuzzer/ClangFuzzer.cpp2
-rw-r--r--tools/clang-fuzzer/DummyClangFuzzer.cpp21
3 files changed, 81 insertions, 53 deletions
diff --git a/tools/clang-fuzzer/CMakeLists.txt b/tools/clang-fuzzer/CMakeLists.txt
index 82c3b0eb02..4abcee3ca6 100644
--- a/tools/clang-fuzzer/CMakeLists.txt
+++ b/tools/clang-fuzzer/CMakeLists.txt
@@ -1,60 +1,65 @@
-if( LLVM_USE_SANITIZE_COVERAGE )
- set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD})
- set(CXX_FLAGS_NOFUZZ ${CMAKE_CXX_FLAGS})
+set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} FuzzMutate)
+set(CXX_FLAGS_NOFUZZ ${CMAKE_CXX_FLAGS})
+set(DUMMY_MAIN DummyClangFuzzer.cpp)
+if(LLVM_USE_SANITIZE_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
+ unset(DUMMY_MAIN)
+endif()
+
+# Hack to bypass LLVM's cmake sources check and allow multiple libraries and
+# executables from this directory.
+set(LLVM_OPTIONAL_SOURCES
+ ClangFuzzer.cpp
+ DummyClangFuzzer.cpp
+ ExampleClangProtoFuzzer.cpp
+ )
+
+if(CLANG_ENABLE_PROTO_FUZZER)
+ # Create protobuf .h and .cc files, and put them in a library for use by
+ # clang-proto-fuzzer components.
+ find_package(Protobuf REQUIRED)
+ add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI)
+ include_directories(${PROTOBUF_INCLUDE_DIRS})
+ include_directories(${CMAKE_CURRENT_BINARY_DIR})
+ protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS cxx_proto.proto)
+ set(LLVM_OPTIONAL_SOURCES ${LLVM_OPTIONAL_SOURCES} ${PROTO_SRCS})
+ add_clang_library(clangCXXProto
+ ${PROTO_SRCS}
+ ${PROTO_HDRS}
+
+ LINK_LIBS
+ ${PROTOBUF_LIBRARIES}
+ )
- if(CLANG_ENABLE_PROTO_FUZZER)
- # Create protobuf .h and .cc files, and put them in a library for use by
- # clang-proto-fuzzer components.
- find_package(Protobuf REQUIRED)
- add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI)
- include_directories(${PROTOBUF_INCLUDE_DIRS})
- include_directories(${CMAKE_CURRENT_BINARY_DIR})
- protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS cxx_proto.proto)
- # Hack to bypass LLVM's cmake sources check and allow multiple libraries and
- # executables from this directory.
- set(LLVM_OPTIONAL_SOURCES
- ClangFuzzer.cpp
- ExampleClangProtoFuzzer.cpp
- ${PROTO_SRCS}
- )
- add_clang_library(clangCXXProto
- ${PROTO_SRCS}
- ${PROTO_HDRS}
-
- LINK_LIBS
- ${PROTOBUF_LIBRARIES}
- )
-
- # Build and include libprotobuf-mutator
- include(ProtobufMutator)
- include_directories(${ProtobufMutator_INCLUDE_DIRS})
-
- # Build the protobuf->C++ translation library and driver.
- add_clang_subdirectory(proto-to-cxx)
-
- # Build the protobuf fuzzer
- add_clang_executable(clang-proto-fuzzer ExampleClangProtoFuzzer.cpp)
- target_link_libraries(clang-proto-fuzzer
- ${ProtobufMutator_LIBRARIES}
- clangCXXProto
- clangHandleCXX
- clangProtoToCXX
- )
- else()
- # Hack to bypass LLVM's cmake sources check and allow multiple libraries and
- # executables from this directory.
- set(LLVM_OPTIONAL_SOURCES ClangFuzzer.cpp ExampleClangProtoFuzzer.cpp)
- endif()
-
- add_clang_subdirectory(handle-cxx)
-
- add_clang_executable(clang-fuzzer
- EXCLUDE_FROM_ALL
- ClangFuzzer.cpp
+ # Build and include libprotobuf-mutator
+ include(ProtobufMutator)
+ include_directories(${ProtobufMutator_INCLUDE_DIRS})
+
+ # Build the protobuf->C++ translation library and driver.
+ add_clang_subdirectory(proto-to-cxx)
+
+ # Build the protobuf fuzzer
+ add_clang_executable(clang-proto-fuzzer
+ ${DUMMY_MAIN}
+ ExampleClangProtoFuzzer.cpp
)
- target_link_libraries(clang-fuzzer
+ target_link_libraries(clang-proto-fuzzer
+ ${ProtobufMutator_LIBRARIES}
+ clangCXXProto
clangHandleCXX
+ clangProtoToCXX
)
endif()
+
+add_clang_subdirectory(handle-cxx)
+
+add_clang_executable(clang-fuzzer
+ EXCLUDE_FROM_ALL
+ ${DUMMY_MAIN}
+ ClangFuzzer.cpp
+ )
+
+target_link_libraries(clang-fuzzer
+ clangHandleCXX
+ )
diff --git a/tools/clang-fuzzer/ClangFuzzer.cpp b/tools/clang-fuzzer/ClangFuzzer.cpp
index 327d955f60..2d35fb7735 100644
--- a/tools/clang-fuzzer/ClangFuzzer.cpp
+++ b/tools/clang-fuzzer/ClangFuzzer.cpp
@@ -17,6 +17,8 @@
using namespace clang_fuzzer;
+extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { return 0; }
+
extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
std::string s((const char *)data, size);
HandleCXX(s, {"-O2"});
diff --git a/tools/clang-fuzzer/DummyClangFuzzer.cpp b/tools/clang-fuzzer/DummyClangFuzzer.cpp
new file mode 100644
index 0000000000..382c161307
--- /dev/null
+++ b/tools/clang-fuzzer/DummyClangFuzzer.cpp
@@ -0,0 +1,21 @@
+//===-- DummyClangFuzzer.cpp - Entry point to sanity check fuzzers --------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Provides a main() to build without linking libFuzzer.
+//
+//===----------------------------------------------------------------------===//
+#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[]) {
+ return llvm::runFuzzerOnInputs(argc, argv, LLVMFuzzerTestOneInput,
+ LLVMFuzzerInitialize);
+}