fix(libc): safely probe the guest memory of mbrtoc32 and mbrtoc16 #20600

Closed
milescoviello wants to merge 1 commit from upstream-pr/20600 into main
milescoviello commented 2026-11-15 10:50:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T10:50:14Z.


What

mbrtoc32 and mbrtoc16 read and wrote the conversion state, read the source bytes and stored the decoded character or code unit straight through the guest pointers. A non-null pointer to unmapped memory faulted the host.

The state's 12 bytes (the 8-byte partial value and the pending count at offset 10) are now copied into a local with GuestCopyIn_nid_no_patch and copied back with GuestCopyOut_nid_no_patch wherever the state was stored before. The source is read one byte at a time the same way, and the result is written with GuestCopyOut_nid_no_patch.

  • An unreadable source byte or an unwritable destination returns (size_t)-1 with errno EFAULT (14), the way the other errno-reporting libc paths fail on bad memory, and leaves the state as it was.
  • An unreadable or unwritable state throws the std::invalid_argument both functions already throw for a null state, since the state is required.

The decoding, the return values, the EILSEQ paths, the pending low surrogate of mbrtoc16 and the null-source and null-destination behaviour are unchanged.

General.hpp is included at the same place #18112 and #13730 include it, so either merges cleanly with this branch (checked with git merge-tree). The debt entry goes in a fragment under docs/dev/debt/changes/ and records this as unmeasured.

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • guest_wide_strings, for both functions:

    • an unmapped destination (0x10) or an unmapped source returns (size_t)-1 with errno 14, and an unmapped state throws;
    • an unmapped destination for the last byte of a split € leaves the pending count at 1, and the retry with a valid destination completes it;
    • for mbrtoc16, an unmapped destination for the high surrogate of U+1F600 leaves the state empty, and an unmapped destination for the pending low surrogate leaves it pending (0x10); the retry returns -3 with 0xdc00.

    The existing mbrtoc32 / mbrtoc16 cases (UTF-8 lengths, split sequences, null source, EILSEQ, surrogate pairs) and the rest of the file still pass.

  • Negative control: with Multibyte.cpp from main and these tests, guest_wide_strings segfaults.

  • git merge-tree against the heads of the open PRs that touch Multibyte.cpp or GuestWideStrings.cpp (#18112, #13730, #20395, #19720, #19670, #19645, #19635, #19628, #19562, #16308): 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:5321953213 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20600). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T10:50:14Z. --- ### What `mbrtoc32` and `mbrtoc16` read and wrote the conversion state, read the source bytes and stored the decoded character or code unit straight through the guest pointers. A non-null pointer to unmapped memory faulted the host. The state's 12 bytes (the 8-byte partial value and the pending count at offset 10) are now copied into a local with `GuestCopyIn_nid_no_patch` and copied back with `GuestCopyOut_nid_no_patch` wherever the state was stored before. The source is read one byte at a time the same way, and the result is written with `GuestCopyOut_nid_no_patch`. - An unreadable source byte or an unwritable destination returns `(size_t)-1` with `errno` `EFAULT` (14), the way the other `errno`-reporting libc paths fail on bad memory, and leaves the state as it was. - An unreadable or unwritable state throws the `std::invalid_argument` both functions already throw for a null state, since the state is required. The decoding, the return values, the `EILSEQ` paths, the pending low surrogate of `mbrtoc16` and the null-source and null-destination behaviour are unchanged. `General.hpp` is included at the same place #18112 and #13730 include it, so either merges cleanly with this branch (checked with `git merge-tree`). The debt entry goes in a fragment under `docs/dev/debt/changes/` and records this as unmeasured. ### Tested Linux (Ubuntu 26.04.1), GCC 15.2, Release. - `guest_wide_strings`, for both functions: - an unmapped destination (`0x10`) or an unmapped source returns `(size_t)-1` with `errno` 14, and an unmapped state throws; - an unmapped destination for the last byte of a split `€` leaves the pending count at 1, and the retry with a valid destination completes it; - for `mbrtoc16`, an unmapped destination for the high surrogate of U+1F600 leaves the state empty, and an unmapped destination for the pending low surrogate leaves it pending (`0x10`); the retry returns -3 with `0xdc00`. The existing `mbrtoc32` / `mbrtoc16` cases (UTF-8 lengths, split sequences, null source, `EILSEQ`, surrogate pairs) and the rest of the file still pass. - Negative control: with `Multibyte.cpp` from `main` and these tests, `guest_wide_strings` segfaults. - `git merge-tree` against the heads of the open PRs that touch `Multibyte.cpp` or `GuestWideStrings.cpp` (#18112, #13730, #20395, #19720, #19670, #19645, #19635, #19628, #19562, #16308): 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

Pull request closed

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!20600
No description provided.