Put a critical section around StepInto to make it thread-safe

This commit is contained in:
Duncan Ogilvie 2021-12-11 23:43:22 +01:00
parent 1a76d61ef6
commit fb1babcbb3
5 changed files with 63 additions and 79 deletions

View File

@ -40,6 +40,7 @@ LPVOID engineAttachedProcessDebugInfo = NULL;
wchar_t szDebuggerName[512]; wchar_t szDebuggerName[512];
bool DebugStepFinal = false; bool DebugStepFinal = false;
LPVOID StepOutCallBack = NULL; LPVOID StepOutCallBack = NULL;
CRITICAL_SECTION engineStepActiveCr;
// Global.Debugger.functions: // Global.Debugger.functions:
long DebugLoopInSecondThread(LPVOID InputParameter) long DebugLoopInSecondThread(LPVOID InputParameter)

View File

@ -2,6 +2,7 @@
#define _GLOBAL_DEBUGGER_H #define _GLOBAL_DEBUGGER_H
#include <vector> #include <vector>
#include <Windows.h>
extern HARDWARE_DATA DebugRegister[4]; extern HARDWARE_DATA DebugRegister[4];
extern PROCESS_INFORMATION dbgProcessInformation; extern PROCESS_INFORMATION dbgProcessInformation;
@ -39,6 +40,7 @@ extern LPVOID engineAttachedProcessDebugInfo;
extern wchar_t szDebuggerName[512]; extern wchar_t szDebuggerName[512];
extern bool DebugStepFinal; extern bool DebugStepFinal;
extern LPVOID StepOutCallBack; extern LPVOID StepOutCallBack;
extern CRITICAL_SECTION engineStepActiveCr;
long DebugLoopInSecondThread(LPVOID InputParameter); long DebugLoopInSecondThread(LPVOID InputParameter);
void DebuggerReset(); void DebuggerReset();

View File

@ -36,6 +36,9 @@ __declspec(dllexport) void TITCALL ForceClose()
} }
__declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack) __declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack)
{
EnterCriticalSection(&engineStepActiveCr);
if (!engineStepActive)
{ {
ULONG_PTR ueCurrentPosition = GetContextData(UE_CIP); ULONG_PTR ueCurrentPosition = GetContextData(UE_CIP);
unsigned char instr[16]; unsigned char instr[16];
@ -62,6 +65,8 @@ __declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack)
engineStepCount = 0; engineStepCount = 0;
} }
} }
LeaveCriticalSection(&engineStepActiveCr);
}
__declspec(dllexport) void TITCALL StepOver(LPVOID StepCallBack) __declspec(dllexport) void TITCALL StepOver(LPVOID StepCallBack)
{ {

View File

@ -12,6 +12,33 @@
#define UE_MODULEx86 0x2000; #define UE_MODULEx86 0x2000;
#define UE_MODULEx64 0x2000; #define UE_MODULEx64 0x2000;
static void engineStep()
{
EnterCriticalSection(&engineStepActiveCr);
if (engineStepActive)
{
DBGCode = DBG_CONTINUE;
if (engineStepCount == 0)
{
typedef void(TITCALL* fCustomBreakPoint)(void);
auto cbStep = fCustomBreakPoint(engineStepCallBack);
engineStepActive = false;
engineStepCallBack = NULL;
LeaveCriticalSection(&engineStepActiveCr);
cbStep();
}
else
{
SingleStep(engineStepCount, engineStepCallBack);
LeaveCriticalSection(&engineStepActiveCr);
}
}
else
{
LeaveCriticalSection(&engineStepActiveCr);
}
}
__declspec(dllexport) void TITCALL DebugLoop() __declspec(dllexport) void TITCALL DebugLoop()
{ {
bool FirstBPX = true; bool FirstBPX = true;
@ -642,20 +669,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
EnableBPX(ResetBPXAddressTo); EnableBPX(ResetBPXAddressTo);
ResetBPXAddressTo = NULL; ResetBPXAddressTo = NULL;
ResetBPX = false; ResetBPX = false;
if(engineStepActive) engineStep();
{
if(engineStepCount == 0)
{
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
engineStepActive = false;
engineStepCallBack = NULL;
myCustomBreakPoint();
}
else
{
SingleStep(engineStepCount, engineStepCallBack);
}
}
} }
else else
{ {
@ -671,20 +685,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
{ {
ResetHwBPX = false; ResetHwBPX = false;
SetHardwareBreakPoint(DebugRegisterX.DrxBreakAddress, DebugRegisterXId, DebugRegisterX.DrxBreakPointType, DebugRegisterX.DrxBreakPointSize, (LPVOID)DebugRegisterX.DrxCallBack); SetHardwareBreakPoint(DebugRegisterX.DrxBreakAddress, DebugRegisterXId, DebugRegisterX.DrxBreakPointType, DebugRegisterX.DrxBreakPointSize, (LPVOID)DebugRegisterX.DrxCallBack);
if(engineStepActive) engineStep();
{
if(engineStepCount == 0)
{
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
engineStepActive = false;
engineStepCallBack = NULL;
myCustomBreakPoint();
}
else
{
SingleStep(engineStepCount, engineStepCallBack);
}
}
} }
if(ResetMemBPX) //restore memory breakpoint if(ResetMemBPX) //restore memory breakpoint
{ {
@ -719,20 +720,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)ResetMemBPXAddress, ResetMemBPXSize, NewProtect, &OldProtect); VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)ResetMemBPXAddress, ResetMemBPXSize, NewProtect, &OldProtect);
} }
if(engineStepActive) engineStep();
{
if(engineStepCount == 0)
{
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
engineStepActive = false;
engineStepCallBack = NULL;
myCustomBreakPoint();
}
else
{
SingleStep(engineStepCount, engineStepCallBack);
}
}
} }
} }
else //no resetting needed (debugger reached hardware breakpoint or the user stepped) else //no resetting needed (debugger reached hardware breakpoint or the user stepped)
@ -867,21 +855,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
if(strstr(DisassembledString, "PUSHF")) if(strstr(DisassembledString, "PUSHF"))
PushfBPX = true; PushfBPX = true;
} }
if(engineStepActive) engineStep();
{
DBGCode = DBG_CONTINUE;
if(engineStepCount == 0)
{
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
engineStepActive = false;
engineStepCallBack = NULL;
myCustomBreakPoint();
}
else
{
SingleStep(engineStepCount, engineStepCallBack);
}
}
} }
if(DBGCode == DBG_EXCEPTION_NOT_HANDLED) //NOTE: only call the chSingleStep callback when the debuggee generated the exception if(DBGCode == DBG_EXCEPTION_NOT_HANDLED) //NOTE: only call the chSingleStep callback when the debuggee generated the exception
{ {

View File

@ -5,6 +5,7 @@
#include "Global.Injector.h" #include "Global.Injector.h"
#include "Global.Engine.Extension.h" #include "Global.Engine.Extension.h"
#include "Global.Engine.Threading.h" #include "Global.Engine.Threading.h"
#include "Global.Debugger.h"
// Global.Engine.Entry: // Global.Engine.Entry:
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
@ -13,6 +14,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
engineHandle = hinstDLL; engineHandle = hinstDLL;
InitializeCriticalSection(&engineStepActiveCr);
EngineInit(); EngineInit();
EmptyGarbage(); EmptyGarbage();
for(int i = 0; i < UE_MAX_RESERVED_MEMORY_LEFT; i++) for(int i = 0; i < UE_MAX_RESERVED_MEMORY_LEFT; i++)