feat(libkernel): implement pthread_cleanup_push and pthread_cleanup_pop #8428

Merged
hypermoist merged 1 commit from upstream-pr/8428 into main 2026-10-21 13:32:45 +00:00
hypermoist commented 2026-10-20 12:34:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T12:34:03Z.


What

Implements pthread_cleanup_push (4ZeZWcMsAV0) and pthread_cleanup_pop (RVxb0Ssa5t0), which are absent today. Discussion #7941 reports them as missing imports for CUSA04865.

In the decrypted libkernel.sprx they are the FreeBSD libthr pair:

  • pthread_cleanup_push (0x50a0) allocates the handler record, marks it as heap-owned and links it on the current thread's handler list, the same list __pthread_cleanup_push_imp uses with a caller-provided record.
  • pthread_cleanup_pop (0x5100) has the same body as __pthread_cleanup_pop_imp (0x5050). It unlinks the newest record, runs it if execute is non-zero, and frees it if it is heap-owned.

Here:

  • pthread_cleanup_push allocates a CleanupHandler with onHeap = 1, a field that already exists but was never set.
  • pthread_cleanup_pop forwards to __pthread_cleanup_pop_imp.
  • __pthread_cleanup_pop_imp now deletes heap-owned records after running them. So records pushed by pthread_cleanup_push and run by scePthreadExit or a cancellation are freed too.

The console push also sets the thread's "unwind disabled" flag. That flag is not modelled, because guest frames are not unwound on exit here.

Tested

Linux (CachyOS, GCC, Release). guest_pthread_cancel gains HeapHandlers, which mixes __pthread_cleanup_push_imp and pthread_cleanup_push records, pops with and without execute through both pop forms, then exits. It checks the run order (3, 4, 1), the count and the exit value. The test passes, as do guest_once, guest_pthread_cancel_recovery and guest_pthread_cancel_recovery_coarse.

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 verified
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code), reviewed by the contributor

🤖 Generated with Claude Code

https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4935780678 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8428). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-20T12:34:03Z. --- ### What Implements `pthread_cleanup_push` (`4ZeZWcMsAV0`) and `pthread_cleanup_pop` (`RVxb0Ssa5t0`), which are absent today. Discussion #7941 reports them as missing imports for CUSA04865. In the decrypted `libkernel.sprx` they are the FreeBSD libthr pair: - **`pthread_cleanup_push` (0x50a0)** allocates the handler record, marks it as heap-owned and links it on the current thread's handler list, the same list `__pthread_cleanup_push_imp` uses with a caller-provided record. - **`pthread_cleanup_pop` (0x5100)** has the same body as `__pthread_cleanup_pop_imp` (0x5050). It unlinks the newest record, runs it if `execute` is non-zero, and frees it if it is heap-owned. Here: - `pthread_cleanup_push` allocates a `CleanupHandler` with `onHeap = 1`, a field that already exists but was never set. - `pthread_cleanup_pop` forwards to `__pthread_cleanup_pop_imp`. - `__pthread_cleanup_pop_imp` now deletes heap-owned records after running them. So records pushed by `pthread_cleanup_push` and run by `scePthreadExit` or a cancellation are freed too. The console push also sets the thread's "unwind disabled" flag. That flag is not modelled, because guest frames are not unwound on exit here. ### Tested Linux (CachyOS, GCC, Release). `guest_pthread_cancel` gains `HeapHandlers`, which mixes `__pthread_cleanup_push_imp` and `pthread_cleanup_push` records, pops with and without `execute` through both pop forms, then exits. It checks the run order (3, 4, 1), the count and the exit value. The test passes, as do `guest_once`, `guest_pthread_cancel_recovery` and `guest_pthread_cancel_recovery_coarse`. ### Checklist - [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 verified - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code), reviewed by the contributor 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
github-actions[bot] commented 2026-10-20 12:34:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T12:34:39Z.


Progress report (88942aa → fa0e7b8)

📈 System libraries: 64.16% (+0.01%, +2 implemented functions)

✅ 2 implemented
Library Name
libkernel pthread_cleanup_pop_nid_postfix
libkernel pthread_cleanup_push_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290953846 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8428#issuecomment-6290953846). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T12:34:39Z. --- <!-- progress-report --> ### Progress report (88942aa → fa0e7b8) 📈 **System libraries**: 64.16% (+0.01%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libkernel | `pthread_cleanup_pop_nid_postfix` | | libkernel | `pthread_cleanup_push_nid_postfix` | </details>
github-actions[bot] commented 2026-10-20 12:45:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T12:45:26Z.


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 3986 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 5021 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6044 core/libs/tests/GuestPthreadCancel.cpp
PR 6879 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7149 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7201 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 8592 core/libs/prx/libkernel/Pthread/src/Thread.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291149221 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8428#issuecomment-6291149221). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T12:45:26Z. --- <!-- pr-overlap --> 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 3986](https://www.github.com/boykopovar/AnyPS5/pull/3986) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestPthreadCancel.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 8592](https://www.github.com/boykopovar/AnyPS5/pull/8592) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` |
adriadam10 commented 2026-10-21 13:34:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T13:34:49Z.


Checked the push/pop pair against the existing __pthread_cleanup_*_imp path: heap records are marked onHeap, freed after running in pop, scePthreadExit and cancellation; caller-provided records are untouched. HeapHandlers pins the order (3, 4, 1), the count and the exit value and cannot pass without the new exports; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6317416282 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8428#issuecomment-6317416282). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T13:34:49Z. --- Checked the push/pop pair against the existing `__pthread_cleanup_*_imp` path: heap records are marked `onHeap`, freed after running in pop, `scePthreadExit` and cancellation; caller-provided records are untouched. `HeapHandlers` pins the order (3, 4, 1), the count and the exit value and cannot pass without the new exports; merged.
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!8428
No description provided.