fix(libkernel): honor caller-provided pthread stacks #9938
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9938"
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 BorchBe99 on 2026-10-22T22:56:06Z.
What
scePthreadCreateignored a stack address set in the thread attributes. A thread created with one now runs on that memory, with the given size, on Linux and Windows. This finishes the caller-stack follow-up from #1988. The caller keeps ownership of the memory. Host startup still runs on the native stack, and TLS stays per host thread.Caller stacks report guard size 0. Thread identity and the stack's lifetime survive return, explicit exit, cancellation and key destructors. On Windows the TEB stack bounds cover the caller's range while the thread runs and are set back to the native bounds before it ends; existing context-stack registrations and pins stay. When
longjmp,siglongjmporsetcontextlands on a caller stack, the TEB gets that stack's range, also when a larger registered context stack contains it.A caller stack has no guard page below it; the libkernel debt file records this.
Merge order with the five open PRs that share behavior with this one:
Thread.cppstays as it is.retiredWinStacks. With #4563AwaitReapruns after the exit bridge has switched back to the native stack, because the joiner may free the caller's stack once join returns.If one of you needs another order, reply here and I will change
Depends on. #7192 (@Jev1337) and #7565 (@pushkarreddyy) can merge before or after this PR, and so can #14190 (@JJenkx) and #15165 (@ljkx), which share only lines indocs/dev/debt/libkernel.mdand.github/sanitizer-known-failures.txtwith it; this comment lists what the later one keeps. With #7565, its MXCSR setup moves into this PR's version ofAps5CallGuestEntry_nid_no_patch. #12688, #12700 and #12965 (all @Felipebmaitan) build on #12682 and touch this PR's files only through #12682's code. Since #9482 is merged, the new test's joined-thread case expectsESRCHfrompthread_attr_get_np. Since #9573 is merged, this PR also changes its testguest_pthread_create_suspended: two of its threads are created from attributes copied from a live thread, which carry that thread's stack address, so they now get a stack of the test's own (as announced). Since #12897 and #12963 are merged,RunGuestThreadcalls_setjmp, andscePthreadCreatekeeps thependingExceptionreset before this PR's caller-stack lines.Tested
The new regression test covers all three setter paths, small stacks with large TLS, detach and reuse, and mixed cleanup chains including libc
_Thrd_exit. Threads with and without a caller stack catch a host exception and then unwind with the standard unwinder across the entry trampoline to the host stack. On Windows it also checks the TEB range after auc_linkreturn onto a caller stack inside a larger registered context stack.Linux x86_64, GCC 15.3.0. This head merges
mainat5cb015ca(2026-11-03). On it the full default build,libs,public_headersand conventions pass, and so do the 9 pthread tests and the 16 neighbouring tests below, with the regression andguest_pthread_create_suspendedagain in 20 repeats each. The other Linux results are from this PR on0278ca98(mainon 2026-11-02), before its line in.github/sanitizer-known-failures.txt, which no build compiles and onlytools/sanitizer_report.pyand its testtools/tests/test_sanitizer_report.pyread (that test passes with the line):libsand conventions pass. The regression passes, also in 20 repeats, and so do 8 other pthread tests,guest_pthread_create_suspended(also in 20 repeats) and 16 neighbouring tests (libc threads, backtrace, mutex, cancel recovery, user context, memory, fixed mmap, andmain's newer tests of thread defaults, handle lifetime, detach attributes and raised exceptions). Under ASan, 6 of 7 pthread tests and a separate range and lifecycle probe pass. The regression aborts in one case: a thread on a caller stack returns throughuc_linkfrom amakecontextcontext on a larger stack that contains its own.setcontext_nid_postfixends in a[[noreturn]]call since #10838, and ASan, which still takes the caller stack for the current one, ignores the__asan_handle_no_returnthere and then reports astack-buffer-overflowon the stale redzones.makecontextandsetcontexthave no fiber annotations, so this PR lists the test in.github/sanitizer-known-failures.txt;tools/sanitizer_report.pythen counts that run as a known failure. The ASan build of this PR onb90d009dpasses with the same ASan runtime, andmain's ownuc_linktests pass under ASan with and without this PR.agc_driver_scattered_runsneeds a udmabuf limit below two chunks and fails the same way on a clean build of0278ca98.tools/check_skips.pyrejects 3 skips:agc_driver_sparse_dma_buf_importandagc_host_import_sparseskip because this host's udmabufsize_limit_mbis above 1024, andagc_driver_optimize_on_fiberskips in a default build, which leavesANYPS5_ENABLE_SPIRV_TOOLSoff.Windows 11 x64, local native run (not CI), this PR on
0278ca98, the tree of the Linux results above:guest_pthread_create_suspended, and 2 user-context tests pass. The WinLibs run also covers the full build,libs, conventions and 2 memory tests.vulkan_validation_gate_reports,main_sync_renames_test_feature_rename_with_detection_disabled,main_sync_renames_test_main_rename_with_detection_disabledandcheck_compile_test_windows_gcc_response_text_mode_is_not_reinterpretedfail the same way on a clean build of0278ca98.pe_import_firsttimed out once at 10.02 s against its 10 s limit and passes when run alone, with and without this PR.tools/check_skips.py --job windowsrejects 3 skips:guest_fopen_errors, which the clean build of0278ca98skips the same way, and 2 that come from this laptop:long_paths(long paths are disabled in the registry) andguest_video_out_high_frame_rate(the display has only 60 Hz modes).Broken versions of the change fail the regression. On Linux, with the trampoline's CFI damaged (CFA without the final
+16, or no CFA expression), it aborts at the unwind check. On Windows (WinLibs, treeebf12e33), ifGuestStackSwitch_nid_no_patchkeeps the current TEB range whenever it already contains the new stack pointer, it fails at theuc_linkcheck.Not covered: Windows was not run on the merge with
5cb015ca, which brings #14402's thread-name change intoStartNativeThread. The host-bounds check runs on the main test thread and does not see a worker's final TEB state. macOS takes the same stack switch as Linux but was not built or run. Project CI, game compatibility and performance are not tested.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-23T03:21:06Z.
Review: COMMENTED
Checked against main
3875f8c8ffand FreeBSD 11.4 libthr (thr_create.ccreate_stack/_pthread_create,thr_attr.c):setstackandsetstackaddrtreat the address as the lowest byte, andsetstackaddrkeeps the attr's current size. The existingEINVALchecks in bothAttr.cppfiles are unchanged and match.cpu_set_upcall(top&~0xf, minus 8). TheDW_CFA_def_cfa_expressionresolves to the native frame (*(rbp+16)+16), so unwinding back across it reads correctly.scePthreadJoinis safe. Join returns only afterWaitForSingleObject/pthread_joinon the host thread, so the thread has left the caller stack, as with FreeBSD'sthr_exittid wakeup.StartNativeThreadbranch is also the macOS path, so macOS now runs caller-stack threads too, while the description and the test registration (if(WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux")) cover only Linux and Windows. Could the description mention that macOS gets the same switch untested, or could the test be registered there once macOS links?__chkstkbehaviour myself. The new debt entry about host code, handlers and probes overrunning below the range reads accurately to me.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T05:36:44Z.
Review: CHANGES_REQUESTED
The caller-stack implementation and its lifetime/unwind coverage were already reviewed in my earlier comments; the author subsequently rebased to
b1f516491827f8e53af5f189d064926a9128f6e2and reports a passing Linux build, repeated focused tests, neighboring tests, ASan and conventions. Those runs are contributor-reported; hosted product build/test jobs remain skipped.This branch is no longer current with upstream:
tools/check_main_sync.py --base upstream/main --head refs/reviews/pr-9938reports 1 ahead /278 behind, withThread.cpp,libkernel/CMakeLists.txt, anddocs/dev/debt/libkernel.mdchanged on both sides since the merge base. The merge is clean, but #9960 has changedThread.cppsince this branch was based. Please refresh against currentmain, preserve the caller-stack lifecycle alongside that thread-startup work, and rerun the focused Linux/Windows stack and unwind tests plus conventions on the refreshed head.The existing overlap feedback on #8041, #7149, #4563 and #9684 also still needs an agreed integration order/dependency where the shared behavior requires it; a general “whichever is ready” statement does not identify dependencies among those specific changes. Please record the agreed order in the checklist. This request is for the repository's current-main and overlap rules, not queue volume.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T09:50:01Z.
Review: COMMENTED
Reviewed — no blocking findings
The change implements caller-provided pthread stacks across Linux and Windows, with a regression test covering stack execution, lifecycle, unwind, and Windows stack bounds; I found no verified blocking defect in the diff inspected.
What I ran
Read the prepared description, commits, manifest, diff, and changed-file text included in the review folder. The diff is for head
db41fa38767f3347af18c7308d517e7359c13950. I did not run commands or tests.Results
Findings
Blocking
None found in the inspected diff.
Non-blocking
None.
Not verified
I could not access the prepared
files/directory through the reader, so I reviewed the diff but did not read the complete head versions of files marked included in the manifest. I did not inspect all changed-file context or run tests. The PR description reports Linux and Windows results, but those were not independently verified. No files are listed as excluded in the manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T20:26:25Z.
Review: COMMENTED
The Windows startup path checks that the original host stack is fully committed and writable. The new caller-stack branch skips that check before publishing the thread and entering the supplied stack, while the attribute setter only checks the pointer and size. Could initialization validate the supplied range before reporting success, with the expected guest error pinned to available measurements?
Source review at
db41fa3876. The trial merge conflicts in the pthread and CMake files. Native Windows execution remains unverified here.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.