fix(libkernel): return EINVAL for the semaphore arguments a PS5 rejects #11195

Merged
orezraey merged 3 commits from upstream-pr/11195 into main 2026-10-26 22:02:55 +00:00
orezraey commented 2026-10-25 21:36:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-25T21:36:22Z.


What

sceKernelCreateSema and sceKernelSignalSema now return EINVAL for the arguments a retail PS5 (firmware 13.60) rejects, where they used to throw. Measured with homebrew probes from a payload process; the sources and console logs are in this gist (psema2, psema3).

On the console:

  • Attributes. sceKernelCreateSema accepts only attributes 0, 1, 2, 0x100, 0x101 and 0x102. Every other value tried returns 0x80020016 (EINVAL): 3 to 0xff, 0x103 to 0x110, 0x200, 0x201, 0x300, 0x320, 0x1000, 0x10000, 0x80000000 and 0xffffffff.
  • Maximum count. -1 and 0 return EINVAL, and so do an initial count above the maximum and an initial count of -1.
  • Signal count. sceKernelSignalSema with -1 or 0 returns EINVAL, and so does sceKernelPollSema with -1 or 0.
  • Null output. A null output pointer faults.

AnyPS5 threw for any attribute above 2, a negative maximum and a negative signal count, and the debt entry said they were not measured.

  • Those cases now return EINVAL. The attribute rule is (attr & ~0x103) != 0 or (attr & 3) == 3.
  • Attributes 0x100 to 0x102 still throw: the console accepts them, but what bit 0x100 changes is not known, so modelling them would be a guess. The debt entry now says so first.
  • A null output pointer still throws where the console faults.

Bogus non-null handles (handle + 1, 0x12345) return ESRCH on the console. Here the handles are host pointers. #10558 adds a handle table for that case, so this PR does not touch it.

Before and after

Case PS5 AnyPS5 before AnyPS5 now
sceKernelCreateSema attribute 3 to 0xff, 0x103 and above, 0xffffffff EINVAL throws EINVAL
attribute 0x100 to 0x102 accepted throws throws (debt)
maximum count -1 EINVAL throws EINVAL
sceKernelSignalSema count -1 EINVAL throws EINVAL
null output pointer faults throws throws

Tested

At b7e5eb7f31: Windows 11, WinLibs GCC 15.2, full build. The four tests matching sema|kernel_errors pass 3 times out of 3.

  • guest_semaphore checks EINVAL for every attribute from 3 to 0x320 except 0x100 to 0x102, and for 0x1000, 0x10000, 0x80000000 and 0xffffffff.
  • It checks that a maximum of -1 is EINVAL and a signal of -1 is EINVAL. Attributes 0x100 to 0x102 still throw in the code, but no test asserts it, so implementing them later does not break a test.

tools/check_conventions.py passes. At head b7b0f58eb0 (merge base main 90f918fa71), guest_semaphore and guest_semaphore_trace pass 3 times out of 3 on Windows 11 (WinLibs GCC 15.2) and on Linux (Arch Linux on WSL2, GCC 16.2.1).

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: #10558 (it edits the same semaphore debt entry), #9491 (it edits sceKernelSignalSema) and #10789 (it edits the debt entry next to it); merge order only
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5008950914 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11195). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-25T21:36:22Z. --- ### What `sceKernelCreateSema` and `sceKernelSignalSema` now return `EINVAL` for the arguments a retail PS5 (firmware 13.60) rejects, where they used to throw. Measured with homebrew probes from a payload process; the sources and console logs are in [this gist](https://gist.github.com/orezraey/5eca7020f92d8444b26fe6f6b94762ea) (`psema2`, `psema3`). On the console: - **Attributes.** `sceKernelCreateSema` accepts only attributes 0, 1, 2, 0x100, 0x101 and 0x102. Every other value tried returns `0x80020016` (EINVAL): 3 to 0xff, 0x103 to 0x110, 0x200, 0x201, 0x300, 0x320, 0x1000, 0x10000, `0x80000000` and `0xffffffff`. - **Maximum count.** -1 and 0 return EINVAL, and so do an initial count above the maximum and an initial count of -1. - **Signal count.** `sceKernelSignalSema` with -1 or 0 returns EINVAL, and so does `sceKernelPollSema` with -1 or 0. - **Null output.** A null output pointer faults. AnyPS5 threw for any attribute above 2, a negative maximum and a negative signal count, and the debt entry said they were not measured. - Those cases now return EINVAL. The attribute rule is (attr & ~0x103) != 0 or (attr & 3) == 3. - Attributes 0x100 to 0x102 still throw: the console accepts them, but what bit 0x100 changes is not known, so modelling them would be a guess. The debt entry now says so first. - A null output pointer still throws where the console faults. Bogus non-null handles (handle + 1, `0x12345`) return ESRCH on the console. Here the handles are host pointers. #10558 adds a handle table for that case, so this PR does not touch it. **Before and after** | Case | PS5 | AnyPS5 before | AnyPS5 now | |---|---|---|---| | `sceKernelCreateSema` attribute 3 to 0xff, 0x103 and above, `0xffffffff` | `EINVAL` | throws | `EINVAL` | | attribute 0x100 to 0x102 | accepted | throws | throws (debt) | | maximum count -1 | `EINVAL` | throws | `EINVAL` | | `sceKernelSignalSema` count -1 | `EINVAL` | throws | `EINVAL` | | null output pointer | faults | throws | throws | ### 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. --> At `b7e5eb7f31`: Windows 11, WinLibs GCC 15.2, full build. The four tests matching `sema|kernel_errors` pass 3 times out of 3. - `guest_semaphore` checks EINVAL for every attribute from 3 to 0x320 except 0x100 to 0x102, and for 0x1000, 0x10000, `0x80000000` and `0xffffffff`. - It checks that a maximum of -1 is EINVAL and a signal of -1 is EINVAL. Attributes 0x100 to 0x102 still throw in the code, but no test asserts it, so implementing them later does not break a test. `tools/check_conventions.py` passes. At head `b7b0f58eb0` (merge base `main` `90f918fa71`), `guest_semaphore` and `guest_semaphore_trace` pass 3 times out of 3 on Windows 11 (WinLibs GCC 15.2) and on Linux (Arch Linux on WSL2, GCC 16.2.1). ### 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: #10558 (it edits the same semaphore debt entry), #9491 (it edits `sceKernelSignalSema`) and #10789 (it edits the debt entry next to it); merge order only - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 21:53:35 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed head b7e5eb7f3156339366707d58295394bbe715cb9c. I independently checked the linked firmware 13.60 logs: they show EINVAL for negative/zero maximum, negative initial count, negative/zero signal and poll counts, and the tested invalid attributes; the 0x100–0x102 attributes are accepted but their behavior is deliberately left unimplemented and documented. The changed guards and regression cases match that evidence. The four Windows tests are author-reported; hosted product builds are skipped. Local git diff --check passes, and check_conventions.py reports no errors but does emit a non-fatal 1184-character debt-entry warning. The declared merge-order dependencies on #10558 and #9491 remain open.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645516788 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11195#pullrequestreview-5645516788). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T21:53:35Z. **Review:** APPROVED --- Reviewed head `b7e5eb7f3156339366707d58295394bbe715cb9c`. I independently checked the linked firmware 13.60 logs: they show `EINVAL` for negative/zero maximum, negative initial count, negative/zero signal and poll counts, and the tested invalid attributes; the 0x100–0x102 attributes are accepted but their behavior is deliberately left unimplemented and documented. The changed guards and regression cases match that evidence. The four Windows tests are author-reported; hosted product builds are skipped. Local `git diff --check` passes, and `check_conventions.py` reports no errors but does emit a non-fatal 1184-character debt-entry warning. The declared merge-order dependencies on #10558 and #9491 remain open.
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!11195
No description provided.