# libFuzzer targets for the code that parses untrusted input.  Configure the whole project with clang, for example:
#   CC=clang CXX=clang++ cmake -B build-fuzz -DCMAKE_BUILD_TYPE=Debug -DBTRFS_ASSISTANT_FUZZ=ON
# then run a target with a corpus directory, for example: build-fuzz/tests/fuzz/FuzzCsvParser corpus/
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    message(FATAL_ERROR "BTRFS_ASSISTANT_FUZZ needs clang")
endif()

set(FUZZ_FLAGS -fsanitize=fuzzer,address,undefined -fno-omit-frame-pointer)

# Instrument the libraries the fuzzers exercise so libFuzzer gets coverage from them.  Everything else linking them, such as
# the GUI and the helper in this build tree, then needs the sanitizer runtimes too.
foreach(library btrfs-assistant-common btrfs-assistant-core)
    target_compile_options(${library} PRIVATE -fsanitize=fuzzer-no-link,address,undefined -fno-omit-frame-pointer)
    target_link_options(${library} INTERFACE -fsanitize=address,undefined)
endforeach()

function(add_fuzzer name source)
    add_executable(${name} ${source})
    target_include_directories(${name} PRIVATE ${PROJECT_SOURCE_DIR}/src)
    target_compile_options(${name} PRIVATE ${FUZZ_FLAGS})
    target_link_options(${name} PRIVATE ${FUZZ_FLAGS})
    target_link_libraries(${name} PRIVATE btrfs-assistant-core)
endfunction()

add_fuzzer(FuzzBtrfsStatus FuzzBtrfsStatus.cpp)
add_fuzzer(FuzzCsvParser FuzzCsvParser.cpp)
add_fuzzer(FuzzSnapperMeta FuzzSnapperMeta.cpp)
add_fuzzer(FuzzValidator FuzzValidator.cpp)
