# libFuzzer targets for CORE parsers. Lane CORE owns tools/fuzz.
#
# Self-guarding: the top-level CMakeLists.txt add_subdirectory()s every tools/* that has a
# CMakeLists, unconditionally, so this file must opt out on its own when fuzzing isn't
# wanted or the compiler can't do libFuzzer.
#
# Each target compiles the parser sources directly (not the whole libveloxcore) so the
# code under test is fully fuzzer-instrumented without a second build of the library.

if(NOT VELOX_BUILD_FUZZ)
    return()
endif()
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    message(STATUS "tools/fuzz: libFuzzer needs Clang (have ${CMAKE_CXX_COMPILER_ID}); "
                   "skipping fuzz targets.")
    return()
endif()

set(_core ${CMAKE_SOURCE_DIR}/core)
set(_fuzz_flags -g -O1 -fsanitize=fuzzer,address,undefined -fno-omit-frame-pointer)

function(vdm_add_fuzzer name)
    add_executable(${name} ${ARGN})
    target_include_directories(${name} PRIVATE ${_core}/include ${_core}/src)
    target_compile_features(${name} PRIVATE cxx_std_23)
    target_compile_options(${name} PRIVATE ${_fuzz_flags})
    target_link_options(${name} PRIVATE ${_fuzz_flags})
endfunction()

vdm_add_fuzzer(fuzz_content_disposition
    content_disposition_fuzz.cpp
    ${_core}/src/net/content_disposition.cpp
    ${_core}/src/net/text_codec.cpp)

vdm_add_fuzzer(fuzz_url
    url_fuzz.cpp
    ${_core}/src/net/url.cpp
    ${_core}/src/net/text_codec.cpp)

# Seed corpora live next to the harnesses.
file(GLOB _cd_seeds ${CMAKE_CURRENT_SOURCE_DIR}/corpus/content_disposition/*)
file(GLOB _url_seeds ${CMAKE_CURRENT_SOURCE_DIR}/corpus/url/*)
