feat(libc): implement binary-safe memmem #2058

Closed
QRMOE wants to merge 3 commits from upstream-pr/2058 into main
QRMOE commented 2026-10-09 06:09:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-09T06:09:25Z.


What

Adds memmem (AyXlXD4Y3n8) using bounded binary search. Returns the first match, handles embedded NUL bytes, returns the haystack for an empty needle, and rejects a needle longer than the haystack without reading it. No heap allocation or guest errno changes. Reference: FreeBSD memmem.

Tested

guest_memmem passes: 5,000 generated cases checked against an independent binary string-search oracle, explicit repeated-prefix and NUL/high-bit inputs, and both buffers positioned immediately before inaccessible pages over multiple lengths.

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7 UCRT/posix/SEH, Ninja Release, BUILD_TESTING=ON, ANYPS5_ENABLE_SPIRV_TOOLS=ON; base f145139676a48306681d3a4d5fabf83938759daf.

Combined validation of these three independent branches: full libs target built successfully, including NID patching; guest_memmem, guest_bcopy, both line-input tests, guest_standard_streams, and guest_errors pass (6/6). All four NIDs are present in patched libc.prx. Conventions, diff whitespace and test registration checks pass. The branches were also built and tested individually before integration.

Before publication, actual library diffs of 559 candidate PR heads were checked for these names and NIDs, including broad PRs and newer heads; no other implementation of these four exports was found. The candidates cover the cached open-PR list and every newer PR head; some candidates may have closed since that list was read.

Linux, console behavior, games and the complete CTest suite have not been run. This adds documented libc interfaces; no particular title starting successfully is claimed.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR
  • No comments added in code
  • No silent success stubs added
  • No investigation notes, reports or images added to the repository
  • No third-party dependencies added
  • Generic behavior, not specific to one title
  • Depends on: none
  • AI-assisted: yes — OpenAI Codex assisted with implementation, tests and this description.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4796702590 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2058). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-09T06:09:25Z. --- ### What Adds `memmem` (`AyXlXD4Y3n8`) using bounded binary search. Returns the first match, handles embedded NUL bytes, returns the haystack for an empty needle, and rejects a needle longer than the haystack without reading it. No heap allocation or guest errno changes. Reference: [FreeBSD memmem](https://github.com/freebsd/freebsd-src/blob/main/lib/libc/string/memmem.c). ### Tested `guest_memmem` passes: 5,000 generated cases checked against an independent binary string-search oracle, explicit repeated-prefix and NUL/high-bit inputs, and both buffers positioned immediately before inaccessible pages over multiple lengths. Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7 UCRT/posix/SEH, Ninja Release, `BUILD_TESTING=ON`, `ANYPS5_ENABLE_SPIRV_TOOLS=ON`; base `f145139676a48306681d3a4d5fabf83938759daf`. Combined validation of these three independent branches: full `libs` target built successfully, including NID patching; `guest_memmem`, `guest_bcopy`, both line-input tests, `guest_standard_streams`, and `guest_errors` pass (6/6). All four NIDs are present in patched `libc.prx`. Conventions, diff whitespace and test registration checks pass. The branches were also built and tested individually before integration. Before publication, actual library diffs of 559 candidate PR heads were checked for these names and NIDs, including broad PRs and newer heads; no other implementation of these four exports was found. The candidates cover the cached open-PR list and every newer PR head; some candidates may have closed since that list was read. Linux, console behavior, games and the complete CTest suite have not been run. This adds documented libc interfaces; no particular title starting successfully is claimed. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR - [x] No comments added in code - [x] No silent success stubs added - [x] No investigation notes, reports or images added to the repository - [x] No third-party dependencies added - [x] Generic behavior, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes — OpenAI Codex assisted with implementation, tests and this description.
MarcZX10 commented 2026-10-09 06:11:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T06:11:00Z.

Review: APPROVED


Reviewed the full diff against the FreeBSD memmem contract. The implementation handles the zero-length needle and too-short haystack before forming/dereferencing ranges, searches bytes (including embedded NULs), and returns the first match. The randomized binary oracle and guard-page cases are useful boundary coverage. I built and ran the exact head on Linux with GCC 12; guest_memmem passed (1/1, 0.02 s). Conventions and diff checks pass. The PR reports Windows builds and tests; upstream currently has no check results for this fork head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5466369697 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2058#pullrequestreview-5466369697). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T06:11:00Z. **Review:** APPROVED --- Reviewed the full diff against the FreeBSD memmem contract. The implementation handles the zero-length needle and too-short haystack before forming/dereferencing ranges, searches bytes (including embedded NULs), and returns the first match. The randomized binary oracle and guard-page cases are useful boundary coverage. I built and ran the exact head on Linux with GCC 12; guest_memmem passed (1/1, 0.02 s). Conventions and diff checks pass. The PR reports Windows builds and tests; upstream currently has no check results for this fork head.
oneandonlydean commented 2026-10-09 08:55:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T08:55:59Z.


Closing: no title is shown to reach memmem on main, and library functions go in when one does. Please reopen with the title ID and the log line where it stops if you find one.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077723153 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2058#issuecomment-6077723153). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T08:55:59Z. --- Closing: no title is shown to reach `memmem` on main, and library functions go in when one does. Please reopen with the title ID and the log line where it stops if you find one.

Pull request closed

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!2058
No description provided.