Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
miasm2.jitter.jitcore_python.JitCore_Python Class Reference

Python jitter Core #. More...

+ Inheritance diagram for miasm2.jitter.jitcore_python.JitCore_Python:
+ Collaboration diagram for miasm2.jitter.jitcore_python.JitCore_Python:

Public Member Functions

def __init__
 
def load
 
def func_read
 
def func_write
 
def jitirblocs
 
def jit_call
 
def set_options
 
def add_disassembly_splits
 
def remove_disassembly_splits
 
def load
 
def get_bloc_min_max
 
def add_bloc_to_mem_interval
 
def add_bloc
 
def disbloc
 
def runbloc
 
def blocs2memrange
 
def del_bloc_in_range
 
def updt_automod_code
 
def automod_cb
 

Public Attributes

 symbexec
 
 ir_arch
 
 cpu
 
 bs
 
 known_blocs
 
 lbl2jitbloc
 
 lbl2bloc
 
 log_mn
 
 log_regs
 
 log_newbloc
 
 segm_to_do
 
 job_done
 
 jitcount
 
 addr2obj
 
 addr2objref
 
 blocs_mem_interval
 
 disasm_cb
 
 split_dis
 
 addr_mod
 
 options
 

Static Public Attributes

 jitted_block_delete_cb = None
 
int jitted_block_max_size = 10000
 

Detailed Description

Python jitter Core #.

Definition at line 49 of file jitcore_python.py.

Constructor & Destructor Documentation

def miasm2.jitter.jitcore_python.JitCore_Python.__init__ (   self,
  ir_arch,
  bs = None 
)

Definition at line 52 of file jitcore_python.py.

52 
53  def __init__(self, ir_arch, bs=None):
54  super(JitCore_Python, self).__init__(ir_arch, bs)
55  self.symbexec = None
56  self.ir_arch = ir_arch

Member Function Documentation

def miasm2.jitter.jitcore.JitCore.add_bloc (   self,
  b 
)
inherited
Add a bloc to JiT and JiT it.
@b: the bloc to add

Definition at line 102 of file jitcore.py.

103  def add_bloc(self, b):
104  """Add a bloc to JiT and JiT it.
105  @b: the bloc to add
106  """
107 
108  irblocs = self.ir_arch.add_bloc(b, gen_pc_updt = True)
109  b.irblocs = irblocs
110  self.jitirblocs(b.label, irblocs)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore.JitCore.add_bloc_to_mem_interval (   self,
  vm,
  bloc 
)
inherited

Definition at line 85 of file jitcore.py.

85 
86  def add_bloc_to_mem_interval(self, vm, bloc):
87  "Update vm to include bloc addresses in its memory range"
88 
89  self.blocs_mem_interval += interval([(bloc.ad_min, bloc.ad_max - 1)])
90 
91  vm.reset_code_bloc_pool()
92  for a, b in self.blocs_mem_interval:
93  vm.add_code_bloc(a, b + 1)

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore.JitCore.add_disassembly_splits (   self,
  args 
)
inherited
The disassembly engine will stop on address in args if they
are not at the block beginning

Definition at line 64 of file jitcore.py.

64 
65  def add_disassembly_splits(self, *args):
66  """The disassembly engine will stop on address in args if they
67  are not at the block beginning"""
68  self.split_dis.update(set(args))
def miasm2.jitter.jitcore.JitCore.automod_cb (   self,
  addr = 0,
  size = 0 
)
inherited

Definition at line 260 of file jitcore.py.

261  def automod_cb(self, addr=0, size=0):
262  self.addr_mod += interval([(addr, addr + size / 8 - 1)])
263  return None
def miasm2.jitter.jitcore.JitCore.blocs2memrange (   self,
  blocs 
)
inherited
Return an interval instance standing for blocs addresses
@blocs: list of asm_bloc instances

Definition at line 183 of file jitcore.py.

184  def blocs2memrange(self, blocs):
185  """Return an interval instance standing for blocs addresses
186  @blocs: list of asm_bloc instances
187  """
188 
189  mem_range = interval()
190 
191  for b in blocs:
192  mem_range += interval([(b.ad_min, b.ad_max - 1)])
193 
194  return mem_range

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore.JitCore.del_bloc_in_range (   self,
  ad1,
  ad2 
)
inherited
Find and remove jitted bloc in range [ad1, ad2].
Return the list of bloc removed.
@ad1: First address
@ad2: Last address

Definition at line 207 of file jitcore.py.

208  def del_bloc_in_range(self, ad1, ad2):
209  """Find and remove jitted bloc in range [ad1, ad2].
210  Return the list of bloc removed.
211  @ad1: First address
212  @ad2: Last address
213  """
214 
215  # Find concerned blocs
216  modified_blocs = set()
217  for b in self.lbl2bloc.values():
218  if not b.lines:
219  continue
220  if b.ad_max <= ad1 or b.ad_min >= ad2:
221  # Bloc not modified
222  pass
223  else:
224  # Modified blocs
225  modified_blocs.add(b)
226 
227  # Generate interval to delete
228  del_interval = self.blocs2memrange(modified_blocs)
229 
230  # Remove interval from monitored interval list
231  self.blocs_mem_interval -= del_interval
232 
233  # Remove modified blocs
234  for b in modified_blocs:
235  try:
236  for irbloc in b.irblocs:
237 
238  # Remove offset -> jitted bloc link
239  if irbloc.label.offset in self.lbl2jitbloc:
240  del(self.lbl2jitbloc[irbloc.label.offset])
241 
242  except AttributeError:
243  # The bloc has never been translated in IR
244  if b.label.offset in self.lbl2jitbloc:
245  del(self.lbl2jitbloc[b.label.offset])
246 
247  # Remove label -> bloc link
248  del(self.lbl2bloc[b.label])
249 
250  return modified_blocs

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore.JitCore.disbloc (   self,
  addr,
  cpu,
  vm 
)
inherited

Definition at line 111 of file jitcore.py.

112  def disbloc(self, addr, cpu, vm):
113  "Disassemble a new bloc and JiT it"
114 
115  # Get the bloc
116  if isinstance(addr, asmbloc.asm_label):
117  addr = addr.offset
118 
119  l = self.ir_arch.symbol_pool.getby_offset_create(addr)
120  cur_bloc = asmbloc.asm_bloc(l)
121 
122  # Disassemble it
123  try:
124  asmbloc.dis_bloc(self.ir_arch.arch, self.bs, cur_bloc, addr,
125  set(), self.ir_arch.symbol_pool, [],
126  follow_call=False, dontdis_retcall=False,
127  lines_wd=self.options["jit_maxline"],
128  # max 10 asm lines
129  attrib=self.ir_arch.attrib,
130  split_dis=self.split_dis)
131  except IOError:
132  # vm_exception_flag is set
133  pass
134 
135  # Logging
136  if self.log_newbloc:
137  print cur_bloc
138  if self.disasm_cb is not None:
139  self.disasm_cb(cur_bloc)
140 
141  # Check for empty blocks
142  if not cur_bloc.lines:
143  raise ValueError("Cannot JIT a block without any assembly line")
144 
145  # Update label -> bloc
146  self.lbl2bloc[l] = cur_bloc
147 
148  # Store min/max bloc address needed in jit automod code
149  self.get_bloc_min_max(cur_bloc)
150 
151  # JiT it
152  self.add_bloc(cur_bloc)
153 
154  # Update jitcode mem range
155  self.add_bloc_to_mem_interval(vm, cur_bloc)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore_python.JitCore_Python.func_read (   self,
  expr_mem 
)
Memory read wrapper for symbolic execution
@expr_mem: ExprMem

Definition at line 68 of file jitcore_python.py.

68 
69  def func_read(self, expr_mem):
70  """Memory read wrapper for symbolic execution
71  @expr_mem: ExprMem"""
72 
73  addr = expr_mem.arg.arg.arg
74  size = expr_mem.size / 8
75  value = self.cpu.get_mem(addr, size)
76 
77  return m2_expr.ExprInt(int(value[::-1].encode("hex"), 16),
78  expr_mem.size)

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore_python.JitCore_Python.func_write (   self,
  symb_exec,
  dest,
  data,
  mem_cache 
)
Memory read wrapper for symbolic execution
@symb_exec: symbexec instance
@dest: ExprMem instance
@data: Expr instance
@mem_cache: dict

Definition at line 79 of file jitcore_python.py.

79 
80  def func_write(self, symb_exec, dest, data, mem_cache):
81  """Memory read wrapper for symbolic execution
82  @symb_exec: symbexec instance
83  @dest: ExprMem instance
84  @data: Expr instance
85  @mem_cache: dict"""
86 
87  # Get the content to write
88  data = expr_simp(data)
89  if not isinstance(data, m2_expr.ExprInt):
90  raise NotImplementedError("A simplification is missing: %s" % data)
91  to_write = data.arg.arg
92 
93  # Format information
94  addr = dest.arg.arg.arg
95  size = data.size / 8
96  content = hex(to_write).replace("0x", "").replace("L", "")
97  content = "0" * (size * 2 - len(content)) + content
98  content = content.decode("hex")[::-1]
99 
100  # Write in VmMngr context
101  self.cpu.set_mem(addr, content)

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore.JitCore.get_bloc_min_max (   self,
  cur_bloc 
)
inherited

Definition at line 78 of file jitcore.py.

78 
79  def get_bloc_min_max(self, cur_bloc):
80  "Update cur_bloc to set min/max address"
81 
82  if cur_bloc.lines:
83  cur_bloc.ad_min = cur_bloc.lines[0].offset
84  cur_bloc.ad_max = cur_bloc.lines[-1].offset + cur_bloc.lines[-1].l

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore_python.JitCore_Python.jit_call (   self,
  label,
  cpu,
  vmmngr,
  _breakpoints 
)
Call the function label with cpu and vmmngr states
@label: function's label
@cpu: JitCpu instance
@vm: VmMngr instance

Definition at line 186 of file jitcore_python.py.

187  def jit_call(self, label, cpu, vmmngr, _breakpoints):
188  """Call the function label with cpu and vmmngr states
189  @label: function's label
190  @cpu: JitCpu instance
191  @vm: VmMngr instance
192  """
193 
194  # Get Python function corresponding to @label
195  fc_ptr = self.lbl2jitbloc[label]
197  self.cpu = cpu
198 
199  # Execute the function
200  return fc_ptr(cpu, vmmngr)
def miasm2.jitter.jitcore_python.JitCore_Python.jitirblocs (   self,
  label,
  irblocs 
)
Create a python function corresponding to an irblocs' group.
@label: the label of the irblocs
@irblocs: a gorup of irblocs

Definition at line 102 of file jitcore_python.py.

103  def jitirblocs(self, label, irblocs):
104  """Create a python function corresponding to an irblocs' group.
105  @label: the label of the irblocs
106  @irblocs: a gorup of irblocs
107  """
108 
109  def myfunc(cpu, vmmngr):
110  """Execute the function according to cpu and vmmngr states
111  @cpu: JitCpu instance
112  @vm: VmMngr instance
113  """
114 
115  # Keep current location in irblocs
116  cur_label = label
117  loop = True
118 
119  # Required to detect new instructions
120  offsets_jitted = set()
121 
122  # Get exec engine
123  exec_engine = self.symbexec
124 
125  # For each irbloc inside irblocs
126  while loop is True:
127 
128  # Get the current bloc
129  loop = False
130  for irb in irblocs:
131  if irb.label == cur_label:
132  loop = True
133  break
134 
135  # Irblocs must end with returning an ExprInt instance
136  assert(loop is not False)
137 
138  # Refresh CPU values according to @cpu instance
139  update_engine_from_cpu(cpu, exec_engine)
140 
141  # Execute current ir bloc
142  for ir, line in zip(irb.irs, irb.lines):
143 
144  # For each new instruction (in assembly)
145  if line.offset not in offsets_jitted:
146  offsets_jitted.add(line.offset)
147 
148  # Log registers values
149  if self.log_regs:
150  update_cpu_from_engine(cpu, exec_engine)
151  cpu.dump_gpregs()
152 
153  # Log instruction
154  if self.log_mn:
155  print "%08x %s" % (line.offset, line)
156 
157  # Check for memory exception
158  if (vmmngr.get_exception() != 0):
159  update_cpu_from_engine(cpu, exec_engine)
160  return line.offset
161 
162  # Eval current instruction (in IR)
163  exec_engine.eval_ir(ir)
164 
165  # Check for memory exception which do not update PC
166  if (vmmngr.get_exception() & csts.EXCEPT_DO_NOT_UPDATE_PC != 0):
167  update_cpu_from_engine(cpu, exec_engine)
168  return line.offset
169 
170  # Get next bloc address
171  ad = expr_simp(exec_engine.eval_expr(self.ir_arch.IRDst))
172 
173  # Updates @cpu instance according to new CPU values
174  update_cpu_from_engine(cpu, exec_engine)
175 
176  # Manage resulting address
177  if isinstance(ad, m2_expr.ExprInt):
178  return ad.arg.arg
179  elif isinstance(ad, m2_expr.ExprId):
180  cur_label = ad.name
181  else:
182  raise NotImplementedError("Type not handled: %s" % ad)
183 
184  # Associate myfunc with current label
185  self.lbl2jitbloc[label.offset] = myfunc
def update_cpu_from_engine
Util methods for Python jitter #.

+ Here is the call graph for this function:

def miasm2.jitter.jitcore_python.JitCore_Python.load (   self)

Definition at line 57 of file jitcore_python.py.

57 
58  def load(self):
59  "Preload symbols according to current architecture"
60 
61  symbols_init = {}
62  for r in self.ir_arch.arch.regs.all_regs_ids_no_alias:
63  symbols_init[r] = self.ir_arch.arch.regs.regs_init[r]
64 
65  self.symbexec = symbexec(self.ir_arch, symbols_init,
66  func_read = self.func_read,
67  func_write = self.func_write)

+ Here is the call graph for this function:

def miasm2.jitter.jitcore.JitCore.load (   self,
  arch,
  attrib 
)
inherited

Definition at line 73 of file jitcore.py.

73 
74  def load(self, arch, attrib):
75  "Initialise the Jitter according to arch and attrib"
76 
77  raise NotImplementedError("Abstract class")
def miasm2.jitter.jitcore.JitCore.remove_disassembly_splits (   self,
  args 
)
inherited
The disassembly engine will no longer stop on address in args

Definition at line 69 of file jitcore.py.

69 
70  def remove_disassembly_splits(self, *args):
71  """The disassembly engine will no longer stop on address in args"""
72  self.split_dis.difference_update(set(args))
def miasm2.jitter.jitcore.JitCore.runbloc (   self,
  cpu,
  vm,
  lbl,
  breakpoints 
)
inherited
Run the bloc starting at lbl.
@cpu: JitCpu instance
@vm: VmMngr instance
@lbl: target label

Definition at line 164 of file jitcore.py.

165  def runbloc(self, cpu, vm, lbl, breakpoints):
166  """Run the bloc starting at lbl.
167  @cpu: JitCpu instance
168  @vm: VmMngr instance
169  @lbl: target label
170  """
171 
172  if lbl is None:
173  lbl = cpu.get_gpreg()[self.ir_arch.pc.name]
174 
175  if not lbl in self.lbl2jitbloc:
176  # Need to JiT the bloc
177  self.disbloc(lbl, cpu, vm)
178 
179  # Run the bloc and update cpu/vmmngr state
180  ret = self.jit_call(lbl, cpu, vm, breakpoints)
181 
182  return ret

+ Here is the call graph for this function:

def miasm2.jitter.jitcore.JitCore.set_options (   self,
  kwargs 
)
inherited

Definition at line 59 of file jitcore.py.

59 
60  def set_options(self, **kwargs):
61  "Set options relative to the backend"
62 
63  self.options.update(kwargs)
def miasm2.jitter.jitcore.JitCore.updt_automod_code (   self,
  vm 
)
inherited
Remove code jitted in range self.addr_mod
@vm: VmMngr instance

Definition at line 251 of file jitcore.py.

252  def updt_automod_code(self, vm):
253  """Remove code jitted in range self.addr_mod
254  @vm: VmMngr instance
255  """
256  for addr_start, addr_stop in self.addr_mod:
257  self.del_bloc_in_range(addr_start, addr_stop + 1)
258  self.__updt_jitcode_mem_range(vm)
259  self.addr_mod = interval()

+ Here is the call graph for this function:

Member Data Documentation

miasm2.jitter.jitcore.JitCore.addr2obj
inherited

Definition at line 49 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.addr2objref
inherited

Definition at line 50 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.addr_mod
inherited

Definition at line 54 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.blocs_mem_interval
inherited

Definition at line 51 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.bs
inherited

Definition at line 38 of file jitcore.py.

miasm2.jitter.jitcore_python.JitCore_Python.cpu

Definition at line 196 of file jitcore_python.py.

miasm2.jitter.jitcore.JitCore.disasm_cb
inherited

Definition at line 52 of file jitcore.py.

miasm2.jitter.jitcore_python.JitCore_Python.ir_arch

Definition at line 55 of file jitcore_python.py.

miasm2.jitter.jitcore.JitCore.jitcount
inherited

Definition at line 48 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.jitted_block_delete_cb = None
staticinherited

Definition at line 28 of file jitcore.py.

int miasm2.jitter.jitcore.JitCore.jitted_block_max_size = 10000
staticinherited

Definition at line 29 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.job_done
inherited

Definition at line 47 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.known_blocs
inherited

Definition at line 39 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.lbl2bloc
inherited

Definition at line 42 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.lbl2jitbloc
inherited

Definition at line 40 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.log_mn
inherited

Definition at line 43 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.log_newbloc
inherited

Definition at line 45 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.log_regs
inherited

Definition at line 44 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.options
inherited

Definition at line 56 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.segm_to_do
inherited

Definition at line 46 of file jitcore.py.

miasm2.jitter.jitcore.JitCore.split_dis
inherited

Definition at line 53 of file jitcore.py.

miasm2.jitter.jitcore_python.JitCore_Python.symbexec

Definition at line 54 of file jitcore_python.py.


The documentation for this class was generated from the following file: