libc: guest setenv(TZ) does not reach tzset, localtime, mktime or strftime, which still use the host time zone; fix branch ready #6910
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-16T17:45:23Z.
A guest that calls
setenv("TZ", ...)and thentzset(),localtime,mktimeorstrftimestill gets host local time. Guestsetenv,putenvandunsetenvchange only the guest environment (Environment.cpp), while the libc time functions read the hostTZ. 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, testsGuestTimeConversion.cppandGuestMktime.cpp, and a debt entry indocs/dev/TechnicalDebt.md). A newapplyGuestZone()compares the guestTZwith the last value it saw and, when it changed, rewrites the hostTZand calls the hosttzset. It runs fromtzset,localtime_s(solocaltimeandctime),mktimeandstrftime, and costs nothing when the value is unchanged. An unset guestTZbecomes:/etc/localtimeon Linux and removes the variable on Windows; an emptyTZbecomesUTC0on 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:
CheckGuestZonecases (a change withouttzset, an explicittzset, an emptyTZ,mktime, and a six-reader race test of about 6 to 9 seconds that checkslocaltime_s,mktimeandstrftime%H %zfor consistency while a thread flipsTZ) fail on main and pass with the change;guest_time_conversion,guest_mktime,guest_environment,guest_rtcandguest_strftimepass.localtime_s,mktime,strftimeandgmtime, with a flipper alternatingEST5andMSK-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, andgmtimeis unaffected.mktimecallinglocaltime_sin a nested zone scope, and a weak race test.check_conventions.pyis clean andpr_overlap.pyfound no overlap; it touches the same file as #5084 (host time zone for the system service) and #2031 (merged,TZset once on Linux) but merges with both.Not covered (listed in the debt entry): a
TZthatsetenvaccepts but the host rejects (for exampleUTC-5plus 40,000 characters) makes the time functions throwstd::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/Berlinstyle names (the host grammar is the C runtime's, not FreeBSD's);tm_zoneof a value computed earlier goes stale afterTZchanges; rewriting the hostTZalso 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
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T18:02:57Z.
I rebased your three commits onto current
main; resolving twoTime.cppconflicts while retaining the currenttznameexport refresh. On Linux,guest_time_conversionandguest_mktimebuild and pass 2/2, and conventions pass.guest_rtcbuilds, 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 bylocaltimewithouttzset, conversions switched zones but the exportedtznamearray 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
CheckRegionZonescurrently changes hostTZdirectly, so this change would restore the guestTZinstead. I asked its author to coordinate the merge order; once aligned, I can adjust the combined test and submit the PR.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.
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_rtcremains unrun past the host CPU guard.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
setenvcontinues 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.