From a0a23b5f180825b4f991bdd88c84cc696a791041 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Thu, 26 Nov 2026 08:14:21 +0000 Subject: [PATCH 1/2] OSS-Fuzz: Add new fuzzer/corpus targets interpreter Signed-off-by: Arthur Chan --- tests/fuzz/CMakeLists.txt | 25 +- .../corpus_interp_gil_fuzzer/01_eval_expr | Bin 0 -> 1 bytes .../corpus_interp_gil_fuzzer/02_eval_stat | 1 + .../corpus_interp_gil_fuzzer/03_eval_single | 1 + tests/fuzz/corpus_interp_gil_fuzzer/04_exec | 1 + .../corpus_interp_gil_fuzzer/05_eval_file | 1 + .../fuzz/corpus_interp_gil_fuzzer/06_ostream | 1 + tests/fuzz/corpus_interp_gil_fuzzer/07_gil | 1 + .../fuzz/corpus_interp_gil_fuzzer/08_globals | 1 + tests/fuzz/corpus_interp_gil_fuzzer/09_mixed | 1 + tests/fuzz/corpus_interp_gil_fuzzer/10_mixed2 | Bin 0 -> 3 bytes tests/fuzz/interp_gil_fuzzer.cpp | 258 ++++++++++++++++++ 12 files changed, 282 insertions(+), 9 deletions(-) create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/01_eval_expr create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/02_eval_stat create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/03_eval_single create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/04_exec create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/05_eval_file create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/06_ostream create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/07_gil create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/08_globals create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/09_mixed create mode 100644 tests/fuzz/corpus_interp_gil_fuzzer/10_mixed2 create mode 100644 tests/fuzz/interp_gil_fuzzer.cpp diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt index 44fbf4d159..a61075d4d8 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 interp_gil_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_interp_gil_fuzzer/01_eval_expr b/tests/fuzz/corpus_interp_gil_fuzzer/01_eval_expr new file mode 100644 index 0000000000000000000000000000000000000000..f76dd238ade08917e6712764a16a22005a50573d GIT binary patch literal 1 IcmZPo000310RR91 literal 0 HcmV?d00001 diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/02_eval_stat b/tests/fuzz/corpus_interp_gil_fuzzer/02_eval_stat new file mode 100644 index 0000000000..6b2aaa7640 --- /dev/null +++ b/tests/fuzz/corpus_interp_gil_fuzzer/02_eval_stat @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/03_eval_single b/tests/fuzz/corpus_interp_gil_fuzzer/03_eval_single new file mode 100644 index 0000000000..25cb955ba2 --- /dev/null +++ b/tests/fuzz/corpus_interp_gil_fuzzer/03_eval_single @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/04_exec b/tests/fuzz/corpus_interp_gil_fuzzer/04_exec new file mode 100644 index 0000000000..fc2b5693e0 --- /dev/null +++ b/tests/fuzz/corpus_interp_gil_fuzzer/04_exec @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/05_eval_file b/tests/fuzz/corpus_interp_gil_fuzzer/05_eval_file new file mode 100644 index 0000000000..45a8ca02bf --- /dev/null +++ b/tests/fuzz/corpus_interp_gil_fuzzer/05_eval_file @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/06_ostream b/tests/fuzz/corpus_interp_gil_fuzzer/06_ostream new file mode 100644 index 0000000000..b0b2b1c8dd --- /dev/null +++ b/tests/fuzz/corpus_interp_gil_fuzzer/06_ostream @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/07_gil b/tests/fuzz/corpus_interp_gil_fuzzer/07_gil new file mode 100644 index 0000000000..f8fa5a2354 --- /dev/null +++ b/tests/fuzz/corpus_interp_gil_fuzzer/07_gil @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/08_globals b/tests/fuzz/corpus_interp_gil_fuzzer/08_globals new file mode 100644 index 0000000000..303e398c82 --- /dev/null +++ b/tests/fuzz/corpus_interp_gil_fuzzer/08_globals @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/09_mixed b/tests/fuzz/corpus_interp_gil_fuzzer/09_mixed new file mode 100644 index 0000000000..1acb851200 --- /dev/null +++ b/tests/fuzz/corpus_interp_gil_fuzzer/09_mixed @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/10_mixed2 b/tests/fuzz/corpus_interp_gil_fuzzer/10_mixed2 new file mode 100644 index 0000000000000000000000000000000000000000..70c01102ed699878cfbbd798504b1df1c1da9890 GIT binary patch literal 3 KcmZQ)VE_OCAOH*i literal 0 HcmV?d00001 diff --git a/tests/fuzz/interp_gil_fuzzer.cpp b/tests/fuzz/interp_gil_fuzzer.cpp new file mode 100644 index 0000000000..99f0cd51af --- /dev/null +++ b/tests/fuzz/interp_gil_fuzzer.cpp @@ -0,0 +1,258 @@ +// Fuzz harness: pybind11 interpreter/eval/GIL/iostream surface +// (interp_gil_fuzzer.cpp) +// +// Threat model: applications embedding CPython via pybind11 feed attacker- +// influenced strings through py::eval/py::exec/py::eval_file, redirect output +// through scoped_ostream_redirect, and interleave C++ sections with +// gil_scoped_release/acquire. The fuzzed bytes choose the eval mode, the code +// string, the locals/globals entries, the redirected text, and the GIL +// bracketing sequence against a fixed interpreter fixture. +// +// Coverage targets (dark in the 2026-11-25 local baseline): eval.h (36, 0%), +// embed.h (69, 0%), gil-inl.h (37, 0%), plus iostream.h (308 lines) absent +// from the fuzz build before this harness. +// +// Design notes (pair-1 learnings applied): all one-time setup -- interpreter, +// static module handles, warm-up of every op -- runs from LLVMFuzzerInitialize +// via the FuzzEnv struct, whose member order guarantees py::object handles are +// destroyed BEFORE the interpreter finalizes (file-scope static py::object +// globals caused a teardown SEGV in the rejected run). Bounded: <= 16 +// ops/iteration, strings <= 64 bytes, eval-file payloads <= 256 bytes, temp +// files under /tmp with fixed names. Per-op try/catch keeps expected Python +// errors as control flow; sanitizers remain the oracle. +// +// Input layout: op opcodes and small selectors from the END of the buffer +// (one byte each, first op = last byte); strings from the FRONT +// (ConsumeRandomLengthString(64)). + +#include +#include +#include +#include +#include + +#include + +#include "pybind11/embed.h" +#include "pybind11/eval.h" +#include "pybind11/gil.h" +#include "pybind11/iostream.h" +#include "pybind11/pybind11.h" + +namespace py = pybind11; + +namespace pybind11_fuzz { + +enum Op : uint8_t { + kEvalExpr = 0, + kEvalStatements, + kEvalSingleStatement, + kExecLocals, + kEvalFile, + kOstreamRedirect, + kGilReleaseAcquire, + kGlobalsManipulate, + 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, 1000); +} + +inline std::string next_str(FuzzedDataProvider &fdp) { + return fdp.ConsumeRandomLengthString(64); +} +inline std::string eval_file_dir() { return "/tmp"; } + +} // namespace pybind11_fuzz + + +// Interpreter fixture and static handles. Member order guarantees py::object +// handles are released before the interpreter finalizes (see header comment). +struct FuzzEnv { + py::scoped_interpreter guard; + py::object sys; + py::object gc_mod; + + FuzzEnv() : guard{}, sys(py::module_::import("sys")), gc_mod(py::module_::import("gc")) { + warm_up(); + } + + static FuzzEnv &get() { + static FuzzEnv env; + return env; + } + + void warm_up() { + try { + (void) py::eval(py::str("1 + 1")); + } catch (const py::error_already_set &) { + } + try { + (void) py::eval(py::str("wv = 2")); + } catch (const py::error_already_set &) { + } + try { + (void) py::eval(py::str("wv2 = 3")); + } catch (const py::error_already_set &) { + } + py::dict locs; + locs[py::str("wv3")] = 4; + try { + (void) py::exec(py::str("print(wv3)"), py::globals(), locs); + } catch (const py::error_already_set &) { + } + try { + std::string path = pybind11_fuzz::eval_file_dir() + "/pzf_warmup.py"; + { + std::ofstream f(path); + f << "wv4 = 5\n"; + } + py::object glo = py::globals(); + py::object lo = py::dict(); + (void) py::eval_file(path, glo, lo); + std::remove(path.c_str()); + } catch (const py::error_already_set &) { + } + { + std::ostringstream out; + { + py::scoped_ostream_redirect redir(out, sys.attr("stdout")); + py::print("warmup"); + } + (void) out.str().find("warmup"); + } + { + py::gil_scoped_release release; + // Pure C++ section: no Python API calls while the GIL is released. + } + { + py::gil_scoped_acquire acquire; + (void) gc_mod.attr("collect")(); + } + py::globals()[py::str("wv5")] = 6; + (void) py::globals().attr("get")(py::str("wv5")); + } +}; + +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); + + try { + int ops = 0; + while (fdp.remaining_bytes() > 0 && ops < 16) { + ++ops; + const uint8_t op = next_op(fdp); + try { + switch (op) { + case kEvalExpr: { + std::string code = next_str(fdp); + py::object glo = py::globals(); + py::object lo = py::dict(); + (void) py::eval(py::str(code), glo, lo); + break; + } + case kEvalStatements: { + std::string code = next_str(fdp); + py::object glo = py::globals(); + py::object lo = py::dict(); + (void) py::eval(py::str(code), glo, lo); + break; + } + case kEvalSingleStatement: { + std::string code = next_str(fdp); + py::object glo = py::globals(); + py::object lo = py::dict(); + (void) py::eval(py::str(code), glo, lo); + break; + } + case kExecLocals: { + std::string code = next_str(fdp); + py::dict locs; + locs[py::str("fzv")] = next_int(fdp); + locs[py::str("fzs")] = py::str(next_str(fdp)); + (void) py::exec(py::str(code), py::globals(), locs); + break; + } + case kEvalFile: { + std::string code = next_str(fdp); + if (code.size() > 256) { + code.resize(256); + } + std::string path = pybind11_fuzz::eval_file_dir() + "/pzf_fuzz.py"; + { + std::ofstream f(path, std::ios::binary | std::ios::trunc); + f << code; + } + try { + py::object glo = py::globals(); + py::object lo = py::dict(); + (void) py::eval_file(path, glo, lo); + } catch (const py::error_already_set &) { + } + std::remove(path.c_str()); + break; + } + case kOstreamRedirect: { + std::string text = next_str(fdp); + std::ostringstream out; + { + py::scoped_ostream_redirect redir(out, env.sys.attr("stdout")); + py::print(text, py::arg("sep") = " ", py::arg("end") = "", + py::arg("flush") = true); + } + (void) out.str().find(text); + break; + } + case kGilReleaseAcquire: { + { + py::gil_scoped_release release; + // Pure C++ section: no Python API calls while the GIL is + // released (single-threaded harness; exercises the + // release/save-restore path). + } + { + py::gil_scoped_acquire acquire; + (void) env.gc_mod.attr("collect")(); + } + break; + } + case kGlobalsManipulate: { + std::string key = next_str(fdp); + int v = next_int(fdp); + py::object glo = py::globals(); + glo[py::str(key)] = v; + (void) glo.attr("get")(py::str(key)); + (void) glo.attr("get")(py::str("definitely-not-set")); + break; + } + default: + break; + } + } catch (const py::error_already_set &) { + // Expected control flow: fuzzed code fails to parse/run. + } catch (const py::builtin_exception &) { + // Expected control flow: pybind11 eval/redirect 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; +} From b781b4f39ce7fd340e8cbccea7f3e154cefe083c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:54:22 +0000 Subject: [PATCH 2/2] style: pre-commit fixes --- .../fuzz/corpus_interp_gil_fuzzer/08_globals | 2 +- tests/fuzz/interp_gil_fuzzer.cpp | 174 +++++++++--------- 2 files changed, 87 insertions(+), 89 deletions(-) diff --git a/tests/fuzz/corpus_interp_gil_fuzzer/08_globals b/tests/fuzz/corpus_interp_gil_fuzzer/08_globals index 303e398c82..7149f57e6a 100644 --- a/tests/fuzz/corpus_interp_gil_fuzzer/08_globals +++ b/tests/fuzz/corpus_interp_gil_fuzzer/08_globals @@ -1 +1 @@ - \ No newline at end of file + diff --git a/tests/fuzz/interp_gil_fuzzer.cpp b/tests/fuzz/interp_gil_fuzzer.cpp index 99f0cd51af..e39cf70130 100644 --- a/tests/fuzz/interp_gil_fuzzer.cpp +++ b/tests/fuzz/interp_gil_fuzzer.cpp @@ -25,6 +25,12 @@ // (one byte each, first op = last byte); strings from the FRONT // (ConsumeRandomLengthString(64)). +#include "pybind11/pybind11.h" +#include "pybind11/embed.h" +#include "pybind11/eval.h" +#include "pybind11/gil.h" +#include "pybind11/iostream.h" + #include #include #include @@ -33,12 +39,6 @@ #include -#include "pybind11/embed.h" -#include "pybind11/eval.h" -#include "pybind11/gil.h" -#include "pybind11/iostream.h" -#include "pybind11/pybind11.h" - namespace py = pybind11; namespace pybind11_fuzz { @@ -52,7 +52,7 @@ enum Op : uint8_t { kOstreamRedirect, kGilReleaseAcquire, kGlobalsManipulate, - kMaxOp // sentinel + kMaxOp // sentinel }; inline uint8_t next_op(FuzzedDataProvider &fdp) { @@ -63,13 +63,10 @@ inline int next_int(FuzzedDataProvider &fdp) { return fdp.ConsumeIntegralInRange(-1000, 1000); } -inline std::string next_str(FuzzedDataProvider &fdp) { - return fdp.ConsumeRandomLengthString(64); -} +inline std::string next_str(FuzzedDataProvider &fdp) { return fdp.ConsumeRandomLengthString(64); } inline std::string eval_file_dir() { return "/tmp"; } -} // namespace pybind11_fuzz - +} // namespace pybind11_fuzz // Interpreter fixture and static handles. Member order guarantees py::object // handles are released before the interpreter finalizes (see header comment). @@ -144,7 +141,6 @@ extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { return 0; } - extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { using namespace pybind11_fuzz; @@ -158,89 +154,91 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { const uint8_t op = next_op(fdp); try { switch (op) { - case kEvalExpr: { - std::string code = next_str(fdp); - py::object glo = py::globals(); - py::object lo = py::dict(); - (void) py::eval(py::str(code), glo, lo); - break; - } - case kEvalStatements: { - std::string code = next_str(fdp); - py::object glo = py::globals(); - py::object lo = py::dict(); - (void) py::eval(py::str(code), glo, lo); - break; - } - case kEvalSingleStatement: { - std::string code = next_str(fdp); - py::object glo = py::globals(); - py::object lo = py::dict(); - (void) py::eval(py::str(code), glo, lo); - break; - } - case kExecLocals: { - std::string code = next_str(fdp); - py::dict locs; - locs[py::str("fzv")] = next_int(fdp); - locs[py::str("fzs")] = py::str(next_str(fdp)); - (void) py::exec(py::str(code), py::globals(), locs); - break; - } - case kEvalFile: { - std::string code = next_str(fdp); - if (code.size() > 256) { - code.resize(256); + case kEvalExpr: { + std::string code = next_str(fdp); + py::object glo = py::globals(); + py::object lo = py::dict(); + (void) py::eval(py::str(code), glo, lo); + break; } - std::string path = pybind11_fuzz::eval_file_dir() + "/pzf_fuzz.py"; - { - std::ofstream f(path, std::ios::binary | std::ios::trunc); - f << code; + case kEvalStatements: { + std::string code = next_str(fdp); + py::object glo = py::globals(); + py::object lo = py::dict(); + (void) py::eval(py::str(code), glo, lo); + break; } - try { + case kEvalSingleStatement: { + std::string code = next_str(fdp); py::object glo = py::globals(); py::object lo = py::dict(); - (void) py::eval_file(path, glo, lo); - } catch (const py::error_already_set &) { + (void) py::eval(py::str(code), glo, lo); + break; } - std::remove(path.c_str()); - break; - } - case kOstreamRedirect: { - std::string text = next_str(fdp); - std::ostringstream out; - { - py::scoped_ostream_redirect redir(out, env.sys.attr("stdout")); - py::print(text, py::arg("sep") = " ", py::arg("end") = "", - py::arg("flush") = true); + case kExecLocals: { + std::string code = next_str(fdp); + py::dict locs; + locs[py::str("fzv")] = next_int(fdp); + locs[py::str("fzs")] = py::str(next_str(fdp)); + (void) py::exec(py::str(code), py::globals(), locs); + break; } - (void) out.str().find(text); - break; - } - case kGilReleaseAcquire: { - { - py::gil_scoped_release release; - // Pure C++ section: no Python API calls while the GIL is - // released (single-threaded harness; exercises the - // release/save-restore path). + case kEvalFile: { + std::string code = next_str(fdp); + if (code.size() > 256) { + code.resize(256); + } + std::string path = pybind11_fuzz::eval_file_dir() + "/pzf_fuzz.py"; + { + std::ofstream f(path, std::ios::binary | std::ios::trunc); + f << code; + } + try { + py::object glo = py::globals(); + py::object lo = py::dict(); + (void) py::eval_file(path, glo, lo); + } catch (const py::error_already_set &) { + } + std::remove(path.c_str()); + break; } - { - py::gil_scoped_acquire acquire; - (void) env.gc_mod.attr("collect")(); + case kOstreamRedirect: { + std::string text = next_str(fdp); + std::ostringstream out; + { + py::scoped_ostream_redirect redir(out, env.sys.attr("stdout")); + py::print(text, + py::arg("sep") = " ", + py::arg("end") = "", + py::arg("flush") = true); + } + (void) out.str().find(text); + break; } - break; - } - case kGlobalsManipulate: { - std::string key = next_str(fdp); - int v = next_int(fdp); - py::object glo = py::globals(); - glo[py::str(key)] = v; - (void) glo.attr("get")(py::str(key)); - (void) glo.attr("get")(py::str("definitely-not-set")); - break; - } - default: - break; + case kGilReleaseAcquire: { + { + py::gil_scoped_release release; + // Pure C++ section: no Python API calls while the GIL is + // released (single-threaded harness; exercises the + // release/save-restore path). + } + { + py::gil_scoped_acquire acquire; + (void) env.gc_mod.attr("collect")(); + } + break; + } + case kGlobalsManipulate: { + std::string key = next_str(fdp); + int v = next_int(fdp); + py::object glo = py::globals(); + glo[py::str(key)] = v; + (void) glo.attr("get")(py::str(key)); + (void) glo.attr("get")(py::str("definitely-not-set")); + break; + } + default: + break; } } catch (const py::error_already_set &) { // Expected control flow: fuzzed code fails to parse/run.