hle: a page count past the title's pages is refused, not allocated #576

Open
qnjk wants to merge 1 commit from qnjk/Darner:xmem-page-count-vec into Main
Contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: two xmem entry points copy a title-supplied page list into a Vec sized straight from the title's count, before anything looks at a page:

    // XMemFreePhysicalPages, xmem.rs:1996 on main
    let numbers = unsafe { std::slice::from_raw_parts(pages, n) }.to_vec();
    // map_pages, xmem.rs:2616 on main
    let pages = (!pages.is_null()).then(|| unsafe { std::slice::from_raw_parts(pages, n) }.to_vec());
    

    Nothing bounds n. PR #411 (merged) bounds n * PAGE and base + len in map_pages, which still lets n reach 2^48, and XMemFreePhysicalPages had no check at all. One garbage count asks for terabytes: the refused allocation is handle_alloc_error, an abort that the run's crash filters do not see, where the console's runtime would have failed the call.

  • This change adds MOST_PAGES (a page count no real title asks for), refuses a count past it in both entry points — the free keeps the same "nothing freed" answer it already gives, map_pages returns the null it already returns for an unknown page — and says so with a warning. The comment in map_pages records why the count is checked separately from the byte arithmetic.

How it was tested

  • Tested revision: 625eca3 (one commit on c8258f9).
  • Commands and results:
    • cargo test --locked -p darner-hle --lib — 511 passed, 0 failed, 15 ignored.
    • Two new tests (xmem.rs): a_mapping_page_count_past_the_title_pages_is_refused and a_free_page_count_past_the_title_pages_is_ignored. They take real physical pages from XMemAllocatePhysicalPages (no adapter, no title) and pass n values past the bound.
    • Fail-before, with the two guards temporarily removed (file copies, no git stash):
      cargo test --locked -p darner-hle --lib modules::xmem::tests::a_mapping_page_count_past_the_title_pages_is_refused
      memory allocation of 140737488355328 bytes failed
      error: test failed ... exit code 0xc0000409, STATUS_STACK_BUFFER_OVERRUN
      
      i.e. the test process is aborted by the allocation the guard now prevents.
    • cargo clippy --locked -p darner-hle --all-targets -- -D warnings passes.
  • Changes a default: no. A count a real title asks for is accepted exactly as before.
  • A/B result: 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 run to completion here; the pre-existing darner-gpu test deadlock (#224) and darner-shader waterfall fixture failure (#227) make the whole-workspace run non-green on this machine. darner-hle's suite is green (511 passed).
  • Gameplay, when relevant: no gameplay run. No title on this machine passes a page count past the bound; the tests supply it.
  • Shader wave sizes tested, when relevant: not applicable.
  • Hardware dependence: none.
  • Coverage gaps: the bound is a page count, chosen above what any real mapping needs; the scatter entry points and the two remaining unchecked adds the earlier finding listed (alias_held's overlap test, alias_constant's to + k * PAGE) are untouched — one change per commit.

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 reaches this path with a bad count; the evidence is the tests, which abort the test process on main.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md. (cargo xtask policy: 576 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 and #337 change crates/darner-hle/src/modules/xmem.rs (view guards / LAZY map / overflow aliases, and dstorage destinations written through their sections); neither mentions the page count or XMemFreePhysicalPages, and this change is confined to the free guard and map_pages' first lines.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: two xmem entry points copy a title-supplied page list into a `Vec` sized straight from the title's count, before anything looks at a page: ```rust // XMemFreePhysicalPages, xmem.rs:1996 on main let numbers = unsafe { std::slice::from_raw_parts(pages, n) }.to_vec(); // map_pages, xmem.rs:2616 on main let pages = (!pages.is_null()).then(|| unsafe { std::slice::from_raw_parts(pages, n) }.to_vec()); ``` Nothing bounds `n`. PR #411 (merged) bounds `n * PAGE` and `base + len` in `map_pages`, which still lets `n` reach 2^48, and `XMemFreePhysicalPages` had no check at all. One garbage count asks for terabytes: the refused allocation is `handle_alloc_error`, an abort that the run's crash filters do not see, where the console's runtime would have failed the call. - This change adds `MOST_PAGES` (a page count no real title asks for), refuses a count past it in both entry points — the free keeps the same "nothing freed" answer it already gives, `map_pages` returns the null it already returns for an unknown page — and says so with a warning. The comment in `map_pages` records why the count is checked separately from the byte arithmetic. ## How it was tested - Tested revision: `625eca3` (one commit on `c8258f9`). - Commands and results: - `cargo test --locked -p darner-hle --lib` — 511 passed, 0 failed, 15 ignored. - Two new tests (`xmem.rs`): `a_mapping_page_count_past_the_title_pages_is_refused` and `a_free_page_count_past_the_title_pages_is_ignored`. They take real physical pages from `XMemAllocatePhysicalPages` (no adapter, no title) and pass `n` values past the bound. - Fail-before, with the two guards temporarily removed (file copies, no `git stash`): ``` cargo test --locked -p darner-hle --lib modules::xmem::tests::a_mapping_page_count_past_the_title_pages_is_refused memory allocation of 140737488355328 bytes failed error: test failed ... exit code 0xc0000409, STATUS_STACK_BUFFER_OVERRUN ``` i.e. the test process is aborted by the allocation the guard now prevents. - `cargo clippy --locked -p darner-hle --all-targets -- -D warnings` passes. - Changes a default: no. A count a real title asks for is accepted exactly as before. - A/B result: 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 run to completion here; the pre-existing `darner-gpu` test deadlock (#224) and `darner-shader` waterfall fixture failure (#227) make the whole-workspace run non-green on this machine. `darner-hle`'s suite is green (511 passed). - Gameplay, when relevant: no gameplay run. No title on this machine passes a page count past the bound; the tests supply it. - Shader wave sizes tested, when relevant: not applicable. - Hardware dependence: none. - Coverage gaps: the bound is a page count, chosen above what any real mapping needs; the scatter entry points and the two remaining unchecked adds the earlier finding listed (`alias_held`'s overlap test, `alias_constant`'s `to + k * PAGE`) are untouched — one change per commit. ## 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 reaches this path with a bad count; the evidence is the tests, which abort the test process on `main`. - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. (`cargo xtask policy`: 576 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 and #337 change `crates/darner-hle/src/modules/xmem.rs` (view guards / LAZY map / overflow aliases, and dstorage destinations written through their sections); neither mentions the page count or `XMemFreePhysicalPages`, and this change is confined to the free guard and `map_pages`' first lines. - Replaces or overlaps: none found
`XMemFreePhysicalPages` copies the page-number list the title gives into a
`Vec` sized from its count (`std::slice::from_raw_parts(pages, n).to_vec()`),
and `map_pages` copies the `n` pages a page mapping names the same way, both
before a single page is looked at. The count is the title's own and nothing
bounded it: a count of garbage asks the allocator for a list no call can
mean, and a failed allocation calls `handle_alloc_error`, which aborts the
process instead of failing the call that carried it. `map_pages` got the byte
sum it adds bounded in #411; the count that sizes this copy first did not.

Both now refuse a count past the pages the title can hold (`MOST_PAGES`, the
window up to `TITLE_TOP_BITS` a page each) as an unknown page is already
refused: null from `map_pages`, false from the free, with a warning saying
why and nothing touched, so the books keep the pages the call named.

The tests ask for 2^44 page numbers — 128 TiB of them, far past anything the
title can hold — and then for one past `MOST_PAGES` itself. With the old copy
they abort the test binary (`memory allocation of 140737488355328 bytes
failed`, then `STATUS_STACK_BUFFER_OVERRUN`); with the bound they are
refused, and the page each test allocated still maps.
TrustNo1 changed title from [pr-fix][memory] hle: a page count past the title's pages is refused, not allocated to hle: a page count past the title's pages is refused, not allocated 2026-12-06 03:26:57 +00:00
Owner

Test needed — commit 625eca34; what to check:

  • The problem on main: two xmem entry points copy a title-supplied page list into a Vec sized straight from the title's count, before anything looks at a page:

    // XMemFreePhysicalPages, xmem.rs:1996 on main
    let numbers = unsafe { std::slice::from_raw_parts(pages, n) }.to_vec();
    // map_pages, xmem.rs:2616 on main
    let pages = (!pages.is_null()).then(|| unsafe { std::slice::from_raw_parts(pages, n) }.to_vec());
    

    Nothing bounds n. PR #411 (merged) bounds n * PAGE and base + len in map_pages, which still lets n reach 2^48, and XMemFreePhysicalPages had no check at all. One garbage count asks for terabytes: the refused allocation is handle_alloc_error, an abort that the run's crash filters do not see, where the console's runtime would have failed the call.

  • This change adds MOST_PAGES (a page count no real title asks for), refuses a count past it in both entry points — the free keeps the same "nothing freed" answer it already gives, map_pages returns the null it already returns for an unknown page — and says so with a warning. The comment in map_pages records why the count is checked separately from the byte arithmetic.

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `625eca34`; what to check: - The problem on main: two xmem entry points copy a title-supplied page list into a `Vec` sized straight from the title's count, before anything looks at a page: ```rust // XMemFreePhysicalPages, xmem.rs:1996 on main let numbers = unsafe { std::slice::from_raw_parts(pages, n) }.to_vec(); // map_pages, xmem.rs:2616 on main let pages = (!pages.is_null()).then(|| unsafe { std::slice::from_raw_parts(pages, n) }.to_vec()); ``` Nothing bounds `n`. PR #411 (merged) bounds `n * PAGE` and `base + len` in `map_pages`, which still lets `n` reach 2^48, and `XMemFreePhysicalPages` had no check at all. One garbage count asks for terabytes: the refused allocation is `handle_alloc_error`, an abort that the run's crash filters do not see, where the console's runtime would have failed the call. - This change adds `MOST_PAGES` (a page count no real title asks for), refuses a count past it in both entry points — the free keeps the same "nothing freed" answer it already gives, `map_pages` returns the null it already returns for an unknown page — and says so with a warning. The comment in `map_pages` records why the count is checked separately from the byte arithmetic. Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
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-page-count-vec:qnjk-xmem-page-count-vec'
git switch 'qnjk-xmem-page-count-vec'

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-page-count-vec'
git switch 'qnjk-xmem-page-count-vec'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'qnjk-xmem-page-count-vec'
git switch 'qnjk-xmem-page-count-vec'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'qnjk-xmem-page-count-vec'
git switch 'Main'
git merge --squash 'qnjk-xmem-page-count-vec'
git switch 'Main'
git merge --ff-only 'qnjk-xmem-page-count-vec'
git switch 'Main'
git merge 'qnjk-xmem-page-count-vec'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
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!576
No description provided.