libkernel: pthread_attr_get_np and scePthreadAttrGet do not report the thread's guard size; fix branch ready #6891

Open
opened 2026-10-16 16:45:47 +00:00 by husseinkad · 3 comments
husseinkad commented 2026-10-16 16:45:47 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-16T16:45:47Z.


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

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/pthread-attr-guardsize (two commits; it merges cleanly into current main; libkernel/Pthread/include/Pthread.hpp, src/Thread.cpp, Posix/Attr.cpp, src/Attr.cpp, the new test tests/GuestPthreadGuardSize.cpp registered in the libkernel CMakeLists.txt, and a debt entry in docs/dev/debt/libkernel.md; the second commit only moves the debt entry away from #4992's lines, so squash on merge). PthreadPrivate now stores the creation guard size (default 0x1000), and both queries report it.

Checks:

  • The new test fails on main and passes with the change; guest_pthread_attr and guest_pthread_stack pass.
  • Probed against main through the guest exports: a thread created with guard size 0, 1, 100, 4096 and 65536 and the default now reports 0, 1, 100, 0x1000, 0x10000 and 0x1000 (main returns the caller's stale value every time); scePthreadAttrGet and pthread_attr_get_np agree, for running and finished threads; the main thread reports 0x1000; a pthread_attr_get_np then pthread_create round trip keeps the value (0x3000 in, 0x3000 out); pthread_attr_getguardsize and the setters, and the null-argument returns, are unchanged. Creating 6,000 threads shows memory plateauing after about 3 batches, with run-to-run noise larger than the 8-byte field.
  • Two independent reviews found nothing wrong with the implementation. check_conventions.py is clean and pr_overlap.py found overlap only with #5451 (it sets a different field in the same functions, merges cleanly) and #4888 (a textual conflict in CMakeLists.txt only).

Not verified (recorded in the debt entry): the guard size is only reported, not applied to the host stack, and the console's value is unmeasured. One reviewer recalls from memory (offline) that libthr rounds a requested guard up to a page when it allocates the stack, so a guard of 100 would read back as 4096, and that the main thread's guard may be 0 rather than 0x1000; the branch stores the raw requested value. create_stack in thr_create.c forces the guard to 0 for a user-supplied stack address, which this repo ignores. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5887864813 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6891). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-16T16:45:47Z. --- `pthread_attr_get_np` and `scePthreadAttrGet` never write the guard size into the destination attribute, so a thread created with a guard size of 0 still reports whatever the caller's attribute already held. FreeBSD 11.4 `_pthread_attr_get_np` in `thr_attr.c` copies the thread's creation attribute, guard size included, and the default is the page size (`_thr_guard_default`). Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/pthread-attr-guardsize (two commits; it merges cleanly into current main; `libkernel/Pthread/include/Pthread.hpp`, `src/Thread.cpp`, `Posix/Attr.cpp`, `src/Attr.cpp`, the new test `tests/GuestPthreadGuardSize.cpp` registered in the libkernel `CMakeLists.txt`, and a debt entry in `docs/dev/debt/libkernel.md`; the second commit only moves the debt entry away from #4992's lines, so squash on merge). `PthreadPrivate` now stores the creation guard size (default 0x1000), and both queries report it. Checks: - The new test fails on main and passes with the change; `guest_pthread_attr` and `guest_pthread_stack` pass. - Probed against main through the guest exports: a thread created with guard size 0, 1, 100, 4096 and 65536 and the default now reports 0, 1, 100, 0x1000, 0x10000 and 0x1000 (main returns the caller's stale value every time); `scePthreadAttrGet` and `pthread_attr_get_np` agree, for running and finished threads; the main thread reports 0x1000; a `pthread_attr_get_np` then `pthread_create` round trip keeps the value (0x3000 in, 0x3000 out); `pthread_attr_getguardsize` and the setters, and the null-argument returns, are unchanged. Creating 6,000 threads shows memory plateauing after about 3 batches, with run-to-run noise larger than the 8-byte field. - Two independent reviews found nothing wrong with the implementation. `check_conventions.py` is clean and `pr_overlap.py` found overlap only with #5451 (it sets a different field in the same functions, merges cleanly) and #4888 (a textual conflict in `CMakeLists.txt` only). Not verified (recorded in the debt entry): the guard size is only reported, not applied to the host stack, and the console's value is unmeasured. One reviewer recalls from memory (offline) that libthr rounds a requested guard up to a page when it allocates the stack, so a guard of 100 would read back as 4096, and that the main thread's guard may be 0 rather than 0x1000; the branch stores the raw requested value. `create_stack` in `thr_create.c` forces the guard to 0 for a user-supplied stack address, which this repo ignores. Only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-16 16:54:26 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T16:54:26Z.


Carried the two commits from husseinkad:fix/pthread-attr-guardsize into PR #6893, preserving Hussein Kadhim as the author of both commits. The branch is based on current main. I built the new focused test on Linux; this host aborts at the project CPU-feature guard before the assertions. The Windows regression and related pthread test results are credited to the issue report in the PR description.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6220448700 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6891#issuecomment-6220448700). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-16T16:54:26Z. --- Carried the two commits from `husseinkad:fix/pthread-attr-guardsize` into [PR #6893](https://github.com/boykopovar/AnyPS5/pull/6893), preserving Hussein Kadhim as the author of both commits. The branch is based on current `main`. I built the new focused test on Linux; this host aborts at the project CPU-feature guard before the assertions. The Windows regression and related pthread test results are credited to the issue report in the PR description.
MarcZX10 commented 2026-10-21 16:29:14 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T16:29:14Z.


A check against the full open PR set found that #8041 already carries the same guard-size behavior and focused regression test. I closed my duplicate #6893 so there is one implementation for review. #8041 reports Linux tests passing; my local focused test attempt on this host stopped at the project CPU-feature guard before assertions.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321036409 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6891#issuecomment-6321036409). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T16:29:14Z. --- A check against the full open PR set found that #8041 already carries the same guard-size behavior and focused regression test. I closed my duplicate #6893 so there is one implementation for review. #8041 reports Linux tests passing; my local focused test attempt on this host stopped at the project CPU-feature guard before assertions.
MarcZX10 commented 2026-10-21 16:30:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T16:30:09Z.


Follow-up after refreshing checks: #8041’s conventions failure is from its October 19 run; running python3 tools/check_conventions.py --base upstream/main against the current fetched main passes. However, #8041 is now substantially behind main: subsequent pthread changes overlap its implementation and the old GuestPthreadGuardSize.cpp has since been removed from main during that work. The branch needs a maintainer rebase and a fresh focused test run before its status can be assessed. I have not pushed any changes to the contributor’s branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321057195 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6891#issuecomment-6321057195). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T16:30:09Z. --- Follow-up after refreshing checks: #8041’s conventions failure is from its October 19 run; running `python3 tools/check_conventions.py --base upstream/main` against the current fetched main passes. However, #8041 is now substantially behind main: subsequent pthread changes overlap its implementation and the old `GuestPthreadGuardSize.cpp` has since been removed from main during that work. The branch needs a maintainer rebase and a fresh focused test run before its status can be assessed. I have not pushed any changes to the contributor’s branch.
Sign in to join this conversation.
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#6891
No description provided.