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
base: main
Are you sure you want to change the base?
Changes from all commits
e5a27f3
cf395ab
9bee1a9
9b77f34
f6878d7
9037e7a
6d29883
c98927e
3ef3356
89e5a4f
318a891
19f256d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,6 +134,40 @@ def handle_aws_lambda() -> None: | |
| run_command(f"bash {DRIVERS_TOOLS}/.evergreen/aws_lambda/{script_name}", env=env) | ||
|
|
||
|
|
||
| def handle_fuzz() -> None: | ||
| """Run the BSON fuzz targets (PYTHON-4232). Not pytest-based.""" | ||
| targets = ["fuzz_decode", "fuzz_decode_all", "fuzz_is_valid"] | ||
|
|
||
| if SUB_TEST_NAME == "regression": | ||
| # Skip dotfiles (pathlib globbing matches them). | ||
| inputs = sorted( | ||
| str(p) for p in (ROOT / "fuzz" / "regression").glob("*") if not p.name.startswith(".") | ||
| ) | ||
| if not inputs: | ||
| raise RuntimeError("No fuzz regression inputs found in fuzz/regression/") | ||
| # Each reproducer is passed as its own file argument, so libFuzzer | ||
| # runs each exactly once; the -atheris_runs budget equals the file | ||
| # count so atheris exits cleanly after the last one. | ||
| runs = len(inputs) | ||
| for target in targets: | ||
| run_command( | ||
| f"{sys.executable} fuzz/{target}.py {' '.join(inputs)} -atheris_runs={runs}" | ||
| ) | ||
| return | ||
|
|
||
| # Time-boxed fuzzing against the generated seed corpus. | ||
| corpus_dir = os.environ.get("FUZZ_CORPUS_DIR", str(ROOT / "fuzz-scratch" / "corpus")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it excessive to add a check here in case the corpus is empty or malformed on a host? |
||
| # Keep libFuzzer artifacts (crash-*, leak-*, ...) under the scratch dir. | ||
| artifact_prefix = f"{ROOT}/fuzz-scratch/" | ||
| max_time = os.environ.get("FUZZ_MAX_TIME", "300") | ||
| for target in targets: | ||
| run_command( | ||
| f"{sys.executable} fuzz/{target}.py {corpus_dir} " | ||
| f"-artifact_prefix={artifact_prefix} " | ||
| f"-max_total_time={max_time} -max_len=1048576 -print_final_stats=1" | ||
| ) | ||
|
|
||
|
|
||
| def run() -> None: | ||
| # Add diagnostic for python version. | ||
| print("Running with python", sys.version) | ||
|
|
@@ -193,6 +227,10 @@ def run() -> None: | |
| handle_aws_lambda() | ||
| return | ||
|
|
||
| if TEST_NAME == "fuzz": | ||
| handle_fuzz() | ||
| return | ||
|
|
||
| if os.environ.get("DEBUG_LOG"): | ||
| TEST_ARGS.extend(f"-o log_cli_level={logging.DEBUG}".split()) | ||
|
|
||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an explainer |
||
| [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) | ||
| ``` | ||
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.
Do we need
-artifact_prefixhere as well?