feat(libc): trap guest reads for the write watch on Windows #26643
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/26643"
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 boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-11-24T13:12:10Z.
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 #18300 inGuestArena.cppnearGuestArenaCommit, where both PRs add lines. #20580 and #26221 change the same files for the macOS and Linux traps and add no Windows code. #1835, #7149, #12792 and #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)Imported from boykopovar/AnyPS5. Originally posted by kripotker on 2026-11-24T18:46:30Z.
Review: APPROVED
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.ReadTraps::fault):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);EXCEPTION_CONTINUE_SEARCH, so the crash handler and theWindowsMappingshandlers still see their faults;_locknothing reads guest memory, so a fault can't re-enter the spin lock.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.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
b0085f0d98. This is a different machine and toolchain from the PR's WinLibs 15.2 laptop run.windows_read_trap_*: 12/12 passed.threadsruns under load) ended with100% tests passed out of 12.mprotecttests: 33/33 passed. Theguest_socket_host_write_*group goes through the changedGuestArenaBeginHostWrite.check_conventions.py:All conventions checks passed.Note: the
mprotectfence isn't widened, but the text says it isThe description and the debt fragment both say libkernel's
mprotectresolves traps "widened to the resident runs it touches". But theTrapFenceincore/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:171-176fences exactly[addr, addr + len). Only the arena operations inGuestArena.cppuseWithResidentRuns.As far as I can read, the code is fine as it is:
WindowsMappings::SetProtectiononly updates bookkeeping for the views in range, and theVirtualProtectinmprotectcovers 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 formprotect. If widening is intended, the fence needsWithResidentRuns(or an exported equivalent).AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T16:36:33Z.
Review: COMMENTED
Reviewed — comments
The Windows read trap implementation has a concrete failure path that can strand trapped pages when resolution fails.
What I ran
574b9a1b0fbab4d6401407a3aa5aaf93d55c62bd; no merged-preview check was run.repo_read.pyto inspect targeted source ranges. No tests were run by me; Windows test results in the description are author-reported.Results
Findings
Blocking
None verified.
Non-blocking
GuestWriteWatch.cpp:2065calls the user resolver without containing exceptions or restoring the traps if it fails. The resolver runs on the dedicated resolver thread; an exception escaping its thread entry terminates the process, while a non-local failure would leave traps owned and waiters blocked. Consider defining and enforcing a non-throwing resolver contract, or adding a failure path that releases waiters and restores a safe page state.Not verified
To get this to a good state
Nothing is required. The resolver failure handling is optional feedback.