fix(libc): reject memset_s violations as titles' libc.prx does #12594

Merged
JJenkx merged 5 commits from upstream-pr/12594 into main 2026-11-11 20:23:27 +00:00
JJenkx commented 2026-10-28 22:03:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-28T22:03:35Z.


Current refresh: head 11301c06e83d570b526baa57644580e4751de731 includes main 151a2cb6e274; scoped Linux CPU tests12/12, main24/24, public headers and regression controls pass. Current evidence is in the refresh comment; earlier platform reports retain their original pins.

What

memset_s now accepts and rejects calls the way the libc.prx that titles ship does. Measured natively on PPSA07923 and PPSA05916, both of which agree:

  • Fills only when dest is not null, destsz <= SIZE_MAX >> 3, and count <= destsz. A 0/0 call on a valid dest succeeds.
  • Every other call returns ERANGE (34), writes nothing, and calls the constraint handler with "memset_s: bad destination argument", ERANGE and a null pointer. That includes a null dest with destsz = count = 0.

On main it returned EINVAL (22) without calling the handler, used SIZE_MAX >> 1 as the limit, and filled destsz bytes when count was larger.

The old checks in guest_strings asserted main's behaviour, which the titles contradict, so they move to guest_bounds_checking as CheckMemset, which installs a recording handler.

Fulfilled dependency: #9935 reached main through merged #10270, so the default constraint handler now writes and returns. orezraey’s request to keep this memset_s change separate from #10270’s copy functions is retained.

Fulfilled #10270 merge note (historical qualification): core/libs/tests/GuestStrings.cpp conflicts textually, because #10270's hunk at line 130 sits next to the removed memset_s lines. The resolution is #10270's merged file with those seven lines removed (six Require(memset_s_nid_postfix(...)) lines and the sentinel declaration). Merged that way on main 319621ba2e with #10270 0bc7597aed:

  • guest_bounds_checking, guest_strings, guest_safe_errors, guest_crt and guest_wide_strings pass 3/3;
  • all nine functions of the native comparison below match the titles.

Tested

Current-main integration — 2026-11-04. Head a2f6e23c8731896479fee84eccfa75396d6050c8 includes main b11c7bf84f5d2f636ff60166cc3371bd071e24d5 and preserves previous public commits and attribution/licence notices. Retains the measured memset_s violation behavior and write-preservation fixtures while preserving main’s updated copy-function size bounds and returning default handler.

Linux x86-64, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON: rebuilt actual libc/Ampr and selected executables. guest_bounds_checking, guest_strings, guest_safe_errors, guest_crt each pass 3/3 (12 runtime passes, zero skips); public_headers, conventions and whitespace checks pass. Pinned main passes all 17 selected baseline groups 3/3 (51 passes) plus headers. Before/after dependency checks agree: 34 recursive trees and 31,551 tracked blobs, zero errors; the update=none QuickJS test262 corpus is explicitly excluded from these library inputs.

Source/parent/gitlink proofs, library/test hashes, raw logs/JUnit and head-leased push receipts: qualification archive, SHA-256 7c347f620951ad6f39068740750949212c6412815da094a46a96eca1d15c8bbf (download verified). Older platform, stress, negative-control and reference reports retain their original pins; no new Windows/macOS, full-suite, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex).

Earlier dated qualifications below retain their original source/platform pins.

Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, main 319621ba2e.

  • Native comparison: a scratch loader calls the titles' memset_s by NID next to this build's, with the same recording handler on both sides. It makes 20,000 random calls:

    • destination sizes including 0, SIZE_MAX >> 1 and one above it;
    • counts below and above the size;
    • null destinations.

    Each call compares the return value, whether the handler ran and with which error, and the destination bytes.

    • main: 11,242 of 20,000 differ, on both titles.
    • This PR: 0 of 20,000, on both titles.
    • The rules above come from 14 chosen cases run on the titles (each limit, each null combination, SIZE_MAX), which also gave the message and the null pointer. CheckMemset checks 10 of those cases against this build.
  • Regression: with the previous Strings.cpp, the new CheckMemset fails (memset_s accepted a violation).

  • Test set: guest_bounds_checking, guest_strings, guest_safe_errors, guest_crt and guest_wide_strings pass 3/3. Conventions pass.

Independent Windows evidence from bunbeans2-arch: Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, Release; head 37d0c191b5a04732daaca697ca5678ac9c670aaf integrated with main 98ddd9c179. guest_bounds_checking and guest_strings: change passed 5/5; reverting only production changes while retaining the regression tests failed 5/5 at memset_s accepted a violation; plain main passed 3/3. This is the reporter's execution, not a new local Windows run.

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 (#9935 fulfilled through merged #10270)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5058408772 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12594). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T22:03:35Z. --- Current refresh: head `11301c06e83d570b526baa57644580e4751de731` includes main `151a2cb6e274`; scoped Linux CPU tests12/12, main24/24, public headers and regression controls pass. Current evidence is in the refresh comment; earlier platform reports retain their original pins. ### What `memset_s` now accepts and rejects calls the way the `libc.prx` that titles ship does. Measured natively on PPSA07923 and PPSA05916, both of which agree: - **Fills only** when `dest` is not null, `destsz <= SIZE_MAX >> 3`, and `count <= destsz`. A `0/0` call on a valid `dest` succeeds. - **Every other call** returns `ERANGE` (34), writes nothing, and calls the constraint handler with `"memset_s: bad destination argument"`, `ERANGE` and a null pointer. That includes a null `dest` with `destsz = count = 0`. On `main` it returned `EINVAL` (22) without calling the handler, used `SIZE_MAX >> 1` as the limit, and filled `destsz` bytes when `count` was larger. The old checks in `guest_strings` asserted `main`'s behaviour, which the titles contradict, so they move to `guest_bounds_checking` as `CheckMemset`, which installs a recording handler. **Fulfilled dependency:** #9935 reached main through merged #10270, so the default constraint handler now writes and returns. orezraey’s request to keep this memset_s change separate from #10270’s copy functions is retained. **Fulfilled #10270 merge note (historical qualification):** `core/libs/tests/GuestStrings.cpp` conflicts textually, because #10270's hunk at line 130 sits next to the removed `memset_s` lines. The resolution is #10270's merged file with those seven lines removed (six `Require(memset_s_nid_postfix(...))` lines and the `sentinel` declaration). Merged that way on `main` `319621ba2e` with #10270 `0bc7597aed`: - `guest_bounds_checking`, `guest_strings`, `guest_safe_errors`, `guest_crt` and `guest_wide_strings` pass 3/3; - all nine functions of the native comparison below match the titles. ### Tested **Current-main integration — 2026-11-04.** Head `a2f6e23c8731896479fee84eccfa75396d6050c8` includes main `b11c7bf84f5d2f636ff60166cc3371bd071e24d5` and preserves previous public commits and attribution/licence notices. Retains the measured memset_s violation behavior and write-preservation fixtures while preserving main’s updated copy-function size bounds and returning default handler. Linux x86-64, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON: rebuilt actual libc/Ampr and selected executables. `guest_bounds_checking`, `guest_strings`, `guest_safe_errors`, `guest_crt` each pass 3/3 (**12 runtime passes, zero skips**); `public_headers`, conventions and whitespace checks pass. Pinned main passes all 17 selected baseline groups 3/3 (51 passes) plus headers. Before/after dependency checks agree: 34 recursive trees and 31,551 tracked blobs, zero errors; the update=none QuickJS test262 corpus is explicitly excluded from these library inputs. Source/parent/gitlink proofs, library/test hashes, raw logs/JUnit and head-leased push receipts: [qualification archive](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-libc10-mainb11c-linux-20261104.tar.gz), SHA-256 `7c347f620951ad6f39068740750949212c6412815da094a46a96eca1d15c8bbf` (download verified). Older platform, stress, negative-control and reference reports retain their original pins; no new Windows/macOS, full-suite, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex). Earlier dated qualifications below retain their original source/platform pins. Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, `main` `319621ba2e`. - **Native comparison:** a scratch loader calls the titles' `memset_s` by NID next to this build's, with the same recording handler on both sides. It makes 20,000 random calls: - destination sizes including 0, `SIZE_MAX >> 1` and one above it; - counts below and above the size; - null destinations. Each call compares the return value, whether the handler ran and with which error, and the destination bytes. - `main`: **11,242 of 20,000** differ, on both titles. - This PR: **0 of 20,000**, on both titles. - The rules above come from 14 chosen cases run on the titles (each limit, each null combination, `SIZE_MAX`), which also gave the message and the null pointer. `CheckMemset` checks 10 of those cases against this build. - **Regression:** with the previous `Strings.cpp`, the new `CheckMemset` fails (`memset_s accepted a violation`). - **Test set:** `guest_bounds_checking`, `guest_strings`, `guest_safe_errors`, `guest_crt` and `guest_wide_strings` pass 3/3. Conventions pass. Independent Windows evidence from [bunbeans2-arch](https://github.com/boykopovar/AnyPS5/pull/12594#issuecomment-6519498272): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, Release; head `37d0c191b5a04732daaca697ca5678ac9c670aaf` integrated with main `98ddd9c179`. `guest_bounds_checking` and `guest_strings`: change passed 5/5; reverting only production changes while retaining the regression tests failed 5/5 at `memset_s accepted a violation`; plain main passed 3/3. This is the reporter's execution, not a new local Windows run. ### 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 (#9935 fulfilled through merged #10270) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
rooklz commented 2026-10-28 22:46:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T22:46:44Z.

Review: APPROVED


I ran this on macOS, which the body doesn't list (head 37d0c191b5, based on main 319621ba2e).

Standalone ASan+UBSan build (macOS 26, Apple clang, -arch x86_64): the PR's Strings.cpp and WideConversion.cpp (for set_constraint_handler_s), linked to CheckMemset and the recording handler taken verbatim from GuestBoundsChecking.cpp. I added three abort-only symbols for unrelated Strings.cpp imports; none of them is called.

  • head: passes, with no sanitizer reports.
  • main's Strings.cpp with the new test: fails with memset_s accepted a violation, as the body says.

Mutants of memset_s_nid_postfix: the test kills all 16.

  • Limit changed to >> 2, to >> 4, or applied to count instead of destsz.
  • count > destsz changed to >=.
  • Each of the three guards dropped one at a time. Dropping the null check is caught as an ASan SEGV.
  • Handler not called, called twice, given dest as its pointer, given error 22, or given the message bad argument.
  • Returning 22.
  • Filling destsz bytes, or count - 1 bytes.
  • Zeroing destsz bytes on a violation.

Other checks:

  • No internal caller of memset_s_nid_postfix exists outside the export, so the stricter checks can't change another libc path.
  • After the removal, GuestStrings.cpp and GuestBoundsChecking.cpp compile without new warnings (-Wall -Wextra).
  • check_conventions passes. git merge-tree against main is clean.
  • pr_overlap lists only the shared docs/dev/debt/libc.md, plus #5021 and #11892 in Strings.cpp. #11892 merges cleanly with this PR. #5021 already conflicts with main itself.

LGTM once #9935 lands, as stated.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5690702165 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12594#pullrequestreview-5690702165). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T22:46:44Z. **Review:** APPROVED --- I ran this on macOS, which the body doesn't list (head `37d0c191b5`, based on `main` `319621ba2e`). **Standalone ASan+UBSan build** (macOS 26, Apple clang, `-arch x86_64`): the PR's `Strings.cpp` and `WideConversion.cpp` (for `set_constraint_handler_s`), linked to `CheckMemset` and the recording handler taken verbatim from `GuestBoundsChecking.cpp`. I added three abort-only symbols for unrelated `Strings.cpp` imports; none of them is called. - head: passes, with no sanitizer reports. - `main`'s `Strings.cpp` with the new test: fails with `memset_s accepted a violation`, as the body says. **Mutants of `memset_s_nid_postfix`: the test kills all 16.** - Limit changed to `>> 2`, to `>> 4`, or applied to `count` instead of `destsz`. - `count > destsz` changed to `>=`. - Each of the three guards dropped one at a time. Dropping the null check is caught as an ASan SEGV. - Handler not called, called twice, given `dest` as its pointer, given error 22, or given the message `bad argument`. - Returning 22. - Filling `destsz` bytes, or `count - 1` bytes. - Zeroing `destsz` bytes on a violation. **Other checks:** - No internal caller of `memset_s_nid_postfix` exists outside the export, so the stricter checks can't change another libc path. - After the removal, `GuestStrings.cpp` and `GuestBoundsChecking.cpp` compile without new warnings (`-Wall -Wextra`). - `check_conventions` passes. `git merge-tree` against `main` is clean. - `pr_overlap` lists only the shared `docs/dev/debt/libc.md`, plus #5021 and #11892 in `Strings.cpp`. #11892 merges cleanly with this PR. #5021 already conflicts with `main` itself. LGTM once #9935 lands, as stated.
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!12594
No description provided.