libc: random, srandom, initstate and setstate are absent imports of eight catalog titles; fix branch ready #3164

Closed
opened 2026-10-10 23:47:33 +00:00 by screamm · 2 comments
screamm commented 2026-10-10 23:47:33 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-10T23:47:33Z.


libc (libSceLibcInternal) does not export the BSD random, srandom, initstate and setstate (NIDs w1o05aHJT4c, sPC7XE6hfFY, IIUY-5hk-4k, woQta4WRpk0, confirmed with tools/nid_names.py against aerolib). An import audit of the homebrew catalog on main 262c0316 (Windows, see my comment on #1578) lists them as absent imports: random in PPSA74126, PPSA85300 (VLC), PPSA99169 (RetroArch), PPSA99177, PPSA99420, PPSA99505, PPSA99515, PPSA99810 (PSBox); srandom in PPSA85300, PPSA99169, PPSA99420, PPSA99810; initstate/setstate in PPSA99177, PPSA99420, PPSA99505, PPSA99515. Each becomes a stop once the title's earlier stop is passed; no run has stopped on them yet.

Fix branch ready: screamm:feat/libc-random-state (dd2e0716, one commit on main 91b13413). I cannot open it as a PR because of the intake check; anyone eligible is welcome to carry it, keeping the commit as is. No open or closed PR implements them.

  • New src/Random.cpp follows FreeBSD 11 lib/libc/stdlib/random.c (stable/11 at 4ab2e064) exactly: the default 128-byte TYPE_3 table, good_rand, the seeding rounds, the degree table and size rounding, the packed type/position word, the bad-state check in setstate; guest long is 64-bit, the seed is cut to 32 bits as in FreeBSD, the state buffer is read and written byte-wise so unaligned buffers are safe. initstate with fewer than 8 bytes returns NULL without touching errno (FreeBSD does not set EINVAL; glibc does). A null state throws. Calls are serialised with one mutex like the existing rand, because unlocked concurrent calls could write outside the guest buffer; the single-thread sequence is unchanged. All of this is in TechnicalDebt, including that the console's generator is unverified.
  • New guest_random_state pins the first values of the default and srandom(1) sequence (1408992891, 1638182910, 83848093, 145962118, 627851256) and of seed 12345, computed from the unchanged FreeBSD file and a separate Python model; plus the 32-bit seed cut, the size rounding, the previous-state returns and resuming through setstate, the bad-state, small-buffer and null cases, and a 4-thread run.

Tested on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, BUILD_TESTING=ON, full build including libs: guest_random_state 3/3; ctest -j8 -E "^agc_driver_|^video_out_flip" 480/480; tools/check_conventions.py --base main and git diff --check pass. Not run on Linux.

Written with Claude Code assistance (implementation by a subagent, reviewed and validated in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5796337453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3164). Originally posted by [screamm](https://github.com/screamm) on 2026-10-10T23:47:33Z. --- libc (libSceLibcInternal) does not export the BSD `random`, `srandom`, `initstate` and `setstate` (NIDs `w1o05aHJT4c`, `sPC7XE6hfFY`, `IIUY-5hk-4k`, `woQta4WRpk0`, confirmed with `tools/nid_names.py` against aerolib). An import audit of the homebrew catalog on `main` 262c0316 (Windows, see my comment on #1578) lists them as absent imports: `random` in PPSA74126, PPSA85300 (VLC), PPSA99169 (RetroArch), PPSA99177, PPSA99420, PPSA99505, PPSA99515, PPSA99810 (PSBox); `srandom` in PPSA85300, PPSA99169, PPSA99420, PPSA99810; `initstate`/`setstate` in PPSA99177, PPSA99420, PPSA99505, PPSA99515. Each becomes a stop once the title's earlier stop is passed; no run has stopped on them yet. **Fix branch ready:** [`screamm:feat/libc-random-state`](https://github.com/screamm/AnyPS5/tree/feat/libc-random-state) (`dd2e0716`, one commit on `main` `91b13413`). I cannot open it as a PR because of the intake check; anyone eligible is welcome to carry it, keeping the commit as is. No open or closed PR implements them. - New `src/Random.cpp` follows FreeBSD 11 `lib/libc/stdlib/random.c` (stable/11 at `4ab2e064`) exactly: the default 128-byte TYPE_3 table, `good_rand`, the seeding rounds, the degree table and size rounding, the packed type/position word, the bad-state check in `setstate`; guest `long` is 64-bit, the seed is cut to 32 bits as in FreeBSD, the state buffer is read and written byte-wise so unaligned buffers are safe. `initstate` with fewer than 8 bytes returns NULL without touching errno (FreeBSD does not set EINVAL; glibc does). A null state throws. Calls are serialised with one mutex like the existing `rand`, because unlocked concurrent calls could write outside the guest buffer; the single-thread sequence is unchanged. All of this is in TechnicalDebt, including that the console's generator is unverified. - New `guest_random_state` pins the first values of the default and `srandom(1)` sequence (1408992891, 1638182910, 83848093, 145962118, 627851256) and of seed 12345, computed from the unchanged FreeBSD file and a separate Python model; plus the 32-bit seed cut, the size rounding, the previous-state returns and resuming through `setstate`, the bad-state, small-buffer and null cases, and a 4-thread run. **Tested** on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `BUILD_TESTING=ON`, full build including `libs`: `guest_random_state` 3/3; `ctest -j8 -E "^agc_driver_|^video_out_flip"` 480/480; `tools/check_conventions.py --base main` and `git diff --check` pass. Not run on Linux. Written with Claude Code assistance (implementation by a subagent, reviewed and validated in this session).
adriadam10 commented 2026-10-11 02:08:45 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T02:08:45Z.


Carried in #3229 with your commit as is. The only change on top moves the TechnicalDebt line to "Unknown function info". I checked the pinned sequences against FreeBSD 11's random.c compiled unchanged, and they match.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104482290 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3164#issuecomment-6104482290). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T02:08:45Z. --- Carried in #3229 with your commit as is. The only change on top moves the TechnicalDebt line to "Unknown function info". I checked the pinned sequences against FreeBSD 11's `random.c` compiled unchanged, and they match.
adriadam10 commented 2026-10-11 02:42:06 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T02:42:06Z.


Fixed by #3229.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104710003 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3164#issuecomment-6104710003). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T02:42:06Z. --- Fixed by #3229.
Sign in to join this conversation.
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#3164
No description provided.