fix(libc): avoid time conversion races #1107

Merged
Sanjaysrinivas merged 1 commit from upstream-pr/1107 into main 2026-10-08 06:46:02 +00:00
Sanjaysrinivas commented 2026-10-07 12:27:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Sanjaysrinivas on 2026-10-07T12:27:30Z.


What

On Linux/glibc, concurrent guest gmtime and localtime calls can return another thread's date. The wrappers copy from the host's shared tm after its conversion has returned, so their thread-local destination does not protect the copy.

Reuse gmtime_s_nid_postfix and localtime_s_nid_postfix to convert directly into the existing thread-local storage. These helpers already use gmtime_r/localtime_r on Linux and secure CRT calls on Windows. Add an eight-thread behavioral regression for both libc-prefixed and plain aliases, UTC/local conversion, and unrepresentable timestamps.

The shared host buffer is visible in glibc's gmtime and localtime implementations. The reproduced race is Linux-specific; UCRT documents per-thread conversion storage.

Tested

Earlier focused validation of the unchanged contribution patch:

Ubuntu 24.04 under WSL2, GCC 13.3.0, x86-64:

  • The original production Time.cpp fails the new concurrent regression on four consecutive runs: Concurrent libc_gmtime returned another thread's date.
  • The modified production Time.cpp and regression compile and pass with -O2 -Wall -Wextra -Werror -pthread.
  • The regression passes with AddressSanitizer and UndefinedBehaviorSanitizer.
  • An isolated CMake harness includes the repository's actual libc CMakeLists.txt, builds the complete libc shared library and guest_time_conversion_tests, and passes ctest -R '^guest_time_conversion$' --output-on-failure --repeat until-fail:3 (0.72 seconds total).

Reproduce the focused test from the repository root:

g++ -std=c++20 -O2 -Wall -Wextra -Werror -pthread -Icore/libs \
    core/libs/tests/GuestTimeConversion.cpp core/libs/prx/libc/src/Time.cpp \
    -o /tmp/anyps5-time-conversion
/tmp/anyps5-time-conversion

With the full repository configured and built with BUILD_TESTING=ON, run:

ctest --test-dir build -R '^guest_time_conversion$' --output-on-failure --repeat until-fail:3

The local validation harness, tooling and investigation report are excluded from the patch. Full-build results and remaining platform limits are recorded below.

PR overlap (earlier checks)

Direct per-file three-way merge checks confirm a conflict with the whole-tree formatting PR #1128 in core/libs/prx/libc/src/Time.cpp. That PR does not implement this bug fix. #1128 currently conflicts with upstream in StdioExtras.cpp, so the repository overlap tool skips its pairwise conflict check; its empty result does not clear this overlap. This PR merges cleanly into upstream 1e5bb880; merging it and #1128 will require rebasing whichever lands second.

Earlier local validation after rebase

Rebased onto upstream main at cea1ef13. Tested head: 732849d5b7e7128e635d3e140a613f0cb9700596.
Ubuntu 24.04 under WSL2, GCC 13.3, Release; the contribution patch is preserved and its tree matches the checked merge result.

  • Repository conventions and git diff --check passed.
  • Full project CMake build passed with BUILD_TESTING=ON.
  • Focused CTest regression selection: 2 passed, 0 skipped, 0 failed (2 registered).
  • Full CTest suite: 377 passed, 22 skipped, 1 failed (400 registered).
  • The sole failure is guest_access with its working directory on the Windows-mounted filesystem. The identical rebuilt executable passed from an ext4 /tmp directory. That local full-suite run is not reported as green.
  • Vulkan used Mesa llvmpipe software execution with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and GALLIUM_OVERRIDE_CPU_CAPS=avx; agc_driver_flat_store passed in this run.

Title/homebrew execution remains untested. The GitHub-hosted fork checks below are separate from these local results.

Independent fork CI

Build run 37676594070 passed on exact PR head 69901c9ac889ac817c09e3d97c81c21cb6c4d172.
Rebased onto upstream main at 4764e59c37b1fc6c89284fdecaef5a5718b6119e. The unchanged contribution's source tree matches the checked automatic merge result; conventions and git diff --check passed.
Both jobs completed the full Release CMake build with BUILD_TESTING=ON, the libs target and the full CTest suite with --timeout 120 --repeat until-fail:3.

  • Linux: Ubuntu 24.04, GCC 13.3, lavapipe: 380 passed, 21 skipped, 0 failed (401 registered).
  • Native Windows: WinLibs GCC 15.2: 240 passed, 156 skipped, 0 failed (396 registered).
  • The newly merged upstream guest_force_tls_destructor regression passed in all three Linux repetitions.
  • guest_access passed on both hosted runners; the Windows-mounted WSL failure described above was not reproduced there.

These fork checks do not replace upstream workflow approval or maintainer review. No title/homebrew execution or RDNA hardware measurement is claimed.

Checklist

  • Based on current main at 4764e59c; no other inspected open PR implements these functions' fix
  • One topic per PR; follow-ups go in a new PR
  • No comments in code added
  • Unimplemented paths unchanged; no silent stubs added
  • No notes, investigation .md files or images added to the tracked repository
  • No new third-party code
  • Generic behavior, not specific to one title
  • Depends on: none. #545 adds other time exports and does not implement this fix.
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4772605703 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1107). Originally posted by [Sanjaysrinivas](https://github.com/Sanjaysrinivas) on 2026-10-07T12:27:30Z. --- ### What On Linux/glibc, concurrent guest `gmtime` and `localtime` calls can return another thread's date. The wrappers copy from the host's shared `tm` after its conversion has returned, so their thread-local destination does not protect the copy. Reuse `gmtime_s_nid_postfix` and `localtime_s_nid_postfix` to convert directly into the existing thread-local storage. These helpers already use `gmtime_r`/`localtime_r` on Linux and secure CRT calls on Windows. Add an eight-thread behavioral regression for both libc-prefixed and plain aliases, UTC/local conversion, and unrepresentable timestamps. The shared host buffer is visible in [glibc's gmtime](https://github.com/bminor/glibc/blob/master/time/gmtime.c) and [localtime](https://github.com/bminor/glibc/blob/master/time/localtime.c) implementations. The reproduced race is Linux-specific; [UCRT documents per-thread conversion storage](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/gmtime-gmtime32-gmtime64?view=msvc-170). ### Tested Earlier focused validation of the unchanged contribution patch: Ubuntu 24.04 under WSL2, GCC 13.3.0, x86-64: - The original production `Time.cpp` fails the new concurrent regression on four consecutive runs: `Concurrent libc_gmtime returned another thread's date`. - The modified production `Time.cpp` and regression compile and pass with `-O2 -Wall -Wextra -Werror -pthread`. - The regression passes with AddressSanitizer and UndefinedBehaviorSanitizer. - An isolated CMake harness includes the repository's actual libc `CMakeLists.txt`, builds the complete libc shared library and `guest_time_conversion_tests`, and passes `ctest -R '^guest_time_conversion$' --output-on-failure --repeat until-fail:3` (0.72 seconds total). Reproduce the focused test from the repository root: ```sh g++ -std=c++20 -O2 -Wall -Wextra -Werror -pthread -Icore/libs \ core/libs/tests/GuestTimeConversion.cpp core/libs/prx/libc/src/Time.cpp \ -o /tmp/anyps5-time-conversion /tmp/anyps5-time-conversion ``` With the full repository configured and built with `BUILD_TESTING=ON`, run: ```sh ctest --test-dir build -R '^guest_time_conversion$' --output-on-failure --repeat until-fail:3 ``` The local validation harness, tooling and investigation report are excluded from the patch. Full-build results and remaining platform limits are recorded below. ### PR overlap (earlier checks) Direct per-file three-way merge checks confirm a conflict with the whole-tree formatting PR #1128 in `core/libs/prx/libc/src/Time.cpp`. That PR does not implement this bug fix. #1128 currently conflicts with upstream in `StdioExtras.cpp`, so the repository overlap tool skips its pairwise conflict check; its empty result does not clear this overlap. This PR merges cleanly into upstream `1e5bb880`; merging it and #1128 will require rebasing whichever lands second. ### Earlier local validation after rebase Rebased onto upstream `main` at `cea1ef13`. Tested head: `732849d5b7e7128e635d3e140a613f0cb9700596`. Ubuntu 24.04 under WSL2, GCC 13.3, Release; the contribution patch is preserved and its tree matches the checked merge result. - Repository conventions and `git diff --check` passed. - Full project CMake build passed with `BUILD_TESTING=ON`. - Focused CTest regression selection: 2 passed, 0 skipped, 0 failed (2 registered). - Full CTest suite: 377 passed, 22 skipped, 1 failed (400 registered). - The sole failure is `guest_access` with its working directory on the Windows-mounted filesystem. The identical rebuilt executable passed from an ext4 `/tmp` directory. That local full-suite run is not reported as green. - Vulkan used Mesa llvmpipe software execution with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `GALLIUM_OVERRIDE_CPU_CAPS=avx`; `agc_driver_flat_store` passed in this run. Title/homebrew execution remains untested. The GitHub-hosted fork checks below are separate from these local results. ### Independent fork CI [Build run 37676594070](https://github.com/Sanjaysrinivas/AnyPS5/actions/runs/37676594070) passed on exact PR head `69901c9ac889ac817c09e3d97c81c21cb6c4d172`. Rebased onto upstream `main` at `4764e59c37b1fc6c89284fdecaef5a5718b6119e`. The unchanged contribution's source tree matches the checked automatic merge result; conventions and `git diff --check` passed. Both jobs completed the full Release CMake build with `BUILD_TESTING=ON`, the `libs` target and the full CTest suite with `--timeout 120 --repeat until-fail:3`. - Linux: Ubuntu 24.04, GCC 13.3, lavapipe: **380 passed, 21 skipped, 0 failed** (401 registered). - Native Windows: WinLibs GCC 15.2: **240 passed, 156 skipped, 0 failed** (396 registered). - The newly merged upstream `guest_force_tls_destructor` regression passed in all three Linux repetitions. - `guest_access` passed on both hosted runners; the Windows-mounted WSL failure described above was not reproduced there. These fork checks do not replace upstream workflow approval or maintainer review. No title/homebrew execution or RDNA hardware measurement is claimed. ### Checklist - [x] Based on current `main` at `4764e59c`; no other inspected open PR implements these functions' fix - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code added - [x] Unimplemented paths unchanged; no silent stubs added - [x] No notes, investigation `.md` files or images added to the tracked repository - [x] No new third-party code - [x] Generic behavior, not specific to one title - [x] Depends on: none. #545 adds other time exports and does not implement this fix. - [x] AI-assisted: yes
adriadam10 commented 2026-10-08 06:45:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T06:45:59Z.


Checked build + full ctest on Linux (423/423, guest_time_conversion passes) and that the new test fails on main's Time.cpp (reverted the fix: concurrent conversion test aborts). Linux and Windows CI green on this head; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6054151997 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1107#issuecomment-6054151997). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T06:45:59Z. --- Checked build + full ctest on Linux (423/423, `guest_time_conversion` passes) and that the new test fails on `main`'s `Time.cpp` (reverted the fix: concurrent conversion test aborts). Linux and Windows CI green on this head; merging.
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!1107
No description provided.