Skip to content

startup: copy guest memory with guarded_memcpy in ring_copy - #3469

Merged
R0Y00 merged 4 commits into
R0Y00:mainfrom
Leyukaka:runtime-ring-copy-guarded
Nov 30, 2026
Merged

R0Y00 merged 4 commits into
R0Y00:mainfrom
Leyukaka:runtime-ring-copy-guarded

Conversation

@Leyukaka

Copy link
Copy Markdown
Collaborator

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 7259bea (base cf0c30a):

  • 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.

# Conflicts:
#	src/startup_stubs/d3d12_xs_device.cpp

@lukaslw-lab lukaslw-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 7259bea", 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.

@R0Y00
R0Y00 merged commit 356a442 into R0Y00:main Nov 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

startup: ring_copy reads guest memory with an unguarded memcpy after the probe

3 participants