From 5c67e9c157a876170b42939909871d80fc03e59b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 1 Dec 2026 12:40:26 -0800 Subject: [PATCH] test: register fuzz targets with their replay corpora Pair each harness with its corpus in one CMake registration. Build and replay every registered pair from the existing fuzz_replay target, after the decoder check. Verified fixed-input replay on Python 3.15 with GIL and free-threaded, native libFuzzer/AddressSanitizer linking and replay, and a disposable two-target check that propagates a replay failure. Repository lint passed. Assisted-by: Codex:GPT-6.2-Sol --- tests/fuzz/CMakeLists.txt | 56 ++++++++++++++++++++++++--------------- tests/fuzz/README.md | 13 +++++++-- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt index 44fbf4d159..46b95a105d 100644 --- a/tests/fuzz/CMakeLists.txt +++ b/tests/fuzz/CMakeLists.txt @@ -41,33 +41,47 @@ endif() configure_file("${PYBIND11_FUZZER_INCLUDE_DIR}/fuzzer/FuzzedDataProvider.h" "${CMAKE_CURRENT_BINARY_DIR}/include/fuzzer/FuzzedDataProvider.h" COPYONLY) -set(_pybind11_fuzz_sources pybind_fuzzer.cpp) # OSS-Fuzz exports LIB_FUZZING_ENGINE to the object that provides main(). # Locally, link a small file-replay driver instead. -if(DEFINED ENV{LIB_FUZZING_ENGINE} AND NOT "$ENV{LIB_FUZZING_ENGINE}" STREQUAL "") - set(_pybind11_fuzz_engine "$ENV{LIB_FUZZING_ENGINE}") -else() - list(APPEND _pybind11_fuzz_sources fuzz_replay.cpp) - set(_pybind11_fuzz_engine "") -endif() +set(_pybind11_fuzz_engine "$ENV{LIB_FUZZING_ENGINE}") -add_executable(pybind_fuzzer ${_pybind11_fuzz_sources}) add_executable(test_fuzz_input test_fuzz_input.cpp) -foreach(target pybind_fuzzer test_fuzz_input) - pybind11_enable_warnings(${target}) - target_include_directories(${target} SYSTEM PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include") -endforeach() -target_link_libraries(pybind_fuzzer PRIVATE pybind11::embed) -if(_pybind11_fuzz_engine) - target_link_libraries(pybind_fuzzer PRIVATE ${_pybind11_fuzz_engine}) -endif() +pybind11_enable_warnings(test_fuzz_input) +target_include_directories(test_fuzz_input SYSTEM PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include") -file(GLOB _pybind11_fuzz_corpus CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/corpus/*") add_custom_target( fuzz_replay COMMAND "$" "${CMAKE_CURRENT_SOURCE_DIR}/corpus/exec_success" - COMMAND "$" ${_pybind11_fuzz_corpus} - DEPENDS pybind_fuzzer test_fuzz_input + DEPENDS test_fuzz_input WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" - USES_TERMINAL - COMMENT "Replaying the checked-in fuzz corpus") + USES_TERMINAL VERBATIM) + +function(pybind11_add_fuzzer target corpus_dir) + add_executable(${target} ${target}.cpp) + pybind11_enable_warnings(${target}) + target_include_directories(${target} SYSTEM PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include") + target_link_libraries(${target} PRIVATE pybind11::embed) + if(_pybind11_fuzz_engine) + target_link_libraries(${target} PRIVATE ${_pybind11_fuzz_engine}) + else() + target_sources(${target} PRIVATE fuzz_replay.cpp) + endif() + + file( + GLOB corpus + LIST_DIRECTORIES FALSE + CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${corpus_dir}/*") + if(NOT corpus) + message(FATAL_ERROR "No replay inputs found for ${target} in ${corpus_dir}") + endif() + add_dependencies(fuzz_replay ${target}) + add_custom_command( + TARGET fuzz_replay + POST_BUILD + COMMAND "$" ${corpus} + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + COMMENT "Replaying the checked-in corpus for ${target}" + VERBATIM) +endfunction() + +pybind11_add_fuzzer(pybind_fuzzer corpus) diff --git a/tests/fuzz/README.md b/tests/fuzz/README.md index 46b57752ea..e7194efadc 100644 --- a/tests/fuzz/README.md +++ b/tests/fuzz/README.md @@ -31,8 +31,8 @@ on `PATH`, set `-DPYBIND11_FUZZER_INCLUDE_DIR=/path/to/include`, where that directory contains `fuzzer/FuzzedDataProvider.h`. GCC can use this header too. `fuzz_replay` first checks the decoded fields of the `exec_success` seed, -then runs `LLVMFuzzerTestOneInput` on every file in `tests/fuzz/corpus`. -To reproduce one OSS-Fuzz artifact with the standalone binary: +then runs each registered harness on its corpus. The original `pybind_fuzzer` +uses `tests/fuzz/corpus`. To replay an input with the standalone binary: ```bash ./build/tests/fuzz/pybind_fuzzer /path/to/crash @@ -43,6 +43,15 @@ Both builds use the same callback and input decoder. When reproducing a sanitizer finding, enable the same sanitizer in the replay build. The libFuzzer binary can also replay one artifact with `-runs=1 `. +## Registering another harness + +Add `_fuzzer.cpp` and a corpus directory beside it, then register both +in `CMakeLists.txt` with `pybind11_add_fuzzer(_fuzzer corpus__fuzzer)`. +The `fuzz_replay` target builds each registered executable and runs it on the +files in its corpus directory. For standalone replay, the driver calls +`LLVMFuzzerTestOneInput` directly, so the callback must ensure any one-time +setup has run even when the engine's initialization hook was not called. + ## Input format The decoder reads five strings, in order: Python code, an `os` attribute