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 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
{
lock->unlock();
DisableBPX(cur_addr);
lock->relock();
curBp->BreakPointActive = UE_BPXACTIVE; //little hack
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 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
{
curBp->BreakPointActive = UE_BPXINACTIVE; //little hack
lock->unlock();
EnableBPX(cur_addr); //needs a cleaner solution
lock->relock();
break;
}
}

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

View File

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

View File

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

View File

@ -285,7 +285,6 @@ __declspec(dllexport) bool TITCALL DeleteBPX(ULONG_PTR bpxAddress)
if(found == -1) //not found
return false;
VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)bpxAddress, BreakPointBuffer.at(found).BreakPointSize, PAGE_EXECUTE_READWRITE, &OldProtect);
lock.unlock();
if(IsBPXEnabled(bpxAddress))
{
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;
}
}
lock.relock();
VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)bpxAddress, BreakPointBuffer.at(found).BreakPointSize, OldProtect, &OldProtect);
BreakPointBuffer.erase(BreakPointBuffer.begin() + found);
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)
{
lock.unlock();
DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress);
lock.relock();
}
else if(BreakPointBuffer.at(i).BreakPointType == UE_MEMORY ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_READ ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_WRITE ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_EXECUTE)
{
lock.unlock();
RemoveMemoryBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress, BreakPointBuffer.at(i).BreakPointSize);
lock.relock();
}
}
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)
{
lock.unlock();
DisableBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress);
lock.relock();
}
else if(BreakPointBuffer.at(i).BreakPointType == UE_MEMORY ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_READ ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_WRITE ||
BreakPointBuffer.at(i).BreakPointType == UE_MEMORY_EXECUTE)
{
lock.unlock();
RemoveMemoryBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress, BreakPointBuffer.at(i).BreakPointSize);
lock.relock();
}
}
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)
{
lock.unlock();
DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress);
lock.relock();
}
}
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)
{
lock.unlock();
DeleteBPX((ULONG_PTR)BreakPointBuffer.at(i).BreakPointAddress);
lock.relock();
}
}
return true;

View File

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

View File

@ -25,7 +25,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
if(lpvReserved)
ExtensionManagerPluginReleaseCallBack();
RemoveDirectoryW(engineSzEngineGarbageFolder);
CriticalSectionDeleteLocks(); //delete critical sections
CriticalSectionLocker::Deinitialize(); //delete critical sections
break;
}
return TRUE;