libkernel: sem_timedwait fails with EINVAL on an available semaphore when the timeout is NULL or out of range; fix branch ready #3119

Closed
opened 2026-10-10 22:35:19 +00:00 by milescoviello · 1 comment
milescoviello commented 2026-10-10 22:35:19 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-10T22:35:19Z.


sem_timedwait validates abs_timeout before it looks at the semaphore. With a count of 1 and a NULL timeout or one with tv_nsec = 1000000000, it returns -1 with errno 22 and leaves the count at 1. FreeBSD's _sem_clockwait_np (lib/libc/gen/sem_new.c) takes an available semaphore first and checks the timeout only when the thread would block ("The timeout argument is only supposed to be checked if the thread would have blocked"). A NULL timeout means no limit there: FreeBSD's sem_wait is sem_timedwait(sem, NULL). POSIX allows skipping the check when the semaphore can be locked immediately.

Fix on my fork: milescoviello/AnyPS5:fix/sem-timedwait-timeout-check (one commit, e7d71abf, on main 4d27834f; the 12 commits on main since then don't touch these files): take the semaphore first, wait without a limit for NULL, and keep EINVAL/ETIMEDOUT when the call would block. guest_posix_sem_post gains checks that fail on main and pass with the fix. Linux only; Windows was not built or tested (no platform-specific code).

Setup: Gentoo, Linux 7.2.8, GCC 15.3.0, Release with CI's configure flags, main 4d27834f.

Tested: the new checks fail on main and pass with the fix. Full ctest with the fix on Linux (RTX 5090 Laptop GPU): no new failures; the only failures are the known agc_driver_dcc_keys_first_write (#2767) and two guest_video_out_* tests that time out with the displays powered off. tools/check_conventions.py passes. All five fixes I am filing today also pass their tests merged together on the current main.

Not opened as a PR because intake is paused for new contributors; a maintainer is welcome to carry the commit.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795893595 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3119). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-10T22:35:19Z. --- `sem_timedwait` validates `abs_timeout` before it looks at the semaphore. With a count of 1 and a NULL timeout or one with `tv_nsec = 1000000000`, it returns -1 with `errno` 22 and leaves the count at 1. FreeBSD's `_sem_clockwait_np` (`lib/libc/gen/sem_new.c`) takes an available semaphore first and checks the timeout only when the thread would block ("The timeout argument is only supposed to be checked if the thread would have blocked"). A NULL timeout means no limit there: FreeBSD's `sem_wait` is `sem_timedwait(sem, NULL)`. POSIX allows skipping the check when the semaphore can be locked immediately. Fix on my fork: [`milescoviello/AnyPS5:fix/sem-timedwait-timeout-check`](https://github.com/milescoviello/AnyPS5/tree/fix/sem-timedwait-timeout-check) (one commit, `e7d71abf`, on `main` 4d27834f; the 12 commits on `main` since then don't touch these files): take the semaphore first, wait without a limit for NULL, and keep `EINVAL`/`ETIMEDOUT` when the call would block. `guest_posix_sem_post` gains checks that fail on main and pass with the fix. Linux only; Windows was not built or tested (no platform-specific code). **Setup:** Gentoo, Linux 7.2.8, GCC 15.3.0, Release with CI's configure flags, `main` 4d27834f. **Tested:** the new checks fail on `main` and pass with the fix. Full `ctest` with the fix on Linux (RTX 5090 Laptop GPU): no new failures; the only failures are the known `agc_driver_dcc_keys_first_write` (#2767) and two `guest_video_out_*` tests that time out with the displays powered off. `tools/check_conventions.py` passes. All five fixes I am filing today also pass their tests merged together on the current `main`. Not opened as a PR because intake is paused for new contributors; a maintainer is welcome to carry the commit. AI-assisted: yes
hikazey commented 2026-10-11 00:53:14 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by hikazey on 2026-10-11T00:53:14Z.


Windows check, since the issue says Windows wasn't tested. This branch merged onto main f1ae427b, Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release with CI's configure flags.

build guest_posix_sem_*
this branch 5/5 passed
Sem.cpp reverted to main, test kept 5/5 failed
full suite with this branch 756/758 passed

The two full-suite failures also fail on plain main here (pr_overlap_encoding: the two *_error_preserves_unicode cases; guest_filesystem: line 276, guest errno 14), so they are unrelated.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103942558 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3119#issuecomment-6103942558). Originally posted by [hikazey](https://github.com/hikazey) on 2026-10-11T00:53:14Z. --- Windows check, since the issue says Windows wasn't tested. This branch merged onto `main` `f1ae427b`, Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release with CI's configure flags. | build | `guest_posix_sem_*` | | --- | --- | | this branch | 5/5 passed | | `Sem.cpp` reverted to `main`, test kept | 5/5 failed | | full suite with this branch | 756/758 passed | The two full-suite failures also fail on plain `main` here (`pr_overlap_encoding`: the two `*_error_preserves_unicode` cases; `guest_filesystem`: line 276, guest errno 14), so they are unrelated. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
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#3119
No description provided.