Repository navigation
feat(libc): implement the remaining C11 Annex K bounds-checking functions - #7841
Conversation
…ions Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Progress report (05f9995 → 7b336be)📈 System libraries: 62.29% (+0.04%, +8 implemented functions) ✅ 8 implemented
|
|
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.
|
fogydes
left a comment
There was a problem hiding this comment.
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.- Properly configures
configure_windows_unwindand DLL path prepending for Windows inCMakeLists.txt. - Zero comments added in code; Conventional Commits title; all CI checks pass.
Ready to merge.
AI-assisted: yes
|
Windows run of 2c1f967 merged onto
AI-assisted: yes (Claude Code). The results above were run on this machine. Edit: corrected the base commit from 585075c to 3611cda, the |
|
The PR adds no What this means at this head:
I'd add a line in the style of the - [wctomb_s, wcrtomb_s, mbstowcs_s and mbsrtowcs_s](../../../core/libs/prx/libc/src/BoundsChecking.cpp) (libc) follow the console's `libc.prx`, but convert in the single-byte C locale of `wctomb`, `wcrtomb` and `mbrtowc` (`MB_CUR_MAX` 1): a character above 0xFF is `EILSEQ` in `wctomb_s` and `wcrtomb_s`, and each byte is one wide character in `mbstowcs_s` and `mbsrtowcs_s`. The locale of a title's `libc.prx` was not measured. The `wcrtomb_s` path where a character does not fit `smax` and the `EILSEQ` paths of `mbstowcs_s` and `mbsrtowcs_s` are not reached |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
|
Thanks @nikowolf123. I added your line to For context: on the firmware's libc the C locale is UTF-8 with Thanks also @Athen2045 for the Windows run. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
|
Re-checked the new head: the code and test are unchanged, and the added |
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