-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgen_vgraph.py
88 lines (64 loc) · 2.71 KB
/
gen_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
import os
import sys
import networkx as nx
import pickle as pkl
from src.graph.utils import tripleize, vectorize
def gen_triplets(V,P):
'''
Gen a set of triplets from V,P in form of CODE,RELATIONSHIP,CODE or TYPE,RELA,TYPE if no code
CVG= { set shared by both }
PVG = { set contained by V but not P }
NVG = { set contained by P but not V }
'''
V_trips = tripleize(V)
P_trips = tripleize(P)
print("Num V triplets: %d" % len(V_trips))
print("Num P triplets: %d" % len(P_trips))
cvg=V_trips.intersection(P_trips)
pvg=V_trips.difference(P_trips)
nvg=P_trips.difference(V_trips)
return cvg, pvg, nvg, V_trips, P_trips
def print_statistics(file_path, v_size, p_size, cvg_size, pvg_size, nvg_size):
print("%s\t%d\t%d\t%d\t%d\t%d" % (file_path, v_size, p_size,cvg_size, pvg_size, nvg_size))
if __name__ == "__main__":
if len(sys.argv) != 5:
print("Usage: python gen_vgraph.py <vuln_graph> <patch_graph> <output_path> <output_name>")
exit()
# Read inputs
vuln_graph = sys.argv[1]
patch_graph = sys.argv[2]
output_path = sys.argv[3]
output_name= sys.argv[4]
# vgraph ID
vuln_function = output_path + '/' + output_name
# Graph Outputs
pvg_output_file = output_path + '/' + output_name + "_pvg.pkl"
nvg_output_file = output_path + '/' + output_name + "_nvg.pkl"
cvg_output_file = output_path + '/' + output_name + "_cvg.pkl"
v_output_file = output_path + '/' + output_name + "_v.pkl"
p_output_file = output_path + '/' + output_name + "_p.pkl"
# Vector Output
vec_output_file = output_path + '/' + output_name + "_vec.pkl"
# Read in the vulnerable and patched graphs
V = nx.read_gpickle(vuln_graph)
P = nx.read_gpickle(patch_graph)
print("V size: %d" % len(V.nodes))
print("P size: %d" % len(P.nodes))
cvg, pvg, nvg, V_trips, P_trips = gen_triplets(V,P)
vec = vectorize(V)
# Check here to make sure we generated some meanigful information
if len(cvg) == 0 or len(pvg) == 0 or len(nvg) == 0:
print("Error: vGraph critical component empty. Skipping")
print_statistics(vuln_function, len(V.nodes), len(P.nodes), len(cvg), len(pvg), len(nvg))
exit()
# if we get here were good
if not os.path.exists(output_path):
os.makedirs(output_path)
pkl.dump(cvg, open(cvg_output_file, 'wb'))
pkl.dump(pvg, open(pvg_output_file, 'wb'))
pkl.dump(nvg, open(nvg_output_file, 'wb'))
pkl.dump(V_trips, open(v_output_file, 'wb'))
pkl.dump(P_trips, open(p_output_file, 'wb'))
pkl.dump(vec, open(vec_output_file, 'wb'))
# Print final statistics
print_statistics(vuln_function, len(V.nodes), len(P.nodes), len(cvg), len(pvg), len(nvg))