feat(libc): implement the remaining C11 Annex K bounds-checking functions #7841
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7841"
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 hypermoist on 2026-10-19T10:18:58Z.
What
Adds the eight C11 Annex K exports that libc was still missing, in a new
src/BoundsChecking.cpp:abort_handler_s,bsearch_s,wcstok_s,wctomb_s,wcrtomb_s,mbstowcs_s,mbsrtowcs_sandgets_s.Each one follows the console's own checks, messages and error codes (read from the console disassembly). Violations go through the existing
SonyInvokeConstraintHandleras(message, NULL, error), the same way the console's helper calls the handler:bsearch_s:"bsearch_s: bad argument"whennmemborsizeexceedsRSIZE_MAX(2^61 - 1), orkey/base/comparis null. A zeronmembreturns NULL without a violation. The search is FreeBSD'sbsearchloop, so the same element is returned among equal ones andcomparis called the same number of times.wcstok_s:"bad argument","missing token"(also for*s1max == 0) and"missing delimiter", with*s1maxdecremented exactly like the console's.wctomb_s,wcrtomb_s: argument checks per the console (including which failures clears[0]and set*retval = -1). A temporary buffer and the"buffer too small"/ERANGE violation apply only whenMB_CUR_MAX > smax. A character the locale can't encode returns EILSEQ without calling the handler.mbstowcs_s,mbsrtowcs_s: count whendstis null, convert up tomin(len, dstmax)characters, report"buffer overflow"/"buffer too small"(ERANGE,dst[0]cleared) whendstmaxfills, and update*srcthe way the console does (NULL after the terminator, the stop position otherwise, untouched when counting or whenlenis 0).gets_s:"zero buffer size","bad argument", and"short buffer"(ERANGE), after which the rest of the line is discarded ands[0]cleared. End of file before any character clearss[0]and returns NULL.abort_handler_s: writes the message and a newline to the gueststderr, then aborts.The multibyte functions convert through the existing
wcrtomb,wctombandmbrtowc, so they follow the C locale those implement. That locale is single-byte here, so a character above 0xFF isEILSEQinwctomb_s/wcrtomb_s, each byte is one wide character inmbstowcs_s/mbsrtowcs_s, and thewcrtomb_stoo-small path and theEILSEQpaths of the two string functions are not reached. Adocs/dev/debt/libc.mdentry records this; #8855 moves the locale to the console's UTF-8.Tested
Linux (CachyOS, GCC Release): new
guest_bounds_checkingtest (stdin is redirected to a scratch file forgets_s). The libc/guest tests pass, exceptguest_kernel_fcntl, which failed once underctest -j16and passes on its own; it's unrelated. All eight NIDs are in the patchedlibc.prx.tools/check_conventions.pypasses, andtools/pr_overlap.py --branchreports no line overlap.Differential test against the console: a retail
libc.prx(J03848509) was mapped into a Linux process with a recording handler in its constraint-handler slot.bsearch_s(300,000 random sorted arrays, keys and bad arguments) andwcstok_s(300,000 full tokenizing sequences with random strings, delimiter sets,s1maxvalues and bad arguments) matched this implementation exactly: return values,*s1max, the context pointer, buffer contents, the number of comparator calls, and every handler call's message, pointer and error. The multibyte functions andgets_sread locale state through TLS or call stdio on the console, so they couldn't run in the harness. They are ports of the disassembled control flow and are covered by the unit test.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T10:19:41Z.
Progress report (
05f9995→7b336be)📈 System libraries: 62.29% (+0.04%, +8 implemented functions)
✅ 8 implemented
abort_handler_s_nid_postfixbsearch_s_nid_postfixgets_s_nid_postfixmbsrtowcs_s_nid_postfixmbstowcs_s_nid_postfixwcrtomb_s_nid_postfixwcstok_s_nid_postfixwctomb_s_nid_postfixImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T10:26:57Z.
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/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-19T12:12:36Z.
Review: APPROVED
Reviewed the diff and test design:
BoundsChecking.cpp: Cleanly implements the remaining C11 Annex K bounds-checking interfaces (abort_handler_s,bsearch_s,gets_s,mbsrtowcs_s,mbstowcs_s,wcrtomb_s,wcstok_s,wctomb_s) respecting runtime-constraint handlers and bounds parameters.GuestBoundsChecking.cpp: Unit tests verify search comparators with context, wide-to-multibyte and multibyte-to-wide conversions with null/non-null states, tokenization with delimiter state, and temporary file reading with buffer bounds enforcement.configure_windows_unwindand DLL path prepending for Windows inCMakeLists.txt.Ready to merge.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by Athen2045 on 2026-10-20T06:00:24Z.
Windows run of
2c1f9672amerged ontomain3611cda29(Windows 11 26300, WinLibs GCC 15.2.0 r7, Release,BUILD_TESTING=ON):BoundsChecking.cpp.guest_bounds_checking: passes, 5 out of 5 repeated runs, including thegets_scases that read the scratch file through the guest__stdinp.abort_handler_sisn't called by the test, so I called it from a small harness (not committed). The harness loads the patchedlibc.prx, resolvesSmYrO79NzeIand calls it in a child process.("bsearch_s: bad argument", NULL, 22): the first stderr line is exactlybsearch_s: bad argument.("", NULL, 22): the first line is empty.FATAL: abort() (SIGABRT) ...), and the process exits with0xc0000409, the same as the otherabort()paths on Windows.select_tests.py ctest ... --timeout 120): 1709 of 1714 pass. 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.guest_kernel_fcntlpassed in the parallel run.AI-assisted: yes (Claude Code). The results above were run on this machine.
Edit: corrected the base commit from
585075cb7to3611cda29, themainthese runs were actually built on. The results are unchanged.Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T20:41:06Z.
The PR adds no
docs/dev/debt/libc.mdentry for a gap its description names.wctomb_s,wcrtomb_s,mbstowcs_sandmbsrtowcs_sconvert through this libc's single-byte C locale (___mb_cur_maxreturns 1). CONTRIBUTING.md asks for gaps like this (what differs from the console, what is unverified) to go in the debt file. The siblingwcsrtombs_sentry records the same gap. Ghost of Yotei importsmbstowcs_s(tdcAqgCS+uIin #8383), so a title already uses one of these functions.What this means at this head:
wctomb_s(&status, buf, 4, 0x263A)returns 86 withstatus == -1, and GuestBoundsChecking.cpp#L133 asserts it. libc.md#L93 records a PS5 measurement where the systemlibSceLibcInternallocale encodes U+263A asE2 98 BA. The locale of thelibc.prxthat titles bundle was not measured.mbstowcs_sturns the UTF-8 bytesE2 98 BAinto three wide characters (0xE2, 0x98, 0xBA). A UTF-8 locale gives one.wcrtomb_s"buffer too small" branch at BoundsChecking.cpp#L157 needsMB_CUR_MAX > smax, butsmaxis at least 1 there. TheEILSEQbranches at #L184, #L198, #L232 and #L246 needmbrtowcto fail, but it returns only 0 or 1 for a non-null source. No input reaches these ported paths, so the test can't cover them.I'd add a line in the style of the
wcsrtombs_sone, for example:Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T23:44:48Z.
Thanks @nikowolf123. I added your line to
docs/dev/debt/libc.mdright after thewcsrtombs_sentry, word for word, in6f77c09bd7, and merged current main. It records the single-byte C-locale conversion, the unmeasured locale of a title'slibc.prx, and thewcrtomb_s/EILSEQpaths the test cannot reach.For context: on the firmware's libc the C locale is UTF-8 with
MB_CUR_MAX3, so U+263A becomesE2 98 BA, matching the libc.md#L93 measurement. I have a separate branch that movesmbrtowc/wcrtomband friends to that. It will be a follow-up PR and will update this entry when it lands, so this PR keeps its current scope.Thanks also @Athen2045 for the Windows run.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T11:11:22Z.
Re-checked the new head: the code and test are unchanged, and the added
docs/dev/debt/libc.mdentry records the single-byte locale gap of the Annex K conversions as @nikowolf123 asked; merged.