feat(libc): implement rand_r #3191

Merged
reyani-dev merged 1 commit from upstream-pr/3191 into main 2026-10-11 06:05:27 +00:00
reyani-dev commented 2026-10-11 00:51:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T00:51:26Z.


Resubmission of #2216, which was closed because no title was shown stopping on rand_r. With tzset (#3124) merged, PS5MC (PPSA99420) now stops at rand_r on main 06da1176, per @babyhuey's catalog run with sce_module/libc.prx unwrapped (comment):

app.elf: symbol lookup error: app.elf: undefined symbol: dW3xsu3EgFI

dW3xsu3EgFI is rand_r. With this change the title moves on to rewind (3QIPIh-GDjw, #1871), per the same run. rand_r is still absent on current main, and no open PR implements it. The branch is rebased onto current main. Windows 11 (WinLibs GCC 15.2.0), Release: guest_math passes 3/3, and check_conventions.py and pr_overlap.py --branch pass.


What

Exports rand_r. In FreeBSD, rand and rand_r share one generator step (do_rand in lib/libc/stdlib/rand.c): rand advances the global state, and rand_r advances the caller's unsigned int. This moves the step that rand_nid_postfix already implements into an internal helper (NextRand_nid_no_patch, not exported) used by both, so rand_r matches this libc's rand exactly. rand's behaviour is unchanged. A null state pointer throws std::invalid_argument.

Title: PPSA99420 PS5MC imports it from libSceLibcInternal (resolved through libc.prx), per the #1578 audit. I haven't run the title.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Release, -DBUILD_TESTING=ON:

  • New checks in guest_math_tests, next to tanf:
    • known answers from the Park-Miller step: state 1 → 33613 and state 0 → 16806, each written back;
    • for seeds 0, 1, 12345, 0x7ffffffe and 0xffffffff, eight rand_r(&seed) calls equal srand(seed) followed by eight rand() calls;
    • null state throws.
  • Mutation check: not writing the state back, or advancing the global state instead, each make guest_math fail.
  • cmake --build build --target libs: the patched libc.prx exports dW3xsu3EgFI (rand_r) and still cpCOXWMgha0 (rand), and the helper isn't exported.
  • tools/check_conventions.py passes, and tools/pr_overlap.py --branch reports no overlap. It also merges cleanly with #2038 (lrand48/srand48, added just below srand), checked with git merge-tree.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4815587454 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3191). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T00:51:26Z. --- Resubmission of #2216, which was closed because no title was shown stopping on `rand_r`. With `tzset` (#3124) merged, PS5MC (PPSA99420) now stops at `rand_r` on `main` 06da1176, per @babyhuey's catalog run with `sce_module/libc.prx` unwrapped ([comment](https://github.com/boykopovar/AnyPS5/pull/2216#issuecomment-6103870205)): ``` app.elf: symbol lookup error: app.elf: undefined symbol: dW3xsu3EgFI ``` `dW3xsu3EgFI` is `rand_r`. With this change the title moves on to `rewind` (`3QIPIh-GDjw`, #1871), per the same run. `rand_r` is still absent on current `main`, and no open PR implements it. The branch is rebased onto current `main`. Windows 11 (WinLibs GCC 15.2.0), Release: `guest_math` passes 3/3, and `check_conventions.py` and `pr_overlap.py --branch` pass. --- ### What Exports `rand_r`. In FreeBSD, `rand` and `rand_r` share one generator step (`do_rand` in `lib/libc/stdlib/rand.c`): `rand` advances the global state, and `rand_r` advances the caller's `unsigned int`. This moves the step that `rand_nid_postfix` already implements into an internal helper (`NextRand_nid_no_patch`, not exported) used by both, so `rand_r` matches this libc's `rand` exactly. `rand`'s behaviour is unchanged. A null state pointer throws `std::invalid_argument`. Title: PPSA99420 PS5MC imports it from `libSceLibcInternal` (resolved through `libc.prx`), per the #1578 audit. I haven't run the title. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, `-DBUILD_TESTING=ON`: - New checks in `guest_math_tests`, next to `tanf`: - known answers from the Park-Miller step: state 1 → 33613 and state 0 → 16806, each written back; - for seeds 0, 1, 12345, 0x7ffffffe and 0xffffffff, eight `rand_r(&seed)` calls equal `srand(seed)` followed by eight `rand()` calls; - null state throws. - Mutation check: not writing the state back, or advancing the global state instead, each make `guest_math` fail. - `cmake --build build --target libs`: the patched `libc.prx` exports `dW3xsu3EgFI` (`rand_r`) and still `cpCOXWMgha0` (`rand`), and the helper isn't exported. - `tools/check_conventions.py` passes, and `tools/pr_overlap.py --branch` reports no overlap. It also merges cleanly with #2038 (`lrand48`/`srand48`, added just below `srand`), checked with `git merge-tree`. ### Checklist - [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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-11 00:51:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T00:51:44Z.


Progress report (06da117 → 86db61c)

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

✅ 1 implemented
Library Name
libc rand_r_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103931467 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3191#issuecomment-6103931467). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T00:51:44Z. --- <!-- progress-report --> ### Progress report (06da117 → 86db61c) ➖ **System libraries**: 85.96% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `rand_r_nid_postfix` | </details>
reyani-dev commented 2026-10-11 01:18:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T01:18:20Z.


Fork CI for this head merged onto current main (run 38099847203):

  • Windows: 759/759 passed.
  • Linux: 752/755 passed. The 3 failures are agc_unit_shadow, agc_driver_flat_apertures (timeout) and agc_driver_image_packed16_load, which fail identically in main's own push build (run 38098335801). This PR only touches libc, so they're unrelated.
  • Relinker jobs: green.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104124003 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3191#issuecomment-6104124003). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T01:18:20Z. --- Fork CI for this head merged onto current `main` ([run 38099847203](https://github.com/AchrafReyani/AnyPS5/actions/runs/38099847203)): - **Windows**: 759/759 passed. - **Linux**: 752/755 passed. The 3 failures are `agc_unit_shadow`, `agc_driver_flat_apertures` (timeout) and `agc_driver_image_packed16_load`, which fail identically in `main`'s own push build ([run 38098335801](https://github.com/boykopovar/AnyPS5/actions/runs/38098335801)). This PR only touches libc, so they're unrelated. - Relinker jobs: green.
babyhuey commented 2026-10-11 01:36:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-11T01:36:59Z.


Checked with #3190, #3191 and #3192 stacked on main d31b57a4 (all merge cleanly), catalog builds with sce_module/libc.prx unwrapped. Linux (Ubuntu 24.04, GCC 13) and Windows 11 (WinLibs GCC 15.2), Release: guest_math and guest_strings pass 5/5 (--repeat until-fail:5) on both, with no warnings in Math.cpp or StringExtensions.cpp.

PS5MC (PPSA99420) gets past rand_r on both and stops at setstate:

  • Linux: app.elf: symbol lookup error: app.elf: undefined symbol: woQta4WRpk0
  • Windows: FAIL: unresolved ELF import woQta4WRpk0 (setstate) from libSceLibcInternal.prx
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104257242 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3191#issuecomment-6104257242). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-11T01:36:59Z. --- Checked with #3190, #3191 and #3192 stacked on `main` d31b57a4 (all merge cleanly), catalog builds with `sce_module/libc.prx` unwrapped. Linux (Ubuntu 24.04, GCC 13) and Windows 11 (WinLibs GCC 15.2), Release: `guest_math` and `guest_strings` pass 5/5 (`--repeat until-fail:5`) on both, with no warnings in `Math.cpp` or `StringExtensions.cpp`. PS5MC (PPSA99420) gets past `rand_r` on both and stops at `setstate`: - Linux: `app.elf: symbol lookup error: app.elf: undefined symbol: woQta4WRpk0` - Windows: `FAIL: unresolved ELF import woQta4WRpk0 (setstate) from libSceLibcInternal.prx`
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!3191
No description provided.