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

Classes

class  DiGraphExpr
 
class  Expr
 
class  ExprAff
 
class  ExprCompose
 
class  ExprCond
 
class  ExprId
 
class  ExprInt
 
class  ExprMem
 
class  ExprOp
 
class  ExprSlice
 

Functions

def visit_chk
 
def compare_exprs_compose
 
def compare_expr_list_compose
 
def compare_expr_list
 
def compare_exprs
 
def canonize_expr_list
 
def canonize_expr_list_compose
 
def ExprInt1
 
def ExprInt8
 
def ExprInt16
 
def ExprInt32
 
def ExprInt64
 
def ExprInt_from
 
def get_expr_ids_visit
 
def get_expr_ids
 
def test_set
 
def MatchExpr
 
def SearchExpr
 
def get_rw
 
def get_list_rw
 
def get_expr_ops
 
def get_expr_mem
 

Variables

string TOK_INF = "<"
 
string TOK_INF_SIGNED = TOK_INF+"s"
 
string TOK_INF_UNSIGNED = TOK_INF+"u"
 
string TOK_INF_EQUAL = "<="
 
string TOK_INF_EQUAL_SIGNED = TOK_INF_EQUAL+"s"
 
string TOK_INF_EQUAL_UNSIGNED = TOK_INF_EQUAL+"u"
 
string TOK_EQUAL = "=="
 
string TOK_POS = "pos"
 
string TOK_POS_STRICT = "Spos"
 
int EXPRINT = 1
 
int EXPRID = 2
 
int EXPRAFF = 3
 
int EXPRCOND = 4
 
int EXPRMEM = 5
 
int EXPROP = 6
 
int EXPRSLICE = 5
 
int EXPRCOMPOSE = 5
 
dictionary expr_order_dict
 

Function Documentation

def miasm2.expression.expression.canonize_expr_list (   l)

Definition at line 1131 of file expression.py.

1132 def canonize_expr_list(l):
1133  l = list(l)
1134  l.sort(cmp=compare_exprs)
1135  return l
1136 

+ Here is the caller graph for this function:

def miasm2.expression.expression.canonize_expr_list_compose (   l)

Definition at line 1137 of file expression.py.

1139  l = list(l)
1140  l.sort(cmp=compare_exprs_compose)
1141  return l
1142 
1143 # Generate ExprInt with common size
1144 

+ Here is the caller graph for this function:

def miasm2.expression.expression.compare_expr_list (   l1_e,
  l2_e 
)

Definition at line 1065 of file expression.py.

1066 def compare_expr_list(l1_e, l2_e):
1067  # Sort by list elements in incremental order, then by list size
1068  for i in xrange(min(len(l1_e), len(l2_e))):
1069  x = compare_exprs(l1_e[i], l2_e[i])
1070  if x:
1071  return x
1072  return cmp(len(l1_e), len(l2_e))
1073 

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.expression.expression.compare_expr_list_compose (   l1_e,
  l2_e 
)

Definition at line 1056 of file expression.py.

1057 def compare_expr_list_compose(l1_e, l2_e):
1058  # Sort by list elements in incremental order, then by list size
1059  for i in xrange(min(len(l1_e), len(l2_e))):
1060  x = compare_exprs_compose(l1_e[i], l2_e[i])
1061  if x:
1062  return x
1063  return cmp(len(l1_e), len(l2_e))
1064 

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.expression.expression.compare_exprs (   e1,
  e2 
)
Compare 2 expressions for canonization
@e1: Expr
@e2: Expr
0  => ==
1  => e1 > e2
-1 => e1 < e2

Definition at line 1074 of file expression.py.

1075 def compare_exprs(e1, e2):
1076  """Compare 2 expressions for canonization
1077  @e1: Expr
1078  @e2: Expr
1079  0 => ==
1080  1 => e1 > e2
1081  -1 => e1 < e2
1082  """
1083  c1 = e1.__class__
1084  c2 = e2.__class__
1085  if c1 != c2:
1086  return cmp(expr_order_dict[c1], expr_order_dict[c2])
1087  if e1 == e2:
1088  return 0
1089  if c1 == ExprInt:
1090  return cmp(e1.arg, e2.arg)
1091  elif c1 == ExprId:
1092  x = cmp(e1.name, e2.name)
1093  if x:
1094  return x
1095  return cmp(e1.size, e2.size)
1096  elif c1 == ExprAff:
1097  raise NotImplementedError(
1098  "Comparaison from an ExprAff not yet implemented")
1099  elif c2 == ExprCond:
1100  x = compare_exprs(e1.cond, e2.cond)
1101  if x:
1102  return x
1103  x = compare_exprs(e1.src1, e2.src1)
1104  if x:
1105  return x
1106  x = compare_exprs(e1.src2, e2.src2)
1107  return x
1108  elif c1 == ExprMem:
1109  x = compare_exprs(e1.arg, e2.arg)
1110  if x:
1111  return x
1112  return cmp(e1.size, e2.size)
1113  elif c1 == ExprOp:
1114  if e1.op != e2.op:
1115  return cmp(e1.op, e2.op)
1116  return compare_expr_list(e1.args, e2.args)
1117  elif c1 == ExprSlice:
1118  x = compare_exprs(e1.arg, e2.arg)
1119  if x:
1120  return x
1121  x = cmp(e1.start, e2.start)
1122  if x:
1123  return x
1124  x = cmp(e1.stop, e2.stop)
1125  return x
1126  elif c1 == ExprCompose:
1127  return compare_expr_list_compose(e1.args, e2.args)
1128  raise NotImplementedError(
1129  "Comparaison between %r %r not implemented" % (e1, e2))
1130 

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.expression.expression.compare_exprs_compose (   e1,
  e2 
)

Definition at line 1044 of file expression.py.

1045 def compare_exprs_compose(e1, e2):
1046  # Sort by start bit address, then expr, then stop but address
1047  x = cmp(e1[1], e2[1])
1048  if x:
1049  return x
1050  x = compare_exprs(e1[0], e2[0])
1051  if x:
1052  return x
1053  x = cmp(e1[2], e2[2])
1054  return x
1055 

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.expression.expression.ExprInt1 (   i)

Definition at line 1145 of file expression.py.

1146 def ExprInt1(i):
1147  return ExprInt(uint1(i))
1148 

+ Here is the caller graph for this function:

def miasm2.expression.expression.ExprInt16 (   i)

Definition at line 1153 of file expression.py.

1154 def ExprInt16(i):
1155  return ExprInt(uint16(i))
1156 

+ Here is the caller graph for this function:

def miasm2.expression.expression.ExprInt32 (   i)

Definition at line 1157 of file expression.py.

1158 def ExprInt32(i):
1159  return ExprInt(uint32(i))
1160 

+ Here is the caller graph for this function:

def miasm2.expression.expression.ExprInt64 (   i)

Definition at line 1161 of file expression.py.

1162 def ExprInt64(i):
1163  return ExprInt(uint64(i))
1164 

+ Here is the caller graph for this function:

def miasm2.expression.expression.ExprInt8 (   i)

Definition at line 1149 of file expression.py.

1150 def ExprInt8(i):
1151  return ExprInt(uint8(i))
1152 

+ Here is the caller graph for this function:

def miasm2.expression.expression.ExprInt_from (   e,
  i 
)

Definition at line 1165 of file expression.py.

1166 def ExprInt_from(e, i):
1167  "Generate ExprInt with size equal to expression"
1168  return ExprInt(mod_size2uint[e.size](i))
1169 

+ Here is the caller graph for this function:

def miasm2.expression.expression.get_expr_ids (   e)

Definition at line 1176 of file expression.py.

1177 def get_expr_ids(e):
1178  ids = set()
1179  e.visit(lambda x: get_expr_ids_visit(x, ids))
1180  return ids
1181 

+ Here is the call graph for this function:

def miasm2.expression.expression.get_expr_ids_visit (   e,
  ids 
)

Definition at line 1170 of file expression.py.

1171 def get_expr_ids_visit(e, ids):
1172  if isinstance(e, ExprId):
1173  ids.add(e)
1174  return e
1175 

+ Here is the caller graph for this function:

def miasm2.expression.expression.get_expr_mem (   e)

Definition at line 1364 of file expression.py.

1365 def get_expr_mem(e):
1366  def visit_getmem(e, out=None):
1367  if out is None:
1368  out = set()
1369  if isinstance(e, ExprMem):
1370  out.add(e)
1371  return e
1372  ops = set()
1373  e.visit(lambda x: visit_getmem(x, ops))
1374  return ops

+ Here is the caller graph for this function:

def miasm2.expression.expression.get_expr_ops (   e)

Definition at line 1352 of file expression.py.

1353 def get_expr_ops(e):
1354  def visit_getops(e, out=None):
1355  if out is None:
1356  out = set()
1357  if isinstance(e, ExprOp):
1358  out.add(e.op)
1359  return e
1360  ops = set()
1361  e.visit(lambda x: visit_getops(x, ops))
1362  return ops
1363 
def miasm2.expression.expression.get_list_rw (   exprs,
  mem_read = False,
  cst_read = True 
)
return list of read/write reg/cst/mem for each expressions

Definition at line 1325 of file expression.py.

1326 def get_list_rw(exprs, mem_read=False, cst_read=True):
1327  """
1328  return list of read/write reg/cst/mem for each expressions
1329  """
1330  list_rw = []
1331  # cst_num = 0
1332  for e in exprs:
1333  o_r = set()
1334  o_w = set()
1335  # get r/w
1336  o_r.update(e.get_r(mem_read=mem_read, cst_read=cst_read))
1337  if isinstance(e.dst, ExprMem):
1338  o_r.update(e.dst.arg.get_r(mem_read=mem_read, cst_read=cst_read))
1339  o_w.update(e.get_w())
1340  # each cst is indexed
1341  o_r_rw = set()
1342  for r in o_r:
1343  # if isinstance(r, ExprInt):
1344  # r = ExprOp('cst_%d'%cst_num, r)
1345  # cst_num += 1
1346  o_r_rw.add(r)
1347  o_r = o_r_rw
1348  list_rw.append((o_r, o_w))
1349 
1350  return list_rw
1351 

+ Here is the caller graph for this function:

def miasm2.expression.expression.get_rw (   exprs)

Definition at line 1315 of file expression.py.

1316 def get_rw(exprs):
1317  o_r = set()
1318  o_w = set()
1319  for e in exprs:
1320  o_r.update(e.get_r(mem_read=True))
1321  for e in exprs:
1322  o_w.update(e.get_w())
1323  return o_r, o_w
1324 
def miasm2.expression.expression.MatchExpr (   e,
  m,
  tks,
  result = None 
)
Try to match m expression with e expression with tks jokers.
Result is output dictionnary with matching joker values.
@e : Expr to test
@m : Targetted Expr
@tks : list of ExprId, available jokers
@result : dictionnary of ExprId -> Expr, output matching context

Definition at line 1199 of file expression.py.

1200 def MatchExpr(e, m, tks, result=None):
1201  """Try to match m expression with e expression with tks jokers.
1202  Result is output dictionnary with matching joker values.
1203  @e : Expr to test
1204  @m : Targetted Expr
1205  @tks : list of ExprId, available jokers
1206  @result : dictionnary of ExprId -> Expr, output matching context
1207  """
1208 
1209  if result is None:
1210  result = {}
1211 
1212  if m in tks:
1213  # m is a Joker
1214  return test_set(e, m, tks, result)
1215 
1216  if isinstance(e, ExprInt):
1217  return test_set(e, m, tks, result)
1218 
1219  elif isinstance(e, ExprId):
1220  return test_set(e, m, tks, result)
1221 
1222  elif isinstance(e, ExprOp):
1223 
1224  # e need to be the same operation than m
1225  if not isinstance(m, ExprOp):
1226  return False
1227  if e.op != m.op:
1228  return False
1229  if len(e.args) != len(m.args):
1230  return False
1231 
1232  # Perform permutation only if the current operation is commutative
1233  if e.is_commutative():
1234  permutations = itertools.permutations(e.args)
1235  else:
1236  permutations = [e.args]
1237 
1238  # For each permutations of arguments
1239  for permut in permutations:
1240  good = True
1241  # We need to use a copy of result to not override it
1242  myresult = dict(result)
1243  for a1, a2 in zip(permut, m.args):
1244  r = MatchExpr(a1, a2, tks, myresult)
1245  # If the current permutation do not match EVERY terms
1246  if r is False:
1247  good = False
1248  break
1249  if good is True:
1250  # We found a possibility
1251  for k, v in myresult.items():
1252  # Updating result in place (to keep pointer in recursion)
1253  result[k] = v
1254  return result
1255  return False
1256 
1257  # Recursive tests
1258 
1259  elif isinstance(e, ExprMem):
1260  if not isinstance(m, ExprMem):
1261  return False
1262  if e.size != m.size:
1263  return False
1264  return MatchExpr(e.arg, m.arg, tks, result)
1265 
1266  elif isinstance(e, ExprSlice):
1267  if not isinstance(m, ExprSlice):
1268  return False
1269  if e.start != m.start or e.stop != m.stop:
1270  return False
1271  return MatchExpr(e.arg, m.arg, tks, result)
1272 
1273  elif isinstance(e, ExprCond):
1274  if not isinstance(m, ExprCond):
1275  return False
1276  r = MatchExpr(e.cond, m.cond, tks, result)
1277  if r is False:
1278  return False
1279  r = MatchExpr(e.src1, m.src1, tks, result)
1280  if r is False:
1281  return False
1282  r = MatchExpr(e.src2, m.src2, tks, result)
1283  if r is False:
1284  return False
1285  return result
1286 
1287  elif isinstance(e, ExprCompose):
1288  if not isinstance(m, ExprCompose):
1289  return False
1290  for a1, a2 in zip(e.args, m.args):
1291  if a1[1] != a2[1] or a1[2] != a2[2]:
1292  return False
1293  r = MatchExpr(a1[0], a2[0], tks, result)
1294  if r is False:
1295  return False
1296  return result
1297 
1298  else:
1299  raise NotImplementedError("MatchExpr: Unknown type: %s" % type(e))
1300 

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.expression.expression.SearchExpr (   e,
  m,
  tks,
  result = None 
)

Definition at line 1301 of file expression.py.

1302 def SearchExpr(e, m, tks, result=None):
1303  # TODO XXX: to test
1304  if result is None:
1305  result = set()
1306 
1307  def visit_search(e, m, tks, result):
1308  r = {}
1309  MatchExpr(e, m, tks, r)
1310  if r:
1311  result.add(tuple(r.items()))
1312  return e
1313  e.visit(lambda x: visit_search(x, m, tks, result))
1314 

+ Here is the call graph for this function:

def miasm2.expression.expression.test_set (   e,
  v,
  tks,
  result 
)
Test if v can correspond to e. If so, update the context in result.
Otherwise, return False
@e : Expr
@v : Expr
@tks : list of ExprId, available jokers
@result : dictionnary of ExprId -> Expr, current context

Definition at line 1182 of file expression.py.

1183 def test_set(e, v, tks, result):
1184  """Test if v can correspond to e. If so, update the context in result.
1185  Otherwise, return False
1186  @e : Expr
1187  @v : Expr
1188  @tks : list of ExprId, available jokers
1189  @result : dictionnary of ExprId -> Expr, current context
1190  """
1191 
1192  if not v in tks:
1193  return e == v
1194  if v in result and result[v] != e:
1195  return False
1196  result[v] = e
1197  return result
1198 

+ Here is the caller graph for this function:

def miasm2.expression.expression.visit_chk (   visitor)

Definition at line 58 of file expression.py.

58 
59 def visit_chk(visitor):
60  "Function decorator launching callback on Expression visit"
61  def wrapped(e, cb, test_visit=lambda x: True):
62  if (test_visit is not None) and (not test_visit(e)):
63  return e
64  e_new = visitor(e, cb, test_visit)
65  if e_new is None:
66  return None
67  e_new2 = cb(e_new)
68  return e_new2
69  return wrapped
70 
71 
72 # Expression display
73 

Variable Documentation

dictionary miasm2.expression.expression.expr_order_dict
Initial value:
1 = {ExprId: 1,
2  ExprCond: 2,
3  ExprMem: 3,
4  ExprOp: 4,
5  ExprSlice: 5,
6  ExprCompose: 7,
7  ExprInt: 8,
8  }

Definition at line 1034 of file expression.py.

int miasm2.expression.expression.EXPRAFF = 3

Definition at line 50 of file expression.py.

int miasm2.expression.expression.EXPRCOMPOSE = 5

Definition at line 55 of file expression.py.

int miasm2.expression.expression.EXPRCOND = 4

Definition at line 51 of file expression.py.

int miasm2.expression.expression.EXPRID = 2

Definition at line 49 of file expression.py.

int miasm2.expression.expression.EXPRINT = 1

Definition at line 48 of file expression.py.

int miasm2.expression.expression.EXPRMEM = 5

Definition at line 52 of file expression.py.

int miasm2.expression.expression.EXPROP = 6

Definition at line 53 of file expression.py.

int miasm2.expression.expression.EXPRSLICE = 5

Definition at line 54 of file expression.py.

string miasm2.expression.expression.TOK_EQUAL = "=="

Definition at line 43 of file expression.py.

string miasm2.expression.expression.TOK_INF = "<"

Definition at line 37 of file expression.py.

string miasm2.expression.expression.TOK_INF_EQUAL = "<="

Definition at line 40 of file expression.py.

string miasm2.expression.expression.TOK_INF_EQUAL_SIGNED = TOK_INF_EQUAL+"s"

Definition at line 41 of file expression.py.

string miasm2.expression.expression.TOK_INF_EQUAL_UNSIGNED = TOK_INF_EQUAL+"u"

Definition at line 42 of file expression.py.

string miasm2.expression.expression.TOK_INF_SIGNED = TOK_INF+"s"

Definition at line 38 of file expression.py.

string miasm2.expression.expression.TOK_INF_UNSIGNED = TOK_INF+"u"

Definition at line 39 of file expression.py.

string miasm2.expression.expression.TOK_POS = "pos"

Definition at line 44 of file expression.py.

string miasm2.expression.expression.TOK_POS_STRICT = "Spos"

Definition at line 45 of file expression.py.