startup: return E_OUTOFMEMORY when a D3D12 wrapper cannot be allocated #2648

Merged
Not-Saii merged 3 commits from upstream-pr/2656 into main 2026-11-26 01:10:38 +00:00
Not-Saii commented 2026-11-26 00:48:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Not-Saii on 2026-11-26T00:48:24Z.


Change

Fixes #2653.

wrap() in src/startup_stubs/d3d12_xs_device.cpp used the _aligned_malloc result without a check. Under the job commit limit, a failed wrapper allocation became a write through a null pointer inside d3d12_xs.dll, so the title never saw an error. This was seen at the slot 55 derived-pipeline placeholder in a startup run, reported on #2653 and #1199.

  • wrap() returns nullptr when the allocation fails, logging the first four failures. It also catches std::bad_alloc from the registry insert and frees the block.
  • Each of its 16 callers releases the desktop object it created (queue, root signature, descriptor heap, command list, resource or pipeline) and returns E_OUTOFMEMORY with a null output. The descriptor heap also frees backing memory the stand-in allocated for it, and the placed buffer drops its linear-buffer entry.
  • x2w_startup::attach_device keeps its documented ownership: on failure it releases the device reference and leaves *wrapped null. D3D12XboxCreateDevice then returns E_OUTOFMEMORY. The device used for removal reporting is recorded only after the wrapper exists.
  • New fixture-only export x2w_d3d12_fail_wraps(count): the next count wrapper allocations fail. It is used only by the new test.

Not changed: memory that the committed-resource path takes from the title's own allocator stays allocated on this failure, because the stand-in has no free hook for it (commented at the call site). The memory growth behind the original stop belongs to #1199.

Verification

Branch head 6864ec86 on 8972929c; it merges cleanly with the current main. Windows 11 Pro 26300.9974, MSVC 19.44 (14.44.35207), Windows SDK 10.0.26100.0, dev preset. Backend: WARP. D3D12 debug layer off (X2W_D3D12_DEBUG=0).

cmake --build --preset dev --target startup_wrap_allocation
ctest --preset dev --no-tests=error -R "^startup_wrap_allocation" -V
  • startup_wrap_allocation_warp: passed. Device creation and slots 8, 9, 14, 16, 55, 73 and 127 each returned E_OUTOFMEMORY with a null output when armed, then S_OK.
  • startup_wrap_allocation_control_warp: passed. The control never arms the failure, so all eight checks fail and the fixture prints FAIL:.
  • Mutation check (local, not committed): with the slot 55 null check removed, startup_wrap_allocation_warp failed on Device::slot55 derived pipeline ... returned 0x00000000 and a null output. With the slot 73 null check removed, it crashed (segfault) at slot 73, the original failure.

Related stand-in tests after a full dev build:

ctest --preset dev --no-tests=error -j 8 -R '^(startup_wrap_allocation|startup_view_census|startup_resource_retire|startup_indirect|startup_dump_fence|startup_stubs|startup_debug|d3d12x_|startup_list_upload|startup_sampler_x|descriptor_)' -E 'hardware'

84 of 84 passed, with no capability skips. tests/docs_links_check.py and tests/bidi_controls_check.py also pass. Hardware variants were not run.

Evidence and follow-up

The fixture is source-built: it creates the device through D3D12XboxCreateDevice on WARP and calls the device slots with synthetic descriptors (an empty serialized root signature, a direct queue, a four-entry view heap). It covers 8 of the 16 call sites: device creation, slot 9 (the shared create_slot path), queue slot 8, descriptor heap, root signature, command list, committed resource and the slot 55 placeholder. The other 8 (queue slot 48, two command-signature placeholders, placed buffer, placed texture and three pipeline paths) follow the same pattern and were checked by reading. The placed-buffer, placed-texture and stream-pipeline paths need title memory or translated shaders to reach; the others could be added cheaply. Whether a title handles E_OUTOFMEMORY from these slots is not established.

Docs: new "Wrapper allocation failure" section in docs/startup-runtime.md with the test command.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6616919926 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2656). Originally posted by [Not-Saii](https://github.com/Not-Saii) on 2026-11-26T00:48:24Z. --- ## Change Fixes #2653. `wrap()` in `src/startup_stubs/d3d12_xs_device.cpp` used the `_aligned_malloc` result without a check. Under the job commit limit, a failed wrapper allocation became a write through a null pointer inside `d3d12_xs.dll`, so the title never saw an error. This was seen at the slot 55 derived-pipeline placeholder in a startup run, reported on #2653 and #1199. - `wrap()` returns `nullptr` when the allocation fails, logging the first four failures. It also catches `std::bad_alloc` from the registry insert and frees the block. - Each of its 16 callers releases the desktop object it created (queue, root signature, descriptor heap, command list, resource or pipeline) and returns `E_OUTOFMEMORY` with a null output. The descriptor heap also frees backing memory the stand-in allocated for it, and the placed buffer drops its linear-buffer entry. - `x2w_startup::attach_device` keeps its documented ownership: on failure it releases the device reference and leaves `*wrapped` null. `D3D12XboxCreateDevice` then returns `E_OUTOFMEMORY`. The device used for removal reporting is recorded only after the wrapper exists. - New fixture-only export `x2w_d3d12_fail_wraps(count)`: the next `count` wrapper allocations fail. It is used only by the new test. Not changed: memory that the committed-resource path takes from the title's own allocator stays allocated on this failure, because the stand-in has no free hook for it (commented at the call site). The memory growth behind the original stop belongs to #1199. ## Verification Branch head `6864ec86` on `8972929c`; it merges cleanly with the current `main`. Windows 11 Pro 26300.9974, MSVC 19.44 (14.44.35207), Windows SDK 10.0.26100.0, `dev` preset. Backend: WARP. D3D12 debug layer off (`X2W_D3D12_DEBUG=0`). ```powershell cmake --build --preset dev --target startup_wrap_allocation ctest --preset dev --no-tests=error -R "^startup_wrap_allocation" -V ``` - `startup_wrap_allocation_warp`: passed. Device creation and slots 8, 9, 14, 16, 55, 73 and 127 each returned `E_OUTOFMEMORY` with a null output when armed, then `S_OK`. - `startup_wrap_allocation_control_warp`: passed. The control never arms the failure, so all eight checks fail and the fixture prints `FAIL:`. - Mutation check (local, not committed): with the slot 55 null check removed, `startup_wrap_allocation_warp` failed on `Device::slot55 derived pipeline ... returned 0x00000000 and a null output`. With the slot 73 null check removed, it crashed (segfault) at slot 73, the original failure. Related stand-in tests after a full `dev` build: ```powershell ctest --preset dev --no-tests=error -j 8 -R '^(startup_wrap_allocation|startup_view_census|startup_resource_retire|startup_indirect|startup_dump_fence|startup_stubs|startup_debug|d3d12x_|startup_list_upload|startup_sampler_x|descriptor_)' -E 'hardware' ``` 84 of 84 passed, with no capability skips. `tests/docs_links_check.py` and `tests/bidi_controls_check.py` also pass. Hardware variants were not run. ## Evidence and follow-up The fixture is source-built: it creates the device through `D3D12XboxCreateDevice` on WARP and calls the device slots with synthetic descriptors (an empty serialized root signature, a direct queue, a four-entry view heap). It covers 8 of the 16 call sites: device creation, slot 9 (the shared `create_slot` path), queue slot 8, descriptor heap, root signature, command list, committed resource and the slot 55 placeholder. The other 8 (queue slot 48, two command-signature placeholders, placed buffer, placed texture and three pipeline paths) follow the same pattern and were checked by reading. The placed-buffer, placed-texture and stream-pipeline paths need title memory or translated shaders to reach; the others could be added cheaply. Whether a title handles `E_OUTOFMEMORY` from these slots is not established. Docs: new "Wrapper allocation failure" section in `docs/startup-runtime.md` with the test command. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
tedin7 commented 2026-11-26 01:02:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T00:51:17Z.

Review: COMMENTED


Review of head a526db0e (comment only, no Win2025 run by me).

What I read: wrap() null/bad_alloc handling and all new caller paths in src/startup_stubs/d3d12_xs_device.cpp, the attach_device ownership change, the new test and its CMake entries, and the docs change.

  • Each caller that gets a null wrapper releases the desktop object it just created and returns E_OUTOFMEMORY with a null output. I found no leaked reference. The budget tickets and backing_budget->begin_use() are taken only after wrap() succeeds, so nothing is left unbalanced on failure. The packed-format tag is private data on the released resource, so no stale entry is left behind.
  • The registry insert failure frees the block and releases the lock before returning, so the lock is not held on the error path.
  • attach_device sets removal_device only on success, and x2w_d3d12_permissive_create_device now returns E_OUTOFMEMORY. That is consistent with the header comment.
  • The test arms the failure for device creation and slots 8, 14, 16 and 55, and expects E_OUTOFMEMORY with a null output, then S_OK once disarmed. The control run never arms it and must print FAIL, which gives a negative control. The test does not cover the other callers (command list, placed buffer, committed, stream pipeline). Optional: extend it if cheap.
  • Minor: the committed-resource path leaves the title's allocation in place on failure. The PR says so openly.

Commands run (Linux, exact head): python3 tests/docs_links_check.py (ok, 19 files, 507 links), python3 tests/bidi_controls_check.py (ok), git diff --check origin/main...HEAD (clean). The C++ fixture builds only on Windows, so I ran no mutation here.

Not merged by me, for two reasons:

  • CI is still pending on this head and there is no Windows run on it yet.
  • The PR says the failure was seen in a startup run on the title. I have no title, so I cannot check that claim and I report fixture results only. The merge stays with MasterDomino or @R0Y00, who can check it on the title.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6067070581 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2656#pullrequestreview-6067070581). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T00:51:17Z. **Review:** COMMENTED --- Review of head a526db0e (comment only, no Win2025 run by me). What I read: `wrap()` null/bad_alloc handling and all new caller paths in `src/startup_stubs/d3d12_xs_device.cpp`, the `attach_device` ownership change, the new test and its CMake entries, and the docs change. - Each caller that gets a null wrapper releases the desktop object it just created and returns `E_OUTOFMEMORY` with a null output. I found no leaked reference. The budget tickets and `backing_budget->begin_use()` are taken only after `wrap()` succeeds, so nothing is left unbalanced on failure. The packed-format tag is private data on the released resource, so no stale entry is left behind. - The registry insert failure frees the block and releases the lock before returning, so the lock is not held on the error path. - `attach_device` sets `removal_device` only on success, and `x2w_d3d12_permissive_create_device` now returns `E_OUTOFMEMORY`. That is consistent with the header comment. - The test arms the failure for device creation and slots 8, 14, 16 and 55, and expects `E_OUTOFMEMORY` with a null output, then `S_OK` once disarmed. The `control` run never arms it and must print FAIL, which gives a negative control. The test does not cover the other callers (command list, placed buffer, committed, stream pipeline). Optional: extend it if cheap. - Minor: the committed-resource path leaves the title's allocation in place on failure. The PR says so openly. Commands run (Linux, exact head): `python3 tests/docs_links_check.py` (ok, 19 files, 507 links), `python3 tests/bidi_controls_check.py` (ok), `git diff --check origin/main...HEAD` (clean). The C++ fixture builds only on Windows, so I ran no mutation here. Not merged by me, for two reasons: - CI is still pending on this head and there is no Windows run on it yet. - The PR says the failure was seen in a startup run on the title. I have no title, so I cannot check that claim and I report fixture results only. The merge stays with MasterDomino or @R0Y00, who can check it on the title. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-26 02:04:56 +00:00
Not-Saii commented 2026-11-26 02:05:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Not-Saii on 2026-11-26T01:03:57Z.


Thanks for the review. Extended in 6864ec86: the fixture now also arms the failure for slot 9 (CreateCommandAllocator, which covers the shared create_slot path), slot 73 (command list) and slot 127 (committed resource). Each returns E_OUTOFMEMORY with a null output, then succeeds; the control reports all eight. With the slot 73 null check removed locally, the test crashed at slot 73.

I left out the placed-buffer, placed-texture and stream-pipeline paths: they need title memory or translated shaders to reach. The PR description lists which call sites are covered.

ctest --preset dev --no-tests=error -R "^startup_wrap_allocation" -V: 2/2 passed on WARP (Windows 11 26300.9974). The 84 related WARP stand-in tests still pass after a full dev build.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7179396853 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2656#issuecomment-7179396853). Originally posted by [Not-Saii](https://github.com/Not-Saii) on 2026-11-26T01:03:57Z. --- Thanks for the review. Extended in 6864ec86: the fixture now also arms the failure for slot 9 (CreateCommandAllocator, which covers the shared `create_slot` path), slot 73 (command list) and slot 127 (committed resource). Each returns `E_OUTOFMEMORY` with a null output, then succeeds; the control reports all eight. With the slot 73 null check removed locally, the test crashed at slot 73. I left out the placed-buffer, placed-texture and stream-pipeline paths: they need title memory or translated shaders to reach. The PR description lists which call sites are covered. `ctest --preset dev --no-tests=error -R "^startup_wrap_allocation" -V`: 2/2 passed on WARP (Windows 11 26300.9974). The 84 related WARP stand-in tests still pass after a full `dev` build. AI-assisted (Claude Code).
R0Y00 commented 2026-11-26 02:05:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-26T01:06:49Z.


I kept the additional allocator, command-list and committed-resource checks from 6864ec86 and pushed a small follow-up in 36081125. In a source-built test, creating a second device with the wrapper allocation forced to fail left the first device's live queue crashing on GetDevice. The follow-up publishes the new device and its debug state only after wrapping succeeds. The test now checks E_OUTOFMEMORY, a null second output, and the identity of the first device returned by the live queue.

Windows Release: ctest --preset release --no-tests=error -j 1 -R '^startup_wrap_allocation' -V passed both WARP tests, including the slot 55 negative control. After this merges, a repeat of the same 12 GiB case would tell us how the caller handles the returned HRESULT. The separate allocation pressure and placement failures remain tracked in #1199.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7179444793 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2656#issuecomment-7179444793). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-26T01:06:49Z. --- I kept the additional allocator, command-list and committed-resource checks from `6864ec86` and pushed a small follow-up in `36081125`. In a source-built test, creating a second device with the wrapper allocation forced to fail left the first device's live queue crashing on `GetDevice`. The follow-up publishes the new device and its debug state only after wrapping succeeds. The test now checks `E_OUTOFMEMORY`, a null second output, and the identity of the first device returned by the live queue. Windows Release: `ctest --preset release --no-tests=error -j 1 -R '^startup_wrap_allocation' -V` passed both WARP tests, including the slot 55 negative control. After this merges, a repeat of the same 12 GiB case would tell us how the caller handles the returned HRESULT. The separate allocation pressure and placement failures remain tracked in #1199.
Not-Saii commented 2026-11-26 02:05:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Not-Saii on 2026-11-26T01:19:47Z.


Repeat of the 12 GiB case after #2656 merged. In this run no wrapper allocation failed, so it does not show how the caller handles the new E_OUTOFMEMORY from wrap(). The first failures were D3D12 placement and page mapping instead.

Setup. Release build of main at f4c6998a (includes the #2656 merge 341215a9); ctest --preset release --no-tests=error -R "^startup_wrap_allocation" passed 2/2 on WARP. Windows 11 Pro 26300.9974, RTX 4090 (driver 32.0.16.1088, debug layer off), 32 GB RAM. Same switches as the earlier 12 GiB run, without lazy commit:

.\tools\startup\run.ps1 -Exe <run dir>\<title>.exe -DataDir G:\ -LogDir <log> -ShaderCache <warm cache> `
    -Seconds 420 -MemoryGiB 12 -Render -Translate -FakeUser -GpuMirror -HostCpu -StartupBelow40 -ContinueAsserts 1000 `
    -InputScript "4@4,4@8,...,4@200"

The cache is the same revision-17 cache as before, translated offline with --pending on this revision first. Input was scripted this time (A every 4 s from the first poll), where the earlier 12 GiB run was driven by hand. The title read 11 presses.

Process commit (DirectStorage heartbeat, MiB): 6,790 at 10 s, 12,005 at 40 s, 12,160 at 50 s, 12,223 at 80 s (last sample).

Failures, in log order:

  1. Device::slot133(CreatePlacedResourceX?) texture, 4096×4096, 11 mips → 0x8007000e, 8 times. The title went on.
  2. x2w-xmem GPU view mirror failed stage=map-view ... error=1455 for a 250-page range (count=0xfa ... -> 0 (err 1455)), once.
  3. XMemMapPhysicalPagesScatter failed stage=map-view ... error=1455, 4 times.
  4. Stop at 109.6 s: second-chance access violation, a write in the title's own code (+0x4da6341) to an address inside one of the scatter ranges that failed to map. Immediately before it, two first-chance read faults in d3d12_xs.dll touched the GPU-view range whose mirror failed; the stand-in handled them.

No x2w-d3d12 wrap(...): wrapper allocation failed line appeared, so the path #2656 changed was not reached. The run stopped before any ExecuteIndirect (0 calls), with 10 graphics pipelines translated and 1,357 textures uploaded.

Compared with the earlier 12 GiB run on d371ce46 (before #2656, controller input): that run stopped at 221.3 s inside d3d12_xs.dll on the unchecked wrapper allocation, at about 12.2 GiB. Here the cap was reached at the same point in loading, but the first failures were placement and page mapping, and the stop was the title writing to an unmapped page. So at this cap, which stage fails first varies between runs. One run; I have not repeated it.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7179664606 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2656#issuecomment-7179664606). Originally posted by [Not-Saii](https://github.com/Not-Saii) on 2026-11-26T01:19:47Z. --- Repeat of the 12 GiB case after #2656 merged. In this run no wrapper allocation failed, so it does not show how the caller handles the new `E_OUTOFMEMORY` from `wrap()`. The first failures were D3D12 placement and page mapping instead. **Setup.** Release build of `main` at `f4c6998a` (includes the #2656 merge `341215a9`); `ctest --preset release --no-tests=error -R "^startup_wrap_allocation"` passed 2/2 on WARP. Windows 11 Pro 26300.9974, RTX 4090 (driver 32.0.16.1088, debug layer off), 32 GB RAM. Same switches as the earlier 12 GiB run, without lazy commit: ```powershell .\tools\startup\run.ps1 -Exe <run dir>\<title>.exe -DataDir G:\ -LogDir <log> -ShaderCache <warm cache> ` -Seconds 420 -MemoryGiB 12 -Render -Translate -FakeUser -GpuMirror -HostCpu -StartupBelow40 -ContinueAsserts 1000 ` -InputScript "4@4,4@8,...,4@200" ``` The cache is the same revision-17 cache as before, translated offline with `--pending` on this revision first. Input was scripted this time (A every 4 s from the first poll), where the earlier 12 GiB run was driven by hand. The title read 11 presses. **Process commit** (DirectStorage heartbeat, MiB): 6,790 at 10 s, 12,005 at 40 s, 12,160 at 50 s, 12,223 at 80 s (last sample). **Failures, in log order:** 1. `Device::slot133(CreatePlacedResourceX?)` texture, 4096×4096, 11 mips → `0x8007000e`, 8 times. The title went on. 2. `x2w-xmem GPU view mirror failed stage=map-view ... error=1455` for a 250-page range (`count=0xfa ... -> 0 (err 1455)`), once. 3. `XMemMapPhysicalPagesScatter failed stage=map-view ... error=1455`, 4 times. 4. Stop at 109.6 s: second-chance access violation, a **write** in the title's own code (`+0x4da6341`) to an address inside one of the scatter ranges that failed to map. Immediately before it, two first-chance read faults in `d3d12_xs.dll` touched the GPU-view range whose mirror failed; the stand-in handled them. No `x2w-d3d12 wrap(...): wrapper allocation failed` line appeared, so the path #2656 changed was not reached. The run stopped before any `ExecuteIndirect` (0 calls), with 10 graphics pipelines translated and 1,357 textures uploaded. **Compared with the earlier 12 GiB run** on `d371ce46` (before #2656, controller input): that run stopped at 221.3 s inside `d3d12_xs.dll` on the unchecked wrapper allocation, at about 12.2 GiB. Here the cap was reached at the same point in loading, but the first failures were placement and page mapping, and the stop was the title writing to an unmapped page. So at this cap, which stage fails first varies between runs. One run; I have not repeated it. AI-assisted (Claude Code).
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!2648
No description provided.