fix(libSceFiber): run fibers on host stacks with room for host calls #4265
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4265"
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 fisherjoey on 2026-10-12T03:32:48Z.
What
Host code that a guest fiber calls runs on the fiber's stack. libSceFiber switches to the context the guest passed to
sceFiberInitialize, so a host call from a fiber gets only what is left of that context. Demon's Souls gives its job fibers 16 KiB contexts placed back to back. A fiber that reaches shader preparation throughDriver::ResolveGraphicsStagesAbigoes at least 21 KiB deep while SPIRV-Tools validates the module, which overwrites the saved frame of the fiber below it. The next switch to that fiber pops validator locals into rbp, rbx and r12-r15, then returns to garbage (0x1a in the crash in #4264).Each fiber now runs on an mmap'd host stack sized to the context plus 1 MiB, with a PROT_NONE guard page at the bottom. Guest threads have had a 1 MiB margin since #2647, placed below the stack the title asked for. That doesn't work for fibers, because the title owns the memory around each context (here the next fiber's context), so the fiber gets its own host stack instead.
size_contextbytes of the host stack, sosize_context_marginmeans what it did before.sceFiberFinalize, andsceFiberInitializeon an object that is already a fiber, unmap the stack.Tested
a host call from a 16 KiB fiber writes nothing below its context) and passes after it.fiberandguest_pthread_stackpass. Linux, Release, GCC.7a84b222+ #4049 + #758 + #4241 with this change (only libSceFiber.prx differs from the crashing build),APS5_SPIRV_OPT=none, direct boot into the tutorial. Without the change it segfaulted at about 880 s during the level load. With it, the title ran to 1209 s and presented 157 frames, then stopped on an unrelated abort:depth export, shader coverage or ordered fragment execution is unsupported (register 0x203 = 0x00000052), the stencil export bit of DB_SHADER_CONTROL.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryFixes #4264
Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-12T07:05:36Z.
Windows run of this head (
9859b11c0) merged onto currentmain(d06c06565): Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, full build.fiberfails 3/3 with this change:Fiber check failed: a host call from a 16 KiB fiber writes nothing below its context. It fails the same way withExport.cppreverted tomain.Under
_WIN32,AllocateHostStackandReleaseHostStackare empty. On Windows a fiber therefore still runs host calls inside its own context, so the overwrite this PR fixes on Linux still happens there. The new check inFiber.cppis not platform-guarded, so merging this as is would turnfiberred on Windowsmain. Upstream skips the Windows job on PRs, so CI here won't show it.Two ways out:
VirtualAllocwith a guard page, the counterpart of themmappath;#ifndef _WIN32and record the Windows gap inTechnicalDebt.md.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T09:15:01Z.
Two things before this can go in:
AllocateHostStackfalls back to the title's context and logs whenmmapfails (libSceFiber/Export.cpp:280-284). Fallbacks aren't allowed here; please throw instead, and check themprotectresult the same way.tests/Fiber.cppfails on Windows, as LeandroLP23 found, because Windows gets no host stack. Either give it one (VirtualAllocwith a guard page), or put the check under#ifndef _WIN32and add a TechnicalDebt line saying a fiber's host calls still run in the title's context there.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T21:43:46Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/tests/Fiber.cppL111, L156core/libs/prx/libSceFiber/Export.cppL31, L55, L261, L333, L341-342, L344-345core/libs/tests/Fiber.cppL8, L111Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-13T07:16:55Z.
Tested the added Linux fiber regression at
9859b11c0b1ee1fc4044ec0d4c921815bd5a49ba(Release, Ninja).Using the candidate test with current public-main
ea2bbeffiber production,fiber_testsexits 134 withFiber check failed: a host call from a 16 KiB fiber writes nothing below its context. With this PR's exact fiber production, the same test exits 0. The main fiber files are byte-identical to this PR's parent for this comparison.This was an isolated exact-file test in an existing shader-test checkout, with dependencies mounted read-only. It verifies the normal-allocation 256 KiB host-call case. I have not run Demon's Souls with this change yet, or tested Windows and allocation/protection failure paths.
Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-13T13:16:15Z.
This PR fixes the crash I was chasing in Demon's Souls (PPSA01342) on Windows, once it has the Windows half that review asked for. I wrote that half on top of your head and it is ready for you to cherry-pick: commit
549322112, on branchfdfawef324:fix/libscefiber-windows-host-stacks, which ismaine09ee165dplus your 9859b11c0 (as 32d50b7b6).What it does on Windows:
AllocateHostStackgets the stack fromVirtualAlloc, sized to the context plusFIBER_HOST_STACK_MARGIN, committed, with aPAGE_NOACCESSpage at the bottom.FiberBoundsgives the TIB stack base and limit and the deallocation stack the host stack's bounds, as Windows does for its own fibers.tests/Fiber.cppalso checks that the stack bounds inside a fiber are its host stack, at least the context plus 256 KiB.fiber,agc_driver_gpu_map_on_fiber,guest_sysmodule,agc_stack_walk,guest_kernel_is_stack,guest_kernel_stack_protectandguest_pthread_stackpass. Host: Windows Server 2025, RTX 3060, WinLibs GCC 15.2.0, Release.agc_driver_gpu_map_on_fiberexpected a GPU mapping on a fiber to start a host thread. A fiber now reports its host stack, which has room for the import, so on Windows it imports inline. The commit changes that expectation; the context is still not written.Demon's Souls on Windows:
BPE JobWorkerThread.BuildShaderStageInputInfo, reached fromsceAgcCreateInterpolantMappingon the fiber above it.vkAllocateMemoryof a 4 MiB host-visible buffer that runs out of device memory.This also makes my #4892 (
APS5_FIBER_GUARD) moot on Windows: once the context is no longer used as a stack, guarding it adds nothing.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:02:34Z.
Rebased onto current
mainand added the two things @oneandonlydean asked for, as our own commits on your branch:fix(libSceFiber): run fibers on host stacks on Windows(VirtualAlloc host stack with a guard page, TIB stack bounds while the fiber runs, a Windows check intests/Fiber.cpp, and theagc_driver_gpu_map_on_fiberexpectation), authorship kept. They measured it on Windows Server 2025 and Demon's Souls.mmapormprotectnow throws instead of falling back to the title's context.fiberandagc_driver_gpu_map_on_fiberpass locally (Linux, Release, GCC). The Windows side runs in the full Build before merging.