runtime: check the CPU-SRV refresh refusals without a device #1707

Merged
Asimmimx merged 1 commit from upstream-pr/1714 into main 2026-11-24 17:45:04 +00:00
Asimmimx commented 2026-11-24 17:37:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-24T17:37:41Z.


Refs #1149.

What the header does

src/cpu_srv_upload.h defines cpu_srv::refresh. Before a submission that reads the SRV, it copies a CPU source into a caller-owned upload buffer. It refuses a null upload or source, a zero size, and a null fence with a nonzero value (E_INVALIDARG). It also refuses a resource that is not a buffer, a range outside the buffer and a heap that is not an upload heap. It returns a failing GetHeapProperties result as it is. It returns DXGI_ERROR_DEVICE_REMOVED for a fence at UINT64_MAX and DXGI_ERROR_WAS_STILL_DRAWING while the fence is short of the previous value. The source is staged through ReadProcessMemory first, so a source on an unreadable page is refused before the live upload changes. Finally it maps subresource 0 with an empty read range, copies the bytes and unmaps exactly the written range.

Tests that reach it

Only cpu_srv_refresh_{warp,hardware} and cpu_srv_stale_{warp,hardware} (tests/descriptor_window_submissions.cpp, in offscreen_check). They refresh a real 20-byte upload buffer with an exact fit at byte 16, then refuse an outstanding fence, a refresh one byte past the end, a SIZE_MAX offset and a source on an unreadable page (checked only with SUCCEEDED). Nothing checks the header without a device.

Mutation probe

30 single-point mutants of the header, one at a time, rebuilding offscreen_check and cpu_srv_upload_check and running ^(cpu_srv_(refresh|stale)_warp|cpu_srv_upload)$. The WARP column counts a mutant as killed if cpu_srv_refresh_warp or cpu_srv_stale_warp failed.

Mutant cpu_srv_{refresh,stale}_warp with cpu_srv_upload
drop !upload survived killed (crash)
drop !source survived killed
drop !bytes survived killed
drop !previous_fence && previous_value survived killed
drop the buffer-dimension check survived killed
destination_offset > Width to >= survived survived (equivalent)
drop destination_offset > Width killed killed
bytes > Width - offset to >= killed killed
bytes > Width - offset + 1 killed killed
failed GetHeapProperties returns E_INVALIDARG survived killed
failed GetHeapProperties ignored survived killed
drop the upload-heap check survived killed
heap check refuses only DEFAULT survived killed
drop the UINT64_MAX removed-device check survived killed
removed device returns WAS_STILL_DRAWING survived killed
done < previous_value to <= killed killed
done < previous_value to != survived killed
still drawing returns E_PENDING killed killed
ReadProcessMemory failure returns E_FAIL survived killed
drop copied != bytes survived survived (not observable)
memcpy from the source instead of ReadProcessMemory killed killed
failed Map returns S_OK survived killed
Map read range nullptr survived killed
memcpy ignores destination_offset killed killed
Unmap written range nullptr survived killed
written range starts at 0 survived killed
written range ends at bytes survived killed
drop Unmap survived killed
memcpy one byte short killed killed
heap refusal after a Map survived killed

WARP tests: 8 of 30 killed. With the new check: 28 of 30 killed. The two survivors:

  • destination_offset >= Width is equivalent. With destination_offset == Width, the nonzero size is refused by bytes > Width - destination_offset.
  • Dropping copied != bytes is not observable. ReadProcessMemory fails with ERROR_PARTIAL_COPY when a read crosses into an inaccessible page; it does not succeed with a short count.

New cases

tests/cpu_srv_upload_check.cpp drives the header with a fake upload resource (GetDesc, GetHeapProperties, Map and Unmap over 32 bytes of sentinel storage) and a fake fence with a fixed completed value. startup_packed_target_check fakes a texture the same way. Expected values are worked by hand.

Refreshed (5), each on a 24-byte upload. Each must return S_OK, write exactly the source bytes and leave every other byte at the sentinel. It must call Map once on subresource 0 with read range [0, 0), and Unmap once on subresource 0 with exactly [offset, offset + bytes):

  • first use with no fence, an exact fit at byte 16;
  • 8 bytes from byte 0;
  • a fence completed at the value (5 of 5);
  • a fence completed past the value (9 of 5);
  • a fence with value 0.

Refused (15), each with its exact result, no Unmap and unchanged bytes. Map may be called only when the fake Map fails:

  • E_INVALIDARG: null upload, null source, zero bytes, no fence with value 1, a 1D texture, offset 24 (the end) with 1 byte, offset 25, offset 17 with 8 bytes, a default heap, a readback heap;
  • E_FAIL from a failing GetHeapProperties;
  • DXGI_ERROR_WAS_STILL_DRAWING for a fence at 4 of 5;
  • DXGI_ERROR_DEVICE_REMOVED for a fence at UINT64_MAX;
  • E_OUTOFMEMORY from a failing Map;
  • HRESULT_FROM_WIN32(ERROR_PARTIAL_COPY) for an 8-byte source whose last 4 bytes lie on a PAGE_NOACCESS page.

Negative control

cpu_srv_upload_written_range_control runs cpu_srv_upload_check --mutate written-range with WILL_FAIL TRUE. The mutation expects every Unmap range to start at byte 0, so the check must fail:

FAIL first use, exact fit at byte 16: hr=0x00000000 contents=1 maps=1 unmaps=1 read=1[0,0) written=1[16,24), expected S_OK, one map with read [0,0) and written [0,24)
...
cpu_srv_upload: 4 FAILED

Not pinned

  • The exact error for a source that is entirely unreadable. It is whatever GetLastError reports after ReadProcessMemory, which the header passes through. The WARP tests already require a failure. Only the straddling case, which the ReadProcessMemory documentation defines as ERROR_PARTIAL_COPY, is pinned.
  • E_OUTOFMEMORY from the staging allocation, which cannot be reached without failing new (std::nothrow).
  • The order of the argument checks relative to each other. Each refusal is checked alone.
  • The two survivors above.

Commands and results

cmake --preset dev
cmake --build --preset dev --target cpu_srv_upload_check offscreen_check
ctest --preset dev --no-tests=error -R '^(cpu_srv_(refresh|stale)_warp|cpu_srv_upload(_written_range_control)?)$'
1/4 Test #106: cpu_srv_upload .........................   Passed    0.01 sec
2/4 Test #107: cpu_srv_upload_written_range_control ...   Passed    0.01 sec
3/4 Test #251: cpu_srv_refresh_warp ...................   Passed    0.09 sec
4/4 Test #252: cpu_srv_stale_warp .....................   Passed    0.08 sec
100% tests passed, 0 tests failed out of 4

cpu_srv_upload_check prints cpu_srv_upload: PASS (5 refreshed, 15 refused). python -B tests/docs_links_check.py passes (17 Markdown files, 364 links). No _hardware test was run for this change, and no capability skips occurred.

Environment

Windows 11 Pro 26300.9974, MSVC from Visual Studio, Windows SDK 10.0.26100.0, dev preset (Debug). The WARP tests use Microsoft WARP. The new check uses no device.

Commit: 62758977

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6587776658 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1714). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T17:37:41Z. --- Refs #1149. ## What the header does `src/cpu_srv_upload.h` defines `cpu_srv::refresh`. Before a submission that reads the SRV, it copies a CPU source into a caller-owned upload buffer. It refuses a null upload or source, a zero size, and a null fence with a nonzero value (`E_INVALIDARG`). It also refuses a resource that is not a buffer, a range outside the buffer and a heap that is not an upload heap. It returns a failing `GetHeapProperties` result as it is. It returns `DXGI_ERROR_DEVICE_REMOVED` for a fence at `UINT64_MAX` and `DXGI_ERROR_WAS_STILL_DRAWING` while the fence is short of the previous value. The source is staged through `ReadProcessMemory` first, so a source on an unreadable page is refused before the live upload changes. Finally it maps subresource 0 with an empty read range, copies the bytes and unmaps exactly the written range. ## Tests that reach it Only `cpu_srv_refresh_{warp,hardware}` and `cpu_srv_stale_{warp,hardware}` (`tests/descriptor_window_submissions.cpp`, in `offscreen_check`). They refresh a real 20-byte upload buffer with an exact fit at byte 16, then refuse an outstanding fence, a refresh one byte past the end, a `SIZE_MAX` offset and a source on an unreadable page (checked only with `SUCCEEDED`). Nothing checks the header without a device. ## Mutation probe 30 single-point mutants of the header, one at a time, rebuilding `offscreen_check` and `cpu_srv_upload_check` and running `^(cpu_srv_(refresh|stale)_warp|cpu_srv_upload)$`. The WARP column counts a mutant as killed if `cpu_srv_refresh_warp` or `cpu_srv_stale_warp` failed. | Mutant | `cpu_srv_{refresh,stale}_warp` | with `cpu_srv_upload` | |---|---|---| | drop `!upload` | survived | killed (crash) | | drop `!source` | survived | killed | | drop `!bytes` | survived | killed | | drop `!previous_fence && previous_value` | survived | killed | | drop the buffer-dimension check | survived | killed | | `destination_offset > Width` to `>=` | survived | survived (equivalent) | | drop `destination_offset > Width` | killed | killed | | `bytes > Width - offset` to `>=` | killed | killed | | `bytes > Width - offset + 1` | killed | killed | | failed `GetHeapProperties` returns `E_INVALIDARG` | survived | killed | | failed `GetHeapProperties` ignored | survived | killed | | drop the upload-heap check | survived | killed | | heap check refuses only `DEFAULT` | survived | killed | | drop the `UINT64_MAX` removed-device check | survived | killed | | removed device returns `WAS_STILL_DRAWING` | survived | killed | | `done < previous_value` to `<=` | killed | killed | | `done < previous_value` to `!=` | survived | killed | | still drawing returns `E_PENDING` | killed | killed | | `ReadProcessMemory` failure returns `E_FAIL` | survived | killed | | drop `copied != bytes` | survived | survived (not observable) | | `memcpy` from the source instead of `ReadProcessMemory` | killed | killed | | failed `Map` returns `S_OK` | survived | killed | | `Map` read range `nullptr` | survived | killed | | `memcpy` ignores `destination_offset` | killed | killed | | `Unmap` written range `nullptr` | survived | killed | | written range starts at 0 | survived | killed | | written range ends at `bytes` | survived | killed | | drop `Unmap` | survived | killed | | `memcpy` one byte short | killed | killed | | heap refusal after a `Map` | survived | killed | WARP tests: 8 of 30 killed. With the new check: 28 of 30 killed. The two survivors: - `destination_offset >= Width` is equivalent. With `destination_offset == Width`, the nonzero size is refused by `bytes > Width - destination_offset`. - Dropping `copied != bytes` is not observable. `ReadProcessMemory` fails with `ERROR_PARTIAL_COPY` when a read crosses into an inaccessible page; it does not succeed with a short count. ## New cases `tests/cpu_srv_upload_check.cpp` drives the header with a fake upload resource (`GetDesc`, `GetHeapProperties`, `Map` and `Unmap` over 32 bytes of sentinel storage) and a fake fence with a fixed completed value. `startup_packed_target_check` fakes a texture the same way. Expected values are worked by hand. Refreshed (5), each on a 24-byte upload. Each must return `S_OK`, write exactly the source bytes and leave every other byte at the sentinel. It must call `Map` once on subresource 0 with read range `[0, 0)`, and `Unmap` once on subresource 0 with exactly `[offset, offset + bytes)`: - first use with no fence, an exact fit at byte 16; - 8 bytes from byte 0; - a fence completed at the value (5 of 5); - a fence completed past the value (9 of 5); - a fence with value 0. Refused (15), each with its exact result, no `Unmap` and unchanged bytes. `Map` may be called only when the fake `Map` fails: - `E_INVALIDARG`: null upload, null source, zero bytes, no fence with value 1, a 1D texture, offset 24 (the end) with 1 byte, offset 25, offset 17 with 8 bytes, a default heap, a readback heap; - `E_FAIL` from a failing `GetHeapProperties`; - `DXGI_ERROR_WAS_STILL_DRAWING` for a fence at 4 of 5; - `DXGI_ERROR_DEVICE_REMOVED` for a fence at `UINT64_MAX`; - `E_OUTOFMEMORY` from a failing `Map`; - `HRESULT_FROM_WIN32(ERROR_PARTIAL_COPY)` for an 8-byte source whose last 4 bytes lie on a `PAGE_NOACCESS` page. ## Negative control `cpu_srv_upload_written_range_control` runs `cpu_srv_upload_check --mutate written-range` with `WILL_FAIL TRUE`. The mutation expects every `Unmap` range to start at byte 0, so the check must fail: ``` FAIL first use, exact fit at byte 16: hr=0x00000000 contents=1 maps=1 unmaps=1 read=1[0,0) written=1[16,24), expected S_OK, one map with read [0,0) and written [0,24) ... cpu_srv_upload: 4 FAILED ``` ## Not pinned - The exact error for a source that is entirely unreadable. It is whatever `GetLastError` reports after `ReadProcessMemory`, which the header passes through. The WARP tests already require a failure. Only the straddling case, which the `ReadProcessMemory` documentation defines as `ERROR_PARTIAL_COPY`, is pinned. - `E_OUTOFMEMORY` from the staging allocation, which cannot be reached without failing `new (std::nothrow)`. - The order of the argument checks relative to each other. Each refusal is checked alone. - The two survivors above. ## Commands and results ```powershell cmake --preset dev cmake --build --preset dev --target cpu_srv_upload_check offscreen_check ctest --preset dev --no-tests=error -R '^(cpu_srv_(refresh|stale)_warp|cpu_srv_upload(_written_range_control)?)$' ``` ``` 1/4 Test #106: cpu_srv_upload ......................... Passed 0.01 sec 2/4 Test #107: cpu_srv_upload_written_range_control ... Passed 0.01 sec 3/4 Test #251: cpu_srv_refresh_warp ................... Passed 0.09 sec 4/4 Test #252: cpu_srv_stale_warp ..................... Passed 0.08 sec 100% tests passed, 0 tests failed out of 4 ``` `cpu_srv_upload_check` prints `cpu_srv_upload: PASS (5 refreshed, 15 refused)`. `python -B tests/docs_links_check.py` passes (17 Markdown files, 364 links). No `_hardware` test was run for this change, and no capability skips occurred. ## Environment Windows 11 Pro 26300.9974, MSVC from Visual Studio, Windows SDK 10.0.26100.0, `dev` preset (Debug). The WARP tests use Microsoft WARP. The new check uses no device. Commit: `62758977` AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 18:02:44 +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!1707
No description provided.