gpu: compare one linear buffer through sharing, copying and refused sharing #1935

Merged
MasterDomino merged 1 commit from upstream-pr/1942 into main 2026-11-24 22:08:07 +00:00
MasterDomino commented 2026-11-24 21:57:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-24T21:57:55Z.


What

This PR adds tests/unified_buffer_paths_check.cpp, a source-built, test-only fixture. It is registered as unified_buffer_paths_{warp,hardware} (SKIP_RETURN_CODE 77) next to the #874 VM tests, with three WILL_FAIL controls on WARP. It runs one ordinary linear buffer that the CPU writes and the GPU reads through four paths with the same cases. docs/experiments.md gains a section next to the #874 VM sections. No production code changes, and no stand-in's default behaviour changes.

Why

In #1914, the decision was a hybrid model whose first slice is one linear CPU-written, GPU-read buffer. It asked for that buffer to be run through real sharing, forced copying and forced sharing failure, with the source-built heap and cross-queue controls from #874. The cases are: bind A and write B before submission; a GPU write seen by the CPU alias after the fence; a submission held in flight whose backing is not reused; and missing refresh or early readback giving stale data or a defined refusal. It asked for the bytes copied, the backing, the HRESULTs and skips. This PR is that comparison. The first buffer change can then be reviewed on its own.

Refs #1914
Refs #874

Fixture design

Title memory, following XMem (src/xmem_mapped_pages.cpp): The section pool is a 256 MiB pagefile section of 64 KiB slots. Each allocation takes two slots, mapped as two page views on a placeholder split at the page boundary. The region the GPU reads straddles the split. The private pool is a committed 1 MiB VirtualAlloc range, the only kind slot 133 aliases. Slots 0-2 hold neighbouring data. Every case first runs a previous user (D) on slots 3-4 through the same path and frees it. The buffer under test therefore sits at heap offset 0x30000 on a reused slot, and that slot must read zero through the title's views before the title fills it. Freed slots stay retired until the fence of their last submission.

Paths:

  • share: one heap per pool. The section uses OpenExistingHeapFromFileMapping. The private range uses OpenExistingHeapFromAddress on its base, after D3D12_FEATURE_EXISTING_HEAPS. A cross-adapter buffer is placed at the slot offset, and nothing is copied.
  • copy: a default buffer fed from a two-slot upload ring. The ring wraps like reserve_upload_ring, but a slot still in flight is refused with DXGI_ERROR_WAS_STILL_DRAWING. The named copy point copies title memory into the ring at bind (as upload_from_title), at record (as frame_copy) or at submit, just before ExecuteCommandLists, which is the contract point. The ring slot is copied into the buffer at the start of the submission. When a use declares GPU writes, the buffer is copied to readback at the end, and completion of that fence copies readback to title memory.
  • fallback: sharing is forced to fail and the actual HRESULT is recorded. The section uses a FILE_MAP_READ handle; the private range uses an interior address. The path falls back to copy only when the copy can keep the guarantees. A GPU-written use with no completion point is refused with the sharing HRESULT.
  • slot133: a model of today's slot 133 fallback. A non-MEM_PRIVATE address gets E_INVALIDARG before D3D12, and a committed default buffer stands in with S_OK and no copy. The oracle must catch this as a success whose backing holds neither the title's bytes nor a refusal.

Cases:

  • a: Bind with A in memory, then write B after recording and before ExecuteCommandLists. The GPU must read B.
  • b: The GPU writes G ^ key. After the fence, the alias must hold it and the untouched words must keep their values.
  • c: X's submission waits on a gate fence. The title frees X and allocates Y (C), whose submission is also held. On the copy paths, a third allocation wraps the ring onto X's in-flight slot.
  • d1b, d1r: As case a at the bind and record copy points. The copy paths must read A.
  • d2: As case b, held on the gate. The queue Wait orders the early query before the GPU write. The oracle is DXGI_ERROR_WAS_STILL_DRAWING, and the P words are its consequence.
  • e: The fallback eligibility rule.

Distinct sentinels (A, B, C, D, E, G, P, filler, neighbour) make every observed word attributable.

Results

On commit 0e814819 (based on main 82f0db65), Windows 11 build 26300, the D3D12 debug layer was tested off and on (X2W_TEST_DEBUG_LAYER=1). The results are identical on in-box WARP (1414:008c, 10.0.26100.9818) and on an AMD Radeon RX 9070 XT (1002:7550, driver 32.0.31044.16). D3D12_FEATURE_EXISTING_HEAPS is 1 on both. The run reports 40 passed, 0 failed, 0 skipped, 2 slot133 stand-ins caught, 0 probe failures, device removed reason S_OK, and 0 debug-layer errors outside the deliberate refusals.

Path Pool Backing Sharing HRESULT Bytes: title → ring / GPU copies / readback → title Results
share section, private title memory, heap offset 0x30000 S_OK 0 / 0 / 0 a: B 1024/1024. b: G^key 1024/1024, A kept, filler 30720/30720. c: B and C, Y on slots 5-6. d1b/d1r: B. d2: 0x887A000A, P 1024/1024, then G^key
copy section, private default buffer + ring slot 1 n/a a 131072 / 131072 / 0; b 131072 / 262144 / 131072; c 262144 / 262144 / 0 a: B. b: G^key. c: B and C, wrap refused 0x887A000A, ring slot 1 reused after the fence. d1b/d1r: A 1024/1024. d2: 0x887A000A, then G^key
fallback section as copy 0x80070057 (FILE_MAP_READ handle) as copy as copy; e: GPU-written refused with 0x80070057, no resource; GPU-read falls back
fallback private as copy 0x80070057 (interior address) as copy as copy; e as above
slot133 section committed default buffer, not aliased E_INVALIDARG before D3D12 0 / 0 / 0 caught: a reads zero 1024/1024 with S_OK; b leaves P with S_OK
slot133 private aliased through the allocation base S_OK 0 / 0 / 0 a, b pass (as share)

Skips: none on either adapter.

Agreement: the paths agree on bytes and lifetime at the points the contract names (submit and fence completion). They do not agree at the production copy points. At bind and record, the copy paths read the stale A while sharing reads B, so today's bind-time and record-time copies cannot stand in for sharing when the title writes between bind and ExecuteCommandLists.

Probes:

  • Refusals (asserted): all return 0x80070057 on both adapters: OpenExistingHeapFromAddress on an interior address or a reserve-only range; OpenExistingHeapFromFileMapping through a FILE_MAP_READ handle; and CreatePlacedResource at offset 0x31000 or past the end of the 256 MiB heap.
  • Finding: OpenExistingHeapFromAddress accepts a 128 KiB MEM_MAPPED view of the section at +0x30000 on both adapters. A buffer placed on that heap read the view's CPU writes in 1024/1024 words, and its GPU writes appeared through the split run in 1024/1024 words. Slot 133 refuses MEM_MAPPED before calling D3D12.
  • Finding: on the first view of a split run, OpenExistingHeapFromAddress gives a 64 KiB heap. A 128 KiB buffer across the split is refused with 0x80070057.
  • Finding (stale-cache row): with a heap from a private range kept alive, as slot 133's existing_heaps cache keeps it, VirtualFree(MEM_RELEASE) and VirtualFree(MEM_DECOMMIT) both fail with error 87 on both adapters. The range therefore cannot be recommitted at the same base. After the heap is released, VirtualFree succeeds. With a cache that is never cleared, the observed hazard is that the title's free of that range fails. A later allocation reading a stale heap was not observed.

Commands (PowerShell, after . .\tools\dev.ps1):

cmake --preset dev -DINTAKE_PYTHON=<repo>/out/pe-env/Scripts/python.exe
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^unified_buffer_paths_' -V        # 5/5 passed
$env:X2W_TEST_DEBUG_LAYER='1'; out\build\dev\unified_buffer_paths_check.exe warp      # PASS
$env:X2W_TEST_DEBUG_LAYER='1'; out\build\dev\unified_buffer_paths_check.exe hardware  # PASS
ctest --preset dev --no-tests=error -R '^(vm_|era_|xmem|unified_buffer_paths)' -j 4   # 82/82 passed
cmake --preset release; cmake --build --preset release --target unified_buffer_paths_check
ctest --preset release --no-tests=error -R '^unified_buffer_paths_'       # 5/5 passed
python -B tests/docs_links_check.py; python -B tests/bidi_controls_check.py  # pass

Negative controls

On both adapters, each mutant must fail every case that its guarantee protects. The run exits 1 only then. The controls are registered on WARP as WILL_FAIL, and skip if a targeted case could not run.

  • --mutate skip-refresh (copy point bind): copy and fallback case a read A=1024/1024. 4 of 4 fail.
  • --mutate skip-copy-back: copy and fallback cases b and d2 leave raw P=1024/1024 in the alias, with readback → title = 0. 8 of 8 fail.
  • --mutate reuse-early (no fence checks on slots or the ring): on share, Y gets X's slots 3-4 and the held submission reads C=1024/1024. On copy and fallback, the wrap takes X's ring slot (taken) and the held submission reads E=1024/1024. 6 of 6 fail.
  • The slot133 section rows act as built-in controls for the oracle: a stand-in with S_OK is caught in both cases.

Not covered

  • Unsynchronized concurrent CPU and GPU writes, which are outside the contract. Because of this, the fixture does not exercise the copy path's whole-buffer copy-back clobbering a concurrent CPU write.
  • Textures, render targets and acceleration structures, multiple queues, and dirty-range tracking. The copy path copies the whole 128 KiB.
  • The real title device and the stand-ins. Nothing is wired into slot 133, upload_from_title or frame_copy. #1860 and slot 148 are unchanged.
  • Whether OpenExistingHeapFromAddress over MEM_MAPPED views is supported by contract on other adapters, and what happens when the view is unmapped while that heap lives.
  • A stale read through a cached heap. On these two adapters it could not be produced, because the free fails first.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6592225233 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1942). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-24T21:57:55Z. --- ## What This PR adds `tests/unified_buffer_paths_check.cpp`, a source-built, test-only fixture. It is registered as `unified_buffer_paths_{warp,hardware}` (`SKIP_RETURN_CODE 77`) next to the #874 VM tests, with three `WILL_FAIL` controls on WARP. It runs one ordinary linear buffer that the CPU writes and the GPU reads through four paths with the same cases. `docs/experiments.md` gains a section next to the #874 VM sections. No production code changes, and no stand-in's default behaviour changes. ## Why In #1914, the decision was a hybrid model whose first slice is one linear CPU-written, GPU-read buffer. It asked for that buffer to be run through real sharing, forced copying and forced sharing failure, with the source-built heap and cross-queue controls from #874. The cases are: bind A and write B before submission; a GPU write seen by the CPU alias after the fence; a submission held in flight whose backing is not reused; and missing refresh or early readback giving stale data or a defined refusal. It asked for the bytes copied, the backing, the HRESULTs and skips. This PR is that comparison. The first buffer change can then be reviewed on its own. Refs #1914 Refs #874 ## Fixture design **Title memory, following XMem (`src/xmem_mapped_pages.cpp`):** The section pool is a 256 MiB pagefile section of 64 KiB slots. Each allocation takes two slots, mapped as two page views on a placeholder split at the page boundary. The region the GPU reads straddles the split. The private pool is a committed 1 MiB `VirtualAlloc` range, the only kind slot 133 aliases. Slots 0-2 hold neighbouring data. Every case first runs a previous user (D) on slots 3-4 through the same path and frees it. The buffer under test therefore sits at heap offset `0x30000` on a reused slot, and that slot must read zero through the title's views before the title fills it. Freed slots stay retired until the fence of their last submission. **Paths:** - `share`: one heap per pool. The section uses `OpenExistingHeapFromFileMapping`. The private range uses `OpenExistingHeapFromAddress` on its base, after `D3D12_FEATURE_EXISTING_HEAPS`. A cross-adapter buffer is placed at the slot offset, and nothing is copied. - `copy`: a default buffer fed from a two-slot upload ring. The ring wraps like `reserve_upload_ring`, but a slot still in flight is refused with `DXGI_ERROR_WAS_STILL_DRAWING`. The named copy point copies title memory into the ring at `bind` (as `upload_from_title`), at `record` (as `frame_copy`) or at `submit`, just before `ExecuteCommandLists`, which is the contract point. The ring slot is copied into the buffer at the start of the submission. When a use declares GPU writes, the buffer is copied to readback at the end, and completion of that fence copies readback to title memory. - `fallback`: sharing is forced to fail and the actual HRESULT is recorded. The section uses a `FILE_MAP_READ` handle; the private range uses an interior address. The path falls back to copy only when the copy can keep the guarantees. A GPU-written use with no completion point is refused with the sharing HRESULT. - `slot133`: a model of today's slot 133 fallback. A non-`MEM_PRIVATE` address gets `E_INVALIDARG` before D3D12, and a committed default buffer stands in with `S_OK` and no copy. The oracle must catch this as a success whose backing holds neither the title's bytes nor a refusal. **Cases:** - **a:** Bind with A in memory, then write B after recording and before `ExecuteCommandLists`. The GPU must read B. - **b:** The GPU writes G ^ key. After the fence, the alias must hold it and the untouched words must keep their values. - **c:** X's submission waits on a gate fence. The title frees X and allocates Y (C), whose submission is also held. On the copy paths, a third allocation wraps the ring onto X's in-flight slot. - **d1b, d1r:** As case a at the bind and record copy points. The copy paths must read A. - **d2:** As case b, held on the gate. The queue `Wait` orders the early query before the GPU write. The oracle is `DXGI_ERROR_WAS_STILL_DRAWING`, and the P words are its consequence. - **e:** The fallback eligibility rule. Distinct sentinels (A, B, C, D, E, G, P, filler, neighbour) make every observed word attributable. ## Results On commit `0e814819` (based on `main` `82f0db65`), Windows 11 build 26300, the D3D12 debug layer was tested off and on (`X2W_TEST_DEBUG_LAYER=1`). The results are identical on in-box WARP (`1414:008c`, 10.0.26100.9818) and on an AMD Radeon RX 9070 XT (`1002:7550`, driver 32.0.31044.16). `D3D12_FEATURE_EXISTING_HEAPS` is 1 on both. The run reports 40 passed, 0 failed, 0 skipped, 2 slot133 stand-ins caught, 0 probe failures, device removed reason `S_OK`, and 0 debug-layer errors outside the deliberate refusals. | Path | Pool | Backing | Sharing HRESULT | Bytes: title → ring / GPU copies / readback → title | Results | |---|---|---|---|---|---| | share | section, private | title memory, heap offset `0x30000` | `S_OK` | 0 / 0 / 0 | a: B 1024/1024. b: G^key 1024/1024, A kept, filler 30720/30720. c: B and C, Y on slots 5-6. d1b/d1r: B. d2: `0x887A000A`, P 1024/1024, then G^key | | copy | section, private | default buffer + ring slot 1 | n/a | a 131072 / 131072 / 0; b 131072 / 262144 / 131072; c 262144 / 262144 / 0 | a: B. b: G^key. c: B and C, wrap refused `0x887A000A`, ring slot 1 reused after the fence. d1b/d1r: A 1024/1024. d2: `0x887A000A`, then G^key | | fallback | section | as copy | `0x80070057` (`FILE_MAP_READ` handle) | as copy | as copy; e: GPU-written refused with `0x80070057`, no resource; GPU-read falls back | | fallback | private | as copy | `0x80070057` (interior address) | as copy | as copy; e as above | | slot133 | section | committed default buffer, not aliased | `E_INVALIDARG` before D3D12 | 0 / 0 / 0 | caught: a reads zero 1024/1024 with `S_OK`; b leaves P with `S_OK` | | slot133 | private | aliased through the allocation base | `S_OK` | 0 / 0 / 0 | a, b pass (as share) | Skips: none on either adapter. **Agreement:** the paths agree on bytes and lifetime at the points the contract names (submit and fence completion). They do **not** agree at the production copy points. At `bind` and `record`, the copy paths read the stale A while sharing reads B, so today's bind-time and record-time copies cannot stand in for sharing when the title writes between bind and `ExecuteCommandLists`. **Probes:** - **Refusals (asserted):** all return `0x80070057` on both adapters: `OpenExistingHeapFromAddress` on an interior address or a reserve-only range; `OpenExistingHeapFromFileMapping` through a `FILE_MAP_READ` handle; and `CreatePlacedResource` at offset `0x31000` or past the end of the 256 MiB heap. - **Finding:** `OpenExistingHeapFromAddress` accepts a 128 KiB `MEM_MAPPED` view of the section at `+0x30000` on both adapters. A buffer placed on that heap read the view's CPU writes in 1024/1024 words, and its GPU writes appeared through the split run in 1024/1024 words. Slot 133 refuses `MEM_MAPPED` before calling D3D12. - **Finding:** on the first view of a split run, `OpenExistingHeapFromAddress` gives a 64 KiB heap. A 128 KiB buffer across the split is refused with `0x80070057`. - **Finding (stale-cache row):** with a heap from a private range kept alive, as slot 133's `existing_heaps` cache keeps it, `VirtualFree(MEM_RELEASE)` and `VirtualFree(MEM_DECOMMIT)` both fail with error 87 on both adapters. The range therefore cannot be recommitted at the same base. After the heap is released, `VirtualFree` succeeds. With a cache that is never cleared, the observed hazard is that the title's free of that range fails. A later allocation reading a stale heap was not observed. Commands (PowerShell, after `. .\tools\dev.ps1`): ``` cmake --preset dev -DINTAKE_PYTHON=<repo>/out/pe-env/Scripts/python.exe cmake --build --preset dev ctest --preset dev --no-tests=error -R '^unified_buffer_paths_' -V # 5/5 passed $env:X2W_TEST_DEBUG_LAYER='1'; out\build\dev\unified_buffer_paths_check.exe warp # PASS $env:X2W_TEST_DEBUG_LAYER='1'; out\build\dev\unified_buffer_paths_check.exe hardware # PASS ctest --preset dev --no-tests=error -R '^(vm_|era_|xmem|unified_buffer_paths)' -j 4 # 82/82 passed cmake --preset release; cmake --build --preset release --target unified_buffer_paths_check ctest --preset release --no-tests=error -R '^unified_buffer_paths_' # 5/5 passed python -B tests/docs_links_check.py; python -B tests/bidi_controls_check.py # pass ``` ## Negative controls On both adapters, each mutant must fail every case that its guarantee protects. The run exits 1 only then. The controls are registered on WARP as `WILL_FAIL`, and skip if a targeted case could not run. - `--mutate skip-refresh` (copy point `bind`): copy and fallback case a read `A=1024/1024`. 4 of 4 fail. - `--mutate skip-copy-back`: copy and fallback cases b and d2 leave `raw P=1024/1024` in the alias, with readback → title = 0. 8 of 8 fail. - `--mutate reuse-early` (no fence checks on slots or the ring): on share, Y gets X's slots 3-4 and the held submission reads `C=1024/1024`. On copy and fallback, the wrap takes X's ring slot `(taken)` and the held submission reads `E=1024/1024`. 6 of 6 fail. - The slot133 section rows act as built-in controls for the oracle: a stand-in with `S_OK` is caught in both cases. ## Not covered - Unsynchronized concurrent CPU and GPU writes, which are outside the contract. Because of this, the fixture does not exercise the copy path's whole-buffer copy-back clobbering a concurrent CPU write. - Textures, render targets and acceleration structures, multiple queues, and dirty-range tracking. The copy path copies the whole 128 KiB. - The real title device and the stand-ins. Nothing is wired into slot 133, `upload_from_title` or `frame_copy`. #1860 and slot 148 are unchanged. - Whether `OpenExistingHeapFromAddress` over `MEM_MAPPED` views is supported by contract on other adapters, and what happens when the view is unmapped while that heap lives. - A stale read through a cached heap. On these two adapters it could not be produced, because the free fails first. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 23:03:09 +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!1935
No description provided.