fix(libkernel): fail scePthreadCondSignalto for a null or non-waiting thread as measured on a PS5 #11263

Merged
orezraey merged 2 commits from upstream-pr/11263 into main 2026-10-27 02:01:45 +00:00
orezraey commented 2026-10-25 22:58:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-25T22:58:58Z.


What

scePthreadCondSignalto and pthread_cond_signalto_np now check the thread they are given, as a retail PS5 (firmware 13.60) does. A null thread returns EINVAL. A thread that is not waiting on that condition variable returns EPERM (1 for the POSIX form). Before, the thread was ignored and every call returned 0. Measured with a homebrew probe from a payload process; the source and console log are in this gist (pcondto).

On the console, through both the sce and the POSIX form:

  • Two waiters, signalto(A). Returns 0. A wakes after about 110 ms with 0. B is not woken and times out at 600 ms.
  • Target not waiting. signalto(self) returns 0x80020001 (POSIX 1), with or without another waiter, and the other waiter still times out.
  • Bad handles. A null thread gives 0x80020016 (22), and 0x12345 gives 0x80020003 (3).

How this works here:

  • Each condition variable's waiting threads are recorded while they wait: added before the wait, removed once it returns, under one small mutex. signalto looks the target up there.
  • The wake itself still notifies every waiter, so the others see a spurious wakeup, which POSIX allows. Waking one chosen thread would need a separate wait per waiter, which every condition-variable wait shares; that stays a recorded gap.
  • A handle that is not a thread returns EPERM here instead of ESRCH, since handles are pointers and the record is only searched, never dereferenced.

A code review noted the cost: every condition-variable wait now locks that mutex twice and allocates one map node. That is small next to the wait itself, and no performance claim is made.

Before and after

Case PS5 AnyPS5 before AnyPS5 now
signalto(A) with A and B waiting 0, only A wakes 0, A and B wake 0, A wakes, B sees a spurious wakeup
signalto a thread that is not waiting 0x80020001 (POSIX 1) 0 0x80020001 (POSIX 1)
null thread 0x80020016 (22) 0 0x80020016 (22)
0x12345 as the thread 0x80020003 (3) 0 0x80020001 (1)

Tested

Windows 11, WinLibs GCC 15.2, full build. The 88 tests matching cond|pthread|thread|sema|mutex pass 3 times out of 3, with select_tests excluded: it is a Python tool test that fails here for an unrelated temporary-directory reason. guest_cond_timedwait now checks that signalto(self) with nobody waiting returns 1 (POSIX) and 0x80020001 (sce), and a null thread returns 22 and 0x80020016. Its existing targeted signal to a waiting thread still returns 0 and wakes it. tools/check_conventions.py passes.

Those runs were at head 9797a214b0 on main 49d1f9b07e.

Head d06b45fba5 adds SignalToEveryRound, which repeats the targeted signal 200 times with a normal mutex and 200 times with a recursive one locked twice: the main thread waits with a 2 s pthread_cond_reltimedwait_np, and a helper locks the mutex, sets ready and calls signalto(main), which must return 0, and every wait must return 0 before its timeout. With NotifyAll removed from scePthreadCondSignalto the test fails (timeout), so it catches a lost wakeup. guest_cond_timedwait and guest_cond_recursive_mutex pass 3 times out of 3 on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL), and tools/check_conventions.py passes.

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: #10725 (dwk001), which edits the debt line next to this PR's signalto entry; merge order only
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5009560923 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11263). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-25T22:58:58Z. --- ### What `scePthreadCondSignalto` and `pthread_cond_signalto_np` now check the thread they are given, as a retail PS5 (firmware 13.60) does. A null thread returns `EINVAL`. A thread that is not waiting on that condition variable returns `EPERM` (1 for the POSIX form). Before, the thread was ignored and every call returned 0. Measured with a homebrew probe from a payload process; the source and console log are in [this gist](https://gist.github.com/orezraey/c423a99dcf8b8775bf116eba2d41fca1) (`pcondto`). On the console, through both the sce and the POSIX form: - **Two waiters, `signalto(A)`.** Returns 0. A wakes after about 110 ms with 0. B is not woken and times out at 600 ms. - **Target not waiting.** `signalto(self)` returns `0x80020001` (POSIX 1), with or without another waiter, and the other waiter still times out. - **Bad handles.** A null thread gives `0x80020016` (22), and `0x12345` gives `0x80020003` (3). How this works here: - Each condition variable's waiting threads are recorded while they wait: added before the wait, removed once it returns, under one small mutex. `signalto` looks the target up there. - The wake itself still notifies every waiter, so the others see a spurious wakeup, which POSIX allows. Waking one chosen thread would need a separate wait per waiter, which every condition-variable wait shares; that stays a recorded gap. - A handle that is not a thread returns `EPERM` here instead of `ESRCH`, since handles are pointers and the record is only searched, never dereferenced. A code review noted the cost: every condition-variable wait now locks that mutex twice and allocates one map node. That is small next to the wait itself, and no performance claim is made. **Before and after** | Case | PS5 | AnyPS5 before | AnyPS5 now | |---|---|---|---| | `signalto(A)` with A and B waiting | 0, only A wakes | 0, A and B wake | 0, A wakes, B sees a spurious wakeup | | `signalto` a thread that is not waiting | `0x80020001` (POSIX 1) | 0 | `0x80020001` (POSIX 1) | | null thread | `0x80020016` (22) | 0 | `0x80020016` (22) | | `0x12345` as the thread | `0x80020003` (3) | 0 | `0x80020001` (1) | ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, WinLibs GCC 15.2, full build. The 88 tests matching `cond|pthread|thread|sema|mutex` pass 3 times out of 3, with `select_tests` excluded: it is a Python tool test that fails here for an unrelated temporary-directory reason. `guest_cond_timedwait` now checks that `signalto(self)` with nobody waiting returns 1 (POSIX) and `0x80020001` (sce), and a null thread returns 22 and `0x80020016`. Its existing targeted signal to a waiting thread still returns 0 and wakes it. `tools/check_conventions.py` passes. Those runs were at head `9797a214b0` on `main` `49d1f9b07e`. Head `d06b45fba5` adds `SignalToEveryRound`, which repeats the targeted signal 200 times with a normal mutex and 200 times with a recursive one locked twice: the main thread waits with a 2 s `pthread_cond_reltimedwait_np`, and a helper locks the mutex, sets `ready` and calls `signalto(main)`, which must return 0, and every wait must return 0 before its timeout. With `NotifyAll` removed from `scePthreadCondSignalto` the test fails (timeout), so it catches a lost wakeup. `guest_cond_timedwait` and `guest_cond_recursive_mutex` pass 3 times out of 3 on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL), and `tools/check_conventions.py` passes. ### 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: #10725 (dwk001), which edits the debt line next to this PR's `signalto` entry; merge order only - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 23:02:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:02:35Z.

Review: CHANGES_REQUESTED


The console evidence and return-code mapping are useful, but I found a blocking lost-wakeup race in the waiter registry. I posted the detailed finding inline on the registration path; please synchronize the registry state with the transition into the actual condition wait and cover the race with a regression before this is ready to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645794411 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11263#pullrequestreview-5645794411). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:02:35Z. **Review:** CHANGES_REQUESTED --- The console evidence and return-code mapping are useful, but I found a blocking lost-wakeup race in the waiter registry. I posted the detailed finding inline on the registration path; please synchronize the registry state with the transition into the actual condition wait and cover the race with a regression before this is ready to merge.
MarcZX10 commented 2026-10-25 23:46:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:46:15Z.

Review: APPROVED


Re-reviewed exact head d06b45fba5eae8a325cc8b94facc7a625762283d and the author’s explanation. I withdraw my earlier lost-wakeup blocker: when the signaler follows the condition-variable mutex protocol, that mutex prevents it from running in the registration-to-block interval; signaling without the mutex has the same race as ordinary condition signaling. The focused stress tests and the documented limitation are consistent with that model. The overlap with #10725 is limited to the debt document; I left a coordination comment because the current checklist says Depends on: none, but I do not consider that a code blocker. Hosted policy checks pass and product jobs are skipped; the Windows/Linux focused results are author-reported. No blocking correctness finding remains.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645981205 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11263#pullrequestreview-5645981205). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:46:15Z. **Review:** APPROVED --- Re-reviewed exact head `d06b45fba5eae8a325cc8b94facc7a625762283d` and the author’s explanation. I withdraw my earlier lost-wakeup blocker: when the signaler follows the condition-variable mutex protocol, that mutex prevents it from running in the registration-to-block interval; signaling without the mutex has the same race as ordinary condition signaling. The focused stress tests and the documented limitation are consistent with that model. The overlap with #10725 is limited to the debt document; I left a coordination comment because the current checklist says `Depends on: none`, but I do not consider that a code blocker. Hosted policy checks pass and product jobs are skipped; the Windows/Linux focused results are author-reported. No blocking correctness finding remains.
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!11263
No description provided.