diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt index 44fbf4d159..bb237ce244 100644 --- a/tests/fuzz/CMakeLists.txt +++ b/tests/fuzz/CMakeLists.txt @@ -41,26 +41,33 @@ 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) +# One fuzz target per *_fuzzer.cpp in this directory. +set(_pybind11_fuzz_targets pybind_fuzzer stl_bind_fuzzer) # 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() -add_executable(pybind_fuzzer ${_pybind11_fuzz_sources}) -add_executable(test_fuzz_input test_fuzz_input.cpp) -foreach(target pybind_fuzzer test_fuzz_input) +foreach(target ${_pybind11_fuzz_targets}) + set(_pybind11_fuzz_sources ${target}.cpp) + if(NOT _pybind11_fuzz_engine) + list(APPEND _pybind11_fuzz_sources fuzz_replay.cpp) + endif() + add_executable(${target} ${_pybind11_fuzz_sources}) 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}) + endif() endforeach() -target_link_libraries(pybind_fuzzer PRIVATE pybind11::embed) -if(_pybind11_fuzz_engine) - target_link_libraries(pybind_fuzzer PRIVATE ${_pybind11_fuzz_engine}) -endif() + +add_executable(test_fuzz_input test_fuzz_input.cpp) +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( diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/01_vecnew b/tests/fuzz/corpus_stl_bind_fuzzer/01_vecnew new file mode 100644 index 0000000000..f76dd238ad Binary files /dev/null and b/tests/fuzz/corpus_stl_bind_fuzzer/01_vecnew differ diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/02_append b/tests/fuzz/corpus_stl_bind_fuzzer/02_append new file mode 100644 index 0000000000..6b2aaa7640 --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/02_append @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/03_insert b/tests/fuzz/corpus_stl_bind_fuzzer/03_insert new file mode 100644 index 0000000000..25cb955ba2 --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/03_insert @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/04_popget b/tests/fuzz/corpus_stl_bind_fuzzer/04_popget new file mode 100644 index 0000000000..fc2b5693e0 --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/04_popget @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/05_setdel b/tests/fuzz/corpus_stl_bind_fuzzer/05_setdel new file mode 100644 index 0000000000..45a8ca02bf --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/05_setdel @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/06_slice b/tests/fuzz/corpus_stl_bind_fuzzer/06_slice new file mode 100644 index 0000000000..b0b2b1c8dd --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/06_slice @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/07_mapnew b/tests/fuzz/corpus_stl_bind_fuzzer/07_mapnew new file mode 100644 index 0000000000..f8fa5a2354 --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/07_mapnew @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/08_mapset b/tests/fuzz/corpus_stl_bind_fuzzer/08_mapset new file mode 100644 index 0000000000..7149f57e6a --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/08_mapset @@ -0,0 +1 @@ + diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/09_mapgetdel b/tests/fuzz/corpus_stl_bind_fuzzer/09_mapgetdel new file mode 100644 index 0000000000..1c990f8e58 --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/09_mapgetdel @@ -0,0 +1 @@ + diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/10_iterate b/tests/fuzz/corpus_stl_bind_fuzzer/10_iterate new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/10_iterate @@ -0,0 +1 @@ + diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/11_wrongtype b/tests/fuzz/corpus_stl_bind_fuzzer/11_wrongtype new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/12_mixed b/tests/fuzz/corpus_stl_bind_fuzzer/12_mixed new file mode 100644 index 0000000000..b812a5f44e Binary files /dev/null and b/tests/fuzz/corpus_stl_bind_fuzzer/12_mixed differ diff --git a/tests/fuzz/corpus_stl_bind_fuzzer/13_mapmix b/tests/fuzz/corpus_stl_bind_fuzzer/13_mapmix new file mode 100644 index 0000000000..6f4882a8d8 --- /dev/null +++ b/tests/fuzz/corpus_stl_bind_fuzzer/13_mapmix @@ -0,0 +1 @@ + diff --git a/tests/fuzz/stl_bind_fuzzer.cpp b/tests/fuzz/stl_bind_fuzzer.cpp new file mode 100644 index 0000000000..8bbd438de7 --- /dev/null +++ b/tests/fuzz/stl_bind_fuzzer.cpp @@ -0,0 +1,264 @@ +// Fuzz harness: pybind11 bound-container protocol (stl_bind_fuzzer.cpp) +// +// Threat model: py::bind_vector/py::bind_map expose std:: containers as real +// Python sequence/mapping objects. Attacker-influenced data reaches them as +// indices, keys, values and slice bounds from Python-side code. This harness +// fuzzes exactly those levers: in-range/out-of-range/negative indices, +// insert/erase positions, missing/existing keys, wrong-typed elements. +// +// Coverage target: stl_bind.h (868 lines, entirely absent from the fuzz +// build before this harness) -- indexing bounds checks, negative index +// handling, insert/erase shifting, iterator paths, mapping lookups. +// +// Design notes (learnings applied): all one-time setup (interpreter, module +// import, handles, warm-up of every op) runs inside LLVMFuzzerInitialize via +// the FuzzEnv struct so traced executions stay malloc/free balanced; member +// order guarantees py::object handles die before the interpreter finalizes. +// Bounded: <= 24 ops, strings <= 32 bytes, ints within [-1000, 1000000]. +// Per-op try/catch keeps expected Python errors (IndexError/KeyError/ +// TypeError surfacing as py::error_already_set, caster rejections as +// py::builtin_exception) as control flow; sanitizers are the oracle. +// +// Input layout: op opcodes and small selectors from the END of the buffer +// (first op = last byte); strings from the FRONT (ConsumeRandomLengthString). + +#include "pybind11/pybind11.h" +#include "pybind11/embed.h" +#include "pybind11/stl_bind.h" + +#include +#include +#include + +#include + +namespace py = pybind11; + +namespace pybind11_fuzz { + +enum Op : uint8_t { + kVecNew = 0, + kVecAppend, + kVecInsert, + kVecPopGet, + kVecSetDel, + kVecSliceLen, + kMapNew, + kMapSet, + kMapGetDel, + kIterate, + kWrongType, + kMaxOp // sentinel +}; + +inline uint8_t next_op(FuzzedDataProvider &fdp) { + return fdp.ConsumeIntegralInRange(0, static_cast(kMaxOp - 1)); +} + +inline int next_int(FuzzedDataProvider &fdp) { + return fdp.ConsumeIntegralInRange(-1000, 1000000); +} + +// index used against containers: fuzzed but bounded around realistic sizes +inline py::object next_index_object(FuzzedDataProvider &fdp, int size) { + int raw = fdp.ConsumeIntegralInRange(-6, 14); + if (raw < 0) { + raw += size; // exercise Python negative-index normalisation + } + return py::int_(raw); +} + +inline std::string next_str(FuzzedDataProvider &fdp) { return fdp.ConsumeRandomLengthString(32); } + +} // namespace pybind11_fuzz + +PYBIND11_EMBEDDED_MODULE(fzb, m) { + py::bind_vector>(m, "IVec"); + py::bind_vector>(m, "SVec"); + py::bind_map>(m, "SIMap"); +} + +// Handles + interpreter, destruction-safe member order (see header comment). +struct FuzzEnv { + py::scoped_interpreter guard; + py::object gc_mod; + py::object IVec; + py::object SVec; + py::object SIMap; + + FuzzEnv() : guard{}, gc_mod(py::module_::import("gc")) { + py::object mod = py::module_::import("fzb"); + IVec = mod.attr("IVec"); + SVec = mod.attr("SVec"); + SIMap = mod.attr("SIMap"); + warm_up(); + } + + static FuzzEnv &get() { + static FuzzEnv env; + return env; + } + + void warm_up() { + py::object v = IVec(); + v.attr("append")(1); + v.attr("insert")(0, 2); + (void) v.attr("pop")(0); + (void) v.attr("__getitem__")(0); + v.attr("__setitem__")(0, 3); + (void) py::len(v); + (void) py::iter(v); + py::object m = SIMap(); + m.attr("__setitem__")(py::str("k"), 1); + (void) m.attr("__getitem__")(py::str("k")); + (void) m.attr("__contains__")(py::str("k")); + m.attr("__delitem__")(py::str("k")); + (void) gc_mod.attr("collect")(); + } +}; + +extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { + FuzzEnv::get(); + return 0; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + using namespace pybind11_fuzz; + + FuzzEnv &env = FuzzEnv::get(); + FuzzedDataProvider fdp(data, size); + py::object live; // most recent container; replaced per op + bool have = false; + + try { + int ops = 0; + while (fdp.remaining_bytes() > 0 && ops < 24) { + ++ops; + const uint8_t op = next_op(fdp); + try { + switch (op) { + case kVecNew: { + live = env.IVec(); + have = true; + break; + } + case kVecAppend: { + if (!have) { + break; + } + live.attr("append")(next_int(fdp)); + break; + } + case kVecInsert: { + if (!have) { + break; + } + int sz = static_cast(py::len(live)); + live.attr("insert")(next_index_object(fdp, sz), next_int(fdp)); + break; + } + case kVecPopGet: { + if (!have) { + break; + } + int sz = static_cast(py::len(live)); + try { + (void) live.attr("pop")(next_index_object(fdp, sz)); + } catch (const py::error_already_set &) { + } + break; + } + case kVecSetDel: { + if (!have) { + break; + } + int sz = static_cast(py::len(live)); + live.attr("__setitem__")(next_index_object(fdp, sz), next_int(fdp)); + break; + } + case kVecSliceLen: { + if (!have) { + break; + } + (void) py::len(live); + (void) py::iter(live); + (void) live.attr("__getitem__")(py::int_(next_int(fdp))); + break; + } + case kMapNew: { + live = env.SIMap(); + have = true; + break; + } + case kMapSet: { + if (!have) { + break; + } + std::string k = next_str(fdp); + live.attr("__setitem__")(py::str(k), next_int(fdp)); + break; + } + case kMapGetDel: { + if (!have) { + break; + } + std::string k = next_str(fdp); + try { + (void) live.attr("__getitem__")(py::str(k)); + } catch (const py::error_already_set &) { + } + (void) live.attr("__contains__")(py::str(k)); + try { + live.attr("__delitem__")(py::str(k)); + } catch (const py::error_already_set &) { + } + break; + } + case kIterate: { + if (!have) { + break; + } + py::iterator it = py::iter(live); + int steps = 0; + while (it != py::iterator::sentinel() && steps < 32) { + ++steps; + (void) py::reinterpret_borrow(*it); + ++it; + } + break; + } + case kWrongType: { + if (!have) { + break; + } + try { + live.attr("append")(py::str(next_str(fdp))); // str into IVec + } catch (const py::error_already_set &) { + } catch (const py::builtin_exception &) { + } + try { + live.attr("__getitem__")(next_int(fdp)); // int key into map + } catch (const py::error_already_set &) { + } + break; + } + default: + break; + } + if (ops % 6 == 0) { + (void) env.gc_mod.attr("collect")(); + } + } catch (const py::error_already_set &) { + // Expected control flow: IndexError/KeyError/TypeError. + } catch (const py::builtin_exception &) { + // Expected control flow: caster rejections. + } catch (const std::exception &) { + // Keep the gate clean from documented C++ exception paths. + } + } + } catch (const py::error_already_set &) { + } catch (const py::builtin_exception &) { + } catch (const std::exception &) { + } + return 0; +}