-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.py
285 lines (232 loc) · 9.55 KB
/
binary.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
import angr
import pickle
import sys
import os
import capstone
import assembly
import logging
import numpy as np
from assembly import Assembly
from patcherex2 import *
from patcherex2.targets import ElfArmMimxrt1052
class Binary:
def __init__(self,bin_path,proj,isa_type):
self.bin_path = bin_path
self.proj = proj # Angr object
self.assembly = Assembly(isa_type)
self.patcher = None
if self.assembly.isaType() == "IMXRT1050":
logging.getLogger("patcherex").setLevel("DEBUG")
self.patcher = Patcherex(self.bin_path, target_cls=ElfArmMimxrt1052)
def applyPatches(self):
file_name = os.path.splitext(self.bin_path)[0]
ext = os.path.splitext(self.bin_path)[1]
self.patcher.apply_patches()
self.patcher.binfmt_tool.save_binary(f"{file_name}_patched{ext}")
def changeData(self, addrs, packed_bytes):
self.patcher.patches.append(ModifyDataPatch(addrs, packed_bytes))
self.applyPatches()
def extractNewOpAsm(self,path_to_asm,target_sym):
with open(path_to_asm) as file:
lines = [line.rstrip() for line in file]
if target_sym != "Conv":
label = target_sym + ":"
label_found = False
for line in lines:
if line == label:
label_found = True
if label_found == True:
tgt = self.assembly.getCallTgtSym(line)
if tgt != None:
target_sym = tgt
break
else:
target_sym = "libjit_conv2d_f"
asm = ""
for line in lines:
line = self.assembly.convertLabelLoadToPCRltv(line)
asm = asm + line + "\n"
return asm, target_sym
def extractCodeBytes(self,path_to_obj,target_sym):
obj = angr.Project(path_to_obj, auto_load_libs=False)
main_obj = obj.loader.main_object
text_section = None
for section in main_obj.sections:
if section.name == '.text':
text_section = section
break
sym_addr = None
symbol = obj.loader.find_symbol(target_sym)
if target_sym == "Conv":
symbol = obj.loader.find_symbol("libjit_conv2d_f")
#mask = 0xfffffffe
sym_addr = symbol.rebased_addr
if symbol:
print(f"Symbol {target_sym} found!")
print(f"Address: {hex(sym_addr)}")
else:
print(f"Symbol {target_sym} not found.")
if target_sym != "Conv":
block = obj.factory.block(sym_addr)
for insn in block.capstone.insns:
#print(f"0x{insn.address:x}: {insn.mnemonic} {insn.op_str}")
target_address = self.assembly.getCallTgt(insn)
if target_address is not None:
sym_addr = target_address
asm = "extracted_code:\n"
if text_section is None:
print(".text section not found.")
else:
start_addr = text_section.vaddr
size = text_section.memsize
# Extract the raw bytes from the memory using angr's memory object
text_bytes = obj.loader.memory.load(start_addr, size)
end_addr = start_addr + size
i = 0
while start_addr < end_addr:
if start_addr == sym_addr:
asm = asm + target_sym + ":\n"
b = text_bytes[i];
#asm = asm + "." + str(start_addr) + ":\n"
asm = asm + self.assembly.byte(b) + "\n"
i += 1
start_addr += 1
#print(asm)
return asm
def addNewOpToDispatcher(self,new_op_sym,predecessor_op):
dispatcher_func = self.proj.funcs[self.proj.dispatch_addr]
#op_call_sites = {}
if predecessor_op is None:
assert False
#Need to add implementation for OP added at the beginning of DNN
dispatcher_sym = "new_dispatcher"
asm = "new_dispatcher:\n";
for block in dispatcher_func.blocks:
for insn in block.capstone.insns:
asm = asm + " " + insn.mnemonic + " " + insn.op_str + "\n"
target_address = self.assembly.getCallTgt(insn)
if target_address is not None and target_address == predecessor_op.addr:
asm = asm + " " + self.assembly.saveGPR() + "\n"
asm = asm + " " + self.assembly.call(new_op_sym) + "\n"
asm = asm + " " + self.assembly.restoreGPR() + "\n"
return dispatcher_sym, asm
def getDispatchCallSite(self):
dispatcher_caller = self.proj.funcs[self.proj.dispatch_caller_addr]
dispatch_call_site = None
for block in dispatcher_caller.blocks:
for insn in block.capstone.insns:
target_address = self.assembly.getCallTgt(insn)
if target_address is not None and target_address == self.proj.dispatch_addr:
dispatch_call_site = insn.address
print("Dispatch call site: ",hex(dispatch_call_site))
return dispatch_call_site
def createCallSiteForNewOp(
self,predecessor_op, successor_op, target_sym,
weight_sym, bias_sym
):
#currently on handle ARM and currently handling only input/output buffer
#passing.
#Needs to be improved to pass a generic list of arguments.
input_buffer = predecessor_op.writebuffer[0]
output_buffer = successor_op.readbuffer[0]
if input_buffer is None and output_buffer is None:
print("Input/Output buffer not available")
assert False
elif input_buffer is None:
input_buffer = output_buffer
elif output_buffer is None:
output_buffer = input_buffer
tramp_sym = target_sym + "_tramp"
asm = tramp_sym + ":\n"
asm = asm + self.assembly.callArgConst(input_buffer[0],1)
asm = asm + self.assembly.callArgConst(output_buffer[0],2)
if weight_sym is not None:
asm = asm + self.assembly.callArgLabel(weight_sym,3)
if bias_sym is not None:
asm = asm + self.assembly.callArgLabel(bias_sym,4)
asm = asm + self.assembly.jump(target_sym) + "\n"
return tramp_sym,asm
def readWeights(self, new_op):
w_file = new_op.weightsfile
asm = " .p2align 16\n"
weight_label = "weight"
bias_label = "bias"
bias_size = new_op.op.info["output_channel"] * 4
ctr = 0
asm = asm + bias_label + ":\n"
bias_crossed = False
weight_found = False
with open(w_file, "rb") as file:
while True:
byte = file.read(1)
if not byte: # End of file
break
# Process the byte here
if bias_crossed == True and weight_found == False:
if ctr % 64 == 0:
weight_found = True
asm = asm + weight_label + ":\n"
byte_int = my_int = int.from_bytes(byte, byteorder='big')
asm = asm + self.assembly.byte(byte_int) + "\n"
ctr = ctr + 1
if ctr >= bias_size:
bias_crossed = True
return weight_label,bias_label,asm
def addNewOp(self, new_op):
cmd = (
"~/glow/build_Debug/bin/model-compiler -backend=CPU -model=" +
new_op.onnxfile +
" -emit-bundle=tmp/ -target=arm -mcpu=cortex-m7 --llvm-save-asm"
)
os.system(cmd)
asm_file = new_op.asmfile
formatted_asm_file = "tmp/" + new_op.name + "_filtered.s"
cmd = (
"cat " + asm_file +
" | grep -v " +
"\"\\.text\\|\\.syntax\\|\\.eabi\\|\\.fpu\\|\\.file\\|" +
"\\.section\\|\\.type\\|\\.code\\|\\.thumb_func\\|" +
"\\.fnstart\\|\\.size\\|\\.cantunwind\\|\\.fnend\\|\\.ident\\|" +
"\\.globl\" > " + formatted_asm_file
)
print(cmd)
os.system(cmd)
#op_asm = self.extractCodeBytes(new_op.objfile, new_op.name)
op_asm,tgt_sym = self.extractNewOpAsm(formatted_asm_file, new_op.name)
weight_sym = None
bias_sym = None
weight_asm = ""
if new_op.name == "Conv":
weight_sym,bias_sym,weight_asm = self.readWeights(new_op)
#op_asm = op_asm + weight_asm
tramp_sym, tramp_asm = self.createCallSiteForNewOp(
new_op.op.parent,
list(new_op.op.child)[0],
#new_op.name,
tgt_sym,
weight_sym,bias_sym
)
dispatcher_sym, dispatcher_asm = self.addNewOpToDispatcher(
tramp_sym,
new_op.op.parent
)
op_asm = dispatcher_asm + weight_asm + tramp_asm + op_asm
test_asm = "tmp/" + new_op.name + "_patch.asm"
text_file = open(test_asm, "w")
text_file.write(op_asm)
text_file.close()
#print(dispatcher_asm)
#print(op_asm)
self.patcher.patches.append(
InsertInstructionPatch("dispatch_new", op_asm, is_thumb=True)
)
dispatch_call_site = self.getDispatchCallSite()
print("Dispatch call site: ",hex(dispatch_call_site))
mask = 0xfffffffe
self.patcher.patches.append(
ModifyInstructionPatch(
dispatch_call_site & mask,
self.assembly.call("{dispatch_new}")
)
)
self.applyPatches()