Skip to content

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

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
JJenkx:windows/libc-strtok-s-c11
Nov 5, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
JJenkx:windows/libc-strtok-s-c11

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

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:

    Conventions pass.

Checklist

The strtok_s that titles' libc.prx exports (NID -vXEQdRADLI, PPSA07923
and PPSA05916) is the C11 Annex K form strtok_s(s1, s1max, s2, ptr).
main implemented the Microsoft form strtok_s(str, delim, context), so
a title's s1max pointer was read as the delimiter string and its
delimiters as the context. The function now takes s1max, stops within
it, updates it, and reports "strtok_s: bad argument", "missing token"
and "missing delimiter" through the constraint handler with EINVAL, as
wcstok_s already does. libSceAmpr re-exports the same NID and changes
with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

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
PR 7081 core/libs/prx/libSceAmpr/Export.cpp
core/libs/prx/libc/include/SonyCrt.hpp
core/libs/tests/GuestCrt.cpp
PR 7851 core/libs/prx/libc/include/Api.hpp
PR 8965 core/libs/prx/libc/src/Strings.cpp
PR 9526 core/libs/tests/GuestCrt.cpp
PR 10397 core/libs/prx/libc/include/Api.hpp
PR 10780 core/libs/prx/libc/include/Api.hpp
PR 11831 core/libs/prx/libc/include/Api.hpp
PR 13486 core/libs/prx/libSceAmpr/Export.cpp
core/libs/prx/libSceAmpr/include/Api.hpp
core/libs/prx/libc/include/Api.hpp
core/libs/prx/libc/src/Strings.cpp
PR 15800 core/libs/prx/libc/include/Api.hpp
PR 15801 core/libs/prx/libc/include/Api.hpp
PR 15802 core/libs/prx/libc/include/Api.hpp
core/libs/prx/libc/src/Strings.cpp
PR 15803 core/libs/prx/libc/include/Api.hpp
core/libs/prx/libc/src/Strings.cpp
PR 15804 core/libs/prx/libc/include/Api.hpp
core/libs/prx/libc/src/Strings.cpp
PR 15807 core/libs/prx/libc/include/Api.hpp
core/libs/prx/libc/src/Strings.cpp

@rooklz rooklz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

A null s1max or ptr, a token that fills s1max, the largest valid
s1max, and unchanged state after missing delimiter and missing token.
Every expected value matches titles' libc.prx (PPSA07923, PPSA05916).
Suggested by rooklz in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx

JJenkx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

@rooklz thanks. I confirmed your new cases against the titles' strtok_s natively before taking them. They match this build on both PPSA07923 and PPSA05916, and every expected value in your diff:

case titles
null s1max null, "strtok_s: bad argument", *ptr untouched
null ptr null, "strtok_s: bad argument", *s1max stays 4
"ab," with s1max == 2 null, "strtok_s: missing delimiter", *s1max stays 2, *ptr untouched
",x" with s1max == 2^61 - 1 token at offset 1, *s1max == 2^61 - 3, *ptr at offset 2, no handler call
then ",,,x" with s1max == 3 null, "strtok_s: missing token", *s1max stays 3, *ptr unchanged

Your diff is applied unchanged as 65ac15cd8f (test-only, on fefc3f1d98). guest_bounds_checking, guest_crt, guest_safe_errors and guest_strings pass 3/3 on Windows, and conventions pass. Thanks for the differential against wcstok_s as well.

AI-assisted: yes (Claude Code).

@adriadam10
adriadam10 merged commit fdd5261 into boykopovar:main Nov 5, 2026
19 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked (incl. the added boundary cases) the new SonyStrtokS against the four-argument form (s1max countdown, *ptr update, EINVAL plus the three messages) by tracing CheckStrtok and the libSceAmpr checks line by line; all callers (Api.hpp, Strings.cpp, libSceAmpr) match. Depends on #9935; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants