Skip to content

Commit 3d74e3b

Browse files
authored
Merge pull request diffblue#172 from diffblue/feature/slicer_integration_in_python
SEC-17: Extension of the Python driver script w.r.t. calling of the slicer.
2 parents a853b6a + 8dc2ccc commit 3d74e3b

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

security-scanner/analyser.py

+38-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import time
33
import json
4-
import shutil
4+
import utility
55

66

77
def __get_my_dir(): return os.path.dirname(os.path.realpath(__file__))
@@ -109,12 +109,44 @@ def run_program_slicing(
109109
timeout,
110110
verbosity
111111
):
112-
prof = {}
112+
prof = {"calling_slicer": []}
113113
prof_start_time = time.time()
114114

115-
# TODO: once the slicer in "goto-instrument" tool is available, then provide proper implementation here!
116-
# Until then we assume the slicing removes nothing from the instrumented program.
117-
shutil.copyfile(json_cfg_fname, os.path.join(os.path.dirname(json_cfg_fname),"sliced_goto_programs.json"))
115+
with open(json_cfg_fname) as data_file:
116+
cfgs = json.load(data_file)
117+
118+
result = []
119+
for cfg in cfgs:
120+
src_plain_fname = os.path.splitext(os.path.basename(cfg["goto_binary_file"]))[0]
121+
src_idx_name = int(src_plain_fname[src_plain_fname.rfind("_")+1:])
122+
dst_goto_program_fname = os.path.join(results_dir, "sliced_goto_program_" + str(src_idx_name) + ".gbf")
123+
124+
command = (
125+
get_goto_instrument_pathname() + " " +
126+
"--full-slice " +
127+
"--verbosity " + str(1) + " " +
128+
cfg["goto_binary_file"] + " " +
129+
"-o " + dst_goto_program_fname
130+
)
131+
prof_calling_goto_instrument_start_time = time.time()
132+
print("Invoking 'goto-instrument' ...")
133+
with utility.PushCwd(results_dir) as cwd:
134+
if verbosity >= 9:
135+
print("CWD: " + cwd.get())
136+
print("CMD: " + command)
137+
# TODO: Uncomment the next line when the slicer is functional!
138+
# os.system(command)
139+
prof["calling_slicer"].append({
140+
"gbf_idx": src_idx_name,
141+
"duration": time.time() - prof_calling_goto_instrument_start_time
142+
})
143+
144+
result.append(cfg.copy())
145+
# TODO: Uncomment the next line when the slicer is functional!
146+
# result[-1]["goto_binary_file"] = dst_goto_program_fname
147+
148+
with open(os.path.join(results_dir, "sliced_goto_programs.json"), "w") as results_json:
149+
results_json.write(json.dumps(result, sort_keys=False, indent=4))
118150

119151
prof["duration"] = time.time() - prof_start_time
120152
return prof
@@ -175,7 +207,7 @@ def run_search_for_error_traces(
175207
get_cbmc_pathname() + " " +
176208
cfg["goto_binary_file"] + " " +
177209
" --trace --lazy-methods --json-ui --unwind 10 " +
178-
"--verbosity " + str(1) + " "
210+
"--verbosity " + str(verbosity) + " "
179211
"> \"cbmc_results.json\""
180212
)
181213
prof["calling_cbmc"] = {}

security-scanner/utility.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
4+
class PushCwd:
5+
def __init__(self, dirname):
6+
self._cwd = os.path.realpath(dirname)
7+
self._old_cwd = None
8+
9+
def __enter__(self):
10+
self._old_cwd = os.getcwd()
11+
os.chdir(self._cwd)
12+
return self
13+
14+
def __exit__(self, _, __, ___):
15+
if self._old_cwd is not None:
16+
os.chdir(self._old_cwd)
17+
18+
def get(self):
19+
return self._cwd

0 commit comments

Comments
 (0)