fix(libc): safely probe the outputs of wctomb_s and wcrtomb_s #20525

Merged
milescoviello merged 1 commit from upstream-pr/20525 into main 2026-11-15 08:41:53 +00:00
milescoviello commented 2026-11-15 08:17:19 +00:00 (Migrated from github.com)

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


What

wctomb_s and wcrtomb_s wrote the converted bytes (via memcpy, or by passing the guest buffer straight to wctomb / wcrtomb), the terminator on their error paths, and the status or result count, all straight through the guest pointers. So a non-null buffer, status or result pointing at unmapped memory faulted the host.

Both now convert into a host MB_LEN_MAX buffer and copy the bytes out with GuestCopyOut_nid_no_patch, then the status or result. Every other store goes through the same helper:

  • wcrtomb_s's (size_t)-1 result and terminator on its error paths.
  • wctomb_s's state-dependence query, which still passes a null buffer to wctomb.

An unwritable buffer, status or result is reported as the "bad argument" runtime-constraint violation (EINVAL, via the constraint handler) that these functions already raise for a null status, result or state. wcrtomb_s also stores the failed result first when the result is writable. The conversions, the buffer-too-small (ERANGE) and EILSEQ results, and the bytes written for valid pointers are unchanged. The wcrtomb_s state is still read in place.

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 (CheckWideToMultibyte), with the test's recording constraint handler: these return 22 and raise one "<function>: bad argument" violation with error 22:
    • wctomb_s with an unmapped status (0x10);
    • wctomb_s with an unmapped buffer (the status is left at its old value);
    • wcrtomb_s with an unmapped result;
    • wcrtomb_s with an unmapped buffer (the result becomes (size_t)-1).
  • The existing conversion, U+263A, buffer-too-small, state-query, zero-size and null cases still pass.
  • Negative control: with BoundsChecking.cpp from main and these tests, guest_bounds_checking segfaults.
  • Merges cleanly with #17900. #8965 already conflicts with main in this file and gets no new conflict from this change.
  • 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:5320887985 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20525). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T08:17:19Z. --- ### What `wctomb_s` and `wcrtomb_s` wrote the converted bytes (via `memcpy`, or by passing the guest buffer straight to `wctomb` / `wcrtomb`), the terminator on their error paths, and the status or result count, all straight through the guest pointers. So a non-null buffer, status or result pointing at unmapped memory faulted the host. Both now convert into a host `MB_LEN_MAX` buffer and copy the bytes out with `GuestCopyOut_nid_no_patch`, then the status or result. Every other store goes through the same helper: - `wcrtomb_s`'s `(size_t)-1` result and terminator on its error paths. - `wctomb_s`'s state-dependence query, which still passes a null buffer to `wctomb`. An unwritable buffer, status or result is reported as the "bad argument" runtime-constraint violation (EINVAL, via the constraint handler) that these functions already raise for a null status, result or state. `wcrtomb_s` also stores the failed result first when the result is writable. The conversions, the buffer-too-small (`ERANGE`) and `EILSEQ` results, and the bytes written for valid pointers are unchanged. The `wcrtomb_s` state is still read in place. 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` (`CheckWideToMultibyte`), with the test's recording constraint handler: these return 22 and raise one `"<function>: bad argument"` violation with error 22: - `wctomb_s` with an unmapped status (`0x10`); - `wctomb_s` with an unmapped buffer (the status is left at its old value); - `wcrtomb_s` with an unmapped result; - `wcrtomb_s` with an unmapped buffer (the result becomes `(size_t)-1`). - The existing conversion, U+263A, buffer-too-small, state-query, zero-size and null cases still pass. - Negative control: with `BoundsChecking.cpp` from `main` and these tests, `guest_bounds_checking` segfaults. - Merges cleanly with #17900. #8965 already conflicts with `main` in this file and gets no new conflict from this change. - `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
PrivacyOnion commented 2026-11-15 08:37:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T08:37:11Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 953e872ed3:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_bounds_checking_tests built cleanly.
  • CTest (guest_bounds_checking):
    1/1 Test #1045: guest_bounds_checking ............   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/BoundsChecking.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1045: guest_bounds_checking ............***Exception: SegFault  0.34 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libc/src/BoundsChecking.cpp: Verified that wctomb_s_nid_postfix and wcrtomb_s_nid_postfix route all guest buffer writes, null-terminator stores, status codes, and result values through GuestCopyOut_nid_no_patch, eliminating unguarded host dereferences of guest pointers.
    • core/libs/prx/libc/src/BoundsChecking.cpp: Verified that unwritable guest pointers (destination, status, result) safely trigger runtime-constraint violations returning guestEinval (22) with appropriate violation messages rather than crashing the host process.
    • core/libs/prx/libc/src/BoundsChecking.cpp: Verified that buffer capacity validations correctly compare converted byte counts against capacity and invoke violation(..., guestErange) (34) before writing to destination buffers.
    • core/libs/prx/libc/src/BoundsChecking.cpp: Verified that destination-null queries (state-dependency inquiry with capacity 0) and encoding failure paths (returning guestEilseq 86 on wctomb negative return or wcrtomb failed) preserve standard return values and correct status/result outputs.
    • core/libs/tests/GuestBoundsChecking.cpp: Verified that CheckWideToMultibyte exercises unwritable status/result (0x10) and unwritable destination buffer (0x10) scenarios, asserting exact return codes (22), constraint violation calls, and status/result preservation.
    • docs/dev/debt/changes/fix-libc-probe-wctomb-s-outputs.md and AGENTS.md: Verified strict rule compliance with zero comments added in source code, technical debt properly recorded in a debt changes fragment, Conventional Commit title format, and no unauthorized files added.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5909874039 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20525#pullrequestreview-5909874039). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-15T08:37:11Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `953e872ed3`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_bounds_checking_tests` built cleanly. - **CTest** (`guest_bounds_checking`): ``` 1/1 Test #1045: guest_bounds_checking ............ Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/BoundsChecking.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1045: guest_bounds_checking ............***Exception: SegFault 0.34 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libc/src/BoundsChecking.cpp: Verified that wctomb_s_nid_postfix and wcrtomb_s_nid_postfix route all guest buffer writes, null-terminator stores, status codes, and result values through GuestCopyOut_nid_no_patch, eliminating unguarded host dereferences of guest pointers. - core/libs/prx/libc/src/BoundsChecking.cpp: Verified that unwritable guest pointers (destination, status, result) safely trigger runtime-constraint violations returning guestEinval (22) with appropriate violation messages rather than crashing the host process. - core/libs/prx/libc/src/BoundsChecking.cpp: Verified that buffer capacity validations correctly compare converted byte counts against capacity and invoke violation(..., guestErange) (34) before writing to destination buffers. - core/libs/prx/libc/src/BoundsChecking.cpp: Verified that destination-null queries (state-dependency inquiry with capacity 0) and encoding failure paths (returning guestEilseq 86 on wctomb negative return or wcrtomb failed) preserve standard return values and correct status/result outputs. - core/libs/tests/GuestBoundsChecking.cpp: Verified that CheckWideToMultibyte exercises unwritable status/result (0x10) and unwritable destination buffer (0x10) scenarios, asserting exact return codes (22), constraint violation calls, and status/result preservation. - docs/dev/debt/changes/fix-libc-probe-wctomb-s-outputs.md and AGENTS.md: Verified strict rule compliance with zero comments added in source code, technical debt properly recorded in a debt changes fragment, Conventional Commit title format, and no unauthorized files added. - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!20525
No description provided.