feat(libkernel_sys): implement pthread_getstack_np #15111

Merged
arcwiser merged 5 commits from upstream-pr/15111 into main 2026-11-18 13:27:30 +00:00
arcwiser commented 2026-11-02 16:40:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by arcwiser on 2026-11-02T16:40:04Z.


What

Implement pthread_getstack_np in libkernel_sys with the firmware 13.42 two-output ABI. Report the calling guest thread's tracked stack base and size, preserve the firmware write order, and document the unmeasured incidental return value plus EFAULT host-safety fallback. Add coverage for the initial thread, a child calling the export on its custom-sized stack, and invalid output pointers.

Tested

Windows, MSYS2 UCRT64 GCC 16.2.0:

  • cmake --build build --target guest_kernel_sys_pthread_np_tests --parallel 6
  • ctest --test-dir build -R '^guest_kernel_sys_pthread_np_(rwlockattr|kind|stack)$|^public_headers$' --output-on-failure
  • python tools/check_conventions.py --base origin/main
  • python tools/pr_overlap.py --branch codex/sys-library-functions; manually compared every reported shared-file PR, none implements pthread_getstack_np

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
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5123874646 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15111). Originally posted by [arcwiser](https://github.com/arcwiser) on 2026-11-02T16:40:04Z. --- ### What Implement `pthread_getstack_np` in libkernel_sys with the firmware 13.42 two-output ABI. Report the calling guest thread's tracked stack base and size, preserve the firmware write order, and document the unmeasured incidental return value plus `EFAULT` host-safety fallback. Add coverage for the initial thread, a child calling the export on its custom-sized stack, and invalid output pointers. ### 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, MSYS2 UCRT64 GCC 16.2.0: - `cmake --build build --target guest_kernel_sys_pthread_np_tests --parallel 6` - `ctest --test-dir build -R '^guest_kernel_sys_pthread_np_(rwlockattr|kind|stack)$|^public_headers$' --output-on-failure` - `python tools/check_conventions.py --base origin/main` - `python tools/pr_overlap.py --branch codex/sys-library-functions`; manually compared every reported shared-file PR, none implements `pthread_getstack_np` ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
BunnyFl commented 2026-11-02 17:01:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T17:01:45Z.

Review: CHANGES_REQUESTED


The firmware gives this export a different signature, so the new argument order doesn't match what titles call.

Firmware 13.42's libkernel.sprx (sha256 4be40bf838a46e1ff83a1809960672526139b09dda6c4a6592cfeb485d29f243), pthread_getstack_np (ruZ9hhQ8oUk, 0xdb70), read from disassembly to the return, not measured on a console. This is the whole function:

db70: mov rax, fs:[0x10]          ; the calling thread
db79: mov rcx, [rax+0xa8]
db80: mov [rdi], rcx              ; *first argument = stack address
db83: mov rax, [rax+0xb0]
db8a: mov [rsi], rax              ; *second argument = stack size
db8d: ret
  1. No thread argument. It reports the calling thread's stack: rdi is the address output and rsi the size output. The PR's (Pthread thread, void** stackAddress, std::size_t* stackSize) reads the caller's address pointer as a thread handle. A title calling pthread_getstack_np(&addr, &size) would get ESRCH, or worse.
  2. No checks. There's no null test and no live-thread lookup, so the EINVAL and ESRCH results have no counterpart in the module. A null output faults on the console. A checked write for null outputs is fine as a host-safety fallback if the debt entry says so; the two error codes shouldn't be pinned as console behaviour.
  3. There's no explicit return value: rax holds the stack size at ret. What callers rely on isn't settled, so if the declaration keeps int, the entry should say the result is unmeasured.

Could it become (void** stackAddress, std::size_t* stackSize) for the current thread, with the tests reworked to match? I didn't build this branch; the points above come from the module.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5740233855 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15111#pullrequestreview-5740233855). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T17:01:45Z. **Review:** CHANGES_REQUESTED --- The firmware gives this export a different signature, so the new argument order doesn't match what titles call. Firmware 13.42's `libkernel.sprx` (sha256 `4be40bf838a46e1ff83a1809960672526139b09dda6c4a6592cfeb485d29f243`), `pthread_getstack_np` (`ruZ9hhQ8oUk`, `0xdb70`), read from disassembly to the return, not measured on a console. This is the whole function: ``` db70: mov rax, fs:[0x10] ; the calling thread db79: mov rcx, [rax+0xa8] db80: mov [rdi], rcx ; *first argument = stack address db83: mov rax, [rax+0xb0] db8a: mov [rsi], rax ; *second argument = stack size db8d: ret ``` 1. **No thread argument.** It reports the *calling* thread's stack: `rdi` is the address output and `rsi` the size output. The PR's `(Pthread thread, void** stackAddress, std::size_t* stackSize)` reads the caller's address pointer as a thread handle. A title calling `pthread_getstack_np(&addr, &size)` would get `ESRCH`, or worse. 2. **No checks.** There's no null test and no live-thread lookup, so the `EINVAL` and `ESRCH` results have no counterpart in the module. A null output faults on the console. A checked write for null outputs is fine as a host-safety fallback if the debt entry says so; the two error codes shouldn't be pinned as console behaviour. 3. There's no explicit return value: `rax` holds the stack size at `ret`. What callers rely on isn't settled, so if the declaration keeps `int`, the entry should say the result is unmeasured. Could it become `(void** stackAddress, std::size_t* stackSize)` for the current thread, with the tests reworked to match? I didn't build this branch; the points above come from the module.
BunnyFl commented 2026-11-02 17:46:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T17:46:24Z.

Review: APPROVED


Thanks. e5322eaed8 matches firmware 13.42's pthread_getstack_np (0xdb70 in libkernel.sprx):

  1. (void** stackAddress, std::size_t* stackSize) for the calling thread, with no thread argument.
  2. No console-side checks. The EFAULT result for a null or unwritable output is listed in libkernel_sys.md as a host-safety fallback where the module faults, with the address written before the size, in the module's order.
  3. The stack size comes back as the result, and the debt entry says callers' use of it is unmeasured.

thread->stackAddress/stackSize are the low address and size of the stack, both for host-started threads (SetStackFromHost) and for guest threads created with a stack size, so both kinds of thread report their real stack.

Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto main: the three guest_kernel_sys_pthread_np_* modes and public_headers pass 4/4, and check_conventions.py passes. With main's ThreadQueries.cpp, Export.Discovery.cpp and Api.hpp, the new tests don't compile against the old parameterless stub.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5740816270 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15111#pullrequestreview-5740816270). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T17:46:24Z. **Review:** APPROVED --- Thanks. e5322eaed8 matches firmware 13.42's `pthread_getstack_np` (`0xdb70` in `libkernel.sprx`): 1. `(void** stackAddress, std::size_t* stackSize)` for the calling thread, with no thread argument. 2. No console-side checks. The `EFAULT` result for a null or unwritable output is listed in `libkernel_sys.md` as a host-safety fallback where the module faults, with the address written before the size, in the module's order. 3. The stack size comes back as the result, and the debt entry says callers' use of it is unmeasured. `thread->stackAddress`/`stackSize` are the low address and size of the stack, both for host-started threads (`SetStackFromHost`) and for guest threads created with a stack size, so both kinds of thread report their real stack. Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto `main`: the three `guest_kernel_sys_pthread_np_*` modes and `public_headers` pass 4/4, and `check_conventions.py` passes. With `main`'s `ThreadQueries.cpp`, `Export.Discovery.cpp` and `Api.hpp`, the new tests don't compile against the old parameterless stub.
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!15111
No description provided.