feat(libkernel): throw for a null pthread_condattr_getclock output #12925

Merged
KernelPanicNT merged 2 commits from upstream-pr/12925 into main 2026-11-02 07:55:04 +00:00
KernelPanicNT commented 2026-10-29 14:38:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by KernelPanicNT on 2026-10-29T14:38:14Z.


What

pthread_condattr_getclock no longer reaches NotImplemented_nid_no_patch: a null output pointer with a valid attribute now throws std::invalid_argument, because the native call faults there. scePthreadCondattrGetclock threw std::runtime_error in the same case and now throws std::invalid_argument too, so both forms report a native fault the same way. Every other path of pthread_condattr_getclock, pthread_condattr_setclock, scePthreadCondattrGetclock and scePthreadCondattrSetclock already matched the measurement and is unchanged. The test now checks every invalid clock ID from -1 to 16 for both setters. The two debt entries now list only what is still open.

Source of the behaviour

Measured with a homebrew probe running as a payload process on a device. The probe resolved the system pthread_condattr_* and scePthreadCondattr* symbols and used only its own attribute objects, with a fault handler around each call. It ran three sequences: POSIX init/set/get, SCE init/set/get, and SCE init/set with the POSIX getter.

Case Device AnyPS5 before AnyPS5 now
Default clock after init 0 0 0
setclock 0, 1, 2, 4 0, then getclock reports the ID same same
setclock -1, 3, 5 to 16 EINVAL (POSIX 22, SCE 0x80020016); stored clock unchanged same same
setclock with a null attribute or a null handle EINVAL same same
getclock with a null attribute, a null handle or a destroyed attribute EINVAL; output not written same same
errno after any call above unchanged (0) unchanged unchanged
SCE init and SCE setter, read by pthread_condattr_getclock the getter reports the stored clock and returns POSIX EINVAL (22) for a null attribute, a null handle or a destroyed attribute, without writing the output same same
destroy 0; handle set to null same same
pthread_condattr_getclock(valid, nullptr) SIGSEGV NotImplemented_nid_no_patch std::invalid_argument
scePthreadCondattrGetclock(valid, nullptr) SIGSEGV std::runtime_error std::invalid_argument

Not measured: a null or destroyed attribute together with a null output. It still returns EINVAL, because FreeBSD 11.4 libthr checks the attribute first.

Tested

Linux (Ubuntu, GCC). guest_pthread_condattr and guest_kernel_sys pass. Both expect std::invalid_argument for a null output with a valid attribute, and guest_pthread_condattr also checks that every invalid clock ID from -1 to 16 is refused by both setters and leaves the stored clock unchanged. With Posix/Cond.cpp and libkernel_sys/Export.cpp from main, both tests abort: guest_pthread_condattr on the std::runtime_error from NotImplemented_nid_no_patch, and guest_kernel_sys on the std::runtime_error from scePthreadCondattrGetclock. tools/progress.py: +1 implemented function (pthread_condattr_getclock, libkernel 700/745 -> 701/745). 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5070014819 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12925). Originally posted by [KernelPanicNT](https://github.com/KernelPanicNT) on 2026-10-29T14:38:14Z. --- ### What `pthread_condattr_getclock` no longer reaches `NotImplemented_nid_no_patch`: a null output pointer with a valid attribute now throws `std::invalid_argument`, because the native call faults there. `scePthreadCondattrGetclock` threw `std::runtime_error` in the same case and now throws `std::invalid_argument` too, so both forms report a native fault the same way. Every other path of `pthread_condattr_getclock`, `pthread_condattr_setclock`, `scePthreadCondattrGetclock` and `scePthreadCondattrSetclock` already matched the measurement and is unchanged. The test now checks every invalid clock ID from -1 to 16 for both setters. The two debt entries now list only what is still open. **Source of the behaviour** Measured with a homebrew probe running as a payload process on a device. The probe resolved the system `pthread_condattr_*` and `scePthreadCondattr*` symbols and used only its own attribute objects, with a fault handler around each call. It ran three sequences: POSIX init/set/get, SCE init/set/get, and SCE init/set with the POSIX getter. | Case | Device | AnyPS5 before | AnyPS5 now | |---|---|---|---| | Default clock after init | 0 | 0 | 0 | | `setclock` 0, 1, 2, 4 | 0, then `getclock` reports the ID | same | same | | `setclock` -1, 3, 5 to 16 | `EINVAL` (POSIX 22, SCE `0x80020016`); stored clock unchanged | same | same | | `setclock` with a null attribute or a null handle | `EINVAL` | same | same | | `getclock` with a null attribute, a null handle or a destroyed attribute | `EINVAL`; output not written | same | same | | errno after any call above | unchanged (0) | unchanged | unchanged | | SCE init and SCE setter, read by `pthread_condattr_getclock` | the getter reports the stored clock and returns POSIX `EINVAL` (22) for a null attribute, a null handle or a destroyed attribute, without writing the output | same | same | | `destroy` | 0; handle set to null | same | same | | `pthread_condattr_getclock(valid, nullptr)` | SIGSEGV | `NotImplemented_nid_no_patch` | `std::invalid_argument` | | `scePthreadCondattrGetclock(valid, nullptr)` | SIGSEGV | `std::runtime_error` | `std::invalid_argument` | Not measured: a null or destroyed attribute together with a null output. It still returns `EINVAL`, because [FreeBSD 11.4 libthr](https://github.com/freebsd/freebsd-src/blob/d106002967/lib/libthr/thread/thr_condattr.c#L82-L90) checks the attribute first. - Previous behaviour: [`Posix/Cond.cpp#L75-L80`](https://github.com/boykopovar/AnyPS5/blob/bb04a4f1ea/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp#L75-L80) (`pthread_condattr_getclock`), [`libkernel_sys/Export.cpp#L1122-L1127`](https://github.com/boykopovar/AnyPS5/blob/bb04a4f1ea/core/libs/prx/libkernel_sys/Export.cpp#L1122-L1127) (`scePthreadCondattrGetclock`), and the setter it already matched: [`Pthread/src/Cond.cpp#L207-L213`](https://github.com/boykopovar/AnyPS5/blob/bb04a4f1ea/core/libs/prx/libkernel/Pthread/src/Cond.cpp#L207-L213). - Debt entries this cuts: [`libkernel.md#L44`](https://github.com/boykopovar/AnyPS5/blob/bb04a4f1ea/docs/dev/debt/libkernel.md#L44), [`libkernel_sys.md#L11`](https://github.com/boykopovar/AnyPS5/blob/bb04a4f1ea/docs/dev/debt/libkernel_sys.md#L11). - Precedent for throwing `std::invalid_argument` where the native code faults: [`libc.md#L38`](https://github.com/boykopovar/AnyPS5/blob/bb04a4f1ea/docs/dev/debt/libc.md#L38) (`feholdexcept`). - Console measurement as a source: [`REVERSE_ENGINEERING.md`](https://github.com/boykopovar/AnyPS5/blob/bb04a4f1ea/docs/dev/REVERSE_ENGINEERING.md). ### 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. --> Linux (Ubuntu, GCC). `guest_pthread_condattr` and `guest_kernel_sys` pass. Both expect `std::invalid_argument` for a null output with a valid attribute, and `guest_pthread_condattr` also checks that every invalid clock ID from -1 to 16 is refused by both setters and leaves the stored clock unchanged. With `Posix/Cond.cpp` and `libkernel_sys/Export.cpp` from `main`, both tests abort: `guest_pthread_condattr` on the `std::runtime_error` from `NotImplemented_nid_no_patch`, and `guest_kernel_sys` on the `std::runtime_error` from `scePthreadCondattrGetclock`. `tools/progress.py`: +1 implemented function (`pthread_condattr_getclock`, libkernel 700/745 -> 701/745). `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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-29 17:24:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T17:24:08Z.

Review: APPROVED


Reviewed head 602fc9c55a against main bb04a4f1ea. I approve it.

What I checked (macOS arm64, Apple clang, host only)

  • git merge-tree against bb04a4f1ea merges cleanly. git diff --check is clean. tools/check_conventions.py --base upstream/main passes, and it also passes with this PR's title and body.
  • The citation d106002967 is the same tree as releng/11.4 (gh api compare reports identical). Lines 82-90 of thr_condattr.c check the attribute before writing through clock_id. That is the order both getters keep for the unmeasured case of a null attribute with a null output.
  • I built a standalone harness under ASan and UBSan: the two getters and scePthreadCondattrSetclock verbatim from this head, plus the PR's rows (null/unset/destroyed attribute with and without an output, valid attribute with a null output for both forms, setclock 0/1/2/4 and -1/3/5..16 for both setters). It passes.
  • Mutants, judged against those rows:
Mutant Result
POSIX getter keeps main's NotImplemented_nid_no_patch killed
SCE getter keeps main's std::runtime_error killed
setclock accepts 3, 6..11 or 16 killed (6..11 and 16 only by the new loop values)
null-output check moved before the attribute check (POSIX / SCE) killed / killed
setclock accepts a value outside -1..16 (e.g. INT_MAX) survives, but that range wasn't measured, so I'm not asking for a row

Optional, not blocking

  • Both debt entries say "a payload process measured on a device". Most neighbouring entries name the firmware, e.g. "PS5 (firmware 13.60)". Adding it would make the measurement easier to reproduce.
  • The old libkernel.md entry cited the payload SDK's pthread.h for the signature. The new entry drops that link, and the signature's source is still useful. Consider keeping it.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5702089274 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12925#pullrequestreview-5702089274). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T17:24:08Z. **Review:** APPROVED --- Reviewed head `602fc9c55a` against `main` `bb04a4f1ea`. I approve it. **What I checked (macOS arm64, Apple clang, host only)** - `git merge-tree` against `bb04a4f1ea` merges cleanly. `git diff --check` is clean. `tools/check_conventions.py --base upstream/main` passes, and it also passes with this PR's title and body. - The citation `d106002967` is the same tree as `releng/11.4` (`gh api compare` reports `identical`). Lines 82-90 of `thr_condattr.c` check the attribute before writing through `clock_id`. That is the order both getters keep for the unmeasured case of a null attribute with a null output. - I built a standalone harness under ASan and UBSan: the two getters and `scePthreadCondattrSetclock` verbatim from this head, plus the PR's rows (null/unset/destroyed attribute with and without an output, valid attribute with a null output for both forms, setclock 0/1/2/4 and -1/3/5..16 for both setters). It passes. - Mutants, judged against those rows: | Mutant | Result | |---|---| | POSIX getter keeps `main`'s `NotImplemented_nid_no_patch` | killed | | SCE getter keeps `main`'s `std::runtime_error` | killed | | setclock accepts 3, 6..11 or 16 | killed (6..11 and 16 only by the new loop values) | | null-output check moved before the attribute check (POSIX / SCE) | killed / killed | | setclock accepts a value outside -1..16 (e.g. `INT_MAX`) | survives, but that range wasn't measured, so I'm not asking for a row | **Optional, not blocking** - Both debt entries say "a payload process measured on a device". Most neighbouring entries name the firmware, e.g. "PS5 (firmware 13.60)". Adding it would make the measurement easier to reproduce. - The old `libkernel.md` entry cited the payload SDK's `pthread.h` for the signature. The new entry drops that link, and the signature's source is still useful. Consider keeping it. AI-assisted: yes
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!12925
No description provided.