Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
jit.py
Go to the documentation of this file.
1 import logging
2 
3 from miasm2.jitter.jitload import jitter, named_arguments
4 from miasm2.core import asmbloc
5 from miasm2.core.utils import *
6 from miasm2.arch.x86.sem import ir_x86_16, ir_x86_32, ir_x86_64
7 
8 log = logging.getLogger('jit_x86')
9 hnd = logging.StreamHandler()
10 hnd.setFormatter(logging.Formatter("[%(levelname)s]: %(message)s"))
11 log.addHandler(hnd)
12 log.setLevel(logging.CRITICAL)
13 
15 
16  def __init__(self, *args, **kwargs):
17  sp = asmbloc.asm_symbol_pool()
18  jitter.__init__(self, ir_x86_16(sp), *args, **kwargs)
19  self.vm.set_little_endian()
20  self.ir_arch.jit_pc = self.ir_arch.arch.regs.RIP
21  self.ir_arch.do_stk_segm = False
22  self.orig_irbloc_fix_regs_for_mode = self.ir_arch.irbloc_fix_regs_for_mode
23  self.ir_arch.irbloc_fix_regs_for_mode = self.ir_archbloc_fix_regs_for_mode
24 
25  def ir_archbloc_fix_regs_for_mode(self, irbloc, attrib=64):
26  self.orig_irbloc_fix_regs_for_mode(irbloc, 64)
27 
28  def push_uint16_t(self, v):
29  self.cpu.SP -= self.ir_arch.sp.size / 8
30  self.vm.set_mem(self.cpu.SP, pck16(v))
31 
32  def pop_uint16_t(self):
33  x = upck16(self.vm.get_mem(self.cpu.SP, self.ir_arch.sp.size / 8))
34  self.cpu.SP += self.ir_arch.sp.size / 8
35  return x
36 
37  def get_stack_arg(self, n):
38  x = upck16(self.vm.get_mem(self.cpu.SP + 4 * n, 4))
39  return x
40 
41  def init_run(self, *args, **kwargs):
42  jitter.init_run(self, *args, **kwargs)
43  self.cpu.IP = self.pc
44 
45 
47 
48  def __init__(self, *args, **kwargs):
49  sp = asmbloc.asm_symbol_pool()
50  jitter.__init__(self, ir_x86_32(sp), *args, **kwargs)
51  self.vm.set_little_endian()
52  self.ir_arch.jit_pc = self.ir_arch.arch.regs.RIP
53  self.ir_arch.do_stk_segm = False
54 
55  self.orig_irbloc_fix_regs_for_mode = self.ir_arch.irbloc_fix_regs_for_mode
56  self.ir_arch.irbloc_fix_regs_for_mode = self.ir_archbloc_fix_regs_for_mode
57 
58  def ir_archbloc_fix_regs_for_mode(self, irbloc, attrib=64):
59  self.orig_irbloc_fix_regs_for_mode(irbloc, 64)
60 
61  def push_uint32_t(self, v):
62  self.cpu.ESP -= self.ir_arch.sp.size / 8
63  self.vm.set_mem(self.cpu.ESP, pck32(v))
64 
65  def pop_uint32_t(self):
66  x = upck32(self.vm.get_mem(self.cpu.ESP, self.ir_arch.sp.size / 8))
67  self.cpu.ESP += self.ir_arch.sp.size / 8
68  return x
69 
70  def get_stack_arg(self, n):
71  x = upck32(self.vm.get_mem(self.cpu.ESP + 4 * n, 4))
72  return x
73 
74  # calling conventions
75 
76  # stdcall
77  @named_arguments
78  def func_args_stdcall(self, n_args):
79  ret_ad = self.pop_uint32_t()
80  args = [self.pop_uint32_t() for _ in xrange(n_args)]
81  return ret_ad, args
82 
83  def func_ret_stdcall(self, ret_addr, ret_value1=None, ret_value2=None):
84  self.cpu.EIP = ret_addr
85  if ret_value1 is not None:
86  self.cpu.EAX = ret_value1
87  if ret_value2 is not None:
88  self.cpu.EDX = ret_value2
89 
90  # cdecl
91  @named_arguments
92  def func_args_cdecl(self, n_args):
93  ret_ad = self.pop_uint32_t()
94  args = [self.get_stack_arg(i) for i in xrange(n_args)]
95  return ret_ad, args
96 
97  def func_ret_cdecl(self, ret_addr, ret_value):
98  self.cpu.EIP = ret_addr
99  self.cpu.EAX = ret_value
100 
101  def init_run(self, *args, **kwargs):
102  jitter.init_run(self, *args, **kwargs)
103  self.cpu.EIP = self.pc
104 
105 
107 
108  def __init__(self, *args, **kwargs):
109  sp = asmbloc.asm_symbol_pool()
110  jitter.__init__(self, ir_x86_64(sp), *args, **kwargs)
111  self.vm.set_little_endian()
112  self.ir_arch.jit_pc = self.ir_arch.arch.regs.RIP
113  self.ir_arch.do_stk_segm = False
114 
115  self.orig_irbloc_fix_regs_for_mode = self.ir_arch.irbloc_fix_regs_for_mode
116  self.ir_arch.irbloc_fix_regs_for_mode = self.ir_archbloc_fix_regs_for_mode
117 
118  def ir_archbloc_fix_regs_for_mode(self, irbloc, attrib=64):
119  self.orig_irbloc_fix_regs_for_mode(irbloc, 64)
120 
121  def push_uint64_t(self, v):
122  self.cpu.RSP -= self.ir_arch.sp.size / 8
123  self.vm.set_mem(self.cpu.RSP, pck64(v))
124 
125  def pop_uint64_t(self):
126  x = upck64(self.vm.get_mem(self.cpu.RSP, self.ir_arch.sp.size / 8))
127  self.cpu.RSP += self.ir_arch.sp.size / 8
128  return x
129 
130  def get_stack_arg(self, n):
131  x = upck64(self.vm.get_mem(self.cpu.RSP + 8 * n, 8))
132  return x
133 
134  @named_arguments
135  def func_args_stdcall(self, n_args):
136  args_regs = ['RCX', 'RDX', 'R8', 'R9']
137  ret_ad = self.pop_uint64_t()
138  args = []
139  for i in xrange(min(n_args, 4)):
140  args.append(self.cpu.get_gpreg()[args_regs[i]])
141  for i in xrange(max(0, n_args - 4)):
142  args.append(self.get_stack_arg(i))
143  return ret_ad, args
144 
145  def func_ret_stdcall(self, ret_addr, ret_value=None):
146  self.pc = self.cpu.RIP = ret_addr
147  if ret_value is not None:
148  self.cpu.RAX = ret_value
149  return True
150 
151  @named_arguments
152  def func_args_cdecl(self, n_args):
153  args_regs = ['RCX', 'RDX', 'R8', 'R9']
154  ret_ad = self.pop_uint64_t()
155  args = []
156  for i in xrange(min(n_args, 4)):
157  args.append(self.cpu.get_gpreg()[args_regs[i]])
158  for i in xrange(max(0, n_args - 4)):
159  args.append(self.get_stack_arg(i))
160  return ret_ad, args
161 
162  def func_ret_cdecl(self, ret_addr, ret_value=None):
163  self.pc = self.cpu.RIP = ret_addr
164  if ret_value is not None:
165  self.cpu.RAX = ret_value
166  return True
167 
168  def init_run(self, *args, **kwargs):
169  jitter.init_run(self, *args, **kwargs)
170  self.cpu.RIP = self.pc
tuple upck64
Definition: utils.py:9
tuple upck16
Definition: utils.py:7
tuple pck16
Definition: utils.py:11
tuple upck32
Definition: utils.py:8
tuple pck32
Definition: utils.py:12
tuple pck64
Definition: utils.py:13