forked from kingnop/SySeVR_fix
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_label.py
168 lines (132 loc) · 5.37 KB
/
make_label.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
## coding:utf-8
import os
import pickle
f = open("dict_cwe2father.pkl", 'rb')
dict_cwe2father = pickle.load(f)
f.close()
#print dict_cwe2father['CWE-787']
f = open("label_vec_type.pkl", 'rb')
label_vec_type = pickle.load(f)
f.close()
f = open("dict_testcase2code.pkl",'rb')
dict_testcase2code = pickle.load(f)
f.close()
def get_label_veclist(list_cwe):
list_label = [0] * len(label_vec_type)
for cweid in list_cwe:
index = label_vec_type.index(cweid)
list_label[index] = 1
return list_label
def get_label_cwe(cweid, label_cwe):
if cweid in label_vec_type:
label_cwe.append(cweid)
return label_cwe
else:
if cweid == 'CWE-1000':
label_cwe = label_vec_type
else:
fathercweid = dict_cwe2father[cweid]
for _id in fathercweid:
label_cwe = get_label_cwe(_id, label_cwe)
return label_cwe
def make_label(path, dict_vuln2testcase, _type):
f = open(path, 'r')
context = f.read().split('------------------------------')[:-1]
f.close()
context[0] = '\n' + context[0]
list_all_label = []
list_all_vulline = []
for _slice in context:
vulline = []
index_line = _slice.split('\n')[1]
list_codes = _slice.split('\n')[2:-1]
case_name = index_line.split(' ')[1]
key_name = '/'.join(index_line.split(' ')[1].split('/')[-2:])
print index_line
if key_name in dict_vuln2testcase.keys():
list_codeline = [code.split(' ')[-1] for code in list_codes]
dict = dict_vuln2testcase[key_name]
_dict_cwe2line_target = {}
_dict_cwe2line = {}
for _dict in dict:
for key in _dict.keys():
if _dict[key] not in _dict_cwe2line_target.keys():
_dict_cwe2line_target[_dict[key]] = [key]
else:
_dict_cwe2line_target[_dict[key]].append(key)
for line in list_codeline:
line = line.strip()
if line in _dict.keys():
if not ' '.join((list_codes[list_codeline.index(line)].strip()).split(' ')[:-1]) == dict_testcase2code[key_name+"/"+line].strip():
continue
cweid = _dict[line]
vulline.append(list_codeline.index(line))
if cweid not in _dict_cwe2line.keys():
_dict_cwe2line[cweid] = [line]
else:
_dict_cwe2line[cweid].append(line)
if _type:
list_vuln_cwe = []
for key in _dict_cwe2line.keys():
if key == 'Any...':
continue
if len(_dict_cwe2line[key]) == len(_dict_cwe2line_target[key]):
label_cwe = []
label_cwe = get_label_cwe(key, label_cwe)
list_vuln_cwe += label_cwe
else:
list_vuln_cwe = []
for key in _dict_cwe2line.keys():
if key == 'Any...':
continue
label_cwe = []
label_cwe = get_label_cwe(key, label_cwe)
list_vuln_cwe += label_cwe
if list_vuln_cwe == []:
list_label = [0] * len(label_vec_type)
else:
list_vuln_cwe = list(set(list_vuln_cwe))
list_label = get_label_veclist(list_vuln_cwe)
else:
list_label = [0] * len(label_vec_type)
list_all_label.append(list_label)
list_all_vulline.append(vulline)
return list_all_label, list_all_vulline
def main():
f = open("dict_flawline2filepath.pkl", 'rb')
dict_vuln2testcase = pickle.load(f)
f.close()
_type = False
time = '4'
# lang = 'C/test_data/' + time
lang = ''
path = os.path.join(lang, 'api_slices.txt')
list_all_apilabel, list_all_vulline = make_label(path, dict_vuln2testcase, _type)
dec_path = os.path.join(lang, 'api_slices_label.pkl')
f = open(dec_path, 'wb')
pickle.dump(list_all_apilabel, f, True)
f.close()
dec_path = os.path.join(lang, 'api_slices_vulline.pkl')
f = open(dec_path, 'wb')
pickle.dump(list_all_vulline, f)
f.close()
path = os.path.join(lang, 'arraysuse_slices.txt')
list_all_arraylabel = make_label(path, dict_vuln2testcase, _type)
dec_path = os.path.join(lang, 'array_slice_label.pkl')
f = open(dec_path, 'wb')
pickle.dump(list_all_arraylabel, f, True)
f.close()
path = os.path.join(lang, 'pointersuse_slices.txt')
list_all_pointerlabel = make_label(path, dict_vuln2testcase, _type)
dec_path = os.path.join(lang, 'pointer_slice_label.pkl')
f = open(dec_path, 'wb')
pickle.dump(list_all_pointerlabel, f, True)
f.close()
path = os.path.join(lang, 'integeroverflow_slices.txt')
list_all_exprlabel = make_label(path, dict_vuln2testcase, _type)
dec_path = os.path.join(lang, 'expr_slice_label.pkl')
f = open(dec_path, 'wb')
pickle.dump(list_all_exprlabel, f, True)
f.close()
if __name__ == '__main__':
main()