feat(libkernel_sys): implement scePthreadMutexIsowned, utrace and tcsetsid #9713

Merged
simonbronk merged 5 commits from upstream-pr/9713 into main 2026-11-02 22:34:44 +00:00
simonbronk commented 2026-10-22 09:38:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-22T09:38:41Z.


What

Implements 3 libkernel_sys exports that were discovery stubs:

  • scePthreadMutexIsowned returns 0 when libkernel's existing pthread_mutex_isowned_np returns 0, and 0x80020000 plus its result (0x80020001) when the calling thread owns the mutex. That is what firmware 13.42's libkernel_sys.sprx does, as BunnyFl measured by running the export in an emulator. An uninitialized or destroyed mutex, or one another thread owns, gets 0.
  • utrace returns 0 without reading its record. sys_utrace does that for a process ktrace does not trace, and nothing traces the guest.
  • tcsetsid(fd, pid) fails with EINVAL unless pid is getsid(0), then issues TIOCSCTTY with a null argument through the guest ioctl, as libc's termios.c does and as BunnyFl confirmed in firmware 13.42's module. No guest descriptor is a terminal, so an open descriptor gets ENOTTY and a closed one EBADF.

The guest socket ioctl returned EFAULT for any null argument before looking at the request, which would make tcsetsid on a socket fail with EFAULT. As in FreeBSD's sys_ioctl, which copies nothing for a void request without a parameter length, Udp.cpp now faults on a null argument only for a request with input or a length; the existing socket checks in guest_kernel_fcntl keep their EFAULT.

The implementations are in libkernel_sys/Export.cpp, next to the other implemented exports, and their stubs are removed from Export.Discovery.cpp. The pthread_switch_add_np/pthread_switch_delete_np part of this PR was dropped when it was rebased, since #9172 already returns ENOTSUP from both. The debt entry records what was measured and the assumption that the PS5 kernel has KTRACE (without it utrace would return ENOSYS).

Tested

Linux, Ubuntu 24.04 x86-64 container under Rosetta on Apple silicon (Colima), GCC 13, RelWithDebInfo with ASan, head eea3c721a7 (main d820fef182 merged in). guest_kernel_sys gets new checks:

  • scePthreadMutexIsowned before init, after init, while locked (0x80020001 from the owner, 0 from another thread), after unlock and after destroy;
  • utrace with a record, with an empty one and with 4096 bytes (errno untouched);
  • tcsetsid with a wrong session (EINVAL, checked before the descriptor), on an open file and on a UDP socket (ENOTTY) and on -1 (EBADF).

The full build fails only in libSceAddressSanitizer, which this PR does not touch: in an ASan build its declarations clash with GCC's __asan_* built-ins, the same on main d820fef182, and #12894 stops building it under ASan. Every test target below was then built explicitly (exit 0). 48 focused tests pass with no ASan reports: the guest_kernel_sys* tests, guest_pthread_mutex_errors, guest_pthread_mutexattr, guest_pthread_barrier, guest_getlogin, guest_getlogin_no_session, guest_np_session_signaling, guest_net_socket_state, guest_kernel_fcntl, guest_kernel_fcntl_sync, guest_udp, guest_sendfile and the guest_socket_* tests. Windows: Dom3005 ran an earlier head on main 11f976b27c (focused suite passes). macOS not run. check_conventions.py --base origin/main passes. pr_overlap.py reports no PR with the same exports; its only conflicting lines are with #11219, which edits the socket ioctl next to the Udp.cpp line changed here; orezraey will keep both if this lands first. No title 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4965287624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9713). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-22T09:38:41Z. --- ### What Implements 3 libkernel_sys exports that were discovery stubs: - `scePthreadMutexIsowned` returns 0 when libkernel's existing `pthread_mutex_isowned_np` returns 0, and `0x80020000` plus its result (`0x80020001`) when the calling thread owns the mutex. That is what firmware 13.42's `libkernel_sys.sprx` does, as BunnyFl measured by running the export in an emulator. An uninitialized or destroyed mutex, or one another thread owns, gets 0. - `utrace` returns 0 without reading its record. [`sys_utrace`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/kern_ktrace.c) does that for a process ktrace does not trace, and nothing traces the guest. - `tcsetsid(fd, pid)` fails with `EINVAL` unless `pid` is `getsid(0)`, then issues `TIOCSCTTY` with a null argument through the guest `ioctl`, as libc's [`termios.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/gen/termios.c) does and as BunnyFl confirmed in firmware 13.42's module. No guest descriptor is a terminal, so an open descriptor gets `ENOTTY` and a closed one `EBADF`. The guest socket `ioctl` returned `EFAULT` for any null argument before looking at the request, which would make `tcsetsid` on a socket fail with `EFAULT`. As in FreeBSD's [`sys_ioctl`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/sys_generic.c), which copies nothing for a void request without a parameter length, `Udp.cpp` now faults on a null argument only for a request with input or a length; the existing socket checks in `guest_kernel_fcntl` keep their `EFAULT`. The implementations are in `libkernel_sys/Export.cpp`, next to the other implemented exports, and their stubs are removed from `Export.Discovery.cpp`. The `pthread_switch_add_np`/`pthread_switch_delete_np` part of this PR was dropped when it was rebased, since #9172 already returns `ENOTSUP` from both. The debt entry records what was measured and the assumption that the PS5 kernel has `KTRACE` (without it `utrace` would return `ENOSYS`). ### Tested Linux, Ubuntu 24.04 x86-64 container under Rosetta on Apple silicon (Colima), GCC 13, RelWithDebInfo with ASan, head eea3c721a7 (`main` d820fef182 merged in). `guest_kernel_sys` gets new checks: - `scePthreadMutexIsowned` before init, after init, while locked (`0x80020001` from the owner, 0 from another thread), after unlock and after destroy; - `utrace` with a record, with an empty one and with 4096 bytes (errno untouched); - `tcsetsid` with a wrong session (`EINVAL`, checked before the descriptor), on an open file and on a UDP socket (`ENOTTY`) and on -1 (`EBADF`). The full build fails only in `libSceAddressSanitizer`, which this PR does not touch: in an ASan build its declarations clash with GCC's `__asan_*` built-ins, the same on `main` d820fef182, and #12894 stops building it under ASan. Every test target below was then built explicitly (exit 0). 48 focused tests pass with no ASan reports: the `guest_kernel_sys*` tests, `guest_pthread_mutex_errors`, `guest_pthread_mutexattr`, `guest_pthread_barrier`, `guest_getlogin`, `guest_getlogin_no_session`, `guest_np_session_signaling`, `guest_net_socket_state`, `guest_kernel_fcntl`, `guest_kernel_fcntl_sync`, `guest_udp`, `guest_sendfile` and the `guest_socket_*` tests. Windows: Dom3005 ran an earlier head on `main` 11f976b27c (focused suite passes). macOS not run. `check_conventions.py --base origin/main` passes. `pr_overlap.py` reports no PR with the same exports; its only conflicting lines are with #11219, which edits the socket `ioctl` next to the `Udp.cpp` line changed here; orezraey will keep both if this lands first. No title 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-22 12:05:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T12:05:48Z.

Review: COMMENTED


Reviewed exact head 085590dd4c31b0ec6ef01cecac623b2d2821cf6d. The implementations are narrowly scoped and the checked behaviors line up with the cited FreeBSD 11.4 sources: the mutex ownership query returns a boolean-style result, switch hooks return ENOTSUP, and tcsetsid validates the session before issuing TIOCSCTTY. The tests cover owner/non-owner and destroyed-mutex cases, errno precedence, and utrace argument sizes. Current conventions and diff checks pass; merge is clean. I could not independently run guest_kernel_sys because there is no configured build here and the filesystem is full. The PR’s Linux/ASan run and Windows/macOS status are author-reported; hosted product CI is skipped. Please refresh this 13-commits-behind branch to current main and rerun the focused suite on that base before approval. No code-level defect found in this review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5613850759 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9713#pullrequestreview-5613850759). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T12:05:48Z. **Review:** COMMENTED --- Reviewed exact head 085590dd4c31b0ec6ef01cecac623b2d2821cf6d. The implementations are narrowly scoped and the checked behaviors line up with the cited FreeBSD 11.4 sources: the mutex ownership query returns a boolean-style result, switch hooks return `ENOTSUP`, and `tcsetsid` validates the session before issuing `TIOCSCTTY`. The tests cover owner/non-owner and destroyed-mutex cases, errno precedence, and utrace argument sizes. Current conventions and diff checks pass; merge is clean. I could not independently run `guest_kernel_sys` because there is no configured build here and the filesystem is full. The PR’s Linux/ASan run and Windows/macOS status are author-reported; hosted product CI is skipped. Please refresh this 13-commits-behind branch to current main and rerun the focused suite on that base before approval. No code-level defect found in this review.
MarcZX10 commented 2026-10-22 13:34:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T13:34:07Z.


I reviewed this head alongside open #9582, which removes the throwing pthread_timedjoin_np declaration from the same libkernel_sys/Export.Discovery.cpp area where this PR removes the two pthread_switch_* declarations. git merge-tree on the exact open heads reports a content conflict in that file. Could you and the #9582 author agree on an order/resolution and preserve both sets of removals when rebasing? This is an integration note only; I am not asking either PR to add a dependency unless one actually needs to wait.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342070525 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9713#issuecomment-6342070525). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T13:34:07Z. --- I reviewed this head alongside open #9582, which removes the throwing `pthread_timedjoin_np` declaration from the same `libkernel_sys/Export.Discovery.cpp` area where this PR removes the two `pthread_switch_*` declarations. `git merge-tree` on the exact open heads reports a content conflict in that file. Could you and the #9582 author agree on an order/resolution and preserve both sets of removals when rebasing? This is an integration note only; I am not asking either PR to add a dependency unless one actually needs to wait.
MarcZX10 commented 2026-10-22 15:37:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T15:37:00Z.

Review: DISMISSED


I reviewed exact head 085590dd4c31b0ec6ef01cecac623b2d2821cf6d.

Checks run locally:

  • C++20 syntax-only compilation passed for the exact-head libkernel_sys/Export.cpp and GuestKernelSys.cpp using current-main shared headers.
  • git diff --check passed.
  • tools/check_conventions.py passed against current main.
  • Hosted eligibility, intake, conventions, labels and report checks pass; hosted Linux/Windows and other product jobs are skipped. The Linux/ASan 7/7 runtime suite in the PR description is author-reported and was not independently rerun here because there is no configured build tree.

The mutex ownership query, unsupported switch hooks, and untraced utrace behavior are consistent with the cited FreeBSD 11.4 implementations, subject to the PS5 KTRACE assumption already documented in the debt note. I found no major functional defect.

One small fidelity detail: FreeBSD 11.4 tcsetsid calls ioctl(fd, TIOCSCTTY, NULL), while this implementation passes a pointer to a local zero integer. AnyPS5 currently returns ENOTTY for guest file descriptors either way, so this does not change the covered result, but forwarding nullptr would match the cited libc behavior.

Before merge, update the branch to current main and rerun the focused suite. This head is 63 commits behind current main; CONTRIBUTING.md requires branches to stay up to date. The main commits since its base do not touch the four changed paths, but the sync and post-sync validation remain outstanding.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5616816335 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9713#pullrequestreview-5616816335). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T15:37:00Z. **Review:** DISMISSED --- I reviewed exact head 085590dd4c31b0ec6ef01cecac623b2d2821cf6d. Checks run locally: - C++20 syntax-only compilation passed for the exact-head libkernel_sys/Export.cpp and GuestKernelSys.cpp using current-main shared headers. - git diff --check passed. - tools/check_conventions.py passed against current main. - Hosted eligibility, intake, conventions, labels and report checks pass; hosted Linux/Windows and other product jobs are skipped. The Linux/ASan 7/7 runtime suite in the PR description is author-reported and was not independently rerun here because there is no configured build tree. The mutex ownership query, unsupported switch hooks, and untraced utrace behavior are consistent with the cited FreeBSD 11.4 implementations, subject to the PS5 KTRACE assumption already documented in the debt note. I found no major functional defect. One small fidelity detail: FreeBSD 11.4 tcsetsid calls ioctl(fd, TIOCSCTTY, NULL), while this implementation passes a pointer to a local zero integer. AnyPS5 currently returns ENOTTY for guest file descriptors either way, so this does not change the covered result, but forwarding nullptr would match the cited libc behavior. Before merge, update the branch to current main and rerun the focused suite. This head is 63 commits behind current main; CONTRIBUTING.md requires branches to stay up to date. The main commits since its base do not touch the four changed paths, but the sync and post-sync validation remain outstanding.
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!9713
No description provided.