feat(libkernel): implement sysctlnametomib for the sysctl names AnyPS5 serves #5502

Merged
ahmad3a4 merged 1 commit from upstream-pr/5502 into main 2026-10-14 12:44:59 +00:00
ahmad3a4 commented 2026-10-13 21:30:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmad3a4 on 2026-10-13T21:30:35Z.


What

sysctlnametomib (UkEV4hvMo1E) is not exported, so Java 25 LAN / DNS (PPSA99869) stops in the loader on main:

FAIL: unresolved ELF import UkEV4hvMo1E from libkernel.prx

It is added in SystemConfiguration.cpp for the names whose MIB sysctl already serves, with their FreeBSD numbers from <sys/sysctl.h>:

Name MIB
hw.model 6.2
hw.ncpu 6.3
hw.physmem 6.5
hw.realmem 6.12
kern.arandom 1.37
kern.proc.pathname 1.14.12 (the caller appends the pid)

The rest follows FreeBSD:

  • Size handling is that of sysctlnametomib.c, which runs name2oid through sysctl with *sizep scaled to bytes and back. With a null mibp it reports the entry count. With a short buffer it copies what fits, sets *sizep to the entries copied and fails with ENOMEM, through the same CopyOut that sysctl uses.
  • Errors are those of sysctl_sysctl_name2oid in kern_sysctl.c: an empty name fails with ENOENT, and a name of MAXPATHLEN (1024) bytes or more fails with ENAMETOOLONG.
  • machdep.cacheline_size fails with ENOENT, as sysctlbyname already answers it.
  • Null pointers for the name or the size throw invalid_argument: sysctlnametomib.c dereferences both (strlen(name), *sizep *= sizeof(int)) before the syscall, so FreeBSD faults there.

Any other name throws "unsupported name", as sysctlbyname does. kern.sdk_version, kern.rng_pseudo, machdep.tsc_freq and vm.overcommit throw too: sysctlbyname answers them by name, but sysctl serves no MIB for them that this could return.

The new function sits at the end of the file, apart from the sysctl and sysctlbyname branches other pull requests change.

guest_system_configuration checks, for every name:

  • the count with a null buffer;
  • the MIB with a larger buffer, the entry after it left untouched.

It then checks that:

  • the hw.ncpu MIB reads the processor count back through sysctl;
  • the hw.realmem MIB reads through sysctl the value sysctlbyname("hw.realmem") gives;
  • kern.proc.pathname plus -1 reads /app0/eboot.bin;
  • a buffer of 1 and of 0 entries fails with ENOMEM and keeps the copied entry;
  • the empty name, machdep.cacheline_size and a 1024-byte name give the errors above, and null pointers throw;
  • a 1023-byte name and kern.cp_times throw.

On main the test does not link (undefined reference to sysctlnametomib_nid_postfix).

Tested

Windows 11 Pro (26200), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, rebased onto main 1209d370:

  • guest_system_configuration passes (3 runs).
  • The full build and full ctest pass: 1140 tests, 8 of them skipped by their own environment checks.
  • The NID patch of libkernel.prx succeeds.

With the PR, Java 25 LAN / DNS (PPSA99869, relinked with --trap-sdk-syscall-stubs) gets past this import, and the loader stops at the next one:

FAIL: unresolved ELF import psx0YiAKm7k from libSceLibcInternal.prx

psx0YiAKm7k is fegetenv (#5563). tools/import_audit.py goes from 4 absent imports to 3.

Linux not tested.

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: PPSA99869 #5501
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4849737140 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5502). Originally posted by [ahmad3a4](https://github.com/ahmad3a4) on 2026-10-13T21:30:35Z. --- ### What `sysctlnametomib` (`UkEV4hvMo1E`) is not exported, so Java 25 LAN / DNS (PPSA99869) stops in the loader on `main`: ``` FAIL: unresolved ELF import UkEV4hvMo1E from libkernel.prx ``` It is added in `SystemConfiguration.cpp` for the names whose MIB `sysctl` already serves, with their FreeBSD numbers from `<sys/sysctl.h>`: | Name | MIB | |---|---| | `hw.model` | 6.2 | | `hw.ncpu` | 6.3 | | `hw.physmem` | 6.5 | | `hw.realmem` | 6.12 | | `kern.arandom` | 1.37 | | `kern.proc.pathname` | 1.14.12 (the caller appends the pid) | The rest follows FreeBSD: - **Size handling** is that of [`sysctlnametomib.c`](https://github.com/freebsd/freebsd-src/blob/main/lib/libc/gen/sysctlnametomib.c), which runs `name2oid` through `sysctl` with `*sizep` scaled to bytes and back. With a null `mibp` it reports the entry count. With a short buffer it copies what fits, sets `*sizep` to the entries copied and fails with `ENOMEM`, through the same `CopyOut` that `sysctl` uses. - **Errors** are those of `sysctl_sysctl_name2oid` in [`kern_sysctl.c`](https://github.com/freebsd/freebsd-src/blob/main/sys/kern/kern_sysctl.c): an empty name fails with `ENOENT`, and a name of `MAXPATHLEN` (1024) bytes or more fails with `ENAMETOOLONG`. - **`machdep.cacheline_size`** fails with `ENOENT`, as `sysctlbyname` already answers it. - **Null pointers** for the name or the size throw `invalid_argument`: `sysctlnametomib.c` dereferences both (`strlen(name)`, `*sizep *= sizeof(int)`) before the syscall, so FreeBSD faults there. Any other name throws "unsupported name", as `sysctlbyname` does. `kern.sdk_version`, `kern.rng_pseudo`, `machdep.tsc_freq` and `vm.overcommit` throw too: `sysctlbyname` answers them by name, but `sysctl` serves no MIB for them that this could return. The new function sits at the end of the file, apart from the `sysctl` and `sysctlbyname` branches other pull requests change. `guest_system_configuration` checks, for every name: - the count with a null buffer; - the MIB with a larger buffer, the entry after it left untouched. It then checks that: - the `hw.ncpu` MIB reads the processor count back through `sysctl`; - the `hw.realmem` MIB reads through `sysctl` the value `sysctlbyname("hw.realmem")` gives; - `kern.proc.pathname` plus `-1` reads `/app0/eboot.bin`; - a buffer of 1 and of 0 entries fails with `ENOMEM` and keeps the copied entry; - the empty name, `machdep.cacheline_size` and a 1024-byte name give the errors above, and null pointers throw; - a 1023-byte name and `kern.cp_times` throw. On `main` the test does not link (`undefined reference to sysctlnametomib_nid_postfix`). ### Tested Windows 11 Pro (26200), WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, rebased onto `main` 1209d370: - `guest_system_configuration` passes (3 runs). - The full build and full `ctest` pass: 1140 tests, 8 of them skipped by their own environment checks. - The NID patch of `libkernel.prx` succeeds. With the PR, Java 25 LAN / DNS (PPSA99869, relinked with `--trap-sdk-syscall-stubs`) gets past this import, and the loader stops at the next one: ``` FAIL: unresolved ELF import psx0YiAKm7k from libSceLibcInternal.prx ``` `psx0YiAKm7k` is `fegetenv` (#5563). `tools/import_audit.py` goes from 4 absent imports to 3. Linux not tested. ### 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 - [x] Unblocks: PPSA99869 #5501 - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
pipe1os commented 2026-10-14 05:42:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pipe1os on 2026-10-14T05:42:03Z.


Built and ran this on Windows: head 0cb9540ab merged onto main f5424b4db (no conflicts), Windows 11, WinLibs GCC 15.2.0, Ninja Release.

  • guest_system_configuration passes. With SystemConfiguration.cpp put back to main and the test kept, it doesn't link (undefined reference to sysctlnametomib_nid_postfix).
  • Java 25 LAN / DNS (PPSA99869, #5501), relinked with --windows --to-intel --trap-sdk-syscall-stubs. On main the loader stops at UkEV4hvMo1E; with this PR it gets to FAIL: unresolved ELF import psx0YiAKm7k from libSceLibcInternal.prx (#5563).

Two things against FreeBSD 11.0:

  1. hw.realmem isn't in the table. main serves it through sysctl (6.12) and sysctlbyname since fe5493849, which landed after this branch's base. On the merge, sysctlbyname("hw.realmem") returns 0 but sysctlnametomib("hw.realmem") throws unsupported name hw.realmem. Probably just needs {"hw.realmem", {6, 12}, 2}.
  2. A null name or size returns EFAULT. sysctlnametomib.c dereferences both in userland (*sizep *= sizeof(int), strlen(name)) before the syscall, so the caller faults there. sysctlbyname on main already answers EFAULT the same way, so maybe that's the accepted convention, but a throw would be closer.

Everything else lines up: ENOENT for an empty name and ENAMETOOLONG from 1024 bytes (sysctl_sysctl_name2oid), a short buffer copying what fits with ENOMEM and the copied length (sysctl_old_user, userland_sysctl L1996-L1999), and the MIB numbers (sysctl.h).

AI-assisted.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161763314 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5502#issuecomment-6161763314). Originally posted by [pipe1os](https://github.com/pipe1os) on 2026-10-14T05:42:03Z. --- Built and ran this on Windows: head `0cb9540ab` merged onto `main` `f5424b4db` (no conflicts), Windows 11, WinLibs GCC 15.2.0, Ninja Release. - `guest_system_configuration` passes. With `SystemConfiguration.cpp` put back to `main` and the test kept, it doesn't link (`undefined reference to sysctlnametomib_nid_postfix`). - Java 25 LAN / DNS (PPSA99869, #5501), relinked with `--windows --to-intel --trap-sdk-syscall-stubs`. On `main` the loader stops at `UkEV4hvMo1E`; with this PR it gets to `FAIL: unresolved ELF import psx0YiAKm7k from libSceLibcInternal.prx` (#5563). Two things against FreeBSD 11.0: 1. `hw.realmem` isn't in the table. `main` serves it through `sysctl` (6.12) and `sysctlbyname` since fe5493849, which landed after this branch's base. On the merge, `sysctlbyname("hw.realmem")` returns 0 but `sysctlnametomib("hw.realmem")` throws `unsupported name hw.realmem`. Probably just needs `{"hw.realmem", {6, 12}, 2}`. 2. A null `name` or `size` returns `EFAULT`. [`sysctlnametomib.c`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/lib/libc/gen/sysctlnametomib.c#L51-L52) dereferences both in userland (`*sizep *= sizeof(int)`, `strlen(name)`) before the syscall, so the caller faults there. `sysctlbyname` on `main` already answers `EFAULT` the same way, so maybe that's the accepted convention, but a throw would be closer. Everything else lines up: `ENOENT` for an empty name and `ENAMETOOLONG` from 1024 bytes ([`sysctl_sysctl_name2oid`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/kern_sysctl.c#L1089-L1092)), a short buffer copying what fits with `ENOMEM` and the copied length ([`sysctl_old_user`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/kern_sysctl.c#L1650-L1686), [`userland_sysctl` L1996-L1999](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/kern_sysctl.c#L1996-L1999)), and the MIB numbers ([`sysctl.h`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/sys/sysctl.h#L788-L900)). AI-assisted.
ahmad3a4 commented 2026-10-14 08:02:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmad3a4 on 2026-10-14T08:02:50Z.


Thanks for building and running it. Both points are in the new head cc3d25d63, rebased onto main 1209d370:

  • hw.realmem maps to {6, 12}. guest_system_configuration reads it through sysctl with that MIB and compares it with sysctlbyname("hw.realmem").
  • A null name or size now throws invalid_argument, since sysctlnametomib.c faults on both before the syscall. sysctlbyname is left as it is.

kern.sdk_version, kern.rng_pseudo, machdep.tsc_freq and vm.overcommit still throw: sysctlbyname answers them by name, but sysctl serves no MIB for them.

Re-tested on Windows: guest_system_configuration passes, and the full ctest passes (1140 tests, 8 skipped by their environment checks). PPSA99869 still gets past UkEV4hvMo1E and stops at psx0YiAKm7k (#5563).

AI-assisted.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6163777914 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5502#issuecomment-6163777914). Originally posted by [ahmad3a4](https://github.com/ahmad3a4) on 2026-10-14T08:02:50Z. --- Thanks for building and running it. Both points are in the new head cc3d25d63, rebased onto `main` 1209d370: - `hw.realmem` maps to `{6, 12}`. `guest_system_configuration` reads it through `sysctl` with that MIB and compares it with `sysctlbyname("hw.realmem")`. - A null name or size now throws `invalid_argument`, since `sysctlnametomib.c` faults on both before the syscall. `sysctlbyname` is left as it is. `kern.sdk_version`, `kern.rng_pseudo`, `machdep.tsc_freq` and `vm.overcommit` still throw: `sysctlbyname` answers them by name, but `sysctl` serves no MIB for them. Re-tested on Windows: `guest_system_configuration` passes, and the full `ctest` passes (1140 tests, 8 skipped by their environment checks). PPSA99869 still gets past `UkEV4hvMo1E` and stops at `psx0YiAKm7k` (#5563). AI-assisted.
github-actions[bot] commented 2026-10-14 11:49:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T11:49:27Z.


Progress report (31bfc4e → cc3d25d)

➖ System libraries: 88.19% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel sysctlnametomib_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6167659746 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5502#issuecomment-6167659746). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T11:49:27Z. --- <!-- progress-report --> ### Progress report (31bfc4e → cc3d25d) ➖ **System libraries**: 88.19% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `sysctlnametomib_nid_postfix` | </details>
adriadam10 commented 2026-10-14 11:50:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T11:50:38Z.


Checked on the merge with main: guest_system_configuration passes and fails to link without the change, the MIB numbers, name2oid errors and ENOMEM handling match FreeBSD, and both points from the earlier review (hw.realmem, null name/size) are addressed with tests. Windows run by @pipe1os also confirms the loader gets past UkEV4hvMo1E. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6167679857 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5502#issuecomment-6167679857). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T11:50:38Z. --- Checked on the merge with main: `guest_system_configuration` passes and fails to link without the change, the MIB numbers, `name2oid` errors and `ENOMEM` handling match FreeBSD, and both points from the earlier review (`hw.realmem`, null name/size) are addressed with tests. Windows run by @pipe1os also confirms the loader gets past `UkEV4hvMo1E`. Merging.
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!5502
No description provided.