| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import argparse |
| 8 |
import asyncio |
| 9 |
import json |
| 10 |
import logging |
| 11 |
import math |
| 12 |
import os |
| 13 |
import pathlib |
| 14 |
import re |
| 15 |
import subprocess |
| 16 |
import sys |
| 17 |
import tempfile |
| 18 |
|
| 19 |
from lib.summarize import print_proof_results |
| 20 |
|
| 21 |
|
| 22 |
DESCRIPTION = "Configure and run all CBMC proofs in parallel" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
EPILOG = """ |
| 27 |
This tool automates the process of running `make report` in each of |
| 28 |
the CBMC proof directories. The tool calculates the dependency graph |
| 29 |
of all tasks needed to build, run, and report on all the proofs, and |
| 30 |
executes these tasks in parallel. |
| 31 |
|
| 32 |
The tool is roughly equivalent to doing this: |
| 33 |
|
| 34 |
litani init --project "my-cool-project"; |
| 35 |
|
| 36 |
find . -name cbmc-proof.txt | while read -r proof; do |
| 37 |
pushd $(dirname ${proof}); |
| 38 |
|
| 39 |
# The `make _report` rule adds a single proof to litani |
| 40 |
# without running it |
| 41 |
make _report; |
| 42 |
|
| 43 |
popd; |
| 44 |
done |
| 45 |
|
| 46 |
litani run-build; |
| 47 |
|
| 48 |
except that it is much faster and provides some convenience options. |
| 49 |
The CBMC CI runs this script with no arguments to build and run all |
| 50 |
proofs in parallel. The value of "my-cool-project" is taken from the |
| 51 |
PROJECT_NAME variable in Makefile-project-defines. |
| 52 |
|
| 53 |
The --no-standalone argument omits the `litani init` and `litani |
| 54 |
run-build`; use it when you want to add additional proof jobs, not |
| 55 |
just the CBMC ones. In that case, you would run `litani init` |
| 56 |
yourself; then run `run-cbmc-proofs --no-standalone`; add any |
| 57 |
additional jobs that you want to execute with `litani add-job`; and |
| 58 |
finally run `litani run-build`. |
| 59 |
|
| 60 |
The litani dashboard will be written under the `output` directory; the |
| 61 |
cbmc-viewer reports remain in the `$PROOF_DIR/report` directory. The |
| 62 |
HTML dashboard from the latest Litani run will always be symlinked to |
| 63 |
`output/latest/html/index.html`, so you can keep that page open in |
| 64 |
your browser and reload the page whenever you re-run this script. |
| 65 |
""" |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
def get_project_name(): |
| 70 |
cmd = [ |
| 71 |
"make", |
| 72 |
"--no-print-directory", |
| 73 |
"-f", "Makefile.common", |
| 74 |
"echo-project-name", |
| 75 |
] |
| 76 |
logging.debug(" ".join(cmd)) |
| 77 |
proc = subprocess.run(cmd, universal_newlines=True, stdout=subprocess.PIPE, check=False) |
| 78 |
if proc.returncode: |
| 79 |
logging.critical("could not run make to determine project name") |
| 80 |
sys.exit(1) |
| 81 |
if not proc.stdout.strip(): |
| 82 |
logging.warning( |
| 83 |
"project name has not been set; using generic name instead. " |
| 84 |
"Set the PROJECT_NAME value in Makefile-project-defines to " |
| 85 |
"remove this warning") |
| 86 |
return "<PROJECT NAME HERE>" |
| 87 |
return proc.stdout.strip() |
| 88 |
|
| 89 |
|
| 90 |
def get_args(): |
| 91 |
pars = argparse.ArgumentParser( |
| 92 |
description=DESCRIPTION, epilog=EPILOG, |
| 93 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
| 94 |
for arg in [{ |
| 95 |
"flags": ["-j", "--parallel-jobs"], |
| 96 |
"type": int, |
| 97 |
"metavar": "N", |
| 98 |
"help": "run at most N proof jobs in parallel", |
| 99 |
}, { |
| 100 |
"flags": ["--fail-on-proof-failure"], |
| 101 |
"action": "store_true", |
| 102 |
"help": "exit with return code `10' if any proof failed" |
| 103 |
" (default: exit 0)", |
| 104 |
}, { |
| 105 |
"flags": ["--no-standalone"], |
| 106 |
"action": "store_true", |
| 107 |
"help": "only configure proofs: do not initialize nor run", |
| 108 |
}, { |
| 109 |
"flags": ["-p", "--proofs"], |
| 110 |
"nargs": "+", |
| 111 |
"metavar": "DIR", |
| 112 |
"help": "only run proof in directory DIR (can pass more than one)", |
| 113 |
}, { |
| 114 |
"flags": ["--project-name"], |
| 115 |
"metavar": "NAME", |
| 116 |
"default": get_project_name(), |
| 117 |
"help": "project name for report. Default: %(default)s", |
| 118 |
}, { |
| 119 |
"flags": ["--marker-file"], |
| 120 |
"metavar": "FILE", |
| 121 |
"default": "cbmc-proof.txt", |
| 122 |
"help": ( |
| 123 |
"name of file that marks proof directories. Default: " |
| 124 |
"%(default)s"), |
| 125 |
}, { |
| 126 |
"flags": ["--no-memory-profile"], |
| 127 |
"action": "store_true", |
| 128 |
"help": "disable memory profiling, even if Litani supports it" |
| 129 |
}, { |
| 130 |
"flags": ["--no-expensive-limit"], |
| 131 |
"action": "store_true", |
| 132 |
"help": "do not limit parallelism of 'EXPENSIVE' jobs", |
| 133 |
}, { |
| 134 |
"flags": ["--expensive-jobs-parallelism"], |
| 135 |
"metavar": "N", |
| 136 |
"default": 1, |
| 137 |
"type": int, |
| 138 |
"help": ( |
| 139 |
"how many proof jobs marked 'EXPENSIVE' to run in parallel. " |
| 140 |
"Default: %(default)s"), |
| 141 |
}, { |
| 142 |
"flags": ["--verbose"], |
| 143 |
"action": "store_true", |
| 144 |
"help": "verbose output", |
| 145 |
}, { |
| 146 |
"flags": ["--debug"], |
| 147 |
"action": "store_true", |
| 148 |
"help": "debug output", |
| 149 |
}, { |
| 150 |
"flags": ["--summarize"], |
| 151 |
"action": "store_true", |
| 152 |
"help": "summarize proof results with two tables on stdout", |
| 153 |
}, { |
| 154 |
"flags": ["--version"], |
| 155 |
"action": "version", |
| 156 |
"version": "CBMC starter kit 2.5", |
| 157 |
"help": "display version and exit" |
| 158 |
}]: |
| 159 |
flags = arg.pop("flags") |
| 160 |
pars.add_argument(*flags, **arg) |
| 161 |
return pars.parse_args() |
| 162 |
|
| 163 |
|
| 164 |
def set_up_logging(verbose): |
| 165 |
if verbose: |
| 166 |
level = logging.DEBUG |
| 167 |
else: |
| 168 |
level = logging.WARNING |
| 169 |
logging.basicConfig( |
| 170 |
format="run-cbmc-proofs: %(message)s", level=level) |
| 171 |
|
| 172 |
|
| 173 |
def task_pool_size(): |
| 174 |
ret = os.cpu_count() |
| 175 |
if ret is None or ret < 3: |
| 176 |
return 1 |
| 177 |
return ret - 2 |
| 178 |
|
| 179 |
|
| 180 |
def print_counter(counter): |
| 181 |
|
| 182 |
print("\rConfiguring CBMC proofs: " |
| 183 |
"{complete:{width}} / {total:{width}}".format(**counter), end="", file=sys.stderr) |
| 184 |
|
| 185 |
|
| 186 |
def get_proof_dirs(proof_root, proof_list, marker_file): |
| 187 |
if proof_list is not None: |
| 188 |
proofs_remaining = list(proof_list) |
| 189 |
else: |
| 190 |
proofs_remaining = [] |
| 191 |
|
| 192 |
for root, _, fyles in os.walk(proof_root): |
| 193 |
proof_name = str(pathlib.Path(root).name) |
| 194 |
if root != str(proof_root) and ".litani_cache_dir" in fyles: |
| 195 |
pathlib.Path(f"{root}/.litani_cache_dir").unlink() |
| 196 |
if proof_list and proof_name not in proof_list: |
| 197 |
continue |
| 198 |
if proof_list and proof_name in proofs_remaining: |
| 199 |
proofs_remaining.remove(proof_name) |
| 200 |
if marker_file in fyles: |
| 201 |
yield root |
| 202 |
|
| 203 |
if proofs_remaining: |
| 204 |
logging.critical( |
| 205 |
"The following proofs were not found: %s", |
| 206 |
", ".join(proofs_remaining)) |
| 207 |
sys.exit(1) |
| 208 |
|
| 209 |
|
| 210 |
def run_build(litani, jobs, fail_on_proof_failure, summarize): |
| 211 |
cmd = [str(litani), "run-build"] |
| 212 |
if jobs: |
| 213 |
cmd.extend(["-j", str(jobs)]) |
| 214 |
if fail_on_proof_failure: |
| 215 |
cmd.append("--fail-on-pipeline-failure") |
| 216 |
if summarize: |
| 217 |
out_file = pathlib.Path(tempfile.gettempdir(), "run.json").resolve() |
| 218 |
cmd.extend(["--out-file", str(out_file)]) |
| 219 |
|
| 220 |
logging.debug(" ".join(cmd)) |
| 221 |
proc = subprocess.run(cmd, check=False) |
| 222 |
|
| 223 |
if proc.returncode and not fail_on_proof_failure: |
| 224 |
logging.critical("Failed to run litani run-build") |
| 225 |
sys.exit(1) |
| 226 |
|
| 227 |
if summarize: |
| 228 |
print_proof_results(out_file) |
| 229 |
out_file.unlink() |
| 230 |
|
| 231 |
if proc.returncode: |
| 232 |
logging.error("One or more proofs failed") |
| 233 |
sys.exit(10) |
| 234 |
|
| 235 |
def get_litani_path(proof_root): |
| 236 |
cmd = [ |
| 237 |
"make", |
| 238 |
"--no-print-directory", |
| 239 |
f"PROOF_ROOT={proof_root}", |
| 240 |
"-f", "Makefile.common", |
| 241 |
"litani-path", |
| 242 |
] |
| 243 |
logging.debug(" ".join(cmd)) |
| 244 |
proc = subprocess.run(cmd, universal_newlines=True, stdout=subprocess.PIPE, check=False) |
| 245 |
if proc.returncode: |
| 246 |
logging.critical("Could not determine path to litani") |
| 247 |
sys.exit(1) |
| 248 |
return proc.stdout.strip() |
| 249 |
|
| 250 |
|
| 251 |
def get_litani_capabilities(litani_path): |
| 252 |
cmd = [litani_path, "print-capabilities"] |
| 253 |
proc = subprocess.run( |
| 254 |
cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, check=False) |
| 255 |
if proc.returncode: |
| 256 |
return [] |
| 257 |
try: |
| 258 |
return json.loads(proc.stdout) |
| 259 |
except RuntimeError: |
| 260 |
logging.warning("Could not load litani capabilities: '%s'", proc.stdout) |
| 261 |
return [] |
| 262 |
|
| 263 |
|
| 264 |
def check_uid_uniqueness(proof_dir, proof_uids): |
| 265 |
with (pathlib.Path(proof_dir) / "Makefile").open() as handle: |
| 266 |
for line in handle: |
| 267 |
match = re.match(r"^PROOF_UID\s*=\s*(?P<uid>\w+)", line) |
| 268 |
if not match: |
| 269 |
continue |
| 270 |
if match["uid"] not in proof_uids: |
| 271 |
proof_uids[match["uid"]] = proof_dir |
| 272 |
return |
| 273 |
|
| 274 |
logging.critical( |
| 275 |
"The Makefile in directory '%s' should have a different " |
| 276 |
"PROOF_UID than the Makefile in directory '%s'", |
| 277 |
proof_dir, proof_uids[match["uid"]]) |
| 278 |
sys.exit(1) |
| 279 |
|
| 280 |
logging.critical( |
| 281 |
"The Makefile in directory '%s' should contain a line like", proof_dir) |
| 282 |
logging.critical("PROOF_UID = ...") |
| 283 |
logging.critical("with a unique identifier for the proof.") |
| 284 |
sys.exit(1) |
| 285 |
|
| 286 |
|
| 287 |
def should_enable_memory_profiling(litani_caps, args): |
| 288 |
if args.no_memory_profile: |
| 289 |
return False |
| 290 |
return "memory_profile" in litani_caps |
| 291 |
|
| 292 |
|
| 293 |
def should_enable_pools(litani_caps, args): |
| 294 |
if args.no_expensive_limit: |
| 295 |
return False |
| 296 |
return "pools" in litani_caps |
| 297 |
|
| 298 |
|
| 299 |
async def configure_proof_dirs( |
| 300 |
queue, counter, proof_uids, enable_pools, enable_memory_profiling, debug): |
| 301 |
while True: |
| 302 |
print_counter(counter) |
| 303 |
path = str(await queue.get()) |
| 304 |
|
| 305 |
check_uid_uniqueness(path, proof_uids) |
| 306 |
|
| 307 |
pools = ["ENABLE_POOLS=true"] if enable_pools else [] |
| 308 |
profiling = [ |
| 309 |
"ENABLE_MEMORY_PROFILING=true"] if enable_memory_profiling else [] |
| 310 |
|
| 311 |
|
| 312 |
proc = await asyncio.create_subprocess_exec( |
| 313 |
"nice", "-n", "15", "make", *pools, |
| 314 |
*profiling, "-B", "_report", "" if debug else "--quiet", cwd=path, |
| 315 |
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) |
| 316 |
stdout, stderr = await proc.communicate() |
| 317 |
logging.debug("returncode: %s", str(proc.returncode)) |
| 318 |
logging.debug("stdout:") |
| 319 |
for line in stdout.decode().splitlines(): |
| 320 |
logging.debug(line) |
| 321 |
logging.debug("stderr:") |
| 322 |
for line in stderr.decode().splitlines(): |
| 323 |
logging.debug(line) |
| 324 |
|
| 325 |
counter["fail" if proc.returncode else "pass"].append(path) |
| 326 |
counter["complete"] += 1 |
| 327 |
|
| 328 |
print_counter(counter) |
| 329 |
queue.task_done() |
| 330 |
|
| 331 |
|
| 332 |
async def main(): |
| 333 |
args = get_args() |
| 334 |
set_up_logging(args.verbose) |
| 335 |
|
| 336 |
proof_root = pathlib.Path(os.getcwd()) |
| 337 |
litani = get_litani_path(proof_root) |
| 338 |
|
| 339 |
litani_caps = get_litani_capabilities(litani) |
| 340 |
enable_pools = should_enable_pools(litani_caps, args) |
| 341 |
init_pools = [ |
| 342 |
"--pools", f"expensive:{args.expensive_jobs_parallelism}" |
| 343 |
] if enable_pools else [] |
| 344 |
|
| 345 |
if not args.no_standalone: |
| 346 |
cmd = [ |
| 347 |
str(litani), "init", *init_pools, "--project", args.project_name, |
| 348 |
"--no-print-out-dir", |
| 349 |
] |
| 350 |
|
| 351 |
if "output_directory_flags" in litani_caps: |
| 352 |
out_prefix = proof_root / "output" |
| 353 |
out_symlink = out_prefix / "latest" |
| 354 |
out_index = out_symlink / "html" / "index.html" |
| 355 |
cmd.extend([ |
| 356 |
"--output-prefix", str(out_prefix), |
| 357 |
"--output-symlink", str(out_symlink), |
| 358 |
]) |
| 359 |
print( |
| 360 |
"\nFor your convenience, the output of this run will be symbolically linked to ", |
| 361 |
out_index, "\n") |
| 362 |
|
| 363 |
logging.debug(" ".join(cmd)) |
| 364 |
proc = subprocess.run(cmd, check=False) |
| 365 |
if proc.returncode: |
| 366 |
logging.critical("Failed to run litani init") |
| 367 |
sys.exit(1) |
| 368 |
|
| 369 |
proof_dirs = list(get_proof_dirs( |
| 370 |
proof_root, args.proofs, args.marker_file)) |
| 371 |
if not proof_dirs: |
| 372 |
logging.critical("No proof directories found") |
| 373 |
sys.exit(1) |
| 374 |
|
| 375 |
proof_queue = asyncio.Queue() |
| 376 |
for proof_dir in proof_dirs: |
| 377 |
proof_queue.put_nowait(proof_dir) |
| 378 |
|
| 379 |
counter = { |
| 380 |
"pass": [], |
| 381 |
"fail": [], |
| 382 |
"complete": 0, |
| 383 |
"total": len(proof_dirs), |
| 384 |
"width": int(math.log10(len(proof_dirs))) + 1 |
| 385 |
} |
| 386 |
|
| 387 |
proof_uids = {} |
| 388 |
tasks = [] |
| 389 |
|
| 390 |
enable_memory_profiling = should_enable_memory_profiling(litani_caps, args) |
| 391 |
|
| 392 |
for _ in range(task_pool_size()): |
| 393 |
task = asyncio.create_task(configure_proof_dirs( |
| 394 |
proof_queue, counter, proof_uids, enable_pools, |
| 395 |
enable_memory_profiling, args.debug)) |
| 396 |
tasks.append(task) |
| 397 |
|
| 398 |
await proof_queue.join() |
| 399 |
|
| 400 |
print_counter(counter) |
| 401 |
print("", file=sys.stderr) |
| 402 |
|
| 403 |
if counter["fail"]: |
| 404 |
logging.critical( |
| 405 |
"Failed to configure the following proofs:\n%s", "\n".join( |
| 406 |
[str(f) for f in counter["fail"]])) |
| 407 |
sys.exit(1) |
| 408 |
|
| 409 |
if not args.no_standalone: |
| 410 |
run_build(litani, args.parallel_jobs, args.fail_on_proof_failure, args.summarize) |
| 411 |
|
| 412 |
|
| 413 |
if __name__ == "__main__": |
| 414 |
asyncio.run(main()) |