diff options
| author | serpilliere <serpilliere@users.noreply.github.com> | 2019-05-29 13:18:49 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-29 13:18:49 +0200 |
| commit | 338dd94dbb83ed12467e6e2465308a35d08734d8 (patch) | |
| tree | 54819e8e88eb908e185a73f45b115da3e918a9e2 | |
| parent | ab673c1c26d2ab63890ed9ee2777faa778df7b64 (diff) | |
| parent | a15b03b615f9690e852fd8e58c198f50aabf3c63 (diff) | |
| download | miasm-338dd94dbb83ed12467e6e2465308a35d08734d8.tar.gz miasm-338dd94dbb83ed12467e6e2465308a35d08734d8.zip | |
Merge pull request #1016 from WilliamBruneau/fix_python_3_getset_reg
Fix PyGetInt for python 3
| -rw-r--r-- | miasm/jitter/JitCore.c | 40 | ||||
| -rw-r--r-- | miasm/jitter/JitCore.h | 101 | ||||
| -rw-r--r-- | miasm/jitter/Jitllvm.c | 2 | ||||
| -rw-r--r-- | miasm/jitter/arch/JitCore_aarch64.c | 39 | ||||
| -rw-r--r-- | miasm/jitter/arch/JitCore_arm.c | 39 | ||||
| -rw-r--r-- | miasm/jitter/arch/JitCore_mep.c | 37 | ||||
| -rw-r--r-- | miasm/jitter/arch/JitCore_mips32.c | 39 | ||||
| -rw-r--r-- | miasm/jitter/arch/JitCore_msp430.c | 40 | ||||
| -rw-r--r-- | miasm/jitter/arch/JitCore_ppc32.c | 37 | ||||
| -rw-r--r-- | miasm/jitter/arch/JitCore_x86.c | 67 | ||||
| -rw-r--r-- | miasm/jitter/compat_py23.h | 120 | ||||
| -rw-r--r-- | miasm/jitter/emulatedsymbexec.py | 5 | ||||
| -rw-r--r-- | miasm/jitter/jitcore_cc_base.py | 2 | ||||
| -rw-r--r-- | miasm/jitter/vm_mngr_py.c | 58 | ||||
| -rw-r--r-- | miasm/jitter/vm_mngr_py.h | 4 | ||||
| -rw-r--r-- | setup.py | 7 | ||||
| -rw-r--r-- | test/jitter/jitcore.py | 47 | ||||
| -rwxr-xr-x | test/test_all.py | 1 |
18 files changed, 241 insertions, 444 deletions
diff --git a/miasm/jitter/JitCore.c b/miasm/jitter/JitCore.c index 37b69aa3..1ba082c5 100644 --- a/miasm/jitter/JitCore.c +++ b/miasm/jitter/JitCore.c @@ -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" @@ -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; -} diff --git a/miasm/jitter/JitCore.h b/miasm/jitter/JitCore.h index 85be57da..7b7f6c13 100644 --- a/miasm/jitter/JitCore.h +++ b/miasm/jitter/JitCore.h @@ -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; \ } @@ -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)){ \ @@ -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 @@ -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); @@ -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); diff --git a/miasm/jitter/Jitllvm.c b/miasm/jitter/Jitllvm.c index 7617954b..3420c254 100644 --- a/miasm/jitter/Jitllvm.c +++ b/miasm/jitter/Jitllvm.c @@ -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" diff --git a/miasm/jitter/arch/JitCore_aarch64.c b/miasm/jitter/arch/JitCore_aarch64.c index c1a89285..7961f3cc 100644 --- a/miasm/jitter/arch/JitCore_aarch64.c +++ b/miasm/jitter/arch/JitCore_aarch64.c @@ -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" @@ -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 */ }; @@ -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 */ }; diff --git a/miasm/jitter/arch/JitCore_arm.c b/miasm/jitter/arch/JitCore_arm.c index 13dcb3f4..b39ae4d2 100644 --- a/miasm/jitter/arch/JitCore_arm.c +++ b/miasm/jitter/arch/JitCore_arm.c @@ -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" @@ -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; @@ -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 */ }; diff --git a/miasm/jitter/arch/JitCore_mep.c b/miasm/jitter/arch/JitCore_mep.c index a69110bc..286a12ac 100644 --- a/miasm/jitter/arch/JitCore_mep.c +++ b/miasm/jitter/arch/JitCore_mep.c @@ -8,8 +8,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 "JitCore_mep.h" @@ -269,39 +269,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 = 0x1337; - - if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer)) - return NULL; - - 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 */ }; @@ -314,8 +281,6 @@ static PyMethodDef JitCpu_methods[] = { {"set_gpreg", (PyCFunction)cpu_set_gpreg, METH_VARARGS, "X"}, {"get_exception", (PyCFunction)cpu_get_exception, METH_VARARGS, "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 */ }; diff --git a/miasm/jitter/arch/JitCore_mips32.c b/miasm/jitter/arch/JitCore_mips32.c index ce894e2a..dc576dfb 100644 --- a/miasm/jitter/arch/JitCore_mips32.c +++ b/miasm/jitter/arch/JitCore_mips32.c @@ -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_mips32.h" @@ -228,39 +228,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 */ @@ -279,10 +246,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 */ }; diff --git a/miasm/jitter/arch/JitCore_msp430.c b/miasm/jitter/arch/JitCore_msp430.c index 3716756e..2bb51c68 100644 --- a/miasm/jitter/arch/JitCore_msp430.c +++ b/miasm/jitter/arch/JitCore_msp430.c @@ -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 "JitCore_msp430.h" @@ -204,40 +204,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 */ }; @@ -257,10 +223,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 */ }; diff --git a/miasm/jitter/arch/JitCore_ppc32.c b/miasm/jitter/arch/JitCore_ppc32.c index 49dc0b1e..086b140f 100644 --- a/miasm/jitter/arch/JitCore_ppc32.c +++ b/miasm/jitter/arch/JitCore_ppc32.c @@ -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 "JitCore_ppc32.h" @@ -161,39 +161,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 = 0x1337; - - if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer)) - return NULL; - - 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 */ }; @@ -207,8 +174,6 @@ static PyMethodDef JitCpu_methods[] = { {"get_exception", (PyCFunction)cpu_get_exception, METH_VARARGS, "X"}, {"set_exception", (PyCFunction)cpu_set_exception, METH_VARARGS, "X"}, {"get_spr_access", (PyCFunction)cpu_get_spr_access, METH_VARARGS, "X"}, - {"set_mem", (PyCFunction)vm_set_mem, METH_VARARGS, "X"}, - {"get_mem", (PyCFunction)vm_get_mem, METH_VARARGS, "X"}, {NULL} /* Sentinel */ }; diff --git a/miasm/jitter/arch/JitCore_x86.c b/miasm/jitter/arch/JitCore_x86.c index 608893a7..361b18b4 100644 --- a/miasm/jitter/arch/JitCore_x86.c +++ b/miasm/jitter/arch/JitCore_x86.c @@ -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_x86.h" @@ -209,13 +209,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) case 128: { bn_t bn; - int j; PyObject* py_long = d_value; - PyObject* py_long_new; - PyObject* py_tmp; - PyObject* cst_32; - PyObject* cst_ffffffff; - uint64_t tmp; #if PY_MAJOR_VERSION >= 3 @@ -227,6 +221,8 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) RAISE(PyExc_TypeError,"arg must be int"); } #else + uint64_t tmp; + if (PyInt_Check(py_long)){ tmp = (uint64_t)PyInt_AsLong(py_long); py_long = PyLong_FromLong((long)tmp); @@ -240,25 +236,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) } #endif - - 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); - tmp = PyLong_AsUnsignedLongMask(py_tmp); - Py_DECREF(py_tmp); - bn = bignum_lshift(bn, 32); - bn = bignum_or(bn, bignum_from_uint64(tmp)); - - py_long_new = PyObject_CallMethod(py_long, "__rshift__", "O", cst_32); - Py_DECREF(py_long); - py_long = py_long_new; - } - Py_DECREF(py_long); - Py_DECREF(cst_32); - Py_DECREF(cst_ffffffff); + bn = PyLong_to_bn(py_long); *(bn_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset) = bignum_mask(bn, 128); } break; @@ -482,39 +460,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 */ }; @@ -538,10 +483,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"}, {"get_interrupt_num", (PyCFunction)cpu_get_interrupt_num, METH_VARARGS, "X"}, {"set_interrupt_num", (PyCFunction)cpu_set_interrupt_num, METH_VARARGS, diff --git a/miasm/jitter/compat_py23.h b/miasm/jitter/compat_py23.h index 936c08f3..6dce7818 100644 --- a/miasm/jitter/compat_py23.h +++ b/miasm/jitter/compat_py23.h @@ -2,15 +2,35 @@ #define __COMPAT_PY23_H__ +#include "bn.h" #if PY_MAJOR_VERSION >= 3 #define PyGetInt_uint_t(size_type, item, value) \ if (PyLong_Check(item)) { \ - unsigned long long tmp; \ - tmp = PyLong_AsUnsignedLongLong(item); \ - if ( tmp > (size_type) -1) { \ + Py_INCREF(item); \ + PyObject* py_long = item; \ + PyObject* py_long_new; \ + bn_t bn; \ + uint64_t tmp; \ + int neg = 0; \ + \ + if (Py_SIZE(py_long) < 0) { \ + neg = 1; \ + py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \ + Py_DECREF(py_long); \ + py_long = py_long_new; \ + } \ + \ + bn = PyLong_to_bn(py_long); \ + \ + bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \ + if (bignum_is_inf_equal_unsigned(mask_bn, bn)) { \ RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \ + } \ + if (neg) { \ + bn = bignum_sub(mask_bn, bn); \ } \ + tmp = bignum_to_uint64(bn); \ value = (size_type) tmp; \ } \ else{ \ @@ -20,12 +40,31 @@ #define PyGetInt_uint_t_retneg(size_type, item, value) \ if (PyLong_Check(item)) { \ - unsigned long long tmp; \ - tmp = PyLong_AsUnsignedLongLong(item); \ - if ( tmp > (size_type) -1) { \ + Py_INCREF(item); \ + PyObject* py_long = item; \ + PyObject* py_long_new; \ + bn_t bn; \ + uint64_t tmp; \ + int neg = 0; \ + \ + if (Py_SIZE(py_long) < 0) { \ + neg = 1; \ + py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \ + Py_DECREF(py_long); \ + py_long = py_long_new; \ + } \ + \ + bn = PyLong_to_bn(py_long); \ + \ + bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \ + if (bignum_is_inf_equal_unsigned(mask_bn, bn)) { \ PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \ return -1; \ + } \ + if (neg) { \ + bn = bignum_sub(mask_bn, bn); \ } \ + tmp = bignum_to_uint64(bn); \ value = (size_type) tmp; \ } \ else{ \ @@ -45,17 +84,42 @@ if (PyInt_Check(item)) { \ long tmp; \ tmp = PyInt_AsLong(item); \ - if ( tmp > (size_type) -1) { \ + \ + if (Py_SIZE(item) < 0) { \ + if (-tmp > ((size_type) -1)) { \ + RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \ + } \ + } \ + else if (tmp > (size_type) -1) { \ RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \ } \ value = (size_type) tmp; \ } \ else if (PyLong_Check(item)){ \ - unsigned long long tmp; \ - tmp = PyLong_AsUnsignedLongLong(item); \ - if ( tmp > (size_type) -1) { \ + Py_INCREF(item); \ + PyObject* py_long = item; \ + PyObject* py_long_new; \ + bn_t bn; \ + uint64_t tmp; \ + int neg = 0; \ + \ + if (Py_SIZE(py_long) < 0) { \ + neg = 1; \ + py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \ + Py_DECREF(py_long); \ + py_long = py_long_new; \ + } \ + \ + bn = PyLong_to_bn(py_long); \ + \ + bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \ + if (bignum_is_inf_equal_unsigned(mask_bn, bn)) { \ RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \ + } \ + if (neg) { \ + bn = bignum_sub(mask_bn, bn); \ } \ + tmp = bignum_to_uint64(bn); \ value = (size_type) tmp; \ } \ else{ \ @@ -66,20 +130,46 @@ #define PyGetInt_uint_t_retneg(size_type, item, value) \ if (PyInt_Check(item)) { \ long tmp; \ - tmp = PyLong_AsLong(item); \ - if ( tmp > (size_type) -1) { \ + tmp = PyInt_AsLong(item); \ + \ + if (Py_SIZE(item) < 0) { \ + if (-tmp > ((size_type) -1)) { \ + PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \ + return -1; \ + } \ + } \ + else if (tmp > (size_type) -1) { \ PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \ return -1; \ } \ value = (size_type) tmp; \ } \ else if (PyLong_Check(item)){ \ - unsigned long long tmp; \ - tmp = PyLong_AsUnsignedLongLong(item); \ - if ( tmp > (size_type) -1) { \ + Py_INCREF(item); \ + PyObject* py_long = item; \ + PyObject* py_long_new; \ + bn_t bn; \ + uint64_t tmp; \ + int neg = 0; \ + \ + if (Py_SIZE(py_long) < 0) { \ + neg = 1; \ + py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \ + Py_DECREF(py_long); \ + py_long = py_long_new; \ + } \ + \ + bn = PyLong_to_bn(py_long); \ + \ + bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \ + if (bignum_is_inf_equal_unsigned(mask_bn, bn)) { \ PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \ return -1; \ + } \ + if (neg) { \ + bn = bignum_sub(mask_bn, bn); \ } \ + tmp = bignum_to_uint64(bn); \ value = (size_type) tmp; \ } \ else{ \ diff --git a/miasm/jitter/emulatedsymbexec.py b/miasm/jitter/emulatedsymbexec.py index 35986fb9..aacfba9f 100644 --- a/miasm/jitter/emulatedsymbexec.py +++ b/miasm/jitter/emulatedsymbexec.py @@ -75,7 +75,7 @@ class EmulatedSymbExec(SymbolicExecutionEngine): return super(EmulatedSymbExec, self).mem_read(expr_mem) addr = int(addr) size = expr_mem.size // 8 - value = self.cpu.get_mem(addr, size) + value = self.vm.get_mem(addr, size) if self.vm.is_little_endian(): value = value[::-1] self.vm.add_mem_read(addr, size) @@ -107,8 +107,7 @@ class EmulatedSymbExec(SymbolicExecutionEngine): content = content[::-1] # Write in VmMngr context - self.cpu.set_mem(addr, content) - self.vm.add_mem_write(addr, len(content)) + self.vm.set_mem(addr, content) # Interaction symbexec <-> jitter def update_cpu_from_engine(self): diff --git a/miasm/jitter/jitcore_cc_base.py b/miasm/jitter/jitcore_cc_base.py index 4ec0e358..995c458b 100644 --- a/miasm/jitter/jitcore_cc_base.py +++ b/miasm/jitter/jitcore_cc_base.py @@ -18,8 +18,8 @@ def gen_core(arch, attrib): txt += '#include "%s/queue.h"\n' % lib_dir txt += '#include "%s/op_semantics.h"\n' % lib_dir txt += '#include "%s/vm_mngr.h"\n' % lib_dir - txt += '#include "%s/vm_mngr_py.h"\n' % lib_dir txt += '#include "%s/bn.h"\n' % lib_dir + txt += '#include "%s/vm_mngr_py.h"\n' % lib_dir txt += '#include "%s/JitCore.h"\n' % lib_dir txt += '#include "%s/arch/JitCore_%s.h"\n' % (lib_dir, arch.name) diff --git a/miasm/jitter/vm_mngr_py.c b/miasm/jitter/vm_mngr_py.c index 9ec87b0d..69e62fef 100644 --- a/miasm/jitter/vm_mngr_py.c +++ b/miasm/jitter/vm_mngr_py.c @@ -23,6 +23,7 @@ #include "compat_py23.h" #include "queue.h" #include "vm_mngr.h" +#include "bn.h" #include "vm_mngr_py.h" #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -1010,3 +1011,60 @@ MOD_INIT(VmMngr) RET_MODULE; } + +bn_t PyLong_to_bn(PyObject* py_long) +{ + int j; + uint64_t tmp_mask; + PyObject* py_tmp; + PyObject* py_long_tmp; + PyObject* cst_32; + PyObject* cst_ffffffff; + bn_t bn; + + 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_tmp = PyObject_CallMethod(py_long, "__rshift__", "O", cst_32); + Py_DECREF(py_long); + py_long = py_long_tmp; + tmp_mask = PyLong_AsUnsignedLongLongMask(py_tmp); + Py_DECREF(py_tmp); + bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp_mask), 8 * j)); + } + + Py_DECREF(cst_32); + Py_DECREF(cst_ffffffff); + + return bn; +} + +PyObject* bn_to_PyLong(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); + + 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); + + return py_long; +} diff --git a/miasm/jitter/vm_mngr_py.h b/miasm/jitter/vm_mngr_py.h index e2e43c65..a8f7dcd0 100644 --- a/miasm/jitter/vm_mngr_py.h +++ b/miasm/jitter/vm_mngr_py.h @@ -11,5 +11,7 @@ typedef struct { vm_mngr_t vm_mngr; } VmMngr; - #endif// VM_MNGR_PY_H + +bn_t PyLong_to_bn(PyObject* py_long); +PyObject* bn_to_PyLong(bn_t bn); diff --git a/setup.py b/setup.py index 983ac99a..e8ea7b3a 100644 --- a/setup.py +++ b/setup.py @@ -57,6 +57,7 @@ def buil_all(): [ "miasm/jitter/JitCore.c", "miasm/jitter/vm_mngr.c", + "miasm/jitter/vm_mngr_py.c", "miasm/jitter/op_semantics.c", "miasm/jitter/bn.c", "miasm/jitter/arch/JitCore_x86.c" @@ -67,6 +68,7 @@ def buil_all(): [ "miasm/jitter/JitCore.c", "miasm/jitter/vm_mngr.c", + "miasm/jitter/vm_mngr_py.c", "miasm/jitter/op_semantics.c", "miasm/jitter/bn.c", "miasm/jitter/arch/JitCore_arm.c" @@ -77,6 +79,7 @@ def buil_all(): [ "miasm/jitter/JitCore.c", "miasm/jitter/vm_mngr.c", + "miasm/jitter/vm_mngr_py.c", "miasm/jitter/op_semantics.c", "miasm/jitter/bn.c", "miasm/jitter/arch/JitCore_aarch64.c" @@ -87,6 +90,7 @@ def buil_all(): [ "miasm/jitter/JitCore.c", "miasm/jitter/vm_mngr.c", + "miasm/jitter/vm_mngr_py.c", "miasm/jitter/op_semantics.c", "miasm/jitter/bn.c", "miasm/jitter/arch/JitCore_msp430.c" @@ -97,6 +101,7 @@ def buil_all(): [ "miasm/jitter/JitCore.c", "miasm/jitter/vm_mngr.c", + "miasm/jitter/vm_mngr_py.c", "miasm/jitter/bn.c", "miasm/jitter/arch/JitCore_mep.c" ] @@ -106,6 +111,7 @@ def buil_all(): [ "miasm/jitter/JitCore.c", "miasm/jitter/vm_mngr.c", + "miasm/jitter/vm_mngr_py.c", "miasm/jitter/op_semantics.c", "miasm/jitter/bn.c", "miasm/jitter/arch/JitCore_mips32.c" @@ -116,6 +122,7 @@ def buil_all(): [ "miasm/jitter/JitCore.c", "miasm/jitter/vm_mngr.c", + "miasm/jitter/vm_mngr_py.c", "miasm/jitter/op_semantics.c", "miasm/jitter/bn.c", "miasm/jitter/arch/JitCore_ppc32.c" diff --git a/test/jitter/jitcore.py b/test/jitter/jitcore.py new file mode 100644 index 00000000..75360542 --- /dev/null +++ b/test/jitter/jitcore.py @@ -0,0 +1,47 @@ +import sys + +from miasm.analysis.machine import Machine +machine = Machine("x86_64") +jitter = machine.jitter(sys.argv[1]) + +jitter.cpu.RAX = 16565615892967251934 +assert jitter.cpu.RAX == 16565615892967251934 + +jitter.cpu.RAX = -1 +assert jitter.cpu.RAX == 0xffffffffffffffff + +jitter.cpu.RAX = -2 +assert jitter.cpu.RAX == 0xfffffffffffffffe + +jitter.cpu.EAX = -2 +assert jitter.cpu.EAX == 0xfffffffe + +jitter.cpu.RAX = -0xffffffffffffffff +assert jitter.cpu.RAX == 1 + +try: + jitter.cpu.RAX = 0x1ffffffffffffffff +except TypeError: + pass +else: + raise Exception("Should see that 0x1ffffffffffffffff is too big for RAX") + +try: + jitter.cpu.RAX = 0x10000000000000000 +except TypeError: + pass +else: + raise Exception("Should see that 0x10000000000000000 is too big for RAX") + +jitter.cpu.EAX = -0xefffffff +assert jitter.cpu.EAX == 0x10000001 + +jitter.cpu.EAX = -0xFFFFFFFF +assert jitter.cpu.EAX == 1 + +try: + jitter.cpu.EAX = -0x1ffffffff +except TypeError: + pass +else: + raise Exception("Should see that -0x1ffffffff is too big for EAX") diff --git a/test/test_all.py b/test/test_all.py index 1f729a71..ce223211 100755 --- a/test/test_all.py +++ b/test/test_all.py @@ -457,6 +457,7 @@ for i, test_args in enumerate(test_args): for script in ["jitload.py", "vm_mngr.py", "jit_options.py", + "jitcore.py", "test_post_instr.py", "bad_block.py", "jmp_out_mem.py", |