Skip to content

Commit 2103e2a

Browse files
committed
Updates requested in PR diffblue#50: using with for to open files.
1 parent 02bed39 commit 2103e2a

File tree

4 files changed

+443
-445
lines changed

4 files changed

+443
-445
lines changed

security-scanner/analyser.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_analyser_binary_pathname(analyser_name):
2626
return None
2727

2828

29-
def run_secutiry_analyser(
29+
def run_security_analyser(
3030
root_jar,
3131
transition_rules_file_pathname,
3232
timeout,
@@ -46,15 +46,13 @@ def run_secutiry_analyser(
4646
program_json_fname = os.path.abspath(os.path.join(results_dir, "program.json"))
4747
print("Building the program JSON file (the list of all JARs application consists of) for the 'goto-analyser': "
4848
+ program_json_fname)
49-
program_json_file = open(program_json_fname,"w")
50-
program_jars_list = [ os.path.relpath(root_jar,results_dir) ]
51-
program_json_file.write(json.dumps(program_jars_list,sort_keys=True,indent=4))
52-
program_json_file.close()
49+
with open(program_json_fname,"w") as program_json_file:
50+
program_jars_list = [ os.path.relpath(root_jar,results_dir) ]
51+
program_json_file.write(json.dumps(program_jars_list,sort_keys=True,indent=4))
5352

5453
# Building the root config file for the security analyser
5554
root_config_json_fname = os.path.abspath(os.path.join(results_dir, "config.json"))
5655
print("Building the root config JSON file for 'goto-analyser': " + root_config_json_fname)
57-
root_config_json_file = open(root_config_json_fname,"w")
5856
root_config_json = {
5957
"program": "./program.json",
6058
"rules": os.path.relpath(transition_rules_file_pathname,results_dir),
@@ -82,8 +80,8 @@ def run_secutiry_analyser(
8280
root_config_json["dump_html_program"] = True
8381
else:
8482
root_config_json["dump_html_program"] = False
85-
root_config_json_file.write(json.dumps(root_config_json,sort_keys=True,indent=4))
86-
root_config_json_file.close()
83+
with open(root_config_json_fname,"w") as root_config_json_file:
84+
root_config_json_file.write(json.dumps(root_config_json,sort_keys=True,indent=4))
8785

8886
if not os.path.exists(results_dir):
8987
os.makedirs(results_dir)
@@ -124,6 +122,35 @@ def run_program_slicing(
124122
return prof
125123

126124

125+
def append_cbmc_error_trace_if_it_is_relevant(trace,trace_id,results_dir,cfg,traces_list):
126+
last_rec = trace[-1]
127+
if "sourceLocation" in last_rec:
128+
loc = last_rec["sourceLocation"]
129+
for sink in cfg["sinks"]:
130+
if loc["file"] == sink["file"] and \
131+
loc["function"] == sink["function"] and \
132+
loc["line"] == str(sink["line"]):
133+
trace_fname = os.path.join(results_dir, "error_trace_" + str(trace_id) + ".json")
134+
with open(trace_fname, "w") as trace_file:
135+
trace_file.write(json.dumps(trace, sort_keys=False, indent=4))
136+
trace_id += 1
137+
138+
found = False
139+
for rec in traces_list:
140+
if rec["file"] == sink["file"] and \
141+
rec["function"] == sink["function"] and \
142+
rec["line"] == sink["line"] and \
143+
rec["goto_binary_file"] == cfg["goto_binary_file"]:
144+
rec["error_traces"].append(trace_fname)
145+
if found == False:
146+
rec = sink
147+
rec["goto_binary_file"] = cfg["goto_binary_file"]
148+
rec["error_traces"] = [trace_fname]
149+
traces_list.append(rec)
150+
break
151+
return trace_id
152+
153+
127154
def run_search_for_error_traces(
128155
json_cfg_fname,
129156
results_dir,
@@ -165,31 +192,13 @@ def run_search_for_error_traces(
165192
for inner_element in outer_element["result"]:
166193
if "trace" in inner_element and len(inner_element["trace"]) > 0:
167194
trace=inner_element["trace"]
168-
last_rec=trace[len(trace)-1]
169-
if "sourceLocation" in last_rec:
170-
loc=last_rec["sourceLocation"]
171-
for sink in cfg["sinks"]:
172-
if loc["file"] == sink["file"] and \
173-
loc["function"] == sink["function"] and \
174-
loc["line"] == str(sink["line"]):
175-
trace_fname = os.path.join(results_dir, "error_trace_" + str(trace_id) + ".json")
176-
with open(trace_fname,"w") as trace_file:
177-
trace_file.write(json.dumps(trace,sort_keys=False,indent=4))
178-
trace_id += 1
179-
180-
found = False
181-
for rec in traces_list:
182-
if rec["file"] == sink["file"] and \
183-
rec["function"] == sink["function"] and \
184-
rec["line"] == sink["line"] and \
185-
rec["goto_binary_file"] == cfg["goto_binary_file"]:
186-
rec["error_traces"].append(trace_fname)
187-
if found == False:
188-
rec=sink
189-
rec["goto_binary_file"]=cfg["goto_binary_file"]
190-
rec["error_traces"]=[trace_fname]
191-
traces_list.append(rec)
192-
break
195+
trace_id = append_cbmc_error_trace_if_it_is_relevant(
196+
trace,
197+
trace_id,
198+
results_dir,
199+
cfg,
200+
traces_list
201+
)
193202

194203
with open(os.path.join(results_dir, "error_traces.json"), "w") as traces_file:
195204
traces_file.write(json.dumps(traces_list, sort_keys=True, indent=4))

security-scanner/mkplot.py

Lines changed: 95 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -5,109 +5,103 @@
55

66

77
def load_input_JSON(filename):
8-
file = open(filename,"r")
9-
stats = json.load(file)
10-
file.close()
8+
with open(filename,"r") as file:
9+
stats = json.load(file)
1110
return stats
1211

1312

1413
def build_data_file(stats,output_file_name):
15-
file = open(output_file_name,"w")
16-
num_functions = 0
17-
num_locations = 0
18-
time = 0.0
19-
num_functions_per_second = 0
20-
num_locations_per_second = 0
21-
for file_stats in stats["table-files"]:
22-
for fn_stats in file_stats["functions"]:
23-
delta_time = fn_stats["LVSA-duration"] + fn_stats["TA-duration"]
24-
#if delta_time > 1.0:
25-
# print("TIME[" + str(delta_time) + "]: " + fn_stats["function-name"])
26-
# continue
27-
time = time + delta_time
28-
num_functions = num_functions + 1
29-
num_locations = num_locations + fn_stats["num-locations"]
30-
if time > 0.0001:
31-
num_functions_per_second = num_functions / time
32-
num_locations_per_second = num_locations / time
33-
file.write(str(time) + " " + \
34-
str(num_functions) + " " + \
35-
str(num_locations) + " " + \
36-
str(num_functions_per_second) + " " + \
37-
str(num_locations_per_second) + "\n")
38-
file.close()
39-
14+
with open(output_file_name,"w") as file:
15+
num_functions = 0
16+
num_locations = 0
17+
time = 0.0
18+
num_functions_per_second = 0
19+
num_locations_per_second = 0
20+
for file_stats in stats["table-files"]:
21+
for fn_stats in file_stats["functions"]:
22+
delta_time = fn_stats["LVSA-duration"] + fn_stats["TA-duration"]
23+
#if delta_time > 1.0:
24+
# print("TIME[" + str(delta_time) + "]: " + fn_stats["function-name"])
25+
# continue
26+
time = time + delta_time
27+
num_functions = num_functions + 1
28+
num_locations = num_locations + fn_stats["num-locations"]
29+
if time > 0.0001:
30+
num_functions_per_second = num_functions / time
31+
num_locations_per_second = num_locations / time
32+
file.write(str(time) + " " + \
33+
str(num_functions) + " " + \
34+
str(num_locations) + " " + \
35+
str(num_functions_per_second) + " " + \
36+
str(num_locations_per_second) + "\n")
37+
4038

4139
def build_functions_speed_plot_file(plot_file_name,data_file_name):
42-
file = open(plot_file_name,"w")
43-
file.write(
44-
"set title \"Number of analysed functions per second.\"\n"
45-
"set terminal svg font 'Liberation,10' size 550,480\n"
46-
"set output './" + os.path.splitext(os.path.basename(plot_file_name))[0] + ".svg'\n"
47-
"set xtic rotate\n"
48-
"set ytic auto\n"
49-
"set xlabel \"Time (sec)\"\n"
50-
"set ylabel \"Speed (funcs/sec)\"\n"
51-
"set grid\n"
52-
"set style data lines\n"
53-
"unset key\n"
54-
"plot \"./" + os.path.basename(data_file_name) + "\" using 1:4 lt -1 lw 2\n"
55-
)
56-
file.close()
40+
with open(plot_file_name,"w") as file:
41+
file.write(
42+
"set title \"Number of analysed functions per second.\"\n"
43+
"set terminal svg font 'Liberation,10' size 550,480\n"
44+
"set output './" + os.path.splitext(os.path.basename(plot_file_name))[0] + ".svg'\n"
45+
"set xtic rotate\n"
46+
"set ytic auto\n"
47+
"set xlabel \"Time (sec)\"\n"
48+
"set ylabel \"Speed (funcs/sec)\"\n"
49+
"set grid\n"
50+
"set style data lines\n"
51+
"unset key\n"
52+
"plot \"./" + os.path.basename(data_file_name) + "\" using 1:4 lt -1 lw 2\n"
53+
)
5754

5855

5956
def build_locations_speed_plot_file(plot_file_name,data_file_name):
60-
file = open(plot_file_name,"w")
61-
file.write(
62-
"set title \"Number of analysed lines per second.\"\n"
63-
"set terminal svg font 'Liberation,10' size 550,480\n"
64-
"set output './" + os.path.splitext(os.path.basename(plot_file_name))[0] + ".svg'\n"
65-
"set xtic rotate\n"
66-
"set ytic auto\n"
67-
"set xlabel \"Time (sec)\"\n"
68-
"set ylabel \"Speed (lines/sec)\"\n"
69-
"set style data lines\n"
70-
"set grid\n"
71-
"unset key\n"
72-
"plot \"./" + os.path.basename(data_file_name) + "\" using 1:5 lt -1 lw 2\n"
73-
)
74-
file.close()
57+
with open(plot_file_name,"w") as file:
58+
file.write(
59+
"set title \"Number of analysed lines per second.\"\n"
60+
"set terminal svg font 'Liberation,10' size 550,480\n"
61+
"set output './" + os.path.splitext(os.path.basename(plot_file_name))[0] + ".svg'\n"
62+
"set xtic rotate\n"
63+
"set ytic auto\n"
64+
"set xlabel \"Time (sec)\"\n"
65+
"set ylabel \"Speed (lines/sec)\"\n"
66+
"set style data lines\n"
67+
"set grid\n"
68+
"unset key\n"
69+
"plot \"./" + os.path.basename(data_file_name) + "\" using 1:5 lt -1 lw 2\n"
70+
)
7571

7672

7773
def build_functions_progress_plot_file(plot_file_name,data_file_name):
78-
file = open(plot_file_name,"w")
79-
file.write(
80-
"set title \"Number of analysed functions in time.\"\n"
81-
"set terminal svg font 'Liberation,10' size 550,480\n"
82-
"set output './" + os.path.splitext(os.path.basename(plot_file_name))[0] + ".svg'\n"
83-
"set xtic rotate\n"
84-
"set ytic auto\n"
85-
"set xlabel \"Time (sec)\"\n"
86-
"set ylabel \"Functions (#)\"\n"
87-
"set style data lines\n"
88-
"set grid\n"
89-
"unset key\n"
90-
"plot \"./" + os.path.basename(data_file_name) + "\" using 1:2 lt -1 lw 2\n"
91-
)
92-
file.close()
93-
74+
with open(plot_file_name,"w") as file:
75+
file.write(
76+
"set title \"Number of analysed functions in time.\"\n"
77+
"set terminal svg font 'Liberation,10' size 550,480\n"
78+
"set output './" + os.path.splitext(os.path.basename(plot_file_name))[0] + ".svg'\n"
79+
"set xtic rotate\n"
80+
"set ytic auto\n"
81+
"set xlabel \"Time (sec)\"\n"
82+
"set ylabel \"Functions (#)\"\n"
83+
"set style data lines\n"
84+
"set grid\n"
85+
"unset key\n"
86+
"plot \"./" + os.path.basename(data_file_name) + "\" using 1:2 lt -1 lw 2\n"
87+
)
88+
9489

9590
def build_locations_progress_plot_file(plot_file_name,data_file_name):
96-
file = open(plot_file_name,"w")
97-
file.write(
98-
"set title \"Number of analysed lines in time.\"\n"
99-
"set terminal svg font 'Liberation,10' size 550,480\n"
100-
"set output './" + os.path.splitext(os.path.basename(plot_file_name))[0] + ".svg'\n"
101-
"set xtic rotate\n"
102-
"set ytic auto\n"
103-
"set xlabel \"Time (sec)\"\n"
104-
"set ylabel \"Lines (#)\"\n"
105-
"set style data lines\n"
106-
"set grid\n"
107-
"unset key\n"
108-
"plot \"./" + os.path.basename(data_file_name) + "\" using 1:3 lt -1 lw 2\n"
109-
)
110-
file.close()
91+
with open(plot_file_name,"w") as file:
92+
file.write(
93+
"set title \"Number of analysed lines in time.\"\n"
94+
"set terminal svg font 'Liberation,10' size 550,480\n"
95+
"set output './" + os.path.splitext(os.path.basename(plot_file_name))[0] + ".svg'\n"
96+
"set xtic rotate\n"
97+
"set ytic auto\n"
98+
"set xlabel \"Time (sec)\"\n"
99+
"set ylabel \"Lines (#)\"\n"
100+
"set style data lines\n"
101+
"set grid\n"
102+
"unset key\n"
103+
"plot \"./" + os.path.basename(data_file_name) + "\" using 1:3 lt -1 lw 2\n"
104+
)
111105

112106

113107
def build_general_stats_text_file(stats,output_file_path_name):
@@ -122,19 +116,18 @@ def build_general_stats_text_file(stats,output_file_path_name):
122116
num_files = len(stats["table-files"])
123117
program_building_duration = stats["table-phases"]["goto-program-building"]
124118

125-
file = open(output_file_path_name,"w")
126-
file.write("number of files = " + str(num_files) + "\n")
127-
file.write("number of functions = " + str(num_functions) + "\n")
128-
file.write("number of locations = " + str(num_locations) + "\n")
129-
file.write("program parsing duration = " + str(program_building_duration) + "\n")
130-
if num_files > 0: file.write("average parsing duration per file = " + str(program_building_duration / num_files) + "\n")
131-
if num_functions > 0: file.write("average parsing duration per function = " + str(program_building_duration / num_functions) + "\n")
132-
if num_locations > 0: file.write("average parsing duration per location = " + str(program_building_duration / num_locations) + "\n")
133-
file.write("analysis duration = " + str(analysis_duration) + "\n")
134-
if num_files > 0: file.write("average analysis duration per file = " + str(analysis_duration / num_files) + "\n")
135-
if num_functions > 0: file.write("average analysis duration per function = " + str(analysis_duration / num_functions) + "\n")
136-
if num_locations > 0: file.write("average analysis duration per location = " + str(analysis_duration / num_locations) + "\n")
137-
file.close()
119+
with open(output_file_path_name,"w") as file:
120+
file.write("number of files = " + str(num_files) + "\n")
121+
file.write("number of functions = " + str(num_functions) + "\n")
122+
file.write("number of locations = " + str(num_locations) + "\n")
123+
file.write("program parsing duration = " + str(program_building_duration) + "\n")
124+
if num_files > 0: file.write("average parsing duration per file = " + str(program_building_duration / num_files) + "\n")
125+
if num_functions > 0: file.write("average parsing duration per function = " + str(program_building_duration / num_functions) + "\n")
126+
if num_locations > 0: file.write("average parsing duration per location = " + str(program_building_duration / num_locations) + "\n")
127+
file.write("analysis duration = " + str(analysis_duration) + "\n")
128+
if num_files > 0: file.write("average analysis duration per file = " + str(analysis_duration / num_files) + "\n")
129+
if num_functions > 0: file.write("average analysis duration per function = " + str(analysis_duration / num_functions) + "\n")
130+
if num_locations > 0: file.write("average analysis duration per location = " + str(analysis_duration / num_locations) + "\n")
138131

139132

140133
def make_plots(input_json,output_dir,do_build_svg):

0 commit comments

Comments
 (0)