fix(libkernel): clamp POSIX deadlines beyond the clock range instead … #4005

Merged
zmeizy merged 1 commit from upstream-pr/4005 into main 2026-10-12 03:12:38 +00:00
zmeizy commented 2026-10-11 21:12:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-11T21:12:33Z.


What

PosixThread::RelativeMicroseconds (Pthread/Posix/Common.hpp) turns an absolute deadline into a relative wait. pthread_rwlock_timedrdlock, pthread_rwlock_timedwrlock, pthread_cond_timedwait and sem_timedwait all use it. It converted the deadline to std::chrono::nanoseconds, so a tv_sec beyond about 9.2 × 10^9 (the year 2262) overflowed 64 bits, which is undefined behaviour. {INT64_MAX, 0}, a common "wait forever" value, wrapped negative and timed out at once.

The seconds are now compared first:

  • A deadline more than a second in the past gives 0.
  • A deadline further ahead than the sce layer accepts (UINT_MAX µs) gives that maximum, as large valid deadlines already did.
  • Anything else uses the same microsecond arithmetic as before.

Tested

Ubuntu 24.04, GCC 13.3, on main 0a8b1d32.

  • guest_pthread_rwlock, new check with a reader holding the lock:
    • pthread_rwlock_timedwrlock with {INT64_MIN, 0} returns ETIMEDOUT.
    • With {INT64_MAX, 0} it waits until the reader releases the lock, then returns 0.
  • The check fails on main without the change (ETIMEDOUT at once for INT64_MAX) and passes with it.
  • guest_cond_timedwait, guest_posix_sem_init, guest_posix_sem_post and guest_pthread_mutexattr pass.

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 in TechnicalDebt (n/a)
  • 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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4822386960 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4005). Originally posted by [zmeizy](https://github.com/zmeizy) on 2026-10-11T21:12:33Z. --- ### What `PosixThread::RelativeMicroseconds` (`Pthread/Posix/Common.hpp`) turns an absolute deadline into a relative wait. `pthread_rwlock_timedrdlock`, `pthread_rwlock_timedwrlock`, `pthread_cond_timedwait` and `sem_timedwait` all use it. It converted the deadline to `std::chrono::nanoseconds`, so a `tv_sec` beyond about 9.2 × 10^9 (the year 2262) overflowed 64 bits, which is undefined behaviour. `{INT64_MAX, 0}`, a common "wait forever" value, wrapped negative and timed out at once. The seconds are now compared first: - A deadline more than a second in the past gives 0. - A deadline further ahead than the sce layer accepts (`UINT_MAX` µs) gives that maximum, as large valid deadlines already did. - Anything else uses the same microsecond arithmetic as before. ### Tested Ubuntu 24.04, GCC 13.3, on `main` `0a8b1d32`. - `guest_pthread_rwlock`, new check with a reader holding the lock: - `pthread_rwlock_timedwrlock` with `{INT64_MIN, 0}` returns `ETIMEDOUT`. - With `{INT64_MAX, 0}` it waits until the reader releases the lock, then returns 0. - The check fails on `main` without the change (`ETIMEDOUT` at once for `INT64_MAX`) and passes with it. - `guest_cond_timedwait`, `guest_posix_sem_init`, `guest_posix_sem_post` and `guest_pthread_mutexattr` pass. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) (n/a) - [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 (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA
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!4005
No description provided.