about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--miasm2/jitter/JitCore.h34
-rw-r--r--miasm2/jitter/arch/JitCore_x86.c7
2 files changed, 19 insertions, 22 deletions
diff --git a/miasm2/jitter/JitCore.h b/miasm2/jitter/JitCore.h
index 0eaf4939..d85b71d9 100644
--- a/miasm2/jitter/JitCore.h
+++ b/miasm2/jitter/JitCore.h
@@ -36,10 +36,11 @@
 	}								\
 
 
-#define getset_reg_bn(regname)						\
+#define getset_reg_bn(regname, size)					\
 	static PyObject *JitCpu_get_ ## regname  (JitCpu *self, void *closure) \
 	{								\
 		bn_t bn;						\
+		int j;							\
 		PyObject* py_long;					\
 		PyObject* py_long_new;					\
 		PyObject* py_tmp;					\
@@ -48,13 +49,13 @@
 		py_long = PyLong_FromLong(0);				\
 		cst_32 = PyLong_FromLong(32);				\
 		bn = ((vm_cpu_t*)(self->cpu))->  regname;		\
-		while (!bignum_is_zero(bn)) {				\
-			tmp = bignum_to_uint64(bignum_mask(bn, 32)) & 0xffffffff; \
-			bn = bignum_rshift(bn, 32);			\
+		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(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_long = PyObject_CallMethod(py_long_new, "__add__", "O", py_tmp); \
 			Py_DECREF(py_long_new);				\
 			Py_DECREF(py_tmp);				\
 		}							\
@@ -82,7 +83,7 @@
 			/* Increment ref as we will decement it next */	\
 			Py_INCREF(py_long);				\
 		}							\
-		else{							\
+		else {							\
 			PyErr_SetString(PyExc_TypeError, "Arg must be int"); \
 			return -1;					\
 		}							\
@@ -93,23 +94,21 @@
 									\
 		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;				\
+			tmp = PyLong_AsUnsignedLongMask(py_tmp);	\
+			Py_DECREF(py_tmp);				\
+			bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \
 		}							\
 									\
-		((vm_cpu_t*)(self->cpu))->  regname   = bn;		\
+		((vm_cpu_t*)(self->cpu))->  regname   = bignum_mask(bn, (size)); \
 		Py_DECREF(py_long);					\
 		Py_DECREF(cst_32);					\
 		Py_DECREF(cst_ffffffff);				\
 		return 0;						\
 	}
 
-
 #define getset_reg_u64(regname)						\
 	static PyObject *JitCpu_get_ ## regname  (JitCpu *self, void *closure) \
 	{								\
@@ -158,8 +157,9 @@
 	} while(0);
 
 
-#define get_reg_bn(reg)  do {						\
+#define get_reg_bn(reg, size)  do {					\
 		bn_t bn;						\
+		int j;							\
 		PyObject* py_long;					\
 		PyObject* py_long_new;					\
 		PyObject* py_tmp;					\
@@ -168,10 +168,10 @@
 		py_long = PyLong_FromLong(0);				\
 		cst_32 = PyLong_FromLong(32);				\
 		bn = ((vm_cpu_t*)(self->cpu))->  reg;			\
-		while (!bignum_is_zero(bn)) {				\
-			tmp = bignum_to_uint64(bignum_mask(bn, 32)) & 0xffffffff; \
-			bn = bignum_rshift(bn, 32);			\
-			py_tmp = PyLong_FromLong(tmp);			\
+		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(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); \
diff --git a/miasm2/jitter/arch/JitCore_x86.c b/miasm2/jitter/arch/JitCore_x86.c
index b8d8270d..4e52ec85 100644
--- a/miasm2/jitter/arch/JitCore_x86.c
+++ b/miasm2/jitter/arch/JitCore_x86.c
@@ -220,6 +220,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args)
 						    py_long = PyLong_FromLong((long)tmp);
 					    } else if (PyLong_Check(py_long)){
 						    /* Already PyLong */
+						    /* Increment ref as we will decement it next */
 						    Py_INCREF(py_long);
 					    }
 					    else{
@@ -227,7 +228,6 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args)
 					    }
 
 
-
 					    cst_ffffffff = PyLong_FromLong(0xffffffff);
 					    cst_32 = PyLong_FromLong(32);
 					    bn = bignum_from_int(0);
@@ -246,10 +246,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args)
 					    Py_DECREF(py_long);
 					    Py_DECREF(cst_32);
 					    Py_DECREF(cst_ffffffff);
-
-
-
-					    *(bn_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset) = bn;
+					    *(bn_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset) = bignum_mask(bn, 128);
 				    }
 				    break;
 		    }