-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstructionAbstraction.py
386 lines (359 loc) · 15.1 KB
/
InstructionAbstraction.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import sys
import os
import random
import re
from collections import namedtuple
import setting as config
import copy
import math
gmem = {}
def isfloat(x):
try:
a = float(x)
except ValueError:
return False
else:
return True
def isint(x):
try:
a = float(x)
b = int(a)
except ValueError:
return False
else:
return a == b
class DDGInst:
def __init__(self, opcode, input, output, funcname="", address=-1, cycle = 0):
self.opcode = opcode
self.input = input
self.output = output
self.address = address
self.funcname = funcname
self.cycle = cycle
class AbstractInst:
def __init__(self, mapping, trace):
self.mapping = mapping
self.trace = trace
self.static_trace = []
self.dynamic_trace = []
with open(self.mapping, "r") as f_mapping:
self.static_trace = f_mapping.readlines()
with open(self.trace, "r") as f_trace:
self.dynamic_trace = f_trace.readlines()
self.memcpyRec = {}
self.bitwiseRec = {}
def parseIRInst(self,static_trace,inst_key,inst_value):
flag = 0
strace = []
block = []
Data_item = namedtuple("data_item", "operand type value")
for item in static_trace:
if item.strip() == "start":
flag = 1
elif item.strip() == "end":
flag = 0
strace.append(block)
block = []
elif flag == 1:
block.append(item)
for block in strace:
opcode = ""
input = []
output = []
funcname = ""
outername = ""
for item in block:
item = item.rstrip("\n")
if "_ZL12center_table" in item:
print "hhhh"
if " -> " in item:
inst_key.append(item.split(" -> ")[1])
if "Opcode" in item:
opcode = item.split(": ")[1]
if "name:" in item and "Func" not in item:
outername = item.split(" ")[1]
if "Source" in item:
if item != "Source: null":
if " = " not in item:
op = item.split(": ")[1]
#type = op.split(" ")[0]
res = re.findall("Type\+\d+", item)
type = int(res[0].split("+")[1])
if opcode == "getelementptr":
if "GepType+" in item:
res = re.findall("GepType\+\d+\+?.*", item)
new_res = res[0].split("+")
if len(new_res) > 2:
type = str(type)+"+"+new_res[1]+"+"+new_res[2]
else:
type = str(type)+"+"+new_res[1]
value = ""
operand = op.split(" ")[1]
operand = operand.lstrip(" ")
#if operand.startswith("%"):
# operand = "bo"+operand
if opcode == "alloca":
type = config.OSbits
d = Data_item(outername+"_root"+"_"+str(random.randint(0,1024)),type, value)
else:
temp = ""
if isint(operand) or isfloat(operand):
temp = operand
else:
temp = outername+"_"+operand
d = Data_item(temp, type, value)
else:
op = item.split(": ")[1]
operand = op.split(" = ")[0].lstrip(" ")
#if operand.startswith("%"):
# operand = "bo"+operand
#res = re.findall("i\d+\*?\*?", op.split(" = ")[1])
res = re.findall("Type\+\d+", item)
type = int(res[0].split("+")[1])
if opcode == "getelementptr":
if "GepType+" in item:
res = re.findall("GepType\+\d+\+?.*", item)
new_res = res[0].split("+")
if len(new_res) > 2:
type = str(type)+"+"+new_res[1]+"+"+new_res[2]
else:
type = str(type)+"+"+new_res[1]
temp = ""
value = ""
if isint(operand) or isfloat(operand):
temp = operand
else:
temp = outername+"_"+operand
d = Data_item(temp, type, value)
#if len(res) > 0:
# d = Data_item(operand, type)
#else:
# d = Data_item(operand, "0")
if "memcpy" in funcname:
if len(input) < 1:
input.append(d)
else:
input.append(d)
if "Dest" in item:
if "unreachable" not in item and "void" not in item:
op = item.split(": ")[1]
operand = op.split(" = ")[0]
operand = operand.lstrip(" ")
#if operand.startswith("%"):
# operand = "bo"+operand
#res = re.findall("i\d+\*?\*?", op.split(" = ")[1])
res = re.findall("Type\+\d+", item)
type = int(res[0].split("+")[1])
value = ""
#if " to " in item and len(res) > 1:
temp = ""
if isint(operand) or isfloat(operand):
temp = operand
else:
temp = outername+"_"+operand
d = Data_item(temp, type, value)
#else:
# if len(res) > 0:
# d = Data_item(operand, res[0])
# else:
# d = Data_item(operand, "0")
output.append(d)
if "Funcname" in item:
funcname = item.split(": ")[1]
ddg_inst = DDGInst(opcode, input, output, funcname)
inst_value.append(ddg_inst)
def export_trace(self):
# ID: 312 OPCode: 26 Value: 139679839926848
global gmem
inst_key = []
inst_value = []
trace = []
self.parseIRInst(self.static_trace, inst_key, inst_value)
inst_map = dict(zip(inst_key,inst_value))
remap = []
memoryBoundary = []
memory = {}
Data_item = namedtuple("data_item", "operand type value")
_count = 0
cycle_index_lookup = {}
index_cycle_lookup = {}
for item in self.dynamic_trace:
if _count == 2:
break
if "profiling.exe" in item:
res = re.findall("[0-9a-fA-F]+-[0-9a-fA-F]+", item)
min = int(res[0].split("-")[0], 16)
max = int(res[0].split("-")[1], 16)
gmem[_count] = []
gmem[_count].append(min)
gmem[_count].append(max)
_count += 1
for idx, item in enumerate(self.dynamic_trace):
if "ID:" in item:
item_new = item.rstrip("\n")
res = re.findall("[-+]?[0-9]*\.?[0-9]+", item_new)
cycle = res[len(res)-1]
res.pop()
_index = int(res[0])
if res[1] != "2" and res[1] != "48" and res[1] != "26":
cycle_index_lookup[int(cycle)] = _index
if _index not in index_cycle_lookup:
index_cycle_lookup[_index] = []
index_cycle_lookup[_index].append(cycle)
else:
index_cycle_lookup[_index].append(cycle)
value = copy.deepcopy(inst_map[res[0]])
assert isinstance(value, DDGInst)
if len(value.output)>0:
output_operand = value.output[0].operand
output_type = value.output[0].type
output_value = ""
if res[1] == "27" or res[1] == "28" or res[1] == "26":
#if len(res) > 2:
value.address = res[2]
if res[1] == "27" and len(res) == 4:
output_value = res[3]
if res[1] == "27" or res[1] == "28":
self.processLDST(memoryBoundary,memory,idx)
memoryBoundary = []
else:
if len(res) >= 3 :
output_value = res[2]
if len(res) == 5:
self.memcpyRec[idx] = []
self.memcpyRec[idx].append(res[2])
self.memcpyRec[idx].append(res[3])
self.memcpyRec[idx].append(res[4])
if len(res) == 6:
self.bitwiseRec[idx] = []
self.bitwiseRec[idx].append(res[2])
self.bitwiseRec[idx].append(res[3])
self.bitwiseRec[idx].append(res[4])
if len(value.output)>0:
value.output[0] = Data_item(output_operand, output_type, output_value)
value.cycle = int(cycle)
trace.append(value)
remap.append(idx)
else:
memoryBoundary.append(idx)
#memory = self.processMemory(memoryBoundary)
ret = []
ret.append(trace)
ret.append(remap)
ret.append(memory)
ret.append(cycle_index_lookup)
ret.append(index_cycle_lookup)
return ret
def processMemory(self, memoryBoundary):
counter = -1
memory = {}
temp = []
for item in memoryBoundary:
counter += 1
if counter != item:
counter = item
memory[item] = []
memory[item].extend(temp)
temp = []
temp.append(self.dynamic_trace[item])
memRange = {}
for key in memory:
maps = memory[key]
memRange[key] = []
for i in maps:
if "profiling.exe" in i or "[heap]" in i or "[stack]" in i or "[stack:"+config.tid+"]" in i:
res = re.findall("[0-9a-fA-F]+-[0-9a-fA-F]+", i)
min = int(res[0].split("-")[0], 16)
max = int(res[0].split("-")[1], 16)
memRange[key].append(min)
memRange[key].append(max)
print memRange
return memRange
def processLDST(self, memoryBoundary, g_memory, index_of_ldst):
global gmem
memory = []
for item in memoryBoundary:
memory.append(self.dynamic_trace[item])
g_memory[index_of_ldst] = {}
for iter, key in enumerate(gmem.keys()):
g_memory[index_of_ldst][iter] = gmem[key]
g_memory[index_of_ldst][iter] = gmem[key]
for i in memory:
if "[stack]" in i:
res = re.findall("[0-9a-fA-F]+-[0-9a-fA-F]+", i)
min = int(res[0].split("-")[0], 16)
max = int(res[0].split("-")[1], 16)
g_memory[index_of_ldst]["stack"] = []
g_memory[index_of_ldst]["stack"].append(min)
g_memory[index_of_ldst]["stack"].append(max)
if "[heap]" in i:
res = re.findall("[0-9a-fA-F]+-[0-9a-fA-F]+", i)
min = int(res[0].split("-")[0], 16)
max = int(res[0].split("-")[1], 16)
g_memory[index_of_ldst]["heap"] = []
g_memory[index_of_ldst]["heap"].append(min)
g_memory[index_of_ldst]["heap"].append(max)
if "esp" in i:
esp = i.rstrip("\n").split(" ")[1]
g_memory[index_of_ldst]["esp"] = []
g_memory[index_of_ldst]["esp"].append(int(esp))
class FunctionMapping:
def __init__(self, ir_file):
with open(ir_file, "r") as irf:
self.ir = irf.readlines()
def extractFuncDef(self):
func = {}
for line in self.ir:
if "define" in line:
line = line.rstrip("\n")
res = re.findall('@.*\(.*\)', line)
funcname = re.findall('.*\(', res[0].lstrip("@"))
funcname = funcname[0].rstrip(")")
paras = re.findall('\(.*\)', res[0])
args = paras[0].split(" ")
items = line.split(" ")
if "void" in line:
if funcname not in func:
func[funcname] = []
func[funcname].append("void")
else:
if funcname not in func:
func[funcname] = []
func[funcname].append(items[1])
for arg in args:
arg = arg.lstrip("(").rstrip(")")
if "%" in arg:
func[funcname].append(arg)
return func
def extractStruct(self):
# %struct.anon = type { i32*, i32*, [48 x float]*, [48 x float]*, [48 x float]* }
struct = {}
for line in self.ir:
if "struct" in line and "type" in line:
line = line.rstrip("\n")
str1 = line.split(" = ")
name = str1[0]
struct[name] = {}
str2 = str1[1].lstrip("type { ").rstrip(" }")
fields = str2.split(",")
for idx, item in enumerate(fields):
type = 0
item1 = item
item1 = item1.lstrip(" ").rstrip(" ")
if "*" in item1:
type = config.OSbits
else:
if "i" in item1:
res = re.findall("[-+]?[0-9]*\.?[0-9]+",item1)
if len(res) == 0:
type = 0
elif len(res) == 2:
type = int(math.ceil(int(res[1]) / 4.0)) * 4
else:
type = int(math.ceil(int(res[0]) / 4.0)) * 4
if "float" in item1:
type = 32
if "double" in item1:
type = 64
struct[name][idx] = type
return struct