fix(libkernel): fill kern.rng_pseudo and kern.arandom from host OS entropy #5275

Closed
winstrex wants to merge 1 commit from upstream-pr/5275 into main
winstrex commented 2026-10-13 16:17:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by winstrex on 2026-10-13T16:17:08Z.


What

Follow-up to #4200. kern.rng_pseudo and kern.arandom (MIB {1, 37} and its sysctlbyname name, both added by #3700) were the last guest entropy sources still drawing from a static std::random_device, one call per output byte. They now fill the buffer with HostEntropy_nid_no_patch (getentropy, rand_s on Windows), like arc4random, /dev/random and sceRandomGetRandomNumber since #4200.

Only the entropy source changes. kern.rng_pseudo still fills *oldLength bytes and leaves the length as it was, returns EPERM for a write and throws without an output buffer; kern.arandom still returns min(*oldLength, 256) bytes, reports that size for a null buffer and returns EPERM for a write. The two docs/dev/debt/libkernel.md entries now name host OS entropy instead of std::random_device.

On the AMD EPYC 9V45 runner measured in #4200, a std::random_device call took about 23.5 µs, so one call per byte cost about 1.5 ms for the 64-byte blocks mbedTLS asks kern.rng_pseudo for and 6 ms for a 256-byte kern.arandom read.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Release.

  • guest_system_configuration (CheckPseudoRandomSysctl, CheckArandomSysctl), guest_posix_random, guest_random and guest_crt pass.
  • No new test: the change replaces one random source with another, and the existing checks (sizes, EPERM, the throw, two reads that differ) pass before and after. No test I could write fails without the change on the machines I have.
  • python tools/check_conventions.py --base upstream/main passes.
  • CI on the fork, same steps as build.yml (run 38423102411): Windows passes; Linux passes except guest_math, which fails the same way on main's own Linux runs (for example a74f2efe8, 39890031e and 765661581) and does not touch these sysctls.

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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4846164729 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5275). Originally posted by [winstrex](https://github.com/winstrex) on 2026-10-13T16:17:08Z. --- ### What Follow-up to #4200. `kern.rng_pseudo` and `kern.arandom` (MIB {1, 37} and its `sysctlbyname` name, both added by #3700) were the last guest entropy sources still drawing from a `static std::random_device`, one call per output byte. They now fill the buffer with `HostEntropy_nid_no_patch` (`getentropy`, `rand_s` on Windows), like `arc4random`, `/dev/random` and `sceRandomGetRandomNumber` since #4200. Only the entropy source changes. `kern.rng_pseudo` still fills `*oldLength` bytes and leaves the length as it was, returns `EPERM` for a write and throws without an output buffer; `kern.arandom` still returns `min(*oldLength, 256)` bytes, reports that size for a null buffer and returns `EPERM` for a write. The two `docs/dev/debt/libkernel.md` entries now name host OS entropy instead of `std::random_device`. On the AMD EPYC 9V45 runner measured in #4200, a `std::random_device` call took about 23.5 µs, so one call per byte cost about 1.5 ms for the 64-byte blocks mbedTLS asks `kern.rng_pseudo` for and 6 ms for a 256-byte `kern.arandom` read. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release. - `guest_system_configuration` (`CheckPseudoRandomSysctl`, `CheckArandomSysctl`), `guest_posix_random`, `guest_random` and `guest_crt` pass. - No new test: the change replaces one random source with another, and the existing checks (sizes, `EPERM`, the throw, two reads that differ) pass before and after. No test I could write fails without the change on the machines I have. - `python tools/check_conventions.py --base upstream/main` passes. - CI on the fork, same steps as `build.yml` ([run 38423102411](https://github.com/winstrex/AnyPS5/actions/runs/38423102411)): Windows passes; Linux passes except `guest_math`, which fails the same way on `main`'s own Linux runs (for example a74f2efe8, 39890031e and 765661581) and does not touch these sysctls. ### 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 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 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
oneandonlydean commented 2026-10-14 02:30:09 +00:00 (Migrated from github.com)

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


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159555185 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5275#issuecomment-6159555185). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:30:09Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

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