fix(libc): leave strtok_r's and wcstok's state at the terminator and a static empty string as titles' libc.prx does #11871
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11871"
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-27T10:56:29Z.
What
strtok_rsets the saved pointer to NULL after the last token and when no token is left, following FreeBSD 11.4 (#4625). Thelibc.prxthat titles ship does neither:"last", or"x:y"with empty delimiters), it leaves the saved pointer at the string's terminator. A laterstrtok_r(NULL, …)continues from there. If the caller has written over that terminator, it returns the new text: after"last", writingxover the old NUL makes the next call return"x"."",",",",,,", or after the last token of",a,,b,"), it points the saved pointer at a static empty string inside the library, the same address every time. That string lies in a read-only segment (PT_LOADR--) of all eleven measured titles, for both functions, so here it is aconstarray, and a write through the pointer faults as it would on the console. Later calls keep returning NULL and leave it there.A title that reads
*saveptrafter the last token gets an empty string on the console and a NULL dereference here.strtok_rnow does the same as the titles' copy.strtok, which runsstrtok_ron a thread-local state, follows. A NULL*saveptrwith a NULL string still returns NULL.wcstokhad the same NULL ends. The titles' copy behaves exactly as itsstrtok_rdoes, measured for"last", the overwritten terminator,",,"and",a,", and the second commit makeswcstokfollow.This reverses the saved-pointer part of #4625, which followed FreeBSD without a console measurement. @QRMOE, for your review, since this changes what your tests checked.
Tested
Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release,
BUILD_TESTING=ON,main3fea2a937b.libc.prxof PPSA07923 and PPSA13934 natively.maineverywhere. Only the saved pointer at the end differs.guest_strings: #4625's checks now expect the measured pointers:second + 3after"y";"last", then the"x"token and its new terminator after the old NUL is overwritten.errnois still preserved.guest_wide_strings: thewcstokchecks expect the same:tail + 3andwhole + 4after a last token that ends the string.tailis overwritten, a resumed call returns the newxtoken attail + 3and leaves the state attail + 4, as on the titles' copy.guest_stringsandguest_wide_stringspass in three runs out of three on the exact headd96d96e28a.main'sStringExtensions.cppandStrings.cppand these tests, both abort (exit 0xc0000409).python tools/check_conventions.py --base origin/mainandgit diff --checkpass.guest_stringsandguest_wide_stringspass three runs each at exact headd96d96e28a; environment and results. Not tested: a console or macOS.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)AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-27T11:39:18Z.
Review: COMMENTED
Thanks for measuring this. #4625 followed FreeBSD 11.4 with no console data, so the titles'
libc.prxshould win. The token-for-token match on 100,000 inputs and the negative control make it convincing. Onmain, nothing outside the two implementations and their tests callsstrtok_rorwcstok, so no other code relied on the NULL state.One question: is the titles' empty string in a writable segment of
libc.prx(.data/.bss) or a read-only one?static char exhausted[] = ""is writable here. A title that writes through*saveptrafter the string is used up, as in your overwritten-terminator case, would change the shared string for every later call. On the console that write might fault instead. If the address you measured falls in read-only data, aconstarray cast tochar*would match it more closely.Nit: the
firstState < first || firstState >= first + sizeof(first)check, and the same check ontokensinGuestWideStrings.cpp, compare pointers into different objects, which C++ leaves unspecified.std::less<>keeps them well-defined.I read the diff but didn't run it; I'm going on your three-of-three runs.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T11:40:01Z.
Review: DISMISSED
Please add a
wcstokregression for the observed overwritten-terminator continuation. The wide-string test currently checks thatlastpoints attail + 3after the final token, but it never overwrites that terminator and resumes withtext == nullptr.GuestStrings.cppcovers that observable case forstrtok_r;wcstokhas a separate implementation, so its corresponding behavior is not protected by a test. Please extend thetailcase to overwrite the terminator, resume, and assert the returned token and updated state.Validation on head
771ea0b3ae35ddd162725873f81d1f7ae7988319:guest_string_testsandguest_wide_string_testsfrom a clean Linux Release build using the pinned submodules.guest_wide_stringspassed.guest_stringsstopped at the host CPU guard (this E5-1620 v2 lacks the required AVX2/FMA/LZCNT/BMI features).qemu-x86_64 -cpu Haswell; both exited successfully. This is emulated execution, not a hardware run.python3 tools/check_conventions.py --base refs/review/upstream-mainandgit diff --check refs/review/upstream-main...HEADpassed.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T10:10:39Z.
Review: APPROVED
Re-reviewed exact head
d96d96e28a595cff2312d4113530dbb8e0eb2663. The follow-up addresses the outstanding points: the exhausted-string behavior is supported by titlelibc.prxmappings reported as read-only across eleven titles; the out-of-object checks now usestd::less<>; andwcstokhas the overwritten-terminator continuation regression. Currentmainhas a clean merge preview. Its changes inStrings.cppand the two tests are in other routines, not these tokenization paths; current-main conventions andgit diff --checkpass. The author reports three repeated Linux runs of both string tests at this head; I did not rerun them locally. No remaining blocker found; approved.