[pr-fix][memory] hle: a mapping past the top of the address space is refused, not aborted on #411

Open
qnjk wants to merge 1 commit from qnjk/Darner:xmem-unmap-overflow into main
First-time contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: xmem adds two numbers the title supplies and then uses the sum as a map range.

    // unmap, xmem.rs:3806 before this change
    let end = base + len;
    physical.views.range(base + 1..end)      // :3817 before this change
    

    map_pages passes XMemMapPhysicalPages's address and page count, and the page-mapping copies pass their destination and length. A range at the top of the address space wraps that sum; BTreeMap::range then panics on a range whose start is above its end, and the panic leaves an extern "system" function, so the process aborts — the test binary dies with thread caused non-unwinding panic. aborting. and STATUS_STACK_BUFFER_OVERRUN (exit 0xc0000409). The comment above the search already says BTreeMap panics on this shape, but the guard after it only covered len == 0. map_pages has the same shape one step earlier: the page count times the page size can wrap before anything is checked.

  • This change bounds both: map_pages checks n.checked_mul(PAGE) and base.checked_add(len) and answers a range that does not fit with the null it already returns for an unknown page (so the title sees a failed call), and unmap checks its own sum and returns instead of adding, so no caller — the page-mapping copies included — can hand BTreeMap::range a backwards range. Ranges that fit behave exactly as before, and len == 0 still returns first.

How it was tested

  • Tested revision: 41e339c (one commit on f3840d7).
  • Commands and results:
    • cargo test --locked -p darner-hle --lib — 494 passed, 0 failed, 15 ignored, with the new test in the same run.
    • New test modules::xmem::tests::a_mapping_past_the_top_of_the_address_space_is_refused (xmem.rs:5743): it takes two physical pages from XMemAllocatePhysicalPages (no adapter and no title, like the neighbouring physical_pages_map_alias_and_unmap), asks XMemMapPhysicalPages for one page at the last page of the address space (usize::MAX & !(PAGE - 1), so address plus page wraps) and asserts null, then asserts XMemMapPhysicalPagesScatter of that page is 0, and frees the pages.
    • Fail-before, with only the old sums put back and the test kept:
      thread '...a_mapping_past_the_top_of_the_address_space_is_refused' panicked at
      crates\darner-hle\src\modules\xmem.rs:3806:15:
      attempt to add with overflow
      ...
      thread caused non-unwinding panic. aborting.
      error: test failed, to rerun pass `-p darner-hle --lib`
      Caused by:
        process didn't exit successfully: ... (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)
      
      So the test pins the abort, not merely a wrong answer.
    • cargo clippy --locked -p darner-hle --all-targets -- -D warnings passes.
  • Changes a default: no. A mapping that fits is unaffected; one whose end does not fit in a usize is now refused instead of aborting the process.
  • A/B result, for a change of a default (on for everyone) or a speed claim: not applicable — no default changes and no speed claim.
  • Gates run on that revision:
    • Gate 1, cargo clippy --workspace --all-targets --locked -- -D warnings: passes.
    • Gate 2, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passes.
    • Gate 3, cargo test --workspace --locked: not green on this machine, and not because of this change. Three pre-existing failures, each covered by an open pull request: darner-gpu stands still in parallel on the test deadlock #224 describes, and serially finishes 1772 passed and 1 failed (pipeline::make::tests::a_capped_draw_waits_once_then_drops, which passes on its own) beside the order-dependent flakes of #150; darner-shader fails emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders, whose fixture #227 fixes. darner-hle's own suite is green.
  • Gameplay, when relevant: no gameplay run. No title on this machine maps a page past the top of the address space; the defect is reachable only from a bad range, which the test supplies.
  • Shader wave sizes tested, when relevant: not applicable.
  • Hardware dependence: none.
  • Coverage gaps: the same unchecked add shape survives in two overlap tests (alias_held's to < from + len && from < to + len, xmem.rs:2810, and alias_constant's at = to + k * PAGE, :3124) and in map_pages' slice::from_raw_parts(pages, n) when pages is non-null. unmap can no longer panic for such a range, but those two can still overflow in a debug build before reaching it; they are a separate change. A page count that sizes a Vec before validation (:2943) is also deliberately left out.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
    • Not run in gameplay: no title here asks for a range past the top of the address space, so a run cannot reach it. The evidence is the test, which aborts the test process on main and passes here.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md. (cargo xtask policy: 555 files ok.)
  • 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: #219 (batches view guards, shards the LAZY map, pre-arms overflow aliases), #334 (a scatter's unmapped pages leave the RT tables) and #337 (dstorage destinations written through their sections) all change crates/darner-hle/src/modules/xmem.rs. None of their bodies mentions unmap, a bound on the sum, or a range past the top of the address space. The change is in map_pages and unmap only; conflicts, if any, would be in map_pages' first lines.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: `xmem` adds two numbers the title supplies and then uses the sum as a map range. ```rust // unmap, xmem.rs:3806 before this change let end = base + len; physical.views.range(base + 1..end) // :3817 before this change ``` `map_pages` passes `XMemMapPhysicalPages`'s address and page count, and the page-mapping copies pass their destination and length. A range at the top of the address space wraps that sum; `BTreeMap::range` then panics on a range whose start is above its end, and the panic leaves an `extern "system"` function, so the process aborts — the test binary dies with `thread caused non-unwinding panic. aborting.` and `STATUS_STACK_BUFFER_OVERRUN` (exit `0xc0000409`). The comment above the search already says `BTreeMap` panics on this shape, but the guard after it only covered `len == 0`. `map_pages` has the same shape one step earlier: the page count times the page size can wrap before anything is checked. - This change bounds both: `map_pages` checks `n.checked_mul(PAGE)` and `base.checked_add(len)` and answers a range that does not fit with the null it already returns for an unknown page (so the title sees a failed call), and `unmap` checks its own sum and returns instead of adding, so no caller — the page-mapping copies included — can hand `BTreeMap::range` a backwards range. Ranges that fit behave exactly as before, and `len == 0` still returns first. ## How it was tested - Tested revision: `41e339c` (one commit on `f3840d7`). - Commands and results: - `cargo test --locked -p darner-hle --lib` — 494 passed, 0 failed, 15 ignored, with the new test in the same run. - New test `modules::xmem::tests::a_mapping_past_the_top_of_the_address_space_is_refused` (`xmem.rs:5743`): it takes two physical pages from `XMemAllocatePhysicalPages` (no adapter and no title, like the neighbouring `physical_pages_map_alias_and_unmap`), asks `XMemMapPhysicalPages` for one page at the last page of the address space (`usize::MAX & !(PAGE - 1)`, so address plus page wraps) and asserts null, then asserts `XMemMapPhysicalPagesScatter` of that page is `0`, and frees the pages. - Fail-before, with only the old sums put back and the test kept: ``` thread '...a_mapping_past_the_top_of_the_address_space_is_refused' panicked at crates\darner-hle\src\modules\xmem.rs:3806:15: attempt to add with overflow ... thread caused non-unwinding panic. aborting. error: test failed, to rerun pass `-p darner-hle --lib` Caused by: process didn't exit successfully: ... (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN) ``` So the test pins the abort, not merely a wrong answer. - `cargo clippy --locked -p darner-hle --all-targets -- -D warnings` passes. - Changes a default: no. A mapping that fits is unaffected; one whose end does not fit in a `usize` is now refused instead of aborting the process. - A/B result, for a change of a default (on for everyone) or a speed claim: not applicable — no default changes and no speed claim. - Gates run on that revision: - Gate 1, `cargo clippy --workspace --all-targets --locked -- -D warnings`: passes. - Gate 2, `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passes. - Gate 3, `cargo test --workspace --locked`: **not green on this machine, and not because of this change.** Three pre-existing failures, each covered by an open pull request: `darner-gpu` stands still in parallel on the test deadlock #224 describes, and serially finishes 1772 passed and 1 failed (`pipeline::make::tests::a_capped_draw_waits_once_then_drops`, which passes on its own) beside the order-dependent flakes of #150; `darner-shader` fails `emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders`, whose fixture #227 fixes. `darner-hle`'s own suite is green. - Gameplay, when relevant: no gameplay run. No title on this machine maps a page past the top of the address space; the defect is reachable only from a bad range, which the test supplies. - Shader wave sizes tested, when relevant: not applicable. - Hardware dependence: none. - Coverage gaps: the same unchecked add shape survives in two overlap tests (`alias_held`'s `to < from + len && from < to + len`, `xmem.rs:2810`, and `alias_constant`'s `at = to + k * PAGE`, `:3124`) and in `map_pages`' `slice::from_raw_parts(pages, n)` when `pages` is non-null. `unmap` can no longer panic for such a range, but those two can still overflow in a debug build before reaching it; they are a separate change. A page count that sizes a `Vec` before validation (`:2943`) is also deliberately left out. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - Not run in gameplay: no title here asks for a range past the top of the address space, so a run cannot reach it. The evidence is the test, which aborts the test process on `main` and passes here. - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. (`cargo xtask policy`: 555 files ok.) - [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: #219 (batches view guards, shards the LAZY map, pre-arms overflow aliases), #334 (a scatter's unmapped pages leave the RT tables) and #337 (dstorage destinations written through their sections) all change `crates/darner-hle/src/modules/xmem.rs`. None of their bodies mentions `unmap`, a bound on the sum, or a range past the top of the address space. The change is in `map_pages` and `unmap` only; conflicts, if any, would be in `map_pages`' first lines. - Replaces or overlaps: none found
`unmap` computed the end of the range it is given as `base + len`, and
both numbers are the title's own: `XMemMapPhysicalPages` passes the
address and the page count of its own call, and the page-mapping copies
pass their destination and length. A range at the top of the address
space wraps that sum, and the view search that follows —
`BTreeMap::range(base + 1..end)` — then panics on a range whose start is
above its end. The panic is thrown out of `extern "system" fn
XMemMapPhysicalPages`, which aborts the process instead of unwinding. The
comment above the search says the author knew `BTreeMap` panics on this
shape, but the guard that followed only covered `len == 0`.

`map_pages` now bounds the page count times the page and that sum with
`checked_mul` and `checked_add` before anything is touched, and returns
the null it returns for an unknown page, with a warning saying why, so
the title sees a failed call rather than the process dying. `unmap`
bounds its own sum as well, so no other caller — the page-mapping copies
included — can hand `BTreeMap::range` a backwards range.

The test asks for a page at the last page of the address space. With the
old sum it aborts the test binary (`attempt to add with overflow`, then a
non-unwinding panic and `STATUS_STACK_BUFFER_OVERRUN`); with the bound it
returns null and the call is refused.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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 'xmem-unmap-overflow:qnjk-xmem-unmap-overflow'
git switch 'qnjk-xmem-unmap-overflow'

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 'main'
git merge --no-ff 'qnjk-xmem-unmap-overflow'
git switch 'qnjk-xmem-unmap-overflow'
git rebase 'main'
git switch 'main'
git merge --ff-only 'qnjk-xmem-unmap-overflow'
git switch 'qnjk-xmem-unmap-overflow'
git rebase 'main'
git switch 'main'
git merge --no-ff 'qnjk-xmem-unmap-overflow'
git switch 'main'
git merge --squash 'qnjk-xmem-unmap-overflow'
git switch 'main'
git merge --ff-only 'qnjk-xmem-unmap-overflow'
git switch 'main'
git merge 'qnjk-xmem-unmap-overflow'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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!411
No description provided.