fix(libkernel): return ESRCH for a null semaphore handle #9005

Merged
JJenkx merged 2 commits from upstream-pr/9005 into main 2026-10-23 22:58:21 +00:00
JJenkx commented 2026-10-21 09:25:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-21T09:25:38Z.


What

sceKernelWaitSema, sceKernelPollSema, sceKernelSignalSema, sceKernelCancelSema and sceKernelDeleteSema threw invalid argument for a null handle. They now return SCE_KERNEL_ERROR_ESRCH (0x80020003), as shadPS4 does for a handle it has not allocated, and as EventFlag.cpp already does for a null event flag. A non-positive count, a negative signal count and the other argument checks are unchanged.

Earthion (PPSA28061) depends on this. Its init function (0x44xx in eboot.bin) starts a worker thread (entry 0x48d0) before it calls sceKernelCreateSema(&sem /* 0x1fc0a8 */, ..., 1, 0, 1, NULL) at 0x4506. The worker loop at 0x4a06 calls sceKernelWaitSema(sem, 1, NULL), prints an error on any non-zero result and loops back. When the worker gets there first, it passes a null handle and expects an error it can retry. On main, 1 of 14 clean starts aborted about 2 s in with sceKernelWaitSema: invalid argument. The abort never occurred under GDB, which slows the start.

Recorded in docs/dev/debt/libkernel.md: the code is from the PS4 reference and unverified on PS5. Handles are pointers, so a non-null handle that is not a live semaphore is still not detected.

Tested

Linux 7.2.9 (Arch), Ryzen 9 7950X, GCC, Release.

  • guest_semaphore gains null-handle checks for all five calls, including that a timed wait leaves its timeout untouched and a cancel leaves its waiter count untouched. With main's Semaphore.cpp restored and only the test kept, it aborts with sceKernelWaitSema: invalid argument. With this PR it passes. ctest -R 'guest_semaphore|semaphore|guest_kernel_errors|guest_umtx': 5/5.

  • Earthion 01.006.000 on this PR (2fc26b970f, Radeon RX 6650 XT, RADV Mesa 26.2.4): a 180 s run through the in-game menu and the intro cinematic, and 8 starts of 12 s each, with no stop. On main, the 1-in-14 abort above. The race is intermittent, so these runs cannot show the abort is gone. They do show the title still starts and plays with the change. Bring-up report: #9004.

  • Independent check by @clairtonmichel (comment): clean build on Fedora with a Ryzen 7 9800X3D and Mesa 26.2.5. The kernel synchronization, error and semaphore tests, including guest_semaphore and guest_semaphore_trace, pass 33/33. No title run or negative control there.

  • Independent check by @xXPBSXx (comment): at 2fc26b970f on WSL2 Ubuntu 24.04 (a virtualised kernel) with GCC 13.3.0, guest_semaphore passes 3/3. With Semaphore.cpp restored to the merge base and the new test kept, it aborts. No title run, macOS or Windows there.

  • Independent check by @valenciana-nc (comment): on Windows 11 with WinLibs GCC 15.2.0, against main a6dcea9279. With only this PR's GuestSemaphore.cpp regression added, guest_semaphore aborts with sceKernelWaitSema: invalid argument. With 2fc26b970f cherry-picked, the semaphore and kernel tests pass 5/5 and guest_semaphore passes 25 runs in a row. Their full-suite failures, the three agc_driver_sample_order* tests, match their control run on unchanged main.

tools/check_conventions.py --base origin/main passes. The PR overlap tool reports only adjacent lines in docs/dev/debt/libkernel.md, and #6044's test reorganisation, which moves GuestSemaphore.cpp.

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: PPSA28061 #9004
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4949352862 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9005). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-21T09:25:38Z. --- ### What `sceKernelWaitSema`, `sceKernelPollSema`, `sceKernelSignalSema`, `sceKernelCancelSema` and `sceKernelDeleteSema` threw `invalid argument` for a null handle. They now return `SCE_KERNEL_ERROR_ESRCH` (`0x80020003`), as [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/60c42b925c3d1e00008621b8587819999260ec31/src/core/libraries/kernel/threads/semaphore.cpp#L329-L366) does for a handle it has not allocated, and as `EventFlag.cpp` already does for a null event flag. A non-positive count, a negative signal count and the other argument checks are unchanged. Earthion (PPSA28061) depends on this. Its init function (`0x44xx` in `eboot.bin`) starts a worker thread (entry `0x48d0`) before it calls `sceKernelCreateSema(&sem /* 0x1fc0a8 */, ..., 1, 0, 1, NULL)` at `0x4506`. The worker loop at `0x4a06` calls `sceKernelWaitSema(sem, 1, NULL)`, prints an error on any non-zero result and loops back. When the worker gets there first, it passes a null handle and expects an error it can retry. On `main`, 1 of 14 clean starts aborted about 2 s in with `sceKernelWaitSema: invalid argument`. The abort never occurred under GDB, which slows the start. Recorded in `docs/dev/debt/libkernel.md`: the code is from the PS4 reference and unverified on PS5. Handles are pointers, so a non-null handle that is not a live semaphore is still not detected. ### Tested Linux 7.2.9 (Arch), Ryzen 9 7950X, GCC, Release. - `guest_semaphore` gains null-handle checks for all five calls, including that a timed wait leaves its timeout untouched and a cancel leaves its waiter count untouched. With `main`'s `Semaphore.cpp` restored and only the test kept, it aborts with `sceKernelWaitSema: invalid argument`. With this PR it passes. `ctest -R 'guest_semaphore|semaphore|guest_kernel_errors|guest_umtx'`: 5/5. - Earthion 01.006.000 on this PR (2fc26b970f, Radeon RX 6650 XT, RADV Mesa 26.2.4): a 180 s run through the in-game menu and the intro cinematic, and 8 starts of 12 s each, with no stop. On `main`, the 1-in-14 abort above. The race is intermittent, so these runs cannot show the abort is gone. They do show the title still starts and plays with the change. Bring-up report: #9004. - Independent check by @clairtonmichel ([comment](https://github.com/boykopovar/AnyPS5/pull/9005#issuecomment-6312717273)): clean build on Fedora with a Ryzen 7 9800X3D and Mesa 26.2.5. The kernel synchronization, error and semaphore tests, including `guest_semaphore` and `guest_semaphore_trace`, pass 33/33. No title run or negative control there. - Independent check by @xXPBSXx ([comment](https://github.com/boykopovar/AnyPS5/pull/9005#issuecomment-6312975282)): at `2fc26b970f` on WSL2 Ubuntu 24.04 (a virtualised kernel) with GCC 13.3.0, `guest_semaphore` passes 3/3. With `Semaphore.cpp` restored to the merge base and the new test kept, it aborts. No title run, macOS or Windows there. - Independent check by @valenciana-nc ([comment](https://github.com/boykopovar/AnyPS5/pull/9005#issuecomment-6328873859)): on Windows 11 with WinLibs GCC 15.2.0, against `main` `a6dcea9279`. With only this PR's `GuestSemaphore.cpp` regression added, `guest_semaphore` aborts with `sceKernelWaitSema: invalid argument`. With `2fc26b970f` cherry-picked, the semaphore and kernel tests pass 5/5 and `guest_semaphore` passes 25 runs in a row. Their full-suite failures, the three `agc_driver_sample_order*` tests, match their control run on unchanged `main`. `tools/check_conventions.py --base origin/main` passes. The PR overlap tool reports only adjacent lines in `docs/dev/debt/libkernel.md`, and #6044's test reorganisation, which moves `GuestSemaphore.cpp`. ### 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: PPSA28061 #9004 - [x] GPUs tested: n/a - [x] AI-assisted: yes
clairtonmichel commented 2026-10-21 09:31:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by clairtonmichel on 2026-10-21T09:31:45Z.


Tested on AMD Ryzen 7 9800X3D and AMD Radeon RX 7900 XTX (Fedora Linux, Mesa 26.2.5):

  • Built cleanly with zero errors.
  • Ran the complete kernel synchronization, error handling, and semaphore test suites (guest_semaphore, guest_semaphore_trace, guest_kernel_sync, rwlocks, etc.): 100% passed successfully (33/33 green in 8.41s).
  • Null semaphore handle correctly returns ESRCH. Ready to merge!
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6312717273 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9005#issuecomment-6312717273). Originally posted by [clairtonmichel](https://github.com/clairtonmichel) on 2026-10-21T09:31:45Z. --- Tested on AMD Ryzen 7 9800X3D and AMD Radeon RX 7900 XTX (Fedora Linux, Mesa 26.2.5): - Built cleanly with zero errors. - Ran the complete kernel synchronization, error handling, and semaphore test suites (`guest_semaphore`, `guest_semaphore_trace`, `guest_kernel_sync`, rwlocks, etc.): **100% passed successfully (33/33 green in 8.41s)**. - Null semaphore handle correctly returns `ESRCH`. Ready to merge!
xXPBSXx commented 2026-10-21 09:46:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T09:46:02Z.


Linux data point for 2fc26b970f: WSL2 Ubuntu 24.04 (a virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, BUILD_TESTING=ON, PR head alone (not merged onto main).

  • With the PR: ctest -R '^guest_semaphore$' passes 3 of 3.
  • With core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp restored to the merge-base (new test kept): fails (Subprocess aborted).
    Not tested: macOS, Windows, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6312975282 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9005#issuecomment-6312975282). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T09:46:02Z. --- Linux data point for `2fc26b970f`: WSL2 Ubuntu 24.04 (a virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, `BUILD_TESTING=ON`, PR head alone (not merged onto `main`). - With the PR: `ctest -R '^guest_semaphore$'` passes 3 of 3. - With `core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp` restored to the merge-base (new test kept): fails (`Subprocess aborted`). Not tested: macOS, Windows, a title run. AI-assisted (Claude Code).
valenciana-nc commented 2026-10-21 23:29:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by valenciana-nc on 2026-10-21T23:29:35Z.


Independent Windows validation of this existing fix on current main a6dcea9279.

I first added only this PR's GuestSemaphore.cpp regression, leaving all runtime-library sources identical to main. Built guest_semaphore_tests and ran:

ctest --test-dir build -R '^guest_semaphore$' --output-on-failure

It aborts with exit 0xc0000409 and the same reported Earthion failure:

what(): sceKernelWaitSema: invalid argument

Then cherry-picked your original 2fc26b970f onto that main, preserving authorship (1f625e0f64 on valenciana-nc/AnyPS5, branch game/earthion-startup). No extra implementation changes and no duplicate PR.

  • cmake --build build --target libs relinker -j 6: passed, including patched runtime libraries.
  • ctest --test-dir build -R 'guest_semaphore|semaphore|guest_kernel_errors|guest_umtx' --output-on-failure --parallel 4: 5/5 passed.
  • guest_semaphore with --repeat until-fail:25: 25 consecutive passes.
  • Full build with Ninja -k 0: all buildable targets completed; unchanged ImageArrayWindowPastLayerLimit.cpp:33 still fails with requested alignment '65536' exceeds object file maximum 8192.
  • Full suite (ctest --test-dir build --output-on-failure --parallel 4): 1,853 passed, 18 skipped, 3 failed, 1 not run; 324.55 s, exit 8. The missing test is agc_driver_image_array_window_past_layer_limit. The failures are agc_driver_sample_order, agc_driver_sample_order_grid_reference, and agc_driver_sample_order_recipe_depth_locations; all three diagnostics match an earlier control on the same machine with runtime-library sources identical to this same main. No new executed-test failure was observed.
  • Conventions against origin/main and git diff --check passed.

Windows 11 x86-64 build 26300.9711, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON; the incidental GPU tests selected an NVIDIA GeForce RTX 4060 Laptop GPU. No Earthion, other title, console, Linux or macOS run in this validation. This independently reproduces the failing API call and verifies the fix on Windows; it is not a new gameplay result or PS5 error-code measurement.

AI-assisted validation and report.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6328873859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9005#issuecomment-6328873859). Originally posted by [valenciana-nc](https://github.com/valenciana-nc) on 2026-10-21T23:29:35Z. --- Independent Windows validation of this existing fix on current main `a6dcea9279`. I first added only this PR's `GuestSemaphore.cpp` regression, leaving all runtime-library sources identical to main. Built `guest_semaphore_tests` and ran: ```text ctest --test-dir build -R '^guest_semaphore$' --output-on-failure ``` It aborts with exit `0xc0000409` and the same reported Earthion failure: ```text what(): sceKernelWaitSema: invalid argument ``` Then cherry-picked your original `2fc26b970f` onto that main, preserving authorship (`1f625e0f64` on `valenciana-nc/AnyPS5`, branch `game/earthion-startup`). No extra implementation changes and no duplicate PR. - `cmake --build build --target libs relinker -j 6`: passed, including patched runtime libraries. - `ctest --test-dir build -R 'guest_semaphore|semaphore|guest_kernel_errors|guest_umtx' --output-on-failure --parallel 4`: 5/5 passed. - `guest_semaphore` with `--repeat until-fail:25`: 25 consecutive passes. - Full build with Ninja `-k 0`: all buildable targets completed; unchanged `ImageArrayWindowPastLayerLimit.cpp:33` still fails with `requested alignment '65536' exceeds object file maximum 8192`. - Full suite (`ctest --test-dir build --output-on-failure --parallel 4`): 1,853 passed, 18 skipped, 3 failed, 1 not run; 324.55 s, exit 8. The missing test is `agc_driver_image_array_window_past_layer_limit`. The failures are `agc_driver_sample_order`, `agc_driver_sample_order_grid_reference`, and `agc_driver_sample_order_recipe_depth_locations`; all three diagnostics match an earlier control on the same machine with runtime-library sources identical to this same main. No new executed-test failure was observed. - Conventions against `origin/main` and `git diff --check` passed. Windows 11 x86-64 build 26300.9711, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`; the incidental GPU tests selected an NVIDIA GeForce RTX 4060 Laptop GPU. No Earthion, other title, console, Linux or macOS run in this validation. This independently reproduces the failing API call and verifies the fix on Windows; it is not a new gameplay result or PS5 error-code measurement. AI-assisted validation and report.
JJenkx commented 2026-10-21 23:32:00 +00:00 (Migrated from github.com)

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


@valenciana-nc thanks, that's a thorough check: the regression test failing on unchanged main with the same sceKernelWaitSema: invalid argument error, then the fix passing 25 runs in a row on Windows. I've added it to the Tested section of the description.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6328906773 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9005#issuecomment-6328906773). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-21T23:32:00Z. --- @valenciana-nc thanks, that's a thorough check: the regression test failing on unchanged `main` with the same `sceKernelWaitSema: invalid argument` error, then the fix passing 25 runs in a row on Windows. I've added it to the Tested section of the description.
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!9005
No description provided.