fix(libkernel): measure the host stack reserve from the frame address #7115
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7115"
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 milescoviello on 2026-10-17T12:11:29Z.
What
In an
ANYPS5_ENABLE_ASANbuild,guest_onceaborts on its firstscePthreadCreate:HostStackReservemeasures how much of a fresh host thread's stack is already in use. Its probe thread did this by subtracting the address of a local variable from the stack top. With AddressSanitizer'sdetect_stack_use_after_return, which is on by default on Linux, an instrumented function's locals live on ASan's fake stack, a separate mapping. In this run the local was more than 170 MiB above the probe thread's stack top, so the subtraction wrapped to just under 2^64 andAllocateGuestStackwas asked for a stack of 18446744073526816768 bytes:The probe now measures from
__builtin_frame_address(0), which is always on the thread's real stack. The codebase already uses it inlibSceFiberand libc. In a normal build the two values differ only by the probe's own frame, a few dozen bytes inside the 1 MiBHOST_STACK_MARGINthatscePthreadCreateadds anyway.Whether a given ASan run hits this depends on where the fake stack lands. In five title runs on ASan libraries, the probe's fake frame happened to sit below the stack top, so thread creation worked there (see Tested).
I found this while running the CPU-side tests in an ASan build. No CI job does that today; #7062, #7096 and #7097 add one for the relinker tests only.
Tested
Ubuntu 24.04, GCC 13.3, NVIDIA RTX 4070 Ti. ASan build:
-DANYPS5_ENABLE_ASAN=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo,ASAN_OPTIONS=detect_leaks=0.maine09dbc2b42:guest_onceaborts as above. With this change, rebuilt on the same commit, it passes 3 of 3.ctest -E '^agc_driver_|^agc_vertex_barrier|videoout|^agc_shader', 843 tests): 15 fail onmainand 14 with this change. The only difference isguest_once. The other 14 are separate ASan-build problems that this PR doesn't touch:guest_thread_self,guest_pthread_stack,guest_kernel_is_stack,guest_kernel_stack_protectandguest_raise_exceptioncheck that the test's own local is on the thread's stack. In an instrumented test, that local is on the fake stack.patched_librariesandguest_memory_lifetimefail because libc's copy of the main image's writable pages (GuestAllocations.cpp:103) reads ASan's redzones around the instrumented executable's globals.fiberfails becauselibSceFiberswitches stacks without telling ASan.guest_module_fini,guest_module_args,guest_deferred_dependenciesandloadexec_argumentsfail because their spawned guests need libasan first in the library list.guest_allocator_failuresfails on ASan's own allocation limit.agc_multisample_guest_accesspassesaligned_alloca size that isn't a multiple of the alignment.guest_pthread*,guest_thread*,guest_once,guest_kernel_is_stack,guest_kernel_stack_protect,guest_raise_exception) pass 3 of 3, andctest -R '^guest_|libc'passes 403 of 403.halt_on_error=1,maine09dbc2b42without this change):tools/check_conventions.pypasses.tools/pr_overlap.pycould not compareThread.cppagainst #3986, #5021, #6222 and #6487, which conflict withmain. None of their diffs touchesMeasureStackReserveorHostStackReserve.Not checked: macOS, Windows (this code is not built there), Clang's ASan.
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 github-actions[bot] on 2026-10-17T12:42:48Z.
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.
core/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/Pthread/src/Thread.cpp