libkernel: pthread_cond_timedwait with negative seconds returns ETIMEDOUT where FreeBSD returns EINVAL; fix branch ready #5561
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T23:56:48Z.
pthread_cond_timedwaitaccepts an absolute time with negative seconds and returnsETIMEDOUT(orEPERMwhen the mutex is not held). FreeBSD 11.4 returnsEINVAL._pthread_cond_timedwaitinthr_cond.c(lines 389 and 401) doesif (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_timeoutandumtx_copyin_umtx_timeinkern_umtx.crejecttv_sec < 0withEINVAL, so a negative absolute time is never treated as already expired. The existing code here only rejected atv_nsecoutside [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.cppplus the testtests/GuestCondTimedwait.cpp). The check addstv_sec < 0next to the existingtv_nseccheck.Checks:
AbsoluteWait()case inguest_cond_timedwaitfails on the original code (abort) and passes with the change;guest_cond_timedwait,guest_cond_recursive_mutexandguest_cond_unowned_mutexpass. The test checks that a nullabstime, negativetv_secand an out-of-rangetv_nsecreturnEINVALwith the mutex still held, and that a past deadline and{0,0}returnETIMEDOUTwith the mutex still held.CLOCK_MONOTONICcondattr 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): onlytv_sec < 0with a validtv_nsecchanges.{-1,0},{INT64_MIN,0},{INT64_MIN,999999999}and{-1,999999999}go fromETIMEDOUT(mutex held) orEPERM(not held) toEINVAL.NULL,{0,0},{0,999999999},{1,0},{now±1,0},{INT64_MAX,*}and the bad-tv_nsecvalues 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.check_conventions.pyis clean andpr_overlap.pyfinds 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 returnETIMEDOUT, which contradicts the FreeBSD source quoted above. Whichever lands second needs a rebase and that expectation should change toEINVAL.Not covered:
Mutex.cpp,Rwlock.cppandSem.cppshareRelativeMicrosecondsand have the sametv_sec < 0gap (not touched here;sem_timedwaitis claimed by #3119 and #3691). Only Windows was built and run.AI-assisted: yes
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
EINVALbefore the mutex-ownership check (EPERM).On Linux (GCC 12.2, Ninja Release),
guest_cond_timedwait,guest_cond_recursive_mutexandguest_cond_unowned_mutexeach 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 andgit diff --checkpass. 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.
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.