fix(libkernel): report the sender in the si_code of sigwaitinfo and sigtimedwait #8497

Merged
xXPBSXx merged 7 commits from upstream-pr/8497 into main 2026-11-11 23:32:32 +00:00
xXPBSXx commented 2026-10-20 13:51:39 +00:00 (Migrated from github.com)

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 FillSiginfo always wrote si_code 0 (and the caller's pid). FreeBSD 11.4 stamps the signal with the sender: kill with SI_USER (0x10001, sys/kern/kern_sig.c:1788), thr_kill (so raise and pthread_kill) with SI_LWP (0x10007, kern_thr.c:401, lib/libc/gen/raise.c:52), and the interval-timer expiry with SI_KERNEL (0x10006) and pid 0 (kern_time.c:899, kern_sig.c:2087). Each pending signal now stores its code when it is marked, and sigwaitinfo/sigtimedwait return it. The expectations in guest_sigtimedwait that pinned 0 change accordingly.
Fixes #8496
The expectations of guest_sigtimedwait that #4138 set to 0 change here; #4138 took the siginfo_t layout from FreeBSD 11 and did not measure si_code on a console, as far as its description says.

The same change also takes the pending signal and its code together: WaitForPendingInSet cleared the pending bit under pending.lock, but FillSiginfo read 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). PendingInSet returns the code together with the signal under the lock, and FillSiginfo takes it as an argument. The branch has three fix commits and four merges of main (the last one, 78db32f978f3, merges main 1a9dbf9cd808, a normal merge without force-push); the debt entry for sigtimedwait also states that a number holds one pending entry where FreeBSD queues one per send.

Tested

Base main 1a9dbf9cd808; the same change as 78db32f978f3 applied 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 same main: with only the test changes 0 of 3 pass (all abort), with the change 3 of 3 pass. The case CheckReportedCodeBelongsToConsumedSignal sends the same signal with kill and then with pthread_kill in 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/main on head 78db32f978f3 reports no error and one debt-length warning for the sigtimedwait entry in docs/dev/debt/libkernel.md (2640 characters; the entry already had 2330 before this change).

  • Not tested on this base: Windows, macOS, the hosted Windows configuration, and the other signal tests (guest_signals, guest_itimer, ...). The earlier runs (20 per arm on Linux, 10 on Windows, where the old code also passed) were on f26434afa3 and are not repeated here.

Overlap (P2, 2026-11-07, list of open pull requests from 21:07 UTC, main 7402c64c33, checked 21:43-21:48 UTC, head 78db32f978f3): of the 102 open pull requests that change core/libs/prx/libkernel/System/src/Signals.cpp, core/libs/tests/GuestSigtimedwait.cpp or docs/dev/debt/libkernel.md, each was merged with and without this branch. None conflicts only with this branch (0 of 102); 42 conflict with main without it as well; the other 60 merge cleanly with and without it. This branch merges cleanly with main.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes #8496
  • Unblocks: none
  • GPUs tested: n/a (signal handling only)
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4936771656 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8497). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T13:51:39Z. --- ### What The guest queue of pending signals kept only a bit per signal, and `FillSiginfo` always wrote `si_code` 0 (and the caller's pid). FreeBSD 11.4 stamps the signal with the sender: `kill` with `SI_USER` (0x10001, sys/kern/kern_sig.c:1788), `thr_kill` (so `raise` and `pthread_kill`) with `SI_LWP` (0x10007, kern_thr.c:401, lib/libc/gen/raise.c:52), and the interval-timer expiry with `SI_KERNEL` (0x10006) and pid 0 (kern_time.c:899, kern_sig.c:2087). Each pending signal now stores its code when it is marked, and `sigwaitinfo`/`sigtimedwait` return it. The expectations in `guest_sigtimedwait` that pinned 0 change accordingly. Fixes #8496 The expectations of `guest_sigtimedwait` that #4138 set to 0 change here; #4138 took the `siginfo_t` layout from FreeBSD 11 and did not measure `si_code` on a console, as far as its description says. The same change also takes the pending signal and its code together: `WaitForPendingInSet` cleared the pending bit under `pending.lock`, but `FillSiginfo` read 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). `PendingInSet` returns the code together with the signal under the lock, and `FillSiginfo` takes it as an argument. The branch has three fix commits and four merges of `main` (the last one, `78db32f978f3`, merges `main` `1a9dbf9cd808`, a normal merge without force-push); the debt entry for `sigtimedwait` also states that a number holds one pending entry where FreeBSD queues one per send. ### Tested Base `main` `1a9dbf9cd808`; the same change as `78db32f978f3` applied 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 same `main`: with only the test changes 0 of 3 pass (all abort), with the change 3 of 3 pass. The case `CheckReportedCodeBelongsToConsumedSignal` sends the same signal with `kill` and then with `pthread_kill` in 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/main` on head `78db32f978f3` reports no error and one `debt-length` warning for the `sigtimedwait` entry in `docs/dev/debt/libkernel.md` (2640 characters; the entry already had 2330 before this change). - Not tested on this base: Windows, macOS, the hosted Windows configuration, and the other signal tests (`guest_signals`, `guest_itimer`, ...). The earlier runs (20 per arm on Linux, 10 on Windows, where the old code also passed) were on `f26434afa3` and are not repeated here. Overlap (P2, 2026-11-07, list of open pull requests from 21:07 UTC, `main` `7402c64c33`, checked 21:43-21:48 UTC, head `78db32f978f3`): of the 102 open pull requests that change `core/libs/prx/libkernel/System/src/Signals.cpp`, `core/libs/tests/GuestSigtimedwait.cpp` or `docs/dev/debt/libkernel.md`, each was merged with and without this branch. None conflicts only with this branch (0 of 102); 42 conflict with `main` without it as well; the other 60 merge cleanly with and without it. This branch merges cleanly with `main`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes #8496 <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a (signal handling only) <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-23 22:05:45 +00:00 (Migrated from github.com)

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. WaitForPendingInSet holds pending.lock while PendingInSet clears the bit, then returns and releases the lock; sigwaitinfo/sigtimedwait call FillSiginfo afterward, which loads Pending().codes[guest]. If another thread queues the same signal in that gap, MarkPending overwrites 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, have PendingInSet return both the signal and its code), then pass that captured value to FillSiginfo. 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5634940194 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8497#pullrequestreview-5634940194). Originally posted by [MarcZX10](https://github.com/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. `WaitForPendingInSet` holds `pending.lock` while `PendingInSet` clears the bit, then returns and releases the lock; `sigwaitinfo`/`sigtimedwait` call `FillSiginfo` afterward, which loads `Pending().codes[guest]`. If another thread queues the same signal in that gap, `MarkPending` overwrites 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, have `PendingInSet` return both the signal and its code), then pass that captured value to `FillSiginfo`. 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.
ahmadmuj commented 2026-10-25 16:16:33 +00:00 (Migrated from github.com)

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 main 0119103fa9).

What I ran, Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release), guest_sigtimedwait 20 runs per arm, head test kept in every arm:

  • head: 20/20 pass
  • main's Signals.cpp: 0/20
  • first commit's Signals.cpp (9a3c3ee): 14/20 pass, so here the race case only catches the old code some of the time

The CR fix looks right to me: codes[] is only written in MarkPending and read in PendingInSet, both under pending.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_add allocates one ksiginfo per send, standard signals too (kern_sig.c:403-414), and sigqueue_get keeps the bit while another one is queued (kern_sig.c:330-335). kill lands on p_sigqueue (pksignal -> tdsendsignal(p, NULL), kern_sig.c:2168), thr_kill on td_sigqueue (kern_sig.c:2170), and kern_sigtimedwait takes the thread queue first (kern_sig.c:1286-1287). So when both sends land before the wait, FreeBSD gives two waits, 0x10007 then 0x10001. The twice branch requires the opposite order, and the once branch only happens with coalescing.

Probe on the head, signal 2 blocked, both sends first, then poll until EAGAIN:

Sends Head FreeBSD 11.4 (from the source)
raise, raise 0x10007, 1 wait 0x10007, 0x10007
kill, pthread_kill(self) 0x10007, 1 wait 0x10007, 0x10001
pthread_kill(self), kill 0x10001, 1 wait 0x10007, 0x10001

The 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 == 0x10007 still fails on the old race (it showed up as 0x10007 twice) and accepts both FreeBSD orders.

2. sendCode leaks into sends from a handler that kill runs inline. kill -> raise_nid_postfix -> DeliverPendingSignal calls the guest handler while sendCode is still SI_USER (a host signal through std::raise -> Dispatch does the same). So a raise of a blocked signal inside that handler gets SI_USER. Probe: SIGUSR2 blocked, the SIGUSR1 handler calls raise(SIGUSR2), then kill(getpid(), SIGUSR1). sigtimedwait on SIGUSR2 reports 0x10001; FreeBSD gives SI_LWP (thr_kill, kern_thr.c:401). A siglongjmp out of that handler also skips ~SentByKill, and then every later raise on that thread reports SI_USER. Passing the code down explicitly fixes both: an internal raise that takes the code, and MarkPending with no default.

3. The debt entry is stale. The sigtimedwait entry in docs/dev/debt/libkernel.md still says only si_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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644093483 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8497#pullrequestreview-5644093483). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T16:16:33Z. **Review:** COMMENTED --- Checked head 09605fb4ad80 (base f26434afa36e; also merges clean onto main 0119103fa9ed). What I ran, Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release), `guest_sigtimedwait` 20 runs per arm, head test kept in every arm: - head: 20/20 pass - main's `Signals.cpp`: 0/20 - first commit's `Signals.cpp` (9a3c3ee): 14/20 pass, so here the race case only catches the old code some of the time The CR fix looks right to me: `codes[]` is only written in `MarkPending` and read in `PendingInSet`, both under `pending.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_add` allocates one ksiginfo per send, standard signals too (kern_sig.c:403-414), and `sigqueue_get` keeps the bit while another one is queued (kern_sig.c:330-335). `kill` lands on `p_sigqueue` (`pksignal` -> `tdsendsignal(p, NULL)`, kern_sig.c:2168), `thr_kill` on `td_sigqueue` (kern_sig.c:2170), and `kern_sigtimedwait` takes the thread queue first (kern_sig.c:1286-1287). So when both sends land before the wait, FreeBSD gives two waits, 0x10007 then 0x10001. The `twice` branch requires the opposite order, and the `once` branch only happens with coalescing. Probe on the head, signal 2 blocked, both sends first, then poll until EAGAIN: | Sends | Head | FreeBSD 11.4 (from the source) | |---|---|---| | `raise`, `raise` | 0x10007, 1 wait | 0x10007, 0x10007 | | `kill`, `pthread_kill(self)` | 0x10007, 1 wait | 0x10007, 0x10001 | | `pthread_kill(self)`, `kill` | 0x10001, 1 wait | 0x10007, 0x10001 | The 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 == 0x10007` still fails on the old race (it showed up as 0x10007 twice) and accepts both FreeBSD orders. **2. `sendCode` leaks into sends from a handler that `kill` runs inline.** `kill` -> `raise_nid_postfix` -> `DeliverPendingSignal` calls the guest handler while `sendCode` is still SI_USER (a host signal through `std::raise` -> `Dispatch` does the same). So a `raise` of a blocked signal inside that handler gets SI_USER. Probe: SIGUSR2 blocked, the SIGUSR1 handler calls `raise(SIGUSR2)`, then `kill(getpid(), SIGUSR1)`. `sigtimedwait` on SIGUSR2 reports 0x10001; FreeBSD gives SI_LWP (thr_kill, kern_thr.c:401). A `siglongjmp` out of that handler also skips `~SentByKill`, and then every later `raise` on that thread reports SI_USER. Passing the code down explicitly fixes both: an internal raise that takes the code, and `MarkPending` with no default. **3. The debt entry is stale.** The `sigtimedwait` entry in `docs/dev/debt/libkernel.md` still says only `si_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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!8497
No description provided.