feat(libc): implement safe wide string and concatenation functions #6144

Merged
freshstart2066-create merged 1 commit from upstream-pr/6144 into main 2026-10-18 21:26:38 +00:00
freshstart2066-create commented 2026-10-14 23:21:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by freshstart2066-create on 2026-10-14T23:21:36Z.


What

Onimusha: Way of the Sword (PPSA27836, #5595) and other RE Engine titles import wide string and memory functions from libc during early startup and initialization. When loading without these exports, dynamic module resolution fails with:

Failed to load module: ...\libs\libc.prx
GetLastError: 127 - The specified procedure could not be found.
The procedure entry point Slmz4HMpNGs could not be located in the dynamic link library ...\libs\libc.prx

This PR implements the missing wide string copy, concatenation, and memory operations:

  • wcsncpy_s (Slmz4HMpNGs)
  • wcsncat (pA9N3VIgEZ4)
  • wcsncat_s (VxG0990tP3E)
  • wcscat_s (MqeMaVUiyU8)
  • wmemcpy_s (BTsuaJ6FxKM)
  • wmemmove_s (F8b+Wb-YQVs)

Implementation Details

  • Follows C11 Annex K constraints:
    • Validates dest and src pointers (GuestEinval = 22).
    • Enforces destsz <= (SIZE_MAX >> 1) / sizeof(char16_t) (GuestErange = 34).
    • Zeroes the destination buffer on constraint violations where dest is non-null and destsz is valid.
    • Handles buffer overlap constraints for wmemcpy_s, wcsncpy_s, and wcsncat_s.
  • Implements standard wcsncat (pA9N3VIgEZ4) appending at most n wide characters from src to null-terminated dest with null-termination.
  • Updates technical debt documentation in docs/dev/debt/libc.md.
  • Comprehensive unit test coverage added to core/libs/tests/GuestWideStrings.cpp (guest_wide_strings), testing parameter validation, out-of-range bounds, overlap rejection, null-termination, and concatenation semantics.

Tested

  • Unit tests in core/libs/tests/GuestWideStrings.cpp cover all new functions and edge cases.
  • git merge-tree upstream/main feat/libc-safe-wide-strings passes cleanly with zero conflicts (tree: d7fc793fe8f7dbdfb82570bee84fe88323b6d4c3).
  • python tools/check_conventions.py --base 584cc4c57: All conventions checks passed.

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 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
  • Unblocks: PPSA27836 #5595
  • GPUs tested: n/a (no AGC, shader recompiler or VideoOut change)
  • AI-assisted: yes

Co-authored-by: Nawasfadili [email protected]

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4865884248 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6144). Originally posted by [freshstart2066-create](https://github.com/freshstart2066-create) on 2026-10-14T23:21:36Z. --- ### What Onimusha: Way of the Sword (`PPSA27836`, #5595) and other RE Engine titles import wide string and memory functions from `libc` during early startup and initialization. When loading without these exports, dynamic module resolution fails with: ```text Failed to load module: ...\libs\libc.prx GetLastError: 127 - The specified procedure could not be found. The procedure entry point Slmz4HMpNGs could not be located in the dynamic link library ...\libs\libc.prx ``` This PR implements the missing wide string copy, concatenation, and memory operations: - `wcsncpy_s` (`Slmz4HMpNGs`) - `wcsncat` (`pA9N3VIgEZ4`) - `wcsncat_s` (`VxG0990tP3E`) - `wcscat_s` (`MqeMaVUiyU8`) - `wmemcpy_s` (`BTsuaJ6FxKM`) - `wmemmove_s` (`F8b+Wb-YQVs`) ### Implementation Details - Follows C11 Annex K constraints: - Validates `dest` and `src` pointers (`GuestEinval = 22`). - Enforces `destsz <= (SIZE_MAX >> 1) / sizeof(char16_t)` (`GuestErange = 34`). - Zeroes the destination buffer on constraint violations where `dest` is non-null and `destsz` is valid. - Handles buffer overlap constraints for `wmemcpy_s`, `wcsncpy_s`, and `wcsncat_s`. - Implements standard `wcsncat` (`pA9N3VIgEZ4`) appending at most `n` wide characters from `src` to null-terminated `dest` with null-termination. - Updates technical debt documentation in `docs/dev/debt/libc.md`. - Comprehensive unit test coverage added to `core/libs/tests/GuestWideStrings.cpp` (`guest_wide_strings`), testing parameter validation, out-of-range bounds, overlap rejection, null-termination, and concatenation semantics. ### Tested - Unit tests in `core/libs/tests/GuestWideStrings.cpp` cover all new functions and edge cases. - `git merge-tree upstream/main feat/libc-safe-wide-strings` passes cleanly with zero conflicts (tree: `d7fc793fe8f7dbdfb82570bee84fe88323b6d4c3`). - `python tools/check_conventions.py --base 584cc4c57`: `All conventions checks passed.` ### 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 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] Unblocks: PPSA27836 #5595 - [x] GPUs tested: n/a (no AGC, shader recompiler or VideoOut change) - [x] AI-assisted: yes Co-authored-by: Nawasfadili <[email protected]>
github-actions[bot] commented 2026-10-14 23:22:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T23:22:21Z.


Progress report (2559e96 → c33036b)

📈 System libraries: 88.73% (+0.02%, +6 implemented functions)

✅ 6 implemented
Library Name
libc wcscat_s_nid_postfix
libc wcsncat_nid_postfix
libc wcsncat_s_nid_postfix
libc wcsncpy_s_nid_postfix
libc wmemcpy_s_nid_postfix
libc wmemmove_s_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6180676556 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6144#issuecomment-6180676556). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T23:22:21Z. --- <!-- progress-report --> ### Progress report (2559e96 → c33036b) 📈 **System libraries**: 88.73% (+0.02%, +6 implemented functions) <details> <summary>✅ 6 implemented</summary> | Library | Name | | - | - | | libc | `wcscat_s_nid_postfix` | | libc | `wcsncat_nid_postfix` | | libc | `wcsncat_s_nid_postfix` | | libc | `wcsncpy_s_nid_postfix` | | libc | `wmemcpy_s_nid_postfix` | | libc | `wmemmove_s_nid_postfix` | </details>
github-actions[bot] commented 2026-10-15 12:15:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:15:27Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/src/Strings.cpp
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/tests/GuestWideStrings.cpp
PR 6226 docs/dev/debt/libc.md
PR 6315 docs/dev/debt/libc.md
PR 6334 docs/dev/debt/libc.md
PR 6541 docs/dev/debt/libc.md
PR 6565 docs/dev/debt/libc.md
PR 6879 core/libs/prx/libc/src/Strings.cpp
PR 7039 docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/src/Strings.cpp
PR 7332 core/libs/prx/libc/src/Strings.cpp
docs/dev/debt/libc.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191809669 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6144#issuecomment-6191809669). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:15:27Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/src/Strings.cpp`<br>`docs/dev/debt/libc.md` | | [PR 5303](https://www.github.com/boykopovar/AnyPS5/pull/5303) | `docs/dev/debt/libc.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestWideStrings.cpp` | | [PR 6226](https://www.github.com/boykopovar/AnyPS5/pull/6226) | `docs/dev/debt/libc.md` | | [PR 6315](https://www.github.com/boykopovar/AnyPS5/pull/6315) | `docs/dev/debt/libc.md` | | [PR 6334](https://www.github.com/boykopovar/AnyPS5/pull/6334) | `docs/dev/debt/libc.md` | | [PR 6541](https://www.github.com/boykopovar/AnyPS5/pull/6541) | `docs/dev/debt/libc.md` | | [PR 6565](https://www.github.com/boykopovar/AnyPS5/pull/6565) | `docs/dev/debt/libc.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libc/src/Strings.cpp` | | [PR 7039](https://www.github.com/boykopovar/AnyPS5/pull/7039) | `docs/dev/debt/libc.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libc/src/Strings.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libc/src/Strings.cpp`<br>`docs/dev/debt/libc.md` |
coltken95 commented 2026-10-15 21:03:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-15T21:03:13Z.


Tested on Linux x86-64 (Release build), with c33036b16 merged into main 4eb91eacd:

  • With the PR: guest_wide_strings passes.
  • With main's libc/src/Strings.cpp: guest_wide_string_tests doesn't link (undefined reference to 'wcscat_s_nid_postfix', 'wcsncat_nid_postfix', 'wcsncat_s_nid_postfix', 'wcsncpy_s_nid_postfix', 'wmemcpy_s_nid_postfix').

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6202222221 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6144#issuecomment-6202222221). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-15T21:03:13Z. --- Tested on Linux x86-64 (Release build), with c33036b16 merged into main 4eb91eacd: - With the PR: `guest_wide_strings` passes. - With main's `libc/src/Strings.cpp`: `guest_wide_string_tests` doesn't link (`undefined reference to 'wcscat_s_nid_postfix'`, `'wcsncat_nid_postfix'`, `'wcsncat_s_nid_postfix'`, `'wcsncpy_s_nid_postfix'`, `'wmemcpy_s_nid_postfix'`). AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-16 23:54:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T23:54:43Z.


Checked the six Annex K / wide functions against C11 K.3.7 (null, RSIZE_MAX, overlap, count vs destsz, destination zeroing on violation): they match, the debt line records the unverified console RSIZE_MAX/error values. guest_wide_strings passes on the PR merged onto main and fails to link without the change; no other open PR adds these functions. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6226927532 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6144#issuecomment-6226927532). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T23:54:43Z. --- Checked the six Annex K / wide functions against C11 K.3.7 (null, RSIZE_MAX, overlap, count vs destsz, destination zeroing on violation): they match, the debt line records the unverified console RSIZE_MAX/error values. `guest_wide_strings` passes on the PR merged onto main and fails to link without the change; no other open PR adds these functions. Merging.
oneandonlydean commented 2026-10-18 01:16:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T01:16:10Z.


@adriadam10 you wrote "Merging." on the 16th, but it didn't go through. The head is unchanged since your review (c33036b16), full CI is green on it (guest_wide_strings passes on Linux and Windows) and it merges cleanly with main. Do you want to merge it yourself, or are you happy for us to merge it?

One optional follow-up, not a blocker: the tests don't yet cover three wcsncat_s cases (exact fit, an unterminated dest, an overlapping src). The code handles all three correctly.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6244123487 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6144#issuecomment-6244123487). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-18T01:16:10Z. --- @adriadam10 you wrote "Merging." on the 16th, but it didn't go through. The head is unchanged since your review (c33036b16), full CI is green on it (guest_wide_strings passes on Linux and Windows) and it merges cleanly with main. Do you want to merge it yourself, or are you happy for us to merge it? One optional follow-up, not a blocker: the tests don't yet cover three `wcsncat_s` cases (exact fit, an unterminated dest, an overlapping src). The code handles all three correctly.
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!6144
No description provided.