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

Open
github-bot wants to merge 2 commits from github-bot/gh-417 into main AGit
Owner

@⁠Kokonotcu on GitHub · 2026-11-24 04:52 UTC · original
Kokonotcu:pr/xmem-window-commit-free → main · +268 −6 · 2 files · 2 commits · open

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

**@⁠Kokonotcu** on GitHub · 2026-11-24 04:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/417) `Kokonotcu:pr/xmem-window-commit-free` → `main` · +268 −6 · 2 files · 2 commits · open 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)
Three ways the title's memory calls fail on the host with the window
model (reserve-only 64 KiB windows as placeholders, physical pages mapped
into them):

- A commit without MEM_RESERVE (the measured 0x20401000) inside a
  placeholder range failed 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 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.
- kernel32!VirtualAlloc from the title passed the console page-size bits
  to the host: MEM_LARGE_PAGES needs SeLockMemoryPrivilege there and
  64 KiB pages (MEM_LARGE_PAGES | MEM_PHYSICAL) are invalid. Both now take
  XMemVirtualAlloc's path (guest page size as alignment and rounding);
  MEM_PHYSICAL alone is dropped.
- Once pages are mapped, a window is many host allocations, so the title's
  VirtualFree(base, 0, MEM_RELEASE) freed only the first and left g_mapped
  naming views at addresses the host hands out again. VirtualFree is now
  shimmed next to VirtualAlloc: a release at a window's base unmaps its
  pages, releases every piece and forgets the window; every other call
  passes through.

xmem_test: commit into a window (including a piece mapped and unmapped
before), the rest 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, free afterwards, the pages
mapping into a new window. The "writing to 512 of them charges about
those 512 pages" check reads the system-wide commit charge; it now
measures up to two more windows of untouched pages before failing (it
failed under parallel builds).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A commit into a physical-page window cuts the committed range out of g_windows (it can no longer take a page), and TitleVirtualFree looked the release up there: a release after a partial commit found only the fragment at the base, or none, and left the rest reserved or committed (1, 1 and 3 of 4 pages freed for a committed first, interior and last page; Win32 frees 4 of 4). Each window's extent as reserved is now kept in g_reservations; a release at a reserved base unmaps and frees every piece of that extent and forgets all its fragments.

xmem_test: the three cases free all 4 pages (1/4, 1/4, 3/4 without the change). xmem and d3d12_xmem_placed pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:59:02 +00:00
Author
Owner

@⁠sodove on GitHub · 2026-11-24 07:24 UTC · original · edited 2026-11-24 07:43 UTC

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.

**@⁠sodove** on GitHub · 2026-11-24 07:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/417#issuecomment-7124949760) · edited 2026-11-24 07:43 UTC 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.
Author
Owner

@⁠Kokonotcu on GitHub · 2026-11-24 07:33 UTC · original

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

**@⁠Kokonotcu** on GitHub · 2026-11-24 07:33 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/417#issuecomment-7125128783) 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)
This pull request has changes conflicting with the target branch.
  • runtime/shims/xmem.cpp
  • runtime/test/xmem_test.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/417/head:github-bot/gh-417'
git switch 'github-bot/gh-417'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-417'
git switch 'github-bot/gh-417'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-417'
git switch 'github-bot/gh-417'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-417'
git switch 'main'
git merge --squash 'github-bot/gh-417'
git switch 'main'
git merge --ff-only 'github-bot/gh-417'
git switch 'main'
git merge 'github-bot/gh-417'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.