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_llvm.JitCore_LLVM Class Reference
+ Inheritance diagram for miasm2.jitter.jitcore_llvm.JitCore_LLVM:
+ Collaboration diagram for miasm2.jitter.jitcore_llvm.JitCore_LLVM:

Public Member Functions

def __init__
 
def load
 
def add_bloc
 
def jitirblocs
 
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 disbloc
 
def jit_call
 
def runbloc
 
def blocs2memrange
 
def del_bloc_in_range
 
def updt_automod_code
 
def automod_cb
 

Public Attributes

 exec_wrapper
 
 exec_engines
 
 ir_arch
 
 context
 
 arch
 
 mod_base_str
 
 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

dictionary arch_dependent_libs
 
 jitted_block_delete_cb = None
 
int jitted_block_max_size = 10000
 

Detailed Description

Definition at line 9 of file jitcore_llvm.py.

Constructor & Destructor Documentation

def miasm2.jitter.jitcore_llvm.JitCore_LLVM.__init__ (   self,
  ir_arch,
  bs = None 
)

Definition at line 19 of file jitcore_llvm.py.

19 
20  def __init__(self, ir_arch, bs=None):
21  super(JitCore_LLVM, self).__init__(ir_arch, bs)
22 
23  self.options.update({"safe_mode": False, # Verify each function
24  "optimise": False, # Optimise functions
25  "log_func": False, # Print LLVM functions
26  "log_assembly": False, # Print assembly executed
27  "cache_ir": None # SaveDir for cached .ll
28  })
29 
30  self.exec_wrapper = Jitllvm.llvm_exec_bloc
31  self.exec_engines = []
32  self.ir_arch = ir_arch

Member Function Documentation

def miasm2.jitter.jitcore_llvm.JitCore_LLVM.add_bloc (   self,
  bloc 
)

Definition at line 68 of file jitcore_llvm.py.

68 
69  def add_bloc(self, bloc):
70 
71  # Search in IR cache
72  if self.options["cache_ir"] is not None:
73 
74  # /!\ This part is under development
75  # Use it at your own risk
76 
77  # Compute Hash : label + bloc binary
78  func_name = bloc.label.name
79  to_hash = func_name
80 
81  # Get binary from bloc
82  for line in bloc.lines:
83  b = line.b
84  to_hash += b
85 
86  # Compute Hash
87  md5 = hashlib.md5(to_hash).hexdigest()
88 
89  # Try to load the function from cache
90  filename = self.options["cache_ir"] + md5 + ".ll"
91 
92  try:
93  fcontent = open(filename)
94  content = fcontent.read()
95  fcontent.close()
96 
97  except IOError:
98  content = None
99 
100  if content is None:
101  # Compute the IR
102  super(JitCore_LLVM, self).add_bloc(bloc)
103 
104  # Save it
105  fdest = open(filename, "w")
106  dump = str(self.context.mod.get_function_named(func_name))
107  my = "declare i16 @llvm.bswap.i16(i16) nounwind readnone\n"
108 
109  fdest.write(self.mod_base_str + my + dump)
110  fdest.close()
111 
112  else:
113  import llvm.core as llvm_c
114  import llvm.ee as llvm_e
115  my_mod = llvm_c.Module.from_assembly(content)
116  func = my_mod.get_function_named(func_name)
117  exec_en = llvm_e.ExecutionEngine.new(my_mod)
118  self.exec_engines.append(exec_en)
119 
120  # We can use the same exec_engine
121  ptr = self.exec_engines[0].get_pointer_to_function(func)
122 
123  # Store a pointer on the function jitted code
124  self.lbl2jitbloc[bloc.label.offset] = ptr
125 
126  else:
127  super(JitCore_LLVM, self).add_bloc(bloc)

+ 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.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.JitCore.jit_call (   self,
  label,
  cpu,
  _vmmngr,
  breakpoints 
)
inherited
Call the function label with cpu and vmmngr states
@label: function's label
@cpu: JitCpu instance
@breakpoints: Dict instance of used breakpoints

Definition at line 156 of file jitcore.py.

157  def jit_call(self, label, cpu, _vmmngr, breakpoints):
158  """Call the function label with cpu and vmmngr states
159  @label: function's label
160  @cpu: JitCpu instance
161  @breakpoints: Dict instance of used breakpoints
162  """
163  return self.exec_wrapper(label, cpu, self.lbl2jitbloc.data, breakpoints)

+ Here is the caller graph for this function:

def miasm2.jitter.jitcore_llvm.JitCore_LLVM.jitirblocs (   self,
  label,
  irblocs 
)

Definition at line 128 of file jitcore_llvm.py.

129  def jitirblocs(self, label, irblocs):
130 
131  # Build a function in the context
132  func = LLVMFunction(self.context, label.name)
133 
134  # Set log level
135  func.log_regs = self.log_regs
136  func.log_mn = self.log_mn
137 
138  # Import irblocs
139  func.from_blocs(irblocs)
140 
141  # Verify
142  if self.options["safe_mode"] is True:
143  func.verify()
144 
145  # Optimise
146  if self.options["optimise"] is True:
147  func.optimise()
148 
149  # Log
150  if self.options["log_func"] is True:
151  print func
152  if self.options["log_assembly"] is True:
153  print func.get_assembly()
154 
155  # Store a pointer on the function jitted code
156  self.lbl2jitbloc[label.offset] = func.get_function_pointer()
def miasm2.jitter.jitcore_llvm.JitCore_LLVM.load (   self)

Definition at line 33 of file jitcore_llvm.py.

33 
34  def load(self):
35 
36  # Library to load within Jit context
37  libs_to_load = []
38 
39  # Get architecture dependant Jitcore library (if any)
40  lib_dir = os.path.dirname(os.path.realpath(__file__))
41  lib_dir = os.path.join(lib_dir, 'arch')
42  try:
43  jit_lib = os.path.join(
44  lib_dir, self.arch_dependent_libs[self.ir_arch.arch.name])
45  libs_to_load.append(jit_lib)
46  except KeyError:
47  pass
48 
49  # Create a context
50  self.context = LLVMContext_JIT(libs_to_load)
51 
52  # Set the optimisation level
53  self.context.optimise_level()
54 
55  # Save the current architecture parameters
56  self.arch = self.ir_arch.arch
57 
58  # Get the correspondance between registers and vmcpu struct
59  mod_name = "miasm2.jitter.arch.JitCore_%s" % (self.ir_arch.arch.name)
60  mod = importlib.import_module(mod_name)
61  self.context.set_vmcpu(mod.get_gpreg_offset_all())
62 
63  # Save module base
64  self.mod_base_str = str(self.context.mod)
65 
66  # Set IRs transformation to apply
67  self.context.set_IR_transformation(self.ir_arch.expr_fix_regs_for_mode)
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_llvm.JitCore_LLVM.arch

Definition at line 55 of file jitcore_llvm.py.

dictionary miasm2.jitter.jitcore_llvm.JitCore_LLVM.arch_dependent_libs
static
Initial value:
1 = {"x86": "JitCore_x86.so",
2  "arm": "JitCore_arm.so",
3  "msp430": "JitCore_msp430.so",
4  "mips32": "JitCore_mips32.so"}

Definition at line 14 of file jitcore_llvm.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_llvm.JitCore_LLVM.context

Definition at line 49 of file jitcore_llvm.py.

miasm2.jitter.jitcore.JitCore.disasm_cb
inherited

Definition at line 52 of file jitcore.py.

miasm2.jitter.jitcore_llvm.JitCore_LLVM.exec_engines

Definition at line 30 of file jitcore_llvm.py.

miasm2.jitter.jitcore_llvm.JitCore_LLVM.exec_wrapper

Definition at line 29 of file jitcore_llvm.py.

miasm2.jitter.jitcore_llvm.JitCore_LLVM.ir_arch

Definition at line 31 of file jitcore_llvm.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_llvm.JitCore_LLVM.mod_base_str

Definition at line 63 of file jitcore_llvm.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.


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