xmem: mapping or unmapping no pages changes nothing instead of aborting #167

Manually merged
github merged 1 commit from gh-pr/167/head into gh-pr/167/base 2026-11-26 03:53:03 +00:00
Owner

@⁠Maraco91 on GitHub · 2026-11-24 01:02 UTC · original
Maraco91:xmem/zero-length-unmap → main · +48 −0 · 1 files · 1 commits · merged 2026-11-24 07:53 UTC

What

XMemMapPhysicalPages(address, 0, pages) (with or without pages) and the GPU's page-mapping copy of a zero-length range (map_alias(to, from, 0), reached from CopyPageMappingsBatchX) abort the process once any physical-page view exists.

Both call unmap(physical, base, 0). There end == base, so the search for views starting inside the range is views.range(base + 1..base): a range that runs backwards, which BTreeMap::range panics on ("range start is greater than range end in BTreeMap") whenever the map is not empty. The panic is inside an extern "system" function, so it cannot unwind and the process ends (0xc0000409). Even without the panic the result would be wrong: the view holding base passes the at + size > base test, so it would be unmapped and mapped again as two views split at base.

unmap now returns at once for a length of 0: no bytes, no view to give back. Nothing else changes; a count of 1 and up takes the same path as before.

I have not seen a title make this call (I have no game files); this is from reading the code. It is a legal argument for both entry points and the cost of the wrong answer is the whole process.

Tested

Windows 11 Pro 10.0.26200, rustc 1.98.1 (x86_64-pc-windows-msvc), no game data, no GPU involved.

New unit test mapping_no_pages_changes_nothing: maps two pages as one view, then calls XMemMapPhysicalPages (with pages and with null) and map_alias (with a source and with 0) with a length of 0 at the start of the view, inside it and just past it; checks the view is still one view of two pages, still committed, with its contents. Control: a count of 1 afterwards still unmaps exactly that page and leaves its neighbour mapped.

  • main (9aef3d9) + the test only: cargo test -p victory-hle --lib mapping_no_pages aborts: panicked at ...\btree\search.rs:121:21: range start is greater than range end in BTreeMap, then thread caused non-unwinding panic. aborting., exit code 0xc0000409.
  • with the fix: cargo test -p victory-hle --lib xmem: 10 passed, 0 failed (the 9 existing xmem tests and the new one).
  • rustfmt --check on xmem.rs: clean. cargo clippy -p victory-hle --tests: nothing new (one existing field_reassign_with_default warning in commit).
  • cargo xtask policy --staged: ok.

Not tested: a title run.

Touches only unmap; #76 rewrites map and does not overlap.

AI-assisted (Claude Code); I ran the tests above myself on the machine described.

🤖 Generated with Claude Code

**@⁠Maraco91** on GitHub · 2026-11-24 01:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/167) `Maraco91:xmem/zero-length-unmap` → `main` · +48 −0 · 1 files · 1 commits · merged 2026-11-24 07:53 UTC ## What `XMemMapPhysicalPages(address, 0, pages)` (with or without `pages`) and the GPU's page-mapping copy of a zero-length range (`map_alias(to, from, 0)`, reached from `CopyPageMappingsBatchX`) abort the process once any physical-page view exists. Both call `unmap(physical, base, 0)`. There `end == base`, so the search for views starting inside the range is `views.range(base + 1..base)`: a range that runs backwards, which `BTreeMap::range` panics on ("range start is greater than range end in BTreeMap") whenever the map is not empty. The panic is inside an `extern "system"` function, so it cannot unwind and the process ends (`0xc0000409`). Even without the panic the result would be wrong: the view holding `base` passes the `at + size > base` test, so it would be unmapped and mapped again as two views split at `base`. `unmap` now returns at once for a length of 0: no bytes, no view to give back. Nothing else changes; a count of 1 and up takes the same path as before. I have not seen a title make this call (I have no game files); this is from reading the code. It is a legal argument for both entry points and the cost of the wrong answer is the whole process. ## Tested Windows 11 Pro 10.0.26200, rustc 1.98.1 (`x86_64-pc-windows-msvc`), no game data, no GPU involved. New unit test `mapping_no_pages_changes_nothing`: maps two pages as one view, then calls `XMemMapPhysicalPages` (with pages and with null) and `map_alias` (with a source and with 0) with a length of 0 at the start of the view, inside it and just past it; checks the view is still one view of two pages, still committed, with its contents. Control: a count of 1 afterwards still unmaps exactly that page and leaves its neighbour mapped. - `main` (9aef3d9) + the test only: `cargo test -p victory-hle --lib mapping_no_pages` **aborts**: `panicked at ...\btree\search.rs:121:21: range start is greater than range end in BTreeMap`, then `thread caused non-unwinding panic. aborting.`, exit code `0xc0000409`. - with the fix: `cargo test -p victory-hle --lib xmem`: **10 passed, 0 failed** (the 9 existing xmem tests and the new one). - `rustfmt --check` on `xmem.rs`: clean. `cargo clippy -p victory-hle --tests`: nothing new (one existing `field_reassign_with_default` warning in `commit`). - `cargo xtask policy --staged`: ok. Not tested: a title run. Touches only `unmap`; #76 rewrites `map` and does not overlap. AI-assisted (Claude Code); I ran the tests above myself on the machine described. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
XMemMapPhysicalPages with a count of 0, and the GPU's page-mapping copy
of a zero-length range, went into unmap with an empty range. Its search
for views starting inside asked the view map for a range that runs
backwards, which BTreeMap panics on once any view exists; in an
extern "system" function that aborts the process. Without that panic
the view holding the address would have been unmapped and mapped again
as two.

unmap now returns at once for a length of 0.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:53:02 +00:00
github manually merged commit 02fcf72407 into gh-pr/167/base 2026-11-26 03:53:03 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:24 UTC · original

Tested 02fcf72 under Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle and a native Microsoft d3dcompiler_47: pass.

  • victory_hle: 86 passed, 1 failed — the failure is modules::net::tests::only_loopback_is_reachable (Wine lacks WSAConnectByList, as in #118), not this PR. The test this PR adds passes.

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/167#issuecomment-7119062558) Tested `02fcf72` under **Wine 11.22 + vkd3d-proton 3.1** on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with `cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle` and a native Microsoft `d3dcompiler_47`: **pass**. - `victory_hle`: **86 passed, 1 failed** — the failure is `modules::net::tests::only_loopback_is_reachable` (Wine lacks `WSAConnectByList`, as in #118), not this PR. The test this PR adds passes. No game data used.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
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
github/Marvisak--VIctory!167
No description provided.