Skip to content

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

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
JJenkx:windows/libc-strerror-s-constraints
Oct 30, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
JJenkx:windows/libc-strerror-s-constraints

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

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:

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 + fix(libc): name the extended errno values in strerror as titles' libc.prx does #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

@rooklz rooklz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 7081 core/libs/prx/libc/src/WideConversion.cpp
docs/dev/debt/libc.md
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9863 docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10620 docs/dev/debt/libc.md
PR 10729 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11661 docs/dev/debt/libc.md
PR 11689 docs/dev/debt/libc.md
PR 11698 docs/dev/debt/libc.md
PR 11831 docs/dev/debt/libc.md
PR 12057 docs/dev/debt/libc.md
PR 12560 docs/dev/debt/libc.md
PR 12605 docs/dev/debt/libc.md

@JJenkx

JJenkx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

@rooklz thanks, applied as 4c9ab04824 (test-only, on 2fc5a44966): your hunk, unchanged.

On Windows (WinLibs GCC 15.2.0 UCRT r7, Release):

  • guest_safe_errors, guest_errors, guest_crt and guest_bounds_checking pass 3/3 at the new head.
  • Both survivors now fail at largest valid capacity: limit >> 3 → >> 4, and capacity > → capacity >=.
  • Conventions pass.

AI-assisted: yes (Claude Code).

@babyhuey

Copy link
Copy Markdown
Contributor

Windows run of this head 4c9ab048240d, merged onto main 3a2b9198165 (clean merge, local only), with the selection CI's skipped Windows product job uses: Windows 11, Ryzen 7 5800X, RTX 3080, WinLibs GCC 15.2, Release, BUILD_TESTING=ON. The tree was configured fresh after the merge, then select_tests.py select / ninja -f select.ninja selected / --target libs / select_tests.py ctest --parallel 4 --timeout 120.

AI-assisted: yes (Claude Code)

@JJenkx

JJenkx commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

@babyhuey thanks for the run. The guest_freebsd_clocks failure you saw on main is fixed by #12660 (rooklz approved it): its KernelTimezoneIsUtc check assumes a host set to UTC.

AI-assisted: yes (Claude Code).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 29, 2026
JJenkx and others added 2 commits October 29, 2026 23:16
The libc.prx that titles ship (PPSA07923, PPSA05916) rejects a
strerror_s capacity above SIZE_MAX >> 3, not SIZE_MAX >> 1, and calls
the constraint handler with "strerror_s: bad argument", EINVAL and a
null pointer for a null destination, a capacity of 0 or one past that
limit. main passed "strerror_s" and the destination, and wrote the
message for a capacity from 2^61 up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The capacity checks only rejected (SIZE_MAX >> 3) + 1, so a tighter
limit or a >= comparison still passed. A call with capacity
SIZE_MAX >> 3 now has to succeed without a handler call. Suggested by
rooklz in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the windows/libc-strerror-s-constraints branch from 4c9ab04 to 2cbccfc Compare October 29, 2026 22:23
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 29, 2026
@adriadam10
adriadam10 merged commit b3773d1 into boykopovar:main Oct 30, 2026
17 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff against the description and the test: strerror_s now passes "strerror_s: bad argument" and a null pointer to the handler and uses the SIZE_MAX >> 3 limit; the new test cases fail on main's WideConversion.cpp, and the largest-valid-capacity case kills the two mutants @rooklz found. The only conflict was docs/dev/debt/libc.md after #12527; I rebased and resolved it by keeping #12527's entries plus this PR's change to the strerror_s and strerrorlen_s entry; merged.

oneandonlydean pushed a commit that referenced this pull request Nov 4, 2026
#12589 passes "strerror_s: bad argument" to the constraint handler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants