xmem: guard address-space overflow and BTreeMap::range panics across FFI #678

Open
MasterWolf wants to merge 1 commit from MasterWolf/Darner:pr/post-merge-xmem-overflow-guards into post-merge
First-time contributor

Not run in the game (verified via cargo test, cargo clippy, and cargo build on 8aece83; overflow and range-bound guards covered by unit tests).

Contributor role (optional)

  • Fluxer ID: 1577824307575267331

What it changes

  • The problem on post-merge: read from crates/darner-hle/src/modules/xmem.rs (28a9b42):
    1. XMemMapPhysicalPages never checks n * PAGE or base + len before passing (base, len) to shown_already, reach, joins, and unmap. In release builds, wrapping base + len produces base + 1 > end in physical.views.range(base + 1..end) (unmap) and base > end in Placeholders::forget, which panics unconditionally inside BTreeMap::range across extern "system" fn (aborting the process).
    2. XMemMapPhysicalPagesScatter compares address(i + run) as usize == address(i) as usize + run * PAGE without checked arithmetic, and mapped_pages / reach / joins / Physical::wanted have unchecked at + size / from..to range arithmetic.

This PR:

  • Guards n.checked_mul(PAGE) and base.checked_add(len) at entry to XMemMapPhysicalPages (returning null_mut() with a warning on overflow, keeping n == 0 returning address).
  • Uses saturating_add / checked_add and guarded range(start..end) (start < end) in reach, unmap, Placeholders::forget, Placeholders::split, mapped_pages, shown_already, joins, and Physical::wanted.
  • Uses checked_mul + checked_add (next_page_address) in XMemMapPhysicalPagesScatter.
  • Adds 5 unit tests covering overflowing count/range, usize::MAX boundary reach/unmap/placeholders, wrapped scatter coalescing, and inverted mapped_pages ranges.

How it was tested

  • Tested revision: 8aece83 (on post-merge 28a9b42)
  • Commands and results:
    • cargo test --offline -p darner-hle --lib modules::xmem — 5 passed; 0 failed (0.00s)
    • cargo clippy --offline -p darner-hle --tests and --features diagnostics --tests --no-deps — passed (0 warnings in darner-hle)
    • cargo build --offline -p darner — passed (12.09s)
  • Changes a default: no
  • A/B result: not applicable (bounds-check and FFI panic-safety fix).
  • Checks run on that revision: cargo build, cargo test, cargo clippy
  • Gameplay, when relevant: not run in the game
  • Hardware dependence: likely general
  • Coverage gaps: verified with unit tests rather than an in-game launch.

Checklist

  • I ran this change in the game, or say below why it can't be (not run in the game; verified via 5 new unit tests, cargo clippy, and cargo build).
  • This PR contains one change based on post-merge.
  • The diff contains no game files, code or assets.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found
  • Replaces or overlaps: none found
**Not run in the game** (verified via `cargo test`, `cargo clippy`, and `cargo build` on `8aece83`; overflow and range-bound guards covered by unit tests). ## Contributor role (optional) - Fluxer ID: 1577824307575267331 ## What it changes - The problem on `post-merge`: read from `crates/darner-hle/src/modules/xmem.rs` (`28a9b42`): 1. `XMemMapPhysicalPages` never checks `n * PAGE` or `base + len` before passing `(base, len)` to `shown_already`, `reach`, `joins`, and `unmap`. In release builds, wrapping `base + len` produces `base + 1 > end` in `physical.views.range(base + 1..end)` (`unmap`) and `base > end` in `Placeholders::forget`, which panics unconditionally inside `BTreeMap::range` across `extern "system" fn` (aborting the process). 2. `XMemMapPhysicalPagesScatter` compares `address(i + run) as usize == address(i) as usize + run * PAGE` without checked arithmetic, and `mapped_pages` / `reach` / `joins` / `Physical::wanted` have unchecked `at + size` / `from..to` range arithmetic. This PR: - Guards `n.checked_mul(PAGE)` and `base.checked_add(len)` at entry to `XMemMapPhysicalPages` (returning `null_mut()` with a warning on overflow, keeping `n == 0` returning `address`). - Uses `saturating_add` / `checked_add` and guarded `range(start..end)` (`start < end`) in `reach`, `unmap`, `Placeholders::forget`, `Placeholders::split`, `mapped_pages`, `shown_already`, `joins`, and `Physical::wanted`. - Uses `checked_mul` + `checked_add` (`next_page_address`) in `XMemMapPhysicalPagesScatter`. - Adds 5 unit tests covering overflowing count/range, `usize::MAX` boundary `reach`/`unmap`/placeholders, wrapped scatter coalescing, and inverted `mapped_pages` ranges. ## How it was tested - Tested revision: `8aece83` (on `post-merge` `28a9b42`) - Commands and results: - `cargo test --offline -p darner-hle --lib modules::xmem` — `5 passed; 0 failed` (`0.00s`) - `cargo clippy --offline -p darner-hle --tests` and `--features diagnostics --tests --no-deps` — passed (`0` warnings in `darner-hle`) - `cargo build --offline -p darner` — passed (`12.09s`) - Changes a default: no - A/B result: not applicable (bounds-check and FFI panic-safety fix). - Checks run on that revision: `cargo build`, `cargo test`, `cargo clippy` - Gameplay, when relevant: not run in the game - Hardware dependence: likely general - Coverage gaps: verified with unit tests rather than an in-game launch. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (not run in the game; verified via 5 new unit tests, `cargo clippy`, and `cargo build`). - [x] This PR contains one change based on `post-merge`. - [x] The diff contains no game files, code or assets. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found - Replaces or overlaps: none found
TrustNo1 changed title from [pr-fix][memory] xmem: guard address-space overflow and BTreeMap::range panics across FFI to xmem: guard address-space overflow and BTreeMap::range panics across FFI 2026-12-06 03:25:12 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'pr/post-merge-xmem-overflow-guards:MasterWolf-pr/post-merge-xmem-overflow-guards'
git switch 'MasterWolf-pr/post-merge-xmem-overflow-guards'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'post-merge'
git merge --no-ff 'MasterWolf-pr/post-merge-xmem-overflow-guards'
git switch 'MasterWolf-pr/post-merge-xmem-overflow-guards'
git rebase 'post-merge'
git switch 'post-merge'
git merge --ff-only 'MasterWolf-pr/post-merge-xmem-overflow-guards'
git switch 'MasterWolf-pr/post-merge-xmem-overflow-guards'
git rebase 'post-merge'
git switch 'post-merge'
git merge --no-ff 'MasterWolf-pr/post-merge-xmem-overflow-guards'
git switch 'post-merge'
git merge --squash 'MasterWolf-pr/post-merge-xmem-overflow-guards'
git switch 'post-merge'
git merge --ff-only 'MasterWolf-pr/post-merge-xmem-overflow-guards'
git switch 'post-merge'
git merge 'MasterWolf-pr/post-merge-xmem-overflow-guards'
git push origin 'post-merge'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!678
No description provided.