fix(libkernel): block a signal while its handler runs and honour SA_RESETHAND #9499
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9499"
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-21T23:32:01Z.
Opened again from #5312: title-less conformance fixes of this kind are merged now, for example #7365 and #8411.
What
A guest signal handler runs with the mask its signal was raised under.
DispatchandDeliverPendingSignalinSignals.cppcall it without blocking anything, so:sigprocmaskinside a handler does not report the signal or the handler'ssa_maskas blocked;SA_RESETHANDleaves 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 itssa_maskblocked, in all four words of the mask, withoutSIGKILLandSIGSTOP, whichsigprocmaskleaves unblocked since #8407.main's mask is process-wide, so these blocks are holds kept apart from the masksigprocmasksets: they count as blocked for delivery and in the masksigprocmaskreports, and the handler releases only its own holds when it returns. A block another thread adds meanwhile is therefore kept, also for a signal in the handler'ssa_mask.SIG_UNBLOCKof a held signal releases its hold, so a handler can still unblock its own signal, andSIG_SETMASK, whichlongjmpuses, releases them all;sigsuspend's own restore keeps them. When the handler's own thread releases a hold that way, for instance by saving the mask and putting it back withSIG_SETMASK, the signal gets back its bit of the mask from before the handler when the handler returns, as FreeBSD'ssigreturnputs back the mask from before it; a mask another thread sets 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 keeps 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 followed CnRJay's review on #5312.guest_signalschanges one expectation. Inmain's check of a handler run bysigsuspendwithSIGKILL,SIGSTOPand signal 29 in the suspend mask (from #8407), the mask seen inside the handler now also holds the handler's own signal 30,(1u << 28) | (1u << 29)instead of1u << 28, as FreeBSD'spostsig_doneadds it. The check thatSIGKILLandSIGSTOPstay unblocked is unchanged.The mask is still
main's process-wide 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:3d1b4d460b, the handler mask andSA_RESETHAND, withguest_signal_handler_mask;ebb86e4c2e, thesigactionentry indocs/dev/debt/libkernel.md;ef45904842, unblocking only what the handler added;624815b200, libc'ssetjmpandlongjmpsaving and restoring the mask, with adocs/dev/debt/libc.mdentry;9f584de29b, a check that leaves a handler withlongjmpand raises its signal again;e607ba1b41, the merge ofmain, which also updates theguest_signalsexpectation above;780aa44726, the handler's mask in all four mask words, sincemainnow keeps them, with a check for signals above 32;d3370bf57a, the jump buffer's calls spelled for Mach-O hosts, asmain's other assembly calls are;56b2a6393f, after MarcZX10's review, the handler holds above, so a concurrent block of the same signal survives the handler's return;f5757aab98, after ahmadmuj's review, a signal whose hold the handler's own thread released getting back its bit of the mask from before the handler when the handler returns.Overlap, merge order: #8866 (
_sigintr) and #8872 (siginterrupt) are merged, and this branch is merged with both. #9001 (Queuse, tests forsiginterrupt) builds on them and conflicts with this PR at the end ofSignals.cpp, where this PR registers libkernel'ssigprocmaskwith libc, and in the libkernel debt file. They compose, and this PR proposed going before it in its thread.#10501 (harisibrar1,
pselect) also moves_sigprocmask's body into a helper inSignals.cpp. It goes after this PR and rebases onto this PR'sChangeBlockedMask(merge order only, agreed on #10501).#10749 (ycbuzi) moves
siginterruptfrom libc to libSceLibcInternal and removes the block at the end ofSignals.cppthat registerssigactionwith libc, next to this PR'sRegisteredForJumps, which registerssigprocmaskwith libc's jump buffers. In the libkernel debt file it rewrites thesignalentry, beside this PR'ssigactionentry. This PR goes after it (merge order only): when it mergesmainafter #10749, it keepsRegisteredForJumpswithout the removed block, and both debt entries.No title is claimed.
Tested
On GitHub's runners from my fork, merged with
mainata6dcea9279(run), on both Linux and Windows:guest_signal_handler_mask: a handler raises its own signal, both onSIGUSR2, which has no host signal, and onSIGTERM, which goes through the host. It also checksSA_NODEFER, the masksigprocmaskreports inside the handler,SA_RESETHAND, a mask change from another thread while the handler runs, a handler left withlongjmpwhose signal is raised again, and a handler whosesa_masknamesSIGKILL, signal 40 and signal 128.guest_signal_setschecks thatsetjmpsaves the mask at 72 with the flag at 88 and thatlongjmprestores it.main'sSignals.cppandJumpBuffer.cpp,guest_signal_handler_mask,guest_signal_setsandguest_signalsfail. With the change all three pass 10 runs in a row.core/libs/prx/libcandcore/libs/prx/libkernelpasses: 115 and 198 on Linux, 142 and 208 on Windows.780aa44726on its own (run): with the merge's code, the new upper-word check inguest_signal_handler_maskfails 3 of 3 on both; with the commit it passes 10 of 10.56b2a6393f(run):guest_signal_handler_maskgains a handler whosesa_maskholdsSIGTERMwhile another thread blocksSIGTERM, which must stay blocked after the handler returns, and a handler that unblocks and raises its own signal, which must run again inside it. With d3370bf57a's code the test fails 3 of 3 on both; with the commitguest_signal_handler_mask,guest_signal_setsandguest_signalspass 10 of 10, and the libc and libkernel suites pass.mainat4a17c71757(280849ebde, no conflicts, the PR's own diff unchanged; run):guest_signal_handler_mask,guest_signal_setsandguest_signalsfail withmain's code on both runners and pass 10 runs in a row with the change. The libc and libkernel suites pass on Windows (143, 208) and libkernel on Linux (198); in libc on Linux,precise_waitfailed with and without the change.check_conventions.pyreports no errors and one warning,debt-length, on thesigactionentry: 2,482 characters, against 1,426 onmain.mainat895f47f4ee(b463132a56, which brings in #8866; run):Signals.cpp,signal_nid_postfixkeeps this PR'sSettleReset(guest)before #8866's handler record, which setsSA_RESTARTunless the signal is in_sigintr. The libkernel debt file keeps this PR'ssigactionentry and #8866'ssignalentry. The PR's own changed lines (git diff -U0againstmain) are unchanged.guest_signal_handler_mask,guest_signal_setsandguest_signalsfail withmain's code on both runners and pass 10 runs in a row with the change, none skipped.APS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.check_conventions.py --base 895f47f4eereports no errors and the samedebt-lengthwarning on thesigactionentry.mainatdc9f1ef656(b72ea79cf9; run):mainrewrote thegetsidentry (nowgetsid, setpgid, setpgrp) on the line before this PR'ssigactionentry. The merge keepsmain'sgetsidentry and this PR'ssigactionentry.main's other changes toSignals.cppandGuestSignals.cpp(discarding a pending signal thatsigactionorsignalignores, with its tests, and the underscore entry points) merged without conflicts, and the PR's own changed lines are unchanged.guest_signal_handler_mask,guest_signal_setsandguest_signalsfail withmain's code on both runners (3 of 3) and pass 10 runs in a row with the change: 3 run, 3 pass, 0 skip, 0 fail on each.APS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.mainat6b496aa1d1(9e7b2ade4c, which brings in #8872; run):Signals.cpp, #8872 registers libkernel'ssigactionand_sigintrwith libc at load, where this PR registers itssigprocmask: both registrations are kept, this PR's first, in the anonymous namespace #8872 adds. In the libkernel debt file, the merge keeps this PR'ssigactionentry and #8872'ssignalentry, which now mentionssiginterrupt. Apart from that namespace, the PR's own changed lines are unchanged.guest_signal_handler_mask,guest_signal_setsandguest_signals, which now holds #8872'ssiginterruptchecks, fail withmain's code on both runners (3 of 3) and pass 10 runs in a row with the change: 3 run, 3 pass, 0 skip, 0 fail on each.APS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.tools/check_conventions.py --base origin/mainpasses for every commit.8be8dc83af, a merge ofmaine05ba6feeb(run).main's #10706 moves the declarations of exported functions and their types into the libraries' public headers, which itspublic_headerstest checks. The merge keeps this PR'sDeliverUnblockedPendingandChangeBlockedMaskwithmain'sKernelTimespectimeout inWaitForPendingInSet, and takesmain's side of the declarations inGuestSignalSets.cpp: libc'sApi.hppnow declares thesetjmpandlongjmpthis PR had added there. The newGuestSignalHandlerMask.cppincludes the libkernel and libc public headers in place of its own declarations and its ownGuestSignalSetandGuestSigactiontypes. Theguest_signal_handler_mask,guest_signal_sets,guest_signalsandpublic_headerstests pass 10 runs in a row. Linux: 4 run, 4 pass, 0 skip, 0 fail. Windows: 4 run, 4 pass, 0 skip, 0 fail. The before arm failsguest_signals,guest_signal_handler_maskandguest_signal_setson both. Suites: libc and libkernel pass on Linux (144 and 208) and on Windows (171 and 224). The suites ran withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.f5757aab98, after ahmadmuj's review (run):guest_signal_handler_maskgains a handler that saves the mask and puts it back withSIG_SETMASK, forSIGUSR2and forSIGTERM, and one that sets an empty mask and puts the saved one back; after each returns its signal must be unblocked and raising it again must run the handler. A fourth handler has another thread set the mask meanwhile, which must be kept, so the fix restores no more than it should. With 8be8dc83af's codeguest_signal_handler_maskfails on both runners; with the commitguest_signal_handler_mask,guest_signal_sets,guest_signalsandpublic_headerspass 10 runs in a row. Linux: 4 run, 4 pass, 0 skip, 0 fail. Windows: 4 run, 4 pass, 0 skip, 0 fail. Suites: libc and libkernel pass on Linux (144 and 208) and on Windows (171 and 224). The suites ran withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.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 MarcZX10 on 2026-10-21T23:34:00Z.
Review: CHANGES_REQUESTED
RunHandlercan lose a concurrent same-signal mask change. For example, install a handler whosesa_maskcontains SIGTERM, enter it while SIGTERM was previously unblocked, and have another guest thread callsigprocmask(SIG_BLOCK, SIGTERM)before the handler returns.added[0]records that this handler set the bit, and the unconditionalblockedMask.fetch_and(~added[0])then clears the other thread's block. The currentCheckOtherThreadKeptonly blocks SIGTERM while the handler's mask is empty, so it does not exercise this same-bit case.Please add a same-bit regression and preserve the concurrent update, or make this change depend on the per-thread-mask change in #3986 and verify the combined result. The PR description currently promises that a concurrent mask change is kept, which this interleaving does not satisfy. The broad code change also has seven matrix jobs still pending; I’ll revisit once they finish.
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-21T23:52:39Z.
Thanks, @MarcZX10, you're right: with one process-wide mask, the handler's unblock on return can't tell its own bit from another thread's.
56b2a6393fkeeps a handler's blocks as holds apart from the masksigprocmasksets. Delivery and the masksigprocmaskreports count them, and the handler releases only its own holds when it returns, so another thread'sSIG_BLOCKsurvives even for a signal in the handler'ssa_mask.SIG_UNBLOCKof a held signal releases its hold, so a handler can still unblock its own signal, andSIG_SETMASK, whichlongjmpuses, releases them all;sigsuspend's own restore keeps them.guest_signal_handler_maskgains your interleaving (a handler whosesa_maskholdsSIGTERMwhile another thread blocksSIGTERM) and a handler that unblocks and raises its own signal. On GitHub's runners (run) the new checks fail 3 of 3 with the previous head and pass 10 of 10 on Linux and Windows, and the libc and libkernel suites pass. The description now says what the holds keep and release.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:09:00Z.
Review: APPROVED
Rechecked the current head after the same-bit race fix. The author added the overlapping
sa_mask/concurrent same-signal block regression and reports the previous head fails while the fix passes 10/10 on Linux and Windows; all applicable relinker, register-context, write-watch, conventions, eligibility and report jobs now pass. Product Linux/Windows builds were skipped. The #8866 dependency is explicit; the #8872/#9001 signal registration changes are documented as composable, and #8872’s author confirmed the registration lines can rebase in either order. #8742/#9378 edit process APIs and the nearby debt sentence, not signal behavior; preserve both process and signal documentation when rebasing. I did not run local tests.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:25:53Z.
Review: DISMISSED
The latest same-signal race fix addresses the concrete issue from my first review: the handler keeps its own mask holds separate, and the author reports the overlapping
sa_mask/other-thread block regression fails on the prior head and passes 10/10 on Linux and Windows. I also confirm the PR documents #8866 as a prerequisite and records the agreed #8872/#9001 composition. I am requesting changes because all reported runs and the merge are based ona6dcea9279, while current upstream main isab347d144d. Please rebase and rerun the signal handler/mask and libc/libkernel focused tests plus conventions on current main, preserving the process/debt entries named in the overlap warnings. MacOS is listed as untested. Product builds were not dispatched; I did not run local tests.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T06:00:54Z.
Thanks, @MarcZX10. The branch now has
mainat4a17c71757merged in (280849ebde), with the PR's own diff unchanged. On GitHub's runners (run) the three signal tests pass 10 runs in a row on both and fail withmain's code; the suites pass apart from libc'sprecise_waiton Linux, which failed with and without the change, and conventions report no errors (onedebt-lengthwarning on thesigactionentry, in Tested). Tested now says so.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T16:28:29Z.
Review: COMMENTED
Reviewed head
8be8dc83afmerged onto main0119103fa9(clean merge). Linux, GCC 13, Release.What I ran:
guest_signal_handler_mask,guest_signal_sets,guest_signals,guest_signal_interrupton the merge: 10/10 runs pass.Signals.cppandJumpBuffer.cpp: the first three abort 3/3,guest_signal_interruptpasses.postsig_doneL1968-1981,sigdfltL961-970,sigactionL707-708 andsetjmp.SL51-85 say what the description says.One regression: a handler that saves and restores the mask with
SIG_SETMASKleaves its own signal blocked after it returns.Example, a SIGUSR2 handler with an empty
sa_mask:After
raise(SIGUSR2)returns, the mask is0x40000000, and a secondraiseonly marks it pending. SIGTERM does the same (0x4000). On main the mask is 0 afterwards and the second raise runs the handler. On FreeBSD the restore keeps it blocked inside the handler, thensigreturnputs back the pre-handler mask (machdep.c L574), so it's unblocked again.By reading, everything that saves and restores the mask inside a handler goes down the same path:
pselect(Pselect.cpp L21/L25),getcontext/setcontext, andsigsetjmp/siglongjmporsetjmp/longjmpwhen both run inside the handler.What I tried locally: in
ChangeBlockedMask'sSIG_SETMASKwithreleaseHolds, keep the hold of a held signal that the new set contains (leaving itsblockedMaskbit as it was), and clear only the holds of signals the set leaves out. That fixed the example above, and the 4 tests still passed 10/10. It doesn't coverpselect, though: its firstSIG_SETMASKdrops the hold, so its restore still sticks. So that case may need a different shape. A save/restore check inguest_signal_handler_maskwould pin the basic case down.Not checked: Windows, macOS, a console.
Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T17:35:36Z.
Review: APPROVED
Re-checked head
f5757aab98merged onto main2e1c72239b(clean merge). Linux, GCC 13, Release.What I ran:
guest_signal_handler_mask,guest_signal_sets,guest_signals,guest_signal_interrupton the merge: 10/10 runs pass.Signals.cpphunks reverted:guest_signal_handler_maskaborts 3/3, the other three pass.f5757aabSIG_BLOCKsave +SIG_SETMASKrestore (SIGUSR2, SIGTERM)pselect_nid_postfixwith an empty mask and a zero timeout (SIGUSR2, SIGTERM)sigsetjmp(buf, 1)+siglongjmp, both inside the handlersetjmp+longjmp, both inside the handlerSo my earlier finding is fixed, including the
pselectand jump paths the new tests don't call directly. Approving.One small thing, a debt-text nit rather than a blocker. If the handler restores the mask and another thread
SIG_BLOCKs the handler's own signal while it runs, that block is gone after the handler returns. The same goes for a signal in the handler'ssa_mask. Example: a SIGUSR1 handler does the save/restore above, then a second thread blocks SIGUSR1 and joins. Afterraisereturns, the mask is 0, not0x20000000. The order of the two steps doesn't matter. This is the per-thread release counter putting back the pre-handler bit, and I don't see a cheap fix with one process-wide mask. On FreeBSD that block would only apply to the other thread anyway. But the debt entry still says a block another thread adds meanwhile "is kept, for the same signal too", so maybe add "unless the handler's own thread replaced the mask" there.Not checked: Windows, macOS, a console,
getcontext/setcontextinside a handler.