runtime: share physical page backing across XMem views #927

Merged
R0Y00 merged 1 commit from upstream-pr/934 into main 2026-11-23 18:03:23 +00:00
R0Y00 commented 2026-11-23 17:48:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:48:09Z.


Refs #870, #892 and #902.

Give live physical-page grants shared Windows section backing. Contiguous and scatter operations map those sections into caller-owned page-sized placeholders. Unmapping preserves the reservation and the page contents. Existing grants keep their ownership rules, and release refuses any grant with a live view.

The source-built test observes shared data between addresses, scatter ordering, remap persistence, bad page numbers, release with a live view and rollback after a later destination fails. This desktop profile requires Windows placeholders from the caller.

Windows x64, MSVC 19.51, Release:

ctest --preset release --no-tests=error -R '^xmem_shim_(mapped_pages|physical_pages|exports|tools_memory)$' --output-on-failure

4/4 passed. The strict package preflight resolved two additional imports, with execution still blocked by unresolved external providers.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5458263116 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/934). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:48:09Z. --- Refs #870, #892 and #902. Give live physical-page grants shared Windows section backing. Contiguous and scatter operations map those sections into caller-owned page-sized placeholders. Unmapping preserves the reservation and the page contents. Existing grants keep their ownership rules, and release refuses any grant with a live view. The source-built test observes shared data between addresses, scatter ordering, remap persistence, bad page numbers, release with a live view and rollback after a later destination fails. This desktop profile requires Windows placeholders from the caller. Windows x64, MSVC 19.51, Release: ```powershell ctest --preset release --no-tests=error -R '^xmem_shim_(mapped_pages|physical_pages|exports|tools_memory)$' --output-on-failure ``` 4/4 passed. The strict package preflight resolved two additional imports, with execution still blocked by unresolved external providers.
upstream-sync closed this pull request 2026-11-24 06:45:28 +00:00
R0Y00 commented 2026-11-24 06:49:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:53:27Z.


I tested 7716a09 together with the #888 reservation draft on Windows x64 Release. Five focused CTests passed. For both 16 GiB and 24 GiB reservations, the combined control split 64 KiB prefixes, mapped the same live grant with contiguous and scatter calls, observed a write through the other address, checked that release refused an active view, then unmounted and remapped to verify persistence. After the final unmap, release and coalescing both succeeded, and remapping the released identifier was refused. This uses the real XMem exports.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7109333591 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/934#issuecomment-7109333591). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:53:27Z. --- I tested 7716a09 together with the #888 reservation draft on Windows x64 Release. Five focused CTests passed. For both 16 GiB and 24 GiB reservations, the combined control split 64 KiB prefixes, mapped the same live grant with contiguous and scatter calls, observed a write through the other address, checked that release refused an active view, then unmounted and remapped to verify persistence. After the final unmap, release and coalescing both succeeded, and remapping the released identifier was refused. This uses the real XMem exports.
thegl0w commented 2026-11-24 06:49:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by thegl0w on 2026-11-23T18:42:49Z.


AI-generated: written with Claude Code; reviewed and posted by thegl0w.

Independent review of #934, as asked on #870, at main 0f0c3eb, which includes #945's placeholder split in map_view.

Environment: Windows 11 build 26300, VS 2022 x64, dev and release presets, CPU only (no GPU involved).

Existing tests: ctest --preset <dev|release> --no-tests=error -R '^xmem_shim_(mapped_pages|physical_pages|exports|tools_memory)$' passes 4/4 on both presets.

Finding: a map inside an XMemVirtualAlloc reservation can discard the caller's committed memory

map_view falls back to VirtualFree(target, page_bytes, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER) whenever MapViewOfFile3 fails at an address inside a tracked reservation. It doesn't check that the target is still a placeholder. A private allocation that replaced a placeholder (VirtualAlloc2 with MEM_REPLACE_PLACEHOLDER) is also accepted by that call. It is released back to a placeholder, and its bytes are lost.

To reproduce, using a source-built probe that drives xmem.dll through ctypes, the same way tests/xmem_mapped_pages_check.py does:

  1. Reserve 16 GiB with XMemVirtualAlloc(nullptr, 16 GiB, 0x20402000, 0x21, PAGE_READWRITE).
  2. Split one page at whole + 5 × 64 KiB, replace it with a committed private allocation, and write 0xDEADBEEF there.
  3. Call XMemMapPhysicalPages(whole + 4 × 64 KiB, 2, pages).
Expected Observed
Return NULL (the second destination isn't a placeholder) the base address (success)
Second page before → after MEM_COMMIT/MEM_PRIVATE, 0xDEADBEEF MEM_COMMIT/MEM_MAPPED, 0x0
After unmapping the caller's allocation is back MEM_RESERVE placeholder; the caller's allocation is gone

The same result appears on dev and release. The existing test doesn't catch it because its rollback case places the non-placeholder page in an ordinary reservation, outside the tracked ranges.

Suggested guard (local patch): split only when VirtualQuery reports MEM_RESERVE at the target, and otherwise fail with ERROR_INVALID_ADDRESS. With it:

  • the 4 existing tests still pass on both presets;
  • the probe gets NULL with error 487;
  • the caller's page stays MEM_COMMIT/MEM_PRIVATE with 0xDEADBEEF;
  • the first page is rolled back: the grant releases afterwards.

A caller allocation that is reserved but not committed also reads as MEM_RESERVE, so the guard still converts that one back to a placeholder. No bytes are lost, but the caller loses its reservation. Telling those two apart would need the shim to track which pages it split itself. I can open that as a focused PR with the probe as a test if you want it.

Other controls I checked, all of which pass at 0f0c3eb:

  • A scatter with a valid page first and an unowned page second, into fresh destinations, is refused (error 87). The existing view is intact, and the first destination is reusable afterwards.
    • Note: the existing test's scatter(destinations, 2, invalid) targets addresses that are already mapped, so it is refused by the live-view check before the token check runs. The fresh-destination case above covers the token check.
  • A write through the second view is visible through the first.
  • One page at two addresses in a single scatter call aliases.
  • Release is refused while one of those views is live, accepted after unmapping, and the released ID is refused afterwards.

These are desktop-profile observations from source-built inputs only. They say nothing about console behaviour.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7110548781 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/934#issuecomment-7110548781). Originally posted by [thegl0w](https://github.com/thegl0w) on 2026-11-23T18:42:49Z. --- > AI-generated: written with Claude Code; reviewed and posted by thegl0w. Independent review of #934, as asked on #870, at main `0f0c3eb`, which includes #945's placeholder split in `map_view`. **Environment:** Windows 11 build 26300, VS 2022 x64, `dev` and `release` presets, CPU only (no GPU involved). **Existing tests:** `ctest --preset <dev|release> --no-tests=error -R '^xmem_shim_(mapped_pages|physical_pages|exports|tools_memory)$'` passes 4/4 on both presets. **Finding: a map inside an `XMemVirtualAlloc` reservation can discard the caller's committed memory** `map_view` falls back to `VirtualFree(target, page_bytes, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)` whenever `MapViewOfFile3` fails at an address inside a tracked reservation. It doesn't check that the target is still a placeholder. A private allocation that replaced a placeholder (`VirtualAlloc2` with `MEM_REPLACE_PLACEHOLDER`) is also accepted by that call. It is released back to a placeholder, and its bytes are lost. To reproduce, using a source-built probe that drives `xmem.dll` through ctypes, the same way `tests/xmem_mapped_pages_check.py` does: 1. Reserve 16 GiB with `XMemVirtualAlloc(nullptr, 16 GiB, 0x20402000, 0x21, PAGE_READWRITE)`. 2. Split one page at `whole + 5 × 64 KiB`, replace it with a committed private allocation, and write `0xDEADBEEF` there. 3. Call `XMemMapPhysicalPages(whole + 4 × 64 KiB, 2, pages)`. | | Expected | Observed | |---|---|---| | Return | NULL (the second destination isn't a placeholder) | the base address (success) | | Second page before → after | `MEM_COMMIT`/`MEM_PRIVATE`, `0xDEADBEEF` | `MEM_COMMIT`/`MEM_MAPPED`, `0x0` | | After unmapping | the caller's allocation is back | `MEM_RESERVE` placeholder; the caller's allocation is gone | The same result appears on `dev` and `release`. The existing test doesn't catch it because its rollback case places the non-placeholder page in an ordinary reservation, outside the tracked ranges. **Suggested guard (local patch):** split only when `VirtualQuery` reports `MEM_RESERVE` at the target, and otherwise fail with `ERROR_INVALID_ADDRESS`. With it: - the 4 existing tests still pass on both presets; - the probe gets NULL with error 487; - the caller's page stays `MEM_COMMIT`/`MEM_PRIVATE` with `0xDEADBEEF`; - the first page is rolled back: the grant releases afterwards. A caller allocation that is reserved but not committed also reads as `MEM_RESERVE`, so the guard still converts that one back to a placeholder. No bytes are lost, but the caller loses its reservation. Telling those two apart would need the shim to track which pages it split itself. I can open that as a focused PR with the probe as a test if you want it. **Other controls I checked, all of which pass at `0f0c3eb`:** - A scatter with a valid page first and an unowned page second, into fresh destinations, is refused (error 87). The existing view is intact, and the first destination is reusable afterwards. - Note: the existing test's `scatter(destinations, 2, invalid)` targets addresses that are already mapped, so it is refused by the live-view check before the token check runs. The fresh-destination case above covers the token check. - A write through the second view is visible through the first. - One page at two addresses in a single scatter call aliases. - Release is refused while one of those views is live, accepted after unmapping, and the released ID is refused afterwards. These are desktop-profile observations from source-built inputs only. They say nothing about console behaviour.
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
R0Y00/Xbox2Windows!927
No description provided.