fix(libc): read guest wide strings as 16-bit units #1116

Merged
Fishybeing merged 1 commit from upstream-pr/1116 into main 2026-10-08 08:15:55 +00:00
Fishybeing commented 2026-10-07 13:02:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Fishybeing on 2026-10-07T13:02:46Z.


What

The guest's wchar_t is 16 bits (UTF-16, see WindowsFormatting.hpp; FormattingWide.cpp and _Mbtowcx already use 16-bit units). The string functions in core/libs/prx/libc/src/Strings.cpp took wchar_t* and forwarded to the host's std::wcs*/std::wmem*, which only matches the guest on Windows. On Linux the host's wchar_t is 32 bits, so these read and wrote guest strings in 4-byte units: wcslen of a guest string returned a wrong length, comparisons were wrong, and the copies could overrun the guest buffer.

This implements them on char16_t:

wmemchr, wmemcmp, wmemcpy, wmemmove, wmemset, wcslen, wcscmp, wcsncmp, wcscpy, wcsncpy, wcschr, wcsrchr, wcsstr, wcspbrk, wcsspn

Comparisons are on unsigned code units and return -1, 0 or 1, as the Windows build returned before, so Windows behaviour does not change.

The wide conversions (wcstod, wcstof, wcstold, wcstol, wcstoll, wcstoul, wcstoull, wcscoll, wcsxfrm, wcsrtombs) and swprintf have the same problem on Linux but need a UTF-16 conversion, so they are left for a follow-up and listed in TechnicalDebt.

guest_wide_strings covers every changed function, including code units above 0x7FFF, embedded terminators, wcsncpy padding and truncation, overlapping wmemmove and writes past the requested count.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs 15.2.0posix-14.0.0-ucrt-r7), Release, -DBUILD_TESTING=ON, on d5adabae
  • Strings.cpp and GuestWideStrings.cpp build without warnings, also with -Wall -Wextra.
  • ctest -E 'agc|shader|recompiler|Vulkan|vulkan|video_out': 197/197 passed, including guest_wide_strings and guest_wide_formatting. GPU tests not run.
  • Not tested on Linux: I have no Linux machine. The new test is what would have failed there before this change.

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:4773027247 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1116). Originally posted by [Fishybeing](https://github.com/Fishybeing) on 2026-10-07T13:02:46Z. --- ### What The guest's `wchar_t` is 16 bits (UTF-16, see `WindowsFormatting.hpp`; `FormattingWide.cpp` and `_Mbtowcx` already use 16-bit units). The string functions in `core/libs/prx/libc/src/Strings.cpp` took `wchar_t*` and forwarded to the host's `std::wcs*`/`std::wmem*`, which only matches the guest on Windows. On Linux the host's `wchar_t` is 32 bits, so these read and wrote guest strings in 4-byte units: `wcslen` of a guest string returned a wrong length, comparisons were wrong, and the copies could overrun the guest buffer. This implements them on `char16_t`: `wmemchr`, `wmemcmp`, `wmemcpy`, `wmemmove`, `wmemset`, `wcslen`, `wcscmp`, `wcsncmp`, `wcscpy`, `wcsncpy`, `wcschr`, `wcsrchr`, `wcsstr`, `wcspbrk`, `wcsspn` Comparisons are on unsigned code units and return -1, 0 or 1, as the Windows build returned before, so Windows behaviour does not change. The wide conversions (`wcstod`, `wcstof`, `wcstold`, `wcstol`, `wcstoll`, `wcstoul`, `wcstoull`, `wcscoll`, `wcsxfrm`, `wcsrtombs`) and `swprintf` have the same problem on Linux but need a UTF-16 conversion, so they are left for a follow-up and listed in TechnicalDebt. `guest_wide_strings` covers every changed function, including code units above 0x7FFF, embedded terminators, `wcsncpy` padding and truncation, overlapping `wmemmove` and writes past the requested count. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs `15.2.0posix-14.0.0-ucrt-r7`), Release, `-DBUILD_TESTING=ON`, on `d5adabae` - `Strings.cpp` and `GuestWideStrings.cpp` build without warnings, also with `-Wall -Wextra`. - `ctest -E 'agc|shader|recompiler|Vulkan|vulkan|video_out'`: 197/197 passed, including `guest_wide_strings` and `guest_wide_formatting`. GPU tests not run. - Not tested on Linux: I have no Linux machine. The new test is what would have failed there before this change. ### 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-07 13:54:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-07T13:54:42Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1092 docs/dev/TechnicalDebt.md L211
#1095 docs/dev/TechnicalDebt.md L211
#1128 wcschr_nid_postfix
wcscmp_nid_postfix
wcscpy_nid_postfix
wcslen_nid_postfix
wcsncmp_nid_postfix
wcsrchr_nid_postfix
wcsspn_nid_postfix
wmemchr_nid_postfix
wmemcmp_nid_postfix
wmemset_nid_postfix
#1184 docs/dev/TechnicalDebt.md L211
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6039469995 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1116#issuecomment-6039469995). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-07T13:54:42Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1092 | `docs/dev/TechnicalDebt.md` [L211](https://github.com/boykopovar/AnyPS5/blob/df16c4c256be3c44e03eb9149a6ce5f8e8a038b2/docs/dev/TechnicalDebt.md#L211) | | | | #1095 | `docs/dev/TechnicalDebt.md` [L211](https://github.com/boykopovar/AnyPS5/blob/df16c4c256be3c44e03eb9149a6ce5f8e8a038b2/docs/dev/TechnicalDebt.md#L211) | | | | #1128 | | `wcschr_nid_postfix`<br>`wcscmp_nid_postfix`<br>`wcscpy_nid_postfix`<br>`wcslen_nid_postfix`<br>`wcsncmp_nid_postfix`<br>`wcsrchr_nid_postfix`<br>`wcsspn_nid_postfix`<br>`wmemchr_nid_postfix`<br>`wmemcmp_nid_postfix`<br>`wmemset_nid_postfix` | | | #1184 | `docs/dev/TechnicalDebt.md` [L211](https://github.com/boykopovar/AnyPS5/blob/df16c4c256be3c44e03eb9149a6ce5f8e8a038b2/docs/dev/TechnicalDebt.md#L211) | | |
fisherjoey commented 2026-10-08 05:24:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-08T05:24:37Z.


This looks like the same change as #1126: both add wcslen_nid_postfix, wcscpy_nid_postfix and wmemcpy_nid_postfix to libc/src/Strings.cpp for 16-bit guest wide strings, with a GuestWideStrings.cpp test under the same guest_wide_strings ctest name. Linking so they can be compared.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6052972587 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1116#issuecomment-6052972587). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-08T05:24:37Z. --- This looks like the same change as #1126: both add `wcslen_nid_postfix`, `wcscpy_nid_postfix` and `wmemcpy_nid_postfix` to `libc/src/Strings.cpp` for 16-bit guest wide strings, with a `GuestWideStrings.cpp` test under the same `guest_wide_strings` ctest name. Linking so they can be compared.
adriadam10 commented 2026-10-08 07:57:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T07:57:40Z.


Checked the 15 rewrites against the C semantics (unsigned unit comparison, wcsncpy padding/truncation, overlapping wmemmove, empty needle in wcsstr, terminator in wcschr/wcsrchr) and ran guest_wide_strings on Linux: it passes, and fails with Strings.cpp reverted, so it covers the 32-bit host wchar_t case. Full ctest green (424/424).

Only issue was a TechnicalDebt.md adjacency conflict with main; I rebased and kept all lines. Merging once CI is green on the rebased head.

#1126 is the follow-up and will need a rebase after this lands.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6055432498 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1116#issuecomment-6055432498). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T07:57:40Z. --- Checked the 15 rewrites against the C semantics (unsigned unit comparison, `wcsncpy` padding/truncation, overlapping `wmemmove`, empty needle in `wcsstr`, terminator in `wcschr`/`wcsrchr`) and ran `guest_wide_strings` on Linux: it passes, and fails with `Strings.cpp` reverted, so it covers the 32-bit host `wchar_t` case. Full ctest green (424/424). Only issue was a TechnicalDebt.md adjacency conflict with main; I rebased and kept all lines. Merging once CI is green on the rebased head. #1126 is the follow-up and will need a rebase after this lands.
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!1116
No description provided.