|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This file is part of the replication artifact for |
| 4 | +# difference verification with conditions: |
| 5 | +# https://gitlab.com/sosy-lab/research/data/difference-data |
| 6 | +# |
| 7 | +# SPDX-FileCopyrightText: 2020 Dirk Beyer <https://sosy-lab.org> |
| 8 | +# SPDX-FileCopyrightText: 2020 The SV-Benchmarks Community |
| 9 | +# |
| 10 | +# SPDX-License-Identifier: Apache-2.0 |
| 11 | + |
| 12 | +import argparse |
| 13 | +from pathlib import Path |
| 14 | +import yaml |
| 15 | +import sys |
| 16 | +import re |
| 17 | + |
| 18 | + |
| 19 | +class TaskError(Exception): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +def get_tasks(benchmark_dir: Path, glob_pattern): |
| 24 | + for file1 in benchmark_dir.glob(glob_pattern): |
| 25 | + input_file = _get_input_file(file1) |
| 26 | + try: |
| 27 | + verdict = _get_verdict(file1) |
| 28 | + except TaskError: |
| 29 | + print( |
| 30 | + "INFO: Ignoring %s because of missing verdict" % str(file1), |
| 31 | + file=sys.stderr, |
| 32 | + ) |
| 33 | + continue |
| 34 | + else: |
| 35 | + yield input_file, verdict |
| 36 | + |
| 37 | + |
| 38 | +def _get_input_file(yml_task_def): |
| 39 | + with yml_task_def.open() as inp: |
| 40 | + yml_content = yaml.safe_load(inp) |
| 41 | + return yml_task_def.parent / Path(yml_content["input_files"]) |
| 42 | + |
| 43 | + |
| 44 | +def _get_verdict(yml_task_def): |
| 45 | + with yml_task_def.open() as inp: |
| 46 | + yml_content = yaml.safe_load(inp) |
| 47 | + try: |
| 48 | + return next( |
| 49 | + p["expected_verdict"] |
| 50 | + for p in yml_content["properties"] |
| 51 | + if p["property_file"].endswith("unreach-call.prp") |
| 52 | + ) |
| 53 | + except StopIteration: |
| 54 | + raise TaskError() |
| 55 | + |
| 56 | + |
| 57 | +def _remove_all_copyright_comments(content): |
| 58 | + filtered_content = list() |
| 59 | + delimiter = "\n" |
| 60 | + for line in content.split(delimiter): |
| 61 | + if _is_singleline_comment(line) and ( |
| 62 | + "This file is part of" in line |
| 63 | + or "sv-benchmarks" in line |
| 64 | + or "SPDX" in line |
| 65 | + or line.strip() == "//" |
| 66 | + ): |
| 67 | + continue |
| 68 | + filtered_content.append(line) |
| 69 | + return delimiter.join(filtered_content) |
| 70 | + |
| 71 | + |
| 72 | +def _create_combo( |
| 73 | + file1: Path, file2: Path, replacement1=None, replacement2=None |
| 74 | +) -> str: |
| 75 | + if replacement1: |
| 76 | + repl1 = replacement1 |
| 77 | + else: |
| 78 | + repl1 = lambda x: x |
| 79 | + if replacement2: |
| 80 | + repl2 = replacement2 |
| 81 | + else: |
| 82 | + repl2 = lambda x: x |
| 83 | + with file1.open() as inp: |
| 84 | + content1 = "".join( |
| 85 | + repl1(line.replace("main(", "main1(")) for line in inp.readlines() |
| 86 | + ) |
| 87 | + with file2.open() as inp: |
| 88 | + content2 = "".join( |
| 89 | + repl2(line.replace("main(", "main2(")) |
| 90 | + for line in inp.readlines() |
| 91 | + if not line.startswith("extern ") |
| 92 | + and not line.startswith("void reach_error") |
| 93 | + ) |
| 94 | + |
| 95 | + additional_defs = """extern unsigned int __VERIFIER_nondet_uint(); |
| 96 | +extern char __VERIFIER_nondet_char(); |
| 97 | +extern int __VERIFIER_nondet_int(); |
| 98 | +extern long __VERIFIER_nondet_long(); |
| 99 | +extern unsigned long __VERIFIER_nondet_ulong(); |
| 100 | +extern float __VERIFIER_nondet_float(); |
| 101 | +extern void exit(int); |
| 102 | +""" |
| 103 | + |
| 104 | + content = ( |
| 105 | + additional_defs |
| 106 | + + content1 |
| 107 | + + content2 |
| 108 | + + """int main() |
| 109 | +{ |
| 110 | + if(__VERIFIER_nondet_int()) |
| 111 | + main1(); |
| 112 | + else |
| 113 | + main2(); |
| 114 | +}""" |
| 115 | + ) |
| 116 | + |
| 117 | + # remove comments to avoid duplicate SPDX and copyright info |
| 118 | + return _remove_all_copyright_comments(content) |
| 119 | + |
| 120 | + |
| 121 | +def _get_yml_content(verdict1, verdict2, input_file: str, data_model="ILP32"): |
| 122 | + verdict = verdict1 == verdict2 == True |
| 123 | + return f"""format_version: '2.0' |
| 124 | +
|
| 125 | +input_files: '{input_file}' |
| 126 | +
|
| 127 | +properties: |
| 128 | + - property_file: ../properties/unreach-call.prp |
| 129 | + expected_verdict: {verdict} |
| 130 | +
|
| 131 | +options: |
| 132 | + language: C |
| 133 | + data_model: {data_model} |
| 134 | +""" |
| 135 | + |
| 136 | + |
| 137 | +def _spdx_info_to_text(spdx_info): |
| 138 | + def to_comment(key, value): |
| 139 | + return f"// {key}: {value}" |
| 140 | + |
| 141 | + content = "" |
| 142 | + for idx, item in enumerate(sorted(spdx_info.items())): |
| 143 | + key, values = item |
| 144 | + if idx > 0: |
| 145 | + content += "//\n" # add one empty line to separate different keys |
| 146 | + text_for_single_key = "\n".join((to_comment(key, v) for v in sorted(values))) |
| 147 | + content += text_for_single_key + "\n" |
| 148 | + return content |
| 149 | + |
| 150 | + |
| 151 | +def _is_singleline_comment(line): |
| 152 | + return line.strip().startswith("//") |
| 153 | + |
| 154 | + |
| 155 | +def _get_spdx_header(*files): |
| 156 | + """Parse all given files for SPDX-metadata and return a header that contains all of that combined information.""" |
| 157 | + |
| 158 | + def _try_extract_spdx(line): |
| 159 | + if not (_is_singleline_comment(line) and "SPDX" in line): |
| 160 | + return None, None |
| 161 | + try: |
| 162 | + matches = re.search(r"(SPDX-[a-zA-Z\-]+):\s*(.+)", line) |
| 163 | + return matches.group(1), matches.group(2) |
| 164 | + except (IndexError, AttributeError): |
| 165 | + # search didn't work |
| 166 | + return None, None |
| 167 | + |
| 168 | + all_spdx_info = dict() |
| 169 | + for f in files: |
| 170 | + with open(f) as inp: |
| 171 | + for line in inp.readlines(): |
| 172 | + key, value = _try_extract_spdx(line) |
| 173 | + if key: |
| 174 | + if key not in all_spdx_info: |
| 175 | + all_spdx_info[key] = set() |
| 176 | + all_spdx_info[key].add(value) |
| 177 | + |
| 178 | + return f"""// This file is part of the SV-Benchmarks collection of verification tasks: |
| 179 | +// https://github.com/sosy-lab/sv-benchmarks |
| 180 | +// |
| 181 | +{_spdx_info_to_text(all_spdx_info)}""" |
| 182 | + |
| 183 | + |
| 184 | +def create_combos( |
| 185 | + pattern1, pattern2, replacement1=None, replacement2=None, output_dir=None |
| 186 | +): |
| 187 | + tasks1 = list(get_tasks(args.benchmark_dir, pattern1)) |
| 188 | + tasks2 = list(get_tasks(args.benchmark_dir, pattern2)) |
| 189 | + |
| 190 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 191 | + |
| 192 | + for file1, verdict1 in tasks1: |
| 193 | + for file2, verdict2 in tasks2: |
| 194 | + assert isinstance(verdict1, bool) |
| 195 | + assert isinstance(verdict2, bool) |
| 196 | + if verdict1 == verdict2 == False: |
| 197 | + continue |
| 198 | + basename = file1.name[:-2] + "+" + file2.name |
| 199 | + c_file = output_dir / Path(basename) |
| 200 | + c_content = _create_combo(file1, file2, replacement1, replacement2) |
| 201 | + c_content = _get_spdx_header(file1, file2) + "\n" + c_content |
| 202 | + yml_content = _get_yml_content(verdict1, verdict2, c_file.name) |
| 203 | + yml_file = output_dir / Path(basename[:-2] + ".yml") |
| 204 | + |
| 205 | + with c_file.open("w") as outp: |
| 206 | + outp.write(c_content) |
| 207 | + with yml_file.open("w") as outp: |
| 208 | + outp.write(yml_content) |
| 209 | + |
| 210 | + |
| 211 | +def _systemc_replacements1(line) -> str: |
| 212 | + return ( |
| 213 | + line.replace("error(", "error1(") |
| 214 | + .replace("init_threads(", "init_threads1(") |
| 215 | + .replace("exists_runnable_thread(", "exists_runnable_thread1(") |
| 216 | + .replace("eval(", "eval1(") |
| 217 | + .replace("init_model(", "init_model1(") |
| 218 | + .replace("stop_simulation(", "stop_simulation1(") |
| 219 | + .replace("start_simulation(", "start_simulation1(") |
| 220 | + .replace("reach_error1(", "reach_error(") |
| 221 | + .replace("update_channels(", "update_channels1(") |
| 222 | + .replace("fire_delta_events(", "fire_delta_events1(") |
| 223 | + .replace("reset_delta_events(", "reset_delta_events1(") |
| 224 | + .replace("activate_threads(", "activate_threads1(") |
| 225 | + .replace("fire_time_events(", "fire_time_events1(") |
| 226 | + .replace("reset_time_events(", "reset_time_events1(") |
| 227 | + ) |
| 228 | + |
| 229 | + |
| 230 | +def _systemc_replacements2(line) -> str: |
| 231 | + return ( |
| 232 | + line.replace("error(", "error2(") |
| 233 | + .replace("init_threads(", "init_threads2(") |
| 234 | + .replace("exists_runnable_thread(", "exists_runnable_thread2(") |
| 235 | + .replace("eval(", "eval2(") |
| 236 | + .replace("init_model(", "init_model2(") |
| 237 | + .replace("stop_simulation(", "stop_simulation2(") |
| 238 | + .replace("start_simulation(", "start_simulation2(") |
| 239 | + .replace("reach_error2(", "reach_error(") |
| 240 | + .replace("update_channels(", "update_channels2(") |
| 241 | + .replace("fire_delta_events(", "fire_delta_events2(") |
| 242 | + .replace("reset_delta_events(", "reset_delta_events2(") |
| 243 | + .replace("activate_threads(", "activate_threads2(") |
| 244 | + .replace("fire_time_events(", "fire_time_events2(") |
| 245 | + .replace("reset_time_events(", "reset_time_events2(") |
| 246 | + ) |
| 247 | + |
| 248 | + |
| 249 | +if __name__ == "__main__": |
| 250 | + parser = argparse.ArgumentParser() |
| 251 | + parser.add_argument( |
| 252 | + "--benchmark-dir", required=True, help="sv-benchmarks directory" |
| 253 | + ) |
| 254 | + parser.add_argument( |
| 255 | + "--output-dir", required=True, help="output directory for created files" |
| 256 | + ) |
| 257 | + |
| 258 | + args = parser.parse_args() |
| 259 | + args.benchmark_dir = Path(args.benchmark_dir) |
| 260 | + args.output_dir = Path(args.output_dir) |
| 261 | + |
| 262 | + create_combos( |
| 263 | + "c/eca-rers2012/Problem05_label4*.yml", |
| 264 | + "c/systemc/token_ring*.yml", |
| 265 | + output_dir=args.output_dir, |
| 266 | + ) |
| 267 | + create_combos( |
| 268 | + "c/bitvector/gcd_*.yml", |
| 269 | + "c/floats-cdfpl/newton_*.yml", |
| 270 | + output_dir=args.output_dir, |
| 271 | + ) |
| 272 | + create_combos( |
| 273 | + "c/seq-mthreaded/pals_lcr.*.yml", |
| 274 | + "c/eca-rers2012/Problem12_label0*.yml", |
| 275 | + output_dir=args.output_dir, |
| 276 | + ) |
| 277 | + create_combos( |
| 278 | + "c/floats-cdfpl/square*.yml", |
| 279 | + "c/bitvector/soft_float_*.yml", |
| 280 | + output_dir=args.output_dir, |
| 281 | + ) |
| 282 | + create_combos( |
| 283 | + "c/systemc/pc_sfifo*.yml", |
| 284 | + "c/systemc/token_ring*.yml", |
| 285 | + replacement1=_systemc_replacements1, |
| 286 | + replacement2=_systemc_replacements2, |
| 287 | + output_dir=args.output_dir, |
| 288 | + ) |
0 commit comments