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 all commits
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
27 changes: 17 additions & 10 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,12 @@ 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;

// We didn't allocate the calling convention, someone else is responsible for it.
m_bAllocatedCallingConvention = false;
Expand Down Expand Up @@ -218,6 +217,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 @@ -314,22 +323,20 @@ object CFunction::Call(tuple args, dict kw)

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);
m_iCallingConvention, m_tArgs, m_eReturnType, m_oConverter).Call(args, kw);
}

object CFunction::SkipHooks(tuple args, dict kw)
{
if (IsHooked())
return CallTrampoline(args, kw);
CHook* pHook = GetHookManager()->FindHook((void *) m_ulAddr);
if (pHook)
return CFunction((unsigned long) pHook->m_pTrampoline, m_eCallingConvention,
m_iCallingConvention, m_tArgs, m_eReturnType, m_oConverter).Call(args, kw);

return Call(args, kw);
}
Expand Down
15 changes: 8 additions & 7 deletions src/core/modules/memory/memory_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,38 @@ 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();

bool IsCallable();
bool IsHookable();

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);

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); }

void RemovePostHook(PyObject* pCallable)
{ RemoveHook(HOOKTYPE_POST, pCallable); }

void DeleteHook();

public:
boost::python::tuple m_tArgs;
object m_oConverter;
Expand Down
6 changes: 6 additions & 0 deletions src/core/modules/memory/memory_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ void export_function(scope _memory)
.def_readonly("convention",
&CFunction::m_eCallingConvention
)

// Properties
.add_property("trampoline",
make_function(&CFunction::GetTrampoline, manage_new_object_policy()),
"Return the trampoline function if the function is hooked."
)
;
}

Expand Down