fix(libc): report null gmtime_s and localtime_s arguments to the constraint handler #12593

Merged
JJenkx merged 4 commits from upstream-pr/12593 into main 2026-11-09 23:15:17 +00:00
JJenkx commented 2026-10-28 21:57:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-28T21:57:42Z.


What

gmtime_s and localtime_s now report a null timer or result to the constraint handler, as titles' libc.prx does. The handler gets "gmtime_s: bad argument" or "localtime_s: bad argument", EINVAL and a null pointer, and the function returns null. On main it returned null without calling the handler.

gmtime, localtime, gmtime_r and localtime_r are built on these two. They keep returning null for a null argument without calling the handler, exactly as on main.

The default constraint-handler prerequisite is fulfilled on current main b11c7bf84f5d2f636ff60166cc3371bd071e24d5: #9935 was closed after its changes landed through #10270. Merging its final head into this main produces the unchanged main tree. Earlier test and title measurements retain their recorded source pins.

Overlap: ntamas94's #10409 (36-byte struct tm, tm_isdst = -1 from gmtime_s) also edits these two functions. It currently conflicts with main on its own; the change here is the four-line null check at the top of each function.

Tested

Current-main refresh — 2026-11-02. Published head 23fcc2d857f40e9c7f2b0cbc451c89d574bddb30 includes pinned main a4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8. The time-conversion constraint implementation and null argument fixtures are retained with main’s Time.cpp updates and timelocal debt. Quiet null returns of ordinary gmtime/localtime and _r forms remain the documented host compatibility behavior, not a new title-library assertion.

Rebuilt actual libc/Ampr and selected tests from the final input tree. guest_crt, guest_time_conversion, guest_time_extras, guest_mktime, guest_strftime, guest_locale each pass three consecutive runs: 18 runtime passes, zero skips, plus public_headers. Conventions and whitespace pass. Source/input manifests, exact dependencies, library/test hashes, logs/JUnit and head-leased push receipts are attached. The same pinned main passes 18 selected groups three times (54 runtime passes) plus headers. All 34 recursive dependency trees/31,551 tracked blobs verify with zero errors; QuickJS’s update=none test262 corpus is explicitly excluded because it is not a library build input.

Linux-7.2.11-arch1-1-x86_64-with-glibc2.44, Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON. Focused Linux CPU qualification only. Older Windows/module/reference reports retain their original pins; no fresh Windows/macOS, SDK-module, console, title/GPU or speed claim. AI-assisted: yes (Codex).

Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-six-main-a4eca-linux-extra10321-20261102.tar.gz; SHA-256 525ccb31a5d3d0f5adced5fda58818d8432f06c0afa7290139adb60ae6664b54.

Earlier dated qualifications below retain their recorded source/platform pins.

Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, main 319621ba2e.

  • Native comparison: a scratch loader calls the titles' functions (PPSA07923) by NID next to this build's, with the same recording handler on both sides.
    • gmtime_s: 50,000 random calls with times across the 64-bit range and null arguments. Calls with a null argument differ in 4,960 cases on main and 0 with this PR.
    • The other differences don't involve null arguments, and this PR leaves them unchanged:
      • 9,549 calls inside 1901 to 2099 differ only in tm_isdst (titles -1, main 0), which #10409 addresses;
      • 35,491 calls outside that range differ because the titles' gmtime_s gets those dates wrong (it returns wrapped years even for INT64_MAX), which this build does not copy.
    • localtime_s: the titles report "localtime_s: bad argument" for a null timer, a null result and both. Its non-null path needs a time-zone import the loader doesn't provide, so it isn't compared.
  • Non-_s functions: the titles' gmtime, localtime, gmtime_r and localtime_r don't check for null at all. Each of the eight null cases (one for each single-argument function, three for each _r function) ends in an access violation on PPSA07923. Returning null without a handler call for these four is therefore main's existing, safer behaviour, which this PR keeps, not a titles rule.
  • Regression: guest_time_conversion gains CheckNullArguments, which covers each null combination for both functions and a valid call with no handler call. With the previous Time.cpp it fails (null argument did not report a bad argument).
  • Test set: guest_time_conversion, guest_time_extras, guest_mktime, guest_strftime, guest_crt and guest_locale pass 3/3. Conventions 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 under Silent stubs in their library's technical debt file
  • 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
  • Generic behaviour, not specific to one title
  • Depends on: none (#9935 fulfilled through merged #10270)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5058339453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12593). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T21:57:42Z. --- ### What `gmtime_s` and `localtime_s` now report a null `timer` or `result` to the constraint handler, as titles' `libc.prx` does. The handler gets `"gmtime_s: bad argument"` or `"localtime_s: bad argument"`, `EINVAL` and a null pointer, and the function returns null. On `main` it returned null without calling the handler. `gmtime`, `localtime`, `gmtime_r` and `localtime_r` are built on these two. They keep returning null for a null argument without calling the handler, exactly as on `main`. The default constraint-handler prerequisite is fulfilled on current main `b11c7bf84f5d2f636ff60166cc3371bd071e24d5`: #9935 was closed after its changes landed through #10270. Merging its final head into this main produces the unchanged main tree. Earlier test and title measurements retain their recorded source pins. **Overlap:** ntamas94's #10409 (36-byte `struct tm`, `tm_isdst = -1` from `gmtime_s`) also edits these two functions. It currently conflicts with `main` on its own; the change here is the four-line null check at the top of each function. ### Tested **Current-main refresh — 2026-11-02.** Published head `23fcc2d857f40e9c7f2b0cbc451c89d574bddb30` includes pinned main `a4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8`. The time-conversion constraint implementation and null argument fixtures are retained with main’s Time.cpp updates and timelocal debt. Quiet null returns of ordinary gmtime/localtime and _r forms remain the documented host compatibility behavior, not a new title-library assertion. Rebuilt actual libc/Ampr and selected tests from the final input tree. `guest_crt`, `guest_time_conversion`, `guest_time_extras`, `guest_mktime`, `guest_strftime`, `guest_locale` each pass three consecutive runs: **18 runtime passes, zero skips**, plus public_headers. Conventions and whitespace pass. Source/input manifests, exact dependencies, library/test hashes, logs/JUnit and head-leased push receipts are attached. The same pinned main passes 18 selected groups three times (54 runtime passes) plus headers. All 34 recursive dependency trees/31,551 tracked blobs verify with zero errors; QuickJS’s update=none test262 corpus is explicitly excluded because it is not a library build input. Linux-7.2.11-arch1-1-x86_64-with-glibc2.44, Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON. Focused Linux CPU qualification only. Older Windows/module/reference reports retain their original pins; no fresh Windows/macOS, SDK-module, console, title/GPU or speed claim. AI-assisted: yes (Codex). Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-six-main-a4eca-linux-extra10321-20261102.tar.gz; SHA-256 525ccb31a5d3d0f5adced5fda58818d8432f06c0afa7290139adb60ae6664b54. Earlier dated qualifications below retain their recorded source/platform pins. Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, `main` `319621ba2e`. - **Native comparison:** a scratch loader calls the titles' functions (PPSA07923) by NID next to this build's, with the same recording handler on both sides. - `gmtime_s`: 50,000 random calls with times across the 64-bit range and null arguments. Calls with a null argument differ in 4,960 cases on `main` and **0** with this PR. - The other differences don't involve null arguments, and this PR leaves them unchanged: - 9,549 calls inside 1901 to 2099 differ only in `tm_isdst` (titles -1, `main` 0), which #10409 addresses; - 35,491 calls outside that range differ because the titles' `gmtime_s` gets those dates wrong (it returns wrapped years even for `INT64_MAX`), which this build does not copy. - `localtime_s`: the titles report `"localtime_s: bad argument"` for a null `timer`, a null `result` and both. Its non-null path needs a time-zone import the loader doesn't provide, so it isn't compared. - **Non-`_s` functions:** the titles' `gmtime`, `localtime`, `gmtime_r` and `localtime_r` don't check for null at all. Each of the eight null cases (one for each single-argument function, three for each `_r` function) ends in an access violation on PPSA07923. Returning null without a handler call for these four is therefore `main`'s existing, safer behaviour, which this PR keeps, not a titles rule. - **Regression:** `guest_time_conversion` gains `CheckNullArguments`, which covers each null combination for both functions and a valid call with no handler call. With the previous `Time.cpp` it fails (`null argument did not report a bad argument`). - **Test set:** `guest_time_conversion`, `guest_time_extras`, `guest_mktime`, `guest_strftime`, `guest_crt` and `guest_locale` pass 3/3. Conventions 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#9935 fulfilled through merged #10270) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
rooklz commented 2026-10-28 22:39:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T22:39:48Z.

Review: COMMENTED


I ran this on macOS x86_64 (Apple clang, ASan+UBSan, -fno-sanitize-recover), at head 704acff4af. It's a standalone build of Time.cpp, TimeExtras.cpp, Environment.cpp and Multibyte.cpp with the target's -include HostStandardStreams.hpp, plus a two-function shim: __error_nid_postfix returns &errno, and set_constraint_handler_s_nid_postfix calls SonySetConstraintHandlerS.

  • guest_time_conversion and guest_time_extras pass at head (3/3 runs).
  • If I swap in main's Time.cpp, guest_time_conversion fails with null argument did not report a bad argument, as your body says.
  • git merge-tree against main 319621ba2e is clean, and conventions pass.
  • Every internal caller of gmtime_s/localtime_s (tzset, mktime, timegm, SonyCtimeS) passes non-null pointers, so the new handler call can't fire from inside the library.

Mutants. I ran 12 against the two tests. 5 are killed: main's Time.cpp, gmtime_s reporting only a null timer, a message of "gmtime_s", ERANGE for EINVAL, and result passed as the handler pointer. These 7 survive:

Mutant Survives because
gmtime_s, both null → silent nullptr (null, null) is never exercised; which == 2 repeats which == 0, as oneandonlydean noted
localtime_s, both null → silent nullptr same
libc_gmtime / libc_localtime without the new timer == nullptr guard no test passes a null timer to the non-_s functions with a recording handler installed
gmtime_r / localtime_r guarding only result same

(If gmtime_r loses its guard completely, guest_time_extras catches it, because its null-result call aborts in the default handler.)

The body says gmtime, localtime, gmtime_r and localtime_r "keep returning null for a null argument without calling the handler". That is half of this PR, but no test pins it. Here is a test-only diff. It includes oneandonlydean's (null, null) fix, and it checks that the four non-_s entry points (plus libc_gmtime/libc_localtime) return null for every null combination while the recording handler is installed, without calling it:

@@ -51,13 +51,22 @@ static void CheckNullArguments() {
         const char* expected = convert == gmtime_s_nid_postfix ? "gmtime_s: bad argument" : "localtime_s: bad argument";
         for (int which = 0; which < 3; ++which) {
             g_constraintCalls = 0;
-            Require(convert(which == 1 ? nullptr : &timer, which == 0 ? nullptr : which == 1 ? &result : nullptr) == nullptr,
+            Require(convert(which == 0 ? &timer : nullptr, which == 1 ? &result : nullptr) == nullptr,
                 "null argument was accepted");
             Require(g_constraintCalls == 1 && g_constraintMessage == expected, "null argument did not report a bad argument");
         }
         g_constraintCalls = 0;
         Require(convert(&timer, &result) == &result && g_constraintCalls == 0, "valid conversion reported a violation");
     }
+    g_constraintCalls = 0;
+    for (const auto convert : {libc_gmtime_nid_postfix, gmtime_nid_postfix, libc_localtime_nid_postfix, localtime_nid_postfix}) {
+        Require(convert(nullptr) == nullptr, "null timer was accepted");
+    }
+    for (const auto convert : {gmtime_r_nid_postfix, localtime_r_nid_postfix}) {
+        Require(convert(nullptr, &result) == nullptr && convert(&timer, nullptr) == nullptr && convert(nullptr, nullptr) == nullptr,
+            "null argument was accepted");
+    }
+    Require(g_constraintCalls == 0, "non-_s conversion reported a violation");
     set_constraint_handler_s_nid_postfix(previous);
 }

With this diff all 12 mutants are killed, and head stays green (3/3 under ASan+UBSan).

One question about provenance. Your native comparison covers gmtime_s and localtime_s. Did the scratch loader also call the titles' gmtime/localtime/gmtime_r/localtime_r with a null argument and a recording handler installed? If it did, a line in Tested would back up the new assertions. If it didn't, the non-_s side is main's behaviour kept unchanged, which is fine, but the body could say so.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5690641902 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12593#pullrequestreview-5690641902). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T22:39:48Z. **Review:** COMMENTED --- I ran this on **macOS x86_64** (Apple clang, ASan+UBSan, `-fno-sanitize-recover`), at head `704acff4af`. It's a standalone build of `Time.cpp`, `TimeExtras.cpp`, `Environment.cpp` and `Multibyte.cpp` with the target's `-include HostStandardStreams.hpp`, plus a two-function shim: `__error_nid_postfix` returns `&errno`, and `set_constraint_handler_s_nid_postfix` calls `SonySetConstraintHandlerS`. - `guest_time_conversion` and `guest_time_extras` pass at head (3/3 runs). - If I swap in `main`'s `Time.cpp`, `guest_time_conversion` fails with `null argument did not report a bad argument`, as your body says. - `git merge-tree` against `main` `319621ba2e` is clean, and conventions pass. - Every internal caller of `gmtime_s`/`localtime_s` (`tzset`, `mktime`, `timegm`, `SonyCtimeS`) passes non-null pointers, so the new handler call can't fire from inside the library. **Mutants.** I ran 12 against the two tests. 5 are killed: `main`'s `Time.cpp`, `gmtime_s` reporting only a null `timer`, a message of `"gmtime_s"`, `ERANGE` for `EINVAL`, and `result` passed as the handler pointer. These 7 survive: | Mutant | Survives because | |---|---| | `gmtime_s`, both null → silent `nullptr` | (null, null) is never exercised; `which == 2` repeats `which == 0`, as oneandonlydean noted | | `localtime_s`, both null → silent `nullptr` | same | | `libc_gmtime` / `libc_localtime` without the new `timer == nullptr` guard | no test passes a null `timer` to the non-`_s` functions with a recording handler installed | | `gmtime_r` / `localtime_r` guarding only `result` | same | (If `gmtime_r` loses its guard completely, `guest_time_extras` catches it, because its null-`result` call aborts in the default handler.) The body says `gmtime`, `localtime`, `gmtime_r` and `localtime_r` "keep returning null for a null argument without calling the handler". That is half of this PR, but no test pins it. Here is a test-only diff. It includes oneandonlydean's (null, null) fix, and it checks that the four non-`_s` entry points (plus `libc_gmtime`/`libc_localtime`) return null for every null combination while the recording handler is installed, without calling it: ```diff @@ -51,13 +51,22 @@ static void CheckNullArguments() { const char* expected = convert == gmtime_s_nid_postfix ? "gmtime_s: bad argument" : "localtime_s: bad argument"; for (int which = 0; which < 3; ++which) { g_constraintCalls = 0; - Require(convert(which == 1 ? nullptr : &timer, which == 0 ? nullptr : which == 1 ? &result : nullptr) == nullptr, + Require(convert(which == 0 ? &timer : nullptr, which == 1 ? &result : nullptr) == nullptr, "null argument was accepted"); Require(g_constraintCalls == 1 && g_constraintMessage == expected, "null argument did not report a bad argument"); } g_constraintCalls = 0; Require(convert(&timer, &result) == &result && g_constraintCalls == 0, "valid conversion reported a violation"); } + g_constraintCalls = 0; + for (const auto convert : {libc_gmtime_nid_postfix, gmtime_nid_postfix, libc_localtime_nid_postfix, localtime_nid_postfix}) { + Require(convert(nullptr) == nullptr, "null timer was accepted"); + } + for (const auto convert : {gmtime_r_nid_postfix, localtime_r_nid_postfix}) { + Require(convert(nullptr, &result) == nullptr && convert(&timer, nullptr) == nullptr && convert(nullptr, nullptr) == nullptr, + "null argument was accepted"); + } + Require(g_constraintCalls == 0, "non-_s conversion reported a violation"); set_constraint_handler_s_nid_postfix(previous); } ``` With this diff all 12 mutants are killed, and head stays green (3/3 under ASan+UBSan). One question about provenance. Your native comparison covers `gmtime_s` and `localtime_s`. Did the scratch loader also call the titles' `gmtime`/`localtime`/`gmtime_r`/`localtime_r` with a null argument and a recording handler installed? If it did, a line in **Tested** would back up the new assertions. If it didn't, the non-`_s` side is `main`'s behaviour kept unchanged, which is fine, but the body could say so.
jotitadev commented 2026-10-30 15:38:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-30T15:38:50Z.

Review: COMMENTED


First Linux run of this head (the thread has Windows and a standalone macOS build).

Ubuntu 26.04, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON. Head b8ea6ed9cc merged (--no-commit) onto current main ed1e0ed08f; the merge is clean.

  • guest_time_conversion: passes 3/3.
  • guest_time_*, guest_mktime, guest_strftime, guest_crt and guest_locale: 10/10 pass.
  • public_headers: passes.
  • Control: with Time.cpp and TimeExtras.cpp from main and this PR's test kept, guest_time_conversion aborts 3/3 at null argument did not report a bad argument.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5714729462 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12593#pullrequestreview-5714729462). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-30T15:38:50Z. **Review:** COMMENTED --- First Linux run of this head (the thread has Windows and a standalone macOS build). Ubuntu 26.04, i7-8700, GCC 15.2, Release, `BUILD_TESTING=ON`. Head `b8ea6ed9cc` merged (`--no-commit`) onto current `main` `ed1e0ed08f`; the merge is clean. - `guest_time_conversion`: passes 3/3. - `guest_time_*`, `guest_mktime`, `guest_strftime`, `guest_crt` and `guest_locale`: 10/10 pass. - `public_headers`: passes. - Control: with `Time.cpp` and `TimeExtras.cpp` from `main` and this PR's test kept, `guest_time_conversion` aborts 3/3 at `null argument did not report a bad argument`. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!12593
No description provided.