-
Notifications
You must be signed in to change notification settings - Fork 1
/
disapi.py
303 lines (248 loc) · 9.11 KB
/
disapi.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
import math
import threading
import multiprocessing
import sys
from collections import deque, Counter
from queue import Queue
def insnentry_to_str(entry, ob):
return entry.opcode + " " + ", ".join(map(lambda v: insn_to_str(v, ob), entry.instructions))
def insn_to_str(insn, ob):
if isinstance(insn, Loc):
label = ob.label(insn.loc)
if label is not None:
return str(label)
return str(insn.loc)
else:
return str(insn)
# Holder for a location, used by branching instructions
class Loc:
def __init__(self, loc):
self.loc = loc
def __int__(self):
return self.loc
def __str__(self):
return str(self.loc)
class Branch:
def __init__(self, ep, to, conditional):
self.ep = ep # int or Label
self.to = to # int or Label
self.conditional = conditional
def __str__(self):
ret = str(self.ep) + " -> " + str(self.to)
if self.conditional:
ret += " ?"
return ret
def _generate_name(prefix):
ident = 0
while True:
yield prefix + format(ident, "X")
ident += 1
class Label:
name_generator = _generate_name("label_")
def __init__(self, location, count = 1, name = None):
self.location = location
self.count = count
self.name = name or next(Label.name_generator)
def __str__(self):
return self.name
# TODO: Better name?
def to_str(self):
return self.name + ": " + str(self.location) + " (" + str(self.count) + ")"
def __int__(self):
return self.location
class OutputBuffer:
def __init__(self, ofile):
self.insnmap = {}
self.branchlist = deque()
self.ofile = ofile
self.labels = {} # List of labels, compute with create_labels()
def insert(self, ep, lst):
if len(lst) > 0:
self.insnmap[ep] = lst
def branch(self, ep, to, conditional = False):
self.branchlist.append(Branch(ep, to, conditional))
def compute_labels(self, lower = 0, upper = sys.maxsize, min_occurance = 0):
# Count occurrences of branch
labels = Counter(map(lambda v: v.to, self.branchlist)).items()
labels = filter(lambda v: v[1] > min_occurance and lower <= v[0] <= upper, labels)
labels = dict(map(lambda v: (v[0], Label(v[0], v[1])), labels))
self.labels = labels
# Update branch list with labels
for branch in self.branchlist:
branch.to = self.label(branch.to) or branch.to
# Misleading information, ep points to the next instruction
# branch.ep = self.label(branch.ep) or branch.ep
def label(self, location):
return self.labels.get(location, None)
class InputBuffer:
def __init__(self, data, available, bounds = None, entry_point = 0):
if bounds is not None:
if len(bounds) > 0:
mn = bounds[0]
if mn > 0:
available = available - mn
data.read(mn) # Skip bytes
if len(bounds) > 1:
available = bounds[1] - bounds[0]
self.buffer = bytearray(available)
# Stores which bytes have already been read
self.access = bytearray(math.ceil(available / 8))
self.entry_point = entry_point
data.readinto(self.buffer)
def was_read(self, o):
o -= self.entry_point
o1 = o // 8
o2 = o % 8
try:
return (self.access[o1] >> o2) & 0x1 == 0x1
except IndexError:
return True
def byte(self, insn, n = 0, peek = False):
o = insn.pc + n
o -= self.entry_point
if o >= len(self.buffer):
insn.kill(True)
return -1
o1 = o // 8
o2 = o % 8
if (self.access[o1] >> o2) & 0x1 == 0:
if not peek:
self.access[o1] |= (0x1 << o2)
return self.buffer[o]
insn.kill(True)
return -1
def word(self, insn, n = 0, peek = False):
b1 = self.byte(insn, n, peek)
b2 = self.byte(insn, n + 1, peek)
return b1 | (b2 << 8)
def lword(self, insn, n = 0, peek = False):
w1 = self.word(insn, n, peek)
w2 = self.word(insn, n + 2, peek)
return w1 | (w2 << 16)
# Not really needed but perhaps at one point this will turn into a framework
def qword(self, insn, n = 0, peek = False):
l1 = self.lword(insn, n, peek)
l2 = self.lword(insn, n + 4, peek)
return l1 | (l2 << 32)
class InsnPool:
def __init__(self, proc, max_threads = None):
self.numThreads = 0
if max_threads is None:
max_threads = multiprocessing.cpu_count() * 5
self.max_threads = max_threads
self.queue = Queue()
self.proc = proc
# A lock used to wake up the polling thread if asynchronous
self.lock = threading.Semaphore()
def query(self, insn):
self.queue.put(insn)
def signal(self):
self.numThreads -= 1
if self.has_cycled():
# Wake up the polling thread if batch is done processing
self.lock.release()
def has_cycled(self):
return self.numThreads == 0
def has_finished(self):
return self.has_cycled() and self.queue.empty()
def poll_all(self, blocking = True, callback = None):
if not blocking and callback is None:
raise ValueError("If called in a non blocking way you must provide a callback function.")
def poll_all_impl():
while not self.has_finished(): # Wait for all threads to process
self.poll()
self.lock.acquire() # Pauses the current thread and waits till batch is processed
if callback:
callback()
if blocking:
poll_all_impl()
else:
thread = threading.Thread(daemon = True, target = poll_all_impl)
thread.start()
def poll(self):
# Batch processing, we only start a new batch when the old
# one has finished. It is only possible to jump to a location once.
if not self.has_cycled(): return
locations = set()
while self.numThreads < self.max_threads and not self.queue.empty():
insn = self.queue.get_nowait()
if insn.pc in locations:
continue
else:
locations.add(insn.pc)
insn.start()
self.numThreads += 1
class InsnEntry:
def __init__(self, pc, length, opcode, instructions):
self.pc = pc
self.length = length
self.opcode = opcode
self.instructions = instructions
def bytes(self, ibuffer):
return ibuffer.buffer[self.pc - ibuffer.entry_point:self.pc + self.length - ibuffer.entry_point]
class Insn(threading.Thread):
def __init__(self, pool, ibuffer, obuffer, pc = 0):
threading.Thread.__init__(self, daemon = True, name = "Insn at " + str(pc))
self.pc = pc
self.ep = pc # Entry point
self.pool = pool
self.ibuffer = ibuffer
self.obuffer = obuffer
self.lastinsn = 0 # Last instruction, this is only set if necessary
self.lastsize = 0
self.lastr = "INVALID"
self.lastmem = "INVALID"
# List of processed instructions to insert at the entry point
self.__instructions = deque()
# Flag to kill of the thread
self.__dead = False
# Flag to check if the last byte should be written.
# This is used if the Insn runs into an already processed segment,
# In this case the last byte is -1 and should not be written.
self.__nowrite = False
def run(self):
while not self.__dead:
pc = self.pc
opc = self.pool.proc.next_insn(self)
self.__instructions.append(InsnEntry(pc, self.pc - pc, opc[0], opc[1:]))
if self.__nowrite:
self.__instructions.pop()
self.obuffer.insert(self.ep, self.__instructions)
self.pool.signal()
def kill(self, nowrite = False):
self.__dead = True
self.__nowrite = nowrite
def peek(self, n = 0):
return self.ibuffer.byte(self, n, True)
def popn(self, n):
if n == 0:
return self.pop()
elif n == 1:
return self.popw()
elif n == 4:
return self.popl()
def pop(self):
b = self.ibuffer.byte(self)
self.pc += 1
return b
def popw(self):
w = self.ibuffer.word(self)
self.pc += 2
return w
def popl(self):
l = self.ibuffer.lword(self)
self.pc += 4
return l
def popq(self):
q = self.ibuffer.qword(self)
self.pc += 4
return q
# Used by JR, JP/etc to branch
def branch(self, to, conditional = False):
to = int(to)
if to < 0: return # We don't want to start jumping to invalid addresses
self.obuffer.branch(self.pc, to, conditional)
# We don't need this one anymore if we know that we have to branch
if not conditional: self.kill()
if not self.ibuffer.was_read(to):
self.pool.query(Insn(self.pool, self.ibuffer, self.obuffer, to))