feat(libc): implement lrand48 and srand48 #2038

Closed
Aryansh1520 wants to merge 3 commits from upstream-pr/2038 into main
Aryansh1520 commented 2026-10-09 05:36:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryansh1520 on 2026-10-09T05:36:54Z.


What

PPSA28997 (God of War Sons of Sparta) imports lrand48 and srand48. They follow FreeBSD libc:

  • 48-bit linear congruential generator with a = 0x5DEECE66D, c = 0xB
  • initial state 0x1234ABCD330E
  • srand48 sets the high 32 bits of the state from the seed and the low 16 bits to 0x330E
  • lrand48 returns the top 31 bits

The state shares the lock used by rand. The guest's long is 64-bit, so both functions use std::int64_t.

Tested

  • Linux (CachyOS, kernel 7.2), GCC: PPSA28997 (God of War Sons of Sparta) runs past the calls.
  • tools/check_conventions.py --base origin/main and tools/pr_overlap.py --branch pass.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4796456133 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2038). Originally posted by [Aryansh1520](https://github.com/Aryansh1520) on 2026-10-09T05:36:54Z. --- ### What PPSA28997 (God of War Sons of Sparta) imports `lrand48` and `srand48`. They follow FreeBSD libc: - 48-bit linear congruential generator with `a = 0x5DEECE66D`, `c = 0xB` - initial state `0x1234ABCD330E` - `srand48` sets the high 32 bits of the state from the seed and the low 16 bits to `0x330E` - `lrand48` returns the top 31 bits The state shares the lock used by `rand`. The guest's `long` is 64-bit, so both functions use `std::int64_t`. ### Tested - Linux (CachyOS, kernel 7.2), GCC: PPSA28997 (God of War Sons of Sparta) runs past the calls. - `tools/check_conventions.py --base origin/main` and `tools/pr_overlap.py --branch` pass. ### 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 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
MarcZX10 commented 2026-10-09 05:57:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T05:57:51Z.


Please add guest_math cases for known srand48/lrand48 sequences (for example, seed 0), reseeding, and independence from rand/srand state. These would catch a wrong mask, shift, or shared-state regression. I built exact head 9f51a0c1 on Linux with GCC 12; the existing guest_math passed 1/1 in 0.01 s and conventions passed, but it does not cover the new sequence. GitHub reports no checks on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075239655 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2038#issuecomment-6075239655). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T05:57:51Z. --- Please add guest_math cases for known srand48/lrand48 sequences (for example, seed 0), reseeding, and independence from rand/srand state. These would catch a wrong mask, shift, or shared-state regression. I built exact head 9f51a0c1 on Linux with GCC 12; the existing guest_math passed 1/1 in 0.01 s and conventions passed, but it does not cover the new sequence. GitHub reports no checks on this head.
oneandonlydean commented 2026-10-09 09:22:53 +00:00 (Migrated from github.com)

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


Where does PPSA28997 stop on main without this? Please add that log line to the description.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078129600 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2038#issuecomment-6078129600). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:22:53Z. --- Where does PPSA28997 stop on main without this? Please add that log line to the description.
Aryansh1520 commented 2026-10-09 10:36:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryansh1520 on 2026-10-09T10:36:53Z.


Added guest_math cases in fdc6d0d9:

  • the unseeded sequence from FreeBSD's initial state 0x1234ABCD330E (851401618, 1804928587, 758783491)
  • sequences after srand48 with 0, 1, -1 and a seed above 32 bits, where only the low 32 bits count. These match host glibc's srand48/lrand48 (glibc differs only in its unseeded state)
  • reseeding restarts the sequence
  • srand/rand and srand48/lrand48 don't affect each other in either direction
  • 1000 results stay within [0, 2^31)

Mutation check: a shift of 16 instead of 17, low bits other than 0x330E, a zero initial state, and srand48 also writing the rand state each make guest_math fail.

On where PPSA28997 stops: lrand48 and srand48 are imported by Il2cppUserAssemblies.prx. I found them in its unresolved imports and implemented them before a run reached them, so I don't have a stop log line for them. On main (as of Oct 8) the title stops earlier, at LTov9gMEqCU (std::ios_base::_Addstd, #545).

The build workflows on this head are waiting for approval to run (fork PR).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079202716 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2038#issuecomment-6079202716). Originally posted by [Aryansh1520](https://github.com/Aryansh1520) on 2026-10-09T10:36:53Z. --- Added `guest_math` cases in fdc6d0d9: - the unseeded sequence from FreeBSD's initial state `0x1234ABCD330E` (851401618, 1804928587, 758783491) - sequences after `srand48` with 0, 1, -1 and a seed above 32 bits, where only the low 32 bits count. These match host glibc's `srand48`/`lrand48` (glibc differs only in its unseeded state) - reseeding restarts the sequence - `srand`/`rand` and `srand48`/`lrand48` don't affect each other in either direction - 1000 results stay within [0, 2^31) Mutation check: a shift of 16 instead of 17, low bits other than `0x330E`, a zero initial state, and `srand48` also writing the `rand` state each make `guest_math` fail. On where PPSA28997 stops: `lrand48` and `srand48` are imported by `Il2cppUserAssemblies.prx`. I found them in its unresolved imports and implemented them before a run reached them, so I don't have a stop log line for them. On main (as of Oct 8) the title stops earlier, at `LTov9gMEqCU` (`std::ios_base::_Addstd`, #545). The build workflows on this head are waiting for approval to run (fork PR).
Aryansh1520 commented 2026-10-09 10:53:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryansh1520 on 2026-10-09T10:53:16Z.


Moved the new checks out of GuestMath.cpp into their own test, GuestRand48.cpp (guest_rand48), in cf569548. This PR no longer touches GuestMath.cpp, so it no longer overlaps #1818 or #2037. The checks are unchanged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079439763 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2038#issuecomment-6079439763). Originally posted by [Aryansh1520](https://github.com/Aryansh1520) on 2026-10-09T10:53:16Z. --- Moved the new checks out of `GuestMath.cpp` into their own test, `GuestRand48.cpp` (`guest_rand48`), in cf569548. This PR no longer touches `GuestMath.cpp`, so it no longer overlaps #1818 or #2037. The checks are unchanged.
popcoder565 commented 2026-10-09 15:46:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T15:46:31Z.


Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head cf569548, merged onto main d70b8998 in one build with 38 other PRs that had no Windows run.

  • This PR's tests pass: guest_rand48. None of them reported skipping a case.
  • Full ctest in that build: 519 of 519. That needed local fixes to three other PRs in the batch that don't build or pass on Windows: #1611, #1796 and #1824 (reported there).

No title was run.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084272913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2038#issuecomment-6084272913). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T15:46:31Z. --- Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head `cf569548`, merged onto `main` d70b8998 in one build with 38 other PRs that had no Windows run. - This PR's tests pass: `guest_rand48`. None of them reported skipping a case. - Full ctest in that build: 519 of 519. That needed local fixes to three other PRs in the batch that don't build or pass on Windows: #1611, #1796 and #1824 (reported there). No title was run. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
oneandonlydean commented 2026-10-09 20:08:32 +00:00 (Migrated from github.com)

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


Same as #2037: PPSA28997 stops at LTov9gMEqCU before lrand48/srand48 on main. Please reopen with the log line when it reaches them.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088420348 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2038#issuecomment-6088420348). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T20:08:32Z. --- Same as #2037: PPSA28997 stops at `LTov9gMEqCU` before `lrand48`/`srand48` on main. Please reopen with the log line when it reaches them.

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