fix(libc): report strerror_s violations as titles' libc.prx does #12589

Merged
JJenkx merged 2 commits from upstream-pr/12589 into main 2026-10-30 09:19:26 +00:00
JJenkx commented 2026-10-28 21:45:41 +00:00 (Migrated from github.com)

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


What

strerror_s now reports its runtime-constraint violations the way the libc.prx that titles ship does. Measured natively on PPSA07923 and PPSA05916:

titles main
handler message "strerror_s: bad argument" "strerror_s"
handler pointer argument null the destination
capacity limit SIZE_MAX >> 3 (2^61 and above is a violation) SIZE_MAX >> 1 (from 2^61 up the message was written)

Unchanged, and already matching the titles: a null destination or a capacity of 0 is a violation, the destination is left untouched, and the error is EINVAL. A short buffer is still truncated with "..." and returns 0. The debt entry said these violation details were "not measured"; it now states the measured rule.

strerror_s already called the handler on main, so unlike #12574 and #12581 this does not wait for #9935.

Merge notes:

  • #9935 and #10270 (both by @orezraey) add a check that the default handler writes "strerror_s\nstrerror_s\n" to stderr. Whichever lands second must change that expected text to "strerror_s: bad argument\nstrerror_s: bad argument\n". With that change, a local merge with #9935 (7aa063d91c) passes guest_safe_errors, guest_crt and guest_bounds_checking 3/3.
  • With my #12527 (extended strerror names), docs/dev/debt/libc.md conflicts on neighbouring lines. The resolution is #12527's file with this PR's change to the strerror_s and strerrorlen_s entry.

Tested

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

  • Native comparison: a scratch loader calls the titles' strerror_s, strerrorlen_s, strnlen_s and wcsnlen_s by NID next to this build's, with the same recording handler on both sides. For each it makes 20,000 random calls:

    • error numbers from -5 to 1199;
    • sizes from 0 to 129, 2^61 - 1, 2^61 and SIZE_MAX;
    • null buffers.

    Each call compares the result, the handler's calls, error, pointer and message, and the buffer bytes.

    20,000 calls each main this PR this PR + #12527
    strerror_s violations 3,741 differ 0 0
    strerror_s successful calls (extended names 108, 152, 1074) 202 differ 202 0
    strerrorlen_s (same names) 133 differ 133 0
    strnlen_s, wcsnlen_s 0 0 0

    Both titles give the same results.

  • Regression: guest_safe_errors now expects the titles' message, a null pointer, and rejection from (SIZE_MAX >> 3) + 1. With the previous WideConversion.cpp it fails (constraint callback message and ABI).

  • Test set: guest_safe_errors, guest_crt, guest_bounds_checking and guest_errors 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 (merge notes above for #9935, #10270 and #12527)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5058193107 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12589). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T21:45:41Z. --- ### What `strerror_s` now reports its runtime-constraint violations the way the `libc.prx` that titles ship does. Measured natively on PPSA07923 and PPSA05916: | | titles | `main` | |---|---|---| | handler message | `"strerror_s: bad argument"` | `"strerror_s"` | | handler pointer argument | null | the destination | | capacity limit | `SIZE_MAX >> 3` (2^61 and above is a violation) | `SIZE_MAX >> 1` (from 2^61 up the message was written) | Unchanged, and already matching the titles: a null destination or a capacity of 0 is a violation, the destination is left untouched, and the error is `EINVAL`. A short buffer is still truncated with `"..."` and returns 0. The debt entry said these violation details were "not measured"; it now states the measured rule. `strerror_s` already called the handler on `main`, so unlike #12574 and #12581 this does not wait for #9935. **Merge notes:** - #9935 and #10270 (both by @orezraey) add a check that the default handler writes `"strerror_s\nstrerror_s\n"` to `stderr`. Whichever lands second must change that expected text to `"strerror_s: bad argument\nstrerror_s: bad argument\n"`. With that change, a local merge with #9935 (`7aa063d91c`) passes `guest_safe_errors`, `guest_crt` and `guest_bounds_checking` 3/3. - With my #12527 (extended `strerror` names), `docs/dev/debt/libc.md` conflicts on neighbouring lines. The resolution is #12527's file with this PR's change to the `strerror_s and strerrorlen_s` entry. ### Tested Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, `main` `319621ba2e`. - **Native comparison:** a scratch loader calls the titles' `strerror_s`, `strerrorlen_s`, `strnlen_s` and `wcsnlen_s` by NID next to this build's, with the same recording handler on both sides. For each it makes 20,000 random calls: - error numbers from -5 to 1199; - sizes from 0 to 129, `2^61 - 1`, `2^61` and `SIZE_MAX`; - null buffers. Each call compares the result, the handler's calls, error, pointer and message, and the buffer bytes. | 20,000 calls each | `main` | this PR | this PR + #12527 | |---|---|---|---| | `strerror_s` violations | 3,741 differ | **0** | **0** | | `strerror_s` successful calls (extended names 108, 152, 1074) | 202 differ | 202 | **0** | | `strerrorlen_s` (same names) | 133 differ | 133 | **0** | | `strnlen_s`, `wcsnlen_s` | 0 | 0 | 0 | Both titles give the same results. - **Regression:** `guest_safe_errors` now expects the titles' message, a null pointer, and rejection from `(SIZE_MAX >> 3) + 1`. With the previous `WideConversion.cpp` it fails (`constraint callback message and ABI`). - **Test set:** `guest_safe_errors`, `guest_crt`, `guest_bounds_checking` and `guest_errors` 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 (merge notes above for #9935, #10270 and #12527) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
rooklz commented 2026-10-28 22:19:54 +00:00 (Migrated from github.com)

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

Review: COMMENTED


I checked this on macOS 26 (x86_64 under Rosetta, Apple clang, ASan+UBSan). I built WideConversion.cpp, Errors.cpp and GuestSafeErrors.cpp by themselves at head 2fc5a44966, with a three-symbol shim (_Stderr_nid_postfix on host stderr, __error, and an aborting wcrtomb).

  • head: safe error message checks passed, and the sanitizers report nothing.
  • main's WideConversion.cpp with this PR's test: fails at constraint callback message and ABI, which matches the Tested section.

Mutants of WideConversion.cpp, run against this PR's test:

mutant result
limit >> 3 → >> 1 (the old value) killed
handler pointer nullptr → non-null killed
guest-handler message → "strerror_s" killed
capacity == 0 check dropped killed
limit >> 3 → >> 4 survives
capacity > → capacity >= survives
default-handler message changed equivalent: SonyDefaultConstraintHandler ignores the message and aborts on main (it only becomes visible with #9935)

The test rejects (SIZE_MAX >> 3) + 1 but never accepts anything at or below the limit. A tighter limit therefore still passes, even though it contradicts the measured rule ("2^61 and above is a violation", so 2^61 − 1 is valid). This test-only diff covers the largest valid capacity (the copy stops at the message's terminator, so nothing past the 128-byte buffer is touched). It kills both survivors, and head stays green:

@@ -85,6 +85,12 @@ void CheckConstraints() {
         Require(violations == before + 1 && violationBuffer == nullptr, "one callback with a null pointer");
         for (const char value : buffer) Require(value == '!', "constraint failure leaves destination untouched");
     }
+    std::array<char, 128> wide;
+    wide.fill('!');
+    const int beforeLargest = violations;
+    Require(strerror_s_nid_postfix(wide.data(), maximum >> 3, 2) == 0, "largest valid capacity");
+    Require(violations == beforeLargest, "largest valid capacity is not a violation");
+    Require(std::strcmp(wide.data(), "No such file or directory") == 0, "largest valid capacity writes the message");
     for (const auto capacity : {std::size_t{0}, std::size_t{1}, maximum}) {

With it, >> 4 and >= both fail at largest valid capacity.

Other checks:

  • check_conventions against 319621ba2e passes.
  • git merge-tree against current main is clean.
  • The only other open PRs that touch strerror_s are #9935, #10270 and #12527, which the merge notes already cover.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5690445109 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12589#pullrequestreview-5690445109). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T22:19:54Z. **Review:** COMMENTED --- I checked this on macOS 26 (x86_64 under Rosetta, Apple clang, ASan+UBSan). I built `WideConversion.cpp`, `Errors.cpp` and `GuestSafeErrors.cpp` by themselves at head `2fc5a44966`, with a three-symbol shim (`_Stderr_nid_postfix` on host `stderr`, `__error`, and an aborting `wcrtomb`). - **head:** `safe error message checks passed`, and the sanitizers report nothing. - **`main`'s `WideConversion.cpp` with this PR's test:** fails at `constraint callback message and ABI`, which matches the Tested section. **Mutants of `WideConversion.cpp`, run against this PR's test:** | mutant | result | |---|---| | limit `>> 3` → `>> 1` (the old value) | killed | | handler pointer `nullptr` → non-null | killed | | guest-handler message → `"strerror_s"` | killed | | `capacity == 0` check dropped | killed | | limit `>> 3` → `>> 4` | **survives** | | `capacity >` → `capacity >=` | **survives** | | default-handler message changed | equivalent: `SonyDefaultConstraintHandler` ignores the message and aborts on `main` (it only becomes visible with #9935) | The test rejects `(SIZE_MAX >> 3) + 1` but never accepts anything at or below the limit. A tighter limit therefore still passes, even though it contradicts the measured rule ("2^61 and above is a violation", so 2^61 − 1 is valid). This test-only diff covers the largest valid capacity (the copy stops at the message's terminator, so nothing past the 128-byte buffer is touched). It kills both survivors, and head stays green: ```diff @@ -85,6 +85,12 @@ void CheckConstraints() { Require(violations == before + 1 && violationBuffer == nullptr, "one callback with a null pointer"); for (const char value : buffer) Require(value == '!', "constraint failure leaves destination untouched"); } + std::array<char, 128> wide; + wide.fill('!'); + const int beforeLargest = violations; + Require(strerror_s_nid_postfix(wide.data(), maximum >> 3, 2) == 0, "largest valid capacity"); + Require(violations == beforeLargest, "largest valid capacity is not a violation"); + Require(std::strcmp(wide.data(), "No such file or directory") == 0, "largest valid capacity writes the message"); for (const auto capacity : {std::size_t{0}, std::size_t{1}, maximum}) { ``` With it, `>> 4` and `>=` both fail at `largest valid capacity`. **Other checks:** - `check_conventions` against `319621ba2e` passes. - `git merge-tree` against current `main` is clean. - The only other open PRs that touch `strerror_s` are #9935, #10270 and #12527, which the merge notes already cover. AI-assisted: yes
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!12589
No description provided.