fix(libc): seed random() and its default state as measured on a PS5 #8979

Merged
ionut27001 merged 1 commit from upstream-pr/8979 into main 2026-10-24 12:51:01 +00:00
ionut27001 commented 2026-10-21 08:13:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ionut27001 on 2026-10-21T08:13:56Z.


What

The libSceLibcInternal of a payload process on a PS5 (firmware 12.70) does not seed random() the way AnyPS5 did. Probe p83 (case random lines) measured, in that process:

  • a fresh process draws 1804289383, 846930886, 1681692777, 1714636915, 1957747793, the same as srandom(1), which continues with 424238335; AnyPS5 drew 1408992891, 1638182910, 83848093, ....
  • srandom(0) draws 577655601, 1248161417, 192959563, ..., srandom(2) draws 1505335290, ..., srandom(12345) draws 383100999, 858300821, 357768173, ..., srandom(0xffffffff) draws 254925627, ... and srandom(0x80000000) draws 1336741213, ....
  • initstate with seed 1 returns null at 0 and 7 bytes. At 8, 15, 31, 32, 63, 64, 127, 128, 129, 255, 256, 257 and 1000 bytes it returns non-null, and the saved type word (after the draws) and first state word are 000000001465fc2d for 8, 15 and 31 bytes, 0100000082016ee0 for 32 and 63, 02000000fea8f5e2 for 64 and 127, 03000000b1391599 for 128, 129 and 255, and 0400000032ba371f for 256, 257 and 1000. The five values drawn after it are the same within each of those groups; the 128-byte group draws the fresh-process sequence.

All of these follow from good_rand(x) reading its argument as an int32, replacing 0 with 123459876 and computing x = 16807 * (x % 127773) - 2836 * (x / 127773), adding 0x7fffffff to a negative result, with the additive feedback generator and the 10 * degree discards (50 for type 0) that AnyPS5 already had. A Python model of that arithmetic reproduces every measured value. AnyPS5's GoodRand instead took the argument modulo 0x7ffffffe, added one and subtracted one from the result, and its default state was a precomputed table that no srandom reproduces.

Changes:

  • GoodRand computes the measured formula.
  • The default state is built at load by seeding the 128-byte table with 1, so a fresh process equals srandom(1).
  • The new guest_libc-random-seeding test pins the sequences above (the first five values of a fresh process, the first six after each of the six measured seeds, the null returns, and for each of the 13 measured sizes the saved type word and first state word and the five drawn values).
  • guest_random_state expected the old generator; its sequences are replaced with the new generator's. Its srandom(1) and srandom(12345) values are measured; its initstate(42) and initstate(7) values and the 7th value after srandom(12345) are not measured (seeds 42 and 7 and a 7th draw were not probed) and follow from the formula, so they only guard against regressions.
  • The debt entry for random, srandom, initstate, setstate now lists what was not measured first.

Not measured and not changed: setstate, which state initstate returns, errno, the null-state throw, initstate seeds other than 1, a title process, and rand/srand. srandomdev is not in this change; #8730 adds it to the same Random.cpp. This change does not depend on it and it does not depend on this one, but its GuestRandomDeviceSeed.cpp and the size-8 check it adds to GuestRandomState.cpp compute the old type 0 generator, and the CMake and debt hunks next to this change need a rebase, whichever merges second.

Tested

  • OS and toolchain: Windows 11, WinLibs GCC 15.2.0 (r7), Ninja, Release; the build completes except for one unrelated AGC test that does not compile with MinGW (alignas(65536))
  • ctest --test-dir build -R "guest_random|guest_libc" run twice: 8 of 8 passed both times, including guest_libc-random-seeding and guest_random_state
  • The same two tests with core/libs/prx/libc/src/Random.cpp reverted to main (the new test must fail): both guest_random_state and guest_libc-random-seeding fail; with the file restored both pass
  • python tools/check_conventions.py --base origin/main: All conventions checks passed
  • Title or homebrew: not tested; the values are from a payload process, not a title

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4948507108 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8979). Originally posted by [ionut27001](https://github.com/ionut27001) on 2026-10-21T08:13:56Z. --- ### What The libSceLibcInternal of a payload process on a PS5 (firmware 12.70) does not seed `random()` the way AnyPS5 did. Probe p83 (`case random` lines) measured, in that process: - a fresh process draws `1804289383, 846930886, 1681692777, 1714636915, 1957747793`, the same as `srandom(1)`, which continues with `424238335`; AnyPS5 drew `1408992891, 1638182910, 83848093, ...`. - `srandom(0)` draws `577655601, 1248161417, 192959563, ...`, `srandom(2)` draws `1505335290, ...`, `srandom(12345)` draws `383100999, 858300821, 357768173, ...`, `srandom(0xffffffff)` draws `254925627, ...` and `srandom(0x80000000)` draws `1336741213, ...`. - `initstate` with seed 1 returns null at 0 and 7 bytes. At 8, 15, 31, 32, 63, 64, 127, 128, 129, 255, 256, 257 and 1000 bytes it returns non-null, and the saved type word (after the draws) and first state word are `000000001465fc2d` for 8, 15 and 31 bytes, `0100000082016ee0` for 32 and 63, `02000000fea8f5e2` for 64 and 127, `03000000b1391599` for 128, 129 and 255, and `0400000032ba371f` for 256, 257 and 1000. The five values drawn after it are the same within each of those groups; the 128-byte group draws the fresh-process sequence. All of these follow from `good_rand(x)` reading its argument as an int32, replacing 0 with 123459876 and computing `x = 16807 * (x % 127773) - 2836 * (x / 127773)`, adding 0x7fffffff to a negative result, with the additive feedback generator and the `10 * degree` discards (50 for type 0) that AnyPS5 already had. A Python model of that arithmetic reproduces every measured value. AnyPS5's `GoodRand` instead took the argument modulo 0x7ffffffe, added one and subtracted one from the result, and its default state was a precomputed table that no `srandom` reproduces. Changes: - `GoodRand` computes the measured formula. - The default state is built at load by seeding the 128-byte table with 1, so a fresh process equals `srandom(1)`. - The new `guest_libc-random-seeding` test pins the sequences above (the first five values of a fresh process, the first six after each of the six measured seeds, the null returns, and for each of the 13 measured sizes the saved type word and first state word and the five drawn values). - `guest_random_state` expected the old generator; its sequences are replaced with the new generator's. Its `srandom(1)` and `srandom(12345)` values are measured; its `initstate(42)` and `initstate(7)` values and the 7th value after `srandom(12345)` are not measured (seeds 42 and 7 and a 7th draw were not probed) and follow from the formula, so they only guard against regressions. - The debt entry for `random, srandom, initstate, setstate` now lists what was not measured first. Not measured and not changed: `setstate`, which state `initstate` returns, `errno`, the null-state throw, `initstate` seeds other than 1, a title process, and `rand`/`srand`. `srandomdev` is not in this change; #8730 adds it to the same `Random.cpp`. This change does not depend on it and it does not depend on this one, but its `GuestRandomDeviceSeed.cpp` and the size-8 check it adds to `GuestRandomState.cpp` compute the old type 0 generator, and the CMake and debt hunks next to this change need a rebase, whichever merges second. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - OS and toolchain: Windows 11, WinLibs GCC 15.2.0 (r7), Ninja, Release; the build completes except for one unrelated AGC test that does not compile with MinGW (`alignas(65536)`) - `ctest --test-dir build -R "guest_random|guest_libc"` run twice: 8 of 8 passed both times, including `guest_libc-random-seeding` and `guest_random_state` - The same two tests with `core/libs/prx/libc/src/Random.cpp` reverted to `main` (the new test must fail): both `guest_random_state` and `guest_libc-random-seeding` fail; with the file restored both pass - `python tools/check_conventions.py --base origin/main`: All conventions checks passed - Title or homebrew: not tested; the values are from a payload process, not a title ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
clairtonmichel commented 2026-10-21 08:46:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by clairtonmichel on 2026-10-21T08:46:52Z.


Tested on AMD Ryzen 7 9800X3D and AMD Radeon RX 7900 XTX (Fedora Linux, Mesa 26.2.5):

  • Compiled successfully with zero errors.
  • Ran the libc random and seeding test suite (guest_random, guest_random_state, guest_libc-random-seeding, guest_posix_random, etc.): 100% passed successfully (11/11 tests green).
  • Random seeding adjustments align and function perfectly on host. Excellent fix!
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6311897255 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8979#issuecomment-6311897255). Originally posted by [clairtonmichel](https://github.com/clairtonmichel) on 2026-10-21T08:46:52Z. --- Tested on AMD Ryzen 7 9800X3D and AMD Radeon RX 7900 XTX (Fedora Linux, Mesa 26.2.5): - Compiled successfully with zero errors. - Ran the libc random and seeding test suite (`guest_random`, `guest_random_state`, `guest_libc-random-seeding`, `guest_posix_random`, etc.): **100% passed successfully** (11/11 tests green). - Random seeding adjustments align and function perfectly on host. Excellent fix!
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!8979
No description provided.