d3d12-hle: arrange forgets the slice a page leaves for an unowned one (D3D12-HLE-XMEM-ARRANGE-STALE-OWNER) #2836

Merged
Kal0s-design merged 1 commit from upstream-pr/2836 into main 2026-12-01 08:55:45 +00:00
Kal0s-design commented 2026-12-01 05:11:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-01T05:11:59Z.


Claimed in the coordination issue as: D3D12-HLE-XMEM-ARRANGE-STALE-OWNER (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7277198683)

What and why

arrange (d3d12/crates/leonida-hle/src/modules/xmem.rs, renderer port 1: the 32-bit heap's slices put in address order) swaps physical pages between slices of one section. When a page moves to a slice that no page has, the owner map still recorded the slice it left as that page's. A later page of the same call moving into that slice then found the first page there and swapped it back, so the first page lost the place it had just been given.

The change: in that case the slice the page leaves is removed from owner (owner.remove(&have)), so it has no owner, as is now true.

This ports ahmetbagci's fix from the sibling VIctory tree (TaynanGT/Vice-Tay fddf8c7, GPL-3.0), credited as co-author.

Tested

  • New test modules::xmem::tests::arrange_forgets_the_slice_a_page_left_for_an_unowned_one: four pages, slice 1 freed, then pages[2] moves to the free slice 1 and pages[3] into slice 2, which pages[2] just left. On main (10bed9ff2) it fails (pages[2] ends back at slice 3); with the change both pages are where they were sent.
  • cargo test -p leonida-hle --lib: 241 passed, 0 failed, 2 ignored. The existing the_32_bit_heap_gets_its_slices_in_address_order is unchanged and passes.
  • cargo clippy -p leonida-hle --lib --tests: no warning in the changed lines.
  • Windows only, unit tests; the title wasn't run. #2681 and #2569 touch other parts of xmem.rs, not arrange.

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5579502853 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2836). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-01T05:11:59Z. --- Claimed in the coordination issue as: D3D12-HLE-XMEM-ARRANGE-STALE-OWNER (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7277198683) ### What and why `arrange` (`d3d12/crates/leonida-hle/src/modules/xmem.rs`, renderer port 1: the 32-bit heap's slices put in address order) swaps physical pages between slices of one section. When a page moves to a slice that no page has, the `owner` map still recorded the slice it left as that page's. A later page of the same call moving into that slice then found the first page there and swapped it back, so the first page lost the place it had just been given. The change: in that case the slice the page leaves is removed from `owner` (`owner.remove(&have)`), so it has no owner, as is now true. This ports ahmetbagci's fix from the sibling VIctory tree (TaynanGT/Vice-Tay `fddf8c7`, GPL-3.0), credited as co-author. ### Tested - New test `modules::xmem::tests::arrange_forgets_the_slice_a_page_left_for_an_unowned_one`: four pages, slice 1 freed, then `pages[2]` moves to the free slice 1 and `pages[3]` into slice 2, which `pages[2]` just left. **On `main` (`10bed9ff2`) it fails** (`pages[2]` ends back at slice 3); with the change both pages are where they were sent. - `cargo test -p leonida-hle --lib`: 241 passed, 0 failed, 2 ignored. The existing `the_32_bit_heap_gets_its_slices_in_address_order` is unchanged and passes. - `cargo clippy -p leonida-hle --lib --tests`: no warning in the changed lines. - Windows only, unit tests; the title wasn't run. #2681 and #2569 touch other parts of `xmem.rs`, not `arrange`. GPUs tested: n/a (CPU unit tests) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
fiestorra commented 2026-12-01 05:37:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-01T05:37:59Z.


Verified on Windows: the crate is green with it.

main 640b0d0df, cherry-picked (one commit, merge-base 10bed9ff2), cargo 1.98.1:

cargo test -p leonida-hle
  exit 0
  test result: ok. 1 passed;    0 failed; 0 ignored
  test result: ok. 241 passed;  0 failed; 2 ignored
  test result: ok. 1 passed;    0 failed; 0 ignored
  test result: ok. 0 passed;    0 failed; 0 ignored

That is the crate's four targets and nothing fails. What I did not do is break arrange to watch a specific test fail, so this is an execution check and not a mutation one - xmem.rs gains 23 lines and the suite is green either way.

What I could not test: what the page's slice being forgotten does to a title's memory, which needs the game. The unit tests are what I ran.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7301974903 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2836#issuecomment-7301974903). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-01T05:37:59Z. --- **Verified on Windows: the crate is green with it.** **`main` `640b0d0df`, cherry-picked (one commit, `merge-base` `10bed9ff2`), `cargo 1.98.1`:** ``` cargo test -p leonida-hle exit 0 test result: ok. 1 passed; 0 failed; 0 ignored test result: ok. 241 passed; 0 failed; 2 ignored test result: ok. 1 passed; 0 failed; 0 ignored test result: ok. 0 passed; 0 failed; 0 ignored ``` **That is the crate's four targets and nothing fails.** **What I did not do is break `arrange` to watch a specific test fail**, **so this is an execution check and not a mutation one** - **`xmem.rs` gains 23 lines and the suite is green either way.** **What I could not test**: **what the page's slice being forgotten does to a title's memory**, **which needs the game.** **The unit tests are what I ran.** **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
KelboLive commented 2026-12-01 05:58:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T05:58:10Z.

Review: COMMENTED


The fix is right and the mutation proves it: owner is rebuilt per call as slice→page (xmem.rs:1266-1271), and when want has no page the page leaving have leaves it empty, so the map must forget have (:1290-1293) — otherwise the next page moved into have finds the departed page as its owner and swaps it back. Removing the new else reproduces exactly that ([Some(3), Some(2)] against [Some(1), Some(2)]), while the head is 241 passed / 0 failed / 2 ignored.

The ownership reasoning holds: the map stays a bijection over the section's occupied slices (each move removes one key and inserts one), no slice is allocated or freed, early returns (:1242-1263) happen before any mutation, and the map is local so nothing persists across calls. I could not construct a double-free or leak path; the only way to corrupt it would be duplicate slice indices in table.pages, which this code does not produce (the old bug permuted pages, it did not duplicate them).

Two test nits:

  • The closing XMemFreePhysicalPages(...) == 1 assertions cannot fail for ownership — that function returns 1 unless the pointer is null and silently drops numbers not in the table — so they are cleanup, not evidence. Asserting the pages are still in page_table() before freeing would make the cleanup meaningful; the index assertion is the real check.
  • The test pins the unowned→unowned chain; the owned-destination swap is left to the older the_32_bit_heap_gets_its_slices_in_address_order, which still passes. That is adequate coverage, just worth knowing where each half lives.
<!-- greyfinch-sync:ubiquitarianism/leonida:review:6125917025 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2836#pullrequestreview-6125917025). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T05:58:10Z. **Review:** COMMENTED --- The fix is right and the mutation proves it: `owner` is rebuilt per call as slice→page (`xmem.rs:1266-1271`), and when `want` has no page the page leaving `have` leaves it empty, so the map must forget `have` (`:1290-1293`) — otherwise the next page moved into `have` finds the departed page as its owner and swaps it back. Removing the new `else` reproduces exactly that (`[Some(3), Some(2)]` against `[Some(1), Some(2)]`), while the head is 241 passed / 0 failed / 2 ignored. The ownership reasoning holds: the map stays a bijection over the section's occupied slices (each move removes one key and inserts one), no slice is allocated or freed, early returns (`:1242-1263`) happen before any mutation, and the map is local so nothing persists across calls. I could not construct a double-free or leak path; the only way to corrupt it would be duplicate slice indices in `table.pages`, which this code does not produce (the old bug permuted pages, it did not duplicate them). Two test nits: - The closing `XMemFreePhysicalPages(...) == 1` assertions cannot fail for ownership — that function returns `1` unless the pointer is null and silently drops numbers not in the table — so they are cleanup, not evidence. Asserting the pages are still in `page_table()` before freeing would make the cleanup meaningful; the index assertion is the real check. - The test pins the unowned→unowned chain; the owned-destination swap is left to the older `the_32_bit_heap_gets_its_slices_in_address_order`, which still passes. That is adequate coverage, just worth knowing where each half lives.
MoonEyes commented 2026-12-01 08:33:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T08:33:25Z.


RUN - VERIFY-2836-RTX4060 - by MoonEyes (another person than the author) on head 3b5e702f8, with #2836, #2835, #2695, #2696, #2689, #2699 and #2705 merged together into main 9372a4d4f (clean).

  • Machine: RTX 4060 8 GB, Windows 11, diag profile, features diagnostics,mimalloc, Series S, BAR window off, texture budget 1024 MB.
  • In-game, no scripted action, 480 s asked, a session that loads into the world and stays: main 9372a4d4f and then main + the seven, one run each, same save: 0 device lost, 0 panics, 0 exceptions in both, about 490 s in the world each, alive at the time limit. Frame rate median 17.8 with the seven and 15.0 on main, not a comparison: other work ran on the PC during the main run.
  • Picture: screenshots every 60 s. The last four show the same scene in both runs (water, sky, the boat, the map) and look the same; the first four are cutscenes at different moments and were only looked at for broken textures (none seen). No screenshot is posted.
  • Tests on main 9372a4d4f + the seven (Windows): leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: two source_hash_tests failed on a stale build script of mine (a target folder shared with an older branch) and pass after a rebuild of it (3 of 3).

Limits: one card, one run per arm, two arms only, the seven together. Values only, no game data.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7305474244 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2836#issuecomment-7305474244). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-01T08:33:25Z. --- RUN - VERIFY-2836-RTX4060 - by MoonEyes (another person than the author) on head `3b5e702f8`, with #2836, #2835, #2695, #2696, #2689, #2699 and #2705 merged together into `main` `9372a4d4f` (clean). - **Machine:** RTX 4060 8 GB, Windows 11, diag profile, features diagnostics,mimalloc, Series S, BAR window off, texture budget 1024 MB. - **In-game, no scripted action, 480 s asked, a session that loads into the world and stays:** `main` `9372a4d4f` and then `main` + the seven, one run each, same save: **0 device lost, 0 panics, 0 exceptions** in both, about 490 s in the world each, alive at the time limit. Frame rate median 17.8 with the seven and 15.0 on `main`, **not a comparison**: other work ran on the PC during the `main` run. - **Picture:** screenshots every 60 s. The last four show the same scene in both runs (water, sky, the boat, the map) and look the same; the first four are cutscenes at different moments and were only looked at for broken textures (none seen). No screenshot is posted. - **Tests on `main` `9372a4d4f` + the seven (Windows):** leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: two `source_hash_tests` failed on a stale build script of mine (a target folder shared with an older branch) and pass after a rebuild of it (3 of 3). Limits: one card, one run per arm, two arms only, the seven together. Values only, no game data. AI-assisted (Claude Code).
MoonEyes commented 2026-12-01 08:55:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T08:55:42Z.


Merging. Head 3b5e702f8 merges cleanly with main ad7c62edb and does not force a rebase of #2566. CI green on this head and on main. In-game run on the RTX 4060 posted above (this head, with the six others, about 490 s in the world, 0 device lost, pictures compared with main). Tests on main + the seven: leonida-hle, leonida-gpu, leonida-formats, leonida, leonida-shader, 0 failed. Verified on it by fiestorra (Windows) and KelboLive (mutation). Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7305973093 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2836#issuecomment-7305973093). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-01T08:55:42Z. --- Merging. Head `3b5e702f8` merges cleanly with `main` `ad7c62edb` and does not force a rebase of #2566. CI green on this head and on main. In-game run on the RTX 4060 posted above (this head, with the six others, about 490 s in the world, 0 device lost, pictures compared with main). Tests on main + the seven: leonida-hle, leonida-gpu, leonida-formats, leonida, leonida-shader, 0 failed. Verified on it by fiestorra (Windows) and KelboLive (mutation). Merged by MoonEyes. AI-assisted (Claude Code).
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
ProjectLeonida/mandy!2836
No description provided.