xmem: VirtualFree(MEM_DECOMMIT) keeps physical-page views, zeroes pool backing #126

Manually merged
github merged 1 commit from github-bot/gh-126 into main 2026-11-26 18:09:26 +00:00 AGit
Owner

@⁠Olima34 on GitHub · 2026-11-26 17:39 UTC · original
Olima34:fix/xmem-decommit-pool-backed-zeroes → main · +150 −3 · 4 files · 1 commits · merged 2026-11-26 18:09 UTC

Second of the two xmem PRs you asked for on #15 (the first is #124). Small and tested, like #108.

Bug. VirtualFree(MEM_DECOMMIT) over a range that shows physical-page views called a plain decommit. That put fresh anonymous pages in place of the pool's at the title address, while Physical.views and the GPU's pool table still showed the pool there. After a recommit the title wrote the anonymous pages, and the GPU and any alias read the pool's old bytes. That is the same class of CPU/GPU split as #108.

Fix (xmem_physical::decommit):

  • The views stay and are made no-access; only the rest of the range is decommitted. A recommit is an mprotect on macOS, so the views come back read-write.
  • Windows only promises that a recommitted page reads zeros. So a page is zeroed when it is XMemVirtualAlloc's pool backing (Pool.backs, set in back_with_pool) and shown lists a single address for it.
  • Every other page keeps its bytes:
    • a page shown elsewhere (aliases, map_alias) is still read there;
    • XMemAllocatePhysicalPages pages outlive their mappings, since the title may map them again.
  • Both conditions are needed. Pool-backed alone would wipe a pool page that map_alias shows at another address. Single-address alone would wipe numbered pages the title can map again.
  • Guess, marked in the code and in docs/native-host.md: the console keeps those pages' contents too.

Tests.

  • xmem_posix::decommitting_pool_backed_memory_zeroes_the_pool: write, decommit and recommit pool-backed memory. The title address and the GPU's pool both read 0, and later writes reach the GPU's pool. Fails on main ("the GPU's pool reads zeros too").
  • host_posix::decommitting_keeps_pages_shown_at_other_addresses: a physical page mapped at two addresses, and XMemVirtualAlloc memory with a map_alias. After decommitting one address, the other still reads the bytes. It guards against zeroing too much: it fails if either condition is dropped.

Known limit (shortcut: comment). Zeroing touches every page it zeroes, including pages never written.

cargo test -p victory-hle --target x86_64-apple-darwin --lib (247), Windows clippy and cargo xtask policy pass. It merges cleanly with #124. Not checked in game.

**@⁠Olima34** on GitHub · 2026-11-26 17:39 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/126) `Olima34:fix/xmem-decommit-pool-backed-zeroes` → `main` · +150 −3 · 4 files · 1 commits · merged 2026-11-26 18:09 UTC Second of the two xmem PRs you asked for on #15 (the first is #124). Small and tested, like #108. **Bug.** `VirtualFree(MEM_DECOMMIT)` over a range that shows physical-page views called a plain decommit. That put fresh anonymous pages in place of the pool's at the title address, while `Physical.views` and the GPU's pool table still showed the pool there. After a recommit the title wrote the anonymous pages, and the GPU and any alias read the pool's old bytes. That is the same class of CPU/GPU split as #108. **Fix** (`xmem_physical::decommit`): - The views stay and are made no-access; only the rest of the range is decommitted. A recommit is an `mprotect` on macOS, so the views come back read-write. - Windows only promises that a recommitted page reads zeros. So a page is zeroed when it is `XMemVirtualAlloc`'s pool backing (`Pool.backs`, set in `back_with_pool`) **and** `shown` lists a single address for it. - Every other page keeps its bytes: - a page shown elsewhere (aliases, `map_alias`) is still read there; - `XMemAllocatePhysicalPages` pages outlive their mappings, since the title may map them again. - Both conditions are needed. Pool-backed alone would wipe a pool page that `map_alias` shows at another address. Single-address alone would wipe numbered pages the title can map again. - Guess, marked in the code and in docs/native-host.md: the console keeps those pages' contents too. **Tests.** - `xmem_posix::decommitting_pool_backed_memory_zeroes_the_pool`: write, decommit and recommit pool-backed memory. The title address and the GPU's pool both read 0, and later writes reach the GPU's pool. Fails on main ("the GPU's pool reads zeros too"). - `host_posix::decommitting_keeps_pages_shown_at_other_addresses`: a physical page mapped at two addresses, and `XMemVirtualAlloc` memory with a `map_alias`. After decommitting one address, the other still reads the bytes. It guards against zeroing too much: it fails if either condition is dropped. **Known limit (`shortcut:` comment).** Zeroing touches every page it zeroes, including pages never written. `cargo test -p victory-hle --target x86_64-apple-darwin --lib` (247), Windows clippy and `cargo xtask policy` pass. It merges cleanly with #124. Not checked in game.
Decommitting a range showing physical-page views put fresh anonymous
pages in place of the pool's, while Physical.views and the GPU's pool
table still showed the pool there. After a recommit the title wrote the
anonymous pages and the GPU and aliases read the pool's old bytes
(issue #15).

The views now stay, made no access; only the rest of the range is
decommitted. Windows only promises that a recommitted page reads zeros,
so a page is zeroed when it is XMemVirtualAlloc's pool backing and no
other address shows it. Other pages keep their bytes: a page shown
elsewhere is still read there, and XMemAllocatePhysicalPages pages
outlive their mappings. That the console keeps those contents is a
guess, marked in the code and the docs.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:43:02 +00:00
github manually merged commit f100c4b54c into main 2026-11-26 18:09:26 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 18:09 UTC · original

Merged, thanks. Views survive a decommit, and only lone pool backing is zeroed. Both x86_64 tests pass.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 18:09 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/126#issuecomment-7200128942) Merged, thanks. Views survive a decommit, and only lone pool backing is zeroed. Both x86_64 tests pass.
This discussion has been locked. Commenting is limited to contributors.
No description provided.