feat(libc): implement rand_r #2216

Closed
reyani-dev wants to merge 1 commit from upstream-pr/2216 into main
reyani-dev commented 2026-10-09 11:51:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T11:51:02Z.


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:4800014076 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2216). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T11:51:02Z. --- ### 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-09 11:51:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T11:51:25Z.


Progress report (d70b899 → 41ddf48)

📈 System libraries: 84.7% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libc rand_r_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080290637 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2216#issuecomment-6080290637). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T11:51:25Z. --- <!-- progress-report --> ### Progress report (d70b899 → 41ddf48) 📈 **System libraries**: 84.7% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `rand_r_nid_postfix` | </details>
reyani-dev commented 2026-10-09 12:12:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T12:12:07Z.


Full Linux and Windows builds and tests for this head (41ddf48e), using the project's Build workflow. I can't start the upstream manual run, so I ran the same build.yml in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current main (d70b8998):

Job Result
linux success; full build, ctest: 516/516 passed
windows success; full build, ctest: 507/507 passed
relinker (Linux, Windows, macOS) success

Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37926277687

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080629289 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2216#issuecomment-6080629289). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T12:12:07Z. --- Full Linux and Windows builds and tests for this head (`41ddf48e`), using the project's Build workflow. I can't start the upstream manual run, so I ran the same `build.yml` in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current `main` (`d70b8998`): | Job | Result | |---|---| | [linux](https://github.com/AchrafReyani/AnyPS5/actions/runs/37926277687/job/113805917029) | success; full build, `ctest`: 516/516 passed | | [windows](https://github.com/AchrafReyani/AnyPS5/actions/runs/37926277687/job/113805917566) | success; full build, `ctest`: 507/507 passed | | relinker (Linux, Windows, macOS) | success | Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37926277687
oneandonlydean commented 2026-10-09 20:10:10 +00:00 (Migrated from github.com)

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


Same as #2167, for rand_r.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088445193 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2216#issuecomment-6088445193). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T20:10:10Z. --- Same as #2167, for `rand_r`.
babyhuey commented 2026-10-11 00:43:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-11T00:43:32Z.


@AchrafReyani rand_r is now a title's next stop on main. With tzset (#3124) merged, PS5MC (PPSA99420) on main 06da1176 reports this on Linux (catalog build, sce_module/libc.prx unwrapped as #2605 requires):

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

This head (41ddf48e) merges cleanly onto 06da1176, guest_math passes 5/5 (--repeat until-fail:5), and PS5MC then stops at rewind (3QIPIh-GDjw, #1871).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103870205 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2216#issuecomment-6103870205). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-11T00:43:32Z. --- @AchrafReyani `rand_r` is now a title's next stop on `main`. With `tzset` (#3124) merged, PS5MC (PPSA99420) on `main` 06da1176 reports this on Linux (catalog build, `sce_module/libc.prx` unwrapped as #2605 requires): `app.elf: symbol lookup error: app.elf: undefined symbol: dW3xsu3EgFI` This head (41ddf48e) merges cleanly onto 06da1176, `guest_math` passes 5/5 (`--repeat until-fail:5`), and PS5MC then stops at `rewind` (`3QIPIh-GDjw`, #1871).
reyani-dev commented 2026-10-11 00:51:28 +00:00 (Migrated from github.com)

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


Thanks @babyhuey, resubmitted as #3191, rebased onto current main with your stop.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103929404 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2216#issuecomment-6103929404). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T00:51:28Z. --- Thanks @babyhuey, resubmitted as #3191, rebased onto current `main` with your stop.

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