Skip to content

PYTHON-6169 Add native ASan-instrumented BSON fuzzing - #3265

Open
blink1073 wants to merge 16 commits into
mongodb:mainfrom
blink1073:PYTHON-6169
Open

blink1073 wants to merge 16 commits into
mongodb:mainfrom
blink1073:PYTHON-6169

Conversation

@blink1073

@blink1073 blink1073 commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

PYTHON-6169

Changes in this PR

Atheris instruments only Python code, so the existing BSON fuzz tasks cannot see memory-safety bugs in the C extensions. This change builds the extensions with ASan and libFuzzer instrumentation and runs the three BSON fuzz targets natively in a daily Evergreen task.

  • Under FUZZ_NATIVE=1 the fuzz targets also decode inputs through a sentinel-free array.array view, since CPython's hidden NUL sentinel masks over-reads from ASan.
  • New build and runner scripts rebuild the extensions with clang ASan, verify the instrumentation, and time-box the targets against the spec seed corpus plus fuzz/corpus/.
  • The fuzz-native task runs both scripts as plain subprocess.exec because the generic run-tests function reinstalls pymongo without sanitizer flags, and runs in the nightly Fuzz RHEL8 variant.

Test Plan

  • just test test/test_fuzz_shared.py: 14 tests covering the input views, native-mode behavior, and replay of every committed reproducer.
  • A patch build of all of the fuzzing tasks.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)? N/A
  • Is there test coverage? N/A
  • Is any followup work tracked in a JIRA ticket? If so, add link(s). N/A

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

@blink1073
blink1073 requested a balanced review from Copilot December 1, 2026 19:45
@blink1073 blink1073 changed the title PYTHON-6169 PYTHON-6169 Add native ASan-instrumented BSON fuzzing Dec 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The editable install causes fuzz targets to fall back to Python after extension cleanup, bypassing the instrumented native code and undermining sanitizer coverage.

4 open issues
What changed in this PR

Adds native AddressSanitizer (ASan) fuzzing for PyMongo’s BSON C extensions, complementing the existing Python-level fuzzing to detect memory-safety errors.

Changes:

  • Introduces instrumented build and time-boxed runner scripts for the three BSON fuzz targets.
  • Adds sentinel-free input buffers and tests for native-mode behavior and regression replay.
  • Schedules daily native fuzzing in Evergreen, moves fuzz variants to larger RHEL8 hosts, and updates documentation.
File Description
test/​test_fuzz_shared.py Tests input views, target behavior, and committed regression replay.
fuzz/​run_native.sh Prepares seeds and runs time-boxed native fuzzing.
fuzz/​README.md Documents native fuzzing requirements and usage.
fuzz/​fuzz_shared.py Adds native-mode input views and defers the Atheris import.
fuzz/​fuzz_is_valid.py Checks decoding across input views while preserving bytes-only validity checks.
fuzz/​fuzz_decode.py Runs decoding comparisons across input views.
fuzz/​fuzz_decode_all.py Runs multi-document decoding comparisons across input views.
fuzz/​build_native.sh Builds and installs instrumented C extensions with validation checks.
AGENTS.md Updates the fuzz-directory description to mention native ASan fuzzing.
.evergreen/​scripts/​test_generate_config.py Tests native task wiring, scheduling, and host selection.
.evergreen/​scripts/​generate_config.py Defines the native task and updates fuzz variants.
.evergreen/​generated_configs/​variants.yml Reflects nightly task additions and larger hosts.
.evergreen/​generated_configs/​tasks.yml Adds generated native build and runner commands.

Review effort: Balanced

Comment thread fuzz/build_native.sh Resolved
Comment thread fuzz/build_native.sh Resolved
Comment thread fuzz/run_native.sh Resolved
Comment thread fuzz/run_native.sh Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The fuzz helper overallocates its input buffer, leaving short over-reads inside addressable memory and preventing ASan from detecting them.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Use an exact-sized copy to expose short over-reads to ASan

fuzz/​fuzz_shared.py:81

Constructing an array from bytes overallocates its backing buffer, so removing the NUL sentinel does not put an ASan redzone immediately after the input. For example, an 8-byte input produces an 11-byte allocation, leaving the short over-reads this helper is intended to expose inside addressable memory even with PYTHONMALLOC=malloc. Yield an exact-sized copy (a full array slice allocates exactly its length), and check allocation size in the helper test so this masking cannot recur.

4 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are identified in the native ASan fuzzing changes.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The script unconditionally deletes existing corpus inputs, and its noncanonical path validation allows the deletion to reach directories outside the repository.

3 open issues

Review effort: Balanced

Comment thread fuzz/run_native.sh Resolved
Comment thread fuzz/run_native.sh Resolved
Comment thread fuzz/run_native.sh Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The documented regression workflow now runs timed mutation tests instead of deterministic replay and no longer includes fuzz/regression/*.

1 open issue
3 resolved since last review

Review effort: Balanced

Comment thread .evergreen/scripts/run_tests.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because an unpublished finding indicates that fuzz regression silently switches to mutation runs, losing deterministic known-bug coverage.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Both native discovery and capture subprocesses omit Evergreen’s FUZZ_MAX_TIME expansion, silently using 300-second per-target budgets instead of configured values.

1 open issue

Review effort: Balanced

Comment thread .evergreen/scripts/generate_config.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because symlink resolution can allow corpus cleanup to delete tracked source directories inside the checkout.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Maintainer confirmation is needed that Evergreen’s external PR webhook still selects fuzz-native-regression on the now-untagged fuzzing-rhel8 variant, since repository tests do not cover webhook selection.

No open issues 🎉

Review effort: Balanced

@blink1073
blink1073 requested a review from sleepyStick December 2, 2026 14:52
@blink1073
blink1073 marked this pull request as ready for review December 2, 2026 14:52
@blink1073
blink1073 requested a review from a team as a code owner December 2, 2026 14:52

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants