fix(libkernel): keep host stack room below guest thread stacks #2647

Merged
oneandonlydean merged 1 commit from upstream-pr/2647 into main 2026-10-10 10:59:24 +00:00
oneandonlydean commented 2026-10-10 09:57:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T09:57:46Z.


What

Fixes #2603.

Since #1988, a guest thread's stack is exactly the size the title asked for. Host code that the thread calls runs on that stack too. sceAgcCreateShader reaches Driver::RegisterShader, whose frame is about 50 KiB, so threads that ask for 48 KiB overflow. The same overflow crashes Astro Bot (PPSA21564) at start-up on main, in shader building under sceAgcCreatePrimState.

  • Each guest thread now gets a fixed 1 MiB of host stack below the range it asked for. The title still sees the requested size and address range (scePthreadAttrGet, sceKernelIsStack). Only host calls use the extra room.
  • Why 1 MiB: the prepared-shader driver tests need between 128 and 192 KiB of main-thread stack (ulimit -s 192 passes, 128 crashes), Vulkan setup included.
  • Windows: the committed size was the requested size. The room below it came from the executable's default stack reserve, so requests at or above that reserve had none. The commit now includes the same margin. macOS does not run guest threads.
  • I chose this over switching to a separate host stack in HLE entry points. That would need a trampoline on every export that can go deep, plus handling for stack-passed arguments and unwinding across the switch.

Tested

Linux (CachyOS, GCC, Release).

  • Astro Bot (PPSA21564): main crashes at about 40–49 s (2 of 2 runs). main with this change gets past that point to the intro.
  • guest_pthread_stack now also calls host code that uses 256 KiB of stack below each thread's guest range (16 KiB, 64 KiB, 1 MiB and 16 MiB requests). It crashes with SIGSEGV without the fix and passes with it, 20 of 20 runs.
  • The pthread, thread, stack, cond, semaphore, cancel, guest memory and register context tests pass (31 tests).
  • Windows: not built.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4810354209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2647). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T09:57:46Z. --- ### What Fixes #2603. Since #1988, a guest thread's stack is exactly the size the title asked for. Host code that the thread calls runs on that stack too. `sceAgcCreateShader` reaches `Driver::RegisterShader`, whose frame is about 50 KiB, so threads that ask for 48 KiB overflow. The same overflow crashes Astro Bot (PPSA21564) at start-up on `main`, in shader building under `sceAgcCreatePrimState`. - Each guest thread now gets a fixed 1 MiB of host stack below the range it asked for. The title still sees the requested size and address range (`scePthreadAttrGet`, `sceKernelIsStack`). Only host calls use the extra room. - Why 1 MiB: the prepared-shader driver tests need between 128 and 192 KiB of main-thread stack (`ulimit -s` 192 passes, 128 crashes), Vulkan setup included. - Windows: the committed size was the requested size. The room below it came from the executable's default stack reserve, so requests at or above that reserve had none. The commit now includes the same margin. macOS does not run guest threads. - I chose this over switching to a separate host stack in HLE entry points. That would need a trampoline on every export that can go deep, plus handling for stack-passed arguments and unwinding across the switch. ### Tested Linux (CachyOS, GCC, Release). - Astro Bot (PPSA21564): `main` crashes at about 40–49 s (2 of 2 runs). `main` with this change gets past that point to the intro. - `guest_pthread_stack` now also calls host code that uses 256 KiB of stack below each thread's guest range (16 KiB, 64 KiB, 1 MiB and 16 MiB requests). It crashes with SIGSEGV without the fix and passes with it, 20 of 20 runs. - The pthread, thread, stack, cond, semaphore, cancel, guest memory and register context tests pass (31 tests). - Windows: not built. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes (Claude Code)
Mouhand-Kaddo commented 2026-10-10 10:16:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-10T10:16:27Z.


Additional platform validation on standalone PR head 84d90152b4cf6bf6e1ce1f12e3090b02b5fe16fa:

Windows: four focused checks passed; one unrelated parent failure — Windows 11, supported WinLibs GCC 15.2.0 UCRT/POSIX/SEH r7; CMake 4.3.1 / Ninja.

Release / BUILD_TESTING=ON: built the selected test targets; 5 CTest cases, 1 failures, 0 CTest skips.

Tests: guest_kernel_sync, guest_pthread_attr, guest_pthread_stack, guest_pthread_rwlock, guest_memory.

Reproduce after building the test targets: ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_(pthread_stack|thread|thread_stack|memory|pthread_attr|kernel_thread|kernel_sync|pthread_rwlock)$".

Failed cases: guest_memory.

Parent be403084c6 reproduces the identical GuestMemory.cpp:1035 read-only pread refusal assertion (abort/0xc0000409); the broader guest_memory failure is pre-existing, not introduced by the stack change. The PR head passed guest_pthread_stack and three other focused pthread/sync cases. The expanded stack test from this PR also segfaults against the parent implementation, while it passes on the PR head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096478351 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2647#issuecomment-6096478351). Originally posted by [Mouhand-Kaddo](https://github.com/Mouhand-Kaddo) on 2026-10-10T10:16:27Z. --- <!-- anyps5-platform-validation:84d90152b4cf6bf6e1ce1f12e3090b02b5fe16fa --> Additional platform validation on standalone PR head `84d90152b4cf6bf6e1ce1f12e3090b02b5fe16fa`: **Windows: four focused checks passed; one unrelated parent failure** — Windows 11, supported WinLibs GCC 15.2.0 UCRT/POSIX/SEH r7; CMake 4.3.1 / Ninja. Release / `BUILD_TESTING=ON`: built the selected test targets; 5 CTest cases, 1 failures, 0 CTest skips. Tests: `guest_kernel_sync`, `guest_pthread_attr`, `guest_pthread_stack`, `guest_pthread_rwlock`, `guest_memory`. Reproduce after building the test targets: `ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_(pthread_stack|thread|thread_stack|memory|pthread_attr|kernel_thread|kernel_sync|pthread_rwlock)$"`. Failed cases: `guest_memory`. Parent be403084c6 reproduces the identical GuestMemory.cpp:1035 read-only pread refusal assertion (abort/0xc0000409); the broader guest_memory failure is pre-existing, not introduced by the stack change. The PR head passed guest_pthread_stack and three other focused pthread/sync cases. The expanded stack test from this PR also segfaults against the parent implementation, while it passes on the PR head.
husseinkad commented 2026-10-10 10:29:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T10:29:31Z.


Windows check of this PR, since the description says Windows was not built or tested. Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7, Release, PR head merged onto main a29ef592.

  • guest_pthread_stack passes 10/10 (--repeat until-fail:10).
  • With only Thread.cpp reverted to main and this PR's test kept, guest_pthread_stack segfaults; with the change it passes, so the test fails before and passes after on Windows too.
  • The broader -R "pthread|thread|stack" set passes (20 of 22). The other two, host_thread_local and linux_guest_stack, were not built in this run, so they say nothing about the PR.
  • tools/check_conventions.py --base origin/main passes.

Reading the Windows path (Thread.cpp around lines 485-487): the margin is applied, _beginthreadex gets stackSize + 1 MiB rounded to pages, and the guest range, scePthreadAttrGetstack and sceKernelIsStack still report only the requested size.

One thing to be aware of, not measured: on Windows the margin is committed, not just reserved, so every thread now costs 1 MiB more commit charge up front (a title that creates hundreds of threads pays hundreds of MiB). On Linux it is lazily mapped. If that matters, a reserve-only margin would avoid it. The 1 MiB value also comes from the PR's own measurements (128 to 192 KiB needed) and has no TechnicalDebt entry.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096574171 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2647#issuecomment-6096574171). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T10:29:31Z. --- Windows check of this PR, since the description says Windows was not built or tested. Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7, Release, PR head merged onto `main` a29ef592. - `guest_pthread_stack` passes 10/10 (`--repeat until-fail:10`). - With only `Thread.cpp` reverted to `main` and this PR's test kept, `guest_pthread_stack` segfaults; with the change it passes, so the test fails before and passes after on Windows too. - The broader `-R "pthread|thread|stack"` set passes (20 of 22). The other two, `host_thread_local` and `linux_guest_stack`, were not built in this run, so they say nothing about the PR. - `tools/check_conventions.py --base origin/main` passes. Reading the Windows path (`Thread.cpp` around lines 485-487): the margin is applied, `_beginthreadex` gets `stackSize + 1 MiB` rounded to pages, and the guest range, `scePthreadAttrGetstack` and `sceKernelIsStack` still report only the requested size. One thing to be aware of, not measured: on Windows the margin is committed, not just reserved, so every thread now costs 1 MiB more commit charge up front (a title that creates hundreds of threads pays hundreds of MiB). On Linux it is lazily mapped. If that matters, a reserve-only margin would avoid it. The 1 MiB value also comes from the PR's own measurements (128 to 192 KiB needed) and has no TechnicalDebt entry. AI-assisted: yes.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!2647
No description provided.