Skip to content

KernelX, D3D12.X, DirectStorage: opt-in XCL_GUEST_WRITE_GUARD for XCL-protected title pages - #3597

Open
iggyvilla wants to merge 1 commit into
xcl-dev:amd-devfrom
iggyvilla:amd-guest-write-guard
Open

iggyvilla wants to merge 1 commit into
xcl-dev:amd-devfrom
iggyvilla:amd-guest-write-guard

Conversation

@iggyvilla

Copy link
Copy Markdown
Collaborator

Adds opt-in XCL_GUEST_WRITE_GUARD=1 (off by default: zero behavior change) so XCL and Windows kernel-mode writes (ReadFile, NtWriteVirtualMemory, recv/recvfrom, and XCL's own writability pre-checks) remain safe when XCL protects title pages as PAGE_READONLY or PAGE_NOACCESS (for XCL_PAGE_ALIAS=1 guarded views or future read-only/no-access page protectors).

TL;DR — Before vs After (RX 6650 XT 8 GB, origin/amd-dev 41aa60ce)

Scenario Metric Before (XCL_GUEST_WRITE_GUARD off) After (XCL_GUEST_WRITE_GUARD=1) Delta
Kernel write (NtWriteVirtualMemory / ReadFile) into XCL-protected RO/NA page Write outcome (guest_write_probe & unit tests) Fails silently (ERROR_NOACCESS / STATUS_PARTIAL_COPY, 0 faults, 0 bytes landed) Lands cleanly (pinned + 1 user-mode fault/page → protector opens page) Silent kernel-write loss eliminated
Socket receive (recv / recvfrom) into XCL-protected or MEM_WRITE_WATCH page Write & dirty tracking WSAEFAULT on RO/NA; TCP recv misses MEM_WRITE_WATCH Bounces through XCL buffer + user-mode store Safe + tracked
60 s smoke (1b6f6748, default off vs main d8bd9a38) Frames shown / Peak commit 1,618 / 15,479 MB (main) 1,655 / 16,648 MB (off) +2.3% frames / 1.076x commit
60 s smoke (1b6f6748, opt-in on) Frames shown / Peak commit 1,655 / 16,648 MB (off) 1,657 / 16,226 MB (=1) +0.1% frames / 0 crashes / 0 warnings

Motivation & empirical OS probe (crates/xcl/tests/guest_write_probe.rs)
User-mode stores into a PAGE_READONLY or PAGE_NOACCESS page raise an access violation that a vectored handler can repair, but Windows kernel-mode writes do not fault in user mode:

  • ReadFile (buffered, NO_BUFFERING, and OVERLAPPED) fails synchronously with ERROR_NOACCESS (998), writing 0 bytes;
  • NtReadFile fails with STATUS_ACCESS_VIOLATION, WriteProcessMemory with ERROR_NOACCESS, NtWriteVirtualMemory with STATUS_PARTIAL_COPY;
  • recv / recvfrom fail with WSAEFAULT (and TCP recv also bypasses MEM_WRITE_WATCH dirty tracking even on PAGE_READWRITE pages);
  • Additionally, the ignored GPU probe (gpu_copy_into_an_imported_heap) documents that OpenExistingHeapFromAddress requires PAGE_READWRITE on entry (0x80070057 otherwise) and dxgkrnl secures imported VADs so later VirtualProtect downgrades fail with Win32 error 87 (ERROR_INVALID_PARAMETER).

What is in it

  • crates/xcl/src/hle/kernelx/guest_write.rs (new):
    • enabled() (XCL_GUEST_WRITE_GUARD=1, default off).
    • register / unregister / is_pinned / xcl_protected range & pin registry for page protectors (fast-path single atomic load when no ranges are registered).
    • writable(p, n): treats committed pages protected by XCL (registered ranges or guards::is_view) as writable for XCL's own pre-checks while still rejecting uncommitted pages and PAGE_GUARD.
    • open(p, n) → Bracket: runs lazy::ensure, pins pages in registered ranges, and performs a no-op user-mode AtomicU8::fetch_add(0) on protected pages so the protector's fault handler opens them before a kernel call writes. Overlapped reads call Bracket::keep_until_done(ov) to keep pins live until OVERLAPPED leaves STATUS_PENDING.
    • kernel_write(what, p, n, write): brackets a status-returning kernel write (NtWriteVirtualMemory) with open, retries once if it failed over XCL-protected pages, and logs the first 8 repairs/failures.
    • bounce(buf, n, call) & install(): hooks ws2_32!recv and ws2_32!recvfrom (only when XCL_GUEST_WRITE_GUARD=1) to bounce receives over XCL-protected pages through an XCL buffer followed by a user-mode store.
    • 6 unit tests covering range merge/split, writable rules, NtWriteVirtualMemory into PAGE_READONLY and PAGE_NOACCESS, pin refcounting, overlapped pin lifetime, and recv bounce.
  • Call sites (all no-op when XCL_GUEST_WRITE_GUARD is off):
    • crates/xcl/src/eh.rs: CLZERO is_writable pre-check.
    • crates/xcl/src/hle/d3d12x/cpu_writes.rs: fence/label writability pre-checks (write_now_in, u64_writable_now, writable).
    • crates/xcl/src/hle/d3d12x/frame.rs: note_signaled output index writability check.
    • crates/xcl/src/hle/dstorage.rs & crates/xcl/src/hle/dstorage/parallel.rs: DirectStorage write_title, writable, and write_destination.
    • crates/xcl/src/vfs_hooks.rs: hk_read_file uses guest_write::open and keep_until_done for ERROR_IO_PENDING.
    • crates/xcl/src/hle/kernelx/guards.rs: expose is_view() and pub(super) fn still_pending().
    • crates/xcl/src/hle/kernelx.rs & crates/xcl/src/host.rs: register module and install socket hooks when enabled.
  • crates/xcl/tests/guest_write_probe.rs (new): integration test proving the Windows kernel-write & write-watch behavior matrix plus ignored D3D12 imported-heap probe.

Tested

  • Hardware / OS / Rust: AMD Radeon RX 6650 XT (8 GB RDNA2, driver 32.0.21045.29003), AMD Ryzen 7 5700X, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09).
  • cargo test -p xcl --lib -- hle::kernelx::guest_write hle::d3d12x::frame::tests hle::d3d12x::cpu_writes::tests hle::dstorage::tests hle::kernelx::guards::tests --test-threads=1 (debug): 53 passed, 0 failed, 0 ignored, 0 skipped (including all 6 guest_write unit tests).
  • cargo test -p xcl --test guest_write_probe -- --test-threads=1 (debug): 1 passed, 0 failed, 1 ignored; plus --ignored: 1 passed (gpu_copy_into_an_imported_heap), 0 failed.
  • Matched 60 s retail smoke on 1b6f6748 with XCL_GUEST_WRITE_GUARD=1: 1,657 frames (60 60 60 per 2 s), 16,226 MB peak commit, exit: 0x0, 0 device removals, 0 crashes (vs 1,655 frames / 16,648 MB with switch off).

Regression run

Baseline (main):

xcl-smoke v1
commit: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15479 MB

PR head (1b6f6748, default off):

xcl-smoke v1
commit: 1b6f6748d9563ce7d5eae97499a48c849e97ff98
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1655 (PresentX 1655)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 16648 MB

Not tested

  • DeviceIoControl and WSARecv overlapped scatter-gather socket I/O (not used by tested titles).
  • NVIDIA / Intel GPUs and Wine/Linux hosts (only tested on Windows 11 with AMD Radeon RX 6650 XT).

AI use

  • Claude Opus drafted the initial guest_write module, call sites, and probe test; Antigravity (Gemini) audited and rebased it onto origin/amd-dev (41aa60ce), resolved frame.rs::writable integration, added overflow/volatile fixes, and ran all unit, probe, and smoke verifications.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:directstorage DirectStorage queues, requests, decompression area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:tests Unit, integration, e2e tests, or testtitle gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 1b6f674, diff read, not a merge-check):

  • Nothing in this PR registers a range. guest_write::register (guest_write.rs ~55) is only called from tests, so with the switch on ANY stays false and open, the pins, touch and the kernel_write retry do nothing. The only XCL-protected pages this PR can meet are guards views, which lazy::ensure already opens for hk_read_file and DirectStorage on the base. So the "kernel write into XCL-protected RO/NA page lands cleanly" row and the switch-on smoke do not exercise a new kernel-write path; the first caller of register is D3D12.X: opt-in XCL_COPY_BACK=fault for GPU-written kept and RT/DS/UAV textures #3601.
  • recv into write-watched memory is still untracked. bounce_with (~319) bounces only when a page is xcl_protected (a registered range or a guarded view, ~99-101). Plain MEM_WRITE_WATCH title memory is neither, so a TCP recv there still goes straight to the kernel and the write watch misses it, while the description's "safe + tracked" row says this case is handled.
  • The documented pin protocol is racy. A protector is told to check is_pinned (~104) and then protect, with the lock released in between; open_with pins (~269) and then touches (~276). A protector can read "not pinned", the writer then pins and touches an already-writable page, and the protector protects it before the kernel call, so ReadFile fails with ERROR_NOACCESS with no retry, against the comment at ~275. D3D12.X: opt-in XCL_COPY_BACK=fault for GPU-written kept and RT/DS/UAV textures #3601 adds while_unpinned for this, which suggests it belongs here.
  • The probe test barely asserts anything. crates/xcl/tests/guest_write_probe.rs calls no XCL code; its only assertions are on the read-write control rows (~301), and the read-only, no-access and write-watch outcomes cited as evidence are only printed. The GPU case is #[ignore]d (~309) instead of using skip_test, and asserts nothing.
  • The call-site changes are untested. enabled() reads the env var once through a OnceLock, so no test runs writable with the switch on, and the changes in cpu_writes.rs, frame.rs, eh.rs and dstorage.rs are never exercised; the unit tests use only a stand-in protector, never a real guards::watch view.
  • Size and scope. 997 changed lines, over ~800 with no reason given, mixing four separable parts: a registry for a protector that is not in this PR, the pre-check changes for guarded views, the recv/recvfrom hooks, and a 407-line OS probe. The registry belongs with its first user.

Fine: with the switch off the default path is unchanged (writable is false, open is lazy::ensure, kernel_write calls the write directly, no hooks installed), and the range merge and split logic is correct.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:directstorage DirectStorage queues, requests, decompression area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:tests Unit, integration, e2e tests, or testtitle gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants