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

Merged
Olima34 merged 1 commit from upstream-pr/126 into main 2026-11-26 18:09:23 +00:00
Olima34 commented 2026-11-26 17:39:37 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T17:39:37Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6632137362 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/126). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T17:39:37Z. --- 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.
LakshanSivakumar commented 2026-11-26 18:09:58 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T18:09:58Z.


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

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7200128942 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/126#issuecomment-7200128942). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T18:09:58Z. --- Merged, thanks. Views survive a decommit, and only lone pool backing is zeroed. Both x86_64 tests pass.
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
LakshanSivakumar/Native-Victory-macOS!126
No description provided.