fix(libc): safely probe the outputs of mbstowcs_s and mbsrtowcs_s #20526
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/20526"
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 milescoviello on 2026-11-15T08:20:00Z.
What
mbstowcs_sandmbsrtowcs_sletmbrtowcwrite each wide character straight into the guest buffer. They also stored the terminator, the result count and (formbsrtowcs_s) the updated source pointer straight through the guest pointers, andmbsrtowcs_sread*sourcein place. So a non-null buffer, result or source pointer to unmapped memory faulted the host.The converted characters are now collected in a host buffer and written to the guest buffer, with the terminator, in one
GuestCopyOut_nid_no_patchper call. The result and the source pointer are written the same way, and*sourceis read withGuestCopyIn_nid_no_patchbefore the argument check. On every path (success,EILSEQ, buffer too small, count-only) the guest ends up with the same contents and the writes happen in the same order as before, including the converted characters left in place after an overflow.An unreadable source pointer or an unwritable buffer, result or source pointer is reported as the "bad argument" runtime-constraint violation (EINVAL) these functions already raise for null arguments, storing
(size_t)-1in the result first when it is writable. The conversions and every result for valid pointers are unchanged. The multibyte text itself is still read in place bymbrtowc.The first version of this PR copied each character out on its own; #20589 showed each
GuestCopyOutcosts about 330 ns on Linux, so this one writes the buffer once. #20525, which this was stacked on, is merged, and the branch is rebased onto currentmain. The debt entry goes in a fragment underdocs/dev/debt/changes/and records this as a host-safety fallback; console behaviour there is not measured.Tested
Linux (Ubuntu 26.04.1), GCC 15.2, Release.
guest_bounds_checking(CheckMultibyteToWide), with the test's recording constraint handler: these return 22 and raise one"<function>: bad argument"violation with error 22:mbstowcs_swith an unmapped result (0x10);mbstowcs_swith an unmapped buffer (result(size_t)-1);mbsrtowcs_swith an unreadable source pointer (result(size_t)-1);mbsrtowcs_swith an unmapped buffer (result(size_t)-1);mbsrtowcs_swith an unmapped result.New valid-pointer cases:
mbsrtowcs_s).The new cases also pass with the per-character version, so the batching keeps the old results. The existing count, convert, length-limit, zero-length, overflow, null-source and oversized-buffer cases still pass.
Negative control: with
main'sBoundsChecking.cppand these tests,guest_bounds_checkingsegfaults.Cost,
mbstowcs_sof a 100-character string into a 128-entry buffer, best of 3 x 20,000 calls, pinned core, two interleaved rounds:main: 166-169 ns;git merge-treeagainst #17900 (the other open PR onBoundsChecking.cpp): no conflicts.tools/check_conventions.pypasses on the branch and this body;tools/debt_fold.py --checkpasses with the fragment applied tomain.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)