fix(libc): check size limits and null terminate on wcscpy_s constraint violations #7952
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7952"
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 emirplus on 2026-10-19T15:57:31Z.
What
Enforce C11 Annex K destination size constraints and truncation handling in SonyWcscpyS (wcscpy_s):
Tested
python tools/check_conventions.py --base origin/main passed. Tested wide string test suite declarations in GuestWideStrings.cpp.
Checklist
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T16:02:37Z.
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.
core/libs/tests/GuestWideStrings.cppcore/libs/prx/libc/include/SonyCrt.hppcore/libs/tests/GuestWideStrings.cppImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T04:02:46Z.
Thanks, the size, null and overlap constraints look right and the new cases cover them. One behaviour change isn't mentioned: on
main,SonyWcscpyScallsSonyInvokeConstraintHandler("wcscpy_s", dest, ...)on both violation paths, so a handler installed withset_constraint_handler_sruns forwcscpy_s. This change removes those calls, while the updated debt entry says violations return without invoking the handler "as before", which is not true forwcscpy_s. Could you keep the handler call on each violation path (with 22 or 34), as Annex K expects, or drop the "as before" and say thatwcscpy_sstops calling it?Small one: the source length scan is unbounded now (
while (src[length] != 0)); bounding it bysizegives the same ERANGE without reading past the destination size on an unterminated source.Imported from boykopovar/AnyPS5. Originally posted by Athen2045 on 2026-10-20T07:54:24Z.
Windows run of f2d9584d2 merged onto
maine9cc4a8ef(Windows 11 26300, WinLibs GCC 15.2.0 r7, Release,BUILD_TESTING=ON):Build: clean, with no warnings in
SonyCrt.hpporGuestWideStrings.cpp.guest_wide_strings(the newwcscpy_scases),guest_safe_errors,guest_wide_conversion,guest_wide_formattingand theguest_ampr_*tests pass.guest_crtfails (3 of 3 runs):guest_crt: line 172: g_handlerCalls == 1. It passes onmainon the same machine.SonyWcscpySalso backssceAmprWcscpyS(libSceAmpr,6f5f-qx4ucA), and this existing test installs a counting handler withsceAmprSetConstraintHandlerSand expects one call onERANGE:guest_crtis registered on every platform incore/libs/CMakeLists.txt, so it should fail on Linux too.Handler calls, observed. This confirms @oneandonlydean's point. A small harness (not committed) loads the patched
libc.prx, installs a handler throughset_constraint_handler_s(ENLfKJEZTjE) and callswcscpy_s(6f5f-qx4ucA). The return values match on both builds; only the handler calls differ:maindest("wcscpy_s", 22)size0("wcscpy_s", 22)src("wcscpy_s", 22)"wide"into size 4("wcscpy_s", 34)"wide"into size 16Keeping the
SonyInvokeConstraintHandlercall on each violation path, as the review suggests, should also makeguest_crtpass again.Full suite (
select_tests.py ctest ... --timeout 120): 1711 of 1717 pass. Besidesguest_crt, the same 5 tests fail onmainon this machine:conventions_env_reference,conventions_blob_reads,windows_import_modules,agc_driver_msaa_dcc_clear_4andagc_driver_pass_input_hazard.AI-assisted: yes (Claude Code). The results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T04:47:41Z.
Windows data point for
f2d9584d2merged ontomain05f999570(clean,git merge-treerc 0): Windows 10.0.19045, MinGW-w64 GCC 15.2.0, Release.ctest -N -R '^guest_wide_strings$'lists exactly that one test;ctest --no-tests=error -R '^guest_wide_strings$'was run three times per tree.core/libs/prx/libc/include/SonyCrt.hpprestored tomain(the new test cases kept): fails 3 of 3 (exit code0xc0000409, the test process aborts).Not tested: other platforms, a title run.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by emirplus on 2026-10-21T16:39:29Z.
@oneandonlydean @Athen2045 Updated: kept the SonyInvokeConstraintHandler invocation on every violation path (EINVAL and ERANGE), bounded the source scan by size, and reverted the debt entry modification so the handler behavior matches Annex K and passes guest_crt.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T00:50:42Z.
@emirplus thanks, the handler calls are back on every violation path and the source scan is now bounded, which covers both points.
One thing still blocks it: the new
wcscpy_srows inGuestWideStrings.cppnow abortguest_wide_strings. The first violation row (wcscpy_s(nullptr, 16, ...)) calls the registered handler, and with no handler installed that'sSonyDefaultConstraintHandler, which callsstd::abort()(SonyCrt.hpp:25-30). Built onmainwith this head's two files,guest_wide_stringsaborts inwcscpy_s_nid_postfix->SonyDefaultConstraintHandler, whileguest_crtandguest_stringspass.The fix is in the test: install a non-aborting handler around the new rows and put the previous one back afterwards, as
GuestCrt.cppdoes around itswcscpy_srows. For example:With those four lines added,
guest_wide_stringspasses here. A counting handler that checks one call per violation row would cover the handler behaviour too, but that's optional.A small one while you're there: with
size > RSIZE_MAXthe new code writesdest[0] = 0before returning 34. C11 K.3.9.2.1.1 only setss1[0]to null whens1maxis not greater thanRSIZE_MAX, and the siblingwcsncpy_sonmainreturns 34 without touching the destination (Strings.cpp:803). Please drop that write. AcpyBuf[0]check on the oversized row would pin it.