fix(libkernel): check getrusage output pointers #8988

Merged
MarcZX10 merged 1 commit from upstream-pr/8988 into main 2026-10-25 02:15:29 +00:00
MarcZX10 commented 2026-10-21 08:41:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T08:41:48Z.


What

Build getrusage results in local storage, then copy them to guest memory with the checked guest-write helper. Inaccessible non-null output pointers now return -1 with guest EFAULT instead of crashing while writing the structure. Invalid who values are still rejected first with EINVAL; null pointers for supported who values still return EFAULT.

Regression coverage checks invalid who and invalid output pointers for self, thread, and children results.

Tested

  • Linux GCC 12.2: cmake --build build --target guest_getrusage_tests -j2 passed.
  • git diff --check passed.
  • ctest --test-dir build --output-on-failure -R '^guest_getrusage$' could not reach assertions because the host CPU gate rejects this Xeon E5-1620 v2 (missing BMI1/BMI2, LZCNT, MOVBE, AVX2 and FMA).
  • Open PR and issue searches found no matching getrusage invalid-pointer work.

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; no new silent stubs
  • No notes or images added to repository
  • No third-party code added
  • Generic behavior, not title-specific
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4948831569 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8988). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T08:41:48Z. --- ### What Build `getrusage` results in local storage, then copy them to guest memory with the checked guest-write helper. Inaccessible non-null output pointers now return `-1` with guest `EFAULT` instead of crashing while writing the structure. Invalid `who` values are still rejected first with `EINVAL`; null pointers for supported `who` values still return `EFAULT`. Regression coverage checks invalid `who` and invalid output pointers for self, thread, and children results. ### Tested - Linux GCC 12.2: `cmake --build build --target guest_getrusage_tests -j2` passed. - `git diff --check` passed. - `ctest --test-dir build --output-on-failure -R '^guest_getrusage$'` could not reach assertions because the host CPU gate rejects this Xeon E5-1620 v2 (missing BMI1/BMI2, LZCNT, MOVBE, AVX2 and FMA). - Open PR and issue searches found no matching `getrusage` invalid-pointer work. ### 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 - [x] Unimplemented paths throw; no new silent stubs - [x] No notes or images added to repository - [x] No third-party code added - [x] Generic behavior, not title-specific - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
xXPBSXx commented 2026-10-21 09:45:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T09:45:54Z.


Windows data point for 0c4556156 merged onto main b5d763770 (clean, git merge-tree rc 0): Windows 10.0.19045, MinGW-w64 GCC 15.2.0, Release. ctest -N -R '^guest_getrusage$' lists exactly that one test; ctest --no-tests=error -R '^guest_getrusage$' was run three times per tree.

  • With the PR: passes 3 of 3 (0.5-0.7 s).
  • Same tree with core/libs/prx/libkernel/System/src/Process.cpp restored to main (the new test cases kept): fails 3 of 3 with a segmentation fault (ctest reports SEGFAULT).
    Not tested: other platforms, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6312972892 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8988#issuecomment-6312972892). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T09:45:54Z. --- Windows data point for `0c4556156` merged onto `main` `b5d763770` (clean, `git merge-tree` rc 0): Windows 10.0.19045, MinGW-w64 GCC 15.2.0, Release. `ctest -N -R '^guest_getrusage$'` lists exactly that one test; `ctest --no-tests=error -R '^guest_getrusage$'` was run three times per tree. - With the PR: passes 3 of 3 (0.5-0.7 s). - Same tree with `core/libs/prx/libkernel/System/src/Process.cpp` restored to `main` (the new test cases kept): fails 3 of 3 with a segmentation fault (ctest reports `SEGFAULT`). Not tested: other platforms, a title run. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-24 13:24:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:24:48Z.

Review: APPROVED


Checked against main 6cd36e6517 and FreeBSD 11.4 sys/kern/kern_resource.c (sys_getrusage / kern_getrusage):

  • #8985/#8986 are already in main, so the real change against main is just the getrusage body plus the 4 new test cases, and it merges cleanly.
  • Main's getrusage_nid_postfix still writes through usage directly (*usage = {}, usage->ru_*), so this fix is still needed.
  • The error order matches FreeBSD: kern_getrusage rejects an unknown who with EINVAL before copyout can fail with EFAULT, and children (who == -1) going through WriteGuest and returning EFAULT on a bad pointer also matches copyout.
  • Building the result locally and writing it once is a clean way to keep a fault from leaving a half-written struct. Looks good to me.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638404034 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8988#pullrequestreview-5638404034). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:24:48Z. **Review:** APPROVED --- Checked against main 6cd36e6517 and FreeBSD 11.4 sys/kern/kern_resource.c (sys_getrusage / kern_getrusage): - #8985/#8986 are already in main, so the real change against main is just the `getrusage` body plus the 4 new test cases, and it merges cleanly. - Main's `getrusage_nid_postfix` still writes through `usage` directly (`*usage = {}`, `usage->ru_*`), so this fix is still needed. - The error order matches FreeBSD: `kern_getrusage` rejects an unknown `who` with EINVAL before `copyout` can fail with EFAULT, and children (`who == -1`) going through `WriteGuest` and returning EFAULT on a bad pointer also matches copyout. - Building the result locally and writing it once is a clean way to keep a fault from leaving a half-written struct. Looks good to me. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!8988
No description provided.