From 8401e3b8da137b3faeae83fed1e5cc6fa30326bc Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Tue, 14 Jul 2026 22:11:01 +0200 Subject: [PATCH] Fix bug in pause command where hActiveThread and dwThreadId can race --- src/dbg/commands/cmd-debug-control.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/dbg/commands/cmd-debug-control.cpp b/src/dbg/commands/cmd-debug-control.cpp index 5d220eca3..45a3f067e 100644 --- a/src/dbg/commands/cmd-debug-control.cpp +++ b/src/dbg/commands/cmd-debug-control.cpp @@ -414,22 +414,27 @@ bool cbDebugPause(int argc, char* argv[]) dputs(QT_TRANSLATE_NOOP("DBG", "Program is not running")); return false; } + // NOTE: As soon as SetBPX plants the INT3, another thread can hit it and the + // breakpoint callback will reassign the global hActiveThread. Operate on a local + // copy so the SuspendThread/ResumeThread pair always targets the same thread. + HANDLE hPauseThread = hActiveThread; + DWORD dwPauseThreadId = GetDebugData()->dwThreadId; // TODO: get suspend count instead, this can be detected // Interesting behavior found by JustMagic, if the active thread is suspended pause would fail - auto previousSuspendCount = SuspendThread(hActiveThread); + auto previousSuspendCount = SuspendThread(hPauseThread); if(previousSuspendCount != 0) { if(previousSuspendCount != -1) - ResumeThread(hActiveThread); + ResumeThread(hPauseThread); dputs(QT_TRANSLATE_NOOP("DBG", "The active thread is suspended, switch to a running thread to pause the process")); // TODO: perhaps inject an INT3 in the process as an alternative to failing? return false; } - duint CIP = GetContextDataEx(hActiveThread, UE_CIP); + duint CIP = GetContextDataEx(hPauseThread, UE_CIP); if(!SetBPX(CIP, UE_BREAKPOINT, cbPauseBreakpoint)) { dprintf(QT_TRANSLATE_NOOP("DBG", "Error setting breakpoint at %p! (SetBPX)\n"), CIP); - if(ResumeThread(hActiveThread) == -1) + if(ResumeThread(hPauseThread) == -1) { dputs(QT_TRANSLATE_NOOP("DBG", "Error resuming thread")); return false; @@ -439,8 +444,8 @@ bool cbDebugPause(int argc, char* argv[]) //WORKAROUND: If a program is stuck in NtUserGetMessage (GetMessage was called), this //will send a WM_NULL to stop the waiting. This only works if the message is not filtered. //OllyDbg also does this in a similar way. - PostThreadMessageA(GetDebugData()->dwThreadId, WM_NULL, 0, 0); - if(ResumeThread(hActiveThread) == -1) + PostThreadMessageA(dwPauseThreadId, WM_NULL, 0, 0); + if(ResumeThread(hPauseThread) == -1) { dputs(QT_TRANSLATE_NOOP("DBG", "Error resuming thread")); return false;