From 46398eba7ba6551f6e049f150aa7f47143ff9ece Mon Sep 17 00:00:00 2001 From: "Mr. eXoDia" Date: Tue, 23 Dec 2014 00:19:11 +0100 Subject: [PATCH] removed useless unlock/relock sequences (CriticalSection objects can be called recursively without problems from the same thread) --- TitanEngine/Global.Breakpoints.cpp | 8 ++------ TitanEngine/Global.Breakpoints.h | 4 ++-- TitanEngine/Global.Engine.Threading.cpp | 12 ++++++------ TitanEngine/Global.Engine.Threading.h | 7 +++++-- TitanEngine/TitanEngine.Breakpoints.cpp | 14 -------------- TitanEngine/TitanEngine.Debugger.Memory.cpp | 4 ++-- TitanEngine/TitanEngine.cpp | 2 +- 7 files changed, 18 insertions(+), 33 deletions(-) diff --git a/TitanEngine/Global.Breakpoints.cpp b/TitanEngine/Global.Breakpoints.cpp index 5932c06..3f19ed2 100644 --- a/TitanEngine/Global.Breakpoints.cpp +++ b/TitanEngine/Global.Breakpoints.cpp @@ -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; } } diff --git a/TitanEngine/Global.Breakpoints.h b/TitanEngine/Global.Breakpoints.h index 02b2da7..ef594ce 100644 --- a/TitanEngine/Global.Breakpoints.h +++ b/TitanEngine/Global.Breakpoints.h @@ -9,7 +9,7 @@ extern std::vector 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 diff --git a/TitanEngine/Global.Engine.Threading.cpp b/TitanEngine/Global.Engine.Threading.cpp index e01074c..6cc0909 100644 --- a/TitanEngine/Global.Engine.Threading.cpp +++ b/TitanEngine/Global.Engine.Threading.cpp @@ -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]); diff --git a/TitanEngine/Global.Engine.Threading.h b/TitanEngine/Global.Engine.Threading.h index 3d6a246..8487664 100644 --- a/TitanEngine/Global.Engine.Threading.h +++ b/TitanEngine/Global.Engine.Threading.h @@ -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; }; diff --git a/TitanEngine/TitanEngine.Breakpoints.cpp b/TitanEngine/TitanEngine.Breakpoints.cpp index a9dd906..0b20f58 100644 --- a/TitanEngine/TitanEngine.Breakpoints.cpp +++ b/TitanEngine/TitanEngine.Breakpoints.cpp @@ -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; diff --git a/TitanEngine/TitanEngine.Debugger.Memory.cpp b/TitanEngine/TitanEngine.Debugger.Memory.cpp index 7564778..58c1aba 100644 --- a/TitanEngine/TitanEngine.Debugger.Memory.cpp +++ b/TitanEngine/TitanEngine.Debugger.Memory.cpp @@ -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; } diff --git a/TitanEngine/TitanEngine.cpp b/TitanEngine/TitanEngine.cpp index 3a5f322..a898414 100644 --- a/TitanEngine/TitanEngine.cpp +++ b/TitanEngine/TitanEngine.cpp @@ -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;