diff options
30 files changed, 1005 insertions, 881 deletions
diff --git a/example/jitter/msp430.py b/example/jitter/msp430.py index 972a1fdc..927fb47b 100755 --- a/example/jitter/msp430.py +++ b/example/jitter/msp430.py @@ -31,7 +31,6 @@ machine = Machine("msp430") def jit_msp430_binary(args): filepath, entryp = args.binary, int(args.addr, 0) myjit = machine.jitter(jit_type = args.jitter) - myjit.init_stack() # Log level (if available with jitter engine) myjit.set_trace_log( diff --git a/miasm/analysis/data_flow.py b/miasm/analysis/data_flow.py index 541e6d24..c86bece5 100644 --- a/miasm/analysis/data_flow.py +++ b/miasm/analysis/data_flow.py @@ -1401,7 +1401,7 @@ def discard_phi_sources(ircfg, deleted_vars): srcs = set(expr for expr in src.args if expr not in deleted_vars) assert(srcs) if len(srcs) > 1: - todo[dst] = srcs + todo[dst] = ExprOp('Phi', *srcs) continue todo[dst] = srcs.pop() modified = True diff --git a/miasm/arch/x86/sem.py b/miasm/arch/x86/sem.py index 52858ad2..b924c44f 100644 --- a/miasm/arch/x86/sem.py +++ b/miasm/arch/x86/sem.py @@ -4781,66 +4781,71 @@ def palignr(ir, instr, dst, src, imm): return [m2_expr.ExprAssign(dst, result)], [] -def _signed_saturation(expr, dst_size): +def _signed_to_signed_saturation(expr, dst_size): """Saturate the expr @expr for @dst_size bit Signed saturation return MAX_INT / MIN_INT or value depending on the value """ assert expr.size > dst_size median = 1 << (dst_size - 1) + min_int = m2_expr.ExprInt(- median, dst_size) max_int = m2_expr.ExprInt(median - 1, dst_size) - signed = expr.msb() - value_unsigned = (expr ^ expr.mask) + m2_expr.ExprInt(1, expr.size) - # Re-use the sign bit - value = m2_expr.ExprCompose(expr[:dst_size - 1], signed) - - # Bit hack: to avoid a double signed comparison, use mask - # ie., in unsigned, 0xXY > 0x0f iff X is not null - - # if expr >s 0 - # if expr[dst_size - 1:] > 0: # bigger than max_int - # -> max_int - # else - # -> value - # else # negative - # if expr[dst_size:-1] > 0: # smaller than min_int - # -> value - # else - # -> min_int + + test_min_int = min_int.signExtend(expr.size) + test_max_int = max_int.signExtend(expr.size) + + value = expr[:dst_size] return m2_expr.ExprCond( - signed, - m2_expr.ExprCond(value_unsigned[dst_size - 1:], - min_int, - value), - m2_expr.ExprCond(expr[dst_size - 1:], - max_int, - value), + m2_expr.ExprOp( + m2_expr.TOK_INF_EQUAL_SIGNED, + expr, + test_min_int + ), + min_int, + m2_expr.ExprCond( + m2_expr.ExprOp( + m2_expr.TOK_INF_SIGNED, + expr, + test_max_int + ), + value, + max_int + ) ) -def _unsigned_saturation(expr, dst_size): +def _signed_to_unsigned_saturation(expr, dst_size): """Saturate the expr @expr for @dst_size bit Unsigned saturation return MAX_INT or value depending on the value """ assert expr.size > dst_size zero = m2_expr.ExprInt(0, dst_size) - max_int = m2_expr.ExprInt(-1, dst_size) - value = expr[:dst_size] - signed = expr.msb() + test_zero = m2_expr.ExprInt(0, expr.size) + max_int = m2_expr.ExprInt(-1, dst_size) + test_max_int = max_int.zeroExtend(expr.size) - # Bit hack: to avoid a double signed comparison, use mask - # ie., in unsigned, 0xXY > 0x0f iff X is not null + value = expr[:dst_size] return m2_expr.ExprCond( - signed, + m2_expr.ExprOp( + m2_expr.TOK_INF_EQUAL_SIGNED, + expr, + test_zero + ), zero, - m2_expr.ExprCond(expr[dst_size:], - max_int, - value), + m2_expr.ExprCond( + m2_expr.ExprOp( + m2_expr.TOK_INF_SIGNED, + expr, + test_max_int + ), + value, + max_int + ) ) @@ -4849,7 +4854,7 @@ def packsswb(ir, instr, dst, src): out = [] for source in [dst, src]: for start in range(0, dst.size, 16): - out.append(_signed_saturation(source[start:start + 16], 8)) + out.append(_signed_to_signed_saturation(source[start:start + 16], 8)) return [m2_expr.ExprAssign(dst, m2_expr.ExprCompose(*out))], [] @@ -4857,7 +4862,7 @@ def packssdw(ir, instr, dst, src): out = [] for source in [dst, src]: for start in range(0, dst.size, 32): - out.append(_signed_saturation(source[start:start + 32], 16)) + out.append(_signed_to_signed_saturation(source[start:start + 32], 16)) return [m2_expr.ExprAssign(dst, m2_expr.ExprCompose(*out))], [] @@ -4865,7 +4870,7 @@ def packuswb(ir, instr, dst, src): out = [] for source in [dst, src]: for start in range(0, dst.size, 16): - out.append(_unsigned_saturation(source[start:start + 16], 8)) + out.append(_signed_to_unsigned_saturation(source[start:start + 16], 8)) return [m2_expr.ExprAssign(dst, m2_expr.ExprCompose(*out))], [] @@ -4876,7 +4881,7 @@ def _saturation_sub_unsigned(expr): # 0x48 - 0xd7 in 8 bit, should saturate arg1 = expr.args[0].zeroExtend(expr.size + 1) arg2 = expr.args[1].args[0].zeroExtend(expr.size + 1) - return _unsigned_saturation(arg1 - arg2, expr.size) + return _signed_to_unsigned_saturation(arg1 - arg2, expr.size) def _saturation_sub_signed(expr): assert expr.is_op("+") and len(expr.args) == 2 and expr.args[-1].is_op("-") @@ -4884,7 +4889,7 @@ def _saturation_sub_signed(expr): # Compute the subtraction on two more bits, see _saturation_sub_unsigned arg1 = expr.args[0].signExtend(expr.size + 2) arg2 = expr.args[1].args[0].signExtend(expr.size + 2) - return _signed_saturation(arg1 - arg2, expr.size) + return _signed_to_signed_saturation(arg1 - arg2, expr.size) def _saturation_add(expr): assert expr.is_op("+") and len(expr.args) == 2 @@ -4895,7 +4900,7 @@ def _saturation_add(expr): arg1 = expr.args[0].zeroExtend(expr.size + 1) arg2 = expr.args[1].zeroExtend(expr.size + 1) - # We can also use _unsigned_saturation with two additional bits (to + # We can also use _signed_to_unsigned_saturation with two additional bits (to # distinguish minus and overflow case) # The resulting expression being more complicated with an impossible case # (signed=True), we rewrite the rule here @@ -4911,7 +4916,7 @@ def _saturation_add_signed(expr): arg1 = expr.args[0].signExtend(expr.size + 2) arg2 = expr.args[1].signExtend(expr.size + 2) - return _signed_saturation(arg1 + arg2, expr.size) + return _signed_to_signed_saturation(arg1 + arg2, expr.size) # Saturate SSE operations diff --git a/miasm/ir/translators/C.py b/miasm/ir/translators/C.py index 9a96487a..6be5d961 100644 --- a/miasm/ir/translators/C.py +++ b/miasm/ir/translators/C.py @@ -36,6 +36,16 @@ TOK_CMP_TO_BIGNUM_C = { } +def get_c_common_next_pow2(size): + # For uncommon expression size, use at least uint8 + size = max(size, 8) + next_power = 1 + while next_power < size: + next_power <<= 1 + size = next_power + return size + + class TranslatorC(Translator): "Translate a Miasm expression to an equivalent C code" @@ -419,21 +429,28 @@ class TranslatorC(Translator): TOK_INF_EQUAL_SIGNED, TOK_INF_EQUAL_UNSIGNED, ]: - arg0 = self.from_expr(expr.args[0]) - arg1 = self.from_expr(expr.args[1]) - + arg0, arg1 = expr.args if expr.size <= self.NATIVE_INT_MAX_SIZE: + size = get_c_common_next_pow2(arg0.size) op = TOK_CMP_TO_NATIVE_C[expr.op] if expr.op in [TOK_INF_SIGNED, TOK_INF_EQUAL_SIGNED]: - cast = "(int%d_t)" % expr.args[0].size + arg0 = arg0.signExtend(size) + arg1 = arg1.signExtend(size) + arg0_C = self.from_expr(arg0) + arg1_C = self.from_expr(arg1) + cast = "(int%d_t)" % size else: - cast = "(uint%d_t)" % expr.args[0].size + arg0 = arg0.signExtend(size) + arg1 = arg1.signExtend(size) + arg0_C = self.from_expr(arg0) + arg1_C = self.from_expr(arg1) + cast = "(uint%d_t)" % size out = '((%s%s %s %s%s)?1:0)' % ( cast, - arg0, + arg0_C, op, cast, - arg1 + arg1_C ) else: op = TOK_CMP_TO_BIGNUM_C[expr.op] @@ -484,17 +501,7 @@ class TranslatorC(Translator): if expr.size <= self.NATIVE_INT_MAX_SIZE: out = [] - # XXX check mask for 64 bit & 32 bit compat - if expr.size in [8, 16, 32, 64, 128]: - size = expr.size - else: - # Uncommon expression size, use at least uint8 - size = max(expr.size, 8) - next_power = 1 - while next_power <= size: - next_power <<= 1 - size = next_power - + size = get_c_common_next_pow2(expr.size) dst_cast = "uint%d_t" % size for index, arg in expr.iter_args(): out.append("(((%s)(%s & %s)) << %d)" % ( diff --git a/miasm/jitter/JitCore.c b/miasm/jitter/JitCore.c index ae5af293..37b69aa3 100644 --- a/miasm/jitter/JitCore.c +++ b/miasm/jitter/JitCore.c @@ -1,6 +1,7 @@ #include <Python.h> #include "structmember.h" #include <stdint.h> +#include <limits.h> #include <inttypes.h> #include "compat_py23.h" #include "queue.h" @@ -235,6 +236,7 @@ PyObject* vm_get_mem(JitCpu *self, PyObject* args) uint64_t addr; uint64_t size; + size_t size_st; PyObject *obj_out; char * buf_out; int ret; @@ -242,16 +244,23 @@ PyObject* vm_get_mem(JitCpu *self, PyObject* args) if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_len)) return NULL; - PyGetInt(py_addr, addr); - PyGetInt(py_len, size); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_len, size); - ret = vm_read_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, &buf_out, 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, size); + 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 15efc7d2..85be57da 100644 --- a/miasm/jitter/JitCore.h +++ b/miasm/jitter/JitCore.h @@ -3,6 +3,15 @@ #if _WIN32 #define _MIASM_EXPORT __declspec(dllexport) + +#ifndef SSIZE_MAX +#ifdef _WIN64 +#define SSIZE_MAX _I64_MAX +#else +#define SSIZE_MAX INT_MAX +#endif +#endif + #else #define _MIASM_EXPORT #endif @@ -24,11 +33,11 @@ uint64_t tmp; \ py_long = PyLong_FromLong(0); \ cst_32 = PyLong_FromLong(32); \ - bn = ((vm_cpu_t*)(self->cpu))-> regname; \ + 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(tmp); \ + 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); \ @@ -39,7 +48,7 @@ return py_long; \ } \ \ - static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ + static PyObject *JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ { \ bn_t bn; \ int j; \ @@ -69,7 +78,7 @@ bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \ } \ \ - ((vm_cpu_t*)(self->cpu))-> regname = bignum_mask(bn, (size)); \ + (self->cpu)->regname = bignum_mask(bn, (size)); \ Py_DECREF(py_long); \ Py_DECREF(cst_32); \ Py_DECREF(cst_ffffffff); \ @@ -90,11 +99,11 @@ uint64_t tmp; \ py_long = PyLong_FromLong(0); \ cst_32 = PyLong_FromLong(32); \ - bn = ((vm_cpu_t*)(self->cpu))-> regname; \ + 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(tmp); \ + 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); \ @@ -105,7 +114,7 @@ return py_long; \ } \ \ - static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ + static PyObject *JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ { \ bn_t bn; \ int j; \ @@ -140,7 +149,7 @@ bn = bignum_or(bn, bignum_lshift(bignum_from_uint64(tmp), 8 * j)); \ } \ \ - ((vm_cpu_t*)(self->cpu))-> regname = bignum_mask(bn, (size)); \ + self->cpu->regname = bignum_mask(bn, (size)); \ Py_DECREF(py_long); \ Py_DECREF(cst_32); \ Py_DECREF(cst_ffffffff); \ @@ -161,26 +170,26 @@ #define getset_reg_u64(regname) \ static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \ { \ - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)(self->cpu))-> regname )); \ + return PyLong_FromUnsignedLongLong(self->cpu->regname); \ } \ static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ { \ uint64_t val; \ - PyGetInt_retneg(value, val); \ - ((vm_cpu_t*)(self->cpu))-> regname = val; \ + PyGetInt_uint64_t_retneg(value, val); \ + self->cpu->regname = val; \ return 0; \ } #define getset_reg_u32(regname) \ static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \ { \ - return PyLong_FromUnsignedLongLong((uint32_t)(((vm_cpu_t*)(self->cpu))-> regname )); \ + return PyLong_FromUnsignedLongLong(self->cpu->regname); \ } \ static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ { \ uint32_t val; \ - PyGetInt_retneg(value, val); \ - ((vm_cpu_t*)(self->cpu))-> regname = val; \ + PyGetInt_uint32_t_retneg(value, val); \ + self->cpu->regname = val; \ return 0; \ } @@ -188,19 +197,33 @@ #define getset_reg_u16(regname) \ static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \ { \ - return PyLong_FromUnsignedLongLong((uint16_t)(((vm_cpu_t*)(self->cpu))-> regname )); \ + return PyLong_FromUnsignedLongLong(self->cpu->regname); \ } \ static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ { \ uint16_t val; \ - PyGetInt_retneg(value, val); \ - ((vm_cpu_t*)(self->cpu))-> regname = val; \ + PyGetInt_uint16_t_retneg(value, val); \ + self->cpu->regname = val; \ + return 0; \ + } + + +#define getset_reg_u8(regname) \ + static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \ + { \ + return PyLong_FromUnsignedLongLong(self->cpu->regname); \ + } \ + static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ + { \ + uint8_t val; \ + PyGetInt_uint8_t_retneg(value, val); \ + self->cpu->regname = val; \ return 0; \ } #define get_reg(reg) do { \ - o = PyLong_FromUnsignedLongLong((uint64_t)((vm_cpu_t*)(self->cpu))->reg); \ + o = PyLong_FromUnsignedLongLong((uint64_t)self->cpu->reg); \ PyDict_SetItemString(dict, #reg, o); \ Py_DECREF(o); \ } while(0); @@ -216,11 +239,11 @@ uint64_t tmp; \ py_long = PyLong_FromLong(0); \ cst_32 = PyLong_FromLong(32); \ - bn = ((vm_cpu_t*)(self->cpu))-> reg; \ + 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(tmp); \ + 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); \ @@ -234,7 +257,7 @@ #define get_reg_off(reg) do { \ - o = PyLong_FromUnsignedLongLong((uint64_t)offsetof(vm_cpu_t, reg)); \ + o = PyLong_FromUnsignedLongLong((uint64_t)offsetof(struct vm_cpu, reg)); \ PyDict_SetItemString(dict, #reg, o); \ Py_DECREF(o); \ } while(0); @@ -247,11 +270,13 @@ typedef struct { uint64_t address; } block_id; +struct vm_cpu; + typedef struct { PyObject_HEAD VmMngr *pyvm; PyObject *jitter; - void* cpu; + struct vm_cpu *cpu; } JitCpu; @@ -298,7 +323,7 @@ _MIASM_EXPORT void MEM_WRITE_INT_BN_FROM_PTR(JitCpu* jitcpu, int size, uint64_t #define VM_exception_flag (jitcpu->pyvm->vm_mngr.exception_flags) -#define CPU_exception_flag (((vm_cpu_t*)jitcpu->cpu)->exception_flags) +#define CPU_exception_flag (((struct vm_cpu*)jitcpu->cpu)->exception_flags) #define CPU_exception_flag_at_instr ((CPU_exception_flag) && ((CPU_exception_flag) > EXCEPT_NUM_UPDT_EIP)) #define JIT_RET_EXCEPTION 1 #define JIT_RET_NO_EXCEPTION 0 diff --git a/miasm/jitter/Jitgcc.c b/miasm/jitter/Jitgcc.c index 0a39c998..9dc7b2fd 100644 --- a/miasm/jitter/Jitgcc.c +++ b/miasm/jitter/Jitgcc.c @@ -82,9 +82,6 @@ PyObject* gcc_exec_block(PyObject* self, PyObject* args) -static PyObject *GccError; - - static PyMethodDef GccMethods[] = { {"gcc_exec_block", gcc_exec_block, METH_VARARGS, "gcc exec block"}, @@ -95,12 +92,9 @@ static PyMethodDef GccMethods[] = { MOD_INIT(Jitgcc) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "Jitgcc", "gcc module", GccMethods); - if (module == NULL) - return NULL; - - return module; + RET_MODULE; } diff --git a/miasm/jitter/Jitllvm.c b/miasm/jitter/Jitllvm.c index efe5250f..7617954b 100644 --- a/miasm/jitter/Jitllvm.c +++ b/miasm/jitter/Jitllvm.c @@ -15,7 +15,7 @@ PyObject* llvm_exec_block(PyObject* self, PyObject* args) { uint64_t (*func)(void*, void*, void*, uint8_t*); - vm_cpu_t* cpu; + struct vm_cpu* cpu; vm_mngr_t* vm; uint64_t ret; JitCpu* jitcpu; @@ -88,12 +88,9 @@ static PyMethodDef LLVMMethods[] = { MOD_INIT(Jitllvm) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "Jitllvm", "llvm module", LLVMMethods); - if (module == NULL) - return NULL; - - return module; + RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_aarch64.c b/miasm/jitter/arch/JitCore_aarch64.c index 9e1a870e..c1a89285 100644 --- a/miasm/jitter/arch/JitCore_aarch64.c +++ b/miasm/jitter/arch/JitCore_aarch64.c @@ -14,48 +14,48 @@ reg_dict gpreg_dict[] = { - {.name = "X0", .offset = offsetof(vm_cpu_t, X0), .size = 64}, - {.name = "X1", .offset = offsetof(vm_cpu_t, X1), .size = 64}, - {.name = "X2", .offset = offsetof(vm_cpu_t, X2), .size = 64}, - {.name = "X3", .offset = offsetof(vm_cpu_t, X3), .size = 64}, - {.name = "X4", .offset = offsetof(vm_cpu_t, X4), .size = 64}, - {.name = "X5", .offset = offsetof(vm_cpu_t, X5), .size = 64}, - {.name = "X6", .offset = offsetof(vm_cpu_t, X6), .size = 64}, - {.name = "X7", .offset = offsetof(vm_cpu_t, X7), .size = 64}, - {.name = "X8", .offset = offsetof(vm_cpu_t, X8), .size = 64}, - {.name = "X9", .offset = offsetof(vm_cpu_t, X9), .size = 64}, - {.name = "X10", .offset = offsetof(vm_cpu_t, X10), .size = 64}, - {.name = "X11", .offset = offsetof(vm_cpu_t, X11), .size = 64}, - {.name = "X12", .offset = offsetof(vm_cpu_t, X12), .size = 64}, - {.name = "X13", .offset = offsetof(vm_cpu_t, X13), .size = 64}, - {.name = "X14", .offset = offsetof(vm_cpu_t, X14), .size = 64}, - {.name = "X15", .offset = offsetof(vm_cpu_t, X15), .size = 64}, - {.name = "X16", .offset = offsetof(vm_cpu_t, X16), .size = 64}, - {.name = "X17", .offset = offsetof(vm_cpu_t, X17), .size = 64}, - {.name = "X18", .offset = offsetof(vm_cpu_t, X18), .size = 64}, - {.name = "X19", .offset = offsetof(vm_cpu_t, X19), .size = 64}, - {.name = "X20", .offset = offsetof(vm_cpu_t, X20), .size = 64}, - {.name = "X21", .offset = offsetof(vm_cpu_t, X21), .size = 64}, - {.name = "X22", .offset = offsetof(vm_cpu_t, X22), .size = 64}, - {.name = "X23", .offset = offsetof(vm_cpu_t, X23), .size = 64}, - {.name = "X24", .offset = offsetof(vm_cpu_t, X24), .size = 64}, - {.name = "X25", .offset = offsetof(vm_cpu_t, X25), .size = 64}, - {.name = "X26", .offset = offsetof(vm_cpu_t, X26), .size = 64}, - {.name = "X27", .offset = offsetof(vm_cpu_t, X27), .size = 64}, - {.name = "X28", .offset = offsetof(vm_cpu_t, X28), .size = 64}, - {.name = "X29", .offset = offsetof(vm_cpu_t, X29), .size = 64}, - {.name = "LR", .offset = offsetof(vm_cpu_t, LR), .size = 64}, - - {.name = "SP", .offset = offsetof(vm_cpu_t, SP), .size = 64}, - {.name = "PC", .offset = offsetof(vm_cpu_t, PC), .size = 64}, - - {.name = "zf", .offset = offsetof(vm_cpu_t, zf), .size = 8}, - {.name = "nf", .offset = offsetof(vm_cpu_t, nf), .size = 8}, - {.name = "of", .offset = offsetof(vm_cpu_t, of), .size = 8}, - {.name = "cf", .offset = offsetof(vm_cpu_t, cf), .size = 8}, - - {.name = "exception_flags", .offset = offsetof(vm_cpu_t, exception_flags), .size = 32}, - {.name = "interrupt_num", .offset = offsetof(vm_cpu_t, interrupt_num), .size = 32}, + {.name = "X0", .offset = offsetof(struct vm_cpu, X0), .size = 64}, + {.name = "X1", .offset = offsetof(struct vm_cpu, X1), .size = 64}, + {.name = "X2", .offset = offsetof(struct vm_cpu, X2), .size = 64}, + {.name = "X3", .offset = offsetof(struct vm_cpu, X3), .size = 64}, + {.name = "X4", .offset = offsetof(struct vm_cpu, X4), .size = 64}, + {.name = "X5", .offset = offsetof(struct vm_cpu, X5), .size = 64}, + {.name = "X6", .offset = offsetof(struct vm_cpu, X6), .size = 64}, + {.name = "X7", .offset = offsetof(struct vm_cpu, X7), .size = 64}, + {.name = "X8", .offset = offsetof(struct vm_cpu, X8), .size = 64}, + {.name = "X9", .offset = offsetof(struct vm_cpu, X9), .size = 64}, + {.name = "X10", .offset = offsetof(struct vm_cpu, X10), .size = 64}, + {.name = "X11", .offset = offsetof(struct vm_cpu, X11), .size = 64}, + {.name = "X12", .offset = offsetof(struct vm_cpu, X12), .size = 64}, + {.name = "X13", .offset = offsetof(struct vm_cpu, X13), .size = 64}, + {.name = "X14", .offset = offsetof(struct vm_cpu, X14), .size = 64}, + {.name = "X15", .offset = offsetof(struct vm_cpu, X15), .size = 64}, + {.name = "X16", .offset = offsetof(struct vm_cpu, X16), .size = 64}, + {.name = "X17", .offset = offsetof(struct vm_cpu, X17), .size = 64}, + {.name = "X18", .offset = offsetof(struct vm_cpu, X18), .size = 64}, + {.name = "X19", .offset = offsetof(struct vm_cpu, X19), .size = 64}, + {.name = "X20", .offset = offsetof(struct vm_cpu, X20), .size = 64}, + {.name = "X21", .offset = offsetof(struct vm_cpu, X21), .size = 64}, + {.name = "X22", .offset = offsetof(struct vm_cpu, X22), .size = 64}, + {.name = "X23", .offset = offsetof(struct vm_cpu, X23), .size = 64}, + {.name = "X24", .offset = offsetof(struct vm_cpu, X24), .size = 64}, + {.name = "X25", .offset = offsetof(struct vm_cpu, X25), .size = 64}, + {.name = "X26", .offset = offsetof(struct vm_cpu, X26), .size = 64}, + {.name = "X27", .offset = offsetof(struct vm_cpu, X27), .size = 64}, + {.name = "X28", .offset = offsetof(struct vm_cpu, X28), .size = 64}, + {.name = "X29", .offset = offsetof(struct vm_cpu, X29), .size = 64}, + {.name = "LR", .offset = offsetof(struct vm_cpu, LR), .size = 64}, + + {.name = "SP", .offset = offsetof(struct vm_cpu, SP), .size = 64}, + {.name = "PC", .offset = offsetof(struct vm_cpu, PC), .size = 64}, + + {.name = "zf", .offset = offsetof(struct vm_cpu, zf), .size = 8}, + {.name = "nf", .offset = offsetof(struct vm_cpu, nf), .size = 8}, + {.name = "of", .offset = offsetof(struct vm_cpu, of), .size = 8}, + {.name = "cf", .offset = offsetof(struct vm_cpu, cf), .size = 8}, + + {.name = "exception_flags", .offset = offsetof(struct vm_cpu, exception_flags), .size = 32}, + {.name = "interrupt_num", .offset = offsetof(struct vm_cpu, interrupt_num), .size = 32}, }; @@ -118,8 +118,9 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject* dict; PyObject *d_key, *d_value = NULL; Py_ssize_t pos = 0; - char* d_key_name; - uint64_t val; + const char *d_key_name; + uint32_t val32; + uint64_t val64; unsigned int i, found; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -128,15 +129,24 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) RAISE(PyExc_TypeError, "arg must be dict"); while(PyDict_Next(dict, &pos, &d_key, &d_value)){ PyGetStr(d_key_name, d_key); - PyGetInt(d_value, val); - found = 0; for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ if (strcmp(d_key_name, gpreg_dict[i].name)) continue; - *((uint32_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val; found = 1; - break; + switch (gpreg_dict[i].size) { + default: + RAISE(PyExc_TypeError, "Unsupported size"); + break; + case 32: + PyGetInt_uint32_t(d_value, val32); + *((uint32_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val32; + break; + case 64: + PyGetInt_uint64_t(d_value, val64); + *((uint64_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val64; + break; + } } if (found) @@ -151,13 +161,13 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject * cpu_init_regs(JitCpu* self) { - memset(self->cpu, 0, sizeof(vm_cpu_t)); + memset(self->cpu, 0, sizeof(struct vm_cpu)); Py_INCREF(Py_None); return Py_None; } -void dump_gpregs(vm_cpu_t* vmcpu) +void dump_gpregs(struct vm_cpu* vmcpu) { printf("X0 %.16"PRIX64" X1 %.16"PRIX64" X2 %.16"PRIX64" X3 %.16"PRIX64" "\ "X4 %.16"PRIX64" X5 %.16"PRIX64" X6 %.16"PRIX64" X7 %.16"PRIX64"\n", @@ -185,7 +195,7 @@ void dump_gpregs(vm_cpu_t* vmcpu) PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { - vm_cpu_t* vmcpu; + struct vm_cpu* vmcpu; vmcpu = self->cpu; dump_gpregs(vmcpu); @@ -203,60 +213,42 @@ PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint32_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, i); + PyGetInt_uint32_t(item1, exception_flags); - ((vm_cpu_t*)self->cpu)->exception_flags = i; + ((struct vm_cpu*)self->cpu)->exception_flags = exception_flags; Py_INCREF(Py_None); return Py_None; } PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->exception_flags)); + return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->exception_flags)); } - - - -void check_automod(JitCpu* jitcpu, uint64_t addr, uint64_t size) -{ - PyObject *result; - - if (!(((VmMngr*)jitcpu->pyvm)->vm_mngr.exception_flags & EXCEPT_CODE_AUTOMOD)) - return; - result = PyObject_CallMethod(jitcpu->jitter, "automod_cb", "LL", addr, size); - Py_DECREF(result); - -} - void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src) { vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 8); } void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src) { vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 16); } void MEM_WRITE_32(JitCpu* jitcpu, uint64_t addr, uint32_t src) { vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 32); } void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src) { vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 64); } @@ -267,25 +259,27 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args) Py_ssize_t py_length; char * buffer; - uint64_t size; + 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(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); if(!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); - size = PyBytes_Size(py_buffer); + 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, size); + ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError,"arg must be str"); - check_automod(self, addr, size*8); Py_INCREF(Py_None); return Py_None; @@ -320,9 +314,9 @@ static PyMethodDef JitCpu_methods[] = { static int JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) { - self->cpu = malloc(sizeof(vm_cpu_t)); + self->cpu = malloc(sizeof(struct vm_cpu)); if (self->cpu == NULL) { - fprintf(stderr, "cannot alloc vm_cpu_t\n"); + fprintf(stderr, "cannot alloc struct vm_cpu\n"); exit(EXIT_FAILURE); } return 0; @@ -543,20 +537,20 @@ static PyMethodDef JitCore_aarch64_Methods[] = { MOD_INIT(JitCore_aarch64) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "JitCore_aarch64", "JitCore_aarch64 module", JitCore_aarch64_Methods); if (module == NULL) - return NULL; + RET_MODULE; if (PyType_Ready(&JitCpuType) < 0) - return NULL; + RET_MODULE; Py_INCREF(&JitCpuType); if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) - return NULL; + RET_MODULE; - return module; + RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_aarch64.h b/miasm/jitter/arch/JitCore_aarch64.h index fa958244..975a93ec 100644 --- a/miasm/jitter/arch/JitCore_aarch64.h +++ b/miasm/jitter/arch/JitCore_aarch64.h @@ -1,5 +1,5 @@ -typedef struct { +struct vm_cpu { uint32_t exception_flags; uint32_t interrupt_num; @@ -45,9 +45,9 @@ typedef struct { uint32_t nf; uint32_t of; uint32_t cf; -}vm_cpu_t; +}; -_MIASM_EXPORT void dump_gpregs(vm_cpu_t* vmcpu); +_MIASM_EXPORT void dump_gpregs(struct vm_cpu* vmcpu); _MIASM_EXPORT void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src); _MIASM_EXPORT void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src); diff --git a/miasm/jitter/arch/JitCore_arm.c b/miasm/jitter/arch/JitCore_arm.c index 64f30cf4..13dcb3f4 100644 --- a/miasm/jitter/arch/JitCore_arm.c +++ b/miasm/jitter/arch/JitCore_arm.c @@ -14,35 +14,35 @@ reg_dict gpreg_dict[] = { - {.name = "R0", .offset = offsetof(vm_cpu_t, R0), .size = 32}, - {.name = "R1", .offset = offsetof(vm_cpu_t, R1), .size = 32}, - {.name = "R2", .offset = offsetof(vm_cpu_t, R2), .size = 32}, - {.name = "R3", .offset = offsetof(vm_cpu_t, R3), .size = 32}, - {.name = "R4", .offset = offsetof(vm_cpu_t, R4), .size = 32}, - {.name = "R5", .offset = offsetof(vm_cpu_t, R5), .size = 32}, - {.name = "R6", .offset = offsetof(vm_cpu_t, R6), .size = 32}, - {.name = "R7", .offset = offsetof(vm_cpu_t, R7), .size = 32}, - {.name = "R8", .offset = offsetof(vm_cpu_t, R8), .size = 32}, - {.name = "R9", .offset = offsetof(vm_cpu_t, R9), .size = 32}, - {.name = "R10", .offset = offsetof(vm_cpu_t, R10), .size = 32}, - {.name = "R11", .offset = offsetof(vm_cpu_t, R11), .size = 32}, - {.name = "R12", .offset = offsetof(vm_cpu_t, R12), .size = 32}, - {.name = "SP", .offset = offsetof(vm_cpu_t, SP), .size = 32}, - {.name = "LR", .offset = offsetof(vm_cpu_t, LR), .size = 32}, - {.name = "PC", .offset = offsetof(vm_cpu_t, PC), .size = 32}, - - {.name = "zf", .offset = offsetof(vm_cpu_t, zf), .size = 8}, - {.name = "nf", .offset = offsetof(vm_cpu_t, nf), .size = 8}, - {.name = "of", .offset = offsetof(vm_cpu_t, of), .size = 8}, - {.name = "cf", .offset = offsetof(vm_cpu_t, cf), .size = 8}, - - {.name = "ge0", .offset = offsetof(vm_cpu_t, ge0), .size = 8}, - {.name = "ge1", .offset = offsetof(vm_cpu_t, ge1), .size = 8}, - {.name = "ge2", .offset = offsetof(vm_cpu_t, ge2), .size = 8}, - {.name = "ge3", .offset = offsetof(vm_cpu_t, ge3), .size = 8}, - - {.name = "exception_flags", .offset = offsetof(vm_cpu_t, exception_flags), .size = 32}, - {.name = "interrupt_num", .offset = offsetof(vm_cpu_t, interrupt_num), .size = 32}, + {.name = "R0", .offset = offsetof(struct vm_cpu, R0), .size = 32}, + {.name = "R1", .offset = offsetof(struct vm_cpu, R1), .size = 32}, + {.name = "R2", .offset = offsetof(struct vm_cpu, R2), .size = 32}, + {.name = "R3", .offset = offsetof(struct vm_cpu, R3), .size = 32}, + {.name = "R4", .offset = offsetof(struct vm_cpu, R4), .size = 32}, + {.name = "R5", .offset = offsetof(struct vm_cpu, R5), .size = 32}, + {.name = "R6", .offset = offsetof(struct vm_cpu, R6), .size = 32}, + {.name = "R7", .offset = offsetof(struct vm_cpu, R7), .size = 32}, + {.name = "R8", .offset = offsetof(struct vm_cpu, R8), .size = 32}, + {.name = "R9", .offset = offsetof(struct vm_cpu, R9), .size = 32}, + {.name = "R10", .offset = offsetof(struct vm_cpu, R10), .size = 32}, + {.name = "R11", .offset = offsetof(struct vm_cpu, R11), .size = 32}, + {.name = "R12", .offset = offsetof(struct vm_cpu, R12), .size = 32}, + {.name = "SP", .offset = offsetof(struct vm_cpu, SP), .size = 32}, + {.name = "LR", .offset = offsetof(struct vm_cpu, LR), .size = 32}, + {.name = "PC", .offset = offsetof(struct vm_cpu, PC), .size = 32}, + + {.name = "zf", .offset = offsetof(struct vm_cpu, zf), .size = 8}, + {.name = "nf", .offset = offsetof(struct vm_cpu, nf), .size = 8}, + {.name = "of", .offset = offsetof(struct vm_cpu, of), .size = 8}, + {.name = "cf", .offset = offsetof(struct vm_cpu, cf), .size = 8}, + + {.name = "ge0", .offset = offsetof(struct vm_cpu, ge0), .size = 8}, + {.name = "ge1", .offset = offsetof(struct vm_cpu, ge1), .size = 8}, + {.name = "ge2", .offset = offsetof(struct vm_cpu, ge2), .size = 8}, + {.name = "ge3", .offset = offsetof(struct vm_cpu, ge3), .size = 8}, + + {.name = "exception_flags", .offset = offsetof(struct vm_cpu, exception_flags), .size = 32}, + {.name = "interrupt_num", .offset = offsetof(struct vm_cpu, interrupt_num), .size = 32}, }; /************************** JitCpu object **************************/ @@ -92,8 +92,8 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject* dict; PyObject *d_key, *d_value = NULL; Py_ssize_t pos = 0; - char* d_key_name; - uint64_t val; + const char *d_key_name; + uint32_t val; unsigned int i, found; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -102,7 +102,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) RAISE(PyExc_TypeError, "arg must be dict"); while(PyDict_Next(dict, &pos, &d_key, &d_value)){ PyGetStr(d_key_name, d_key); - PyGetInt(d_value, val); + PyGetInt_uint32_t(d_value, val); found = 0; for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ @@ -115,7 +115,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) if (found) continue; - fprintf(stderr, "unknown key: %s\n", d_key); + fprintf(stderr, "unknown key: %s\n", d_key_name); RAISE(PyExc_ValueError, "unknown reg"); } Py_INCREF(Py_None); @@ -125,13 +125,13 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject * cpu_init_regs(JitCpu* self) { - memset(self->cpu, 0, sizeof(vm_cpu_t)); + memset(self->cpu, 0, sizeof(struct vm_cpu)); Py_INCREF(Py_None); return Py_None; } -void dump_gpregs(vm_cpu_t* vmcpu) +void dump_gpregs(struct vm_cpu* vmcpu) { printf("R0 %.8"PRIX32" R1 %.8"PRIX32" R2 %.8"PRIX32" R3 %.8"PRIX32" ", vmcpu->R0, vmcpu->R1, vmcpu->R2, vmcpu->R3); @@ -148,7 +148,7 @@ void dump_gpregs(vm_cpu_t* vmcpu) PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { - vm_cpu_t* vmcpu; + struct vm_cpu* vmcpu; vmcpu = self->cpu; dump_gpregs(vmcpu); @@ -167,60 +167,41 @@ PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint32_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, i); + PyGetInt_uint32_t(item1, exception_flags); - ((vm_cpu_t*)self->cpu)->exception_flags = i; + ((struct vm_cpu*)self->cpu)->exception_flags = exception_flags; Py_INCREF(Py_None); return Py_None; } PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->exception_flags)); -} - - - - - -void check_automod(JitCpu* jitcpu, uint64_t addr, uint64_t size) -{ - PyObject *result; - - if (!(((VmMngr*)jitcpu->pyvm)->vm_mngr.exception_flags & EXCEPT_CODE_AUTOMOD)) - return; - result = PyObject_CallMethod(jitcpu->jitter, "automod_cb", "LL", addr, size); - Py_DECREF(result); - + return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->exception_flags)); } void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src) { vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 8); } void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src) { vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 16); } void MEM_WRITE_32(JitCpu* jitcpu, uint64_t addr, uint32_t src) { vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 32); } void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src) { vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 64); } PyObject* vm_set_mem(JitCpu *self, PyObject* args) @@ -230,25 +211,27 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args) Py_ssize_t py_length; char * buffer; - uint64_t size; + 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(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); if(!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); - size = PyBytes_Size(py_buffer); + 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, size); + ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError,"arg must be str"); - check_automod(self, addr, size*8); Py_INCREF(Py_None); return Py_None; @@ -257,21 +240,21 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args) PyObject* cpu_set_interrupt_num(JitCpu* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint32_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, i); + PyGetInt_uint32_t(item1, exception_flags); - ((vm_cpu_t*)self->cpu)->interrupt_num = i; + ((struct vm_cpu*)self->cpu)->interrupt_num = exception_flags; Py_INCREF(Py_None); return Py_None; } PyObject* cpu_get_interrupt_num(JitCpu* self, PyObject* args) { - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->interrupt_num)); + return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->interrupt_num)); } static PyMemberDef JitCpu_members[] = { @@ -307,9 +290,9 @@ static PyMethodDef JitCpu_methods[] = { static int JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) { - self->cpu = malloc(sizeof(vm_cpu_t)); + self->cpu = malloc(sizeof(struct vm_cpu)); if (self->cpu == NULL) { - fprintf(stderr, "cannot alloc vm_cpu_t\n"); + fprintf(stderr, "cannot alloc struct vm_cpu\n"); exit(EXIT_FAILURE); } return 0; @@ -488,20 +471,20 @@ static PyMethodDef JitCore_arm_Methods[] = { MOD_INIT(JitCore_arm) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "JitCore_arm", "JitCore_arm module", JitCore_arm_Methods); if (module == NULL) - return NULL; + RET_MODULE; if (PyType_Ready(&JitCpuType) < 0) - return NULL; + RET_MODULE; Py_INCREF(&JitCpuType); if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) - return NULL; + RET_MODULE; - return module; + RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_arm.h b/miasm/jitter/arch/JitCore_arm.h index 67a1096a..30a38344 100644 --- a/miasm/jitter/arch/JitCore_arm.h +++ b/miasm/jitter/arch/JitCore_arm.h @@ -1,5 +1,5 @@ -typedef struct { +struct vm_cpu { uint32_t exception_flags; uint32_t interrupt_num; @@ -34,10 +34,10 @@ typedef struct { uint32_t ge3; uint32_t bp_num; -}vm_cpu_t; +}; -_MIASM_EXPORT void dump_gpregs(vm_cpu_t* vmcpu); +_MIASM_EXPORT void dump_gpregs(struct vm_cpu* vmcpu); _MIASM_EXPORT void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src); _MIASM_EXPORT void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src); diff --git a/miasm/jitter/arch/JitCore_mep.c b/miasm/jitter/arch/JitCore_mep.c index 6e7f1767..a69110bc 100644 --- a/miasm/jitter/arch/JitCore_mep.c +++ b/miasm/jitter/arch/JitCore_mep.c @@ -15,63 +15,63 @@ reg_dict gpreg_dict[] = { - {.name = "R0", .offset = offsetof(vm_cpu_t, R0), .size = 32}, - {.name = "R1", .offset = offsetof(vm_cpu_t, R1), .size = 32}, - {.name = "R2", .offset = offsetof(vm_cpu_t, R2), .size = 32}, - {.name = "R3", .offset = offsetof(vm_cpu_t, R3), .size = 32}, - {.name = "R4", .offset = offsetof(vm_cpu_t, R4), .size = 32}, - {.name = "R5", .offset = offsetof(vm_cpu_t, R5), .size = 32}, - {.name = "R6", .offset = offsetof(vm_cpu_t, R6), .size = 32}, - {.name = "R7", .offset = offsetof(vm_cpu_t, R7), .size = 32}, - {.name = "R8", .offset = offsetof(vm_cpu_t, R8), .size = 32}, - {.name = "R9", .offset = offsetof(vm_cpu_t, R9), .size = 32}, - {.name = "R10", .offset = offsetof(vm_cpu_t, R10), .size = 32}, - {.name = "R11", .offset = offsetof(vm_cpu_t, R11), .size = 32}, - {.name = "R12", .offset = offsetof(vm_cpu_t, R12), .size = 32}, - {.name = "TP", .offset = offsetof(vm_cpu_t, TP), .size = 32}, - {.name = "GP", .offset = offsetof(vm_cpu_t, GP), .size = 32}, - {.name = "SP", .offset = offsetof(vm_cpu_t, SP), .size = 32}, - - {.name = "PC", .offset = offsetof(vm_cpu_t, PC), .size = 32}, - {.name = "LP", .offset = offsetof(vm_cpu_t, LP), .size = 32}, - {.name = "SAR", .offset = offsetof(vm_cpu_t, SAR), .size = 32}, - {.name = "S3", .offset = offsetof(vm_cpu_t, S3), .size = 32}, - {.name = "RPB", .offset = offsetof(vm_cpu_t, RPB), .size = 32}, - {.name = "RPE", .offset = offsetof(vm_cpu_t, RPE), .size = 32}, - {.name = "RPC", .offset = offsetof(vm_cpu_t, RPC), .size = 32}, - {.name = "HI", .offset = offsetof(vm_cpu_t, HI), .size = 32}, - {.name = "LO", .offset = offsetof(vm_cpu_t, LO), .size = 32}, - {.name = "S9", .offset = offsetof(vm_cpu_t, S9), .size = 32}, - {.name = "S10", .offset = offsetof(vm_cpu_t, S10), .size = 32}, - {.name = "S11", .offset = offsetof(vm_cpu_t, S11), .size = 32}, - {.name = "MB0", .offset = offsetof(vm_cpu_t, MB0), .size = 32}, - {.name = "ME0", .offset = offsetof(vm_cpu_t, ME0), .size = 32}, - {.name = "MB1", .offset = offsetof(vm_cpu_t, MB1), .size = 32}, - {.name = "ME1", .offset = offsetof(vm_cpu_t, ME1), .size = 32}, - {.name = "PSW", .offset = offsetof(vm_cpu_t, PSW), .size = 32}, - {.name = "ID", .offset = offsetof(vm_cpu_t, ID), .size = 32}, - {.name = "TMP", .offset = offsetof(vm_cpu_t, TMP), .size = 32}, - {.name = "EPC", .offset = offsetof(vm_cpu_t, EPC), .size = 32}, - {.name = "EXC", .offset = offsetof(vm_cpu_t, EXC), .size = 32}, - {.name = "CFG", .offset = offsetof(vm_cpu_t, CFG), .size = 32}, - {.name = "S22", .offset = offsetof(vm_cpu_t, S22), .size = 32}, - {.name = "NPC", .offset = offsetof(vm_cpu_t, NPC), .size = 32}, - {.name = "DBG", .offset = offsetof(vm_cpu_t, DBG), .size = 32}, - {.name = "DEPC", .offset = offsetof(vm_cpu_t, DEPC), .size = 32}, - {.name = "OPT", .offset = offsetof(vm_cpu_t, OPT), .size = 32}, - {.name = "RCFG", .offset = offsetof(vm_cpu_t, RCFG), .size = 32}, - {.name = "CCFG", .offset = offsetof(vm_cpu_t, CCFG), .size = 32}, - {.name = "S29", .offset = offsetof(vm_cpu_t, S29), .size = 32}, - {.name = "S30", .offset = offsetof(vm_cpu_t, S30), .size = 32}, - {.name = "S31", .offset = offsetof(vm_cpu_t, S31), .size = 32}, - {.name = "S32", .offset = offsetof(vm_cpu_t, S32), .size = 32}, - {.name = "take_jmp", .offset = offsetof(vm_cpu_t, take_jmp), .size = 32}, - {.name = "last_addr", .offset = offsetof(vm_cpu_t, last_addr), .size = 32}, - {.name = "is_repeat_end", .offset = offsetof(vm_cpu_t, is_repeat_end), .size = 32}, - - {.name = "PC_end", .offset = offsetof(vm_cpu_t, PC_end), .size = 32}, - {.name = "RPE_instr_count", .offset = offsetof(vm_cpu_t, RPE_instr_count), .size = 32}, - {.name = "RPC_current", .offset = offsetof(vm_cpu_t, RPC_current), .size = 32}, + {.name = "R0", .offset = offsetof(struct vm_cpu, R0), .size = 32}, + {.name = "R1", .offset = offsetof(struct vm_cpu, R1), .size = 32}, + {.name = "R2", .offset = offsetof(struct vm_cpu, R2), .size = 32}, + {.name = "R3", .offset = offsetof(struct vm_cpu, R3), .size = 32}, + {.name = "R4", .offset = offsetof(struct vm_cpu, R4), .size = 32}, + {.name = "R5", .offset = offsetof(struct vm_cpu, R5), .size = 32}, + {.name = "R6", .offset = offsetof(struct vm_cpu, R6), .size = 32}, + {.name = "R7", .offset = offsetof(struct vm_cpu, R7), .size = 32}, + {.name = "R8", .offset = offsetof(struct vm_cpu, R8), .size = 32}, + {.name = "R9", .offset = offsetof(struct vm_cpu, R9), .size = 32}, + {.name = "R10", .offset = offsetof(struct vm_cpu, R10), .size = 32}, + {.name = "R11", .offset = offsetof(struct vm_cpu, R11), .size = 32}, + {.name = "R12", .offset = offsetof(struct vm_cpu, R12), .size = 32}, + {.name = "TP", .offset = offsetof(struct vm_cpu, TP), .size = 32}, + {.name = "GP", .offset = offsetof(struct vm_cpu, GP), .size = 32}, + {.name = "SP", .offset = offsetof(struct vm_cpu, SP), .size = 32}, + + {.name = "PC", .offset = offsetof(struct vm_cpu, PC), .size = 32}, + {.name = "LP", .offset = offsetof(struct vm_cpu, LP), .size = 32}, + {.name = "SAR", .offset = offsetof(struct vm_cpu, SAR), .size = 32}, + {.name = "S3", .offset = offsetof(struct vm_cpu, S3), .size = 32}, + {.name = "RPB", .offset = offsetof(struct vm_cpu, RPB), .size = 32}, + {.name = "RPE", .offset = offsetof(struct vm_cpu, RPE), .size = 32}, + {.name = "RPC", .offset = offsetof(struct vm_cpu, RPC), .size = 32}, + {.name = "HI", .offset = offsetof(struct vm_cpu, HI), .size = 32}, + {.name = "LO", .offset = offsetof(struct vm_cpu, LO), .size = 32}, + {.name = "S9", .offset = offsetof(struct vm_cpu, S9), .size = 32}, + {.name = "S10", .offset = offsetof(struct vm_cpu, S10), .size = 32}, + {.name = "S11", .offset = offsetof(struct vm_cpu, S11), .size = 32}, + {.name = "MB0", .offset = offsetof(struct vm_cpu, MB0), .size = 32}, + {.name = "ME0", .offset = offsetof(struct vm_cpu, ME0), .size = 32}, + {.name = "MB1", .offset = offsetof(struct vm_cpu, MB1), .size = 32}, + {.name = "ME1", .offset = offsetof(struct vm_cpu, ME1), .size = 32}, + {.name = "PSW", .offset = offsetof(struct vm_cpu, PSW), .size = 32}, + {.name = "ID", .offset = offsetof(struct vm_cpu, ID), .size = 32}, + {.name = "TMP", .offset = offsetof(struct vm_cpu, TMP), .size = 32}, + {.name = "EPC", .offset = offsetof(struct vm_cpu, EPC), .size = 32}, + {.name = "EXC", .offset = offsetof(struct vm_cpu, EXC), .size = 32}, + {.name = "CFG", .offset = offsetof(struct vm_cpu, CFG), .size = 32}, + {.name = "S22", .offset = offsetof(struct vm_cpu, S22), .size = 32}, + {.name = "NPC", .offset = offsetof(struct vm_cpu, NPC), .size = 32}, + {.name = "DBG", .offset = offsetof(struct vm_cpu, DBG), .size = 32}, + {.name = "DEPC", .offset = offsetof(struct vm_cpu, DEPC), .size = 32}, + {.name = "OPT", .offset = offsetof(struct vm_cpu, OPT), .size = 32}, + {.name = "RCFG", .offset = offsetof(struct vm_cpu, RCFG), .size = 32}, + {.name = "CCFG", .offset = offsetof(struct vm_cpu, CCFG), .size = 32}, + {.name = "S29", .offset = offsetof(struct vm_cpu, S29), .size = 32}, + {.name = "S30", .offset = offsetof(struct vm_cpu, S30), .size = 32}, + {.name = "S31", .offset = offsetof(struct vm_cpu, S31), .size = 32}, + {.name = "S32", .offset = offsetof(struct vm_cpu, S32), .size = 32}, + {.name = "take_jmp", .offset = offsetof(struct vm_cpu, take_jmp), .size = 32}, + {.name = "last_addr", .offset = offsetof(struct vm_cpu, last_addr), .size = 32}, + {.name = "is_repeat_end", .offset = offsetof(struct vm_cpu, is_repeat_end), .size = 32}, + + {.name = "PC_end", .offset = offsetof(struct vm_cpu, PC_end), .size = 32}, + {.name = "RPE_instr_count", .offset = offsetof(struct vm_cpu, RPE_instr_count), .size = 32}, + {.name = "RPC_current", .offset = offsetof(struct vm_cpu, RPC_current), .size = 32}, }; @@ -149,8 +149,8 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject* dict; PyObject *d_key, *d_value = NULL; Py_ssize_t pos = 0; - char* d_key_name; - uint64_t val; + const char *d_key_name; + uint32_t val; unsigned int i, found; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -159,7 +159,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) RAISE(PyExc_TypeError, "arg must be dict"); while(PyDict_Next(dict, &pos, &d_key, &d_value)){ PyGetStr(d_key_name, d_key); - PyGetInt(d_value, val); + PyGetInt_uint32_t(d_value, val); found = 0; for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ @@ -184,14 +184,14 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject * cpu_init_regs(JitCpu* self) { - memset(self->cpu, 0, sizeof(vm_cpu_t)); + memset(self->cpu, 0, sizeof(struct vm_cpu)); Py_INCREF(Py_None); return Py_None; } -void dump_gpregs(vm_cpu_t* vmcpu) +void dump_gpregs(struct vm_cpu* vmcpu) { printf("R0 %.4"PRIX32" ", vmcpu->R0); printf("R1 %.4"PRIX32" ", vmcpu->R1); @@ -215,7 +215,7 @@ void dump_gpregs(vm_cpu_t* vmcpu) PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { - vm_cpu_t* vmcpu; + struct vm_cpu* vmcpu; vmcpu = self->cpu; dump_gpregs(vmcpu); @@ -231,56 +231,41 @@ PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint32_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) return NULL; - PyGetInt(item1, i); + PyGetInt_uint32_t(item1, exception_flags); - ((vm_cpu_t*)self->cpu)->exception_flags = i; + ((struct vm_cpu*)self->cpu)->exception_flags = exception_flags; Py_INCREF(Py_None); return Py_None; } PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->exception_flags)); -} - -void check_automod(JitCpu* jitcpu, uint64_t addr, uint64_t size) -{ - PyObject *result; - - if (!(((VmMngr*)jitcpu->pyvm)->vm_mngr.exception_flags & EXCEPT_CODE_AUTOMOD)) - return; - result = PyObject_CallMethod(jitcpu->jitter, "automod_cb", "LL", addr, size); - Py_DECREF(result); - + return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->exception_flags)); } void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src) { vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 8); } void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src) { vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 16); } void MEM_WRITE_32(JitCpu* jitcpu, uint64_t addr, uint32_t src) { vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 32); } void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src) { vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 64); } @@ -291,25 +276,27 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args) Py_ssize_t py_length; char * buffer; - uint64_t size; + Py_ssize_t pysize; uint64_t addr; int ret = 0x1337; if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer)) return NULL; - PyGetInt(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); if(!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); - size = PyBytes_Size(py_buffer); + 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, size); + ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError,"arg must be str"); - check_automod(self, addr, size*8); Py_INCREF(Py_None); return Py_None; @@ -335,9 +322,9 @@ static PyMethodDef JitCpu_methods[] = { static int JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) { - self->cpu = malloc(sizeof(vm_cpu_t)); + self->cpu = malloc(sizeof(struct vm_cpu)); if (self->cpu == NULL) { - fprintf(stderr, "cannot alloc vm_cpu_t\n"); + fprintf(stderr, "cannot alloc struct vm_cpu\n"); exit(0); } return 0; @@ -599,19 +586,19 @@ static PyMethodDef JitCore_mep_Methods[] = { MOD_INIT(JitCore_mep) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "JitCore_mep", "JitCore_mep module", JitCore_mep_Methods); if (module == NULL) - return NULL; + RET_MODULE; if (PyType_Ready(&JitCpuType) < 0) - return NULL; + RET_MODULE; Py_INCREF(&JitCpuType); if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) - return NULL; + RET_MODULE; - return module; + RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_mep.h b/miasm/jitter/arch/JitCore_mep.h index bcf2283e..f656b3a0 100644 --- a/miasm/jitter/arch/JitCore_mep.h +++ b/miasm/jitter/arch/JitCore_mep.h @@ -1,6 +1,6 @@ // Inspired from JitCore_msp430.h -typedef struct { +struct vm_cpu { /* miasm flags */ uint32_t exception_flags; @@ -70,9 +70,9 @@ typedef struct { /* flags */ -} vm_cpu_t; +}; -_MIASM_EXPORT void dump_gpregs(vm_cpu_t* vmcpu); +_MIASM_EXPORT void dump_gpregs(struct vm_cpu* vmcpu); _MIASM_EXPORT void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src); _MIASM_EXPORT void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src); diff --git a/miasm/jitter/arch/JitCore_mips32.c b/miasm/jitter/arch/JitCore_mips32.c index 1455fec9..ce894e2a 100644 --- a/miasm/jitter/arch/JitCore_mips32.c +++ b/miasm/jitter/arch/JitCore_mips32.c @@ -13,42 +13,42 @@ -reg_dict gpreg_dict[] = { {.name = "ZERO", .offset = offsetof(vm_cpu_t, ZERO), .size = 32}, - {.name = "AT", .offset = offsetof(vm_cpu_t, AT), .size = 32}, - {.name = "V0", .offset = offsetof(vm_cpu_t, V0), .size = 32}, - {.name = "V1", .offset = offsetof(vm_cpu_t, V1), .size = 32}, - {.name = "A0", .offset = offsetof(vm_cpu_t, A0), .size = 32}, - {.name = "A1", .offset = offsetof(vm_cpu_t, A1), .size = 32}, - {.name = "A2", .offset = offsetof(vm_cpu_t, A2), .size = 32}, - {.name = "A3", .offset = offsetof(vm_cpu_t, A3), .size = 32}, - {.name = "T0", .offset = offsetof(vm_cpu_t, T0), .size = 32}, - {.name = "T1", .offset = offsetof(vm_cpu_t, T1), .size = 32}, - {.name = "T2", .offset = offsetof(vm_cpu_t, T2), .size = 32}, - {.name = "T3", .offset = offsetof(vm_cpu_t, T3), .size = 32}, - {.name = "T4", .offset = offsetof(vm_cpu_t, T4), .size = 32}, - {.name = "T5", .offset = offsetof(vm_cpu_t, T5), .size = 32}, - {.name = "T6", .offset = offsetof(vm_cpu_t, T6), .size = 32}, - {.name = "T7", .offset = offsetof(vm_cpu_t, T7), .size = 32}, - {.name = "S0", .offset = offsetof(vm_cpu_t, S0), .size = 32}, - {.name = "S1", .offset = offsetof(vm_cpu_t, S1), .size = 32}, - {.name = "S2", .offset = offsetof(vm_cpu_t, S2), .size = 32}, - {.name = "S3", .offset = offsetof(vm_cpu_t, S3), .size = 32}, - {.name = "S4", .offset = offsetof(vm_cpu_t, S4), .size = 32}, - {.name = "S5", .offset = offsetof(vm_cpu_t, S5), .size = 32}, - {.name = "S6", .offset = offsetof(vm_cpu_t, S6), .size = 32}, - {.name = "S7", .offset = offsetof(vm_cpu_t, S7), .size = 32}, - {.name = "T8", .offset = offsetof(vm_cpu_t, T8), .size = 32}, - {.name = "T9", .offset = offsetof(vm_cpu_t, T9), .size = 32}, - {.name = "K0", .offset = offsetof(vm_cpu_t, K0), .size = 32}, - {.name = "K1", .offset = offsetof(vm_cpu_t, K1), .size = 32}, - {.name = "GP", .offset = offsetof(vm_cpu_t, GP), .size = 32}, - {.name = "SP", .offset = offsetof(vm_cpu_t, SP), .size = 32}, - {.name = "FP", .offset = offsetof(vm_cpu_t, FP), .size = 32}, - {.name = "RA", .offset = offsetof(vm_cpu_t, RA), .size = 32}, - {.name = "PC", .offset = offsetof(vm_cpu_t, PC), .size = 32}, - {.name = "PC_FETCH", .offset = offsetof(vm_cpu_t, PC_FETCH), .size = 32}, - {.name = "R_LO", .offset = offsetof(vm_cpu_t, R_LO), .size = 32}, - {.name = "R_HI", .offset = offsetof(vm_cpu_t, R_HI), .size = 32}, +reg_dict gpreg_dict[] = { {.name = "ZERO", .offset = offsetof(struct vm_cpu, ZERO), .size = 32}, + {.name = "AT", .offset = offsetof(struct vm_cpu, AT), .size = 32}, + {.name = "V0", .offset = offsetof(struct vm_cpu, V0), .size = 32}, + {.name = "V1", .offset = offsetof(struct vm_cpu, V1), .size = 32}, + {.name = "A0", .offset = offsetof(struct vm_cpu, A0), .size = 32}, + {.name = "A1", .offset = offsetof(struct vm_cpu, A1), .size = 32}, + {.name = "A2", .offset = offsetof(struct vm_cpu, A2), .size = 32}, + {.name = "A3", .offset = offsetof(struct vm_cpu, A3), .size = 32}, + {.name = "T0", .offset = offsetof(struct vm_cpu, T0), .size = 32}, + {.name = "T1", .offset = offsetof(struct vm_cpu, T1), .size = 32}, + {.name = "T2", .offset = offsetof(struct vm_cpu, T2), .size = 32}, + {.name = "T3", .offset = offsetof(struct vm_cpu, T3), .size = 32}, + {.name = "T4", .offset = offsetof(struct vm_cpu, T4), .size = 32}, + {.name = "T5", .offset = offsetof(struct vm_cpu, T5), .size = 32}, + {.name = "T6", .offset = offsetof(struct vm_cpu, T6), .size = 32}, + {.name = "T7", .offset = offsetof(struct vm_cpu, T7), .size = 32}, + {.name = "S0", .offset = offsetof(struct vm_cpu, S0), .size = 32}, + {.name = "S1", .offset = offsetof(struct vm_cpu, S1), .size = 32}, + {.name = "S2", .offset = offsetof(struct vm_cpu, S2), .size = 32}, + {.name = "S3", .offset = offsetof(struct vm_cpu, S3), .size = 32}, + {.name = "S4", .offset = offsetof(struct vm_cpu, S4), .size = 32}, + {.name = "S5", .offset = offsetof(struct vm_cpu, S5), .size = 32}, + {.name = "S6", .offset = offsetof(struct vm_cpu, S6), .size = 32}, + {.name = "S7", .offset = offsetof(struct vm_cpu, S7), .size = 32}, + {.name = "T8", .offset = offsetof(struct vm_cpu, T8), .size = 32}, + {.name = "T9", .offset = offsetof(struct vm_cpu, T9), .size = 32}, + {.name = "K0", .offset = offsetof(struct vm_cpu, K0), .size = 32}, + {.name = "K1", .offset = offsetof(struct vm_cpu, K1), .size = 32}, + {.name = "GP", .offset = offsetof(struct vm_cpu, GP), .size = 32}, + {.name = "SP", .offset = offsetof(struct vm_cpu, SP), .size = 32}, + {.name = "FP", .offset = offsetof(struct vm_cpu, FP), .size = 32}, + {.name = "RA", .offset = offsetof(struct vm_cpu, RA), .size = 32}, + {.name = "PC", .offset = offsetof(struct vm_cpu, PC), .size = 32}, + {.name = "PC_FETCH", .offset = offsetof(struct vm_cpu, PC_FETCH), .size = 32}, + {.name = "R_LO", .offset = offsetof(struct vm_cpu, R_LO), .size = 32}, + {.name = "R_HI", .offset = offsetof(struct vm_cpu, R_HI), .size = 32}, }; /************************** JitCpu object **************************/ @@ -108,8 +108,8 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject* dict; PyObject *d_key, *d_value = NULL; Py_ssize_t pos = 0; - char* d_key_name; - uint64_t val; + const char *d_key_name; + uint32_t val; unsigned int i, found; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -118,7 +118,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) RAISE(PyExc_TypeError, "arg must be dict"); while(PyDict_Next(dict, &pos, &d_key, &d_value)){ PyGetStr(d_key_name, d_key); - PyGetInt(d_value, val); + PyGetInt_uint32_t(d_value, val); found = 0; for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ @@ -143,7 +143,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject * cpu_init_regs(JitCpu* self) { - memset(self->cpu, 0, sizeof(vm_cpu_t)); + memset(self->cpu, 0, sizeof(struct vm_cpu)); Py_INCREF(Py_None); return Py_None; @@ -151,7 +151,7 @@ PyObject * cpu_init_regs(JitCpu* self) } -void dump_gpregs(vm_cpu_t* vmcpu) +void dump_gpregs(struct vm_cpu* vmcpu) { printf("ZR %.8"PRIX32" AT %.8"PRIX32" V0 %.8"PRIX32" V1 %.8"PRIX32" ", @@ -177,7 +177,7 @@ void dump_gpregs(vm_cpu_t* vmcpu) PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { - vm_cpu_t* vmcpu; + struct vm_cpu* vmcpu; vmcpu = self->cpu; dump_gpregs(vmcpu); @@ -189,62 +189,42 @@ PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint32_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, i); + PyGetInt_uint32_t(item1, exception_flags); - ((vm_cpu_t*)self->cpu)->exception_flags = i; + ((struct vm_cpu*)self->cpu)->exception_flags = exception_flags; Py_INCREF(Py_None); return Py_None; } PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->exception_flags)); -} - - - - - - -void check_automod(JitCpu* jitcpu, uint64_t addr, uint64_t size) -{ - PyObject *result; - - if (!(((VmMngr*)jitcpu->pyvm)->vm_mngr.exception_flags & EXCEPT_CODE_AUTOMOD)) - return; - result = PyObject_CallMethod(jitcpu->jitter, "automod_cb", "LL", addr, size); - Py_DECREF(result); - + return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->exception_flags)); } void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src) { vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 8); } void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src) { vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 16); } void MEM_WRITE_32(JitCpu* jitcpu, uint64_t addr, uint32_t src) { vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 32); } void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src) { vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 64); } @@ -255,25 +235,28 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args) Py_ssize_t py_length; char * buffer; - uint64_t size; + 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(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); - if(!PyBytes_Check(py_buffer)) + if (!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); - size = PyBytes_Size(py_buffer); + 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, size); + ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError,"arg must be str"); - check_automod(self, addr, size*8); Py_INCREF(Py_None); return Py_None; @@ -307,9 +290,9 @@ static PyMethodDef JitCpu_methods[] = { static int JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) { - self->cpu = malloc(sizeof(vm_cpu_t)); + self->cpu = malloc(sizeof(struct vm_cpu)); if (self->cpu == NULL) { - fprintf(stderr, "cannot alloc vm_cpu_t\n"); + fprintf(stderr, "cannot alloc struct vm_cpu\n"); exit(EXIT_FAILURE); } return 0; @@ -513,19 +496,19 @@ static PyMethodDef JitCore_mips32_Methods[] = { MOD_INIT(JitCore_mips32) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "JitCore_mips32", "JitCore_mips32 module", JitCore_mips32_Methods); if (module == NULL) - return NULL; + RET_MODULE; if (PyType_Ready(&JitCpuType) < 0) - return NULL; + RET_MODULE; Py_INCREF(&JitCpuType); if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) - return NULL; + RET_MODULE; - return module; + RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_mips32.h b/miasm/jitter/arch/JitCore_mips32.h index e20d5133..74eb35ef 100644 --- a/miasm/jitter/arch/JitCore_mips32.h +++ b/miasm/jitter/arch/JitCore_mips32.h @@ -1,5 +1,5 @@ -typedef struct { +struct vm_cpu { uint32_t exception_flags; /* gpregs */ @@ -331,9 +331,9 @@ typedef struct { uint32_t CPR0_253; uint32_t CPR0_254; uint32_t CPR0_255; -}vm_cpu_t; +}; -_MIASM_EXPORT void dump_gpregs(vm_cpu_t* vmcpu); +_MIASM_EXPORT void dump_gpregs(struct vm_cpu* vmcpu); _MIASM_EXPORT void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src); _MIASM_EXPORT void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src); diff --git a/miasm/jitter/arch/JitCore_msp430.c b/miasm/jitter/arch/JitCore_msp430.c index c21296c7..3716756e 100644 --- a/miasm/jitter/arch/JitCore_msp430.c +++ b/miasm/jitter/arch/JitCore_msp430.c @@ -11,34 +11,34 @@ #include "JitCore_msp430.h" -reg_dict gpreg_dict[] = { {.name = "PC", .offset = offsetof(vm_cpu_t, PC)}, - {.name = "SP", .offset = offsetof(vm_cpu_t, SP)}, - //{.name = "SR", .offset = offsetof(vm_cpu_t, SR)}, - {.name = "R3", .offset = offsetof(vm_cpu_t, R3)}, - {.name = "R4", .offset = offsetof(vm_cpu_t, R4)}, - {.name = "R5", .offset = offsetof(vm_cpu_t, R5)}, - {.name = "R6", .offset = offsetof(vm_cpu_t, R6)}, - {.name = "R7", .offset = offsetof(vm_cpu_t, R7)}, - {.name = "R8", .offset = offsetof(vm_cpu_t, R8)}, - {.name = "R9", .offset = offsetof(vm_cpu_t, R9)}, - {.name = "R10", .offset = offsetof(vm_cpu_t, R10)}, - {.name = "R11", .offset = offsetof(vm_cpu_t, R11)}, - {.name = "R12", .offset = offsetof(vm_cpu_t, R12)}, - {.name = "R13", .offset = offsetof(vm_cpu_t, R13)}, - {.name = "R14", .offset = offsetof(vm_cpu_t, R14)}, - {.name = "R15", .offset = offsetof(vm_cpu_t, R15)}, - - {.name = "zf", .offset = offsetof(vm_cpu_t, zf)}, - {.name = "nf", .offset = offsetof(vm_cpu_t, nf)}, - {.name = "of", .offset = offsetof(vm_cpu_t, of)}, - {.name = "cf", .offset = offsetof(vm_cpu_t, cf)}, - - {.name = "cpuoff", .offset = offsetof(vm_cpu_t, zf)}, - {.name = "gie", .offset = offsetof(vm_cpu_t, zf)}, - {.name = "osc", .offset = offsetof(vm_cpu_t, zf)}, - {.name = "scg0", .offset = offsetof(vm_cpu_t, zf)}, - {.name = "scg1", .offset = offsetof(vm_cpu_t, zf)}, - {.name = "res", .offset = offsetof(vm_cpu_t, zf)}, +reg_dict gpreg_dict[] = { {.name = "PC", .offset = offsetof(struct vm_cpu, PC)}, + {.name = "SP", .offset = offsetof(struct vm_cpu, SP)}, + //{.name = "SR", .offset = offsetof(struct vm_cpu, SR)}, + {.name = "R3", .offset = offsetof(struct vm_cpu, R3)}, + {.name = "R4", .offset = offsetof(struct vm_cpu, R4)}, + {.name = "R5", .offset = offsetof(struct vm_cpu, R5)}, + {.name = "R6", .offset = offsetof(struct vm_cpu, R6)}, + {.name = "R7", .offset = offsetof(struct vm_cpu, R7)}, + {.name = "R8", .offset = offsetof(struct vm_cpu, R8)}, + {.name = "R9", .offset = offsetof(struct vm_cpu, R9)}, + {.name = "R10", .offset = offsetof(struct vm_cpu, R10)}, + {.name = "R11", .offset = offsetof(struct vm_cpu, R11)}, + {.name = "R12", .offset = offsetof(struct vm_cpu, R12)}, + {.name = "R13", .offset = offsetof(struct vm_cpu, R13)}, + {.name = "R14", .offset = offsetof(struct vm_cpu, R14)}, + {.name = "R15", .offset = offsetof(struct vm_cpu, R15)}, + + {.name = "zf", .offset = offsetof(struct vm_cpu, zf)}, + {.name = "nf", .offset = offsetof(struct vm_cpu, nf)}, + {.name = "of", .offset = offsetof(struct vm_cpu, of)}, + {.name = "cf", .offset = offsetof(struct vm_cpu, cf)}, + + {.name = "cpuoff", .offset = offsetof(struct vm_cpu, zf)}, + {.name = "gie", .offset = offsetof(struct vm_cpu, zf)}, + {.name = "osc", .offset = offsetof(struct vm_cpu, zf)}, + {.name = "scg0", .offset = offsetof(struct vm_cpu, zf)}, + {.name = "scg1", .offset = offsetof(struct vm_cpu, zf)}, + {.name = "res", .offset = offsetof(struct vm_cpu, zf)}, }; @@ -90,8 +90,8 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject* dict; PyObject *d_key, *d_value = NULL; Py_ssize_t pos = 0; - char* d_key_name; - uint64_t val; + const char *d_key_name; + uint32_t val; unsigned int i, found; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -100,7 +100,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) RAISE(PyExc_TypeError, "arg must be dict"); while(PyDict_Next(dict, &pos, &d_key, &d_value)){ PyGetStr(d_key_name, d_key); - PyGetInt(d_value, val); + PyGetInt_uint32_t(d_value, val); found = 0; for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ if (strcmp(d_key_name, gpreg_dict[i].name)) @@ -124,14 +124,14 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject * cpu_init_regs(JitCpu* self) { - memset(self->cpu, 0, sizeof(vm_cpu_t)); + memset(self->cpu, 0, sizeof(struct vm_cpu)); Py_INCREF(Py_None); return Py_None; } -void dump_gpregs(vm_cpu_t* vmcpu) +void dump_gpregs(struct vm_cpu* vmcpu) { printf("PC %.4"PRIX32" SP %.4"PRIX32" R3 %.4"PRIX32" ", @@ -149,7 +149,7 @@ void dump_gpregs(vm_cpu_t* vmcpu) PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { - vm_cpu_t* vmcpu; + struct vm_cpu* vmcpu; vmcpu = self->cpu; dump_gpregs(vmcpu); @@ -166,60 +166,41 @@ PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint32_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, i); + PyGetInt_uint32_t(item1, exception_flags); - ((vm_cpu_t*)self->cpu)->exception_flags = i; + ((struct vm_cpu*)self->cpu)->exception_flags = exception_flags; Py_INCREF(Py_None); return Py_None; } PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->exception_flags)); -} - - - - - -void check_automod(JitCpu* jitcpu, uint64_t addr, uint64_t size) -{ - PyObject *result; - - if (!(((VmMngr*)jitcpu->pyvm)->vm_mngr.exception_flags & EXCEPT_CODE_AUTOMOD)) - return; - result = PyObject_CallMethod(jitcpu->jitter, "automod_cb", "LL", addr, size); - Py_DECREF(result); - + return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->exception_flags)); } void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src) { vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 8); } void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src) { vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 16); } void MEM_WRITE_32(JitCpu* jitcpu, uint64_t addr, uint32_t src) { vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 32); } void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src) { vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 64); } @@ -230,25 +211,28 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args) Py_ssize_t py_length; char * buffer; - uint64_t size; + 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(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); if(!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); - size = PyBytes_Size(py_buffer); + 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, size); + ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError,"arg must be str"); - check_automod(self, addr, size*8); Py_INCREF(Py_None); return Py_None; @@ -283,9 +267,9 @@ static PyMethodDef JitCpu_methods[] = { static int JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) { - self->cpu = malloc(sizeof(vm_cpu_t)); + self->cpu = malloc(sizeof(struct vm_cpu)); if (self->cpu == NULL) { - fprintf(stderr, "cannot alloc vm_cpu_t\n"); + fprintf(stderr, "cannot alloc struct vm_cpu\n"); exit(EXIT_FAILURE); } return 0; @@ -459,19 +443,19 @@ static PyMethodDef JitCore_msp430_Methods[] = { MOD_INIT(JitCore_msp430) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "JitCore_msp430", "JitCore_msp430 module", JitCore_msp430_Methods); if (module == NULL) - return NULL; + RET_MODULE; if (PyType_Ready(&JitCpuType) < 0) - return NULL; + RET_MODULE; Py_INCREF(&JitCpuType); if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) - return NULL; + RET_MODULE; - return module; + RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_msp430.h b/miasm/jitter/arch/JitCore_msp430.h index 1c802e9e..d7b6a7b9 100644 --- a/miasm/jitter/arch/JitCore_msp430.h +++ b/miasm/jitter/arch/JitCore_msp430.h @@ -1,5 +1,5 @@ -typedef struct { +struct vm_cpu { uint32_t exception_flags; /* gpregs */ @@ -32,11 +32,11 @@ typedef struct { uint32_t scg1; uint32_t res; -}vm_cpu_t; +}; #define RETURN_PC return BlockDst; -_MIASM_EXPORT void dump_gpregs(vm_cpu_t* vmcpu); +_MIASM_EXPORT void dump_gpregs(struct vm_cpu* vmcpu); _MIASM_EXPORT void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src); _MIASM_EXPORT void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src); diff --git a/miasm/jitter/arch/JitCore_ppc32.c b/miasm/jitter/arch/JitCore_ppc32.c index 8a1bb79e..49dc0b1e 100644 --- a/miasm/jitter/arch/JitCore_ppc32.c +++ b/miasm/jitter/arch/JitCore_ppc32.c @@ -37,8 +37,8 @@ cpu_set_gpreg(JitCpu *self, PyObject *args) { PyObject *dict; PyObject *d_key, *d_value = NULL; Py_ssize_t pos = 0; - char* d_key_name; - uint64_t val; + const char *d_key_name; + uint32_t val; unsigned int i; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -49,7 +49,7 @@ cpu_set_gpreg(JitCpu *self, PyObject *args) { while(PyDict_Next(dict, &pos, &d_key, &d_value)) { int found = 0; PyGetStr(d_key_name, d_key); - PyGetInt(d_value, val); + PyGetInt_uint32_t(d_value, val); for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ if (strcmp(d_key_name, gpreg_dict[i].name)) @@ -116,14 +116,14 @@ cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) PyObject * cpu_set_exception(JitCpu *self, PyObject *args) { PyObject *item1; - uint64_t i; + uint64_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) return NULL; - PyGetInt(item1, i); + PyGetInt_uint64_t(item1, exception_flags); - ((struct vm_cpu *)self->cpu)->exception_flags = i; + ((struct vm_cpu *)self->cpu)->exception_flags = exception_flags; Py_INCREF(Py_None); return Py_None; @@ -139,38 +139,24 @@ cpu_get_spr_access(JitCpu *self, PyObject *args) { return PyLong_FromUnsignedLongLong(((struct vm_cpu *) self->cpu)->spr_access); } -void -check_automod(JitCpu *jitcpu, uint64_t addr, uint64_t size) { - PyObject *result; - - if (!(((VmMngr*)jitcpu->pyvm)->vm_mngr.exception_flags & EXCEPT_CODE_AUTOMOD)) - return; - result = PyObject_CallMethod(jitcpu->jitter, "automod_cb", "LL", addr, size); - Py_DECREF(result); -} - void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src) { vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 8); } void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src) { vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 16); } void MEM_WRITE_32(JitCpu* jitcpu, uint64_t addr, uint32_t src) { vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 32); } void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src) { vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); - check_automod(jitcpu, addr, 64); } @@ -182,25 +168,27 @@ vm_set_mem(JitCpu *self, PyObject *args) { Py_ssize_t py_length; char *buffer; - uint64_t size; + Py_ssize_t pysize; uint64_t addr; int ret = 0x1337; if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer)) return NULL; - PyGetInt(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); if(!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); - size = PyBytes_Size(py_buffer); + 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, size); + ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError,"arg must be str"); - check_automod(self, addr, size*8); Py_INCREF(Py_None); return Py_None; @@ -326,19 +314,19 @@ static PyMethodDef JitCore_ppc32_Methods[] = { MOD_INIT(JitCore_ppc32) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "JitCore_ppc32", "JitCore_ppc32 module", JitCore_ppc32_Methods); if (module == NULL) - return NULL; + RET_MODULE; if (PyType_Ready(&JitCpuType) < 0) - return NULL; + RET_MODULE; Py_INCREF(&JitCpuType); if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) - return NULL; + RET_MODULE; - return module; + RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_ppc32.h b/miasm/jitter/arch/JitCore_ppc32.h index f2a5200e..abb04941 100644 --- a/miasm/jitter/arch/JitCore_ppc32.h +++ b/miasm/jitter/arch/JitCore_ppc32.h @@ -16,8 +16,6 @@ struct vm_cpu { _MIASM_EXPORT void dump_gpregs(struct vm_cpu *); -typedef struct vm_cpu vm_cpu_t; - _MIASM_EXPORT void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src); _MIASM_EXPORT void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src); _MIASM_EXPORT void MEM_WRITE_32(JitCpu* jitcpu, uint64_t addr, uint32_t src); diff --git a/miasm/jitter/arch/JitCore_x86.c b/miasm/jitter/arch/JitCore_x86.c index 50ce6bd5..608893a7 100644 --- a/miasm/jitter/arch/JitCore_x86.c +++ b/miasm/jitter/arch/JitCore_x86.c @@ -12,74 +12,74 @@ #include "JitCore_x86.h" -vm_cpu_t ref_arch_regs; +struct vm_cpu ref_arch_regs; reg_dict gpreg_dict[] = { - {.name = "RAX", .offset = offsetof(vm_cpu_t, RAX), .size = 64}, - {.name = "RBX", .offset = offsetof(vm_cpu_t, RBX), .size = 64}, - {.name = "RCX", .offset = offsetof(vm_cpu_t, RCX), .size = 64}, - {.name = "RDX", .offset = offsetof(vm_cpu_t, RDX), .size = 64}, - {.name = "RSI", .offset = offsetof(vm_cpu_t, RSI), .size = 64}, - {.name = "RDI", .offset = offsetof(vm_cpu_t, RDI), .size = 64}, - {.name = "RSP", .offset = offsetof(vm_cpu_t, RSP), .size = 64}, - {.name = "RBP", .offset = offsetof(vm_cpu_t, RBP), .size = 64}, - - {.name = "R8", .offset = offsetof(vm_cpu_t, R8), .size = 64}, - {.name = "R9", .offset = offsetof(vm_cpu_t, R9), .size = 64}, - {.name = "R10", .offset = offsetof(vm_cpu_t, R10), .size = 64}, - {.name = "R11", .offset = offsetof(vm_cpu_t, R11), .size = 64}, - {.name = "R12", .offset = offsetof(vm_cpu_t, R12), .size = 64}, - {.name = "R13", .offset = offsetof(vm_cpu_t, R13), .size = 64}, - {.name = "R14", .offset = offsetof(vm_cpu_t, R14), .size = 64}, - {.name = "R15", .offset = offsetof(vm_cpu_t, R15), .size = 64}, - - {.name = "RIP", .offset = offsetof(vm_cpu_t, RIP), .size = 64}, - - {.name = "zf", .offset = offsetof(vm_cpu_t, zf), .size = 8}, - {.name = "nf", .offset = offsetof(vm_cpu_t, nf), .size = 8}, - {.name = "pf", .offset = offsetof(vm_cpu_t, pf), .size = 8}, - {.name = "of", .offset = offsetof(vm_cpu_t, of), .size = 8}, - {.name = "cf", .offset = offsetof(vm_cpu_t, cf), .size = 8}, - {.name = "af", .offset = offsetof(vm_cpu_t, af), .size = 8}, - {.name = "df", .offset = offsetof(vm_cpu_t, df), .size = 8}, - - {.name = "ES", .offset = offsetof(vm_cpu_t, ES), .size = 16}, - {.name = "CS", .offset = offsetof(vm_cpu_t, CS), .size = 16}, - {.name = "SS", .offset = offsetof(vm_cpu_t, SS), .size = 16}, - {.name = "DS", .offset = offsetof(vm_cpu_t, DS), .size = 16}, - {.name = "FS", .offset = offsetof(vm_cpu_t, FS), .size = 16}, - {.name = "GS", .offset = offsetof(vm_cpu_t, GS), .size = 16}, - - {.name = "MM0", .offset = offsetof(vm_cpu_t, MM0), .size = 64}, - {.name = "MM1", .offset = offsetof(vm_cpu_t, MM1), .size = 64}, - {.name = "MM2", .offset = offsetof(vm_cpu_t, MM2), .size = 64}, - {.name = "MM3", .offset = offsetof(vm_cpu_t, MM3), .size = 64}, - {.name = "MM4", .offset = offsetof(vm_cpu_t, MM4), .size = 64}, - {.name = "MM5", .offset = offsetof(vm_cpu_t, MM5), .size = 64}, - {.name = "MM6", .offset = offsetof(vm_cpu_t, MM6), .size = 64}, - {.name = "MM7", .offset = offsetof(vm_cpu_t, MM7), .size = 64}, - - {.name = "XMM0", .offset = offsetof(vm_cpu_t, XMM0), .size = 128}, - {.name = "XMM1", .offset = offsetof(vm_cpu_t, XMM1), .size = 128}, - {.name = "XMM2", .offset = offsetof(vm_cpu_t, XMM2), .size = 128}, - {.name = "XMM3", .offset = offsetof(vm_cpu_t, XMM3), .size = 128}, - {.name = "XMM4", .offset = offsetof(vm_cpu_t, XMM4), .size = 128}, - {.name = "XMM5", .offset = offsetof(vm_cpu_t, XMM5), .size = 128}, - {.name = "XMM6", .offset = offsetof(vm_cpu_t, XMM6), .size = 128}, - {.name = "XMM7", .offset = offsetof(vm_cpu_t, XMM7), .size = 128}, - {.name = "XMM8", .offset = offsetof(vm_cpu_t, XMM8), .size = 128}, - {.name = "XMM9", .offset = offsetof(vm_cpu_t, XMM9), .size = 128}, - {.name = "XMM10", .offset = offsetof(vm_cpu_t, XMM10), .size = 128}, - {.name = "XMM11", .offset = offsetof(vm_cpu_t, XMM11), .size = 128}, - {.name = "XMM12", .offset = offsetof(vm_cpu_t, XMM12), .size = 128}, - {.name = "XMM13", .offset = offsetof(vm_cpu_t, XMM13), .size = 128}, - {.name = "XMM14", .offset = offsetof(vm_cpu_t, XMM14), .size = 128}, - {.name = "XMM15", .offset = offsetof(vm_cpu_t, XMM15), .size = 128}, - - {.name = "tsc", .offset = offsetof(vm_cpu_t, tsc), .size = 64}, - - {.name = "exception_flags", .offset = offsetof(vm_cpu_t, exception_flags), .size = 32}, - {.name = "interrupt_num", .offset = offsetof(vm_cpu_t, interrupt_num), .size = 32}, + {.name = "RAX", .offset = offsetof(struct vm_cpu, RAX), .size = 64}, + {.name = "RBX", .offset = offsetof(struct vm_cpu, RBX), .size = 64}, + {.name = "RCX", .offset = offsetof(struct vm_cpu, RCX), .size = 64}, + {.name = "RDX", .offset = offsetof(struct vm_cpu, RDX), .size = 64}, + {.name = "RSI", .offset = offsetof(struct vm_cpu, RSI), .size = 64}, + {.name = "RDI", .offset = offsetof(struct vm_cpu, RDI), .size = 64}, + {.name = "RSP", .offset = offsetof(struct vm_cpu, RSP), .size = 64}, + {.name = "RBP", .offset = offsetof(struct vm_cpu, RBP), .size = 64}, + + {.name = "R8", .offset = offsetof(struct vm_cpu, R8), .size = 64}, + {.name = "R9", .offset = offsetof(struct vm_cpu, R9), .size = 64}, + {.name = "R10", .offset = offsetof(struct vm_cpu, R10), .size = 64}, + {.name = "R11", .offset = offsetof(struct vm_cpu, R11), .size = 64}, + {.name = "R12", .offset = offsetof(struct vm_cpu, R12), .size = 64}, + {.name = "R13", .offset = offsetof(struct vm_cpu, R13), .size = 64}, + {.name = "R14", .offset = offsetof(struct vm_cpu, R14), .size = 64}, + {.name = "R15", .offset = offsetof(struct vm_cpu, R15), .size = 64}, + + {.name = "RIP", .offset = offsetof(struct vm_cpu, RIP), .size = 64}, + + {.name = "zf", .offset = offsetof(struct vm_cpu, zf), .size = 8}, + {.name = "nf", .offset = offsetof(struct vm_cpu, nf), .size = 8}, + {.name = "pf", .offset = offsetof(struct vm_cpu, pf), .size = 8}, + {.name = "of", .offset = offsetof(struct vm_cpu, of), .size = 8}, + {.name = "cf", .offset = offsetof(struct vm_cpu, cf), .size = 8}, + {.name = "af", .offset = offsetof(struct vm_cpu, af), .size = 8}, + {.name = "df", .offset = offsetof(struct vm_cpu, df), .size = 8}, + + {.name = "ES", .offset = offsetof(struct vm_cpu, ES), .size = 16}, + {.name = "CS", .offset = offsetof(struct vm_cpu, CS), .size = 16}, + {.name = "SS", .offset = offsetof(struct vm_cpu, SS), .size = 16}, + {.name = "DS", .offset = offsetof(struct vm_cpu, DS), .size = 16}, + {.name = "FS", .offset = offsetof(struct vm_cpu, FS), .size = 16}, + {.name = "GS", .offset = offsetof(struct vm_cpu, GS), .size = 16}, + + {.name = "MM0", .offset = offsetof(struct vm_cpu, MM0), .size = 64}, + {.name = "MM1", .offset = offsetof(struct vm_cpu, MM1), .size = 64}, + {.name = "MM2", .offset = offsetof(struct vm_cpu, MM2), .size = 64}, + {.name = "MM3", .offset = offsetof(struct vm_cpu, MM3), .size = 64}, + {.name = "MM4", .offset = offsetof(struct vm_cpu, MM4), .size = 64}, + {.name = "MM5", .offset = offsetof(struct vm_cpu, MM5), .size = 64}, + {.name = "MM6", .offset = offsetof(struct vm_cpu, MM6), .size = 64}, + {.name = "MM7", .offset = offsetof(struct vm_cpu, MM7), .size = 64}, + + {.name = "XMM0", .offset = offsetof(struct vm_cpu, XMM0), .size = 128}, + {.name = "XMM1", .offset = offsetof(struct vm_cpu, XMM1), .size = 128}, + {.name = "XMM2", .offset = offsetof(struct vm_cpu, XMM2), .size = 128}, + {.name = "XMM3", .offset = offsetof(struct vm_cpu, XMM3), .size = 128}, + {.name = "XMM4", .offset = offsetof(struct vm_cpu, XMM4), .size = 128}, + {.name = "XMM5", .offset = offsetof(struct vm_cpu, XMM5), .size = 128}, + {.name = "XMM6", .offset = offsetof(struct vm_cpu, XMM6), .size = 128}, + {.name = "XMM7", .offset = offsetof(struct vm_cpu, XMM7), .size = 128}, + {.name = "XMM8", .offset = offsetof(struct vm_cpu, XMM8), .size = 128}, + {.name = "XMM9", .offset = offsetof(struct vm_cpu, XMM9), .size = 128}, + {.name = "XMM10", .offset = offsetof(struct vm_cpu, XMM10), .size = 128}, + {.name = "XMM11", .offset = offsetof(struct vm_cpu, XMM11), .size = 128}, + {.name = "XMM12", .offset = offsetof(struct vm_cpu, XMM12), .size = 128}, + {.name = "XMM13", .offset = offsetof(struct vm_cpu, XMM13), .size = 128}, + {.name = "XMM14", .offset = offsetof(struct vm_cpu, XMM14), .size = 128}, + {.name = "XMM15", .offset = offsetof(struct vm_cpu, XMM15), .size = 128}, + + {.name = "tsc", .offset = offsetof(struct vm_cpu, tsc), .size = 64}, + + {.name = "exception_flags", .offset = offsetof(struct vm_cpu, exception_flags), .size = 32}, + {.name = "interrupt_num", .offset = offsetof(struct vm_cpu, interrupt_num), .size = 32}, }; @@ -167,9 +167,12 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) { PyObject* dict; PyObject *d_key, *d_value = NULL; - char* d_key_name; + const char *d_key_name; Py_ssize_t pos = 0; - uint64_t val; + uint8_t val08; + uint16_t val16; + uint32_t val32; + uint64_t val64; unsigned int i, found; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -184,21 +187,24 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) continue; found = 1; switch (gpreg_dict[i].size) { + default: + RAISE(PyExc_TypeError, "Unsupported size"); + break; case 8: - PyGetInt(d_value, val); - *((uint8_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val; + PyGetInt_uint8_t(d_value, val08); + *((uint8_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val08; break; case 16: - PyGetInt(d_value, val); - *((uint16_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val; + PyGetInt_uint16_t(d_value, val16); + *((uint16_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val16; break; case 32: - PyGetInt(d_value, val); - *((uint32_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val; + PyGetInt_uint32_t(d_value, val32); + *((uint32_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val32; break; case 64: - PyGetInt(d_value, val); - *((uint64_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val; + PyGetInt_uint64_t(d_value, val64); + *((uint64_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val64; break; case 128: { @@ -271,15 +277,15 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) PyObject * cpu_init_regs(JitCpu* self) { - memset(self->cpu, 0, sizeof(vm_cpu_t)); - ((vm_cpu_t*)self->cpu)->tsc = 0x1122334455667788ULL; - ((vm_cpu_t*)self->cpu)->i_f = 1; + memset(self->cpu, 0, sizeof(struct vm_cpu)); + ((struct vm_cpu*)self->cpu)->tsc = 0x1122334455667788ULL; + ((struct vm_cpu*)self->cpu)->i_f = 1; Py_INCREF(Py_None); return Py_None; } -void dump_gpregs_16(vm_cpu_t* vmcpu) +void dump_gpregs_16(struct vm_cpu* vmcpu) { printf("EAX %.8"PRIX32" EBX %.8"PRIX32" ECX %.8"PRIX32" EDX %.8"PRIX32" ", (uint32_t)(vmcpu->RAX & 0xFFFFFFFF), @@ -300,7 +306,7 @@ void dump_gpregs_16(vm_cpu_t* vmcpu) (uint32_t)(vmcpu->cf & 0x1)); } -void dump_gpregs_32(vm_cpu_t* vmcpu) +void dump_gpregs_32(struct vm_cpu* vmcpu) { printf("EAX %.8"PRIX32" EBX %.8"PRIX32" ECX %.8"PRIX32" EDX %.8"PRIX32" ", @@ -323,7 +329,7 @@ void dump_gpregs_32(vm_cpu_t* vmcpu) } -void dump_gpregs_64(vm_cpu_t* vmcpu) +void dump_gpregs_64(struct vm_cpu* vmcpu) { printf("RAX %.16"PRIX64" RBX %.16"PRIX64" RCX %.16"PRIX64" RDX %.16"PRIX64" ", @@ -345,7 +351,7 @@ void dump_gpregs_64(vm_cpu_t* vmcpu) PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { - vm_cpu_t* vmcpu; + struct vm_cpu* vmcpu; vmcpu = self->cpu; dump_gpregs_64(vmcpu); @@ -356,14 +362,14 @@ PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) { - vm_cpu_t* vmcpu; + struct vm_cpu* vmcpu; PyObject *item1; uint64_t attrib; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, attrib); + PyGetInt_uint64_t(item1, attrib); vmcpu = self->cpu; if (attrib == 16 || attrib == 32) @@ -383,41 +389,41 @@ PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint32_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, i); + PyGetInt_uint32_t(item1, exception_flags); - ((vm_cpu_t*)self->cpu)->exception_flags = i; + ((struct vm_cpu*)self->cpu)->exception_flags = exception_flags; Py_INCREF(Py_None); return Py_None; } PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->exception_flags)); + return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->exception_flags)); } PyObject* cpu_set_interrupt_num(JitCpu* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint32_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, i); + PyGetInt_uint32_t(item1, exception_flags); - ((vm_cpu_t*)self->cpu)->interrupt_num = i; + ((struct vm_cpu*)self->cpu)->interrupt_num = exception_flags; Py_INCREF(Py_None); return Py_None; } PyObject* cpu_get_interrupt_num(JitCpu* self, PyObject* args) { - return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->interrupt_num)); + return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->interrupt_num)); } PyObject* cpu_set_segm_base(JitCpu* self, PyObject* args) @@ -428,9 +434,9 @@ PyObject* cpu_set_segm_base(JitCpu* self, PyObject* args) if (!PyArg_ParseTuple(args, "OO", &item1, &item2)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, segm_num); - PyGetInt(item2, segm_base); - ((vm_cpu_t*)self->cpu)->segm_base[segm_num] = segm_base; + PyGetInt_uint64_t(item1, segm_num); + PyGetInt_uint64_t(item2, segm_base); + ((struct vm_cpu*)self->cpu)->segm_base[segm_num] = segm_base; Py_INCREF(Py_None); return Py_None; @@ -444,14 +450,14 @@ PyObject* cpu_get_segm_base(JitCpu* self, PyObject* args) if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, segm_num); - v = PyLong_FromLong((long)(((vm_cpu_t*)self->cpu)->segm_base[segm_num])); + PyGetInt_uint64_t(item1, segm_num); + v = PyLong_FromLong((long)(((struct vm_cpu*)self->cpu)->segm_base[segm_num])); return v; } uint64_t segm2addr(JitCpu* jitcpu, uint64_t segm, uint64_t addr) { - return addr + ((vm_cpu_t*)jitcpu->cpu)->segm_base[segm]; + return addr + ((struct vm_cpu*)jitcpu->cpu)->segm_base[segm]; } void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src) @@ -483,22 +489,25 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args) Py_ssize_t py_length; char * buffer; - uint64_t size; + 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(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); if(!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); - size = PyBytes_Size(py_buffer); + 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, size); + ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError,"arg must be str"); @@ -543,43 +552,45 @@ static PyMethodDef JitCpu_methods[] = { static int JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) { - self->cpu = malloc(sizeof(vm_cpu_t)); + self->cpu = malloc(sizeof(struct vm_cpu)); if (self->cpu == NULL) { - fprintf(stderr, "cannot alloc vm_cpu_t\n"); + fprintf(stderr, "cannot alloc struct vm_cpu\n"); exit(EXIT_FAILURE); } return 0; } -#define getset_reg_E_u32(regname) \ +#define getset_reg_E_u32(regname) \ static PyObject *JitCpu_get_E ## regname (JitCpu *self, void *closure) \ { \ - return PyLong_FromUnsignedLongLong((uint32_t)(((vm_cpu_t*)(self->cpu))->R ## regname & 0xFFFFFFFF )); \ + return PyLong_FromUnsignedLongLong((uint32_t)(self->cpu->R ## regname & 0xFFFFFFFF )); \ } \ static int JitCpu_set_E ## regname (JitCpu *self, PyObject *value, void *closure) \ { \ - uint64_t val; \ - PyGetInt_retneg(value, val); \ - val &= 0xFFFFFFFF; \ - val |= ((vm_cpu_t*)(self->cpu))->R ##regname & 0xFFFFFFFF00000000ULL; \ - ((vm_cpu_t*)(self->cpu))->R ## regname = val; \ + uint32_t val32; \ + uint64_t val64; \ + PyGetInt_uint32_t_retneg(value, val32); \ + val64 = val32; \ + val64 |= self->cpu->R ##regname & 0xFFFFFFFF00000000ULL; \ + self->cpu->R ## regname = val64; \ return 0; \ } -#define getset_reg_R_u16(regname) \ +#define getset_reg_R_u16(regname) \ static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \ { \ - return PyLong_FromUnsignedLongLong((uint16_t)(((vm_cpu_t*)(self->cpu))->R ## regname & 0xFFFF )); \ + return PyLong_FromUnsignedLongLong((uint16_t)(self->cpu->R ## regname & 0xFFFF )); \ } \ static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \ { \ - uint64_t val; \ - PyGetInt_retneg(value, val); \ - val &= 0xFFFF; \ - val |= ((vm_cpu_t*)(self->cpu))->R ##regname & 0xFFFFFFFFFFFF0000ULL; \ - ((vm_cpu_t*)(self->cpu))->R ## regname = val; \ + uint16_t val16; \ + uint64_t val64; \ + PyGetInt_uint16_t_retneg(value, val16); \ + val64 = val16; \ + val64 |= self->cpu->R ##regname & 0xFFFFFFFFFFFF0000ULL; \ + self->cpu->R ## regname = val64; \ return 0; \ } @@ -604,13 +615,13 @@ getset_reg_u64(R15); getset_reg_u64(RIP); -getset_reg_u64(zf); -getset_reg_u64(nf); -getset_reg_u64(pf); -getset_reg_u64(of); -getset_reg_u64(cf); -getset_reg_u64(af); -getset_reg_u64(df); +getset_reg_u8(zf); +getset_reg_u8(nf); +getset_reg_u8(pf); +getset_reg_u8(of); +getset_reg_u8(cf); +getset_reg_u8(af); +getset_reg_u8(df); getset_reg_u16(ES); @@ -928,19 +939,19 @@ static PyMethodDef JitCore_x86_Methods[] = { MOD_INIT(JitCore_x86) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "JitCore_x86", "JitCore_x86 module", JitCore_x86_Methods); if (module == NULL) - return NULL; + RET_MODULE; if (PyType_Ready(&JitCpuType) < 0) - return NULL; + RET_MODULE; Py_INCREF(&JitCpuType); if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) - return NULL; + RET_MODULE; - return module; + RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_x86.h b/miasm/jitter/arch/JitCore_x86.h index 27d94d7c..5c005e86 100644 --- a/miasm/jitter/arch/JitCore_x86.h +++ b/miasm/jitter/arch/JitCore_x86.h @@ -6,7 +6,7 @@ #define _MIASM_EXPORT #endif -typedef struct { +struct vm_cpu { uint32_t exception_flags; uint32_t interrupt_num; @@ -120,12 +120,12 @@ typedef struct { bn_t XMM14; bn_t XMM15; - uint32_t segm_base[0x10000]; + uint64_t segm_base[0x10000]; -}vm_cpu_t; +}; -_MIASM_EXPORT void dump_gpregs_32(vm_cpu_t* vmcpu); -_MIASM_EXPORT void dump_gpregs_64(vm_cpu_t* vmcpu); +_MIASM_EXPORT void dump_gpregs_32(struct vm_cpu* vmcpu); +_MIASM_EXPORT void dump_gpregs_64(struct vm_cpu* vmcpu); _MIASM_EXPORT uint64_t segm2addr(JitCpu* jitcpu, uint64_t segm, uint64_t addr); _MIASM_EXPORT void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src); diff --git a/miasm/jitter/bn.c b/miasm/jitter/bn.c index dd4f34ef..43e552a4 100644 --- a/miasm/jitter/bn.c +++ b/miasm/jitter/bn.c @@ -67,10 +67,10 @@ bn_t bignum_from_int(DTYPE_TMP i) n.array[0] = (i & 0x0000ffff); n.array[1] = (i & 0xffff0000) >> 16; #elif (WORD_SIZE == 4) - n.array[0] = i; + n.array[0] = (DTYPE)i; DTYPE_TMP num_32 = 32; DTYPE_TMP tmp = i >> num_32; /* bit-shift with U64 operands to force 64-bit results */ - n.array[1] = tmp; + n.array[1] = (DTYPE)tmp; #endif #endif @@ -94,10 +94,10 @@ bn_t bignum_from_uint64(uint64_t i) n.array[0] = (i & 0x0000ffff); n.array[1] = (i & 0xffff0000) >> 16; #elif (WORD_SIZE == 4) - n.array[0] = i; + n.array[0] = (DTYPE)i; DTYPE_TMP num_32 = 32; DTYPE_TMP tmp = i >> num_32; /* bit-shift with U64 operands to force 64-bit results */ - n.array[1] = tmp; + n.array[1] = (DTYPE)tmp; #endif #endif @@ -242,7 +242,7 @@ bn_t bignum_inc(bn_t n) //require(n, "n is null"); DTYPE res; - DTYPE_TMP tmp; /* copy of n */ + DTYPE tmp; /* copy of n */ int i; for (i = 0; i < BN_ARRAY_SIZE; ++i) { diff --git a/miasm/jitter/codegen.py b/miasm/jitter/codegen.py index 0f40fb6e..792feae0 100644 --- a/miasm/jitter/codegen.py +++ b/miasm/jitter/codegen.py @@ -97,7 +97,7 @@ class CGen(object): CODE_INIT = r""" int DST_case; uint64_t DST_value; - vm_cpu_t* mycpu = (vm_cpu_t*)jitcpu->cpu; + struct vm_cpu *mycpu = jitcpu->cpu; goto %s; """ diff --git a/miasm/jitter/compat_py23.h b/miasm/jitter/compat_py23.h index bc66d80b..936c08f3 100644 --- a/miasm/jitter/compat_py23.h +++ b/miasm/jitter/compat_py23.h @@ -4,18 +4,29 @@ #if PY_MAJOR_VERSION >= 3 -#define PyGetInt(item, value) \ - if (PyLong_Check(item)){ \ - value = (uint64_t)PyLong_AsUnsignedLongLong(item); \ +#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) { \ + RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \ + } \ + value = (size_type) tmp; \ } \ else{ \ - RAISE(PyExc_TypeError,"arg must be int"); \ + RAISE(PyExc_TypeError, "Arg must be int"); \ } -#define PyGetInt_retneg(item, value) \ - if (PyLong_Check(item)){ \ - value = (uint64_t)PyLong_AsUnsignedLongLong(item); \ +#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) { \ + PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \ + return -1; \ + } \ + value = (size_type) tmp; \ } \ else{ \ PyErr_SetString(PyExc_TypeError, "Arg must be int"); \ @@ -30,24 +41,46 @@ #else -#define PyGetInt(item, value) \ - if (PyInt_Check(item)){ \ - value = (uint64_t)PyInt_AsLong(item); \ +#define PyGetInt_uint_t(size_type, item, value) \ + if (PyInt_Check(item)) { \ + long tmp; \ + tmp = PyInt_AsLong(item); \ + if ( tmp > (size_type) -1) { \ + RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \ + } \ + value = (size_type) tmp; \ } \ else if (PyLong_Check(item)){ \ - value = (uint64_t)PyLong_AsUnsignedLongLong(item); \ + unsigned long long tmp; \ + tmp = PyLong_AsUnsignedLongLong(item); \ + if ( tmp > (size_type) -1) { \ + RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \ + } \ + value = (size_type) tmp; \ } \ else{ \ - RAISE(PyExc_TypeError,"arg must be int"); \ + RAISE(PyExc_TypeError, "Arg must be int"); \ } -#define PyGetInt_retneg(item, value) \ - if (PyInt_Check(item)){ \ - value = (uint64_t)PyLong_AsLong(item); \ +#define PyGetInt_uint_t_retneg(size_type, item, value) \ + if (PyInt_Check(item)) { \ + long tmp; \ + tmp = PyLong_AsLong(item); \ + 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)){ \ - value = (uint64_t)PyLong_AsUnsignedLongLong(item); \ + unsigned long long tmp; \ + tmp = PyLong_AsUnsignedLongLong(item); \ + if ( tmp > (size_type) -1) { \ + PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \ + return -1; \ + } \ + value = (size_type) tmp; \ } \ else{ \ PyErr_SetString(PyExc_TypeError, "Arg must be int"); \ @@ -64,6 +97,20 @@ +#define PyGetInt_size_t(item, value) PyGetInt_uint_t(size_t, item, value) + +#define PyGetInt_uint8_t(item, value) PyGetInt_uint_t(uint8_t, item, value) +#define PyGetInt_uint16_t(item, value) PyGetInt_uint_t(uint16_t, item, value) +#define PyGetInt_uint32_t(item, value) PyGetInt_uint_t(uint32_t, item, value) +#define PyGetInt_uint64_t(item, value) PyGetInt_uint_t(uint64_t, item, value) + +#define PyGetInt_uint8_t_retneg(item, value) PyGetInt_uint_t_retneg(uint8_t, item, value) +#define PyGetInt_uint16_t_retneg(item, value) PyGetInt_uint_t_retneg(uint16_t, item, value) +#define PyGetInt_uint32_t_retneg(item, value) PyGetInt_uint_t_retneg(uint32_t, item, value) +#define PyGetInt_uint64_t_retneg(item, value) PyGetInt_uint_t_retneg(uint64_t, item, value) + + + #if PY_MAJOR_VERSION >= 3 #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void) @@ -72,12 +119,16 @@ static struct PyModuleDef moduledef = { \ PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \ ob = PyModule_Create(&moduledef); +#define RET_MODULE return module + #else #define MOD_INIT(name) PyMODINIT_FUNC init##name(void) #define MOD_DEF(ob, name, doc, methods) \ ob = Py_InitModule3(name, methods, doc); + +#define RET_MODULE return #endif diff --git a/miasm/jitter/op_semantics.c b/miasm/jitter/op_semantics.c index 46e6cca1..79dcdcf4 100644 --- a/miasm/jitter/op_semantics.c +++ b/miasm/jitter/op_semantics.c @@ -403,92 +403,136 @@ void dump_float(void) */ } +typedef union { + uint32_t u32; + float flt; +} float_uint32_t; + + +typedef union { + uint64_t u64; + double dbl; +} double_uint64_t; + + uint32_t fpu_fadd32(uint32_t a, uint32_t b) { - float c; - c = *((float*)&a) + *((float*)&b); + float_uint32_t a_cast, b_cast, c_cast; + + a_cast.u32 = a; + b_cast.u32 = b; + + c_cast.flt = a_cast.flt + b_cast.flt; #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e + %e -> %e\n", a, b, c); + printf("%e + %e -> %e\n", a, b, c_cast.flt); #endif - return *((uint32_t*)&c); + return c_cast.u32; } uint64_t fpu_fadd64(uint64_t a, uint64_t b) { - double c; - c = *((double*)&a) + *((double*)&b); + double_uint64_t a_cast, b_cast, c_cast; + + a_cast.u64 = a; + b_cast.u64 = b; + + c_cast.dbl = a_cast.dbl + b_cast.dbl; #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e + %e -> %e\n", a, b, c); + printf("%e + %e -> %e\n", a, b, c_cast.dbl); #endif - return *((uint64_t*)&c); + return c_cast.u64; } uint32_t fpu_fsub32(uint32_t a, uint32_t b) { - float c; - c = *((float*)&a) - *((float*)&b); + float_uint32_t a_cast, b_cast, c_cast; + + a_cast.u32 = a; + b_cast.u32 = b; + + c_cast.flt = a_cast.flt - b_cast.flt; #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e + %e -> %e\n", a, b, c); + printf("%e + %e -> %e\n", a, b, c_cast.flt); #endif - return *((uint32_t*)&c); + return c_cast.u32; } uint64_t fpu_fsub64(uint64_t a, uint64_t b) { - double c; - c = *((double*)&a) - *((double*)&b); + double_uint64_t a_cast, b_cast, c_cast; + + a_cast.u64 = a; + b_cast.u64 = b; + + c_cast.dbl = a_cast.dbl - b_cast.dbl; #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e + %e -> %e\n", a, b, c); + printf("%e + %e -> %e\n", a, b, c_cast.dbl); #endif - return *((uint64_t*)&c); + return c_cast.u64; } uint32_t fpu_fmul32(uint32_t a, uint32_t b) { - float c; - c = *((float*)&a) * *((float*)&b); + float_uint32_t a_cast, b_cast, c_cast; + + a_cast.u32 = a; + b_cast.u32 = b; + + c_cast.flt = a_cast.flt * b_cast.flt; #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e * %e -> %e\n", a, b, c); + printf("%e * %e -> %e\n", a, b, c_cast.flt); #endif - return *((uint32_t*)&c); + return c_cast.u32; } uint64_t fpu_fmul64(uint64_t a, uint64_t b) { - double c; - c = *((double*)&a) * *((double*)&b); + double_uint64_t a_cast, b_cast, c_cast; + + a_cast.u64 = a; + b_cast.u64 = b; + + c_cast.dbl = a_cast.dbl * b_cast.dbl; #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e * %e -> %e\n", a, b, c); + printf("%e * %e -> %e\n", a, b, c_cast.dbl); #endif - return *((uint64_t*)&c); + return c_cast.u64; } uint32_t fpu_fdiv32(uint32_t a, uint32_t b) { - float c; - c = *((float*)&a) / *((float*)&b); + float_uint32_t a_cast, b_cast, c_cast; + + a_cast.u32 = a; + b_cast.u32 = b; + + c_cast.flt = a_cast.flt / b_cast.flt; #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e * %e -> %e\n", a, b, c); + printf("%e * %e -> %e\n", a, b, c_cast.flt); #endif - return *((uint32_t*)&c); + return c_cast.u32; } uint64_t fpu_fdiv64(uint64_t a, uint64_t b) { - double c; - c = *((double*)&a) / *((double*)&b); + double_uint64_t a_cast, b_cast, c_cast; + + a_cast.u64 = a; + b_cast.u64 = b; + + c_cast.dbl = a_cast.dbl / b_cast.dbl; #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e * %e -> %e\n", a, b, c); + printf("%e * %e -> %e\n", a, b, c_cast.dbl); #endif - return *((uint64_t*)&c); + return c_cast.u64; } double fpu_ftan(double a) @@ -562,46 +606,55 @@ double fpu_f2xm1(double a) uint32_t fpu_fsqrt32(uint32_t a) { - float b; - b = sqrtf(*((float*)&a)); + float_uint32_t a_cast; + a_cast.u32 = a; + a_cast.flt = sqrtf(a_cast.flt); #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e sqrt %e\n", a, b); + printf("%e sqrt %e\n", a, a_cast.flt); #endif - return *((uint32_t*)&b); + return a_cast.u32; } uint64_t fpu_fsqrt64(uint64_t a) { - double b; - b = sqrt(*((double*)&a)); + double_uint64_t a_cast; + + a_cast.u64 = a; + a_cast.dbl = sqrt(a_cast.dbl); #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e sqrt %e\n", a, b); + printf("%e sqrt %e\n", a, a_cast.dbl); #endif - return *((uint64_t*)&b); + return a_cast.u64; } uint64_t fpu_fabs64(uint64_t a) { - double b; - b = abs(*((double*)&a)); + double_uint64_t a_cast; + + a_cast.u64 = a; + a_cast.dbl = fabs(a_cast.dbl); #ifdef DEBUG_MIASM_DOUBLE dump_float(); - printf("%e abs %e\n", a, b); + printf("%e abs %e\n", a, a_cast.dbl); #endif - return *((uint64_t*)&b); + return a_cast.u64; } uint64_t fpu_fprem64(uint64_t a, uint64_t b) { - double c; - c = fmod(*((double*)&a), *((double*)&b)); + double_uint64_t a_cast, b_cast, c_cast; + + a_cast.u64 = a; + b_cast.u64 = b; + + c_cast.dbl = fmod(a_cast.dbl, b_cast.dbl); #ifdef DEBUG_MIASM_DOUBLE dump_float(); printf("%e %% %e -> %e\n", a, b, c); #endif - return *((uint64_t*)&c); + return c_cast.u64; } double fpu_fchs(double a) @@ -667,64 +720,80 @@ unsigned int fpu_fcom_c3(double a, double b) uint64_t sint_to_fp_64(int64_t a) { - double result = (double) a; - return *((uint64_t*)&result); + double_uint64_t a_cast; + a_cast.dbl = (double) a; + return a_cast.u64; } uint32_t sint_to_fp_32(int32_t a) { - float result = (float) a; - return *((uint32_t*)&result); + float_uint32_t a_cast; + a_cast.flt = (float) a; + return a_cast.u32; } int32_t fp32_to_sint32(uint32_t a) { // Enforce nearbyint (IEEE-754 behavior) - float rounded = *((float*)&a); - rounded = nearbyintf(rounded); + float rounded; + float_uint32_t a_cast; + a_cast.u32 = a; + rounded = nearbyintf(a_cast.flt); return (int32_t) rounded; } int64_t fp64_to_sint64(uint64_t a) { // Enforce nearbyint (IEEE-754 behavior) - double rounded = *((double*)&a); - rounded = nearbyint(rounded); + double rounded; + double_uint64_t a_cast; + a_cast.u64 = a; + rounded = nearbyint(a_cast.dbl); return (int64_t) rounded; } int32_t fp64_to_sint32(uint64_t a) { // Enforce nearbyint (IEEE-754 behavior) - double rounded = *((double*)&a); - rounded = nearbyint(rounded); + double rounded; + double_uint64_t a_cast; + a_cast.u64 = a; + rounded = nearbyint(a_cast.dbl); return (int32_t) rounded; } uint32_t fp64_to_fp32(uint64_t a) { - float result = (float) *((double*)&a); - return *((uint32_t*)&result); + float_uint32_t a_cast32; + double_uint64_t a_cast64; + a_cast64.u64 = a; + a_cast32.flt = (float)a_cast64.dbl; + return a_cast32.u32; } uint64_t fp32_to_fp64(uint32_t a) { - double result = (double) *((float*)&a); - return *((uint64_t*)&result); + float_uint32_t a_cast32; + double_uint64_t a_cast64; + a_cast32.u32 = a; + a_cast64.dbl = (double)a_cast32.flt; + return a_cast64.u64; } uint32_t fpround_towardszero_fp32(uint32_t a) { - float rounded = *((float*)&a); - rounded = truncf(rounded); - return *((uint32_t*)&rounded); + float_uint32_t a_cast; + a_cast.u32 = a; + a_cast.flt = truncf(a_cast.flt); + return a_cast.u32; } uint64_t fpround_towardszero_fp64(uint64_t a) { - double rounded = *((float*)&a); - rounded = trunc(rounded); - return *((uint64_t*)&rounded); + double_uint64_t a_cast; + a_cast.u64 = a; + a_cast.dbl = trunc(a_cast.dbl); + return a_cast.u64; } diff --git a/miasm/jitter/vm_mngr.c b/miasm/jitter/vm_mngr.c index bd1de2f4..43d6db53 100644 --- a/miasm/jitter/vm_mngr.c +++ b/miasm/jitter/vm_mngr.c @@ -21,6 +21,7 @@ #include <stdio.h> #include <stdlib.h> +#include <stdint.h> #include "queue.h" @@ -58,8 +59,13 @@ void memory_access_list_add(struct memory_access_list * access, uint64_t start, if (access->num >= access->allocated) { if (access->allocated == 0) access->allocated = 1; - else + else { + if (access->allocated >= SIZE_MAX / 2) { + fprintf(stderr, "Cannot alloc more pages\n"); + exit(EXIT_FAILURE); + } access->allocated *= 2; + } access->array = realloc(access->array, access->allocated * sizeof(struct memory_access)); if (access->array == NULL) { fprintf(stderr, "cannot realloc struct memory_access access->array\n"); @@ -370,7 +376,7 @@ void add_mem_write(vm_mngr_t* vm_mngr, uint64_t addr, uint64_t size) void check_invalid_code_blocs(vm_mngr_t* vm_mngr) { - int i; + size_t i; struct code_bloc_node * cbp; for (i=0;i<vm_mngr->memory_w.num; i++) { if (vm_mngr->exception_flags & EXCEPT_CODE_AUTOMOD) @@ -399,7 +405,7 @@ void check_invalid_code_blocs(vm_mngr_t* vm_mngr) void check_memory_breakpoint(vm_mngr_t* vm_mngr) { - int i; + size_t i; struct memory_breakpoint_info * memory_bp; /* Check memory breakpoints */ @@ -430,9 +436,10 @@ void check_memory_breakpoint(vm_mngr_t* vm_mngr) PyObject* get_memory_pylist(vm_mngr_t* vm_mngr, struct memory_access_list* memory_list) { - int i; + size_t i; PyObject *pylist; PyObject *range; + pylist = PyList_New(memory_list->num); for (i=0;i<memory_list->num;i++) { range = PyTuple_New(2); @@ -506,10 +513,12 @@ uint64_t vm_MEM_LOOKUP_64(vm_mngr_t* vm_mngr, uint64_t addr) } -int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, uint64_t size) +int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, size_t size) { char* buffer; - uint64_t len; + size_t len; + uint64_t addr_diff; + size_t addr_diff_st; struct memory_page_node * mpn; buffer = malloc(size); @@ -528,8 +537,14 @@ int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, uint64_t s return -1; } - len = MIN(size, mpn->size - (addr - mpn->ad)); - memcpy(buffer, (char*)mpn->ad_hp + (addr - mpn->ad), len); + addr_diff = addr - mpn->ad; + if (addr_diff > SIZE_MAX) { + fprintf(stderr, "Size too big\n"); + exit(EXIT_FAILURE); + } + addr_diff_st = (size_t) addr_diff; + len = MIN(size, mpn->size - addr_diff_st); + memcpy(buffer, (char*)mpn->ad_hp + (addr_diff_st), len); buffer += len; addr += len; size -= len; @@ -538,11 +553,18 @@ int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, uint64_t s return 0; } -int vm_write_mem(vm_mngr_t* vm_mngr, uint64_t addr, char *buffer, uint64_t size) +int vm_write_mem(vm_mngr_t* vm_mngr, uint64_t addr, char *buffer, size_t size) { - uint64_t len; + size_t len; + uint64_t addr_diff; + size_t addr_diff_st; struct memory_page_node * mpn; + if (size > SIZE_MAX) { + fprintf(stderr, "Write size wider than supported system\n"); + exit(EXIT_FAILURE); + } + /* write is multiple page wide */ while (size){ mpn = get_memory_page_from_address(vm_mngr, addr, 1); @@ -551,8 +573,14 @@ int vm_write_mem(vm_mngr_t* vm_mngr, uint64_t addr, char *buffer, uint64_t size) return -1; } - len = MIN(size, mpn->size - (addr - mpn->ad)); - memcpy((char*)mpn->ad_hp + (addr-mpn->ad), buffer, len); + addr_diff = addr - mpn->ad; + if (addr_diff > SIZE_MAX) { + fprintf(stderr, "Size too big\n"); + exit(EXIT_FAILURE); + } + addr_diff_st = (size_t) addr_diff; + len = MIN(size, mpn->size - addr_diff_st); + memcpy((char*)mpn->ad_hp + addr_diff_st, buffer, len); buffer += len; addr += len; size -= len; @@ -563,18 +591,31 @@ int vm_write_mem(vm_mngr_t* vm_mngr, uint64_t addr, char *buffer, uint64_t size) -int is_mapped(vm_mngr_t* vm_mngr, uint64_t addr, uint64_t size) +int is_mapped(vm_mngr_t* vm_mngr, uint64_t addr, size_t size) { - uint64_t len; + size_t len; + uint64_t addr_diff; + size_t addr_diff_st; struct memory_page_node * mpn; + if (size > SIZE_MAX) { + fprintf(stderr, "Test size wider than supported system\n"); + exit(EXIT_FAILURE); + } + /* test multiple page wide */ while (size){ mpn = get_memory_page_from_address(vm_mngr, addr, 0); if (!mpn) return 0; - len = MIN(size, mpn->size - (addr - mpn->ad)); + addr_diff = addr - mpn->ad; + if (addr_diff > SIZE_MAX) { + fprintf(stderr, "Size too big\n"); + exit(EXIT_FAILURE); + } + addr_diff_st = (size_t) addr_diff; + len = MIN(size, mpn->size - addr_diff_st); addr += len; size -= len; } @@ -582,7 +623,7 @@ int is_mapped(vm_mngr_t* vm_mngr, uint64_t addr, uint64_t size) return 1; } -struct memory_page_node * create_memory_page_node(uint64_t ad, unsigned int size, unsigned int access, char* name) +struct memory_page_node * create_memory_page_node(uint64_t ad, unsigned int size, unsigned int access, const char *name) { struct memory_page_node * mpn; void* ad_hp; diff --git a/miasm/jitter/vm_mngr.h b/miasm/jitter/vm_mngr.h index 660e6998..913d06f8 100644 --- a/miasm/jitter/vm_mngr.h +++ b/miasm/jitter/vm_mngr.h @@ -85,7 +85,7 @@ LIST_HEAD(memory_breakpoint_info_head, memory_breakpoint_info); struct memory_page_node { uint64_t ad; - uint64_t size; + size_t size; uint64_t access; void* ad_hp; char* name; @@ -98,8 +98,8 @@ struct memory_access { struct memory_access_list { struct memory_access *array; - uint64_t allocated; - uint64_t num; + size_t allocated; + size_t num; }; typedef struct { @@ -185,7 +185,7 @@ int is_mem_mapped(vm_mngr_t* vm_mngr, uint64_t ad); uint64_t get_mem_base_addr(vm_mngr_t* vm_mngr, uint64_t addr, uint64_t *addr_base); unsigned int MEM_LOOKUP(vm_mngr_t* vm_mngr, unsigned int my_size, uint64_t addr); -int is_mapped(vm_mngr_t* vm_mngr, uint64_t addr, uint64_t size); +int is_mapped(vm_mngr_t* vm_mngr, uint64_t addr, size_t size); void vm_throw(vm_mngr_t* vm_mngr, unsigned long flags); void vm_MEM_WRITE_08(vm_mngr_t* vm_mngr, uint64_t addr, unsigned char src); @@ -207,8 +207,8 @@ unsigned short MEM_LOOKUP_16_PASSTHROUGH(uint64_t addr); unsigned int MEM_LOOKUP_32_PASSTHROUGH(uint64_t addr); uint64_t MEM_LOOKUP_64_PASSTHROUGH(uint64_t addr); -int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, uint64_t size); -int vm_write_mem(vm_mngr_t* vm_mngr, uint64_t addr, char *buffer, uint64_t size); +int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, size_t size); +int vm_write_mem(vm_mngr_t* vm_mngr, uint64_t addr, char *buffer, size_t size); void memory_access_list_init(struct memory_access_list * access); void memory_access_list_reset(struct memory_access_list * access); @@ -224,7 +224,7 @@ void hexdump(char* m, unsigned int l); struct code_bloc_node * create_code_bloc_node(uint64_t ad_start, uint64_t ad_stop); void add_code_bloc(vm_mngr_t* vm_mngr, struct code_bloc_node* cbp); -struct memory_page_node * create_memory_page_node(uint64_t ad, unsigned int size, unsigned int access, char* name);//memory_page* mp); +struct memory_page_node * create_memory_page_node(uint64_t ad, unsigned int size, unsigned int access, const char *name);//memory_page* mp); void init_memory_page_pool(vm_mngr_t* vm_mngr); void init_code_bloc_pool(vm_mngr_t* vm_mngr); void reset_memory_page_pool(vm_mngr_t* vm_mngr); diff --git a/miasm/jitter/vm_mngr_py.c b/miasm/jitter/vm_mngr_py.c index 1173146b..9ec87b0d 100644 --- a/miasm/jitter/vm_mngr_py.c +++ b/miasm/jitter/vm_mngr_py.c @@ -79,19 +79,20 @@ PyObject* vm_add_memory_page(VmMngr* self, PyObject* args) PyObject *item_str; PyObject *name=NULL; uint64_t buf_size; + size_t buf_size_st; char* buf_data; Py_ssize_t length; uint64_t page_addr; uint64_t page_access; - char* name_ptr; + const char *name_ptr; struct memory_page_node * mpn; if (!PyArg_ParseTuple(args, "OOO|O", &addr, &access, &item_str, &name)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(addr, page_addr); - PyGetInt(access, page_access); + PyGetInt_uint64_t(addr, page_addr); + PyGetInt_uint64_t(access, page_access); if(!PyBytes_Check(item_str)) RAISE(PyExc_TypeError,"arg must be bytes"); @@ -113,7 +114,13 @@ PyObject* vm_add_memory_page(VmMngr* self, PyObject* args) RAISE(PyExc_TypeError,"known page in memory"); } - memcpy(mpn->ad_hp, buf_data, buf_size); + if (buf_size > SIZE_MAX) { + fprintf(stderr, "Size too big\n"); + exit(EXIT_FAILURE); + } + buf_size_st = (size_t) buf_size; + + memcpy(mpn->ad_hp, buf_data, buf_size_st); add_memory_page(&self->vm_mngr, mpn); Py_INCREF(Py_None); @@ -133,8 +140,8 @@ PyObject* vm_set_mem_access(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "OO", &addr, &access)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(addr, page_addr); - PyGetInt(access, page_access); + PyGetInt_uint64_t(addr, page_addr); + PyGetInt_uint64_t(access, page_access); mpn = get_memory_page_from_address(&self->vm_mngr, page_addr, 1); if (!mpn){ @@ -155,26 +162,29 @@ PyObject* vm_set_mem(VmMngr* self, PyObject* args) Py_ssize_t py_length; char * buffer; - uint64_t size; + 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(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); if (!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); - size = PyBytes_Size(py_buffer); + pysize = PyBytes_Size(py_buffer); + if (pysize < 0) { + RAISE(PyExc_TypeError,"Python error"); + } PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length); - ret = vm_write_mem(&self->vm_mngr, addr, buffer, size); + ret = vm_write_mem(&self->vm_mngr, addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError, "Error in set_mem"); - add_mem_write(&self->vm_mngr, addr, size); + add_mem_write(&self->vm_mngr, addr, (size_t)pysize); check_invalid_code_blocs(&self->vm_mngr); Py_INCREF(Py_None); @@ -192,7 +202,7 @@ PyObject* vm_get_mem_access(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "O", &py_addr)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, page_addr); + PyGetInt_uint64_t(py_addr, page_addr); mpn = get_memory_page_from_address(&self->vm_mngr, page_addr, 1); if (!mpn){ @@ -210,6 +220,7 @@ PyObject* vm_get_mem(VmMngr* self, PyObject* args) uint64_t addr; uint64_t size; + size_t size_st; PyObject *obj_out; char * buf_out; int ret; @@ -217,15 +228,21 @@ PyObject* vm_get_mem(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_len)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); - PyGetInt(py_len, size); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_len, size); + + if (size > SIZE_MAX) { + fprintf(stderr, "Size too big\n"); + exit(EXIT_FAILURE); + } + size_st = (size_t) size; - ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, size); + ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, size_st); if (ret < 0) { RAISE(PyExc_RuntimeError,"Cannot find address"); } - obj_out = PyBytes_FromStringAndSize(buf_out, size); + obj_out = PyBytes_FromStringAndSize(buf_out, size_st); free(buf_out); return obj_out; } @@ -243,7 +260,7 @@ PyObject* vm_get_u8(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "O", &py_addr)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 1); if (ret < 0) { @@ -270,7 +287,7 @@ PyObject* vm_get_u16(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "O", &py_addr)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 2); if (ret < 0) { @@ -297,7 +314,7 @@ PyObject* vm_get_u32(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "O", &py_addr)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 4); if (ret < 0) { @@ -325,7 +342,7 @@ PyObject* vm_get_u64(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "O", &py_addr)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 8); if (ret < 0) { @@ -344,24 +361,17 @@ PyObject* vm_set_u8(VmMngr* self, PyObject* args) { PyObject *py_addr; PyObject *py_val; - uint64_t value; + uint8_t value; uint64_t addr; - uint8_t final_value; int ret; if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); - PyGetInt(py_val, value); - - if (value > 0xFF) { - fprintf(stderr, "Warning: int to big\n"); - } + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint8_t(py_val, value); - final_value = value; - - ret = vm_write_mem(&self->vm_mngr, addr, (char*)&final_value, 1); + ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 1); if (ret < 0) RAISE(PyExc_TypeError, "Error in set_mem"); @@ -376,24 +386,19 @@ PyObject* vm_set_u16(VmMngr* self, PyObject* args) { PyObject *py_addr; PyObject *py_val; - uint64_t value; + uint16_t value; + uint64_t addr; - uint16_t final_value; int ret; if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); - PyGetInt(py_val, value); - - if (value > 0xFFFF) { - fprintf(stderr, "Warning: int to big\n"); - } - - final_value = set_endian16(&self->vm_mngr, value); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint16_t(py_val, value); - ret = vm_write_mem(&self->vm_mngr, addr, (char*)&final_value, 2); + value = set_endian16(&self->vm_mngr, value); + ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 2); if (ret < 0) RAISE(PyExc_TypeError, "Error in set_mem"); @@ -408,24 +413,19 @@ PyObject* vm_set_u32(VmMngr* self, PyObject* args) { PyObject *py_addr; PyObject *py_val; - uint64_t value; + uint32_t value; uint64_t addr; - uint32_t final_value; int ret; if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); - PyGetInt(py_val, value); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint32_t(py_val, value); - if (value > 0xFFFFFFFF) { - fprintf(stderr, "Warning: int to big\n"); - } + value = set_endian32(&self->vm_mngr, value); - final_value = set_endian32(&self->vm_mngr, value); - - ret = vm_write_mem(&self->vm_mngr, addr, (char*)&final_value, 4); + ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 4); if (ret < 0) RAISE(PyExc_TypeError, "Error in set_mem"); @@ -442,18 +442,17 @@ PyObject* vm_set_u64(VmMngr* self, PyObject* args) PyObject *py_val; uint64_t value; uint64_t addr; - uint64_t final_value; int ret; if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); - PyGetInt(py_val, value); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_val, value); - final_value = set_endian64(&self->vm_mngr, value); + value = set_endian64(&self->vm_mngr, value); - ret = vm_write_mem(&self->vm_mngr, addr, (char*)&final_value, 8); + ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 8); if (ret < 0) RAISE(PyExc_TypeError, "Error in set_mem"); @@ -481,9 +480,9 @@ PyObject* vm_add_memory_breakpoint(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "OOO", &ad, &size, &access)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(ad, b_ad); - PyGetInt(size, b_size); - PyGetInt(access, b_access); + PyGetInt_uint64_t(ad, b_ad); + PyGetInt_uint64_t(size, b_size); + PyGetInt_uint64_t(access, b_access); add_memory_breakpoint(&self->vm_mngr, b_ad, b_size, (unsigned int)b_access); @@ -509,8 +508,8 @@ PyObject* vm_remove_memory_breakpoint(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "OO", &ad, &access)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(ad, b_ad); - PyGetInt(access, b_access); + PyGetInt_uint64_t(ad, b_ad); + PyGetInt_uint64_t(access, b_access); remove_memory_breakpoint(&self->vm_mngr, b_ad, (unsigned int)b_access); Py_INCREF(Py_None); @@ -521,14 +520,14 @@ PyObject* vm_remove_memory_breakpoint(VmMngr* self, PyObject* args) PyObject* vm_set_exception(VmMngr* self, PyObject* args) { PyObject *item1; - uint64_t i; + uint64_t exception_flags; if (!PyArg_ParseTuple(args, "O", &item1)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, i); + PyGetInt_uint64_t(item1, exception_flags); - self->vm_mngr.exception_flags = i; + self->vm_mngr.exception_flags = exception_flags; Py_INCREF(Py_None); return Py_None; } @@ -589,8 +588,8 @@ PyObject* py_add_mem_read(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_size)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); - PyGetInt(py_size, size); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_size, size); add_mem_read(&self->vm_mngr, addr, size); Py_INCREF(Py_None); return Py_None; @@ -607,8 +606,8 @@ PyObject* py_add_mem_write(VmMngr* self, PyObject* args) if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_size)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(py_addr, addr); - PyGetInt(py_size, size); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_size, size); add_mem_write(&self->vm_mngr, addr, size); Py_INCREF(Py_None); return Py_None; @@ -713,8 +712,8 @@ PyObject* vm_add_code_bloc(VmMngr *self, PyObject *args) if (!PyArg_ParseTuple(args, "OO", &item1, &item2)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(item1, ad_start); - PyGetInt(item2, ad_stop); + PyGetInt_uint64_t(item1, ad_start); + PyGetInt_uint64_t(item2, ad_stop); cbp = create_code_bloc_node(ad_start, ad_stop); cbp->ad_start = ad_start; @@ -741,14 +740,14 @@ PyObject* vm_is_mapped(VmMngr* self, PyObject* args) PyObject *ad; PyObject *size; uint64_t b_ad; - uint64_t b_size; + size_t b_size; int ret; if (!PyArg_ParseTuple(args, "OO", &ad, &size)) RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt(ad, b_ad); - PyGetInt(size, b_size); + PyGetInt_uint64_t(ad, b_ad); + PyGetInt_size_t(size, b_size); ret = is_mapped(&self->vm_mngr, b_ad, b_size); return PyLong_FromUnsignedLongLong((uint64_t)ret); } @@ -995,19 +994,19 @@ char vm_mngr_mod_name[] = "VmMngr"; MOD_INIT(VmMngr) { - PyObject *module; + PyObject *module = NULL; MOD_DEF(module, "VmMngr", "vm_mngr module", VmMngr_Methods); if (module == NULL) - return NULL; + RET_MODULE; if (PyType_Ready(&VmMngrType) < 0) - return NULL; + RET_MODULE; Py_INCREF(&VmMngrType); if (PyModule_AddObject(module, "Vm", (PyObject *)&VmMngrType) < 0) - return NULL; + RET_MODULE; - return module; + RET_MODULE; } diff --git a/test/ir/ir2C.py b/test/ir/ir2C.py index c41c98c9..61c1bbfd 100755 --- a/test/ir/ir2C.py +++ b/test/ir/ir2C.py @@ -49,7 +49,7 @@ class TestIrIr2C(unittest.TestCase): self.translationTest( ExprOp('segm', *args[:2]), r'segm2addr(jitcpu, 0x0, 0x1)') self.translationTest( - ExprOp('imod', *args[:2]), r'imod32((vm_cpu_t*)jitcpu->cpu, 0x0, 0x1)') + ExprOp('imod', *args[:2]), r'imod32((struct vm_cpu*)jitcpu->cpu, 0x0, 0x1)') self.translationTest( ExprOp('bcdadd', *args[:2]), r'bcdadd_32(0x0, 0x1)') self.assertRaises(NotImplementedError, translator.from_expr, |