libkernel: alarm() accepts seconds above INT32_MAX/2 that FreeBSD rejects with EINVAL (returns -1); fix branch ready #6940

Closed
opened 2026-10-16 20:28:24 +00:00 by husseinkad · 0 comments
husseinkad commented 2026-10-16 20:28:24 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-16T20:28:24Z.


alarm() accepts any number of seconds, so alarm(UINT_MAX) arms a timer about 136 years out and returns the old remaining time. FreeBSD 11.4 alarm() is a thin wrapper over setitimer(ITIMER_REAL), and kern_setitimer fails any it_value.tv_sec above INT32_MAX / 2 (1,073,741,823) with EINVAL (if (itimerfix(&aitv->it_value) || aitv->it_value.tv_sec > INT32_MAX / 2) return (EINVAL);), so alarm() returns -1 (the largest unsigned value) and the pending alarm is left alone. setitimer here already applies that bound (#3800), so alarm and setitimer disagreed.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-alarm-range (one commit; it merges cleanly into current main; libkernel/System/src/Signals.cpp and tests/GuestGetitimer.cpp). For seconds above 1,073,741,823, GuestAlarm_nid_no_patch now sets errno to 22, returns UINT_MAX and changes nothing; MaxItimerSeconds was moved above the alarm code. GuestGetitimer.cpp previously asserted that alarm(UINT_MAX) arms a long alarm; it now arms the largest valid alarm, checks that the three out-of-range values fail with EINVAL, and checks the pending alarm is unchanged.

Checks:

  • The updated test fails on main (abort, "an alarm beyond the itimer range did not fail with EINVAL") and passes with the change; guest_getitimer, guest_itimer and guest_signals pass.
  • Probed through the guest exports against main: alarm(1073741824), alarm(INT_MAX), alarm(2^31) and alarm(UINT_MAX) now return 4294967295 with errno 22 (main accepted them); after alarm(30), a failing alarm(UINT_MAX) leaves about 30 s pending and alarm(0) returns 30 (main replaced it with a roughly 4.29e9 s alarm). Unchanged: alarm(0), 1, 2, 100,000,000, 100,000,001 and 1,073,741,823 behave as on main, successive alarms return the previous remaining seconds, errno is untouched on success, a 1 s alarm still delivers SIGALRM (exit 142 with the default disposition), SIG_IGN is harmless and alarm(0) cancels. setitimer agrees with alarm at every value tested.
  • Two independent reviews quoted the FreeBSD 11.4 lines above, found nothing in the repo that relies on alarm(UINT_MAX) arming a long alarm, and found nothing wrong. check_conventions.py is clean.

Not covered: one probe run hung at process exit once and did not recur in 15 branch runs and 12 main runs, so I cannot tie it to this change. pr_overlap.py could not compare #3986 (the signal mask, other parts of Signals.cpp) and #6044 (it moves GuestGetitimer.cpp to tests/time/, so a trivial rebase of the test edit is needed when it lands); by hand neither conflicts. Based on the FreeBSD source, not a console measurement. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5890742761 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6940). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-16T20:28:24Z. --- `alarm()` accepts any number of seconds, so `alarm(UINT_MAX)` arms a timer about 136 years out and returns the old remaining time. FreeBSD 11.4 `alarm()` is a thin wrapper over `setitimer(ITIMER_REAL)`, and `kern_setitimer` fails any `it_value.tv_sec` above `INT32_MAX / 2` (1,073,741,823) with `EINVAL` (`if (itimerfix(&aitv->it_value) || aitv->it_value.tv_sec > INT32_MAX / 2) return (EINVAL);`), so `alarm()` returns -1 (the largest unsigned value) and the pending alarm is left alone. `setitimer` here already applies that bound (#3800), so `alarm` and `setitimer` disagreed. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-alarm-range (one commit; it merges cleanly into current main; `libkernel/System/src/Signals.cpp` and `tests/GuestGetitimer.cpp`). For seconds above 1,073,741,823, `GuestAlarm_nid_no_patch` now sets `errno` to 22, returns `UINT_MAX` and changes nothing; `MaxItimerSeconds` was moved above the alarm code. `GuestGetitimer.cpp` previously asserted that `alarm(UINT_MAX)` arms a long alarm; it now arms the largest valid alarm, checks that the three out-of-range values fail with `EINVAL`, and checks the pending alarm is unchanged. Checks: - The updated test fails on main (abort, "an alarm beyond the itimer range did not fail with EINVAL") and passes with the change; `guest_getitimer`, `guest_itimer` and `guest_signals` pass. - Probed through the guest exports against main: `alarm(1073741824)`, `alarm(INT_MAX)`, `alarm(2^31)` and `alarm(UINT_MAX)` now return 4294967295 with `errno` 22 (main accepted them); after `alarm(30)`, a failing `alarm(UINT_MAX)` leaves about 30 s pending and `alarm(0)` returns 30 (main replaced it with a roughly 4.29e9 s alarm). Unchanged: `alarm(0)`, 1, 2, 100,000,000, 100,000,001 and 1,073,741,823 behave as on main, successive alarms return the previous remaining seconds, `errno` is untouched on success, a 1 s alarm still delivers `SIGALRM` (exit 142 with the default disposition), `SIG_IGN` is harmless and `alarm(0)` cancels. `setitimer` agrees with `alarm` at every value tested. - Two independent reviews quoted the FreeBSD 11.4 lines above, found nothing in the repo that relies on `alarm(UINT_MAX)` arming a long alarm, and found nothing wrong. `check_conventions.py` is clean. Not covered: one probe run hung at process exit once and did not recur in 15 branch runs and 12 main runs, so I cannot tie it to this change. `pr_overlap.py` could not compare #3986 (the signal mask, other parts of `Signals.cpp`) and #6044 (it moves `GuestGetitimer.cpp` to `tests/time/`, so a trivial rebase of the test edit is needed when it lands); by hand neither conflicts. Based on the FreeBSD source, not a console measurement. Only Windows was built and run. AI-assisted: yes
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#6940
No description provided.