Repository navigation
fix(libc): report strerror_s violations as titles' libc.prx does - #12589
Conversation
rooklz
left a comment
There was a problem hiding this comment.
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'sWideConversion.cppwith this PR's test: fails atconstraint 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_conventionsagainst319621ba2epasses.git merge-treeagainst currentmainis clean.- The only other open PRs that touch
strerror_sare #9935, #10270 and #12527, which the merge notes already cover.
AI-assisted: yes
|
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.
|
|
@rooklz thanks, applied as On Windows (WinLibs GCC 15.2.0 UCRT r7, Release):
AI-assisted: yes (Claude Code). |
|
Windows run of this head
AI-assisted: yes (Claude Code) |
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>
4c9ab04 to
2cbccfc
Compare
|
Checked the diff against the description and the test: |
#12589 passes "strerror_s: bad argument" to the constraint handler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
What
strerror_snow reports its runtime-constraint violations the way thelibc.prxthat titles ship does. Measured natively on PPSA07923 and PPSA05916:main"strerror_s: bad argument""strerror_s"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_salready called the handler onmain, so unlike #12574 and #12581 this does not wait for #9935.Merge notes:
"strerror_s\nstrerror_s\n"tostderr. 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 fix(libc): write the constraint message and return from the default handler #9935 (7aa063d91c) passesguest_safe_errors,guest_crtandguest_bounds_checking3/3.strerrornames),docs/dev/debt/libc.mdconflicts on neighbouring lines. The resolution is fix(libc): name the extended errno values in strerror as titles' libc.prx does #12527's file with this PR's change to thestrerror_s and strerrorlen_sentry.Tested
Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release,
main319621ba2e.Native comparison: a scratch loader calls the titles'
strerror_s,strerrorlen_s,strnlen_sandwcsnlen_sby NID next to this build's, with the same recording handler on both sides. For each it makes 20,000 random calls:2^61 - 1,2^61andSIZE_MAX;Each call compares the result, the handler's calls, error, pointer and message, and the buffer bytes.
mainstrerror_sviolationsstrerror_ssuccessful calls (extended names 108, 152, 1074)strerrorlen_s(same names)strnlen_s,wcsnlen_sBoth titles give the same results.
Regression:
guest_safe_errorsnow expects the titles' message, a null pointer, and rejection from(SIZE_MAX >> 3) + 1. With the previousWideConversion.cppit fails (constraint callback message and ABI).Test set:
guest_safe_errors,guest_crt,guest_bounds_checkingandguest_errorspass 3/3. Conventions pass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)