KernelX, D3D12.X, DirectStorage: opt-in XCL_GUEST_WRITE_GUARD for XCL-protected title pages #3597
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3597"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by iggyvilla on 2026-12-01T17:45:42Z.
Adds opt-in
XCL_GUEST_WRITE_GUARD=1(off by default: zero behavior change) to bracket kernel and XCL writes into title pages that XCL itself has made read-only or no-access (guards::watchviews today, and registered protector ranges for stacked #3601 / #3639).TL;DR — Before vs After
origin/amd-dev41aa60ce)07d529ea,XCL_GUEST_WRITE_GUARDunset)1,654/15,902 MB(0crashes)1,643/15,913 MB(0crashes)NtWriteVirtualMemoryfailswritable()accepts XCL-protected pages;open()/kernel_write()pin, touch, and retry cleanlyVirtualProtectvsopen()is_pinnedcheckwhile_unpinned()+FREEZE_LOCKwait inopen_with()Scope note: This PR provides the core
guest_writemechanism, race-freewhile_unpinned/meets_guarded_viewprotector protocol, and XCL / DirectStorage /ReadFilecall-site brackets. No runtime caller callsguest_write::registerin this PR yet (the first callers are stacked #3601 and #3639); withXCL_GUEST_WRITE_GUARD=1alone, onlyguards::watchviews are recognized as XCL-protected. Socketrecv/recvfromhooks and the standalone OS probe were removed to keep this PR single-purpose (~420 runtime lines + ~200 test lines, down from 997).What is in it
crates/xcl/src/hle/kernelx/guest_write.rs:XCL_GUEST_WRITE_GUARD=1switch, registered range map (register/unregister),xcl_protected,writable(covers),open/Bracket(withkeep_until_donefor overlapped reads),kernel_write(pin + touch + single retry), and race-free protector serialization (while_unpinned,meets_guarded_view,FREEZE_LOCKwait inopen_with). Includes 7 unit tests covering range merge/split,covers,NtWriteVirtualMemoryinto RO/NA pages, overlapped pins, concurrentwhile_unpinned↔open_withserialization, and a realguards::watchview exercised throughenabled()(writable+kernel_write).crates/xcl/src/hle/kernelx.rs,crates/xcl/src/hle/kernelx/guards.rs: exposesguest_write,guards::is_view, andguards::still_pending.crates/xcl/src/eh.rs,crates/xcl/src/hle/d3d12x/cpu_writes.rs,crates/xcl/src/hle/d3d12x/frame.rs,crates/xcl/src/hle/dstorage.rs,crates/xcl/src/hle/dstorage/parallel.rs,crates/xcl/src/vfs_hooks.rs: route writability pre-checks throughguest_write::writable, DirectStorage writes throughguest_write::kernel_write, andhk_read_filethroughguest_write::open/keep_until_done(all no-ops when off).Tested
32.0.21045.29003), AMD Ryzen 7 5700X, Windows 11 x64,rustc 1.100.0 (0ef1bc386 2026-11-09), debug tests + release smoke.cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 2,965 passed, 13 failed, 49 ignored. All 13 failures reproduce identically on cleanorigin/amd-dev41aa60ce(2,954 passed, 17 failed, 49 ignored: 3 pre-existing RDNA2 f32/NaN tests, 6 parallelsparse/title_texturesshared-global contention tests,test_title_runs_and_game_folder_is_untouched, andtwo_pass_driver_cache_e2e).SKIPPED(7): 4×f16_programs_on_the_gpu, 2× heap-window tests, 1×pso_measurement_compiles_an_exact_synthetic_record(Developer Mode off).cargo test -p xcl --lib -- hle::kernelx::guest_write::tests --test-threads=1: 7 passed, 0 failed, 0 ignored, 0 skipped.Regression run
Base (
main, required bypr_check.py):Base (
origin/amd-devmerge base):PR head (
07d529ea,XCL_GUEST_WRITE_GUARDunset):Not tested
AI use
while_unpinned/meets_guarded_viewhere, added realguards::watchand concurrency tests), ran the workspace GPU test suite and 60 s smoke, and updated this description.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T20:33:13Z.
Review (head
1b6f6748, diff read, not a merge-check):guest_write::register(guest_write.rs~55) is only called from tests, so with the switch onANYstays false andopen, the pins,touchand thekernel_writeretry do nothing. The only XCL-protected pages this PR can meet areguardsviews, whichlazy::ensurealready opens forhk_read_fileand 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 ofregisteris #3601.recvinto write-watched memory is still untracked.bounce_with(~319) bounces only when a page isxcl_protected(a registered range or a guarded view, ~99-101). PlainMEM_WRITE_WATCHtitle memory is neither, so a TCPrecvthere still goes straight to the kernel and the write watch misses it, while the description's "safe + tracked" row says this case is handled.is_pinned(~104) and then protect, with the lock released in between;open_withpins (~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, soReadFilefails withERROR_NOACCESSwith no retry, against the comment at ~275. #3601 addswhile_unpinnedfor this, which suggests it belongs here.crates/xcl/tests/guest_write_probe.rscalls 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 usingskip_test, and asserts nothing.enabled()reads the env var once through aOnceLock, so no test runswritablewith the switch on, and the changes incpu_writes.rs,frame.rs,eh.rsanddstorage.rsare never exercised; the unit tests use only a stand-in protector, never a realguards::watchview.recv/recvfromhooks, and a 407-line OS probe. The registry belongs with its first user.Fine: with the switch off the default path is unchanged (
writableis false,openislazy::ensure,kernel_writecalls 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.
Imported from ivall/xcl. Originally posted by iggyvilla on 2026-12-02T06:23:25Z.
Updated to
07d529eaaddressing @Foniass's review:\n- Size & scope: removed the 407-lineguest_write_probe.rsOS probe and thews2_32recv/recvfromhooks (which did not track plainMEM_WRITE_WATCHmemory), shrinking the diff from 997 to 624 lines (~420 runtime + ~200 tests).\n- Race-free pin protocol: movedFREEZE_LOCK,while_unpinned,meets_guarded_view, and theopen_withwait directly into this PR (and removed publicis_pinned), with a multi-threaded serialization test (while_unpinned_serializes_concurrent_protect_and_open).\n- Realguards::watch&enabled()coverage: addedreal_guarded_view_counts_as_writable_and_opens_on_kernel_writeexercising a real read-onlyguards::watchview throughwritable()andkernel_write().\n- Honest scope & testing: noted explicitly that no runtime caller callsregisteryet (stacked #3601 and #3639 are the first callers), and added whole-workspace GPU test counts +41aa60cemerge-base smoke.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T06:25:52Z.
Follow-up review (head
07d529ea, since1b6f6748, diff read, not a merge-check):recvinto write-watched memory untracked: fixed; therecv/recvfromhooks and their install inhost.rsare gone, and the description no longer claims it.guest_write_probe.rsis gone, and the failing kernel write is asserted ina_kernel_write_into_a_protected_page_lands_once_bracketed(guest_write.rs~482).while_unpinnednow checks the pins and publishesFREEZE_AT/FREEZE_ENDunderPINS, andopen_withwaits on an overlapping freeze; the new wait has a lost wakeup (below).real_guarded_view_counts_as_writable_and_opens_on_kernel_write(~557) driveswritableandkernel_writeon a realguards::watchview, but no test reaches the changed call sites (eh.rs~536,cpu_writes.rs~149 / ~189 / ~230,frame.rs~381,dstorage.rs~896 / ~941,parallel.rs~491,vfs_hooks.rs~537).register: still open, now stated in the description. With the switch onANYstays false, so the pins,touch, the freeze wait and thekernel_writeretry never run.New:
open_withcan spin forever after a freeze ends. It readsFREEZE_END(~330) and thenFREEZES_DONE(~332), butDone::drop(~144-145) clearsFREEZE_ENDand bumpsFREEZES_DONEwithout takingPINS, so it can run between the two loads; the writer then waits (~336) for a count that has already moved, until some laterwhile_unpinnedsucceeds, and one that overlaps the writer's pinned pages returns at ~135 without bumping it. AReadFileor DirectStorage write over those pages can hang. ReadingFREEZES_DONEbeforeFREEZE_END, or bumping it underPINS, closes this; the description's "race-freewhile_unpinned()" does not hold as is.1b6f6748; #3601 adds its ownwhile_unpinnedandmeets_guarded_view, which this head now defines, so it conflicts.Fine: with the switch off the default path is unchanged; the range merge / split and
coverslogic is correct; the new tests assert real outcomes.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T13:05:17Z.
merge-check
base:
41aa60ce84head:
07d529ea94merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2968 passed, 9 failed, 49 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on amd-dev); failing: gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_fract_f32_edges, hle::d3d12x::sparse::tests::private_physical_pages_reach_the_gpu_on_mapping, hle::d3d12x::title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, hle::d3d12x::title_textures::tests::remapped_pages_are_compared_with_what_they_held, hle::d3d12x::title_textures::tests::stream_recheck_keeps_the_whole_upload_baseline_over_partial_uploads, hle::d3d12x::title_textures::tests::stream_recheck_partial_upload_without_an_entry_queues_a_whole_upload, hle::d3d12x::title_textures::tests::stream_recheck_schedule_catches_late_writes_and_resets_milestones (the same tests fail on amd-dev
41aa60cealone on this card, so none is from this PR)review: read by one reviewer at this same head; the review is at https://github.com/xcl-dev/xcl/pull/3597#issuecomment-7337005484 and its findings stand. Most serious: a lost wakeup:
Done::drop(guest_write.rs~144) clearsFREEZE_ENDand bumpsFREEZES_DONEwithout takingPINS, soopen_with(~330-336) can spin forever once #3639 / #3601 call it; it is also over 400 lines and adds the protocol without its first user.performance: 60 s smoke of a retail title, amd-dev
41aa60cevs this merge (07d529ea): 1671 -> 1687 frames shown (100 %; last presents per 2 s: 60 60 60), peak commit 16722 -> 16817 MB, 0 device removals, 0 crashes.verdict: not yet: a lost wakeup:
Done::drop(guest_write.rs~144) clearsFREEZE_ENDand bumpsFREEZES_DONEwithout takingPINS, soopen_with(~330-336) can spin forever once #3639 / #3601 call it; it is also over 400 lines and adds the protocol without its first user (see the review)hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
AI-written and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:33:51Z.
Code review location:
crates/xcl/src/hle/kernelx/guest_write.rs; line 330Critical:
open_withreads FREEZE_END and then FREEZES_DONE, andwhile_unpinned'sDone::dropclears FREEZE_END and bumps FREEZES_DONE without holding PINS, so the snapshot can be taken after the protector has already finished, and the spin-wait waits for a completion that never comes. Thread A (hk_read_fileor a DirectStoragekernel_write) pins pages under PINS and loads fe != 0 while protector B is inprotect(). B returns,Done::dropsets FREEZE_END=0 and FREEZES_DONE=k+1, and A then loads FREEZES_DONE=k+1 aswait_forand spins inwhile FREEZES_DONE == k+1 { yield_now() }until some otherwhile_unpinnedcompletes. If none does, the title's ReadFile or the DirectStorage worker hangs at 100% CPU. Loading FREEZES_DONE before FREEZE_END, or publishing a generation under PINS, would fix it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:33:51Z.
Code review location:
crates/xcl/src/hle/kernelx/guest_write.rs; line 290Critical:
touch()doesn't exclude PAGE_GUARD pages (covers()does), so a pinned page with PAGE_READONLY|PAGE_GUARD gets a locked RMW that raises STATUS_GUARD_PAGE_VIOLATION, which no protector handles, and it consumes the title's one-shot guard. If a registered range contains a title guard page (e.g. a stack-growth guard in a pool XCL registered), a ReadFile or DirectStorage write over it callsopen_with->touch(), andfetch_add(0)on that page raises a guard-page exception. The protector's handler only claims EXCEPTION_ACCESS_VIOLATION, so it's unhandled insidehk_read_fileor the worker (a crash), or the guard is silently removed. Skippingr.protect & PAGE_GUARD != 0intouchwould fix it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:33:51Z.
Code review location:
crates/xcl/src/hle/kernelx/guest_write.rs; line 106Critical:
xcl_protectedanswers "XCL may have protected this page" (any page in a registered range or a guarded view), not "XCL protected it now", sowritable()/covers()report pages the title itself made read-only or no-access as writable, and every pre-check that used to skip safely now does a user-mode store there. With XCL_GUEST_WRITE_GUARD=1, if the title VirtualProtects part of a registered range (or a guarded view) to PAGE_READONLY/PAGE_NOACCESS, or the range holds PAGE_EXECUTE_READ code, the cpu_writes worker (write_now_in/write_u64_now), frame.rsnote_signaled, the eh.rs CLZERO emulation, or the DirectStorage copy now stores there. If the protector's handler doesn't claim the fault, the result is an unhandled access violation that crashes the process, where before the write was skipped. On a guarded view, the guards handler also silently overrides the title's own protection with RW. Tracking which pages XCL actually protected (and when) would fix it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:33:51Z.
Code review location:
crates/xcl/src/hle/kernelx/guest_write.rs; line 331High: The freeze overlap test
fa < end && p < fecompares the unaligned p against the unaligned fe = p'+n', but VirtualProtect, the pins and thewhile_unpinnedpin check all work on whole 4 KiB pages, so an open on the same page just past fe skips the wait. If the protector callswhile_unpinned(0x10000, 0x1800)(fe=0x11800, so VirtualProtect makes 0x10000-0x11fff read-only) andopen(0x11900, 0x100)runs concurrently, it pins page 0x11000, sees p >= fe and doesn't wait.touchskips the page because it's still read-write,protect()then makes it read-only, and the kernel ReadFile into 0x11900 fails with ERROR_NOACCESS.hk_read_filehas no retry, so the title gets a failed read. Rounding fe up to STEP would fix it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:33:51Z.
Review: COMMENTED
Review of
07d529ea: 4 finding(s) (3 critical, 1 high), most severe first.crates/xcl/src/hle/kernelx/guest_write.rs:330:open_withreads FREEZE_END and then FREEZES_DONE, andwhile_unpinned'sDone::dropclears FREEZE_END and bumps FREEZES_DONE without holding PINS, so the snapshot can be taken after the protector has already finished, and the spin-wa.... (inline)crates/xcl/src/hle/kernelx/guest_write.rs:290:touch()doesn't exclude PAGE_GUARD pages (covers()does), so a pinned page with PAGE_READONLY|PAGE_GUARD gets a locked RMW that raises STATUS_GUARD_PAGE_VIOLATION, which no protector handles, and it consumes the title's one-shot guard. (inline)crates/xcl/src/hle/kernelx/guest_write.rs:106:xcl_protectedanswers "XCL may have protected this page" (any page in a registered range or a guarded view), not "XCL protected it now", sowritable()/covers()report pages the title itself made read-only or no-access as writable, .... (inline)crates/xcl/src/hle/kernelx/guest_write.rs:331: The freeze overlap testfa < end && p < fecompares the unaligned p against the unaligned fe = p'+n', but VirtualProtect, the pins and thewhile_unpinnedpin check all work on whole 4 KiB pages, so an open on the same page just past .... (inline)These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:33:55Z.
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.