Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .evergreen/generated_configs/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ tasks:
UV_PYTHON: "3.13"
tags: [fuzz, fuzz-regression, pr]
- name: fuzz
commands:
- func: run tests
vars:
TEST_NAME: fuzz
SUB_TEST_NAME: fuzz
UV_PYTHON: "3.13"
tags: [fuzz, fuzz-nightly]
- name: fuzz-capture
commands:
- func: run tests
vars:
Expand All @@ -234,7 +242,7 @@ tasks:
- AWS_SECRET_ACCESS_KEY
- AWS_SESSION_TOKEN
type: test
tags: [fuzz, fuzz-nightly]
tags: [fuzz, fuzz-weekly]

# Getdata tests
- name: getdata
Expand Down
7 changes: 7 additions & 0 deletions .evergreen/generated_configs/variants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ buildvariants:
run_on:
- rhel8.10-small
batchtime: 1440
- name: fuzz-capture-rhel8
tasks:
- name: .fuzz-weekly
display_name: Fuzz Capture RHEL8
run_on:
- rhel8.10-small
batchtime: 10080

# Green framework tests
- name: green-gevent-rhel8
Expand Down
5 changes: 3 additions & 2 deletions .evergreen/scripts/capture-fuzz-corpus.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash
# PYTHON-6178: capture coverage-increasing inputs from a fuzz run and open
# an automated PR growing fuzz/corpus/. Runs as the last command of the
# nightly "fuzz" task, while the scratch corpus exists; manual
# `evergreen patch --variant fuzz-rhel8 --task fuzz` runs capture too.
# weekly "fuzz-capture" task, while the scratch corpus exists; manual
# `evergreen patch --variant fuzz-capture-rhel8 --task fuzz-capture` runs
# capture too.
set -eu

# Same environment as just.sh: venv on PATH for a modern Python.
Expand Down
31 changes: 23 additions & 8 deletions .evergreen/scripts/generate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,13 @@ def create_doctests_variants():


def create_fuzz_variants():
# Two variants for the two fuzz flows: the regression reproducers run
# on every PR, the time-boxed fuzzing runs nightly. The nightly variant
# carries no "pr" tag, so PR patch builds never select it, while its
# default patchability keeps it manually runnable
# (`evergreen patch --variant fuzz-rhel8 --task fuzz`). The host needs
# CPython, which the rhel8 toolchain provides.
# Three variants for the three fuzz flows: the regression reproducers run
# on every PR, the time-boxed fuzzing for crash discovery runs nightly,
# and the weekly variant also captures the corpus (opens the automated
# [Fuzz Corpus] PR). The non-pr variants carry no "pr" tag, so PR patch
# builds never select them, while their default patchability keeps them
# manually runnable (`evergreen patch --variant fuzz-rhel8 --task fuzz`).
# The host needs CPython, which the rhel8 toolchain provides.
host = HOSTS["rhel8"]
return [
create_variant(
Expand All @@ -403,6 +404,12 @@ def create_fuzz_variants():
create_variant(
[".fuzz-nightly"], get_variant_name("Fuzz", host), host=host, batchtime=BATCHTIME_DAY
),
create_variant(
[".fuzz-weekly"],
get_variant_name("Fuzz Capture", host),
host=host,
batchtime=BATCHTIME_WEEK,
),
]


Expand Down Expand Up @@ -722,10 +729,18 @@ def create_fuzz_tasks():
test_func = FunctionCall(func="run tests", vars=test_vars)
tasks = [EvgTask(name="fuzz-regression", tags=tags, commands=[test_func])]

# Time-boxed fuzzing against the generated seed corpus.
# Time-boxed fuzzing against the generated seed corpus, for crash
# discovery; corpus capture moved to the weekly fuzz-capture task.
tags = ["fuzz", "fuzz-nightly"]
test_vars = dict(TEST_NAME="fuzz", SUB_TEST_NAME="fuzz", UV_PYTHON="3.13")
test_func = FunctionCall(func="run tests", vars=test_vars)
tasks.append(EvgTask(name="fuzz", tags=tags, commands=[test_func]))

# The weekly capture task fuzzes too (the capture reads the task-local
# fuzz-scratch/ corpus, so it must stay attached to a fuzzing run).
tags = ["fuzz", "fuzz-weekly"]
test_vars = dict(TEST_NAME="fuzz", SUB_TEST_NAME="fuzz", UV_PYTHON="3.13")
test_func = FunctionCall(func="run tests", vars=test_vars)
# PYTHON-6178: while the scratch corpus still exists, capture the new
# coverage-increasing inputs, replay them through every target so a
# unit that fails one is never published, and open an automated PR
Expand All @@ -743,7 +758,7 @@ def create_fuzz_tasks():
"AWS_SESSION_TOKEN",
],
)
tasks.append(EvgTask(name="fuzz", tags=tags, commands=[test_func, capture_cmd]))
tasks.append(EvgTask(name="fuzz-capture", tags=tags, commands=[test_func, capture_cmd]))
return tasks


Expand Down
7 changes: 4 additions & 3 deletions .evergreen/scripts/setup_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,10 @@ def handle_test_env() -> None:
zip_path.unlink()
if sub_test_name == "fuzz":
# PYTHON-6178 Also seed from the committed corpus of previously
# captured inputs, so the nightly builds on prior discoveries
# instead of the spec seeds alone. Regression runs (PRs) keep
# running only the reproducers and the spec seed corpus.
# captured inputs, so the nightly and weekly runs build on prior
# discoveries instead of the spec seeds alone. Regression runs
# (PRs) keep running only the reproducers and the spec seed
# corpus.
committed_dir = ROOT / "fuzz" / "corpus"
if committed_dir.is_dir():
for path in sorted(committed_dir.iterdir()):
Expand Down
29 changes: 20 additions & 9 deletions .evergreen/scripts/test_generate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,32 +443,43 @@ def test_non_standard_cov_tasks_have_no_cov_companions():
def test_fuzz_task_wiring():
"""Test the fuzz task wiring.
Two fuzz variants: the pr-tagged regression variant runs the reproducers on
every PR; the nightly variant (which also captures the corpus) carries no
"pr" tag, so PR patch builds never select it, while its default patchability
keeps it manually runnable (`evergreen patch --variant fuzz-rhel8 --task fuzz`).
Three fuzz flows: the pr-tagged regression variant runs the reproducers
on every PR; the nightly variant fuzzes for crash discovery; the weekly
variant also captures the corpus. The non-pr variants carry no "pr" tag,
so PR patch builds never select them, while their default patchability
keeps them manually runnable
(`evergreen patch --variant fuzz-rhel8 --task fuzz`).
"""
tasks = [task for task in all_tasks() if "fuzz" in (task.tags or [])]
assert [task.name for task in tasks if "pr" in (task.tags or [])] == ["fuzz-regression"]
assert [task.name for task in tasks if "pr" not in (task.tags or [])] == ["fuzz"]
assert [task.name for task in tasks if "pr" not in (task.tags or [])] == [
"fuzz",
"fuzz-capture",
]
variants = generate_config.create_fuzz_variants()
assert len(variants) == 2
regression, nightly = variants
assert len(variants) == 3
regression, nightly, capture_variant = variants
assert variant_selects(regression, tasks[0])
assert "pr" in (regression.tags or [])
assert variant_selects(nightly, tasks[1])
assert nightly.batchtime == generate_config_utils.BATCHTIME_DAY
assert variant_selects(capture_variant, tasks[2])
assert capture_variant.batchtime == generate_config_utils.BATCHTIME_WEEK
# Webhook PR patch builds only select pr-tagged variants.
assert not nightly.tags
assert not capture_variant.tags
# The nightly fuzz task is crash discovery only: its last command is
# the "run tests" FunctionCall, with no capture command.
assert tasks[1].commands[-1].func == "run tests"
# The capture subprocess bootstraps the comment-bot github app, whose
# secrets live in AWS Secrets Manager: it needs the assumed-role
# credentials (as resync_specs and backport-pr also pass them).
capture = tasks[1].commands[-1]
capture = tasks[2].commands[-1]
env_vars = capture.params["include_expansions_in_env"]
for var in ("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"):
assert var in env_vars, f"capture subprocess missing {var}"
# The capture no longer keys off is_patch: manual patch runs must
# capture too, and the nightly variant never runs in a PR build.
# capture too, and the weekly variant never runs in a PR build.
assert "is_patch" not in env_vars


Expand Down
11 changes: 6 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,14 @@ wheels (CPython 3.12-3.14).

- Regression test (runs on every PR):
`just setup-tests fuzz regression` then `just run-tests`.
- Time-boxed fuzzing (runs daily on Evergreen): `just setup-tests fuzz fuzz`
then `just run-tests`. Set `FUZZ_MAX_TIME` (seconds, default 300) to change
the per-target budget.
- Nightly fuzzing captures coverage-increasing inputs into `fuzz/corpus/`
- Time-boxed fuzzing for crash discovery (runs daily on Evergreen):
`just setup-tests fuzz fuzz` then `just run-tests`. Set `FUZZ_MAX_TIME`
(seconds, default 300) to change the per-target budget.
- Weekly capture (runs weekly on Evergreen): the `fuzz-capture` task also
fuzzes, then captures coverage-increasing inputs into `fuzz/corpus/`
via an automated PR (PYTHON-6178); see [fuzz/README.md](fuzz/README.md).
To run that flow on demand, patch the task:
`evergreen patch --variant fuzz-rhel8 --task fuzz`.
`evergreen patch --variant fuzz-capture-rhel8 --task fuzz-capture`.

## Testing time-dependent operations

Expand Down
22 changes: 12 additions & 10 deletions fuzz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ 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:
Evergreen as the `fuzz` sub-test, daily. The nightly `fuzz` task is crash
discovery only; the weekly `fuzz-capture` task fuzzes and captures the
corpus (below):

```bash
just setup-tests fuzz fuzz
Expand All @@ -95,15 +97,15 @@ 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).

### Captured corpus (nightly)
### Captured corpus (weekly)

The nightly run seeds from the spec seed corpus plus every unit in
The weekly capture run seeds from the spec seed corpus plus every unit in
`corpus/`, and libFuzzer writes each coverage-increasing ("interesting")
input it discovers back into its corpus directory. After the targets
finish, the `fuzz` task captures those new units, replays them through
every target once so a unit that fails any target is never published (it
stays in `fuzz-scratch/` for triage), and opens an automated
"[Fuzz Corpus]" PR against main (PYTHON-6178). Later nights
finish, the `fuzz-capture` task captures those new units, replays them
through every target once so a unit that fails any target is never
published (it stays in `fuzz-scratch/` for triage), and opens an automated
"[Fuzz Corpus]" PR against main (PYTHON-6178). Later weeks
build on prior discoveries instead of restarting from the spec seeds.

Growth is bounded by per-unit and per-run caps, overridable on Evergreen:
Expand All @@ -115,7 +117,7 @@ Growth is bounded by per-unit and per-run caps, overridable on Evergreen:
Units over a cap are discarded, not deferred: they are never persisted, so
the fuzzer may re-derive them on later nights. A run that finds nothing new
opens no PR. The capture deduplicates against `main` only, so while a
`[Fuzz Corpus]` PR is open, later nights can re-capture its still-pending
`[Fuzz Corpus]` PR is open, later weeks can re-capture its still-pending
units and the PRs overlap until one merges (digest-named files keep that
conflict-free). Crashing inputs are excluded:
they land in `fuzz-scratch/` as `crash-*` artifacts and follow the
Expand All @@ -141,11 +143,11 @@ gives atheris a graceful exit so coverage.py can flush its report.

```
build_corpus.py Seed corpus builder (mirrors the Go driver's build-oss-fuzz-corpus)
capture_corpus.py Nightly capture of new coverage-increasing inputs (PYTHON-6178)
capture_corpus.py Weekly capture of new coverage-increasing inputs (PYTHON-6178)
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
corpus/ Captured coverage-increasing inputs; grown by automated nightly PRs
corpus/ Captured coverage-increasing inputs; grown by automated weekly PRs
regression/ Known bug reproducers; run with the seed corpus on PRs
```
2 changes: 1 addition & 1 deletion fuzz/capture_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
units into an output directory named by their SHA-256 digests, and writes
a Markdown summary for the automated corpus PR body.
Pure stdlib: this runs on the Evergreen nightly host after the fuzz
Pure stdlib: this runs on the Evergreen weekly capture host after the fuzz
targets finish, and its unit tests must run everywhere (no atheris, no
bson).
Expand Down
Loading