feat(libc): implement lfind and lsearch #11621

Merged
B-Howell merged 13 commits from upstream-pr/11621 into main 2026-11-04 17:35:47 +00:00
B-Howell commented 2026-10-26 23:13:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-10-26T23:13:44Z.


What

Implement lfind and lsearch for the libc PRX, matching POSIX.1-2008 and FreeBSD 11 lsearch.c: linear scan returning the first matching element, and for lsearch, append the key and increment *nelp on a miss. A null nelp throws std::invalid_argument. lfind returns null for *nelp == 0 before checking unused arguments. For nonempty lfind and every lsearch call, null base/key/compare or a zero size throw std::invalid_argument. A *nelp * size span past SIZE_MAX throws std::overflow_error before comparator calls or element access. For lsearch, a successful match returns immediately; only after the linear scan finishes with no match does the append path check that (*nelp + 1) * size still fits in SIZE_MAX, so an existing-element match never requires space for an extra element. Documented in docs/dev/debt/libc.md.

Tested

Main refresh at eb364c8536b8 (2026-11-02)

Head eb364c8536b83b72d8f8754a75488bb0ed2e198a ordinarily merges previous head 0aecada0f0fe8dd2ea79a85f5983d7fb1ce34b2e with upstream main 88e804b0a7df1f1ac373a9e2f72fb378d925fdd4, after #9688 rewrote the neighbouring qsort_s entry in docs/dev/debt/libc.md. That entry was the only conflict: this PR's lfind and lsearch entry 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.py from 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 / tree 5e2ad2f2207125ea17b4c9d8571ad941f94ed4b1 ordinarily merges previous public head b8106dd96f42c6ba387036d6d06bc7faa1daf6dc with upstream main b6e3f68173c767f692f6c04eef04c73eab424e04. #13220 has landed: its original commit e9f09f54fabb6408d3e2711b8df10c10112ab00e is an ancestor of that main, so the write-watch registration fix now comes from main and this PR no longer changes core/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.py from 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 e4b645a6f531

Head e4b645a6f5316e9aff461654f7f5dcb9b43000c9, tree 45ac9db4e287db5761749db5cbaa23491e11f121, ordinarily merges 0b349fa4c140634267345d7d43ee18cb02de48b8 with main c0fb4a6aad1012b7e2abecaf2949e3d26c0e525d, observed at 2026-10-30 20:15:30 UTC; published 4eff863bc8d0a6d6b2388cd4083bb9c5636f62a7 remains 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 exact 0b349fa4 head: Windows/WinLibs GCC 15.2.0 r7 and WSL Ubuntu 22.04/GCC 11.4, Release, focused build and ctest -R '^(guest_search|public_headers)$' -V -j1 --repeat until-fail:3 --timeout 30 --no-tests=error passed 12 selected executions across both OS = 6 native guest runs + 6 audit-only header runs, with no failures/skips. No fresh global-header audit at e4b645a6 is 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 921acc6 is 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, tree 33e9ef395f6582282fd4a72b579efc15480547f4, normally merges main 4c8cff191609bc5f28bf9ee2f93672b531285aa4 into published head 9e09e7efe714275ca7cce7b2f48c3282b38bee07. 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 2 passes. ctest --test-dir build -R '^(guest_search|public_headers)$' -V --repeat until-fail:3 passes 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 1c8ac1874c0ed5ccafef5112d59da6739d4a2fb6 changes 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 (main 319621ba2e578bd053b0baba2877fe70520e0737): cmake --build build-focused --target guest_search_tests --parallel 2 passed; ctest --test-dir build-focused -R '^guest_search$' -V passed 1/1 (guest_search, test 515, 0.03 sec). python tools/check_conventions.py --base origin/main passed 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_tests and ran ctest -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 with calls == 0 and *nelp unchanged. Not run on a console.

Linux (WSL Ubuntu 22.04, GCC 11.4, Release) at a5be9abc16: ctest -R '^guest_search$' passed. With the previous head 921acc6 and this head's GuestSearch.cpp, the same test aborts with lfind: invalid arguments, so the null-base zero-count case is covered by the real libc.prx build.

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 at 0b349fa4, not e4b645a or 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 e9f09f54fabb6408d3e2711b8df10c10112ab00e as 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 b8106dd96f42

Candidate b8106dd96f42c6ba387036d6d06bc7faa1daf6dc / tree 452bc21d8a66b75bf5f40d662471a882a1a37512 ordinarily merges previous public head 9c151b9c5892a1c00749af1614a7bcc5fd04c59d with frozen upstream-main snapshot f776fddcffd1cfece220ecdd894371988b164f6a. 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 existing b12250a cache and built guest_search_tests with two jobs. ctest --test-dir b12250a -R '^guest_search$' --output-on-failure -V passed 1/1 (guest_search, test 681; 0.04 seconds for the test, 0.14 seconds total), using the freshly built candidate libc.prx from CTest's generated working directory. python tools/check_conventions.py --base origin/main completed 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 functions

  • One 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 file

  • No notes, investigation .md files 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)

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5026048767 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11621). Originally posted by [B-Howell](https://github.com/B-Howell) on 2026-10-26T23:13:44Z. --- ### What Implement `lfind` and `lsearch` for the libc PRX, matching POSIX.1-2008 and FreeBSD 11 `lsearch.c`: linear scan returning the first matching element, and for `lsearch`, append the key and increment `*nelp` on a miss. A null `nelp` throws `std::invalid_argument`. `lfind` returns null for `*nelp == 0` before checking unused arguments. For nonempty `lfind` and every `lsearch` call, null `base`/`key`/`compare` or a zero `size` throw `std::invalid_argument`. A `*nelp * size` span past `SIZE_MAX` throws `std::overflow_error` before comparator calls or element access. For `lsearch`, a successful match returns immediately; only after the linear scan finishes with no match does the append path check that `(*nelp + 1) * size` still fits in `SIZE_MAX`, so an existing-element match never requires space for an extra element. Documented in `docs/dev/debt/libc.md`. ### Tested #### Main refresh at `eb364c8536b8` (2026-11-02) Head `eb364c8536b83b72d8f8754a75488bb0ed2e198a` ordinarily merges previous head `0aecada0f0fe8dd2ea79a85f5983d7fb1ce34b2e` with upstream main `88e804b0a7df1f1ac373a9e2f72fb378d925fdd4`, after #9688 rewrote the neighbouring `qsort_s` entry in `docs/dev/debt/libc.md`. That entry was the only conflict: this PR's `lfind and lsearch` entry 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.py` from 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` / tree `5e2ad2f2207125ea17b4c9d8571ad941f94ed4b1` ordinarily merges previous public head `b8106dd96f42c6ba387036d6d06bc7faa1daf6dc` with upstream main `b6e3f68173c767f692f6c04eef04c73eab424e04`. #13220 has landed: its original commit `e9f09f54fabb6408d3e2711b8df10c10112ab00e` is an ancestor of that main, so the write-watch registration fix now comes from main and this PR no longer changes `core/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.py` from 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 `e4b645a6f531` Head `e4b645a6f5316e9aff461654f7f5dcb9b43000c9`, tree `45ac9db4e287db5761749db5cbaa23491e11f121`, ordinarily merges `0b349fa4c140634267345d7d43ee18cb02de48b8` with main `c0fb4a6aad1012b7e2abecaf2949e3d26c0e525d`, observed at 2026-10-30 20:15:30 UTC; published `4eff863bc8d0a6d6b2388cd4083bb9c5636f62a7` remains 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 exact `0b349fa4` head: Windows/WinLibs GCC 15.2.0 r7 and WSL Ubuntu 22.04/GCC 11.4, Release, focused build and `ctest -R '^(guest_search|public_headers)$' -V -j1 --repeat until-fail:3 --timeout 30 --no-tests=error` passed **12 selected executions across both OS = 6 native guest runs + 6 audit-only header runs**, with no failures/skips. No fresh global-header audit at `e4b645a6` is 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 `921acc6` is 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`, tree `33e9ef395f6582282fd4a72b579efc15480547f4`, normally merges main `4c8cff191609bc5f28bf9ee2f93672b531285aa4` into published head `9e09e7efe714275ca7cce7b2f48c3282b38bee07`. 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 2` passes. `ctest --test-dir build -R '^(guest_search|public_headers)$' -V --repeat until-fail:3` passes 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 `1c8ac1874c0ed5ccafef5112d59da6739d4a2fb6` changes 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` (main `319621ba2e578bd053b0baba2877fe70520e0737`): `cmake --build build-focused --target guest_search_tests --parallel 2` passed; `ctest --test-dir build-focused -R '^guest_search$' -V` passed 1/1 (`guest_search`, test 515, 0.03 sec). `python tools/check_conventions.py --base origin/main` passed after the merge commit. No new Linux or console execution is claimed for this integrated head. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Earlier published-head Windows evidence: Windows 11 Pro, MinGW UCRT64 (gcc 16.2.0): built `guest_search_tests` and ran `ctest -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 with `calls == 0` and `*nelp` unchanged. Not run on a console. Linux (WSL Ubuntu 22.04, GCC 11.4, Release) at a5be9abc16: `ctest -R '^guest_search$'` passed. With the previous head 921acc6 and this head's `GuestSearch.cpp`, the same test aborts with `lfind: invalid arguments`, so the null-base zero-count case is covered by the real `libc.prx` build. ### Write-watch CI prerequisite This update merges the original #13220 commit (e9f09f54fabb6408d3e2711b8df10c10112ab00e) 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 at 0b349fa4, not e4b645a or 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 `e9f09f54fabb6408d3e2711b8df10c10112ab00e` as 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 `b8106dd96f42` Candidate `b8106dd96f42c6ba387036d6d06bc7faa1daf6dc` / tree `452bc21d8a66b75bf5f40d662471a882a1a37512` ordinarily merges previous public head `9c151b9c5892a1c00749af1614a7bcc5fd04c59d` with frozen upstream-main snapshot `f776fddcffd1cfece220ecdd894371988b164f6a`. 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 existing `b12250a` cache and built `guest_search_tests` with two jobs. `ctest --test-dir b12250a -R '^guest_search$' --output-on-failure -V` passed 1/1 (`guest_search`, test 681; 0.04 seconds for the test, 0.14 seconds total), using the freshly built candidate `libc.prx` from CTest's generated working directory. `python tools/check_conventions.py --base origin/main` completed 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 <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Opus 4.7 + Codex)
MarcZX10 commented 2026-10-26 23:23:10 +00:00 (Migrated from github.com)

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, leave nelp unchanged on a hit, and append/increment only on an lsearch miss. 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 built libc.prx), and ran ctest -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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5660404913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11621#pullrequestreview-5660404913). Originally posted by [MarcZX10](https://github.com/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, leave `nelp` unchanged on a hit, and append/increment only on an `lsearch` miss. 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 built `libc.prx`), and ran `ctest -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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!11621
No description provided.