fix(libkernel): report the sender in the si_code of sigwaitinfo and sigtimedwait #8497
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8497"
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 xXPBSXx on 2026-10-20T13:51:39Z.
What
The guest queue of pending signals kept only a bit per signal, and
FillSiginfoalways wrotesi_code0 (and the caller's pid). FreeBSD 11.4 stamps the signal with the sender:killwithSI_USER(0x10001, sys/kern/kern_sig.c:1788),thr_kill(soraiseandpthread_kill) withSI_LWP(0x10007, kern_thr.c:401, lib/libc/gen/raise.c:52), and the interval-timer expiry withSI_KERNEL(0x10006) and pid 0 (kern_time.c:899, kern_sig.c:2087). Each pending signal now stores its code when it is marked, andsigwaitinfo/sigtimedwaitreturn it. The expectations inguest_sigtimedwaitthat pinned 0 change accordingly.Fixes #8496
The expectations of
guest_sigtimedwaitthat #4138 set to 0 change here; #4138 took thesiginfo_tlayout from FreeBSD 11 and did not measuresi_codeon a console, as far as its description says.The same change also takes the pending signal and its code together:
WaitForPendingInSetcleared the pending bit underpending.lock, butFillSiginforead the code after the lock was released, so a second sender could overwrite the code in between and the wait reported the later sender for the signal it had already consumed (review of 23 October).PendingInSetreturns the code together with the signal under the lock, andFillSiginfotakes it as an argument. The branch has three fix commits and four merges ofmain(the last one,78db32f978f3, mergesmain1a9dbf9cd808, a normal merge without force-push); the debt entry forsigtimedwaitalso states that a number holds one pending entry where FreeBSD queues one per send.Tested
Base
main1a9dbf9cd808; the same change as78db32f978f3applied on it as one diff. Linux (WSL2 Ubuntu 24.04: a virtualised kernel, not bare-metal; GCC 13.3, Release),ctest -R '^guest_sigtimedwait$', 3 runs per arm on the samemain: with only the test changes 0 of 3 pass (all abort), with the change 3 of 3 pass. The caseCheckReportedCodeBelongsToConsumedSignalsends the same signal withkilland then withpthread_killin each of 300 rounds; there is no deterministic hook between releasing the lock and reading the code, so it is a stress test.python tools/check_conventions.py --base upstream/mainon head78db32f978f3reports no error and onedebt-lengthwarning for thesigtimedwaitentry indocs/dev/debt/libkernel.md(2640 characters; the entry already had 2330 before this change).guest_signals,guest_itimer, ...). The earlier runs (20 per arm on Linux, 10 on Windows, where the old code also passed) were onf26434afa3and are not repeated here.Overlap (P2, 2026-11-07, list of open pull requests from 21:07 UTC,
main7402c64c33, checked 21:43-21:48 UTC, head78db32f978f3): of the 102 open pull requests that changecore/libs/prx/libkernel/System/src/Signals.cpp,core/libs/tests/GuestSigtimedwait.cppordocs/dev/debt/libkernel.md, each was merged with and without this branch. None conflicts only with this branch (0 of 102); 42 conflict withmainwithout it as well; the other 60 merge cleanly with and without it. This branch merges cleanly withmain.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)AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T22:05:45Z.
Review: DISMISSED
The signal code can race with a second sender after the wait has consumed the pending bit.
WaitForPendingInSetholdspending.lockwhilePendingInSetclears the bit, then returns and releases the lock;sigwaitinfo/sigtimedwaitcallFillSiginfoafterward, which loadsPending().codes[guest]. If another thread queues the same signal in that gap,MarkPendingoverwrites the code and the waiter reports the later sender for the signal it already consumed. The current tests use one sender at a time, so they do not cover this interleaving.Please capture the code together with consuming the pending signal while holding
pending.lock(for example, havePendingInSetreturn both the signal and its code), then pass that captured value toFillSiginfo. Add a focused concurrent regression if practical. I have not run this branch locally; conventions and eligibility pass, while product jobs are skipped by the current workflow.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T16:16:33Z.
Review: COMMENTED
Checked head 09605fb4ad80 (base
f26434afa3; also merges clean onto main0119103fa9).What I ran, Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release),
guest_sigtimedwait20 runs per arm, head test kept in every arm:Signals.cpp: 0/20Signals.cpp(9a3c3ee): 14/20 pass, so here the race case only catches the old code some of the timeThe CR fix looks right to me:
codes[]is only written inMarkPendingand read inPendingInSet, both underpending.lock, so the code always goes with the bit that was taken.Three things, checked against FreeBSD
release/11.4.0:1. The new race test pins coalescing that FreeBSD doesn't do.
sigqueue_addallocates one ksiginfo per send, standard signals too (kern_sig.c:403-414), andsigqueue_getkeeps the bit while another one is queued (kern_sig.c:330-335).killlands onp_sigqueue(pksignal->tdsendsignal(p, NULL), kern_sig.c:2168),thr_killontd_sigqueue(kern_sig.c:2170), andkern_sigtimedwaittakes the thread queue first (kern_sig.c:1286-1287). So when both sends land before the wait, FreeBSD gives two waits, 0x10007 then 0x10001. Thetwicebranch requires the opposite order, and theoncebranch only happens with coalescing.Probe on the head, signal 2 blocked, both sends first, then poll until EAGAIN:
raise,raisekill,pthread_kill(self)pthread_kill(self),killThe single bit is older than this PR, so a real queue can be a follow-up. But then the test shouldn't lock in the one-slot result, and last-sender-wins belongs in the debt entry. For example
twice ? first.code != second.code : first.code == 0x10007still fails on the old race (it showed up as 0x10007 twice) and accepts both FreeBSD orders.2.
sendCodeleaks into sends from a handler thatkillruns inline.kill->raise_nid_postfix->DeliverPendingSignalcalls the guest handler whilesendCodeis still SI_USER (a host signal throughstd::raise->Dispatchdoes the same). So araiseof a blocked signal inside that handler gets SI_USER. Probe: SIGUSR2 blocked, the SIGUSR1 handler callsraise(SIGUSR2), thenkill(getpid(), SIGUSR1).sigtimedwaiton SIGUSR2 reports 0x10001; FreeBSD gives SI_LWP (thr_kill, kern_thr.c:401). Asiglongjmpout of that handler also skips~SentByKill, and then every laterraiseon that thread reports SI_USER. Passing the code down explicitly fixes both: an internal raise that takes the code, andMarkPendingwith no default.3. The debt entry is stale. The
sigtimedwaitentry indocs/dev/debt/libkernel.mdstill says onlysi_code(SI_USER) is written. CONTRIBUTING asks to cut it to what's still open, so: per-send queuing and last sender wins.Not checked: Windows, macOS, a console. The codes come from the FreeBSD source only.