feat(libc): implement wcsrtombs_s #736

Merged
adriadam10 merged 1 commit from upstream-pr/736 into main 2026-10-05 20:31:25 +00:00
adriadam10 commented 2026-10-05 18:29:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T18:29:40Z.


What

Implements wcsrtombs_s (79s2tnYQI6I, renamed from libcCyberUnknown04) in Multibyte.cpp, using the 16-bit guest wchar_t and the existing C-locale wcrtomb.

No emulator implements it (KytyPS5, shadPS4 and fpPS4 have no body), so the behaviour comes from disassembling the console's own sce_module/libc.prx (Dinkumware, C11 K.3.6.5.2):

  • Runtime-constraint violation → EINVAL (22), *retval = -1, and dst[0] = 0 when dst is non-null with a positive size. Violations: null retval, src, *src or ps; null dst with a non-zero dstsz; and with a non-null dst, dstsz or len above RSIZE_MAX (checked as signed) or dstsz == 0 (dst is left untouched then).
  • dst null: counts the bytes up to the terminator, *src unchanged.
  • Unconvertible character → EILSEQ (86) without going through the constraint handler, *retval = -1, a terminator written at the current output position, *src unchanged.
  • len reached → terminates the output, *src points to the next wide character, *retval = bytes written.
  • The output fills before the terminator fits → ERANGE (34), dst[0] = 0, *retval = -1, *src advanced past the characters converted. The dstsz check runs before the len check.
  • Terminator converted → *src = NULL, *retval = length without the terminator.

The console's default constraint handler (libc.prx 0x4170) writes the message to stderr and returns. That message is not written here, like the other _s functions that only return the error. Listed in TechnicalDebt together with the C-locale-only conversion.

Tested

Linux, homebrew test guest_wcsrtombs_s (registered in ctest, libc CMakeLists). It covers the count mode, a full conversion, the exact fit, the len limit, len 0, ERANGE with dstsz and len both reaching zero, EILSEQ in both modes, and every constraint violation listed above. Expected values come from the disassembly, not from this implementation. Each branch was checked by breaking it in turn, and the test fails each time. The one exception is the "multibyte character does not fit" branch, which can't be reached while MB_CUR_MAX is 1. On main the export throws (NotImplemented_nid_no_patch). The full Release build (GCC) and every guest_ test pass.

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
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4749238964 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/736). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T18:29:40Z. --- ### What Implements `wcsrtombs_s` (`79s2tnYQI6I`, renamed from `libcCyberUnknown04`) in `Multibyte.cpp`, using the 16-bit guest `wchar_t` and the existing C-locale `wcrtomb`. No emulator implements it (KytyPS5, shadPS4 and fpPS4 have no body), so the behaviour comes from disassembling the console's own `sce_module/libc.prx` (Dinkumware, C11 K.3.6.5.2): - Runtime-constraint violation → `EINVAL` (22), `*retval = -1`, and `dst[0] = 0` when `dst` is non-null with a positive size. Violations: null `retval`, `src`, `*src` or `ps`; null `dst` with a non-zero `dstsz`; and with a non-null `dst`, `dstsz` or `len` above `RSIZE_MAX` (checked as signed) or `dstsz == 0` (`dst` is left untouched then). - `dst` null: counts the bytes up to the terminator, `*src` unchanged. - Unconvertible character → `EILSEQ` (86) without going through the constraint handler, `*retval = -1`, a terminator written at the current output position, `*src` unchanged. - `len` reached → terminates the output, `*src` points to the next wide character, `*retval` = bytes written. - The output fills before the terminator fits → `ERANGE` (34), `dst[0] = 0`, `*retval = -1`, `*src` advanced past the characters converted. The `dstsz` check runs before the `len` check. - Terminator converted → `*src = NULL`, `*retval` = length without the terminator. The console's default constraint handler (`libc.prx` 0x4170) writes the message to `stderr` and returns. That message is not written here, like the other `_s` functions that only return the error. Listed in TechnicalDebt together with the C-locale-only conversion. ### Tested Linux, homebrew test `guest_wcsrtombs_s` (registered in ctest, libc CMakeLists). It covers the count mode, a full conversion, the exact fit, the `len` limit, `len` 0, `ERANGE` with `dstsz` and `len` both reaching zero, `EILSEQ` in both modes, and every constraint violation listed above. Expected values come from the disassembly, not from this implementation. Each branch was checked by breaking it in turn, and the test fails each time. The one exception is the "multibyte character does not fit" branch, which can't be reached while `MB_CUR_MAX` is 1. On `main` the export throws (`NotImplemented_nid_no_patch`). The full Release build (GCC) and every `guest_` test pass. ### 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) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-05 18:30:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T18:30:05Z.


Progress report (fdd6519 → 55f548d)

📈 System libraries: 79.24% (+0.03%, +1 implemented, -1 removed functions)

✅ 1 implemented
Library Name
libc wcsrtombs_s_nid_postfix
🗑️ 1 removed
Library Name
libc libcCyberUnknown04
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6000625005 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/736#issuecomment-6000625005). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T18:30:05Z. --- <!-- progress-report --> ### Progress report (fdd6519 → 55f548d) 📈 **System libraries**: 79.24% (+0.03%, +1 implemented, -1 removed functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `wcsrtombs_s_nid_postfix` | </details> <details> <summary>🗑️ 1 removed</summary> | Library | Name | | - | - | | libc | `libcCyberUnknown04` | </details>
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!736
No description provided.