diff options
Diffstat (limited to 'miasm2/jitter')
| -rw-r--r-- | miasm2/jitter/Jitgcc.c | 2 | ||||
| -rw-r--r-- | miasm2/jitter/Jittcc.c | 14 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_aarch64.c | 2 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_arm.c | 2 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_mips32.c | 2 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_msp430.c | 2 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_x86.c | 2 | ||||
| -rw-r--r-- | miasm2/jitter/llvmconvert.py | 48 | ||||
| -rw-r--r-- | miasm2/jitter/vm_mngr.c | 129 | ||||
| -rw-r--r-- | miasm2/jitter/vm_mngr.h | 10 |
10 files changed, 101 insertions, 112 deletions
diff --git a/miasm2/jitter/Jitgcc.c b/miasm2/jitter/Jitgcc.c index 71023902..79274f24 100644 --- a/miasm2/jitter/Jitgcc.c +++ b/miasm2/jitter/Jitgcc.c @@ -59,7 +59,7 @@ PyObject* gcc_exec_bloc(PyObject* self, PyObject* args) else { if (BlockDst.is_local == 1) { fprintf(stderr, "return on local label!\n"); - exit(1); + exit(EXIT_FAILURE); } // retaddr is not jitted yet return retaddr; diff --git a/miasm2/jitter/Jittcc.c b/miasm2/jitter/Jittcc.c index 88359147..61a6cda4 100644 --- a/miasm2/jitter/Jittcc.c +++ b/miasm2/jitter/Jittcc.c @@ -41,7 +41,7 @@ TCCState * tcc_init_state(void) tcc_state = tcc_new(); if (!tcc_state) { fprintf(stderr, "Impossible de creer un contexte TCC\n"); - exit(1); + exit(EXIT_FAILURE); } tcc_set_output_type(tcc_state, TCC_OUTPUT_MEMORY); @@ -184,7 +184,7 @@ PyObject* tcc_exec_bloc(PyObject* self, PyObject* args) else { if (BlockDst.is_local == 1) { fprintf(stderr, "return on local label!\n"); - exit(1); + exit(EXIT_FAILURE); } // retaddr is not jitted yet return retaddr; @@ -221,25 +221,25 @@ PyObject* tcc_compil(PyObject* self, PyObject* args) if (tcc_compile_string(tcc_state, func_code) != 0) { fprintf(stderr, "Error compiling !\n"); fprintf(stderr, "%s\n", func_code); - exit(1); + exit(EXIT_FAILURE); } /* XXX configure tinycc install with --disable-static */ if (tcc_relocate(tcc_state, TCC_RELOCATE_AUTO) < 0) { fprintf(stderr, "TCC relocate error\n"); - exit(1); + exit(EXIT_FAILURE); } entry = tcc_get_symbol(tcc_state, func_name); if (!entry){ fprintf(stderr, "Error getting symbol %s!\n", func_name); fprintf(stderr, "%s\n", func_name); - exit(1); + exit(EXIT_FAILURE); } ret = PyTuple_New(2); if (ret == NULL) { fprintf(stderr, "Error alloc %s!\n", func_name); fprintf(stderr, "%s\n", func_name); - exit(1); + exit(EXIT_FAILURE); } PyTuple_SetItem(ret, 0, PyLong_FromUnsignedLongLong((intptr_t) tcc_state)); @@ -267,7 +267,7 @@ PyObject* tcc_loop_exec(PyObject* self, PyObject* args) while (1) { if (!PyCallable_Check (func)) { fprintf(stderr, "function not callable!\n"); - exit(0); + exit(EXIT_FAILURE); } pArgs = PyTuple_New(2); diff --git a/miasm2/jitter/arch/JitCore_aarch64.c b/miasm2/jitter/arch/JitCore_aarch64.c index e10d847e..ff8241c6 100644 --- a/miasm2/jitter/arch/JitCore_aarch64.c +++ b/miasm2/jitter/arch/JitCore_aarch64.c @@ -332,7 +332,7 @@ JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) self->cpu = malloc(sizeof(vm_cpu_t)); if (self->cpu == NULL) { fprintf(stderr, "cannot alloc vm_cpu_t\n"); - exit(0); + exit(EXIT_FAILURE); } return 0; } diff --git a/miasm2/jitter/arch/JitCore_arm.c b/miasm2/jitter/arch/JitCore_arm.c index 84716c2d..6b167da5 100644 --- a/miasm2/jitter/arch/JitCore_arm.c +++ b/miasm2/jitter/arch/JitCore_arm.c @@ -277,7 +277,7 @@ JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) self->cpu = malloc(sizeof(vm_cpu_t)); if (self->cpu == NULL) { fprintf(stderr, "cannot alloc vm_cpu_t\n"); - exit(0); + exit(EXIT_FAILURE); } return 0; } diff --git a/miasm2/jitter/arch/JitCore_mips32.c b/miasm2/jitter/arch/JitCore_mips32.c index 04e4d883..19b24f1f 100644 --- a/miasm2/jitter/arch/JitCore_mips32.c +++ b/miasm2/jitter/arch/JitCore_mips32.c @@ -307,7 +307,7 @@ JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) self->cpu = malloc(sizeof(vm_cpu_t)); if (self->cpu == NULL) { fprintf(stderr, "cannot alloc vm_cpu_t\n"); - exit(0); + exit(EXIT_FAILURE); } return 0; } diff --git a/miasm2/jitter/arch/JitCore_msp430.c b/miasm2/jitter/arch/JitCore_msp430.c index d30655dd..7fe41413 100644 --- a/miasm2/jitter/arch/JitCore_msp430.c +++ b/miasm2/jitter/arch/JitCore_msp430.c @@ -279,7 +279,7 @@ JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) self->cpu = malloc(sizeof(vm_cpu_t)); if (self->cpu == NULL) { fprintf(stderr, "cannot alloc vm_cpu_t\n"); - exit(0); + exit(EXIT_FAILURE); } return 0; } diff --git a/miasm2/jitter/arch/JitCore_x86.c b/miasm2/jitter/arch/JitCore_x86.c index 407a01c7..5c929dab 100644 --- a/miasm2/jitter/arch/JitCore_x86.c +++ b/miasm2/jitter/arch/JitCore_x86.c @@ -437,7 +437,7 @@ JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) self->cpu = malloc(sizeof(vm_cpu_t)); if (self->cpu == NULL) { fprintf(stderr, "cannot alloc vm_cpu_t\n"); - exit(0); + exit(EXIT_FAILURE); } return 0; } diff --git a/miasm2/jitter/llvmconvert.py b/miasm2/jitter/llvmconvert.py index 65c6aa07..9796b265 100644 --- a/miasm2/jitter/llvmconvert.py +++ b/miasm2/jitter/llvmconvert.py @@ -223,24 +223,6 @@ class LLVMContext_JIT(LLVMContext): itype = LLVMType.IntType(64) fc = {"llvm.ctpop.i8": {"ret": i8, "args": [i8]}, - "rot_left": {"ret": itype, - "args": [itype, - itype, - itype]}, - "rot_right": {"ret": itype, - "args": [itype, - itype, - itype]}, - "rcr_rez_op": {"ret": itype, - "args": [itype, - itype, - itype, - itype]}, - "rcl_rez_op": {"ret": itype, - "args": [itype, - itype, - itype, - itype]}, "x86_bsr": {"ret": itype, "args": [itype, itype]}, @@ -391,11 +373,7 @@ class LLVMFunction(): op_translate = {'cpuid': 'cpuid', } ## Add the size as first argument - op_translate_with_size = {'<<<': 'rot_left', - '>>>': 'rot_right', - '<<<c_rez': 'rcl_rez_op', - '>>>c_rez': 'rcr_rez_op', - 'bsr': 'x86_bsr', + op_translate_with_size = {'bsr': 'x86_bsr', 'bsf': 'x86_bsf', } ## Add the size as suffix @@ -790,6 +768,30 @@ class LLVMFunction(): self.update_cache(expr, ret) return ret + + if op in ['<<<', '>>>']: + assert len(expr.args) == 2 + # First compute rotation modulus size + count = self.add_ir(expr.args[1]) + value = self.add_ir(expr.args[0]) + itype = LLVMType.IntType(expr.size) + expr_size = itype(expr.size) + + shift = builder.urem(count, expr_size) + shift_inv = builder.sub(expr_size, shift) + + if op == '<<<': + part_a = builder.shl(value, shift) + part_b = builder.lshr(value, shift_inv) + else: + part_a = builder.lshr(value, shift) + part_b = builder.shl(value, shift_inv) + ret = builder.or_(part_a, part_b) + self.update_cache(expr, ret) + return ret + + + if op in ["int_16_to_double", "int_32_to_double", "int_64_to_double", "mem_16_to_double", "mem_32_to_double", "mem_64_to_double"]: arg = self.add_ir(expr.args[0]) diff --git a/miasm2/jitter/vm_mngr.c b/miasm2/jitter/vm_mngr.c index c628aeff..3c324a08 100644 --- a/miasm2/jitter/vm_mngr.c +++ b/miasm2/jitter/vm_mngr.c @@ -245,7 +245,7 @@ static uint64_t memory_page_read(vm_mngr_t* vm_mngr, unsigned int my_size, uint6 ret = set_endian64(vm_mngr, ret); break; default: - exit(0); + exit(EXIT_FAILURE); break; } } @@ -277,7 +277,7 @@ static uint64_t memory_page_read(vm_mngr_t* vm_mngr, unsigned int my_size, uint6 ret = set_endian64(vm_mngr, ret); break; default: - exit(0); + exit(EXIT_FAILURE); break; } } @@ -330,7 +330,7 @@ static void memory_page_write(vm_mngr_t* vm_mngr, unsigned int my_size, *((uint64_t*)addr) = src&0xFFFFFFFFFFFFFFFFULL; break; default: - exit(0); + exit(EXIT_FAILURE); break; } } @@ -351,7 +351,7 @@ static void memory_page_write(vm_mngr_t* vm_mngr, unsigned int my_size, src = set_endian64(vm_mngr, src); break; default: - exit(0); + exit(EXIT_FAILURE); break; } while (my_size){ @@ -607,7 +607,7 @@ int vm_read_mem(vm_mngr_t* vm_mngr, uint64_t addr, char** buffer_ptr, uint64_t s *buffer_ptr = buffer; if (!buffer){ fprintf(stderr, "Error: cannot alloc read\n"); - exit(-1); + exit(EXIT_FAILURE); } /* read is multiple page wide */ @@ -681,7 +681,7 @@ unsigned int mul_lo_op(unsigned int size, unsigned int a, unsigned int b) 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(0); + default: fprintf(stderr, "inv size in mul %d\n", size); exit(EXIT_FAILURE); } a &= mask; @@ -698,7 +698,7 @@ unsigned int mul_hi_op(unsigned int size, unsigned int a, unsigned int b) 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(0); + default: fprintf(stderr, "inv size in mul %d\n", size); exit(EXIT_FAILURE); } a &= mask; @@ -760,24 +760,37 @@ uint64_t rot_left(uint64_t size, uint64_t a, uint64_t b) { uint64_t tmp; - b = b&0x3F; + b = b & 0x3F; b %= size; switch(size){ case 8: - tmp = (a << b) | ((a&0xFF) >> (size-b)); - return tmp&0xff; + tmp = (a << b) | ((a & 0xFF) >> (size - b)); + return tmp & 0xFF; case 16: - tmp = (a << b) | ((a&0xFFFF) >> (size-b)); - return tmp&0xffff; + tmp = (a << b) | ((a & 0xFFFF) >> (size - b)); + return tmp & 0xFFFF; case 32: - tmp = (a << b) | ((a&0xFFFFFFFF) >> (size-b)); - return tmp&0xffffffff; + tmp = (a << b) | ((a & 0xFFFFFFFF) >> (size - b)); + return tmp & 0xFFFFFFFF; case 64: - tmp = (a << b) | ((a&0xFFFFFFFFFFFFFFFF) >> (size-b)); - return tmp&0xFFFFFFFFFFFFFFFF; + 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(0); + exit(EXIT_FAILURE); } } @@ -785,62 +798,38 @@ uint64_t rot_right(uint64_t size, uint64_t a, uint64_t b) { uint64_t tmp; - b = b&0x3F; + b = b & 0x3F; b %= size; switch(size){ case 8: - tmp = ((a&0xFF) >> b) | (a << (size-b)); - return tmp&0xff; + tmp = ((a & 0xFF) >> b) | (a << (size - b)); + return tmp & 0xff; case 16: - tmp = ((a&0xFFFF) >> b) | (a << (size-b)); - return tmp&0xffff; + tmp = ((a & 0xFFFF) >> b) | (a << (size - b)); + return tmp & 0xFFFF; case 32: - tmp = ((a&0xFFFFFFFF) >> b) | (a << (size-b)); - return tmp&0xffffffff; + tmp = ((a & 0xFFFFFFFF) >> b) | (a << (size - b)); + return tmp & 0xFFFFFFFF; case 64: - tmp = ((a&0xFFFFFFFFFFFFFFFF) >> b) | (a << (size-b)); - return tmp&0xFFFFFFFFFFFFFFFF; + 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(0); - } -} - - -unsigned int rcl_rez_op(unsigned int size, unsigned int a, unsigned int b, unsigned int cf) -{ - uint64_t tmp; - uint64_t tmp_count; - uint64_t tmp_cf; - - tmp = a; - // TODO 64bit mode - tmp_count = (b & 0x1f) % (size + 1); - while (tmp_count != 0) { - tmp_cf = (tmp >> (size - 1)) & 1; - tmp = (tmp << 1) + cf; - cf = tmp_cf; - tmp_count -= 1; - } - return tmp; -} - -unsigned int rcr_rez_op(unsigned int size, unsigned int a, unsigned int b, unsigned int cf) -{ - uint64_t tmp; - uint64_t tmp_count; - uint64_t tmp_cf; - - tmp = a; - // TODO 64bit mode - tmp_count = (b & 0x1f) % (size + 1); - while (tmp_count != 0) { - tmp_cf = tmp & 1; - tmp = (tmp >> 1) + (cf << (size - 1)); - cf = tmp_cf; - tmp_count -= 1; + exit(EXIT_FAILURE); } - return tmp; } unsigned int x86_bsr(uint64_t size, uint64_t src) @@ -852,7 +841,7 @@ unsigned int x86_bsr(uint64_t size, uint64_t src) return i; } fprintf(stderr, "sanity check error bsr\n"); - exit(0); + exit(EXIT_FAILURE); } unsigned int x86_bsf(uint64_t size, uint64_t src) @@ -863,7 +852,7 @@ unsigned int x86_bsf(uint64_t size, uint64_t src) return i; } fprintf(stderr, "sanity check error bsf\n"); - exit(0); + exit(EXIT_FAILURE); } @@ -884,7 +873,7 @@ unsigned int cpuid(unsigned int a, unsigned int reg_num) { if (reg_num >3){ fprintf(stderr, "not implemented cpuid reg %x\n", reg_num); - exit(-1); + exit(EXIT_FAILURE); } if (a == 0){ @@ -918,7 +907,7 @@ unsigned int cpuid(unsigned int a, unsigned int reg_num) } else{ fprintf(stderr, "WARNING not implemented cpuid index %X!\n", a); - //exit(-1); + //exit(EXIT_FAILURE); } return 0; } @@ -1400,7 +1389,7 @@ struct code_bloc_node * create_code_bloc_node(uint64_t ad_start, uint64_t ad_sto cbp = malloc(sizeof(*cbp)); if (!cbp){ fprintf(stderr, "Error: cannot alloc cbp\n"); - exit(-1); + exit(EXIT_FAILURE); } cbp->ad_start = ad_start; @@ -1630,7 +1619,7 @@ void add_memory_breakpoint(vm_mngr_t* vm_mngr, uint64_t ad, uint64_t size, unsig mpn_a = malloc(sizeof(*mpn_a)); if (!mpn_a) { fprintf(stderr, "Error: cannot alloc\n"); - exit(0); + exit(EXIT_FAILURE); } mpn_a->ad = ad; mpn_a->size = size; diff --git a/miasm2/jitter/vm_mngr.h b/miasm2/jitter/vm_mngr.h index 757c3b3e..71ecc246 100644 --- a/miasm2/jitter/vm_mngr.h +++ b/miasm2/jitter/vm_mngr.h @@ -216,8 +216,6 @@ unsigned int umul16_hi(unsigned short a, unsigned short b); uint64_t rot_left(uint64_t size, uint64_t a, uint64_t b); uint64_t rot_right(uint64_t size, uint64_t a, uint64_t b); -unsigned int rcl_rez_op(unsigned int size, unsigned int a, unsigned int b, unsigned int cf); -unsigned int rcr_rez_op(unsigned int size, unsigned int a, unsigned int b, unsigned int cf); #define UDIV(sizeA) \ @@ -226,7 +224,7 @@ unsigned int rcr_rez_op(unsigned int size, unsigned int a, unsigned int b, unsig uint ## sizeA ## _t r; \ if (b == 0) { \ fprintf(stderr, "Should not happen\n"); \ - exit(0); \ + exit(EXIT_FAILURE); \ } \ r = a/b; \ return r; \ @@ -239,7 +237,7 @@ unsigned int rcr_rez_op(unsigned int size, unsigned int a, unsigned int b, unsig uint ## sizeA ## _t r; \ if (b == 0) { \ fprintf(stderr, "Should not happen\n"); \ - exit(0); \ + exit(EXIT_FAILURE); \ } \ r = a%b; \ return r; \ @@ -252,7 +250,7 @@ unsigned int rcr_rez_op(unsigned int size, unsigned int a, unsigned int b, unsig int ## sizeA ## _t r; \ if (b == 0) { \ fprintf(stderr, "Should not happen\n"); \ - exit(0); \ + exit(EXIT_FAILURE); \ } \ r = a/b; \ return r; \ @@ -265,7 +263,7 @@ unsigned int rcr_rez_op(unsigned int size, unsigned int a, unsigned int b, unsig int ## sizeA ## _t r; \ if (b == 0) { \ fprintf(stderr, "Should not happen\n"); \ - exit(0); \ + exit(EXIT_FAILURE); \ } \ r = a%b; \ return r; \ |