libkernel: pthread_cond_timedwait with negative seconds returns ETIMEDOUT where FreeBSD returns EINVAL; fix branch ready #5561

Closed
opened 2026-10-13 23:56:48 +00:00 by husseinkad · 2 comments
husseinkad commented 2026-10-13 23:56:48 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T23:56:48Z.


pthread_cond_timedwait accepts an absolute time with negative seconds and returns ETIMEDOUT (or EPERM when the mutex is not held). FreeBSD 11.4 returns EINVAL. _pthread_cond_timedwait in thr_cond.c (lines 389 and 401) does if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) return (EINVAL); before it checks that the mutex is owned, and the kernel agrees: umtx_copyin_timeout and umtx_copyin_umtx_time in kern_umtx.c reject tv_sec < 0 with EINVAL, so a negative absolute time is never treated as already expired. The existing code here only rejected a tv_nsec outside [0, 1e9).

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-cond-timedwait-negative-seconds (one commit; the branch point is about 300 commits behind main, so it needs a rebase, which is a one-line change in libkernel/Pthread/Posix/Cond.cpp plus the test tests/GuestCondTimedwait.cpp). The check adds tv_sec < 0 next to the existing tv_nsec check.

Checks:

  • The new AbsoluteWait() case in guest_cond_timedwait fails on the original code (abort) and passes with the change; guest_cond_timedwait, guest_cond_recursive_mutex and guest_cond_unowned_mutex pass. The test checks that a null abstime, negative tv_sec and an out-of-range tv_nsec return EINVAL with the mutex still held, and that a past deadline and {0,0} return ETIMEDOUT with the mutex still held.
  • The probe ran against current main and main plus the one-line patch, with default and CLOCK_MONOTONIC condattr clocks, default, error-checking, recursive (depth 1 and 2) and normal mutexes, held and not held, and 8 threads (about 12,000 waits per mode): only tv_sec < 0 with a valid tv_nsec changes. {-1,0}, {INT64_MIN,0}, {INT64_MIN,999999999} and {-1,999999999} go from ETIMEDOUT (mutex held) or EPERM (not held) to EINVAL. NULL, {0,0}, {0,999999999}, {1,0}, {now±1,0}, {INT64_MAX,*} and the bad-tv_nsec values give the same results as before. The mutex ownership state is identical after every return (recursive depth preserved), and a signalled waiter still returns 0.
  • Two independent reviews quoted the FreeBSD lines above and found nothing wrong. check_conventions.py is clean and pr_overlap.py finds one overlap, below.

Heads-up for #4953 (rejecting unsupported POSIX condition clocks): it edits the same test file, so the test additions conflict textually, and its test expects {-1, 0} to return ETIMEDOUT, which contradicts the FreeBSD source quoted above. Whichever lands second needs a rebase and that expectation should change to EINVAL.

Not covered: Mutex.cpp, Rwlock.cpp and Sem.cpp share RelativeMicroseconds and have the same tv_sec < 0 gap (not touched here; sem_timedwait is claimed by #3119 and #3691). Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5839546253 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5561). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T23:56:48Z. --- `pthread_cond_timedwait` accepts an absolute time with negative seconds and returns `ETIMEDOUT` (or `EPERM` when the mutex is not held). FreeBSD 11.4 returns `EINVAL`. `_pthread_cond_timedwait` in `thr_cond.c` (lines 389 and 401) does `if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) return (EINVAL);` before it checks that the mutex is owned, and the kernel agrees: `umtx_copyin_timeout` and `umtx_copyin_umtx_time` in `kern_umtx.c` reject `tv_sec < 0` with `EINVAL`, so a negative absolute time is never treated as already expired. The existing code here only rejected a `tv_nsec` outside [0, 1e9). Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-cond-timedwait-negative-seconds (one commit; the branch point is about 300 commits behind main, so it needs a rebase, which is a one-line change in `libkernel/Pthread/Posix/Cond.cpp` plus the test `tests/GuestCondTimedwait.cpp`). The check adds `tv_sec < 0` next to the existing `tv_nsec` check. Checks: - The new `AbsoluteWait()` case in `guest_cond_timedwait` fails on the original code (abort) and passes with the change; `guest_cond_timedwait`, `guest_cond_recursive_mutex` and `guest_cond_unowned_mutex` pass. The test checks that a null `abstime`, negative `tv_sec` and an out-of-range `tv_nsec` return `EINVAL` with the mutex still held, and that a past deadline and `{0,0}` return `ETIMEDOUT` with the mutex still held. - The probe ran against current main and main plus the one-line patch, with default and `CLOCK_MONOTONIC` condattr clocks, default, error-checking, recursive (depth 1 and 2) and normal mutexes, held and not held, and 8 threads (about 12,000 waits per mode): only `tv_sec < 0` with a valid `tv_nsec` changes. `{-1,0}`, `{INT64_MIN,0}`, `{INT64_MIN,999999999}` and `{-1,999999999}` go from `ETIMEDOUT` (mutex held) or `EPERM` (not held) to `EINVAL`. `NULL`, `{0,0}`, `{0,999999999}`, `{1,0}`, `{now±1,0}`, `{INT64_MAX,*}` and the bad-`tv_nsec` values give the same results as before. The mutex ownership state is identical after every return (recursive depth preserved), and a signalled waiter still returns 0. - Two independent reviews quoted the FreeBSD lines above and found nothing wrong. `check_conventions.py` is clean and `pr_overlap.py` finds one overlap, below. Heads-up for #4953 (rejecting unsupported POSIX condition clocks): it edits the same test file, so the test additions conflict textually, and its test expects `{-1, 0}` to return `ETIMEDOUT`, which contradicts the FreeBSD source quoted above. Whichever lands second needs a rebase and that expectation should change to `EINVAL`. Not covered: `Mutex.cpp`, `Rwlock.cpp` and `Sem.cpp` share `RelativeMicroseconds` and have the same `tv_sec < 0` gap (not touched here; `sem_timedwait` is claimed by #3119 and #3691). Only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-14 00:03:33 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T00:03:33Z.


Carried the ready fix as #5566. Hussein Kadhim's implementation commit retains his original author attribution; MarcZX100's follow-up regression also checks that a negative deadline returns EINVAL before the mutex-ownership check (EPERM).

On Linux (GCC 12.2, Ninja Release), guest_cond_timedwait, guest_cond_recursive_mutex and guest_cond_unowned_mutex each pass three consecutive runs. With the new timedwait test retained and the guard removed, the baseline test aborts as expected; the fixed head passes. Conventions and git diff --check pass. I did not rerun Windows; the Windows evidence remains the results recorded here by the original author.

AI-assisted: yes; reviewed and tested by MarcZX100.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157805240 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5561#issuecomment-6157805240). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T00:03:33Z. --- Carried the ready fix as #5566. Hussein Kadhim's implementation commit retains his original author attribution; MarcZX100's follow-up regression also checks that a negative deadline returns `EINVAL` before the mutex-ownership check (`EPERM`). On Linux (GCC 12.2, Ninja Release), `guest_cond_timedwait`, `guest_cond_recursive_mutex` and `guest_cond_unowned_mutex` each pass three consecutive runs. With the new timedwait test retained and the guard removed, the baseline test aborts as expected; the fixed head passes. Conventions and `git diff --check` pass. I did not rerun Windows; the Windows evidence remains the results recorded here by the original author. AI-assisted: yes; reviewed and tested by MarcZX100.
oneandonlydean commented 2026-10-14 01:07:47 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:07:47Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. #5566 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158586679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5561#issuecomment-6158586679). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T01:07:47Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. #5566 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
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#5561
No description provided.