From 8072f96a260e9fff99af6be107eaf1699acb55e6 Mon Sep 17 00:00:00 2001 From: AzuLX Date: Mon, 5 Jan 2026 15:47:03 +0000 Subject: [PATCH] fix multi-thread breakpoint deletion race condition --- .../TitanEngine.Debugger.DebugLoop.cpp | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp b/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp index 51942cb..5ebfb09 100644 --- a/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp +++ b/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp @@ -589,11 +589,36 @@ __declspec(dllexport) void TITCALL DebugLoop() { if(DebugAttachedToProcess || !FirstBPX) //program generated a breakpoint exception { - DBGCode = DBG_EXCEPTION_NOT_HANDLED; - if(DBGCustomHandler->chBreakPoint != NULL) + ULONG_PTR exceptionAddress = (ULONG_PTR)DBGEvent.u.Exception.ExceptionRecord.ExceptionAddress; + unsigned char currentByte = 0xCC; + MemoryReadSafe(dbgProcessInformation.hProcess, (void*)exceptionAddress, ¤tByte, 1, nullptr); + + if(currentByte != 0xCC) { - myCustomHandler = (fCustomHandler)((LPVOID)DBGCustomHandler->chBreakPoint); - myCustomHandler(&DBGEvent.u.Exception.ExceptionRecord); + //breakpoint was deleted - the byte is no longer 0xCC + //reset IP to exception address and continue gracefully + DBGCode = DBG_CONTINUE; + hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId); + CONTEXT myDBGContext; + myDBGContext.ContextFlags = ContextControlFlags; + GetThreadContext(hActiveThread, &myDBGContext); +#if defined(_WIN64) + myDBGContext.Rip = exceptionAddress; +#else + myDBGContext.Eip = (DWORD)exceptionAddress; +#endif + SetThreadContext(hActiveThread, &myDBGContext); + EngineCloseHandle(hActiveThread); + } + else + { + //byte is still 0xCC - this is a real int3 in the original code!! + DBGCode = DBG_EXCEPTION_NOT_HANDLED; + if(DBGCustomHandler->chBreakPoint != NULL) + { + myCustomHandler = (fCustomHandler)((LPVOID)DBGCustomHandler->chBreakPoint); + myCustomHandler(&DBGEvent.u.Exception.ExceptionRecord); + } } } else //system breakpoint