perf(libc): find strnlen terminators with memchr #17354
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17354"
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 rocklouis055 on 2026-11-08T18:30:43Z.
What
strnlenchecked one byte per step, andstrnlen_sandstrlcatcall it.stpncpycopied and padded one byte at a time.strnlennow finds the terminator with the hostmemchr. Since C11,memchrbehaves as if it reads sequentially and stops at the first match, so it reads no further than the old loop did. The limit is clamped to the end of the address space, so aSIZE_MAXlimit never makes the end pointer wrap. A zero limit returns before callingmemchr.stpncpytakes the length fromstrnlen, copies that prefix with onememmoveand fills the rest with onememset. Overlapping buffers are undefined forstpncpy;memmovekeeps that case well defined on the host. A zero count returns before any call.The results are unchanged, and no exported functions or NIDs are added or changed.
Tested
Ubuntu 26.04, GCC 15.2, Release,
-DBUILD_TESTING=ON.guest_strings,guest_bounded_search_guards,guest_strndup_guarded,guest_strncmp_boundary,guest_strncat_protected,guest_strnstr_boundaryandguest_strncpy_protectedpass.The new
guest_bounded_copy_blockstest runs 40000 random cases against a naive reference:PROT_NONE(PAGE_NOACCESS) page on both sides, often ending exactly at the guard page;countfails;strnlenwithSIZE_MAX.Mutation check. Each change below makes the new test fail:
destination + count.These still pass on glibc, because its
memchr,memmoveandmemsetalready handle the cases:They are kept for hosts whose functions are not as forgiving.
Windows:
StringExtensions.cppand the new test pass a syntax-only build forx86_64-windows-gnu(zig/clang). Not built with MinGW or run on Windows.Performance: I have no title capture. I can't run a title, so there are no
main/PR summary lines.Micro-benchmark through the built
libc.prx, at4e20b1bf98against mainbfdb58119e. Medians of 31 samples, in ns per call. The limit isunits + 1on a string ofa. "pad" copies an empty string, so the whole count is padding.The trade-off is
stpncpywith an empty or very short source:memset;memchrto return before it can start thememset;memsetnow starts one byte earlier, at a different alignment;Order with other open PRs
Strings.cppand other places inlibc/CMakeLists.txt. All four branches merge with each other cleanly (git merge-tree).StringExtensions.cpp. For every PR listed bypr_overlaponlibc/CMakeLists.txt,git merge-treeshows the same conflicting files against this branch as againstmain.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. This PR adds no unimplemented paths..mdfiles or images added to the repository🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T19:17:31Z.
Review: COMMENTED
Windows run of head
4e20b1bf98, merged ontomain9fabd4aa7e. To save builds, this ran in one tree together with 11 other open PRs that touch different code (four libc string speed-ups and eight test-only PRs); all merged cleanly. Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release,BUILD_TESTING=ON, i7-12800H.guest_bounded_copy_blockswith--repeat until-fail:5: 5 / 5 pass.strnlen,stpncpy,memrchr,memccpy,wcslen,wcschr,wcsrchr,wmemchr,wcsnlen_s,wcscmp,wcsncmpandwmemcmpwith byte-by-byte reference loops. It runs 400,000 rounds with random lengths, alphabets and odd or even alignments. Half of the strings end right before aPAGE_NOACCESSpage, andmemrchralso starts right after one, so any read past the bounds faults. With this PR's code (all four speed-ups together), 5,600,000 checks agree and nothing faults.main'sStrings.cppandStringExtensions.cpp, the same tests and the fuzz pass too, so the new test pins the result ofstrnlenandstpncpy, not the speed-up. That's expected for a speed change, but it means the new code paths are covered only by the equivalence checks.I didn't measure the speed.
From reading:
strnlennow relies onmemchrnot reading past the first match whenlimitis larger than the string. The page-guard rows of the fuzz exercise exactly that with UCRT'smemchr, and it holds there.