xmem: VirtualFree(MEM_RELEASE) gives the pool backing back too #108

Manually merged
github merged 1 commit from github-bot/gh-108 into main 2026-11-26 16:35:37 +00:00 AGit
Owner

@⁠Olima34 on GitHub · 2026-11-26 16:25 UTC · original
Olima34:fix/xmem-virtualfree-releases-pool-backing → main · +55 −3 · 5 files · 1 commits · merged 2026-11-26 16:32 UTC

What this changes

Refs #15 ("physical pages owned twice in xmem_posix"). One sure way two allocations end up sharing a title address on main:

  1. XMemVirtualAlloc(…, MEM_RESERVE | MEM_COMMIT, …) backs the range with a pool of its own (back_with_pool), and track hands the region to host_posix (adopt_region, x86_64).
  2. The title frees it with VirtualFree(p, 0, MEM_RELEASE), as the GDK allows. host_posix::VirtualFree removed the region and released the address space, but never called xmem_physical::release_pool_backing and never dropped the reservation, although release_reservation's doc said VirtualFree "does the same". The pool's views stayed in Physical.views, and the GPU's pool table still mapped p to the freed pool.
  3. reserve_in is first fit, so the next allocation soon gets p again. From then on the CPU writes the new memory while the GPU, map_alias (CopyPageMappings) and the aliases read the old pool. A new back_with_pool at p also records an overlapping view.

Both releases now go through xmem_posix::forget_physical (drop the reservation, then release_pool_backing), which VirtualFree(MEM_RELEASE) calls before giving the address space back. It also covers XMemMapPhysicalPages views placed in a plain VirtualAlloc region.

How it was tested

  • virtual_free_gives_the_pool_backing_back_too (x86_64): after VirtualFree(MEM_RELEASE) of pool-backed XMemVirtualAlloc memory, the address no longer shows that pool and the reservation is gone. It compares pool ids, so another test taking the address at once can't make it flaky. It fails on main: "the old pool is still mapped at a freed address". virtual_alloc_meets_console_constraints now checks the same after release_reservation.
  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory (282, native probe included), arm64 cargo test --workspace (690)
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings, also with --all-features; clippy for macOS reports nothing new in the files touched
  • cargo xtask policy
  • Ran the game: not done, no copy of it here

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • No guesses about console behaviour
  • No new diagnostics
**@⁠Olima34** on GitHub · 2026-11-26 16:25 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/108) `Olima34:fix/xmem-virtualfree-releases-pool-backing` → `main` · +55 −3 · 5 files · 1 commits · merged 2026-11-26 16:32 UTC ## What this changes Refs #15 ("physical pages owned twice in xmem_posix"). One sure way two allocations end up sharing a title address on `main`: 1. `XMemVirtualAlloc(…, MEM_RESERVE | MEM_COMMIT, …)` backs the range with a pool of its own (`back_with_pool`), and `track` hands the region to `host_posix` (`adopt_region`, x86_64). 2. The title frees it with `VirtualFree(p, 0, MEM_RELEASE)`, as the GDK allows. `host_posix::VirtualFree` removed the region and released the address space, but never called `xmem_physical::release_pool_backing` and never dropped the reservation, although `release_reservation`'s doc said `VirtualFree` "does the same". The pool's views stayed in `Physical.views`, and the GPU's pool table still mapped `p` to the freed pool. 3. `reserve_in` is first fit, so the next allocation soon gets `p` again. From then on the CPU writes the new memory while the GPU, `map_alias` (CopyPageMappings) and the aliases read the old pool. A new `back_with_pool` at `p` also records an overlapping view. Both releases now go through `xmem_posix::forget_physical` (drop the reservation, then `release_pool_backing`), which `VirtualFree(MEM_RELEASE)` calls before giving the address space back. It also covers `XMemMapPhysicalPages` views placed in a plain `VirtualAlloc` region. ## How it was tested - [x] `virtual_free_gives_the_pool_backing_back_too` (x86_64): after `VirtualFree(MEM_RELEASE)` of pool-backed `XMemVirtualAlloc` memory, the address no longer shows that pool and the reservation is gone. It compares pool ids, so another test taking the address at once can't make it flaky. It fails on `main`: "the old pool is still mapped at a freed address". `virtual_alloc_meets_console_constraints` now checks the same after `release_reservation`. - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory` (282, native probe included), arm64 `cargo test --workspace` (690) - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`, also with `--all-features`; clippy for macOS reports nothing new in the files touched - [x] `cargo xtask policy` - [ ] Ran the game: not done, no copy of it here ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] No guesses about console behaviour - [x] No new diagnostics
The title frees XMemVirtualAlloc memory with VirtualFree, as the GDK allows. host_posix's VirtualFree(MEM_RELEASE) only dropped the region and released the address space: it never removed the physical-page views (xmem_physical::release_pool_backing) nor the reservation, though release_reservation's comment said it did the same. The freed pool so stayed in the GPU's pool table and in the views aliases are made from, at an address reserve_in (first fit) soon gives the next allocation: two memories at one title address, the pages owned twice of issue #15. Both releases now share xmem_posix::forget_physical, which VirtualFree calls before giving the address space back; it also covers XMemMapPhysicalPages views in a VirtualAlloc region.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:30:12 +00:00
github manually merged commit 13a3ec3ad7 into main 2026-11-26 16:35:37 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 16:32 UTC · original · approved these changes

Checked the diff and merged it onto current main: virtual_free_gives_the_pool_backing_back_too and the rest of victory-hle pass on x86_64, native tests, windows clippy and xtask policy are clean, CI is green.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 16:32 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/108#pullrequestreview-6077604778) · **approved these changes** Checked the diff and merged it onto current main: virtual_free_gives_the_pool_backing_back_too and the rest of victory-hle pass on x86_64, native tests, windows clippy and xtask policy are clean, CI is green.
This discussion has been locked. Commenting is limited to contributors.
No description provided.