Skip to content

OSS-Fuzz: Move fuzzer to upstream repository - #6203

Merged
rwgk merged 13 commits into
pybind:masterfrom
arthurscchan:fuzz/upstream-harness
Nov 24, 2026
Merged

rwgk merged 13 commits into
pybind:masterfrom
arthurscchan:fuzz/upstream-harness

Conversation

@arthurscchan

Copy link
Copy Markdown
Contributor

This PR moves all the OSS-Fuzz fuzzers to the upstream storage for easier management. This PR works together with OSS-Fuzz (google/oss-fuzz#16274) to accommodate the moving of fuzzers upstream.

Of course, this is only a suggestion. If the maintainers would prefer the fuzzers to remain in OSS-Fuzz, I am happy to keep the current setup and to ping the relevant maintainers for review whenever I add new fuzzers later in the process.

Thank you for considering this, and I would welcome any feedback on the preferred approach.

Remark: After this PR is accepted and merged, there will be additional PRs for adding new fuzzers for this project later.

@arthurscchan
arthurscchan force-pushed the fuzz/upstream-harness branch from 20f42db to 26dc0e3 Compare October 13, 2026 09:07
Comment thread tests/test_gil_scoped.py Resolved

@rwgk rwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for proposing this! Keeping the fuzzers alongside the code they exercise would let us review and update both together.

My concern is that this PR adds the source file without integrating it into our build or tests. Changes in either pybind11 or the OSS-Fuzz environment could then break the harness while all pybind11 CI checks remain green. Before we take ownership of this code, I’d like us to establish a sustainable way to build, exercise, and maintain it.

I don't have experience working with fuzzers, so the requests below were mostly written by codex GPT-6.1-Sol ultra, with small manual edits:

Could you please add the following?

  1. A supported build target for the harness. Ideally, our tests and OSS-Fuzz would use the same build rule. Making this optional and enabling it in one suitable CI configuration would be sufficient; we don’t need LLVM dependencies throughout our test matrix.

  2. A small checked-in corpus and an automated replay test. This should exercise the actual fuzz callback, with inputs covering both successful operations and expected Python exceptions. There should be at least one CI job.

  3. An automated check of the OSS-Fuzz integration. CIFuzz looks like a suitable option for building and briefly running the fuzzers on PRs. Another approach would also be welcome if it checks compatibility with the actual OSS-Fuzz build environment.

I’d also appreciate an explanation of which pybind11 behavior this harness is intended to exercise, particularly because it involves py::exec. When a failure is reported, we’ll need to reproduce it locally, understand the operations involved, and reduce it to a focused regression test. Could you document that workflow? Would fixed Python snippets with fuzzed arguments, or a selection of explicit binding and conversion operations, make failures easier to investigate? I’m open to your advice on the approach; I’d like us to establish that the harness provides useful coverage and that maintaining and debugging it will be practical for pybind11 contributors.

@arthurscchan

arthurscchan commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for your reply. I will address those concern and try implementing that into the build/test system, please spare me for a few days to handle that and sorry for the slightly late reply. @rwgk

@arthurscchan
arthurscchan marked this pull request as draft October 29, 2026 04:55
@arthurscchan
arthurscchan force-pushed the fuzz/upstream-harness branch from 52e7a4a to 8a6e88c Compare October 29, 2026 04:55
@arthurscchan

Copy link
Copy Markdown
Contributor Author

Hi, I have a push a drafted version that addressing your comments and suggested approach. Here are the summary and I am doing longer test to verify some ofthe changes, Please do let me know if you find additional comments and suggestions on the work, thanks. Remark: Part of the README and cifuzz process has been helped by AI.

  1. Dropped the test_gil_scoped.py change on rebase. That was a pre-commit side effect; tests: fix GIL subprocess test correctness #6211 and tests: consolidate GIL subprocess runs #6212 cover that file.
  2. Added an opt-in PYBIND11_BUILD_FUZZERS target that builds tests/fuzz/pybind_fuzzer.cc against pybind11::embed. It is off in the normal matrix. OSS-Fuzz uses the same target when LIB_FUZZING_ENGINE is set.
  3. Checked in a small corpus and one Ubuntu 3.13 CI job that replays it through LLVMFuzzerTestOneInput. The inputs cover successful Python and expected error_already_set cases.
  4. Added a CIFuzz workflow for the pybind11 OSS-Fuzz project. It is non-blocking until pybind11: Move fuzzer to upstream repository google/oss-fuzz#16274 points build.sh at this target, because CIFuzz uses that script rather than this CMake option.
  5. Documented the coverage and the reproduce/reduce workflow in tests/fuzz/README.md.

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
@arthurscchan
arthurscchan force-pushed the fuzz/upstream-harness branch from 7bdc94d to 5f09526 Compare October 29, 2026 09:45
@rwgk

rwgk commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

@arthurscchan I only have a minute for a super quick glance right now. Thanks for fully building this out! I might only get a chance to take a careful look in a couple days from now. What I can squeeze in in the meantime is help with reruns to get the CI green. (IIUC only a maintainer can do that, is that true?) I briefly looked at the Ubuntu GraalPy failures: they don't look like flakes. Have you analyzed the failures already? Please ping me here to request reruns, if needed.

@arthurscchan

Copy link
Copy Markdown
Contributor Author

Hi @rwgk, I have analyse the CI failure briefly, I do think it is something unrelated to this PR, but I need to do a little bit more understanding of that to confirm. Possibly there is a version problem of a moduel that cause the failing, I will double confirm the reason and keep you updated.

@rwgk

rwgk commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

— codex GPT-6 on behalf of Ralf —

A CI baseline: the scheduled master run is green, with all 69 jobs successful after retrying the dependency-download failures.

The two recurring Ubuntu GraalPy failures here are definitely related to this PR’s workflow change. For both 25.0 and 25.3, the Python tests and existing parallel “Interface test” succeed. The added second “Interface test” then fails with:

Could NOT find Python (missing: Development.Module)

This pattern already occurred on October 30 and November 2 and persists in the latest rerun.

The workflow already runs test_cmake_build in the parallel group, then runs it again after the C++ tests. Master has only the first invocation.

I’d start by checking whether that second invocation is intentional. If it is needed, running test_cmake_build twice consecutively in the same GraalPy build directory would be a useful reproducer. Comparing the nested CMake configuration and Python-discovery cache between those invocations might explain why Development.Module is found initially but missing on reconfiguration.

Also note that the full CI only runs if you mark this CI as Ready for review.

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
@arthurscchan
arthurscchan marked this pull request as ready for review November 9, 2026 10:38
@arthurscchan

Copy link
Copy Markdown
Contributor Author

Hi @rwgk, I have analysed the CI problem and agree it is my double invocation. I have removed that and the CI passed. Please do let me know if there are additional problems in your consideration. Thanks again for your time.

@rwgk

rwgk commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

Hi @arthurscchan, I'll try to help a little bit, although I don't know how the fuzzing actually works. (I want to learn on the go.)

I asked codex, roughly, what's the long-term value of this work, and what should we do before triggering the CI again (our CI capacity is very limited). See below for the recommendations. I'll follow that, hoping we get this branch up-to-date and the CI green with a small effort. I'll stop once I achieve that, or if things become more complicated than anticipated.

codex GPT-6.1-Sol ultra

Yes. Pushing a new commit to the ready PR triggers synchronize, so the previously skipped Ubuntu GraalPy jobs will run. Their condition excludes only ready_for_review. Workflow.

I would address a few things before that push.

The long-term value is maintainable fuzz testing: updating the harness alongside pybind11, reproducing findings locally, and retaining useful inputs as regression coverage. That is worth having. The immediate coverage is modest, though: this harness exercises embedding and Python-object wrappers, and OSS-Fuzz already runs it externally. Moving it upstream creates a useful foundation for future binding, conversion, and ownership fuzzers; it does not itself add that broader coverage. Current scope.

Before spending another full CI run, my priorities would be:

  1. Fix replay fidelity. This is essential.

    There are two concrete problems:

    • The standalone decoder uses length selectors, while FuzzedDataProvider uses backslash termination/escaping. The same artifact therefore exercises different operations locally and under OSS-Fuzz.
    • The five stateful reads appear in a single function’s argument list. Their evaluation order is unspecified. Decoder and callback.

    I checked this with a small decoding fixture under GCC 13 and Clang 18. For exec_success, the Python-code field is x = 1 with Clang but ok with GCC. Thus the named success input does not reliably exercise its intended paths.

    The small repair is to use the same actual FuzzedDataProvider implementation in both builds, read fields sequentially into named variables, regenerate the corpus, and correct the README. This is directly relevant to the promised maintenance benefit.

  2. Reuse an existing CI job for replay.

    The dedicated replay job spent approximately six minutes rebuilding and testing the whole suite; the replay step itself took under one second. Job timings.

    I would enable the fuzzer and replay in one existing ordinary Ubuntu CPython job—for example, the Ubuntu 3.10/C++20 configuration—and remove the additional full-suite job. That retains coverage with much less recurring cost.

  3. Make CIFuzz manual-only until the integration catches up.

    Its latest run took about 15½ minutes. It still uses the external harness: it can test pybind11’s headers, but it does not validate this PR’s new upstream harness or CMake target. The companion OSS-Fuzz PR remains open and its proposed recipe still compiles manually.

    Given your capacity constraint, I would retain the workflow as a manual tool, then enable automatic PR runs once the adapter actually consumes the upstream target. This adjusts my earlier recommendation in light of the measured cost and incomplete integration.

Resolve the CMake conflict in the same batch, retaining the stable-ABI option. Defining PYBIND11_BUILD_FUZZERS once would also remove its current three duplicate declarations.

Then build both harness variants and replay the corrected corpus locally before making one push. Broader harness improvements can follow separately; dependable replay and proportionate CI cost are the important prerequisites here.

rwgk added 4 commits November 15, 2026 11:11
— codex GPT-6 on behalf of Ralf —

Retain both CMake options when combining the fuzzer changes with stable-ABI support.

Assisted-by: Codex:GPT-6
— codex GPT-6 on behalf of Ralf —

Share LLVM's decoder and consume fields in a fixed order so replayed artifacts exercise the same operations with GCC and Clang. Reframe the seed corpus and check its successful field mapping before replay. Discover the LLVM header without exposing Clang's compiler-specific standard headers to GCC.

Assisted-by: Codex:GPT-6
— codex GPT-6 on behalf of Ralf —

Run corpus replay in the existing Ubuntu CPython 3.15/C++20 job instead of rebuilding the full suite in a separate job. Keep CIFuzz manual until the OSS-Fuzz adapter consumes the upstream harness.

Assisted-by: Codex:GPT-6
— codex GPT-6 on behalf of Ralf —

Wrap the static interpreter guard so both internal managers are constructed before its enclosing static finishes construction. This keeps the managers alive during interpreter finalization instead of accessing them after static destruction.

Validated corpus replay and decoder checks on Python 3.14 with GIL, Python 3.14 free-threaded, and Python 3.15; checked-in input replay with libFuzzer/AddressSanitizer; complete prek hooks.

Assisted-by: Codex:GPT-6
@rwgk

rwgk commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

— codex GPT-6 on behalf of Ralf —

@arthurscchan This PR now provides a shared decoder for local replay and libFuzzer, corpus replay in the existing Python 3.15 CI job, and documented reproduction steps. Replay also caught an interpreter shutdown issue, which is now fixed (see commit a067c70); that job passes.

I think this is a useful milestone suitable for merging. Could you please review the updated PR and let me know if it looks good to you?

For follow-up work, I’d suggest:

  • Switch OSS-Fuzz to the upstream build target and validate the integration.
  • Enable automatic CIFuzz once it exercises the upstream harness.
  • Gradually add focused binding and conversion coverage, including considering alternatives to arbitrary py::exec.

Those improvements can follow separately.

@rwgk

rwgk commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

@arthurscchan I forgot before:

We have 110 .cpp files; the only .cc files are these three introduced by this PR:

  • tests/fuzz/pybind_fuzzer.cc
  • tests/fuzz/fuzz_replay.cc
  • tests/fuzz/test_fuzz_input.cc

I will rename all three to .cpp for consistency, update the CMake/documentation references and coordinate the filename with the companion OSS-Fuzz change. I'll add another commit and retest.

— codex GPT-6 on behalf of Ralf —

Match the existing C++ filename convention for all three fuzz sources and update the CMake, README, and CIFuzz comment references. Source contents and executable names are unchanged.

Validated fresh CMake corpus replay builds with the Python 3.15 GIL/free-threaded pair, Clang/libFuzzer/AddressSanitizer replay of the checked-in corpus, and complete prek hooks.

Assisted-by: Codex:GPT-6
@rwgk

rwgk commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Hi @arthurscchan, I’ve renamed the three .cc files to .cpp and updated their references (commit 839a73b).

While reviewing the CIFuzz logs, I noticed it still builds the old external harness. It encountered an interpreter shutdown crash matching the lifetime issue already fixed in our upstream copy. CIFuzz reproduced the crash against its baseline and filtered it as pre-existing, so the workflow passed.

Codex suggests resolving this in the companion OSS-Fuzz PR, probably by:

  1. Building the corrected upstream harness, ideally through our shared CMake target.
  2. Updating filename references to .cpp.
  3. Rerunning CIFuzz and checking the logs for a clean shutdown.

Could you please take it from here?

@arthurscchan

Copy link
Copy Markdown
Contributor Author

Ok. give me a day or two to follow up.

@arthurscchan

arthurscchan commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @rwgk, thanks for addressing the issues and the CI work. To be honest, the CIFuzz problem is more of a chicken-and-egg problem. By default, CIFuzz takes the Dockerfile/build.sh from the upstream OSS-Fuzz repository in order to carry out the sample fuzzing of the project. At the same time, however, updating the Dockerfile/build.sh (which I have done in google/oss-fuzz#16274) requires the upstream PR to be merged first before its CI can succeed on the OSS-Fuzz side. This is also why I kept the OSS-Fuzz-side changes as a draft. There is a workaround, though: CIFuzz could provide a CIFuzz-specific Dockerfile and build.sh on the pybind11 side and use those instead of the upstream OSS-Fuzz Dockerfile/build.sh for CIFuzz runs in pybind11. However, that would risk the fuzzing build mismatching the OSS-Fuzz side. For this reason, my usual workflow is to create a draft PR in OSS-Fuzz for the changes, together with a separate PR upstream for the new fuzzers (or fixes to them); once the upstream PR has merged, I rebase the OSS-Fuzz-side PR and merge that as well. Alternatively, for some projects, a dynamic approach is used on the OSS-Fuzz side to pick up all new fuzzer builds automatically, which makes it unnecessary to modify the OSS-Fuzz-side build every time. That said, since we are changing the infrastructure to move the fuzzers upstream, this first time we still need to merge the upstream side first and then rebase the OSS-Fuzz PR to resolve the problem.

What do you think? Please do let me know if you have any other considerations or opinions on this. Thanks a lot for your time on this matter.

@rwgk

rwgk commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

— codex GPT-6 on behalf of Ralf —

pre-commit.ci run

@rwgk rwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

— codex GPT-6 on behalf of Ralf —

Approving based on the following understanding of what this PR establishes, our maintenance responsibilities, and the follow-up work.

pybind11 already participates in Google’s OSS-Fuzz service. This change brings the harness into pybind11’s repository so that we can review, test, and update it alongside the library. The OSS-Fuzz repository continues to hold the service configuration: the container environment, dependencies, build adapter, and project metadata. The intended integration is a thin adapter invoking our native build rules, with the harness maintained upstream.

This gives pybind11 maintainers more direct control over what the harness exercises, while making our responsibility for its quality explicit. We will need to keep it working as pybind11 evolves, make failures reproducible and understandable, triage findings, and retain useful regression coverage. Google provides the hosted fuzzing infrastructure and reporting machinery; we remain responsible for investigating findings and fixing the appropriate code.

I understand the three testing mechanisms as follows:

  • Corpus replay, locally and in ordinary pybind11 CI, executes fixed inputs through the actual callback. This provides an exercised maintenance path for the harness.
  • CIFuzz, in GitHub Actions, runs a short fuzzing campaign using the OSS-Fuzz integration.
  • OSS-Fuzz, on Google’s infrastructure, runs ongoing campaigns using periodically rebuilt binaries, exploring generated inputs beyond our checked-in corpus.

For routine work, changes to pybind11 headers or harness behavior should normally require only a pybind11 PR. After merging, the next scheduled OSS-Fuzz build picks up those changes. When a campaign finds a defect, we can reproduce it, fix it upstream, and add a focused regression test or useful corpus input. OSS-Fuzz can then retest the reported testcase against subsequent builds.

Coordinated changes across both repositories will still be needed when the adapter’s assumptions change—for example, source paths, target names, exported binaries, build options, or dependencies. Adding another fuzzer may also require an OSS-Fuzz change unless the adapter already discovers and exports it automatically. Keeping the adapter generic should reduce this coordination over time.

The immediate follow-up belongs in the companion OSS-Fuzz PR: update it for the .cpp filenames and shared CMake target, complete the migration, and verify that CIFuzz actually builds the upstream harness and shuts down cleanly. A green CIFuzz result alone is insufficient for that verification because pre-existing crashes can be filtered.

CIFuzz remains manual and advisory for now. Automatic PR runs and their frequency can be decided separately, taking our CI capacity into account.

I consider this a useful milestone to merge. Broader coverage can follow incrementally, through focused targets whose coverage, runtime, reproducibility, and diagnostic quality we can assess.

Thanks for the contribution and for working through these integration details!

@rwgk
rwgk merged commit a395471 into pybind:master Nov 24, 2026
94 checks passed
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Nov 24, 2026
@arthurscchan
arthurscchan deleted the fuzz/upstream-harness branch November 25, 2026 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants