Repository navigation
PYTHON-4232 Add Fuzzing Tests #3128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blink1073
wants to merge
12
commits into
mongodb:main
Choose a base branch
from
blink1073:PYTHON-4232
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e5a27f3
PYTHON-4232 Add fuzzing tests
blink1073 cf395ab
PYTHON-4232 Trim fuzz test comments and docstrings
blink1073 9bee1a9
PYTHON-4232 Address review comments
blink1073 9b77f34
Merge branch 'main' of github.com:mongodb/mongo-python-driver into PY…
blink1073 f6878d7
PYTHON-4232 Address review comments
blink1073 9037e7a
PYTHON-4232 Address review comments
blink1073 6d29883
Address Copilot review comments
blink1073 c98927e
Seed raw BSON variants, document atheris platform support
blink1073 3ef3356
Fail is_valid fuzz case on any decode error
blink1073 89e5a4f
Launch fuzz targets with sys.executable
blink1073 318a891
Dedup fuzz helpers, use zipfile, clarify docs
blink1073 19f256d
add helper file
blink1073 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # BSON fuzz tests | ||
|
|
||
| Coverage-guided fuzz tests for the `bson` package, built on | ||
| [atheris](https://github.com/google/atheris) (PYTHON-4232). | ||
|
|
||
| "Coverage-guided" means the fuzzer records which code paths each input | ||
| reaches and steers mutations toward untested code; it does not enumerate | ||
| the API surface. | ||
|
|
||
| Each target runs the C extension and the pure-Python implementation and | ||
| compares their results when both are present. | ||
|
|
||
| ## Fuzz targets | ||
|
|
||
| | File | What it tests | | ||
| |---|---| | ||
| | `fuzz_decode.py` | round-trip `bson.decode()` + `encode()` | | ||
| | `fuzz_decode_all.py` | `bson.decode_all()` + per-document `encode()` | | ||
| | `fuzz_is_valid.py` | `bson.is_valid()` and the invariant that `is_valid() == True` implies `decode()` succeeds | | ||
|
|
||
| ## Running | ||
|
|
||
| Atheris is a `uv` dependency group. Run targets from the repository root: | ||
|
|
||
| ```bash | ||
| uv run --group fuzz python fuzz/fuzz_decode.py fuzz/regression/* | ||
| ``` | ||
|
|
||
| Atheris publishes CPython 3.12 to 3.14 wheels for Linux x86_64 only, so the | ||
| `fuzz` group cannot install on other interpreters or platforms. Pin a | ||
| compatible interpreter where one is available, and run in a Linux x86_64 | ||
| container otherwise: | ||
|
|
||
| ```bash | ||
| uv run --group fuzz --python 3.13 python fuzz/fuzz_decode.py fuzz/regression/* | ||
| ``` | ||
|
|
||
| The Evergreen fuzz tasks pin Linux CPython 3.13 and are unaffected. | ||
|
|
||
| ### Regression inputs (deterministic) | ||
|
|
||
| `regression/` holds reproducers for known bugs. Running the targets | ||
| over them runs on Evergreen as the `fuzz-regression` sub-test on every PR: | ||
|
|
||
| ```bash | ||
| just setup-tests fuzz regression | ||
| just run-tests | ||
| ``` | ||
|
|
||
| Or without the justfile: | ||
|
|
||
| ```bash | ||
| uv run --group fuzz python fuzz/fuzz_decode.py fuzz/regression/* -atheris_runs=3 | ||
| uv run --group fuzz python fuzz/fuzz_decode_all.py fuzz/regression/* -atheris_runs=3 | ||
| uv run --group fuzz python fuzz/fuzz_is_valid.py fuzz/regression/* -atheris_runs=3 | ||
| ``` | ||
|
|
||
| When a target finds a bug, create a new ticket, add the reproducer as a new | ||
| file in `fuzz/regression/`, and fix the bug in the same PR. | ||
|
|
||
| ### Seed corpus (discovery fuzzing) | ||
|
|
||
| The seed corpus is generated at build time from the vendored BSON spec corpus | ||
| (`../test/bson_corpus`), mirroring the Go driver's build-oss-fuzz-corpus: | ||
|
|
||
| ```bash | ||
| uv run python fuzz/build_corpus.py /tmp/fuzz-seed.zip --data-dir test/bson_corpus | ||
| uv run python -m zipfile -e /tmp/fuzz-seed.zip /tmp/fuzz-corpus | ||
| ``` | ||
|
|
||
| The builder also synthesizes seeds the spec corpus lacks: boundary-value | ||
| documents and a ~500 KB stress document. Without a large seed, libFuzzer's | ||
| default mutation cap of 4096 bytes would keep the nightly away from | ||
| long-string and many-field code paths. | ||
|
|
||
| Run the targets against the corpus. Time-boxed runs suit CI; this runs on | ||
| Evergreen as the `fuzz` sub-test, daily: | ||
|
|
||
| ```bash | ||
| just setup-tests fuzz fuzz | ||
| FUZZ_MAX_TIME=300 just run-tests | ||
| ``` | ||
|
|
||
| Or without the justfile: | ||
|
|
||
| ```bash | ||
| uv run --group fuzz python fuzz/fuzz_decode.py /tmp/fuzz-corpus -max_total_time=60 | ||
| ``` | ||
|
|
||
| Pass any [libFuzzer flags](https://llvm.org/docs/LibFuzzer.html#options) | ||
| after the corpus directory. The `Failed to find function __sanitizer_*` | ||
| startup warnings are benign libFuzzer symbol probes on uninstrumented runs; | ||
| native ASan-instrumented fuzzing is tracked separately (PYTHON-6169). | ||
|
|
||
| ## Coverage report | ||
|
|
||
| Measure which lines the seed corpus reaches: | ||
|
|
||
| ```bash | ||
| uv run --group fuzz --group coverage python -m coverage run fuzz/fuzz_decode.py /tmp/fuzz-corpus/* \ | ||
| -atheris_runs=$(( $(ls /tmp/fuzz-corpus | wc -l) )) | ||
| uv run --group coverage python -m coverage html | ||
| ``` | ||
|
|
||
| The run count equals the file count: replaying individual files (not a corpus | ||
| directory) performs no empty-input warmup, and letting the budget reach zero | ||
| gives atheris a graceful exit so coverage.py can flush its report. | ||
|
|
||
| ## Files | ||
|
|
||
| ``` | ||
| build_corpus.py Seed corpus builder (mirrors the Go driver's build-oss-fuzz-corpus) | ||
| fuzz_decode.py Fuzz target: bson.decode() | ||
| fuzz_decode_all.py Fuzz target: bson.decode_all() | ||
| fuzz_is_valid.py Fuzz target: bson.is_valid() | ||
| fuzz_shared.py Code shared by the fuzz targets | ||
| regression/ Known bug reproducers (no corpus setup required) | ||
| ``` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "coverage-guided" mean? Does this mean that the fuzz tests try to cover the full surface of the API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added an explainer