mirror of https://github.com/x64dbg/GleeBug
some locking
This commit is contained in:
parent
3fb8539c7a
commit
cd01d22686
|
|
@ -107,12 +107,13 @@ namespace GleeBug
|
||||||
ResumeFlag(this)
|
ResumeFlag(this)
|
||||||
{
|
{
|
||||||
memset(&this->mContext, 0, sizeof(CONTEXT));
|
memset(&this->mContext, 0, sizeof(CONTEXT));
|
||||||
|
InitializeCriticalSection(&mCr);
|
||||||
}
|
}
|
||||||
|
|
||||||
CONTEXT* Registers::GetContext()
|
LockedPtr<CONTEXT> Registers::GetContext()
|
||||||
{
|
{
|
||||||
handleLazyContext();
|
handleLazyContext();
|
||||||
return &mContext;
|
return LockedPtr<CONTEXT>(&mCr, &mContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*void Registers::SetContext(const CONTEXT & context)
|
/*void Registers::SetContext(const CONTEXT & context)
|
||||||
|
|
@ -130,6 +131,8 @@ namespace GleeBug
|
||||||
|
|
||||||
bool Registers::handleLazyContext()
|
bool Registers::handleLazyContext()
|
||||||
{
|
{
|
||||||
|
ScopedCriticalSection lock(&mCr);
|
||||||
|
|
||||||
if(!this->mLazySet)
|
if(!this->mLazySet)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,63 @@
|
||||||
|
|
||||||
namespace GleeBug
|
namespace GleeBug
|
||||||
{
|
{
|
||||||
|
class ScopedCriticalSection
|
||||||
|
{
|
||||||
|
PCRITICAL_SECTION cr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ScopedCriticalSection(PCRITICAL_SECTION cr)
|
||||||
|
: cr(cr)
|
||||||
|
{
|
||||||
|
EnterCriticalSection(cr);
|
||||||
|
}
|
||||||
|
|
||||||
|
~ScopedCriticalSection()
|
||||||
|
{
|
||||||
|
LeaveCriticalSection(cr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class LockedPtr
|
||||||
|
{
|
||||||
|
PCRITICAL_SECTION locker;
|
||||||
|
T* ptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit LockedPtr(PCRITICAL_SECTION locker, T* ptr)
|
||||||
|
: locker(locker), ptr(ptr)
|
||||||
|
{
|
||||||
|
EnterCriticalSection(locker);
|
||||||
|
}
|
||||||
|
|
||||||
|
~LockedPtr()
|
||||||
|
{
|
||||||
|
LeaveCriticalSection(locker);
|
||||||
|
}
|
||||||
|
|
||||||
|
LockedPtr(const LockedPtr<T> &) = delete;
|
||||||
|
|
||||||
|
LockedPtr<T> &operator=(const LockedPtr<T> &) = delete;
|
||||||
|
|
||||||
|
LockedPtr(LockedPtr<T> && other)
|
||||||
|
: locker(other.locker), ptr(other.ptr)
|
||||||
|
{
|
||||||
|
other.locker = nullptr;
|
||||||
|
other.ptr = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*operator T*() const
|
||||||
|
{
|
||||||
|
return ptr;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
T* operator->() const
|
||||||
|
{
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\brief Thread register context.
|
\brief Thread register context.
|
||||||
*/
|
*/
|
||||||
|
|
@ -162,7 +219,7 @@ namespace GleeBug
|
||||||
\brief Gets a pointer to the context object.
|
\brief Gets a pointer to the context object.
|
||||||
\return This function will never return a nullptr.
|
\return This function will never return a nullptr.
|
||||||
*/
|
*/
|
||||||
CONTEXT* GetContext();
|
LockedPtr<CONTEXT> GetContext();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\brief Sets the CONTEXT.
|
\brief Sets the CONTEXT.
|
||||||
|
|
@ -172,6 +229,7 @@ namespace GleeBug
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CONTEXT mContext;
|
CONTEXT mContext;
|
||||||
|
CRITICAL_SECTION mCr;
|
||||||
|
|
||||||
LPCONTEXT mLazyOldContext = nullptr;
|
LPCONTEXT mLazyOldContext = nullptr;
|
||||||
HANDLE mLazyThread = nullptr;
|
HANDLE mLazyThread = nullptr;
|
||||||
|
|
|
||||||
|
|
@ -284,7 +284,7 @@ namespace GleeBug
|
||||||
bool mBreakDebugger = false;
|
bool mBreakDebugger = false;
|
||||||
DEBUG_EVENT mDebugEvent;
|
DEBUG_EVENT mDebugEvent;
|
||||||
ProcessMap mProcesses;
|
ProcessMap mProcesses;
|
||||||
bool mIsRunning = false;
|
bool mIsRunning = false; //TODO: needs a dedicated critical section to prevent ContinueDebugEvent and change of registers to race
|
||||||
bool mIsDebugging = false;
|
bool mIsDebugging = false;
|
||||||
bool mDetach = false;
|
bool mDetach = false;
|
||||||
bool mDetachAndBreak = false;
|
bool mDetachAndBreak = false;
|
||||||
|
|
|
||||||
|
|
@ -533,6 +533,7 @@ public:
|
||||||
if (!thread || !titcontext)
|
if (!thread || !titcontext)
|
||||||
return false;
|
return false;
|
||||||
ThreadSuspender suspender(thread, mIsRunning, false);
|
ThreadSuspender suspender(thread, mIsRunning, false);
|
||||||
|
auto context = thread->registers.GetContext();
|
||||||
memset(titcontext, 0, sizeof(TITAN_ENGINE_CONTEXT_t));
|
memset(titcontext, 0, sizeof(TITAN_ENGINE_CONTEXT_t));
|
||||||
//General purpose registers
|
//General purpose registers
|
||||||
titcontext->cax = thread->registers.Gax();
|
titcontext->cax = thread->registers.Gax();
|
||||||
|
|
@ -571,7 +572,6 @@ public:
|
||||||
titcontext->cs = thread->registers.Cs();
|
titcontext->cs = thread->registers.Cs();
|
||||||
titcontext->ss = thread->registers.Ss();
|
titcontext->ss = thread->registers.Ss();
|
||||||
// x87
|
// x87
|
||||||
auto context = thread->registers.GetContext();
|
|
||||||
#ifdef _WIN64
|
#ifdef _WIN64
|
||||||
titcontext->x87fpu.ControlWord = context->FltSave.ControlWord;
|
titcontext->x87fpu.ControlWord = context->FltSave.ControlWord;
|
||||||
titcontext->x87fpu.StatusWord = context->FltSave.StatusWord;
|
titcontext->x87fpu.StatusWord = context->FltSave.StatusWord;
|
||||||
|
|
@ -618,6 +618,7 @@ public:
|
||||||
if (!thread || !titcontext)
|
if (!thread || !titcontext)
|
||||||
return false;
|
return false;
|
||||||
ThreadSuspender suspender(thread, mIsRunning, true);
|
ThreadSuspender suspender(thread, mIsRunning, true);
|
||||||
|
auto context = thread->registers.GetContext();
|
||||||
// General purpose registers
|
// General purpose registers
|
||||||
thread->registers.Gax = titcontext->cax;
|
thread->registers.Gax = titcontext->cax;
|
||||||
thread->registers.Gcx = titcontext->ccx;
|
thread->registers.Gcx = titcontext->ccx;
|
||||||
|
|
@ -655,7 +656,6 @@ public:
|
||||||
thread->registers.Cs = titcontext->cs;
|
thread->registers.Cs = titcontext->cs;
|
||||||
thread->registers.Ss = titcontext->ss;
|
thread->registers.Ss = titcontext->ss;
|
||||||
// x87
|
// x87
|
||||||
auto context = thread->registers.GetContext();
|
|
||||||
#ifdef _WIN64
|
#ifdef _WIN64
|
||||||
context->FltSave.ControlWord = titcontext->x87fpu.ControlWord;
|
context->FltSave.ControlWord = titcontext->x87fpu.ControlWord;
|
||||||
context->FltSave.StatusWord = titcontext->x87fpu.StatusWord;
|
context->FltSave.StatusWord = titcontext->x87fpu.StatusWord;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue