fix(libkernel): block a signal while its handler runs and honour SA_RESETHAND #9499

Merged
EagleBearerYS merged 15 commits from upstream-pr/9499 into main 2026-10-25 21:05:13 +00:00
EagleBearerYS commented 2026-10-21 23:32:01 +00:00 (Migrated from github.com)

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. Dispatch and DeliverPendingSignal in Signals.cpp call it without blocking anything, so:

  • a handler that raises its own signal re-enters itself at once, instead of the signal staying pending until the handler returns;
  • sigprocmask inside a handler does not report the signal or the handler's sa_mask as blocked;
  • SA_RESETHAND leaves the handler installed for the next delivery.

FreeBSD sets up the handler's frame and then, in postsig_done, blocks sa_mask and, unless SA_NODEFER is set, the signal itself. It also resets the disposition to SIG_DFL when SA_RESETHAND is set (sigdflt), and sigreturn restores the previous mask. sigaction keeps reporting SA_RESETHAND afterwards (L707-L708), because sigdflt does not clear ps_sigreset, which kern_sigaction sets from the flag.

The handler now runs with its signal (unless SA_NODEFER) and its sa_mask blocked, in all four words of the mask, without SIGKILL and SIGSTOP, which sigprocmask leaves unblocked since #8407. main's mask is process-wide, so these blocks are holds kept apart from the mask sigprocmask sets: they count as blocked for delivery and in the mask sigprocmask reports, 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's sa_mask. SIG_UNBLOCK of a held signal releases its hold, so a handler can still unblock its own signal, and SIG_SETMASK, which longjmp uses, 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 with SIG_SETMASK, the signal gets back its bit of the mask from before the handler when the handler returns, as FreeBSD's sigreturn puts back the mask from before it; a mask another thread sets meanwhile is kept. Pending signals that this unblocks are delivered when raise returns, as _sigprocmask already does. SA_RESETHAND resets the handler to the default before it runs.

A handler that leaves with longjmp never returns there, so its signal would stay blocked. FreeBSD's setjmp saves the signal mask in the jump buffer and its longjmp restores it (setjmp.S), but ours keeps only the registers. libc's setjmp now saves the mask at 72 in the jump buffer, with the flag sigsetjmp keeps at 88, and longjmp restores it. libc doesn't link libkernel, so libkernel registers its sigprocmask with libc when it loads. Both changes followed CnRJay's review on #5312.

guest_signals changes one expectation. In main's check of a handler run by sigsuspend with SIGKILL, SIGSTOP and 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 of 1u << 28, as FreeBSD's postsig_done adds it. The check that SIGKILL and SIGSTOP stay 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 and SA_RESETHAND, with guest_signal_handler_mask;
  • ebb86e4c2e, the sigaction entry in docs/dev/debt/libkernel.md;
  • ef45904842, unblocking only what the handler added;
  • 624815b200, libc's setjmp and longjmp saving and restoring the mask, with a docs/dev/debt/libc.md entry;
  • 9f584de29b, a check that leaves a handler with longjmp and raises its signal again;
  • e607ba1b41, the merge of main, which also updates the guest_signals expectation above;
  • 780aa44726, the handler's mask in all four mask words, since main now keeps them, with a check for signals above 32;
  • d3370bf57a, the jump buffer's calls spelled for Mach-O hosts, as main'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 for siginterrupt) builds on them and conflicts with this PR at the end of Signals.cpp, where this PR registers libkernel's sigprocmask with 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 in Signals.cpp. It goes after this PR and rebases onto this PR's ChangeBlockedMask (merge order only, agreed on #10501).

#10749 (ycbuzi) moves siginterrupt from libc to libSceLibcInternal and removes the block at the end of Signals.cpp that registers sigaction with libc, next to this PR's RegisteredForJumps, which registers sigprocmask with libc's jump buffers. In the libkernel debt file it rewrites the signal entry, beside this PR's sigaction entry. This PR goes after it (merge order only): when it merges main after #10749, it keeps RegisteredForJumps without the removed block, and both debt entries.

No title is claimed.

Tested

On GitHub's runners from my fork, merged with main at a6dcea9279 (run), on both Linux and Windows:

  • guest_signal_handler_mask: a handler raises its own signal, both on SIGUSR2, which has no host signal, and on SIGTERM, which goes through the host. It also checks SA_NODEFER, the mask sigprocmask reports inside the handler, SA_RESETHAND, a mask change from another thread while the handler runs, a handler left with longjmp whose signal is raised again, and a handler whose sa_mask names SIGKILL, signal 40 and signal 128.
  • guest_signal_sets checks that setjmp saves the mask at 72 with the flag at 88 and that longjmp restores it.
  • With main's Signals.cpp and JumpBuffer.cpp, guest_signal_handler_mask, guest_signal_sets and guest_signals fail. With the change all three pass 10 runs in a row.
  • Every test registered under core/libs/prx/libc and core/libs/prx/libkernel passes: 115 and 198 on Linux, 142 and 208 on Windows.
  • 780aa44726 on its own (run): with the merge's code, the new upper-word check in guest_signal_handler_mask fails 3 of 3 on both; with the commit it passes 10 of 10.
  • 56b2a6393f (run): guest_signal_handler_mask gains a handler whose sa_mask holds SIGTERM while another thread blocks SIGTERM, 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 commit guest_signal_handler_mask, guest_signal_sets and guest_signals pass 10 of 10, and the libc and libkernel suites pass.
  • After the merge with main at 4a17c71757 (280849ebde, no conflicts, the PR's own diff unchanged; run): guest_signal_handler_mask, guest_signal_sets and guest_signals fail with main'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_wait failed with and without the change. check_conventions.py reports no errors and one warning, debt-length, on the sigaction entry: 2,482 characters, against 1,426 on main.
  • After the merge with main at 895f47f4ee (b463132a56, which brings in #8866; run):
    • Two conflicts, both resolved by keeping both sides. In Signals.cpp, signal_nid_postfix keeps this PR's SettleReset(guest) before #8866's handler record, which sets SA_RESTART unless the signal is in _sigintr. The libkernel debt file keeps this PR's sigaction entry and #8866's signal entry. The PR's own changed lines (git diff -U0 against main) are unchanged.
    • guest_signal_handler_mask, guest_signal_sets and guest_signals fail with main's code on both runners and pass 10 runs in a row with the change, none skipped.
    • The libc and libkernel suites pass: 117 and 199 on Linux, 144 and 209 on Windows. They ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
    • check_conventions.py --base 895f47f4ee reports no errors and the same debt-length warning on the sigaction entry.
  • After the merge with main at dc9f1ef656 (b72ea79cf9; run):
    • One conflict, in the libkernel debt file: main rewrote the getsid entry (now getsid, setpgid, setpgrp) on the line before this PR's sigaction entry. The merge keeps main's getsid entry and this PR's sigaction entry. main's other changes to Signals.cpp and GuestSignals.cpp (discarding a pending signal that sigaction or signal ignores, 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_sets and guest_signals fail with main'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.
    • The libc and libkernel suites pass: 143 and 205 on Linux, 170 and 218 on Windows. They ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • After the merge with main at 6b496aa1d1 (9e7b2ade4c, which brings in #8872; run):
    • Two conflicts. At the end of Signals.cpp, #8872 registers libkernel's sigaction and _sigintr with libc at load, where this PR registers its sigprocmask: both registrations are kept, this PR's first, in the anonymous namespace #8872 adds. In the libkernel debt file, the merge keeps this PR's sigaction entry and #8872's signal entry, which now mentions siginterrupt. Apart from that namespace, the PR's own changed lines are unchanged.
    • guest_signal_handler_mask, guest_signal_sets and guest_signals, which now holds #8872's siginterrupt checks, fail with main'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.
    • The libc and libkernel suites pass: 143 and 206 on Linux, 170 and 222 on Windows. They ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • tools/check_conventions.py --base origin/main passes for every commit.
  • Not tested: macOS (the Mach-O spelling is built only there), a console, any title.
  • 8be8dc83af, a merge of main e05ba6feeb (run). main's #10706 moves the declarations of exported functions and their types into the libraries' public headers, which its public_headers test checks. The merge keeps this PR's DeliverUnblockedPending and ChangeBlockedMask with main's KernelTimespec timeout in WaitForPendingInSet, and takes main's side of the declarations in GuestSignalSets.cpp: libc's Api.hpp now declares the setjmp and longjmp this PR had added there. The new GuestSignalHandlerMask.cpp includes the libkernel and libc public headers in place of its own declarations and its own GuestSignalSet and GuestSigaction types. The guest_signal_handler_mask, guest_signal_sets, guest_signals and public_headers tests 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 fails guest_signals, guest_signal_handler_mask and guest_signal_sets on both. Suites: libc and libkernel pass on Linux (144 and 208) and on Windows (171 and 224). The suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • f5757aab98, after ahmadmuj's review (run): guest_signal_handler_mask gains a handler that saves the mask and puts it back with SIG_SETMASK, for SIGUSR2 and for SIGTERM, 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 code guest_signal_handler_mask fails on both runners; with the commit guest_signal_handler_mask, guest_signal_sets, guest_signals and public_headers pass 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 with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.

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: #10749 (merge order only, see Overlap); #8866 and #8872 are merged
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4959763827 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9499). Originally posted by [EagleBearerYS](https://github.com/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. `Dispatch` and `DeliverPendingSignal` in `Signals.cpp` call it without blocking anything, so: - a handler that raises its own signal re-enters itself at once, instead of the signal staying pending until the handler returns; - `sigprocmask` inside a handler does not report the signal or the handler's `sa_mask` as blocked; - `SA_RESETHAND` leaves the handler installed for the next delivery. FreeBSD sets up the handler's frame and then, in [`postsig_done`](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/kern_sig.c#L1968-L1981), blocks `sa_mask` and, unless `SA_NODEFER` is set, the signal itself. It also resets the disposition to `SIG_DFL` when `SA_RESETHAND` is set ([`sigdflt`](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/kern_sig.c#L961-L970)), and `sigreturn` restores the previous mask. `sigaction` keeps reporting `SA_RESETHAND` afterwards ([L707-L708](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/kern_sig.c#L707-L708)), because `sigdflt` does not clear `ps_sigreset`, which [`kern_sigaction`](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/kern_sig.c#L752-L759) sets from the flag. The handler now runs with its signal (unless `SA_NODEFER`) and its `sa_mask` blocked, in all four words of the mask, without `SIGKILL` and `SIGSTOP`, which `sigprocmask` leaves unblocked since #8407. `main`'s mask is process-wide, so these blocks are holds kept apart from the mask `sigprocmask` sets: they count as blocked for delivery and in the mask `sigprocmask` reports, 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's `sa_mask`. `SIG_UNBLOCK` of a held signal releases its hold, so a handler can still unblock its own signal, and `SIG_SETMASK`, which `longjmp` uses, 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 with `SIG_SETMASK`, the signal gets back its bit of the mask from before the handler when the handler returns, as FreeBSD's [`sigreturn`](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/amd64/amd64/machdep.c#L574) puts back the mask from before it; a mask another thread sets meanwhile is kept. Pending signals that this unblocks are delivered when `raise` returns, as `_sigprocmask` already does. `SA_RESETHAND` resets the handler to the default before it runs. A handler that leaves with `longjmp` never returns there, so its signal would stay blocked. FreeBSD's `setjmp` saves the signal mask in the jump buffer and its `longjmp` restores it ([`setjmp.S`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/amd64/gen/setjmp.S#L51-L85)), but ours keeps only the registers. libc's `setjmp` now saves the mask at 72 in the jump buffer, with the flag `sigsetjmp` keeps at 88, and `longjmp` restores it. libc doesn't link libkernel, so libkernel registers its `sigprocmask` with libc when it loads. Both changes followed CnRJay's review on #5312. **`guest_signals` changes one expectation.** In `main`'s check of a handler run by `sigsuspend` with `SIGKILL`, `SIGSTOP` and 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 of `1u << 28`, as FreeBSD's `postsig_done` adds it. The check that `SIGKILL` and `SIGSTOP` stay 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 and `SA_RESETHAND`, with `guest_signal_handler_mask`; - ebb86e4c2e, the `sigaction` entry in `docs/dev/debt/libkernel.md`; - ef45904842, unblocking only what the handler added; - 624815b200, libc's `setjmp` and `longjmp` saving and restoring the mask, with a `docs/dev/debt/libc.md` entry; - 9f584de29b, a check that leaves a handler with `longjmp` and raises its signal again; - e607ba1b41, the merge of `main`, which also updates the `guest_signals` expectation above; - 780aa44726, the handler's mask in all four mask words, since `main` now keeps them, with a check for signals above 32; - d3370bf57a, the jump buffer's calls spelled for Mach-O hosts, as `main`'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 for `siginterrupt`) builds on them and conflicts with this PR at the end of `Signals.cpp`, where this PR registers libkernel's `sigprocmask` with 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 in `Signals.cpp`. It goes after this PR and rebases onto this PR's `ChangeBlockedMask` (merge order only, [agreed on #10501](https://github.com/boykopovar/AnyPS5/pull/10501#issuecomment-6384305140)). #10749 (ycbuzi) moves `siginterrupt` from libc to libSceLibcInternal and removes the block at the end of `Signals.cpp` that registers `sigaction` with libc, next to this PR's `RegisteredForJumps`, which registers `sigprocmask` with libc's jump buffers. In the libkernel debt file it rewrites the `signal` entry, beside this PR's `sigaction` entry. This PR goes after it (merge order only): when it merges `main` after #10749, it keeps `RegisteredForJumps` without the removed block, and both debt entries. No title is claimed. ### Tested On GitHub's runners from my fork, merged with `main` at a6dcea9279 ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39689751257)), on both Linux and Windows: - `guest_signal_handler_mask`: a handler raises its own signal, both on `SIGUSR2`, which has no host signal, and on `SIGTERM`, which goes through the host. It also checks `SA_NODEFER`, the mask `sigprocmask` reports inside the handler, `SA_RESETHAND`, a mask change from another thread while the handler runs, a handler left with `longjmp` whose signal is raised again, and a handler whose `sa_mask` names `SIGKILL`, signal 40 and signal 128. - `guest_signal_sets` checks that `setjmp` saves the mask at 72 with the flag at 88 and that `longjmp` restores it. - With `main`'s `Signals.cpp` and `JumpBuffer.cpp`, `guest_signal_handler_mask`, `guest_signal_sets` and `guest_signals` fail. With the change all three pass 10 runs in a row. - Every test registered under `core/libs/prx/libc` and `core/libs/prx/libkernel` passes: 115 and 198 on Linux, 142 and 208 on Windows. - 780aa44726 on its own ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39689835667)): with the merge's code, the new upper-word check in `guest_signal_handler_mask` fails 3 of 3 on both; with the commit it passes 10 of 10. - 56b2a6393f ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39694854617)): `guest_signal_handler_mask` gains a handler whose `sa_mask` holds `SIGTERM` while another thread blocks `SIGTERM`, 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 commit `guest_signal_handler_mask`, `guest_signal_sets` and `guest_signals` pass 10 of 10, and the libc and libkernel suites pass. - **After the merge with `main` at 4a17c71757** (280849ebde, no conflicts, the PR's own diff unchanged; [run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39728939165)): `guest_signal_handler_mask`, `guest_signal_sets` and `guest_signals` fail with `main`'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_wait` failed with and without the change. `check_conventions.py` reports no errors and one warning, `debt-length`, on the `sigaction` entry: 2,482 characters, against 1,426 on `main`. - **After the merge with `main` at 895f47f4ee** (b463132a56, which brings in #8866; [run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39872058359)): - Two conflicts, both resolved by keeping both sides. In `Signals.cpp`, `signal_nid_postfix` keeps this PR's `SettleReset(guest)` before #8866's handler record, which sets `SA_RESTART` unless the signal is in `_sigintr`. The libkernel debt file keeps this PR's `sigaction` entry and #8866's `signal` entry. The PR's own changed lines (`git diff -U0` against `main`) are unchanged. - `guest_signal_handler_mask`, `guest_signal_sets` and `guest_signals` fail with `main`'s code on both runners and pass 10 runs in a row with the change, none skipped. - The libc and libkernel suites pass: 117 and 199 on Linux, 144 and 209 on Windows. They ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - `check_conventions.py --base 895f47f4ee` reports no errors and the same `debt-length` warning on the `sigaction` entry. - **After the merge with `main` at dc9f1ef656** (b72ea79cf9; [run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40114047583)): - One conflict, in the libkernel debt file: `main` rewrote the `getsid` entry (now `getsid, setpgid, setpgrp`) on the line before this PR's `sigaction` entry. The merge keeps `main`'s `getsid` entry and this PR's `sigaction` entry. `main`'s other changes to `Signals.cpp` and `GuestSignals.cpp` (discarding a pending signal that `sigaction` or `signal` ignores, 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_sets` and `guest_signals` fail with `main`'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. - The libc and libkernel suites pass: 143 and 205 on Linux, 170 and 218 on Windows. They ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - **After the merge with `main` at 6b496aa1d1** (9e7b2ade4c, which brings in #8872; [run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40117788465)): - Two conflicts. At the end of `Signals.cpp`, #8872 registers libkernel's `sigaction` and `_sigintr` with libc at load, where this PR registers its `sigprocmask`: both registrations are kept, this PR's first, in the anonymous namespace #8872 adds. In the libkernel debt file, the merge keeps this PR's `sigaction` entry and #8872's `signal` entry, which now mentions `siginterrupt`. Apart from that namespace, the PR's own changed lines are unchanged. - `guest_signal_handler_mask`, `guest_signal_sets` and `guest_signals`, which now holds #8872's `siginterrupt` checks, fail with `main`'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. - The libc and libkernel suites pass: 143 and 206 on Linux, 170 and 222 on Windows. They ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - `tools/check_conventions.py --base origin/main` passes for every commit. - Not tested: macOS (the Mach-O spelling is built only there), a console, any title. - **8be8dc83af, a merge of `main` e05ba6feeb** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40200814495)). `main`'s #10706 moves the declarations of exported functions and their types into the libraries' public headers, which its `public_headers` test checks. The merge keeps this PR's `DeliverUnblockedPending` and `ChangeBlockedMask` with `main`'s `KernelTimespec` timeout in `WaitForPendingInSet`, and takes `main`'s side of the declarations in `GuestSignalSets.cpp`: libc's `Api.hpp` now declares the `setjmp` and `longjmp` this PR had added there. The new `GuestSignalHandlerMask.cpp` includes the libkernel and libc public headers in place of its own declarations and its own `GuestSignalSet` and `GuestSigaction` types. The `guest_signal_handler_mask`, `guest_signal_sets`, `guest_signals` and `public_headers` tests 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 fails `guest_signals`, `guest_signal_handler_mask` and `guest_signal_sets` on both. Suites: libc and libkernel pass on Linux (144 and 208) and on Windows (171 and 224). The suites ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - **f5757aab98, after ahmadmuj's review** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40213776330)): `guest_signal_handler_mask` gains a handler that saves the mask and puts it back with `SIG_SETMASK`, for `SIGUSR2` and for `SIGTERM`, 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 code `guest_signal_handler_mask` fails on both runners; with the commit `guest_signal_handler_mask`, `guest_signal_sets`, `guest_signals` and `public_headers` pass 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 with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. ### 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: #10749 (merge order only, see Overlap); #8866 and #8872 are merged - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-21 23:34:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T23:34:00Z.

Review: CHANGES_REQUESTED


RunHandler can lose a concurrent same-signal mask change. For example, install a handler whose sa_mask contains SIGTERM, enter it while SIGTERM was previously unblocked, and have another guest thread call sigprocmask(SIG_BLOCK, SIGTERM) before the handler returns. added[0] records that this handler set the bit, and the unconditional blockedMask.fetch_and(~added[0]) then clears the other thread's block. The current CheckOtherThreadKept only 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607511952 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9499#pullrequestreview-5607511952). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T23:34:00Z. **Review:** CHANGES_REQUESTED --- `RunHandler` can lose a concurrent same-signal mask change. For example, install a handler whose `sa_mask` contains SIGTERM, enter it while SIGTERM was previously unblocked, and have another guest thread call `sigprocmask(SIG_BLOCK, SIGTERM)` before the handler returns. `added[0]` records that this handler set the bit, and the unconditional `blockedMask.fetch_and(~added[0])` then clears the other thread's block. The current `CheckOtherThreadKept` only 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.
EagleBearerYS commented 2026-10-21 23:52:39 +00:00 (Migrated from github.com)

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. 56b2a6393f keeps a handler's blocks as holds apart from the mask sigprocmask sets. Delivery and the mask sigprocmask reports count them, and the handler releases only its own holds when it returns, so another thread's SIG_BLOCK survives even for a signal in the handler's sa_mask. SIG_UNBLOCK of a held signal releases its hold, so a handler can still unblock its own signal, and SIG_SETMASK, which longjmp uses, releases them all; sigsuspend's own restore keeps them.

guest_signal_handler_mask gains your interleaving (a handler whose sa_mask holds SIGTERM while another thread blocks SIGTERM) 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329184324 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9499#issuecomment-6329184324). Originally posted by [EagleBearerYS](https://github.com/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. 56b2a6393f keeps a handler's blocks as holds apart from the mask `sigprocmask` sets. Delivery and the mask `sigprocmask` reports count them, and the handler releases only its own holds when it returns, so another thread's `SIG_BLOCK` survives even for a signal in the handler's `sa_mask`. `SIG_UNBLOCK` of a held signal releases its hold, so a handler can still unblock its own signal, and `SIG_SETMASK`, which `longjmp` uses, releases them all; `sigsuspend`'s own restore keeps them. `guest_signal_handler_mask` gains your interleaving (a handler whose `sa_mask` holds `SIGTERM` while another thread blocks `SIGTERM`) and a handler that unblocks and raises its own signal. On GitHub's runners ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39694854617)) 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.
MarcZX10 commented 2026-10-22 02:09:00 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5608469099 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9499#pullrequestreview-5608469099). Originally posted by [MarcZX10](https://github.com/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.
MarcZX10 commented 2026-10-22 04:25:53 +00:00 (Migrated from github.com)

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 on a6dcea9279, while current upstream main is ab347d144d. 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609252228 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9499#pullrequestreview-5609252228). Originally posted by [MarcZX10](https://github.com/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 on a6dcea9279, while current upstream main is ab347d144d88fae054d9770cf0db8d3565735c02. 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.
EagleBearerYS commented 2026-10-22 06:00:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T06:00:54Z.


Thanks, @MarcZX10. The branch now has main at 4a17c71757 merged 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 with main's code; the suites pass apart from libc's precise_wait on Linux, which failed with and without the change, and conventions report no errors (one debt-length warning on the sigaction entry, in Tested). Tested now says so.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6333922246 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9499#issuecomment-6333922246). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T06:00:54Z. --- Thanks, @MarcZX10. The branch now has `main` at 4a17c71757 merged in (280849ebde), with the PR's own diff unchanged. On GitHub's runners ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39728939165)) the three signal tests pass 10 runs in a row on both and fail with `main`'s code; the suites pass apart from libc's `precise_wait` on Linux, which failed with and without the change, and conventions report no errors (one `debt-length` warning on the `sigaction` entry, in Tested). Tested now says so.
ahmadmuj commented 2026-10-25 16:28:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T16:28:29Z.

Review: COMMENTED


Reviewed head 8be8dc83af merged onto main 0119103fa9 (clean merge). Linux, GCC 13, Release.

What I ran:

  • guest_signal_handler_mask, guest_signal_sets, guest_signals, guest_signal_interrupt on the merge: 10/10 runs pass.
  • The same tests with main's Signals.cpp and JumpBuffer.cpp: the first three abort 3/3, guest_signal_interrupt passes.
  • FreeBSD at d106002967 (that's releng/11.4): postsig_done L1968-1981, sigdflt L961-970, sigaction L707-708 and setjmp.S L51-85 say what the description says.

One regression: a handler that saves and restores the mask with SIG_SETMASK leaves its own signal blocked after it returns.

Example, a SIGUSR2 handler with an empty sa_mask:

GuestSignalSet old{};
sigprocmask_nid_postfix(SIG_BLOCK, nullptr, &old);   // old has SIGUSR2, reported from the hold
sigprocmask_nid_postfix(SIG_SETMASK, &old, nullptr); // stores SIGUSR2 in blockedMask and clears the hold

After raise(SIGUSR2) returns, the mask is 0x40000000, and a second raise only 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, then sigreturn puts 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, and sigsetjmp/siglongjmp or setjmp/longjmp when both run inside the handler.

What I tried locally: in ChangeBlockedMask's SIG_SETMASK with releaseHolds, keep the hold of a held signal that the new set contains (leaving its blockedMask bit 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 cover pselect, though: its first SIG_SETMASK drops the hold, so its restore still sticks. So that case may need a different shape. A save/restore check in guest_signal_handler_mask would pin the basic case down.

Not checked: Windows, macOS, a console.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644130916 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9499#pullrequestreview-5644130916). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T16:28:29Z. **Review:** COMMENTED --- Reviewed head 8be8dc83af0c merged onto main 0119103fa9ed (clean merge). Linux, GCC 13, Release. What I ran: - `guest_signal_handler_mask`, `guest_signal_sets`, `guest_signals`, `guest_signal_interrupt` on the merge: 10/10 runs pass. - The same tests with main's `Signals.cpp` and `JumpBuffer.cpp`: the first three abort 3/3, `guest_signal_interrupt` passes. - FreeBSD at d106002967 (that's releng/11.4): `postsig_done` L1968-1981, `sigdflt` L961-970, `sigaction` L707-708 and `setjmp.S` L51-85 say what the description says. One regression: a handler that saves and restores the mask with `SIG_SETMASK` leaves its own signal blocked after it returns. Example, a SIGUSR2 handler with an empty `sa_mask`: ```cpp GuestSignalSet old{}; sigprocmask_nid_postfix(SIG_BLOCK, nullptr, &old); // old has SIGUSR2, reported from the hold sigprocmask_nid_postfix(SIG_SETMASK, &old, nullptr); // stores SIGUSR2 in blockedMask and clears the hold ``` After `raise(SIGUSR2)` returns, the mask is `0x40000000`, and a second `raise` only 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, then `sigreturn` puts back the pre-handler mask ([machdep.c L574](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/amd64/amd64/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`, and `sigsetjmp`/`siglongjmp` or `setjmp`/`longjmp` when both run inside the handler. What I tried locally: in `ChangeBlockedMask`'s `SIG_SETMASK` with `releaseHolds`, keep the hold of a held signal that the new set contains (leaving its `blockedMask` bit 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 cover `pselect`, though: its first `SIG_SETMASK` drops the hold, so its restore still sticks. So that case may need a different shape. A save/restore check in `guest_signal_handler_mask` would pin the basic case down. Not checked: Windows, macOS, a console.
ahmadmuj commented 2026-10-25 17:35:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T17:35:36Z.

Review: APPROVED


Re-checked head f5757aab98 merged onto main 2e1c72239b (clean merge). Linux, GCC 13, Release.

What I ran:

  • guest_signal_handler_mask, guest_signal_sets, guest_signals, guest_signal_interrupt on the merge: 10/10 runs pass.
  • Same merge with only f5757aab's Signals.cpp hunks reverted: guest_signal_handler_mask aborts 3/3, the other three pass.
  • A probe, run on both trees, where a handler does something that replaces the mask and returns. Then I check the mask and raise again:
inside the handler f5757aab without its hunks
SIG_BLOCK save + SIG_SETMASK restore (SIGUSR2, SIGTERM) mask 0, runs again stuck blocked
real pselect_nid_postfix with an empty mask and a zero timeout (SIGUSR2, SIGTERM) mask 0, runs again stuck blocked
sigsetjmp(buf, 1) + siglongjmp, both inside the handler mask 0, runs again stuck blocked
setjmp + longjmp, both inside the handler mask 0, runs again stuck blocked

So my earlier finding is fixed, including the pselect and 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's sa_mask. Example: a SIGUSR1 handler does the save/restore above, then a second thread blocks SIGUSR1 and joins. After raise returns, the mask is 0, not 0x20000000. 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/setcontext inside a handler.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644371177 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9499#pullrequestreview-5644371177). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T17:35:36Z. **Review:** APPROVED --- Re-checked head f5757aab9861 merged onto main 2e1c72239b23 (clean merge). Linux, GCC 13, Release. What I ran: - `guest_signal_handler_mask`, `guest_signal_sets`, `guest_signals`, `guest_signal_interrupt` on the merge: 10/10 runs pass. - Same merge with only f5757aab's `Signals.cpp` hunks reverted: `guest_signal_handler_mask` aborts 3/3, the other three pass. - A probe, run on both trees, where a handler does something that replaces the mask and returns. Then I check the mask and raise again: | inside the handler | f5757aab | without its hunks | |---|---|---| | `SIG_BLOCK` save + `SIG_SETMASK` restore (SIGUSR2, SIGTERM) | mask 0, runs again | stuck blocked | | real `pselect_nid_postfix` with an empty mask and a zero timeout (SIGUSR2, SIGTERM) | mask 0, runs again | stuck blocked | | `sigsetjmp(buf, 1)` + `siglongjmp`, both inside the handler | mask 0, runs again | stuck blocked | | `setjmp` + `longjmp`, both inside the handler | mask 0, runs again | stuck blocked | So my earlier finding is fixed, including the `pselect` and 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_BLOCK`s 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's `sa_mask`. Example: a SIGUSR1 handler does the save/restore above, then a second thread blocks SIGUSR1 and joins. After `raise` returns, the mask is 0, not `0x20000000`. 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`/`setcontext` inside a handler.
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!9499
No description provided.