-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_ARM_original.py
859 lines (732 loc) · 28 KB
/
format_ARM_original.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
import ast, json, os, pickle, re, time
from os.path import expanduser
from collections import defaultdict, deque
# gld formatting addition
def find_max(string):
result = [e for e in re.split("[^0-9]", string) if e != '']
# list 'result' elements are strings: ['3', '17', '14'], so we use map(int, list) to get integers
return max(map(int, result))
# end gdl addition
def handle_filename_too_long(long_name):
long_name = long_name.split(".gdl")[0:]
long_name = long_name[0]
before_at_1 = long_name.split("@")[0]
after_at_1 = long_name.split("@")[1]
new_name = before_at_1 +"@"+ after_at_1[:30] + after_at_1[-30:] + '_funcGNN_formatted.txt'
return new_name
def write_to_file(instruction_list, file_path):
with open(file_path, 'w') as fp_out:
for inst in instruction_list:
fp_out.write(inst + '\n')
fp_out.close()
def replace_dummy(token):
# replace dummy name prefixes from ida
# this has to be the last step of the whole filtering process
# refer to https://hex-rays.com/blog/igors-tip-of-the-week-34-dummy-names/
# replace string data type
if token.startswith('_STR_'):
return '<STRING>'
if token.startswith('sub_'):
return 'sub_<FOO>'
if token.startswith('locret_'):
return 'locret_<TAG>'
if token.startswith('def_'):
return 'def_<TAG>'
if token.startswith('loc_'):
return 'loc_<TAG>'
if token.startswith('off_'):
return 'off_<OFFSET>'
if token.startswith('seg_'):
return 'seg_<ADDR>'
if token.startswith('asc_'):
return 'asc_<STR>'
if token.startswith('byte_'):
return 'byte_<BYTE>'
if token.startswith('word_'):
return 'word_<WORD>'
if token.startswith('dword_'):
return 'DWORD_<WORD>'
if token.startswith('qword_'):
return 'qword_<WORD>'
if token.startswith('byte3_'):
return 'byte3_<BYTE>'
if token.startswith('xmmword_'):
return 'xmmword_<WORD>'
if token.startswith('ymmword_'):
return 'ymmword_<WORD>'
if token.startswith('packreal_'):
return 'packreal_<BIT>'
if token.startswith('flt_'):
return 'flt_<BIT>'
if token.startswith('dbl_'):
return 'dbl_<BIT>'
if token.startswith('tbyte_'):
return 'tbyte_<BYTE>'
if token.startswith('stru_'):
return 'stru_<TAG>'
if token.startswith('custdata_'):
return 'custdata_<TAG>'
if token.startswith('algn_'):
return 'algn_<TAG>'
if token.startswith('unk_'):
return 'unk_<TAG>'
return None
def helper(cur_token, function_names):
res = ''
zero = False
cur_puncs = deque()
for ch in cur_token:
if ch == '+':
cur_puncs.append('+')
elif ch == '-':
cur_puncs.append('-')
subs = re.split('\+|\-', cur_token)
for sub in subs:
sub = sub.strip()
if sub.startswith('#'):
sub = sub[1:]
if sub.startswith('('):
par = True
sub = sub[1:]
else:
par = False
# "#0x20+n"
if sub.isdigit() or sub.startswith('0x'):
res += '0'
zero = True
elif sub in function_names:
res += '<FOO>'
elif sub in arm_addrs:
res += '<ADDR>'
elif sub.startswith('var_'):
res += '<VAR>'
elif sub.startswith('arg_'):
res += '<ARG>'
elif replace_dummy(sub):
res += replace_dummy(sub)
elif zero:
res += '<ADDR>'
else:
res += '<TAG>'
if cur_puncs:
res += cur_puncs.popleft()
if par:
res += ')'
return res
def replace_labels(line, function_names):
for i, token in enumerate(line):
if token == '=':
if i == 0:
return None
continue
# tag the comma
if token[-1] == ',':
token = token[:-1]
comma = True
else:
comma = False
if token[0] == '#':
if i == 0:
return None
token = token[1:]
# # replace '#-' with '$'
token = re.sub('#(0x[0-9a-fA-F ]+)', '0', token)
token = re.sub('#-(0x[0-9a-fA-F ]+)', '$', token)
token = re.sub('#([0-9a-fA-F ]+)', '0', token)
token = re.sub('#-([0-9a-fA-F ]+)', '$', token)
# TODO
# decimals
# token = re.sub('#-[\d\.e]+', '$', token)
# token = re.sub('#[\d\.e]+', '0', token)
# token = re.sub('=-[\d\.e]+', '$', token)
# token = re.sub('=[\d\.e]+', '0', token)
# # INF
# token = re.sub('[#-Inf]+', '$', token)
# token = re.sub('[#+Inf]+', '0', token)
# token = re.sub('[=-Inf]', '$', token)
# token = re.sub('[=+Inf]', '0', token)
# # addresses
# token = re.sub('#([(\w. \-+)]+)', '<TAG>', token)
# token = re.sub('=([(\w. \-+)]+)', '<TAG>', token)
if token[0] == '=':
equal_sign = True
token = token[1:]
else:
equal_sign = False
if token[0] == '-':
minus_sign = True
token = token[1:]
else:
minus_sign = False
# conditional operators
# https://developer.arm.com/documentation/dui0068/b/ARM-Instruction-Reference/Conditional-execution
if token == 'EQ' or token == 'NE' or token == 'CS' or token == 'HS' or token == 'CC':
continue
if token == 'LO' or token == 'MI' or token == 'PL' or token == 'VS' or token == 'VC':
continue
if token == 'HI' or token == 'LS' or token == 'GE' or token == 'LT' or token == 'GT':
continue
if token == 'LE' or token == 'AL':
continue
if token in arm_directives:
return None
# replace function names:
if token in function_names:
if i == 0:
return None
line[i] = "<FOO>"
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
# replace function names:
if token.startswith('_STR_'):
if i == 0:
return None
line[i] = "<STRING>"
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
# :upper16: or :lower16:
if token.startswith(':upper16:'):
if i == 0:
return None
line[i] = 'UPPER<ADDR>'
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
if token.startswith(':lower16:'):
if i == 0:
return None
line[i] = 'LOWER<ADDR>'
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
if token.startswith("arg_"):
if i == 0:
return None
line[i] = '<ARG>'
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
if token.startswith("var_"):
if i == 0:
return None
line[i] = '<VAR>'
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
if token.startswith('jpt_'):
if i == 0:
return None
line[i] = 'JPT<ADDR>'
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
# dummy generated by IDA
dummy = replace_dummy(token)
if dummy:
if i == 0:
return None
line[i] = dummy
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
# immediate operands
if token.isdigit() or token.startswith('0x'):
if i == 0:
return None
line[i] = '0'
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
if token[0] == '(' and token[-1] == ')':
if i == 0:
return None
item = token[1:-1]
if '+' or '-' in item:
line[i] = helper(item, function_names)
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
if token[0] == '[':
if i == 0:
return None
# ['LDRB', 'R4,', '[R0],#1']
# [R3, #-2]
# LDR R3, [R10,#0x14]
# STRBVS R5, [R9,-R0,LSR#4]!
# split by comma:
exclamation = False
suffix = None
if token[-1] == '!':
items = token[1:-2].split(',')
exclamation = True
elif token[-1] == ']':
items = token[1:-1].split(',')
else:
items = token[1:].split(']')[0].split(',')
suffix = token[1:].split(']')[1]
cur = '['
for item in items:
if item in arm_regi_set:
cur += item
# STRBVS R5, [R9,-R0,LSR#4]!
elif item[0] == '-' and item[1:] in arm_regi_set:
cur += item
elif item == '0':
cur += '0'
elif item == '$':
cur += '$'
elif '+' in item or '-' in item:
cur += helper(item, function_names)
elif item.startswith('#'):
cur += '0'
# [R5,R2,LSL#2]
elif '0' in item or '$' in item:
cur += item
else:
cur += '<TAG>'
cur += ','
cur = cur[:-1]
if exclamation:
cur += ']!'
else:
cur += ']'
line[i] = cur
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if suffix:
line[i] += suffix
if comma:
line[i] += ','
continue
# registers
if token in arm_regi_set:
continue
if token[0] == '-1' and token [1:] in arm_regi_set:
continue
if i == 0:
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
if ',' in token:
line[i] = token
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
continue
# 'ADD.W R1, R1, R1,LSR#31'
if '#' in token:
idx = token.index('#')
line[i] = token[:idx] + '0'
else:
line[i] = '<TAG>'
if equal_sign:
line[i] = "=" + line[i]
if minus_sign:
line[i] = '-' + line[i]
if comma:
line[i] += ','
return line
# connect the line with ~
def write_line(line):
out_line = ''
for i, token in enumerate(line):
if i > 0 and out_line[-1] != ',':
out_line += '~'
out_line += token.upper()
out_line = re.sub('0RG_[0-9a-zA-Z]+', '<ARG>', out_line)
out_line = out_line.replace("$", "-0")
return out_line
def split_registers(token):
# POP.W {R4-R8,PC}
cur_registers = token.split(',')
register_list = []
for cur_regi in cur_registers:
if '-' in cur_regi:
cur_range = cur_regi.split('-')
# replace sp, lr, pc with r13 - r15
for cur_idx, cur_item in enumerate(cur_range):
if cur_item in map_regi:
cur_range[cur_idx] = map_regi[cur_item]
# R4-R11; D0-D5
char_regi = cur_range[0][0]
start, end = int(cur_range[0][1:]), int(cur_range[1][1:])
for val in range(start, end + 1):
register_list.append(char_regi + str(val))
else:
register_list.append(cur_regi)
return register_list
def stack_operation(line):
opcode = line[0]
if line[1][-2] == '!':
exclamation = '!'
else:
exclamation = ''
sec = line[1][:-1].replace('!','')
# stack operation on ARM:
# https://www.cnblogs.com/goodhacker/p/3206405.html
punc = '+'
if opcode.startswith('S'):
punc = '-'
elif opcode.startswith('L'):
punc = '+'
else:
unknown_opcodes.add(opcode)
register_list = []
result = []
# LDMDB R11, {R4-R11,SP,PC}
cur_registers = line[2][1:-1].split(',')
for cur_regi in cur_registers:
if '-' in cur_regi:
cur_range = cur_regi.split('-')
# replace sp, lr, pc with r13 - r15
# e.g., LDMFD SP, {SP-PC}
for cur_idx, cur_item in enumerate(cur_range):
if cur_item in map_regi:
cur_range[cur_idx] = map_regi[cur_item]
# R4-R11
char_regi = cur_range[0][0]
start, end = int(cur_range[0][1:]), int(cur_range[1][1:])
for val in range(start, end + 1):
register_list.append(char_regi + str(val))
else:
register_list.append(cur_regi)
if punc == '-':
register_list = register_list[::-1] # reverse
for regi_item in register_list:
result.append("{}~[{}{}0]{},{}".format(opcode, sec, punc, exclamation, regi_item))
return result
def replace_range(line, fp_out):
ori = line
line = line.split()
opcode = line[0]
if opcode.startswith('PUSH'):
rev = True
else:
rev = False
if len(line) == 2:
# POP.W {R4-R8,PC}
# LDM R5, {R9,R10}
register_list = split_registers(line[1][1:-1])
if rev:
register_list.reverse()
for regi_item in register_list:
written_line = line[0] + '~' + regi_item.upper()
written_line = re.sub('0RG_[0-9a-zA-Z]+', '<ARG>', written_line)
written_line = written_line.replace('$', '-0')
fp_out.write(written_line + ' ')
unique_instructions.add(written_line)
instruction_mapping[written_line].add(ori)
elif len(line) == 3:
# LDM R5!, {R0-R3}
# LDMDB R11, {R4-R11,SP,PC}
cur_lines = stack_operation(line)
for cur_line in cur_lines:
written_line = cur_line.upper()
written_line = re.sub('0RG_[0-9a-zA-Z]+', '<ARG>', written_line)
written_line = written_line.replace('$', '-0')
fp_out.write(written_line + ' ')
unique_instructions.add(written_line)
instruction_mapping[written_line].add(ori)
elif len(line) == 4 and line[2].startswith('{'):
# VTBL.8 D6, {D22-D23}, D24
idx_end = line[2].index('}')
cur_remain = line[2][idx_end + 1:]
register_list = split_registers(line[2][1:idx_end])
for regi_item in register_list:
written_line = line[0] + '~' + line[1] + regi_item.upper() + cur_remain + line[3]
written_line = re.sub('0RG_[0-9a-zA-Z]+', '<ARG>', written_line)
written_line = written_line.replace('$', '-0')
fp_out.write(written_line + ' ')
unique_instructions.add(written_line)
instruction_mapping[written_line].add(ori)
else:
with open(HOME + '/Desktop/Data/ARM/length_does_not_match.txt', 'w') as f:
f.write("LENGTH DOES NOT MATCH: " + ori + "\n\n")
return None
def gdl_preprocessing(input_file, output_file):
with open(input_file, 'r') as fp_in, open(output_file, 'w') as fp_out:
lines = fp_in.readlines()
for i, line in enumerate(lines):
if " = " in line or "%" in line:
continue
ori = line
# gdl formatting additions
if i > 10:
last_line = lines[-1]
if line != last_line:
next_line = lines[i+1]
if line.startswith('node:') and next_line.startswith('// node 0'):
fp_out.write('\n0')
continue
if line.startswith('node:') and next_line.startswith('node:'):
fp_out.write('\n0')
continue
if line.startswith('// node 0'):
fp_out.write('\n')
continue
# if line.startswith('// node'):
# continue
if line.startswith('colorentry'):
continue
if line.startswith('endbr64'):
continue
if line.startswith('}'):
continue
if line.startswith('node'):
fp_out.write('\n')
continue
if line.startswith('edge'):
line = line.replace('"', '')
res = [int(number) for number in line.split() if number.isdigit()]
fp_out.write(str(res) + " ")
continue
if line.startswith('// node 0'):
fp_out.write("\n")
continue
if line.startswith('// node '):
continue
if '"' in line:
line = line.split('"')
line = line[0]
if ';' in line:
line = line.split(';')
line = line[0]
else:
continue
# end gdl formatting
if '{' in line:
replace_range(line, fp_out)
else:
# replace " - " with '-'
line = line.replace(" - ", '-')
line = line.split()
if len(line) == 1:
continue
# ignore directives
if line[0] in arm_directives:
continue
formatted_line = replace_labels(line, function_names)
if not formatted_line:
continue
written_line = write_line(formatted_line)
fp_out.write(written_line + " ")
# fp_out.write(written_line + "\n")
unique_instructions.add(written_line)
instruction_mapping[written_line].add(ori)
fp_in.close()
fp_out.close()
# gdl formatting additions
with open(output_file, "r") as inp:
lines = inp.readlines()
if lines[0] == '\n':
lines = lines[1:]
count = len(lines)
# count = len(open(output_file).readlines())
new_file_lines = []
current_line = [None]*count
for i,line in enumerate(lines):
current_line[i] = line.strip()
if len(current_line) > 2 and not re.search('[a-zA-Z]', current_line[-1]):
new_file_lines.append(current_line[:len(current_line)-1])
new_file_lines[0] = list(filter(None, new_file_lines[0]))
new_file_lines.append(current_line[len(current_line)-1:])
# elif current_line[-1]
else:
new_file_lines.append(current_line)
new_file_lines[0] = list(filter(None, new_file_lines[0]))
with open(output_file, "w") as outfile:
if len(new_file_lines) > 1:
for line in new_file_lines[:-1]:
outfile.write("labels_ ")
jd = json.dumps(line)
print(jd,end="\n", file=outfile)
for line in new_file_lines[1:]:
line = str(line)
line = re.sub(r"\]\s","], ",line)
line = ast.literal_eval(line)
outfile.write("graph_ [" + line[0] + "]")
else:
for line in new_file_lines:
outfile.write("labels_ ")
jd = json.dumps(line)
print(jd,end="\n", file=outfile)
outfile.write("graph_ [[0, 0]]")
with open((output_file), 'r') as input:
lines = input.readlines()
labels = lines[0].split("_ ")
labels = ast.literal_eval(labels[1])
graph_length = len(labels)-1
graph = lines[1].split("_ ")
graph = graph[1]
graph_max = find_max(graph)
# while graph_length > graph_max:
# if labels[-1] == "0":
# labels.pop()
# graph_length = len(labels)-1
# else:
# break
while graph_max > graph_length:
labels.append('0')
graph_length = len(labels)-1
with open(output_file, "w") as outfile:
outfile.write("labels_ ")
jd = json.dumps(labels)
print(jd,end="\n", file=outfile)
outfile.write("graph_ " + str(graph))
# end gdl formatting
def preprocessing(input_file, output_file):
with open(input_file, 'r') as fp_in, open(output_file, 'w') as fp_out:
lines = fp_in.readlines()
for line in lines:
if " = " in line or "%" in line:
continue
ori = line
if '{' in line:
replace_range(line, fp_out)
else:
# replace " - " with '-'
line = line.replace(" - ", '-')
line = line.split()
if len(line) == 1:
continue
# ignore directives
if line[0] in arm_directives:
continue
formatted_line = replace_labels(line, function_names)
if not formatted_line:
continue
written_line = write_line(formatted_line)
fp_out.write(written_line + "\n")
unique_instructions.add(written_line)
instruction_mapping[written_line].add(ori)
fp_in.close()
fp_out.close()
# ref:
# https://developer.arm.com/documentation/dui0473/i/CJAJBFHC
# https://www.keil.com/support/man/docs/armasm/armasm_dom1359731136117.htm
arm_directives = set(['CODE16', 'CODE32', 'EXPORT', 'ALIGN', 'DCD', 'nptr', 'DCB'])
arm_addrs = set(['errnum', 'name', 's'])
gen_regi = set(['R' + str(i) for i in range(0, 16)] + ['R' + str(i) + '!' for i in range(0, 16)])
map_regi = {'SP': 'R13', 'LR': 'R14', 'PC': 'R15'}
var_regi = set(['V' + str(i) for i in range(1, 9)])
arg_regi = set(['A' + str(i) for i in range(1, 5)])
# floating point registers: https://blog.csdn.net/notbaron/article/details/106577545
float_regi = set(['D' + str(i) for i in range(0, 16)] + ['d' + str(i) for i in range(0, 16)] + ['S' + str(i) for i in range(0, 32)] + ['s' + str(i) for i in range(0, 32)])
# https://blog.51cto.com/u_14592069/2591169
cp15_regi = set(['C' + str(i) for i in range(0, 16)] + ['P' + str(i) for i in range(0, 16)] + ['c' + str(i) for i in range(0, 16)] + ['p' + str(i) for i in range(0, 16)])
# https://developer.arm.com/documentation/den0024/a/ARMv8-Registers/NEON-and-floating-point-registers/Scalar-register-sizes
scalar_regi = set (['B' + str(i) for i in range(0, 32)] + ['b' + str(i) for i in range(0, 32)] + ['D' + str(i) for i in range(0, 32)] + ['d' + str(i) for i in range(0, 32)])
scalar_regi2 = set (['H' + str(i) for i in range(0, 32)] + ['h' + str(i) for i in range(0, 32)] + ['Q' + str(i) for i in range(0, 32)] + ['q' + str(i) for i in range(0, 32)])
program_regi = set(['CPSR', 'SPSR'])
other_regi = set(['SB', 'IP', 'SP', 'LR', 'PC', 'FP'])
arm_regi_set = set.union(gen_regi, var_regi, arg_regi, float_regi, cp15_regi, program_regi, other_regi, scalar_regi, scalar_regi2)
########## MAIN ##########
HOME = expanduser("~")
architecture = 'arm'
folder_names = ['coreutils','diffutils','findutils','libgcrypt','libgpg-error','openssl','cmake']
# FILE TYPE
gdl = True
start_time = time.time()
instruction_mapping = defaultdict(set)
unique_instructions = set()
unknown_opcodes = set()
for folder_name in folder_names:
for x in range(0,4):
# UBUNTU
input_path = HOME + '/Desktop/Data/ARM/raws/' + folder_name + '-O' + str(x)
output_path = HOME + '/Desktop/Data/ARM/funcGNN_labeled_graphs/' + folder_name + '-O' + str(x)
if not os.path.exists(output_path):
os.makedirs(output_path)
# get all .txt file names
file_names = os.listdir(input_path)
file_names.remove('funcname.txt')
# load function names into a set
function_list = input_path + "/funcname.txt"
try:
with open(function_list, 'rb') as handle:
function_names = pickle.load(handle)
except:
with open(function_list, 'rb') as handle:
names = set(handle.readlines())
with open(input_path + "/funcname.txt", "wb") as f:
pickle.dump(names, f)
with open(function_list, 'rb') as handle:
function_names = pickle.load(handle)
for file in file_names:
input_file = input_path + '/' + file
output_file = output_path + '/' + file.split('.gdl')[0] + '_funcGNN_formatted.txt'
if gdl:
try:
gdl_preprocessing(input_file, output_file)
except OSError as exc:
if exc.errno == 36:
new_name = handle_filename_too_long(file)
new_file = output_path + '/' + new_name
gdl_preprocessing(input_file, new_file)
except:
print('GDL:' + input_file)
continue
input_path = HOME + '/Desktop/Data/ARM/raws/' + folder_name + '-O' + str(x)
inst_map_path = HOME + '/Desktop/Data/ARM/arm_txt/' + folder_name + '-O' + str(x) + '_inst_map.txt'
with open(inst_map_path, 'w') as f:
for key, val in instruction_mapping.items():
f.write(key + ':' + '\n')
for v in val:
f.write('\t' + v + '\n')
f.write('-' * 24 + '\n')
f.close()
unique_inst_path = HOME + '/Desktop/Data/ARM/arm_txt/ALL_unique_inst.txt'
write_to_file(unique_instructions, unique_inst_path)
execution_time = time.time() - start_time
print("--- {} seconds ---".format(execution_time))