From bf502ae3b012d7e7ffea2185a9952553e13c9337 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Mon, 21 Mar 2022 11:36:41 +0100 Subject: [PATCH] CMake: Allow building fuzzing targets without libFuzzer or OSS-Fuzz --- Documentation/AdvancedBuildInstructions.md | 1 + Meta/CMake/lagom_options.cmake | 1 + Meta/Lagom/CMakeLists.txt | 23 +++++++++++++++------- Meta/Lagom/Fuzzers/CMakeLists.txt | 4 ++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Documentation/AdvancedBuildInstructions.md b/Documentation/AdvancedBuildInstructions.md index 2b719fefcb..948e7b72e8 100644 --- a/Documentation/AdvancedBuildInstructions.md +++ b/Documentation/AdvancedBuildInstructions.md @@ -46,6 +46,7 @@ There are some optional features that can be enabled during compilation that are - `ENABLE_MEMORY_SANITIZER`: enables runtime checks for uninitialized memory accesses in Lagom test cases. - `ENABLE_UNDEFINED_SANITIZER`: builds in runtime checks for [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) (like null pointer dereferences and signed integer overflows) in Lagom test cases. - `ENABLE_COMPILER_EXPLORER_BUILD`: Skip building non-library entities in Lagom (this only applies to Lagom). +- `ENABLE_FUZZERS`: builds [fuzzers](https://en.wikipedia.org/wiki/Fuzzing) for various parts of the system. - `ENABLE_FUZZERS_LIBFUZZER`: builds Clang libFuzzer-based [fuzzers](https://en.wikipedia.org/wiki/Fuzzing) for various parts of the system. - `ENABLE_FUZZERS_OSSFUZZ`: builds OSS-Fuzz compatible [fuzzers](https://en.wikipedia.org/wiki/Fuzzing) for various parts of the system. - `ENABLE_EXTRA_KERNEL_DEBUG_SYMBOLS`: sets -Og and -ggdb3 compile options for building the Kernel. Allows for easier debugging of Kernel code. By default, the Kernel is built with -O2 instead. diff --git a/Meta/CMake/lagom_options.cmake b/Meta/CMake/lagom_options.cmake index 4765c4ff90..5d252c4b16 100644 --- a/Meta/CMake/lagom_options.cmake +++ b/Meta/CMake/lagom_options.cmake @@ -6,6 +6,7 @@ include(${CMAKE_CURRENT_LIST_DIR}/common_options.cmake) serenity_option(ENABLE_ADDRESS_SANITIZER OFF CACHE BOOL "Enable address sanitizer testing in gcc/clang") serenity_option(ENABLE_MEMORY_SANITIZER OFF CACHE BOOL "Enable memory sanitizer testing in gcc/clang") +serenity_option(ENABLE_FUZZERS OFF CACHE BOOL "Build fuzzing targets") serenity_option(ENABLE_FUZZERS_LIBFUZZER OFF CACHE BOOL "Build fuzzers using Clang's libFuzzer") serenity_option(ENABLE_FUZZERS_OSSFUZZ OFF CACHE BOOL "Build OSS-Fuzz compatible fuzzers") serenity_option(BUILD_LAGOM OFF CACHE BOOL "Build parts of the system targeting the host OS for fuzzing/testing") diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index b7bbf1d886..7ff8693f33 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -55,6 +55,10 @@ if (ENABLE_LAGOM_CCACHE) endif() endif() +if (ENABLE_FUZZERS_LIBFUZZER OR ENABLE_FUZZERS_OSSFUZZ) + set(ENABLE_FUZZERS ON) +endif() + include(wasm_spec_tests) add_compile_options(-fsigned-char) @@ -64,7 +68,7 @@ add_compile_options(-Wall -Wextra -Werror) add_compile_options(-fPIC -g) add_compile_options(-Wno-maybe-uninitialized) add_compile_options(-fno-exceptions) -if (NOT ENABLE_FUZZERS_LIBFUZZER) +if (NOT ENABLE_FUZZERS) add_compile_options(-fno-semantic-interposition) endif() @@ -105,12 +109,16 @@ if (ENABLE_UNDEFINED_SANITIZER) set(LINKER_FLAGS "${LINKER_FLAGS} -fsanitize=undefined -fno-sanitize=vptr") endif() +if (ENABLE_FUZZERS) + add_compile_options(-fno-omit-frame-pointer) +endif() + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang$") # Clang's default constexpr-steps limit is 1048576(2^20), GCC doesn't have one add_compile_options(-Wno-overloaded-virtual -Wno-user-defined-literals -fconstexpr-steps=16777216) if (ENABLE_FUZZERS_LIBFUZZER) - add_compile_options(-fsanitize=fuzzer -fno-omit-frame-pointer) + add_compile_options(-fsanitize=fuzzer) set(LINKER_FLAGS "${LINKER_FLAGS} -fsanitize=fuzzer") endif() @@ -119,7 +127,8 @@ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if (ENABLE_FUZZERS_LIBFUZZER) message(FATAL_ERROR "Fuzzer Sanitizer (-fsanitize=fuzzer) is only supported for Fuzzer targets with LLVM. " - "Reconfigure CMake with -DCMAKE_C_COMPILER and -DCMAKE_CXX_COMPILER pointing to a clang-based toolchain" + "Reconfigure CMake with -DCMAKE_C_COMPILER and -DCMAKE_CXX_COMPILER pointing to a clang-based toolchain " + "or build binaries without built-in fuzzing support by setting -DENABLE_FUZZERS instead." ) endif() endif() @@ -173,7 +182,7 @@ function(lagom_lib library fs_name) # Don't make alias when we're going to import a previous build for Tools # FIXME: Is there a better way to write this? - if (NOT ENABLE_FUZZERS_OSSFUZZ AND NOT ENABLE_FUZZERS_LIBFUZZER) + if (NOT ENABLE_FUZZERS) # alias for parity with exports add_library(Lagom::${library} ALIAS ${target_name}) endif() @@ -272,7 +281,7 @@ install( # Code Generators and other host tools # We need to make sure not to build code generators for Fuzzer builds, as they already have their own main.cpp # Instead, we import them from a previous install of Lagom. This mandates a two-stage build for fuzzers. -if (ENABLE_FUZZERS_OSSFUZZ OR ENABLE_FUZZERS_LIBFUZZER) +if (ENABLE_FUZZERS) find_package(Lagom REQUIRED) else() add_subdirectory(Tools) @@ -483,7 +492,7 @@ if (BUILD_LAGOM) lagom_lib(XML xml SOURCES ${LIBXML_SOURCES}) - if (NOT ENABLE_FUZZERS_OSSFUZZ AND NOT ENABLE_FUZZERS_LIBFUZZER AND NOT ENABLE_COMPILER_EXPLORER_BUILD) + if (NOT ENABLE_FUZZERS AND NOT ENABLE_COMPILER_EXPLORER_BUILD) # Lagom Examples add_executable(TestApp TestApp.cpp) target_link_libraries(TestApp LagomCore) @@ -711,6 +720,6 @@ if (BUILD_LAGOM) endif() endif() -if (ENABLE_FUZZERS_LIBFUZZER OR ENABLE_FUZZERS_OSSFUZZ) +if (ENABLE_FUZZERS) add_subdirectory(Fuzzers) endif() diff --git a/Meta/Lagom/Fuzzers/CMakeLists.txt b/Meta/Lagom/Fuzzers/CMakeLists.txt index fcca4e55df..674205aff6 100644 --- a/Meta/Lagom/Fuzzers/CMakeLists.txt +++ b/Meta/Lagom/Fuzzers/CMakeLists.txt @@ -4,7 +4,7 @@ function(add_simple_fuzzer name) if (ENABLE_FUZZERS_OSSFUZZ) target_link_libraries(${name} PUBLIC ${ARGN} LagomCore) - else() + elseif (ENABLE_FUZZERS_LIBFUZZER) target_compile_options(${name} PRIVATE $<$:-g -O1 -fsanitize=fuzzer> ) @@ -63,7 +63,7 @@ add_simple_fuzzer(FuzzWasmParser LagomWasm) add_simple_fuzzer(FuzzZip LagomArchive) add_simple_fuzzer(FuzzZlibDecompression LagomCompress) -if (NOT ENABLE_FUZZERS_OSSFUZZ) +if (ENABLE_FUZZERS_LIBFUZZER) set(CMAKE_EXE_LINKER_FLAGS "${ORIGINAL_CMAKE_EXE_LINKER_FLAGS} -fsanitize=address") set(CMAKE_SHARED_LINKER_FLAGS "${ORIGINAL_CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address") set(CMAKE_MODULE_LINKER_FLAGS "${ORIGINAL_CMAKE_MODULE_LINKER_FLAGS} -fsanitize=address")