fix(libkernel): block a signal while its handler runs and honour SA_RESETHAND #5312
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5312"
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 EagleBearerYS on 2026-10-13T16:59:34Z.
What
A guest signal handler ran with the mask its signal was raised under.
DispatchandDeliverPendingSignalinSignals.cppcalled it without blocking anything, so:sigprocmaskinside a handler did not report the signal or the handler'ssa_maskas blocked;SA_RESETHANDleft the handler installed for the next delivery.FreeBSD sets up the handler's frame and then, in
postsig_done, blockssa_maskand, unlessSA_NODEFERis set, the signal itself. It also resets the disposition toSIG_DFLwhenSA_RESETHANDis set (sigdflt), andsigreturnrestores the previous mask.sigactionkeeps reportingSA_RESETHANDafterwards (L707-L708), becausesigdfltdoes not clearps_sigreset, whichkern_sigactionsets from the flag.The handler now runs with its signal (unless
SA_NODEFER) and the first 32 signals of itssa_maskadded to the guest mask. When it returns, only the signals it added are unblocked again, so a change another thread makes to the mask meanwhile is kept. Pending signals that this unblocks are delivered whenraisereturns, as_sigprocmaskalready does.SA_RESETHANDresets the handler to the default before it runs.A handler that leaves with
longjmpnever returns there, so its signal would stay blocked. FreeBSD'ssetjmpsaves the signal mask in the jump buffer and itslongjmprestores it (setjmp.S), but ours kept only the registers. libc'ssetjmpnow saves the mask at 72 in the jump buffer, with the flagsigsetjmpkeeps at 88, andlongjmprestores it. libc doesn't link libkernel, so libkernel registers itssigprocmaskwith libc when it loads. Both changes follow CnRJay's review.The mask is still
main's process-wide 32-bit mask. #3986 makes it per thread; with that, the handler's mask belongs to the thread the signal is delivered on, so whichever of the two lands second adapts. The commits:SA_RESETHAND, withguest_signal_handler_mask;sigactionentry indocs/dev/debt/libkernel.md;setjmpandlongjmpsaving and restoring the mask, with adocs/dev/debt/libc.mdentry;longjmpand raises its signal again.No title is claimed.
Tested
On GitHub's runners from my fork, on
mainat0214069e3(run):-DANYPS5_ENABLE_SPIRV_TOOLS=ON.On both:
guest_signal_handler_mask. A handler raises its own signal, both onSIGUSR2, which has no host signal, and onSIGTERM, which goes through the host. The test also checksSA_NODEFER, the masksigprocmaskreports inside the handler, andSA_RESETHAND. Withmain'sSignals.cppit aborts at its first check, where the nestedraisere-enters the handler. With the change it passes 20 runs in a row.core/libs/prx/libcandcore/libs/prx/libkernelpasses: 69 and 124 on Linux, 90 and 127 on Windows.tools/check_conventions.py --base origin/mainpasses for both commits.After the review, on the same runners:
guest_signal_handler_maskgains a check where another thread blocksSIGTERMwhile the handler runs. It aborts with the first two commits and passes 20 runs in a row with the third.guest_signal_setsgainssetjmp/longjmpchecks (the mask comes back, saved at 72 with the flag at 88), andguest_signal_handler_maskleaves a handler withlongjmpand raises the signal again. Both abort with the first three commits and pass 20 runs in a row with all five.core/libs/prx/libcandcore/libs/prx/libkernelpasses: 69 and 124 on Linux, 90 and 127 on Windows.tools/check_conventions.pypasses for all five commits.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 CnRJay on 2026-10-14T01:16:26Z.
Review: COMMENTED
Blocking the signal and
sa_maskduring the handler matchespostsig_done. One interaction this introduces: a handler that leaves bylongjmpnow leaves its signal blocked for good.RunHandlerrestores the mask afterhandler(guest)returns. A handler that jumps out never returns there, and leaving a SIGSEGV/SIGBUS/SIGFPE handler through a jump is the classic pattern for guarded probes and crash recovery. What happens next depends on which jump the guest uses:siglongjmpwith asigsetjmp(env, 1)buffer restores the saved mask through_sigprocmask(SIG_SETMASK)(SignalSets.cpp), so that case works.longjmp(JumpBuffer.cppin libc) doesn't touch the mask. On FreeBSD,setjmp/longjmpdo save and restore the signal mask (BSD semantics, unlike_setjmp/_longjmp). Onmainthat gap was invisible, because nothing was blocked during a handler. With this PR, a guest that leaves a handler withlongjmpkeeps that signal blocked, and the next fault of that kind is never delivered. Instead it stays pending, or the host kills the process on a synchronous fault it can't deliver.So either
setjmp/longjmpin libc should save and restore the mask as FreeBSD's do (the same_sigprocmaskcallssigsetjmp(env, 1)makes), or this PR should land together with that change. A test where a handler leaves withlongjmpand the same signal is then raised again would pin it.Smaller:
blockedMask.store(saved)after the handler overwrites any change another thread made to the process-wide mask while the handler ran. The description notes that #3986 moves to per-thread masks. Until then,fetch_and(~added)with only the bits this handler added (mask & ~saved) would avoid clobbering concurrent changes.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T02:12:33Z.
Thanks, both points hold, and both are fixed now.
fetch_and(~added)), so a mask change another thread makes meanwhile is kept. That'sef4590484, with a check where another thread blocksSIGTERMwhile the handler runs.setjmpnow saves the signal mask at 72 in the jump buffer andlongjmprestores it, as FreeBSD 11.4'ssetjmp.Sdoes (624815b20). libc doesn't link libkernel, so libkernel registers itssigprocmaskwith libc when it loads.9f584de29makesguest_signal_handler_maskleave a handler withlongjmpand raise the same signal again: without the libc change, the second raise stays blocked.On GitHub's runners (run), the new
longjmpchecks inguest_signal_handler_maskandguest_signal_setsfail without the libc change and pass 20 runs in a row with it, on Linux and Windows, and the libc and libkernel suites pass.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:29:02Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed