CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / test_core_residue_mutants.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Recompile admission and packing mutants. Compilation failure is not a kill.""" |
| 4 | import os |
| 5 | import shlex |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import tempfile |
| 9 | from pathlib import Path |
| 10 | |
| 11 | ROOT = Path(__file__).resolve().parents[1] |
| 12 | OMC = os.environ.get("OMEGA_OMC", "") |
| 13 | BRIDGE = os.environ.get("OMEGA_BRIDGE", "") |
| 14 | CC = ["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", "-pthread", "-Isrc"] |
| 15 | LINK = [ |
| 16 | "src/omega_cubin.c", |
| 17 | "src/cudapp_om_cubin_elf.c", |
| 18 | "src/cudapp_freest_core_cubinmsg.c", |
| 19 | "src/cudapp_om_core_fmtnum.c", |
| 20 | ] |
| 21 | |
| 22 | MUTANTS = ( |
| 23 | ("sm86", "cubadmit", "ne sm 86", "ne sm 87"), |
| 24 | ("sm121", "cubadmit", "ne sm 121", "ne sm 120"), |
| 25 | ("target-code", "cubadmit", "ret 8", "ret 2"), |
| 26 | ("null-text", "cubadmit", "put o add p 0 uchar 110", "put o add p 0 uchar 78"), |
| 27 | ("target-text", "cubadmit", "put o add p 0 uchar 117", "put o add p 0 uchar 85"), |
| 28 | ("sign-bit", "ampack", "if ne and v 2147483648 0", "if ne and v 1073741824 0"), |
| 29 | ("control-shift", "ampack", "shl ctl 32", "shl ctl 31"), |
| 30 | ("bar-shift", "ampack", "shl get src 15 8", "shl get src 15 7"), |
| 31 | ) |
| 32 | |
| 33 | |
| 34 | def run(command, log, env=None): |
| 35 | result = subprocess.run(command, cwd=ROOT, env=env, stdout=subprocess.PIPE, |
| 36 | stderr=subprocess.STDOUT, text=True, timeout=300) |
| 37 | log.write("$ " + shlex.join(map(str, command)) + "\n" + result.stdout) |
| 38 | return result |
| 39 | |
| 40 | |
| 41 | def main(): |
| 42 | if not OMC or not BRIDGE: |
| 43 | print("NOT_RUN: set OMEGA_OMC and OMEGA_BRIDGE") |
| 44 | sys.exit(3) |
| 45 | killed = 0 |
| 46 | for name, unit, old, new in MUTANTS: |
| 47 | src = (ROOT / "om/core" / f"{unit}.om").read_text() |
| 48 | if src.count(old) != 1: |
| 49 | print(f"MUTANT {name} BROKEN: site found {src.count(old)} times") |
| 50 | sys.exit(1) |
| 51 | with tempfile.TemporaryDirectory() as temp: |
| 52 | temp = Path(temp) |
| 53 | mutant = temp / f"{unit}.om" |
| 54 | mutant.write_text(src.replace(old, new, 1)) |
| 55 | log_path = temp / "log" |
| 56 | with log_path.open("w") as log: |
| 57 | spec = f"core_{unit}={mutant}" |
| 58 | if unit == "cubadmit": |
| 59 | spec = f"core_cubadmit={ROOT / 'om/core/fmtnum.om'},{mutant}" |
| 60 | env = os.environ.copy() |
| 61 | env.update({ |
| 62 | "OMEGA_UNITS": spec, |
| 63 | "OMEGA_UNITS_DEST": str(temp), |
| 64 | "OMEGA_OMC": OMC, |
| 65 | "OMEGA_BRIDGE": BRIDGE, |
| 66 | }) |
| 67 | gen = run(["sh", "om/units.sh"], log, env) |
| 68 | if gen.returncode != 0: |
| 69 | print(f"MUTANT {name} BROKEN: units.sh {gen.returncode}") |
| 70 | sys.exit(1) |
| 71 | generated = temp / (f"cudapp_freest_core_{unit}.c" if unit == "cubadmit" else f"cudapp_om_core_{unit}.c") |
| 72 | other = ROOT / "src" / ( |
| 73 | "cudapp_om_core_ampack.c" if unit == "cubadmit" else "cudapp_freest_core_cubadmit.c") |
| 74 | binary = temp / "residue" |
| 75 | built = run(CC + ["-o", str(binary), "tests/test_core_residue.c", *LINK, str(generated), str(other)], log) |
| 76 | if built.returncode != 0: |
| 77 | print(f"MUTANT {name} not killed: compilation failed") |
| 78 | sys.exit(1) |
| 79 | ran = run([str(binary)], log) |
| 80 | text = ran.stdout or "" |
| 81 | if ran.returncode == 0 or "RESIDUE FAIL" not in text: |
| 82 | print(f"MUTANT {name} SURVIVED") |
| 83 | sys.exit(1) |
| 84 | print(f"CORE-RESIDUE mutant {name} KILLED", flush=True) |
| 85 | killed += 1 |
| 86 | print(f"CORE-RESIDUE mutants PASS: {killed} recompiled semantic mutants killed") |
| 87 | |
| 88 | |
| 89 | if __name__ == "__main__": |
| 90 | main() |