feat(libkernel_sys): implement kenv #11032

Merged
Gruzzberry merged 1 commit from upstream-pr/11032 into main 2026-10-28 07:39:52 +00:00
Gruzzberry commented 2026-10-25 14:35:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-25T14:35:27Z.


What

kenv was a throwing stub in libkernel_sys. It now follows FreeBSD 11.4's sys_kenv over an in-memory kernel environment:

  • KENV_GET copies out min(len, strlen+1) bytes and returns the count (ENOENT for a missing name; a negative len is not truncated, as the kernel compares it as unsigned).
  • KENV_SET fails with EINVAL for a len of 0 and caps len at 129; a negative len becomes a large size_t in the kernel, so it passes that check and is capped like any other. It and replaces an existing variable; a name or value that does not fit its 128-byte limit plus NUL fails with ENAMETOOLONG. A 513th variable is dropped silently, as in FreeBSD.
  • KENV_UNSET removes a variable (ENOENT if absent).
  • KENV_DUMP writes name=value strings separated by NULs, truncating at len and returning the size needed (0 when everything fit).
  • The name is read before the action is checked, so an unreadable name fails with EFAULT before an unknown action gives EINVAL. The guest is uid 0, so the privilege checks pass.

Guest strings are read with CopyGuestPath_nid_no_patch and output goes through GuestCopyOut_nid_no_patch, so a bad pointer returns EFAULT instead of faulting the host. The table starts empty because the console's boot environment is unmeasured; the debt entry records that and the copy-limit difference.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON.

  • guest_kernel_sys: new CheckKernelEnvironment covers get/set/unset/dump, replacement, truncation, negative and zero len (a negative len on KENV_SET stores the value), 128-byte limits, the 512-variable cap, = inside a name, and EFAULT for a null and an unreadable (0x10) name, value and dump buffer. On main it aborts with kenv_nid_postfix not implemented; on this branch it passes, three runs out of three.
  • Full suite on this branch: no build failures. pr_overlap_encoding, agc_host_import_cut_lifetime and agc_driver_depth_plane_partial_store fail the same way on plain main in this environment; agc_import_settle_earlier_import failed once in the parallel run and passes alone.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, a console, a title run.

Overlaps

  • #10196 (Export.cpp L133-134), #10421 (Export.Discovery.cpp L110-119), #10427 (GuestKernelSys.cpp L524), #10799 and #10838 (GuestKernelSys.cpp L536), #10332 and #10335 (libkernel_sys.md debt L12) touch neighbouring lines: proposed on each that it goes first and this PR rebases.
  • #11031 (getfsstat, mine) adds code at the same place: this PR goes first, then #11031 rebases.
  • No other open PR mentions kenv.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: #10196; merge order: after #10196, #10332, #10335, #10421, #10427, #10799 and #10838 (neighbouring lines); before #11031
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5005660482 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11032). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-25T14:35:27Z. --- ### What `kenv` was a throwing stub in libkernel_sys. It now follows FreeBSD 11.4's [`sys_kenv`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_environment.c#L87-L204) over an in-memory kernel environment: - `KENV_GET` copies out `min(len, strlen+1)` bytes and returns the count (`ENOENT` for a missing name; a negative `len` is not truncated, as the kernel compares it as unsigned). - `KENV_SET` fails with `EINVAL` for a `len` of 0 and caps `len` at 129; a negative `len` becomes a large `size_t` in the kernel, so it passes that check and is capped like any other. It and replaces an existing variable; a name or value that does not fit its 128-byte limit plus NUL fails with `ENAMETOOLONG`. A 513th variable is dropped silently, as in FreeBSD. - `KENV_UNSET` removes a variable (`ENOENT` if absent). - `KENV_DUMP` writes `name=value` strings separated by NULs, truncating at `len` and returning the size needed (0 when everything fit). - The name is read before the action is checked, so an unreadable name fails with `EFAULT` before an unknown action gives `EINVAL`. The guest is uid 0, so the privilege checks pass. Guest strings are read with `CopyGuestPath_nid_no_patch` and output goes through `GuestCopyOut_nid_no_patch`, so a bad pointer returns `EFAULT` instead of faulting the host. The table starts empty because the console's boot environment is unmeasured; the debt entry records that and the copy-limit difference. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`. - `guest_kernel_sys`: new `CheckKernelEnvironment` covers get/set/unset/dump, replacement, truncation, negative and zero `len` (a negative `len` on `KENV_SET` stores the value), 128-byte limits, the 512-variable cap, `=` inside a name, and `EFAULT` for a null and an unreadable (`0x10`) name, value and dump buffer. On `main` it aborts with `kenv_nid_postfix not implemented`; on this branch it passes, three runs out of three. - Full suite on this branch: no build failures. `pr_overlap_encoding`, `agc_host_import_cut_lifetime` and `agc_driver_depth_plane_partial_store` fail the same way on plain `main` in this environment; `agc_import_settle_earlier_import` failed once in the parallel run and passes alone. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, a console, a title run. ### Overlaps - #10196 (`Export.cpp` L133-134), #10421 (`Export.Discovery.cpp` L110-119), #10427 (`GuestKernelSys.cpp` L524), #10799 and #10838 (`GuestKernelSys.cpp` L536), #10332 and #10335 (`libkernel_sys.md` debt L12) touch neighbouring lines: proposed on each that it goes first and this PR rebases. - #11031 (`getfsstat`, mine) adds code at the same place: this PR goes first, then #11031 rebases. - No other open PR mentions `kenv`. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [x] Generic behaviour, not specific to one title - [x] Depends on: #10196; merge order: after #10196, #10332, #10335, #10421, #10427, #10799 and #10838 (neighbouring lines); before #11031 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 14:37:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:37:04Z.

Review: COMMENTED


The added test file contains literal NUL bytes in the empty-value assertion and expected dump string (lines 555 and 561). GCC accepts them but emits null character(s) preserved in literal warnings, and tools treat the source as binary. Please spell these as the C++ escapes \0 (\x00 also works) so the test remains ordinary text and the intended embedded NUL is explicit. Exact-head checks: git diff --check for the PR files and tools/check_conventions.py --base upstream/main pass; g++ -std=c++20 -fsyntax-only on GuestKernelSys.cpp succeeds with the two warnings noted.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643693152 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11032#pullrequestreview-5643693152). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:37:04Z. **Review:** COMMENTED --- The added test file contains literal NUL bytes in the empty-value assertion and expected dump string (lines 555 and 561). GCC accepts them but emits `null character(s) preserved in literal` warnings, and tools treat the source as binary. Please spell these as the C++ escapes `\0` (`\x00` also works) so the test remains ordinary text and the intended embedded NUL is explicit. Exact-head checks: `git diff --check` for the PR files and `tools/check_conventions.py --base upstream/main` pass; `g++ -std=c++20 -fsyntax-only` on `GuestKernelSys.cpp` succeeds with the two warnings noted.
MarcZX10 commented 2026-10-25 14:37:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:37:23Z.

Review: APPROVED


Reviewed the exact head against FreeBSD 11.4 sys_kenv. The GET, SET, UNSET and DUMP paths and their return/error behavior match the reference for the modeled in-memory environment; the implementation records the unmeasured boot environment and copy-limit difference as debt. Exact-head checks: git diff --check for the PR files and tools/check_conventions.py --base upstream/main pass. g++ -std=c++20 -fsyntax-only on GuestKernelSys.cpp succeeds, with warnings for the two raw NUL bytes called out in my comment. The author reports the focused Windows suite passing three times; hosted product build/test checks are skipped, so I could not independently verify runtime behavior here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643694099 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11032#pullrequestreview-5643694099). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:37:23Z. **Review:** APPROVED --- Reviewed the exact head against FreeBSD 11.4 `sys_kenv`. The GET, SET, UNSET and DUMP paths and their return/error behavior match the reference for the modeled in-memory environment; the implementation records the unmeasured boot environment and copy-limit difference as debt. Exact-head checks: `git diff --check` for the PR files and `tools/check_conventions.py --base upstream/main` pass. `g++ -std=c++20 -fsyntax-only` on `GuestKernelSys.cpp` succeeds, with warnings for the two raw NUL bytes called out in my comment. The author reports the focused Windows suite passing three times; hosted product build/test checks are skipped, so I could not independently verify runtime behavior here.
MarcZX10 commented 2026-10-25 15:21:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:21:20Z.

Review: DISMISSED


Following up on my earlier note: the current head af5ebe43b4d1a45a7e5363a7b5472d43e935b861 still has two literal NUL bytes in core/libs/tests/GuestKernelSys.cpp (confirmed in the fetched file bytes). Please replace them with the C++ escape \\0 so the file remains plain text and the intended embedded NUL is explicit. The previous exact-head syntax check succeeded but emitted GCC’s null character(s) preserved in literal warnings; I’m requesting this small cleanup before the draft is ready.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643841158 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11032#pullrequestreview-5643841158). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:21:20Z. **Review:** DISMISSED --- Following up on my earlier note: the current head `af5ebe43b4d1a45a7e5363a7b5472d43e935b861` still has two literal NUL bytes in `core/libs/tests/GuestKernelSys.cpp` (confirmed in the fetched file bytes). Please replace them with the C++ escape `\\0` so the file remains plain text and the intended embedded NUL is explicit. The previous exact-head syntax check succeeded but emitted GCC’s `null character(s) preserved in literal` warnings; I’m requesting this small cleanup before the draft is ready.
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!11032
No description provided.