fix(libkernel): keep the Windows APR read staging buffer off the stack #8394

Merged
Sam-mon7 merged 1 commit from upstream-pr/8394 into main 2026-10-20 23:14:31 +00:00
Sam-mon7 commented 2026-10-20 12:05:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Sam-mon7 on 2026-10-20T12:05:23Z.


What

Windows APR reads are staged through a 64 KiB std::array on the stack of the thread that submits them (#6449). Titles submit reads from fibers whose stacks they allocate themselves, which can be smaller than that and have no guard page, so the array runs past the bottom of the fiber stack and the file data lands on whatever lies below it.

Demon's Souls (PPSA01341 01.007.000) on current main crashes about half a second after start, in scePthreadMutexLock called from a BPE JobWorkerThread fiber, on a mutex that holds bytes of /app0/playgo-chunkdefs.xml. A run with --trace-imports and APS5_TRACE_CALLS=libkernel shows that thread's last calls were sceKernelAprResolveFilepathsToIdsAndFileSizes, then sceKernelAprSubmitCommandBuffer, then the failing lock. The overwritten mutex lies 56,784 bytes below the thread's stack pointer at the crash, and the bytes on it are the file's bytes at offset 9138: where a 64 KiB array placed 386 bytes of frames below that stack pointer would hold them.

The staging buffer is now allocated for each read, at most 64 KiB. The new guest_apr_read_stack case runs a read on a thread with a 64 KiB stack.

Tested

Windows 11 (10.0.26200), winlibs GCC 15.2, Release builds. The tests were run on this branch rebased onto main at 3dc9b794e, and the game runs used main at 1d4164552 with and without the change.

  • guest_apr_read_{host,direct,eof,errors,streams,fault,stack} pass. With Apr.cpp reverted to main and the new case kept, guest_apr_read_stack ends in 0xc00000fd (stack overflow).
  • Demon's Souls (PPSA01341 01.007.000), relinked with --windows --registry, default settings, empty shader cache. On main it stops in the crash above in every run (four runs). With this change it finishes initialisation at 13.3 s, starts its main loop at 50.2 s and presents frames.

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
  • Fixes: none
  • Unblocks: PPSA01341 #1283
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4935454881 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8394). Originally posted by [Sam-mon7](https://github.com/Sam-mon7) on 2026-10-20T12:05:23Z. --- ### What Windows APR reads are staged through a 64 KiB `std::array` on the stack of the thread that submits them (#6449). Titles submit reads from fibers whose stacks they allocate themselves, which can be smaller than that and have no guard page, so the array runs past the bottom of the fiber stack and the file data lands on whatever lies below it. Demon's Souls (PPSA01341 01.007.000) on current `main` crashes about half a second after start, in `scePthreadMutexLock` called from a `BPE JobWorkerThread` fiber, on a mutex that holds bytes of `/app0/playgo-chunkdefs.xml`. A run with `--trace-imports` and `APS5_TRACE_CALLS=libkernel` shows that thread's last calls were `sceKernelAprResolveFilepathsToIdsAndFileSizes`, then `sceKernelAprSubmitCommandBuffer`, then the failing lock. The overwritten mutex lies 56,784 bytes below the thread's stack pointer at the crash, and the bytes on it are the file's bytes at offset 9138: where a 64 KiB array placed 386 bytes of frames below that stack pointer would hold them. The staging buffer is now allocated for each read, at most 64 KiB. The new `guest_apr_read_stack` case runs a read on a thread with a 64 KiB stack. ### Tested Windows 11 (10.0.26200), winlibs GCC 15.2, Release builds. The tests were run on this branch rebased onto `main` at 3dc9b794e, and the game runs used `main` at 1d4164552 with and without the change. - `guest_apr_read_{host,direct,eof,errors,streams,fault,stack}` pass. With `Apr.cpp` reverted to `main` and the new case kept, `guest_apr_read_stack` ends in `0xc00000fd` (stack overflow). - Demon's Souls (PPSA01341 01.007.000), relinked with `--windows --registry`, default settings, empty shader cache. On `main` it stops in the crash above in every run (four runs). With this change it finishes initialisation at 13.3 s, starts its main loop at 50.2 s and presents frames. ### 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] Fixes: none - [x] Unblocks: PPSA01341 #1283 - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Izamaru commented 2026-10-20 17:00:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Izamaru on 2026-10-20T17:00:22Z.


@Sam-mon7, as the author of #6449, I support merging this follow-up once the required project CI is satisfied. Small stacks were a gap in our original test coverage; thanks for identifying it and adding a regression case. I found no blocking issue in this change.

Independent validation of head 928a11bf2af54d8de8a3a25a821e192d0816ca0f, using parent/main 3dc9b794e8365545746912f6b56abb9ad47c9858 as the control: Windows 11 build 26200, WinLibs GCC 15.2.0 r7, Release, normal CMake configuration with BUILD_TESTING=ON.

  • Built guest_apr_read_tests and its libc/libkernel dependencies. All 7/7 guest_apr_read_{host,direct,eof,errors,streams,fault,stack} cases passed.
  • Keeping the same PR fixtures and build settings, I replaced only production Apr.cpp with the parent version and rebuilt. The six existing cases passed; the new small-stack case exited with 0xC00000FD (STATUS_STACK_OVERFLOW). The direct control used the same DLL directory as CTest.
  • Restoring the exact PR source and rebuilding gave 7/7 passing again, with a clean checkout.
  • git diff --check and the conventions check against that pinned base passed.

The change retains #6449's host staging and CPU copy, destination protection checks, chunked reads and EOF handling, while removing the 64 KiB local array from the submitting thread's stack.

Limitations: these were focused CPU APR tests. I did not run Linux, the full suite, Demon's Souls or Yōtei, and did not validate graphics or measure FPS, loading time or whole-game RAM. This validation does not establish a title unblock and does not replace the project's full CI.

AI-assisted: yes (OpenAI Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6296444818 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8394#issuecomment-6296444818). Originally posted by [Izamaru](https://github.com/Izamaru) on 2026-10-20T17:00:22Z. --- @Sam-mon7, as the author of #6449, I support merging this follow-up once the required project CI is satisfied. Small stacks were a gap in our original test coverage; thanks for identifying it and adding a regression case. I found no blocking issue in this change. Independent validation of head `928a11bf2af54d8de8a3a25a821e192d0816ca0f`, using parent/main `3dc9b794e8365545746912f6b56abb9ad47c9858` as the control: Windows 11 build 26200, WinLibs GCC 15.2.0 r7, Release, normal CMake configuration with `BUILD_TESTING=ON`. - Built `guest_apr_read_tests` and its `libc`/`libkernel` dependencies. All **7/7** `guest_apr_read_{host,direct,eof,errors,streams,fault,stack}` cases passed. - Keeping the same PR fixtures and build settings, I replaced only production `Apr.cpp` with the parent version and rebuilt. The six existing cases passed; the new small-stack case exited with **`0xC00000FD` (`STATUS_STACK_OVERFLOW`)**. The direct control used the same DLL directory as CTest. - Restoring the exact PR source and rebuilding gave **7/7 passing again**, with a clean checkout. - `git diff --check` and the conventions check against that pinned base passed. The change retains #6449's host staging and CPU copy, destination protection checks, chunked reads and EOF handling, while removing the 64 KiB local array from the submitting thread's stack. **Limitations:** these were focused CPU APR tests. I did not run Linux, the full suite, Demon's Souls or Yōtei, and did not validate graphics or measure FPS, loading time or whole-game RAM. This validation does not establish a title unblock and does not replace the project's full CI. AI-assisted: yes (OpenAI Codex).
hamb3r commented 2026-10-20 20:43:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-20T20:43:04Z.


Confirmed this exact crash and fix independently on Windows 10 / AMD Ryzen 7 7700 in Demon's Souls (PPSA01341 01.007.000) on main (6e9279c51a).

Without this change, BPE JobWorkerThread fibers crash ~0.5 s after boot when Apr::_readFile's 64 KiB stack std::array overflows the guest libSceFiber.prx stack and overwrites the caller's frame (rbp = 0x616e696624207475 = "ut $fina", [rbx] / [rdi]) with the contents of /app0/playgo-chunkdefs.xml:

FATAL: unhandled exception 0xc0000005 on thread 64720 'BPE JobWorkerThread CPU7'
  rip 0x00007ffa69da71f8 libkernel.prx+0x571f8
  execute of 0x0000000000000000
  rax 0000000000000009 rbx 000000026857fb80 rcx 0000000000000001 rdx 00007ffa8af98b31
  rsi 000000026858da18 rdi 000000026857fb80 rbp 616e696624207475 rsp 000000026858d950
  [rbx] "ut $finalAssets /source $desiredAssets /destPathName \"dst_path\" /srcPathName \"src_path\" /srcBasePath \"BASE_PATH\" /attributes \"chunk=\"\"$chunk\"\"\"</value>\r\n\t\t\t</cmd>\r\n\t\t</chunk>\r\n\t\t<chunk id=\"2\" label=\"common\" enabled=\"true\">\r\n\r\n\t\t\t<!-- Gather Boletaria -->..."

Moving the staging buffer to std::vector<char> on the heap completely resolves the startup crash in PPSA01341, allowing the game to proceed through boot, intro video, and title screen rendering.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6301046781 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8394#issuecomment-6301046781). Originally posted by [hamb3r](https://github.com/hamb3r) on 2026-10-20T20:43:04Z. --- Confirmed this exact crash and fix independently on **Windows 10 / AMD Ryzen 7 7700** in **Demon's Souls (`PPSA01341` 01.007.000)** on `main` (`6e9279c51a`). Without this change, `BPE JobWorkerThread` fibers crash ~0.5 s after boot when `Apr::_readFile`'s 64 KiB stack `std::array` overflows the guest `libSceFiber.prx` stack and overwrites the caller's frame (`rbp = 0x616e696624207475` = `"ut $fina"`, `[rbx]` / `[rdi]`) with the contents of `/app0/playgo-chunkdefs.xml`: ```text FATAL: unhandled exception 0xc0000005 on thread 64720 'BPE JobWorkerThread CPU7' rip 0x00007ffa69da71f8 libkernel.prx+0x571f8 execute of 0x0000000000000000 rax 0000000000000009 rbx 000000026857fb80 rcx 0000000000000001 rdx 00007ffa8af98b31 rsi 000000026858da18 rdi 000000026857fb80 rbp 616e696624207475 rsp 000000026858d950 [rbx] "ut $finalAssets /source $desiredAssets /destPathName \"dst_path\" /srcPathName \"src_path\" /srcBasePath \"BASE_PATH\" /attributes \"chunk=\"\"$chunk\"\"\"</value>\r\n\t\t\t</cmd>\r\n\t\t</chunk>\r\n\t\t<chunk id=\"2\" label=\"common\" enabled=\"true\">\r\n\r\n\t\t\t<!-- Gather Boletaria -->..." ``` Moving the staging buffer to `std::vector<char>` on the heap completely resolves the startup crash in `PPSA01341`, allowing the game to proceed through boot, intro video, and title screen rendering.
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!8394
No description provided.