feat(libc): implement lfind and lsearch #11621
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11621"
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 B-Howell on 2026-10-26T23:13:44Z.
What
Implement
lfindandlsearchfor the libc PRX, matching POSIX.1-2008 and FreeBSD 11lsearch.c: linear scan returning the first matching element, and forlsearch, append the key and increment*nelpon a miss. A nullnelpthrowsstd::invalid_argument.lfindreturns null for*nelp == 0before checking unused arguments. For nonemptylfindand everylsearchcall, nullbase/key/compareor a zerosizethrowstd::invalid_argument. A*nelp * sizespan pastSIZE_MAXthrowsstd::overflow_errorbefore comparator calls or element access. Forlsearch, a successful match returns immediately; only after the linear scan finishes with no match does the append path check that(*nelp + 1) * sizestill fits inSIZE_MAX, so an existing-element match never requires space for an extra element. Documented indocs/dev/debt/libc.md.Tested
Main refresh at
eb364c8536b8(2026-11-02)Head
eb364c8536b83b72d8f8754a75488bb0ed2e198aordinarily merges previous head0aecada0f0fe8dd2ea79a85f5983d7fb1ce34b2ewith upstream main88e804b0a7df1f1ac373a9e2f72fb378d925fdd4, after #9688 rewrote the neighbouringqsort_sentry indocs/dev/debt/libc.md. That entry was the only conflict: this PR'slfind and lsearchentry stays directly before main's new text, which is kept verbatim. The diff against main is still the four owned files, and its added lines are identical to the previous head's.tools/check_conventions.pyfrom this main passed. No new local build or runtime result is claimed for this head; hosted CI at this head is pending after push.Main refresh at
0aecada0f0fe(2026-11-01)Head
0aecada0f0fe8dd2ea79a85f5983d7fb1ce34b2e/ tree5e2ad2f2207125ea17b4c9d8571ad941f94ed4b1ordinarily merges previous public headb8106dd96f42c6ba387036d6d06bc7faa1daf6dcwith upstream mainb6e3f68173c767f692f6c04eef04c73eab424e04. #13220 has landed: its original commite9f09f54fabb6408d3e2711b8df10c10112ab00eis an ancestor of that main, so the write-watch registration fix now comes from main and this PR no longer changescore/libs/tests/write_watch/CMakeLists.txt. The remaining diff against main is the four owned files (Api.hpp,FileAndHeap.cpp,GuestSearch.cpp,docs/dev/debt/libc.md); its added and removed lines in those files are identical to the previous head's changes. Git's three-way merge was clean; GitHub reported the previous head as conflicting, and a merge using only the older main base conflicts on that shared CMake file.tools/check_conventions.pyfrom this main passed against this head. No new local build or runtime result is claimed for this head; hosted CI at this head is pending after push. Earlier results below keep their original pins.Main refresh at
e4b645a6f531Head
e4b645a6f5316e9aff461654f7f5dcb9b43000c9, tree45ac9db4e287db5761749db5cbaa23491e11f121, ordinarily merges0b349fa4c140634267345d7d43ee18cb02de48b8with mainc0fb4a6aad1012b7e2abecaf2949e3d26c0e525d, observed at 2026-10-30 20:15:30 UTC; published4eff863bc8d0a6d6b2388cd4083bb9c5636f62a7remains an ancestor. This is the documented main baseline for this refresh; later remote-main advances are not certified. The four owned contribution files and their additions remain unchanged. Earlier evidence below keeps its original pins.This new head has source-equivalence evidence: all 82 direct compiled libc/search/nid_patcher source inputs match tested
0b349fa4c140634267345d7d43ee18cb02de48b8, and the libc, NID and third-party source trees match. This is not a new-head binary or test pass. Runtime evidence stays at that exact0b349fa4head: Windows/WinLibs GCC 15.2.0 r7 and WSL Ubuntu 22.04/GCC 11.4, Release, focused build andctest -R '^(guest_search|public_headers)$' -V -j1 --repeat until-fail:3 --timeout 30 --no-tests=errorpassed 12 selected executions across both OS = 6 native guest runs + 6 audit-only header runs, with no failures/skips. No fresh global-header audit ate4b645a6is claimed.The #11621-before-#9935-before-#12574 order, other scoped/shared-file qualifications, future combined validation, reviewer acceptance and actual upstream landing remain separate. The historical approval at
921acc6is not approval of this new head.The audit-only case retains its generated 90-second timeout; the 64 header compile batches are excluded. Fresh hosted CI at the new head is pending after push. No new console, GPU, title, macOS, whole-product or full-suite result is claimed.
Current main integration:
4eff863bc8d0a6d6b2388cd4083bb9c5636f62a7, tree33e9ef395f6582282fd4a72b579efc15480547f4, normally merges main4c8cff191609bc5f28bf9ee2f93672b531285aa4into published head9e09e7efe714275ca7cce7b2f48c3282b38bee07. The owned implementation, declaration and fixture additions are unchanged.Windows 11, WinLibs GCC 15.2.0 r7, Release:
cmake --build build --target guest_search_tests --parallel 2passes.ctest --test-dir build -R '^(guest_search|public_headers)$' -V --repeat until-fail:3passes 2/2 cases, each three repetitions (six passes, no skips), in 46.62 seconds. Conventions against current main pass. No fresh Linux, console, full-suite, title or GPU result is claimed.The historical evidence below retains its original pins/toolchains. The declared #11621-before-#9935-before-#12574 chain remains visible in the later PRs. Current #9688 at
1c8ac1874c0ed5ccafef5112d59da6739d4a2fb6changes independent qsort/qsort_s helpers and adjacent debt; complete shared-file patches show no lfind/lsearch implementation dependency. Its earlier order proposal is unanswered, not an established dependency or a standalone readiness blocker. Preserve both search and sort debt when reconciling the adjacent lines. Other current-head overlap deltas, future combinations and reviewer acceptance remain separately qualified. No all-comments-complete or readiness claim is made.Earlier integrated-head evidence:
Windows 11 Pro, MSYS2 UCRT64 GCC 16.2.0, integrated head
9e09e7efe714275ca7cce7b2f48c3282b38bee07(main319621ba2e578bd053b0baba2877fe70520e0737):cmake --build build-focused --target guest_search_tests --parallel 2passed;ctest --test-dir build-focused -R '^guest_search$' -Vpassed 1/1 (guest_search, test 515, 0.03 sec).python tools/check_conventions.py --base origin/mainpassed after the merge commit. No new Linux or console execution is claimed for this integrated head.Earlier published-head Windows evidence: Windows 11 Pro, MinGW UCRT64 (gcc 16.2.0): built
guest_search_testsand ranctest -R "^guest_search$"->1/1 Test #414: guest_search ..... Passed 0.05 sec. Covers duplicate-first-match, zero-count no-op, byte-exact appended payload, zero-count seed, and that the pre-loop overflow guard fires withcalls == 0and*nelpunchanged. Not run on a console.Linux (WSL Ubuntu 22.04, GCC 11.4, Release) at
a5be9abc16:ctest -R '^guest_search$'passed. With the previous head921acc6and this head'sGuestSearch.cpp, the same test aborts withlfind: invalid arguments, so the null-base zero-count case is covered by the reallibc.prxbuild.Write-watch CI prerequisite
This update merges the original #13220 commit (
e9f09f54fa) as a second parent. It removes only the duplicate execute-fault and read-fault registrations from the shared write-watch CMake file; the function change remains the first-parent history. Historical function-test results retain their original pins as recorded above; the latest paired runtime evidence for #11621 is at0b349fa4, note4b645aor this prerequisite merge. A separate configure-only Windows/Linux comparison generated all 18 unique test registrations after the fix, with no fixture build or runtime tests. The hosted macOS checks on this updated head are pending; no macOS runtime result is claimed here.#13220 landed in upstream main on 2026-11-01. Earlier heads carried its original commit
e9f09f54fabb6408d3e2711b8df10c10112ab00eas a second parent; after the 2026-11-01 main refresh above, the fix comes from main and this PR no longer changes the write-watch CMake file. The historical configure-only comparison and the pins recorded here are unchanged.Candidate qualification at
b8106dd96f42Candidate
b8106dd96f42c6ba387036d6d06bc7faa1daf6dc/ tree452bc21d8a66b75bf5f40d662471a882a1a37512ordinarily merges previous public head9c151b9c5892a1c00749af1614a7bcc5fd04c59dwith frozen upstream-main snapshotf776fddcffd1cfece220ecdd894371988b164f6a. This result is for that frozen composition, not later upstream-main tips. Windows Release with WinLibs GCC 15.2.0 r7, CMake 4.4.4 and Ninja 1.13.2 configured the existingb12250acache and builtguest_search_testswith two jobs.ctest --test-dir b12250a -R '^guest_search$' --output-on-failure -Vpassed 1/1 (guest_search, test 681; 0.04 seconds for the test, 0.14 seconds total), using the freshly built candidatelibc.prxfrom CTest's generated working directory.python tools/check_conventions.py --base origin/maincompleted with 0 errors and two existing debt-length warnings. This is a focused candidate-only Windows result, not a main-vs-candidate comparison, Linux result, full-suite result or hosted-CI result.Checklist
Based on current
main; no other open PR implements the same functionsOne topic per PR; follow-ups go in a new PR
No comments in code except technical debt
Unimplemented paths throw (
NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt fileNo notes, investigation
.mdfiles or images added to the repository (attach them to this PR)New third-party code is a submodule built from source
Generic behaviour, not specific to one title
Depends on: none
Fixes: none
Unblocks: none
GPUs tested: n/a
AI-assisted: yes (Claude Opus 4.7 + Codex)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T23:23:10Z.
Review: APPROVED
Reviewed head
921acc674910090fb1368b6fd313cfc541f54398. The search loops compare the key against elements in table order, return the first match, leavenelpunchanged on a hit, and append/increment only on anlsearchmiss. The checked multiplication bounds are applied before element access; the append bound is applied only after a miss. The tests cover duplicate first-match behavior, unchanged data/count on a hit, append payload/count, empty-table behavior, and overflow rejection before comparator or memory access. I also checked the public declaration and debt entry against the implementation.Validation run locally on Linux (Release): configured the PR worktree, built
guest_search_tests(which also builtlibc.prx), and ranctest -R ^guest_search$→ 1/1 passed. The public-header audit and all 64 compile batches passed (65/65). Hosted conventions, intake, labels, report, register-context, write-watch, ASan, and the completed relinker jobs pass; hosted Linux/Windows full product builds are skipped by path policy. I attempted the documented manual full-build dispatch, but GitHub denied it because the account lacks repository admin rights. The author reports the focused Windows test; I did not independently rerun it.