diff options
| -rw-r--r-- | miasm/arch/ia32_sem.py | 134 | ||||
| -rw-r--r-- | miasm/expression/expression.py | 9 | ||||
| -rw-r--r-- | miasm/tools/emul_lib/libcodenat.c | 76 | ||||
| -rw-r--r-- | miasm/tools/emul_lib/libcodenat.h | 15 |
4 files changed, 221 insertions, 13 deletions
diff --git a/miasm/arch/ia32_sem.py b/miasm/arch/ia32_sem.py index 0b0565da..2708192e 100644 --- a/miasm/arch/ia32_sem.py +++ b/miasm/arch/ia32_sem.py @@ -18,7 +18,7 @@ from miasm.expression.expression import * from miasm.arch.ia32_reg import * from miasm.arch.ia32_arch import * - +import math EXCEPT_PRIV_INSN = 1<<7 reg_eax = 'eax' @@ -1512,8 +1512,6 @@ def fst(a): src = ExprOp('double_to_mem_%2d'%a.get_size(), float_st0) else: src = float_st0 - - e.append(ExprAff(a, src)) return e @@ -1533,31 +1531,135 @@ def fistp(a): return e def fild(a): - #XXXXX src = ExprOp('int_%.2d_to_double'%a.get_size(), a) return fld(src) def fldz(): - #XXX return fld(ExprOp('int_32_to_double', ExprInt(uint32(0)))) - -def fadd(a): + +def fld1(): + return fld(ExprOp('int_32_to_double', ExprInt(uint32(1)))) + +def fldl2e(): + x = struct.pack('d', 1/math.log(2)) + x = struct.unpack('Q', x)[0] + return fld(ExprOp('mem_64_to_double', ExprInt(uint64(x)))) + +def fadd(a, b = None): + if b == None: + b = a + a = float_st0 + e = [] + if isinstance(b, ExprMem): + src = ExprOp('mem_%.2d_to_double'%b.get_size(), b) + else: + src = b + e.append(ExprAff(a, ExprOp('fadd', a, src))) + return e + +def faddp(a, b = None): + e = fadd(a, b) + if b == None: + e+=float_pop(float_st0) + else: + e+=float_pop(a) + return e + + +def fsub(a, b = None): + if b == None: + b = a + a = float_st0 + e = [] + if isinstance(b, ExprMem): + src = ExprOp('mem_%.2d_to_double'%b.get_size(), b) + else: + src = b + e.append(ExprAff(a, ExprOp('fsub', a, src))) + return e + +def fmul(a, b = None): + if b == None: + b = a + a = float_st0 + e = [] + if isinstance(b, ExprMem): + src = ExprOp('mem_%.2d_to_double'%b.get_size(), b) + else: + src = b + e.append(ExprAff(a, ExprOp('fmul', a, src))) + return e + +def fdiv(a, b = None): + if b == None: + b = a + a = float_st0 + e = [] + if isinstance(b, ExprMem): + src = ExprOp('mem_%.2d_to_double'%b.get_size(), b) + else: + src = b + e.append(ExprAff(a, ExprOp('fdiv', a, src))) + return e + +def ftan(a): e = [] if isinstance(a, ExprMem): src = ExprOp('mem_%.2d_to_double'%a.get_size(), a) else: src = a - e.append(ExprAff(float_st0, ExprOp('fadd', float_st0, src))) + e.append(ExprAff(float_st0, ExprOp('ftan', src))) return e -def fdiv(a): +def fxch(a): e = [] if isinstance(a, ExprMem): src = ExprOp('mem_%.2d_to_double'%a.get_size(), a) else: src = a - e.append(ExprAff(float_st0, ExprOp('fdiv', float_st0, src))) + e.append(ExprAff(float_st0, src)) + e.append(ExprAff(src, float_st0)) + return e + +def fptan(): + e= [] + e.append(ExprAff(float_st7, float_st6)) + e.append(ExprAff(float_st6, float_st5)) + e.append(ExprAff(float_st5, float_st4)) + e.append(ExprAff(float_st4, float_st3)) + e.append(ExprAff(float_st3, float_st2)) + e.append(ExprAff(float_st2, float_st1)) + e.append(ExprAff(float_st1, ExprOp('ftan', float_st0))) + e.append(ExprAff(float_st0, ExprOp('int_32_to_double', ExprInt(uint32(1))))) + e.append(ExprAff(float_stack_ptr, ExprOp('+', float_stack_ptr, ExprInt(uint32(1))))) + return e + + e.append(ExprAff(float_st0, ExprOp('ftan', src))) + + e = ftan(a) + e+=float_pop(a) + return e + + +def frndint(): + e = [] + e.append(ExprAff(float_st0, ExprOp('frndint', float_st0))) + return e + +def fsin(): + e = [] + e.append(ExprAff(float_st0, ExprOp('fsin', float_st0))) + return e + +def fscale(): + e = [] + e.append(ExprAff(float_st0, ExprOp('fscale', float_st0, float_st1))) + return e + +def f2xm1(): + e = [] + e.append(ExprAff(float_st0, ExprOp('f2xm1', float_st0))) return e @@ -1594,6 +1696,7 @@ def hlt(): def rdtsc(): e = [] + e.append(ExprAff(tsc1, ExprOp('+', tsc1, ExprInt(uint32(1))))) e.append(ExprAff(eax, tsc1)) e.append(ExprAff(edx, tsc2)) return e @@ -1892,9 +1995,20 @@ mnemo_func = {'mov': mov, 'fistp':fistp, 'fld':fld, 'fldz':fldz, + 'fld1':fld1, + 'fldl2e':fldl2e, 'fild':fild, 'fadd':fadd, + 'faddp':faddp, + 'fsub':fsub, + 'fmul':fmul, 'fdiv':fdiv, + 'fxch':fxch, + 'fptan':fptan, + 'frndint':frndint, + 'fsin':fsin, + 'fscale':fscale, + 'f2xm1':f2xm1, 'fnstsw':fnstsw, 'fnstcw':fnstcw, 'fldcw':fldcw, diff --git a/miasm/expression/expression.py b/miasm/expression/expression.py index e5ea8eda..8fa9eb27 100644 --- a/miasm/expression/expression.py +++ b/miasm/expression/expression.py @@ -457,8 +457,11 @@ class ExprOp(Expr): return "%s(%s)"%(self.op, self.args[0].toC()) elif self.op.startswith("double_to_mem_"): return "%s(%s)"%(self.op, self.args[0].toC()) + elif self.op in ["ftan", "frndint", "f2xm1", "fsin"]: + return "%s(%s)"%(self.op, self.args[0].toC()) else: - fsdfdsf + print self.op + raise ValueError('unknown op!!', str(self.op)) return '('+str(self.op)+self.args[0].toC()+')' elif len(self.args)==2: if self.op == "==": @@ -478,7 +481,7 @@ class ExprOp(Expr): elif self.op in dct_rot: return '(%s(%s, %s, %s) &0x%x)'%(dct_rot[self.op], self.args[0].get_size(), - self.args[0].toC(), + self.args[0].toC(), self.args[1].toC(), my_size_mask[self.args[0].get_size()]) @@ -534,7 +537,7 @@ class ExprOp(Expr): return "%s(%s, %s)"%(self.op, self.args[0].toC(), self.args[1].toC()) elif self.op.startswith("fcom"): return "%s(%s, %s)"%(self.op, self.args[0].toC(), self.args[1].toC()) - elif self.op in ["fadd", "fdiv"]: + elif self.op in ["fadd", "fsub", "fdiv", 'fmul', "fscale"]: return "%s(%s, %s)"%(self.op, self.args[0].toC(), self.args[1].toC()) else: print self.op diff --git a/miasm/tools/emul_lib/libcodenat.c b/miasm/tools/emul_lib/libcodenat.c index 170726e8..aedbce49 100644 --- a/miasm/tools/emul_lib/libcodenat.c +++ b/miasm/tools/emul_lib/libcodenat.c @@ -24,6 +24,7 @@ #include <stdint.h> #include <inttypes.h> +#include <math.h> #include "queue.h" #include "libcodenat.h" @@ -863,6 +864,26 @@ double fadd(double a, double b) return c; } +double fsub(double a, double b) +{ + double c; + c = a - b; +#ifdef DEBUG_MIASM_DOUBLE + printf("%e %e %e\n", a, b, c); +#endif + return c; +} + +double fmul(double a, double b) +{ + double c; + c = a * b; +#ifdef DEBUG_MIASM_DOUBLE + printf("%e %e %e\n", a, b, c); +#endif + return c; +} + double fdiv(double a, double b) { double c; @@ -873,6 +894,59 @@ double fdiv(double a, double b) return c; } +double ftan(double a) +{ + double b; + b = tan(a); +#ifdef DEBUG_MIASM_DOUBLE + printf("%e %e\n", a, b); +#endif + return b; +} + +double frndint(double a) +{ + int64_t b; + double c; + b = (int64_t)a; + c = (double)b; +#ifdef DEBUG_MIASM_DOUBLE + printf("%e %e\n", a, c); +#endif + return c; +} + +double fsin(double a) +{ + double b; + b = sin(a); +#ifdef DEBUG_MIASM_DOUBLE + printf("%e %e\n", a, b); +#endif + return b; +} + + +double fscale(double a, double b) +{ + double c; + c = a * exp2(trunc(b)); +#ifdef DEBUG_MIASM_DOUBLE + printf("%e %e %e\n", a, b, c); +#endif + return c; +} + +double f2xm1(double a) +{ + double b; + b = exp2(a)-1; +#ifdef DEBUG_MIASM_DOUBLE + printf("%e %e\n", a, b); +#endif + return b; +} + unsigned int fcom_c0(double a, double b) @@ -1219,6 +1293,8 @@ void _vm_init_regs() vmcpu.zf_new = vmcpu.nf_new = vmcpu.pf_new = vmcpu.of_new = 0; vmcpu.cf_new = vmcpu.af_new = vmcpu.df_new = 0; vmcpu.esp = 0; + vmcpu.tsc1 = 0x22222222; + vmcpu.tsc2 = 0x11111111; } diff --git a/miasm/tools/emul_lib/libcodenat.h b/miasm/tools/emul_lib/libcodenat.h index c1ae4e7a..d91532c0 100644 --- a/miasm/tools/emul_lib/libcodenat.h +++ b/miasm/tools/emul_lib/libcodenat.h @@ -163,6 +163,9 @@ typedef struct { unsigned int tsc1; unsigned int tsc2; + unsigned int tsc1_new; + unsigned int tsc2_new; + unsigned int cr0; unsigned int cr0_new; @@ -301,6 +304,11 @@ typedef struct _reg_dict{ unsigned int* ptr; } reg_dict; +typedef struct _reg_float_dict{ + char* name; + void* ptr; +} reg_float_dict; + extern reg_dict gpreg_dict[]; //PyObject* _vm_set_gpreg(PyObject *dict); @@ -376,7 +384,14 @@ double int2double(unsigned int m); //PyObject* _vm_exec_blocs(PyObject* my_eip); double fadd(double a, double b); +double fsub(double a, double b); +double fmul(double a, double b); double fdiv(double a, double b); +double ftan(double a); +double frndint(double a); +double fsin(double a); +double fscale(double a, double b); +double f2xm1(double a); unsigned int fcom_c0(double a, double b); unsigned int fcom_c1(double a, double b); unsigned int fcom_c2(double a, double b); |