critical section locker

This commit is contained in:
Mr. eXoDia 2014-05-18 01:07:09 +02:00
parent aa8e991f08
commit 51bf507216
8 changed files with 59 additions and 46 deletions

View File

@ -113,7 +113,7 @@ void uintdr7(ULONG_PTR dr7, DR7* ret)
void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer, SIZE_T nSize)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
ULONG_PTR start=lpBaseAddress;
ULONG_PTR end=start+nSize;
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))
continue;
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
{
@ -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 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))
continue;
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
{
@ -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 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))
continue;
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
{

View File

@ -9,7 +9,7 @@ extern std::vector<BreakPointDetail> BreakPointBuffer;
void uintdr7(ULONG_PTR dr7, DR7* ret);
ULONG_PTR dr7uint(DR7* dr7);
void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer, SIZE_T nSize);
void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, MutexLocker* lock);
void BreakPointPostWriteFilter(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, CriticalSectionLocker* lock);
#endif //_GLOBAL_BREAKPOINTS_H

View File

@ -2,35 +2,37 @@
#include "definitions.h"
#include "Global.Engine.Threading.h"
MutexLocker::MutexLocker(const char* name)
static CRITICAL_SECTION locks[LockLast];
void CriticalSectionInitializeLocks()
{
int len=strlen(name);
DynBuf newNameBuf(len+20);
char* newName = (char*)newNameBuf.GetPtr();
sprintf(newName, "Local\\%s%X", name, GetCurrentProcessId());
gMutex=CreateMutexA(0, true, newName);
bUnlocked=false;
WaitForSingleObject(gMutex, INFINITE);
for(int i=0; i<LockLast; i++)
InitializeCriticalSection(&locks[i]);
}
MutexLocker::~MutexLocker()
void CriticalSectionDeleteLocks()
{
if(!bUnlocked)
ReleaseMutex(gMutex);
CloseHandle(gMutex);
for(int i=0; i<LockLast; i++)
DeleteCriticalSection(&locks[i]);
}
void MutexLocker::relock()
CriticalSectionLocker::CriticalSectionLocker(CriticalSectionLock lock)
{
if(bUnlocked)
{
bUnlocked=false;
WaitForSingleObject(gMutex, INFINITE);
}
gCriticalSection=&locks[lock];
EnterCriticalSection(gCriticalSection);
}
void MutexLocker::unlock()
CriticalSectionLocker::~CriticalSectionLocker()
{
ReleaseMutex(gMutex);
bUnlocked=true;
LeaveCriticalSection(gCriticalSection);
}
void CriticalSectionLocker::unlock()
{
LeaveCriticalSection(gCriticalSection);
}
void CriticalSectionLocker::relock()
{
EnterCriticalSection(gCriticalSection);
}

View File

@ -1,16 +1,25 @@
#ifndef _GLOBAL_ENGINE_THREADING_H
#define _GLOBAL_ENGINE_THREADING_H
class MutexLocker
enum CriticalSectionLock
{
LockBreakPointBuffer,
LockLast
};
void CriticalSectionInitializeLocks();
void CriticalSectionDeleteLocks();
class CriticalSectionLocker
{
public:
MutexLocker(const char* name);
~MutexLocker();
void relock();
CriticalSectionLocker(CriticalSectionLock lock);
~CriticalSectionLocker();
void unlock();
void relock();
private:
HANDLE gMutex;
bool bUnlocked;
LPCRITICAL_SECTION gCriticalSection;
};
#endif //_GLOBAL_ENGINE_THREADING_H

View File

@ -25,7 +25,7 @@ __declspec(dllexport) void TITCALL SetBPXOptions(long DefaultBreakPointType)
__declspec(dllexport) bool TITCALL IsBPXEnabled(ULONG_PTR bpxAddress)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
ULONG_PTR NumberOfBytesReadWritten = 0;
DWORD MaximumBreakPoints = 0;
BYTE ReadData[10] = {};
@ -59,7 +59,7 @@ __declspec(dllexport) bool TITCALL IsBPXEnabled(ULONG_PTR bpxAddress)
__declspec(dllexport) bool TITCALL EnableBPX(ULONG_PTR bpxAddress)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
MEMORY_BASIC_INFORMATION MemInfo;
ULONG_PTR NumberOfBytesReadWritten = 0;
DWORD MaximumBreakPoints = 0;
@ -129,7 +129,7 @@ __declspec(dllexport) bool TITCALL EnableBPX(ULONG_PTR bpxAddress)
__declspec(dllexport) bool TITCALL DisableBPX(ULONG_PTR bpxAddress)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
MEMORY_BASIC_INFORMATION MemInfo;
ULONG_PTR NumberOfBytesReadWritten = 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)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
void* bpxDataPrt;
PMEMORY_COMPARE_HANDLER bpxDataCmpPtr;
MEMORY_BASIC_INFORMATION MemInfo;
ULONG_PTR NumberOfBytesReadWritten = 0;
BYTE SelectedBreakPointType;
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)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
ULONG_PTR NumberOfBytesReadWritten = 0;
DWORD OldProtect;
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)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
MEMORY_BASIC_INFORMATION MemInfo;
ULONG_PTR NumberOfBytesReadWritten = 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)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
MEMORY_BASIC_INFORMATION MemInfo;
ULONG_PTR NumberOfBytesReadWritten = 0;
DWORD NewProtect = 0;
@ -786,7 +785,7 @@ __declspec(dllexport) bool TITCALL DeleteHardwareBreakPoint(DWORD IndexOfRegiste
__declspec(dllexport) bool TITCALL RemoveAllBreakPoints(DWORD RemoveOption)
{
MutexLocker lock("BreakPointBuffer");
CriticalSectionLocker lock(LockBreakPointBuffer);
int bpcount=BreakPointBuffer.size();
if(RemoveOption == UE_OPTION_REMOVEALL)
{

View File

@ -387,7 +387,7 @@ __declspec(dllexport) bool TITCALL MemoryWriteSafe(HANDLE hProcess, LPVOID lpBas
return false;
}
MutexLocker lock("BreakPointBuffer"); //thread-safe
CriticalSectionLocker lock(LockBreakPointBuffer); //thread-safe
//disable breakpoints that interfere with the memory to write
BreakPointPreWriteFilter((ULONG_PTR)lpBaseAddress, nSize, &lock);

View File

@ -48,7 +48,7 @@ __declspec(dllexport) long TITCALL GetPE32SectionNumberFromVA(ULONG_PTR FileMapV
{
__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);
DWORD SectionNumber = PEHeader64->FileHeader.NumberOfSections;
DWORD FoundInSection = -1;

View File

@ -4,6 +4,7 @@
#include "Global.Garbage.h"
#include "Global.Injector.h"
#include "Global.Engine.Extension.h"
#include "Global.Engine.Threading.h"
// Global.Engine.Entry:
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
@ -11,6 +12,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
CriticalSectionInitializeLocks(); //initialize critical sections
engineHandle=hinstDLL;
EngineInit();
EmptyGarbage();
@ -23,6 +25,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
if(lpvReserved)
ExtensionManagerPluginReleaseCallBack();
RemoveDirectoryW(engineSzEngineGarbageFolder);
CriticalSectionDeleteLocks(); //delete critical sections
break;
}
return TRUE;