libc: guest setenv(TZ) does not reach tzset, localtime, mktime or strftime, which still use the host time zone; fix branch ready #6910

Closed
opened 2026-10-16 17:45:23 +00:00 by husseinkad · 4 comments
husseinkad commented 2026-10-16 17:45:23 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-16T17:45:23Z.


A guest that calls setenv("TZ", ...) and then tzset(), localtime, mktime or strftime still gets host local time. Guest setenv, putenv and unsetenv change only the guest environment (Environment.cpp), while the libc time functions read the host TZ. On FreeBSD those functions read the process environment, so a program that sets its own time zone (a game that converts to UTC, a log that uses a fixed zone) gets the wrong time here.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-tz-guest-environment (three commits; it merges cleanly into current main; libc/src/Time.cpp, libc/src/Environment.cpp, libc/include/General.hpp, tests GuestTimeConversion.cpp and GuestMktime.cpp, and a debt entry in docs/dev/TechnicalDebt.md). A new applyGuestZone() compares the guest TZ with the last value it saw and, when it changed, rewrites the host TZ and calls the host tzset. It runs from tzset, localtime_s (so localtime and ctime), mktime and strftime, and costs nothing when the value is unchanged. An unset guest TZ becomes :/etc/localtime on Linux and removes the variable on Windows; an empty TZ becomes UTC0 on Windows. The value is copied into a string while the guest-environment lock is held, and one recursive mutex covers the zone switch and the conversion, with the zone read only at the outermost call.

Checks:

  • The new CheckGuestZone cases (a change without tzset, an explicit tzset, an empty TZ, mktime, and a six-reader race test of about 6 to 9 seconds that checks localtime_s, mktime and strftime %H %z for consistency while a thread flips TZ) fail on main and pass with the change; guest_time_conversion, guest_mktime, guest_environment, guest_rtc and guest_strftime pass.
  • An independent stress of 8 threads and 1,000,000 iterations each (localtime_s, mktime, strftime and gmtime, with a flipper alternating EST5 and MSK-3, and a set/unset variant): zero inconsistencies in hour, offset, instant or %Z. The throw path leaves the nesting depth at 0 and the mutex released across 1,000 throws, and gmtime is unaffected.
  • Four independent reviews. They found and I fixed: a use-after-free window (a pointer kept after the environment lock was released), torn results from mktime calling localtime_s in a nested zone scope, and a weak race test. check_conventions.py is clean and pr_overlap.py found no overlap; it touches the same file as #5084 (host time zone for the system service) and #2031 (merged, TZ set once on Linux) but merges with both.

Not covered (listed in the debt entry): a TZ that setenv accepts but the host rejects (for example UTC-5 plus 40,000 characters) makes the time functions throw std::runtime_error, which aborts the process in the real libc (the rule here is that unsupported input throws; the debt note says "throw" but it is fatal); Windows gives a garbage zone name for :Europe/Berlin style names (the host grammar is the C runtime's, not FreeBSD's); tm_zone of a value computed earlier goes stale after TZ changes; rewriting the host TZ also shifts other host consumers such as libSceRtc once a guest changes it. Linux was read, not run. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5888643944 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6910). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-16T17:45:23Z. --- A guest that calls `setenv("TZ", ...)` and then `tzset()`, `localtime`, `mktime` or `strftime` still gets host local time. Guest `setenv`, `putenv` and `unsetenv` change only the guest environment (`Environment.cpp`), while the libc time functions read the host `TZ`. On FreeBSD those functions read the process environment, so a program that sets its own time zone (a game that converts to UTC, a log that uses a fixed zone) gets the wrong time here. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-tz-guest-environment (three commits; it merges cleanly into current main; `libc/src/Time.cpp`, `libc/src/Environment.cpp`, `libc/include/General.hpp`, tests `GuestTimeConversion.cpp` and `GuestMktime.cpp`, and a debt entry in `docs/dev/TechnicalDebt.md`). A new `applyGuestZone()` compares the guest `TZ` with the last value it saw and, when it changed, rewrites the host `TZ` and calls the host `tzset`. It runs from `tzset`, `localtime_s` (so `localtime` and `ctime`), `mktime` and `strftime`, and costs nothing when the value is unchanged. An unset guest `TZ` becomes `:/etc/localtime` on Linux and removes the variable on Windows; an empty `TZ` becomes `UTC0` on Windows. The value is copied into a string while the guest-environment lock is held, and one recursive mutex covers the zone switch and the conversion, with the zone read only at the outermost call. Checks: - The new `CheckGuestZone` cases (a change without `tzset`, an explicit `tzset`, an empty `TZ`, `mktime`, and a six-reader race test of about 6 to 9 seconds that checks `localtime_s`, `mktime` and `strftime` `%H %z` for consistency while a thread flips `TZ`) fail on main and pass with the change; `guest_time_conversion`, `guest_mktime`, `guest_environment`, `guest_rtc` and `guest_strftime` pass. - An independent stress of 8 threads and 1,000,000 iterations each (`localtime_s`, `mktime`, `strftime` and `gmtime`, with a flipper alternating `EST5` and `MSK-3`, and a set/unset variant): zero inconsistencies in hour, offset, instant or `%Z`. The throw path leaves the nesting depth at 0 and the mutex released across 1,000 throws, and `gmtime` is unaffected. - Four independent reviews. They found and I fixed: a use-after-free window (a pointer kept after the environment lock was released), torn results from `mktime` calling `localtime_s` in a nested zone scope, and a weak race test. `check_conventions.py` is clean and `pr_overlap.py` found no overlap; it touches the same file as #5084 (host time zone for the system service) and #2031 (merged, `TZ` set once on Linux) but merges with both. Not covered (listed in the debt entry): a `TZ` that `setenv` accepts but the host rejects (for example `UTC-5` plus 40,000 characters) makes the time functions throw `std::runtime_error`, which aborts the process in the real libc (the rule here is that unsupported input throws; the debt note says "throw" but it is fatal); Windows gives a garbage zone name for `:Europe/Berlin` style names (the host grammar is the C runtime's, not FreeBSD's); `tm_zone` of a value computed earlier goes stale after `TZ` changes; rewriting the host `TZ` also shifts other host consumers such as libSceRtc once a guest changes it. Linux was read, not run. Only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-16 18:02:57 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T18:02:57Z.


I rebased your three commits onto current main; resolving two Time.cpp conflicts while retaining the current tzname export refresh. On Linux, guest_time_conversion and guest_mktime build and pass 2/2, and conventions pass. guest_rtc builds, but its runtime aborts at the host CPU guard before assertions on this Xeon E5-1620 v2 (missing LZCNT/BMI1/BMI2/MOVBE/AVX2/FMA).

I found and fixed one additional regression in my local branch: after setenv_nid_postfix("TZ", ...) followed by localtime without tzset, conversions switched zones but the exported tzname array still pointed at the old names. The added regression failed before the fix and passes after it. The added commit is authored by MarcZX100; your three commits retain your authorship.

There is also an integration overlap with #6686: its CheckRegionZones currently changes host TZ directly, so this change would restore the guest TZ instead. I asked its author to coordinate the merge order; once aligned, I can adjust the combined test and submit the PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6221657759 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6910#issuecomment-6221657759). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-16T18:02:57Z. --- I rebased your three commits onto current `main`; resolving two `Time.cpp` conflicts while retaining the current `tzname` export refresh. On Linux, `guest_time_conversion` and `guest_mktime` build and pass 2/2, and conventions pass. `guest_rtc` builds, but its runtime aborts at the host CPU guard before assertions on this Xeon E5-1620 v2 (missing LZCNT/BMI1/BMI2/MOVBE/AVX2/FMA). I found and fixed one additional regression in my local branch: after `setenv_nid_postfix("TZ", ...)` followed by `localtime` without `tzset`, conversions switched zones but the exported `tzname` array still pointed at the old names. The added regression failed before the fix and passes after it. The added commit is authored by MarcZX100; your three commits retain your authorship. There is also an integration overlap with #6686: its `CheckRegionZones` currently changes host `TZ` directly, so this change would restore the guest `TZ` instead. I asked its author to coordinate the merge order; once aligned, I can adjust the combined test and submit the PR.
MarcZX10 commented 2026-10-16 18:16:18 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T18:16:18Z.


I opened PR #6916 carrying your three commits with your original authorship preserved, plus the tzname regression fix and the component debt-note move. The focused Linux conversion/mktime tests passed 2/2; the hosted conventions check now passes on the corrected PR template. The PR explicitly records the #6686 region-zone test interaction; please coordinate/adapt that test before both changes merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6221894695 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6910#issuecomment-6221894695). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-16T18:16:18Z. --- I opened PR #6916 carrying your three commits with your original authorship preserved, plus the tzname regression fix and the component debt-note move. The focused Linux conversion/mktime tests passed 2/2; the hosted conventions check now passes on the corrected PR template. The PR explicitly records the #6686 region-zone test interaction; please coordinate/adapt that test before both changes merge.
MarcZX10 commented 2026-10-16 18:21:23 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T18:21:23Z.


PR #6916 now passes its hosted matrix on head c5a2f3631: Ubuntu/macOS/Windows relinker, register-context, and macOS write-watch jobs pass. Full Linux/Windows application builds were skipped by the path filters. The focused Linux time conversion and mktime tests also pass 2/2; guest_rtc remains unrun past the host CPU guard.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6221982297 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6910#issuecomment-6221982297). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-16T18:21:23Z. --- PR #6916 now passes its hosted matrix on head `c5a2f3631`: Ubuntu/macOS/Windows relinker, register-context, and macOS write-watch jobs pass. Full Linux/Windows application builds were skipped by the path filters. The focused Linux time conversion and mktime tests also pass 2/2; `guest_rtc` remains unrun past the host CPU guard.
MarcZX10 commented 2026-10-16 18:31:56 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T18:31:56Z.


Correction to my earlier #6686 integration warning: I verified the combined #6686/#6916 test locally. Its existing host setenv continues to exercise each region while the guest TZ value is unchanged, and the combined guest time-conversion test passed with explicit temporary offset assertions for all four regions. I corrected the documentation and #6916 body; no #6686 test adaptation is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6222163251 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6910#issuecomment-6222163251). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-16T18:31:56Z. --- Correction to my earlier #6686 integration warning: I verified the combined #6686/#6916 test locally. Its existing host `setenv` continues to exercise each region while the guest TZ value is unchanged, and the combined guest time-conversion test passed with explicit temporary offset assertions for all four regions. I corrected the documentation and #6916 body; no #6686 test adaptation is required.
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#6910
No description provided.