mirror of https://github.com/x64dbg/TitanEngine
- added mutex locker thread
- thread-safe TitanEngine.Debugger.Context
This commit is contained in:
parent
d4265de1e4
commit
f65ee470fb
|
|
@ -0,0 +1,30 @@
|
||||||
|
#include "stdafx.h"
|
||||||
|
#include "definitions.h"
|
||||||
|
#include "Global.Engine.Threading.h"
|
||||||
|
|
||||||
|
MutexLocker::MutexLocker(const char* name)
|
||||||
|
{
|
||||||
|
gMutex=CreateMutexA(0, false, name);
|
||||||
|
bUnlocked=false;
|
||||||
|
WaitForSingleObject(gMutex, INFINITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
MutexLocker::~MutexLocker()
|
||||||
|
{
|
||||||
|
ReleaseMutex(gMutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MutexLocker::relock()
|
||||||
|
{
|
||||||
|
if(bUnlocked)
|
||||||
|
{
|
||||||
|
bUnlocked=false;
|
||||||
|
WaitForSingleObject(gMutex, INFINITE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MutexLocker::unlock()
|
||||||
|
{
|
||||||
|
ReleaseMutex(gMutex);
|
||||||
|
bUnlocked=true;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef _GLOBAL_ENGINE_THREADING_H
|
||||||
|
#define _GLOBAL_ENGINE_THREADING_H
|
||||||
|
|
||||||
|
class MutexLocker
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MutexLocker(const char* name);
|
||||||
|
~MutexLocker();
|
||||||
|
void relock();
|
||||||
|
void unlock();
|
||||||
|
private:
|
||||||
|
HANDLE gMutex;
|
||||||
|
bool bUnlocked;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //_GLOBAL_ENGINE_THREADING_H
|
||||||
|
|
@ -3,36 +3,36 @@
|
||||||
#include "Global.Debugger.h"
|
#include "Global.Debugger.h"
|
||||||
#include "Global.Engine.h"
|
#include "Global.Engine.h"
|
||||||
#include "Global.Handle.h"
|
#include "Global.Handle.h"
|
||||||
|
#include "Global.Engine.Threading.h"
|
||||||
|
|
||||||
static CONTEXT DBGContext = {};
|
static CONTEXT DBGContext = {};
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL GetContextFPUDataEx(HANDLE hActiveThread, void* FPUSaveArea)
|
__declspec(dllexport) bool TITCALL GetContextFPUDataEx(HANDLE hActiveThread, void* FPUSaveArea)
|
||||||
{
|
{
|
||||||
|
MutexLocker locker("DBGContext"); //lock DBGContext
|
||||||
if(FPUSaveArea != NULL)
|
if(FPUSaveArea)
|
||||||
{
|
{
|
||||||
RtlZeroMemory(&DBGContext, sizeof CONTEXT);
|
RtlZeroMemory(&DBGContext, sizeof CONTEXT);
|
||||||
DBGContext.ContextFlags = CONTEXT_ALL;
|
DBGContext.ContextFlags = CONTEXT_ALL;
|
||||||
if(!GetThreadContext(hActiveThread, &DBGContext))
|
if(!GetThreadContext(hActiveThread, &DBGContext))
|
||||||
return(false);
|
return false;
|
||||||
#if !defined (_WIN64)
|
#ifndef _WIN64
|
||||||
RtlMoveMemory(FPUSaveArea, &DBGContext.FloatSave, sizeof FLOATING_SAVE_AREA);
|
RtlMoveMemory(FPUSaveArea, &DBGContext.FloatSave, sizeof FLOATING_SAVE_AREA);
|
||||||
#else
|
#else
|
||||||
RtlMoveMemory(FPUSaveArea, &DBGContext.FltSave, sizeof XMM_SAVE_AREA32);
|
RtlMoveMemory(FPUSaveArea, &DBGContext.FltSave, sizeof XMM_SAVE_AREA32);
|
||||||
#endif
|
#endif
|
||||||
return(true);
|
return true;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return(false);
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
__declspec(dllexport) long long TITCALL GetContextDataEx(HANDLE hActiveThread, DWORD IndexOfRegister)
|
__declspec(dllexport) long long TITCALL GetContextDataEx(HANDLE hActiveThread, DWORD IndexOfRegister)
|
||||||
{
|
{
|
||||||
|
MutexLocker locker("DBGContext"); //lock DBGContext
|
||||||
RtlZeroMemory(&DBGContext, sizeof CONTEXT);
|
RtlZeroMemory(&DBGContext, sizeof CONTEXT);
|
||||||
DBGContext.ContextFlags = CONTEXT_ALL;
|
DBGContext.ContextFlags = CONTEXT_ALL;
|
||||||
#if defined(_WIN64)
|
|
||||||
GetThreadContext(hActiveThread, &DBGContext);
|
GetThreadContext(hActiveThread, &DBGContext);
|
||||||
|
#ifdef _WIN64
|
||||||
if(IndexOfRegister == UE_EAX)
|
if(IndexOfRegister == UE_EAX)
|
||||||
{
|
{
|
||||||
return((DWORD)DBGContext.Rax);
|
return((DWORD)DBGContext.Rax);
|
||||||
|
|
@ -202,7 +202,6 @@ __declspec(dllexport) long long TITCALL GetContextDataEx(HANDLE hActiveThread, D
|
||||||
return(DBGContext.SegSs);
|
return(DBGContext.SegSs);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
GetThreadContext(hActiveThread, &DBGContext);
|
|
||||||
if(IndexOfRegister == UE_EAX)
|
if(IndexOfRegister == UE_EAX)
|
||||||
{
|
{
|
||||||
return(DBGContext.Eax);
|
return(DBGContext.Eax);
|
||||||
|
|
@ -300,51 +299,47 @@ __declspec(dllexport) long long TITCALL GetContextDataEx(HANDLE hActiveThread, D
|
||||||
return(DBGContext.SegSs);
|
return(DBGContext.SegSs);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return(NULL);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
__declspec(dllexport) long long TITCALL GetContextData(DWORD IndexOfRegister)
|
__declspec(dllexport) long long TITCALL GetContextData(DWORD IndexOfRegister)
|
||||||
{
|
{
|
||||||
|
MutexLocker locker("DBGContext"); //lock DBGContext
|
||||||
HANDLE hActiveThread = 0;
|
HANDLE hActiveThread = OpenThread(THREAD_GET_CONTEXT, false, DBGEvent.dwThreadId);
|
||||||
long long ContextReturn;
|
long long ContextReturn = GetContextDataEx(hActiveThread, IndexOfRegister);
|
||||||
|
|
||||||
hActiveThread = OpenThread(THREAD_GET_CONTEXT|THREAD_SET_CONTEXT|THREAD_QUERY_INFORMATION, false, DBGEvent.dwThreadId);
|
|
||||||
ContextReturn = GetContextDataEx(hActiveThread, IndexOfRegister);
|
|
||||||
EngineCloseHandle(hActiveThread);
|
EngineCloseHandle(hActiveThread);
|
||||||
return(ContextReturn);
|
return(ContextReturn);
|
||||||
}
|
}
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL SetContextFPUDataEx(HANDLE hActiveThread, void* FPUSaveArea)
|
__declspec(dllexport) bool TITCALL SetContextFPUDataEx(HANDLE hActiveThread, void* FPUSaveArea)
|
||||||
{
|
{
|
||||||
|
MutexLocker locker("DBGContext"); //lock DBGContext
|
||||||
if(FPUSaveArea != NULL)
|
if(FPUSaveArea)
|
||||||
{
|
{
|
||||||
RtlZeroMemory(&DBGContext, sizeof CONTEXT);
|
RtlZeroMemory(&DBGContext, sizeof CONTEXT);
|
||||||
DBGContext.ContextFlags = CONTEXT_ALL;
|
DBGContext.ContextFlags = CONTEXT_ALL;
|
||||||
if(!GetThreadContext(hActiveThread, &DBGContext))
|
if(!GetThreadContext(hActiveThread, &DBGContext))
|
||||||
return(false);
|
return(false);
|
||||||
#if !defined (_WIN64)
|
#ifndef _WIN64
|
||||||
RtlMoveMemory(&DBGContext.FloatSave, FPUSaveArea, sizeof FLOATING_SAVE_AREA);
|
RtlMoveMemory(&DBGContext.FloatSave, FPUSaveArea, sizeof FLOATING_SAVE_AREA);
|
||||||
#else
|
#else
|
||||||
RtlMoveMemory(&DBGContext.FltSave, FPUSaveArea, sizeof XMM_SAVE_AREA32);
|
RtlMoveMemory(&DBGContext.FltSave, FPUSaveArea, sizeof XMM_SAVE_AREA32);
|
||||||
#endif
|
#endif
|
||||||
if(SetThreadContext(hActiveThread, &DBGContext))
|
if(SetThreadContext(hActiveThread, &DBGContext))
|
||||||
{
|
return true;
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return(false);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL SetContextDataEx(HANDLE hActiveThread, DWORD IndexOfRegister, ULONG_PTR NewRegisterValue)
|
__declspec(dllexport) bool TITCALL SetContextDataEx(HANDLE hActiveThread, DWORD IndexOfRegister, ULONG_PTR NewRegisterValue)
|
||||||
{
|
{
|
||||||
SuspendThread(hActiveThread);
|
MutexLocker locker("DBGContext"); //lock DBGContext
|
||||||
RtlZeroMemory(&DBGContext, sizeof CONTEXT);
|
RtlZeroMemory(&DBGContext, sizeof CONTEXT);
|
||||||
DBGContext.ContextFlags = CONTEXT_ALL;
|
DBGContext.ContextFlags = CONTEXT_ALL;
|
||||||
#ifdef _WIN64
|
|
||||||
if(!GetThreadContext(hActiveThread, &DBGContext))
|
if(!GetThreadContext(hActiveThread, &DBGContext))
|
||||||
{
|
return false;
|
||||||
ResumeThread(hActiveThread);
|
SuspendThread(hActiveThread);
|
||||||
return(false);
|
#ifdef _WIN64
|
||||||
}
|
|
||||||
if(IndexOfRegister == UE_EAX)
|
if(IndexOfRegister == UE_EAX)
|
||||||
{
|
{
|
||||||
NewRegisterValue = DBGContext.Rax - (DWORD)DBGContext.Rax + NewRegisterValue;
|
NewRegisterValue = DBGContext.Rax - (DWORD)DBGContext.Rax + NewRegisterValue;
|
||||||
|
|
@ -522,22 +517,7 @@ __declspec(dllexport) bool TITCALL SetContextDataEx(HANDLE hActiveThread, DWORD
|
||||||
{
|
{
|
||||||
DBGContext.SegSs = (WORD)NewRegisterValue;
|
DBGContext.SegSs = (WORD)NewRegisterValue;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
ResumeThread(hActiveThread);
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
if(SetThreadContext(hActiveThread, &DBGContext))
|
|
||||||
{
|
|
||||||
ResumeThread(hActiveThread);
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
if(!GetThreadContext(hActiveThread, &DBGContext))
|
|
||||||
{
|
|
||||||
ResumeThread(hActiveThread);
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
if(IndexOfRegister == UE_EAX)
|
if(IndexOfRegister == UE_EAX)
|
||||||
{
|
{
|
||||||
DBGContext.Eax = NewRegisterValue;
|
DBGContext.Eax = NewRegisterValue;
|
||||||
|
|
@ -634,28 +614,26 @@ __declspec(dllexport) bool TITCALL SetContextDataEx(HANDLE hActiveThread, DWORD
|
||||||
{
|
{
|
||||||
DBGContext.SegSs = NewRegisterValue;
|
DBGContext.SegSs = NewRegisterValue;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ResumeThread(hActiveThread);
|
ResumeThread(hActiveThread);
|
||||||
return(false);
|
return false;
|
||||||
}
|
}
|
||||||
if(SetThreadContext(hActiveThread, &DBGContext))
|
if(SetThreadContext(hActiveThread, &DBGContext))
|
||||||
{
|
{
|
||||||
ResumeThread(hActiveThread);
|
ResumeThread(hActiveThread);
|
||||||
return(true);
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
ResumeThread(hActiveThread);
|
ResumeThread(hActiveThread);
|
||||||
return(false);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL SetContextData(DWORD IndexOfRegister, ULONG_PTR NewRegisterValue)
|
__declspec(dllexport) bool TITCALL SetContextData(DWORD IndexOfRegister, ULONG_PTR NewRegisterValue)
|
||||||
{
|
{
|
||||||
|
MutexLocker locker("DBGContext"); //lock DBGContext
|
||||||
HANDLE hActiveThread = 0;
|
HANDLE hActiveThread = OpenThread(THREAD_SUSPEND_RESUME|THREAD_SET_CONTEXT|THREAD_GET_CONTEXT, false, DBGEvent.dwThreadId);
|
||||||
bool ContextReturn;
|
bool ContextReturn = SetContextDataEx(hActiveThread, IndexOfRegister, NewRegisterValue);
|
||||||
|
|
||||||
hActiveThread = OpenThread(THREAD_ALL_ACCESS, false, DBGEvent.dwThreadId);
|
|
||||||
ContextReturn = SetContextDataEx(hActiveThread, IndexOfRegister, NewRegisterValue);
|
|
||||||
EngineCloseHandle(hActiveThread);
|
EngineCloseHandle(hActiveThread);
|
||||||
return(ContextReturn);
|
return(ContextReturn);
|
||||||
}
|
}
|
||||||
|
|
@ -223,6 +223,7 @@
|
||||||
<ClCompile Include="Global.Engine.Hider.cpp" />
|
<ClCompile Include="Global.Engine.Hider.cpp" />
|
||||||
<ClCompile Include="Global.Engine.Hook.cpp" />
|
<ClCompile Include="Global.Engine.Hook.cpp" />
|
||||||
<ClCompile Include="Global.Engine.Simplification.cpp" />
|
<ClCompile Include="Global.Engine.Simplification.cpp" />
|
||||||
|
<ClCompile Include="Global.Engine.Threading.cpp" />
|
||||||
<ClCompile Include="Global.Garbage.cpp" />
|
<ClCompile Include="Global.Garbage.cpp" />
|
||||||
<ClCompile Include="Global.Handle.cpp" />
|
<ClCompile Include="Global.Handle.cpp" />
|
||||||
<ClCompile Include="Global.Injector.cpp" />
|
<ClCompile Include="Global.Injector.cpp" />
|
||||||
|
|
@ -284,6 +285,7 @@
|
||||||
<ClInclude Include="Global.Engine.Hider.h" />
|
<ClInclude Include="Global.Engine.Hider.h" />
|
||||||
<ClInclude Include="Global.Engine.Hook.h" />
|
<ClInclude Include="Global.Engine.Hook.h" />
|
||||||
<ClInclude Include="Global.Engine.Simplification.h" />
|
<ClInclude Include="Global.Engine.Simplification.h" />
|
||||||
|
<ClInclude Include="Global.Engine.Threading.h" />
|
||||||
<ClInclude Include="Global.Garbage.h" />
|
<ClInclude Include="Global.Garbage.h" />
|
||||||
<ClInclude Include="Global.Handle.h" />
|
<ClInclude Include="Global.Handle.h" />
|
||||||
<ClInclude Include="Global.Injector.h" />
|
<ClInclude Include="Global.Injector.h" />
|
||||||
|
|
|
||||||
|
|
@ -198,6 +198,9 @@
|
||||||
<ClCompile Include="Global.Garbage.cpp">
|
<ClCompile Include="Global.Garbage.cpp">
|
||||||
<Filter>Source Files\TitanEngine</Filter>
|
<Filter>Source Files\TitanEngine</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Global.Engine.Threading.cpp">
|
||||||
|
<Filter>Source Files\TitanEngine</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="resource.h">
|
<ClInclude Include="resource.h">
|
||||||
|
|
@ -278,6 +281,9 @@
|
||||||
<ClInclude Include="Global.Garbage.h">
|
<ClInclude Include="Global.Garbage.h">
|
||||||
<Filter>Header Files\TitanEngine</Filter>
|
<Filter>Header Files\TitanEngine</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Global.Engine.Threading.h">
|
||||||
|
<Filter>Header Files\TitanEngine</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ResourceCompile Include="TitanEngine.rc">
|
<ResourceCompile Include="TitanEngine.rc">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue