runtime: cap the permissive XMemMapPhysicalPages page count #1518

Merged
RenzoWit35 merged 1 commit from upstream-pr/1525 into main 2026-11-24 14:26:28 +00:00
RenzoWit35 commented 2026-11-24 13:37:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T13:37:56Z.


Change

With X2W_XMEM_PERMISSIVE=1, XMemMapPhysicalPages allocates std::vectors sized by the caller's count, and to_pages (also used by XMemMapPhysicalPagesScatter) does pages.resize(count). Only an address-overflow guard bounded count (about 2^48), unlike the strict path (map_pages, free_pages), which rejects count > era_physical::capacity_pages. A huge count therefore threw std::bad_alloc out of an extern "C" function.

This rejects count > era_physical::capacity_pages up front in the permissive XMemMapPhysicalPages and in to_pages, returning NULL/FALSE with ERROR_INVALID_PARAMETER, the strict path's convention. No behavior changes for counts up to the capacity (including the zero-count null-array unmap).

Verification

Windows 11, MSVC 14.51.36231, cmake --preset dev, base origin/main 60c72330.

  • Before the fix, X2W_XMEM_PERMISSIVE=1 with XMemMapPhysicalPages(0x10000, 1 << 40, NULL) in a child process raised C++ exception 0xE06D7363 (ctypes reports it as OSError; a native caller would terminate).
  • New subprocess case check_permissive_count in tests/xmem_mapped_pages_check.py: with count 1 << 40 the map call (null and non-null token array) must return NULL with error 87 and scatter must return FALSE with error 87; control: a zero-count permissive call still returns the base. Negative control: on unmodified main the child dies and the check fails; with the fix it passes.
  • cmake --build out/build/dev --target xmem_shim then ctest --test-dir out/build/dev -R xmem_shim_mapped_pages : Passed.

Not run: other xmem CTests whose helper executables/DLLs were not built here (xmem_shim_page_runs, xmem_attribute_scan, cpu_native_xmem_*); they were "Not Run"/missing-file, not failures of this change.

Evidence and follow-up

Follow-up (separate behavior, not changed here): map_pages in src/xmem_mapped_pages.cpp (if (!section_for(page)) return FALSE;, around line 398) returns FALSE without calling SetLastError.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6583046750 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1525). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T13:37:56Z. --- ## Change With `X2W_XMEM_PERMISSIVE=1`, `XMemMapPhysicalPages` allocates `std::vector`s sized by the caller's `count`, and `to_pages` (also used by `XMemMapPhysicalPagesScatter`) does `pages.resize(count)`. Only an address-overflow guard bounded `count` (about 2^48), unlike the strict path (`map_pages`, `free_pages`), which rejects `count > era_physical::capacity_pages`. A huge count therefore threw `std::bad_alloc` out of an `extern "C"` function. This rejects `count > era_physical::capacity_pages` up front in the permissive `XMemMapPhysicalPages` and in `to_pages`, returning NULL/FALSE with `ERROR_INVALID_PARAMETER`, the strict path's convention. No behavior changes for counts up to the capacity (including the zero-count null-array unmap). ## Verification Windows 11, MSVC 14.51.36231, `cmake --preset dev`, base `origin/main` 60c72330. - Before the fix, `X2W_XMEM_PERMISSIVE=1` with `XMemMapPhysicalPages(0x10000, 1 << 40, NULL)` in a child process raised C++ exception 0xE06D7363 (ctypes reports it as `OSError`; a native caller would terminate). - New subprocess case `check_permissive_count` in `tests/xmem_mapped_pages_check.py`: with count `1 << 40` the map call (null and non-null token array) must return NULL with error 87 and scatter must return FALSE with error 87; control: a zero-count permissive call still returns the base. Negative control: on unmodified `main` the child dies and the check fails; with the fix it passes. - `cmake --build out/build/dev --target xmem_shim` then `ctest --test-dir out/build/dev -R xmem_shim_mapped_pages` : Passed. Not run: other xmem CTests whose helper executables/DLLs were not built here (`xmem_shim_page_runs`, `xmem_attribute_scan`, `cpu_native_xmem_*`); they were "Not Run"/missing-file, not failures of this change. ## Evidence and follow-up Follow-up (separate behavior, not changed here): `map_pages` in `src/xmem_mapped_pages.cpp` (`if (!section_for(page)) return FALSE;`, around line 398) returns FALSE without calling `SetLastError`. AI-assisted.
upstream-sync closed this pull request 2026-11-24 15:01:51 +00:00
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!1518
No description provided.