fix(libc): take strtok_s's C11 s1max argument as titles' libc.prx does #12581

Merged
JJenkx merged 2 commits from upstream-pr/12581 into main 2026-11-05 19:14:28 +00:00
JJenkx commented 2026-10-28 21:21:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-28T21:21:50Z.


What

strtok_s (NID -vXEQdRADLI, libc and the libSceAmpr re-export) now takes the four arguments that titles' libc.prx uses: the C11 Annex K form strtok_s(s1, s1max, s2, ptr).

main implemented the Microsoft three-argument form strtok_s(str, delim, context). A title's call therefore had its s1max pointer read as the delimiter string, and its delimiter string treated as the char** context and written through. Run against the titles' calls in the comparison below, main crashes with an access violation.

The new SonyStrtokS is the narrow form of the existing wcstok_s:

  • It scans only within *s1max, counts it down, and stores the position in *ptr.
  • Violations call the constraint handler with EINVAL and a null pointer:
    • "strtok_s: bad argument": a null s1max, s2 or ptr; *s1max above SIZE_MAX >> 3; or a null s1 with a null *ptr.
    • "strtok_s: missing token": s1max ran out while skipping delimiters.
    • "strtok_s: missing delimiter": s1max ran out inside a token.

The default constraint-handler prerequisite is fulfilled on current main b11c7bf84f5d2f636ff60166cc3371bd071e24d5: #9935 was closed after its changes landed through #10270. Merging its final head into this main produces the unchanged main tree. Earlier test and title measurements retain their recorded source pins.

Prior work. @ivica00 described this four-argument signature from a title's disassembly in #8711, which they closed on 2026-10-21 after it needed a rebase. This PR measures the behaviour by running the titles' code and agrees with that description.

Tested

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

  • Native comparison: a scratch loader calls the titles' strtok_s by NID next to this build's, with the same recording handler installed on both sides. It runs 50,000 chains of up to 6 calls:

    • random strings over a, b, , and space;
    • s1max from 0 to 19, 2^61 - 1, 2^61 and SIZE_MAX;
    • a null s1, s1max, s2 or ptr.

    Each step compares the returned token, *s1max, *ptr, the buffer bytes, and the handler's calls, error, pointer and message.

    PPSA07923 PPSA05916
    this PR 0 of 103,167 steps differ 0 of 103,167 steps differ
    main crashes (access violation) not run
  • Regression: guest_bounds_checking gains CheckStrtok, the narrow version of CheckWcstok: the three tokens with their s1max and context, and each violation and its message. guest_crt's libSceAmpr checks now use four arguments.

  • Test set: guest_crt, guest_bounds_checking, guest_safe_errors and guest_strings pass 3/3. On the full suite, 2858 of 2861 pass:

    • guest_job_manager_worker_count fails as it does on main on this machine (#12085 fixes it);
    • agc_compute_pipeline_capture_optional_byte_cap and guest_http2_transport failed under -j32 and pass when rerun alone.

    Conventions pass.

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:5057895088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12581). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T21:21:50Z. --- ### What `strtok_s` (NID `-vXEQdRADLI`, libc and the libSceAmpr re-export) now takes the four arguments that titles' `libc.prx` uses: the C11 Annex K form `strtok_s(s1, s1max, s2, ptr)`. `main` implemented the Microsoft three-argument form `strtok_s(str, delim, context)`. A title's call therefore had its `s1max` pointer read as the delimiter string, and its delimiter string treated as the `char**` context and written through. Run against the titles' calls in the comparison below, `main` crashes with an access violation. The new `SonyStrtokS` is the narrow form of the existing `wcstok_s`: - It scans only within `*s1max`, counts it down, and stores the position in `*ptr`. - Violations call the constraint handler with `EINVAL` and a null pointer: - `"strtok_s: bad argument"`: a null `s1max`, `s2` or `ptr`; `*s1max` above `SIZE_MAX >> 3`; or a null `s1` with a null `*ptr`. - `"strtok_s: missing token"`: `s1max` ran out while skipping delimiters. - `"strtok_s: missing delimiter"`: `s1max` ran out inside a token. The default constraint-handler prerequisite is fulfilled on current main `b11c7bf84f5d2f636ff60166cc3371bd071e24d5`: #9935 was closed after its changes landed through #10270. Merging its final head into this main produces the unchanged main tree. Earlier test and title measurements retain their recorded source pins. **Prior work.** @ivica00 described this four-argument signature from a title's disassembly in #8711, which they closed on 2026-10-21 after it needed a rebase. This PR measures the behaviour by running the titles' code and agrees with that description. ### Tested Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, `main` `319621ba2e`. - **Native comparison:** a scratch loader calls the titles' `strtok_s` by NID next to this build's, with the same recording handler installed on both sides. It runs 50,000 chains of up to 6 calls: - random strings over `a`, `b`, `,` and space; - `s1max` from 0 to 19, `2^61 - 1`, `2^61` and `SIZE_MAX`; - a null `s1`, `s1max`, `s2` or `ptr`. Each step compares the returned token, `*s1max`, `*ptr`, the buffer bytes, and the handler's calls, error, pointer and message. | | PPSA07923 | PPSA05916 | |---|---|---| | this PR | **0 of 103,167 steps differ** | **0 of 103,167 steps differ** | | `main` | crashes (access violation) | not run | - **Regression:** `guest_bounds_checking` gains `CheckStrtok`, the narrow version of `CheckWcstok`: the three tokens with their `s1max` and context, and each violation and its message. `guest_crt`'s libSceAmpr checks now use four arguments. - **Test set:** `guest_crt`, `guest_bounds_checking`, `guest_safe_errors` and `guest_strings` pass 3/3. On the full suite, 2858 of 2861 pass: - `guest_job_manager_worker_count` fails as it does on `main` on this machine (#12085 fixes it); - `agc_compute_pipeline_capture_optional_byte_cap` and `guest_http2_transport` failed under `-j32` and pass when rerun alone. Conventions pass. ### 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:51:41 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Thanks for this. I ran a macOS check of the head, since the body only lists Windows.

What I ran

macOS arm64, Apple clang, -fsanitize=address,undefined -fno-sanitize-recover=all, head fefc3f1d98. I built a standalone harness around SonyStrtokS from SonyCrt.hpp, with a recording handler in SonyConstraintHandlerSlot().

  • I copied in this PR's CheckStrtok cases and the guest_crt libSceAmpr cases. All pass. ASan and UBSan report nothing.
  • Differential test: I compared it with a narrow copy of main's wcstok_s_nid_postfix (BoundsChecking.cpp:74). 50,000 random chains of up to 6 calls, using the alphabet ab, , s1max from 0 to 19, 2^61 - 1, 2^61 and SIZE_MAX, and a null s1, s1max, s2 or ptr. Each step compares the token, *s1max, *ptr, the buffer, and the handler's message and pointer. 0 of 137,891 steps differ.

The one textual difference is harmless. wcstok_s guards *remaining = left - length - 1 with if (left != length). Here that line is unconditional, but it is safe: when length == left, the loop already returned "missing delimiter".

Mutants

I applied 25 mutations to SonyStrtokS. 17 are killed by this PR's tests. The 8 below survive them:

Mutant Killed by the tests below
*s1max >= SIZE_MAX >> 3 (rejects the largest valid s1max) yes
no null-s1max check yes (UBSan null dereference)
no null-ptr check yes
length++ == left (accepts a token that fills s1max exactly) yes
sets *s1max = 0 on "missing delimiter" yes
sets *s1max = 0 on "missing token" yes
*ptr = nullptr on "bad argument" no: not covered; same as wcstok_s
s1 || *ptr == s1 start choice equivalent

Here is a test-only diff for CheckStrtok. It kills the first six, and the head passes it:

diff --git a/core/libs/tests/GuestBoundsChecking.cpp b/core/libs/tests/GuestBoundsChecking.cpp
index a9bc1a3b38..250f86914f 100644
--- a/core/libs/tests/GuestBoundsChecking.cpp
+++ b/core/libs/tests/GuestBoundsChecking.cpp
@@ -153,6 +153,25 @@ void CheckStrtok() {
     before = constraintCalls;
     Require(strtok_s_nid_postfix(separators, &remaining, nullptr, &context) == nullptr, "strtok_s accepted null delimiters");
     RequireViolation(before, "strtok_s: bad argument", 22);
+    before = constraintCalls;
+    Require(strtok_s_nid_postfix(separators, nullptr, ",;", &context) == nullptr, "strtok_s accepted a null s1max");
+    RequireViolation(before, "strtok_s: bad argument", 22);
+    before = constraintCalls;
+    Require(strtok_s_nid_postfix(separators, &remaining, ",;", nullptr) == nullptr && remaining == 4, "strtok_s accepted a null ptr");
+    RequireViolation(before, "strtok_s: bad argument", 22);
+    before = constraintCalls;
+    char exact[] = "ab,";
+    remaining = 2;
+    context = nullptr;
+    Require(strtok_s_nid_postfix(exact, &remaining, ",;", &context) == nullptr && remaining == 2 && context == nullptr, "strtok_s accepted a token that fills s1max");
+    RequireViolation(before, "strtok_s: missing delimiter", 22);
+    before = constraintCalls;
+    char largest[] = ",x";
+    remaining = tooLarge - 1;
+    Require(strtok_s_nid_postfix(largest, &remaining, ",;", &context) == largest + 1 && remaining == tooLarge - 3 && context == largest + 2 && constraintCalls == before, "strtok_s rejected the largest s1max");
+    remaining = 3;
+    Require(strtok_s_nid_postfix(separators, &remaining, ",;", &context) == nullptr && remaining == 3 && context == largest + 2, "strtok_s changed its state on a missing token");
+    RequireViolation(before, "strtok_s: missing token", 22);
 }
 
 void CheckWideToMultibyte() {

The expected values follow this PR's rule, which is wcstok_s's rule. Can you confirm the four new cases against the titles' strtok_s with your native comparison? Your s1max set already includes 2^61 - 1, so this should only be a rerun. The cases are: s1max == strlen(token), 2^61 - 1 accepted, and *s1max/*ptr unchanged after the two "missing" violations.

Not blocking: no changes outside this, and the PR does what the title says.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5690742384 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12581#pullrequestreview-5690742384). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T22:51:41Z. **Review:** COMMENTED --- Thanks for this. I ran a macOS check of the head, since the body only lists Windows. ### What I ran macOS arm64, Apple clang, `-fsanitize=address,undefined -fno-sanitize-recover=all`, head `fefc3f1d98`. I built a standalone harness around `SonyStrtokS` from `SonyCrt.hpp`, with a recording handler in `SonyConstraintHandlerSlot()`. - I copied in this PR's `CheckStrtok` cases and the `guest_crt` libSceAmpr cases. **All pass. ASan and UBSan report nothing.** - **Differential test:** I compared it with a narrow copy of `main`'s `wcstok_s_nid_postfix` (`BoundsChecking.cpp:74`). 50,000 random chains of up to 6 calls, using the alphabet `ab, `, `s1max` from 0 to 19, `2^61 - 1`, `2^61` and `SIZE_MAX`, and a null `s1`, `s1max`, `s2` or `ptr`. Each step compares the token, `*s1max`, `*ptr`, the buffer, and the handler's message and pointer. **0 of 137,891 steps differ.** The one textual difference is harmless. `wcstok_s` guards `*remaining = left - length - 1` with `if (left != length)`. Here that line is unconditional, but it is safe: when `length == left`, the loop already returned "missing delimiter". ### Mutants I applied 25 mutations to `SonyStrtokS`. 17 are killed by this PR's tests. The 8 below survive them: | Mutant | Killed by the tests below | |---|---| | `*s1max >= SIZE_MAX >> 3` (rejects the largest valid `s1max`) | yes | | no null-`s1max` check | yes (UBSan null dereference) | | no null-`ptr` check | yes | | `length++ == left` (accepts a token that fills `s1max` exactly) | yes | | sets `*s1max = 0` on "missing delimiter" | yes | | sets `*s1max = 0` on "missing token" | yes | | `*ptr = nullptr` on "bad argument" | no: not covered; same as `wcstok_s` | | `s1 \|\| *ptr == s1` start choice | equivalent | Here is a test-only diff for `CheckStrtok`. It kills the first six, and the head passes it: ```diff diff --git a/core/libs/tests/GuestBoundsChecking.cpp b/core/libs/tests/GuestBoundsChecking.cpp index a9bc1a3b38..250f86914f 100644 --- a/core/libs/tests/GuestBoundsChecking.cpp +++ b/core/libs/tests/GuestBoundsChecking.cpp @@ -153,6 +153,25 @@ void CheckStrtok() { before = constraintCalls; Require(strtok_s_nid_postfix(separators, &remaining, nullptr, &context) == nullptr, "strtok_s accepted null delimiters"); RequireViolation(before, "strtok_s: bad argument", 22); + before = constraintCalls; + Require(strtok_s_nid_postfix(separators, nullptr, ",;", &context) == nullptr, "strtok_s accepted a null s1max"); + RequireViolation(before, "strtok_s: bad argument", 22); + before = constraintCalls; + Require(strtok_s_nid_postfix(separators, &remaining, ",;", nullptr) == nullptr && remaining == 4, "strtok_s accepted a null ptr"); + RequireViolation(before, "strtok_s: bad argument", 22); + before = constraintCalls; + char exact[] = "ab,"; + remaining = 2; + context = nullptr; + Require(strtok_s_nid_postfix(exact, &remaining, ",;", &context) == nullptr && remaining == 2 && context == nullptr, "strtok_s accepted a token that fills s1max"); + RequireViolation(before, "strtok_s: missing delimiter", 22); + before = constraintCalls; + char largest[] = ",x"; + remaining = tooLarge - 1; + Require(strtok_s_nid_postfix(largest, &remaining, ",;", &context) == largest + 1 && remaining == tooLarge - 3 && context == largest + 2 && constraintCalls == before, "strtok_s rejected the largest s1max"); + remaining = 3; + Require(strtok_s_nid_postfix(separators, &remaining, ",;", &context) == nullptr && remaining == 3 && context == largest + 2, "strtok_s changed its state on a missing token"); + RequireViolation(before, "strtok_s: missing token", 22); } void CheckWideToMultibyte() { ``` The expected values follow this PR's rule, which is `wcstok_s`'s rule. Can you confirm the four new cases against the titles' `strtok_s` with your native comparison? Your `s1max` set already includes `2^61 - 1`, so this should only be a rerun. The cases are: `s1max == strlen(token)`, `2^61 - 1` accepted, and `*s1max`/`*ptr` unchanged after the two "missing" violations. Not blocking: no changes outside this, and the PR does what the title says.
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!12581
No description provided.