fix(libkernel): measure the host stack reserve from the frame address #7115

Merged
milescoviello merged 1 commit from upstream-pr/7115 into main 2026-10-17 21:25:49 +00:00
milescoviello commented 2026-10-17 12:11:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T12:11:29Z.


What

In an ANYPS5_ENABLE_ASAN build, guest_once aborts on its first scePthreadCreate:

terminate called after throwing an instance of 'std::runtime_error'
  what():  guest address space arena exhausted

HostStackReserve measures 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's detect_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 and AllocateGuestStack was asked for a stack of 18446744073526816768 bytes:

#12 GuestArena::GuestArenaAllocate_nid_postfix (bytes=<optimized out>, ...)
#15 DoMapAnon (..., len=18446744073526837248, prot=3, ...)
#16 AllocateGuestStack (usableBytes=18446744073526816768, ...) at Thread.cpp:115
#17 scePthreadCreate (...) at Thread.cpp:758

The probe now measures from __builtin_frame_address(0), which is always on the thread's real stack. The codebase already uses it in libSceFiber and libc. In a normal build the two values differ only by the probe's own frame, a few dozen bytes inside the 1 MiB HOST_STACK_MARGIN that scePthreadCreate adds 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.

  • ASan build, main e09dbc2b42: guest_once aborts as above. With this change, rebuilt on the same commit, it passes 3 of 3.
  • ASan build, all non-GPU tests (ctest -E '^agc_driver_|^agc_vertex_barrier|videoout|^agc_shader', 843 tests): 15 fail on main and 14 with this change. The only difference is guest_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_protect and guest_raise_exception check that the test's own local is on the thread's stack. In an instrumented test, that local is on the fake stack.
    • patched_libraries and guest_memory_lifetime fail because libc's copy of the main image's writable pages (GuestAllocations.cpp:103) reads ASan's redzones around the instrumented executable's globals.
    • fiber fails because libSceFiber switches stacks without telling ASan.
    • guest_module_fini, guest_module_args, guest_deferred_dependencies and loadexec_arguments fail because their spawned guests need libasan first in the library list.
    • guest_allocator_failures fails on ASan's own allocation limit.
    • agc_multisample_guest_access passes aligned_alloc a size that isn't a multiple of the alignment.
  • Normal Release build, same commit plus this change: 31 thread and stack tests (guest_pthread*, guest_thread*, guest_once, guest_kernel_is_stack, guest_kernel_stack_protect, guest_raise_exception) pass 3 of 3, and ctest -R '^guest_|libc' passes 403 of 403.
  • Titles on the ASan libraries (libasan preloaded, halt_on_error=1, main e09dbc2b42 without this change):
    • ProsperoEden PPSA99008 (120 s), Signal Companion PPSA69426, ReKHCoM PPSA98511 and PS5LM PPSA99575 run to the limit.
    • Afterglow PPSA47770 exits at 50 s on its own 2 s submission watchdog.
    • None of them produced an ASan report.
  • tools/check_conventions.py passes. tools/pr_overlap.py could not compare Thread.cpp against #3986, #5021, #6222 and #6487, which conflict with main. None of their diffs touches MeasureStackReserve or HostStackReserve.

Not checked: macOS, Windows (this code is not built there), Clang's ASan.

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 under Silent stubs in their library's technical debt file
  • 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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4899940548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7115). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-17T12:11:29Z. --- ### What In an `ANYPS5_ENABLE_ASAN` build, `guest_once` aborts on its first `scePthreadCreate`: ``` terminate called after throwing an instance of 'std::runtime_error' what(): guest address space arena exhausted ``` `HostStackReserve` measures 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's `detect_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 and `AllocateGuestStack` was asked for a stack of 18446744073526816768 bytes: ``` #12 GuestArena::GuestArenaAllocate_nid_postfix (bytes=<optimized out>, ...) #15 DoMapAnon (..., len=18446744073526837248, prot=3, ...) #16 AllocateGuestStack (usableBytes=18446744073526816768, ...) at Thread.cpp:115 #17 scePthreadCreate (...) at Thread.cpp:758 ``` The probe now measures from `__builtin_frame_address(0)`, which is always on the thread's real stack. The codebase already uses it in `libSceFiber` and libc. In a normal build the two values differ only by the probe's own frame, a few dozen bytes inside the 1 MiB `HOST_STACK_MARGIN` that `scePthreadCreate` adds 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`. - **ASan build, `main` e09dbc2b42:** `guest_once` aborts as above. With this change, rebuilt on the same commit, it passes 3 of 3. - **ASan build, all non-GPU tests** (`ctest -E '^agc_driver_|^agc_vertex_barrier|videoout|^agc_shader'`, 843 tests): 15 fail on `main` and 14 with this change. The only difference is `guest_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_protect` and `guest_raise_exception` check that the test's own local is on the thread's stack. In an instrumented test, that local is on the fake stack. - `patched_libraries` and `guest_memory_lifetime` fail because libc's copy of the main image's writable pages (`GuestAllocations.cpp:103`) reads ASan's redzones around the instrumented executable's globals. - `fiber` fails because `libSceFiber` switches stacks without telling ASan. - `guest_module_fini`, `guest_module_args`, `guest_deferred_dependencies` and `loadexec_arguments` fail because their spawned guests need libasan first in the library list. - `guest_allocator_failures` fails on ASan's own allocation limit. - `agc_multisample_guest_access` passes `aligned_alloc` a size that isn't a multiple of the alignment. - **Normal Release build, same commit plus this change:** 31 thread and stack tests (`guest_pthread*`, `guest_thread*`, `guest_once`, `guest_kernel_is_stack`, `guest_kernel_stack_protect`, `guest_raise_exception`) pass 3 of 3, and `ctest -R '^guest_|libc'` passes 403 of 403. - **Titles on the ASan libraries** (libasan preloaded, `halt_on_error=1`, `main` e09dbc2b42 without this change): - ProsperoEden PPSA99008 (120 s), Signal Companion PPSA69426, ReKHCoM PPSA98511 and PS5LM PPSA99575 run to the limit. - Afterglow PPSA47770 exits at 50 s on its own 2 s submission watchdog. - None of them produced an ASan report. - `tools/check_conventions.py` passes. `tools/pr_overlap.py` could not compare `Thread.cpp` against #3986, #5021, #6222 and #6487, which conflict with `main`. None of their diffs touches `MeasureStackReserve` or `HostStackReserve`. Not checked: macOS, Windows (this code is not built there), Clang's ASan. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-17 12:42:48 +00:00 (Migrated from github.com)

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.

Pull request Shared files not compared
PR 3986 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 5021 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6222 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7135 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7201 core/libs/prx/libkernel/Pthread/src/Thread.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6234012173 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7115#issuecomment-6234012173). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T12:42:48Z. --- <!-- pr-overlap --> 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 3986](https://www.github.com/boykopovar/AnyPS5/pull/3986) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7135](https://www.github.com/boykopovar/AnyPS5/pull/7135) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` |
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!7115
No description provided.