interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / amd-kfd / control.py

67 lines · 2810 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Test instrument: acceptance comparisons and their guard-removal controls.
4
5Both the positive gate and the negative case call matches. A mutant of this
6module deletes its final comparison; each negative case must then fail, without
7printing CAUGHT. Producer executions happen before this test, not in its setup.
8"""
9import hashlib
10import pathlib
11import subprocess
12import sys
13
14
15def matches(kind, actual, expected):
16 if kind == 'table':
17 def rows(path):
18 return [tuple(map(int, line.split('#', 1)[0].split()))
19 for line in pathlib.Path(path).read_text().splitlines()]
20 actual, expected = rows(actual), rows(expected)
21 elif kind == 'digest':
22 actual = hashlib.sha256(pathlib.Path(actual).read_bytes()).hexdigest()
23 elif kind == 'exit':
24 actual, expected = int(actual), int(expected)
25 else:
26 raise ValueError('unknown comparison ' + kind)
27 return actual == expected
28
29
30def reject(label, kind, actual, expected):
31 if matches(kind, actual, expected):
32 raise AssertionError('CONTROL ESCAPED ' + label)
33 print('CONTROL CAUGHT ' + label)
34
35
36def prove(out, label, kind, actual, expected):
37 """Run the same negative case against a copy with the acceptance guard deleted."""
38 source = pathlib.Path(__file__).read_text()
39 guard = ' return actual == expected\n'
40 assert source.count(guard) == 1, 'INVALID guard-removal setup'
41 mutant = pathlib.Path(out) / 'comparison-deleted.py'
42 mutant.write_text(source.replace(guard, ' return True\n'))
43 cmd = [sys.executable, '-I', str(mutant), 'reject', label, kind, str(actual), str(expected)]
44 p = subprocess.run(cmd, capture_output=True, timeout=10)
45 log = pathlib.Path(out) / ('guard-removal-' + label + '.log')
46 log.write_text('command=' + repr(cmd) + '\nexit=' + str(p.returncode) + '\n'
47 + p.stdout.decode() + p.stderr.decode())
48 assert p.returncode == 1 and ('CONTROL ESCAPED ' + label).encode() in p.stderr, ('INVALID or escaped guard mutant', label, p.returncode)
49 assert b'CONTROL CAUGHT' not in p.stdout, ('guard mutant printed CAUGHT', label)
50 reject(label, kind, actual, expected)
51 print('GUARD-REMOVAL PASS ' + label + ' mutant_exit=1 log=' + str(log))
52
53
54if __name__ == '__main__':
55 try:
56 mode, *args = sys.argv[1:]
57 if mode == 'compare':
58 sys.exit(0 if matches(*args) else 1)
59 elif mode == 'reject':
60 reject(*args)
61 elif mode == 'prove':
62 prove(*args)
63 else:
64 raise ValueError('unknown mode ' + mode)
65 except (AssertionError, ValueError, OSError, subprocess.SubprocessError) as exc:
66 print(str(exc), file=sys.stderr)
67 sys.exit(1)