fix(libSceFiber): run fibers on host stacks with room for host calls #4265

Merged
fisherjoey merged 3 commits from upstream-pr/4265 into main 2026-10-13 14:12:14 +00:00
fisherjoey commented 2026-10-12 03:32:48 +00:00 (Migrated from github.com)

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 through Driver::ResolveGraphicsStagesAbi goes 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.

  • The guest context is no longer written.
  • Context size checking fills and scans the top size_context bytes of the host stack, so size_context_margin means what it did before.
  • sceFiberFinalize, and sceFiberInitialize on an object that is already a fiber, unmap the stack.
  • If the mapping fails, the fiber runs in its context as before and a line is logged.
  • Windows is unchanged. There the fiber path pins the guest context and publishes it as the stack bounds, and I can't build or test that path.

Tested

  • The fiber test now runs a 256 KiB host call from a 16 KiB fiber placed at the top of a guard-filled area and checks that nothing below the context changed. It fails before the fix (a host call from a 16 KiB fiber writes nothing below its context) and passes after it. fiber and guest_pthread_stack pass. Linux, Release, GCC.
  • Demon's Souls PPSA01341 v1.07, main 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

  • 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; this change adds no stubs
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source; no third-party code added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

Fixes #4264

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4824591963 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4265). Originally posted by [fisherjoey](https://github.com/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 through `Driver::ResolveGraphicsStagesAbi` goes 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. - The guest context is no longer written. - Context size checking fills and scans the top `size_context` bytes of the host stack, so `size_context_margin` means what it did before. - `sceFiberFinalize`, and `sceFiberInitialize` on an object that is already a fiber, unmap the stack. - If the mapping fails, the fiber runs in its context as before and a line is logged. - Windows is unchanged. There the fiber path pins the guest context and publishes it as the stack bounds, and I can't build or test that path. ### Tested - The fiber test now runs a 256 KiB host call from a 16 KiB fiber placed at the top of a guard-filled area and checks that nothing below the context changed. It fails before the fix (`a host call from a 16 KiB fiber writes nothing below its context`) and passes after it. `fiber` and `guest_pthread_stack` pass. Linux, Release, GCC. - Demon's Souls PPSA01341 v1.07, main 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 - [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 - [x] Unimplemented paths throw; this change adds no stubs - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source; no third-party code added - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes Fixes #4264
LeandroLP23 commented 2026-10-12 07:05:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-12T07:05:36Z.


Windows run of this head (9859b11c0) merged onto current main (d06c06565): Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, full build.

fiber fails 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 with Export.cpp reverted to main.

Under _WIN32, AllocateHostStack and ReleaseHostStack are 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 in Fiber.cpp is not platform-guarded, so merging this as is would turn fiber red on Windows main. Upstream skips the Windows job on PRs, so CI here won't show it.

Two ways out:

  • give Windows its own host stack: VirtualAlloc with a guard page, the counterpart of the mmap path;
  • keep the change Linux-only: put the new check under #ifndef _WIN32 and record the Windows gap in TechnicalDebt.md.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6119432359 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4265#issuecomment-6119432359). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-12T07:05:36Z. --- Windows run of this head (`9859b11c0`) merged onto current `main` (`d06c06565`): Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, full build. **`fiber` fails 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 with `Export.cpp` reverted to `main`. Under `_WIN32`, `AllocateHostStack` and `ReleaseHostStack` are 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 in `Fiber.cpp` is not platform-guarded, so merging this as is would turn `fiber` red on Windows `main`. Upstream skips the Windows job on PRs, so CI here won't show it. Two ways out: - give Windows its own host stack: `VirtualAlloc` with a guard page, the counterpart of the `mmap` path; - keep the change Linux-only: put the new check under `#ifndef _WIN32` and record the Windows gap in `TechnicalDebt.md`.
oneandonlydean commented 2026-10-12 09:15:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T09:15:01Z.


Two things before this can go in:

  • AllocateHostStack falls back to the title's context and logs when mmap fails (libSceFiber/Export.cpp:280-284). Fallbacks aren't allowed here; please throw instead, and check the mprotect result the same way.
  • The new check in tests/Fiber.cpp fails on Windows, as LeandroLP23 found, because Windows gets no host stack. Either give it one (VirtualAlloc with a guard page), or put the check under #ifndef _WIN32 and add a TechnicalDebt line saying a fiber's host calls still run in the title's context there.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6121270631 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4265#issuecomment-6121270631). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T09:15:01Z. --- Two things before this can go in: - `AllocateHostStack` falls back to the title's context and logs when `mmap` fails (`libSceFiber/Export.cpp:280-284`). Fallbacks aren't allowed here; please throw instead, and check the `mprotect` result the same way. - The new check in `tests/Fiber.cpp` fails on Windows, as LeandroLP23 found, because Windows gets no host stack. Either give it one (`VirtualAlloc` with a guard page), or put the check under `#ifndef _WIN32` and add a TechnicalDebt line saying a fiber's host calls still run in the title's context there.
github-actions[bot] commented 2026-10-12 21:43:46 +00:00 (Migrated from github.com)

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.

Pull request Conflicting files (lines on main) Same exports Same new files
#4770 core/libs/tests/Fiber.cpp L111, L156
#4892 core/libs/prx/libSceFiber/Export.cpp L31, L55, L261, L333, L341-342, L344-345
core/libs/tests/Fiber.cpp L8, L111
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133159665 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4265#issuecomment-6133159665). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T21:43:46Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4770 | `core/libs/tests/Fiber.cpp` [L111](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/tests/Fiber.cpp#L111), [L156](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/tests/Fiber.cpp#L156) | | | | #4892 | `core/libs/prx/libSceFiber/Export.cpp` [L31](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/prx/libSceFiber/Export.cpp#L31), [L55](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/prx/libSceFiber/Export.cpp#L55), [L261](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/prx/libSceFiber/Export.cpp#L261), [L333](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/prx/libSceFiber/Export.cpp#L333), [L341-342](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/prx/libSceFiber/Export.cpp#L341-L342), [L344-345](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/prx/libSceFiber/Export.cpp#L344-L345)<br>`core/libs/tests/Fiber.cpp` [L8](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/tests/Fiber.cpp#L8), [L111](https://github.com/boykopovar/AnyPS5/blob/d0711248b19594e04c0c02278314a9e21e75613a/core/libs/tests/Fiber.cpp#L111) | | |
Mouhand-Kaddo commented 2026-10-13 07:16:55 +00:00 (Migrated from github.com)

Imported 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 ea2bbef fiber production, fiber_tests exits 134 with Fiber 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140388898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4265#issuecomment-6140388898). Originally posted by [Mouhand-Kaddo](https://github.com/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 `ea2bbef` fiber production, `fiber_tests` exits 134 with `Fiber 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.
fdfawef324 commented 2026-10-13 13:16:15 +00:00 (Migrated from github.com)

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 branch fdfawef324:fix/libscefiber-windows-host-stacks, which is main e09ee165d plus your 9859b11c0 (as 32d50b7b6).

What it does on Windows:

  • AllocateHostStack gets the stack from VirtualAlloc, sized to the context plus FIBER_HOST_STACK_MARGIN, committed, with a PAGE_NOACCESS page at the bottom.
  • While the fiber runs, FiberBounds gives the TIB stack base and limit and the deallocation stack the host stack's bounds, as Windows does for its own fibers.
  • Allocate, protect and release failures throw. There is no fallback to the context, as @oneandonlydean asked.
  • tests/Fiber.cpp also checks that the stack bounds inside a fiber are its host stack, at least the context plus 256 KiB.
    • With 9859b11c0 alone it fails at "the thread's stack bounds are the fiber's host stack". Without that check it fails at "a host call from a 16 KiB fiber writes nothing below its context", which is what @LeandroLP23 saw.
    • With the commit, fiber, agc_driver_gpu_map_on_fiber, guest_sysmodule, agc_stack_walk, guest_kernel_is_stack, guest_kernel_stack_protect and guest_pthread_stack pass. Host: Windows Server 2025, RTX 3060, WinLibs GCC 15.2.0, Release.
  • agc_driver_gpu_map_on_fiber expected 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:

  • Before: three runs crashed on the loading screen after the opening cinematic, on a BPE JobWorkerThread.
    • Two failed the title's stack check or returned to 0x7fffffff.
    • One read back a guest stack canary while running the recompiler 0x200 bytes below the bottom of a 16 KiB job fiber.
    • The overwritten frame sits at the top of the first 16 KiB job context. Its return slot held a return address into BuildShaderStageInputInfo, reached from sceAgcCreateInterpolantMapping on the fiber above it.
  • With this PR and the Windows commit: the two job fibers above that context use 17,592 and 16,936 bytes of their host stacks, and the stack check no longer fails. The load continues until a separate failure, a vkAllocateMemory of 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6146415353 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4265#issuecomment-6146415353). Originally posted by [fdfawef324](https://github.com/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`](https://github.com/fdfawef324/AnyPS5/commit/549322112), on branch [`fdfawef324:fix/libscefiber-windows-host-stacks`](https://github.com/fdfawef324/AnyPS5/tree/fix/libscefiber-windows-host-stacks), which is `main` e09ee165d plus your 9859b11c0 (as 32d50b7b6). **What it does on Windows:** - `AllocateHostStack` gets the stack from `VirtualAlloc`, sized to the context plus `FIBER_HOST_STACK_MARGIN`, committed, with a `PAGE_NOACCESS` page at the bottom. - While the fiber runs, `FiberBounds` gives the TIB stack base and limit and the deallocation stack the host stack's bounds, as Windows does for its own fibers. - Allocate, protect and release failures throw. There is no fallback to the context, as @oneandonlydean asked. - `tests/Fiber.cpp` also checks that the stack bounds inside a fiber are its host stack, at least the context plus 256 KiB. - With 9859b11c0 alone it fails at "the thread's stack bounds are the fiber's host stack". Without that check it fails at "a host call from a 16 KiB fiber writes nothing below its context", which is what @LeandroLP23 saw. - With the commit, `fiber`, `agc_driver_gpu_map_on_fiber`, `guest_sysmodule`, `agc_stack_walk`, `guest_kernel_is_stack`, `guest_kernel_stack_protect` and `guest_pthread_stack` pass. Host: Windows Server 2025, RTX 3060, WinLibs GCC 15.2.0, Release. - `agc_driver_gpu_map_on_fiber` expected 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:** - **Before:** three runs crashed on the loading screen after the opening cinematic, on a `BPE JobWorkerThread`. - Two failed the title's stack check or returned to 0x7fffffff. - One read back a guest stack canary while running the recompiler 0x200 bytes below the bottom of a 16 KiB job fiber. - The overwritten frame sits at the top of the first 16 KiB job context. Its return slot held a return address into `BuildShaderStageInputInfo`, reached from `sceAgcCreateInterpolantMapping` on the fiber above it. - **With this PR and the Windows commit:** the two job fibers above that context use 17,592 and 16,936 bytes of their host stacks, and the stack check no longer fails. The load continues until a separate failure, a `vkAllocateMemory` of 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.
adriadam10 commented 2026-10-13 14:02:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:02:34Z.


Rebased onto current main and added the two things @oneandonlydean asked for, as our own commits on your branch:

  • Windows: cherry-picked @fdfawef324's 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 in tests/Fiber.cpp, and the agc_driver_gpu_map_on_fiber expectation), authorship kept. They measured it on Windows Server 2025 and Demon's Souls.
  • Linux: a failed mmap or mprotect now throws instead of falling back to the title's context.

fiber and agc_driver_gpu_map_on_fiber pass locally (Linux, Release, GCC). The Windows side runs in the full Build before merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6147328550 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4265#issuecomment-6147328550). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:02:34Z. --- Rebased onto current `main` and added the two things @oneandonlydean asked for, as our own commits on your branch: - Windows: cherry-picked @fdfawef324's `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 in `tests/Fiber.cpp`, and the `agc_driver_gpu_map_on_fiber` expectation), authorship kept. They measured it on Windows Server 2025 and Demon's Souls. - Linux: a failed `mmap` or `mprotect` now throws instead of falling back to the title's context. `fiber` and `agc_driver_gpu_map_on_fiber` pass locally (Linux, Release, GCC). The Windows side runs in the full Build before merging.
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!4265
No description provided.