-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-suite.py
375 lines (337 loc) · 9.58 KB
/
run-suite.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import argparse
import os
from concurrent.futures import ThreadPoolExecutor
import threading
import subprocess
import sys
import datetime
from enum import Enum
RED = "\033[31m" # Red text
GREEN = "\033[32m" # Green text
YELLOW = "\033[33m" # Yellow text
RESET = "\033[0m" # Reset to default terminal color
# coreutils tested in the initial KLEE paper
all_coreutils = [
"[",
"base64",
"basename",
"cat",
"chcon",
"chgrp",
"chmod",
"chown",
"chroot",
"cksum",
"comm",
"cp",
"csplit",
"cut",
"date",
"dd",
"df",
"dircolors",
"dirname",
"du",
"echo",
"env",
"expand",
"expr",
"factor",
"false",
"fmt",
"fold",
"head",
"hostid",
"hostname",
"id",
"ginstall",
"join",
"kill",
"link",
"ln",
"logname",
"ls",
"md5sum",
"mkdir",
"mkfifo",
"mknod",
"mktemp",
"mv",
"nice",
"nl",
"nohup",
"od",
"paste",
"pathchk",
"pinky",
"pr",
"printenv",
"printf",
"ptx",
"pwd",
"readlink",
"rm",
"rmdir",
"runcon",
"seq",
"setuidgid",
"shred",
"shuf",
"sleep",
"sort",
"split",
"stat",
"stty",
"sum",
"sync",
"tac",
"tail",
"tee",
"touch",
"tr",
"tsort",
"tty",
"uname",
"unexpand",
"uniq",
"unlink",
"uptime",
"users",
"wc",
"whoami",
"who",
"yes",
]
class State(Enum):
WATING = 0
STARTED = 1
FINISHED_ANALYSIS = 2
FINISHED_COVERAGE = 3
ERROR = 4
print_lock = threading.Lock()
counter_lock = threading.Lock()
states: dict[str, State] = {e: State.WATING for e in all_coreutils}
def update_counter(util: str, state: State) -> str:
with counter_lock:
global states
global all_coreutils
states[util] = state
state_counts = [
list(states.values()).count(e) for e in State._member_map_.values()
]
state_counts = [f"{e:>{len(str(len(all_coreutils)))}}" for e in state_counts]
state_counts[-1] = f"{RED}{state_counts[-1]}{RESET}"
state_counts = ", ".join(state_counts)
state_counts = f"({state_counts})"
return state_counts
def thread_safe_print(*args, **kwargs):
with print_lock:
print(datetime.datetime.now().isoformat(), *args, **kwargs)
def print_with_counter(util: str, newState: State, message: str):
if newState == State.ERROR:
thread_safe_print(
f"{update_counter(util, newState)} — {RED}{message}{RESET}",
file=sys.stderr,
)
else:
thread_safe_print(f"{update_counter(util, newState)} — {message}")
def init_parser():
parser = argparse.ArgumentParser(description="Run KLEE on coreutils.")
parser.add_argument(
"output_dir",
help="Output directory",
)
parser.add_argument(
"--max-threads",
type=int,
help="Maximum number of threads",
default=1,
)
parser.add_argument(
"--image-name",
help='Name of the image built, defaults to "klee-coreutils(-[name of the passed dockerfile])',
)
parser.add_argument(
"--dockerfile",
help="Path of the dockerfile to use",
default="Dockerfile",
)
parser.add_argument(
"--util",
action="append",
help="Utils to test",
)
parser.add_argument(
"--env",
"-e",
action="append",
nargs=2,
metavar=("key", "value"),
help="Environment variables",
)
parser.add_argument(
"--force",
"-f",
action="store_true",
help="Force execution, even if target directory already exists",
)
return parser
# Function to run klee-coreutils for a given util
def run_klee_coreutils(image_name, util, env_vars):
# Create a specific environment for the current util
env = {"UTIL": util}
env.update(env_vars)
# Create a subfolder for the current util within the output directory
util_output_dir = os.path.join(output_dir, util)
os.makedirs(util_output_dir, exist_ok=True)
# Obtain the absolute path for the output directory
abs_util_output_dir = os.path.abspath(util_output_dir)
# Define output filenames for stdout and stderr
stdout_file = os.path.join(util_output_dir, "orchestration-stdout.txt")
stderr_file = os.path.join(util_output_dir, "orchestration-stderr.txt")
# Command to run klee-coreutils docker image and direct output to the util-specific folder
exec_command = ["docker", "run", "--rm", "-it"]
# Append environment variables to the command
for key, value in env.items():
exec_command += ["-e", f"{key}={value}"]
# Append image name and output directory volume mapping to the command
exec_command += ["-v", f"{abs_util_output_dir}:/home/klee/out", image_name]
# Command to gather coverage information
cov_command = [
"docker",
"run",
"--rm",
"-it",
"-e",
f"UTIL={util}",
"-v",
f"{abs_util_output_dir}:/out",
f"{image_name}-cov",
]
# Execute the command using subprocess.run
with open(stdout_file, "w") as stdout_f, open(stderr_file, "w") as stderr_f:
print_with_counter(util, State.STARTED, f'Starting run for util "{util}".')
process = subprocess.run(
exec_command, stdout=stdout_f, stderr=stderr_f, text=True
)
if process.returncode != 0:
print_with_counter(
util,
State.ERROR,
f"=== ERROR: Failed to execute {util} with exit code {process.returncode}",
)
return
print_with_counter(
util, State.FINISHED_ANALYSIS, f'Starting coverage gathering for "{util}".'
)
process = subprocess.run(
cov_command, stdout=stdout_f, stderr=stderr_f, text=True
)
if process.returncode != 0:
print_with_counter(
util,
State.ERROR,
f"=== ERROR: Failed to gather coverage for {util} with exit code {process.returncode}",
)
return
print_with_counter(util, State.FINISHED_COVERAGE, f'Done with "{util}".')
if __name__ == "__main__":
# Parse command-line arguments
args = init_parser().parse_args()
thread_safe_print(f"Analyzing with the following args: {args}")
output_dir = args.output_dir
image_name = args.image_name
dockerfile: str = args.dockerfile
if image_name is None:
image_name = "klee-coreutils"
if len(dockerfile) > len("Dockerfile"):
image_name = f"{image_name}-{dockerfile.replace('.Dockerfile', '')}"
# Check if the output directory exists and is empty
if (not args.force) and (
os.path.exists(output_dir)
and os.path.isdir(output_dir)
and os.listdir(output_dir)
):
response = (
input(
f"{YELLOW}Output directory is not empty, this might not work. Continue anyway? (y/n){RESET} "
)
.lower()
.strip()
)
if response not in ["y", "yes"]:
thread_safe_print("Exiting")
exit(1)
# Make sure the docker image is up to date
thread_safe_print(
f"Building docker image from dockerfile {dockerfile} to image {image_name}[-cov]"
)
process = subprocess.run(
[
"docker",
"build",
"--progress=plain",
"--target",
"klee-coreutils-exec",
"-t",
f"{image_name}",
"-f",
dockerfile,
".",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if process.returncode != 0:
thread_safe_print(
f"{RED}=== ERROR: Failed to build docker image exec with exit code {process.returncode}{RESET}",
file=sys.stderr,
)
exit(1)
process = subprocess.run(
[
"docker",
"build",
"--progress=plain",
"--target",
"klee-coreutils-gcov",
"-t",
f"{image_name}-cov",
"-f",
dockerfile,
".",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if process.returncode != 0:
thread_safe_print(
f"=== ERROR: Failed to build docker image cov with exit code {process.returncode}",
file=sys.stderr,
)
exit(1)
coreutils = all_coreutils
if args.util is not None:
coreutils = [e for e in args.util if e in all_coreutils]
states = {e: State.WATING for e in coreutils}
thread_safe_print(
f"{YELLOW}Only running the following coreutils: {coreutils}{RESET}"
)
# Prepare environment variables
env_vars = {}
if args.env:
for key, value in args.env:
env_vars[key] = value
futures = []
# Run klee-coreutils for each coreutil with specific environment variables using ThreadPoolExecutor
max_threads = args.max_threads
with ThreadPoolExecutor(max_workers=max_threads) as executor:
for util in coreutils:
futures.append(
executor.submit(run_klee_coreutils, image_name, util, env_vars)
)
for future in futures:
future.result() # Wait for each task to complete
num_succ = list(states.values()).count(State.FINISHED_COVERAGE)
num_error = list(states.values()).count(State.ERROR)
thread_safe_print(f"All tasks completed. Successes/Errors: {num_succ}/{num_error}.")