fix(libkernel): clear the thread handle when scePthreadCreate rejects a long name as measured on a PS5 #11201

Merged
orezraey merged 2 commits from upstream-pr/11201 into main 2026-10-26 21:39:26 +00:00
orezraey commented 2026-10-25 21:45:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-25T21:45:47Z.


What

scePthreadCreate now sets the thread handle to 0 when it rejects a name of 32 bytes or more with ENAMETOOLONG, as a retail PS5 (firmware 13.60) does. Before, it left the handle as it was. Measured with a homebrew probe from a payload process; the source and console log are in this gist (pthrname2).

On the console, a handle pre-filled with 0xDEADBEEF:

  • reads 0 after a 32-byte or a 33-byte name returns 0x8002003f;
  • holds a real thread after a 31-byte name, and that thread joins with its exit value.

The debt entry said whether the handle is written before the name check was not measured; it now records the result. Whether the console clears the handle on other errors was not measured, so only the name check changes.

Before and after

scePthreadCreate, handle pre-filled with 0xDEADBEEF PS5 AnyPS5 before AnyPS5 now
32- or 33-byte name 0x8002003f, handle 0 0x8002003f, handle 0xDEADBEEF 0x8002003f, handle 0
31-byte name real thread, joins the same the same

Tested

Windows 11, WinLibs GCC 15.2, full build. The 31 tests matching pthread|thread_name|second_join pass 3 times out of 3. guest_pthread_second_join checks that a handle pre-filled with 0x77 reads null after a 32-byte and a 33-byte name. tools/check_conventions.py passes.

Code and tests at 3836941fb2 on main 9c6d153dec; head 3a2638ada6 only adds a debt note on pthread_create_name_np, so the tests were not rerun.

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: #8916 (it edits the debt entries next to this one; merge order only)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5009024773 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11201). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-25T21:45:47Z. --- ### What `scePthreadCreate` now sets the thread handle to 0 when it rejects a name of 32 bytes or more with `ENAMETOOLONG`, as a retail PS5 (firmware 13.60) does. Before, it left the handle as it was. Measured with a homebrew probe from a payload process; the source and console log are in [this gist](https://gist.github.com/orezraey/55c9100c4a2441ef7609513ca95e0f66) (`pthrname2`). On the console, a handle pre-filled with `0xDEADBEEF`: - reads 0 after a 32-byte or a 33-byte name returns `0x8002003f`; - holds a real thread after a 31-byte name, and that thread joins with its exit value. The debt entry said whether the handle is written before the name check was not measured; it now records the result. Whether the console clears the handle on other errors was not measured, so only the name check changes. **Before and after** | `scePthreadCreate`, handle pre-filled with `0xDEADBEEF` | PS5 | AnyPS5 before | AnyPS5 now | |---|---|---|---| | 32- or 33-byte name | `0x8002003f`, handle 0 | `0x8002003f`, handle `0xDEADBEEF` | `0x8002003f`, handle 0 | | 31-byte name | real thread, joins | the same | the same | ### 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. --> Windows 11, WinLibs GCC 15.2, full build. The 31 tests matching `pthread|thread_name|second_join` pass 3 times out of 3. `guest_pthread_second_join` checks that a handle pre-filled with 0x77 reads null after a 32-byte and a 33-byte name. `tools/check_conventions.py` passes. Code and tests at `3836941fb2` on `main` `9c6d153dec`; head `3a2638ada6` only adds a debt note on `pthread_create_name_np`, so the tests were not rerun. ### 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: #8916 (it edits the debt entries next to this one; merge order only) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 21:48:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T21:48:50Z.

Review: APPROVED


Reviewed head 3836941fb248bcff5af158efdfc71dade27bb71f. The linked PS5 probe log directly shows a 31-byte name succeeds and 32-/33-byte names return 0x8002003f with the output handle cleared. The updated regression test covers those same boundary cases and retains the 31-byte success check. The implementation clears the handle only on the measured long-name error path. Local check_conventions.py and git diff --check pass. The Windows full-build/test run is author-reported; hosted product builds are skipped. I found no remaining correctness issue in this change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645494634 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11201#pullrequestreview-5645494634). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T21:48:50Z. **Review:** APPROVED --- Reviewed head `3836941fb248bcff5af158efdfc71dade27bb71f`. The linked PS5 probe log directly shows a 31-byte name succeeds and 32-/33-byte names return `0x8002003f` with the output handle cleared. The updated regression test covers those same boundary cases and retains the 31-byte success check. The implementation clears the handle only on the measured long-name error path. Local `check_conventions.py` and `git diff --check` pass. The Windows full-build/test run is author-reported; hosted product builds are skipped. I found no remaining correctness issue in this change.
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!11201
No description provided.