Skip to content

Commit

Permalink
feat: add custom solver options (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
daejunpark authored Sep 25, 2023
1 parent e16be9f commit 5926a9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/halmos/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ def run(
else:
stuck.append((opcode, idx, ex))

if len(execs_to_model) > 0 and args.dump_smt_queries:
dirname = f"/tmp/{funname}.{uuid.uuid4().hex}"
os.makedirs(dirname)
print(f"Generating SMT queries in {dirname}")
for idx, ex in execs_to_model:
fname = f"{dirname}/{idx+1}.smt2"
query = ex.solver.to_smt2()
with open(fname, "w") as f:
f.write("(set-logic QF_AUFBV)\n")
f.write(query)

if len(execs_to_model) > 0 and args.verbose >= 1:
print(
f"# of potential paths involving assertion violations: {len(execs_to_model)} / {len(exs)}"
Expand Down Expand Up @@ -789,7 +800,7 @@ def gen_model(args: Namespace, idx: int, ex: Exec) -> ModelWithContext:
print(f" Checking again in an external process")
fname = f"/tmp/{uuid.uuid4().hex}.smt2"
if args.verbose >= 1:
print(f" z3 -model {fname} >{fname}.out")
print(f" {args.solver_subprocess_command} {fname} >{fname}.out")
query = ex.solver.to_smt2()
# replace uninterpreted abstraction with actual symbols for assertion solving
# TODO: replace `(evm_bvudiv x y)` with `(ite (= y (_ bv0 256)) (_ bv0 256) (bvudiv x y))`
Expand All @@ -798,9 +809,8 @@ def gen_model(args: Namespace, idx: int, ex: Exec) -> ModelWithContext:
with open(fname, "w") as f:
f.write("(set-logic QF_AUFBV)\n")
f.write(query)
res_str = subprocess.run(
["z3", "-model", fname], capture_output=True, text=True
).stdout.strip()
cmd = args.solver_subprocess_command.split() + [fname]
res_str = subprocess.run(cmd, capture_output=True, text=True).stdout.strip()
res_str_head = res_str.split("\n", 1)[0]
with open(f"{fname}.out", "w") as f:
f.write(res_str)
Expand Down
12 changes: 12 additions & 0 deletions src/halmos/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ def mk_arg_parser() -> argparse.ArgumentParser:
help="turn unknown counterexample warnings to errors",
)

group_debug.add_argument(
"--dump-smt-queries",
action="store_true",
help="dump SMT queries for assertion violations",
)

# build options
group_build = parser.add_argument_group("Build options")

Expand Down Expand Up @@ -209,6 +215,12 @@ def mk_arg_parser() -> argparse.ArgumentParser:
action="store_true",
help="run an extra solver in subprocess for unknown",
)
group_solver.add_argument(
"--solver-subprocess-command",
metavar="COMMAND",
default="z3 -model",
help="use the given command for the subprocess solver (requires --solver-subprocess) (default: %(default)s)",
)
group_solver.add_argument(
"--solver-parallel",
action="store_true",
Expand Down

0 comments on commit 5926a9d

Please sign in to comment.