feat(libSceLibcInternal): implement memrchr #1906

Closed
Amirtheshwaran wants to merge 1 commit from upstream-pr/1906 into main
Amirtheshwaran commented 2026-10-08 23:14:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Amirtheshwaran on 2026-10-08T23:14:52Z.


What

Adds the missing memrchr export to libSceLibcInternal, implementing reverse byte search. Homebrew title ProsperoRadio (PPSA99001) imports it from libSceLibcInternal.prx (#1578).

The backwards loop is implemented directly rather than calling a host runtime function because memrchr is a GNU extension absent from Windows runtimes. Searches match the last occurrence of c (converted to unsigned char) within the first n bytes, returning nullptr if n == 0 or no match is found.

Note: Touches the same extern "C" block in libSceLibcInternal/Export.cpp as #1852 (atoll) and #1869 (getprogname), and GuestThreadAtexit.cpp as #1869.

Tested

  • Linux:
    • libSceLibcInternal.prx exports memrchr_nid_postfix (renamed to 5G2ONUzRgjY).
    • ctest -R guest_thread_atexit passes with the new memrchr assertions.
  • Windows 11:
    • python tools/check_conventions.py --base origin/main passes with zero errors.
    • git diff --check passes.
  • Regression assertions in GuestThreadAtexit.cpp covering:
    • n == 0 returns nullptr without reading s (including when s == nullptr)
    • Match at end, middle, and start of buffer
    • Multiple occurrences returning the last (highest index) occurrence
    • Search across binary data containing embedded NUL bytes
    • Search values exceeding unsigned char range (truncated before comparison)
    • Non-matching bytes returning nullptr

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4793910519 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1906). Originally posted by [Amirtheshwaran](https://github.com/Amirtheshwaran) on 2026-10-08T23:14:52Z. --- ### What Adds the missing `memrchr` export to `libSceLibcInternal`, implementing reverse byte search. Homebrew title ProsperoRadio (PPSA99001) imports it from `libSceLibcInternal.prx` (#1578). The backwards loop is implemented directly rather than calling a host runtime function because `memrchr` is a GNU extension absent from Windows runtimes. Searches match the last occurrence of `c` (converted to `unsigned char`) within the first `n` bytes, returning `nullptr` if `n == 0` or no match is found. Note: Touches the same `extern "C"` block in `libSceLibcInternal/Export.cpp` as #1852 (`atoll`) and #1869 (`getprogname`), and `GuestThreadAtexit.cpp` as #1869. ### Tested - Linux: - `libSceLibcInternal.prx` exports `memrchr_nid_postfix` (renamed to `5G2ONUzRgjY`). - `ctest -R guest_thread_atexit` passes with the new `memrchr` assertions. - Windows 11: - `python tools/check_conventions.py --base origin/main` passes with zero errors. - `git diff --check` passes. - Regression assertions in `GuestThreadAtexit.cpp` covering: - `n == 0` returns `nullptr` without reading `s` (including when `s == nullptr`) - Match at end, middle, and start of buffer - Multiple occurrences returning the last (highest index) occurrence - Search across binary data containing embedded NUL bytes - Search values exceeding `unsigned char` range (truncated before comparison) - Non-matching bytes returning `nullptr` ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-08 23:15:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T23:15:17Z.


Progress report (f145139 → 109aef1)

➖ System libraries: 84.76% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libSceLibcInternal memrchr_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6070917361 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1906#issuecomment-6070917361). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T23:15:17Z. --- <!-- progress-report --> ### Progress report (f145139 → 109aef1) ➖ **System libraries**: 84.76% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libSceLibcInternal | `memrchr_nid_postfix` | </details>
github-actions[bot] commented 2026-10-08 23:28:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T23:28:05Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1852 core/libs/prx/libSceLibcInternal/Export.cpp L106
#1869 core/libs/prx/libSceLibcInternal/Export.cpp L106
core/libs/tests/GuestThreadAtexit.cpp L12, L58
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6071069274 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1906#issuecomment-6071069274). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T23:28:05Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1852 | `core/libs/prx/libSceLibcInternal/Export.cpp` [L106](https://github.com/boykopovar/AnyPS5/blob/d9484c719b2a5ac6201e27f8657e0fb0f01e648c/core/libs/prx/libSceLibcInternal/Export.cpp#L106) | | | | #1869 | `core/libs/prx/libSceLibcInternal/Export.cpp` [L106](https://github.com/boykopovar/AnyPS5/blob/d9484c719b2a5ac6201e27f8657e0fb0f01e648c/core/libs/prx/libSceLibcInternal/Export.cpp#L106)<br>`core/libs/tests/GuestThreadAtexit.cpp` [L12](https://github.com/boykopovar/AnyPS5/blob/d9484c719b2a5ac6201e27f8657e0fb0f01e648c/core/libs/tests/GuestThreadAtexit.cpp#L12), [L58](https://github.com/boykopovar/AnyPS5/blob/d9484c719b2a5ac6201e27f8657e0fb0f01e648c/core/libs/tests/GuestThreadAtexit.cpp#L58) | | |
klosixgaming-arch commented 2026-10-09 00:42:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-09T00:42:52Z.

Review: APPROVED


Correct, clean memrchr (backwards memchr). Handles the n == 0 edge case, casts int c to unsigned char (so values like 0x142/0x200 correctly compare their low byte), and returns the last match or nullptr. The test covers the happy path, not-found, binary data with embedded NULs, and the int→byte truncation — good coverage for a one-line function.

Minor: the test lives in GuestThreadAtexit.cpp, which is otherwise about thread atexit destructors. A memrchr test doesn't belong there — consider moving it to a string/mem test file (or a new one) so the suite stays organized.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5464498552 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1906#pullrequestreview-5464498552). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-09T00:42:52Z. **Review:** APPROVED --- Correct, clean `memrchr` (backwards `memchr`). Handles the `n == 0` edge case, casts `int c` to `unsigned char` (so values like 0x142/0x200 correctly compare their low byte), and returns the last match or nullptr. The test covers the happy path, not-found, binary data with embedded NULs, and the int→byte truncation — good coverage for a one-line function. Minor: the test lives in `GuestThreadAtexit.cpp`, which is otherwise about thread atexit destructors. A `memrchr` test doesn't belong there — consider moving it to a string/mem test file (or a new one) so the suite stays organized. Written with AI assistance.
oneandonlydean commented 2026-10-09 09:30:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:30:31Z.


Closing: ProsperoRadio (PPSA99001) stops on the missing libSceOpusDec.prx on main, and PICO-8 reaches memrchr only after five other missing imports (#1578). Reopen when it's a title's next stop on main, with the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078242117 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1906#issuecomment-6078242117). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:30:31Z. --- Closing: ProsperoRadio (PPSA99001) stops on the missing `libSceOpusDec.prx` on main, and PICO-8 reaches `memrchr` only after five other missing imports (#1578). Reopen when it's a title's next stop on main, with the log line.

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