Repository navigation
fix(libc): take strtok_s's C11 s1max argument as titles' libc.prx does - #12581
Conversation
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>
|
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.
|
rooklz
left a comment
There was a problem hiding this comment.
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
CheckStrtokcases and theguest_crtlibSceAmpr cases. All pass. ASan and UBSan report nothing. - Differential test: I compared it with a narrow copy of
main'swcstok_s_nid_postfix(BoundsChecking.cpp:74). 50,000 random chains of up to 6 calls, using the alphabetab,,s1maxfrom 0 to 19,2^61 - 1,2^61andSIZE_MAX, and a nulls1,s1max,s2orptr. 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>
|
@rooklz thanks. I confirmed your new cases against the titles'
Your diff is applied unchanged as AI-assisted: yes (Claude Code). |
|
Checked (incl. the added boundary cases) the new |
What
strtok_s(NID-vXEQdRADLI, libc and the libSceAmpr re-export) now takes the four arguments that titles'libc.prxuses: the C11 Annex K formstrtok_s(s1, s1max, s2, ptr).mainimplemented the Microsoft three-argument formstrtok_s(str, delim, context). A title's call therefore had itss1maxpointer read as the delimiter string, and its delimiter string treated as thechar**context and written through. Run against the titles' calls in the comparison below,maincrashes with an access violation.The new
SonyStrtokSis the narrow form of the existingwcstok_s:*s1max, counts it down, and stores the position in*ptr.EINVALand a null pointer:"strtok_s: bad argument": a nulls1max,s2orptr;*s1maxaboveSIZE_MAX >> 3; or a nulls1with a null*ptr."strtok_s: missing token":s1maxran out while skipping delimiters."strtok_s: missing delimiter":s1maxran 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,
main319621ba2e.Native comparison: a scratch loader calls the titles'
strtok_sby 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:a,b,,and space;s1maxfrom 0 to 19,2^61 - 1,2^61andSIZE_MAX;s1,s1max,s2orptr.Each step compares the returned token,
*s1max,*ptr, the buffer bytes, and the handler's calls, error, pointer and message.mainRegression:
guest_bounds_checkinggainsCheckStrtok, the narrow version ofCheckWcstok: the three tokens with theirs1maxand 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_errorsandguest_stringspass 3/3. On the full suite, 2858 of 2861 pass:guest_job_manager_worker_countfails as it does onmainon this machine (fix(libSceJobManager): spin idle workers without taking the manager lock #12085 fixes it);agc_compute_pipeline_capture_optional_byte_capandguest_http2_transportfailed under-j32and pass when rerun alone.Conventions pass.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)