fix(libkernel): keep the Windows APR read staging buffer off the stack #8394
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8394"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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::arrayon 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
maincrashes about half a second after start, inscePthreadMutexLockcalled from aBPE JobWorkerThreadfiber, on a mutex that holds bytes of/app0/playgo-chunkdefs.xml. A run with--trace-importsandAPS5_TRACE_CALLS=libkernelshows that thread's last calls weresceKernelAprResolveFilepathsToIdsAndFileSizes, thensceKernelAprSubmitCommandBuffer, 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_stackcase 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
mainat3dc9b794e, and the game runs usedmainat1d4164552with and without the change.guest_apr_read_{host,direct,eof,errors,streams,fault,stack}pass. WithApr.cppreverted tomainand the new case kept,guest_apr_read_stackends in0xc00000fd(stack overflow).--windows --registry, default settings, empty shader cache. Onmainit 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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/main3dc9b794e8365545746912f6b56abb9ad47c9858as the control: Windows 11 build 26200, WinLibs GCC 15.2.0 r7, Release, normal CMake configuration withBUILD_TESTING=ON.guest_apr_read_testsand itslibc/libkerneldependencies. All 7/7guest_apr_read_{host,direct,eof,errors,streams,fault,stack}cases passed.Apr.cppwith the parent version and rebuilt. The six existing cases passed; the new small-stack case exited with0xC00000FD(STATUS_STACK_OVERFLOW). The direct control used the same DLL directory as CTest.git diff --checkand 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).
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 (
PPSA0134101.007.000) onmain(6e9279c51a).Without this change,
BPE JobWorkerThreadfibers crash ~0.5 s after boot whenApr::_readFile's 64 KiB stackstd::arrayoverflows the guestlibSceFiber.prxstack and overwrites the caller's frame (rbp = 0x616e696624207475="ut $fina",[rbx]/[rdi]) with the contents of/app0/playgo-chunkdefs.xml:Moving the staging buffer to
std::vector<char>on the heap completely resolves the startup crash inPPSA01341, allowing the game to proceed through boot, intro video, and title screen rendering.