fix(libkernel): report the creation guard size from the thread attribute queries #8041

Closed
plokmklo wants to merge 3 commits from upstream-pr/8041 into main
plokmklo commented 2026-10-19 18:39:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by plokmklo on 2026-10-19T18:39:52Z.


What

pthread_attr_get_np and scePthreadAttrGet never wrote the guard size into the destination attribute, so a thread created with a guard size of 0 still reported whatever the caller's attribute already held. FreeBSD 11.4 _pthread_attr_get_np (thr_attr.c) copies the thread's creation attribute, guard size included, and the default is the page size (_thr_guard_default).

PthreadPrivate now stores the guard size of the attribute the thread was created with (0x1000 for default-attribute and adopted threads), and both queries report it. A new guest_pthread_guard_size test creates threads with guard sizes 0, 1, 100, 0x1000 and 0x10000 and requires both queries to report exactly those values — from inside the live thread and after join, against a destination attribute pre-filled with a stale value — while a default-attribute thread and the adopted main thread report 0x1000. It fails on main (the stale value survives) and passes with this change.

Tested

Ubuntu 24.04 (GCC 13.3, sandbox build): guest_pthread_guard_size, guest_pthread_attr, guest_pthread_attr_stack, guest_pthread_attr_errors, guest_pthread_stack, guest_pthread_join_detach all pass; the PthreadPrivate field default keeps adopted threads at 0x1000.

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: #6891
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4925661106 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8041). Originally posted by [plokmklo](https://github.com/plokmklo) on 2026-10-19T18:39:52Z. --- ### What `pthread_attr_get_np` and `scePthreadAttrGet` never wrote the guard size into the destination attribute, so a thread created with a guard size of 0 still reported whatever the caller's attribute already held. FreeBSD 11.4 `_pthread_attr_get_np` (`thr_attr.c`) copies the thread's creation attribute, guard size included, and the default is the page size (`_thr_guard_default`). `PthreadPrivate` now stores the guard size of the attribute the thread was created with (0x1000 for default-attribute and adopted threads), and both queries report it. A new `guest_pthread_guard_size` test creates threads with guard sizes 0, 1, 100, 0x1000 and 0x10000 and requires both queries to report exactly those values — from inside the live thread and after join, against a destination attribute pre-filled with a stale value — while a default-attribute thread and the adopted main thread report 0x1000. It fails on main (the stale value survives) and passes with this change. ### Tested Ubuntu 24.04 (GCC 13.3, sandbox build): `guest_pthread_guard_size`, `guest_pthread_attr`, `guest_pthread_attr_stack`, `guest_pthread_attr_errors`, `guest_pthread_stack`, `guest_pthread_join_detach` all pass; the PthreadPrivate field default keeps adopted threads at 0x1000. ### 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 under Silent stubs in their library's technical 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 - [ ] Depends on: none - [x] Fixes: #6891 - [ ] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
wachi35676 commented 2026-10-19 22:29:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T22:29:43Z.


Three things, from a Windows build of this PR merged onto main 9127ea36d (Windows 11 Home 26300, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON).

  1. Duplicate. #6893 (open since 2026-10-16) makes the same change:

    • guardSize in PthreadPrivate;
    • reported by pthread_attr_get_np and scePthreadAttrGet;
    • a new GuestPthreadGuardSize.cpp.
      Its head has been run on Windows and on Linux by other reviewers, with a negative control.
  2. The new test doesn't compile with MinGW. ProbeEntry has no APS5_VABI, so it can't convert to PthreadEntry (sysv_abi):

    GuestPthreadGuardSize.cpp:70:50: error: invalid conversion from 'void* (*)(void*)' to 'PthreadEntry' {aka 'void* (__attribute__((sysv_abi)) *)(void*)'} [-fpermissive]
    

    The same error appears at line 82. A Windows build with BUILD_TESTING=ON stops there.

  3. Wrong value for a caller-supplied stack. FreeBSD 11.4 create_stack sets pattr->guardsize_attr = 0 when stackaddr_attr is set, and pthread_attr_get_np then reports 0. Here the thread keeps the attribute's guard size. A probe:

    • creates a thread with scePthreadAttrSetstack(&attr, stack, 0x40000) and scePthreadAttrSetguardsize(&attr, 0x2000);
    • joins it;
    • calls scePthreadAttrGet into an attribute pre-set to 0x7000.

    It prints:

    user stack + guard 0x2000: create=0 attrget=0 reported guard=0x2000 (FreeBSD 11.4 create_stack: 0)
    

    #6893 handles this case: guardSize = attr->stackAddress ? 0 : attr->_guardsize.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6278786395 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8041#issuecomment-6278786395). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-19T22:29:43Z. --- Three things, from a Windows build of this PR merged onto `main` 9127ea36d (Windows 11 Home 26300, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`). 1. **Duplicate.** #6893 (open since 2026-10-16) makes the same change: - `guardSize` in `PthreadPrivate`; - reported by `pthread_attr_get_np` and `scePthreadAttrGet`; - a new `GuestPthreadGuardSize.cpp`. Its head has been run on Windows and on Linux by other reviewers, with a negative control. 2. **The new test doesn't compile with MinGW.** `ProbeEntry` has no `APS5_VABI`, so it can't convert to `PthreadEntry` (`sysv_abi`): ``` GuestPthreadGuardSize.cpp:70:50: error: invalid conversion from 'void* (*)(void*)' to 'PthreadEntry' {aka 'void* (__attribute__((sysv_abi)) *)(void*)'} [-fpermissive] ``` The same error appears at line 82. A Windows build with `BUILD_TESTING=ON` stops there. 3. **Wrong value for a caller-supplied stack.** FreeBSD 11.4 [`create_stack`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_create.c#L237-L250) sets `pattr->guardsize_attr = 0` when `stackaddr_attr` is set, and `pthread_attr_get_np` then reports 0. Here the thread keeps the attribute's guard size. A probe: - creates a thread with `scePthreadAttrSetstack(&attr, stack, 0x40000)` and `scePthreadAttrSetguardsize(&attr, 0x2000)`; - joins it; - calls `scePthreadAttrGet` into an attribute pre-set to 0x7000. It prints: ``` user stack + guard 0x2000: create=0 attrget=0 reported guard=0x2000 (FreeBSD 11.4 create_stack: 0) ``` #6893 handles this case: `guardSize = attr->stackAddress ? 0 : attr->_guardsize`. AI-assisted: yes (Claude Code); results above were run on this machine.
oneandonlydean commented 2026-10-20 04:50:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T04:50:18Z.


Thanks. The attribute change itself looks right, but the new test doesn't compile on Windows, so CI fails before it runs (run 39370718241):

GuestPthreadGuardSize.cpp:70:50: error: invalid conversion from 'void* (*)(void*)' to 'PthreadEntry' {aka 'void* (__attribute__((sysv_abi)) *)(void*)'}
GuestPthreadGuardSize.cpp:82:50: (same)

PthreadEntry is a System V function pointer, and ProbeEntry (line 29) is declared with the host's default ABI. On Linux the two are the same ABI, so it compiles there; on Windows they aren't. The other pthread tests declare their entries with the macro, e.g. static void* APS5_VABI ReturnArgument(void* arg). A one-word fix:

static void* APS5_VABI ProbeEntry(void* opaque) {

With that change, the file compiles cleanly for Windows here (llvm-mingw, -fsyntax-only), and both errors go away.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6283645479 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8041#issuecomment-6283645479). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T04:50:18Z. --- Thanks. The attribute change itself looks right, but the new test doesn't compile on Windows, so CI fails before it runs (run 39370718241): ``` GuestPthreadGuardSize.cpp:70:50: error: invalid conversion from 'void* (*)(void*)' to 'PthreadEntry' {aka 'void* (__attribute__((sysv_abi)) *)(void*)'} GuestPthreadGuardSize.cpp:82:50: (same) ``` `PthreadEntry` is a System V function pointer, and `ProbeEntry` (line 29) is declared with the host's default ABI. On Linux the two are the same ABI, so it compiles there; on Windows they aren't. The other pthread tests declare their entries with the macro, e.g. `static void* APS5_VABI ReturnArgument(void* arg)`. A one-word fix: ```cpp static void* APS5_VABI ProbeEntry(void* opaque) { ``` With that change, the file compiles cleanly for Windows here (llvm-mingw, `-fsyntax-only`), and both errors go away.
MarcZX10 commented 2026-11-21 07:10:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T07:10:04Z.

Review: COMMENTED


Reviewed — no blocking findings

The guard size is copied from the creation attribute into each thread and returned by both query functions; the prepared bundle includes all six changed files, and I found no verified blocking defect.

What I ran

Reviewed head 0d8f38812afa0b39ff117cbf7a16561889f08be0 using the prepared diff and repo_read.py targeted reads. No tests or build commands were run. The author reports running six tests on Ubuntu 24.04; I did not independently verify those results.

Results

Check Result Note
Diff and changed code passed All six changed files are included and reviewed
Build and tests not run Results in the PR description are author reported
Security and performance passed No concrete concern found in inspected changes

Negative control

Not run. No assertion or code mutation was applied.

Findings

Blocking

None.

Non-blocking

None.

Sources

Reviewed core/libs/prx/libkernel/CMakeLists.txt, core/libs/prx/libkernel/Pthread/Posix/Attr.cpp, core/libs/prx/libkernel/Pthread/include/Pthread.hpp, core/libs/prx/libkernel/Pthread/src/Attr.cpp, core/libs/prx/libkernel/Pthread/src/Thread.cpp, and core/libs/tests/GuestPthreadGuardSize.cpp against the prepared diff at the pinned head.

Not verified

Build and test results were not independently verified. The reviewed bundle listed no excluded changed files; I read the relevant changed code and test source shown in the diff and targeted repository ranges for surrounding implementation.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6003726825 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8041#pullrequestreview-6003726825). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T07:10:04Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The guard size is copied from the creation attribute into each thread and returned by both query functions; the prepared bundle includes all six changed files, and I found no verified blocking defect. ## What I ran Reviewed head `0d8f38812afa0b39ff117cbf7a16561889f08be0` using the prepared diff and `repo_read.py` targeted reads. No tests or build commands were run. The author reports running six tests on Ubuntu 24.04; I did not independently verify those results. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | All six changed files are included and reviewed | | Build and tests | not run | Results in the PR description are author reported | | Security and performance | passed | No concrete concern found in inspected changes | ## Negative control Not run. No assertion or code mutation was applied. ## Findings ### Blocking None. ### Non-blocking None. ## Sources Reviewed `core/libs/prx/libkernel/CMakeLists.txt`, `core/libs/prx/libkernel/Pthread/Posix/Attr.cpp`, `core/libs/prx/libkernel/Pthread/include/Pthread.hpp`, `core/libs/prx/libkernel/Pthread/src/Attr.cpp`, `core/libs/prx/libkernel/Pthread/src/Thread.cpp`, and `core/libs/tests/GuestPthreadGuardSize.cpp` against the prepared diff at the pinned head. ## Not verified Build and test results were not independently verified. The reviewed bundle listed no excluded changed files; I read the relevant changed code and test source shown in the diff and targeted repository ranges for surrounding implementation. ## To get this to a good state Nothing is required.

Pull request closed

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!8041
No description provided.