-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathemu.py
72 lines (52 loc) · 1.56 KB
/
emu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from op_classes import *
STACK_MEM_TOP = 0x11220000
def emu_init_ctx():
ctx = Ctx()
ctx.set_reg(ESP, STACK_MEM_TOP)
return ctx
def emu_decide_jump(dbb, ctx):
#don't ask about the last BB
#assert(dbb.child1 != None or dbb.child2 != None)
taken = None
seen_jxx = True
for instr in dbb.body:
instr.eval(ctx)
if instr.is_jxx():
seen_jxx = True
if instr.can_eval(ctx):
taken = instr.eval_jxx(ctx)
#jumps are last instructions in BBs
break
#if there is no jump at the end, then execution transfers to the only child
if not seen_jxx:
assert(dbb.child1 != None and dbb.child2 == None)
taken = True
return taken
def emulate_list(ctx, instrs):
for instr in instrs:
instr.eval(ctx)
def emulate(dbb):
visited = set()
ctx = emu_init_ctx()
emulate_rec(dbb, ctx, visited)
def emulate_rec(dbb, ctx, visited):
assert(dbb not in visited)
visited.add(dbb)
for instr in dbb.body:
instr.eval(ctx)
print instr.dump()
o = ctx.dump()
print o
if instr.is_jxx():
taken = None
if instr.can_eval(ctx):
taken = instr.eval_jxx(ctx)
#jumps are last instructions
if taken == None:
break
if taken:
child = dbb.true_branch()
else:
child = dbb.false_branch()
emulate_rec(child, ctx, visited)
print "#"*10