generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 23
PyAnalyze run multiple Z3 configs in parallel. #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
|
|
||
| (set-option :smt.mbqi false) (set-option :auto_config false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| # Copyright Strata Contributors | ||
|
|
||
| # SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| # This file runs several configurations of Z3 in parallel, and returns SAT/UNSAT if | ||
| # any return the same, only returning unknown if all return unknown / timeout. | ||
| # Configurations can be added to z3_configs.txt, one per line. | ||
| # The solvers currently run in parallel until completion. This could be improved, but | ||
| # we currently use a 1s timeout, so it's not a high priority. | ||
|
|
||
| import sys | ||
| import subprocess | ||
| import tempfile | ||
| from pathlib import Path | ||
| from concurrent.futures import ProcessPoolExecutor, as_completed | ||
|
|
||
| def run_z3_config(smt_content, config_pair, timeout): | ||
| with tempfile.NamedTemporaryFile(mode='w', suffix='.smt2', delete=False) as f: | ||
| f.write(f"{config_pair[0]} {config_pair[1]}\n") | ||
| f.write(smt_content) | ||
| f.flush() | ||
|
|
||
| process = None | ||
| try: | ||
| process = subprocess.Popen( | ||
| ['z3', f'-T:{timeout}', f.name], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True | ||
| ) | ||
| stdout, stderr = process.communicate(timeout=timeout) | ||
| Path(f.name).unlink() | ||
|
|
||
| output = stdout.strip() | ||
| first_line = output.split('\n')[0].lower() if output else '' | ||
| if first_line == 'sat': | ||
| return 'sat', output | ||
| elif first_line == 'unsat': | ||
| return 'unsat', output | ||
| return None, output | ||
| except subprocess.TimeoutExpired: | ||
| if process: | ||
| process.kill() | ||
| process.wait() | ||
| Path(f.name).unlink() | ||
| return None, "timeout" | ||
| except Exception as e: | ||
| Path(f.name).unlink() | ||
| return None, str(e) | ||
|
|
||
| def main(): | ||
| if len(sys.argv) < 2: | ||
| print("Usage: z3_parallel.py [-v] [-c config_file] <smt_file>") | ||
| sys.exit(1) | ||
|
|
||
| verbose = False | ||
| config_file = None | ||
| args = sys.argv[1:] | ||
|
|
||
| while args and args[0].startswith('-'): | ||
| if args[0] == '-v': | ||
| verbose = True | ||
| args = args[1:] | ||
| elif args[0] == '-c': | ||
| if len(args) < 2: | ||
| print("Usage: z3_parallel.py [-v] [-c config_file] <smt_file>") | ||
| sys.exit(1) | ||
| config_file = args[1] | ||
| args = args[2:] | ||
| else: | ||
| break | ||
|
|
||
| if len(args) != 1: | ||
| print("Usage: z3_parallel.py [-v] [-c config_file] <smt_file>") | ||
| sys.exit(1) | ||
|
|
||
| smt_file = args[0] | ||
|
|
||
| if config_file is None: | ||
| script_dir = Path(__file__).parent | ||
| config_file = script_dir / 'z3_configs.txt' | ||
|
|
||
| timeout = 1 | ||
|
|
||
| configs = [] | ||
| with open(config_file) as f: | ||
| for line in f: | ||
| line = line.strip() | ||
| parts = line.split(maxsplit=1) | ||
| if len(parts) == 2: | ||
| configs.append(parts) | ||
| elif len(parts) == 0: | ||
| configs.append(('', '')) | ||
| else: | ||
| configs.append((parts[0], '')) | ||
|
|
||
| with open(smt_file) as f: | ||
| smt_content = f.read() | ||
|
|
||
| with ProcessPoolExecutor(max_workers=len(configs)) as executor: | ||
| futures = [executor.submit(run_z3_config, smt_content, cfg, timeout) for cfg in configs] | ||
|
|
||
| sat_result = None | ||
| all_results = [] | ||
| for future in as_completed(futures): | ||
| result, output = future.result() | ||
| all_results.append((result, output)) | ||
| if result and not sat_result: | ||
| sat_result = (result, output) | ||
|
|
||
| if verbose: | ||
| for i, (result, output) in enumerate(all_results): | ||
| print(f"Config {i}: {result or 'unknown'} - {output}") | ||
|
|
||
| if sat_result: | ||
| print(sat_result[0]) | ||
| return | ||
|
|
||
| print("unknown") | ||
|
|
||
| if __name__ == '__main__': | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.