Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
Classes | Functions | Variables
miasm2.expression.expression_helper Namespace Reference

Classes

class  ExprRandom
 
class  Variables_Identifier
 

Functions

def parity
 
def merge_sliceto_slice
 
def is_pure_int
 
def is_int_or_cond_src_int
 
def fast_unify
 
def get_missing_interval
 
def _expr_cmp_gen
 
def expr_cmpu
 
def expr_cmps
 

Variables

list op_propag_cst
 

Function Documentation

def miasm2.expression.expression_helper._expr_cmp_gen (   arg1,
  arg2 
)
private

Definition at line 536 of file expression_helper.py.

537 def _expr_cmp_gen(arg1, arg2):
538  return (arg2 - arg1) ^ ((arg2 ^ arg1) & ((arg2 - arg1) ^ arg2))

+ Here is the caller graph for this function:

def miasm2.expression.expression_helper.expr_cmps (   arg1,
  arg2 
)
Returns a one bit long Expression:
* 1 if @arg1 is strictly greater than @arg2 (signed)
* 0 otherwise.

Definition at line 547 of file expression_helper.py.

548 def expr_cmps(arg1, arg2):
549  """
550  Returns a one bit long Expression:
551  * 1 if @arg1 is strictly greater than @arg2 (signed)
552  * 0 otherwise.
553  """
554  return _expr_cmp_gen(arg1, arg2).msb()

+ Here is the call graph for this function:

def miasm2.expression.expression_helper.expr_cmpu (   arg1,
  arg2 
)
Returns a one bit long Expression:
* 1 if @arg1 is strictly greater than @arg2 (unsigned)
* 0 otherwise.

Definition at line 539 of file expression_helper.py.

540 def expr_cmpu(arg1, arg2):
541  """
542  Returns a one bit long Expression:
543  * 1 if @arg1 is strictly greater than @arg2 (unsigned)
544  * 0 otherwise.
545  """
546  return (_expr_cmp_gen(arg1, arg2) ^ arg2 ^ arg1).msb()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.expression.expression_helper.fast_unify (   seq,
  idfun = None 
)

Definition at line 172 of file expression_helper.py.

173 def fast_unify(seq, idfun=None):
174  # order preserving unifying list function
175  if idfun is None:
176  idfun = lambda x: x
177  seen = {}
178  result = []
179  for item in seq:
180  marker = idfun(item)
181 
182  if marker in seen:
183  continue
184  seen[marker] = 1
185  result.append(item)
186  return result

+ Here is the caller graph for this function:

def miasm2.expression.expression_helper.get_missing_interval (   all_intervals,
  i_min = 0,
  i_max = 32 
)
Return a list of missing interval in all_interval
@all_interval: list of (int, int)
@i_min: int, minimal missing interval bound
@i_max: int, maximal missing interval bound

Definition at line 187 of file expression_helper.py.

188 def get_missing_interval(all_intervals, i_min=0, i_max=32):
189  """Return a list of missing interval in all_interval
190  @all_interval: list of (int, int)
191  @i_min: int, minimal missing interval bound
192  @i_max: int, maximal missing interval bound"""
193 
194  my_intervals = all_intervals[:]
195  my_intervals.sort()
196  my_intervals.append((i_max, i_max))
197 
198  missing_i = []
199  last_pos = i_min
200  for start, stop in my_intervals:
201  if last_pos != start:
202  missing_i.append((last_pos, start))
203  last_pos = stop
204  return missing_i
205 

+ Here is the caller graph for this function:

def miasm2.expression.expression_helper.is_int_or_cond_src_int (   e)

Definition at line 163 of file expression_helper.py.

165  if isinstance(e, m2_expr.ExprInt):
166  return True
167  if isinstance(e, m2_expr.ExprCond):
168  return (isinstance(e.src1, m2_expr.ExprInt) and
169  isinstance(e.src2, m2_expr.ExprInt))
170  return False
171 

+ Here is the caller graph for this function:

def miasm2.expression.expression_helper.is_pure_int (   e)
return True if expr is only composed with integers
/!\ ExprCond returns True is src1 and src2 are integers

Definition at line 141 of file expression_helper.py.

142 def is_pure_int(e):
143  """
144  return True if expr is only composed with integers
145  /!\ ExprCond returns True is src1 and src2 are integers
146  """
147  def modify_cond(e):
148  if isinstance(e, m2_expr.ExprCond):
149  return e.src1 | e.src2
150  return e
151 
152  def find_int(e, s):
153  if isinstance(e, m2_expr.ExprId) or isinstance(e, m2_expr.ExprMem):
154  s.add(e)
155  return e
156  s = set()
157  new_e = e.visit(modify_cond)
158  new_e.visit(lambda x: find_int(x, s))
159  if s:
160  return False
161  return True
162 
def miasm2.expression.expression_helper.merge_sliceto_slice (   args)

Definition at line 37 of file expression_helper.py.

37 
38 def merge_sliceto_slice(args):
39  sources = {}
40  non_slice = {}
41  sources_int = {}
42  for a in args:
43  if isinstance(a[0], m2_expr.ExprInt):
44  # sources_int[a.start] = a
45  # copy ExprInt because we will inplace modify arg just below
46  # /!\ TODO XXX never ever modify inplace args...
47  sources_int[a[1]] = (m2_expr.ExprInt(int(a[0].arg),
48  a[2] - a[1]),
49  a[1],
50  a[2])
51  elif isinstance(a[0], m2_expr.ExprSlice):
52  if not a[0].arg in sources:
53  sources[a[0].arg] = []
54  sources[a[0].arg].append(a)
55  else:
56  non_slice[a[1]] = a
57  # find max stop to determine size
58  max_size = None
59  for a in args:
60  if max_size is None or max_size < a[2]:
61  max_size = a[2]
62 
63  # first simplify all num slices
64  final_sources = []
65  sorted_s = []
66  for x in sources_int.values():
67  x = list(x)
68  # mask int
69  v = x[0].arg & ((1 << (x[2] - x[1])) - 1)
70  x[0] = m2_expr.ExprInt_from(x[0], v)
71  x = tuple(x)
72  sorted_s.append((x[1], x))
73  sorted_s.sort()
74  while sorted_s:
75  start, v = sorted_s.pop()
76  out = [m2_expr.ExprInt(v[0].arg), v[1], v[2]]
77  size = v[2] - v[1]
78  while sorted_s:
79  if sorted_s[-1][1][2] != start:
80  break
81  s_start, s_stop = sorted_s[-1][1][1], sorted_s[-1][1][2]
82  size += s_stop - s_start
83  a = m2_expr.mod_size2uint[size](
84  (int(out[0].arg) << (out[1] - s_start)) +
85  int(sorted_s[-1][1][0].arg))
86  out[0] = m2_expr.ExprInt(a)
87  sorted_s.pop()
88  out[1] = s_start
89  out[0] = m2_expr.ExprInt(int(out[0].arg), size)
90  final_sources.append((start, out))
91 
92  final_sources_int = final_sources
93  # check if same sources have corresponding start/stop
94  # is slice AND is sliceto
95  simp_sources = []
96  for args in sources.values():
97  final_sources = []
98  sorted_s = []
99  for x in args:
100  sorted_s.append((x[1], x))
101  sorted_s.sort()
102  while sorted_s:
103  start, v = sorted_s.pop()
104  ee = v[0].arg[v[0].start:v[0].stop]
105  out = ee, v[1], v[2]
106  while sorted_s:
107  if sorted_s[-1][1][2] != start:
108  break
109  if sorted_s[-1][1][0].stop != out[0].start:
110  break
111 
112  start = sorted_s[-1][1][1]
113  # out[0].start = sorted_s[-1][1][0].start
114  o_e, _, o_stop = out
115  o1, o2 = sorted_s[-1][1][0].start, o_e.stop
116  o_e = o_e.arg[o1:o2]
117  out = o_e, start, o_stop
118  # update _size
119  # out[0]._size = out[0].stop-out[0].start
120  sorted_s.pop()
121  out = out[0], start, out[2]
122 
123  final_sources.append((start, out))
124 
125  simp_sources += final_sources
126 
127  simp_sources += final_sources_int
128 
129  for i, v in non_slice.items():
130  simp_sources.append((i, v))
131 
132  simp_sources.sort()
133  simp_sources = [x[1] for x in simp_sources]
134  return simp_sources
135 

+ Here is the caller graph for this function:

def miasm2.expression.expression_helper.parity (   a)

Definition at line 28 of file expression_helper.py.

28 
29 def parity(a):
30  tmp = (a) & 0xFFL
31  cpt = 1
32  while tmp != 0:
33  cpt ^= tmp & 1
34  tmp >>= 1
35  return cpt
36 

+ Here is the caller graph for this function:

Variable Documentation

list miasm2.expression.expression_helper.op_propag_cst
Initial value:
1 = ['+', '*', '^', '&', '|', '>>',
2  '<<', "a>>", ">>>", "<<<",
3  "/", "%", 'idiv', 'imod', 'umod', 'udiv']

Definition at line 136 of file expression_helper.py.