fix(libc): check size limits and null terminate on wcscpy_s constraint violations #7952

Merged
emirplus merged 1 commit from upstream-pr/7952 into main 2026-10-22 16:47:23 +00:00
emirplus commented 2026-10-19 15:57:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by emirplus on 2026-10-19T15:57:31Z.


What

Enforce C11 Annex K destination size constraints and truncation handling in SonyWcscpyS (wcscpy_s):

  • Reject oversized destination buffers (size > (SIZE_MAX >> 1) / sizeof(uint16_t)) with ERANGE without modifying the destination.
  • Set dest[0] = 0 on null source pointer or truncation rather than leaving uninitialized or partial strings.
  • Reject overlapping source and destination buffers with EINVAL and set dest[0] = 0.
  • Update docs/dev/debt/libc.md and add unit test coverage in GuestWideStrings.cpp.

Tested

python tools/check_conventions.py --base origin/main passed. Tested wide string test suite declarations in GuestWideStrings.cpp.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4923730831 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7952). Originally posted by [emirplus](https://github.com/emirplus) on 2026-10-19T15:57:31Z. --- ### What Enforce C11 Annex K destination size constraints and truncation handling in SonyWcscpyS (wcscpy_s): - Reject oversized destination buffers (size > (SIZE_MAX >> 1) / sizeof(uint16_t)) with ERANGE without modifying the destination. - Set dest[0] = 0 on null source pointer or truncation rather than leaving uninitialized or partial strings. - Reject overlapping source and destination buffers with EINVAL and set dest[0] = 0. - Update docs/dev/debt/libc.md and add unit test coverage in GuestWideStrings.cpp. ### Tested python tools/check_conventions.py --base origin/main passed. Tested wide string test suite declarations in GuestWideStrings.cpp. ### 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 - [x] Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical 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
github-actions[bot] commented 2026-10-19 16:02:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T16:02:37Z.


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 6044 core/libs/tests/GuestWideStrings.cpp
PR 7081 core/libs/prx/libc/include/SonyCrt.hpp
PR 8965 core/libs/tests/GuestWideStrings.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6271537158 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7952#issuecomment-6271537158). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T16:02:37Z. --- <!-- 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 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestWideStrings.cpp` | | [PR 7081](https://www.github.com/boykopovar/AnyPS5/pull/7081) | `core/libs/prx/libc/include/SonyCrt.hpp` | | [PR 8965](https://www.github.com/boykopovar/AnyPS5/pull/8965) | `core/libs/tests/GuestWideStrings.cpp` |
oneandonlydean commented 2026-10-20 04:02:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T04:02:46Z.


Thanks, the size, null and overlap constraints look right and the new cases cover them. One behaviour change isn't mentioned: on main, SonyWcscpyS calls SonyInvokeConstraintHandler("wcscpy_s", dest, ...) on both violation paths, so a handler installed with set_constraint_handler_s runs for wcscpy_s. This change removes those calls, while the updated debt entry says violations return without invoking the handler "as before", which is not true for wcscpy_s. Could you keep the handler call on each violation path (with 22 or 34), as Annex K expects, or drop the "as before" and say that wcscpy_s stops calling it?

Small one: the source length scan is unbounded now (while (src[length] != 0)); bounding it by size gives the same ERANGE without reading past the destination size on an unterminated source.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6283084759 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7952#issuecomment-6283084759). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T04:02:46Z. --- Thanks, the size, null and overlap constraints look right and the new cases cover them. One behaviour change isn't mentioned: on `main`, `SonyWcscpyS` calls `SonyInvokeConstraintHandler("wcscpy_s", dest, ...)` on both violation paths, so a handler installed with `set_constraint_handler_s` runs for `wcscpy_s`. This change removes those calls, while the updated debt entry says violations return without invoking the handler "as before", which is not true for `wcscpy_s`. Could you keep the handler call on each violation path (with 22 or 34), as Annex K expects, or drop the "as before" and say that `wcscpy_s` stops calling it? Small one: the source length scan is unbounded now (`while (src[length] != 0)`); bounding it by `size` gives the same ERANGE without reading past the destination size on an unterminated source.
Athen2045 commented 2026-10-20 07:54:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Athen2045 on 2026-10-20T07:54:24Z.


Windows run of f2d9584d2 merged onto main e9cc4a8ef (Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON):

  • Build: clean, with no warnings in SonyCrt.hpp or GuestWideStrings.cpp.

  • guest_wide_strings (the new wcscpy_s cases), guest_safe_errors, guest_wide_conversion, guest_wide_formatting and the guest_ampr_* tests pass.

  • guest_crt fails (3 of 3 runs): guest_crt: line 172: g_handlerCalls == 1. It passes on main on the same machine. SonyWcscpyS also backs sceAmprWcscpyS (libSceAmpr, 6f5f-qx4ucA), and this existing test installs a counting handler with sceAmprSetConstraintHandlerS and expects one call on ERANGE:

    Require(sceAmprWcscpyS(buffer, 4, longText) == ERANGE);
    Require(g_handlerCalls == 1);
    

    guest_crt is registered on every platform in core/libs/CMakeLists.txt, so it should fail on Linux too.

  • Handler calls, observed. This confirms @oneandonlydean's point. A small harness (not committed) loads the patched libc.prx, installs a handler through set_constraint_handler_s (ENLfKJEZTjE) and calls wcscpy_s (6f5f-qx4ucA). The return values match on both builds; only the handler calls differ:

    Case main This PR
    null dest 22, handler ("wcscpy_s", 22) 22, no handler call
    size 0 22, handler ("wcscpy_s", 22) 22, no handler call
    null src 22, handler ("wcscpy_s", 22) 22, no handler call
    "wide" into size 4 34, handler ("wcscpy_s", 34) 34, no handler call
    "wide" into size 16 0 0

    Keeping the SonyInvokeConstraintHandler call on each violation path, as the review suggests, should also make guest_crt pass again.

  • Full suite (select_tests.py ctest ... --timeout 120): 1711 of 1717 pass. Besides guest_crt, the same 5 tests fail on main on this machine: conventions_env_reference, conventions_blob_reads, windows_import_modules, agc_driver_msaa_dcc_clear_4 and agc_driver_pass_input_hazard.

AI-assisted: yes (Claude Code). The results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6286193962 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7952#issuecomment-6286193962). Originally posted by [Athen2045](https://github.com/Athen2045) on 2026-10-20T07:54:24Z. --- Windows run of f2d9584d2 merged onto `main` e9cc4a8ef (Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`): - **Build:** clean, with no warnings in `SonyCrt.hpp` or `GuestWideStrings.cpp`. - **`guest_wide_strings`** (the new `wcscpy_s` cases), `guest_safe_errors`, `guest_wide_conversion`, `guest_wide_formatting` and the `guest_ampr_*` tests pass. - **`guest_crt` fails** (3 of 3 runs): `guest_crt: line 172: g_handlerCalls == 1`. It passes on `main` on the same machine. `SonyWcscpyS` also backs `sceAmprWcscpyS` (libSceAmpr, `6f5f-qx4ucA`), and this existing test installs a counting handler with `sceAmprSetConstraintHandlerS` and expects one call on `ERANGE`: ```cpp Require(sceAmprWcscpyS(buffer, 4, longText) == ERANGE); Require(g_handlerCalls == 1); ``` `guest_crt` is registered on every platform in `core/libs/CMakeLists.txt`, so it should fail on Linux too. - **Handler calls, observed.** This confirms @oneandonlydean's point. A small harness (not committed) loads the patched `libc.prx`, installs a handler through `set_constraint_handler_s` (`ENLfKJEZTjE`) and calls `wcscpy_s` (`6f5f-qx4ucA`). The return values match on both builds; only the handler calls differ: | Case | `main` | This PR | |---|---|---| | null `dest` | 22, handler `("wcscpy_s", 22)` | 22, no handler call | | `size` 0 | 22, handler `("wcscpy_s", 22)` | 22, no handler call | | null `src` | 22, handler `("wcscpy_s", 22)` | 22, no handler call | | `"wide"` into size 4 | 34, handler `("wcscpy_s", 34)` | 34, no handler call | | `"wide"` into size 16 | 0 | 0 | Keeping the `SonyInvokeConstraintHandler` call on each violation path, as the review suggests, should also make `guest_crt` pass again. - **Full suite** (`select_tests.py ctest ... --timeout 120`): 1711 of 1717 pass. Besides `guest_crt`, the same 5 tests fail on `main` on this machine: `conventions_env_reference`, `conventions_blob_reads`, `windows_import_modules`, `agc_driver_msaa_dcc_clear_4` and `agc_driver_pass_input_hazard`. AI-assisted: yes (Claude Code). The results above were run on this machine.
xXPBSXx commented 2026-10-21 04:47:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T04:47:41Z.


Windows data point for f2d9584d2 merged onto main 05f999570 (clean, git merge-tree rc 0): Windows 10.0.19045, MinGW-w64 GCC 15.2.0, Release. ctest -N -R '^guest_wide_strings$' lists exactly that one test; ctest --no-tests=error -R '^guest_wide_strings$' was run three times per tree.

  • With the PR: passes 3 of 3 (0.2-1.2 s).
  • Same tree with core/libs/prx/libc/include/SonyCrt.hpp restored to main (the new test cases kept): fails 3 of 3 (exit code 0xc0000409, the test process aborts).
    Not tested: other platforms, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6308289578 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7952#issuecomment-6308289578). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T04:47:41Z. --- Windows data point for `f2d9584d2` merged onto `main` `05f999570` (clean, `git merge-tree` rc 0): Windows 10.0.19045, MinGW-w64 GCC 15.2.0, Release. `ctest -N -R '^guest_wide_strings$'` lists exactly that one test; `ctest --no-tests=error -R '^guest_wide_strings$'` was run three times per tree. - With the PR: passes 3 of 3 (0.2-1.2 s). - Same tree with `core/libs/prx/libc/include/SonyCrt.hpp` restored to `main` (the new test cases kept): fails 3 of 3 (exit code `0xc0000409`, the test process aborts). Not tested: other platforms, a title run. AI-assisted (Claude Code).
emirplus commented 2026-10-21 16:39:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by emirplus on 2026-10-21T16:39:29Z.


@oneandonlydean @Athen2045 Updated: kept the SonyInvokeConstraintHandler invocation on every violation path (EINVAL and ERANGE), bounded the source scan by size, and reverted the debt entry modification so the handler behavior matches Annex K and passes guest_crt.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321266697 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7952#issuecomment-6321266697). Originally posted by [emirplus](https://github.com/emirplus) on 2026-10-21T16:39:29Z. --- @oneandonlydean @Athen2045 Updated: kept the SonyInvokeConstraintHandler invocation on every violation path (EINVAL and ERANGE), bounded the source scan by size, and reverted the debt entry modification so the handler behavior matches Annex K and passes guest_crt.
oneandonlydean commented 2026-10-22 00:50:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T00:50:42Z.


@emirplus thanks, the handler calls are back on every violation path and the source scan is now bounded, which covers both points.

One thing still blocks it: the new wcscpy_s rows in GuestWideStrings.cpp now abort guest_wide_strings. The first violation row (wcscpy_s(nullptr, 16, ...)) calls the registered handler, and with no handler installed that's SonyDefaultConstraintHandler, which calls std::abort() (SonyCrt.hpp:25-30). Built on main with this head's two files, guest_wide_strings aborts in wcscpy_s_nid_postfix -> SonyDefaultConstraintHandler, while guest_crt and guest_strings pass.

The fix is in the test: install a non-aborting handler around the new rows and put the previous one back afterwards, as GuestCrt.cpp does around its wcscpy_s rows. For example:

void* APS5_VABI set_constraint_handler_s_nid_postfix(void* handler);
void APS5_VABI ignore_handler_s_nid_postfix(const char* message, void* pointer, int error);
...
void* previousHandler = set_constraint_handler_s_nid_postfix(reinterpret_cast<void*>(&ignore_handler_s_nid_postfix));
// the wcscpy_s rows
set_constraint_handler_s_nid_postfix(previousHandler);

With those four lines added, guest_wide_strings passes here. A counting handler that checks one call per violation row would cover the handler behaviour too, but that's optional.

A small one while you're there: with size > RSIZE_MAX the new code writes dest[0] = 0 before returning 34. C11 K.3.9.2.1.1 only sets s1[0] to null when s1max is not greater than RSIZE_MAX, and the sibling wcsncpy_s on main returns 34 without touching the destination (Strings.cpp:803). Please drop that write. A cpyBuf[0] check on the oversized row would pin it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329961150 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7952#issuecomment-6329961150). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-22T00:50:42Z. --- @emirplus thanks, the handler calls are back on every violation path and the source scan is now bounded, which covers both points. One thing still blocks it: the new `wcscpy_s` rows in `GuestWideStrings.cpp` now abort `guest_wide_strings`. The first violation row (`wcscpy_s(nullptr, 16, ...)`) calls the registered handler, and with no handler installed that's `SonyDefaultConstraintHandler`, which calls `std::abort()` (`SonyCrt.hpp:25-30`). Built on `main` with this head's two files, `guest_wide_strings` aborts in `wcscpy_s_nid_postfix` -> `SonyDefaultConstraintHandler`, while `guest_crt` and `guest_strings` pass. The fix is in the test: install a non-aborting handler around the new rows and put the previous one back afterwards, as `GuestCrt.cpp` does around its `wcscpy_s` rows. For example: ```cpp void* APS5_VABI set_constraint_handler_s_nid_postfix(void* handler); void APS5_VABI ignore_handler_s_nid_postfix(const char* message, void* pointer, int error); ... void* previousHandler = set_constraint_handler_s_nid_postfix(reinterpret_cast<void*>(&ignore_handler_s_nid_postfix)); // the wcscpy_s rows set_constraint_handler_s_nid_postfix(previousHandler); ``` With those four lines added, `guest_wide_strings` passes here. A counting handler that checks one call per violation row would cover the handler behaviour too, but that's optional. A small one while you're there: with `size > RSIZE_MAX` the new code writes `dest[0] = 0` before returning 34. C11 K.3.9.2.1.1 only sets `s1[0]` to null when `s1max` is not greater than `RSIZE_MAX`, and the sibling `wcsncpy_s` on `main` returns 34 without touching the destination (`Strings.cpp:803`). Please drop that write. A `cpyBuf[0]` check on the oversized row would pin it.
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!7952
No description provided.