mirror of
https://gitlab.kitware.com/cmake/cmake.git
synced 2026-09-25 04:09:36 +03:00
Add infrastructure for building fuzz testing targets with libFuzzer or other fuzzing engines (e.g., OSS-Fuzz's LIB_FUZZING_ENGINE). Features: - CMake_BUILD_FUZZING option in root CMakeLists.txt - Fuzzing/CMakeLists.txt with add_fuzzer() macro - Support for libFuzzer and external fuzzing engines - Documentation in Tests/Fuzzing/README.rst The infrastructure is opt-in and requires a compatible fuzzing engine. If CMake_BUILD_FUZZING is enabled but no engine is found, configuration fails with a clear error message. See Tests/Fuzzing/README.rst for build instructions.
55 lines
1.8 KiB
CMake
55 lines
1.8 KiB
CMake
# Fuzzing targets for CMake
|
|
# See README.rst for documentation.
|
|
|
|
# Determine fuzzing engine
|
|
# OSS-Fuzz sets LIB_FUZZING_ENGINE, otherwise use libFuzzer
|
|
if(DEFINED ENV{LIB_FUZZING_ENGINE})
|
|
set(FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE})
|
|
set(FUZZING_ENGINE_FOUND TRUE)
|
|
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
|
# Check if libFuzzer is available (needs both compile and link flags)
|
|
include(CheckCXXSourceCompiles)
|
|
set(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer")
|
|
set(CMAKE_REQUIRED_LINK_OPTIONS "-fsanitize=fuzzer")
|
|
check_cxx_source_compiles("extern \"C\" int LLVMFuzzerTestOneInput(const char *data, long size) { return 0; }" HAVE_LIBFUZZER)
|
|
unset(CMAKE_REQUIRED_FLAGS)
|
|
unset(CMAKE_REQUIRED_LINK_OPTIONS)
|
|
if(HAVE_LIBFUZZER)
|
|
set(FUZZING_ENGINE "-fsanitize=fuzzer")
|
|
set(FUZZING_ENGINE_FOUND TRUE)
|
|
endif()
|
|
endif()
|
|
|
|
if(NOT FUZZING_ENGINE_FOUND)
|
|
message(FATAL_ERROR "No fuzzing engine found. CMake_BUILD_FUZZING requires libFuzzer or LIB_FUZZING_ENGINE.")
|
|
endif()
|
|
|
|
# Common link libraries
|
|
set(FUZZER_LINK_LIBS
|
|
CMakeLib
|
|
)
|
|
|
|
# Macro to add a fuzzer target
|
|
macro(add_fuzzer name source)
|
|
add_executable(${name} ${source})
|
|
target_link_libraries(${name} PRIVATE ${FUZZER_LINK_LIBS})
|
|
|
|
# If using libFuzzer directly, add the flag
|
|
if(FUZZING_ENGINE STREQUAL "-fsanitize=fuzzer")
|
|
target_compile_options(${name} PRIVATE -fsanitize=fuzzer)
|
|
target_link_options(${name} PRIVATE -fsanitize=fuzzer)
|
|
else()
|
|
# OSS-Fuzz provides engine as a library
|
|
target_link_libraries(${name} PRIVATE ${FUZZING_ENGINE})
|
|
endif()
|
|
|
|
# Ensure we don't apply clang-tidy to fuzzers
|
|
set_property(TARGET ${name} PROPERTY C_CLANG_TIDY "")
|
|
set_property(TARGET ${name} PROPERTY CXX_CLANG_TIDY "")
|
|
endmacro()
|
|
|
|
# Existing fuzzer from OSS-Fuzz integration
|
|
add_fuzzer(xml_parser_fuzzer xml_parser_fuzzer.cc)
|
|
|
|
message(STATUS "Fuzzing targets enabled with engine: ${FUZZING_ENGINE}")
|