fix(libc): safely probe the outputs of mbstowcs_s and mbsrtowcs_s #20526

Merged
milescoviello merged 1 commit from upstream-pr/20526 into main 2026-11-15 12:00:49 +00:00
milescoviello commented 2026-11-15 08:20:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T08:20:00Z.


What

mbstowcs_s and mbsrtowcs_s let mbrtowc write each wide character straight into the guest buffer. They also stored the terminator, the result count and (for mbsrtowcs_s) the updated source pointer straight through the guest pointers, and mbsrtowcs_s read *source in 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_patch per call. The result and the source pointer are written the same way, and *source is read with GuestCopyIn_nid_no_patch before 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)-1 in 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 by mbrtowc.

The first version of this PR copied each character out on its own; #20589 showed each GuestCopyOut costs 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 current main. The debt entry goes in a fragment under docs/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_s with an unmapped result (0x10);
    • mbstowcs_s with an unmapped buffer (result (size_t)-1);
    • mbsrtowcs_s with an unreadable source pointer (result (size_t)-1);
    • mbsrtowcs_s with an unmapped buffer (result (size_t)-1);
    • mbsrtowcs_s with an unmapped result.
  • New valid-pointer cases:

    • after an overflow, both functions keep the converted characters behind the cleared first entry;
    • a 300-character string converts into a 400-entry buffer with the terminator at 300 and entry 301 untouched (and the source pointer cleared for 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's BoundsChecking.cpp and these tests, guest_bounds_checking segfaults.

  • Cost, mbstowcs_s of 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;
    • per-character version: 33.5-33.7 µs;
    • this version: 968-971 ns.
  • git merge-tree against #17900 (the other open PR on BoundsChecking.cpp): no conflicts.

  • tools/check_conventions.py passes on the branch and this body; tools/debt_fold.py --check passes with the fragment applied to main.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5320905354 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20526). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T08:20:00Z. --- ### What `mbstowcs_s` and `mbsrtowcs_s` let `mbrtowc` write each wide character straight into the guest buffer. They also stored the terminator, the result count and (for `mbsrtowcs_s`) the updated source pointer straight through the guest pointers, and `mbsrtowcs_s` read `*source` in 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_patch` per call. The result and the source pointer are written the same way, and `*source` is read with `GuestCopyIn_nid_no_patch` before 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)-1` in 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 by `mbrtowc`. The first version of this PR copied each character out on its own; #20589 showed each `GuestCopyOut` costs 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 current `main`. The debt entry goes in a fragment under `docs/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_s` with an unmapped result (`0x10`); - `mbstowcs_s` with an unmapped buffer (result `(size_t)-1`); - `mbsrtowcs_s` with an unreadable source pointer (result `(size_t)-1`); - `mbsrtowcs_s` with an unmapped buffer (result `(size_t)-1`); - `mbsrtowcs_s` with an unmapped result. - New valid-pointer cases: - after an overflow, both functions keep the converted characters behind the cleared first entry; - a 300-character string converts into a 400-entry buffer with the terminator at 300 and entry 301 untouched (and the source pointer cleared for `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`'s `BoundsChecking.cpp` and these tests, `guest_bounds_checking` segfaults. - Cost, `mbstowcs_s` of 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; - per-character version: 33.5-33.7 µs; - this version: 968-971 ns. - `git merge-tree` against #17900 (the other open PR on `BoundsChecking.cpp`): no conflicts. - `tools/check_conventions.py` passes on the branch and this body; `tools/debt_fold.py --check` passes with the fragment applied to `main`. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!20526
No description provided.