Skip to content

Commit

Permalink
Merge pull request #1016 from WilliamBruneau/fix_python_3_getset_reg
Browse files Browse the repository at this point in the history
Fix PyGetInt for python 3
  • Loading branch information
serpilliere authored May 29, 2019
2 parents ab673c1 + a15b03b commit 338dd94
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 444 deletions.
40 changes: 1 addition & 39 deletions miasm/jitter/JitCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "compat_py23.h"
#include "queue.h"
#include "vm_mngr.h"
#include "vm_mngr_py.h"
#include "bn.h"
#include "vm_mngr_py.h"
#include "JitCore.h"


Expand Down Expand Up @@ -226,41 +226,3 @@ void MEM_WRITE_INT_BN_FROM_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* pt
memcpy(&val, ptr, size / 8);
MEM_WRITE_INT_BN(jitcpu, size, addr, val);
}



PyObject* vm_get_mem(JitCpu *self, PyObject* args)
{
PyObject *py_addr;
PyObject *py_len;

uint64_t addr;
uint64_t size;
size_t size_st;
PyObject *obj_out;
char * buf_out;
int ret;

if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_len))
return NULL;

PyGetInt_uint64_t(py_addr, addr);
PyGetInt_uint64_t(py_len, size);


if (size > SSIZE_MAX) {
fprintf(stderr, "Read size wider than supported system\n");
exit(EXIT_FAILURE);
}
size_st = (size_t)size;

ret = vm_read_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, &buf_out, size_st);
if (ret < 0) {
PyErr_SetString(PyExc_RuntimeError, "cannot find address");
return NULL;
}

obj_out = PyBytes_FromStringAndSize(buf_out, (Py_ssize_t)size_st);
free(buf_out);
return obj_out;
}
101 changes: 5 additions & 96 deletions miasm/jitter/JitCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,26 @@
static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \
{ \
bn_t bn; \
int j; \
PyObject* py_long; \
PyObject* py_long_new; \
PyObject* py_tmp; \
PyObject* cst_32; \
uint64_t tmp; \
py_long = PyLong_FromLong(0); \
cst_32 = PyLong_FromLong(32); \
bn = (self->cpu)->regname; \
bn = bignum_mask(bn, (size)); \
for (j = BN_BYTE_SIZE - 4; j >= 0 ; j -= 4) { \
tmp = bignum_to_uint64(bignum_mask(bignum_rshift(bn, 8 * j), 32)); \
py_tmp = PyLong_FromUnsignedLong((unsigned long)tmp); \
py_long_new = PyObject_CallMethod(py_long, "__lshift__", "O", cst_32); \
Py_DECREF(py_long); \
py_long = PyObject_CallMethod(py_long_new, "__add__", "O", py_tmp); \
Py_DECREF(py_long_new); \
Py_DECREF(py_tmp); \
} \
Py_DECREF(cst_32); \
py_long = bn_to_PyLong(bn); \
return py_long; \
} \
\
static PyObject *JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \
{ \
bn_t bn; \
int j; \
PyObject* py_long = value; \
PyObject* py_long_new; \
PyObject* py_tmp; \
PyObject* cst_32; \
PyObject* cst_ffffffff; \
uint64_t tmp; \
if (PyLong_Check(py_long)){ \
Py_INCREF(py_long); \
} else { \
RAISE(PyExc_TypeError,"arg must be int"); \
} \
\
cst_ffffffff = PyLong_FromLong(0xffffffff); \
cst_32 = PyLong_FromLong(32); \
bn = bignum_from_int(0); \
\
for (j = 0; j < BN_BYTE_SIZE; j += 4) { \
py_tmp = PyObject_CallMethod(py_long, "__and__", "O", cst_ffffffff); \
py_long_new = PyObject_CallMethod(py_long, "__rshift__", "O", cst_32); \
Py_DECREF(py_long); \
py_long = py_long_new; \
tmp = PyLong_AsUnsignedLongMask(py_tmp); \
Py_DECREF(py_tmp); \
bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \
} \
bn = PyLong_to_bn(py_long); \
\
(self->cpu)->regname = bignum_mask(bn, (size)); \
Py_DECREF(py_long); \
Py_DECREF(cst_32); \
Py_DECREF(cst_ffffffff); \
return 0; \
}

Expand All @@ -91,38 +54,17 @@
static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \
{ \
bn_t bn; \
int j; \
PyObject* py_long; \
PyObject* py_long_new; \
PyObject* py_tmp; \
PyObject* cst_32; \
uint64_t tmp; \
py_long = PyLong_FromLong(0); \
cst_32 = PyLong_FromLong(32); \
bn = (self->cpu)->regname; \
bn = bignum_mask(bn, (size)); \
for (j = BN_BYTE_SIZE - 4; j >= 0 ; j -= 4) { \
tmp = bignum_to_uint64(bignum_mask(bignum_rshift(bn, 8 * j), 32)); \
py_tmp = PyLong_FromUnsignedLong((unsigned long)tmp); \
py_long_new = PyObject_CallMethod(py_long, "__lshift__", "O", cst_32); \
Py_DECREF(py_long); \
py_long = PyObject_CallMethod(py_long_new, "__add__", "O", py_tmp); \
Py_DECREF(py_long_new); \
Py_DECREF(py_tmp); \
} \
Py_DECREF(cst_32); \
py_long = bn_to_PyLong(bn); \
return py_long; \
} \
\
static PyObject *JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \
{ \
bn_t bn; \
int j; \
PyObject* py_long = value; \
PyObject* py_long_new; \
PyObject* py_tmp; \
PyObject* cst_32; \
PyObject* cst_ffffffff; \
uint64_t tmp; \
\
if (PyInt_Check(py_long)){ \
Expand All @@ -135,24 +77,9 @@
RAISE(PyExc_TypeError,"arg must be int"); \
} \
\
cst_ffffffff = PyLong_FromLong(0xffffffff); \
cst_32 = PyLong_FromLong(32); \
bn = bignum_from_int(0); \
\
for (j = 0; j < BN_BYTE_SIZE; j += 4) { \
py_tmp = PyObject_CallMethod(py_long, "__and__", "O", cst_ffffffff); \
py_long_new = PyObject_CallMethod(py_long, "__rshift__", "O", cst_32); \
Py_DECREF(py_long); \
py_long = py_long_new; \
tmp = PyLong_AsUnsignedLongMask(py_tmp); \
Py_DECREF(py_tmp); \
bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \
} \
bn = PyLong_to_bn(py_long); \
\
self->cpu->regname = bignum_mask(bn, (size)); \
Py_DECREF(py_long); \
Py_DECREF(cst_32); \
Py_DECREF(cst_ffffffff); \
return 0; \
}
#endif
Expand Down Expand Up @@ -231,28 +158,12 @@

#define get_reg_bn(reg, size) do { \
bn_t bn; \
int j; \
PyObject* py_long; \
PyObject* py_long_new; \
PyObject* py_tmp; \
PyObject* cst_32; \
uint64_t tmp; \
py_long = PyLong_FromLong(0); \
cst_32 = PyLong_FromLong(32); \
bn = self->cpu->reg; \
bn = bignum_mask(bn, size); \
for (j = BN_BYTE_SIZE - 4; j >= 0 ; j -= 4) { \
tmp = bignum_to_uint64(bignum_mask(bignum_rshift(bn, 8 * j), 32)); \
py_tmp = PyLong_FromUnsignedLong((unsigned long)tmp); \
py_long_new = PyObject_CallMethod(py_long, "__lshift__", "O", cst_32); \
Py_DECREF(py_long); \
py_long = PyObject_CallMethod(py_long_new, "__add__", "O", py_tmp); \
Py_DECREF(py_long_new); \
Py_DECREF(py_tmp); \
} \
py_long = bn_to_PyLong(bn); \
PyDict_SetItemString(dict, #reg, py_long); \
Py_DECREF(py_long); \
Py_DECREF(cst_32); \
} while(0);


Expand Down Expand Up @@ -315,8 +226,6 @@ _MIASM_EXPORT void MEM_WRITE_BN_INT(JitCpu* jitcpu, int size, bn_t addr, uint64_
_MIASM_EXPORT void MEM_WRITE_INT_BN(JitCpu* jitcpu, int size, uint64_t addr, bn_t src);


PyObject* vm_get_mem(JitCpu *self, PyObject* args);

_MIASM_EXPORT void MEM_LOOKUP_INT_BN_TO_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr);
_MIASM_EXPORT void MEM_WRITE_INT_BN_FROM_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr);

Expand Down
2 changes: 1 addition & 1 deletion miasm/jitter/Jitllvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "compat_py23.h"
#include "queue.h"
#include "vm_mngr.h"
#include "vm_mngr_py.h"
#include "bn.h"
#include "vm_mngr_py.h"
#include "JitCore.h"
// Needed to get the JitCpu.cpu offset, arch independent
#include "arch/JitCore_x86.h"
Expand Down
39 changes: 1 addition & 38 deletions miasm/jitter/arch/JitCore_aarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "../compat_py23.h"
#include "../queue.h"
#include "../vm_mngr.h"
#include "../vm_mngr_py.h"
#include "../bn.h"
#include "../vm_mngr_py.h"
#include "../JitCore.h"
#include "../op_semantics.h"
#include "JitCore_aarch64.h"
Expand Down Expand Up @@ -252,39 +252,6 @@ void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src)
}


PyObject* vm_set_mem(JitCpu *self, PyObject* args)
{
PyObject *py_addr;
PyObject *py_buffer;
Py_ssize_t py_length;

char * buffer;
Py_ssize_t pysize;
uint64_t addr;
int ret;

if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer))
RAISE(PyExc_TypeError,"Cannot parse arguments");

PyGetInt_uint64_t(py_addr, addr);

if(!PyBytes_Check(py_buffer))
RAISE(PyExc_TypeError,"arg must be bytes");

pysize = PyBytes_Size(py_buffer);
if (pysize < 0) {
RAISE(PyExc_TypeError,"Python error");
}
PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length);

ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize);
if (ret < 0)
RAISE(PyExc_TypeError,"arg must be str");

Py_INCREF(Py_None);
return Py_None;
}

static PyMemberDef JitCpu_members[] = {
{NULL} /* Sentinel */
};
Expand All @@ -304,10 +271,6 @@ static PyMethodDef JitCpu_methods[] = {
"X"},
{"set_exception", (PyCFunction)cpu_set_exception, METH_VARARGS,
"X"},
{"set_mem", (PyCFunction)vm_set_mem, METH_VARARGS,
"X"},
{"get_mem", (PyCFunction)vm_get_mem, METH_VARARGS,
"X"},
{NULL} /* Sentinel */
};

Expand Down
39 changes: 1 addition & 38 deletions miasm/jitter/arch/JitCore_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "../compat_py23.h"
#include "../queue.h"
#include "../vm_mngr.h"
#include "../vm_mngr_py.h"
#include "../bn.h"
#include "../vm_mngr_py.h"
#include "../JitCore.h"
#include "../op_semantics.h"
#include "JitCore_arm.h"
Expand Down Expand Up @@ -204,39 +204,6 @@ void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src)
vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src);
}

PyObject* vm_set_mem(JitCpu *self, PyObject* args)
{
PyObject *py_addr;
PyObject *py_buffer;
Py_ssize_t py_length;

char * buffer;
Py_ssize_t pysize;
uint64_t addr;
int ret;

if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer))
RAISE(PyExc_TypeError,"Cannot parse arguments");

PyGetInt_uint64_t(py_addr, addr);

if(!PyBytes_Check(py_buffer))
RAISE(PyExc_TypeError,"arg must be bytes");

pysize = PyBytes_Size(py_buffer);
if (pysize < 0) {
RAISE(PyExc_TypeError,"Python error");
}
PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length);

ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize);
if (ret < 0)
RAISE(PyExc_TypeError,"arg must be str");

Py_INCREF(Py_None);
return Py_None;
}

PyObject* cpu_set_interrupt_num(JitCpu* self, PyObject* args)
{
PyObject *item1;
Expand Down Expand Up @@ -280,10 +247,6 @@ static PyMethodDef JitCpu_methods[] = {
"X"},
{"set_interrupt_num", (PyCFunction)cpu_set_interrupt_num, METH_VARARGS,
"X"},
{"set_mem", (PyCFunction)vm_set_mem, METH_VARARGS,
"X"},
{"get_mem", (PyCFunction)vm_get_mem, METH_VARARGS,
"X"},
{NULL} /* Sentinel */
};

Expand Down
Loading

0 comments on commit 338dd94

Please sign in to comment.