fix(libkernel): deliver a raised exception at the guest return of a thread in host code #10666
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10666"
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 WallisSkill on 2026-10-24T16:01:13Z.
What
sceKernelRaiseExceptionon Windows no longer redirects a thread that is in host code. Before,RaiseOnsuspended the target and, while it sat in host code (libkernel, libc, the HLE modules, the C runtime, ntdll), resampled it until a 50 ms limit and then redirected it into the handler right there. A handler that waits, as Unity's GC thread-stop handler does, then kept whatever host lock the thread held.In Graveyard Keeper 2 that froze the load after New game in about 1 of 15 runs. The capture of one freeze: the GC thread waited in
sceKernelWaitSemawith 39 of 40 stop acknowledgements; 39 threads waited on the resume event in the handler; the missing job worker sat atAps5RedirectedEntryStubbecause it had been redirected while blocked in a winpthreads lock wait insideDriver::RegisterShader(a redirect only takes effect when the kernel wait ends), and the holder of that lock had been redirected out ofShaderRecompiler::SpecializeSpirvafter the 50 ms limit and was parked in the handler.Now a target in host code is never interrupted where it is. The first of these to reach it delivers (one atomic
takenflag per delivery):Pthread::waitCountabove 0, the point where the handler already ran before), an APC queued to that wait;.pdatatable andRtlVirtualUnwind; the search stops at frames that push a machine frame and is guarded against faults; the return site must follow a call instruction; a vectored handler takes the single-step, clears the breakpoint and runs the handler on the thread;The raise returns as soon as the delivery is queued. A target in guest code is still redirected at once, and a target inside NtContinue still makes the raise throw after 1 s. The technical debt entry is rewritten for the new behaviour and its gaps (the handler's context holds no vector registers beyond what the C runtime context saves; a target that stays in host code waiting on a thread already stopped is never stopped; debug register 0 used by a debugger or the title is not modeled).
New test
guest_raise_leaves_host_code: the handler checks that the interruptedriplies in the test executable, for a target blocked inWaitForSingleObject(each raise must return in under 500 ms), one busy inMultiByteToWideCharover 64 MiB, and one in an alertableWaitForSingleObjectEx, where the handler must also run only after the wait was released (an APC for every host-code target, the first version of this PR, fails it), 10 rounds each.Related: #8592 defers Windows exceptions during shader preparation, a different approach to a similar deadlock; #10630 changes the
guest_raise_in_system_calltest only.Tested
Windows 11 Pro 10.0.26200, AMD Ryzen 7 9800X3D, MinGW Release build with
BUILD_TESTING=ONon this branch (main3fcb5e4d7+ this commit).ctest -R "raise|semaphore|equeue|signal|sync_on_address|umtx"at efb57d464: 15 of 15 pass, among themguest_raise_exception,guest_raise_in_host_code,guest_raise_in_system_call,guest_mutex_raise_exception,guest_raise_nested_sleepand the newguest_raise_leaves_host_code. The raise tests passed 5 repeated runs (--repeat until-fail:5).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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T16:03:13Z.
Review: CHANGES_REQUESTED
At the current head, APC delivery is queued unconditionally for a target classified as host code (
QueueDeliveryinTryDeliver, thenWaitingEntrycallsDeliverdirectly). If that host code is in an alertable wait, Windows runs the APC before the wait returns, so the guest handler runs inside host code while its locks are still held. This reproduces the lock-retention/reentrancy hazard that the change is intended to avoid, and contradicts the new test’s invariant that the handler never runs outside the guest image. The debt entry acknowledges this for host code that holds a lock across an alertable wait, but this path is a core delivery route rather than an incidental edge case. Please constrain APC delivery to contexts where the wait/lock state is known safe (such as the existing libkernel deferral points), or otherwise defer it until the guest return, and add a regression using an alertable host wait that verifies the handler’s saved RIP is in the guest image and the host lock can be acquired after delivery.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T19:33:57Z.
Review: CHANGES_REQUESTED
The current head still queues
WaitingEntryfor every target classified as host code (TryDeliver, around line 715);WaitingEntrythen callsDeliveras soon as that APC runs. A target already inside an alertable host wait can therefore run the guest handler inside the host call, before the wait returns, retaining any host locks the change is intended to protect. The newGuestRaiseLeavesHostCode.cpptests a non-alertableWaitForSingleObjectand a busy conversion, but contains no alertableWaitForSingleObjectExcase despite the description saying it does. Please prevent generic host APC delivery from invoking the handler inside host code and add the alertable-wait regression, checking the saved RIP is in the guest image and delivery occurs only after the wait is released.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:26:59Z.
Review: COMMENTED
Correction to my prior review on this exact head: I said
GuestRaiseLeavesHostCode.cppdid not contain an alertableWaitForSingleObjectExcase. I re-opened the file atefb57d464d7f242ed4b8bb32dfc6db4b79195512; the test is present. It repeatedly raises while the target is in that alertable wait, releases it only after a delay, and asserts the handler did not run before release and that saved RIP stays in the test image. The implementation also queues the APC only whenPthread::waitCountindicates a libkernel wait; otherwise it arms delivery for return to guest code. My earlier statement that the test was missing was incorrect.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:27:01Z.
Review: APPROVED
Re-reviewed exact head
efb57d464d7f242ed4b8bb32dfc6db4b79195512and confirmed the alertable-wait regression is present. Delivery is queued as an APC for a thread in a libkernel wait; host-code targets outside such a wait retain the return breakpoint/sampling path, preventing handler invocation from an unrelated alertable host wait. The test checks the saved RIP against the executable image, delays release ofWaitForSingleObjectExuntil after the raise, and verifies delivery occurs after release over ten rounds. The PR reports the 15 focused Windows tests passing, including five repeated raise-test runs; I did not independently run this Windows-only test here. The author also reports compatibility testing in Graveyard Keeper 2. The overlap with #9573 is documented by the author as compatible in either order, with only the shared debt entry requiring a rebase adjustment. No blocking issue remains.