Skip to content

fix(libkernel): honor caller-provided pthread stacks - #9938

Open
BorchBe99 wants to merge 2 commits into
boykopovar:mainfrom
BorchBe99:pr/caller-provided-stacks-20261022
Open

BorchBe99 wants to merge 2 commits into
boykopovar:mainfrom
BorchBe99:pr/caller-provided-stacks-20261022

Conversation

@BorchBe99

@BorchBe99 BorchBe99 commented Oct 22, 2026 •

Copy link
Copy Markdown

What

scePthreadCreate ignored 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, siglongjmp or setcontext lands 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:

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 in docs/dev/debt/libkernel.md and .github/sanitizer-known-failures.txt with it; this comment lists what the later one keeps. With #7565, its MXCSR setup moves into this PR's version of Aps5CallGuestEntry_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 expects ESRCH from pthread_attr_get_np. Since #9573 is merged, this PR also changes its test guest_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, RunGuestThread calls _setjmp, and scePthreadCreate keeps the pendingException reset 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 a uc_link return onto a caller stack inside a larger registered context stack.

Linux x86_64, GCC 15.3.0. This head merges main at 5cb015ca (2026-11-03). On it the full default build, libs, public_headers and conventions pass, and so do the 9 pthread tests and the 16 neighbouring tests below, with the regression and guest_pthread_create_suspended again in 20 repeats each. The other Linux results are from this PR on 0278ca98 (main on 2026-11-02), before its line in .github/sanitizer-known-failures.txt, which no build compiles and only tools/sanitizer_report.py and its test tools/tests/test_sanitizer_report.py read (that test passes with the line):

  • Full default build, libs and 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, and main'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 through uc_link from a makecontext context on a larger stack that contains its own. setcontext_nid_postfix ends in a [[noreturn]] call since feat(libkernel_sys): finish sched_setscheduler and sigreturn #10838, and ASan, which still takes the caller stack for the current one, ignores the __asan_handle_no_return there and then reports a stack-buffer-overflow on the stale redzones. makecontext and setcontext have no fiber annotations, so this PR lists the test in .github/sanitizer-known-failures.txt; tools/sanitizer_report.py then counts that run as a known failure. The ASan build of this PR on b90d009d passes with the same ASan runtime, and main's own uc_link tests pass under ASan with and without this PR.
  • Full suite: of 3,492 registered tests, 3,448 pass, 43 are skipped and 1 fails (CTest exit 8): agc_driver_scattered_runs needs a udmabuf limit below two chunks and fails the same way on a clean build of 0278ca98. tools/check_skips.py rejects 3 skips: agc_driver_sparse_dma_buf_import and agc_host_import_sparse skip because this host's udmabuf size_limit_mb is above 1024, and agc_driver_optimize_on_fiber skips in a default build, which leaves ANYPS5_ENABLE_SPIRV_TOOLS off.

Windows 11 x64, local native run (not CI), this PR on 0278ca98, the tree of the Linux results above:

  • WinLibs GCC 15.2.0 (DWARF exceptions) and MSYS2 UCRT64 GCC 16.2.0 (SEH exceptions): in both, the regression (also in 20 repeats), 20 other pthread, libc-thread and timed-join tests including guest_pthread_create_suspended, and 2 user-context tests pass. The WinLibs run also covers the full build, libs, conventions and 2 memory tests.
  • Full suite (WinLibs): of 3,622 registered tests, 2,788 pass, 829 are skipped and 5 fail (CTest exit 8). vulkan_validation_gate_reports, main_sync_renames_test_feature_rename_with_detection_disabled, main_sync_renames_test_main_rename_with_detection_disabled and check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted fail the same way on a clean build of 0278ca98. pe_import_first timed 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 windows rejects 3 skips: guest_fopen_errors, which the clean build of 0278ca98 skips the same way, and 2 that come from this laptop: long_paths (long paths are disabled in the registry) and guest_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, tree ebf12e33), if GuestStackSwitch_nid_no_patch keeps the current TEB range whenever it already contains the new stack pointer, it fails at the uc_link check.

Not covered: Windows was not run on the merge with 5cb015ca, which brings #14402's thread-name change into StartNativeThread. 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

🤖 Generated with Claude Code

@MarcZX10

Copy link
Copy Markdown
Contributor

I reviewed the caller-stack lifecycle across attribute capture, Linux/Windows thread startup, guest entry trampoline, return/exit/cancellation cleanup, stack-bound tracking, and the new lifecycle/unwind tests. The implementation consistently keeps host startup/TLS on the native thread, switches only for the guest entry, and restores native bounds before teardown; the test matrix described covers the main return, detach/reuse, cleanup and unwind cases. I ran git diff --check upstream/main...refs/review/9938 and python3 -I tools/check_conventions.py --base upstream/main --head refs/review/9938; both pass. Hosted conventions, intake and labels pass, but all build/test jobs are skipped by policy for this PR, so the extensive Linux/Windows runs in the description are contributor-reported, not hosted CI results. I could not run local C++ builds because the root filesystem is full.

Current main is 262 commits ahead of the merge base and has changed the shared libkernel test CMake file and debt entry; the pthread implementation paths themselves are unchanged. Please refresh those shared files before merge and preserve current registrations/debt. The listed adjacent PRs warrant the stated merge coordination. I found no independent blocker in this pass.

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Some 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.

Pull request Shared files not compared
PR 6149 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 8605 docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 10558 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 12130 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12544 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12700 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12965 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 14103 docs/dev/debt/libkernel.md
PR 14108 docs/dev/debt/libkernel.md
PR 15040 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 16372 core/libs/prx/libkernel/CMakeLists.txt
PR 16790 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 16826 docs/dev/debt/libkernel.md
PR 17439 core/libs/prx/libkernel/CMakeLists.txt
PR 17463 core/libs/prx/libkernel/CMakeLists.txt
PR 17484 core/libs/prx/libkernel/CMakeLists.txt
PR 17513 core/libs/prx/libkernel/CMakeLists.txt
PR 18112 core/libs/prx/libkernel/CMakeLists.txt
PR 18190 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/Posix/Attr.cpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 18293 core/libs/prx/libkernel/CMakeLists.txt
PR 18324 docs/dev/debt/libkernel.md
PR 18462 docs/dev/debt/libkernel.md
PR 19181 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 21026 core/libs/prx/libkernel/CMakeLists.txt
PR 21058 core/libs/prx/libkernel/CMakeLists.txt
PR 21132 core/libs/prx/libkernel/CMakeLists.txt
PR 21135 core/libs/prx/libkernel/CMakeLists.txt
PR 21464 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 21571 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 21625 core/libs/prx/libkernel/CMakeLists.txt
PR 21626 core/libs/prx/libkernel/CMakeLists.txt
PR 21655 core/libs/prx/libkernel/CMakeLists.txt
PR 21663 core/libs/prx/libkernel/CMakeLists.txt
PR 22391 core/libs/prx/libkernel/CMakeLists.txt
PR 22479 core/libs/prx/libkernel/CMakeLists.txt
PR 23953 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 25949 core/libs/prx/libkernel/CMakeLists.txt
PR 26174 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 26206 core/libs/prx/libkernel/CMakeLists.txt
PR 27563 core/libs/prx/libkernel/CMakeLists.txt
PR 27619 core/libs/prx/libc/src/GuestStacks.cpp
PR 27813 core/libs/prx/libkernel/CMakeLists.txt
PR 27864 core/libs/prx/libc/include/GuestStacks.hpp
core/libs/prx/libc/src/GuestStacks.cpp
core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28043 core/libs/prx/libkernel/CMakeLists.txt
PR 28045 core/libs/prx/libkernel/CMakeLists.txt
PR 28051 core/libs/prx/libkernel/CMakeLists.txt
PR 28253 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28255 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28299 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28323 core/libs/prx/libkernel/CMakeLists.txt
PR 28434 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28702 core/libs/prx/libkernel/CMakeLists.txt
PR 28770 core/libs/prx/libkernel/CMakeLists.txt
PR 28801 core/libs/prx/libkernel/CMakeLists.txt
PR 28808 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28960 core/libs/prx/libkernel/CMakeLists.txt
PR 28962 core/libs/prx/libkernel/CMakeLists.txt
PR 28964 core/libs/prx/libkernel/CMakeLists.txt
PR 29246 core/libs/prx/libkernel/Pthread/src/Thread.cpp

@MarcZX10

Copy link
Copy Markdown
Contributor

I reviewed head 65f0cc18fb41d6f3366f95f510df09a0680e62dc; current-main git diff --check and conventions pass. The report gives substantial Linux and Windows local coverage for supplied-stack entry, cleanup/exit, unwind, TEB bounds and ASan switching; project CI and title compatibility are explicitly not claimed. I could not rerun the C++ tests here because the root filesystem has no free space.

The live overlap scan identifies comparable shared hunks with #8041 (guard-size queries/attributes), #8477 and #9160 (thread startup), and #9684 (Linux stack placement); its comparisons against #4563 and #7149 are incomplete because those branches conflict with main. #7149 is particularly important to reconcile manually because both change GuestStacks and Windows pthread stack placement. #9573 also touches the pthread attribute/thread files, while #9482 shares the debt entry. The PR currently suggests merging in any order; please identify which behaviors are independent and preserve/retest them together, and add Depends on where one change is actually required first. I did not find evidence that these are duplicate implementations, and I am not treating file overlap alone as a blocker.

@BorchBe99
BorchBe99 force-pushed the pr/caller-provided-stacks-20261022 branch from 65f0cc1 to b1f5164 Compare October 23, 2026 01:50
@BorchBe99

Copy link
Copy Markdown
Author

Rebased onto 78becbb3; the new head is b1f51649. The patch is unchanged (same git patch-id). The test CMake file and debt entry merged without conflicts, so current registrations and debt entries are kept, and the branch also merges cleanly with main as of b1496678.

Linux on the rebased tree: the full build, the regression (also in 20 repeats), 9 neighbouring tests, ASan and conventions pass. Full suite: 1,826 of 1,848 pass, 19 skip, and the same 3 agc_driver_sample_order* tests fail as on the old base. tools/check_skips.py rejects 2 of the skips: two sparse-import AGC tests, new on main, that skip here because of this machine's udmabuf size limit. I did not rerun Windows because the patch is unchanged.

None of the PRs below needs another one merged first, so Depends on stays none. Neither side calls or relies on code the other adds; the order only decides who resolves the conflict. The description asked their authors about the order and none has asked for a different one so far; if one does, I'll add it to Depends on. Whoever merges second keeps both sides of each conflicting test registration, debt line and declaration, and also:

PR Shared code Keep when merging second Retest together
#8041 guard size in Pthread.hpp and both Attr.cpp guard 0 for caller stacks: _guardsize = thread->callerStack ? 0 : thread->guardSize guest_pthread_guard_size, guest_pthread_provided_stack, guest_pthread_attr, guest_pthread_attr_stack
#7149 GuestStacks, Windows scePthreadCreate, StartNativeThread and scePthreadExit caller-stack threads skip AllocateGuestStack, #7149's stack check and its mapping registration, keep their own address and run through our GuestStackEnter/GuestStackLeave path; our exit longjmp runs before #7149's exit block on Windows: guest_pthread_provided_stack, guest_pthread_stack, guest_user_context, guest_user_context_exit, guest_pthread_frame_chain, guest_kernel_is_stack, guest_pthread_attr_stack, guest_pthread_cancel; on Linux: guest_pthread_provided_stack, guest_pthread_stack
#4563 scePthreadExit, test CMakeLists.txt both lines, ours first, so the thread leaves the caller stack before it waits on Windows: guest_pthread_finished_stack, guest_pthread_provided_stack, guest_pthread_join_detach, guest_pthread_cancel, guest_pthread_stack
#9684 Linux StartNativeThread, test CMakeLists.txt, debt entry its size check, alignment and frame move only for library stacks, inside if (!self->callerStack) on Linux with and without ASan: guest_thread_stack_guard, guest_pthread_provided_stack, guest_pthread_stack, guest_kernel_is_stack, guest_kernel_stack_protect, guest_pthread_frame_chain, guest_pthread_attr_stack, guest_thread_self
#9482 debt entry, pthread_attr_get_np it returns ESRCH for a joined thread, so the joined-thread case in our test (RequireReported(thread, stack, false)) has to expect that and skip its base and size check guest_pthread_attr, guest_pthread_provided_stack, guest_pthread_attr_stack
#7192, #8477, #9160, #9573 adjacent lines (debt entry, macOS block in RunThread, includes, scePthreadCreate) both sides guest_pthread_provided_stack, each PR's own tests, and guest_pthread_attr for #9160 and #9573

Two of them also meet this PR at run time without a conflict: #9160's new pthread_attr_setstackaddr is another way to create a caller-stack thread, and with #9573 a suspended caller-stack thread waits on its caller stack (on Windows with the caller bounds set and its pages pinned). No test of either PR covers these combinations.

I have read the PR heads but not built the combinations. #7149 with this PR needs a Windows run, mainly to see whether its 256 KiB native stack is enough for caller-stack threads (this PR reserves 1 MiB).

@harisibrar1 harisibrar1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked against main 3875f8c and FreeBSD 11.4 libthr (thr_create.c create_stack/_pthread_create, thr_attr.c):

  • The semantics match libthr. A stack given in the attributes gets guard size 0 and is never freed or reprotected by the library. Both setstack and setstackaddr treat the address as the lowest byte, and setstackaddr keeps the attr's current size. The existing EINVAL checks in both Attr.cpp files are unchanged and match.
  • The entry trampoline leaves the guest entry with rsp ≡ 8 mod 16, the same as FreeBSD's cpu_set_upcall (top &~0xf, minus 8). The DW_CFA_def_cfa_expression resolves to the native frame (*(rbp+16)+16), so unwinding back across it reads correctly.
  • Freeing the stack after scePthreadJoin is safe. Join returns only after WaitForSingleObject/pthread_join on the host thread, so the thread has left the caller stack, as with FreeBSD's thr_exit tid wakeup.
  • Small question: the non-Windows StartNativeThread branch 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?
  • I couldn't build or run this here, so I can't confirm the Windows TEB and __chkstk behaviour 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.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caller-stack implementation and its lifetime/unwind coverage were already reviewed in my earlier comments; the author subsequently rebased to b1f516491827f8e53af5f189d064926a9128f6e2 and 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-9938 reports 1 ahead /278 behind, with Thread.cpp, libkernel/CMakeLists.txt, and docs/dev/debt/libkernel.md changed on both sides since the merge base. The merge is clean, but #9960 has changed Thread.cpp since this branch was based. Please refresh against current main, 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.

@BorchBe99
BorchBe99 force-pushed the pr/caller-provided-stacks-20261022 branch from b1f5164 to dca8f5b Compare October 23, 2026 20:18
@BorchBe99

Copy link
Copy Markdown
Author

Thanks for checking it against libthr.

macOS: yes, the non-Windows branch of StartNativeThread is its path too, so a macOS thread with a caller stack switches to it the same way. I have no Mac to build or run it on, so I added macOS to "Not covered" in the description. The test stays registered for Linux and Windows; once #8658 (macOS host support) is in, it can be added for macOS after a run there.

Windows: the TEB bounds were checked in the native runs under Tested, with WinLibs and with MSYS2 UCRT64. __chkstk has no test of its own.

@BorchBe99

Copy link
Copy Markdown
Author

Refreshed onto 938812df (main on 2026-10-23); it also merges cleanly with main as of b4cafbe2. The rebase changed only context lines. #9960 turns stackLock into a reference that outlives static destruction; this PR does not touch the lock or the code that takes it, so the caller-stack lifecycle is unchanged.

On the new head, Linux passes the full build, libs, conventions, the regression (also in 20 repeats) with its unwind checks, the neighbouring stack and thread tests and the ASan run. Windows passes the regression (also in 20 repeats) and the neighbouring pthread and user-context tests with WinLibs (DWARF) and MSYS2 UCRT64 (SEH); the WinLibs run also passes the full build, libs and conventions. The numbers are in the description.

Order: the description now proposes #8041 and #9684 first, then this PR, then #7149 and #4563, with what each step needs, and the checklist says Depends on: #8041, #9684. @plokmklo, @NevesHenry, @Pouare514, @poonasor: if you need another order, reply here and I will change it.

@valenciana-nc

Copy link
Copy Markdown
Contributor

@BorchBe99 Thanks for spelling out the #9482 interaction in your integration table. I agree with retaining its ended-thread ESRCH behavior and updating the supplied-stack test's joined-thread query rather than requiring it to succeed or reading output attributes after failure.

For order, #9684's author has already agreed to #9482 first (#9684 (comment)); keeping that order also fits this PR's existing dependency on #9684. Your comment permits either order, so I am recording the #9482-first preference on my PR, without adding a runtime dependency or importing your stack implementation. If you need to go first, let me know so the later update preserves the guard and adjusts that combined test.

I checked the current overlap and that #9482 still merges with main, but have not built the combined supplied-stack branch; no new runtime validation of your changes is claimed. AI-assisted coordination.

@BorchBe99

Copy link
Copy Markdown
Author

@zaryabzip, one combination with #9573 that my earlier comment missed. ReportsCreateSuspendedInAttrGet creates threads from what pthread_attr_get_np and scePthreadAttrGet return for another thread. On main that includes the other thread's stack address, and with this PR a new thread runs on the stack address in its attributes. libthr does the same: pthread_attr_get_np copies stackaddr_attr, and create_stack then uses it as the caller's stack. So with both PRs the copies run on the stack of the thread they were copied from.

On Linux, with both PRs merged onto main at b607bfdc, guest_pthread_create_suspended crashed in 3 of 3 runs (once a stack-smashing abort, twice a segfault); with #9573 alone it passed 5 of 5. With danielradosa/AnyPS5@165523e0, which gives the copies their own stacks with pthread_attr_setstack before pthread_create, it passed 5 of 5. Whichever PR merges second needs that change in the test; if that is this one, I will add it.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 25, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@NevesHenry

Copy link
Copy Markdown
Contributor

@BorchBe99 the order in your description is fine with me: #8041 and #9684 first, then this PR. #9684 doesn't depend on this one.

One change for your rebase: #9684 now sizes the alloca in the Linux StartNativeThread from __builtin_frame_address(0) instead of &stackEnd (9c1557e). With ASan's use-after-return detection on, &stackEnd could point into ASan's fake stack, and guest threads crashed. The alignment and that frame move still apply only to library-allocated stacks, as your table says. #9684 is rebased onto main 504ea3b and keeps #9482's debt entry next to its own.

AI-assisted (Claude Code).

@BorchBe99
BorchBe99 force-pushed the pr/caller-provided-stacks-20261022 branch from dca8f5b to 36a6b5c Compare October 26, 2026 04:21
@BorchBe99

Copy link
Copy Markdown
Author

Rebased onto b90d009d (main on 2026-10-25). Apart from context lines, three things changed since the last push:

The branch merges cleanly with main as of 14eb2cda. On Linux and on Windows (WinLibs and MSYS2 UCRT64) the regression passes, also in 20 repeats, and so do guest_pthread_create_suspended and the neighbouring tests. The description has the numbers and the full-suite failures, which a clean b90d009d shares.

@NevesHenry thanks. When I rebase onto #9684, its page alignment and the frame-address alloca stay on library-allocated stacks.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@adriadam10 adriadam10 self-assigned this Oct 26, 2026
@pushkarreddyy

Copy link
Copy Markdown
Contributor

@BorchBe99 Thanks for the coordination! That merge order works great for #7565, and moving the MXCSR setup into Aps5CallGuestEntry_nid_no_patch sounds good.

@Felipebmaitan

Copy link
Copy Markdown
Contributor

@BorchBe99 I am coordinating #11872 (POSIX read/receive cancellation, head bcf5df1) with this PR under CONTRIBUTING.md.

Proposed order: #11872 first, then #9938. Both branches touch the guest-entry area of Thread.cpp. #11872 uses opaque saved-context identities for deferred I/O delivery and retains its native thread/cancellation lifetime rules. With #11872 first, your later rebase should retain those hooks alongside your caller-stack switch, native-bound restoration and exit/unwind order. Combined checks should include your supplied-stack/unwind/cancel cases and guest_io_context/guest_io_signal cases. This proposal does not change your existing #8041/#9684 ordering; I have not run the combined branch.

Would you agree to that order and record #11872 in your Depends on line for the ordering? If your PR needs to go first, please say so; I can handle the later conflict resolution on #11872 instead. This is a proposal awaiting your confirmation, not an existing agreement.

AI-assisted coordination.

@maskym

maskym commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 36a6b5c6c7, merged into main 75f3702aed (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. Build clean.

The tree also carried the open PRs #10615 and #10416, all merged cleanly, so the run is a joint one; the tests of this PR are guest_pthread_create_suspended, guest_pthread_provided_stack. ctest --test-dir build -R "^(guest_cnd_at_thread_exit|guest_dynamic_cast|guest_pthread_create_suspended|guest_pthread_provided_stack)$": 4/4 passed, this PR's in 0.63 s and 0.10 s. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@BorchBe99 I reviewed db41fa3. The production diff since the last card is only the main merge and I found no code defect in it, but it cannot pass yet.

  1. Host calls (AGC submit, recompile, file I/O, Vulkan) now run on the caller's stack with no margin, so an overrun is silent. The 16 KiB guard page you offered is not in the branch. We need that guard page or another overrun detection.
  2. The Astro A/B asked for earlier is still missing, and there is no Windows result on this exact head.
  3. The PR conflicts with main, and fix(libkernel): report the creation guard size from the thread attribute queries #8041, fix(libkernel): start thread stacks on a 16 KiB page boundary on Linux #9684 and feat(libkernel): cancel blocked POSIX reads with owned operations #12682 need to land first. The feat(libkernel): cancel blocked POSIX reads with owned operations #12682 RetireIoStack change has to be composed with this one.

I saw you have stopped work on AnyPS5. If so, we will look at carrying it with the maintainer edits.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Check Result Note
Diff review passed Core implementation and test hunks inspected
Build and tests not run Results in the PR description are author-reported

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.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

closing this one as stale: it has been waiting on a hold for over a week with no new push. if you still want it, please reopen or rebase it onto current main and it goes back in the queue.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

I closed this by mistake, sorry. The next step here is ours, not yours, so it is reopened and back in our queue. You do not need to do anything.

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@THEMANnt THEMANnt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On db41fa3, the ASan exception applies to all of guest_pthread_provided_stack, but the documented false positive is specific to returning through uc_link. The same executable also checks explicit exit, detach, and stack reuse. Could we isolate the uc_link case in a separate CTest entry so those other cases stay covered by ASan?

Source review only. AI-assisted.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build ci .github docs libc libkernel needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.