diff options
| author | William Bruneau <william.bruneau@epfedu.fr> | 2019-05-15 11:17:24 +0200 |
|---|---|---|
| committer | William Bruneau <william.bruneau@epfedu.fr> | 2019-05-15 12:32:32 +0200 |
| commit | bba990f43b73cbae980c70f6dc3a82511fcac3be (patch) | |
| tree | e5290dc6a1943fd803fb57304b579d84431169a5 | |
| parent | e3288bca2ef9ec016cc972233622b43a17a47d3a (diff) | |
| download | miasm-bba990f43b73cbae980c70f6dc3a82511fcac3be.tar.gz miasm-bba990f43b73cbae980c70f6dc3a82511fcac3be.zip | |
PyLong_to_bn
| -rw-r--r-- | miasm/jitter/JitCore.h | 45 | ||||
| -rw-r--r-- | miasm/jitter/arch/JitCore_x86.c | 28 | ||||
| -rw-r--r-- | miasm/jitter/compat_py23.h | 93 | ||||
| -rw-r--r-- | miasm/jitter/vm_mngr_py.c | 31 | ||||
| -rw-r--r-- | miasm/jitter/vm_mngr_py.h | 3 |
5 files changed, 47 insertions, 153 deletions
diff --git a/miasm/jitter/JitCore.h b/miasm/jitter/JitCore.h index 85be57da..ecd1efb1 100644 --- a/miasm/jitter/JitCore.h +++ b/miasm/jitter/JitCore.h @@ -51,37 +51,16 @@ 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; \ } @@ -117,12 +96,7 @@ 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 +109,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 diff --git a/miasm/jitter/arch/JitCore_x86.c b/miasm/jitter/arch/JitCore_x86.c index 608893a7..35cf25a5 100644 --- a/miasm/jitter/arch/JitCore_x86.c +++ b/miasm/jitter/arch/JitCore_x86.c @@ -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; diff --git a/miasm/jitter/compat_py23.h b/miasm/jitter/compat_py23.h index 416a3a68..6dce7818 100644 --- a/miasm/jitter/compat_py23.h +++ b/miasm/jitter/compat_py23.h @@ -3,21 +3,15 @@ #include "bn.h" + #if PY_MAJOR_VERSION >= 3 #define PyGetInt_uint_t(size_type, item, value) \ if (PyLong_Check(item)) { \ Py_INCREF(item); \ PyObject* py_long = item; \ - bn_t bn; \ - int j; \ PyObject* py_long_new; \ - PyObject* py_tmp; \ - PyObject* cst_32; \ - PyObject* cst_ffffffff; \ + bn_t bn; \ uint64_t tmp; \ - cst_ffffffff = PyLong_FromLong(0xffffffff); \ - cst_32 = PyLong_FromLong(32); \ - bn = bignum_from_int(0); \ int neg = 0; \ \ if (Py_SIZE(py_long) < 0) { \ @@ -27,19 +21,7 @@ py_long = py_long_new; \ } \ \ - 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_AsUnsignedLongLongMask(py_tmp); \ - Py_DECREF(py_tmp); \ - bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \ - } \ - \ - Py_DECREF(py_long); \ - Py_DECREF(cst_32); \ - Py_DECREF(cst_ffffffff); \ + 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)) { \ @@ -60,16 +42,9 @@ if (PyLong_Check(item)) { \ Py_INCREF(item); \ PyObject* py_long = item; \ - bn_t bn; \ - int j; \ PyObject* py_long_new; \ - PyObject* py_tmp; \ - PyObject* cst_32; \ - PyObject* cst_ffffffff; \ + bn_t bn; \ uint64_t tmp; \ - cst_ffffffff = PyLong_FromLong(0xffffffff); \ - cst_32 = PyLong_FromLong(32); \ - bn = bignum_from_int(0); \ int neg = 0; \ \ if (Py_SIZE(py_long) < 0) { \ @@ -79,19 +54,7 @@ py_long = py_long_new; \ } \ \ - 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_AsUnsignedLongLongMask(py_tmp); \ - Py_DECREF(py_tmp); \ - bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \ - } \ - \ - Py_DECREF(py_long); \ - Py_DECREF(cst_32); \ - Py_DECREF(cst_ffffffff); \ + 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)) { \ @@ -135,16 +98,9 @@ else if (PyLong_Check(item)){ \ Py_INCREF(item); \ PyObject* py_long = item; \ - bn_t bn; \ - int j; \ PyObject* py_long_new; \ - PyObject* py_tmp; \ - PyObject* cst_32; \ - PyObject* cst_ffffffff; \ + bn_t bn; \ uint64_t tmp; \ - cst_ffffffff = PyLong_FromLong(0xffffffff); \ - cst_32 = PyLong_FromLong(32); \ - bn = bignum_from_int(0); \ int neg = 0; \ \ if (Py_SIZE(py_long) < 0) { \ @@ -154,19 +110,7 @@ py_long = py_long_new; \ } \ \ - 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_AsUnsignedLongLongMask(py_tmp); \ - Py_DECREF(py_tmp); \ - bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \ - } \ - \ - Py_DECREF(py_long); \ - Py_DECREF(cst_32); \ - Py_DECREF(cst_ffffffff); \ + 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)) { \ @@ -203,16 +147,9 @@ else if (PyLong_Check(item)){ \ Py_INCREF(item); \ PyObject* py_long = item; \ - bn_t bn; \ - int j; \ PyObject* py_long_new; \ - PyObject* py_tmp; \ - PyObject* cst_32; \ - PyObject* cst_ffffffff; \ + bn_t bn; \ uint64_t tmp; \ - cst_ffffffff = PyLong_FromLong(0xffffffff); \ - cst_32 = PyLong_FromLong(32); \ - bn = bignum_from_int(0); \ int neg = 0; \ \ if (Py_SIZE(py_long) < 0) { \ @@ -222,19 +159,7 @@ py_long = py_long_new; \ } \ \ - 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_AsUnsignedLongLongMask(py_tmp); \ - Py_DECREF(py_tmp); \ - bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \ - } \ - \ - Py_DECREF(py_long); \ - Py_DECREF(cst_32); \ - Py_DECREF(cst_ffffffff); \ + 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)) { \ diff --git a/miasm/jitter/vm_mngr_py.c b/miasm/jitter/vm_mngr_py.c index 9ec87b0d..4684f8f8 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,33 @@ 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; +} diff --git a/miasm/jitter/vm_mngr_py.h b/miasm/jitter/vm_mngr_py.h index e2e43c65..4e10803b 100644 --- a/miasm/jitter/vm_mngr_py.h +++ b/miasm/jitter/vm_mngr_py.h @@ -11,5 +11,6 @@ typedef struct { vm_mngr_t vm_mngr; } VmMngr; - #endif// VM_MNGR_PY_H + +bn_t PyLong_to_bn(PyObject* py_long); |