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 / runtime / cm_source_mutants.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Attributed source mutants; compile common objects once, no checked-in mutation.""" |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import shlex |
| 7 | import subprocess |
| 8 | import tempfile |
| 9 | |
| 10 | root = Path(__file__).resolve().parents[2] |
| 11 | bridge = Path(os.environ['OMEGA_BRIDGE']) |
| 12 | env = dict(os.environ, OMEGA_SEEDED_OMC=os.environ['OMEGA_OMC']) |
| 13 | cc = shlex.split(os.environ.get('CC', 'cc')) |
| 14 | flags = ['-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-I' + str(root / 'src')] |
| 15 | math = ['om/components/cm_math.om', 'om/components/cm_math_extra.om', 'om/components/cm_math_erfc.om'] |
| 16 | units = {name: math + ['om/components/' + name + '.om'] for name in ( |
| 17 | 'cm_fit', 'cm_stats', 'cm_features', 'cm_quantile', 'cm_decision', 'cm_prediction')} |
| 18 | units['cm_math'] = math |
| 19 | units['cm_sort'] = ['om/components/cm_sort.om'] |
| 20 | units['cm_calibration'] = math + ['om/components/cm_quantile.om', 'om/components/cm_calibration.om'] |
| 21 | units['cm_blob'] = ['om/components/cm_blob.om'] |
| 22 | units['cm_digest'] = ['om/cache/digest.om', 'om/components/cm_digest.om'] |
| 23 | units['cm_route'] = ['om/components/cm_route.om'] |
| 24 | mutants = ( |
| 25 | ('cm_features', 'cm_features', 'ret 4294967295', 'ret 0', 'rx_costmodel_unit', ['refuse']), |
| 26 | ('cm_stats', 'cm_stats', 'put out 45 add get out 45 1', 'put out 45 add get out 45 2', 'rx_costmodel_unit', []), |
| 27 | ('cm_fit', 'cm_fit', 'sigma = div b a', 'sigma = mul b a', 'rx_costmodel_unit', []), |
| 28 | ('cm_quantile', 'cm_quantile', 'ret bitcast ulong add add z a b', 'ret bitcast ulong neg add add z a b', 'rx_costmodel_unit', []), |
| 29 | ('cm_prediction', 'cm_prediction', 'put words 17 bitcast ulong mul sd scale', 'put words 17 bitcast ulong add sd scale', 'rx_costmodel_unit', []), |
| 30 | ('cm_decision', 'cm_decision', 'put data 100 ulong 2', 'put data 100 ulong 3', 'rx_costmodel_unit', []), |
| 31 | ('cm_calibration', 'cm_calibration', 'if lt scale f64 1.0', 'if gt scale f64 1.0', 'rx_costmodel_unit', []), |
| 32 | ('cm_blob', 'cm_blob', 'put words 16 ulong 5591222095', 'put words 16 ulong 5591222094', 'cm_blob_unit', []), |
| 33 | ('cm_digest', 'cm_digest', 'put data q uchar and v 255', 'put data q uchar and v 254', 'cm_blob_unit', []), |
| 34 | ('cm_math', 'cm_math_erfc', 'ret bitcast ulong cm_log bitcast f64 bits false', 'ret bitcast ulong cm_log bitcast f64 bits true', 'cm_math_unit', []), |
| 35 | ) |
| 36 | |
| 37 | with tempfile.TemporaryDirectory(prefix='cudapp-cm-mutants-') as temporary: |
| 38 | tmp = Path(temporary) |
| 39 | def compile_object(source, name, extra=()): |
| 40 | obj = tmp / (name + '.o') |
| 41 | subprocess.run(cc + flags + list(extra) + ['-c', str(source), '-o', str(obj)], check=True) |
| 42 | return str(obj) |
| 43 | originals = {name: compile_object(root / 'src' / ('cudapp_om_' + name + '.c'), name) for name in units} |
| 44 | common = [compile_object(root / 'src/runtime/rx_costmodel.c', 'model', ['-ffp-contract=off']), |
| 45 | compile_object(root / 'src/sha256.c', 'sha'), |
| 46 | compile_object(root / 'src/cudapp_om_core_sha.c', 'core-sha'), |
| 47 | compile_object(root / 'tests/runtime/cm_oracle.c', 'oracle', ['-ffp-contract=off'])] |
| 48 | tests = {name: compile_object(root / 'tests/runtime' / (name + '.c'), name) for name in ( |
| 49 | 'rx_costmodel_unit', 'cm_blob_unit', 'cm_math_unit')} |
| 50 | verifier = compile_object(root / 'tests/runtime/cm_decision_diff.c', 'decision-verifier', ['-DCM_DIFF_REPLAY_MAIN']) |
| 51 | disabled = subprocess.run(cc + flags + ['-DNDEBUG', '-c', str(root / 'tests/runtime/cm_decision_diff.c'), |
| 52 | '-o', str(tmp / 'disabled.o')], capture_output=True, text=True) |
| 53 | assert disabled.returncode != 0 and 'requires enabled assertions' in disabled.stderr |
| 54 | print('CM DECISION verifier control PASS: assertion-disabled build refused', flush=True) |
| 55 | for name, part, old, new, test, args in mutants: |
| 56 | contents = [] |
| 57 | changed = 0 |
| 58 | for path in units[name]: |
| 59 | source = (root / path).read_text() |
| 60 | if Path(path).stem == part: |
| 61 | assert source.count(old) == 1, (name, old, source.count(old)) |
| 62 | source = source.replace(old, new) |
| 63 | changed += 1 |
| 64 | contents.append(source) |
| 65 | assert changed == 1 |
| 66 | om = tmp / (name + '-mutant.om') |
| 67 | om.write_text('\n'.join(contents) + '\n') |
| 68 | generated = tmp / (name + '-mutant.c') |
| 69 | with generated.open('w') as output: |
| 70 | subprocess.run(['sh', str(bridge / 'tools/bridge-seeded'), 'c', str(om)], |
| 71 | env=env, stdout=output, check=True) |
| 72 | mutated = compile_object(generated, name + '-mutant') |
| 73 | objects = [mutated] if test == 'cm_math_unit' else common + [ |
| 74 | mutated if key == name else value for key, value in originals.items()] |
| 75 | executable = tmp / (name + '-unit') |
| 76 | subprocess.run(cc + [tests[test]] + objects + ['-lm', '-o', str(executable)], check=True) |
| 77 | result = subprocess.run([str(executable)] + args, capture_output=True, text=True) |
| 78 | failures = [line for line in result.stderr.splitlines() if 'FAIL' in line or 'Assertion' in line] |
| 79 | assert result.returncode != 0 and failures, (name, result.returncode, result.stdout, result.stderr) |
| 80 | print('CM SOURCE mutant caught:', name, 'rc=' + str(result.returncode), failures[0], flush=True) |
| 81 | if name == 'cm_decision': |
| 82 | executable = tmp / 'decision-verifier-mutant' |
| 83 | wraps = ['-Wl,--wrap=' + name for name in ('rx_cm_decide', 'rx_cm_predict', |
| 84 | 'rx_cm_init', 'rx_cm_observe', 'rx_cm_calibrate', 'rx_cm_charge')] |
| 85 | subprocess.run(cc + [verifier] + objects + wraps + ['-lm', '-o', str(executable)], check=True) |
| 86 | result = subprocess.run([str(executable)], capture_output=True, text=True) |
| 87 | assert result.returncode != 0 and 'out->action == expected.action' in result.stderr, result |
| 88 | print('CM DECISION verifier control PASS: discrete action mutant caught', flush=True) |
| 89 | print('CM SOURCE mutants PASS (10 attributed arithmetic/header/digest/decision mutants)') |