removed useless unlock/relock sequences (CriticalSection objects can be called recursively without problems from the same thread)

This commit is contained in:
Mr. eXoDia 2014-12-23 00:19:11 +01:00
parent f04f96e83d
commit 46398eba7b
7 changed files with 18 additions and 33 deletions

View File

@ -135,7 +135,7 @@ void BreakPointPostReadFilter(ULONG_PTR lpBaseAddress, unsigned char* lpBuffer,
} }
} }
void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSectionLocker* lock) void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize)
{ {
ULONG_PTR start = lpBaseAddress; ULONG_PTR start = lpBaseAddress;
ULONG_PTR end = start + nSize; ULONG_PTR end = start + nSize;
@ -151,9 +151,7 @@ void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSec
{ {
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
{ {
lock->unlock();
DisableBPX(cur_addr); DisableBPX(cur_addr);
lock->relock();
curBp->BreakPointActive = UE_BPXACTIVE; //little hack curBp->BreakPointActive = UE_BPXACTIVE; //little hack
break; break;
} }
@ -161,7 +159,7 @@ void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSec
} }
} }
void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSectionLocker* lock) void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize)
{ {
ULONG_PTR start = lpBaseAddress; ULONG_PTR start = lpBaseAddress;
ULONG_PTR end = start + nSize; ULONG_PTR end = start + nSize;
@ -178,9 +176,7 @@ void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSe
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
{ {
curBp->BreakPointActive = UE_BPXINACTIVE; //little hack curBp->BreakPointActive = UE_BPXINACTIVE; //little hack
lock->unlock();
EnableBPX(cur_addr); //needs a cleaner solution EnableBPX(cur_addr); //needs a cleaner solution
lock->relock();
break; break;
} }
} }

View File

@ -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, CriticalSectionLocker* lock); void BreakPointPreWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize);
void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize, CriticalSectionLocker* lock); void BreakPointPostWriteFilter(ULONG_PTR lpBaseAddress, SIZE_T nSize);
#endif //_GLOBAL_BREAKPOINTS_H #endif //_GLOBAL_BREAKPOINTS_H

View File

@ -2,10 +2,10 @@
#include "definitions.h" #include "definitions.h"
#include "Global.Engine.Threading.h" #include "Global.Engine.Threading.h"
static CRITICAL_SECTION locks[LockLast] = {}; CRITICAL_SECTION CriticalSectionLocker::locks[LockLast] = {};
static bool bInitDone = false; bool CriticalSectionLocker::bInitDone = false;
static void CriticalSectionInitializeLocks() void CriticalSectionLocker::Initialize()
{ {
if(bInitDone) if(bInitDone)
return; return;
@ -14,13 +14,13 @@ static void CriticalSectionInitializeLocks()
bInitDone = true; bInitDone = true;
} }
void CriticalSectionDeleteLocks() void CriticalSectionLocker::Deinitialize()
{ {
if(!bInitDone) if(!bInitDone)
return; return;
for(int i = 0; i < LockLast; i++) for(int i = 0; i < LockLast; i++)
{ {
EnterCriticalSection(&locks[i]); EnterCriticalSection(&locks[i]); //obtain ownership
DeleteCriticalSection(&locks[i]); DeleteCriticalSection(&locks[i]);
} }
bInitDone = false; bInitDone = false;
@ -28,7 +28,7 @@ void CriticalSectionDeleteLocks()
CriticalSectionLocker::CriticalSectionLocker(CriticalSectionLock lock) CriticalSectionLocker::CriticalSectionLocker(CriticalSectionLock lock)
{ {
CriticalSectionInitializeLocks(); //initialize critical sections Initialize(); //initialize critical sections
gLock = lock; gLock = lock;
EnterCriticalSection(&locks[gLock]); EnterCriticalSection(&locks[gLock]);

View File

@ -13,17 +13,20 @@ enum CriticalSectionLock
LockLast LockLast
}; };
void CriticalSectionDeleteLocks();
class CriticalSectionLocker class CriticalSectionLocker
{ {
public: public:
static void Deinitialize();
CriticalSectionLocker(CriticalSectionLock lock); CriticalSectionLocker(CriticalSectionLock lock);
~CriticalSectionLocker(); ~CriticalSectionLocker();
void unlock(); void unlock();
void relock(); void relock();
private: private:
static void Initialize();
static bool bInitDone;
static CRITICAL_SECTION locks[LockLast];
CriticalSectionLock gLock; CriticalSectionLock gLock;
bool Locked; bool Locked;
}; };

View File

@ -285,7 +285,6 @@ __declspec(dllexport) bool TITCALL DeleteBPX(ULONG_PTR bpxAddress)
if(found == -1) //not found if(found == -1) //not found
return false; return false;
VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)bpxAddress, BreakPointBuffer.at(found).BreakPointSize, PAGE_EXECUTE_READWRITE, &OldProtect); VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)bpxAddress, BreakPointBuffer.at(found).BreakPointSize, PAGE_EXECUTE_READWRITE, &OldProtect);
lock.unlock();
if(IsBPXEnabled(bpxAddress)) if(IsBPXEnabled(bpxAddress))
{ {
if(!WriteProcessMemory(dbgProcessInformation.hProcess, (LPVOID)bpxAddress, &BreakPointBuffer.at(found).OriginalByte[0], BreakPointBuffer.at(found).BreakPointSize, &NumberOfBytesReadWritten)) if(!WriteProcessMemory(dbgProcessInformation.hProcess, (LPVOID)bpxAddress, &BreakPointBuffer.at(found).OriginalByte[0], BreakPointBuffer.at(found).BreakPointSize, &NumberOfBytesReadWritten))
@ -294,7 +293,6 @@ __declspec(dllexport) bool TITCALL DeleteBPX(ULONG_PTR bpxAddress)
return false; return false;
} }
} }
lock.relock();
VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)bpxAddress, BreakPointBuffer.at(found).BreakPointSize, OldProtect, &OldProtect); VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)bpxAddress, BreakPointBuffer.at(found).BreakPointSize, OldProtect, &OldProtect);
BreakPointBuffer.erase(BreakPointBuffer.begin() + found); BreakPointBuffer.erase(BreakPointBuffer.begin() + found);
return true; return true;
@ -824,18 +822,14 @@ __declspec(dllexport) bool TITCALL RemoveAllBreakPoints(DWORD RemoveOption)
{ {
if(BreakPointBuffer.at(i).BreakPointType == UE_BREAKPOINT || BreakPointBuffer.at(i).BreakPointType == UE_SINGLESHOOT) if(BreakPointBuffer.at(i).BreakPointType == UE_BREAKPOINT || BreakPointBuffer.at(i).BreakPointType == UE_SINGLESHOOT)
{ {
lock.unlock();
DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress); DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress);
lock.relock();
} }
else if(BreakPointBuffer.at(i).BreakPointType == UE_MEMORY || else if(BreakPointBuffer.at(i).BreakPointType == UE_MEMORY ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_READ || BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_READ ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_WRITE || BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_WRITE ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_EXECUTE) BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_EXECUTE)
{ {
lock.unlock();
RemoveMemoryBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress, BreakPointBuffer.at(i).BreakPointSize); RemoveMemoryBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress, BreakPointBuffer.at(i).BreakPointSize);
lock.relock();
} }
} }
DeleteHardwareBreakPoint(UE_DR0); DeleteHardwareBreakPoint(UE_DR0);
@ -850,18 +844,14 @@ __declspec(dllexport) bool TITCALL RemoveAllBreakPoints(DWORD RemoveOption)
{ {
if((BreakPointBuffer.at(i).BreakPointType == UE_BREAKPOINT || BreakPointBuffer.at(i).BreakPointType == UE_SINGLESHOOT) && BreakPointBuffer.at(i).BreakPointActive == UE_BPXACTIVE) if((BreakPointBuffer.at(i).BreakPointType == UE_BREAKPOINT || BreakPointBuffer.at(i).BreakPointType == UE_SINGLESHOOT) && BreakPointBuffer.at(i).BreakPointActive == UE_BPXACTIVE)
{ {
lock.unlock();
DisableBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress); DisableBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress);
lock.relock();
} }
else if(BreakPointBuffer.at(i).BreakPointType == UE_MEMORY || else if(BreakPointBuffer.at(i).BreakPointType == UE_MEMORY ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_READ || BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_READ ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_WRITE || BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_WRITE ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_EXECUTE) BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_EXECUTE)
{ {
lock.unlock();
RemoveMemoryBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress, BreakPointBuffer.at(i).BreakPointSize); RemoveMemoryBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress, BreakPointBuffer.at(i).BreakPointSize);
lock.relock();
} }
} }
return true; return true;
@ -872,9 +862,7 @@ __declspec(dllexport) bool TITCALL RemoveAllBreakPoints(DWORD RemoveOption)
{ {
if((BreakPointBuffer.at(i).BreakPointType == UE_BREAKPOINT || BreakPointBuffer.at(i).BreakPointType == UE_SINGLESHOOT) && BreakPointBuffer.at(i).BreakPointActive == UE_BPXINACTIVE) if((BreakPointBuffer.at(i).BreakPointType == UE_BREAKPOINT || BreakPointBuffer.at(i).BreakPointType == UE_SINGLESHOOT) && BreakPointBuffer.at(i).BreakPointActive == UE_BPXINACTIVE)
{ {
lock.unlock();
DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress); DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress);
lock.relock();
} }
} }
return true; return true;
@ -885,9 +873,7 @@ __declspec(dllexport) bool TITCALL RemoveAllBreakPoints(DWORD RemoveOption)
{ {
if((BreakPointBuffer.at(i).BreakPointType == UE_BREAKPOINT || BreakPointBuffer.at(i).BreakPointType == UE_SINGLESHOOT) && BreakPointBuffer.at(i).BreakPointActive == UE_BPXACTIVE) if((BreakPointBuffer.at(i).BreakPointType == UE_BREAKPOINT || BreakPointBuffer.at(i).BreakPointType == UE_SINGLESHOOT) && BreakPointBuffer.at(i).BreakPointActive == UE_BPXACTIVE)
{ {
lock.unlock();
DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress); DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress);
lock.relock();
} }
} }
return true; return true;

View File

@ -389,7 +389,7 @@ __declspec(dllexport) bool TITCALL MemoryWriteSafe(HANDLE hProcess, LPVOID lpBas
CriticalSectionLocker lock(LockBreakPointBuffer); //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);
if(!lpNumberOfBytesWritten) if(!lpNumberOfBytesWritten)
{ {
@ -417,7 +417,7 @@ __declspec(dllexport) bool TITCALL MemoryWriteSafe(HANDLE hProcess, LPVOID lpBas
} }
//re-enable breakpoints that interfere with the memory to write //re-enable breakpoints that interfere with the memory to write
BreakPointPostWriteFilter((ULONG_PTR)lpBaseAddress, nSize, &lock); BreakPointPostWriteFilter((ULONG_PTR)lpBaseAddress, nSize);
return retValue; return retValue;
} }

View File

@ -25,7 +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 CriticalSectionLocker::Deinitialize(); //delete critical sections
break; break;
} }
return TRUE; return TRUE;