guest_raise_short_sleep: unhandled single step at Aps5RedirectedEntryStub on Windows #24358

Closed
opened 2026-11-20 15:06:35 +00:00 by einfachama · 0 comments
einfachama commented 2026-11-20 15:06:35 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by einfachama on 2026-11-20T15:06:35Z.


What happens

guest_raise_short_sleep and guest_raise_short_sleep_without_wake stop after 3 to 53 delivered raises, without a check message. Almost every run ends with exit code 0x80000004 (STATUS_SINGLE_STEP). The single-step exception that ends the process is raised at Aps5RedirectedEntryStub+0 with the debug registers already cleared, so ReturnBreakHandler passes it on.

Title, test or build step

ctest guest_raise_short_sleep and guest_raise_short_sleep_without_wake (core/libs/tests/GuestRaiseShortSleep.cpp, registered in core/libs/prx/libkernel/CMakeLists.txt:3166)

AnyPS5 commit

Reproduced at main 0a025bd3c6, no pull requests on top. core/libs/prx/libkernel/System/src/Exception.cpp and the test are unchanged at 7fa48cd1c6; the line numbers below are from there.

System

Windows 11 Pro 26H2 (10.0.26300), AMD Ryzen 7 5800X, MSYS2 UCRT64 GCC 16.2.0-7 (SEH), Release. GPU: NVIDIA GeForce RTX 4060, driver 621.24; the test uses no GPU.

Steps to reproduce

ctest --test-dir build -R "^guest_raise_short_sleep" --output-on-failure --timeout 120

Each test stops after 1 to 4 s.

Log

[src/Thread.cpp:964 RunThread] RunThread entry=0x7ff65d6d15b0 arg=0000000000000000 self=0000000001235a70
[exception] guest context signal=0x1e rip=0x00007ffeb044faff ...
[exception] guest context signal=0x1e rip=0x00007ffeb044fdff ...
(a few more of these)

Exit code: 0x80000004.

A last-chance vectored exception handler, added to a local copy of the test, logs the exception the process ends with. All 4 runs logged the same values:

unhandled EXCEPTION_SINGLE_STEP: ExceptionAddress = Rip = Aps5RedirectedEntryStub+0
Dr0 = 0, Dr6 = 0, Dr7 = 0, EFlags = 0x204 (trap flag clear)
Rip saved in the Delivery that Redirect copied to the stack: Napper+0x1b

Napper+0x1b is the instruction after call sceKernelUsleep_nid_postfix (GuestRaiseShortSleep.cpp:23). That is the guest return address ArmReturnBreak sets Dr0 to while the napper sleeps in host code.

What was checked

How the logged values fit core/libs/prx/libkernel/System/src/Exception.cpp:

  1. A raise that finds the napper in host code arms a return break: TryDeliver (:727) → ArmReturnBreak (:693) → SetReturnBreak (:667) sets Dr0 to the guest return address and enables it in Dr7.
  2. The napper returns and the breakpoint fires at that address. Before the exception reaches user mode, the arm is retired: once the delivery counts as done, DisarmReturnBreak (:846, :888) → RetireReturnBreak (:684) → SetReturnBreak(false) clears Dr0 and Dr7.
  3. The next raise's TryDeliver suspends the napper, reads Rip = the return address and classifies it as guest code. It then calls Redirect (:771), which sets Rip to Aps5RedirectedEntryStub (:441). The check at :762 only covers the arm of the same raise (arm->entry != nullptr && delivery.context.Rip == arm->at). A new raise has no arm yet.
  4. The pending debug exception is dispatched with the redirected context. Dr6 has no hit bit, so ReturnBreakHandler (:631) calls DisarmedReturnBreak. That accepts only a Rip armed on the thread (:603, ArmedOnThread :585), and the stub never is. The handler returns EXCEPTION_CONTINUE_SEARCH and the process ends.

Shown: the exception address and Rip (the stub), the cleared Dr0, Dr6 and Dr7, and that the redirected thread stood at the armed return address. Inferred: the order in step 2, that the breakpoint fired and the arm was retired before the user-mode dispatch. I have not traced the kernel side.

  • 20 of 20 runs fail when not elevated, 10 per variant: 19 with 0x80000004 and 1 with 0xc0000409. In an elevated full -j16 run, guest_raise_short_sleep failed the same way and guest_raise_short_sleep_without_wake passed once. Elevation is not needed for the failure.
  • The 0xc0000409 run was not examined further; I have not shown it is the same race.
  • CI: the WinLibs windows job of the main push build passes both tests in about 0.05 s each (run 44738270262). The Windows MSYS2 nightly passes them, except guest_raise_short_sleep_without_wake with 0xc0000409 on 11-19 (run 44487917040).
  • Why this host hits the window every run and the runners do not is not settled. Candidates are the OS build (10.0.26300), the CPU and timing.
  • No bisect: the test came with #12135 (03722a1570), and libkernel and libc changed in 297 files since.

Proposed change

In TryDeliver, do not redirect a thread whose Rip is a return address armed on that thread (ArmedOnThread(GetThreadId(native), Rip), not only the current raise's arm->at). Treat it like host code and retry. The pending debug exception is then raised at the armed address, where DisarmedReturnBreak already continues it. The change is a few lines, and this test reproduces the failure on every run on the host above.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6514045547 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/24358). Originally posted by [einfachama](https://github.com/einfachama) on 2026-11-20T15:06:35Z. --- ### What happens `guest_raise_short_sleep` and `guest_raise_short_sleep_without_wake` stop after 3 to 53 delivered raises, without a check message. Almost every run ends with exit code `0x80000004` (`STATUS_SINGLE_STEP`). The single-step exception that ends the process is raised at `Aps5RedirectedEntryStub+0` with the debug registers already cleared, so `ReturnBreakHandler` passes it on. ### Title, test or build step ctest `guest_raise_short_sleep` and `guest_raise_short_sleep_without_wake` (`core/libs/tests/GuestRaiseShortSleep.cpp`, registered in `core/libs/prx/libkernel/CMakeLists.txt:3166`) ### AnyPS5 commit Reproduced at `main` 0a025bd3c6, no pull requests on top. `core/libs/prx/libkernel/System/src/Exception.cpp` and the test are unchanged at 7fa48cd1c6; the line numbers below are from there. ### System Windows 11 Pro 26H2 (10.0.26300), AMD Ryzen 7 5800X, MSYS2 UCRT64 GCC 16.2.0-7 (SEH), Release. GPU: NVIDIA GeForce RTX 4060, driver 621.24; the test uses no GPU. ### Steps to reproduce ``` ctest --test-dir build -R "^guest_raise_short_sleep" --output-on-failure --timeout 120 ``` Each test stops after 1 to 4 s. ### Log ``` [src/Thread.cpp:964 RunThread] RunThread entry=0x7ff65d6d15b0 arg=0000000000000000 self=0000000001235a70 [exception] guest context signal=0x1e rip=0x00007ffeb044faff ... [exception] guest context signal=0x1e rip=0x00007ffeb044fdff ... (a few more of these) ``` Exit code: `0x80000004`. A last-chance vectored exception handler, added to a local copy of the test, logs the exception the process ends with. All 4 runs logged the same values: ``` unhandled EXCEPTION_SINGLE_STEP: ExceptionAddress = Rip = Aps5RedirectedEntryStub+0 Dr0 = 0, Dr6 = 0, Dr7 = 0, EFlags = 0x204 (trap flag clear) Rip saved in the Delivery that Redirect copied to the stack: Napper+0x1b ``` `Napper+0x1b` is the instruction after `call sceKernelUsleep_nid_postfix` (`GuestRaiseShortSleep.cpp:23`). That is the guest return address `ArmReturnBreak` sets `Dr0` to while the napper sleeps in host code. ### What was checked How the logged values fit `core/libs/prx/libkernel/System/src/Exception.cpp`: 1. A raise that finds the napper in host code arms a return break: `TryDeliver` (:727) → `ArmReturnBreak` (:693) → `SetReturnBreak` (:667) sets `Dr0` to the guest return address and enables it in `Dr7`. 2. The napper returns and the breakpoint fires at that address. Before the exception reaches user mode, the arm is retired: once the delivery counts as done, `DisarmReturnBreak` (:846, :888) → `RetireReturnBreak` (:684) → `SetReturnBreak(false)` clears `Dr0` and `Dr7`. 3. The next raise's `TryDeliver` suspends the napper, reads `Rip` = the return address and classifies it as guest code. It then calls `Redirect` (:771), which sets `Rip` to `Aps5RedirectedEntryStub` (:441). The check at :762 only covers the arm of the same raise (`arm->entry != nullptr && delivery.context.Rip == arm->at`). A new raise has no arm yet. 4. The pending debug exception is dispatched with the redirected context. `Dr6` has no hit bit, so `ReturnBreakHandler` (:631) calls `DisarmedReturnBreak`. That accepts only a `Rip` armed on the thread (:603, `ArmedOnThread` :585), and the stub never is. The handler returns `EXCEPTION_CONTINUE_SEARCH` and the process ends. Shown: the exception address and `Rip` (the stub), the cleared `Dr0`, `Dr6` and `Dr7`, and that the redirected thread stood at the armed return address. Inferred: the order in step 2, that the breakpoint fired and the arm was retired before the user-mode dispatch. I have not traced the kernel side. - 20 of 20 runs fail when not elevated, 10 per variant: 19 with `0x80000004` and 1 with `0xc0000409`. In an elevated full `-j16` run, `guest_raise_short_sleep` failed the same way and `guest_raise_short_sleep_without_wake` passed once. Elevation is not needed for the failure. - The `0xc0000409` run was not examined further; I have not shown it is the same race. - CI: the WinLibs `windows` job of the `main` push build passes both tests in about 0.05 s each ([run 44738270262](https://github.com/boykopovar/AnyPS5/actions/runs/44738270262)). The Windows MSYS2 nightly passes them, except `guest_raise_short_sleep_without_wake` with `0xc0000409` on 11-19 ([run 44487917040](https://github.com/boykopovar/AnyPS5/actions/runs/44487917040)). - Why this host hits the window every run and the runners do not is not settled. Candidates are the OS build (10.0.26300), the CPU and timing. - No bisect: the test came with #12135 (03722a1570), and libkernel and libc changed in 297 files since. ### Proposed change In `TryDeliver`, do not redirect a thread whose `Rip` is a return address armed on that thread (`ArmedOnThread(GetThreadId(native), Rip)`, not only the current raise's `arm->at`). Treat it like host code and retry. The pending debug exception is then raised at the armed address, where `DisarmedReturnBreak` already continues it. The change is a few lines, and this test reproduces the failure on every run on the host above. AI-assisted: yes
Sign in to join this conversation.
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#24358
No description provided.