mirror of https://github.com/x64dbg/TitanEngine
critical section locker
This commit is contained in:
parent
aa8e991f08
commit
51bf507216
|
|
@ -113,7 +113,7 @@ void uintdr7(ULONG_PTR dr7, DR7* ret)
|
||||||
|
|
||||||
void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer, SIZE_T nSize)
|
void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer, SIZE_T nSize)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
ULONG_PTR start=lpBaseAddress;
|
ULONG_PTR start=lpBaseAddress;
|
||||||
ULONG_PTR end=start+nSize;
|
ULONG_PTR end=start+nSize;
|
||||||
int bpcount=BreakPointBuffer.size();
|
int bpcount=BreakPointBuffer.size();
|
||||||
|
|
@ -124,7 +124,7 @@ void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer,
|
||||||
if(curBp->BreakPointActive != UE_BPXACTIVE || (curBp->BreakPointType != UE_BREAKPOINT && curBp->BreakPointType != UE_SINGLESHOOT))
|
if(curBp->BreakPointActive != UE_BPXACTIVE || (curBp->BreakPointType != UE_BREAKPOINT && curBp->BreakPointType != UE_SINGLESHOOT))
|
||||||
continue;
|
continue;
|
||||||
ULONG_PTR cur_addr=curBp->BreakPointAddress;
|
ULONG_PTR cur_addr=curBp->BreakPointAddress;
|
||||||
for(int j=0; j<curBp->BreakPointSize; j++)
|
for(SIZE_T j=0; j<curBp->BreakPointSize; j++)
|
||||||
{
|
{
|
||||||
if(cur_addr+j==start && cur_addr+j<end) //breakpoint is in range
|
if(cur_addr+j==start && cur_addr+j<end) //breakpoint is in range
|
||||||
{
|
{
|
||||||
|
|
@ -140,7 +140,7 @@ void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, MutexLocker* lock)
|
void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSectionLocker* lock)
|
||||||
{
|
{
|
||||||
ULONG_PTR start=lpBaseAddress;
|
ULONG_PTR start=lpBaseAddress;
|
||||||
ULONG_PTR end=start+nSize;
|
ULONG_PTR end=start+nSize;
|
||||||
|
|
@ -152,7 +152,7 @@ void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, MutexLocker
|
||||||
if(curBp->BreakPointActive != UE_BPXACTIVE || (curBp->BreakPointType != UE_BREAKPOINT && curBp->BreakPointType != UE_SINGLESHOOT))
|
if(curBp->BreakPointActive != UE_BPXACTIVE || (curBp->BreakPointType != UE_BREAKPOINT && curBp->BreakPointType != UE_SINGLESHOOT))
|
||||||
continue;
|
continue;
|
||||||
ULONG_PTR cur_addr=curBp->BreakPointAddress;
|
ULONG_PTR cur_addr=curBp->BreakPointAddress;
|
||||||
for(int j=0; j<curBp->BreakPointSize; j++)
|
for(SIZE_T j=0; j<curBp->BreakPointSize; j++)
|
||||||
{
|
{
|
||||||
if(cur_addr+j==start && cur_addr+j<end) //breakpoint byte is in range
|
if(cur_addr+j==start && cur_addr+j<end) //breakpoint byte is in range
|
||||||
{
|
{
|
||||||
|
|
@ -166,7 +166,7 @@ void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, MutexLocker
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, MutexLocker* lock)
|
void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSectionLocker* lock)
|
||||||
{
|
{
|
||||||
ULONG_PTR start=lpBaseAddress;
|
ULONG_PTR start=lpBaseAddress;
|
||||||
ULONG_PTR end=start+nSize;
|
ULONG_PTR end=start+nSize;
|
||||||
|
|
@ -178,7 +178,7 @@ void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, MutexLocke
|
||||||
if(curBp->BreakPointActive != UE_BPXACTIVE || (curBp->BreakPointType != UE_BREAKPOINT && curBp->BreakPointType != UE_SINGLESHOOT))
|
if(curBp->BreakPointActive != UE_BPXACTIVE || (curBp->BreakPointType != UE_BREAKPOINT && curBp->BreakPointType != UE_SINGLESHOOT))
|
||||||
continue;
|
continue;
|
||||||
ULONG_PTR cur_addr=curBp->BreakPointAddress;
|
ULONG_PTR cur_addr=curBp->BreakPointAddress;
|
||||||
for(int j=0; j<curBp->BreakPointSize; j++)
|
for(SIZE_T j=0; j<curBp->BreakPointSize; j++)
|
||||||
{
|
{
|
||||||
if(cur_addr+j==start && cur_addr+j<end) //breakpoint byte is in range
|
if(cur_addr+j==start && cur_addr+j<end) //breakpoint byte is in range
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ extern std::vector<BreakPointDetail> BreakPointBuffer;
|
||||||
void uintdr7(ULONG_PTR dr7, DR7* ret);
|
void uintdr7(ULONG_PTR dr7, DR7* ret);
|
||||||
ULONG_PTR dr7uint(DR7* dr7);
|
ULONG_PTR dr7uint(DR7* dr7);
|
||||||
void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer, SIZE_T nSize);
|
void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer, SIZE_T nSize);
|
||||||
void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, MutexLocker* lock);
|
void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSectionLocker* lock);
|
||||||
void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, MutexLocker* lock);
|
void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSectionLocker* lock);
|
||||||
|
|
||||||
#endif //_GLOBAL_BREAKPOINTS_H
|
#endif //_GLOBAL_BREAKPOINTS_H
|
||||||
|
|
|
||||||
|
|
@ -2,35 +2,37 @@
|
||||||
#include "definitions.h"
|
#include "definitions.h"
|
||||||
#include "Global.Engine.Threading.h"
|
#include "Global.Engine.Threading.h"
|
||||||
|
|
||||||
MutexLocker::MutexLocker(const char* name)
|
static CRITICAL_SECTION locks[LockLast];
|
||||||
|
|
||||||
|
void CriticalSectionInitializeLocks()
|
||||||
{
|
{
|
||||||
int len=strlen(name);
|
for(int i=0; i<LockLast; i++)
|
||||||
DynBuf newNameBuf(len+20);
|
InitializeCriticalSection(&locks[i]);
|
||||||
char* newName = (char*)newNameBuf.GetPtr();
|
|
||||||
sprintf(newName, "Local\\%s%X", name, GetCurrentProcessId());
|
|
||||||
gMutex=CreateMutexA(0, true, newName);
|
|
||||||
bUnlocked=false;
|
|
||||||
WaitForSingleObject(gMutex, INFINITE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MutexLocker::~MutexLocker()
|
void CriticalSectionDeleteLocks()
|
||||||
{
|
{
|
||||||
if(!bUnlocked)
|
for(int i=0; i<LockLast; i++)
|
||||||
ReleaseMutex(gMutex);
|
DeleteCriticalSection(&locks[i]);
|
||||||
CloseHandle(gMutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MutexLocker::relock()
|
CriticalSectionLocker::CriticalSectionLocker(CriticalSectionLock lock)
|
||||||
{
|
{
|
||||||
if(bUnlocked)
|
gCriticalSection=&locks[lock];
|
||||||
{
|
EnterCriticalSection(gCriticalSection);
|
||||||
bUnlocked=false;
|
|
||||||
WaitForSingleObject(gMutex, INFINITE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MutexLocker::unlock()
|
CriticalSectionLocker::~CriticalSectionLocker()
|
||||||
{
|
{
|
||||||
ReleaseMutex(gMutex);
|
LeaveCriticalSection(gCriticalSection);
|
||||||
bUnlocked=true;
|
}
|
||||||
|
|
||||||
|
void CriticalSectionLocker::unlock()
|
||||||
|
{
|
||||||
|
LeaveCriticalSection(gCriticalSection);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CriticalSectionLocker::relock()
|
||||||
|
{
|
||||||
|
EnterCriticalSection(gCriticalSection);
|
||||||
}
|
}
|
||||||
|
|
@ -1,16 +1,25 @@
|
||||||
#ifndef _GLOBAL_ENGINE_THREADING_H
|
#ifndef _GLOBAL_ENGINE_THREADING_H
|
||||||
#define _GLOBAL_ENGINE_THREADING_H
|
#define _GLOBAL_ENGINE_THREADING_H
|
||||||
|
|
||||||
class MutexLocker
|
enum CriticalSectionLock
|
||||||
|
{
|
||||||
|
LockBreakPointBuffer,
|
||||||
|
LockLast
|
||||||
|
};
|
||||||
|
|
||||||
|
void CriticalSectionInitializeLocks();
|
||||||
|
void CriticalSectionDeleteLocks();
|
||||||
|
|
||||||
|
class CriticalSectionLocker
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MutexLocker(const char* name);
|
CriticalSectionLocker(CriticalSectionLock lock);
|
||||||
~MutexLocker();
|
~CriticalSectionLocker();
|
||||||
void relock();
|
|
||||||
void unlock();
|
void unlock();
|
||||||
|
void relock();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HANDLE gMutex;
|
LPCRITICAL_SECTION gCriticalSection;
|
||||||
bool bUnlocked;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //_GLOBAL_ENGINE_THREADING_H
|
#endif //_GLOBAL_ENGINE_THREADING_H
|
||||||
|
|
@ -25,7 +25,7 @@ __declspec(dllexport) void TITCALL SetBPXOptions(long DefaultBreakPointType)
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL IsBPXEnabled(ULONG_PTR bpxAddress)
|
__declspec(dllexport) bool TITCALL IsBPXEnabled(ULONG_PTR bpxAddress)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
ULONG_PTR NumberOfBytesReadWritten = 0;
|
ULONG_PTR NumberOfBytesReadWritten = 0;
|
||||||
DWORD MaximumBreakPoints = 0;
|
DWORD MaximumBreakPoints = 0;
|
||||||
BYTE ReadData[10] = {};
|
BYTE ReadData[10] = {};
|
||||||
|
|
@ -59,7 +59,7 @@ __declspec(dllexport) bool TITCALL IsBPXEnabled(ULONG_PTR bpxAddress)
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL EnableBPX(ULONG_PTR bpxAddress)
|
__declspec(dllexport) bool TITCALL EnableBPX(ULONG_PTR bpxAddress)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
MEMORY_BASIC_INFORMATION MemInfo;
|
MEMORY_BASIC_INFORMATION MemInfo;
|
||||||
ULONG_PTR NumberOfBytesReadWritten = 0;
|
ULONG_PTR NumberOfBytesReadWritten = 0;
|
||||||
DWORD MaximumBreakPoints = 0;
|
DWORD MaximumBreakPoints = 0;
|
||||||
|
|
@ -129,7 +129,7 @@ __declspec(dllexport) bool TITCALL EnableBPX(ULONG_PTR bpxAddress)
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL DisableBPX(ULONG_PTR bpxAddress)
|
__declspec(dllexport) bool TITCALL DisableBPX(ULONG_PTR bpxAddress)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
MEMORY_BASIC_INFORMATION MemInfo;
|
MEMORY_BASIC_INFORMATION MemInfo;
|
||||||
ULONG_PTR NumberOfBytesReadWritten = 0;
|
ULONG_PTR NumberOfBytesReadWritten = 0;
|
||||||
DWORD MaximumBreakPoints = 0;
|
DWORD MaximumBreakPoints = 0;
|
||||||
|
|
@ -168,10 +168,9 @@ __declspec(dllexport) bool TITCALL DisableBPX(ULONG_PTR bpxAddress)
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL SetBPX(ULONG_PTR bpxAddress, DWORD bpxType, LPVOID bpxCallBack)
|
__declspec(dllexport) bool TITCALL SetBPX(ULONG_PTR bpxAddress, DWORD bpxType, LPVOID bpxCallBack)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
void* bpxDataPrt;
|
void* bpxDataPrt;
|
||||||
PMEMORY_COMPARE_HANDLER bpxDataCmpPtr;
|
PMEMORY_COMPARE_HANDLER bpxDataCmpPtr;
|
||||||
MEMORY_BASIC_INFORMATION MemInfo;
|
|
||||||
ULONG_PTR NumberOfBytesReadWritten = 0;
|
ULONG_PTR NumberOfBytesReadWritten = 0;
|
||||||
BYTE SelectedBreakPointType;
|
BYTE SelectedBreakPointType;
|
||||||
DWORD checkBpxType;
|
DWORD checkBpxType;
|
||||||
|
|
@ -269,7 +268,7 @@ __declspec(dllexport) bool TITCALL SetBPX(ULONG_PTR bpxAddress, DWORD bpxType, L
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL DeleteBPX(ULONG_PTR bpxAddress)
|
__declspec(dllexport) bool TITCALL DeleteBPX(ULONG_PTR bpxAddress)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
ULONG_PTR NumberOfBytesReadWritten = 0;
|
ULONG_PTR NumberOfBytesReadWritten = 0;
|
||||||
DWORD OldProtect;
|
DWORD OldProtect;
|
||||||
int bpcount=BreakPointBuffer.size();
|
int bpcount=BreakPointBuffer.size();
|
||||||
|
|
@ -435,7 +434,7 @@ __declspec(dllexport) bool TITCALL SetMemoryBPX(ULONG_PTR MemoryStart, SIZE_T Si
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL SetMemoryBPXEx(ULONG_PTR MemoryStart, SIZE_T SizeOfMemory, DWORD BreakPointType, bool RestoreOnHit, LPVOID bpxCallBack)
|
__declspec(dllexport) bool TITCALL SetMemoryBPXEx(ULONG_PTR MemoryStart, SIZE_T SizeOfMemory, DWORD BreakPointType, bool RestoreOnHit, LPVOID bpxCallBack)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
MEMORY_BASIC_INFORMATION MemInfo;
|
MEMORY_BASIC_INFORMATION MemInfo;
|
||||||
ULONG_PTR NumberOfBytesReadWritten = 0;
|
ULONG_PTR NumberOfBytesReadWritten = 0;
|
||||||
DWORD NewProtect = 0;
|
DWORD NewProtect = 0;
|
||||||
|
|
@ -476,7 +475,7 @@ __declspec(dllexport) bool TITCALL SetMemoryBPXEx(ULONG_PTR MemoryStart, SIZE_T
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL RemoveMemoryBPX(ULONG_PTR MemoryStart, SIZE_T SizeOfMemory)
|
__declspec(dllexport) bool TITCALL RemoveMemoryBPX(ULONG_PTR MemoryStart, SIZE_T SizeOfMemory)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
MEMORY_BASIC_INFORMATION MemInfo;
|
MEMORY_BASIC_INFORMATION MemInfo;
|
||||||
ULONG_PTR NumberOfBytesReadWritten = 0;
|
ULONG_PTR NumberOfBytesReadWritten = 0;
|
||||||
DWORD NewProtect = 0;
|
DWORD NewProtect = 0;
|
||||||
|
|
@ -786,7 +785,7 @@ __declspec(dllexport) bool TITCALL DeleteHardwareBreakPoint(DWORD IndexOfRegiste
|
||||||
|
|
||||||
__declspec(dllexport) bool TITCALL RemoveAllBreakPoints(DWORD RemoveOption)
|
__declspec(dllexport) bool TITCALL RemoveAllBreakPoints(DWORD RemoveOption)
|
||||||
{
|
{
|
||||||
MutexLocker lock("BreakPointBuffer");
|
CriticalSectionLocker lock(LockBreakPointBuffer);
|
||||||
int bpcount=BreakPointBuffer.size();
|
int bpcount=BreakPointBuffer.size();
|
||||||
if(RemoveOption == UE_OPTION_REMOVEALL)
|
if(RemoveOption == UE_OPTION_REMOVEALL)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -387,7 +387,7 @@ __declspec(dllexport) bool TITCALL MemoryWriteSafe(HANDLE hProcess, LPVOID lpBas
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
MutexLocker lock("BreakPointBuffer"); //thread-safe
|
CriticalSectionLocker lock(LockBreakPointBuffer); //thread-safe
|
||||||
//disable breakpoints that interfere with the memory to write
|
//disable breakpoints that interfere with the memory to write
|
||||||
BreakPointPreWriteFilter((ULONG_PTR)lpBaseAddress, nSize, &lock);
|
BreakPointPreWriteFilter((ULONG_PTR)lpBaseAddress, nSize, &lock);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ __declspec(dllexport) long TITCALL GetPE32SectionNumberFromVA(ULONG_PTR FileMapV
|
||||||
{
|
{
|
||||||
__try
|
__try
|
||||||
{
|
{
|
||||||
ULONG_PTR ConvertAddress = AddressToConvert - PEHeader64->OptionalHeader.ImageBase;
|
ULONG_PTR ConvertAddress = AddressToConvert - (ULONG_PTR)PEHeader64->OptionalHeader.ImageBase;
|
||||||
PIMAGE_SECTION_HEADER PESections = IMAGE_FIRST_SECTION(PEHeader64);
|
PIMAGE_SECTION_HEADER PESections = IMAGE_FIRST_SECTION(PEHeader64);
|
||||||
DWORD SectionNumber = PEHeader64->FileHeader.NumberOfSections;
|
DWORD SectionNumber = PEHeader64->FileHeader.NumberOfSections;
|
||||||
DWORD FoundInSection = -1;
|
DWORD FoundInSection = -1;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include "Global.Garbage.h"
|
#include "Global.Garbage.h"
|
||||||
#include "Global.Injector.h"
|
#include "Global.Injector.h"
|
||||||
#include "Global.Engine.Extension.h"
|
#include "Global.Engine.Extension.h"
|
||||||
|
#include "Global.Engine.Threading.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)
|
||||||
|
|
@ -11,6 +12,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
||||||
switch(fdwReason)
|
switch(fdwReason)
|
||||||
{
|
{
|
||||||
case DLL_PROCESS_ATTACH:
|
case DLL_PROCESS_ATTACH:
|
||||||
|
CriticalSectionInitializeLocks(); //initialize critical sections
|
||||||
engineHandle=hinstDLL;
|
engineHandle=hinstDLL;
|
||||||
EngineInit();
|
EngineInit();
|
||||||
EmptyGarbage();
|
EmptyGarbage();
|
||||||
|
|
@ -23,6 +25,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
||||||
if(lpvReserved)
|
if(lpvReserved)
|
||||||
ExtensionManagerPluginReleaseCallBack();
|
ExtensionManagerPluginReleaseCallBack();
|
||||||
RemoveDirectoryW(engineSzEngineGarbageFolder);
|
RemoveDirectoryW(engineSzEngineGarbageFolder);
|
||||||
|
CriticalSectionDeleteLocks(); //delete critical sections
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue