Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
Classes | Functions
miasm2.analysis.data_analysis Namespace Reference

Classes

class  symb_exec_func
 

Functions

def get_node_name
 
def intra_bloc_flow_raw
 
def intra_bloc_flow_symbexec
 
def inter_bloc_flow_link
 
def create_implicit_flow
 
def inter_bloc_flow
 

Function Documentation

def miasm2.analysis.data_analysis.create_implicit_flow (   ir_arch,
  flow_graph 
)

Definition at line 161 of file data_analysis.py.

162 def create_implicit_flow(ir_arch, flow_graph):
163 
164  # first fix IN/OUT
165  # If a son read a node which in not in OUT, add it
166  todo = set(ir_arch.blocs.keys())
167  while todo:
168  lbl = todo.pop()
169  irb = ir_arch.blocs[lbl]
170  for lbl_son in ir_arch.g.successors(irb.label):
171  if not lbl_son in ir_arch.blocs:
172  print "cannot find bloc!!", lbl
173  continue
174  irb_son = ir_arch.blocs[lbl_son]
175  for n_r in irb_son.in_nodes:
176  if n_r in irb.out_nodes:
177  continue
178  if not isinstance(n_r, ExprId):
179  continue
180 
181  # print "###", n_r
182  # print "###", irb
183  # print "###", 'OUT', [str(x) for x in irb.out_nodes]
184  # print "###", irb_son
185  # print "###", 'IN', [str(x) for x in irb_son.in_nodes]
186 
187  node_n_w = irb.label, len(irb.lines), n_r
188  irb.out_nodes[n_r] = node_n_w
189  if not n_r in irb.in_nodes:
190  irb.in_nodes[n_r] = irb.label, 0, n_r
191  node_n_r = irb.in_nodes[n_r]
192  # print "###", node_n_r
193  for lbl_p in ir_arch.g.predecessors(irb.label):
194  todo.add(lbl_p)
195 
196  flow_graph.add_uniq_edge(node_n_r, node_n_w)
197 
def miasm2.analysis.data_analysis.get_node_name (   label,
  i,
  n 
)

Definition at line 6 of file data_analysis.py.

6 
7 def get_node_name(label, i, n):
8  # n_name = "%s_%d_%s"%(label.name, i, n)
9  n_name = (label, i, n)
10  return n_name
11 

+ Here is the caller graph for this function:

def miasm2.analysis.data_analysis.inter_bloc_flow (   ir_arch,
  flow_graph,
  irb_0,
  link_exec_to_data = True 
)

Definition at line 198 of file data_analysis.py.

199 def inter_bloc_flow(ir_arch, flow_graph, irb_0, link_exec_to_data=True):
200 
201  todo = set()
202  done = set()
203  todo.add((irb_0, (), ()))
204 
205  while todo:
206  state = todo.pop()
207  if state in done:
208  continue
209  done.add(state)
210  out = inter_bloc_flow_link(ir_arch, flow_graph, state, link_exec_to_data)
211  todo.update(out)
212 

+ Here is the call graph for this function:

def miasm2.analysis.data_analysis.inter_bloc_flow_link (   ir_arch,
  flow_graph,
  todo,
  link_exec_to_data 
)

Definition at line 114 of file data_analysis.py.

115 def inter_bloc_flow_link(ir_arch, flow_graph, todo, link_exec_to_data):
116  lbl, current_nodes, exec_nodes = todo
117  # print 'TODO'
118  # print lbl
119  # print [(str(x[0]), str(x[1])) for x in current_nodes]
120  current_nodes = dict(current_nodes)
121 
122  # link current nodes to bloc in_nodes
123  if not lbl in ir_arch.blocs:
124  print "cannot find bloc!!", lbl
125  return set()
126  irb = ir_arch.blocs[lbl]
127  # pp(('IN', lbl, [(str(x[0]), str(x[1])) for x in current_nodes.items()]))
128  to_del = set()
129  for n_r, node_n_r in irb.in_nodes.items():
130  if not n_r in current_nodes:
131  continue
132  # print 'add link', current_nodes[n_r], node_n_r
133  flow_graph.add_uniq_edge(current_nodes[n_r], node_n_r)
134  to_del.add(n_r)
135 
136  # if link exec to data, all nodes depends on exec nodes
137  if link_exec_to_data:
138  for n_x_r in exec_nodes:
139  for n_r, node_n_r in irb.in_nodes.items():
140  if not n_x_r in current_nodes:
141  continue
142  if isinstance(n_r, ExprInt):
143  continue
144  flow_graph.add_uniq_edge(current_nodes[n_x_r], node_n_r)
145 
146  # update current nodes using bloc out_nodes
147  for n_w, node_n_w in irb.out_nodes.items():
148  current_nodes[n_w] = node_n_w
149 
150  # get nodes involved in exec flow
151  x_nodes = tuple(sorted(list(irb.dst.get_r())))
152 
153  todo = set()
154  for lbl_dst in ir_arch.g.successors(irb.label):
155  todo.add((lbl_dst, tuple(current_nodes.items()), x_nodes))
156 
157  # pp(('OUT', lbl, [(str(x[0]), str(x[1])) for x in current_nodes.items()]))
158 
159  return todo
160 

+ Here is the caller graph for this function:

def miasm2.analysis.data_analysis.intra_bloc_flow_raw (   ir_arch,
  flow_graph,
  irb 
)
Create data flow for an irbloc using raw IR expressions

Definition at line 12 of file data_analysis.py.

12 
13 def intra_bloc_flow_raw(ir_arch, flow_graph, irb):
14  """
15  Create data flow for an irbloc using raw IR expressions
16  """
17  in_nodes = {}
18  out_nodes = {}
19  current_nodes = {}
20  for i, exprs in enumerate(irb.irs):
21  list_rw = get_list_rw(exprs)
22  current_nodes.update(out_nodes)
23 
24  # gen mem arg to mem node links
25  all_mems = set()
26  for nodes_r, nodes_w in list_rw:
27  for n in nodes_r.union(nodes_w):
28  all_mems.update(get_expr_mem(n))
29  if not all_mems:
30  continue
31 
32  # print [str(x) for x in all_mems]
33  for n in all_mems:
34  node_n_w = get_node_name(irb.label, i, n)
35  if not n in nodes_r:
36  continue
37  o_r = n.arg.get_r(mem_read=False, cst_read=True)
38  for n_r in o_r:
39  if n_r in current_nodes:
40  node_n_r = current_nodes[n_r]
41  else:
42  node_n_r = get_node_name(irb.label, i, n_r)
43  current_nodes[n_r] = node_n_r
44  in_nodes[n_r] = node_n_r
45  flow_graph.add_uniq_edge(node_n_r, node_n_w)
46 
47  # gen data flow links
48  for nodes_r, nodes_w in list_rw:
49  for n_r in nodes_r:
50  if n_r in current_nodes:
51  node_n_r = current_nodes[n_r]
52  else:
53  node_n_r = get_node_name(irb.label, i, n_r)
54  current_nodes[n_r] = node_n_r
55  in_nodes[n_r] = node_n_r
56 
57  flow_graph.add_node(node_n_r)
58  for n_w in nodes_w:
59  node_n_w = get_node_name(irb.label, i + 1, n_w)
60  out_nodes[n_w] = node_n_w
61  # current_nodes[n_w] = node_n_w
62 
63  flow_graph.add_node(node_n_w)
64  flow_graph.add_uniq_edge(node_n_r, node_n_w)
65  irb.in_nodes = in_nodes
66  irb.out_nodes = out_nodes
67 

+ Here is the call graph for this function:

def miasm2.analysis.data_analysis.intra_bloc_flow_symbexec (   ir_arch,
  flow_graph,
  irb 
)
Create data flow for an irbloc using symbolic execution

Definition at line 68 of file data_analysis.py.

68 
69 def intra_bloc_flow_symbexec(ir_arch, flow_graph, irb):
70  """
71  Create data flow for an irbloc using symbolic execution
72  """
73  in_nodes = {}
74  out_nodes = {}
75  current_nodes = {}
76 
77  symbols_init = {}
78  for r in ir_arch.arch.regs.all_regs_ids:
79  # symbols_init[r] = ir_arch.arch.regs.all_regs_ids_init[i]
80  x = ExprId(r.name, r.size)
81  x.is_term = True
82  symbols_init[r] = x
83 
84  sb = symbexec(ir_arch, dict(symbols_init))
85  sb.emulbloc(irb)
86  # print "*"*40
87  # print irb
88  # print sb.dump_id()
89  # print sb.dump_mem()
90 
91  for n_w in sb.symbols:
92  # print n_w
93  v = sb.symbols[n_w]
94  if n_w in symbols_init and symbols_init[n_w] == v:
95  continue
96  read_values = v.get_r(cst_read=True)
97  # print n_w, v, [str(x) for x in read_values]
98  node_n_w = get_node_name(irb.label, len(irb.lines), n_w)
99 
100  for n_r in read_values:
101  if n_r in current_nodes:
102  node_n_r = current_nodes[n_r]
103  else:
104  node_n_r = get_node_name(irb.label, 0, n_r)
105  current_nodes[n_r] = node_n_r
106  in_nodes[n_r] = node_n_r
107 
108  out_nodes[n_w] = node_n_w
109  flow_graph.add_uniq_edge(node_n_r, node_n_w)
110 
111  irb.in_nodes = in_nodes
112  irb.out_nodes = out_nodes
113 

+ Here is the call graph for this function: