Skip to content

Add trampoline property to CFunction. #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added trampoline property to Function.
Removed call_trampoline.

Removed trampoline_address.
  • Loading branch information
CookStar committed Sep 23, 2020
commit ad6d71854b973f327cd08ded3aae0e97c830bd46
45 changes: 17 additions & 28 deletions src/core/modules/memory/memory_function.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<class ReturnType, class Function>
ReturnType CallHelper(Function func, DCCallVM* vm, unsigned long addr)
{
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions src/core/modules/memory/memory_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);

Expand Down
11 changes: 3 additions & 8 deletions src/core/modules/memory/memory_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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."
)
;
}
Expand Down