kernel: give small fibers their own guest stacks (Pumpkin Jack) #2188
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2188
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2188"
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?
@Nawid3333 on GitHub · 2026-11-09 17:37 UTC · original
Nawid3333:fix/fiber-small-context-stack→main· +677 −29 · 4 files · 2 commits · openCause
In #2162, Pumpkin Jack runs
Psr::buildManyBottomLevelBvhfibers with 6144-byte contexts. Kyty executes directly on those contexts, including its host code. Lab measurements found Windows exception dispatch for a fault Kyty intentionally handles needs about 4.5 KB belowrsp, and a log line about 2 KB. This overwrote game data below the context; the game crashed during startup logos reading a host-DLL-range value from there.Change
Contexts below 64 KiB get a Kyty-allocated guest stack: context size + 64 KiB, rounded to 16 KiB. It is allocated on first run, retained while suspended, and released only after execution leaves it. Initialize and Finalize wait while a fiber is
RETURNING;RECLAIMINGgives one caller the release.A stack is released only through the fiber object that owns it, so a copy of a fiber object cannot unmap the original's stack. Windows TEB
StackBase/StackLimitfollow each switch.PthreadExitinside a fiber runs thread destructors there before leaving it; other threads retain their existing behavior.sceFiberGetInfostill reports the supplied context. Contexts of at least 64 KiB still execute on it.Tests
virtual_memory_allocation_testsadds:FiberExceptionStack: WindowsRaiseExceptionon 512/6144-byte fibers, with canaries below the context.FiberHostStack: host calls, suspension, migration, stack reclamation, and Windows stack bounds.FiberSwitchReturnStack: a returning fiber releases its stack while the suspended caller retains its stack and resumes.FiberStackExtents: the owned stack is the context size + 64 KiB rounded to 16 KiB, a 64 KiB context still runs on itself, and initializing a copy of a suspended fiber leaves the original's stack mapped.FiberOwnStackFp: two fibers on their own stacks start with the console's FP environment (f399d4818), keep their own FP controls across a switch, and leave the thread's controls alone. It fails if the restore path dropsldmxcsr/fldcw.Main (
f399d4818) with these tests failsFiberExceptionStack: “exception dispatch wrote below the supplied fiber context”. With the fix,--fiber-onlyand the full suite pass on Windows 11 with clang-cl.CI on this head (
ad7a6f77e): Windows, Linux and macOS build and pass the tests: https://github.com/KytyPS5/KytyPS5/actions/runs/42669455321.No runtime test covers
PthreadExitinside a fiber.Costs
A small fiber keeps a mapping of context size + 64 KiB, rounded to 16 KiB (80 KiB for both 512- and 6144-byte contexts), from its first run until its entry returns,
PthreadExitleaves it, or it is finalized or initialized again. A suspended fiber dropped without Finalize keeps its stack. There is no guard page, unlike the test build in #2162. Small fibers execute outsideaddr_context; guest code comparing stack addresses with its context would notice.Overlaps
83d96d56dit clears the TEB bounds while a fiber runs; this PR saves and restores the bounds of each fiber's own stack on every context switch, which replaces that step when both are merged.In the game
Ekt0re ran the Windows build of this PR's CI run at its head
ad7a6f77e(run) on an RTX 4070 Ti (i7-13700K, Windows 11): Pumpkin Jack loads the menus and gets into the game without a crash, and the log reaches VS 143 / PS 310 / CS 77 / GS 18 shaders. With main58ca8d1(their own build, marked-dirty) on the same PC the game crashes before the menu, after VS 13 / PS 18 / CS 20 shaders, with a guest fault on theRHIThreadthread, a read of address0xc(comment).Before that, they ran build f75f420 (main
7816ac790+ an earlier version of this change, which still had a 16 KiB guard page below each stack) on a laptop with a Vega 8 iGPU, with two local changes so it builds and starts there (mesh draws dropped, thesizeof(Inst)assert raised). The crash at the logos was gone there too; the game compiled up to VS 63 / PS 104 / CS 56 shaders and then lost the device on that iGPU: #2162.The crash needs a handled host exception on one of these fibers, so it does not show on every PC; nmzik reports the game working fine in #2162.
Written with Claude Code (AI): it found the cause, wrote the change and tests, and ran them. Ekt0re tested the game.
@coderabbitai[bot] on GitHub · 2026-11-09 17:46 UTC · original · edited 2026-11-09 18:04 UTC
Walkthrough
Fiber contexts now track stack bounds and can acquire owned stacks for small contexts. Fiber initialization, switching, return, and finalization manage these stacks through added lifecycle states. Fiber thread exit runs destructors before restoring the thread context. New tests cover stack behavior, floating-point state, Windows exception stack bounds, and fiber return chains.
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Moderate · up to
ad7a6Stack allocation failure can terminate the emulator or cause an invalid stack write. Handle both paths before merging.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 30 functions across 4 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
@coderabbitai[bot] on GitHub · 2026-11-09 17:46 UTC · original · edited 2026-11-09 18:04 UTC
On
tests/VirtualMemoryAllocationTests.cppline 6148 (outdated):Check the result of
FiberInitializeon the running fiber.This block calls
FiberInitializeon a fiber that the worker still runs, and it discards the return value.FiberClaimForReclaimmust reject aRUNNINGfiber withFIBER_ERROR_STATE. The test already checks the same contract forbusy_runandbusy_finalize. Without a check, a regression that allows reinitialization goes undetected here. That regression would also corrupt the fiber while it is running, and the later failure would be hard to diagnose. Capture the result and assertFiberErrorStatewith the other ownership checks.Proposed fix
Then extend the existing ownership check:
✅ Addressed in commit
ad7a6f7@coderabbitai[bot] on GitHub · 2026-11-09 17:46 UTC · original · reviewed
Actionable comments posted: 1
9cd86915-4452-4539-a6db-61f01a7f056dReviewing files that changed from the base of the PR and between
f399d48183and016458937d.src/kernel/pthread.cppsrc/kernel/pthread.hsrc/libs/libKernel.cpptests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@Nawid3333 on GitHub · 2026-11-09 17:56 UTC · original
On
tests/VirtualMemoryAllocationTests.cppline 6148 (outdated):Fixed in
ad7a6f77e: the result is kept asbusy_initializeand checked to beFiberErrorStatetogether withbusy_runandbusy_finalize. WithFiberInitializechanged to ignore the failed claim, the test binary crashes, because the running fiber gets reinitialized under the worker.@coderabbitai[bot] on GitHub · 2026-11-09 18:03 UTC · original · reviewed
9ab81f8d-4ba3-49c0-b74c-a89bb6e7590aReviewing files that changed from the base of the PR and between
016458937dandad7a6f77e3.tests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-09 18:04 UTC · original
On
tests/VirtualMemoryAllocationTests.cppline 6148 (outdated):@Nawid3333 Thanks for the fix and the mutation test. The test now checks that
FiberInitializereturnsFiberErrorStatefor the running fiber.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
Review thread resolved.
You are interacting with an AI system.
@Nawid3333 on GitHub · 2026-11-09 18:22 UTC · original
@coderabbitai on the two outside-diff notes about
EXIT_IF(!stack_acquired)inFiberRunandFiberSwitch: no build configuration definesKYTY_FINAL(it is only a#cmakedefineinsrc/cmake_config.h.in, with no CMake option or CI setting behind it), so the check always stops the emulator with the file and line. The fiber error codes end at0x80590006(SCE_FIBER_ERROR_STATE) and none of them means out of memory, so returning one would give the game a wrong reason. Failing to map an 80 KiB stack is handled like other host allocation failures in Kyty, so I'm leaving these as they are.@coderabbitai[bot] on GitHub · 2026-11-09 18:23 UTC · original
Analysis chain
Script executed:
Length of output: 14031
@Nawid3333Thanks for the explanation. I found no checked-in build setting that definesKYTY_FINAL, and the declared fiber error codes do not include an out-of-memory error. I accept your decision to keep both checks as they are.You are interacting with an AI system.
@Ekt0re on GitHub · 2026-11-12 19:23 UTC · original
@Nawid3333 Hi, I personally tested the fix on my own PC with an RTX 4070 Ti.
The fix version:
(https://github.com/KytyPS5/KytyPS5/actions/runs/42669455321.)
The game works perfectly—it loads the menus and is in game.
There are no crashes.
Log:
The current main version (KytyPS5-2026-11-12-58ca8d1):
The game fails to launch and crashes with an error.
The game does not reach the menu or enter gameplay.
Log:
Hardware:
OS
Windows 11 Pro, 26H2
CPU
13th Gen Intel(R) Core(TM) i7-13700K (3.40 GHz)
GPU
NVIDIA GeForce RTX 4070 Ti
RAM / VRAM
32 GB DDR5 / 12 GB VRAM
@Nawid3333 on GitHub · 2026-11-12 19:40 UTC · original
@Ekt0re Thanks, that is the test this pull request needed: on the same PC, main crashes before the menu and this change gets into the game. I've added it to the description.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.