forked from lin-tan/llm-vul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvul4j_projects.py
134 lines (121 loc) · 4.84 KB
/
vul4j_projects.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
import subprocess
import multiprocessing
import json
import os
import sys
from pathlib import Path
CUR_DIR = os.path.abspath(__file__)[: os.path.abspath(__file__).rindex('/') + 1]
sys.path.insert(1, CUR_DIR+'../') # utils file
from util import vjbench_bug_id_list,vul4j_bug_id_list,info_json,VUL4J_DIR
bug_range_list = vul4j_bug_id_list
def copy_original_file():
with open(info_json, "r") as f:
data = json.load(f)
print('data len', len(data))
print('bug range len', len(bug_range_list))
for i in bug_range_list:
print(i)
project_path =os.path.join(VUL4J_DIR ,"VUL4J-{}".format(i))
original_file_path = os.path.join(project_path ,"VUL4J","original_whole_file.txt")
if not os.path.exists(original_file_path):
print(i)
if "buggy_file" in data[i-1]:
code_file_path = os.path.join(project_path, data[i-1]["buggy_file"])
# copy code file to original_whole_file.txt
os.system("cp {} {}".format(code_file_path, original_file_path))
def checkout_all_projects():
if not os.path.exists(VUL4J_DIR ):
os.makedirs(VUL4J_DIR )
for i in bug_range_list:
print("checkout VUL4J-{}".format(i))
cmd = "vul4j checkout --id VUL4J-{} -d {}/VUL4J-{}".format(i,VUL4J_DIR ,i)
print(cmd)
cmd =cmd.split()
p3 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out3, err = p3.communicate()
print(out3)
print(err)
def compile_all_projects():
for i in bug_range_list:
cmd = "vul4j compile -d {}/VUL4J-{}".format(VUL4J_DIR,i,i)
cmd =cmd.split()
p3 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out3, err = p3.communicate()
print(out3)
print(err)
def compile_all_projects_parallel(i):
cmd = "vul4j compile -d {}/VUL4J-{}".format(VUL4J_DIR,i,i)
print(cmd)
cmd =cmd.split()
p3 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out3, err = p3.communicate()
print(out3)
# p = multiprocessing.Pool(multiprocessing.cpu_count()-8)
# p.map(compile_all_projects_parallel, bug_range_list)
# exit()
def count_the_compile_fail_project():
fail_compile_list = []
no_compile_result_list = []
count = 0
for i in bug_range_list:
result_file_path = "{}/VUL4J-{}/VUL4J/compile_result.txt".format(VUL4J_DIR,i)
if not os.path.exists(result_file_path):
print("VUL4J-{} compile result not exist".format(i))
no_compile_result_list.append(i)
continue
with open(result_file_path) as f:
r = f.read()
if "0" in r:
print("VUL4J-{} compile failed".format(i))
fail_compile_list.append(i)
count += 1
print("{} projects compile failed".format(count))
print("compile fail", fail_compile_list)
print(len(fail_compile_list))
print("no compie result ", no_compile_result_list)
print(len(no_compile_result_list))
# count_the_compile_fail_project()
# exit()
compile_fail_list = []
def test_all_projects_parallel(i):
if i not in compile_fail_list:
# for i in range(start,end+1):
cmd = "vul4j test -d {}/VUL4J-{}".format(VUL4J_DIR,i)
print(cmd)
cmd =cmd.split()
p3 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out3, err = p3.communicate()
print(out3)
test_available_list = []
for x in bug_range_list:
if x not in compile_fail_list:
test_available_list.append(x)
print(len(test_available_list))
# p = multiprocessing.Pool(multiprocessing.cpu_count()-8)
# p.map(test_all_projects_parallel, test_available_list)
def check_test_result():
test_fail = []
for i in test_available_list:
if i not in compile_fail_list:
test_result_json = "{}/VUL4J-{}/VUL4J/testing_results.json".format(VUL4J_DIR,i)
if not os.path.exists(test_result_json):
print("Error: VUL4J-{} test result does not exist".format(i))
else:
with open(test_result_json) as f:
r = json.load(f)
cve_id = r["cve_id"]
tests = r["tests"]
metric = tests["overall_metrics"]
if metric["number_running"] == 0:
print("Error: VUL4J-{} ZERO tests run.".format(i))
test_fail.append(i)
elif metric["number_failing"] == 0 and metric["number_error"] == 0:
print("VUL4J-{} No test fail".format(i))
# print("VUL4J-{} {} tests failed, {} tests error.".format(i,metric["number_failing"],metric["number_error"]))
test_fail.append(i)
print("{} projects test failed".format(len(test_fail)))
print("test fail", test_fail)
# main function
if __name__ == "__main__":
checkout_all_projects()
copy_original_file()