kernel: thread cancellation crashes with STATUS_BAD_STACK (0xC0000028) on Windows #2015

Open
opened 2026-11-26 10:11:36 +00:00 by github-bot · 0 comments
Owner

@⁠lukanatroshvili67 on GitHub · 2026-10-31 11:35 UTC · original

Summary

On Windows, a guest thread that acts on a pending cancellation kills the whole process with 0xC0000028 (STATUS_BAD_STACK). The kernel_file_system test reproduces it every time. That test isn't part of the Windows CI run, so this hasn't shown up there.

Reproduction

Windows 11, VS 2022 Build Tools, clang-cl 19.1.5, main at 0261891:

kernel_file_system_tests.exe --mutex-only   ->  exit 0xC0000028, no output

With temporary trace prints in TestMutexHandoff, all three timed-waiter iterations pass, and the cancellation section gets through PthreadCancel and the second park. The process dies right after waiter.resume = 2, which is when the canceled waiter leaves its signal handler and reaches PthreadTestcancel().

What I think is happening

  1. PthreadCancel forwards to the host's pthread_cancel(thread->p) (L3400), and PthreadTestcancel forwards to the host's pthread_testcancel() (L3555). On Windows that's winpthread.
  2. The waiter runs on its guest stack via RunOnGuestStack. On Windows that function zeroes gs:0x08/gs:0x10 (the TEB StackBase/StackLimit, L896) for as long as the guest code runs.
  3. When winpthread acts on the cancellation, it terminates the thread by unwinding. As far as I know, mingw-w64 winpthreads' pthread_exit longjmps back to its thread wrapper, and x64 longjmp goes through RtlUnwindEx. With the stack pointer on the guest stack and the TEB bounds zeroed, the unwinder raises STATUS_BAD_STACK. I haven't confirmed this inside the bundled libwinpthread-1.dll, but it matches the exit code and the crash point.

PthreadExit already avoids this: when g_guest_entry_return_rsp is set, it returns to the RunOnGuestStack frame and restores the TEB fields itself. Cancellation never goes through that path.

Impact

Beyond the test, any title that cancels a thread blocked at a cancellation point would probably crash on Windows the same way. Normal scePthreadExit isn't affected. I haven't checked Linux or macOS; glibc's forced unwind behaves differently, so they may not see this.

Possible directions

  • Track cancellation requests in KytyPS5's own Pthread state, and have PthreadTestcancel (plus any other guest cancellation points) exit through PthreadExit(PTHREAD_CANCELED) instead of winpthread on Windows.
  • Or restore the host TEB stack bounds before any host code that may unwind runs on the guest stack.

Happy to test a fix on Windows (RTX 3060), or put together a PR if one of these directions sounds right.

**@⁠lukanatroshvili67** on GitHub · 2026-10-31 11:35 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/2015) ## Summary On Windows, a guest thread that acts on a pending cancellation kills the whole process with `0xC0000028` (`STATUS_BAD_STACK`). The `kernel_file_system` test reproduces it every time. That test isn't part of the Windows CI run, so this hasn't shown up there. ## Reproduction Windows 11, VS 2022 Build Tools, clang-cl 19.1.5, `main` at `0261891`: ``` kernel_file_system_tests.exe --mutex-only -> exit 0xC0000028, no output ``` With temporary trace prints in `TestMutexHandoff`, all three timed-waiter iterations pass, and the cancellation section gets through `PthreadCancel` and the second park. The process dies right after `waiter.resume = 2`, which is when the canceled waiter leaves its signal handler and reaches [`PthreadTestcancel()`](https://github.com/KytyPS5/KytyPS5/blob/026189166f71cc1995f5851dee1ddad7d5826e99/tests/KernelFileSystemTests.cpp#L147). ## What I think is happening 1. [`PthreadCancel`](https://github.com/KytyPS5/KytyPS5/blob/026189166f71cc1995f5851dee1ddad7d5826e99/src/kernel/pthread.cpp#L3388) forwards to the host's `pthread_cancel(thread->p)` ([L3400](https://github.com/KytyPS5/KytyPS5/blob/026189166f71cc1995f5851dee1ddad7d5826e99/src/kernel/pthread.cpp#L3400)), and [`PthreadTestcancel`](https://github.com/KytyPS5/KytyPS5/blob/026189166f71cc1995f5851dee1ddad7d5826e99/src/kernel/pthread.cpp#L3552) forwards to the host's `pthread_testcancel()` ([L3555](https://github.com/KytyPS5/KytyPS5/blob/026189166f71cc1995f5851dee1ddad7d5826e99/src/kernel/pthread.cpp#L3555)). On Windows that's winpthread. 2. The waiter runs on its guest stack via [`RunOnGuestStack`](https://github.com/KytyPS5/KytyPS5/blob/026189166f71cc1995f5851dee1ddad7d5826e99/src/kernel/pthread.cpp#L820). On Windows that function zeroes `gs:0x08`/`gs:0x10` (the TEB StackBase/StackLimit, [L896](https://github.com/KytyPS5/KytyPS5/blob/026189166f71cc1995f5851dee1ddad7d5826e99/src/kernel/pthread.cpp#L896)) for as long as the guest code runs. 3. When winpthread acts on the cancellation, it terminates the thread by unwinding. As far as I know, mingw-w64 winpthreads' `pthread_exit` longjmps back to its thread wrapper, and x64 `longjmp` goes through `RtlUnwindEx`. With the stack pointer on the guest stack and the TEB bounds zeroed, the unwinder raises `STATUS_BAD_STACK`. I haven't confirmed this inside the bundled `libwinpthread-1.dll`, but it matches the exit code and the crash point. [`PthreadExit`](https://github.com/KytyPS5/KytyPS5/blob/026189166f71cc1995f5851dee1ddad7d5826e99/src/kernel/pthread.cpp#L3558) already avoids this: when `g_guest_entry_return_rsp` is set, it returns to the `RunOnGuestStack` frame and restores the TEB fields itself. Cancellation never goes through that path. ## Impact Beyond the test, any title that cancels a thread blocked at a cancellation point would probably crash on Windows the same way. Normal `scePthreadExit` isn't affected. I haven't checked Linux or macOS; glibc's forced unwind behaves differently, so they may not see this. ## Possible directions - Track cancellation requests in KytyPS5's own `Pthread` state, and have `PthreadTestcancel` (plus any other guest cancellation points) exit through `PthreadExit(PTHREAD_CANCELED)` instead of winpthread on Windows. - Or restore the host TEB stack bounds before any host code that may unwind runs on the guest stack. Happy to test a fix on Windows (RTX 3060), or put together a PR if one of these directions sounds right.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:11:36 +00:00
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/KytyPS5--KytyPS5#2015
No description provided.