xmem: commits into placeholder windows, title page-size bits, window release (RT-XMEM-WINDOWS) #417

Closed
Kokonotcu wants to merge 2 commits from upstream-pr/417 into main
Kokonotcu commented 2026-11-24 04:52:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kokonotcu on 2026-11-24T04:52:19Z.


Claim: RT-XMEM-WINDOWS (#14).

Three ways the title's memory calls fail on the host with the current window model (reserve-only 64 KiB windows kept as placeholders, physical pages mapped into them):

  1. Commit inside a window. A commit without MEM_RESERVE (the measured 0x20401000 shape) into a placeholder range fails with ERROR_INVALID_ADDRESS: the host cannot commit a placeholder. When the host refuses such a commit, each placeholder piece is now replaced by committed memory (VirtualAlloc2 with MEM_REPLACE_PLACEHOLDER), plain reserved or committed pieces are committed in place, and a range with a page mapped or a free piece fails before anything changes. The committed part leaves the window map; the rest of the window still takes physical pages.
  2. Console page-size bits through kernel32!VirtualAlloc. The title's own VirtualAlloc passed MEM_LARGE_PAGES (needs SeLockMemoryPrivilege on the host) and 64 KiB pages (MEM_LARGE_PAGES | MEM_PHYSICAL, invalid on the host) straight through. Both now take XMemVirtualAlloc's path (guest page size as alignment and rounding, 4 KiB host pages); MEM_PHYSICAL alone is dropped.
  3. Releasing a window with pages mapped. Once pages are mapped, a window is many host allocations, so the title's VirtualFree(base, 0, MEM_RELEASE) freed only the first piece and left g_mapped naming views at addresses the host hands out again. VirtualFree is now shimmed next to VirtualAlloc for the title's memory DLLs: a release at a window's base unmaps its pages, releases every piece and forgets the window; every other call passes through.

Tests (xmem_test, new checks): commit into a window, including a piece that had a page mapped and unmapped, and the rest of the window still maps; title VirtualAlloc with each page mode, aligned and writable, and a plain commit into a 64 KiB title reservation; a window with two pages mapped released whole (every 64 KiB free afterwards), taking no pages after, and the same pages mapping into a new window and holding data.

The existing check "writing to 512 of them charges about those 512 pages" reads the system-wide commit charge, so another process allocating or freeing tens of MB in the window fails it; it now measures up to two more windows of untouched pages before failing. The neighbouring "mapping them does not charge them" check has the same weakness and still fails about one run in fifteen while builds run beside it; left as is here.

Tested on Windows 10, i7-11800H, RTX 3080 Laptop (no GPU needed), on main d2cd6c0d9: xmem and d3d12_xmem_placed pass; xmem_test run 15 times, the new checks never failed.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5466305479 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/417). Originally posted by [Kokonotcu](https://github.com/Kokonotcu) on 2026-11-24T04:52:19Z. --- Claim: RT-XMEM-WINDOWS (#14). Three ways the title's memory calls fail on the host with the current window model (reserve-only 64 KiB windows kept as placeholders, physical pages mapped into them): 1. **Commit inside a window.** A commit without `MEM_RESERVE` (the measured `0x20401000` shape) into a placeholder range fails with `ERROR_INVALID_ADDRESS`: the host cannot commit a placeholder. When the host refuses such a commit, each placeholder piece is now replaced by committed memory (`VirtualAlloc2` with `MEM_REPLACE_PLACEHOLDER`), plain reserved or committed pieces are committed in place, and a range with a page mapped or a free piece fails before anything changes. The committed part leaves the window map; the rest of the window still takes physical pages. 2. **Console page-size bits through `kernel32!VirtualAlloc`.** The title's own `VirtualAlloc` passed `MEM_LARGE_PAGES` (needs `SeLockMemoryPrivilege` on the host) and 64 KiB pages (`MEM_LARGE_PAGES | MEM_PHYSICAL`, invalid on the host) straight through. Both now take `XMemVirtualAlloc`'s path (guest page size as alignment and rounding, 4 KiB host pages); `MEM_PHYSICAL` alone is dropped. 3. **Releasing a window with pages mapped.** Once pages are mapped, a window is many host allocations, so the title's `VirtualFree(base, 0, MEM_RELEASE)` freed only the first piece and left `g_mapped` naming views at addresses the host hands out again. `VirtualFree` is now shimmed next to `VirtualAlloc` for the title's memory DLLs: a release at a window's base unmaps its pages, releases every piece and forgets the window; every other call passes through. **Tests (`xmem_test`, new checks):** commit into a window, including a piece that had a page mapped and unmapped, and the rest of the window still maps; title `VirtualAlloc` with each page mode, aligned and writable, and a plain commit into a 64 KiB title reservation; a window with two pages mapped released whole (every 64 KiB free afterwards), taking no pages after, and the same pages mapping into a new window and holding data. The existing check "writing to 512 of them charges about those 512 pages" reads the system-wide commit charge, so another process allocating or freeing tens of MB in the window fails it; it now measures up to two more windows of untouched pages before failing. The neighbouring "mapping them does not charge them" check has the same weakness and still fails about one run in fifteen while builds run beside it; left as is here. Tested on Windows 10, i7-11800H, RTX 3080 Laptop (no GPU needed), on `main` `d2cd6c0d9`: `xmem` and `d3d12_xmem_placed` pass; `xmem_test` run 15 times, the new checks never failed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sodove commented 2026-11-24 07:24:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by sodove on 2026-11-24T07:24:02Z.


Independent Windows 11 26100.7623 check of f2de415223 applied to main d42773d073, MSVC 19.44.35220 Release. Stock xmem and d3d12_xmem_placed both pass.

A release after a partial commit still leaves part of the original XMem reservation behind. I reserved a four-page window, committed one 64 KiB page, verified the page states with VirtualQuery, and called the shims VirtualFree at the original base with size zero and MEM_RELEASE. Each scenario ran in its own synthetic process. Plain Win32 followed the same reserve, partial commit and release sequence.

Committed page Native Win32 pages freed #417 pages freed #417 pages left
First 4/4 1/4 3 reserved
Interior 4/4 1/4 2 reserved, 1 committed
Last 4/4 3/4 1 committed

VirtualFree returned success in every case. The release contract covers the entire original reservation, including mixed committed and reserved pages. Win32 reference

This matches the current bookkeeping. forget_window_range splits or removes the original entry after a commit, and TitleVirtualFree only looks up the fragment starting at the original base. The original reservation extent is needed for release, while the fragments describe where physical pages can still map.

Synthetic process-owned allocations only. AI-assisted verification.

Baseline on unmodified main d42773d: the XMem partial commit failed with ERROR_INVALID_ADDRESS (487) in all three cases. With f2de415 applied to the same main, all three partial commits succeeded, then release reproduced the counts above. The native Win32 control freed 4/4 pages in every case on both builds.

Retest of ba26fe91 on main 5bd50af2, Windows 11 26100.7623 with MSVC Release. Running the updated regression test with only the earlier f2de415 implementation produced exactly the three expected failures (1/4, 1/4, 3/4 pages freed). With the follow-up applied, all three released 4/4, xmem passed all 253 checks, and d3d12_xmem_placed also passed.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7124949760 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/417#issuecomment-7124949760). Originally posted by [sodove](https://github.com/sodove) on 2026-11-24T07:24:02Z. --- Independent Windows 11 26100.7623 check of f2de4152237ae6de3de799fce2a57c58ddc85e59 applied to main d42773d0734d5fd7b3a32a1bfd536f61186322a5, MSVC 19.44.35220 Release. Stock xmem and d3d12_xmem_placed both pass. A release after a partial commit still leaves part of the original XMem reservation behind. I reserved a four-page window, committed one 64 KiB page, verified the page states with VirtualQuery, and called the shims VirtualFree at the original base with size zero and MEM_RELEASE. Each scenario ran in its own synthetic process. Plain Win32 followed the same reserve, partial commit and release sequence. | Committed page | Native Win32 pages freed | #417 pages freed | #417 pages left | | --- | --- | --- | --- | | First | 4/4 | 1/4 | 3 reserved | | Interior | 4/4 | 1/4 | 2 reserved, 1 committed | | Last | 4/4 | 3/4 | 1 committed | VirtualFree returned success in every case. The release contract covers the entire original reservation, including mixed committed and reserved pages. [Win32 reference](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree) This matches the current bookkeeping. forget_window_range splits or removes the original entry after a commit, and TitleVirtualFree only looks up the fragment starting at the original base. The original reservation extent is needed for release, while the fragments describe where physical pages can still map. Synthetic process-owned allocations only. AI-assisted verification. Baseline on unmodified main d42773d: the XMem partial commit failed with ERROR_INVALID_ADDRESS (487) in all three cases. With f2de415 applied to the same main, all three partial commits succeeded, then release reproduced the counts above. The native Win32 control freed 4/4 pages in every case on both builds. Retest of ba26fe91 on main 5bd50af2, Windows 11 26100.7623 with MSVC Release. Running the updated regression test with only the earlier f2de415 implementation produced exactly the three expected failures (1/4, 1/4, 3/4 pages freed). With the follow-up applied, all three released 4/4, xmem passed all 253 checks, and d3d12_xmem_placed also passed.
Kokonotcu commented 2026-11-24 07:33:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kokonotcu on 2026-11-24T07:33:56Z.


Thanks, confirmed and fixed. With your three cases added to xmem_test, the branch before the fix freed 1, 1 and 3 of 4 pages (first, interior and last page committed), exactly as your table shows.

Cause: as you found, the commit cuts the committed range out of g_windows, and TitleVirtualFree looked the release up there.

Fix (latest commit on this branch): each window's extent as reserved is now kept in a separate map. A VirtualFree(base, 0, MEM_RELEASE) at a reserved base unmaps and frees every piece of that extent, committed replacements included, and forgets all its fragments. The fragments still decide where pages can map.

xmem_test now checks the three cases free 4 of 4 pages (253 checks, OK), and d3d12_xmem_placed passes. Tested on Windows 10, RTX 3080 Laptop.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7125128783 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/417#issuecomment-7125128783). Originally posted by [Kokonotcu](https://github.com/Kokonotcu) on 2026-11-24T07:33:56Z. --- Thanks, confirmed and fixed. With your three cases added to `xmem_test`, the branch before the fix freed 1, 1 and 3 of 4 pages (first, interior and last page committed), exactly as your table shows. Cause: as you found, the commit cuts the committed range out of `g_windows`, and `TitleVirtualFree` looked the release up there. Fix (latest commit on this branch): each window's extent as reserved is now kept in a separate map. A `VirtualFree(base, 0, MEM_RELEASE)` at a reserved base unmaps and frees every piece of that extent, committed replacements included, and forgets all its fragments. The fragments still decide where pages can map. `xmem_test` now checks the three cases free 4 of 4 pages (253 checks, OK), and `d3d12_xmem_placed` passes. Tested on Windows 10, RTX 3080 Laptop. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MoonEyes commented 2026-11-27 23:19:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-27T23:19:33Z.


Hi @Kokonotcu, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/shims/xmem.cpp, runtime/test/xmem_test.cpp), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7230052787 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/417#issuecomment-7230052787). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-27T23:19:33Z. --- Hi @Kokonotcu, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/shims/xmem.cpp`, `runtime/test/xmem_test.cpp`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:29:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:29:56Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317706492 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/417#issuecomment-7317706492). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:29:56Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

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!417
No description provided.