fix(libkernel): return EFAULT from cancellation state and type setters for unwritable pointers #13582

Merged
pushkarreddyy merged 5 commits from upstream-pr/13582 into main 2026-11-11 14:01:22 +00:00
pushkarreddyy commented 2026-10-30 18:47:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-30T18:47:48Z.


What

Fixes #13568.
scePthreadSetcancelstate, scePthreadSetcanceltype, pthread_setcancelstate_nid_postfix, and pthread_setcanceltype_nid_postfix wrote old_state and old_type directly to guest pointers without copy safety verification. When called with an unwritable non-null pointer (e.g. into read-only memory or unmapped space), the host crashed with SIGSEGV.

This change:

  • Uses GuestCopyOut_nid_no_patch in scePthreadSetcancelstate and scePthreadSetcanceltype to safely copy the previous cancellation state/type to guest memory.
  • Returns SCE_KERNEL_ERROR_EFAULT (0x8002000e) and 14 (EFAULT) without mutating the thread's cancellation state or type on copy-out failure.
  • Adds regression unit tests in core/libs/tests/GuestPthreadCancel.cpp testing unwritable pointers and ensuring state remains unaltered on failure.
  • Updates documentation in docs/dev/debt/libkernel.md.

Tested

  • Windows 11 (WinLibs GCC 15.2.0, MSYS2 UCRT64 GCC 16.1.0, Release): guest_pthread_cancel passes 3 of 3 runs (verified by @maskym, @JJenkx, @gabichuuuuu); negative control with unmodified Thread.cpp reproduces access violation 0xc0000005 5 of 5 runs (verified by @bunbeans2-arch).
  • Linux x86_64: guest_pthread_cancel passes 3 of 3 runs (verified by @jotitadev).
  • Pre-flight validation with python tools/check_conventions.py --base origin/main (passed: 0 findings).

Checklist

  • What
  • Tested
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5089450024 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13582). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-30T18:47:48Z. --- ### What Fixes #13568. `scePthreadSetcancelstate`, `scePthreadSetcanceltype`, `pthread_setcancelstate_nid_postfix`, and `pthread_setcanceltype_nid_postfix` wrote `old_state` and `old_type` directly to guest pointers without copy safety verification. When called with an unwritable non-null pointer (e.g. into read-only memory or unmapped space), the host crashed with `SIGSEGV`. This change: - Uses `GuestCopyOut_nid_no_patch` in `scePthreadSetcancelstate` and `scePthreadSetcanceltype` to safely copy the previous cancellation state/type to guest memory. - Returns `SCE_KERNEL_ERROR_EFAULT` (`0x8002000e`) and `14` (`EFAULT`) without mutating the thread's cancellation state or type on copy-out failure. - Adds regression unit tests in `core/libs/tests/GuestPthreadCancel.cpp` testing unwritable pointers and ensuring state remains unaltered on failure. - Updates documentation in `docs/dev/debt/libkernel.md`. ### Tested - Windows 11 (WinLibs GCC 15.2.0, MSYS2 UCRT64 GCC 16.1.0, Release): `guest_pthread_cancel` passes 3 of 3 runs (verified by @maskym, @JJenkx, @gabichuuuuu); negative control with unmodified `Thread.cpp` reproduces access violation `0xc0000005` 5 of 5 runs (verified by @bunbeans2-arch). - Linux x86_64: `guest_pthread_cancel` passes 3 of 3 runs (verified by @jotitadev). - Pre-flight validation with `python tools/check_conventions.py --base origin/main` (passed: 0 findings). ### Checklist - [x] What - [x] Tested - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
jotitadev commented 2026-10-30 21:31:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-30T21:31:47Z.

Review: COMMENTED


First Linux run of head 7b18b9ff4a, merged onto main 6472c708ae (clean merge). Ubuntu 26.04, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON, Ninja.

  • guest_pthread_cancel: passes 3/3.
  • ctest -R ^guest_pthread_cancel: 27/27.
  • public_headers: passes.
  • Red check: with only core/libs/prx/libkernel/Pthread/src/Thread.cpp reverted to main (new test kept), guest_pthread_cancel segfaults 3/3. So on Linux the new checks fail without the fix.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5718883745 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13582#pullrequestreview-5718883745). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-30T21:31:47Z. **Review:** COMMENTED --- First Linux run of head `7b18b9ff4a`, merged onto `main` `6472c708ae` (clean merge). Ubuntu 26.04, i7-8700, GCC 15.2, Release, `BUILD_TESTING=ON`, Ninja. - `guest_pthread_cancel`: passes 3/3. - `ctest -R ^guest_pthread_cancel`: 27/27. - `public_headers`: passes. - Red check: with only `core/libs/prx/libkernel/Pthread/src/Thread.cpp` reverted to `main` (new test kept), `guest_pthread_cancel` segfaults 3/3. So on Linux the new checks fail without the fix. Written with AI assistance (Claude Code). I ran the builds and tests above 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!13582
No description provided.