fix(libSceFiber): run a fiber on its own context when the context is large #6003

Merged
XairRrR merged 1 commit from upstream-pr/6003 into main 2026-10-17 00:45:05 +00:00
XairRrR commented 2026-10-14 18:52:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-14T18:52:31Z.


What

Since #4265 every fiber runs on an mmap'd host stack (its context size plus 1 MiB), so that host calls made from a fiber cannot overrun the small contexts PPSA01341 packs next to each other. A title that derives state from the stack pointer then reads the wrong memory:

  • PPSA05684 (Uncharted: Legacy of Thieves Collection) creates 36 job fibers with 512 KiB contexts at 1 MiB-aligned addresses and 64 fibers with 64 KiB contexts. Its job fibers find their job record by masking the stack pointer (and rsp, ~0xfffff for the 512 KiB ones). On a host stack the mask lands outside the context, the job system loses track of a GameInit job, and the main thread waits forever (while (job->counter) usleep(1)) right after loading big4r/actor76.txt.
  • With only the 512 KiB fibers back on their contexts, the game passes GameInit and then faults in guest code running on a 64 KiB fiber's host stack.

A fiber now gets a host stack only when its context is smaller than 64 KiB; a larger context is the stack again, as on the console. StackTop, FiberBounds and SizedWindow already handle a fiber without a host stack, so sceFiberGetInfo (#5348) keeps reporting the context the fiber runs on.

The limit sits between the 16 KiB contexts PPSA01341 needs protected and the 64 KiB ones PPSA05684 needs as its stack; it is chosen between these two titles, not measured, and recorded in docs/dev/debt/libSceFiber.md. A title with small contexts that also derives state from the stack pointer, or one whose host calls overrun what is left of a large context, is not covered.

Tested

  • Linux 7.2.9-zen, GCC, Ninja, Release, main 2b7e3ce69: full ctest 1182 of 1182, tools/check_conventions.py --base origin/main passes.
  • New check in fiber (RunOnLargeContext): a fiber with a 512 KiB context runs with its stack pointer inside its context. With Export.cpp from main it fails with a fiber with a 512 KiB context runs on its context. RunHostCallOnSmallContext (the PPSA01341 case from #4265) and agc_driver_gpu_map_on_fiber still pass.
  • PPSA05684 on a local stack (main plus #757/#758, which it needs to draw): without this change it hangs in GameInit; with it (and the other local patches that stack carries), it reaches the main menu. With a 256 KiB limit it faulted later in a 64 KiB fiber, which is why the limit is 64 KiB.
  • PPSA01341 itself was not run.

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 (#5735 edits AllocateHostStack in the same file, no overlap in lines)
  • Unblocks: PPSA05684 #4001 (GameInit hang on current main)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4862880874 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6003). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-14T18:52:31Z. --- ### What Since #4265 every fiber runs on an mmap'd host stack (its context size plus 1 MiB), so that host calls made from a fiber cannot overrun the small contexts PPSA01341 packs next to each other. A title that derives state from the stack pointer then reads the wrong memory: - PPSA05684 (Uncharted: Legacy of Thieves Collection) creates 36 job fibers with 512 KiB contexts at 1 MiB-aligned addresses and 64 fibers with 64 KiB contexts. Its job fibers find their job record by masking the stack pointer (`and rsp, ~0xfffff` for the 512 KiB ones). On a host stack the mask lands outside the context, the job system loses track of a GameInit job, and the main thread waits forever (`while (job->counter) usleep(1)`) right after loading `big4r/actor76.txt`. - With only the 512 KiB fibers back on their contexts, the game passes GameInit and then faults in guest code running on a 64 KiB fiber's host stack. A fiber now gets a host stack only when its context is smaller than 64 KiB; a larger context is the stack again, as on the console. `StackTop`, `FiberBounds` and `SizedWindow` already handle a fiber without a host stack, so `sceFiberGetInfo` (#5348) keeps reporting the context the fiber runs on. The limit sits between the 16 KiB contexts PPSA01341 needs protected and the 64 KiB ones PPSA05684 needs as its stack; it is chosen between these two titles, not measured, and recorded in `docs/dev/debt/libSceFiber.md`. A title with small contexts that also derives state from the stack pointer, or one whose host calls overrun what is left of a large context, is not covered. ### Tested - Linux 7.2.9-zen, GCC, Ninja, Release, `main` 2b7e3ce69: full ctest 1182 of 1182, `tools/check_conventions.py --base origin/main` passes. - New check in `fiber` (`RunOnLargeContext`): a fiber with a 512 KiB context runs with its stack pointer inside its context. With `Export.cpp` from `main` it fails with `a fiber with a 512 KiB context runs on its context`. `RunHostCallOnSmallContext` (the PPSA01341 case from #4265) and `agc_driver_gpu_map_on_fiber` still pass. - PPSA05684 on a local stack (`main` plus #757/#758, which it needs to draw): without this change it hangs in GameInit; with it (and the other local patches that stack carries), it reaches the main menu. With a 256 KiB limit it faulted later in a 64 KiB fiber, which is why the limit is 64 KiB. - PPSA01341 itself was not run. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 (#5735 edits `AllocateHostStack` in the same file, no overlap in lines) - [x] Unblocks: PPSA05684 #4001 (GameInit hang on current `main`) - [x] GPUs tested: n/a - [x] AI-assisted: yes
Dextroid17 commented 2026-10-15 02:09:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T02:09:43Z.


I ran this PR's test on Linux.

Setup: this PR's diff applied to main at 8cd937e29 (the PR's base is 282 commits behind it), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), clean environment. ctest -R '^fiber$', 3 runs each. The test did not open a GPU device, so this is a CPU-only run.

  • with this PR: 3 of 3 pass.
  • with libSceFiber/Export.cpp put back to main's version (the new test kept): 3 of 3 fail at "Fiber check failed: a fiber with a 512 KiB context runs on its context".

So the new test catches the old behaviour here. I did not check Windows, macOS or a real GPU.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6182791926 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6003#issuecomment-6182791926). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-15T02:09:43Z. --- I ran this PR's test on Linux. Setup: this PR's diff applied to main at 8cd937e29 (the PR's base is 282 commits behind it), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), clean environment. `ctest -R '^fiber$'`, 3 runs each. The test did not open a GPU device, so this is a CPU-only run. - with this PR: 3 of 3 pass. - with libSceFiber/Export.cpp put back to main's version (the new test kept): 3 of 3 fail at "Fiber check failed: a fiber with a 512 KiB context runs on its context". So the new test catches the old behaviour here. I did not check Windows, macOS or a real GPU. AI-assisted: yes
github-actions[bot] commented 2026-10-15 12:13:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:13:06Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/tests/Fiber.cpp
PR 6222 core/libs/prx/libSceFiber/Export.cpp
PR 6361 core/libs/prx/libSceFiber/Export.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191766141 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6003#issuecomment-6191766141). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:13:06Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/Fiber.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libSceFiber/Export.cpp` | | [PR 6361](https://www.github.com/boykopovar/AnyPS5/pull/6361) | `core/libs/prx/libSceFiber/Export.cpp` |
XairRrR commented 2026-10-16 11:45:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-16T11:45:23Z.


Thanks for running it, Dextroid17.

Still needed on current main: Uncharted: Legacy of Thieves Collection (PPSA05684, #4001) built from unchanged main at 5d9dcc42d stalls during startup (its log stops and its fiber worker threads spin at 100% CPU), and it boots with this PR layered on the same commit. Linux, RTX 4070 Ti SUPER, driver 615.78.08.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6214782403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6003#issuecomment-6214782403). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-16T11:45:23Z. --- Thanks for running it, Dextroid17. Still needed on current `main`: Uncharted: Legacy of Thieves Collection (PPSA05684, #4001) built from unchanged `main` at 5d9dcc42d stalls during startup (its log stops and its fiber worker threads spin at 100% CPU), and it boots with this PR layered on the same commit. Linux, RTX 4070 Ti SUPER, driver 615.78.08. AI-assisted: yes
adriadam10 commented 2026-10-16 20:50:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T20:50:04Z.


Checked on Linux: build + ctest in scope 34/34 green; fiber fails with Export.cpp reverted to main and passes with the PR. The 64 KiB limit is recorded as unmeasured in docs/dev/debt/libSceFiber.md. Merging once the Windows build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6224436883 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6003#issuecomment-6224436883). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T20:50:04Z. --- Checked on Linux: build + ctest in scope 34/34 green; `fiber` fails with `Export.cpp` reverted to `main` and passes with the PR. The 64 KiB limit is recorded as unmeasured in `docs/dev/debt/libSceFiber.md`. Merging once the Windows build is green.
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!6003
No description provided.