From 5468ac224e579ba82b28b9907f2d5ba0e3357214 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:31:33 +0100 Subject: [PATCH 1/9] Fix typo and dont enforce py2 --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 9a687160e..72b47356e 100755 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python2 +#! /usr/bin/env python from __future__ import print_function from distutils.core import setup, Extension @@ -11,7 +11,7 @@ is_win = platform.system() == "Windows" -def buil_all(): +def build_all(): packages=[ "miasm", "miasm/arch", @@ -209,5 +209,5 @@ def buil_all(): print("Copying", lib, "to", dst) copy2(lib, dst) -buil_all() +build_all() From 0a03419ac877dfe31ed15bacce8e2c868a0b5042 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:31:55 +0100 Subject: [PATCH 2/9] Add missing Copyright statements --- miasm/jitter/JitCore.c | 2 ++ miasm/jitter/Jitgcc.c | 2 ++ miasm/jitter/Jitllvm.c | 4 ++-- miasm/jitter/bn.c | 2 ++ miasm/jitter/op_semantics.c | 2 ++ miasm/jitter/vm_mngr.c | 2 +- miasm/jitter/vm_mngr_py.c | 4 ++-- 7 files changed, 13 insertions(+), 5 deletions(-) diff --git a/miasm/jitter/JitCore.c b/miasm/jitter/JitCore.c index 37b69aa36..a213f1717 100644 --- a/miasm/jitter/JitCore.c +++ b/miasm/jitter/JitCore.c @@ -1,3 +1,5 @@ +/* Copyright (C) 2011-2019 EADS France, Fabrice Desclaux */ + #include #include "structmember.h" #include diff --git a/miasm/jitter/Jitgcc.c b/miasm/jitter/Jitgcc.c index 9dc7b2fd8..e03295b0d 100644 --- a/miasm/jitter/Jitgcc.c +++ b/miasm/jitter/Jitgcc.c @@ -1,3 +1,5 @@ +/* Copyright (C) 2011-2019 EADS France, Fabrice Desclaux */ + #include #include #include diff --git a/miasm/jitter/Jitllvm.c b/miasm/jitter/Jitllvm.c index 7617954be..fb68610f7 100644 --- a/miasm/jitter/Jitllvm.c +++ b/miasm/jitter/Jitllvm.c @@ -1,7 +1,7 @@ -#include +/* Copyright (C) 2011-2019 EADS France, Fabrice Desclaux */ +#include #include - #include #include "compat_py23.h" #include "queue.h" diff --git a/miasm/jitter/bn.c b/miasm/jitter/bn.c index 43e552a41..c1cbd83bc 100644 --- a/miasm/jitter/bn.c +++ b/miasm/jitter/bn.c @@ -1,3 +1,5 @@ +/* Copyright (C) 2011-2019 EADS France, Fabrice Desclaux */ + /* Big number library - arithmetic on multiple-precision unsigned integers. diff --git a/miasm/jitter/op_semantics.c b/miasm/jitter/op_semantics.c index 79dcdcf43..8682d27a6 100644 --- a/miasm/jitter/op_semantics.c +++ b/miasm/jitter/op_semantics.c @@ -1,3 +1,5 @@ +/* Copyright (C) 2011-2019 EADS France, Fabrice Desclaux */ + #include #include #include diff --git a/miasm/jitter/vm_mngr.c b/miasm/jitter/vm_mngr.c index 43d6db53d..0b8881300 100644 --- a/miasm/jitter/vm_mngr.c +++ b/miasm/jitter/vm_mngr.c @@ -1,5 +1,5 @@ /* -** Copyright (C) 2011 EADS France, Fabrice Desclaux +** Copyright (C) 2011-2019 EADS France, Fabrice Desclaux ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by diff --git a/miasm/jitter/vm_mngr_py.c b/miasm/jitter/vm_mngr_py.c index 9ec87b0d5..12b35cf79 100644 --- a/miasm/jitter/vm_mngr_py.c +++ b/miasm/jitter/vm_mngr_py.c @@ -1,5 +1,5 @@ /* -** Copyright (C) 2011 EADS France, Fabrice Desclaux +** Copyright (C) 2011-2019 EADS France, Fabrice Desclaux ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by @@ -8,7 +8,7 @@ ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License along From 9c82bf58bafdd0cd3409fc7c25f65fc52473a46f Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:34:31 +0100 Subject: [PATCH 3/9] Use the boolean type when needed --- miasm/jitter/Jitgcc.c | 22 ++++++++--------- miasm/jitter/Jitllvm.c | 32 +++++++++++++----------- miasm/jitter/bn.c | 56 ++++++++++-------------------------------- miasm/jitter/bn.h | 13 +++++----- 4 files changed, 47 insertions(+), 76 deletions(-) diff --git a/miasm/jitter/Jitgcc.c b/miasm/jitter/Jitgcc.c index e03295b0d..08708a7df 100644 --- a/miasm/jitter/Jitgcc.c +++ b/miasm/jitter/Jitgcc.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "compat_py23.h" typedef struct { @@ -25,7 +26,7 @@ PyObject* gcc_exec_block(PyObject* self, PyObject* args) block_id BlockDst; uint64_t max_exec_per_call = 0; uint64_t cpt; - int do_cpt; + bool do_cpt; if (!PyArg_ParseTuple(args, "OOOO|K", @@ -37,35 +38,32 @@ PyObject* gcc_exec_block(PyObject* self, PyObject* args) Py_INCREF(retaddr); if (max_exec_per_call == 0) { - do_cpt = 0; + do_cpt = false; cpt = 1; } else { - do_cpt = 1; + do_cpt = true; cpt = max_exec_per_call; } - - - for (;;) { - if (cpt == 0) - return retaddr; - if (do_cpt) + while (cpt) { + if (do_cpt) { cpt --; + } // Init BlockDst.is_local = 0; BlockDst.address = 0; // Get the expected jitted function address func_py = PyDict_GetItem(lbl2ptr, retaddr); - if (func_py) + if (func_py) { func = (jitted_func) PyLong_AsVoidPtr((PyObject*) func_py); - else { + } else { if (BlockDst.is_local == 1) { fprintf(stderr, "return on local label!\n"); exit(EXIT_FAILURE); } // retaddr is not jitted yet - return retaddr; + break; } // Execute it status = func(&BlockDst, jitcpu); diff --git a/miasm/jitter/Jitllvm.c b/miasm/jitter/Jitllvm.c index fb68610f7..7262c005c 100644 --- a/miasm/jitter/Jitllvm.c +++ b/miasm/jitter/Jitllvm.c @@ -26,7 +26,7 @@ PyObject* llvm_exec_block(PyObject* self, PyObject* args) PyObject* retaddr = NULL; uint64_t max_exec_per_call = 0; uint64_t cpt; - int do_cpt; + bool do_cpt; if (!PyArg_ParseTuple(args, "OOOO|K", &retaddr, &jitcpu, &lbl2ptr, &stop_offsets, @@ -39,27 +39,25 @@ PyObject* llvm_exec_block(PyObject* self, PyObject* args) Py_INCREF(retaddr); if (max_exec_per_call == 0) { - do_cpt = 0; + do_cpt = false; cpt = 1; } else { - do_cpt = 1; + do_cpt = true; cpt = max_exec_per_call; } - for (;;) { - // Handle cpt - if (cpt == 0) - return retaddr; - if (do_cpt) + while (cpt) { + if (do_cpt) { cpt --; - + } // Get the expected jitted function address func_py = PyDict_GetItem(lbl2ptr, retaddr); + if (!func_py) { + break; + } if (func_py) func = PyLong_AsVoidPtr((PyObject*) func_py); else - // retaddr is not jitted yet - return retaddr; // Execute it ret = func((void*) jitcpu, (void*)(intptr_t) cpu, (void*)(intptr_t) vm, &status); @@ -67,13 +65,17 @@ PyObject* llvm_exec_block(PyObject* self, PyObject* args) retaddr = PyLong_FromUnsignedLongLong(ret); // Check exception - if (status) - return retaddr; + if (status) { + break; + } // Check stop offsets - if (PySet_Contains(stop_offsets, retaddr)) - return retaddr; + if (PySet_Contains(stop_offsets, retaddr)) { + break; + } } + // retaddr is not jitted yet + return retaddr; } diff --git a/miasm/jitter/bn.c b/miasm/jitter/bn.c index c1cbd83bc..daf2fdeb3 100644 --- a/miasm/jitter/bn.c +++ b/miasm/jitter/bn.c @@ -599,90 +599,60 @@ int bignum_cmp_signed(bn_t a, bn_t b) return EQUAL; } - /* Unsigned compare bn */ int bignum_cmp_unsigned(bn_t a, bn_t b) { return bignum_cmp(a, b); } - /* Return 1 if a == b else 0 */ -int bignum_is_equal(bn_t a, bn_t b) +bool bignum_is_equal(bn_t a, bn_t b) { - int ret; - ret = bignum_cmp_unsigned(a, b); - if (ret == EQUAL) - return 1; - else - return 0; + return bignum_cmp_unsigned(a, b) == EQUAL; } - /* Return 1 if a +#include #include @@ -136,15 +137,15 @@ _MIASM_EXPORT bn_t bignum_not(bn_t a); /* c = ~a */ /* Special operators and comparison */ _MIASM_EXPORT int bignum_cmp(bn_t a, bn_t b); /* Compare: returns LARGER, EQUAL or SMALLER */ -_MIASM_EXPORT int bignum_is_equal(bn_t a, bn_t b); /* Return 1 if a == b else 0 */ -_MIASM_EXPORT int bignum_is_inf_unsigned(bn_t a, bn_t b); /* Return 1 if a 1024 */ From 287e219909ed496e343d4796691d048f3d49aa5f Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:34:52 +0100 Subject: [PATCH 4/9] Remove unnecessary empty lines --- miasm/jitter/JitCore.c | 7 ---- miasm/jitter/Jitgcc.c | 15 ++++---- miasm/jitter/Jitllvm.c | 2 -- miasm/jitter/arch/JitCore_mips32.c | 29 ++++----------- miasm/jitter/arch/JitCore_x86.c | 12 ------- miasm/jitter/bn.c | 57 ------------------------------ miasm/jitter/compat_py23.h | 8 ----- miasm/jitter/vm_mngr.c | 22 ++---------- miasm/jitter/vm_mngr.h | 14 -------- miasm/jitter/vm_mngr_py.c | 35 ++++++------------ 10 files changed, 25 insertions(+), 176 deletions(-) diff --git a/miasm/jitter/JitCore.c b/miasm/jitter/JitCore.c index a213f1717..9ee8ae760 100644 --- a/miasm/jitter/JitCore.c +++ b/miasm/jitter/JitCore.c @@ -98,7 +98,6 @@ bn_t MEM_LOOKUP_BN_BN(JitCpu* jitcpu, int size, bn_t addr) return val; } - uint64_t MEM_LOOKUP_BN_INT(JitCpu* jitcpu, int size, bn_t addr) { uint64_t ptr; @@ -145,7 +144,6 @@ bn_t MEM_LOOKUP_INT_BN(JitCpu* jitcpu, int size, uint64_t addr) return val; } - void MEM_LOOKUP_INT_BN_TO_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr) { bn_t ret; @@ -159,7 +157,6 @@ void MEM_LOOKUP_INT_BN_TO_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr memcpy(ptr, (char*)&ret, size / 8); } - void MEM_WRITE_BN_BN(JitCpu* jitcpu, int size, bn_t addr, bn_t src) { uint64_t ptr; @@ -214,7 +211,6 @@ void MEM_WRITE_INT_BN(JitCpu* jitcpu, int size, uint64_t addr, bn_t src) } } - void MEM_WRITE_INT_BN_FROM_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr) { bn_t val; @@ -229,8 +225,6 @@ void MEM_WRITE_INT_BN_FROM_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* pt MEM_WRITE_INT_BN(jitcpu, size, addr, val); } - - PyObject* vm_get_mem(JitCpu *self, PyObject* args) { PyObject *py_addr; @@ -249,7 +243,6 @@ PyObject* vm_get_mem(JitCpu *self, PyObject* args) PyGetInt_uint64_t(py_addr, addr); PyGetInt_uint64_t(py_len, size); - if (size > SSIZE_MAX) { fprintf(stderr, "Read size wider than supported system\n"); exit(EXIT_FAILURE); diff --git a/miasm/jitter/Jitgcc.c b/miasm/jitter/Jitgcc.c index 08708a7df..c92efe4fb 100644 --- a/miasm/jitter/Jitgcc.c +++ b/miasm/jitter/Jitgcc.c @@ -71,25 +71,24 @@ PyObject* gcc_exec_block(PyObject* self, PyObject* args) retaddr = PyLong_FromUnsignedLongLong(BlockDst.address); // Check exception - if (status) - return retaddr; + if (status) { + break; + } // Check stop offsets - if (PySet_Contains(stop_offsets, retaddr)) - return retaddr; + if (PySet_Contains(stop_offsets, retaddr)) { + break; + } } + return retaddr; } - - static PyMethodDef GccMethods[] = { {"gcc_exec_block", gcc_exec_block, METH_VARARGS, "gcc exec block"}, {NULL, NULL, 0, NULL} /* Sentinel */ }; - - MOD_INIT(Jitgcc) { PyObject *module = NULL; diff --git a/miasm/jitter/Jitllvm.c b/miasm/jitter/Jitllvm.c index 7262c005c..57af8b240 100644 --- a/miasm/jitter/Jitllvm.c +++ b/miasm/jitter/Jitllvm.c @@ -86,8 +86,6 @@ static PyMethodDef LLVMMethods[] = { }; - - MOD_INIT(Jitllvm) { PyObject *module = NULL; diff --git a/miasm/jitter/arch/JitCore_mips32.c b/miasm/jitter/arch/JitCore_mips32.c index ce894e2a5..1b9a570d4 100644 --- a/miasm/jitter/arch/JitCore_mips32.c +++ b/miasm/jitter/arch/JitCore_mips32.c @@ -150,10 +150,8 @@ PyObject * cpu_init_regs(JitCpu* self) } - void dump_gpregs(struct vm_cpu* vmcpu) { - printf("ZR %.8"PRIX32" AT %.8"PRIX32" V0 %.8"PRIX32" V1 %.8"PRIX32" ", vmcpu->ZERO, vmcpu->AT, vmcpu->V0, vmcpu->V1); printf("A0 %.8"PRIX32" A1 %.8"PRIX32" A2 %.8"PRIX32" A3 %.8"PRIX32" ", @@ -174,7 +172,6 @@ void dump_gpregs(struct vm_cpu* vmcpu) vmcpu->PC); } - PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { struct vm_cpu* vmcpu; @@ -185,7 +182,6 @@ PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) return Py_None; } - PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; @@ -206,7 +202,6 @@ PyObject* cpu_get_exception(JitCpu* self, PyObject* args) 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); @@ -227,7 +222,6 @@ void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src) vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); } - PyObject* vm_set_mem(JitCpu *self, PyObject* args) { PyObject *py_addr; @@ -478,37 +472,26 @@ static PyTypeObject JitCpuType = { JitCpu_new, /* tp_new */ }; - - static PyMethodDef JitCore_mips32_Methods[] = { - - /* - - */ {"get_gpreg_offset_all", (PyCFunction)get_gpreg_offset_all, METH_NOARGS}, {NULL, NULL, 0, NULL} /* Sentinel */ - }; - - - - MOD_INIT(JitCore_mips32) { PyObject *module = NULL; MOD_DEF(module, "JitCore_mips32", "JitCore_mips32 module", JitCore_mips32_Methods); - if (module == NULL) + if (module == NULL) { RET_MODULE; - - if (PyType_Ready(&JitCpuType) < 0) + } + if (PyType_Ready(&JitCpuType) < 0) { RET_MODULE; - + } Py_INCREF(&JitCpuType); - if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) + if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) { RET_MODULE; - + } RET_MODULE; } diff --git a/miasm/jitter/arch/JitCore_x86.c b/miasm/jitter/arch/JitCore_x86.c index 608893a77..10f008a32 100644 --- a/miasm/jitter/arch/JitCore_x86.c +++ b/miasm/jitter/arch/JitCore_x86.c @@ -308,7 +308,6 @@ void dump_gpregs_16(struct vm_cpu* vmcpu) void dump_gpregs_32(struct vm_cpu* vmcpu) { - printf("EAX %.8"PRIX32" EBX %.8"PRIX32" ECX %.8"PRIX32" EDX %.8"PRIX32" ", (uint32_t)(vmcpu->RAX & 0xFFFFFFFF), (uint32_t)(vmcpu->RBX & 0xFFFFFFFF), @@ -331,7 +330,6 @@ void dump_gpregs_32(struct vm_cpu* vmcpu) void dump_gpregs_64(struct vm_cpu* vmcpu) { - printf("RAX %.16"PRIX64" RBX %.16"PRIX64" RCX %.16"PRIX64" RDX %.16"PRIX64" ", vmcpu->RAX, vmcpu->RBX, vmcpu->RCX, vmcpu->RDX); printf("RSI %.16"PRIX64" RDI %.16"PRIX64" RSP %.16"PRIX64" RBP %.16"PRIX64" ", @@ -346,7 +344,6 @@ void dump_gpregs_64(struct vm_cpu* vmcpu) printf("zf %.1d nf %.1d of %.1d cf %.1d\n", vmcpu->zf, vmcpu->nf, vmcpu->of, vmcpu->cf); - } PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) @@ -359,7 +356,6 @@ PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) return Py_None; } - PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) { struct vm_cpu* vmcpu; @@ -384,8 +380,6 @@ PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args) return Py_None; } - - PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; @@ -480,8 +474,6 @@ void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src) vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src); } - - PyObject* vm_set_mem(JitCpu *self, PyObject* args) { PyObject *py_addr; @@ -576,8 +568,6 @@ JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) return 0; \ } - - #define getset_reg_R_u16(regname) \ static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \ { \ @@ -594,7 +584,6 @@ JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) return 0; \ } - getset_reg_u64(RAX); getset_reg_u64(RBX); getset_reg_u64(RCX); @@ -782,7 +771,6 @@ PyObject* get_gpreg_offset_all(void) return dict; } - static PyGetSetDef JitCpu_getseters[] = { {"vmmngr", (getter)JitCpu_get_vmmngr, (setter)JitCpu_set_vmmngr, diff --git a/miasm/jitter/bn.c b/miasm/jitter/bn.c index daf2fdeb3..89cc6f497 100644 --- a/miasm/jitter/bn.c +++ b/miasm/jitter/bn.c @@ -37,8 +37,6 @@ static bn_t _lshift_word(bn_t a, int nwords); static bn_t _rshift_word(bn_t a, int nwords); - - /* Public / Exported functions. */ bn_t bignum_init(void) { @@ -52,7 +50,6 @@ bn_t bignum_init(void) return n; } - bn_t bignum_from_int(DTYPE_TMP i) { bn_t n; @@ -79,8 +76,6 @@ bn_t bignum_from_int(DTYPE_TMP i) return n; } - - bn_t bignum_from_uint64(uint64_t i) { bn_t n; @@ -106,13 +101,8 @@ bn_t bignum_from_uint64(uint64_t i) return n; } - - - - int bignum_to_int(bn_t n) { - int ret = 0; /* Endianness issue if machine is not little-endian? */ @@ -127,15 +117,11 @@ int bignum_to_int(bn_t n) #elif (WORD_SIZE == 4) ret += n.array[0]; #endif - - return ret; } - uint64_t bignum_to_uint64(bn_t n) { - uint64_t ret = 0; /* Endianness issue if machine is not little-endian? */ @@ -149,8 +135,6 @@ uint64_t bignum_to_uint64(bn_t n) ret += (uint64_t)(n.array[5]) << 40; ret += (uint64_t)(n.array[6]) << 48; ret += (uint64_t)(n.array[7]) << 56; - - #elif (WORD_SIZE == 2) ret += (uint64_t)(n.array[0]); ret += (uint64_t)(n.array[1]) << 16; @@ -160,16 +144,11 @@ uint64_t bignum_to_uint64(bn_t n) ret += n.array[0]; ret += (uint64_t)(n.array[1]) << 32; #endif - return ret; } - - - bn_t bignum_from_string(char* str, int nbytes) { - require(str, "str is null"); require(nbytes > 0, "nbytes must be positive"); require((nbytes & 1) == 0, "string format must be in hex -> equal number of bytes"); @@ -215,8 +194,6 @@ void bignum_to_string(bn_t n, char* str, int nbytes) str[i] = 0; } - - bn_t bignum_dec(bn_t n) { //require(n, "n is null"); @@ -238,7 +215,6 @@ bn_t bignum_dec(bn_t n) return n; } - bn_t bignum_inc(bn_t n) { //require(n, "n is null"); @@ -260,8 +236,6 @@ bn_t bignum_inc(bn_t n) return n; } - - bn_t bignum_add(bn_t a, bn_t b) { //require(a, "a is null"); @@ -281,7 +255,6 @@ bn_t bignum_add(bn_t a, bn_t b) return c; } - bn_t bignum_sub(bn_t a, bn_t b) { //require(a, "a is null"); @@ -305,9 +278,6 @@ bn_t bignum_sub(bn_t a, bn_t b) return c; } - - - bn_t bignum_mul(bn_t a, bn_t b) { //require(a, "a is null"); @@ -339,7 +309,6 @@ bn_t bignum_mul(bn_t a, bn_t b) return c; } - bn_t bignum_udiv(bn_t a, bn_t b) { //require(a, "a is null"); @@ -384,8 +353,6 @@ bn_t bignum_udiv(bn_t a, bn_t b) return c; } - - bn_t bignum_lshift(bn_t a, int nbits) { //require(a, "a is null"); @@ -414,7 +381,6 @@ bn_t bignum_lshift(bn_t a, int nbits) return b; } - bn_t bignum_rshift(bn_t a, int nbits) { //require(a, "a is null"); @@ -443,8 +409,6 @@ bn_t bignum_rshift(bn_t a, int nbits) return b; } - - bn_t bignum_a_rshift(bn_t a, int size, int nbits) { //require(a, "a is null"); @@ -490,8 +454,6 @@ bn_t bignum_not(bn_t a) return b; } - - bn_t bignum_umod(bn_t a, bn_t b) { /* @@ -513,7 +475,6 @@ bn_t bignum_umod(bn_t a, bn_t b) return d; } - bn_t bignum_and(bn_t a, bn_t b) { //require(a, "a is null"); @@ -529,7 +490,6 @@ bn_t bignum_and(bn_t a, bn_t b) return c; } - bn_t bignum_or(bn_t a, bn_t b) { //require(a, "a is null"); @@ -544,7 +504,6 @@ bn_t bignum_or(bn_t a, bn_t b) return c; } - bn_t bignum_xor(bn_t a, bn_t b) { //require(a, "a is null"); @@ -559,7 +518,6 @@ bn_t bignum_xor(bn_t a, bn_t b) return c; } - int bignum_cmp(bn_t a, bn_t b) { //require(a, "a is null"); @@ -667,7 +625,6 @@ bn_t bignum_assign(bn_t src) return dst; } - bn_t bignum_mask(bn_t src, int bits) { bn_t dst; @@ -707,7 +664,6 @@ static bn_t _rshift_word(bn_t a, int nwords) return a; } - static bn_t _lshift_word(bn_t a, int nwords) { //require(a, "a is null"); @@ -734,7 +690,6 @@ static bn_t _lshift_word(bn_t a, int nwords) return a; } - static bn_t _lshift_one_bit(bn_t a) { //require(a, "a is null"); @@ -748,7 +703,6 @@ static bn_t _lshift_one_bit(bn_t a) return a; } - static bn_t _rshift_one_bit(bn_t a) { //require(a, "a is null"); @@ -762,7 +716,6 @@ static bn_t _rshift_one_bit(bn_t a) return a; } - bn_t bignum_rol(bn_t a, int size, int nbits) { bn_t c; @@ -775,7 +728,6 @@ bn_t bignum_rol(bn_t a, int size, int nbits) return c; } - bn_t bignum_ror(bn_t a, int size, int nbits) { bn_t c; @@ -801,8 +753,6 @@ int bignum_getbit(bn_t a, int pos) } - - /* * Count leading zeros - count the number of zero starting at the most * significant bit @@ -826,8 +776,6 @@ int bignum_cntleadzeros(bn_t n, int size) return i; } - - /* * Count trailing zeros - count the number of zero starting at the least * significant bit @@ -851,9 +799,6 @@ int bignum_cnttrailzeros(bn_t n, int size) return i; } - - - bn_t bignum_sdiv(bn_t a, bn_t b, int size) { require(size, "size must be greater than 0"); @@ -888,8 +833,6 @@ bn_t bignum_sdiv(bn_t a, bn_t b, int size) return c; } - - bn_t bignum_smod(bn_t a, bn_t b, int size) { require(size, "size must be greater than 0"); diff --git a/miasm/jitter/compat_py23.h b/miasm/jitter/compat_py23.h index 936c08f3c..88c9fb5c6 100644 --- a/miasm/jitter/compat_py23.h +++ b/miasm/jitter/compat_py23.h @@ -1,8 +1,6 @@ #ifndef __COMPAT_PY23_H__ #define __COMPAT_PY23_H__ - - #if PY_MAJOR_VERSION >= 3 #define PyGetInt_uint_t(size_type, item, value) \ if (PyLong_Check(item)) { \ @@ -95,8 +93,6 @@ #endif - - #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) @@ -131,8 +127,4 @@ #define RET_MODULE return #endif - - - - #endif diff --git a/miasm/jitter/vm_mngr.c b/miasm/jitter/vm_mngr.c index 0b8881300..1d448bb24 100644 --- a/miasm/jitter/vm_mngr.c +++ b/miasm/jitter/vm_mngr.c @@ -25,15 +25,9 @@ #include "queue.h" - - /****************memory manager**************/ - - - #define MIN(a,b) (((a)<(b))?(a):(b)) -#define MAX(a,b) (((a)>(b))?(a):(b)) // #define DEBUG_MIASM_AUTOMOD_CODE @@ -109,12 +103,11 @@ void print_val(uint64_t base, uint64_t addr) fprintf(stderr, "addr 0x%"PRIX64" val 0x%"PRIX64"\n", addr-base, *ptr); } -int midpoint(int imin, int imax) +static int midpoint(int imin, int imax) { return (imin + imax) / 2; } - int find_page_node(struct memory_page_node * array, uint64_t key, int imin, int imax) { // continue searching while [imin,imax] is not empty @@ -363,7 +356,6 @@ void add_range_to_list(struct memory_access_list * access, uint64_t addr1, uint6 memory_access_list_add(access, addr1, addr2); } - void add_mem_read(vm_mngr_t* vm_mngr, uint64_t addr, uint64_t size) { add_range_to_list(&(vm_mngr->memory_r), addr, addr + size); @@ -402,7 +394,6 @@ void check_invalid_code_blocs(vm_mngr_t* vm_mngr) } } - void check_memory_breakpoint(vm_mngr_t* vm_mngr) { size_t i; @@ -433,7 +424,6 @@ void check_memory_breakpoint(vm_mngr_t* vm_mngr) } } - PyObject* get_memory_pylist(vm_mngr_t* vm_mngr, struct memory_access_list* memory_list) { size_t i; @@ -512,7 +502,6 @@ uint64_t vm_MEM_LOOKUP_64(vm_mngr_t* vm_mngr, uint64_t addr) return ret; } - int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, size_t size) { char* buffer; @@ -589,8 +578,6 @@ int vm_write_mem(vm_mngr_t* vm_mngr, uint64_t addr, char *buffer, size_t size) return 0; } - - int is_mapped(vm_mngr_t* vm_mngr, uint64_t addr, size_t size) { size_t len; @@ -673,7 +660,6 @@ struct code_bloc_node * create_code_bloc_node(uint64_t ad_start, uint64_t ad_sto return cbp; } - void add_code_bloc(vm_mngr_t* vm_mngr, struct code_bloc_node* cbp) { LIST_INSERT_HEAD(&vm_mngr->code_bloc_pool, cbp, next); @@ -687,14 +673,13 @@ void dump_code_bloc_pool(vm_mngr_t* vm_mngr) { struct code_bloc_node * cbp; - LIST_FOREACH(cbp, &vm_mngr->code_bloc_pool, next){ + LIST_FOREACH(cbp, &vm_mngr->code_bloc_pool, next) { printf("ad start %"PRIX64" ad_stop %"PRIX64"\n", cbp->ad_start, cbp->ad_stop); } } - void init_memory_page_pool(vm_mngr_t* vm_mngr) { @@ -734,7 +719,6 @@ void reset_memory_page_pool(vm_mngr_t* vm_mngr) vm_mngr->memory_pages_number = 0; } - void reset_code_bloc_pool(vm_mngr_t* vm_mngr) { struct code_bloc_node * cbp; @@ -814,7 +798,6 @@ void add_memory_page(vm_mngr_t* vm_mngr, struct memory_page_node* mpn_a) exit(EXIT_FAILURE); } - memmove(&vm_mngr->memory_pages_array[i+1], &vm_mngr->memory_pages_array[i], sizeof(struct memory_page_node) * (vm_mngr->memory_pages_number - i) @@ -887,7 +870,6 @@ void dump_memory_breakpoint_pool(vm_mngr_t* vm_mngr) } } - void add_memory_breakpoint(vm_mngr_t* vm_mngr, uint64_t ad, uint64_t size, unsigned int access) { struct memory_breakpoint_info * mpn_a; diff --git a/miasm/jitter/vm_mngr.h b/miasm/jitter/vm_mngr.h index 913d06f87..7e234748c 100644 --- a/miasm/jitter/vm_mngr.h +++ b/miasm/jitter/vm_mngr.h @@ -126,15 +126,10 @@ typedef struct { }vm_mngr_t; - - typedef struct { PyObject *func; }func_resolver; - - - //extern vm_mngr_t vmmngr; struct code_bloc_node { @@ -152,8 +147,6 @@ struct memory_breakpoint_info { LIST_ENTRY(memory_breakpoint_info) next; }; - - #define PAGE_READ 1 #define PAGE_WRITE 2 #define PAGE_EXEC 4 @@ -248,22 +241,16 @@ _MIASM_EXPORT void reset_memory_access(vm_mngr_t* vm_mngr); PyObject* get_memory_read(vm_mngr_t* vm_mngr); PyObject* get_memory_write(vm_mngr_t* vm_mngr); - char* dump(vm_mngr_t* vm_mngr); void dump_memory_breakpoint_pool(vm_mngr_t* vm_mngr); PyObject* addr2BlocObj(vm_mngr_t* vm_mngr, uint64_t addr); - - - /********************************************/ unsigned int get_memory_page_max_address(void); unsigned int get_memory_page_max_user_address(void); - int is_mpn_in_tab(vm_mngr_t* vm_mngr, struct memory_page_node* mpn_a); - void _func_free(void); void _func_alloc(void); unsigned int _get_memory_page_max_address_py(void); @@ -277,7 +264,6 @@ void _func_virtualfree_memory_page(void); void _func_loadlib_fake(void); void _func_getproc_fake(void); - void func_free(void); void func_alloc(void); unsigned int get_memory_page_max_address_py(void); diff --git a/miasm/jitter/vm_mngr_py.c b/miasm/jitter/vm_mngr_py.c index 12b35cf79..d6c6fdb83 100644 --- a/miasm/jitter/vm_mngr_py.c +++ b/miasm/jitter/vm_mngr_py.c @@ -70,8 +70,6 @@ PyObject* set_alarm(VmMngr* self) return Py_None; } - - PyObject* vm_add_memory_page(VmMngr* self, PyObject* args) { PyObject *addr; @@ -127,8 +125,6 @@ PyObject* vm_add_memory_page(VmMngr* self, PyObject* args) return Py_None; } - - PyObject* vm_set_mem_access(VmMngr* self, PyObject* args) { PyObject *addr; @@ -497,7 +493,6 @@ PyObject* vm_add_memory_breakpoint(VmMngr* self, PyObject* args) return Py_None; } - PyObject* vm_remove_memory_breakpoint(VmMngr* self, PyObject* args) { PyObject *ad; @@ -516,7 +511,6 @@ PyObject* vm_remove_memory_breakpoint(VmMngr* self, PyObject* args) return Py_None; } - PyObject* vm_set_exception(VmMngr* self, PyObject* args) { PyObject *item1; @@ -646,7 +640,6 @@ PyObject* vm_dump_memory_breakpoint(VmMngr* self, PyObject* args) return Py_None; } - PyObject* vm_get_all_memory(VmMngr* self, PyObject* args) { PyObject *o; @@ -655,13 +648,12 @@ PyObject* vm_get_all_memory(VmMngr* self, PyObject* args) PyObject *dict2; int i; - - dict = PyDict_New(); + dict = PyDict_New(); for (i=0;ivm_mngr.memory_pages_number; i++) { mpn = &self->vm_mngr.memory_pages_array[i]; - dict2 = PyDict_New(); + dict2 = PyDict_New(); o = PyBytes_FromStringAndSize(mpn->ad_hp, mpn->size); PyDict_SetItemString(dict2, "data", o); @@ -683,24 +675,20 @@ PyObject* vm_get_all_memory(VmMngr* self, PyObject* args) return dict; } - PyObject* vm_reset_memory_page_pool(VmMngr* self, PyObject* args) { - reset_memory_page_pool(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; - + reset_memory_page_pool(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_reset_code_bloc_pool(VmMngr* self, PyObject* args) { - reset_code_bloc_pool(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; - + reset_code_bloc_pool(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } - PyObject* vm_add_code_bloc(VmMngr *self, PyObject *args) { PyObject *item1; @@ -768,12 +756,10 @@ PyObject* vm_get_memory_write(VmMngr* self, PyObject* args) return result; } - - static PyObject * vm_set_big_endian(VmMngr *self, PyObject *value, void *closure) { - self->vm_mngr.sex = __BIG_ENDIAN; + self->vm_mngr.sex = __BIG_ENDIAN; Py_INCREF(Py_None); return Py_None; } @@ -781,12 +767,11 @@ vm_set_big_endian(VmMngr *self, PyObject *value, void *closure) static PyObject * vm_set_little_endian(VmMngr *self, PyObject *value, void *closure) { - self->vm_mngr.sex = __LITTLE_ENDIAN; + self->vm_mngr.sex = __LITTLE_ENDIAN; Py_INCREF(Py_None); return Py_None; } - static PyObject * vm_is_little_endian(VmMngr *self, PyObject *value, void *closure) { From ed48bc4b0d821a30787d4f57d90582ca26e92346 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:35:18 +0100 Subject: [PATCH 5/9] Do not mix spaces and tabs and use consistent indentation in C files --- miasm/jitter/JitCore.c | 85 ++- miasm/jitter/JitCore.h | 9 +- miasm/jitter/arch/JitCore_mips32.c | 227 ++++--- miasm/jitter/arch/JitCore_x86.c | 951 ++++++++++++++--------------- miasm/jitter/compat_py23.h | 4 +- miasm/jitter/op_semantics.c | 175 +++--- miasm/jitter/vm_mngr.c | 146 ++--- miasm/jitter/vm_mngr_py.c | 557 +++++++++-------- 8 files changed, 1049 insertions(+), 1105 deletions(-) diff --git a/miasm/jitter/JitCore.c b/miasm/jitter/JitCore.c index 9ee8ae760..00e192f16 100644 --- a/miasm/jitter/JitCore.c +++ b/miasm/jitter/JitCore.c @@ -15,16 +15,15 @@ void JitCpu_dealloc(JitCpu* self) { - Py_TYPE(self)->tp_free((PyObject*)self); + Py_TYPE(self)->tp_free((PyObject*)self); } - PyObject * JitCpu_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - JitCpu *self; + JitCpu *self; - self = (JitCpu *)type->tp_alloc(type, 0); - return (PyObject *)self; + self = (JitCpu *)type->tp_alloc(type, 0); + return (PyObject *)self; } PyObject * JitCpu_get_vmmngr(JitCpu *self, void *closure) @@ -71,7 +70,7 @@ uint16_t MEM_LOOKUP_16(JitCpu* jitcpu, uint64_t addr) uint32_t MEM_LOOKUP_32(JitCpu* jitcpu, uint64_t addr) { - return vm_MEM_LOOKUP_32(&(jitcpu->pyvm->vm_mngr), addr); + return vm_MEM_LOOKUP_32(&(jitcpu->pyvm->vm_mngr), addr); } uint64_t MEM_LOOKUP_64(JitCpu* jitcpu, uint64_t addr) @@ -88,8 +87,7 @@ bn_t MEM_LOOKUP_BN_BN(JitCpu* jitcpu, int size, bn_t addr) ptr = bignum_to_uint64(addr); - - for (i=0; i < size; i += 8) { + for (i = 0; i < size; i += 8) { tmp = vm_MEM_LOOKUP_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr); ptr += 1; val = bignum_or(val, bignum_lshift(bignum_from_int(tmp), i)); @@ -106,36 +104,34 @@ uint64_t MEM_LOOKUP_BN_INT(JitCpu* jitcpu, int size, bn_t addr) ptr = bignum_to_uint64(addr); switch (size) { - case 8: - val = vm_MEM_LOOKUP_08(&(jitcpu->pyvm->vm_mngr), ptr); - break; - case 16: - val = vm_MEM_LOOKUP_16(&(jitcpu->pyvm->vm_mngr), ptr); - break; - case 32: - val = vm_MEM_LOOKUP_32(&(jitcpu->pyvm->vm_mngr), ptr); - break; - case 64: - val = vm_MEM_LOOKUP_64(&(jitcpu->pyvm->vm_mngr), ptr); - break; - default: - fprintf(stderr, "Error: bad READ size %d\n", size); - exit(-1); - break; + case 8: + val = vm_MEM_LOOKUP_08(&(jitcpu->pyvm->vm_mngr), ptr); + break; + case 16: + val = vm_MEM_LOOKUP_16(&(jitcpu->pyvm->vm_mngr), ptr); + break; + case 32: + val = vm_MEM_LOOKUP_32(&(jitcpu->pyvm->vm_mngr), ptr); + break; + case 64: + val = vm_MEM_LOOKUP_64(&(jitcpu->pyvm->vm_mngr), ptr); + break; + default: + fprintf(stderr, "Error: bad READ size %d\n", size); + exit(-1); + break; } return val; } - - bn_t MEM_LOOKUP_INT_BN(JitCpu* jitcpu, int size, uint64_t addr) { int i; uint8_t tmp; bn_t val = bignum_from_int(0); - for (i=0; i < size; i += 8) { + for (i = 0; i < size; i += 8) { tmp = vm_MEM_LOOKUP_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr); addr += 1; val = bignum_or(val, bignum_lshift(bignum_from_int(tmp), i)); @@ -164,7 +160,7 @@ void MEM_WRITE_BN_BN(JitCpu* jitcpu, int size, bn_t addr, bn_t src) int i; ptr = bignum_to_uint64(addr); - for (i=0; i < size; i += 8) { + for (i = 0; i < size; i += 8) { val = bignum_to_uint64(src) & 0xFF; vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, val); ptr += 1; @@ -172,29 +168,28 @@ void MEM_WRITE_BN_BN(JitCpu* jitcpu, int size, bn_t addr, bn_t src) } } - void MEM_WRITE_BN_INT(JitCpu* jitcpu, int size, bn_t addr, uint64_t src) { uint64_t ptr; ptr = bignum_to_uint64(addr); switch (size) { - case 8: - vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned char)src); - break; - case 16: - vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned short)src); - break; - case 32: - vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned int)src); - break; - case 64: - vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, src); - break; - default: - fprintf(stderr, "Error: bad write size %d\n", size); - exit(-1); - break; + case 8: + vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned char)src); + break; + case 16: + vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned short)src); + break; + case 32: + vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned int)src); + break; + case 64: + vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, src); + break; + default: + fprintf(stderr, "Error: bad write size %d\n", size); + exit(-1); + break; } } diff --git a/miasm/jitter/JitCore.h b/miasm/jitter/JitCore.h index 85be57da0..14e4ec167 100644 --- a/miasm/jitter/JitCore.h +++ b/miasm/jitter/JitCore.h @@ -58,7 +58,7 @@ PyObject* cst_32; \ PyObject* cst_ffffffff; \ uint64_t tmp; \ - if (PyLong_Check(py_long)){ \ + if (PyLong_Check(py_long)) { \ Py_INCREF(py_long); \ } else { \ RAISE(PyExc_TypeError,"arg must be int"); \ @@ -125,13 +125,12 @@ PyObject* cst_ffffffff; \ uint64_t tmp; \ \ - if (PyInt_Check(py_long)){ \ + if (PyInt_Check(py_long)) { \ tmp = (uint64_t)PyInt_AsLong(py_long); \ py_long = PyLong_FromLong((long)tmp); \ - } else if (PyLong_Check(py_long)){ \ + } else if (PyLong_Check(py_long)) { \ Py_INCREF(py_long); \ - } \ - else{ \ + } else { \ RAISE(PyExc_TypeError,"arg must be int"); \ } \ \ diff --git a/miasm/jitter/arch/JitCore_mips32.c b/miasm/jitter/arch/JitCore_mips32.c index 1b9a570d4..bc4581855 100644 --- a/miasm/jitter/arch/JitCore_mips32.c +++ b/miasm/jitter/arch/JitCore_mips32.c @@ -13,134 +13,129 @@ -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}, +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 **************************/ - - PyObject* cpu_get_gpreg(JitCpu* self) { - PyObject *dict = PyDict_New(); - PyObject *o; - - get_reg(ZERO); - get_reg(AT); - get_reg(V0); - get_reg(V1); - get_reg(A0); - get_reg(A1); - get_reg(A2); - get_reg(A3); - get_reg(T0); - get_reg(T1); - get_reg(T2); - get_reg(T3); - get_reg(T4); - get_reg(T5); - get_reg(T6); - get_reg(T7); - get_reg(S0); - get_reg(S1); - get_reg(S2); - get_reg(S3); - get_reg(S4); - get_reg(S5); - get_reg(S6); - get_reg(S7); - get_reg(T8); - get_reg(T9); - get_reg(K0); - get_reg(K1); - get_reg(GP); - get_reg(SP); - get_reg(FP); - get_reg(RA); - get_reg(PC); - get_reg(PC_FETCH); - get_reg(R_LO); - get_reg(R_HI); - - return dict; + PyObject *dict = PyDict_New(); + PyObject *o; + + get_reg(ZERO); + get_reg(AT); + get_reg(V0); + get_reg(V1); + get_reg(A0); + get_reg(A1); + get_reg(A2); + get_reg(A3); + get_reg(T0); + get_reg(T1); + get_reg(T2); + get_reg(T3); + get_reg(T4); + get_reg(T5); + get_reg(T6); + get_reg(T7); + get_reg(S0); + get_reg(S1); + get_reg(S2); + get_reg(S3); + get_reg(S4); + get_reg(S5); + get_reg(S6); + get_reg(S7); + get_reg(T8); + get_reg(T9); + get_reg(K0); + get_reg(K1); + get_reg(GP); + get_reg(SP); + get_reg(FP); + get_reg(RA); + get_reg(PC); + get_reg(PC_FETCH); + get_reg(R_LO); + get_reg(R_HI); + + return dict; } - - - PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) { - PyObject* dict; - PyObject *d_key, *d_value = NULL; - Py_ssize_t pos = 0; - const char *d_key_name; - uint32_t val; - unsigned int i, found; - - if (!PyArg_ParseTuple(args, "O", &dict)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); - if(!PyDict_Check(dict)) - RAISE(PyExc_TypeError, "arg must be dict"); - while(PyDict_Next(dict, &pos, &d_key, &d_value)){ - PyGetStr(d_key_name, d_key); - 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)) - continue; - *((uint32_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val; - found = 1; - break; - } - - if (found) - continue; - fprintf(stderr, "unknown key: %s\n", d_key_name); - RAISE(PyExc_ValueError, "unknown reg"); - } - Py_INCREF(Py_None); - return Py_None; + PyObject* dict; + PyObject *d_key, *d_value = NULL; + Py_ssize_t pos = 0; + const char *d_key_name; + uint32_t val; + unsigned int i; + bool found; + + if (!PyArg_ParseTuple(args, "O", &dict)) { + RAISE(PyExc_TypeError,"Cannot parse arguments"); + } + if(!PyDict_Check(dict)) { + RAISE(PyExc_TypeError, "arg must be dict"); + } + while(PyDict_Next(dict, &pos, &d_key, &d_value)) { + PyGetStr(d_key_name, d_key); + PyGetInt_uint32_t(d_value, val); + + found = false; + 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 = true; + break; + } + if (found) + continue; + fprintf(stderr, "unknown key: %s\n", d_key_name); + RAISE(PyExc_ValueError, "unknown reg"); + } + Py_INCREF(Py_None); + return Py_None; } - - - PyObject * cpu_init_regs(JitCpu* self) { memset(self->cpu, 0, sizeof(struct vm_cpu)); diff --git a/miasm/jitter/arch/JitCore_x86.c b/miasm/jitter/arch/JitCore_x86.c index 10f008a32..a6d9a7eae 100644 --- a/miasm/jitter/arch/JitCore_x86.c +++ b/miasm/jitter/arch/JitCore_x86.c @@ -15,264 +15,259 @@ struct vm_cpu ref_arch_regs; reg_dict gpreg_dict[] = { - {.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}, + {.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}, }; - - /************************** JitCpu object **************************/ - - - - PyObject* cpu_get_gpreg(JitCpu* self) { - PyObject *dict = PyDict_New(); - PyObject *o; - - get_reg(RAX); - get_reg(RBX); - get_reg(RCX); - get_reg(RDX); - get_reg(RSI); - get_reg(RDI); - get_reg(RSP); - get_reg(RBP); - - get_reg(R8); - get_reg(R9); - get_reg(R10); - get_reg(R11); - get_reg(R12); - get_reg(R13); - get_reg(R14); - get_reg(R15); - - get_reg(RIP); - - get_reg(zf); - get_reg(nf); - get_reg(pf); - get_reg(of); - get_reg(cf); - get_reg(af); - get_reg(df); - - - get_reg(ES); - get_reg(CS); - get_reg(SS); - get_reg(DS); - get_reg(FS); - get_reg(GS); - - get_reg(MM0); - get_reg(MM1); - get_reg(MM2); - get_reg(MM3); - get_reg(MM4); - get_reg(MM5); - get_reg(MM6); - get_reg(MM7); - - get_reg_bn(XMM0, 128); - get_reg_bn(XMM1, 128); - get_reg_bn(XMM2, 128); - get_reg_bn(XMM3, 128); - get_reg_bn(XMM4, 128); - get_reg_bn(XMM5, 128); - get_reg_bn(XMM6, 128); - get_reg_bn(XMM7, 128); - get_reg_bn(XMM8, 128); - get_reg_bn(XMM9, 128); - get_reg_bn(XMM10, 128); - get_reg_bn(XMM11, 128); - get_reg_bn(XMM12, 128); - get_reg_bn(XMM13, 128); - get_reg_bn(XMM14, 128); - get_reg_bn(XMM15, 128); - - get_reg(tsc); - - return dict; + PyObject *dict = PyDict_New(); + PyObject *o; + + get_reg(RAX); + get_reg(RBX); + get_reg(RCX); + get_reg(RDX); + get_reg(RSI); + get_reg(RDI); + get_reg(RSP); + get_reg(RBP); + + get_reg(R8); + get_reg(R9); + get_reg(R10); + get_reg(R11); + get_reg(R12); + get_reg(R13); + get_reg(R14); + get_reg(R15); + + get_reg(RIP); + + get_reg(zf); + get_reg(nf); + get_reg(pf); + get_reg(of); + get_reg(cf); + get_reg(af); + get_reg(df); + + + get_reg(ES); + get_reg(CS); + get_reg(SS); + get_reg(DS); + get_reg(FS); + get_reg(GS); + + get_reg(MM0); + get_reg(MM1); + get_reg(MM2); + get_reg(MM3); + get_reg(MM4); + get_reg(MM5); + get_reg(MM6); + get_reg(MM7); + + get_reg_bn(XMM0, 128); + get_reg_bn(XMM1, 128); + get_reg_bn(XMM2, 128); + get_reg_bn(XMM3, 128); + get_reg_bn(XMM4, 128); + get_reg_bn(XMM5, 128); + get_reg_bn(XMM6, 128); + get_reg_bn(XMM7, 128); + get_reg_bn(XMM8, 128); + get_reg_bn(XMM9, 128); + get_reg_bn(XMM10, 128); + get_reg_bn(XMM11, 128); + get_reg_bn(XMM12, 128); + get_reg_bn(XMM13, 128); + get_reg_bn(XMM14, 128); + get_reg_bn(XMM15, 128); + + get_reg(tsc); + + return dict; } - PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) { - PyObject* dict; - PyObject *d_key, *d_value = NULL; - const char *d_key_name; - Py_ssize_t pos = 0; - uint8_t val08; - uint16_t val16; - uint32_t val32; - uint64_t val64; - unsigned int i, found; - - if (!PyArg_ParseTuple(args, "O", &dict)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); - if(!PyDict_Check(dict)) - RAISE(PyExc_TypeError, "arg must be dict"); - while(PyDict_Next(dict, &pos, &d_key, &d_value)){ - PyGetStr(d_key_name, d_key); - found = 0; - for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ - if (strcmp(d_key_name, gpreg_dict[i].name)) - continue; - found = 1; - switch (gpreg_dict[i].size) { - default: - RAISE(PyExc_TypeError, "Unsupported size"); - break; - case 8: - PyGetInt_uint8_t(d_value, val08); - *((uint8_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val08; - break; - case 16: - PyGetInt_uint16_t(d_value, val16); - *((uint16_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val16; - 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; - case 128: - { - bn_t bn; - int j; - PyObject* py_long = d_value; - PyObject* py_long_new; - PyObject* py_tmp; - PyObject* cst_32; - PyObject* cst_ffffffff; - uint64_t tmp; + PyObject* dict; + PyObject *d_key, *d_value = NULL; + const char *d_key_name; + Py_ssize_t pos = 0; + uint8_t val08; + uint16_t val16; + uint32_t val32; + uint64_t val64; + unsigned int i; + bool found; + + if (!PyArg_ParseTuple(args, "O", &dict)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); + if(!PyDict_Check(dict)) + RAISE(PyExc_TypeError, "arg must be dict"); + while (PyDict_Next(dict, &pos, &d_key, &d_value)) { + PyGetStr(d_key_name, d_key); + found = false; + for (i = 0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++) { + if (strcmp(d_key_name, gpreg_dict[i].name)) + continue; + found = true; + switch (gpreg_dict[i].size) { + case 8: + PyGetInt_uint8_t(d_value, val08); + *((uint8_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val08; + break; + case 16: + PyGetInt_uint16_t(d_value, val16); + *((uint16_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset)) = val16; + 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; + case 128: + { + bn_t bn; + int j; + PyObject* py_long = d_value; + PyObject* py_long_new; + PyObject* py_tmp; + PyObject* cst_32; + PyObject* cst_ffffffff; + uint64_t tmp; #if PY_MAJOR_VERSION >= 3 - if (PyLong_Check(py_long)){ - /* Already PyLong */ - /* Increment ref as we will decement it next */ - Py_INCREF(py_long); - } else { - RAISE(PyExc_TypeError,"arg must be int"); - } + if (PyLong_Check(py_long)) { + /* Already PyLong */ + /* Increment ref as we will decement it next */ + Py_INCREF(py_long); + } else { + RAISE(PyExc_TypeError,"arg must be int"); + } #else - if (PyInt_Check(py_long)){ - tmp = (uint64_t)PyInt_AsLong(py_long); - py_long = PyLong_FromLong((long)tmp); - } else if (PyLong_Check(py_long)){ - /* Already PyLong */ - /* Increment ref as we will decement it next */ - Py_INCREF(py_long); - } - else{ - RAISE(PyExc_TypeError,"arg must be int"); - } + if (PyInt_Check(py_long)) { + tmp = (uint64_t)PyInt_AsLong(py_long); + py_long = PyLong_FromLong((long)tmp); + } else if (PyLong_Check(py_long)) { + /* Already PyLong */ + /* Increment ref as we will decement it next */ + Py_INCREF(py_long); + } + else{ + RAISE(PyExc_TypeError,"arg must be int"); + } #endif - cst_ffffffff = PyLong_FromLong(0xffffffff); - cst_32 = PyLong_FromLong(32); - bn = bignum_from_int(0); - - for (j = 0; j < BN_BYTE_SIZE; j += 4) { - py_tmp = PyObject_CallMethod(py_long, "__and__", "O", cst_ffffffff); - tmp = PyLong_AsUnsignedLongMask(py_tmp); - Py_DECREF(py_tmp); - bn = bignum_lshift(bn, 32); - bn = bignum_or(bn, bignum_from_uint64(tmp)); - - py_long_new = PyObject_CallMethod(py_long, "__rshift__", "O", cst_32); - Py_DECREF(py_long); - py_long = py_long_new; - } - Py_DECREF(py_long); - Py_DECREF(cst_32); - Py_DECREF(cst_ffffffff); - *(bn_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset) = bignum_mask(bn, 128); - } - break; - } - break; - } - - if (found) - continue; - fprintf(stderr, "unknown key: %s\n", d_key_name); - RAISE(PyExc_ValueError, "unknown reg"); - } - Py_INCREF(Py_None); - return Py_None; + cst_ffffffff = PyLong_FromLong(0xffffffff); + cst_32 = PyLong_FromLong(32); + bn = bignum_from_int(0); + + for (j = 0; j < BN_BYTE_SIZE; j += 4) { + py_tmp = PyObject_CallMethod(py_long, "__and__", "O", cst_ffffffff); + tmp = PyLong_AsUnsignedLongMask(py_tmp); + Py_DECREF(py_tmp); + bn = bignum_lshift(bn, 32); + bn = bignum_or(bn, bignum_from_uint64(tmp)); + + py_long_new = PyObject_CallMethod(py_long, "__rshift__", "O", cst_32); + Py_DECREF(py_long); + py_long = py_long_new; + } + Py_DECREF(py_long); + Py_DECREF(cst_32); + Py_DECREF(cst_ffffffff); + *(bn_t*)(((char*)(self->cpu)) + gpreg_dict[i].offset) = bignum_mask(bn, 128); + } + break; + default: + RAISE(PyExc_TypeError, "Unsupported size"); + break; + } + break; + } + + if (found) { + continue; + } + fprintf(stderr, "unknown key: %s\n", d_key_name); + RAISE(PyExc_ValueError, "unknown reg"); + } + Py_INCREF(Py_None); + return Py_None; } PyObject * cpu_init_regs(JitCpu* self) @@ -282,7 +277,6 @@ PyObject * cpu_init_regs(JitCpu* self) ((struct vm_cpu*)self->cpu)->i_f = 1; Py_INCREF(Py_None); return Py_None; - } void dump_gpregs_16(struct vm_cpu* vmcpu) @@ -325,7 +319,6 @@ void dump_gpregs_32(struct vm_cpu* vmcpu) (uint32_t)(vmcpu->nf & 0x1), (uint32_t)(vmcpu->of & 0x1), (uint32_t)(vmcpu->cf & 0x1)); - } void dump_gpregs_64(struct vm_cpu* vmcpu) @@ -675,271 +668,261 @@ getset_reg_u32(interrupt_num); PyObject* get_gpreg_offset_all(void) { - PyObject *dict = PyDict_New(); - PyObject *o; - get_reg_off(exception_flags); - - get_reg_off(RAX); - get_reg_off(RBX); - get_reg_off(RCX); - get_reg_off(RDX); - get_reg_off(RSI); - get_reg_off(RDI); - get_reg_off(RSP); - get_reg_off(RBP); - get_reg_off(R8); - get_reg_off(R9); - get_reg_off(R10); - get_reg_off(R11); - get_reg_off(R12); - get_reg_off(R13); - get_reg_off(R14); - get_reg_off(R15); - get_reg_off(RIP); - get_reg_off(zf); - get_reg_off(nf); - get_reg_off(pf); - get_reg_off(of); - get_reg_off(cf); - get_reg_off(af); - get_reg_off(df); - get_reg_off(tf); - get_reg_off(i_f); - get_reg_off(iopl_f); - get_reg_off(nt); - get_reg_off(rf); - get_reg_off(vm); - get_reg_off(ac); - get_reg_off(vif); - get_reg_off(vip); - get_reg_off(i_d); - get_reg_off(my_tick); - get_reg_off(cond); - - get_reg_off(float_st0); - get_reg_off(float_st1); - get_reg_off(float_st2); - get_reg_off(float_st3); - get_reg_off(float_st4); - get_reg_off(float_st5); - get_reg_off(float_st6); - get_reg_off(float_st7); - - get_reg_off(ES); - get_reg_off(CS); - get_reg_off(SS); - get_reg_off(DS); - get_reg_off(FS); - get_reg_off(GS); - - get_reg_off(MM0); - get_reg_off(MM1); - get_reg_off(MM2); - get_reg_off(MM3); - get_reg_off(MM4); - get_reg_off(MM5); - get_reg_off(MM6); - get_reg_off(MM7); - - get_reg_off(XMM0); - get_reg_off(XMM1); - get_reg_off(XMM2); - get_reg_off(XMM3); - get_reg_off(XMM4); - get_reg_off(XMM5); - get_reg_off(XMM6); - get_reg_off(XMM7); - get_reg_off(XMM8); - get_reg_off(XMM9); - get_reg_off(XMM10); - get_reg_off(XMM11); - get_reg_off(XMM12); - get_reg_off(XMM13); - get_reg_off(XMM14); - get_reg_off(XMM15); - - get_reg_off(tsc); - - get_reg_off(interrupt_num); - get_reg_off(exception_flags); - - get_reg_off(float_stack_ptr); - get_reg_off(reg_float_cs); - get_reg_off(reg_float_eip); - get_reg_off(reg_float_control); - - return dict; + PyObject *dict = PyDict_New(); + PyObject *o; + get_reg_off(exception_flags); + + get_reg_off(RAX); + get_reg_off(RBX); + get_reg_off(RCX); + get_reg_off(RDX); + get_reg_off(RSI); + get_reg_off(RDI); + get_reg_off(RSP); + get_reg_off(RBP); + get_reg_off(R8); + get_reg_off(R9); + get_reg_off(R10); + get_reg_off(R11); + get_reg_off(R12); + get_reg_off(R13); + get_reg_off(R14); + get_reg_off(R15); + get_reg_off(RIP); + get_reg_off(zf); + get_reg_off(nf); + get_reg_off(pf); + get_reg_off(of); + get_reg_off(cf); + get_reg_off(af); + get_reg_off(df); + get_reg_off(tf); + get_reg_off(i_f); + get_reg_off(iopl_f); + get_reg_off(nt); + get_reg_off(rf); + get_reg_off(vm); + get_reg_off(ac); + get_reg_off(vif); + get_reg_off(vip); + get_reg_off(i_d); + get_reg_off(my_tick); + get_reg_off(cond); + + get_reg_off(float_st0); + get_reg_off(float_st1); + get_reg_off(float_st2); + get_reg_off(float_st3); + get_reg_off(float_st4); + get_reg_off(float_st5); + get_reg_off(float_st6); + get_reg_off(float_st7); + + get_reg_off(ES); + get_reg_off(CS); + get_reg_off(SS); + get_reg_off(DS); + get_reg_off(FS); + get_reg_off(GS); + + get_reg_off(MM0); + get_reg_off(MM1); + get_reg_off(MM2); + get_reg_off(MM3); + get_reg_off(MM4); + get_reg_off(MM5); + get_reg_off(MM6); + get_reg_off(MM7); + + get_reg_off(XMM0); + get_reg_off(XMM1); + get_reg_off(XMM2); + get_reg_off(XMM3); + get_reg_off(XMM4); + get_reg_off(XMM5); + get_reg_off(XMM6); + get_reg_off(XMM7); + get_reg_off(XMM8); + get_reg_off(XMM9); + get_reg_off(XMM10); + get_reg_off(XMM11); + get_reg_off(XMM12); + get_reg_off(XMM13); + get_reg_off(XMM14); + get_reg_off(XMM15); + + get_reg_off(tsc); + + get_reg_off(interrupt_num); + get_reg_off(exception_flags); + + get_reg_off(float_stack_ptr); + get_reg_off(reg_float_cs); + get_reg_off(reg_float_eip); + get_reg_off(reg_float_control); + + return dict; } static PyGetSetDef JitCpu_getseters[] = { - {"vmmngr", - (getter)JitCpu_get_vmmngr, (setter)JitCpu_set_vmmngr, - "vmmngr", - NULL}, - - {"jitter", - (getter)JitCpu_get_jitter, (setter)JitCpu_set_jitter, - "jitter", - NULL}, - - - {"RAX", (getter)JitCpu_get_RAX, (setter)JitCpu_set_RAX, "RAX", NULL}, - {"RBX", (getter)JitCpu_get_RBX, (setter)JitCpu_set_RBX, "RBX", NULL}, - {"RCX", (getter)JitCpu_get_RCX, (setter)JitCpu_set_RCX, "RCX", NULL}, - {"RDX", (getter)JitCpu_get_RDX, (setter)JitCpu_set_RDX, "RDX", NULL}, - {"RSI", (getter)JitCpu_get_RSI, (setter)JitCpu_set_RSI, "RSI", NULL}, - {"RDI", (getter)JitCpu_get_RDI, (setter)JitCpu_set_RDI, "RDI", NULL}, - {"RSP", (getter)JitCpu_get_RSP, (setter)JitCpu_set_RSP, "RSP", NULL}, - {"RBP", (getter)JitCpu_get_RBP, (setter)JitCpu_set_RBP, "RBP", NULL}, - {"R8", (getter)JitCpu_get_R8, (setter)JitCpu_set_R8, "R8", NULL}, - {"R9", (getter)JitCpu_get_R9, (setter)JitCpu_set_R9, "R9", NULL}, - {"R10", (getter)JitCpu_get_R10, (setter)JitCpu_set_R10, "R10", NULL}, - {"R11", (getter)JitCpu_get_R11, (setter)JitCpu_set_R11, "R11", NULL}, - {"R12", (getter)JitCpu_get_R12, (setter)JitCpu_set_R12, "R12", NULL}, - {"R13", (getter)JitCpu_get_R13, (setter)JitCpu_set_R13, "R13", NULL}, - {"R14", (getter)JitCpu_get_R14, (setter)JitCpu_set_R14, "R14", NULL}, - {"R15", (getter)JitCpu_get_R15, (setter)JitCpu_set_R15, "R15", NULL}, - {"RIP", (getter)JitCpu_get_RIP, (setter)JitCpu_set_RIP, "RIP", NULL}, - {"zf", (getter)JitCpu_get_zf, (setter)JitCpu_set_zf, "zf", NULL}, - {"nf", (getter)JitCpu_get_nf, (setter)JitCpu_set_nf, "nf", NULL}, - {"pf", (getter)JitCpu_get_pf, (setter)JitCpu_set_pf, "pf", NULL}, - {"of", (getter)JitCpu_get_of, (setter)JitCpu_set_of, "of", NULL}, - {"cf", (getter)JitCpu_get_cf, (setter)JitCpu_set_cf, "cf", NULL}, - {"af", (getter)JitCpu_get_af, (setter)JitCpu_set_af, "af", NULL}, - {"df", (getter)JitCpu_get_df, (setter)JitCpu_set_df, "df", NULL}, - {"ES", (getter)JitCpu_get_ES, (setter)JitCpu_set_ES, "ES", NULL}, - {"CS", (getter)JitCpu_get_CS, (setter)JitCpu_set_CS, "CS", NULL}, - {"SS", (getter)JitCpu_get_SS, (setter)JitCpu_set_SS, "SS", NULL}, - {"DS", (getter)JitCpu_get_DS, (setter)JitCpu_set_DS, "DS", NULL}, - {"FS", (getter)JitCpu_get_FS, (setter)JitCpu_set_FS, "FS", NULL}, - {"GS", (getter)JitCpu_get_GS, (setter)JitCpu_set_GS, "GS", NULL}, - - {"EAX", (getter)JitCpu_get_EAX, (setter)JitCpu_set_EAX, "EAX", NULL}, - {"EBX", (getter)JitCpu_get_EBX, (setter)JitCpu_set_EBX, "EBX", NULL}, - {"ECX", (getter)JitCpu_get_ECX, (setter)JitCpu_set_ECX, "ECX", NULL}, - {"EDX", (getter)JitCpu_get_EDX, (setter)JitCpu_set_EDX, "EDX", NULL}, - {"ESI", (getter)JitCpu_get_ESI, (setter)JitCpu_set_ESI, "ESI", NULL}, - {"EDI", (getter)JitCpu_get_EDI, (setter)JitCpu_set_EDI, "EDI", NULL}, - {"ESP", (getter)JitCpu_get_ESP, (setter)JitCpu_set_ESP, "ESP", NULL}, - {"EBP", (getter)JitCpu_get_EBP, (setter)JitCpu_set_EBP, "EBP", NULL}, - {"EIP", (getter)JitCpu_get_EIP, (setter)JitCpu_set_EIP, "EIP", NULL}, - - {"AX", (getter)JitCpu_get_AX, (setter)JitCpu_set_AX, "AX", NULL}, - {"BX", (getter)JitCpu_get_BX, (setter)JitCpu_set_BX, "BX", NULL}, - {"CX", (getter)JitCpu_get_CX, (setter)JitCpu_set_CX, "CX", NULL}, - {"DX", (getter)JitCpu_get_DX, (setter)JitCpu_set_DX, "DX", NULL}, - {"SI", (getter)JitCpu_get_SI, (setter)JitCpu_set_SI, "SI", NULL}, - {"DI", (getter)JitCpu_get_DI, (setter)JitCpu_set_DI, "DI", NULL}, - {"SP", (getter)JitCpu_get_SP, (setter)JitCpu_set_SP, "SP", NULL}, - {"BP", (getter)JitCpu_get_BP, (setter)JitCpu_set_BP, "BP", NULL}, - - {"IP", (getter)JitCpu_get_IP, (setter)JitCpu_set_IP, "IP", NULL}, - - {"MM0", (getter)JitCpu_get_MM0, (setter)JitCpu_set_MM0, "MM0", NULL}, - {"MM1", (getter)JitCpu_get_MM1, (setter)JitCpu_set_MM1, "MM1", NULL}, - {"MM2", (getter)JitCpu_get_MM2, (setter)JitCpu_set_MM2, "MM2", NULL}, - {"MM3", (getter)JitCpu_get_MM3, (setter)JitCpu_set_MM3, "MM3", NULL}, - {"MM4", (getter)JitCpu_get_MM4, (setter)JitCpu_set_MM4, "MM4", NULL}, - {"MM5", (getter)JitCpu_get_MM5, (setter)JitCpu_set_MM5, "MM5", NULL}, - {"MM6", (getter)JitCpu_get_MM6, (setter)JitCpu_set_MM6, "MM6", NULL}, - {"MM7", (getter)JitCpu_get_MM7, (setter)JitCpu_set_MM7, "MM7", NULL}, - - {"XMM0", (getter)JitCpu_get_XMM0, (setter)JitCpu_set_XMM0, "XMM0", NULL}, - {"XMM1", (getter)JitCpu_get_XMM1, (setter)JitCpu_set_XMM1, "XMM1", NULL}, - {"XMM2", (getter)JitCpu_get_XMM2, (setter)JitCpu_set_XMM2, "XMM2", NULL}, - {"XMM3", (getter)JitCpu_get_XMM3, (setter)JitCpu_set_XMM3, "XMM3", NULL}, - {"XMM4", (getter)JitCpu_get_XMM4, (setter)JitCpu_set_XMM4, "XMM4", NULL}, - {"XMM5", (getter)JitCpu_get_XMM5, (setter)JitCpu_set_XMM5, "XMM5", NULL}, - {"XMM6", (getter)JitCpu_get_XMM6, (setter)JitCpu_set_XMM6, "XMM6", NULL}, - {"XMM7", (getter)JitCpu_get_XMM7, (setter)JitCpu_set_XMM7, "XMM7", NULL}, - {"XMM8", (getter)JitCpu_get_XMM8, (setter)JitCpu_set_XMM8, "XMM8", NULL}, - {"XMM9", (getter)JitCpu_get_XMM9, (setter)JitCpu_set_XMM9, "XMM9", NULL}, - {"XMM10", (getter)JitCpu_get_XMM10, (setter)JitCpu_set_XMM10, "XMM10", NULL}, - {"XMM11", (getter)JitCpu_get_XMM11, (setter)JitCpu_set_XMM11, "XMM11", NULL}, - {"XMM12", (getter)JitCpu_get_XMM12, (setter)JitCpu_set_XMM12, "XMM12", NULL}, - {"XMM13", (getter)JitCpu_get_XMM13, (setter)JitCpu_set_XMM13, "XMM13", NULL}, - {"XMM14", (getter)JitCpu_get_XMM14, (setter)JitCpu_set_XMM14, "XMM14", NULL}, - {"XMM15", (getter)JitCpu_get_XMM15, (setter)JitCpu_set_XMM15, "XMM15", NULL}, - - {"tsc", (getter)JitCpu_get_tsc, (setter)JitCpu_set_tsc, "tsc", NULL}, - - {"exception_flags", (getter)JitCpu_get_exception_flags, (setter)JitCpu_set_exception_flags, "exception_flags", NULL}, - {"interrupt_num", (getter)JitCpu_get_interrupt_num, (setter)JitCpu_set_interrupt_num, "interrupt_num", NULL}, + {"vmmngr", + (getter)JitCpu_get_vmmngr, (setter)JitCpu_set_vmmngr, + "vmmngr", + NULL}, + + {"jitter", + (getter)JitCpu_get_jitter, (setter)JitCpu_set_jitter, + "jitter", + NULL}, + + {"RAX", (getter)JitCpu_get_RAX, (setter)JitCpu_set_RAX, "RAX", NULL}, + {"RBX", (getter)JitCpu_get_RBX, (setter)JitCpu_set_RBX, "RBX", NULL}, + {"RCX", (getter)JitCpu_get_RCX, (setter)JitCpu_set_RCX, "RCX", NULL}, + {"RDX", (getter)JitCpu_get_RDX, (setter)JitCpu_set_RDX, "RDX", NULL}, + {"RSI", (getter)JitCpu_get_RSI, (setter)JitCpu_set_RSI, "RSI", NULL}, + {"RDI", (getter)JitCpu_get_RDI, (setter)JitCpu_set_RDI, "RDI", NULL}, + {"RSP", (getter)JitCpu_get_RSP, (setter)JitCpu_set_RSP, "RSP", NULL}, + {"RBP", (getter)JitCpu_get_RBP, (setter)JitCpu_set_RBP, "RBP", NULL}, + {"R8", (getter)JitCpu_get_R8, (setter)JitCpu_set_R8, "R8", NULL}, + {"R9", (getter)JitCpu_get_R9, (setter)JitCpu_set_R9, "R9", NULL}, + {"R10", (getter)JitCpu_get_R10, (setter)JitCpu_set_R10, "R10", NULL}, + {"R11", (getter)JitCpu_get_R11, (setter)JitCpu_set_R11, "R11", NULL}, + {"R12", (getter)JitCpu_get_R12, (setter)JitCpu_set_R12, "R12", NULL}, + {"R13", (getter)JitCpu_get_R13, (setter)JitCpu_set_R13, "R13", NULL}, + {"R14", (getter)JitCpu_get_R14, (setter)JitCpu_set_R14, "R14", NULL}, + {"R15", (getter)JitCpu_get_R15, (setter)JitCpu_set_R15, "R15", NULL}, + {"RIP", (getter)JitCpu_get_RIP, (setter)JitCpu_set_RIP, "RIP", NULL}, + {"zf", (getter)JitCpu_get_zf, (setter)JitCpu_set_zf, "zf", NULL}, + {"nf", (getter)JitCpu_get_nf, (setter)JitCpu_set_nf, "nf", NULL}, + {"pf", (getter)JitCpu_get_pf, (setter)JitCpu_set_pf, "pf", NULL}, + {"of", (getter)JitCpu_get_of, (setter)JitCpu_set_of, "of", NULL}, + {"cf", (getter)JitCpu_get_cf, (setter)JitCpu_set_cf, "cf", NULL}, + {"af", (getter)JitCpu_get_af, (setter)JitCpu_set_af, "af", NULL}, + {"df", (getter)JitCpu_get_df, (setter)JitCpu_set_df, "df", NULL}, + {"ES", (getter)JitCpu_get_ES, (setter)JitCpu_set_ES, "ES", NULL}, + {"CS", (getter)JitCpu_get_CS, (setter)JitCpu_set_CS, "CS", NULL}, + {"SS", (getter)JitCpu_get_SS, (setter)JitCpu_set_SS, "SS", NULL}, + {"DS", (getter)JitCpu_get_DS, (setter)JitCpu_set_DS, "DS", NULL}, + {"FS", (getter)JitCpu_get_FS, (setter)JitCpu_set_FS, "FS", NULL}, + {"GS", (getter)JitCpu_get_GS, (setter)JitCpu_set_GS, "GS", NULL}, + + {"EAX", (getter)JitCpu_get_EAX, (setter)JitCpu_set_EAX, "EAX", NULL}, + {"EBX", (getter)JitCpu_get_EBX, (setter)JitCpu_set_EBX, "EBX", NULL}, + {"ECX", (getter)JitCpu_get_ECX, (setter)JitCpu_set_ECX, "ECX", NULL}, + {"EDX", (getter)JitCpu_get_EDX, (setter)JitCpu_set_EDX, "EDX", NULL}, + {"ESI", (getter)JitCpu_get_ESI, (setter)JitCpu_set_ESI, "ESI", NULL}, + {"EDI", (getter)JitCpu_get_EDI, (setter)JitCpu_set_EDI, "EDI", NULL}, + {"ESP", (getter)JitCpu_get_ESP, (setter)JitCpu_set_ESP, "ESP", NULL}, + {"EBP", (getter)JitCpu_get_EBP, (setter)JitCpu_set_EBP, "EBP", NULL}, + {"EIP", (getter)JitCpu_get_EIP, (setter)JitCpu_set_EIP, "EIP", NULL}, + + {"AX", (getter)JitCpu_get_AX, (setter)JitCpu_set_AX, "AX", NULL}, + {"BX", (getter)JitCpu_get_BX, (setter)JitCpu_set_BX, "BX", NULL}, + {"CX", (getter)JitCpu_get_CX, (setter)JitCpu_set_CX, "CX", NULL}, + {"DX", (getter)JitCpu_get_DX, (setter)JitCpu_set_DX, "DX", NULL}, + {"SI", (getter)JitCpu_get_SI, (setter)JitCpu_set_SI, "SI", NULL}, + {"DI", (getter)JitCpu_get_DI, (setter)JitCpu_set_DI, "DI", NULL}, + {"SP", (getter)JitCpu_get_SP, (setter)JitCpu_set_SP, "SP", NULL}, + {"BP", (getter)JitCpu_get_BP, (setter)JitCpu_set_BP, "BP", NULL}, + + {"IP", (getter)JitCpu_get_IP, (setter)JitCpu_set_IP, "IP", NULL}, + + {"MM0", (getter)JitCpu_get_MM0, (setter)JitCpu_set_MM0, "MM0", NULL}, + {"MM1", (getter)JitCpu_get_MM1, (setter)JitCpu_set_MM1, "MM1", NULL}, + {"MM2", (getter)JitCpu_get_MM2, (setter)JitCpu_set_MM2, "MM2", NULL}, + {"MM3", (getter)JitCpu_get_MM3, (setter)JitCpu_set_MM3, "MM3", NULL}, + {"MM4", (getter)JitCpu_get_MM4, (setter)JitCpu_set_MM4, "MM4", NULL}, + {"MM5", (getter)JitCpu_get_MM5, (setter)JitCpu_set_MM5, "MM5", NULL}, + {"MM6", (getter)JitCpu_get_MM6, (setter)JitCpu_set_MM6, "MM6", NULL}, + {"MM7", (getter)JitCpu_get_MM7, (setter)JitCpu_set_MM7, "MM7", NULL}, + + {"XMM0", (getter)JitCpu_get_XMM0, (setter)JitCpu_set_XMM0, "XMM0", NULL}, + {"XMM1", (getter)JitCpu_get_XMM1, (setter)JitCpu_set_XMM1, "XMM1", NULL}, + {"XMM2", (getter)JitCpu_get_XMM2, (setter)JitCpu_set_XMM2, "XMM2", NULL}, + {"XMM3", (getter)JitCpu_get_XMM3, (setter)JitCpu_set_XMM3, "XMM3", NULL}, + {"XMM4", (getter)JitCpu_get_XMM4, (setter)JitCpu_set_XMM4, "XMM4", NULL}, + {"XMM5", (getter)JitCpu_get_XMM5, (setter)JitCpu_set_XMM5, "XMM5", NULL}, + {"XMM6", (getter)JitCpu_get_XMM6, (setter)JitCpu_set_XMM6, "XMM6", NULL}, + {"XMM7", (getter)JitCpu_get_XMM7, (setter)JitCpu_set_XMM7, "XMM7", NULL}, + {"XMM8", (getter)JitCpu_get_XMM8, (setter)JitCpu_set_XMM8, "XMM8", NULL}, + {"XMM9", (getter)JitCpu_get_XMM9, (setter)JitCpu_set_XMM9, "XMM9", NULL}, + {"XMM10", (getter)JitCpu_get_XMM10, (setter)JitCpu_set_XMM10, "XMM10", NULL}, + {"XMM11", (getter)JitCpu_get_XMM11, (setter)JitCpu_set_XMM11, "XMM11", NULL}, + {"XMM12", (getter)JitCpu_get_XMM12, (setter)JitCpu_set_XMM12, "XMM12", NULL}, + {"XMM13", (getter)JitCpu_get_XMM13, (setter)JitCpu_set_XMM13, "XMM13", NULL}, + {"XMM14", (getter)JitCpu_get_XMM14, (setter)JitCpu_set_XMM14, "XMM14", NULL}, + {"XMM15", (getter)JitCpu_get_XMM15, (setter)JitCpu_set_XMM15, "XMM15", NULL}, + + {"tsc", (getter)JitCpu_get_tsc, (setter)JitCpu_set_tsc, "tsc", NULL}, + + {"exception_flags", (getter)JitCpu_get_exception_flags, (setter)JitCpu_set_exception_flags, "exception_flags", NULL}, + {"interrupt_num", (getter)JitCpu_get_interrupt_num, (setter)JitCpu_set_interrupt_num, "interrupt_num", NULL}, - {NULL} /* Sentinel */ + {NULL} /* Sentinel */ }; - static PyTypeObject JitCpuType = { - PyVarObject_HEAD_INIT(NULL, 0) - "JitCore_x86.JitCpu", /*tp_name*/ - sizeof(JitCpu), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)JitCpu_dealloc,/*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "JitCpu objects", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - JitCpu_methods, /* tp_methods */ - JitCpu_members, /* tp_members */ - JitCpu_getseters, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)JitCpu_init, /* tp_init */ - 0, /* tp_alloc */ - JitCpu_new, /* tp_new */ + PyVarObject_HEAD_INIT(NULL, 0) + "JitCore_x86.JitCpu", /*tp_name*/ + sizeof(JitCpu), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)JitCpu_dealloc,/*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "JitCpu objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + JitCpu_methods, /* tp_methods */ + JitCpu_members, /* tp_members */ + JitCpu_getseters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)JitCpu_init, /* tp_init */ + 0, /* tp_alloc */ + JitCpu_new, /* tp_new */ }; - - static PyMethodDef JitCore_x86_Methods[] = { - - /* - - */ {"get_gpreg_offset_all", (PyCFunction)get_gpreg_offset_all, METH_NOARGS}, {NULL, NULL, 0, NULL} /* Sentinel */ - }; - MOD_INIT(JitCore_x86) { PyObject *module = NULL; MOD_DEF(module, "JitCore_x86", "JitCore_x86 module", JitCore_x86_Methods); - if (module == NULL) + if (module == NULL) { RET_MODULE; - - if (PyType_Ready(&JitCpuType) < 0) + } + if (PyType_Ready(&JitCpuType) < 0) { RET_MODULE; - + } Py_INCREF(&JitCpuType); - if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) + if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0) { RET_MODULE; - + } RET_MODULE; } diff --git a/miasm/jitter/compat_py23.h b/miasm/jitter/compat_py23.h index 88c9fb5c6..2030c7e98 100644 --- a/miasm/jitter/compat_py23.h +++ b/miasm/jitter/compat_py23.h @@ -48,7 +48,7 @@ } \ value = (size_type) tmp; \ } \ - else if (PyLong_Check(item)){ \ + else if (PyLong_Check(item)) { \ unsigned long long tmp; \ tmp = PyLong_AsUnsignedLongLong(item); \ if ( tmp > (size_type) -1) { \ @@ -71,7 +71,7 @@ } \ value = (size_type) tmp; \ } \ - else if (PyLong_Check(item)){ \ + else if (PyLong_Check(item)) { \ unsigned long long tmp; \ tmp = PyLong_AsUnsignedLongLong(item); \ if ( tmp > (size_type) -1) { \ diff --git a/miasm/jitter/op_semantics.c b/miasm/jitter/op_semantics.c index 8682d27a6..fa9dd3f9c 100644 --- a/miasm/jitter/op_semantics.c +++ b/miasm/jitter/op_semantics.c @@ -93,10 +93,12 @@ unsigned int mul_lo_op(unsigned int size, unsigned int a, unsigned int b) unsigned int mask; switch (size) { - case 8: mask = 0xff; break; - case 16: mask = 0xffff; break; - case 32: mask = 0xffffffff; break; - default: fprintf(stderr, "inv size in mul %d\n", size); exit(EXIT_FAILURE); + case 8: mask = 0xff; break; + case 16: mask = 0xffff; break; + case 32: mask = 0xffffffff; break; + default: + fprintf(stderr, "inv size in mul %d\n", size); + exit(EXIT_FAILURE); } a &= mask; @@ -110,10 +112,12 @@ unsigned int mul_hi_op(unsigned int size, unsigned int a, unsigned int b) unsigned int mask; switch (size) { - case 8: mask = 0xff; break; - case 16: mask = 0xffff; break; - case 32: mask = 0xffffffff; break; - default: fprintf(stderr, "inv size in mul %d\n", size); exit(EXIT_FAILURE); + case 8: mask = 0xff; break; + case 16: mask = 0xffff; break; + case 32: mask = 0xffffffff; break; + default: + fprintf(stderr, "inv size in mul %d\n", size); + exit(EXIT_FAILURE); } a &= mask; @@ -177,74 +181,74 @@ uint64_t rot_left(uint64_t size, uint64_t a, uint64_t b) b = b & 0x3F; b %= size; - switch(size){ - case 8: - tmp = (a << b) | ((a & 0xFF) >> (size - b)); - return tmp & 0xFF; - case 16: - tmp = (a << b) | ((a & 0xFFFF) >> (size - b)); - return tmp & 0xFFFF; - case 32: - tmp = (a << b) | ((a & 0xFFFFFFFF) >> (size - b)); - return tmp & 0xFFFFFFFF; - case 64: - tmp = (a << b) | ((a&0xFFFFFFFFFFFFFFFF) >> (size - b)); - return tmp & 0xFFFFFFFFFFFFFFFF; - - /* Support cases for rcl */ - case 9: - tmp = (a << b) | ((a & 0x1FF) >> (size - b)); - return tmp & 0x1FF; - case 17: - tmp = (a << b) | ((a & 0x1FFFF) >> (size - b)); - return tmp & 0x1FFFF; - case 33: - tmp = (a << b) | ((a & 0x1FFFFFFFF) >> (size - b)); - return tmp & 0x1FFFFFFFF; - /* TODO XXX: support rcl in 64 bit mode */ - - default: - fprintf(stderr, "inv size in rotleft %"PRIX64"\n", size); - exit(EXIT_FAILURE); + switch(size) { + case 8: + tmp = (a << b) | ((a & 0xFF) >> (size - b)); + return tmp & 0xFF; + case 16: + tmp = (a << b) | ((a & 0xFFFF) >> (size - b)); + return tmp & 0xFFFF; + case 32: + tmp = (a << b) | ((a & 0xFFFFFFFF) >> (size - b)); + return tmp & 0xFFFFFFFF; + case 64: + tmp = (a << b) | ((a&0xFFFFFFFFFFFFFFFF) >> (size - b)); + return tmp & 0xFFFFFFFFFFFFFFFF; + + /* Support cases for rcl */ + case 9: + tmp = (a << b) | ((a & 0x1FF) >> (size - b)); + return tmp & 0x1FF; + case 17: + tmp = (a << b) | ((a & 0x1FFFF) >> (size - b)); + return tmp & 0x1FFFF; + case 33: + tmp = (a << b) | ((a & 0x1FFFFFFFF) >> (size - b)); + return tmp & 0x1FFFFFFFF; + /* TODO XXX: support rcl in 64 bit mode */ + + default: + fprintf(stderr, "inv size in rotleft %"PRIX64"\n", size); + exit(EXIT_FAILURE); } } uint64_t rot_right(uint64_t size, uint64_t a, uint64_t b) { - uint64_t tmp; + uint64_t tmp; - b = b & 0x3F; - b %= size; - switch(size){ - case 8: - tmp = ((a & 0xFF) >> b) | (a << (size - b)); - return tmp & 0xff; - case 16: - tmp = ((a & 0xFFFF) >> b) | (a << (size - b)); - return tmp & 0xFFFF; - case 32: - tmp = ((a & 0xFFFFFFFF) >> b) | (a << (size - b)); - return tmp & 0xFFFFFFFF; - case 64: - tmp = ((a & 0xFFFFFFFFFFFFFFFF) >> b) | (a << (size - b)); - return tmp & 0xFFFFFFFFFFFFFFFF; - - /* Support cases for rcr */ - case 9: - tmp = ((a & 0x1FF) >> b) | (a << (size - b)); - return tmp & 0x1FF; - case 17: - tmp = ((a & 0x1FFFF) >> b) | (a << (size - b)); - return tmp & 0x1FFFF; - case 33: - tmp = ((a & 0x1FFFFFFFF) >> b) | (a << (size - b)); - return tmp & 0x1FFFFFFFF; - /* TODO XXX: support rcr in 64 bit mode */ - - default: - fprintf(stderr, "inv size in rotright %"PRIX64"\n", size); - exit(EXIT_FAILURE); - } + b = b & 0x3F; + b %= size; + switch (size) { + case 8: + tmp = ((a & 0xFF) >> b) | (a << (size - b)); + return tmp & 0xff; + case 16: + tmp = ((a & 0xFFFF) >> b) | (a << (size - b)); + return tmp & 0xFFFF; + case 32: + tmp = ((a & 0xFFFFFFFF) >> b) | (a << (size - b)); + return tmp & 0xFFFFFFFF; + case 64: + tmp = ((a & 0xFFFFFFFFFFFFFFFF) >> b) | (a << (size - b)); + return tmp & 0xFFFFFFFFFFFFFFFF; + + /* Support cases for rcr */ + case 9: + tmp = ((a & 0x1FF) >> b) | (a << (size - b)); + return tmp & 0x1FF; + case 17: + tmp = ((a & 0x1FFFF) >> b) | (a << (size - b)); + return tmp & 0x1FFFF; + case 33: + tmp = ((a & 0x1FFFFFFFF) >> b) | (a << (size - b)); + return tmp & 0x1FFFFFFFF; + /* TODO XXX: support rcr in 64 bit mode */ + + default: + fprintf(stderr, "inv size in rotright %"PRIX64"\n", size); + exit(EXIT_FAILURE); + } } /* @@ -259,7 +263,7 @@ uint64_t cntleadzeros(uint64_t size, uint64_t src) { int64_t i; - for (i=(int64_t)size-1; i>=0; i--){ + for (i=(int64_t)size-1; i>=0; i--) { if (src & (1ull << i)) return (uint64_t)(size - (i + 1)); } @@ -277,7 +281,7 @@ uint64_t cntleadzeros(uint64_t size, uint64_t src) unsigned int cnttrailzeros(uint64_t size, uint64_t src) { uint64_t i; - for (i=0; i3){ + if (reg_num >3) { fprintf(stderr, "not implemented x86_cpuid reg %x\n", reg_num); exit(EXIT_FAILURE); } // cases are output: EAX: 0; EBX: 1; ECX: 2; EDX: 3 - if (a == 0){ - switch(reg_num){ + if (a == 0) { + switch(reg_num) { case 0: return 0xa; // "GenuineIntel" @@ -319,8 +321,8 @@ unsigned int x86_cpuid(unsigned int a, unsigned int reg_num) } } - else if (a == 1){ - switch(reg_num){ + else if (a == 1) { + switch(reg_num) { case 0: // Using a version too high will enable recent // instruction set @@ -344,8 +346,8 @@ unsigned int x86_cpuid(unsigned int a, unsigned int reg_num) } } // Cache and TLB - else if (a == 2){ - switch(reg_num){ + else if (a == 2) { + switch(reg_num) { case 0: return 0x00000000; case 1: @@ -357,8 +359,8 @@ unsigned int x86_cpuid(unsigned int a, unsigned int reg_num) } } // Intel thread/core and cache topology - else if (a == 4){ - switch(reg_num){ + else if (a == 4) { + switch(reg_num) { case 0: return 0x00000000; case 1: @@ -370,8 +372,8 @@ unsigned int x86_cpuid(unsigned int a, unsigned int reg_num) } } // Extended features - else if (a == 7){ - switch(reg_num){ + else if (a == 7) { + switch(reg_num) { case 0: return 0x00000000; case 1: @@ -381,8 +383,7 @@ unsigned int x86_cpuid(unsigned int a, unsigned int reg_num) case 3: return 0x00000000; } - } - else{ + } else { fprintf(stderr, "WARNING not implemented x86_cpuid index %X!\n", a); exit(EXIT_FAILURE); } diff --git a/miasm/jitter/vm_mngr.c b/miasm/jitter/vm_mngr.c index 1d448bb24..de4f375da 100644 --- a/miasm/jitter/vm_mngr.c +++ b/miasm/jitter/vm_mngr.c @@ -51,9 +51,9 @@ void memory_access_list_reset(struct memory_access_list * access) void memory_access_list_add(struct memory_access_list * access, uint64_t start, uint64_t stop) { if (access->num >= access->allocated) { - if (access->allocated == 0) + 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); @@ -71,30 +71,25 @@ void memory_access_list_add(struct memory_access_list * access, uint64_t start, access->num += 1; } - - uint16_t set_endian16(vm_mngr_t* vm_mngr, uint16_t val) { if (vm_mngr->sex == __BYTE_ORDER) return val; - else - return Endian16_Swap(val); + return Endian16_Swap(val); } uint32_t set_endian32(vm_mngr_t* vm_mngr, uint32_t val) { if (vm_mngr->sex == __BYTE_ORDER) return val; - else - return Endian32_Swap(val); + return Endian32_Swap(val); } uint64_t set_endian64(vm_mngr_t* vm_mngr, uint64_t val) { if (vm_mngr->sex == __BYTE_ORDER) return val; - else - return Endian64_Swap(val); + return Endian64_Swap(val); } void print_val(uint64_t base, uint64_t addr) @@ -162,14 +157,14 @@ static uint64_t memory_page_read(vm_mngr_t* vm_mngr, unsigned int my_size, uint6 if (!mpn) return 0; - if ((mpn->access & PAGE_READ) == 0){ + if ((mpn->access & PAGE_READ) == 0) { fprintf(stderr, "access to non readable page!! %"PRIX64"\n", ad); vm_mngr->exception_flags |= EXCEPT_ACCESS_VIOL; return 0; } /* check read breakpoint */ - LIST_FOREACH(b, &vm_mngr->memory_breakpoint_pool, next){ + LIST_FOREACH(b, &vm_mngr->memory_breakpoint_pool, next) { if ((b->access & BREAKPOINT_READ) == 0) continue; if ((b->ad <= ad) && (ad < b->ad + b->size)) @@ -180,8 +175,8 @@ static uint64_t memory_page_read(vm_mngr_t* vm_mngr, unsigned int my_size, uint6 addr = &((unsigned char*)mpn->ad_hp)[ad - mpn->ad]; /* read fits in a page */ - if (ad - mpn->ad + my_size/8 <= mpn->size){ - switch(my_size){ + if (ad - mpn->ad + my_size/8 <= mpn->size) { + switch (my_size) { case 8: ret = *((unsigned char*)addr)&0xFF; break; @@ -207,7 +202,7 @@ static uint64_t memory_page_read(vm_mngr_t* vm_mngr, unsigned int my_size, uint6 else{ unsigned int new_size = my_size; int index = 0; - while (new_size){ + while (new_size) { mpn = get_memory_page_from_address(vm_mngr, ad, 1); if (!mpn) return 0; @@ -250,14 +245,14 @@ static void memory_page_write(vm_mngr_t* vm_mngr, unsigned int my_size, if (!mpn) return; - if ((mpn->access & PAGE_WRITE) == 0){ + if ((mpn->access & PAGE_WRITE) == 0) { fprintf(stderr, "access to non writable page!! %"PRIX64"\n", ad); vm_mngr->exception_flags |= EXCEPT_ACCESS_VIOL; return ; } /* check read breakpoint*/ - LIST_FOREACH(b, &vm_mngr->memory_breakpoint_pool, next){ + LIST_FOREACH(b, &vm_mngr->memory_breakpoint_pool, next) { if ((b->access & BREAKPOINT_WRITE) == 0) continue; if ((b->ad <= ad) && (ad < b->ad + b->size)) @@ -267,8 +262,8 @@ static void memory_page_write(vm_mngr_t* vm_mngr, unsigned int my_size, addr = &((unsigned char*)mpn->ad_hp)[ad - mpn->ad]; /* write fits in a page */ - if (ad - mpn->ad + my_size/8 <= mpn->size){ - switch(my_size){ + if (ad - mpn->ad + my_size/8 <= mpn->size) { + switch (my_size) { case 8: *((unsigned char*)addr) = src&0xFF; break; @@ -311,7 +306,7 @@ static void memory_page_write(vm_mngr_t* vm_mngr, unsigned int my_size, exit(EXIT_FAILURE); break; } - while (my_size){ + while (my_size) { mpn = get_memory_page_from_address(vm_mngr, ad, 1); if (!mpn) return; @@ -330,7 +325,7 @@ static void memory_page_write(vm_mngr_t* vm_mngr, unsigned int my_size, void dump_code_bloc(vm_mngr_t* vm_mngr) { struct code_bloc_node * cbp; - LIST_FOREACH(cbp, &vm_mngr->code_bloc_pool, next){ + LIST_FOREACH(cbp, &vm_mngr->code_bloc_pool, next) { fprintf(stderr, "%"PRIX64"%"PRIX64"\n", cbp->ad_start, cbp->ad_stop); } @@ -377,9 +372,9 @@ void check_invalid_code_blocs(vm_mngr_t* vm_mngr) vm_mngr->memory_w.array[i].start >=vm_mngr->code_bloc_pool_ad_max) continue; - LIST_FOREACH(cbp, &vm_mngr->code_bloc_pool, next){ + LIST_FOREACH(cbp, &vm_mngr->code_bloc_pool, next) { if ((cbp->ad_start < vm_mngr->memory_w.array[i].stop) && - (vm_mngr->memory_w.array[i].start < cbp->ad_stop)){ + (vm_mngr->memory_w.array[i].start < cbp->ad_stop)) { #ifdef DEBUG_MIASM_AUTOMOD_CODE fprintf(stderr, "**********************************\n"); fprintf(stderr, "self modifying code %"PRIX64" %"PRIX64"\n", @@ -512,15 +507,15 @@ int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, size_t siz buffer = malloc(size); *buffer_ptr = buffer; - if (!buffer){ + if (!buffer) { fprintf(stderr, "Error: cannot alloc read\n"); exit(EXIT_FAILURE); } /* read is multiple page wide */ - while (size){ + while (size) { mpn = get_memory_page_from_address(vm_mngr, addr, 1); - if (!mpn){ + if (!mpn) { free(*buffer_ptr); PyErr_SetString(PyExc_RuntimeError, "Error: cannot find address"); return -1; @@ -555,9 +550,9 @@ int vm_write_mem(vm_mngr_t* vm_mngr, uint64_t addr, char *buffer, size_t size) } /* write is multiple page wide */ - while (size){ + while (size) { mpn = get_memory_page_from_address(vm_mngr, addr, 1); - if (!mpn){ + if (!mpn) { PyErr_SetString(PyExc_RuntimeError, "Error: cannot find address"); return -1; } @@ -591,7 +586,7 @@ int is_mapped(vm_mngr_t* vm_mngr, uint64_t addr, size_t size) } /* test multiple page wide */ - while (size){ + while (size) { mpn = get_memory_page_from_address(vm_mngr, addr, 0); if (!mpn) return 0; @@ -616,18 +611,18 @@ struct memory_page_node * create_memory_page_node(uint64_t ad, unsigned int size void* ad_hp; mpn = malloc(sizeof(*mpn)); - if (!mpn){ + if (!mpn) { fprintf(stderr, "Error: cannot alloc mpn\n"); return NULL; } ad_hp = malloc(size); - if (!ad_hp){ + if (!ad_hp) { free(mpn); fprintf(stderr, "Error: cannot alloc %d\n", size); return NULL; } mpn->name = malloc(strlen(name) + 1); - if (!mpn->name){ + if (!mpn->name) { free(mpn); free(ad_hp); fprintf(stderr, "Error: cannot alloc\n"); @@ -643,13 +638,12 @@ struct memory_page_node * create_memory_page_node(uint64_t ad, unsigned int size return mpn; } - struct code_bloc_node * create_code_bloc_node(uint64_t ad_start, uint64_t ad_stop) { struct code_bloc_node * cbp; cbp = malloc(sizeof(*cbp)); - if (!cbp){ + if (!cbp) { fprintf(stderr, "Error: cannot alloc cbp\n"); exit(EXIT_FAILURE); } @@ -748,18 +742,15 @@ void reset_memory_breakpoint(vm_mngr_t* vm_mngr) LIST_REMOVE(mpn, next); free(mpn); } - } - - /* We don't use dichotomy here for the insertion */ int is_mpn_in_tab(vm_mngr_t* vm_mngr, struct memory_page_node* mpn_a) { struct memory_page_node * mpn; int i; - for (i=0;imemory_pages_number; i++) { + for (i = 0; i < vm_mngr->memory_pages_number; i++) { mpn = &vm_mngr->memory_pages_array[i]; if (mpn->ad >= mpn_a->ad + mpn_a->size) continue; @@ -770,21 +761,18 @@ int is_mpn_in_tab(vm_mngr_t* vm_mngr, struct memory_page_node* mpn_a) "overlapping page (0x%"PRIX64" 0x%"PRIX64")\n", mpn_a->ad, mpn_a->ad + mpn_a->size, mpn->ad, mpn->ad + mpn->size); - return 1; } - return 0; } - /* We don't use dichotomy here for the insertion */ void add_memory_page(vm_mngr_t* vm_mngr, struct memory_page_node* mpn_a) { struct memory_page_node * mpn; int i; - for (i=0; i < vm_mngr->memory_pages_number; i++) { + for (i = 0; i < vm_mngr->memory_pages_number; i++) { mpn = &vm_mngr->memory_pages_array[i]; if (mpn->ad < mpn_a->ad) continue; @@ -861,7 +849,7 @@ void dump_memory_breakpoint_pool(vm_mngr_t* vm_mngr) { struct memory_breakpoint_info * mpn; - LIST_FOREACH(mpn, &vm_mngr->memory_breakpoint_pool, next){ + LIST_FOREACH(mpn, &vm_mngr->memory_breakpoint_pool, next) { printf("ad %"PRIX64" size %"PRIX64" access %"PRIX64"\n", mpn->ad, mpn->size, @@ -890,56 +878,52 @@ void remove_memory_breakpoint(vm_mngr_t* vm_mngr, uint64_t ad, unsigned int acce { struct memory_breakpoint_info * mpn; - LIST_FOREACH(mpn, &vm_mngr->memory_breakpoint_pool, next){ + LIST_FOREACH(mpn, &vm_mngr->memory_breakpoint_pool, next) { if (mpn->ad == ad && mpn->access == access) LIST_REMOVE(mpn, next); } } - /********************************************/ void hexdump(char* m, unsigned int l) { - unsigned int i, j, last; - last = 0; - for (i=0;i SIZE_MAX) { - fprintf(stderr, "Size too big\n"); - exit(EXIT_FAILURE); + fprintf(stderr, "Size too big\n"); + exit(EXIT_FAILURE); } buf_size_st = (size_t) buf_size; @@ -140,7 +139,7 @@ PyObject* vm_set_mem_access(VmMngr* self, PyObject* args) PyGetInt_uint64_t(access, page_access); mpn = get_memory_page_from_address(&self->vm_mngr, page_addr, 1); - if (!mpn){ + if (!mpn) { PyErr_SetString(PyExc_RuntimeError, "cannot find address"); return 0; } @@ -153,42 +152,40 @@ PyObject* vm_set_mem_access(VmMngr* self, PyObject* args) PyObject* vm_set_mem(VmMngr* self, PyObject* args) { - PyObject *py_addr; - PyObject *py_buffer; - Py_ssize_t py_length; + PyObject *py_addr; + PyObject *py_buffer; + Py_ssize_t py_length; - char * buffer; - Py_ssize_t pysize; - uint64_t addr; - int ret; + char * buffer; + Py_ssize_t pysize; + uint64_t addr; + int ret; - if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); - if (!PyBytes_Check(py_buffer)) - RAISE(PyExc_TypeError,"arg must be bytes"); + if (!PyBytes_Check(py_buffer)) + RAISE(PyExc_TypeError,"arg must be bytes"); - pysize = PyBytes_Size(py_buffer); - if (pysize < 0) { - RAISE(PyExc_TypeError,"Python error"); - } - PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length); + 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, pysize); - if (ret < 0) - RAISE(PyExc_TypeError, "Error in set_mem"); + 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_t)pysize); - check_invalid_code_blocs(&self->vm_mngr); + add_mem_write(&self->vm_mngr, addr, (size_t)pysize); + check_invalid_code_blocs(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } - - PyObject* vm_get_mem_access(VmMngr* self, PyObject* args) { PyObject *py_addr; @@ -201,7 +198,7 @@ PyObject* vm_get_mem_access(VmMngr* self, PyObject* args) PyGetInt_uint64_t(py_addr, page_addr); mpn = get_memory_page_from_address(&self->vm_mngr, page_addr, 1); - if (!mpn){ + if (!mpn) { PyErr_SetString(PyExc_RuntimeError, "cannot find address"); return 0; } @@ -211,258 +208,253 @@ PyObject* vm_get_mem_access(VmMngr* self, PyObject* args) PyObject* vm_get_mem(VmMngr* self, PyObject* args) { - PyObject *py_addr; - PyObject *py_len; - - uint64_t addr; - uint64_t size; - size_t size_st; - PyObject *obj_out; - char * buf_out; - int ret; + PyObject *py_addr; + PyObject *py_len; - if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_len)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + uint64_t addr; + uint64_t size; + size_t size_st; + PyObject *obj_out; + char * buf_out; + int ret; - PyGetInt_uint64_t(py_addr, addr); - PyGetInt_uint64_t(py_len, size); + if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_len)) { + RAISE(PyExc_TypeError,"Cannot parse arguments"); + } - if (size > SIZE_MAX) { - fprintf(stderr, "Size too big\n"); - exit(EXIT_FAILURE); - } - size_st = (size_t) size; + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_len, size); - ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, size_st); - if (ret < 0) { - RAISE(PyExc_RuntimeError,"Cannot find address"); - } + if (size > SIZE_MAX) { + fprintf(stderr, "Size too big\n"); + exit(EXIT_FAILURE); + } + size_st = (size_t) size; - obj_out = PyBytes_FromStringAndSize(buf_out, size_st); - free(buf_out); - return obj_out; + 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_st); + free(buf_out); + return obj_out; } PyObject* vm_get_u8(VmMngr* self, PyObject* args) { - PyObject *py_addr; + PyObject *py_addr; - uint64_t addr; - PyObject *obj_out; - char * buf_out; - int ret; - uint32_t value; + uint64_t addr; + PyObject *obj_out; + char * buf_out; + int ret; + uint32_t value; - if (!PyArg_ParseTuple(args, "O", &py_addr)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "O", &py_addr)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); - ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 1); - if (ret < 0) { - RAISE(PyExc_RuntimeError,"Cannot find address"); - } + ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 1); + if (ret < 0) { + RAISE(PyExc_RuntimeError,"Cannot find address"); + } - value = *(uint8_t*)buf_out; + value = *(uint8_t*)buf_out; - obj_out = PyLong_FromUnsignedLongLong(value); - free(buf_out); - return obj_out; + obj_out = PyLong_FromUnsignedLongLong(value); + free(buf_out); + return obj_out; } PyObject* vm_get_u16(VmMngr* self, PyObject* args) { - PyObject *py_addr; + PyObject *py_addr; - uint64_t addr; - PyObject *obj_out; - char * buf_out; - int ret; - uint16_t value; + uint64_t addr; + PyObject *obj_out; + char * buf_out; + int ret; + uint16_t value; - if (!PyArg_ParseTuple(args, "O", &py_addr)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "O", &py_addr)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); - ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 2); - if (ret < 0) { - RAISE(PyExc_RuntimeError,"Cannot find address"); - } + ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 2); + if (ret < 0) { + RAISE(PyExc_RuntimeError,"Cannot find address"); + } - value = set_endian16(&self->vm_mngr, *(uint16_t*)buf_out); + value = set_endian16(&self->vm_mngr, *(uint16_t*)buf_out); - obj_out = PyLong_FromUnsignedLongLong(value); - free(buf_out); - return obj_out; + obj_out = PyLong_FromUnsignedLongLong(value); + free(buf_out); + return obj_out; } PyObject* vm_get_u32(VmMngr* self, PyObject* args) { - PyObject *py_addr; + PyObject *py_addr; - uint64_t addr; - PyObject *obj_out; - char * buf_out; - int ret; - uint32_t value; + uint64_t addr; + PyObject *obj_out; + char * buf_out; + int ret; + uint32_t value; - if (!PyArg_ParseTuple(args, "O", &py_addr)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "O", &py_addr)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); - ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 4); - if (ret < 0) { - RAISE(PyExc_RuntimeError,"Cannot find address"); - } + ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 4); + if (ret < 0) { + RAISE(PyExc_RuntimeError,"Cannot find address"); + } - value = set_endian32(&self->vm_mngr, *(uint32_t*)buf_out); + value = set_endian32(&self->vm_mngr, *(uint32_t*)buf_out); - obj_out = PyLong_FromUnsignedLongLong(value); - free(buf_out); - return obj_out; + obj_out = PyLong_FromUnsignedLongLong(value); + free(buf_out); + return obj_out; } - PyObject* vm_get_u64(VmMngr* self, PyObject* args) { - PyObject *py_addr; + PyObject *py_addr; - uint64_t addr; - PyObject *obj_out; - char * buf_out; - int ret; - uint64_t value; + uint64_t addr; + PyObject *obj_out; + char * buf_out; + int ret; + uint64_t value; - if (!PyArg_ParseTuple(args, "O", &py_addr)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "O", &py_addr)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_addr, addr); - ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 8); - if (ret < 0) { - RAISE(PyExc_RuntimeError,"Cannot find address"); - } + ret = vm_read_mem(&self->vm_mngr, addr, &buf_out, 8); + if (ret < 0) { + RAISE(PyExc_RuntimeError,"Cannot find address"); + } - value = set_endian64(&self->vm_mngr, *(uint64_t*)buf_out); + value = set_endian64(&self->vm_mngr, *(uint64_t*)buf_out); - obj_out = PyLong_FromUnsignedLongLong(value); - free(buf_out); - return obj_out; + obj_out = PyLong_FromUnsignedLongLong(value); + free(buf_out); + return obj_out; } - PyObject* vm_set_u8(VmMngr* self, PyObject* args) { - PyObject *py_addr; - PyObject *py_val; - uint8_t value; - uint64_t addr; - int ret; + PyObject *py_addr; + PyObject *py_val; + uint8_t value; + uint64_t addr; + int ret; - if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); - PyGetInt_uint8_t(py_val, value); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint8_t(py_val, value); - ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 1); - if (ret < 0) - RAISE(PyExc_TypeError, "Error in set_mem"); + ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 1); + if (ret < 0) { + RAISE(PyExc_TypeError, "Error in set_mem"); + } - add_mem_write(&self->vm_mngr, addr, 1); - check_invalid_code_blocs(&self->vm_mngr); + add_mem_write(&self->vm_mngr, addr, 1); + check_invalid_code_blocs(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_set_u16(VmMngr* self, PyObject* args) { - PyObject *py_addr; - PyObject *py_val; - uint16_t value; + PyObject *py_addr; + PyObject *py_val; + uint16_t value; - uint64_t addr; - int ret; + uint64_t addr; + int ret; - if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); - PyGetInt_uint16_t(py_val, value); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint16_t(py_val, value); - 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"); + 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"); - add_mem_write(&self->vm_mngr, addr, 2); - check_invalid_code_blocs(&self->vm_mngr); + add_mem_write(&self->vm_mngr, addr, 2); + check_invalid_code_blocs(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_set_u32(VmMngr* self, PyObject* args) { - PyObject *py_addr; - PyObject *py_val; - uint32_t value; - uint64_t addr; - int ret; + PyObject *py_addr; + PyObject *py_val; + uint32_t value; + uint64_t addr; + int ret; - if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); - PyGetInt_uint32_t(py_val, value); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint32_t(py_val, value); - value = set_endian32(&self->vm_mngr, value); + value = set_endian32(&self->vm_mngr, value); - ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 4); - if (ret < 0) - RAISE(PyExc_TypeError, "Error in set_mem"); + ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 4); + if (ret < 0) + RAISE(PyExc_TypeError, "Error in set_mem"); - add_mem_write(&self->vm_mngr, addr, 4); - check_invalid_code_blocs(&self->vm_mngr); + add_mem_write(&self->vm_mngr, addr, 4); + check_invalid_code_blocs(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_set_u64(VmMngr* self, PyObject* args) { - PyObject *py_addr; - PyObject *py_val; - uint64_t value; - uint64_t addr; - int ret; + PyObject *py_addr; + PyObject *py_val; + uint64_t value; + uint64_t addr; + int ret; - if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) - RAISE(PyExc_TypeError,"Cannot parse arguments"); + if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_val)) + RAISE(PyExc_TypeError,"Cannot parse arguments"); - PyGetInt_uint64_t(py_addr, addr); - PyGetInt_uint64_t(py_val, value); + PyGetInt_uint64_t(py_addr, addr); + PyGetInt_uint64_t(py_val, value); - value = set_endian64(&self->vm_mngr, value); + value = set_endian64(&self->vm_mngr, value); - ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 8); - if (ret < 0) - RAISE(PyExc_TypeError, "Error in set_mem"); + ret = vm_write_mem(&self->vm_mngr, addr, (char*)&value, 8); + if (ret < 0) + RAISE(PyExc_TypeError, "Error in set_mem"); - add_mem_write(&self->vm_mngr, addr, 8); - check_invalid_code_blocs(&self->vm_mngr); + add_mem_write(&self->vm_mngr, addr, 8); + check_invalid_code_blocs(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } - - - - PyObject* vm_add_memory_breakpoint(VmMngr* self, PyObject* args) { PyObject *ad; @@ -531,45 +523,39 @@ PyObject* vm_get_exception(VmMngr* self, PyObject* args) return PyLong_FromUnsignedLongLong((uint64_t)self->vm_mngr.exception_flags); } - - - PyObject* vm_init_memory_page_pool(VmMngr* self, PyObject* args) { - init_memory_page_pool(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + init_memory_page_pool(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_init_code_bloc_pool(VmMngr* self, PyObject* args) { - init_code_bloc_pool(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; - + init_code_bloc_pool(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_init_memory_breakpoint(VmMngr* self, PyObject* args) { - init_memory_breakpoint(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; - + init_memory_breakpoint(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_reset_memory_breakpoint(VmMngr* self, PyObject* args) { - reset_memory_breakpoint(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; - + reset_memory_breakpoint(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_reset_memory_access(VmMngr* self, PyObject* args) { - reset_memory_access(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + reset_memory_access(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } PyObject* py_add_mem_read(VmMngr* self, PyObject* args) @@ -610,16 +596,16 @@ PyObject* py_add_mem_write(VmMngr* self, PyObject* args) PyObject* vm_check_invalid_code_blocs(VmMngr* self, PyObject* args) { - check_invalid_code_blocs(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + check_invalid_code_blocs(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } PyObject* vm_check_memory_breakpoint(VmMngr* self, PyObject* args) { - check_memory_breakpoint(&self->vm_mngr); - Py_INCREF(Py_None); - return Py_None; + check_memory_breakpoint(&self->vm_mngr); + Py_INCREF(Py_None); + return Py_None; } PyObject *vm_dump(PyObject* self) @@ -782,24 +768,22 @@ vm_is_little_endian(VmMngr *self, PyObject *value, void *closure) } } - -static void + static void VmMngr_dealloc(VmMngr* self) { - vm_reset_memory_page_pool(self, NULL); - vm_reset_code_bloc_pool(self, NULL); - vm_reset_memory_breakpoint(self, NULL); - Py_TYPE(self)->tp_free((PyObject*)self); + vm_reset_memory_page_pool(self, NULL); + vm_reset_code_bloc_pool(self, NULL); + vm_reset_memory_breakpoint(self, NULL); + Py_TYPE(self)->tp_free((PyObject*)self); } - static PyObject * VmMngr_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - VmMngr *self; + VmMngr *self; - self = (VmMngr *)type->tp_alloc(type, 0); - return (PyObject *)self; + self = (VmMngr *)type->tp_alloc(type, 0); + return (PyObject *)self; } static PyObject * @@ -816,7 +800,7 @@ VmMngr_set_vmmngr(VmMngr *self, PyObject *value, void *closure) } static PyMemberDef VmMngr_members[] = { - {NULL} /* Sentinel */ + {NULL} /* Sentinel */ }; static PyMethodDef VmMngr_methods[] = { @@ -920,52 +904,52 @@ VmMngr_init(VmMngr *self, PyObject *args, PyObject *kwds) } static PyGetSetDef VmMngr_getseters[] = { - {"vmmngr", - (getter)VmMngr_get_vmmngr, (setter)VmMngr_set_vmmngr, - "vmmngr object", - NULL}, - {NULL} /* Sentinel */ + {"vmmngr", + (getter)VmMngr_get_vmmngr, (setter)VmMngr_set_vmmngr, + "vmmngr object", + NULL}, + {NULL} /* Sentinel */ }; static PyTypeObject VmMngrType = { - PyVarObject_HEAD_INIT(NULL, 0) - "VmMngr", /*tp_name*/ - sizeof(VmMngr), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)VmMngr_dealloc,/*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - vm_dump, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "VmMngr object", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - VmMngr_methods, /* tp_methods */ - VmMngr_members, /* tp_members */ - VmMngr_getseters, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)VmMngr_init, /* tp_init */ - 0, /* tp_alloc */ - VmMngr_new, /* tp_new */ + PyVarObject_HEAD_INIT(NULL, 0) + "VmMngr", /*tp_name*/ + sizeof(VmMngr), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)VmMngr_dealloc,/*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + vm_dump, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "VmMngr object", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + VmMngr_methods, /* tp_methods */ + VmMngr_members, /* tp_members */ + VmMngr_getseters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)VmMngr_init, /* tp_init */ + 0, /* tp_alloc */ + VmMngr_new, /* tp_new */ }; static PyMethodDef VmMngr_Methods[] = { @@ -983,15 +967,18 @@ MOD_INIT(VmMngr) MOD_DEF(module, "VmMngr", "vm_mngr module", VmMngr_Methods); - if (module == NULL) + if (module == NULL) { RET_MODULE; + } - if (PyType_Ready(&VmMngrType) < 0) + if (PyType_Ready(&VmMngrType) < 0) { RET_MODULE; + } Py_INCREF(&VmMngrType); - if (PyModule_AddObject(module, "Vm", (PyObject *)&VmMngrType) < 0) + if (PyModule_AddObject(module, "Vm", (PyObject *)&VmMngrType) < 0) { RET_MODULE; + } RET_MODULE; } From 85313ecf186d8822ce04f73e50d33d65170ca15d Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:35:37 +0100 Subject: [PATCH 6/9] Remove dead code --- miasm/jitter/vm_mngr.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/miasm/jitter/vm_mngr.c b/miasm/jitter/vm_mngr.c index de4f375da..c59e3a400 100644 --- a/miasm/jitter/vm_mngr.c +++ b/miasm/jitter/vm_mngr.c @@ -212,9 +212,9 @@ static uint64_t memory_page_read(vm_mngr_t* vm_mngr, unsigned int my_size, uint6 new_size -= 8; ad ++; } - switch(my_size){ + switch (my_size) { case 8: - ret = ret; + // DO NOTHING // ret = ret; break; case 16: ret = set_endian16(vm_mngr, (uint16_t)ret); @@ -286,11 +286,10 @@ static void memory_page_write(vm_mngr_t* vm_mngr, unsigned int my_size, } } /* write is multiple page wide */ - else{ - switch(my_size){ - + else { + switch (my_size) { case 8: - src = src; + // DO NOTHING src = src; break; case 16: src = set_endian16(vm_mngr, (uint16_t)src); From b1a2318664d6a2a89bdb17ce91c45b43e0f723ed Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:35:56 +0100 Subject: [PATCH 7/9] Use UINT64_MAX constant instead of hardcode a big number --- miasm/jitter/vm_mngr.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/miasm/jitter/vm_mngr.c b/miasm/jitter/vm_mngr.c index c59e3a400..3a337ad4a 100644 --- a/miasm/jitter/vm_mngr.c +++ b/miasm/jitter/vm_mngr.c @@ -683,13 +683,11 @@ void init_memory_page_pool(vm_mngr_t* vm_mngr) void init_code_bloc_pool(vm_mngr_t* vm_mngr) { LIST_INIT(&vm_mngr->code_bloc_pool); - vm_mngr->code_bloc_pool_ad_min = 0xffffffffffffffffULL; + vm_mngr->code_bloc_pool_ad_min = UINT64_MAX; vm_mngr->code_bloc_pool_ad_max = 0; memory_access_list_init(&(vm_mngr->memory_r)); memory_access_list_init(&(vm_mngr->memory_w)); - - } void init_memory_breakpoint(vm_mngr_t* vm_mngr) From 388dc0d9da6760fb8f47954801eb66a098942642 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:36:10 +0100 Subject: [PATCH 8/9] Fix bad practices found in vm_mngr.c:dump --- miasm/jitter/vm_mngr.c | 48 ++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/miasm/jitter/vm_mngr.c b/miasm/jitter/vm_mngr.c index 3a337ad4a..86ec641ae 100644 --- a/miasm/jitter/vm_mngr.c +++ b/miasm/jitter/vm_mngr.c @@ -797,46 +797,38 @@ void add_memory_page(vm_mngr_t* vm_mngr, struct memory_page_node* mpn_a) char* dump(vm_mngr_t* vm_mngr) { char buf[0x100]; - int length; - char *buf_final; + char *buf_final, *tmp; int i; char buf_addr[0x20]; char buf_size[0x20]; + size_t buf_final_len, length; struct memory_page_node * mpn; /* 0x1234567812345678 0x1234567812345678 */ - char* intro = "Addr Size Access Comment\n"; - size_t total_len = strlen(intro) + 1; + const char* intro = "Addr Size Access Comment\n"; - buf_final = malloc(total_len); - if (buf_final == NULL) { - fprintf(stderr, "Error: cannot alloc char* buf_final\n"); - exit(EXIT_FAILURE); - } - strcpy(buf_final, intro); - for (i=0; i< vm_mngr->memory_pages_number; i++) { + buf_final = strdup (intro); + buf_final_len = strlen (intro); + for (i = 0; i < vm_mngr->memory_pages_number; i++) { mpn = &vm_mngr->memory_pages_array[i]; - snprintf(buf_addr, sizeof(buf_addr), - "0x%"PRIX64, (uint64_t)mpn->ad); - snprintf(buf_size, sizeof(buf_size), - "0x%"PRIX64, (uint64_t)mpn->size); + snprintf(buf_addr, sizeof(buf_addr), "0x%"PRIX64, mpn->ad); + snprintf(buf_size, sizeof(buf_size), "0x%zx", mpn->size); length = snprintf(buf, sizeof(buf) - 1, - "%-18s %-18s %c%c%c %s", - buf_addr, - buf_size, - mpn->access & PAGE_READ? 'R':'_', - mpn->access & PAGE_WRITE? 'W':'_', - mpn->access & PAGE_EXEC? 'X':'_', - mpn->name - ); - strcat(buf, "\n"); - total_len += length + 1 + 1; - buf_final = realloc(buf_final, total_len); - if (buf_final == NULL) { + "%-18s %-18s %c%c%c %s\n", + buf_addr, + buf_size, + mpn->access & PAGE_READ? 'R':'_', + mpn->access & PAGE_WRITE? 'W':'_', + mpn->access & PAGE_EXEC? 'X':'_', + mpn->name); + tmp = realloc(buf_final, buf_final_len + length + 1); + if (tmp == NULL) { fprintf(stderr, "cannot realloc char* buf_final\n"); + free(buf_final); exit(EXIT_FAILURE); } - strcat(buf_final, buf); + strcpy (buf_final + buf_final_len, buf); + buf_final_len += length; } return buf_final; From 0577b3d4a32e48bf99ad27cea4f4de859f667696 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 Mar 2019 13:36:29 +0100 Subject: [PATCH 9/9] More fixes related to good C coding practices --- miasm/jitter/vm_mngr.h | 2 +- miasm/jitter/vm_mngr_py.c | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/miasm/jitter/vm_mngr.h b/miasm/jitter/vm_mngr.h index 7e234748c..16ed1430e 100644 --- a/miasm/jitter/vm_mngr.h +++ b/miasm/jitter/vm_mngr.h @@ -74,7 +74,7 @@ LIST_HEAD(memory_breakpoint_info_head, memory_breakpoint_info); #define BREAKPOINT_READ 1 #define BREAKPOINT_WRITE 2 -#define BREAK_SIGALARM 1<<5 +#define BREAK_SIGALARM (1<<5) #define MAX_MEMORY_PAGE_POOL_TAB 0x100000 #define MEMORY_PAGE_POOL_MASK_BIT 12 diff --git a/miasm/jitter/vm_mngr_py.c b/miasm/jitter/vm_mngr_py.c index ce6ec1711..11e7d3fd0 100644 --- a/miasm/jitter/vm_mngr_py.c +++ b/miasm/jitter/vm_mngr_py.c @@ -25,18 +25,13 @@ #include "vm_mngr.h" #include "vm_mngr_py.h" -#define MIN(a,b) (((a)<(b))?(a):(b)) -#define MAX(a,b) (((a)>(b))?(a):(b)) - extern struct memory_page_list_head memory_page_pool; extern struct code_bloc_list_head code_bloc_pool; -#define RAISE(errtype, msg) {PyObject* p; p = PyErr_Format( errtype, msg ); return p;} - - +#define RAISE(errtype, msg) do {PyObject* p; p = PyErr_Format( errtype, msg ); return p;} while(0) /* XXX POC signals */ -VmMngr* global_vmmngr; +static VmMngr* global_vmmngr; PyObject* _vm_get_exception(unsigned int xcpt) { @@ -957,9 +952,8 @@ static PyMethodDef VmMngr_Methods[] = { }; -char vm_mngr_mod_docs[] = "vm_mngr module."; -char vm_mngr_mod_name[] = "VmMngr"; - +const char vm_mngr_mod_docs[] = "vm_mngr module."; +const char vm_mngr_mod_name[] = "VmMngr"; MOD_INIT(VmMngr) {