Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions tests/fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<TARGET_FILE:test_fuzz_input>" "${CMAKE_CURRENT_SOURCE_DIR}/corpus/exec_success"
COMMAND "$<TARGET_FILE:pybind_fuzzer>" ${_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 "$<TARGET_FILE:${target}>" ${corpus}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
COMMENT "Replaying the checked-in corpus for ${target}"
VERBATIM)
endfunction()

pybind11_add_fuzzer(pybind_fuzzer corpus)
13 changes: 11 additions & 2 deletions tests/fuzz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <file>`.

## Registering another harness

Add `<target>_fuzzer.cpp` and a corpus directory beside it, then register both
in `CMakeLists.txt` with `pybind11_add_fuzzer(<target>_fuzzer corpus_<target>_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
Expand Down
Loading