fix(libkernel): report the stack of adopted threads #761

Merged
merwie merged 1 commit from upstream-pr/761 into main 2026-10-05 23:16:47 +00:00
merwie commented 2026-10-05 21:34:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-05T21:34:18Z.


What

scePthreadAttrGet copies the thread's stackAddress / stackSize. Both stayed null/0:

  • for an adopted thread (the main thread, or any host thread that calls scePthreadSelf), on Windows and Linux;
  • for every guest thread on Linux, where RunThread never recorded the range (on Windows StartNativeThread already does).

The range now comes from the host thread when it is not already known: GetCurrentThreadStackLimits on Windows, pthread_getattr_np + pthread_attr_getstack on Linux.

Found with PPSA12544 (Hollow Knight: Silksong, Unity IL2CPP): its garbage collector gets each thread's stack through scePthreadAttrGet + scePthreadAttrGetstackaddr / scePthreadAttrGetstacksize (imported by Il2cppUserAssemblies.prx). With a null/0 main-thread stack it did not scan that stack, freed objects still referenced from it, and the title then faulted dividing by the length field of a zeroed array (0xc0000094) in its first managed code after a collection.

  • guest_thread_self: the main thread's and a worker's scePthreadAttrGet stack contain a local of that thread. With the change reverted the test aborts.

#702 adds a similar host stack query in the same file for sceKernelIsStack; whichever lands second can share it.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Ryzen 7 9800X3D, Radeon RX 9070 XT.

  • ctest: 315/317 passed, including guest_thread_self. agc_driver_lds_atomics64 and agc_driver_vopc_compare_table fail on this machine on main as well (Vulkan device tests).
  • PPSA12544 with its other startup fixes (#726): the divide fault is gone and the title loads sharedassets0.assets and level0.

Not tested on Linux.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4751028138 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/761). Originally posted by [merwie](https://github.com/merwie) on 2026-10-05T21:34:18Z. --- ### What `scePthreadAttrGet` copies the thread's `stackAddress` / `stackSize`. Both stayed null/0: - for an adopted thread (the main thread, or any host thread that calls `scePthreadSelf`), on Windows and Linux; - for every guest thread on Linux, where `RunThread` never recorded the range (on Windows `StartNativeThread` already does). The range now comes from the host thread when it is not already known: `GetCurrentThreadStackLimits` on Windows, `pthread_getattr_np` + `pthread_attr_getstack` on Linux. Found with PPSA12544 (Hollow Knight: Silksong, Unity IL2CPP): its garbage collector gets each thread's stack through `scePthreadAttrGet` + `scePthreadAttrGetstackaddr` / `scePthreadAttrGetstacksize` (imported by `Il2cppUserAssemblies.prx`). With a null/0 main-thread stack it did not scan that stack, freed objects still referenced from it, and the title then faulted dividing by the length field of a zeroed array (`0xc0000094`) in its first managed code after a collection. - `guest_thread_self`: the main thread's and a worker's `scePthreadAttrGet` stack contain a local of that thread. With the change reverted the test aborts. #702 adds a similar host stack query in the same file for `sceKernelIsStack`; whichever lands second can share it. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Ryzen 7 9800X3D, Radeon RX 9070 XT. - `ctest`: 315/317 passed, including `guest_thread_self`. `agc_driver_lds_atomics64` and `agc_driver_vopc_compare_table` fail on this machine on `main` as well (Vulkan device tests). - PPSA12544 with its other startup fixes (#726): the divide fault is gone and the title loads `sharedassets0.assets` and `level0`. Not tested on Linux. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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] AI-assisted: yes 🤖 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!761
No description provided.