libkernel: on Linux, guest thread stacks are mapped near 0x7fff00000000, outside the application area (ChakraCore OutOfMemoryException in Ratchet & Clank: Rift Apart) #3283

Closed
opened 2026-10-11 05:12:13 +00:00 by Tamalero · 1 comment
Tamalero commented 2026-10-11 05:12:13 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-11T05:12:13Z.


On Linux, Ratchet & Clank: Rift Apart ends early in its intro with Terminating due to uncaught exception of type Js::OutOfMemoryException. The JavaScript engine it ships (ChakraCore, used by its UI) runs out of memory not for lack of memory, but because of where AnyPS5 puts guest thread stacks.

glibc maps each guest thread's stack near the top of the host address space, around 0x7fff00000000 (~128 TiB). On the console, application memory, stacks included, lies in the application area below 0xFC00000000 (AnyPS5's own ApplicationAreaEnd in GuestArena.cpp), and ChakraCore relies on that. Its garbage collector indexes a 4 MiB table with address >> 18, which covers addresses up to 1 TiB, so a stack address far above that falls outside the table and the engine reports out-of-memory.

I have a fix on a branch with a test, and I'm opening this as an issue because PRs from new authors are closed by intake: github.com/Tamalero/AnyPS5@4e97c683 (branch fix/guest-thread-stacks on my fork, based on main 961b25fe). If it looks right, I can open it as a PR whenever that suits you.

Details

Title: Ratchet & Clank: Rift Apart (PPSA01474, content 01.005.003)
Where it stops: early in the intro, a few frames in, in every run without the change. CachyOS (Linux 7.2), GCC, Release, RTX 4090, i9-14900K, relinked with --to-intel. The title needs other local changes to get this far (see #3272), but this failure is independent of them: removing only this change from that tree brings the exception back.

What the trace shows

  • A gdb breakpoint on the title's VirtualAlloc-style shim: ChakraCore reserves a 4 MiB table and then commits table + (address >> 18) for an address around 0x7fff00000000. That offset (~512 MiB) is past the end of the table, the commit fails, and the engine throws Js::OutOfMemoryException.
  • The address is the current guest thread's stack, created by pthread_create with a size-only attribute, so glibc placed it.

The change

  • scePthreadCreate maps each guest thread's stack with DoMapAnon, so it comes from the guest arena and is a registered guest allocation. A PROT_NONE guard page sits below it, and it is passed to pthread_attr_setstack.
  • The size is the existing nativeStack, so #2647's HOST_STACK_MARGIN below the guest range is unchanged.
  • The stack is freed after a join, or on creation failure. A detached thread's stack is kept on a retired list and freed once its host thread is gone from /proc/self/task.
  • Linux only. Windows and macOS keep their current paths.
  • Technical debt: a detached thread retired before it has recorded its host tid is never reaped. That leaks the stack only; nothing breaks.

Tested

  • guest_kernel_is_stack now also checks, from a guest thread, that its stack is below 0xFC00000000 and that sceKernelVirtualQuery finds it. Without the change the test aborts; with it, it passes.
  • guest_pthread_stack (#2647's host-margin test), linux_guest_stack, guest_sigaltstack, and the thread, TLS, cancel and memory tests: 80/80.
  • The non-GPU suite passes 400/401. The failure is pr_overlap_encoding, a local Python 3.10 environment issue that also fails on main.
  • tools/check_conventions.py passes.
  • A trial merge with #2982 (stack page protection on Windows) is clean. On Linux, this change makes the stack an ordinary guest allocation, which is what #2982 does for mprotect on Windows.
  • Ratchet: with the change, ChakraCore initialises and the intro continues to the later stops in #3272 and #3274.

Related: #2647, #2982, #3272.

Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5798192090 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3283). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-11T05:12:13Z. --- On Linux, Ratchet & Clank: Rift Apart ends early in its intro with `Terminating due to uncaught exception of type Js::OutOfMemoryException`. The JavaScript engine it ships (ChakraCore, used by its UI) runs out of memory not for lack of memory, but because of where AnyPS5 puts guest thread stacks. glibc maps each guest thread's stack near the top of the host address space, around `0x7fff00000000` (~128 TiB). On the console, application memory, stacks included, lies in the application area below `0xFC00000000` (AnyPS5's own `ApplicationAreaEnd` in `GuestArena.cpp`), and ChakraCore relies on that. Its garbage collector indexes a 4 MiB table with `address >> 18`, which covers addresses up to 1 TiB, so a stack address far above that falls outside the table and the engine reports out-of-memory. I have a fix on a branch with a test, and I'm opening this as an issue because PRs from new authors are closed by intake: https://github.com/Tamalero/AnyPS5/commit/4e97c683 (branch `fix/guest-thread-stacks` on my fork, based on `main` `961b25fe`). If it looks right, I can open it as a PR whenever that suits you. <details><summary>Details</summary> **Title:** Ratchet & Clank: Rift Apart (PPSA01474, content 01.005.003) **Where it stops:** early in the intro, a few frames in, in every run without the change. CachyOS (Linux 7.2), GCC, Release, RTX 4090, i9-14900K, relinked with `--to-intel`. The title needs other local changes to get this far (see #3272), but this failure is independent of them: removing only this change from that tree brings the exception back. ### What the trace shows - A gdb breakpoint on the title's VirtualAlloc-style shim: ChakraCore reserves a 4 MiB table and then commits `table + (address >> 18)` for an address around `0x7fff00000000`. That offset (~512 MiB) is past the end of the table, the commit fails, and the engine throws `Js::OutOfMemoryException`. - The address is the current guest thread's stack, created by `pthread_create` with a size-only attribute, so glibc placed it. ### The change - `scePthreadCreate` maps each guest thread's stack with `DoMapAnon`, so it comes from the guest arena and is a registered guest allocation. A `PROT_NONE` guard page sits below it, and it is passed to `pthread_attr_setstack`. - The size is the existing `nativeStack`, so #2647's `HOST_STACK_MARGIN` below the guest range is unchanged. - The stack is freed after a join, or on creation failure. A detached thread's stack is kept on a retired list and freed once its host thread is gone from `/proc/self/task`. - Linux only. Windows and macOS keep their current paths. - Technical debt: a detached thread retired before it has recorded its host tid is never reaped. That leaks the stack only; nothing breaks. ### Tested - `guest_kernel_is_stack` now also checks, from a guest thread, that its stack is below `0xFC00000000` and that `sceKernelVirtualQuery` finds it. Without the change the test aborts; with it, it passes. - `guest_pthread_stack` (#2647's host-margin test), `linux_guest_stack`, `guest_sigaltstack`, and the thread, TLS, cancel and memory tests: 80/80. - The non-GPU suite passes 400/401. The failure is `pr_overlap_encoding`, a local Python 3.10 environment issue that also fails on `main`. - `tools/check_conventions.py` passes. - A trial merge with #2982 (stack page protection on Windows) is clean. On Linux, this change makes the stack an ordinary guest allocation, which is what #2982 does for `mprotect` on Windows. - Ratchet: with the change, ChakraCore initialises and the intro continues to the later stops in #3272 and #3274. Related: #2647, #2982, #3272. </details> *Tested and written by Claude (AI assistant) on a contributor's machine.*
oneandonlydean commented 2026-10-14 00:08:30 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T00:08:30Z.


Fixed by #4283, which carried your branch: guest thread stacks on Linux now come from the guest arena. If ChakraCore still runs out of memory on current main, comment here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157867539 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3283#issuecomment-6157867539). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T00:08:30Z. --- Fixed by #4283, which carried your branch: guest thread stacks on Linux now come from the guest arena. If ChakraCore still runs out of memory on current main, comment here.
Sign in to join this conversation.
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#3283
No description provided.