fix(libc): bound and report the Annex K copies as measured on a PS5 #10270
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10270"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-23T16:09:21Z.
What
memcpy_s,memmove_s,strcpy_s,strncpy_s,strcat_s,strncat_sand their wide forms (wmemcpy_s,wmemmove_s,wcscpy_s,wcsncpy_s,wcscat_s,wcsncat_s) measured on a retail PS5 (firmware 13.60) throughlibSceLibcInternal.sprxfrom a payload process. The sources and console logs are in this gist (psafestrtopsafestr4).The console, for all twelve:
char16_t, not bytes); a size or count above it isEINVAL(22), notERANGE;"<function>: bad destination argument"for a null destination or a size of 0 or above RSIZE_MAX (nothing is written),"bad argument"for a null source, an oversized count or an unterminatedstrcat_sdestination,"buffer too small"(ERANGE) and"buffer overlap"(EINVAL);dest[0]for the string forms and the whole destination for the memory forms after a source, count, size or overlap violation;strcat_s/strncat_soverlap only against the characters it reads and the bytes it writes after the existing string, sostrncat_s(d, 8, d, 2)on"ab"gives"abab";errnoalone.strncat_sandwcsncat_sonly (not run on the console, read from the PPSA31872libc.prxcode after JJenkx's native comparison of titles'libc.prx): a destination with no terminator withindestszis"buffer too small"(ERANGE) withdest[0]cleared, and thestrncat_soverlap check leaves the terminators out of both ranges, aswcsncat_sdoes;strcat_sandwcscat_skeep the measuredEINVALandstrcat_skeeps counting the terminators.AnyPS5 used SIZE_MAX >> 1, returned
ERANGEfor an oversized size, passed the destination to the handler with the bare function name, and never called the handler from the wide forms. The byte and wide forms now share one template inSonyCrt.hpp, whichwcscpy_sin libSceAmpr also uses, and the debt entry is removed.wcsncat_sandwcscat_skeep the overlap rule #9290 read from the console'swcsncat_s, which leaves the terminators out of both ranges, since the wide concatenations' overlap was not run on the console (psafestr4runs their other cases); its debt entry now says the narrow forms count them, as measured, and thatwcscat_s(d, 8, d + 1), which the narrow form refuses, is unmeasured, so no test requires its result.This branch carries #9935, since the tests install and restore handlers around the violations and the default handler on
mainaborts.Tested
0bc7597aed(unterminatedstrncat_s/wcsncat_sdestination andstrncat_soverlap from the titles'libc.prx):guest_strings,guest_wide_strings,guest_safe_errors,guest_crtandguest_bounds_checkingpass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); the newguest_stringscases and the changedguest_wide_stringscase fail onee3d2ffb81;check_conventionspasses.main(f80ce6cec1, with #11121):ee3d2ffb81. The only conflict wasdocs/dev/debt/libc.md: kept main'slocaleconventry (and its removal of thesetlocaleentry) and this PR'sset_constraint_handler_swording.guest_strings,guest_wide_stringsandguest_safe_errorspass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC);check_conventionspasses.main(f26434afa3, with #10706):eb9f9bfcda. The tests take their declarations fromlibc/include/Api.hpp(wcscpy_sis declared there withuint16_t, soguest_wide_stringscalls it through a small cast helper);SonyCrt.hppincludesApi.hppinstead of declaringfputs/fputcagain, as in #9935.guest_strings,guest_wide_strings,guest_safe_errors,guest_crt,guest_bounds_checkingandguest_qsortpass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC);public_headersaudit andcheck_conventionspass.main(8c8b998620, with #12845):ec13bce3d8. The only conflict wasdocs/dev/debt/libc.md: kept main'sqsort and qsort_rentry from #12845, and this PR's removal of the Annex K entry and itswcsncat_s and wcscat_swording.guest_strings,guest_wide_strings,guest_safe_errors,guest_crtandguest_bounds_checkingpass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC);check_conventionspasses.main(a2c36d4ed6, with #12589) and #9935 (9260172a4c):061d56633c.guest_safe_errorsnow expects the default handler to writestrerror_s: bad argument\ntwice, the message #12589 passes.guest_strings,guest_wide_strings,guest_safe_errors,guest_crtandguest_bounds_checkingpass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC);check_conventionspasses.Windows 11, MinGW (winlibs).
guest_stringschecks the new limit, the error values, the handler's message and pointer and the overlap rules;guest_wide_stringschecks the wide limits,EINVALfor an unterminatedwcsncat_s, the oversized counts and the overlaps. Both pass three runs in a row.-E "^agc_|^select_tests$") passes apart fromguest_kqueue, which failed once and passed on its three reruns.tools/check_conventions.pypasses.tools/pr_overlap.py: #9087, #9290, #9688 touch nearby lines; whichever lands second keeps both.main(ba8dcc6ae8, with #13220, which removes the duplicate write-watch test names that failed the macOS jobs) and #9935 (d7b7f9ad59):cfa07b700e.guest_strings,guest_wide_stringsandguest_safe_errorspass three runs in a row on Windows (MinGW, GCC 15.2).tools/check_conventions.pypasses.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)Strings.cpp,GuestSafeErrors.cpp,GuestStrings.cppandGuestWideStrings.cpp(merge order only); #11621 (B-Howell) addslibc.mdentries next to the ones this PR edits, and #11664 (B-Howell) appends itsstrmodechecks at the same place inGuestStrings.cpp(merge order only); #12594 (JJenkx) moves thememset_schecks next to thestrcpy_sline this PR changes inGuestStrings.cpp, and #12574 (JJenkx) edits theset_constraint_handler_sentry this branch rewrites (merge order only); #12627 (JJenkx) rewrites thesnwprintf_sandswprintf_sentries next to thewcsncat_s and wcscat_sentry this PR edits inlibc.md(merge order only); #12598 (JJenkx) rewrites theqsort and qsort_rentry right above this PR'swcsncat_s and wcscat_sentry inlibc.md, and also edits theqsort_sentry next to theset_constraint_handler_sone this branch rewrites (merge order only); #9688 has mergedImported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T05:19:49Z.
Review: COMMENTED
Checked against main
7966e590ef, C11 Annex K (K.3.7.1.1-K.3.7.2.4) and the psafestr–psafestr4 logs and sources in your gist:char16_tcounts, andstrncat_s(d, 8, d, 2)giving "abab". The Annex K rules for clearing dest and calling the handler hold throughout, and it lines these functions up with what BoundsChecking.cpp on main already does (rsizeMax,"fn: reason"with a null pointer). It merges cleanly onto current main.wcscat_s(strBuf, 8, strBuf + 1) == 0with "abb", butstrcat_s(d, 8, d + 1)on "ab" is EINVAL in psafestr4 (cat_overlap). #9290's rule came from a static reading ofwcsncat_sonly, andwcscat_s's console code wasn't read. psafestr4.c already resolveswcscat_sandwcsncat_s, so could you addwcat_overlap/wncat_overlapcases? If that can't be done now, could therequirego, or the debt line say this case is unmeasured?wcscat_sandwcsncat_s(unterminated, short, null src, dmax, count); only their overlap wasn't run, so "their overlap was not run on a console" would be accurate. The same applies to the description's "the wide concatenations were not run".Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:30:27Z.
Review: APPROVED
Reviewed current head
4e579c7ac5. The implementation consolidates the Annex K byte/wide string and memory helpers while keeping the measured byte-versus-wide overlap distinction explicit. The unsupported wide-overlap assertion was removed, and the debt entry now states what was and was not measured. The default-handler test redirects guest stderr and verifies the exact message and newline; the handler-call tests check codes, arguments and destination clearing. I ran diff whitespace and repository convention checks on this head; both pass. The PR reports three consecutive focused runs on Linux and Windows, while hosted product jobs are skipped. The declared integration order for overlapping PRs remains applicable before merge. I found no additional blocking correctness issue.