fix(libkernel): answer kern.proc.pid and pid_td for a live thread's id, as FreeBSD 11.4's pget does #21986

Merged
brajankw merged 1 commit from upstream-pr/21986 into main 2026-11-17 01:52:44 +00:00
brajankw commented 2026-11-17 00:45:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-17T00:45:25Z.


What

sysctl {CTL_KERN, KERN_PROC, KERN_PROC_PID, id} and its KERN_PROC_INC_THREAD form failed with ESRCH for anything but the calling process's pid. FreeBSD 11.4's sysctl_kern_proc looks the id up with pget, which without PGET_NOTID maps an id above PID_MAX (99999) to the process of that thread (the kern.proc.pid_td entry in docs/dev/debt/libkernel.md).

  • Both MIBs accept the calling process's pid, or an id above PID_MAX that is the calling thread's or a live guest thread's id (pthread_getthreadid_np values start at 100000), and return the same records as for the pid. Other pids still fail with ESRCH.
  • GuestSystemConfiguration.cpp: the calling thread's id returns the process record; a worker thread's id returns every thread's record, the worker and the caller included; after the worker is joined its id fails with ESRCH. The foreign-pid checks use pid 1 (or 2) instead of pid + 1000, which could now be a thread id.
  • The debt fragment updates the two entries and adds one for kern.proc.pathname, kern.proc.env and kern.proc.vmmap, which still accept only the pid where FreeBSD 11.4 calls pget without PGET_NOTID too; that is a follow-up.

Tested

Linux (Debian 13, GCC 14.2.0, Release), on main 258ab7b6bd.

  • ctest -R '^guest_system_configuration$': passed.
  • Negative control: with main's SystemConfiguration.cpp and this branch's test, guest_system_configuration aborts at the first thread-id check.
  • Full ctest (5891 tests, -j 12) with this branch merged together with four unrelated fixes of mine: 5875 passed. The 16 failures are the set that fails on main in the same environment: six agc_driver cases (agc_driver_image_table_packed_guards passes when run on its own) and ten that need a .git directory, network or a passwd entry for the user.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch: no overlap with open pull requests.
  • Not tested on Windows or macOS. No title.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5348079539 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21986). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-17T00:45:25Z. --- ### What `sysctl` {`CTL_KERN`, `KERN_PROC`, `KERN_PROC_PID`, id} and its `KERN_PROC_INC_THREAD` form failed with `ESRCH` for anything but the calling process's pid. FreeBSD 11.4's `sysctl_kern_proc` looks the id up with [`pget`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_proc.c), which without `PGET_NOTID` maps an id above `PID_MAX` (99999) to the process of that thread (the `kern.proc.pid_td` entry in `docs/dev/debt/libkernel.md`). - Both MIBs accept the calling process's pid, or an id above `PID_MAX` that is the calling thread's or a live guest thread's id (`pthread_getthreadid_np` values start at 100000), and return the same records as for the pid. Other pids still fail with `ESRCH`. - `GuestSystemConfiguration.cpp`: the calling thread's id returns the process record; a worker thread's id returns every thread's record, the worker and the caller included; after the worker is joined its id fails with `ESRCH`. The foreign-pid checks use pid 1 (or 2) instead of `pid + 1000`, which could now be a thread id. - The debt fragment updates the two entries and adds one for `kern.proc.pathname`, `kern.proc.env` and `kern.proc.vmmap`, which still accept only the pid where FreeBSD 11.4 calls `pget` without `PGET_NOTID` too; that is a follow-up. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux (Debian 13, GCC 14.2.0, Release), on `main` `258ab7b6bd`. - `ctest -R '^guest_system_configuration$'`: passed. - Negative control: with `main`'s `SystemConfiguration.cpp` and this branch's test, `guest_system_configuration` aborts at the first thread-id check. - Full `ctest` (5891 tests, `-j 12`) with this branch merged together with four unrelated fixes of mine: 5875 passed. The 16 failures are the set that fails on `main` in the same environment: six `agc_driver` cases (`agc_driver_image_table_packed_guards` passes when run on its own) and ten that need a `.git` directory, network or a passwd entry for the user. - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch`: no overlap with open pull requests. - Not tested on Windows or macOS. No title. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-17 00:50:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T00:50:42Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against cf8666eb58:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_system_configuration_tests built cleanly.
  • CTest (guest_system_configuration):
    1/1 Test #5366: guest_system_configuration .......   Passed    0.06 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/System/src/SystemConfiguration.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5366: guest_system_configuration .......Subprocess aborted***Exception:   0.53 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified CallingProcessId in core/libs/prx/libkernel/System/src/SystemConfiguration.cpp correctly checks id == getpid_nid_postfix() and id > PidMax (99999) && (id == scePthreadGetthreadid() || LiveGuestThreadExists(id)) matching FreeBSD 11.4 pget semantics for KERN_PROC_PID and KERN_PROC_INC_THREAD.
    • Verified error handling for sysctl KERN_PROC_PID and KERN_PROC_PID | KERN_PROC_INC_THREAD returns ESRCH (3) when CallingProcessId returns false, matching FreeBSD sysctl behavior.
    • Verified memory safety, buffer bounds, and pointer copy operations using GuestCopyIn_nid_no_patch / GuestCopyOut_nid_no_patch across the modified sysctl branches.
    • Verified test coverage in core/libs/tests/GuestSystemConfiguration.cpp checking calling thread ID, joined worker thread cleanup (ESRCH), and foreign PID rejection without regressions.
    • Verified technical debt updates in docs/dev/debt/changes/fix-libkernel-sysctl-proc-pid-thread-id.md adhere to AGENTS.md fragment formatting and conventions.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5930686935 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21986#pullrequestreview-5930686935). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T00:50:42Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `cf8666eb58`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_system_configuration_tests` built cleanly. - **CTest** (`guest_system_configuration`): ``` 1/1 Test #5366: guest_system_configuration ....... Passed 0.06 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/System/src/SystemConfiguration.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5366: guest_system_configuration .......Subprocess aborted***Exception: 0.53 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified CallingProcessId in core/libs/prx/libkernel/System/src/SystemConfiguration.cpp correctly checks id == getpid_nid_postfix() and id > PidMax (99999) && (id == scePthreadGetthreadid() || LiveGuestThreadExists(id)) matching FreeBSD 11.4 pget semantics for KERN_PROC_PID and KERN_PROC_INC_THREAD. - Verified error handling for sysctl KERN_PROC_PID and KERN_PROC_PID | KERN_PROC_INC_THREAD returns ESRCH (3) when CallingProcessId returns false, matching FreeBSD sysctl behavior. - Verified memory safety, buffer bounds, and pointer copy operations using GuestCopyIn_nid_no_patch / GuestCopyOut_nid_no_patch across the modified sysctl branches. - Verified test coverage in core/libs/tests/GuestSystemConfiguration.cpp checking calling thread ID, joined worker thread cleanup (ESRCH), and foreign PID rejection without regressions. - Verified technical debt updates in docs/dev/debt/changes/fix-libkernel-sysctl-proc-pid-thread-id.md adhere to AGENTS.md fragment formatting and conventions. Looks Good To Me
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!21986
No description provided.