forked from bsdphk/PyRevEng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisass.py
344 lines (292 loc) · 7.28 KB
/
disass.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/local/bin/python
#
from __future__ import print_function
import sys
import bitmap
import copy
import mem
#######################################################################
#
# We report trouble as these exceptions
#
class DisassError(Exception):
def __init__(self, reason, diag = None):
self.reason = reason
self.diag = diag
self.value = (str(self.reason),)
def __str__(self):
return repr(self.value)
#######################################################################
class disass(object):
"""Some kind of execution or interpretation unit.
This will usually be a CPU, but it can also be interpreters for
the meta-code of high level languages.
"""
def __init__(self, p, name):
self.p = p
self.name = name
self.bm = bitmap.bitmap()
self.ins = dict()
p.c[name] = self
self.fails = 0
self.is_clone = False
self.follow_calls = True
self.special_dst = dict()
def clone(self, follow_calls = True):
"""Make a clone for exploratory purposes
"""
this = copy.copy(self)
this.ins = copy.copy(this.ins)
this.bm = copy.deepcopy(this.bm)
this.fails = 0
this.is_clone = True
this.follow_calls = follow_calls
return this
def disass(self, adr, priv=None):
"""Schedule the address for disassembly
The instruction is returned immediately, but the
disassembly may not happen until later.
"""
assert type(adr) == int
if adr in self.ins:
i = self.ins[adr]
if i.status == "OK":
return i
elif i.status == "new":
return i
elif i.status == "prospective":
return i
elif self.is_clone:
self.fails += 1
return i
ins = instruction(self, adr)
self.ins[adr] = ins
if self.bm.tst(adr):
ins.status = "overlap"
self.fails += 1
else:
ins.status = "prospective"
self.bm.set(adr)
self.p.todo(adr, self.xxdo_disass, ins)
return ins
def xxdo_disass(self, p, adr, ins):
assert p == self.p
if ins.status != "prospective":
print(ins)
assert ins.status == "prospective"
assert self.fails != None
if self.fails > 0 and self.is_clone:
ins.fail("cascade")
return
try:
self.do_disass(adr, ins)
except mem.MemError as bug:
ins.fail("no mem", str(bug))
except DisassError as bug:
ins.fail(bug.reason, bug.diag)
assert self.fails != None
self.finish_ins(ins)
def is_ins(self, adr):
"""Test if we have an instruction on address already
"""
return adr in self.ins
def new_ins(self, adr):
"""Create a new instruction
Return None if it already exists
Return False if it overlaps an existing instruction
"""
if adr in self.ins:
return None
if self.bm.tst(adr):
return False
i = instruction(self, adr)
self.bm.set(adr)
self.ins[adr] = i
return i
def finish_ins(self, ins):
"""Finish the definition of an instruction
"""
assert type(ins.lo) == int
assert type(ins.hi) == int
if ins.status == "fail":
return
assert ins.lo >= self.p.lo
assert ins.hi <= self.p.hi
assert self.bm.tst(ins.lo)
assert ins == self.ins[ins.lo]
if ins.hi > ins.lo + 1 and \
self.bm.mtst(ins.lo + 1, ins.hi) != False:
self.fails += 1
if self.is_clone:
return
print("ERROR: Overlapping instructions:")
l = list()
ins.status = "overlap"
self.bm.mset(ins.lo, ins.hi)
print("\t", ins)
l.append(ins)
ins.overlap = l
for j in range (ins.lo + 1, ins.hi):
if j in self.ins:
ii = self.ins[j]
print("\t", ii)
ii.status = "overlap"
ii.overlap = l
l.append(ii)
return
self.bm.mset(ins.lo, ins.hi)
if ins.status != "prospective":
return
ins.status = "OK"
# Only process flow for good instructions
if len(ins.flow_out) == 0:
j = self.disass(ins.hi)
for i in ins.flow_out:
if type(i[2]) != int:
continue
if not i[2] in self.special_dst:
continue
self.special_dst[i[2]](ins, i[2])
has_cond = False
for i in ins.flow_out:
if i[0] == "cond":
has_cond = True
break
for i in ins.flow_out:
if i[0] == "call":
if not has_cond:
j = self.disass(ins.hi)
if not self.follow_calls:
continue
if type(i[2]) != int:
continue
try:
self.p.m.chkadr(i[2])
j = self.disass(i[2])
j.flow_in.append((i[0], i[1], ins.lo))
except:
pass
def to_tree(self):
# print("Inserting", self.name, "instructions in tree")
for i in self.ins:
ins = self.ins[i]
if ins.status != "OK":
continue
x = self.p.t.add(ins.lo, ins.hi, "ins")
x.render = self.render
x.a['flow'] = ins.flow_out
x.a['ins'] = ins
if ins.cmt != "":
for i in ins.cmt.split("\n"):
x.cmt.append(i)
class assy(disass):
"""Disassembler for ass' code
Instructions have .mne and .oper attributes
Render function does label resolution on .oper elements
of the format:
(int, "foo(%s)")
Substitute label or m.afmt() if "%s" present
(int, "foo(%s)", "bar(%s)")
Substitute label in "foo(%s)" (if "%s" present)
Substitute m.afmt() in "bar(%s)" (if "%s" present)
"""
def __init__(self, p, name):
disass.__init__(self, p, name)
def disass(self, adr, priv=None):
assert type(adr) == int
j = disass.disass(self, adr, priv)
if j.status == "prospective":
j.mne = None
j.oper = list()
return j
def render(self, p, ins):
if type(ins) != instruction:
# transistional hack
ins = ins.a['ins']
assert ins.mne != None
assert ins.status == "OK"
s = ins.mne
d = "\t"
for i in ins.oper:
s += d
d = ","
if type(i) == str:
s += i
continue
if type(i[0]) == int:
if i[0] in self.p.label:
s += i[1] % self.p.label[i[0]]
elif len(i) < 3 and i[1].find('%s') != -1:
s += i[1] % self.p.m.afmt(i[0])
elif len(i) < 3:
s += i[1]
elif i[2].find('%s') != -1:
s += i[2] % self.p.m.afmt(i[0])
else:
s += i[2]
else:
s += "<XXX " + str(i) + ">"
return (s,)
class instruction(object):
"""A single instruction.
"""
def __init__(self, disass, adr):
self.disass = disass
self.flow_in = list()
self.flow_out = list()
self.render = None
self.lo = adr
self.hi = adr + 1
self.model = None
self.status = "new"
self.cmt = ""
self.diag = None
self.pseudo = None
def __repr__(self):
s = "<ins " + self.disass.name + " " + str(self.status)
s += " " + self.disass.p.m.afmt(self.lo)
if self.hi != None:
s += "-" + self.disass.p.m.afmt(self.hi)
s += ">"
return s
def dflow(self, fl):
s = fl[0] + ":" + fl[1]
if type(fl[2]) == int:
s += ":" + self.disass.p.m.afmt(fl[2])
elif fl[2] != None:
s += ":" + str(fl[2])
return s
def lcmt(self, s):
if len(self.cmt) > 0:
self.cmt += "\n"
self.cmt += s
def debug(self):
s = self.__repr__()
s = s[:-1]
if len(self.flow_in) > 0:
t = ""
for i in self.flow_in:
s += " <" + self.dflow(i)
t = " "
if len(self.flow_out) > 0:
t = ""
for i in self.flow_out:
s += " >" + self.dflow(i)
t = " "
if self.reason != None:
s += " " + str(self.reason)
s += " >"
return s
def flow(self, mode, cc, dst):
self.flow_out.append((mode, cc, dst))
def fail(self, reason, diag = None):
assert self.status == "prospective"
self.status = "fail"
self.reason = reason
if diag != None:
self.diag = diag
if not self.disass.is_clone:
print("FAIL: ", self.debug())
if self.diag != None:
print("\t", self.diag);
self.disass.fails += 1