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

Merged
Olima34 merged 1 commit from upstream-pr/108 into main 2026-11-26 16:32:32 +00:00
Olima34 commented 2026-11-26 16:25:46 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T16:25:46Z.


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
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6630914073 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/108). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T16:25:46Z. --- ## 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
LakshanSivakumar commented 2026-11-26 16:32:28 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T16:32:28Z.

Review: APPROVED


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6077604778 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/108#pullrequestreview-6077604778). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T16:32:28Z. **Review:** APPROVED --- 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.
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!108
No description provided.