runtime: keep XMem allocation failures inside the shim #3697

Merged
lambirou merged 7 commits from upstream-pr/3697 into main 2026-12-02 20:22:13 +00:00
lambirou commented 2026-12-01 03:03:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lambirou on 2026-12-01T03:03:27Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5578100914 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3697). Originally posted by [lambirou](https://github.com/lambirou) on 2026-12-01T03:03:27Z. --- ## 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.
R0Y00 commented 2026-12-02 05:43:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T05:43:39Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6145389511 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6145389511). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T05:43:39Z. **Review:** COMMENTED --- 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.
lambirou commented 2026-12-02 14:44:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lambirou on 2026-12-02T14:44:00Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6153040385 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6153040385). Originally posted by [lambirou](https://github.com/lambirou) on 2026-12-02T14:44:00Z. **Review:** COMMENTED --- 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.
R0Y00 commented 2026-12-02 19:01:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T19:01:53Z.

Review: APPROVED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6157008390 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6157008390). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T19:01:53Z. **Review:** APPROVED --- 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 commented 2026-12-02 19:19:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T19:19:20Z.

Review: COMMENTED


The focused XMem checks passed 3/3 on this head. After #3725 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6157296623 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6157296623). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T19:19:20Z. **Review:** COMMENTED --- The focused XMem checks passed 3/3 on this head. After #3725 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 commented 2026-12-02 20:09:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T20:09:44Z.

Review: APPROVED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6158100856 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3697#pullrequestreview-6158100856). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T20:09:44Z. **Review:** APPROVED --- 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.
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!3697
No description provided.