startup: copy guest memory with guarded_memcpy in ring_copy #3469

Merged
Leyukaka merged 4 commits from upstream-pr/3469 into main 2026-11-30 19:58:32 +00:00
Leyukaka commented 2026-11-29 16:11:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T16:11:44Z.


Fixes #3468.

Change

ring_copy (src/startup_stubs/d3d12_xs_device.cpp:2885) copied guest memory with a plain std::memcpy. A page decommitted or protected after the caller's readable_window probe faulted inside the stand-in.

  • The copy now goes through x2w_local::guarded_memcpy(..., false), which returns false on an access violation or an in-page error.
  • *offset and *resource are cleared on entry and written only after the copy succeeds, so a refused copy never publishes a partially copied range.
  • A refused copy does not roll back its reservation: the ring bytes stay consumed until the normal chunk reuse.
  • A refusal logs one line, limited to the first 8 occurrences.

No default changes.

Callers

All three already handle a false return, and none is modified:

Call site On false
src/startup_stubs/d3d12_draw.inl:2065, texture upload state.done = false, the upload is retried later
src/startup_stubs/d3d12_draw.inl:2686, compute input snapshot refuse(...), the existing "could not be copied" path
src/startup_stubs/d3d12_xs_device.cpp:2906, upload_from_title returns 0; its caller returns on 0

Fixture

offscreen_check --ring-copy-fault reserves one page with MEM_RESERVE / PAGE_NOACCESS and calls x2w_test_ring_copy (compiled under X2W_RING_COPY_TEST). The fixture calls ring_copy directly, without the callers' probe, so the copy faults inside the guard. The fixture does three 1-byte snapshot copies into a 1536-byte chunk:

  1. from a readable byte: succeeds, byte present at offset 0;
  2. from the reserved page: returns false, offset stays 0;
  3. from a readable byte again: succeeds at offset 1024, byte present.

It is registered as startup_ring_copy_fault in tests/cmake/startup_ring_copy.cmake; the root CMakeLists.txt is unchanged.

Tests

On 7259bea9 (base cf0c30af):

  • startup_ring_copy_fault: passed.
  • startup_local_log* (7): passed.
  • upload_reservation, startup_upload_arena_{warp,hardware}, startup_upload_lifetime_{warp,hardware}, startup_input_view_token, stream_root_srv_{warp,hardware}: passed.

Mutations, each restored afterwards:

Mutation startup_ring_copy_fault
guarded_memcpy replaced by std::memcpy crashes (SegFault)
*offset = reservation.offset moved before the copy fails: "ring-copy fixture failed the refusal or adjacent copy checks"

Not covered

  • The timing between a successful probe and a later fault is not reproduced. The fixture tests the copy itself on a page that is never readable.
  • The fixture passes resource as nullptr, so only the offset publication is checked.
  • The fixture hard-codes the 512-byte reservation rounding (offset 1024, 1536-byte chunk) and the absence of rollback.
  • The Python-driven snapshot and compute input tests were not run here.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5552564311 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3469). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T16:11:44Z. --- Fixes #3468. ### Change `ring_copy` (`src/startup_stubs/d3d12_xs_device.cpp:2885`) copied guest memory with a plain `std::memcpy`. A page decommitted or protected after the caller's `readable_window` probe faulted inside the stand-in. - The copy now goes through `x2w_local::guarded_memcpy(..., false)`, which returns false on an access violation or an in-page error. - `*offset` and `*resource` are cleared on entry and written only after the copy succeeds, so a refused copy never publishes a partially copied range. - A refused copy does not roll back its reservation: the ring bytes stay consumed until the normal chunk reuse. - A refusal logs one line, limited to the first 8 occurrences. No default changes. ### Callers All three already handle a false return, and none is modified: | Call site | On false | | --- | --- | | `src/startup_stubs/d3d12_draw.inl:2065`, texture upload | `state.done = false`, the upload is retried later | | `src/startup_stubs/d3d12_draw.inl:2686`, compute input snapshot | `refuse(...)`, the existing "could not be copied" path | | `src/startup_stubs/d3d12_xs_device.cpp:2906`, `upload_from_title` | returns 0; its caller returns on 0 | ### Fixture `offscreen_check --ring-copy-fault` reserves one page with `MEM_RESERVE` / `PAGE_NOACCESS` and calls `x2w_test_ring_copy` (compiled under `X2W_RING_COPY_TEST`). The fixture calls `ring_copy` directly, without the callers' probe, so the copy faults inside the guard. The fixture does three 1-byte snapshot copies into a 1536-byte chunk: 1. from a readable byte: succeeds, byte present at offset 0; 2. from the reserved page: returns false, `offset` stays 0; 3. from a readable byte again: succeeds at offset 1024, byte present. It is registered as `startup_ring_copy_fault` in `tests/cmake/startup_ring_copy.cmake`; the root `CMakeLists.txt` is unchanged. ### Tests On 7259bea9 (base cf0c30af): - `startup_ring_copy_fault`: passed. - `startup_local_log*` (7): passed. - `upload_reservation`, `startup_upload_arena_{warp,hardware}`, `startup_upload_lifetime_{warp,hardware}`, `startup_input_view_token`, `stream_root_srv_{warp,hardware}`: passed. Mutations, each restored afterwards: | Mutation | `startup_ring_copy_fault` | | --- | --- | | `guarded_memcpy` replaced by `std::memcpy` | crashes (SegFault) | | `*offset = reservation.offset` moved before the copy | fails: "ring-copy fixture failed the refusal or adjacent copy checks" | ### Not covered - The timing between a successful probe and a later fault is not reproduced. The fixture tests the copy itself on a page that is never readable. - The fixture passes `resource` as `nullptr`, so only the `offset` publication is checked. - The fixture hard-codes the 512-byte reservation rounding (offset 1024, 1536-byte chunk) and the absence of rollback. - The Python-driven snapshot and compute input tests were not run here.
lukaslw-lab commented 2026-11-30 08:54:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T08:54:11Z.

Review: APPROVED


Reviewed 766f1e30 by reading the diff; not built or run.

Verdict: approve. A fault now refuses the copy instead of crashing, and no partial range is published. One SHOULD, on the PR body only. No MUST.

  • Outputs are cleared on entry and set only after guarded_memcpy succeeds (d3d12_xs_device.cpp:2887-2898). All three callers treat false as a refusal and never read the cleared values (d3d12_draw.inl:2079, :2700, d3d12_xs_device.cpp:2906). #3481 moves the :2700 call into its deferred loop with the same || !view.resource check, so the two combine cleanly.
  • Test seam: X2W_RING_COPY_TEST goes on d3d12_xs_device_obj, which only the test executables link (offscreen_check, cpu_placed_dxr_provider, startup_present_log). The else branch matches the OFFSCREEN_AGILITY_620_DIR build, where offscreen_check compiles the file itself (CMakeLists.txt:2181-2187). So the fixture always links the seam, and the shipped DLL never contains it.
  • SHOULD: the body describes an older head. Tests ran "on 7259bea9", before c8e24e1d added the straddling-prefix refusal and the resource checks, and before the merge. The fixture section lists three copies with the last at offset 1024, while the code makes four and checks 1536 (:8188). "Not covered" still says resource is passed as nullptr, which c8e24e1d fixed. Please rerun at 766f1e30 and update the counts.

Tests: startup_ring_copy_fault covers a fault on a never-readable page, a fault after a 16-byte readable prefix (so guarded_memcpy really starts copying), and that neither output is published. The std::memcpy mutation shows the guard is needed. CI did not run it: it is not in the pr preset's list, and /O2 was skipped on the PR.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6109203461 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3469#pullrequestreview-6109203461). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T08:54:11Z. **Review:** APPROVED --- Reviewed `766f1e30` by reading the diff; not built or run. **Verdict: approve.** A fault now refuses the copy instead of crashing, and no partial range is published. One SHOULD, on the PR body only. No MUST. - Outputs are cleared on entry and set only after `guarded_memcpy` succeeds (`d3d12_xs_device.cpp:2887`-`2898`). All three callers treat false as a refusal and never read the cleared values (`d3d12_draw.inl:2079`, `:2700`, `d3d12_xs_device.cpp:2906`). #3481 moves the `:2700` call into its deferred loop with the same `|| !view.resource` check, so the two combine cleanly. - Test seam: `X2W_RING_COPY_TEST` goes on `d3d12_xs_device_obj`, which only the test executables link (`offscreen_check`, `cpu_placed_dxr_provider`, `startup_present_log`). The `else` branch matches the `OFFSCREEN_AGILITY_620_DIR` build, where `offscreen_check` compiles the file itself (`CMakeLists.txt:2181`-`2187`). So the fixture always links the seam, and the shipped DLL never contains it. - **SHOULD:** the body describes an older head. Tests ran "on 7259bea9", before `c8e24e1d` added the straddling-prefix refusal and the `resource` checks, and before the merge. The fixture section lists three copies with the last at offset 1024, while the code makes four and checks 1536 (`:8188`). "Not covered" still says `resource` is passed as `nullptr`, which `c8e24e1d` fixed. Please rerun at `766f1e30` and update the counts. Tests: `startup_ring_copy_fault` covers a fault on a never-readable page, a fault after a 16-byte readable prefix (so `guarded_memcpy` really starts copying), and that neither output is published. The `std::memcpy` mutation shows the guard is needed. CI did not run it: it is not in the `pr` preset's list, and `/O2` was skipped on the PR.
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!3469
No description provided.