Skip to content

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

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/xmem-arrange-stale-owner
Dec 1, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/xmem-arrange-stale-owner

Conversation

@Kal0s-design

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-HLE-XMEM-ARRANGE-STALE-OWNER (#2570 (comment))

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. D3D12-CLIPPY-NON-GPU-CRATES: fix all clippy warnings across non-gpu d3d12 crates #2681 and runtime, d3d12: memory leak fixes from one sweep (RT-LEAK-SWEEP) #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

In xmem's arrange (renderer port 1, the 32-bit heap's slices in address
order), a page moving to a slice that no page has left its own slice
still recorded as its own in the owner map. 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
slice it leaves now has no owner.

Ported from ahmetbagci's fix in Vice-Tay (fddf8c7).

Co-Authored-By: ahmetbagci <6625856+ahmetbagci@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fiestorra

Copy link
Copy Markdown
Contributor

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 KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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).

@MoonEyes
MoonEyes merged commit 76746d3 into ProjectLeonida:main Dec 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants