runtime: keep XMem allocation failures inside the shim #3699

Manually merged
github merged 7 commits from github-bot/gh-3697 into gh-pr/3697/base 2026-12-02 20:27:55 +00:00 AGit
Owner

@⁠lambirou on GitHub · 2026-12-01 03:03 UTC · original
lambirou:runtime/xmem-allocation-boundary → main · +155 −37 · 5 files · 7 commits · merged 2026-12-02 20:22 UTC

Problem

xmem.dll is called by the title through a C ABI, and the title does not install C++ exception handlers. A long run on main stopped at 478.39 s with 0xe06d7363; the return-address chain went through XMemMapPhysicalPages, the GPU-mirror batch and section_for. Any failed allocation in the backing table, the slab record, the mirror vectors or a mapping batch therefore ended the process instead of returning a Win32 error.

Change

  • SlabSlots::take_fresh releases the new slab and returns false on std::bad_alloc instead of rethrowing.
  • section_for handles both backing-table emplace sites, unwinds a fresh slot and returns nullptr with ERROR_NOT_ENOUGH_MEMORY.
  • local_mirror_locked is best-effort: an allocation failure in the GPU mirror is logged and skipped, while the CPU mapping the title asked for remains successful.
  • local_apply, XMemMapPhysicalPages, XMemMapPhysicalPagesScatter and XMemFreePhysicalPages translate std::exception into a failed call with ERROR_NOT_ENOUGH_MEMORY; unknown exceptions are also caught at the apply boundary.
  • The test-controls build injects an allocation failure at the two sites (X2W_XMEM_TEST_ALLOC_FAILURE_BATCH, X2W_XMEM_TEST_ALLOC_FAILURE_SECTION). The new xmem_mapped_pages_allocation scenario checks the error code and the control mapping from the same process.

No GDK or game file is included.

Tests

ctest --test-dir out/build/release -R '^xmem': 73 tests, 0 failed (3 placeholder tests skipped).

Negative control: with the XMemMapPhysicalPages boundary catch changed to throw;, xmem_mapped_pages_check.py --allocation-failure fails with OSError: [WinError -529697949] Windows Error 0xe06d7363; restoring the catch makes it pass.

Long runs on main (1a9ccbc4, 32 GiB job, lazy commit/startup/shared views): gta6-main-20261201-5 reached 11 630 frames at 23 fps, 1 446 textures and 341 coherent .s57 mesh captures, past the previous 478 s crash point. The remaining mesh refusals are missing reviewed .mesh57-state profiles, not an XMem allocation.

Limitations

The change reports allocation failure; it does not make more memory available. section_for callers already test its return value. The GPU mirror remains an addition to the CPU mapping and can be skipped under memory pressure.

**@⁠lambirou** on GitHub · 2026-12-01 03:03 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3697) `lambirou:runtime/xmem-allocation-boundary` → `main` · +155 −37 · 5 files · 7 commits · merged 2026-12-02 20:22 UTC ## Problem `xmem.dll` is called by the title through a C ABI, and the title does not install C++ exception handlers. A long run on `main` stopped at 478.39 s with `0xe06d7363`; the return-address chain went through `XMemMapPhysicalPages`, the GPU-mirror batch and `section_for`. Any failed allocation in the backing table, the slab record, the mirror vectors or a mapping batch therefore ended the process instead of returning a Win32 error. ## Change - `SlabSlots::take_fresh` releases the new slab and returns `false` on `std::bad_alloc` instead of rethrowing. - `section_for` handles both backing-table `emplace` sites, unwinds a fresh slot and returns `nullptr` with `ERROR_NOT_ENOUGH_MEMORY`. - `local_mirror_locked` is best-effort: an allocation failure in the GPU mirror is logged and skipped, while the CPU mapping the title asked for remains successful. - `local_apply`, `XMemMapPhysicalPages`, `XMemMapPhysicalPagesScatter` and `XMemFreePhysicalPages` translate `std::exception` into a failed call with `ERROR_NOT_ENOUGH_MEMORY`; unknown exceptions are also caught at the apply boundary. - The test-controls build injects an allocation failure at the two sites (`X2W_XMEM_TEST_ALLOC_FAILURE_BATCH`, `X2W_XMEM_TEST_ALLOC_FAILURE_SECTION`). The new `xmem_mapped_pages_allocation` scenario checks the error code and the control mapping from the same process. No GDK or game file is included. ## Tests `ctest --test-dir out/build/release -R '^xmem'`: **73 tests, 0 failed** (3 placeholder tests skipped). Negative control: with the `XMemMapPhysicalPages` boundary catch changed to `throw;`, `xmem_mapped_pages_check.py --allocation-failure` fails with `OSError: [WinError -529697949] Windows Error 0xe06d7363`; restoring the catch makes it pass. Long runs on `main` (`1a9ccbc4`, 32 GiB job, lazy commit/startup/shared views): `gta6-main-20261201-5` reached 11 630 frames at 23 fps, 1 446 textures and 341 coherent `.s57` mesh captures, past the previous 478 s crash point. The remaining mesh refusals are missing reviewed `.mesh57-state` profiles, not an XMem allocation. ## Limitations The change reports allocation failure; it does not make more memory available. `section_for` callers already test its return value. The GPU mirror remains an addition to the CPU mapping and can be skipped under memory pressure.
The title calls xmem.dll through a C ABI and does not catch C++ exceptions.
A failed allocation while recording a backing, a slab, the GPU mirror
vectors or a mapping batch unwound 0xe06d7363 into the title, ending the
run after 478 s.

Report the failure instead. section_for and SlabSlots::take_fresh return
false with ERROR_NOT_ENOUGH_MEMORY, the GPU mirror stays best-effort and
logs the skipped batch, and XMemMapPhysicalPages, its scatter form and
XMemFreePhysicalPages convert std::exception into a failed call. The
test-controls build injects the failure at both allocation sites; the new
xmem_mapped_pages_allocation test checks the error and the control mapping.
Removing the boundary catch makes the same test terminate with 0xe06d7363.

Tests: ctest --test-dir out/build/release -R '^xmem': 73 tests, 0 failed
(3 placeholder skips). Long runs on main: 29 fps, 1 446 textures, 341
.s57 mesh captures, past the previous 478 s crash point; the remaining
mesh refusal is the missing reviewed .mesh57-state profile, not an XMem
allocation.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 03:06:41 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 05:43 UTC · original · reviewed

I merged current main into the PR branch and resolved the only conflict in CMakePresets.json, retaining all main selections and adding xmem_mapped_pages_allocation to pr. The selection is present in ctest --preset pr -N --no-tests=error -R '^xmem_mapped_pages_allocation$'. ctest --preset dev --no-tests=error -j 2 -R '^xmem_(mapped_pages_allocation|shim_mapped_pages)$' --output-on-failure passed on the integrated head. New hosted checks and review can now run on it.

**@⁠R0Y00** on GitHub · 2026-12-02 05:43 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6145389511) · **reviewed** I merged current main into the PR branch and resolved the only conflict in `CMakePresets.json`, retaining all main selections and adding `xmem_mapped_pages_allocation` to `pr`. The selection is present in `ctest --preset pr -N --no-tests=error -R '^xmem_mapped_pages_allocation$'`. `ctest --preset dev --no-tests=error -j 2 -R '^xmem_(mapped_pages_allocation|shim_mapped_pages)$' --output-on-failure` passed on the integrated head. New hosted checks and review can now run on it.
Author
Owner

@⁠lambirou on GitHub · 2026-12-02 14:44 UTC · original · reviewed

Reviewed 7361b5a9 after the maintainer's main merge; read the diff, not built here.

Verdict: approve. The allocation-failure conversion is in the right places: the C ABI exports now return failure with ERROR_NOT_ENOUGH_MEMORY instead of letting std::bad_alloc cross into the title, and the new control covers both the batch-vector and section-table injection points.

  • SHOULD local_apply() catches an exception after local_apply_locked() may already have applied some pages. The comment says "the batch has not been applied in this path", but that is only guaranteed when the allocation fails before the first page. Either document that a failed batch can be partially applied, or restructure the apply path to stage all allocations before the first mutation.
  • NIT XMemFreePhysicalPages reports era_physical::Status::exhausted for an internal allocation failure. That is a reasonable conservative result, but a short comment would make it clear that the failure is the shim's, not the title's invalid input.
  • Checked, no finding: take_fresh releases the fresh slab and reports failure on bad_alloc; section_for releases the slot before returning; the test covers both allocation points and then verifies the same page maps successfully once the injection is removed. xmem_mapped_pages_allocation is in the pr filter.

The PR is MERGEABLE/CLEAN at this head.

**@⁠lambirou** on GitHub · 2026-12-02 14:44 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6153040385) · **reviewed** Reviewed `7361b5a9` after the maintainer's `main` merge; read the diff, not built here. **Verdict: approve.** The allocation-failure conversion is in the right places: the C ABI exports now return failure with `ERROR_NOT_ENOUGH_MEMORY` instead of letting `std::bad_alloc` cross into the title, and the new control covers both the batch-vector and section-table injection points. - **SHOULD** `local_apply()` catches an exception after `local_apply_locked()` may already have applied some pages. The comment says "the batch has not been applied in this path", but that is only guaranteed when the allocation fails before the first page. Either document that a failed batch can be partially applied, or restructure the apply path to stage all allocations before the first mutation. - **NIT** `XMemFreePhysicalPages` reports `era_physical::Status::exhausted` for an internal allocation failure. That is a reasonable conservative result, but a short comment would make it clear that the failure is the shim's, not the title's invalid input. - Checked, no finding: `take_fresh` releases the fresh slab and reports failure on `bad_alloc`; `section_for` releases the slot before returning; the test covers both allocation points and then verifies the same page maps successfully once the injection is removed. `xmem_mapped_pages_allocation` is in the `pr` filter. The PR is `MERGEABLE/CLEAN` at this head.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 19:01 UTC · original · approved these changes

Reviewed 003609d9 after the main update. The change handles allocation failures before they can cross the permissive mapping C ABI, unwinds the fresh slab on backing-table allocation failure, and keeps the CPU mapping successful when the optional GPU mirror throws. Independent Windows 11 build 26300 and MSVC Debug ctest --preset dev --no-tests=error -V -j 1 -R '^xmem_mapped_pages_(allocation|commit|commit_failure)$' passed 3/3. Both injected sites returned ERROR_NOT_ENOUGH_MEMORY, and the same-process control mapping and readback passed. The main merge was clean.

**@⁠R0Y00** on GitHub · 2026-12-02 19:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6157008390) · **approved these changes** Reviewed 003609d9 after the main update. The change handles allocation failures before they can cross the permissive mapping C ABI, unwinds the fresh slab on backing-table allocation failure, and keeps the CPU mapping successful when the optional GPU mirror throws. Independent Windows 11 build 26300 and MSVC Debug `ctest --preset dev --no-tests=error -V -j 1 -R '^xmem_mapped_pages_(allocation|commit|commit_failure)$'` passed 3/3. Both injected sites returned ERROR_NOT_ENOUGH_MEMORY, and the same-process control mapping and readback passed. The main merge was clean.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 19:19 UTC · original · reviewed

The focused XMem checks passed 3/3 on this head. After #3727 landed, the current main trial merge conflicts in CMakePresets.json. I paused auto-merge so the resolved head can be checked first. Please keep xmem_mapped_pages_allocation and the Mesh86 additions in the PR preset when refreshing the branch.

**@⁠R0Y00** on GitHub · 2026-12-02 19:19 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6157296623) · **reviewed** The focused XMem checks passed 3/3 on this head. After #3727 landed, the current main trial merge conflicts in `CMakePresets.json`. I paused auto-merge so the resolved head can be checked first. Please keep `xmem_mapped_pages_allocation` and the Mesh86 additions in the PR preset when refreshing the branch.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 20:09 UTC · original · approved these changes

Revalidated 0ed14675. I updated the contributor branch without rewriting it. The only conflict resolution preserves main's PR selections and adds xmem_mapped_pages_allocation; the runtime changes are those reviewed earlier. ctest --preset pr -N lists the new check. Independent Windows/MSVC Debug ctest --preset dev --no-tests=error --output-on-failure -j 1 -R '^xmem_mapped_pages_(allocation|commit|commit_failure)$' passed 3/3 on the exact head, including both injected ERROR_NOT_ENOUGH_MEMORY paths and the control mapping. The export catalog passed all 476 checks and six mutation controls.

**@⁠R0Y00** on GitHub · 2026-12-02 20:09 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6158100856) · **approved these changes** Revalidated 0ed14675. I updated the contributor branch without rewriting it. The only conflict resolution preserves main's PR selections and adds `xmem_mapped_pages_allocation`; the runtime changes are those reviewed earlier. `ctest --preset pr -N` lists the new check. Independent Windows/MSVC Debug `ctest --preset dev --no-tests=error --output-on-failure -j 1 -R '^xmem_mapped_pages_(allocation|commit|commit_failure)$'` passed 3/3 on the exact head, including both injected `ERROR_NOT_ENOUGH_MEMORY` paths and the control mapping. The export catalog passed all 476 checks and six mutation controls.
github-bot changed target branch from main to gh-pr/3697/base 2026-12-02 20:27:53 +00:00
github manually merged commit 0ed146756f into gh-pr/3697/base 2026-12-02 20:27:55 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.