From 11cec941ce1c56f92dd5cb11a4c8df58e2abc675 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Mon, 17 Aug 2020 14:03:13 +0900 Subject: [PATCH 1/5] Added trampoline_address property to CFunction. --- src/core/modules/memory/memory_function.cpp | 9 +++++++++ src/core/modules/memory/memory_function.h | 12 +++++++----- src/core/modules/memory/memory_wrap.cpp | 6 ++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/core/modules/memory/memory_function.cpp b/src/core/modules/memory/memory_function.cpp index 897878ea3..db4d644b9 100644 --- a/src/core/modules/memory/memory_function.cpp +++ b/src/core/modules/memory/memory_function.cpp @@ -334,6 +334,15 @@ object CFunction::SkipHooks(tuple args, dict kw) return Call(args, kw); } +unsigned long CFunction::GetTrampolineAddress() +{ + CHook* pHook = GetHookManager()->FindHook((void *) m_ulAddr); + if (pHook) + return (unsigned long) pHook->m_pTrampoline; + + return m_ulAddr; +} + CHook* HookFunctionHelper(void* addr, ICallingConvention* pConv) { CHook* result; diff --git a/src/core/modules/memory/memory_function.h b/src/core/modules/memory/memory_function.h index 0d25b0e7e..7d4511929 100644 --- a/src/core/modules/memory/memory_function.h +++ b/src/core/modules/memory/memory_function.h @@ -66,20 +66,22 @@ class CFunction: public CPointer, private boost::noncopyable bool IsHookable(); bool IsHooked(); - + object Call(boost::python::tuple args, dict kw); object CallTrampoline(boost::python::tuple args, dict kw); object SkipHooks(boost::python::tuple args, dict kw); - + + unsigned long GetTrampolineAddress(); + void AddHook(HookType_t eType, PyObject* pCallable); void RemoveHook(HookType_t eType, PyObject* pCallable); - + void AddPreHook(PyObject* pCallable) { return AddHook(HOOKTYPE_PRE, pCallable); } void AddPostHook(PyObject* pCallable) { return AddHook(HOOKTYPE_POST, pCallable); } - + void RemovePreHook(PyObject* pCallable) { RemoveHook(HOOKTYPE_PRE, pCallable); } @@ -87,7 +89,7 @@ class CFunction: public CPointer, private boost::noncopyable { RemoveHook(HOOKTYPE_POST, pCallable); } void DeleteHook(); - + public: boost::python::tuple m_tArgs; object m_oConverter; diff --git a/src/core/modules/memory/memory_wrap.cpp b/src/core/modules/memory/memory_wrap.cpp index 2dad81f88..68e85c476 100644 --- a/src/core/modules/memory/memory_wrap.cpp +++ b/src/core/modules/memory/memory_wrap.cpp @@ -539,6 +539,12 @@ void export_function(scope _memory) .def_readonly("convention", &CFunction::m_eCallingConvention ) + + // Properties + .add_property("trampoline_address", + &CFunction::GetTrampolineAddress, + "Return the trampoline address if the function is hooked, otherwise return the function address." + ) ; } From 3cb810d967d219948777acd06fde0519911fc159 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Wed, 23 Sep 2020 20:50:48 +0900 Subject: [PATCH 2/5] Changed GetTrampolineAddress to check for Hook. --- src/core/modules/memory/memory_function.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/modules/memory/memory_function.cpp b/src/core/modules/memory/memory_function.cpp index db4d644b9..b457faf95 100644 --- a/src/core/modules/memory/memory_function.cpp +++ b/src/core/modules/memory/memory_function.cpp @@ -337,10 +337,10 @@ object CFunction::SkipHooks(tuple args, dict kw) unsigned long CFunction::GetTrampolineAddress() { CHook* pHook = GetHookManager()->FindHook((void *) m_ulAddr); - if (pHook) - return (unsigned long) pHook->m_pTrampoline; + if (!pHook) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Function was not hooked.") - return m_ulAddr; + return (unsigned long) pHook->m_pTrampoline; } CHook* HookFunctionHelper(void* addr, ICallingConvention* pConv) From ad6d71854b973f327cd08ded3aae0e97c830bd46 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Wed, 23 Sep 2020 23:44:26 +0900 Subject: [PATCH 3/5] Added trampoline property to Function. Removed call_trampoline. Removed trampoline_address. --- src/core/modules/memory/memory_function.cpp | 45 ++++++++------------- src/core/modules/memory/memory_function.h | 8 ++-- src/core/modules/memory/memory_wrap.cpp | 11 ++--- 3 files changed, 23 insertions(+), 41 deletions(-) mode change 100644 => 100755 src/core/modules/memory/memory_function.cpp diff --git a/src/core/modules/memory/memory_function.cpp b/src/core/modules/memory/memory_function.cpp old mode 100644 new mode 100755 index b457faf95..303bceab6 --- a/src/core/modules/memory/memory_function.cpp +++ b/src/core/modules/memory/memory_function.cpp @@ -170,13 +170,13 @@ CFunction::CFunction(unsigned long ulAddr, object oCallingConvention, object oAr } CFunction::CFunction(unsigned long ulAddr, Convention_t eCallingConvention, - int iCallingConvention, ICallingConvention* pCallingConvention, tuple tArgs, - DataType_t eReturnType, object oConverter) + int iCallingConvention, tuple tArgs, DataType_t eReturnType, object oConverter) :CPointer(ulAddr) { m_eCallingConvention = eCallingConvention; m_iCallingConvention = iCallingConvention; - m_pCallingConvention = pCallingConvention; + m_pCallingConvention = NULL; + m_oCallingConvention = object(); // We didn't allocate the calling convention, someone else is responsible for it. m_bAllocatedCallingConvention = false; @@ -218,6 +218,16 @@ bool CFunction::IsHooked() return GetHookManager()->FindHook((void *) m_ulAddr) != NULL; } +CFunction* CFunction::GetTrampoline() +{ + CHook* pHook = GetHookManager()->FindHook((void *) m_ulAddr); + if (!pHook) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Function was not hooked.") + + return new CFunction((unsigned long) pHook->m_pTrampoline, m_eCallingConvention, + m_iCallingConvention, m_tArgs, m_eReturnType, m_oConverter); +} + template ReturnType CallHelper(Function func, DCCallVM* vm, unsigned long addr) { @@ -312,35 +322,14 @@ object CFunction::Call(tuple args, dict kw) return object(); } -object CFunction::CallTrampoline(tuple args, dict kw) -{ - if (!IsCallable()) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Function is not callable.") - - Validate(); - CHook* pHook = GetHookManager()->FindHook((void *) m_ulAddr); - if (!pHook) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Function was not hooked.") - - return CFunction((unsigned long) pHook->m_pTrampoline, m_eCallingConvention, - m_iCallingConvention, m_pCallingConvention, m_tArgs, m_eReturnType, m_oConverter).Call(args, kw); -} - object CFunction::SkipHooks(tuple args, dict kw) -{ - if (IsHooked()) - return CallTrampoline(args, kw); - - return Call(args, kw); -} - -unsigned long CFunction::GetTrampolineAddress() { CHook* pHook = GetHookManager()->FindHook((void *) m_ulAddr); - if (!pHook) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Function was not hooked.") + if (pHook) + return CFunction((unsigned long) pHook->m_pTrampoline, m_eCallingConvention, + m_iCallingConvention, m_tArgs, m_eReturnType, m_oConverter).Call(args, kw); - return (unsigned long) pHook->m_pTrampoline; + return Call(args, kw); } CHook* HookFunctionHelper(void* addr, ICallingConvention* pConv) diff --git a/src/core/modules/memory/memory_function.h b/src/core/modules/memory/memory_function.h index 7d4511929..3f0fa0d59 100644 --- a/src/core/modules/memory/memory_function.h +++ b/src/core/modules/memory/memory_function.h @@ -57,8 +57,7 @@ class CFunction: public CPointer, private boost::noncopyable public: CFunction(unsigned long ulAddr, object oCallingConvention, object oArgs, object oReturnType); CFunction(unsigned long ulAddr, Convention_t eCallingConvention, int iCallingConvention, - ICallingConvention* pCallingConvention, boost::python::tuple tArgs, - DataType_t eReturnType, object oConverter); + boost::python::tuple tArgs, DataType_t eReturnType, object oConverter); ~CFunction(); @@ -67,12 +66,11 @@ class CFunction: public CPointer, private boost::noncopyable bool IsHooked(); + CFunction* GetTrampoline(); + object Call(boost::python::tuple args, dict kw); - object CallTrampoline(boost::python::tuple args, dict kw); object SkipHooks(boost::python::tuple args, dict kw); - unsigned long GetTrampolineAddress(); - void AddHook(HookType_t eType, PyObject* pCallable); void RemoveHook(HookType_t eType, PyObject* pCallable); diff --git a/src/core/modules/memory/memory_wrap.cpp b/src/core/modules/memory/memory_wrap.cpp index 68e85c476..96621868d 100644 --- a/src/core/modules/memory/memory_wrap.cpp +++ b/src/core/modules/memory/memory_wrap.cpp @@ -476,11 +476,6 @@ void export_function(scope _memory) "Return True if the function is hooked." ) - .def("call_trampoline", - raw_method(&CFunction::CallTrampoline), - "Calls the trampoline function dynamically." - ) - .def("skip_hooks", raw_method(&CFunction::SkipHooks), "Call the function, but skip hooks if there are any." @@ -541,9 +536,9 @@ void export_function(scope _memory) ) // Properties - .add_property("trampoline_address", - &CFunction::GetTrampolineAddress, - "Return the trampoline address if the function is hooked, otherwise return the function address." + .add_property("trampoline", + make_function(&CFunction::GetTrampoline, manage_new_object_policy()), + "Return the trampoline function if the function is hooked." ) ; } From 52e19681cdf7ec9c805bce6c04038524e34072e0 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Thu, 24 Sep 2020 00:54:14 +0900 Subject: [PATCH 4/5] Fixed a compile error. --- src/core/modules/memory/memory_function.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/modules/memory/memory_function.cpp b/src/core/modules/memory/memory_function.cpp index 303bceab6..5384590ff 100755 --- a/src/core/modules/memory/memory_function.cpp +++ b/src/core/modules/memory/memory_function.cpp @@ -176,7 +176,6 @@ CFunction::CFunction(unsigned long ulAddr, Convention_t eCallingConvention, m_eCallingConvention = eCallingConvention; m_iCallingConvention = iCallingConvention; m_pCallingConvention = NULL; - m_oCallingConvention = object(); // We didn't allocate the calling convention, someone else is responsible for it. m_bAllocatedCallingConvention = false; From 8c28170e8eade372a0bb6bef190bb4f0ee56926e Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Fri, 25 Sep 2020 11:34:20 +0900 Subject: [PATCH 5/5] Reverted the deletion of call_trampoline. --- src/core/modules/memory/memory_function.cpp | 10 ++++++++++ src/core/modules/memory/memory_function.h | 1 + src/core/modules/memory/memory_wrap.cpp | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/src/core/modules/memory/memory_function.cpp b/src/core/modules/memory/memory_function.cpp index 5384590ff..3ee4144da 100755 --- a/src/core/modules/memory/memory_function.cpp +++ b/src/core/modules/memory/memory_function.cpp @@ -321,6 +321,16 @@ object CFunction::Call(tuple args, dict kw) return object(); } +object CFunction::CallTrampoline(tuple args, dict kw) +{ + CHook* pHook = GetHookManager()->FindHook((void *) m_ulAddr); + if (!pHook) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Function was not hooked.") + + return CFunction((unsigned long) pHook->m_pTrampoline, m_eCallingConvention, + m_iCallingConvention, m_tArgs, m_eReturnType, m_oConverter).Call(args, kw); +} + object CFunction::SkipHooks(tuple args, dict kw) { CHook* pHook = GetHookManager()->FindHook((void *) m_ulAddr); diff --git a/src/core/modules/memory/memory_function.h b/src/core/modules/memory/memory_function.h index 3f0fa0d59..548d9bbae 100644 --- a/src/core/modules/memory/memory_function.h +++ b/src/core/modules/memory/memory_function.h @@ -69,6 +69,7 @@ class CFunction: public CPointer, private boost::noncopyable CFunction* GetTrampoline(); object Call(boost::python::tuple args, dict kw); + object CallTrampoline(boost::python::tuple args, dict kw); object SkipHooks(boost::python::tuple args, dict kw); void AddHook(HookType_t eType, PyObject* pCallable); diff --git a/src/core/modules/memory/memory_wrap.cpp b/src/core/modules/memory/memory_wrap.cpp index 96621868d..d8ef5190f 100644 --- a/src/core/modules/memory/memory_wrap.cpp +++ b/src/core/modules/memory/memory_wrap.cpp @@ -476,6 +476,11 @@ void export_function(scope _memory) "Return True if the function is hooked." ) + .def("call_trampoline", + raw_method(&CFunction::CallTrampoline), + "Calls the trampoline function dynamically." + ) + .def("skip_hooks", raw_method(&CFunction::SkipHooks), "Call the function, but skip hooks if there are any."