fix(libc): seed random() and its default state as measured on a PS5 #8979
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8979"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 randomlines) measured, in that process:1804289383, 846930886, 1681692777, 1714636915, 1957747793, the same assrandom(1), which continues with424238335; AnyPS5 drew1408992891, 1638182910, 83848093, ....srandom(0)draws577655601, 1248161417, 192959563, ...,srandom(2)draws1505335290, ...,srandom(12345)draws383100999, 858300821, 357768173, ...,srandom(0xffffffff)draws254925627, ...andsrandom(0x80000000)draws1336741213, ....initstatewith 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 are000000001465fc2dfor 8, 15 and 31 bytes,0100000082016ee0for 32 and 63,02000000fea8f5e2for 64 and 127,03000000b1391599for 128, 129 and 255, and0400000032ba371ffor 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 computingx = 16807 * (x % 127773) - 2836 * (x / 127773), adding 0x7fffffff to a negative result, with the additive feedback generator and the10 * degreediscards (50 for type 0) that AnyPS5 already had. A Python model of that arithmetic reproduces every measured value. AnyPS5'sGoodRandinstead took the argument modulo 0x7ffffffe, added one and subtracted one from the result, and its default state was a precomputed table that nosrandomreproduces.Changes:
GoodRandcomputes the measured formula.srandom(1).guest_libc-random-seedingtest 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_stateexpected the old generator; its sequences are replaced with the new generator's. Itssrandom(1)andsrandom(12345)values are measured; itsinitstate(42)andinitstate(7)values and the 7th value aftersrandom(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.random, srandom, initstate, setstatenow lists what was not measured first.Not measured and not changed:
setstate, which stateinitstatereturns,errno, the null-state throw,initstateseeds other than 1, a title process, andrand/srand.srandomdevis not in this change; #8730 adds it to the sameRandom.cpp. This change does not depend on it and it does not depend on this one, but itsGuestRandomDeviceSeed.cppand the size-8 check it adds toGuestRandomState.cppcompute the old type 0 generator, and the CMake and debt hunks next to this change need a rebase, whichever merges second.Tested
alignas(65536))ctest --test-dir build -R "guest_random|guest_libc"run twice: 8 of 8 passed both times, includingguest_libc-random-seedingandguest_random_statecore/libs/prx/libc/src/Random.cppreverted tomain(the new test must fail): bothguest_random_stateandguest_libc-random-seedingfail; with the file restored both passpython tools/check_conventions.py --base origin/main: All conventions checks passedChecklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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):
guest_random,guest_random_state,guest_libc-random-seeding,guest_posix_random, etc.): 100% passed successfully (11/11 tests green).