Skip to content

Commit c386410

Browse files
committed
Added DynCall functions
1 parent 71a591f commit c386410

File tree

2 files changed

+143
-68
lines changed

2 files changed

+143
-68
lines changed

src/core/modules/memory/memory_tools.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ using namespace DynamicHooks;
3939
using namespace boost::python;
4040

4141

42+
// Externals
43+
extern DCCallVM* g_pCallVM;
44+
extern unsigned long ExtractPyPtr(object obj);
45+
4246
//-----------------------------------------------------------------------------
4347
// Memory functions
4448
//-----------------------------------------------------------------------------
@@ -195,6 +199,50 @@ class CPointer
195199
CFunction* MakeFunction(Convention_t eConv, char* szParams);
196200
CFunction* MakeVirtualFunction(int iIndex, Convention_t eConv, char* szParams);
197201

202+
// DynCall
203+
inline void ResetVM()
204+
{ dcReset(g_pCallVM); }
205+
206+
inline void SetMode(int iMode)
207+
{ dcMode(g_pCallVM, iMode); }
208+
209+
// A macro to shorten the implementation
210+
#define IMPLEMENT_DC(type, fname, dcname) \
211+
inline void SetArg##fname(type value) \
212+
{ dcArg##dcname(g_pCallVM, value); } \
213+
\
214+
inline type Call##fname() \
215+
{ return dcCall##dcname(g_pCallVM, m_ulAddr); }
216+
217+
IMPLEMENT_DC(bool, Bool, Bool)
218+
IMPLEMENT_DC(char, Char, Char)
219+
IMPLEMENT_DC(unsigned char, UChar, Char)
220+
IMPLEMENT_DC(short, Short, Short)
221+
IMPLEMENT_DC(unsigned short, UShort, Short)
222+
IMPLEMENT_DC(int, Int, Int)
223+
IMPLEMENT_DC(unsigned int, UInt, Int)
224+
IMPLEMENT_DC(long, Long, Long)
225+
IMPLEMENT_DC(unsigned long, ULong, Long)
226+
IMPLEMENT_DC(float, Float, Float)
227+
IMPLEMENT_DC(double, Double, Double)
228+
229+
inline void CallVoid()
230+
{ dcCallVoid(g_pCallVM, m_ulAddr); }
231+
232+
inline void SetArgPointer(object value)
233+
{
234+
unsigned long ptr = ExtractPyPtr(value);
235+
dcArgPointer(g_pCallVM, ptr);
236+
}
237+
238+
inline CPointer CallPointer()
239+
{ return CPointer(dcCallPointer(g_pCallVM, m_ulAddr)); }
240+
241+
inline void SetArgString(const char* value)
242+
{ dcArgPointer(g_pCallVM, (unsigned long) value); }
243+
244+
const char* CallString()
245+
{ return (const char *) dcCallPointer(g_pCallVM, m_ulAddr); }
198246

199247
public:
200248
unsigned long m_ulAddr;

src/core/modules/memory/memory_wrap_python.cpp

Lines changed: 95 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -111,88 +111,102 @@ void export_binaryfile()
111111
//-----------------------------------------------------------------------------
112112
// Exposes CPointer
113113
//-----------------------------------------------------------------------------
114-
// These two macros ease the exposing part of get_<type> methods
115114
#define OVERLOAD_GET_TYPE(name, type) \
116115
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_##name##_overload, CPointer::Get<type>, 0, 1)
117116

118-
#define EXPOSE_GET_TYPE(name, type) \
119-
def("get_" XSTRINGIFY(name), \
120-
&CPointer::Get<type>, \
121-
get_##name##_overload( \
122-
args("offset"), \
123-
"Returns the value at the given memory location." \
124-
) \
125-
)
126-
127-
// These two macros ease the exposing part of set_<type> methods
128117
#define OVERLOAD_SET_TYPE(name, type) \
129118
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(set_##name##_overload, CPointer::Set<type>, 1, 2)
130119

120+
#define OVERLOAD_GET_SET_TYPE(name, type) \
121+
OVERLOAD_GET_TYPE(name, type) \
122+
OVERLOAD_SET_TYPE(name, type)
123+
131124
#define EXPOSE_SET_TYPE(name, type) \
132-
def("set_" XSTRINGIFY(name), \
125+
.def("set_" XSTRINGIFY(name), \
133126
&CPointer::Set<type>, \
134127
set_##name##_overload( \
135128
args("value", "offset"), \
136129
"Sets the value at the given memory location." \
137130
) \
138131
)
132+
133+
#define EXPOSE_GET_TYPE(name, type) \
134+
.def("get_" XSTRINGIFY(name), \
135+
&CPointer::Get<type>, \
136+
get_##name##_overload( \
137+
args("offset"), \
138+
"Returns the value at the given memory location." \
139+
) \
140+
)
141+
142+
#define EXPOSE_GET_SET_TYPE(name, type) \
143+
EXPOSE_SET_TYPE(name, type) \
144+
EXPOSE_GET_TYPE(name, type)
139145

146+
// get/set_<type> overloads
147+
OVERLOAD_GET_SET_TYPE(bool, bool)
148+
OVERLOAD_GET_SET_TYPE(char, char)
149+
OVERLOAD_GET_SET_TYPE(uchar, unsigned char)
150+
OVERLOAD_GET_SET_TYPE(short, short)
151+
OVERLOAD_GET_SET_TYPE(ushort, unsigned short)
152+
OVERLOAD_GET_SET_TYPE(int, int)
153+
OVERLOAD_GET_SET_TYPE(uint, unsigned int)
154+
OVERLOAD_GET_SET_TYPE(long, long)
155+
OVERLOAD_GET_SET_TYPE(ulong, unsigned long)
156+
OVERLOAD_GET_SET_TYPE(long_long, long long)
157+
OVERLOAD_GET_SET_TYPE(ulong_long, unsigned long long)
158+
OVERLOAD_GET_SET_TYPE(float, float)
159+
OVERLOAD_GET_SET_TYPE(double, double)
160+
140161
// get_<type> overloads
141-
OVERLOAD_GET_TYPE(bool, bool)
142-
OVERLOAD_GET_TYPE(char, char)
143-
OVERLOAD_GET_TYPE(uchar, unsigned char)
144-
OVERLOAD_GET_TYPE(short, short)
145-
OVERLOAD_GET_TYPE(ushort, unsigned short)
146-
OVERLOAD_GET_TYPE(int, int)
147-
OVERLOAD_GET_TYPE(uint, unsigned int)
148-
OVERLOAD_GET_TYPE(long, long)
149-
OVERLOAD_GET_TYPE(ulong, unsigned long)
150-
OVERLOAD_GET_TYPE(long_long, long long)
151-
OVERLOAD_GET_TYPE(ulong_long, unsigned long long)
152-
OVERLOAD_GET_TYPE(float, float)
153-
OVERLOAD_GET_TYPE(double, double)
154162
OVERLOAD_GET_TYPE(string_ptr, const char*)
155163

156164
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_ptr_overload, GetPtr, 0, 1)
157165
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_string_array_overload, GetStringArray, 0, 1)
158166

159167
// set_<type> overloads
160-
OVERLOAD_SET_TYPE(bool, bool)
161-
OVERLOAD_SET_TYPE(char, char)
162-
OVERLOAD_SET_TYPE(uchar, unsigned char)
163-
OVERLOAD_SET_TYPE(short, short)
164-
OVERLOAD_SET_TYPE(ushort, unsigned short)
165-
OVERLOAD_SET_TYPE(int, int)
166-
OVERLOAD_SET_TYPE(uint, unsigned int)
167-
OVERLOAD_SET_TYPE(long, long)
168-
OVERLOAD_SET_TYPE(ulong, unsigned long)
169-
OVERLOAD_SET_TYPE(long_long, long long)
170-
OVERLOAD_SET_TYPE(ulong_long, unsigned long long)
171-
OVERLOAD_SET_TYPE(float, float)
172-
OVERLOAD_SET_TYPE(double, double)
173-
174168
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(set_ptr_overload, SetPtr, 1, 2)
175169
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(set_string_ptr_overload, SetStringPtr, 1, 2)
176170
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(set_string_array_overload, SetStringArray, 1, 2)
177171

172+
// These three macros ease the exposing part of DynCall
173+
#define EXPOSE_DC_SET(pyname, cppname) \
174+
.def("set_arg_" XSTRINGIFY(pyname), \
175+
&CPointer::SetArg##cppname, \
176+
args("value"), \
177+
"Adds a new parameter to the virtual machine." \
178+
)
179+
180+
#define EXPOSE_DC_CALL(pyname, cppname) \
181+
.def("call_" XSTRINGIFY(pyname), \
182+
&CPointer::Call##cppname, \
183+
"Calls the virtual machine." \
184+
)
185+
186+
#define EXPOSE_DC(pyname, cppname) \
187+
EXPOSE_DC_SET(pyname, cppname) \
188+
EXPOSE_DC_CALL(pyname, cppname)
189+
178190
void export_memtools()
179191
{
180192
class_<CPointer>("Pointer", init< optional<unsigned long> >())
193+
// get/set_<type> methods
194+
EXPOSE_GET_SET_TYPE(bool, bool)
195+
EXPOSE_GET_SET_TYPE(char, char)
196+
EXPOSE_GET_SET_TYPE(uchar, unsigned char)
197+
EXPOSE_GET_SET_TYPE(short, short)
198+
EXPOSE_GET_SET_TYPE(ushort, unsigned short)
199+
EXPOSE_GET_SET_TYPE(int, int)
200+
EXPOSE_GET_SET_TYPE(uint, unsigned int)
201+
EXPOSE_GET_SET_TYPE(long, long)
202+
EXPOSE_GET_SET_TYPE(ulong, unsigned long)
203+
EXPOSE_GET_SET_TYPE(long_long, long long)
204+
EXPOSE_GET_SET_TYPE(ulong_long, unsigned long long)
205+
EXPOSE_GET_SET_TYPE(float, float)
206+
EXPOSE_GET_SET_TYPE(double, double)
207+
181208
// get_<type> methods
182-
.EXPOSE_GET_TYPE(bool, bool)
183-
.EXPOSE_GET_TYPE(char, char)
184-
.EXPOSE_GET_TYPE(uchar, unsigned char)
185-
.EXPOSE_GET_TYPE(short, short)
186-
.EXPOSE_GET_TYPE(ushort, unsigned short)
187-
.EXPOSE_GET_TYPE(int, int)
188-
.EXPOSE_GET_TYPE(uint, unsigned int)
189-
.EXPOSE_GET_TYPE(long, long)
190-
.EXPOSE_GET_TYPE(ulong, unsigned long)
191-
.EXPOSE_GET_TYPE(long_long, long long)
192-
.EXPOSE_GET_TYPE(ulong_long, unsigned long long)
193-
.EXPOSE_GET_TYPE(float, float)
194-
.EXPOSE_GET_TYPE(double, double)
195-
.EXPOSE_GET_TYPE(string_ptr, const char*)
209+
EXPOSE_GET_TYPE(string_ptr, const char*)
196210

197211
.def("get_ptr",
198212
&CPointer::GetPtr,
@@ -211,20 +225,6 @@ void export_memtools()
211225
)
212226

213227
// set_<type> methods
214-
.EXPOSE_SET_TYPE(bool, bool)
215-
.EXPOSE_SET_TYPE(char, char)
216-
.EXPOSE_SET_TYPE(uchar, unsigned char)
217-
.EXPOSE_SET_TYPE(short, short)
218-
.EXPOSE_SET_TYPE(ushort, unsigned short)
219-
.EXPOSE_SET_TYPE(int, int)
220-
.EXPOSE_SET_TYPE(uint, unsigned int)
221-
.EXPOSE_SET_TYPE(long, long)
222-
.EXPOSE_SET_TYPE(ulong, unsigned long)
223-
.EXPOSE_SET_TYPE(long_long, long long)
224-
.EXPOSE_SET_TYPE(ulong_long, unsigned long long)
225-
.EXPOSE_SET_TYPE(float, float)
226-
.EXPOSE_SET_TYPE(double, double)
227-
228228
.def("set_ptr",
229229
&CPointer::SetPtr,
230230
set_ptr_overload(
@@ -249,10 +249,37 @@ void export_memtools()
249249
)
250250
)
251251

252+
// DynCall methods
253+
.def("reset_vm",
254+
&CPointer::ResetVM,
255+
"Resets the virtual machine."
256+
)
257+
258+
.def("set_mode",
259+
&CPointer::SetMode,
260+
"Sets the calling convention.",
261+
args("convention")
262+
)
263+
264+
EXPOSE_DC_CALL(void, Void)
265+
EXPOSE_DC(bool, Bool)
266+
EXPOSE_DC(char, Char)
267+
EXPOSE_DC(uchar, UChar)
268+
EXPOSE_DC(short, Short)
269+
EXPOSE_DC(ushort, UShort)
270+
EXPOSE_DC(int, Int)
271+
EXPOSE_DC(uint, UInt)
272+
EXPOSE_DC(long, Long)
273+
EXPOSE_DC(ulong, ULong)
274+
EXPOSE_DC(float, Float)
275+
EXPOSE_DC(double, Double)
276+
EXPOSE_DC(pointer, Pointer)
277+
EXPOSE_DC(string, String)
278+
252279
// Other methods
253280
.def("get_virtual_func",
254281
&CPointer::GetVirtualFunc,
255-
"Returns the address (as a CPointer instance) of a virtual function at the given index.",
282+
"Returns the address (as a Pointer instance) of a virtual function at the given index.",
256283
args("index"),
257284
manage_new_object_policy()
258285
)

0 commit comments

Comments
 (0)