Repository navigation
feat(libc): trap guest reads for the write watch on Windows - #26643
LeandroLP23 wants to merge 3 commits into
Conversation
Port the Linux read traps of GuestWriteWatch to Windows: a trapped range is made PAGE_NOACCESS and a vectored exception handler added with the first resolver runs the resolver on its own thread. Traps are set only on read-write resident guest arena pages that WindowsMappings does not protect itself. The arena's commit, reset and mappings and libkernel's mprotect resolve traps over their range first, and an alias of a resident run drops the traps in it. No library sets traps on Windows yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
GuestArenaBeginHostWrite went to WindowsMappings alone on Windows, so a host write through GuestArena::HostWrite (file reads, socket receives) into a trapped page would fail or fault instead of resolving the trap first, as it does on Linux and macOS. Open the write watch's host write around the WindowsMappings one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
Review: feat(libc): trap guest reads for the write watch on WindowsRecommendation: merge. All tests pass on a second Windows machine, a 200× stress run of all 12 modes is clean, and the 33 existing arena and host-write tests pass. One wording mismatch is noted below. What I checked
What I ranWindows 11 (desktop: Ryzen 7 7700, 32 GB), MSYS2 UCRT64 GCC 16.2.0 (Rev6), Ninja, Release, PR head b0085f0. This is a different machine and toolchain from the PR's WinLibs 15.2 laptop run.
Note: the
|
There was a problem hiding this comment.
Review: feat(libc): trap guest reads for the write watch on Windows
Recommendation: merge. All tests pass on a second Windows machine, a 200× stress run of all 12 modes is clean, and the 33 existing arena and host-write tests pass. One wording mismatch is noted below.
What I checked
ReadTraps(Windows) against the Linux class onmain. The trap set, resolver slots, batches, holder bypass andretryOpencounters match. Only the host primitives differ:VirtualProtect/VirtualQuery, a vectored handler, semaphores and thread IDs.- The vectored handler (
ReadTraps::fault):- it acts only on
EXCEPTION_ACCESS_VIOLATION, and claims a fault only when the page is in its own trap list or a page it just reopened (retryOpen, bounded at 16 retries per generation); - every other fault returns
EXCEPTION_CONTINUE_SEARCH, so the crash handler and theWindowsMappingshandlers still see their faults; - under
_locknothing reads guest memory, so a fault can't re-enter the spin lock.
- it acts only on
TrapReadssets a trap only afterreadWrite()(MEM_COMMIT+ exactlyPAGE_READWRITE) andProtectsItself(), and refuses ranges in_hostWrites, all under the same lock. A failedPAGE_NOACCESSgives the range back read-write.GuestArenaBeginHostWritenow opens the write-watch host write first on Windows, and closes it again whenWindowsMappings::BeginHostWriterefuses.GuestArenaEndHostWritecloses both. The open and close are balanced on every path.- Sabotage check: the PR only touches the write watch, the arena,
mprotect, one test and one debt fragment. The handler has no file, network or process side effects.
What I ran
Windows 11 (desktop: Ryzen 7 7700, 32 GB), MSYS2 UCRT64 GCC 16.2.0 (Rev6), Ninja, Release, PR head b0085f0. This is a different machine and toolchain from the PR's WinLibs 15.2 laptop run.
cmake --build build --target windows_read_trap_tests
ctest --test-dir build -R "^windows_read_trap_"
ctest --test-dir build -R "^windows_read_trap_" -j 16 --repeat until-fail:200
ctest --test-dir build -j 16 -R "^(windows_write_watch.*|windows_mappings_.*|guest_copy_.*|guest_socket_host_write_.*|savedata_memory_host_write|guest_stream_buffer_host_write|guest_np_trophy_host_write|guest_direct_memory_for_app|guest_named_shm_windows_mapping|guest_mprotect_unmapped|guest_kernel_mprotect_prot_none)$"
python3 tools/check_conventions.py
windows_read_trap_*: 12/12 passed.- The stress run (each mode repeated up to 200 times, 16 at a time, so
threadsruns under load) ended with100% tests passed out of 12. - Existing arena / mapping / host-write /
mprotecttests: 33/33 passed. Theguest_socket_host_write_*group goes through the changedGuestArenaBeginHostWrite. check_conventions.py:All conventions checks passed.- No new warnings in the changed files.
Note: the mprotect fence isn't widened, but the text says it is
The description and the debt fragment both say libkernel's mprotect resolves traps "widened to the resident runs it touches". But the TrapFence in core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:171-176 fences exactly [addr, addr + len). Only the arena operations in GuestArena.cpp use WithResidentRuns.
As far as I can read, the code is fine as it is: WindowsMappings::SetProtection only updates bookkeeping for the views in range, and the VirtualProtect in mprotect covers just [addr, addr + len), so a trap elsewhere in the run is left alone. If that is the intent, please reword the debt fragment so it doesn't promise widening for mprotect. If widening is intended, the fence needs WithResidentRuns (or an exported equivalent).
AI-assisted: yes (Claude Code).
Only the guest arena operations widen the fenced range to the resident runs they touch; libkernel's mprotect changes and fences exactly the range it is given. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@kripotker thanks for the review and the stress run. You are right about AI-assisted: yes (Claude Code) |
What
The write watch's read traps (
GuestWriteWatchTrapReadsand the rest of that API) now work on Windows. Before this, every call there was a no-op on Windows. The class is a port of the Linux one from #19054: the same trap set, resolver slots, batches and holder bypass. Only the host primitives change. This PR does not change what the driver does: no library sets a trap on Windows yet. A follow-up enables the label read traps in libSceAgcDriver on top of it.PAGE_NOACCESSwithVirtualProtect. A vectored exception handler, added first in the chain when the first resolver is set, waits while the resolver runs on the watch's own thread, then gives the pages back read-write. It passes every other fault on, so the crash report andWindowsMappingshandlers still see theirs. A thread that holds what the bypass reports (theGpuMutexholder, in the driver) opens the page unstored, as on Linux.VirtualQueryreports as exactlyPAGE_READWRITE, and only inside resident runs ofWindowsMappings.WindowsMappings::ProtectsItselfrefuses shared views and views demoted for an alias, since write tracking and eviction change their protection themselves.mprotectdoes the same over its exact range:WindowsMappings::SetProtectiononly updates the bookkeeping of the views in range, and itsVirtualProtectcovers only that range;GuestArenaBeginHostWrite, whichGuestArena::HostWriteuses for file reads and socket receives into guest memory, now opens the write watch's host write on Windows too, as on Linux and macOS. Before, it went toWindowsMappingsalone, so a host write into a trapped page would fail or fault instead of resolving the trap first;GuestArenaMapAliasdrops the traps in the resident runs it demotes, without running the resolver, because a host import maps that alias under the write tracker lock the resolver takes.GuestWriteWatchUnregisterdrops them the same way on Windows. The drop shows inGuestWriteWatchReadTrapGeneration, so a caller can store the range again.windows_read_trap_testshas 12 modes: trap, release, host, arenahost, unknown, reset, alias, trapped, generation, untrapped, holder, threads. Thethreadsresolver does not touch the pages, as a store through a host import does not. Its readers check that none of them goes on before the resolver returns.Tested
Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release.
windows_read_trap_tests: all 12 modes pass in 20 of 20 rounds.HandleFault,trappedandholderfail andthreadstimes out; without the write watch's host write inGuestArenaBeginHostWrite,arenahostfails (an arena host write over a trapped page did not resolve it first).ctest -j8withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, before the alias drop was added: 2 failures,agc_driver_linear_target_import_creation(private gate: a color target's import of direct memory under the settle floor was made where it would be compared) andagc_driver_store_over_untracked(a pending storage result was stored over bytes the CPU wrote to untracked memory). Both fail with the same messages, 3 of 3 runs, on the base4c9e324231without this change.windows_read_trap|guest_arena|arena|write_watch|windows_mappings|direct_memory|guest_copy|guest_memory|host_write|mprotectpass (1 skipped), with and withoutVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer. With the layer, the log shows 12Insert instance layer "VK_LAYER_KHRONOS_validation"lines, nowas not foundand noVUID-.windows_read_trap_tests: all 11 modes it had then pass in 20 of 20 rounds.GuestArenaMapAlias,aliasfails (an alias that demotes a resident run kept the generation of the trap in it).tools/check_conventions.pyandtools/debt_fold.py --checkpass.tools/pr_overlap.py --branchreports a textual conflict with fix(libc): reject Windows guest commits over host regions #18300 inGuestArena.cppnearGuestArenaCommit, where both PRs add lines. fix(libc): order read-trap resolution against the GPU mutex #20580 and fix(agc): refuse a host import over read-trapped pages instead of resolving the trap under its locks #26221 change the same files for the macOS and Linux traps and add no Windows code. feat(aot): discuss native ARM64 compiler architecture and integration #1835, fix(threads): run Windows pthread and small fiber stacks in guest VA range #7149, fix(agc): compare pending storage results over a surface whose page walk fails #12792 and perf(agc): keep shaders off host imports on NVIDIA's Windows driver and cut per-draw CPU #20315 also share files, but each already conflicts withmain.Not run: Linux, macOS (the shared code there is unchanged).
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)