-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathevaluate_vgraph.py
699 lines (638 loc) · 27.5 KB
/
evaluate_vgraph.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
import os
import subprocess
import filecmp
from tqdm import tqdm
import numpy as np
import pickle as pkl
from src.graph.utils import load_vgraph_db, load_target_db
from src.matching.triplet_match import *
from multiprocessing import Pool,Process, Queue, SimpleQueue
import time
def decision_function(cvg_score, pvg_score, nvg_score):
return cvg_score >= CVG_THRESH and pvg_score >= PVG_THRESH and pvg_score > nvg_score
def consume(work):
(target_id, vg, t_trips) = work
cvg_score, pvg_score, nvg_score = triplet_match_exact(vg, t_trips)
return (target_id, vg, cvg_score, pvg_score, nvg_score)
def generate_ground_truth(target_graphs):
NUM_VULN=0
NUM_PATCH=0
NUM_BEFORE=0
NUM_AFTER=0
for g in target_graphs:
repo,cve,t,hash,f,_,_=g['path'].split('/')[-7:]
func=g['base_name']
if t == 'vuln':
NUM_VULN += 1
elif t == 'patch':
NUM_PATCH += 1
elif t == 'before':
NUM_BEFORE += 1
elif t == 'after':
NUM_AFTER += 1
print("Ground truth stats:")
print("NUM_VULN: %d" % NUM_VULN)
print("NUM_PATCH: %d" % NUM_PATCH)
print("NUM_BEFORE: %d" % NUM_BEFORE)
print("NUM_AFTER: %d" % NUM_AFTER)
print("TOT_VULN: %d" % (NUM_VULN + NUM_BEFORE))
print("TOT_NOT_VULN: %d" % (NUM_PATCH + NUM_AFTER))
return NUM_VULN, NUM_PATCH, NUM_BEFORE, NUM_AFTER
def get_hits_multi(vgraph_db, target_db):
work=[]
scores = []
for i, tg in enumerate(target_db):
t_trips = tg['triples']
tg['hits'] = [] # place to put hits
for vg in vgraph_db:
work.append((i,vg, t_trips))
print("Work size: %d" % len(work))
print("Applying pool...")
p = Pool(NUM_PROCS)
res = p.map(consume, work)
print("done..")
for (target_id, vg, cvg_score, pvg_score, nvg_score) in res:
scores.append((vg['cve'],vg['repo'],vg['func'],target_db[target_id]['path'],cvg_score, pvg_score, nvg_score))
if decision_function(cvg_score, pvg_score, nvg_score):
target_db[target_id]['hits'].append(vg)
def get_hits(vgraph_db, target_db):
skipped=0
scores = []
for i, tg in tqdm(enumerate(target_db)):
tg['hits'] = [] # place to store our hits
t_trips = tg['triples']
t_vec = np.array(tg['vec'])
for vg in vgraph_db:
cvg_score, pvg_score, nvg_score = triplet_match_exact(vg, t_trips)
scores.append((vg['cve'],vg['repo'],vg['func'],tg['path'],cvg_score, pvg_score, nvg_score))
if decision_function(cvg_score, pvg_score, nvg_score):
# we have a hit
tg['hits'].append(vg)
return scores
def eval_vgraph(vgraph_db, target_db, gt, manual_labels):
# Loop through target graphs. Get any vGraph hits and evaluate for truthiness
# Now we score
print("Scoring results...")
# Score all:
TP=0.
FP=0.
TN=0.
FN=0.
UNK=0
for tg in target_db:
if len(tg['hits']) == 0: # nothing hit on this target
if '/patch/' in tg['path'] or '/after/' in tg['path']:
TN +=1
else: # something should have hit
FN += 1
if(PRINT_FN):
print("FN: %s" % (tg['path']))
else:
# something hit
#accounted_for = False
for vg in tg['hits']:
if vg['cve'] in tg['path'] and ('/vuln/' in tg['path'] or '/before/' in tg['path']):
TP += 1
#accounted_for = True
#break
elif vg['cve'] in tg['path'] and ('/patch/' in tg['path'] or '/after/' in tg['path']):
FP += 1
if PRINT_FP:
print("FP: %s %s" % (vg['cve'], tg['path']))
#accounted_for = True
#break
else:
#continue # until we find either TP or FP
# Need to check
tg_name = tg['path'].split('/')[-1][:-len('.gpickle')]
vg_name = vg['func']
tg_time = int(tg['path'].split('/')[-4].split('_')[1])
vg_time = int(vg['hsh'].split('_')[1])
if tg_name == vg_name and tg_time < vg_time:
TP += 1
else:
# check in manual labels
found=False
for label in manual_labels:
label_split = label.rstrip().split(' ')
if label_split[1] == vg['cve'] and label_split[2] in tg['path']:
if label_split[0] == 'TP':
TP += 1
else:
FP += 1
if PRINT_FP:
print("FP: %s %s" % (vg['cve'], tg['path']))
found=True
break
if not found:
UNK += 1
if(PRINT_UNK):
print("UNK: %s %s/%s/%s/%s/%s)" % (tg['path'], vg['repo'], vg['cve'], vg['hsh'],vg['file'], vg['func']))
#if not accounted_for:
# if '/patch/' in tg['path'] or '/after/' in tg['path']:
# TN +=1
# else: # something should have hit
# FN += 1
P = TP/(TP+FP)
R = TP/(TP+FN)
F1 = 2*(P*R)/(P+R)
print("All Score:")
print("TP\tFP\tTN\tFN\tUNK\tP\tR\tF1")
print("%d\t%d\t%d\t%d\t%d\t%02f\t%02f\t%02f"%(TP,FP,TN,FN,UNK,P,R,F1))
print("Worst Case:")
P = TP/(TP+FP + UNK)
R = TP/(TP+FN)
F1 = 2*(P*R)/(P+R)
print("%02f\t%02f\t%02f"%(P,R,F1))
print("Best Case:")
P = (TP+UNK)/(TP+FP+UNK)
R = (TP+UNK)/(TP+UNK+FN)
F1 = 2*(P*R)/(P+R)
print("%02f\t%02f\t%02f"%(P,R,F1))
# Score train data:
TP=0.
FP=0.
TN=0.
FN=0.
UNK=0
for tg in target_db:
if not ('/vuln/' in tg['path'] or '/patch/' in tg['path']):
continue # only vuln/patch
if len(tg['hits']) == 0: # nothing hit on this target
if '/patch/' in tg['path'] or '/after/' in tg['path']:
TN +=1
else: # something should have hit
if(PRINT_FN):
print("FN: %s" % (tg['path']))
FN += 1
else:
# something hit
accounted_for = False
for vg in tg['hits']:
if vg['cve'] in tg['path'] and ('/vuln/' in tg['path'] or '/before/' in tg['path']):
TP += 1
#accounted_for = True
#break
elif vg['cve'] in tg['path'] and ('/patch/' in tg['path'] or '/after/' in tg['path']):
FP += 1
if PRINT_FP:
print("FP: %s %s" % (vg['cve'], tg['path']))
#accounted_for = True
#break
else:
#continue
# Need to check
tg_name = tg['path'].split('/')[-1][:-len('.gpickle')]
vg_name = vg['func']
tg_time = int(tg['path'].split('/')[-4].split('_')[1])
vg_time = int(vg['hsh'].split('_')[1])
if tg_name == vg_name and tg_time < vg_time:
TP += 1
else:
# check in manual labels
found=False
for label in manual_labels:
label_split = label.rstrip().split(' ')
if label_split[1] == vg['cve'] and label_split[2] in tg['path']:
if label_split[0] == 'TP':
TP += 1
else:
FP += 1
if PRINT_FP:
print("FP: %s %s" % (vg['cve'], tg['path']))
found=True
break
if not found:
UNK += 1
if(PRINT_UNK):
print("UNK: %s %s/%s/%s/%s/%s)" % (tg['path'], vg['repo'], vg['cve'], vg['hsh'],vg['file'], vg['func']))
#if not accounted_for:
# if '/patch/' in tg['path'] or '/after/' in tg['path']:
# TN +=1
# else: # something should have hit
# FN += 1
P = TP/(TP+FP)
R = TP/(TP+FN)
F1 = 2*(P*R)/(P+R)
print("Train Score:")
print("TP\tFP\tTN\tFN\tUNK\tP\tR\tF1")
print("%d\t%d\t%d\t%d\t%d\t%02f\t%02f\t%02f"%(TP,FP,TN,FN,UNK,P,R,F1))
print("Worst Case:")
P = TP/(TP+FP + UNK)
R = TP/(TP+FN)
F1 = 2*(P*R)/(P+R)
print("%02f\t%02f\t%02f"%(P,R,F1))
print("Best Case:")
P = (TP+UNK)/(TP+FP+UNK)
R = (TP+UNK)/(TP+UNK+FN)
F1 = 2*(P*R)/(P+R)
print("%02f\t%02f\t%02f"%(P,R,F1))
# score test data:
TP=0.
FP=0.
TN=0.
FN=0.
UNK=0
for tg in target_db:
if not ('/before/' in tg['path'] or '/after/' in tg['path']):
continue # only before/after
if len(tg['hits']) == 0: # nothing hit on this target
if '/patch/' in tg['path'] or '/after/' in tg['path']:
TN +=1
else: # something should have hit
if(PRINT_FN):
print("FN: %s" % (tg['path']))
FN += 1
else:
# something hit
accounted_for = False
for vg in tg['hits']:
if vg['cve'] in tg['path'] and ('/vuln/' in tg['path'] or '/before/' in tg['path']):
TP += 1
#accounted_for = True
#break
elif vg['cve'] in tg['path'] and ('/patch/' in tg['path'] or '/after/' in tg['path']):
FP += 1
if PRINT_FP:
print("FP: %s %s" % (vg['cve'], tg['path']))
#accounted_for = True
#break
else:
#continue
# Need to check
tg_name = tg['path'].split('/')[-1][:-len('.gpickle')]
vg_name = vg['func']
tg_time = int(tg['path'].split('/')[-4].split('_')[1])
vg_time = int(vg['hsh'].split('_')[1])
if tg_name == vg_name and tg_time < vg_time:
TP += 1
else:
# check in manual labels
found=False
for label in manual_labels:
label_split = label.rstrip().split(' ')
if label_split[1] == vg['cve'] and label_split[2] in tg['path']:
if label_split[0] == 'TP':
TP += 1
else:
FP += 1
if PRINT_FP:
print("FP: %s %s" % (vg['cve'], tg['path']))
found=True
break
if not found:
UNK += 1
if(PRINT_UNK):
print("UNK: %s %s/%s/%s/%s/%s)" % (tg['path'], vg['repo'], vg['cve'], vg['hsh'],vg['file'], vg['func']))
#if not accounted_for:
# if '/patch/' in tg['path'] or '/after/' in tg['path']:
# TN +=1
# else: # something should have hit
# FN += 1
P = TP/(TP+FP)
R = TP/(TP+FN)
F1 = 2*(P*R)/(P+R)
print("Test Score:")
print("TP\tFP\tTN\tFN\tUNK\tP\tR\tF1")
print("%d\t%d\t%d\t%d\t%d\t%02f\t%02f\t%02f"%(TP,FP,TN,FN,UNK,P,R,F1))
print("Worst Case:")
P = TP/(TP+FP + UNK)
R = TP/(TP+FN)
F1 = 2*(P*R)/(P+R)
print("%02f\t%02f\t%02f"%(P,R,F1))
print("Best Case:")
P = (TP+UNK)/(TP+FP+UNK)
R = (TP+UNK)/(TP+UNK+FN)
F1 = 2*(P*R)/(P+R)
print("%02f\t%02f\t%02f"%(P,R,F1))
# score test modified only:
test_mod_tps = []
score_by_line_mods = []
TP=0.
FP=0.
TN=0.
FN=0.
UNK=0
for tg in target_db:
if not ('/before/' in tg['path'] or '/after/' in tg['path']):
continue # only before/after
d = '/'.join(tg['path'].split('/')[0:4]) # root/repo/CVE
func = tg['path'].split('/')[-1] # function.gpickle
if '/before/' in tg['path']: # should be vuln
is_same = False
for (root,dirs,files) in os.walk(d):
if func in files and '/vuln/' in root:
# This is orig vuln file. lets check for differences
before_src = tg['path'].replace('/graph/', '/code/')
before_src = before_src.replace('.gpickle','.c')
vuln_src = (root + '/' + func).replace('/graph/','/code/')
vuln_src = vuln_src.replace('.gpickle','.c')
if filecmp.cmp(before_src, vuln_src):
# found match
is_same = True
break
if is_same:
continue # skip this one
# Count line diffs of files
res = subprocess.check_output('diff %s %s | grep "^>" | wc -l' % (vuln_src, before_src), shell=True)
num_right_mods = int(res.decode('utf-8').rstrip())
res = subprocess.check_output('diff %s %s | grep "^<" | wc -l' % (vuln_src, before_src), shell=True)
num_left_mods = int(res.decode('utf-8').rstrip())
res = subprocess.check_output("wc -l %s | awk '{print $1}'" % (vuln_src), shell=True)
num_lines_orig = int(res.decode('utf-8').rstrip())
#os.system('diff %s %s | grep "^>" | wc -l > scratch' % (vuln_src, before_src))
else: # after patch so should be patched
is_same = False
for (root,dirs,files) in os.walk(d):
if func in files and '/patch/' in root:
# This is orig vuln file. lets check for differences
after_src = tg['path'].replace('/graph/', '/code/')
after_src = after_src.replace('.gpickle','.c')
patch_src = (root + '/' + func).replace('/graph/','/code/')
patch_src = patch_src.replace('.gpickle','.c')
if filecmp.cmp(after_src, patch_src):
is_same = True
break
if is_same:
continue # skip this
# Count line diffs of files
res = subprocess.check_output('diff %s %s | grep "^>" | wc -l' % (patch_src, after_src), shell=True)
num_right_mods = int(res.decode('utf-8').rstrip())
res = subprocess.check_output('diff %s %s | grep "^<" | wc -l' % (patch_src, after_src), shell=True)
num_left_mods = int(res.decode('utf-8').rstrip())
res = subprocess.check_output("wc -l %s | awk '{print $1}'" % (patch_src), shell=True)
num_lines_orig = int(res.decode('utf-8').rstrip())
#os.system('diff %s %s | grep "^>" | wc -l > scratch' % (patch_src, after_src))
# If we make it here, this is either a before/after target graph which has
# source code thats modified from the vuln/patch file used to generate vGraph
# count num lines
#with open('scratch', 'r') as fp:
# num_mods = fp.readlines()[0]
if num_right_mods == num_left_mods:
print("Type-2")
clone_type=2
elif num_right_mods+num_left_mods > int(0.5*num_lines_orig):
print("Type-4")
clone_type=4
else:
print("Type-3")
clone_type=3
num_mods = num_right_mods
#print("Num mods: %s" % num_mods)
# score it
if len(tg['hits']) == 0: # nothing hit on this target
if '/patch/' in tg['path'] or '/after/' in tg['path']:
TN +=1
score_by_line_mods.append([tg['path'],num_mods,clone_type,'TN'])
else: # something should have hit
FN += 1
score_by_line_mods.append([tg['path'],num_mods,clone_type,'FN'])
else:
# something hit
accounted_for = False
for vg in tg['hits']:
if vg['cve'] in tg['path'] and ('/vuln/' in tg['path'] or '/before/' in tg['path']):
TP += 1
score_by_line_mods.append([tg['path'],num_mods,clone_type,'TP'])
accounted_for = True
break
#test_mod_tps.append((tg['path'],vg['cve'],vg['file'],vg['func']))
elif vg['cve'] in tg['path'] and ('/patch/' in tg['path'] or '/after/' in tg['path']):
FP += 1
score_by_line_mods.append([tg['path'],num_mods,clone_type,'FP'])
accounted_for = True
break
else:
continue # Not considering cross-cve clones for per-line mode experiment
# Need to check
tg_name = tg['path'].split('/')[-1][:-len('.gpickle')]
vg_name = vg['func']
tg_time = int(tg['path'].split('/')[-4].split('_')[1])
vg_time = int(vg['hsh'].split('_')[1])
if tg_name == vg_name and tg_time < vg_time:
test_mod_tps.append((tg['path'],vg['cve'],vg['file'],vg['func']))
TP += 1
else:
# check in manual labels
found=False
for label in manual_labels:
label_split = label.rstrip().split(' ')
if label_split[1] == vg['cve'] and label_split[2] in tg['path']:
if label_split[0] == 'TP':
TP += 1
else:
FP += 1
found=True
break
if not found:
UNK += 1
if(PRINT_UNK):
print("UNK: %s %s/%s/%s/%s/%s)" % (tg['path'], vg['repo'], vg['cve'], vg['hsh'],vg['file'], vg['func']))
if not accounted_for:
if '/patch/' in tg['path'] or '/after/' in tg['path']:
TN +=1
score_by_line_mods.append([tg['path'],num_mods,clone_type,'TN'])
else: # something should have hit
FN += 1
score_by_line_mods.append([tg['path'],num_mods,clone_type,'FN'])
with open('vgraph_score_by_line_mods.txt','w') as fp:
for s in score_by_line_mods:
fp.write("%s %s %s %s\n" % (s[0], s[1], s[2], s[3]))
P = TP/(TP+FP)
R = TP/(TP+FN)
F1 = 2*(P*R)/(P+R)
print("Test Modified")
print("TP\tFP\tTN\tFN\tUNK\tP\tR\tF1")
print("%d\t%d\t%d\t%d\t%d\t%02f\t%02f\t%02f"%(TP,FP,TN,FN,UNK,P,R,F1))
print("Worst Case:")
P = TP/(TP+FP + UNK)
R = TP/(TP+FN)
F1 = 2*(P*R)/(P+R)
print("%02f\t%02f\t%02f"%(P,R,F1))
print("Best Case:")
P = (TP+UNK)/(TP+FP+UNK)
R = (TP+UNK)/(TP+UNK+FN)
F1 = 2*(P*R)/(P+R)
print("%02f\t%02f\t%02f"%(P,R,F1))
def eval_vgraph_mods_only(vgraph_db, target_db, gt, manual_labels):
# score test modified
test_mod_tps = []
score_by_line_mods = []
TP=0.
FP=0.
TN=0.
FN=0.
UNK=0
for tg in target_db:
if not ('/before/' in tg['path'] or '/after/' in tg['path']):
continue # only before/after
d = '/'.join(tg['path'].split('/')[0:4]) # root/repo/CVE
func = tg['path'].split('/')[-1] # function.gpickle
if '/before/' in tg['path']: # should be vuln
is_same = False
for (root,dirs,files) in os.walk(d):
if func in files and '/vuln/' in root:
# This is orig vuln file. lets check for differences
before_src = tg['path'].replace('/graph/', '/code/')
before_src = before_src.replace('.gpickle','.c')
vuln_src = (root + '/' + func).replace('/graph/','/code/')
vuln_src = vuln_src.replace('.gpickle','.c')
if filecmp.cmp(before_src, vuln_src):
# found match
is_same = True
break
if is_same:
continue # skip this one
print('diff %s %s | grep "^>" | wc -l > scratch' % (vuln_src, before_src))
#os.system('diff %s %s | grep "^>" | wc -l > scratch' % (vuln_src, before_src))
else: # after patch so should be patched
is_same = False
for (root,dirs,files) in os.walk(d):
if func in files and '/patch/' in root:
# This is orig vuln file. lets check for differences
after_src = tg['path'].replace('/graph/', '/code/')
after_src = after_src.replace('.gpickle','.c')
patch_src = (root + '/' + func).replace('/graph/','/code/')
patch_src = patch_src.replace('.gpickle','.c')
if filecmp.cmp(after_src, patch_src):
is_same = True
break
if is_same:
continue # skip this
#os.system('diff %s %s | grep "^>" | wc -l > scratch' % (patch_src, after_src))
# If we make it here, this is either a before/after target graph which has
# source code thats modified from the vuln/patch file used to generate vGraph
# count num lines
#with open('scratch', 'r') as fp:
# num_mods = fp.readlines()[0]
#print("Num mods: %s" % num_mods)
# score it
if len(tg['hits']) == 0: # nothing hit on this target
if '/patch/' in tg['path'] or '/after/' in tg['path']:
TN +=1
#score_by_line_mods.append([tg['path'],num_mods,'TN'])
else: # something should have hit
FN += 1
#score_by_line_mods.append([tg['path'],num_mods,'FN'])
else:
# something hit
accounted_for = False
for vg in tg['hits']:
if vg['cve'] in tg['path'] and ('/vuln/' in tg['path'] or '/before/' in tg['path']):
TP += 1
#score_by_line_mods.append([tg['path'],num_mods,'TP'])
accounted_for = True
break
#test_mod_tps.append((tg['path'],vg['cve'],vg['file'],vg['func']))
elif vg['cve'] in tg['path'] and ('/patch/' in tg['path'] or '/after/' in tg['path']):
FP += 1
#score_by_line_mods.append([tg['path'],num_mods,'FP'])
accounted_for = True
break
else:
continue
# Need to check
tg_name = tg['path'].split('/')[-1][:-len('.gpickle')]
vg_name = vg['func']
tg_time = int(tg['path'].split('/')[-4].split('_')[1])
vg_time = int(vg['hsh'].split('_')[1])
if tg_name == vg_name and tg_time < vg_time:
# test_mod_tps.append((tg['path'],vg['cve'],vg['file'],vg['func']))
TP += 1
else:
# check in manual labels
found=False
for label in manual_labels:
label_split = label.rstrip().split(' ')
if label_split[1] == vg['cve'] and label_split[2] in tg['path']:
if label_split[0] == 'TP':
TP += 1
else:
FP += 1
found=True
break
if not found:
UNK += 1
if(PRINT_UNK):
print("UNK: %s %s/%s/%s/%s/%s)" % (tg['path'], vg['repo'], vg['cve'], vg['hsh'],vg['file'], vg['func']))
if not accounted_for:
if '/patch/' in tg['path'] or '/after/' in tg['path']:
TN +=1
#score_by_line_mods.append([tg['path'],num_mods,'TN'])
else: # something should have hit
FN += 1
#score_by_line_mods.append([tg['path'],num_mods,'FN'])
#with open('vgraph_score_by_line_mods.txt','w') as fp:
# for s in score_by_line_mods:
# fp.write("%s %s %s\n" % (s[0], s[1], s[2]))
try:
P = TP/(TP+FP)
except:
P=0.
try:
R = TP/(TP+FN)
except:
R = 0.
try:
F1 = 2*(P*R)/(P+R)
except:
F1 = 0.
print("Test Modified")
print("TP\tFP\tTN\tFN\tUNK\tP\tR\tF1")
print("%d\t%d\t%d\t%d\t%d\t%02f\t%02f\t%02f"%(TP,FP,TN,FN,UNK,P,R,F1))
##############################################################################################
# main
#############################################################################################
PRINT_FN=True
PRINT_FP=True
PRINT_UNK=False
CVG_THRESH=25
PVG_THRESH=60
NUM_PROCS=20
print("Loading VGraph DB...")
vgraph_db = load_vgraph_db('data/vgraph_db')
func_list = []
for vg in vgraph_db:
if vg['func'] not in func_list:
func_list.append(vg['func'])
# load manual labels
with open('./manual_labels.txt', 'r') as fp:
manual_labels = fp.readlines()
print("Loading target graphs..")
target_db = load_target_db('data/vuln_patch_graph_db', func_list)
target_db_clean = []
for tg in target_db:
cve = tg['path'].split('/')[3]
func = tg['base_name']
for vg in vgraph_db:
if vg['cve'] == cve and vg['func'] == func:
# function has a vgraph, so we will compare
target_db_clean.append(tg)
break
print("Calculating ground truth...")
gt = generate_ground_truth(target_db_clean)
#start_time = time.time()
#for thresh_c in [ 0, 20, 40, 60, 80, 100 ]:
# for thresh_p in [ 0, 20, 40, 60, 80, 100]:
# print("thresh_c: %d" % thresh_c)
# print("thresh_p: %d" % thresh_p)
# PVG_THRESH = thresh_p
# CVG_THRESH = thresh_c
# scores = get_hits(vgraph_db, target_db_clean)
# eval_vgraph_mods_only(vgraph_db, target_db_clean, gt, manual_labels)
if os.path.exists('evaluate_vgraph_scores.pkl'):
print("Loading saved results...")
scores = pkl.load(open('evaluate_vgraph_scores.pkl', 'rb'))
target_db_clean = pkl.load(open('evaluate_vgraph_target_db.pkl', 'rb'))
else:
start_time = time.time()
scores = get_hits(vgraph_db, target_db_clean)
pkl.dump(scores, open('evaluate_vgraph_scores.pkl', 'wb'))
pkl.dump(target_db_clean, open('evaluate_vgraph_target_db.pkl', 'wb'))
end_time = time.time()
print("Time to generate results: %d" % (end_time - start_time))
#eval_vgraph_mods_only(vgraph_db, target_db_clean, gt, manual_labels)
eval_vgraph(vgraph_db, target_db_clean, gt, manual_labels)
#with open('eval_all_scores.txt','w') as fp:
# for score in scores:
# fp.write("%s/%s/%s %s %d %d %d\n" % (score[0], score[1], score[2], score[3], score[4], score[5], score[6]))