Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
miasm2.jitter.jitload.jitter Class Reference
+ Inheritance diagram for miasm2.jitter.jitload.jitter:
+ Collaboration diagram for miasm2.jitter.jitload.jitter:

Public Member Functions

def __init__
 
def init_exceptions_handler
 
def add_breakpoint
 
def set_breakpoint
 
def remove_breakpoints_by_callback
 
def add_exception_handler
 
def runbloc
 
def runiter_once
 
def init_run
 
def continue_run
 
def init_stack
 
def get_exception
 
def get_str_ansi
 
def get_str_unic
 
def set_str_ansi
 
def set_str_unic
 
def handle_function
 
def add_lib_handler
 

Static Public Member Functions

def handle_lib
 

Public Attributes

 arch
 
 attrib
 
 vm
 
 cpu
 
 bs
 
 ir_arch
 
 jit
 
 stack_size
 
 stack_base
 
 breakpoints_handler
 
 exceptions_handler
 
 exec_cb
 
 pc
 
 run_iterator
 
 run
 
 libs
 
 user_globals
 

Detailed Description

Definition at line 173 of file jitload.py.

Constructor & Destructor Documentation

def miasm2.jitter.jitload.jitter.__init__ (   self,
  ir_arch,
  jit_type = "tcc" 
)
Init an instance of jitter.
@ir_arch: ir instance for this architecture
@jit_type: JiT backend to use. Available options are:
    - "tcc"
    - "llvm"
    - "python"

Definition at line 177 of file jitload.py.

178  def __init__(self, ir_arch, jit_type="tcc"):
179  """Init an instance of jitter.
180  @ir_arch: ir instance for this architecture
181  @jit_type: JiT backend to use. Available options are:
182  - "tcc"
183  - "llvm"
184  - "python"
185  """
187  self.arch = ir_arch.arch
188  self.attrib = ir_arch.attrib
189  arch_name = ir_arch.arch.name # (ir_arch.arch.name, ir_arch.attrib)
190  if arch_name == "x86":
191  from miasm2.jitter.arch import JitCore_x86 as jcore
192  elif arch_name == "arm":
193  from miasm2.jitter.arch import JitCore_arm as jcore
194  elif arch_name == "aarch64":
195  from miasm2.jitter.arch import JitCore_aarch64 as jcore
196  elif arch_name == "msp430":
197  from miasm2.jitter.arch import JitCore_msp430 as jcore
198  elif arch_name == "mips32":
199  from miasm2.jitter.arch import JitCore_mips32 as jcore
200  else:
201  raise ValueError("unsupported jit arch!")
203  self.vm = VmMngr.Vm()
204  self.cpu = jcore.JitCpu()
206  self.bs = bin_stream_vm(self.vm)
207  self.ir_arch = ir_arch
208  init_arch_C(self.arch)
209 
210  if jit_type == "tcc":
211  self.jit = JitCore_Tcc(self.ir_arch, self.bs)
212  elif jit_type == "llvm":
213  self.jit = JitCore_LLVM(self.ir_arch, self.bs)
214  elif jit_type == "python":
215  self.jit = JitCore_Python(self.ir_arch, self.bs)
216  else:
217  raise Exception("Unkown JiT Backend")
218 
219  self.cpu.init_regs()
220  self.vm.init_memory_page_pool()
221  self.vm.init_code_bloc_pool()
222  self.vm.init_memory_breakpoint()
223 
224  self.vm.set_addr2obj(self.jit.addr2obj)
225 
226  self.jit.load()
227  self.cpu.vmmngr = self.vm
228  self.cpu.jitter = self.jit
229  self.stack_size = 0x10000
230  self.stack_base = 0x1230000
231 
232 
233  # Init callback handler
237  self.exec_cb = None
def init_arch_C
Definition: ir2C.py:28

Member Function Documentation

def miasm2.jitter.jitload.jitter.add_breakpoint (   self,
  addr,
  callback 
)
Add a callback associated with addr.
@addr: breakpoint address
@callback: function with definition (jitter instance)

Definition at line 257 of file jitload.py.

258  def add_breakpoint(self, addr, callback):
259  """Add a callback associated with addr.
260  @addr: breakpoint address
261  @callback: function with definition (jitter instance)
262  """
263  self.breakpoints_handler.add_callback(addr, callback)
264  self.jit.add_disassembly_splits(addr)

+ Here is the caller graph for this function:

def miasm2.jitter.jitload.jitter.add_exception_handler (   self,
  flag,
  callback 
)
Add a callback associated with an exception flag.
@flag: bitflag
@callback: function with definition (jitter instance)

Definition at line 281 of file jitload.py.

282  def add_exception_handler(self, flag, callback):
283  """Add a callback associated with an exception flag.
284  @flag: bitflag
285  @callback: function with definition (jitter instance)
286  """
287  self.exceptions_handler.add_callback(flag, callback)

+ Here is the caller graph for this function:

def miasm2.jitter.jitload.jitter.add_lib_handler (   self,
  libs,
  user_globals = None 
)
Add a function to handle libs call with breakpoints
@libs: libimp instance
@user_globals: dictionnary for defined user function

Definition at line 424 of file jitload.py.

425  def add_lib_handler(self, libs, user_globals=None):
426  """Add a function to handle libs call with breakpoints
427  @libs: libimp instance
428  @user_globals: dictionnary for defined user function
429  """
430  if user_globals is None:
431  user_globals = {}
433  self.libs = libs
434  self.user_globals = user_globals
435 
436  for f_addr in libs.fad2cname:
437  self.handle_function(f_addr)
def miasm2.jitter.jitload.jitter.continue_run (   self,
  step = False 
)
PRE: init_run.
Continue the run of the current session until iterator returns or run is
set to False.
If step is True, run only one time.
Return the iterator value

Definition at line 336 of file jitload.py.

337  def continue_run(self, step=False):
338  """PRE: init_run.
339  Continue the run of the current session until iterator returns or run is
340  set to False.
341  If step is True, run only one time.
342  Return the iterator value"""
343 
344  while self.run:
345  try:
346  return self.run_iterator.next()
347  except StopIteration:
348  pass
349 
350  self.run_iterator = self.runiter_once(self.pc)
351 
352  if step is True:
353  return None
354 
355  return None

+ Here is the call graph for this function:

def miasm2.jitter.jitload.jitter.get_exception (   self)

Definition at line 365 of file jitload.py.

366  def get_exception(self):
367  return self.cpu.get_exception() | self.vm.get_exception()
def miasm2.jitter.jitload.jitter.get_str_ansi (   self,
  addr,
  max_char = None 
)
Get ansi str from vm.
@addr: address in memory
@max_char: maximum len

Definition at line 369 of file jitload.py.

370  def get_str_ansi(self, addr, max_char=None):
371  """Get ansi str from vm.
372  @addr: address in memory
373  @max_char: maximum len"""
374  l = 0
375  tmp = addr
376  while ((max_char is None or l < max_char) and
377  self.vm.get_mem(tmp, 1) != "\x00"):
378  tmp += 1
379  l += 1
380  return self.vm.get_mem(addr, l)
def miasm2.jitter.jitload.jitter.get_str_unic (   self,
  addr,
  max_char = None 
)
Get unicode str from vm.
@addr: address in memory
@max_char: maximum len

Definition at line 381 of file jitload.py.

382  def get_str_unic(self, addr, max_char=None):
383  """Get unicode str from vm.
384  @addr: address in memory
385  @max_char: maximum len"""
386  l = 0
387  tmp = addr
388  while ((max_char is None or l < max_char) and
389  self.vm.get_mem(tmp, 2) != "\x00\x00"):
390  tmp += 2
391  l += 2
392  s = self.vm.get_mem(addr, l)
393  s = s[::2] # TODO: real unicode decoding
394  return s
def miasm2.jitter.jitload.jitter.handle_function (   self,
  f_addr 
)
Add a brakpoint which will trigger the function handler

Definition at line 420 of file jitload.py.

421  def handle_function(self, f_addr):
422  """Add a brakpoint which will trigger the function handler"""
423  self.add_breakpoint(f_addr, self.handle_lib)

+ Here is the call graph for this function:

def miasm2.jitter.jitload.jitter.handle_lib (   jitter)
static
Resolve the name of the function which cause the handler call. Then
call the corresponding handler from users callback.

Definition at line 406 of file jitload.py.

407  def handle_lib(jitter):
408  """Resolve the name of the function which cause the handler call. Then
409  call the corresponding handler from users callback.
410  """
411  fname = jitter.libs.fad2cname[jitter.pc]
412  if fname in jitter.user_globals:
413  func = jitter.user_globals[fname]
414  else:
415  log.debug('%r', fname)
416  raise ValueError('unknown api', hex(jitter.pc), repr(fname))
417  func(jitter)
418  jitter.pc = getattr(jitter.cpu, jitter.ir_arch.pc.name)
419  return True

+ Here is the caller graph for this function:

def miasm2.jitter.jitload.jitter.init_exceptions_handler (   self)

Definition at line 238 of file jitload.py.

239  def init_exceptions_handler(self):
240  "Add common exceptions handlers"
241 
242  def exception_automod(jitter):
243  "Tell the JiT backend to update blocs modified"
244 
245  self.jit.updt_automod_code(jitter.vm)
246  self.vm.set_exception(0)
247 
248  return True
249 
250  def exception_memory_breakpoint(jitter):
251  "Stop the execution and return an identifier"
252  return ExceptionHandle.memoryBreakpoint()
253 
254  self.add_exception_handler(EXCEPT_CODE_AUTOMOD, exception_automod)
255  self.add_exception_handler(EXCEPT_BREAKPOINT_INTERN,
256  exception_memory_breakpoint)

+ Here is the call graph for this function:

def miasm2.jitter.jitload.jitter.init_run (   self,
  pc 
)
Create an iterator on pc with runiter.
@pc: address of code to run

Definition at line 328 of file jitload.py.

329  def init_run(self, pc):
330  """Create an iterator on pc with runiter.
331  @pc: address of code to run
332  """
333  self.run_iterator = self.runiter_once(pc)
334  self.pc = pc
335  self.run = True
def miasm2.jitter.jitload.jitter.init_stack (   self)

Definition at line 356 of file jitload.py.

357  def init_stack(self):
358  self.vm.add_memory_page(
359  self.stack_base, PAGE_READ | PAGE_WRITE, "\x00" * self.stack_size)
360  sp = self.arch.getsp(self.attrib)
361  setattr(self.cpu, sp.name, self.stack_base + self.stack_size)
362  # regs = self.cpu.get_gpreg()
363  # regs[sp.name] = self.stack_base+self.stack_size
364  # self.cpu.set_gpreg(regs)
def miasm2.jitter.jitload.jitter.remove_breakpoints_by_callback (   self,
  callback 
)
Remove callbacks associated with breakpoint.
@callback: callback to remove

Definition at line 273 of file jitload.py.

274  def remove_breakpoints_by_callback(self, callback):
275  """Remove callbacks associated with breakpoint.
276  @callback: callback to remove
277  """
278  empty_keys = self.breakpoints_handler.remove_callback(callback)
279  for key in empty_keys:
280  self.jit.remove_disassembly_splits(key)
def miasm2.jitter.jitload.jitter.runbloc (   self,
  pc 
)
Wrapper on JiT backend. Run the code at PC and return the next PC.
@pc: address of code to run

Definition at line 288 of file jitload.py.

289  def runbloc(self, pc):
290  """Wrapper on JiT backend. Run the code at PC and return the next PC.
291  @pc: address of code to run"""
292 
293  return self.jit.runbloc(self.cpu, self.vm, pc, self.breakpoints_handler.callbacks)
def miasm2.jitter.jitload.jitter.runiter_once (   self,
  pc 
)
Iterator on callbacks results on code running from PC.
Check exceptions before breakpoints.

Definition at line 294 of file jitload.py.

295  def runiter_once(self, pc):
296  """Iterator on callbacks results on code running from PC.
297  Check exceptions before breakpoints."""
299  self.pc = pc
300 
301  # Callback called before exec
302  if self.exec_cb is not None:
303  res = self.exec_cb(self)
304  if res is not True:
305  yield res
306 
307  # Check breakpoints
308  old_pc = self.pc
309  for res in self.breakpoints_handler.call_callbacks(self.pc, self):
310  if res is not True:
311  yield res
312 
313  # If a callback changed pc, re call every callback
314  if old_pc != self.pc:
315  return
316 
317  # Exceptions should never be activated before run
318  assert(self.get_exception() == 0)
319 
320  # Run the bloc at PC
321  self.pc = self.runbloc(self.pc)
322 
323  # Check exceptions
324  exception_flag = self.get_exception()
325  for res in self.exceptions_handler(exception_flag, self):
326  if res is not True:
327  yield res

+ Here is the caller graph for this function:

def miasm2.jitter.jitload.jitter.set_breakpoint (   self,
  addr,
  args 
)
Set callbacks associated with addr.
@addr: breakpoint address
@args: functions with definition (jitter instance)

Definition at line 265 of file jitload.py.

266  def set_breakpoint(self, addr, *args):
267  """Set callbacks associated with addr.
268  @addr: breakpoint address
269  @args: functions with definition (jitter instance)
270  """
271  self.breakpoints_handler.set_callback(addr, *args)
272  self.jit.add_disassembly_splits(addr)
def miasm2.jitter.jitload.jitter.set_str_ansi (   self,
  addr,
  s 
)
Set an ansi string in memory

Definition at line 395 of file jitload.py.

396  def set_str_ansi(self, addr, s):
397  """Set an ansi string in memory"""
398  s = s + "\x00"
399  self.vm.set_mem(addr, s)
def miasm2.jitter.jitload.jitter.set_str_unic (   self,
  addr,
  s 
)
Set an unicode string in memory

Definition at line 400 of file jitload.py.

401  def set_str_unic(self, addr, s):
402  """Set an unicode string in memory"""
403  s = "\x00".join(list(s)) + '\x00' * 3
404  self.vm.set_mem(addr, s)

Member Data Documentation

miasm2.jitter.jitload.jitter.arch

Definition at line 186 of file jitload.py.

miasm2.jitter.jitload.jitter.attrib

Definition at line 187 of file jitload.py.

miasm2.jitter.jitload.jitter.breakpoints_handler

Definition at line 233 of file jitload.py.

miasm2.jitter.jitload.jitter.bs

Definition at line 205 of file jitload.py.

miasm2.jitter.jitload.jitter.cpu

Definition at line 203 of file jitload.py.

miasm2.jitter.jitload.jitter.exceptions_handler

Definition at line 234 of file jitload.py.

miasm2.jitter.jitload.jitter.exec_cb

Definition at line 236 of file jitload.py.

miasm2.jitter.jitload.jitter.ir_arch

Definition at line 206 of file jitload.py.

miasm2.jitter.jitload.jitter.jit

Definition at line 210 of file jitload.py.

miasm2.jitter.jitload.jitter.libs

Definition at line 432 of file jitload.py.

miasm2.jitter.jitload.jitter.pc

Definition at line 298 of file jitload.py.

miasm2.jitter.jitload.jitter.run

Definition at line 334 of file jitload.py.

miasm2.jitter.jitload.jitter.run_iterator

Definition at line 332 of file jitload.py.

miasm2.jitter.jitload.jitter.stack_base

Definition at line 229 of file jitload.py.

miasm2.jitter.jitload.jitter.stack_size

Definition at line 228 of file jitload.py.

miasm2.jitter.jitload.jitter.user_globals

Definition at line 433 of file jitload.py.

miasm2.jitter.jitload.jitter.vm

Definition at line 202 of file jitload.py.


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