Skip to content

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

Merged
oneandonlydean merged 5 commits into
boykopovar:mainfrom
simonbronk:feat/libkernel_sys-switch-utrace-tcsetsid
Nov 2, 2026
Merged

oneandonlydean merged 5 commits into
boykopovar:mainfrom
simonbronk:feat/libkernel_sys-switch-utrace-tcsetsid

Conversation

@simonbronk

@simonbronk simonbronk commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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 eea3c72 (main d820fef 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 d820fef, 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 11f976b (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

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Progress report (a4eca2a → 73cebee)

📈 System libraries: 26.04% (+0.0082%, +3 implemented functions)

✅ 3 implemented
Library Name
libkernel_sys scePthreadMutexIsowned
libkernel_sys tcsetsid_nid_postfix
libkernel_sys utrace_nid_postfix
⚪ 1 behaviourally empty (constant return or throw)
Library Name
libkernel_sys utrace_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libkernel_sys 139/830 (16.75%) 142/830 (17.11%) +0.36%

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 9242 core/libs/prx/libkernel_sys/Export.Discovery.cpp L2137-2146
core/libs/prx/libkernel_sys/include/Api.hpp L547-548
PR 9326 core/libs/prx/libkernel_sys/Export.Discovery.cpp L2021-2030
core/libs/prx/libkernel_sys/include/Api.hpp L523-524
PR 15037 core/libs/prx/libkernel_sys/Export.Discovery.cpp L2142-2156
core/libs/prx/libkernel_sys/include/Api.hpp L548-550

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
core/libs/prx/libkernel_sys/Export.cpp
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 9019 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/Export.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 9097 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/Export.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
core/libs/tests/GuestKernelSys.cpp
PR 9156 core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 9162 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 9293 core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 9297 core/libs/prx/libkernel_sys/Export.Discovery.cpp
docs/dev/debt/libkernel_sys.md
PR 9302 core/libs/prx/libkernel_sys/Export.Discovery.cpp
docs/dev/debt/libkernel_sys.md
PR 9383 core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 9389 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/Export.cpp
PR 9948 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12130 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 12642 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 12682 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12700 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12965 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 13129 docs/dev/debt/libkernel_sys.md
PR 13253 docs/dev/debt/libkernel_sys.md
PR 13256 docs/dev/debt/libkernel_sys.md
PR 13560 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 14101 core/libs/prx/libkernel_sys/Export.cpp
docs/dev/debt/libkernel_sys.md
PR 14212 docs/dev/debt/libkernel_sys.md
PR 14299 docs/dev/debt/libkernel_sys.md
PR 14310 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 14403 docs/dev/debt/libkernel_sys.md

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 085590d. 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

Copy link
Copy Markdown
Contributor

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 MarcZX10 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed exact head 085590d.

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.

@MarcZX10

MarcZX10 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

I’ve noticed that asking you to hold this PR was unnecessary. It’s fine to keep submitting PRs; maintainers will handle them in due course.

@Dom3005

Dom3005 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Windows run of 085590d merged onto current main 11f976b (clean merge, together with #9750, which touches different files). The description has Windows as not run, and the review above asked for the focused suite on current main.

Environment: Windows 11 Home 10.0.26200, WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Ninja, Release, BUILD_TESTING=ON.

Focused suite (--repeat until-fail:5): guest_kernel_sys, guest_kernel_sys_thread_defaults, guest_pthread_mutex_errors, guest_pthread_mutexattr, guest_pthread_barrier and guest_np_session_signaling all pass. guest_getlogin_no_session is not registered in this Windows build, so it did not run.

  • tcsetsid reaches ENOTTY on an open file and EBADF on -1 through the Windows ioctl path, as on Linux.

Baseline: with core/libs/prx/libkernel_sys restored from main and this PR's test kept, guest_kernel_sys aborts with 0xc0000409, what(): scePthreadMutexIsowned not implemented.

Full suite: 1886 of 1894 pass. The 8 failures are AGC tests that fail on this machine's AMD GPU on main too (#7451, #9056, #8310).

The branch itself still needs the rebase the review asks for. This only shows that the change holds on Windows on top of current main.

AI-assisted: yes

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@simonbronk
simonbronk force-pushed the feat/libkernel_sys-switch-utrace-tcsetsid branch from 085590d to c45fd94 Compare October 23, 2026 08:38
@simonbronk

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thanks. Rebased on main 0a0ed72 as c45fd94:

  • Rebase. The only conflict was docs/dev/debt/libkernel_sys.md, where main added the sceKernelGetModuleInfoInternal entry at the same spot; both entries are kept.
  • tcsetsid now passes nullptr to TIOCSCTTY, as libc's termios.c does. That exposed one difference: the guest socket ioctl returned EFAULT for any null argument before looking at the request, so tcsetsid on a socket would have gone from ENOTTY to EFAULT. FreeBSD's sys_ioctl copies nothing for a void request with no parameter length, so Udp.cpp now faults on a null argument only for a request with input or a length. The existing IntegerArgumentRequest and EmptyInputRequest socket checks in GuestKernelFcntl.cpp still get EFAULT. guest_kernel_sys gains a tcsetsid case on a UDP socket (ENOTTY).

Linux run of c45fd94 (Ubuntu 24.04 x86-64 container under Rosetta on Apple silicon, GCC 13, RelWithDebInfo with ASan): the full build has no errors in AnyPS5 code; the only failure is the atrac9_gradient_tests link (pow needs -lm), which this container also hits on the previous base. 27 focused tests pass with no ASan report: guest_kernel_sys, its thread_defaults, barrier, sdk_version and module_internal forms, guest_pthread_mutex_errors, guest_pthread_mutexattr, guest_pthread_barrier, guest_getlogin, guest_getlogin_no_session, guest_np_session_signaling, guest_kernel_fcntl, guest_udp, guest_sendfile and the 13 guest_socket_* tests. check_conventions.py --base origin/main passes. Windows not rerun on this head; Dom3005's run above covers the previous one.

On overlap: the rebased head merges into main cleanly, but it conflicts with #8890 and #9582 (and adjacent removals in #9195, #9242, #9326) in Export.Discovery.cpp, where each removes neighbouring declarations. None needs the other's code; whichever lands second keeps both sets of removals.

AI-assisted: yes

@simonbronk
simonbronk force-pushed the feat/libkernel_sys-switch-utrace-tcsetsid branch from c45fd94 to 44fa8ea Compare October 23, 2026 09:04
@simonbronk

Copy link
Copy Markdown
Contributor Author

#8890 merged right after the push above, so I rebased again: 44fa8ea on main 44a59ed. The only conflict was Export.Discovery.cpp, where main removed the pthread_timedjoin_np stub next to the two pthread_switch_* stubs this PR removes; all three are gone and nothing else changed (#9314 and #9355 merged cleanly). This head also merges cleanly with #9582.

Linux run of 44fa8ea, same container and configuration as above: no build errors in AnyPS5 code (only the known atrac9_gradient_tests link failure in this container), and the same filter now runs 29 tests, all passing with no ASan report: the 27 listed above plus guest_kernel_sys_aio and guest_kernel_sys_ktimer from #9355 and #9314. check_conventions.py --base origin/main passes.

AI-assisted: yes

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 23, 2026
@simonbronk
simonbronk force-pushed the feat/libkernel_sys-switch-utrace-tcsetsid branch from 44fa8ea to dbb29c0 Compare October 23, 2026 14:19
@simonbronk

Copy link
Copy Markdown
Contributor Author

Rebased again for the needs rebase label: dbb29c0 on main 938812d. The only conflict was libkernel_sys/Export.cpp, where #9040 added sceKernelTitleWorkaroundIsEnabled and sceKernelGetProcessType at the same spot at the end of the file; both blocks are kept, main's first. The diff against main is unchanged (5 files, +85/-26).

Linux run of dbb29c0, same container and configuration as above: no build errors in AnyPS5 code (only the known atrac9_gradient_tests link failure in this container), and the same filter now runs 30 tests, all passing with no ASan report: the 29 listed above plus guest_kernel_sys_pthread from #9141. check_conventions.py --base origin/main passes. pr_overlap.py finds no other PR with these exports, only adjacent edits in Export.Discovery.cpp (#9172, #9195, #9242, #9326), the debt file (#9162) and GuestKernelSys.cpp (#10196).

AI-assisted: yes

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@adriadam10 adriadam10 self-assigned this Oct 25, 2026
@adriadam10
adriadam10 force-pushed the feat/libkernel_sys-switch-utrace-tcsetsid branch from dbb29c0 to 9c49cb2 Compare October 25, 2026 18:43
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 25, 2026 18:44

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 25, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@MarcZX10

Copy link
Copy Markdown
Contributor

Integration note from the re-reviewed #11402: its current head adds pointer-validation assertions in GuestKernelSys.cpp within CheckSettimeofday, while this PR adds the separate ThreadAndSessionTests helper and call. The author proposes #11402 first; if rebasing after it lands, please retain both changes. I found no behavioral overlap in those test additions.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@simonbronk Thanks. This now conflicts with main in Export.Discovery.cpp, include/Api.hpp, tests/GuestKernelSys.cpp and docs/dev/debt/libkernel_sys.md; the conflicts are neighbouring stubs and entries, so keeping both sides resolves them (and keep #11402's CheckSettimeofday assertions if it lands first). Could you merge main?

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonbronk

Copy link
Copy Markdown
Contributor Author

@oneandonlydean merged main cc672fd into the branch, new head 8d0d8c6. All four conflicts kept both sides:

Linux run of 8d0d8c6, same container and configuration as before: the full build has no errors (exit 0), and 46 focused tests pass, guest_kernel_sys included, with no ASan report. check_conventions.py --base origin/main passes; pr_overlap.py finds no PR with these exports or conflicting lines.

AI-assisted: yes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonbronk

Copy link
Copy Markdown
Contributor Author

Merged main d820fef again, new head eea3c72. The only conflict was the include/Api.hpp line next to this PR's: it keeps main's scePthreadMutexInitForInternalLibc(PthreadMutex*, const PthreadMutexattr*) and this PR's scePthreadMutexIsowned(PthreadMutex*).

Linux run of eea3c72, same container and configuration as before: 48 focused tests pass, guest_kernel_sys included, with no ASan report, after building every tested target explicitly (exit 0). The full build stops only in libSceAddressSanitizer, unchanged here, whose declarations clash with GCC's __asan_* built-ins under ASan, on main too; #12894 addresses that. check_conventions.py --base origin/main passes. pr_overlap.py lists only #11219 on the Udp.cpp line agreed with @orezraey: whichever lands second keeps both.

AI-assisted: yes

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 30, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 30, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 30, 2026
@Retr0G28

Copy link
Copy Markdown
Contributor

@simonbronk #13695 adds a branch for FIOCLEX and FIONCLEX to the socket path of ioctl_nid_postfix, just before the if (!argument) line this PR changes, so git will report a conflict on neighbouring lines. The changes don't depend on each other. Whichever merges second keeps both: the new branch, then your argument check.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 1, 2026
# Conflicts:
#	core/libs/prx/libkernel/Socket/src/Udp.cpp
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 2, 2026
@oneandonlydean
oneandonlydean merged commit 9551c50 into boykopovar:main Nov 2, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants