fix(libc): take strtok_s's C11 s1max argument as titles' libc.prx does #12581
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12581"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.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 (#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)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, headfefc3f1d98. I built a standalone harness aroundSonyStrtokSfromSonyCrt.hpp, with a recording handler inSonyConstraintHandlerSlot().CheckStrtokcases and theguest_crtlibSceAmpr cases. All pass. ASan and UBSan report nothing.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_sguards*remaining = left - length - 1withif (left != length). Here that line is unconditional, but it is safe: whenlength == 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:*s1max >= SIZE_MAX >> 3(rejects the largest valids1max)s1maxcheckptrchecklength++ == left(accepts a token that fillss1maxexactly)*s1max = 0on "missing delimiter"*s1max = 0on "missing token"*ptr = nullptron "bad argument"wcstok_ss1 || *ptr == s1start choiceHere is a test-only diff for
CheckStrtok. It kills the first six, and the head passes it: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_swith your native comparison? Yours1maxset already includes2^61 - 1, so this should only be a rerun. The cases are:s1max == strlen(token),2^61 - 1accepted, and*s1max/*ptrunchanged after the two "missing" violations.Not blocking: no changes outside this, and the PR does what the title says.