From 48da5f07a548e69e2dc5da043e49757fb6b1a7b3 Mon Sep 17 00:00:00 2001 From: Bhargava Shastry Date: Mon, 16 Sep 2019 16:18:27 +0200 Subject: [PATCH] ossfuzz: Create a separate cmake toolchain file for ossfuzz so that multiple fuzzing backends may be used --- cmake/toolchains/libfuzzer.cmake | 10 ++++++---- cmake/toolchains/ossfuzz.cmake | 11 +++++++++++ test/tools/ossfuzz/CMakeLists.txt | 18 +++++++++--------- 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 cmake/toolchains/ossfuzz.cmake diff --git a/cmake/toolchains/libfuzzer.cmake b/cmake/toolchains/libfuzzer.cmake index 06c27d931..8a295e05b 100644 --- a/cmake/toolchains/libfuzzer.cmake +++ b/cmake/toolchains/libfuzzer.cmake @@ -1,9 +1,11 @@ # Inherit default options include("${CMAKE_CURRENT_LIST_DIR}/default.cmake") # Disable Z3 and CVC4 since none of the existing fuzzers need them -set(USE_Z3 OFF CACHE BOOL "" FORCE) -set(USE_CVC4 OFF CACHE BOOL "" FORCE) +set(USE_Z3 OFF CACHE BOOL "Disable Z3" FORCE) +set(USE_CVC4 OFF CACHE BOOL "Disable CVC4" FORCE) # Build fuzzing binaries -set(OSSFUZZ 1) +set(OSSFUZZ ON CACHE BOOL "Enable fuzzer build" FORCE) +# Use libfuzzer as the fuzzing back-end +set(LIB_FUZZING_ENGINE "-fsanitize=fuzzer" CACHE STRING "Use libfuzzer back-end" FORCE) # clang/libfuzzer specific flags for ASan instrumentation -set(CMAKE_CXX_FLAGS "-O1 -gline-tables-only -fsanitize=address -fsanitize-address-use-after-scope -fsanitize=fuzzer-no-link -stdlib=libstdc++") +set(CMAKE_CXX_FLAGS "-O1 -gline-tables-only -fsanitize=address -fsanitize-address-use-after-scope -fsanitize=fuzzer-no-link -stdlib=libstdc++" CACHE STRING "Custom compilation flags" FORCE) diff --git a/cmake/toolchains/ossfuzz.cmake b/cmake/toolchains/ossfuzz.cmake new file mode 100644 index 000000000..113b6b453 --- /dev/null +++ b/cmake/toolchains/ossfuzz.cmake @@ -0,0 +1,11 @@ +# Inherit default options +include("${CMAKE_CURRENT_LIST_DIR}/default.cmake") +# Disable CVC4. +set(USE_CVC4 OFF CACHE BOOL "Disable CVC4" FORCE) +# Enable fuzzers +set(OSSFUZZ ON CACHE BOOL "Enable fuzzer build" FORCE) +set(LIB_FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE} CACHE STRING "Use fuzzer back-end defined by environment variable" FORCE) +# Link statically against boost libraries +set(BOOST_FOUND ON CACHE BOOL "" FORCE) +set(Boost_USE_STATIC_LIBS ON CACHE BOOL "Link against static Boost libraries" FORCE) +set(Boost_USE_STATIC_RUNTIME ON CACHE BOOL "Link against static Boost runtime library" FORCE) diff --git a/test/tools/ossfuzz/CMakeLists.txt b/test/tools/ossfuzz/CMakeLists.txt index 1eca9fb2b..2481c63c3 100644 --- a/test/tools/ossfuzz/CMakeLists.txt +++ b/test/tools/ossfuzz/CMakeLists.txt @@ -19,27 +19,27 @@ endif() if (OSSFUZZ) add_executable(solc_opt_ossfuzz solc_opt_ossfuzz.cpp ../fuzzer_common.cpp) target_link_libraries(solc_opt_ossfuzz PRIVATE libsolc evmasm) - set_target_properties(solc_opt_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(solc_opt_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) add_executable(solc_noopt_ossfuzz solc_noopt_ossfuzz.cpp ../fuzzer_common.cpp) target_link_libraries(solc_noopt_ossfuzz PRIVATE libsolc evmasm) - set_target_properties(solc_noopt_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(solc_noopt_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) add_executable(const_opt_ossfuzz const_opt_ossfuzz.cpp ../fuzzer_common.cpp) target_link_libraries(const_opt_ossfuzz PRIVATE libsolc evmasm) - set_target_properties(const_opt_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(const_opt_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) add_executable(strictasm_diff_ossfuzz strictasm_diff_ossfuzz.cpp yulFuzzerCommon.cpp) target_link_libraries(strictasm_diff_ossfuzz PRIVATE libsolc evmasm yulInterpreter) - set_target_properties(strictasm_diff_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(strictasm_diff_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) add_executable(strictasm_opt_ossfuzz strictasm_opt_ossfuzz.cpp) target_link_libraries(strictasm_opt_ossfuzz PRIVATE yul) - set_target_properties(strictasm_opt_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(strictasm_opt_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) add_executable(strictasm_assembly_ossfuzz strictasm_assembly_ossfuzz.cpp) target_link_libraries(strictasm_assembly_ossfuzz PRIVATE yul) - set_target_properties(strictasm_assembly_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(strictasm_assembly_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) add_executable(yul_proto_ossfuzz yulProtoFuzzer.cpp protoToYul.cpp yulProto.pb.cc) target_include_directories(yul_proto_ossfuzz PRIVATE /usr/include/libprotobuf-mutator) @@ -48,7 +48,7 @@ if (OSSFUZZ) protobuf-mutator.a protobuf.a ) - set_target_properties(yul_proto_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(yul_proto_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) add_executable(yul_proto_diff_ossfuzz yulProto_diff_ossfuzz.cpp yulFuzzerCommon.cpp protoToYul.cpp yulProto.pb.cc) target_include_directories(yul_proto_diff_ossfuzz PRIVATE /usr/include/libprotobuf-mutator) @@ -58,7 +58,7 @@ if (OSSFUZZ) protobuf-mutator.a protobuf.a ) - set_target_properties(yul_proto_diff_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(yul_proto_diff_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) add_executable(abiv2_proto_ossfuzz ../../EVMHost.cpp @@ -76,7 +76,7 @@ if (OSSFUZZ) protobuf-mutator.a protobuf.a ) - set_target_properties(abiv2_proto_ossfuzz PROPERTIES LINK_FLAGS "-fsanitize=fuzzer") + set_target_properties(abiv2_proto_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE}) else() add_library(solc_opt_ossfuzz solc_opt_ossfuzz.cpp