From 68a7f8e4b95376092a0e5827dd7efeddd51fafc7 Mon Sep 17 00:00:00 2001 From: NevesHenry Date: Thu, 22 Oct 2026 04:58:05 -0300 Subject: [PATCH 1/5] fix(libkernel): start thread stacks on a 16 KiB page boundary on Linux scePthreadAttrGet, pthread_attr_get_np and sceKernelIsStack reported a Linux thread's stack from an address the host chose: a created thread's stack began wherever the host's start frames left it (0x200107f90), and the main thread's at the 4 KiB boundary glibc reports. On the console a stack starts on a page boundary. HotSpot aligns its stack guard zone down to the 16 KiB page before protecting it, so the protect started below the reported stack and failed, and the VM stopped with "memory to guard stack pages". A created thread's stack now starts at the 16 KiB boundary below the requested size and keeps that size; the thread enters it below its top. An adopted host thread's stack starts at the first 16 KiB boundary above the bottom of its host stack. Co-Authored-By: Claude Opus 5.5 (1M context) --- core/libs/prx/libkernel/CMakeLists.txt | 7 +++ .../libs/prx/libkernel/Pthread/src/Thread.cpp | 14 +++-- core/libs/tests/GuestThreadStackGuard.cpp | 59 +++++++++++++++++++ docs/dev/debt/libkernel.md | 2 +- 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 core/libs/tests/GuestThreadStackGuard.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 9d3d39d0e588..53f071b9ca5a 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -723,6 +723,13 @@ if(BUILD_TESTING) set_tests_properties(guest_pthread_stack PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + add_test_executable(guest_thread_stack_guard_tests ${LIBS_INCLUDE_DIR}/tests/GuestThreadStackGuard.cpp) + target_include_directories(guest_thread_stack_guard_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_thread_stack_guard_tests PRIVATE libkernel libc) + add_test(NAME guest_thread_stack_guard COMMAND guest_thread_stack_guard_tests) + set_tests_properties(guest_thread_stack_guard PROPERTIES TIMEOUT 20) + endif() add_test_executable(guest_cond_timedwait_tests ${LIBS_INCLUDE_DIR}/tests/GuestCondTimedwait.cpp) target_include_directories(guest_cond_timedwait_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_cond_timedwait_tests PRIVATE libkernel libc) diff --git a/core/libs/prx/libkernel/Pthread/src/Thread.cpp b/core/libs/prx/libkernel/Pthread/src/Thread.cpp index 71680fca259b..73f981870c9f 100644 --- a/core/libs/prx/libkernel/Pthread/src/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Thread.cpp @@ -29,6 +29,7 @@ #include #ifndef _WIN32 +#include #include #include #include @@ -329,8 +330,10 @@ static void SetStackFromHost(PthreadPrivate* thread) { std::uintptr_t low = 0; std::uintptr_t high = 0; if (!HostStackLimits(&low, &high)) throw std::runtime_error("Cannot query the host thread stack"); - thread->stackAddress = reinterpret_cast(low); - thread->stackSize = static_cast(high - low); + const auto start = (low + PS5_PAGE_SIZE - 1) & ~std::uintptr_t{PS5_PAGE_SIZE - 1}; + if (start >= high) throw std::runtime_error("The host thread stack holds no whole guest page"); + thread->stackAddress = reinterpret_cast(start); + thread->stackSize = static_cast(high - start); } static bool CurrentStack(std::uintptr_t* low, std::uintptr_t* high) { @@ -917,9 +920,9 @@ static void* StartNativeThread(void* opaque) { std::uintptr_t low = 0; std::uintptr_t high = 0; const auto reserve = HostStackReserve(); - if (!HostStackLimits(&low, &high) || high - low < reserve + self->stackSize) + if (!HostStackLimits(&low, &high) || high - low < reserve + self->stackSize + PS5_PAGE_SIZE - 1) throw std::runtime_error("Cannot query guest thread stack"); - self->stackAddress = reinterpret_cast(high - reserve - self->stackSize); + self->stackAddress = reinterpret_cast((high - reserve - self->stackSize) & ~std::uintptr_t{PS5_PAGE_SIZE - 1}); self->threadId = std::this_thread::get_id(); #if defined(__linux__) self->hostTid = static_cast(gettid()); @@ -941,6 +944,9 @@ static void* StartNativeThread(void* opaque) { ReleaseThread(self); } } guard{self}; + const auto stackEnd = reinterpret_cast(self->stackAddress) + self->stackSize; + if (const auto frame = reinterpret_cast(&stackEnd); frame > stackEnd) + static_cast(alloca(frame - stackEnd))[0] = 0; std::jmp_buf exitJump; if (setjmp(exitJump) == 0) { threadExitJump = &exitJump; diff --git a/core/libs/tests/GuestThreadStackGuard.cpp b/core/libs/tests/GuestThreadStackGuard.cpp new file mode 100644 index 000000000000..1dd7fcef76ca --- /dev/null +++ b/core/libs/tests/GuestThreadStackGuard.cpp @@ -0,0 +1,59 @@ +#include "SceTypes.hpp" +#include +#include +#include +#include + +extern "C" { +int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); +int APS5_VABI scePthreadJoin(Pthread thread, void** retval); +int APS5_VABI scePthreadAttrInit(PthreadAttr* attr); +int APS5_VABI scePthreadAttrDestroy(PthreadAttr* attr); +int APS5_VABI scePthreadAttrGet(Pthread thread, PthreadAttr* attr); +int APS5_VABI scePthreadAttrSetstacksize(PthreadAttr* attr, std::size_t stackSize); +int APS5_VABI scePthreadAttrGetstack(const PthreadAttr* attr, void** stackAddress, std::size_t* stackSize); +Pthread APS5_VABI scePthreadSelf(); +int APS5_VABI sceKernelIsStack(void* addr, void** start, void** end); +int APS5_VABI mprotect_nid_postfix(void* address, std::size_t length, int protection) noexcept; +} + +static constexpr int SCE_OK = 0; +static constexpr std::uintptr_t GUEST_PAGE = 0x4000; +static constexpr std::size_t GUARD_SIZE = 3 * GUEST_PAGE; +static constexpr std::size_t STACK_SIZES[] = {65536, 1u << 20, 16u << 20}; + +static void Require(bool value) { if (!value) std::abort(); } + +static void* APS5_VABI GuardStackBottom(void* requested) { + PthreadAttr attr = nullptr; + Require(scePthreadAttrInit(&attr) == SCE_OK); + Require(scePthreadAttrGet(scePthreadSelf(), &attr) == SCE_OK); + void* address = nullptr; + std::size_t size = 0; + Require(scePthreadAttrGetstack(&attr, &address, &size) == SCE_OK); + Require(scePthreadAttrDestroy(&attr) == SCE_OK); + const auto start = reinterpret_cast(address); + Require(start % GUEST_PAGE == 0); + if (requested != nullptr) Require(size == *static_cast(requested)); + int local = 0; + Require(reinterpret_cast(&local) > start + GUARD_SIZE && reinterpret_cast(&local) < start + size); + void* low = nullptr; + void* high = nullptr; + Require(sceKernelIsStack(&local, &low, &high) == SCE_OK); + Require(low == address && reinterpret_cast(high) == start + size); + for (const int protection : {3, 0, 3}) Require(mprotect_nid_postfix(address, GUARD_SIZE, protection) == 0); + return nullptr; +} + +int main() { + GuardStackBottom(nullptr); + for (auto stackSize : STACK_SIZES) { + PthreadAttr attr = nullptr; + Require(scePthreadAttrInit(&attr) == SCE_OK); + Require(scePthreadAttrSetstacksize(&attr, stackSize) == SCE_OK); + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, &attr, GuardStackBottom, &stackSize, nullptr) == SCE_OK); + Require(scePthreadAttrDestroy(&attr) == SCE_OK); + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + } +} diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index 1fcd143e48a3..3ad507df2234 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -177,7 +177,7 @@ - [getcontext and setcontext](../../../core/libs/prx/libkernel/System/src/UserContext.cpp) (libkernel) keep the 512-byte FXSAVE image in `mc_fpstate` (x87, MXCSR, XMM0-15), so the upper halves of YMM0-15 are neither saved nor restored, and `getcontext` leaves `mc_xfpustate` and `mc_xfpustate_len` 0. `getcontext` sets `mc_fpformat` and `mc_ownedfp` to FreeBSD's `_MC_FPFMT_XMM` and `_MC_FPOWNED_FPU`, `mc_cs` and `mc_ss` to the host selectors, and `mc_flags`, `mc_fsbase` and `mc_gsbase` to 0. `setcontext` loads `mc_fpstate` whatever `mc_fpformat` and `mc_ownedfp` say, and restores neither the segment selectors, `mc_fsbase` and `mc_gsbase` nor the extended state, which FreeBSD restores when `mc_flags` asks for them - [setcontext](../../../core/libs/prx/libkernel/System/src/UserContext.cpp) (libkernel) and [longjmp](../../../core/libs/prx/libc/src/JumpBuffer.cpp) (libc) on Windows set the thread's stack bounds in the TEB to the stack they switch to: a `makecontext` stack, or else the committed region around the target stack pointer. A switch to any other guest stack, such as a context built by hand, gets the bounds of that region only - [scePthreadAttrGet](../../../core/libs/prx/libkernel/Pthread/src/Attr.cpp) and [pthread_attr_get_np](../../../core/libs/prx/libkernel/Pthread/Posix/Attr.cpp) (libkernel) - the answer for a thread that has ended is not measured on the console; the implementation follows FreeBSD 11.4's [`_pthread_attr_get_np`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_attr.c#L139-L172) through `_thr_find_thread`. On Windows a host thread adopted by `scePthreadSelf` that ends without `scePthreadExit` keeps answering 0. Monster Hunter Wilds (PPSA24191) reuses a slot of its thread table only when `scePthreadAttrGet` returns `0x80020003` -- [scePthreadAttrGet, pthread_attr_get_np and sceKernelIsStack](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) report the stack of a host thread the library did not create, on Windows, from one allocation granule (64 KiB) above the bottom of its reservation when everything above that is committed read-write, as in a relinked executable's main thread and its default-size host threads (0x200000 committed of 0x210000 reserved), so the guest can protect the lowest reported pages, as HotSpot does for its stack guard pages in Java 25 LAN / DNS (PPSA99869). Any other such thread, and every such thread on Linux, gets its whole host stack: on Windows `mprotect` commits its reserved lower pages before protecting them and drops the guard flag of Windows' guard pages, and on Linux it cannot change the unmapped lower pages +- [scePthreadAttrGet, pthread_attr_get_np and sceKernelIsStack](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) report the stack of a host thread the library did not create, on Windows, from one allocation granule (64 KiB) above the bottom of its reservation when everything above that is committed read-write, as in a relinked executable's main thread and its default-size host threads (0x200000 committed of 0x210000 reserved), so the guest can protect the lowest reported pages, as HotSpot does for its stack guard pages in Java 25 LAN / DNS (PPSA99869). Any other such thread gets its whole host stack, and every such thread on Linux its host stack from the first 16 KiB boundary up: on Windows `mprotect` commits its reserved lower pages before protecting them and drops the guard flag of Windows' guard pages, and on Linux it cannot change the unmapped lower pages - [sceKernelInstallExceptionHandler](../../../core/libs/prx/libkernel/System/src/Exception.cpp) records the handler, but a host fault still ends the process: only signals raised with `sceKernelRaiseException` are delivered. Unity's PS5Util module installs a SIGUSR1 handler that reads `uc_mcontext.mc_rsp` (offset 0xf8 of the context) and raises SIGUSR1 on other threads to stop them for garbage collection - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Linux sends the host SIGUSR1, which the library takes over, to the target thread, so the handler runs wherever the thread was interrupted, emulator code included. On the console kernel objects are system calls and libthr defers signals while a thread holds its internal locks; here a handler that needs a host lock the interrupted code holds deadlocks, and a thread that the handler keeps waiting keeps its host locks. A thread holding the address-wait lock (`sceKernelSyncOnAddressWait`, `sceKernelSyncOnAddressWake`, `_umtx_op`) records the signal and runs the handler once it releases the lock or while it waits; other emulator locks are not deferred. Host waits resume after the handler. The host calls Linux does not restart end early: sleeps that do not retry return before their time, and the poll of `sceNetEpollWait` and socket calls with a send or receive timeout return `EINTR` to the guest - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Windows suspends the target and redirects it to the handler. A target running host code (any module other than the executable and the `*.guest.prx` modules, so libkernel, libc and the other HLE modules count too) is resumed and sampled again until it is back in guest code, since a handler that waits there would keep the host locks that code holds. The raising thread spins for 20 µs before each new host-code sample: suspended again at once, the target could stay at the same instruction for every sample until the 50 ms below ran out (on a 16-core Ryzen 9 5950X, `guest_raise_in_host_code` then raised into `fwrite` and deadlocked on its stream lock), and the 20 µs is a measured choice, not a console value; a target blocked in an ntdll wait is redirected in place, as are NtContinue and winpthreads after their retry limit. A target that stays in host code without waiting for 50 ms after the raise began, such as a long HLE or driver call, is then redirected in host code anyway, with the same hazard. A target seen right after a `syscall` instruction in ntdll, inside a system call that is not a wait such as the `NtWriteFile` of an `fwrite` holding its stream lock, restarts those 50 ms, so a raise can cost the raising thread up to the 1 s retry limit. While the target is suspended only `VirtualQuery` and a lock-free cache of image bases are consulted; an image not yet in the cache is classified with `GetModuleFileNameW` after the target is resumed. An APC queued at the first host-code sample, which an alertable guest wait would run sooner, is not implemented From 6586e2504800df98b4727219c11fc86b85704cd1 Mon Sep 17 00:00:00 2001 From: NevesHenry Date: Thu, 22 Oct 2026 14:03:49 -0300 Subject: [PATCH 2/5] test(libkernel): run guest_thread_stack_guard without ASan's fake stacks With ASan's use-after-return detection on, the test's local variable can live on ASan's fake stack in the heap instead of the native main-thread stack, so the check that it lies inside the reported stack aborts the test in a -DANYPS5_ENABLE_ASAN=ON build. Turn it off for this test, as guest_kernel_is_stack, guest_kernel_stack_protect, guest_thread_self and guest_pthread_stack already do. Co-Authored-By: Claude Opus 5.5 (1M context) --- core/libs/prx/libkernel/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 53f071b9ca5a..38d727aee708 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -729,6 +729,7 @@ if(BUILD_TESTING) target_link_libraries(guest_thread_stack_guard_tests PRIVATE libkernel libc) add_test(NAME guest_thread_stack_guard COMMAND guest_thread_stack_guard_tests) set_tests_properties(guest_thread_stack_guard PROPERTIES TIMEOUT 20) + add_asan_test_options(guest_thread_stack_guard detect_stack_use_after_return=0) endif() add_test_executable(guest_cond_timedwait_tests ${LIBS_INCLUDE_DIR}/tests/GuestCondTimedwait.cpp) target_include_directories(guest_cond_timedwait_tests PRIVATE ${LIBS_INCLUDE_DIR}) From 9c1557eaf922387be1656afa1812d8ca66080e85 Mon Sep 17 00:00:00 2001 From: NevesHenry Date: Sun, 25 Oct 2026 23:11:01 -0300 Subject: [PATCH 3/5] fix(libkernel): size the thread start alloca from the frame address StartNativeThread sized the alloca that moves a created thread below the top of its reported stack from the address of a local. With ASan's use-after-return detection on, that local can live on ASan's fake stack in the heap, so the distance was not one on the thread stack and guest threads crashed in a -DANYPS5_ENABLE_ASAN=ON build with default options. Take the frame from __builtin_frame_address(0), as MeasureStackReserve does. Co-Authored-By: Claude Opus 5.5 (1M context) --- core/libs/prx/libkernel/Pthread/src/Thread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/libs/prx/libkernel/Pthread/src/Thread.cpp b/core/libs/prx/libkernel/Pthread/src/Thread.cpp index 73f981870c9f..17b78c8c8d01 100644 --- a/core/libs/prx/libkernel/Pthread/src/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Thread.cpp @@ -945,7 +945,7 @@ static void* StartNativeThread(void* opaque) { } } guard{self}; const auto stackEnd = reinterpret_cast(self->stackAddress) + self->stackSize; - if (const auto frame = reinterpret_cast(&stackEnd); frame > stackEnd) + if (const auto frame = reinterpret_cast(__builtin_frame_address(0)); frame > stackEnd) static_cast(alloca(frame - stackEnd))[0] = 0; std::jmp_buf exitJump; if (setjmp(exitJump) == 0) { From 229567502aec8d18c87cc8337396612e719060b0 Mon Sep 17 00:00:00 2001 From: NevesHenry Date: Sun, 25 Oct 2026 23:11:01 -0300 Subject: [PATCH 4/5] docs(libkernel): drop the stale Linux clause from the host stack entry Since b5909a5ee DoMprotect maps a host stack's unmapped pages before protecting them on Linux, so the entry no longer says it cannot change them. Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/dev/debt/libkernel.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index 3ad507df2234..a79e590a95a6 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -177,7 +177,7 @@ - [getcontext and setcontext](../../../core/libs/prx/libkernel/System/src/UserContext.cpp) (libkernel) keep the 512-byte FXSAVE image in `mc_fpstate` (x87, MXCSR, XMM0-15), so the upper halves of YMM0-15 are neither saved nor restored, and `getcontext` leaves `mc_xfpustate` and `mc_xfpustate_len` 0. `getcontext` sets `mc_fpformat` and `mc_ownedfp` to FreeBSD's `_MC_FPFMT_XMM` and `_MC_FPOWNED_FPU`, `mc_cs` and `mc_ss` to the host selectors, and `mc_flags`, `mc_fsbase` and `mc_gsbase` to 0. `setcontext` loads `mc_fpstate` whatever `mc_fpformat` and `mc_ownedfp` say, and restores neither the segment selectors, `mc_fsbase` and `mc_gsbase` nor the extended state, which FreeBSD restores when `mc_flags` asks for them - [setcontext](../../../core/libs/prx/libkernel/System/src/UserContext.cpp) (libkernel) and [longjmp](../../../core/libs/prx/libc/src/JumpBuffer.cpp) (libc) on Windows set the thread's stack bounds in the TEB to the stack they switch to: a `makecontext` stack, or else the committed region around the target stack pointer. A switch to any other guest stack, such as a context built by hand, gets the bounds of that region only - [scePthreadAttrGet](../../../core/libs/prx/libkernel/Pthread/src/Attr.cpp) and [pthread_attr_get_np](../../../core/libs/prx/libkernel/Pthread/Posix/Attr.cpp) (libkernel) - the answer for a thread that has ended is not measured on the console; the implementation follows FreeBSD 11.4's [`_pthread_attr_get_np`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_attr.c#L139-L172) through `_thr_find_thread`. On Windows a host thread adopted by `scePthreadSelf` that ends without `scePthreadExit` keeps answering 0. Monster Hunter Wilds (PPSA24191) reuses a slot of its thread table only when `scePthreadAttrGet` returns `0x80020003` -- [scePthreadAttrGet, pthread_attr_get_np and sceKernelIsStack](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) report the stack of a host thread the library did not create, on Windows, from one allocation granule (64 KiB) above the bottom of its reservation when everything above that is committed read-write, as in a relinked executable's main thread and its default-size host threads (0x200000 committed of 0x210000 reserved), so the guest can protect the lowest reported pages, as HotSpot does for its stack guard pages in Java 25 LAN / DNS (PPSA99869). Any other such thread gets its whole host stack, and every such thread on Linux its host stack from the first 16 KiB boundary up: on Windows `mprotect` commits its reserved lower pages before protecting them and drops the guard flag of Windows' guard pages, and on Linux it cannot change the unmapped lower pages +- [scePthreadAttrGet, pthread_attr_get_np and sceKernelIsStack](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) report the stack of a host thread the library did not create, on Windows, from one allocation granule (64 KiB) above the bottom of its reservation when everything above that is committed read-write, as in a relinked executable's main thread and its default-size host threads (0x200000 committed of 0x210000 reserved), so the guest can protect the lowest reported pages, as HotSpot does for its stack guard pages in Java 25 LAN / DNS (PPSA99869). Any other such thread gets its whole host stack, and every such thread on Linux its host stack from the first 16 KiB boundary up: on Windows `mprotect` commits its reserved lower pages before protecting them and drops the guard flag of Windows' guard pages - [sceKernelInstallExceptionHandler](../../../core/libs/prx/libkernel/System/src/Exception.cpp) records the handler, but a host fault still ends the process: only signals raised with `sceKernelRaiseException` are delivered. Unity's PS5Util module installs a SIGUSR1 handler that reads `uc_mcontext.mc_rsp` (offset 0xf8 of the context) and raises SIGUSR1 on other threads to stop them for garbage collection - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Linux sends the host SIGUSR1, which the library takes over, to the target thread, so the handler runs wherever the thread was interrupted, emulator code included. On the console kernel objects are system calls and libthr defers signals while a thread holds its internal locks; here a handler that needs a host lock the interrupted code holds deadlocks, and a thread that the handler keeps waiting keeps its host locks. A thread holding the address-wait lock (`sceKernelSyncOnAddressWait`, `sceKernelSyncOnAddressWake`, `_umtx_op`) records the signal and runs the handler once it releases the lock or while it waits; other emulator locks are not deferred. Host waits resume after the handler. The host calls Linux does not restart end early: sleeps that do not retry return before their time, and the poll of `sceNetEpollWait` and socket calls with a send or receive timeout return `EINTR` to the guest - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Windows suspends the target and redirects it to the handler. A target running host code (any module other than the executable and the `*.guest.prx` modules, so libkernel, libc and the other HLE modules count too) is resumed and sampled again until it is back in guest code, since a handler that waits there would keep the host locks that code holds. The raising thread spins for 20 µs before each new host-code sample: suspended again at once, the target could stay at the same instruction for every sample until the 50 ms below ran out (on a 16-core Ryzen 9 5950X, `guest_raise_in_host_code` then raised into `fwrite` and deadlocked on its stream lock), and the 20 µs is a measured choice, not a console value; a target blocked in an ntdll wait is redirected in place, as are NtContinue and winpthreads after their retry limit. A target that stays in host code without waiting for 50 ms after the raise began, such as a long HLE or driver call, is then redirected in host code anyway, with the same hazard. A target seen right after a `syscall` instruction in ntdll, inside a system call that is not a wait such as the `NtWriteFile` of an `fwrite` holding its stream lock, restarts those 50 ms, so a raise can cost the raising thread up to the 1 s retry limit. While the target is suspended only `VirtualQuery` and a lock-free cache of image bases are consulted; an image not yet in the cache is classified with `GetModuleFileNameW` after the target is resumed. An APC queued at the first host-code sample, which an alertable guest wait would run sooner, is not implemented From 2d1d41779e22266275f103085a77b6a055e1c1a9 Mon Sep 17 00:00:00 2001 From: NevesHenry Date: Tue, 27 Oct 2026 14:08:03 -0300 Subject: [PATCH 5/5] test(libkernel): take the stack guard test's declarations from Api.hpp The test declared ten libkernel exports itself, which the public header audit from #10706 rejects: each export must be declared once, in its library's public header. Include prx/libkernel/include/Api.hpp instead. Co-Authored-By: Claude Opus 5.5 --- core/libs/tests/GuestThreadStackGuard.cpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/core/libs/tests/GuestThreadStackGuard.cpp b/core/libs/tests/GuestThreadStackGuard.cpp index 1dd7fcef76ca..28fb679bfb9d 100644 --- a/core/libs/tests/GuestThreadStackGuard.cpp +++ b/core/libs/tests/GuestThreadStackGuard.cpp @@ -1,22 +1,9 @@ -#include "SceTypes.hpp" +#include "prx/libkernel/include/Api.hpp" #include #include #include #include -extern "C" { -int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); -int APS5_VABI scePthreadJoin(Pthread thread, void** retval); -int APS5_VABI scePthreadAttrInit(PthreadAttr* attr); -int APS5_VABI scePthreadAttrDestroy(PthreadAttr* attr); -int APS5_VABI scePthreadAttrGet(Pthread thread, PthreadAttr* attr); -int APS5_VABI scePthreadAttrSetstacksize(PthreadAttr* attr, std::size_t stackSize); -int APS5_VABI scePthreadAttrGetstack(const PthreadAttr* attr, void** stackAddress, std::size_t* stackSize); -Pthread APS5_VABI scePthreadSelf(); -int APS5_VABI sceKernelIsStack(void* addr, void** start, void** end); -int APS5_VABI mprotect_nid_postfix(void* address, std::size_t length, int protection) noexcept; -} - static constexpr int SCE_OK = 0; static constexpr std::uintptr_t GUEST_PAGE = 0x4000; static constexpr std::size_t GUARD_SIZE = 3 * GUEST_PAGE;