fix(libc): bound and report the Annex K copies as measured on a PS5 #10270

Merged
orezraey merged 21 commits from upstream-pr/10270 into main 2026-11-04 03:58:57 +00:00
orezraey commented 2026-10-23 16:09:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-23T16:09:21Z.


What

memcpy_s, memmove_s, strcpy_s, strncpy_s, strcat_s, strncat_s and their wide forms (wmemcpy_s, wmemmove_s, wcscpy_s, wcsncpy_s, wcscat_s, wcsncat_s) measured on a retail PS5 (firmware 13.60) through libSceLibcInternal.sprx from a payload process. The sources and console logs are in this gist (psafestr to psafestr4).

The console, for all twelve:

  • takes RSIZE_MAX as 2^61 - 1 elements (the wide forms count char16_t, not bytes); a size or count above it is EINVAL (22), not ERANGE;
  • calls the constraint handler on every violation, the wide forms included, with a null pointer and a fixed message: "<function>: bad destination argument" for a null destination or a size of 0 or above RSIZE_MAX (nothing is written), "bad argument" for a null source, an oversized count or an unterminated strcat_s destination, "buffer too small" (ERANGE) and "buffer overlap" (EINVAL);
  • clears dest[0] for the string forms and the whole destination for the memory forms after a source, count, size or overlap violation;
  • checks a strcat_s/strncat_s overlap only against the characters it reads and the bytes it writes after the existing string, so strncat_s(d, 8, d, 2) on "ab" gives "abab";
  • leaves errno alone.
  • for strncat_s and wcsncat_s only (not run on the console, read from the PPSA31872 libc.prx code after JJenkx's native comparison of titles' libc.prx): a destination with no terminator within destsz is "buffer too small" (ERANGE) with dest[0] cleared, and the strncat_s overlap check leaves the terminators out of both ranges, as wcsncat_s does; strcat_s and wcscat_s keep the measured EINVAL and strcat_s keeps counting the terminators.

AnyPS5 used SIZE_MAX >> 1, returned ERANGE for an oversized size, passed the destination to the handler with the bare function name, and never called the handler from the wide forms. The byte and wide forms now share one template in SonyCrt.hpp, which wcscpy_s in libSceAmpr also uses, and the debt entry is removed. wcsncat_s and wcscat_s keep the overlap rule #9290 read from the console's wcsncat_s, which leaves the terminators out of both ranges, since the wide concatenations' overlap was not run on the console (psafestr4 runs their other cases); its debt entry now says the narrow forms count them, as measured, and that wcscat_s(d, 8, d + 1), which the narrow form refuses, is unmeasured, so no test requires its result.

This branch carries #9935, since the tests install and restore handlers around the violations and the default handler on main aborts.

Tested

  • Head 0bc7597aed (unterminated strncat_s/wcsncat_s destination and strncat_s overlap from the titles' libc.prx): guest_strings, guest_wide_strings, guest_safe_errors, guest_crt and guest_bounds_checking pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); the new guest_strings cases and the changed guest_wide_strings case fail on ee3d2ffb81; check_conventions passes.
  • Head after merging main (f80ce6cec1, with #11121): ee3d2ffb81. The only conflict was docs/dev/debt/libc.md: kept main's localeconv entry (and its removal of the setlocale entry) and this PR's set_constraint_handler_s wording. guest_strings, guest_wide_strings and guest_safe_errors pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); check_conventions passes.
  • Head after merging main (f26434afa3, with #10706): eb9f9bfcda. The tests take their declarations from libc/include/Api.hpp (wcscpy_s is declared there with uint16_t, so guest_wide_strings calls it through a small cast helper); SonyCrt.hpp includes Api.hpp instead of declaring fputs/fputc again, as in #9935. guest_strings, guest_wide_strings, guest_safe_errors, guest_crt, guest_bounds_checking and guest_qsort pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); public_headers audit and check_conventions pass.
  • Head after merging main (8c8b998620, with #12845): ec13bce3d8. The only conflict was docs/dev/debt/libc.md: kept main's qsort and qsort_r entry from #12845, and this PR's removal of the Annex K entry and its wcsncat_s and wcscat_s wording. guest_strings, guest_wide_strings, guest_safe_errors, guest_crt and guest_bounds_checking pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); check_conventions passes.
  • Head after merging main (a2c36d4ed6, with #12589) and #9935 (9260172a4c): 061d56633c. guest_safe_errors now expects the default handler to write strerror_s: bad argument\n twice, the message #12589 passes. guest_strings, guest_wide_strings, guest_safe_errors, guest_crt and guest_bounds_checking pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); check_conventions passes.
    Windows 11, MinGW (winlibs).
  • guest_strings checks the new limit, the error values, the handler's message and pointer and the overlap rules; guest_wide_strings checks the wide limits, EINVAL for an unterminated wcsncat_s, the oversized counts and the overlaps. Both pass three runs in a row.
  • The full suite (-E "^agc_|^select_tests$") passes apart from guest_kqueue, which failed once and passed on its three reruns.
  • tools/check_conventions.py passes.
  • tools/pr_overlap.py: #9087, #9290, #9688 touch nearby lines; whichever lands second keeps both.
  • Head after merging main (ba8dcc6ae8, with #13220, which removes the duplicate write-watch test names that failed the macOS jobs) and #9935 (d7b7f9ad59): cfa07b700e. guest_strings, guest_wide_strings and guest_safe_errors pass three runs in a row on Windows (MinGW, GCC 15.2). tools/check_conventions.py passes.

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: #9935, #7081, #8965; #7081, #8965 edit the same lines too (merge order only); #10706 edits the declarations and nearby lines this PR changes in Strings.cpp, GuestSafeErrors.cpp, GuestStrings.cpp and GuestWideStrings.cpp (merge order only); #11621 (B-Howell) adds libc.md entries next to the ones this PR edits, and #11664 (B-Howell) appends its strmode checks at the same place in GuestStrings.cpp (merge order only); #12594 (JJenkx) moves the memset_s checks next to the strcpy_s line this PR changes in GuestStrings.cpp, and #12574 (JJenkx) edits the set_constraint_handler_s entry this branch rewrites (merge order only); #12627 (JJenkx) rewrites the snwprintf_s and swprintf_s entries next to the wcsncat_s and wcscat_s entry this PR edits in libc.md (merge order only); #12598 (JJenkx) rewrites the qsort and qsort_r entry right above this PR's wcsncat_s and wcscat_s entry in libc.md, and also edits the qsort_s entry next to the set_constraint_handler_s one this branch rewrites (merge order only); #9688 has merged
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4985708654 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10270). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-23T16:09:21Z. --- ### What `memcpy_s`, `memmove_s`, `strcpy_s`, `strncpy_s`, `strcat_s`, `strncat_s` and their wide forms (`wmemcpy_s`, `wmemmove_s`, `wcscpy_s`, `wcsncpy_s`, `wcscat_s`, `wcsncat_s`) measured on a retail PS5 (firmware 13.60) through `libSceLibcInternal.sprx` from a payload process. The sources and console logs are in [this gist](https://gist.github.com/orezraey/dcadb97109bb78711ff3f7ba1dc479ed) (`psafestr` to `psafestr4`). The console, for all twelve: - takes RSIZE_MAX as 2^61 - 1 elements (the wide forms count `char16_t`, not bytes); a size or count above it is `EINVAL` (22), not `ERANGE`; - calls the constraint handler on every violation, the wide forms included, with a null pointer and a fixed message: `"<function>: bad destination argument"` for a null destination or a size of 0 or above RSIZE_MAX (nothing is written), `"bad argument"` for a null source, an oversized count or an unterminated `strcat_s` destination, `"buffer too small"` (`ERANGE`) and `"buffer overlap"` (`EINVAL`); - clears `dest[0]` for the string forms and the whole destination for the memory forms after a source, count, size or overlap violation; - checks a `strcat_s`/`strncat_s` overlap only against the characters it reads and the bytes it writes after the existing string, so `strncat_s(d, 8, d, 2)` on `"ab"` gives `"abab"`; - leaves `errno` alone. - for `strncat_s` and `wcsncat_s` only (not run on the console, read from the PPSA31872 `libc.prx` code after JJenkx's native comparison of titles' `libc.prx`): a destination with no terminator within `destsz` is `"buffer too small"` (`ERANGE`) with `dest[0]` cleared, and the `strncat_s` overlap check leaves the terminators out of both ranges, as `wcsncat_s` does; `strcat_s` and `wcscat_s` keep the measured `EINVAL` and `strcat_s` keeps counting the terminators. AnyPS5 used SIZE_MAX >> 1, returned `ERANGE` for an oversized size, passed the destination to the handler with the bare function name, and never called the handler from the wide forms. The byte and wide forms now share one template in `SonyCrt.hpp`, which `wcscpy_s` in libSceAmpr also uses, and the debt entry is removed. `wcsncat_s` and `wcscat_s` keep the overlap rule #9290 read from the console's `wcsncat_s`, which leaves the terminators out of both ranges, since the wide concatenations' overlap was not run on the console (`psafestr4` runs their other cases); its debt entry now says the narrow forms count them, as measured, and that `wcscat_s(d, 8, d + 1)`, which the narrow form refuses, is unmeasured, so no test requires its result. This branch carries #9935, since the tests install and restore handlers around the violations and the default handler on `main` aborts. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Head `0bc7597aed` (unterminated `strncat_s`/`wcsncat_s` destination and `strncat_s` overlap from the titles' `libc.prx`): `guest_strings`, `guest_wide_strings`, `guest_safe_errors`, `guest_crt` and `guest_bounds_checking` pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); the new `guest_strings` cases and the changed `guest_wide_strings` case fail on `ee3d2ffb81`; `check_conventions` passes. - Head after merging `main` (`f80ce6cec1`, with #11121): `ee3d2ffb81`. The only conflict was `docs/dev/debt/libc.md`: kept main's `localeconv` entry (and its removal of the `setlocale` entry) and this PR's `set_constraint_handler_s` wording. `guest_strings`, `guest_wide_strings` and `guest_safe_errors` pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); `check_conventions` passes. - Head after merging `main` (`f26434afa3`, with #10706): `eb9f9bfcda`. The tests take their declarations from `libc/include/Api.hpp` (`wcscpy_s` is declared there with `uint16_t`, so `guest_wide_strings` calls it through a small cast helper); `SonyCrt.hpp` includes `Api.hpp` instead of declaring `fputs`/`fputc` again, as in #9935. `guest_strings`, `guest_wide_strings`, `guest_safe_errors`, `guest_crt`, `guest_bounds_checking` and `guest_qsort` pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); `public_headers` audit and `check_conventions` pass. - Head after merging `main` (`8c8b998620`, with #12845): `ec13bce3d8`. The only conflict was `docs/dev/debt/libc.md`: kept main's `qsort and qsort_r` entry from #12845, and this PR's removal of the Annex K entry and its `wcsncat_s and wcscat_s` wording. `guest_strings`, `guest_wide_strings`, `guest_safe_errors`, `guest_crt` and `guest_bounds_checking` pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); `check_conventions` passes. - Head after merging `main` (`a2c36d4ed6`, with #12589) and #9935 (`9260172a4c`): `061d56633c`. `guest_safe_errors` now expects the default handler to write `strerror_s: bad argument\n` twice, the message #12589 passes. `guest_strings`, `guest_wide_strings`, `guest_safe_errors`, `guest_crt` and `guest_bounds_checking` pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); `check_conventions` passes. Windows 11, MinGW (winlibs). - `guest_strings` checks the new limit, the error values, the handler's message and pointer and the overlap rules; `guest_wide_strings` checks the wide limits, `EINVAL` for an unterminated `wcsncat_s`, the oversized counts and the overlaps. Both pass three runs in a row. - The full suite (`-E "^agc_|^select_tests$"`) passes apart from `guest_kqueue`, which failed once and passed on its three reruns. - `tools/check_conventions.py` passes. - `tools/pr_overlap.py`: #9087, #9290, #9688 touch nearby lines; whichever lands second keeps both. - Head after merging `main` (`ba8dcc6ae8`, with #13220, which removes the duplicate write-watch test names that failed the macOS jobs) and #9935 (`d7b7f9ad59`): `cfa07b700e`. `guest_strings`, `guest_wide_strings` and `guest_safe_errors` pass three runs in a row on Windows (MinGW, GCC 15.2). `tools/check_conventions.py` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: #9935, #7081, #8965; #7081, #8965 edit the same lines too (merge order only); #10706 edits the declarations and nearby lines this PR changes in `Strings.cpp`, `GuestSafeErrors.cpp`, `GuestStrings.cpp` and `GuestWideStrings.cpp` (merge order only); #11621 (B-Howell) adds `libc.md` entries next to the ones this PR edits, and #11664 (B-Howell) appends its `strmode` checks at the same place in `GuestStrings.cpp` (merge order only); #12594 (JJenkx) moves the `memset_s` checks next to the `strcpy_s` line this PR changes in `GuestStrings.cpp`, and #12574 (JJenkx) edits the `set_constraint_handler_s` entry this branch rewrites (merge order only); #12627 (JJenkx) rewrites the `snwprintf_s` and `swprintf_s` entries next to the `wcsncat_s and wcscat_s` entry this PR edits in `libc.md` (merge order only); #12598 (JJenkx) rewrites the `qsort and qsort_r` entry right above this PR's `wcsncat_s and wcscat_s` entry in `libc.md`, and also edits the `qsort_s` entry next to the `set_constraint_handler_s` one this branch rewrites (merge order only); #9688 has merged - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 05:19:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T05:19:49Z.

Review: COMMENTED


Checked against main 7966e590ef, C11 Annex K (K.3.7.1.1-K.3.7.2.4) and the psafestr–psafestr4 logs and sources in your gist:

  • Thanks, this is a careful one. Every case logged in psafestr through psafestr4 agrees with the three new templates: return values, handler message and pointer, which bytes get cleared, the 2^61-1 limit for both byte and char16_t counts, and strncat_s(d, 8, d, 2) giving "abab". The Annex K rules for clearing dest and calling the handler hold throughout, and it lines these functions up with what BoundsChecking.cpp on main already does (rsizeMax, "fn: reason" with a null pointer). It merges cleanly onto current main.
  • One test pins down an unmeasured result exactly where the narrow form was measured to differ. GuestWideStrings now requires wcscat_s(strBuf, 8, strBuf + 1) == 0 with "abb", but strcat_s(d, 8, d + 1) on "ab" is EINVAL in psafestr4 (cat_overlap). #9290's rule came from a static reading of wcsncat_s only, and wcscat_s's console code wasn't read. psafestr4.c already resolves wcscat_s and wcsncat_s, so could you add wcat_overlap/wncat_overlap cases? If that can't be done now, could the require go, or the debt line say this case is unmeasured?
  • Small wording point in the debt entry: "the wide forms were not run on a console". psafestr4 does run wcscat_s and wcsncat_s (unterminated, short, null src, dmax, count); only their overlap wasn't run, so "their overlap was not run on a console" would be accurate. The same applies to the description's "the wide concatenations were not run".

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636985532 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10270#pullrequestreview-5636985532). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T05:19:49Z. **Review:** COMMENTED --- Checked against main 7966e590ef, C11 Annex K (K.3.7.1.1-K.3.7.2.4) and the psafestr–psafestr4 logs and sources in your gist: - Thanks, this is a careful one. Every case logged in psafestr through psafestr4 agrees with the three new templates: return values, handler message and pointer, which bytes get cleared, the 2^61-1 limit for both byte and `char16_t` counts, and `strncat_s(d, 8, d, 2)` giving "abab". The Annex K rules for clearing dest and calling the handler hold throughout, and it lines these functions up with what BoundsChecking.cpp on main already does (`rsizeMax`, `"fn: reason"` with a null pointer). It merges cleanly onto current main. - One test pins down an unmeasured result exactly where the narrow form was measured to differ. GuestWideStrings now requires `wcscat_s(strBuf, 8, strBuf + 1) == 0` with "abb", but `strcat_s(d, 8, d + 1)` on "ab" is EINVAL in psafestr4 (`cat_overlap`). #9290's rule came from a static reading of `wcsncat_s` only, and `wcscat_s`'s console code wasn't read. psafestr4.c already resolves `wcscat_s` and `wcsncat_s`, so could you add `wcat_overlap`/`wncat_overlap` cases? If that can't be done now, could the `require` go, or the debt line say this case is unmeasured? - Small wording point in the debt entry: "the wide forms were not run on a console". psafestr4 does run `wcscat_s` and `wcsncat_s` (unterminated, short, null src, dmax, count); only their overlap wasn't run, so "their overlap was not run on a console" would be accurate. The same applies to the description's "the wide concatenations were not run". Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-24 21:30:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:30:27Z.

Review: APPROVED


Reviewed current head 4e579c7ac5. The implementation consolidates the Annex K byte/wide string and memory helpers while keeping the measured byte-versus-wide overlap distinction explicit. The unsupported wide-overlap assertion was removed, and the debt entry now states what was and was not measured. The default-handler test redirects guest stderr and verifies the exact message and newline; the handler-call tests check codes, arguments and destination clearing. I ran diff whitespace and repository convention checks on this head; both pass. The PR reports three consecutive focused runs on Linux and Windows, while hosted product jobs are skipped. The declared integration order for overlapping PRs remains applicable before merge. I found no additional blocking correctness issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640103063 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10270#pullrequestreview-5640103063). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T21:30:27Z. **Review:** APPROVED --- Reviewed current head 4e579c7ac5b9fb998a8f82b6aa40106be27a279c. The implementation consolidates the Annex K byte/wide string and memory helpers while keeping the measured byte-versus-wide overlap distinction explicit. The unsupported wide-overlap assertion was removed, and the debt entry now states what was and was not measured. The default-handler test redirects guest stderr and verifies the exact message and newline; the handler-call tests check codes, arguments and destination clearing. I ran diff whitespace and repository convention checks on this head; both pass. The PR reports three consecutive focused runs on Linux and Windows, while hosted product jobs are skipped. The declared integration order for overlapping PRs remains applicable before merge. I found no additional blocking correctness issue.
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!10270
No description provided.