CMake: Allow building fuzzing targets without libFuzzer or OSS-Fuzz

This commit is contained in:
Tim Schumacher 2022-03-21 11:36:41 +01:00 committed by Brian Gianforcaro
parent e3519b8e5c
commit bf502ae3b0
Notes: sideshowbarker 2024-07-17 16:22:55 +09:00
4 changed files with 20 additions and 9 deletions

View file

@ -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.

View file

@ -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")

View file

@ -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()

View file

@ -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 $<$<CXX_COMPILER_ID:Clang>:-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")