fix(libkernel): fail scePthreadGetname and pthread_getname_np with EFAULT on an unwritable buffer #15571

Merged
Gruzzberry merged 2 commits from upstream-pr/15571 into main 2026-11-13 11:26:33 +00:00
Gruzzberry commented 2026-11-03 19:19:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-03T19:19:34Z.


What

scePthreadGetname and pthread_getname_np wrote the thread name and its terminator to the guest buffer with a raw memcpy, so a non-null buffer that is not writable, such as 0x10, a PROT_NONE page, a read-only page or one that runs out before the terminator, faulted the host. scePthreadGetname now builds the name (at most 31 characters and the terminator) in a local buffer, releases the thread's name lock, and writes it with GuestCopyOut_nid_no_patch, returning SCE_KERNEL_ERROR_EFAULT when the write fails; pthread_getname_np returns 14.

The order of the checks is unchanged: scePthreadGetname gives EINVAL for a null thread or buffer and ESRCH for an unknown handle before it touches the buffer, and pthread_getname_np still gives ESRCH for a null or joined thread and EFAULT for a null buffer (the existing measured behaviour), with the buffer untouched in those cases. A valid buffer receives the same bytes as before.

This is a host-safety fix, not console behaviour: EFAULT for a non-null bad buffer was not measured on a console, and FreeBSD 11.4's libthr has no pthread_get_name_np to take the order from. The entry is in docs/dev/debt/libkernel.md.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on current main (rebased after the full suite onto 130 newer commits, one of which adds 6 lines elsewhere in Pthread/src/Thread.cpp; guest_pthread_getname was rerun 3 times out of 3 on the rebased head).

  • guest_pthread_getname: new CheckUnusableBuffers covers a buffer at 0x10, on a PROT_NONE page, on a read-only page, and one byte short of the 5 bytes kept needs before an unreadable page (EFAULT from both functions, also for a joined thread through scePthreadGetname); the earlier checks winning over a bad buffer (ESRCH for a null thread, a joined thread and an unknown handle; EINVAL for a null thread with scePthreadGetname); a buffer that ends exactly at the unreadable page (written, nothing before it disturbed); and a null buffer (EINVAL). On main the test crashes; with this change it passes, three runs out of three. The existing name, joined-thread and null-buffer cases still pass.
  • Full suite (3924 tests) on this branch: 98 failed, the same set as on the scePthreadRename branch (#15567). 95 are the relinker and ELF tool tests that main gained recently, whose Python driver cannot start the relinker executable, which is not in this build tree; vulkan_validation_gate_reports and check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted fail because of the WindowsApps Python on this machine; agc_driver_store_over_untracked and agc_driver_direct_write_completion are GPU tests that also fail with the thread code reverted to main. Nothing thread related fails.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run.

Overlaps

tools/pr_overlap.py reports no conflicting file or export. It lists #3986, #4563, #8592, #12130 and #12642 as sharing Thread.cpp without comparing them, because those PRs conflict with main themselves; none of their diffs changes scePthreadGetname (#12130 and #12642 only call it in new tests with a valid buffer, which still works). My own #15567 changes scePthreadRename in the same file, a different function, and appends its own entry to docs/dev/debt/libkernel.md; the two entries are adjacent lines. #15567 was opened first, so it goes first and this PR rebases.

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: #15567; merge order: after #15567 (adjacent lines in docs/dev/debt/libkernel.md)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5143160680 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15571). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-03T19:19:34Z. --- ### What `scePthreadGetname` and `pthread_getname_np` wrote the thread name and its terminator to the guest buffer with a raw `memcpy`, so a non-null buffer that is not writable, such as 0x10, a `PROT_NONE` page, a read-only page or one that runs out before the terminator, faulted the host. `scePthreadGetname` now builds the name (at most 31 characters and the terminator) in a local buffer, releases the thread's name lock, and writes it with `GuestCopyOut_nid_no_patch`, returning `SCE_KERNEL_ERROR_EFAULT` when the write fails; `pthread_getname_np` returns 14. The order of the checks is unchanged: `scePthreadGetname` gives `EINVAL` for a null thread or buffer and `ESRCH` for an unknown handle before it touches the buffer, and `pthread_getname_np` still gives `ESRCH` for a null or joined thread and `EFAULT` for a null buffer (the existing measured behaviour), with the buffer untouched in those cases. A valid buffer receives the same bytes as before. This is a host-safety fix, not console behaviour: `EFAULT` for a non-null bad buffer was not measured on a console, and FreeBSD 11.4's libthr has no `pthread_get_name_np` to take the order from. The entry is in `docs/dev/debt/libkernel.md`. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main` (rebased after the full suite onto 130 newer commits, one of which adds 6 lines elsewhere in `Pthread/src/Thread.cpp`; `guest_pthread_getname` was rerun 3 times out of 3 on the rebased head). - `guest_pthread_getname`: new `CheckUnusableBuffers` covers a buffer at 0x10, on a `PROT_NONE` page, on a read-only page, and one byte short of the 5 bytes `kept` needs before an unreadable page (`EFAULT` from both functions, also for a joined thread through `scePthreadGetname`); the earlier checks winning over a bad buffer (`ESRCH` for a null thread, a joined thread and an unknown handle; `EINVAL` for a null thread with `scePthreadGetname`); a buffer that ends exactly at the unreadable page (written, nothing before it disturbed); and a null buffer (`EINVAL`). On `main` the test crashes; with this change it passes, three runs out of three. The existing name, joined-thread and null-buffer cases still pass. - Full suite (3924 tests) on this branch: 98 failed, the same set as on the `scePthreadRename` branch (#15567). 95 are the relinker and ELF tool tests that main gained recently, whose Python driver cannot start the `relinker` executable, which is not in this build tree; `vulkan_validation_gate_reports` and `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted` fail because of the WindowsApps Python on this machine; `agc_driver_store_over_untracked` and `agc_driver_direct_write_completion` are GPU tests that also fail with the thread code reverted to `main`. Nothing thread related fails. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run. ### Overlaps `tools/pr_overlap.py` reports no conflicting file or export. It lists #3986, #4563, #8592, #12130 and #12642 as sharing `Thread.cpp` without comparing them, because those PRs conflict with `main` themselves; none of their diffs changes `scePthreadGetname` (#12130 and #12642 only call it in new tests with a valid buffer, which still works). My own #15567 changes `scePthreadRename` in the same file, a different function, and appends its own entry to `docs/dev/debt/libkernel.md`; the two entries are adjacent lines. #15567 was opened first, so it goes first and this PR rebases. ### 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: #15567; merge order: after #15567 (adjacent lines in `docs/dev/debt/libkernel.md`) - [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)
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!15571
No description provided.