Unity Game Fixes - common/kernel: deliver guest signals only at safe points #215

Closed
github-bot wants to merge 3 commits from gh-pr/215/head into gh-pr/215/base
Owner

@⁠StefanosCosta on GitHub · 2026-08-08 19:36 UTC · original
StefanosCosta:pr/guest-signal-safe-points → main · +376 −54 · 9 files · 3 commits · closed without merging

NOTE

These are commits from PR #156, compressed and cleaned up to be isolated from the rest of the changes that have either a) been committed or b) been identified as hacks/incomplete implementations.

These changes make Subnautica, Powerwash Simulator and cult of the lamb boot and enter in-game. I do not claim that these changes are objectively the correct implementation. This is just a PR separating commits from #156 so that they can be seen in the correct context and provide better insight as to what their purpose is. More details below:

Summary

A guest signal handler could run nested inside a host wait, or while the thread held an
emulator-global lock. Either one deadlocks the emulator: the handler blocks, the frame never
unwinds, and the state it holds is stranded. This leaves the signal queued in both cases and
dispatches it once the frame has unwound.

What's included

Guest signal delivery — libKernel.cpp, memory.cpp, graphicsRun.cpp

  • Leave a signal queued when it interrupted host code, and dispatch it from
    KernelDispatchPendingSignalForCurrentThread once the frame unwinds. Running IL2CPP's GC-suspend
    handler nested inside a host wait strands glibc's condvar group reference, after which every later
    broadcast on that variable blocks forever — the hard freeze on Subnautica's loading screen.
  • Declare the 33 g_memory_operation_mutex sites and the GPU submission lock as critical sections,
    and refuse to dispatch inside one. A handler that blocks while holding either stalls every other
    guest thread; both mutexes were confirmed owned by a single parked thread via __data.__owner.
  • Gate the dispatcher on one atomic load. It now runs on every wait and sleep exit, where the
    previous path did a read-modify-write per signal number.

Condition variables — threads.cpp, threads.h

  • Wake SignalThread's targets with the registry lock released. A broadcast can block until the
    waiters it is retiring have left, and they leave through UnregisterCondWaiter, which needs that
    same mutex — so the waker deadlocked against every waiter it was trying to wake. Signal() and
    SignalAll() already woke without the lock.
  • Hold registered waiters by shared_ptr. Between the collect and the wake, a waiter can return,
    unregister and have its owning CondVar destroyed, so the wake touched freed memory.
  • Add a dispatch point to CondVar::WaitFor. It had none, so a deferred signal sat undelivered for
    the whole of an arbitrarily long timeout.

Build and tests — CMakeLists.txt, tests/SignalSafetyTests.cpp

  • Add signal_safety_tests, covering both the deadlock and the use-after-free. The use-after-free
    oracle is ASan: built with -fsanitize=address it reports heap-use-after-free in WakeCondVar
    reached from SignalThread, on memory freed by ~CondVar, and is clean with the fix. A
    regression in the deadlock hangs the test rather than failing it, which is the honest shape for a
    deadlock.

Platform impact

  • Windows — changed, and not built or run here. Two pieces are excluded by existing guards: the
    CondVar::WaitFor dispatch point sits in the non-KYTY_WIN_CS arm, and the
    HostSignalDispatchHandler deferral is inside
    #if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS && defined(__x86_64__). Windows keeps its own APC
    path (SignalApcHandler / nt_queue_apc_thread_ex), untouched. Everything else compiles there:
    the SignalThread fix, the shared_ptr waiter lifetime, HleCriticalSection, the 33
    MemoryOperationLock sites, and the dispatcher's two new early returns.
  • macOS — rides the POSIX arm; compiles, not run.

Testing

Built on Linux (Clang, Ninja). ctest 27/27 including the new signal_safety.

**@⁠StefanosCosta** on GitHub · 2026-08-08 19:36 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/215) `StefanosCosta:pr/guest-signal-safe-points` → `main` · +376 −54 · 9 files · 3 commits · closed without merging ## NOTE These are commits from PR #156, compressed and cleaned up to be isolated from the rest of the changes that have either a) been committed or b) been identified as hacks/incomplete implementations. These changes make Subnautica, Powerwash Simulator and cult of the lamb boot and enter in-game. I do not claim that these changes are objectively the correct implementation. This is just a PR separating commits from #156 so that they can be seen in the correct context and provide better insight as to what their purpose is. More details below: ## Summary A guest signal handler could run nested inside a host wait, or while the thread held an emulator-global lock. Either one deadlocks the emulator: the handler blocks, the frame never unwinds, and the state it holds is stranded. This leaves the signal queued in both cases and dispatches it once the frame has unwound. ## What's included ### Guest signal delivery — `libKernel.cpp`, `memory.cpp`, `graphicsRun.cpp` - Leave a signal queued when it interrupted host code, and dispatch it from `KernelDispatchPendingSignalForCurrentThread` once the frame unwinds. Running IL2CPP's GC-suspend handler nested inside a host wait strands glibc's condvar group reference, after which every later broadcast on that variable blocks forever — the hard freeze on Subnautica's loading screen. - Declare the 33 `g_memory_operation_mutex` sites and the GPU submission lock as critical sections, and refuse to dispatch inside one. A handler that blocks while holding either stalls every other guest thread; both mutexes were confirmed owned by a single parked thread via `__data.__owner`. - Gate the dispatcher on one atomic load. It now runs on every wait and sleep exit, where the previous path did a read-modify-write per signal number. ### Condition variables — `threads.cpp`, `threads.h` - Wake `SignalThread`'s targets with the registry lock released. A broadcast can block until the waiters it is retiring have left, and they leave through `UnregisterCondWaiter`, which needs that same mutex — so the waker deadlocked against every waiter it was trying to wake. `Signal()` and `SignalAll()` already woke without the lock. - Hold registered waiters by `shared_ptr`. Between the collect and the wake, a waiter can return, unregister and have its owning `CondVar` destroyed, so the wake touched freed memory. - Add a dispatch point to `CondVar::WaitFor`. It had none, so a deferred signal sat undelivered for the whole of an arbitrarily long timeout. ### Build and tests — `CMakeLists.txt`, `tests/SignalSafetyTests.cpp` - Add `signal_safety_tests`, covering both the deadlock and the use-after-free. The use-after-free oracle is ASan: built with `-fsanitize=address` it reports `heap-use-after-free` in `WakeCondVar` reached from `SignalThread`, on memory freed by `~CondVar`, and is clean with the fix. A regression in the deadlock hangs the test rather than failing it, which is the honest shape for a deadlock. ## Platform impact - **Windows — changed, and not built or run here.** Two pieces are excluded by existing guards: the `CondVar::WaitFor` dispatch point sits in the non-`KYTY_WIN_CS` arm, and the `HostSignalDispatchHandler` deferral is inside `#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS && defined(__x86_64__)`. Windows keeps its own APC path (`SignalApcHandler` / `nt_queue_apc_thread_ex`), untouched. Everything else compiles there: the `SignalThread` fix, the `shared_ptr` waiter lifetime, `HleCriticalSection`, the 33 `MemoryOperationLock` sites, and the dispatcher's two new early returns. - **macOS** — rides the POSIX arm; compiles, not run. ## Testing Built on Linux (Clang, Ninja). `ctest` 27/27 including the new `signal_safety`.
SignalThread woke its targets while holding the waiter-registry mutex. Waking is
not guaranteed non-blocking -- a broadcast can block until the waiters it is
retiring have left the variable, and they leave through UnregisterCondWaiter,
which needs that same mutex -- so the waker deadlocked against every waiter it
was trying to wake. Collect the targets under the lock and wake with it
released, as Signal() and SignalAll() already do.

HleCriticalSection marks a scope holding an emulator-global lock. A guest
exception handler can block for an unbounded time -- IL2CPP's collector parks
every managed thread in one until the collection finishes -- so pending-signal
dispatch refuses to run a handler inside one and leaves the signal queued.
Running a guest signal handler inside a host wait deadlocked the emulator. The
handler runs nested inside the wait, and one that blocks -- IL2CPP's GC suspend
handler waits on an event flag until the collection ends -- never lets the wait
unwind. That strands the state the wait holds: glibc keeps a group reference on
a condition variable for the whole of pthread_cond_wait, and a stranded one
makes every later broadcast on that variable block forever, freezing every
thread using it.

The host signal handler leaves the signal queued when it interrupted host code
or while this thread holds an emulator-global lock, and
KernelDispatchPendingSignalForCurrentThread delivers it once the frame has
unwound. The 33 g_memory_operation_mutex sites and the GPU submission lock
declare themselves as critical sections.

PthreadHasAnyPendingSignal keeps the dispatcher's common no-signal path to a
single atomic load rather than one read-modify-write per signal number.
SignalThread collects its targets under the waiter-registry lock and wakes them
with it released, because waking while holding it deadlocks the waker against
every waiter it is retiring. That leaves a gap: between the collect and the wake
a waiter can return, unregister, and have its owning CondVar destroyed, so the
wake touches freed memory. The registry now holds a strong reference, which
keeps the private object alive across the gap.

signal_safety_tests covers both that and the deadlock it came from. The
use-after-free case is a stress loop whose oracle is ASan -- built with
-fsanitize=address it reports a heap-use-after-free in WakeCondVar reached from
SignalThread, on memory freed by ~CondVar, and is clean with the fix. In a normal
build it stands as a no-crash smoke test. A regression in the deadlock hangs
TestSignalThreadWakesAWaiter rather than failing it, which is the honest shape
for a deadlock.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:13:43 +00:00
github-bot closed this pull request 2026-11-26 09:13:43 +00:00
Author
Owner

@⁠svoboda18 on GitHub · 2026-08-09 02:20 UTC · original · approved these changes

**@⁠svoboda18** on GitHub · 2026-08-09 02:20 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/215#pullrequestreview-4890333110) · **approved these changes**
Author
Owner

@⁠Nmzik on GitHub · 2026-08-29 06:47 UTC · original

@⁠StefanosCosta Is it still needed? All the games are running successfully on Linux, and the main development is now happening on Linux. Powerwash Simulator is definitely goes in-game

**@⁠Nmzik** on GitHub · 2026-08-29 06:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/215#issuecomment-5460896158) @⁠StefanosCosta Is it still needed? All the games are running successfully on Linux, and the main development is now happening on Linux. Powerwash Simulator is definitely goes in-game
Author
Owner

@⁠StefanosCosta on GitHub · 2026-09-02 01:17 UTC · original

Hey @⁠Nmzik. I checked your question around 3 days ago with the main of that day. I couldn't load the games on ubuntu far enough into the game to test if this is still needed. Subnautica didn't even boot to menu, though I guess it could be a problem on my side because powerwash simulator didn't boot for me either. I made sure to pull all recent changes from main though and created a new build. I'm going to be taking care of a few personal things so i'm going to be away from github for a while. Hope all goes well.

**@⁠StefanosCosta** on GitHub · 2026-09-02 01:17 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/215#issuecomment-5502874461) Hey @⁠Nmzik. I checked your question around 3 days ago with the main of that day. I couldn't load the games on ubuntu far enough into the game to test if this is still needed. Subnautica didn't even boot to menu, though I guess it could be a problem on my side because powerwash simulator didn't boot for me either. I made sure to pull all recent changes from main though and created a new build. I'm going to be taking care of a few personal things so i'm going to be away from github for a while. Hope all goes well.
Author
Owner

@⁠Nmzik on GitHub · 2026-09-12 11:32 UTC · original

Should be good on Linux

**@⁠Nmzik** on GitHub · 2026-09-12 11:32 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/215#issuecomment-5645620567) Should be good on Linux

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.