-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
1095 lines (896 loc) · 38.2 KB
/
inference.py
File metadata and controls
1095 lines (896 loc) · 38.2 KB
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import random
import torch
import os
import numpy as np
import pickle
import argparse
from tqdm import tqdm
import matplotlib.pyplot as plt
from model import PLModelForAST, pearson_score
from dataset import ASTGraphDataModule
from typing import Dict, List, Tuple
from sklearn.metrics import roc_auc_score
import dgl
def get_cos_similar_multi(v1, v2):
num = np.dot([v1], v2.T) # 向量点乘
denom = np.linalg.norm(v1) * np.linalg.norm(v2) # 求模长的乘积
res = num / denom
return 0.5 + 0.5 * res
def similarity_score(query, vectors):
distance = np.linalg.norm(query - vectors, axis=-1)
score = 1 / (1 + distance)
return score
def get_pearson_score(query, vectors):
cov = (query * vectors).mean(axis=-1)
pearson = cov / (query.std(axis=-1) * vectors.std(axis=-1))
return abs(pearson), pearson
class FunctionEmbedding:
def __init__(self, name: str, embedding):
self.name = name
self.embedding = embedding
self.cosine = 0
class ModelConfig:
def __init__(self):
# file location
self.model_path = (
"lightning_logs/c_re_door/checkpoints/epoch=59-step=42554.ckpt"
)
self.dataset_path = "./c_language"
self.exclude_list = []
# Runtime Setting
self.cuda = False
# If no dataset is provided
self.feature_length = 139
self.max_length = 1000
# Model Hyper-Parameters
self.alpha = 0.2
self.dropout = 0.3
self.hidden_features = 64
self.n_heads = 6
self.output_features = 128
# Predict
self.topK = 3
class InferenceModel:
def __init__(self, config: ModelConfig):
self.config = config
torch.set_float32_matmul_precision("high")
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:100"
if self.config.dataset_path:
total_path = os.path.join(self.config.dataset_path, "!total.pkl")
self.dataset = ASTGraphDataModule(
total_path, exclude=self.config.exclude_list, batch_size=1
)
self.dataset.prepare_data()
self.feature_length = self.dataset.feature_length
self.max_length = self.dataset.max_length
else:
self.feature_length = self.config.feature_length
self.max_length = self.config.max_length
self.dataset = None
self.model = PLModelForAST.load_from_checkpoint(
self.config.model_path, strict=False
)
if self.config.cuda:
self.device_name = "cuda"
else:
self.device_name = "cpu"
if self.config.cuda:
self.model = self.model.cuda()
else:
self.model = self.model.cpu()
self.model = torch.compile(self.model)
self.model.eval()
def single_dgl_to_embedding(self, data: dgl.DGLGraph):
with torch.no_grad():
# padding = self.max_length - data.num_nodes()
# data = dgl.add_nodes(data, padding)
# data = dgl.add_self_loop(data)
if self.config.cuda:
data = data.to("cuda")
tembedding = self.model.my_model(data)
tembedding = tembedding.detach().cpu()
embedding = tembedding.numpy()
del tembedding
return embedding
def judge(self, func: str, candidate: list):
if func in candidate:
return True
return False
def merge_dgl_dict(self, dataset: dict, graphs: List[dgl.DGLGraph]):
# Collect all graphs and their positions to be processed
tasks = []
positions = []
for binary in dataset['data']:
for function_name in dataset['data'][binary]:
for i in range(len(dataset['data'][binary][function_name])):
index = dataset['data'][binary][function_name][i]['index']
tasks.append(graphs[index])
positions.append((binary, function_name, i))
# Set up progress bar
pbar = tqdm(total=len(tasks))
pbar.set_description("Padding Graphs")
# Process all graphs serially
prepared_graphs = {}
for i, graph in enumerate(tasks):
# Process a single graph
padding = self.max_length - graph.num_nodes()
graph = dgl.add_nodes(graph, padding)
graph = dgl.add_self_loop(graph)
prepared_graphs[i] = graph
pbar.update()
pbar.close()
print("Finished Converting... Fetching Data From Pool")
pbar = tqdm(total=(len(tasks)))
pbar.set_description("Inference")
# Batch processing parameters
batch_size = 256 # Adjust based on GPU memory size
results = [None] * len(tasks) # Pre-allocate results list
# Process prepared graphs in batches
for i in range(0, len(tasks), batch_size):
batch_indices = range(i, min(i + batch_size, len(tasks)))
batch_padding = []
# Use already prepared graphs
for idx in batch_indices:
graph = prepared_graphs[idx]
if self.config.cuda:
graph = graph.to('cuda')
batch_padding.append(graph)
# Batch processing
if len(batch_padding) > 0:
batched_graphs = dgl.batch(batch_padding)
with torch.no_grad():
embeddings = self.model.my_model(batched_graphs)
embeddings = embeddings.detach().cpu().numpy()
# Unbatch and store results
for idx, embedding in zip(batch_indices, np.split(embeddings, len(batch_padding))):
results[idx] = embedding
# Periodically clear GPU cache
if self.config.cuda:
torch.cuda.empty_cache()
# Update progress bar
pbar.update(batch_size)
# Put results back into the dataset
for (binary, function_name, i), embedding in zip(positions, results):
dataset['data'][binary][function_name][i]['embedding'] = embedding
pbar.close()
return dataset
def test_strip_recall_K(
self,
dataset_origin: dict,
dataset_strip: dict,
graph_strip: list,
max_k: int = 10,
n_candidates: int = 100,
mode: str = "pool",
cache_path: str = "",
):
# Dataset 2 simulates a harder comparison
recall = {x: [] for x in range(1, max_k + 1)}
self.config.topK = max_k
if cache_path:
if os.path.exists(cache_path):
with open(cache_path, "rb") as f:
dataset_strip = pickle.load(f)
f.close()
else:
dataset_strip = self.merge_dgl_dict(dataset_strip, graph_strip)
with open(cache_path, "wb") as f:
pickle.dump(dataset_strip, f)
f.close()
else:
dataset_strip = self.merge_dgl_dict(dataset_strip, graph_strip)
pbar = tqdm(total=self.get_dataset_function_num(dataset_strip))
length = []
temp_ref = []
for binary in dataset_origin["data"]:
record_total = {x: [0, 0] for x in range(1, max_k + 1)}
if binary not in dataset_strip["data"]:
continue
candidate_pool, candidate_name_list = self.get_function_file_set(
dataset=dataset_strip, binary_name=binary
)
function_list1 = dataset_origin["data"][binary].keys()
for function_name in function_list1:
pbar.update()
if len(dataset_origin["data"][binary][function_name]) < 2:
continue
if function_name.startswith("function_"):
continue
if function_name not in dataset_strip["data"][binary]:
for k in range(1, max_k + 1):
record_total[k][1] += 1
continue
if len(dataset_strip["data"][binary][function_name]) < 2:
continue
for function_body in dataset_origin["data"][binary][function_name]:
left_function = random.choice(
dataset_strip["data"][binary][function_name]
)
right_function = left_function
random_count = 0
while (
right_function["opt"] == left_function["opt"]
or right_function["arch"] == left_function["arch"]
):
right_function = random.choice(
dataset_strip["data"][binary][function_name]
)
random_count += 1
if random_count > 100:
break
if random_count > 100:
continue
left_embedding = left_function["embedding"]
right_embedding = right_function["embedding"]
arch, opt = function_body["arch"], function_body["opt"]
function_names = []
function_candidates = []
random_count = 0
for i in range(n_candidates):
arch, opt = random.choice(list(candidate_pool.keys()))
while (
arch == function_body["arch"] or opt == function_body["opt"]
):
arch, opt = random.choice(list(candidate_pool.keys()))
random_count += 1
if random_count > 100:
break
if random_count > 100:
break
selected = random.choice(candidate_pool[(arch, opt)])
function_names.append(selected.name)
function_candidates.append(selected.embedding)
if random_count > 100:
# No right function, continue
continue
function_candidates.append(right_embedding)
function_names.append(function_name)
function_candidates = np.vstack(function_candidates)
length.append(len(function_candidates))
# mm = similarity_score(left_embedding, function_candidates)
mm, ref = get_pearson_score(left_embedding, function_candidates)
rank_list = sorted(
zip(mm.reshape(-1), function_names),
key=lambda x: x[0],
reverse=True,
)[: self.config.topK]
for k in range(1, max_k + 1):
is_correct = self.judge(
right_function["name"], [x[1] for x in rank_list[:k]]
)
record_total[k][0] += int(is_correct)
record_total[k][1] += 1
if k == 1:
temp_ref.append(rank_list[0][2])
success_result = True
for k in range(1, max_k + 1):
if record_total[k][1] == 0:
success_result = False
break
if not success_result:
continue
for k in range(1, max_k + 1):
recall[k].append(record_total[k][0] / record_total[k][1])
recall_avg = []
for k in range(1, max_k + 1):
recall_avg.append(np.mean(recall[k]))
pbar.close()
# print("recall_avg", recall_avg)
print("Pearson", np.mean(temp_ref), np.std(temp_ref))
return recall_avg, temp_ref
# @profile
def test_recall_K(
self,
dataset: dict,
graph_path: str,
max_k: int = 10,
n_candidates: int = 100,
mode: str = "pool",
cache_path: str = "",
):
recall = {x: [] for x in range(1, max_k + 1)}
self.config.topK = max_k
if cache_path:
if os.path.exists(cache_path):
with open(cache_path, "rb") as f:
dataset = pickle.load(f)
f.close()
else:
graph, _ = dgl.load_graphs(graph_path)
dataset = self.merge_dgl_dict(dataset, graph)
with open(cache_path, "wb") as f:
pickle.dump(dataset, f)
f.close()
else:
graph, _ = dgl.load_graphs(graph_path)
dataset = self.merge_dgl_dict(dataset, graph)
pbar = tqdm(total=self.get_dataset_function_num(dataset))
length = []
for binary in dataset["data"]:
record_total = {x: [0, 0] for x in range(1, max_k + 1)}
print("Generating Function Pool for {}".format(binary))
candidate_pool, candidate_name_list = self.get_function_file_set(
dataset=dataset, binary_name=binary
)
# return
function_list1 = dataset["data"][binary].keys()
for function_name in function_list1:
for function_body in dataset["data"][binary][function_name]:
pbar.update()
if len(dataset["data"][binary][function_name]) < 2:
continue
arch, opt = function_body["arch"], function_body["opt"]
if (arch, opt) not in candidate_pool:
continue
name, query_embedding = function_body["name"], FunctionEmbedding(
name=function_body["name"], embedding=function_body["embedding"]
)
query_embedding: FunctionEmbedding
query_embedding = query_embedding.embedding
mat2 = []
name_list = []
itself = random.choice(dataset["data"][binary][function_name])
random_count = 0
while (itself["arch"], itself["opt"]) == (arch, opt):
itself = random.choice(dataset["data"][binary][function_name])
random_count += 1
if random_count > 100:
break
if random_count > 100:
continue
# mat2.append(function_body['embedding'])
if mode == "file":
random_arch = (itself["arch"], itself["opt"])
for c in candidate_pool[random_arch]:
if c.name == name:
continue
c: FunctionEmbedding
mat2.append(c.embedding)
name_list.append(c.name)
if n_candidates:
if len(mat2) > n_candidates:
mat2 = random.sample(mat2, n_candidates)
name_list = random.sample(name_list, n_candidates)
else:
mat2.append(itself["embedding"])
name_list.append(itself["name"])
assert (
n_candidates > 0
), "If you choose pool mode, you must specify the number of candidates"
for i in range(n_candidates):
arch, opt = random.choice(list(candidate_pool.keys()))
while (
arch == function_body["arch"]
or opt == function_body["opt"]
):
arch, opt = random.choice(list(candidate_pool.keys()))
selected = random.choice(candidate_pool[(arch, opt)])
name_list.append(selected.name)
mat2.append(selected.embedding)
mat2 = np.vstack(mat2)
length.append(len(mat2))
# mm = get_cos_similar_multi(query_embedding, mat2)
# mm = similarity_score(query_embedding, mat2)
mm, ref = get_pearson_score(query_embedding, mat2)
rank_list = sorted(
zip(mm.reshape(-1), name_list), key=lambda x: x[0], reverse=True
)[: self.config.topK]
for k in range(1, max_k + 1):
is_correct = self.judge(name, [x[1] for x in rank_list[:k]])
record_total[k][0] += int(is_correct)
record_total[k][1] += 1
success_result = True
for k in range(1, max_k + 1):
if record_total[k][1] == 0:
success_result = False
break
if not success_result:
continue
for k in range(1, max_k + 1):
recall[k].append(record_total[k][0] / record_total[k][1])
# return
recall_avg = []
for k in range(1, max_k + 1):
recall_avg.append(np.mean(recall[k]))
pbar.close()
# print("recall_avg", recall_avg, "\n")
return recall_avg
def test_recall_K_asm(
self,
dataset: dict,
graph_path: str,
max_k: int = 10,
n_candidates: int = 100,
cache_path: str = "",
):
recall = {x: [] for x in range(1, max_k + 1)}
self.config.topK = max_k
if cache_path:
if os.path.exists(cache_path):
with open(cache_path, "rb") as f:
dataset = pickle.load(f)
f.close()
else:
graph, _ = dgl.load_graphs(graph_path)
dataset = self.merge_dgl_dict(dataset, graph)
with open(cache_path, "wb") as f:
pickle.dump(dataset, f)
f.close()
else:
graph, _ = dgl.load_graphs(graph_path)
dataset = self.merge_dgl_dict(dataset, graph)
pbar = tqdm(total=self.get_dataset_function_num(dataset))
length = []
for binary in dataset["data"]:
record_total = {x: [0, 0] for x in range(1, max_k + 1)}
print("Generating Function Pool for {}".format(binary))
candidate_pool, candidate_name_list = self.get_function_file_set(
dataset=dataset, binary_name=binary
)
# return
function_list1 = dataset["data"][binary].keys()
for function_name in function_list1:
for function_body in dataset["data"][binary][function_name]:
pbar.update()
if len(dataset["data"][binary][function_name]) < 2:
continue
arch, opt = function_body["arch"], function_body["opt"]
if (arch, opt) not in candidate_pool:
continue
name, query_embedding = function_body["name"], FunctionEmbedding(
name=function_body["name"], embedding=function_body["embedding"]
)
query_embedding: FunctionEmbedding
query_embedding = query_embedding.embedding
mat2 = []
name_list = []
itself = random.choice(dataset["data"][binary][function_name])
random_count = 0
while itself["opt"] == opt:
itself = random.choice(dataset["data"][binary][function_name])
random_count += 1
if random_count > 100:
break
if random_count > 100:
continue
# mat2.append(function_body['embedding'])
assert (
n_candidates > 0
), "If you choose pool mode, you must specify the number of candidates"
for i in range(n_candidates):
arch, opt = random.choice(list(candidate_pool.keys()))
while opt == function_body["opt"]:
arch, opt = random.choice(list(candidate_pool.keys()))
selected = random.choice(candidate_pool[(arch, opt)])
name_list.append(selected.name)
mat2.append(selected.embedding)
mat2.append(itself["embedding"])
mat2 = np.vstack(mat2)
length.append(len(mat2))
# mm = get_cos_similar_multi(query_embedding, mat2)
# mm = similarity_score(query_embedding, mat2)
# NaN output corrupt the result, change the seq to mitigate
mm, ref = get_pearson_score(query_embedding, mat2)
rank_list = sorted(
zip(mm.reshape(-1), name_list + [function_name]),
key=lambda x: x[0],
reverse=True,
)[: self.config.topK]
for k in range(1, max_k + 1):
is_correct = self.judge(name, [x[1] for x in rank_list[:k]])
record_total[k][0] += int(is_correct)
record_total[k][1] += 1
success_result = True
for k in range(1, max_k + 1):
if record_total[k][1] == 0:
success_result = False
break
if not success_result:
continue
for k in range(1, max_k + 1):
recall[k].append(record_total[k][0] / record_total[k][1])
# return
recall_avg = []
for k in range(1, max_k + 1):
recall_avg.append(np.mean(recall[k]))
pbar.close()
# print("recall_avg", recall_avg, "\n")
return recall_avg
def test_recall_K_x86(
self,
dataset: dict,
graph_path: str,
max_k: int = 10,
n_candidates: int = 100,
cache_path: str = "",
):
recall = {x: [] for x in range(1, max_k + 1)}
self.config.topK = max_k
if cache_path:
if os.path.exists(cache_path):
with open(cache_path, "rb") as f:
dataset = pickle.load(f)
f.close()
else:
graph, _ = dgl.load_graphs(graph_path)
dataset = self.merge_dgl_dict(dataset, graph)
with open(cache_path, "wb") as f:
pickle.dump(dataset, f)
f.close()
else:
graph, _ = dgl.load_graphs(graph_path)
dataset = self.merge_dgl_dict(dataset, graph)
pbar = tqdm(total=self.get_dataset_function_num(dataset))
length = []
for binary in dataset["data"]:
record_total = {x: [0, 0] for x in range(1, max_k + 1)}
print("Generating Function Pool for {}".format(binary))
candidate_pool, candidate_name_list = self.get_function_file_set(
dataset=dataset, binary_name=binary
)
# return
function_list1 = dataset["data"][binary].keys()
for function_name in function_list1:
for function_body in dataset["data"][binary][function_name]:
pbar.update()
if len(dataset["data"][binary][function_name]) < 2:
continue
arch, opt = function_body["arch"], function_body["opt"]
if (arch, opt) not in candidate_pool:
continue
name, query_embedding = function_body["name"], FunctionEmbedding(
name=function_body["name"], embedding=function_body["embedding"]
)
query_embedding: FunctionEmbedding
query_embedding = query_embedding.embedding
itself = random.choice(dataset["data"][binary][function_name])
random_count = 0
while itself["opt"] == opt:
itself = random.choice(dataset["data"][binary][function_name])
random_count += 1
if random_count > 100:
break
if random_count > 100:
continue
mat2 = [itself["embedding"]]
name_list = [function_name]
# mat2.append(function_body['embedding'])
assert (
n_candidates > 0
), "If you choose pool mode, you must specify the number of candidates"
for i in range(n_candidates):
arch, opt = random.choice(list(candidate_pool.keys()))
while opt == function_body["opt"]:
arch, opt = random.choice(list(candidate_pool.keys()))
selected = random.choice(candidate_pool[(arch, opt)])
name_list.append(selected.name)
mat2.append(selected.embedding)
mat2.append(itself["embedding"])
mat2 = np.vstack(mat2)
length.append(len(mat2))
# mm = get_cos_similar_multi(query_embedding, mat2)
# mm = similarity_score(query_embedding, mat2)
mm, ref = get_pearson_score(query_embedding, mat2)
rank_list = sorted(
zip(mm.reshape(-1), name_list), key=lambda x: x[0], reverse=True
)[: self.config.topK]
for k in range(1, max_k + 1):
is_correct = self.judge(name, [x[1] for x in rank_list[:k]])
record_total[k][0] += int(is_correct)
record_total[k][1] += 1
success_result = True
for k in range(1, max_k + 1):
if record_total[k][1] == 0:
success_result = False
break
if not success_result:
continue
for k in range(1, max_k + 1):
recall[k].append(record_total[k][0] / record_total[k][1])
# return
recall_avg = []
for k in range(1, max_k + 1):
recall_avg.append(np.mean(recall[k]))
pbar.close()
# print("recall_avg", recall_avg, "\n")
return recall_avg
# @profile
def get_function_file_set(
self, dataset: dict, binary_name
) -> Tuple[Dict[tuple, List[FunctionEmbedding]], Dict[tuple, List[str]]]:
candidate_pool: Dict[tuple, List[FunctionEmbedding]] = {}
candidate_name_pool: Dict[tuple, List[str]] = {}
count = 0
for function_name in dataset["data"][binary_name]:
for function_body in dataset["data"][binary_name][function_name]:
arch, opt = function_body["arch"], function_body["opt"]
if (arch, opt) not in candidate_pool:
candidate_pool[(arch, opt)] = []
candidate_name_pool[(arch, opt)] = []
name, embedding = function_body["name"], FunctionEmbedding(
name=function_body["name"], embedding=function_body["embedding"]
)
candidate_pool[(arch, opt)].append(embedding)
candidate_name_pool[(arch, opt)].append(name)
count += 1
# if count >= 5:
# return candidate_pool, candidate_name_pool
return candidate_pool, candidate_name_pool
def AUC(self, dataset: dict, graphs: List[dgl.DGLGraph]):
scores = []
labels = []
dataset = self.merge_dgl_dict(dataset, graphs)
for binary_name in dataset["data"]:
for function_name in dataset["data"][binary_name]:
if len(dataset["data"][binary_name][function_name]) < 2:
continue
for i in range(len(dataset["data"][binary_name][function_name])):
this_embedding = dataset["data"][binary_name][function_name][i][
"embedding"
]
candidates = dataset["data"][binary_name][function_name].copy()
del candidates[i]
same = random.choice(candidates)
diff = random.choice(
self.get_different_function_sample(
dataset, binary_name, function_name
)
)
same_embedding = same["embedding"]
diff_embedding = diff["embedding"]
same_score = pearson_score(this_embedding, same_embedding)
diff_score = pearson_score(this_embedding, diff_embedding)
scores.extend([same_score, diff_score])
labels.extend([1, 0])
roc_score = roc_auc_score(y_true=labels, y_score=scores)
print(roc_score)
return roc_score
def get_different_function_sample(
self, dataset: dict, binary_name: str, function_name: str
):
selected_binary_name = random.choice(list(dataset["data"].keys()))
selected_function_name = random.choice(
list(dataset["data"][selected_binary_name].keys())
)
while (
selected_binary_name == binary_name
and selected_function_name == function_name
):
selected_binary_name = random.choice(list(dataset["data"].keys()))
selected_function_name = random.choice(
list(dataset["data"][selected_binary_name].keys())
)
return random.choice(
dataset["data"][selected_binary_name][selected_function_name]
)
def get_dataset_function_num(self, dataset: dict):
num = 0
for binary_name in dataset["data"]:
for function_name in dataset["data"][binary_name]:
num += len(dataset["data"][binary_name][function_name])
return num
def load_pickle(path):
with open(path, "rb") as f:
data = pickle.load(f)
f.close()
return data
def save_pickle(data, path):
with open(path, "wb") as f:
pickle.dump(data, f)
f.close()
def load_model(path, use_cuda=True):
model_config = ModelConfig()
model_config.model_path = path
model_config.dataset_path = ""
model_config.feature_length = 151
model_config.max_length = 1000
model_config.cuda = use_cuda
model_config.topK = 50
model = InferenceModel(model_config)
return model
def evaluate_dataset_1(pretrained_model, dataset_folder, use_cuda=True):
model = load_model(pretrained_model, use_cuda)
graph_path = os.path.join(dataset_folder, "dataset_1", "dgl_graphs.dgl")
dataset = load_pickle(
os.path.join(dataset_folder, "dataset_1", "index_test_data_{}.pkl".format(5))
)
res = model.test_recall_K(
dataset, graph_path, max_k=50, mode="pool", n_candidates=100, cache_path=""
)
return res
def evaluate_dataset_1_asm_x86(
pretained_model_asm, pretrained_model_x86, dataset_folder
):
model_asm = load_model(pretained_model_asm, use_cuda=True)
graph_path = os.path.join(dataset_folder, "dataset_1_asm", "dgl_graphs.dgl")
dataset = load_pickle(
os.path.join(
dataset_folder, "dataset_1_asm", "index_test_data_{}.pkl".format(5)
)
)
res_asm = model_asm.test_recall_K_asm(
dataset, graph_path, max_k=50, n_candidates=100, cache_path=""
)
model_x86 = load_model(pretrained_model_x86)
graph_path = os.path.join(dataset_folder, "dataset_1_x86", "dgl_graphs.dgl")
dataset = load_pickle(
os.path.join(
dataset_folder, "dataset_1_x86", "index_test_data_{}.pkl".format(5)
)
)
res_x86 = model_x86.test_recall_K_x86(
dataset, graph_path, max_k=50, n_candidates=100, cache_path=""
)
return res_asm, res_x86
def evaluate_dataset_2(pretrained_model, dataset_folder, use_cuda=True):
model = load_model(pretrained_model, use_cuda)
total_res = []
graph_strip_path = os.path.join(dataset_folder, "dataset_2", "dgl_graphs.dgl")
graph_strip, _ = dgl.load_graphs(graph_strip_path)
for i in range(1, 6):
print("Running Fold: ", i)
dataset_origin = load_pickle(
os.path.join(
dataset_folder, "dataset_2", "index_test_data_{}.pkl".format(i)
)
)
res, ref = model.test_strip_recall_K(
dataset_origin=dataset_origin,
dataset_strip=dataset_origin,
graph_strip=graph_strip,
max_k=50,
n_candidates=100,
)
total_res.append(res)
total_res = np.array(total_res)
mean_res = np.mean(total_res, axis=0)
return mean_res
def evaluate_dataset_2_strip(pretrained_model, dataset_folder, use_cuda=True):
model = load_model(pretrained_model, use_cuda)
total_res = []
graph_strip_path = os.path.join(
dataset_folder, "dataset_2_decompressed_stripped", "dgl_graphs.dgl"
)
graph_strip, _ = dgl.load_graphs(graph_strip_path)
for i in range(1, 6):
print("Running Fold: ", i)
dataset_strip = load_pickle(
os.path.join(
dataset_folder,
"dataset_2_decompressed_stripped",
"index_test_data_{}.pkl".format(i),
)
)
dataset_origin = load_pickle(
os.path.join(
dataset_folder,
"dataset_2_decompressed_nostrip",
"index_test_data_{}.pkl".format(i),
)
)
res, _ = model.test_strip_recall_K(
dataset_origin=dataset_origin,
dataset_strip=dataset_strip,
graph_strip=graph_strip,
max_k=50,
n_candidates=100,
)
total_res.append(res)
total_res = np.array(total_res)
mean_res = np.mean(total_res, axis=0)
return mean_res
def evaluate_dataset_openplc(
pretrained_model, openplc_dataset_folder, index_test_data_path, top_k, use_cuda=True
):
model = load_model(pretrained_model, use_cuda)
model.config.topK = top_k
graph_path = os.path.join(openplc_dataset_folder, "dgl_graphs.dgl")
dataset = load_pickle(index_test_data_path)
res = model.test_recall_K(
dataset, graph_path, max_k=50, mode="pool", n_candidates=100, cache_path=""
)
return res
def parse_args():
ap = argparse.ArgumentParser()
ap.add_argument(
"-p",
"--pretrained_model",
required=True,
help="Path to the pretrained model folder",
)
ap.add_argument(
"-d", "--graph_dataset", required=True, help="Path to the graph dataset folder"
)
ap.add_argument(
"-c",
"--cpu",
action="store_true",
help="Enforce using CPU or not, adding this flag will enforce using CPU",
)
args = ap.parse_args()
return args
def plot_recall_k(ax: plt.Axes, label, recall, label2=None, recall2=None):
if isinstance(recall, list):
recall = np.array(recall)