fix(libkernel): report thread guard size from attribute queries #6893
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6893"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T16:54:04Z.
What
Carries Hussein Kadhim's two commits from #6891, preserving his original commit authorship.
pthread_attr_get_npandscePthreadAttrGetnow copy the thread's guard-size attribute into the destination attribute.PthreadPrivaterecords the value used at creation, including the existing 0x1000 default. This reports the recorded attribute; it does not claim that the host stack applies that guard size.Adds
guest_pthread_guard_sizecoverage for the default and explicit sizes, for both query exports, while the thread is running and for apthread_attr_get_npround trip. A caller-supplied stack reports a zero guard size, matching FreeBSD libthr behavior.The current open #5918 changes the reported bounds of adopted host-thread stacks on Windows. It shares
Thread.cppand test registration, but does not implement either attribute query; the merge preview is clean.Tested
main66e290f1c:python3 tools/check_conventions.py --base upstream/main --head HEADandgit diff --check upstream/main...HEADpass.BUILD_TESTING=ON:guest_pthread_guard_size_testsbuilds successfully. CTest was attempted but the process aborts at the emulator's host CPU guard before assertions: this Xeon E5-1620 v2 lacks LZCNT, BMI1/2, MOVBE, AVX2 and FMA.guest_pthread_attrandguest_pthread_stackalso pass. The reporter's probe covers requested sizes 0, 1, 100, 4096 and 65536, default size, running and finished threads, and the round trip. I did not run these Windows tests.Checklist
main; no other open PR implements the same functionsImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T17:38:32Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/tests/GuestPthreadGuardSize.cppcore/libs/tests/GuestPthreadGuardSize.cppdocs/dev/debt/libkernel.mdL169-170Some 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.
core/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Pthread/include/Pthread.hppcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/include/Pthread.hppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/include/Pthread.hppcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/include/Pthread.hppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdImported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-16T19:58:57Z.
@MarcZX100 thanks for carrying this. One case differs from the FreeBSD 11.4 libthr behavior the new debt entry cites: a thread created with a caller-supplied stack (
scePthreadAttrSetstack/pthread_attr_setstack) reports the attribute's guard size. FreeBSD reports 0 for that thread.In FreeBSD 11.4,
_pthread_createcopies the attribute intonew_thread->attrand then callscreate_stack(&new_thread->attr). Whenstackaddr_attris set, that function zeroes the guard size (thr_create.c#L242-L244):pthread_attr_get_npcopiespthread->attrafter that change, so it returnsguardsize_attr == 0for that thread. The caller's own attribute object is not touched (only the thread's copy is), sopthread_attr_getguardsizeon it still returns the value that was set. On this PR's head,PthreadPrivaterecordsattr->_guardsizeunconditionally (core/libs/prx/libkernel/Pthread/src/Thread.cpp:320). I ran a small probe (I put it in place ofGuestPthreadGuardSize.cpplocally) that sets a 256 KiB stack withscePthreadAttrSetstack, creates a thread and queries it with both exports:Following FreeBSD,
pthread_attr_get_npshould report0x0here;scePthreadAttrGetreads the same recorded value in this PR. A minimal fix forThread.cpp:320:With that line the probe reports
0x0for both user-stack threads and still reports0x1000for a library-allocated stack.guest_pthread_guard_size,guest_pthread_attrandguest_pthread_stackstill pass (3 runs). A user-stack case inGuestPthreadGuardSize.cppwould cover this. I did not check what the console returns.Separately, the PR body says the test covers queries made "while the thread is running and after join".
GuestPthreadGuardSize.cpponly queries beforescePthreadJoin, and nothing calls the query exports after the join.guest_pthread_guard_sizePosix/Attr.cpp,src/Attr.cpporThread.cppline)406fd9ea9guest_pthread_guard_size/_attr/_stackpass 3/3 (git merge-treeis also clean against upstream/mainea4cc112a; not run there)Setup: Linux x86-64, lavapipe (Mesa 26.2.4) clean env,
ctest -V --no-tests=error -R '^guest_pthread_guard_size$', head 2b5a3c329204c41f63123bd072a6a464a016cc05.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T20:01:10Z.
Thanks for the FreeBSD source trace and the user-stack probe. I applied the reported rule:
PthreadPrivatenow records guard size 0 whenstackAddressis supplied, and the focused test creates a caller-stack thread with a nonzero requested guard and checks both query exports return 0 while it runs. I also removed the unsupported “after join” coverage claim from the PR body.git diff --checkand conventions pass. I could not locally build the test: the prior CMake directory points at a different worktree, and a fresh configure with system dependencies stops because SDL2 development files are absent. I have not claimed the probe passes locally; your reported focused Linux probe remains the runtime evidence.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-16T20:27:02Z.
@MarcZX100 since you couldn't build it locally: I built and ran the new head 2969fb45e on Linux x86-64.
guest_pthread_guard_size(now including your caller-stack check) passes, and it aborts when only the Thread.cpp line is reverted. CI's linux/windows jobs were skipped on this head.My user-stack probe, which queries from the creating thread rather than from inside the worker, also reports guard 0 from both
pthread_attr_get_npandscePthreadAttrGet, for the default guard and for 0x2000 requested (a library stack still reports 0x1000).Setup: the 3x rows ran
ctest -V --no-tests=error -R '^<test>$'per test in a cleanenv -iwith a freshXDG_CACHE_HOMEper run. The parallel rows and the probe ran theguest_pthread_guard_size_testsbinary directly, also underenv -i.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T20:38:44Z.
Thanks for the current-head run and the creating-thread probe. This confirms the caller-supplied stack case returns guard size 0 through both query exports, while the library-allocated stack retains 0x1000. The focused guard-size, attribute, and stack tests pass 3/3 each, and the Thread.cpp negative control fails the guard-size test as expected. I have recorded this runtime confirmation; the hosted Linux/Windows jobs remain skipped on this head.
Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-17T01:08:54Z.
Windows build check of the current head 2969fb45e (the hosted Windows jobs were skipped on it): the new test does not compile with MinGW.
Windows 11 10.0.26200.9711, WinLibs GCC 15.2.0 posix UCRT r7, Release, BUILD_TESTING=ON, PR merged onto main
f3263dfbf(no conflicts):CheckUserStackGuardSize()(added in 2969fb45e) allocates the caller stack withstd::aligned_allocand frees it withstd::free. The Windows CRT does not providealigned_alloc, soguest_pthread_guard_size_testsis not built and ctest reportsguest_pthread_guard_sizeas Not Run.Using C++17 aligned new/delete instead builds and passes:
With that change on the same merge: guest_pthread_guard_size, guest_pthread_attr and guest_pthread_stack pass 3/3 each (ctest --repeat until-fail:3). I did not run the Thread.cpp negative control on Windows, and did not check the change on Linux.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T01:12:06Z.
Thanks for catching the MinGW portability issue and for testing the aligned allocation replacement. I applied it in
ece11f830: the caller stack now uses C++17 alignedoperator new/operator deletewith<new>, avoidingstd::aligned_allocandstd::freeon Windows. The focusedguest_pthread_guard_size_teststarget builds on Linux with GCC 12.2. I attempted the CTest case too, but this host aborts at the emulator CPU-feature guard (missing LZCNT, BMI1/2, MOVBE, AVX2 and FMA), before the test assertions. Your Windows 3/3 result covers the exact allocation change.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-17T13:17:33Z.
Review: COMMENTED
Linux runtime run of the current head
ece11f830, since the author's host stops at the CPU-feature guard and the MinGW check did not cover Linux.Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON.Head
ece11f830merged ontomain20a84154f(clean merge).So the aligned
operator new/operator deletestack builds and runs on Linux, and the caller-stack case still catches theThread.cppchange.Setup:
ctest --no-tests=error -R '^<test>$' --repeat until-fail:3per test underenv -iwith a freshXDG_CACHE_HOME; the parallel row ranguest_pthread_guard_size_testsdirectly.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T13:18:05Z.
Thanks for running the current head on Linux. The three focused tests pass 3/3 each, and the reverted Thread.cpp control fails only the guard-size check as intended. That confirms the aligned stack allocation works on Linux as well as the earlier Windows result. I’ve recorded this validation on the PR.
AI-assisted: yes (Codex); report reviewed.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T13:50:27Z.
Thanks for the independent Linux run on the current head
ece11f830. The three focused tests passed 3/3 each, the revertedThread.cppcontrol failed at the expected guard-size assertion, and the 120 parallel runs had no failures. I have recorded this as an independent Linux runtime result; it complements the earlier Windows check.Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T16:13:26Z.
Windows run of the current head
ece11f830: with the alignedoperator newchange,GuestPthreadGuardSize.cppnow builds with MinGW, and the focused tests pass.Radeon 860M (RDNA 3.5), Ryzen AI 7 350, Windows 11 26300, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON.main6efb7261bwith #7432, #6912 and #6893 merged on top (no conflicts, disjoint files).guest_pthread_guard_size,guest_pthread_attr,guest_pthread_attr_stackandguest_pthread_stackpass, 3 runs of 3 each.AI-assisted
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T22:07:18Z.
Thanks for the Windows run on head
ece11f830. The MinGW build and 3/3 passes for the guard-size and related pthread tests confirm the aligned stack allocation works there. Together with the Linux focused runs and reverted-code control, this gives us coverage on both Windows and Linux.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T17:53:58Z.
Rebased PR #6893 onto current
mainat892a4f1328a14a123eca4af8f3c9ea5d176133aband pushed the updated headd2f913181e96bb29c9e76629d7a5a6afb2942608. The original implementation and documentation commits retain Hussein Kadhim as author; my follow-up commits remain under my account. I resolved the debt-file conflicts by retaining current main’s entries and preserving the guard-size note in the pthread section.git diff --check,check_conventions.py --base upstream/main, and GCC syntax-only forGuestPthreadGuardSize.cpppass. The cached CMake directory has no generated build files, so it could not provide a runtime test in this environment; existing focused Linux and Windows runtime results were on the prior headece11f830.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T16:29:08Z.
Closing this duplicate: #8041 already implements the same pthread_attr_get_np/scePthreadAttrGet guard-size behavior and adds the focused regression test. Its Linux test report is available in #8041; keeping one PR for maintainers to review.
Pull request closed