fix(libkernel): check signal dispositions before taking the pending lock in sigsuspend #26206

Open
GeremC wants to merge 1 commit from upstream-pr/26206 into main
GeremC commented 2026-11-23 13:10:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by GeremC on 2026-11-23T13:10:01Z.


What

A thread that changes a signal's disposition while another thread is in sigsuspend can deadlock both. ThreadSanitizer found the lock-order inversion in guest_signals, and the new test hangs on every run without the fix.

libkernel's signal code takes two locks in opposite orders:

  • signal_nid_postfix and sigaction_nid_postfix hold registration, then call DiscardPendingIfIgnored, which takes pending.lock when the new disposition ignores the signal.
  • WaitForDeliverableSignal (used by sigsuspend) holds pending.lock and calls TakePendingUnblocked, which calls Deliverable → CaughtByGuest/TerminatesWithoutHandler → Disposition for each unblocked signal. Disposition takes registration.

The fix computes Deliverable for every unblocked signal before WaitForDeliverableSignal takes pending.lock, and passes the result to TakePendingUnblocked. Every other path already takes the locks in the order registration, then pending.lock, or one at a time.

A disposition can now change between that check and the take. That changes nothing a guest can see:

  • A signal that becomes ignored is cleared from the pending set by DiscardPendingIfIgnored, under pending.lock, so the take finds nothing.
  • A taken signal whose handler became SIG_IGN makes DeliverPendingSignal return false, and the wait loops.
  • A signal that becomes deliverable while pending is seen on the next wake-up. Before this change it was seen at the same point, because signal() does not notify pending.changed either.

The new guest_signal_disposition_while_suspended test:

  • One thread blocks SIGUSR1 and calls sigsuspend with an empty mask 2,000 times.
  • A second thread sends it SIGUSR1 with pthread_kill each time the previous one has been handled.
  • The main thread keeps switching SIGUSR2 between SIG_IGN and SIG_DFL.
  • The test checks that all 2,000 rounds return EINTR and run the handler.

Tested

Linux Mint 22.3 (Ubuntu 24.04 base), GCC 13.3, Release, BUILD_TESTING=ON.

  • New test on main: timed out at 20 s in 5 of 5 runs. In gdb:
    • the sigsuspend thread waits on registration in WaitForDeliverableSignal;
    • the main thread waits on pending.lock in DiscardPendingIfIgnored ← signal_nid_postfix;
    • the sender spins waiting for the handler.
  • New test with this branch: 20 of 20 passes, about 0.15 s each.
  • ThreadSanitizer (-DANYPS5_ENABLE_TSAN=ON, run under setarch -R because GCC 13's libtsan aborts with unexpected memory mapping on this kernel's mmap randomization):
    • On main, guest_signals fails with lock-order-inversion (potential deadlock) between DiscardPendingIfIgnored ← signal_nid_postfix and Disposition ← Deliverable ← TakePendingUnblocked ← WaitForDeliverableSignal ← sigsuspend_nid_postfix.
    • With this branch, guest_signals and the new test pass 3 of 3 runs with no ThreadSanitizer report.
  • Signal suites: ctest -R 'signal|sig|alarm|timer|kqueue|equeue|pthread_kill': 55/55 passed.
  • All guest tests: ctest -R '^guest_': 2862/2862 passed.

python3 tools/check_conventions.py --base origin/main passes. #25999 is the only open PR that touches Signals.cpp. It changes other functions, and the two branches merge without conflict (git merge-tree).

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

https://claude.ai/code/session_014W1f1RvDMX4BmYudy9J2Vj

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5453736790 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26206). Originally posted by [GeremC](https://github.com/GeremC) on 2026-11-23T13:10:01Z. --- ### What A thread that changes a signal's disposition while another thread is in `sigsuspend` can deadlock both. ThreadSanitizer found the lock-order inversion in `guest_signals`, and the new test hangs on every run without the fix. `libkernel`'s signal code takes two locks in opposite orders: - `signal_nid_postfix` and `sigaction_nid_postfix` hold `registration`, then call `DiscardPendingIfIgnored`, which takes `pending.lock` when the new disposition ignores the signal. - `WaitForDeliverableSignal` (used by `sigsuspend`) holds `pending.lock` and calls `TakePendingUnblocked`, which calls `Deliverable` → `CaughtByGuest`/`TerminatesWithoutHandler` → `Disposition` for each unblocked signal. `Disposition` takes `registration`. The fix computes `Deliverable` for every unblocked signal before `WaitForDeliverableSignal` takes `pending.lock`, and passes the result to `TakePendingUnblocked`. Every other path already takes the locks in the order `registration`, then `pending.lock`, or one at a time. A disposition can now change between that check and the take. That changes nothing a guest can see: - A signal that becomes ignored is cleared from the pending set by `DiscardPendingIfIgnored`, under `pending.lock`, so the take finds nothing. - A taken signal whose handler became `SIG_IGN` makes `DeliverPendingSignal` return false, and the wait loops. - A signal that becomes deliverable while pending is seen on the next wake-up. Before this change it was seen at the same point, because `signal()` does not notify `pending.changed` either. The new `guest_signal_disposition_while_suspended` test: - One thread blocks `SIGUSR1` and calls `sigsuspend` with an empty mask 2,000 times. - A second thread sends it `SIGUSR1` with `pthread_kill` each time the previous one has been handled. - The main thread keeps switching `SIGUSR2` between `SIG_IGN` and `SIG_DFL`. - The test checks that all 2,000 rounds return `EINTR` and run the handler. ### Tested Linux Mint 22.3 (Ubuntu 24.04 base), GCC 13.3, Release, `BUILD_TESTING=ON`. - **New test on `main`:** timed out at 20 s in 5 of 5 runs. In gdb: - the `sigsuspend` thread waits on `registration` in `WaitForDeliverableSignal`; - the main thread waits on `pending.lock` in `DiscardPendingIfIgnored` ← `signal_nid_postfix`; - the sender spins waiting for the handler. - **New test with this branch:** 20 of 20 passes, about 0.15 s each. - **ThreadSanitizer** (`-DANYPS5_ENABLE_TSAN=ON`, run under `setarch -R` because GCC 13's libtsan aborts with `unexpected memory mapping` on this kernel's mmap randomization): - On `main`, `guest_signals` fails with `lock-order-inversion (potential deadlock)` between `DiscardPendingIfIgnored` ← `signal_nid_postfix` and `Disposition` ← `Deliverable` ← `TakePendingUnblocked` ← `WaitForDeliverableSignal` ← `sigsuspend_nid_postfix`. - With this branch, `guest_signals` and the new test pass 3 of 3 runs with no ThreadSanitizer report. - **Signal suites:** `ctest -R 'signal|sig|alarm|timer|kqueue|equeue|pthread_kill'`: 55/55 passed. - **All guest tests:** `ctest -R '^guest_'`: 2862/2862 passed. `python3 tools/check_conventions.py --base origin/main` passes. #25999 is the only open PR that touches `Signals.cpp`. It changes other functions, and the two branches merge without conflict (`git merge-tree`). ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_014W1f1RvDMX4BmYudy9J2Vj
PrivacyOnion commented 2026-11-23 13:14:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T13:14:48Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 7bb7e2d18f:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_signal_disposition_while_suspended_tests built cleanly.
  • CTest (guest_signal_disposition_while_suspended):
    1/1 Test #6047: guest_signal_disposition_while_suspended ...   Passed    0.29 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/System/src/Signals.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #6047: guest_signal_disposition_while_suspended ...***Timeout  20.11 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified that std::array<bool, MaxSignal + 1> deliverable bounds (1 to MaxSignal = 128) are safe and do not cause buffer overruns.
    • Verified that computing Deliverable(candidate) outside pending.lock completely resolves the lock inversion between registration mutex and pending.lock.
    • Verified that TakePendingUnblocked respects both precomputed deliverability and per-thread SignalBlocked(candidate) status under pending.lock.
    • Verified that guest_signal_disposition_while_suspended_tests exercises concurrent sigsuspend and signal disposition changes without deadlocks or races.
    • Verified full AGENTS.md compliance, including absence of forbidden code comments and standard test target setup in CMakeLists.txt.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6022316074 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26206#pullrequestreview-6022316074). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-23T13:14:48Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `7bb7e2d18f`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_signal_disposition_while_suspended_tests` built cleanly. - **CTest** (`guest_signal_disposition_while_suspended`): ``` 1/1 Test #6047: guest_signal_disposition_while_suspended ... Passed 0.29 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/System/src/Signals.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #6047: guest_signal_disposition_while_suspended ...***Timeout 20.11 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified that std::array<bool, MaxSignal + 1> deliverable bounds (1 to MaxSignal = 128) are safe and do not cause buffer overruns. - Verified that computing Deliverable(candidate) outside pending.lock completely resolves the lock inversion between registration mutex and pending.lock. - Verified that TakePendingUnblocked respects both precomputed deliverability and per-thread SignalBlocked(candidate) status under pending.lock. - Verified that guest_signal_disposition_while_suspended_tests exercises concurrent sigsuspend and signal disposition changes without deadlocks or races. - Verified full AGENTS.md compliance, including absence of forbidden code comments and standard test target setup in CMakeLists.txt. Looks Good To Me
MarcZX10 commented 2026-11-25 10:23:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T10:23:52Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head 7bb7e2d18fa8e721ea502d8d328735915b5fdffb from the prepared diff and targeted source reads. No build or tests were run.

Results

Check Result Note
Diff and changed source passed Three changed files read
Build and tests not run Results in the description were author reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests or build were run. The author reports Linux Mint 22.3/GCC 13.3 results, including 2,862 guest tests, but these were not independently verified. The reviewed files were core/libs/prx/libkernel/CMakeLists.txt, core/libs/prx/libkernel/System/src/Signals.cpp, and core/libs/tests/GuestSignalDispositionWhileSuspended.cpp; all changed files listed in the manifest were read. No files were excluded in the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6055779693 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26206#pullrequestreview-6055779693). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T10:23:52Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `7bb7e2d18fa8e721ea502d8d328735915b5fdffb` from the prepared diff and targeted source reads. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source | passed | Three changed files read | | Build and tests | not run | Results in the description were author reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests or build were run. The author reports Linux Mint 22.3/GCC 13.3 results, including 2,862 guest tests, but these were not independently verified. The reviewed files were `core/libs/prx/libkernel/CMakeLists.txt`, `core/libs/prx/libkernel/System/src/Signals.cpp`, and `core/libs/tests/GuestSignalDispositionWhileSuspended.cpp`; all changed files listed in the manifest were read. No files were excluded in the manifest. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 10:38:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T10:38:16Z.

Review: COMMENTED


The GuestSignalDispositionWhileSuspended fixture coverage passed on Linux with the exact implementation from this head. The fixture exercises the disposition change while the target thread is suspended.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6168112227 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26206#pullrequestreview-6168112227). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T10:38:16Z. **Review:** COMMENTED --- The GuestSignalDispositionWhileSuspended fixture coverage passed on Linux with the exact implementation from this head. The fixture exercises the disposition change while the target thread is suspended. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/26206:upstream-pr/26206'
git switch 'upstream-pr/26206'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/26206'
git switch 'upstream-pr/26206'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/26206'
git switch 'upstream-pr/26206'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/26206'
git switch 'main'
git merge --squash 'upstream-pr/26206'
git switch 'main'
git merge --ff-only 'upstream-pr/26206'
git switch 'main'
git merge 'upstream-pr/26206'
git push origin 'main'
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!26206
No description provided.