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 / amd / receipt.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Host-test receipt admission. Historical evidence is never current-head evidence. |
| 4 | |
| 5 | Usage: receipt.py write|check ROOT OUTPUT_DIRECTORY |
| 6 | All tracked backend and gate inputs are included, not a hand-maintained file list. |
| 7 | The compiler is external: its independently enforced pin stays in the gate log, |
| 8 | not an unresolvable absolute pathname in inputs.sha256. |
| 9 | """ |
| 10 | import hashlib |
| 11 | import pathlib |
| 12 | import re |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | def git(root, *args): |
| 18 | return subprocess.check_output(['git', '-C', str(root), *args]) |
| 19 | |
| 20 | |
| 21 | def inputs(root): |
| 22 | paths = git(root, 'ls-tree', '-r', '--name-only', 'HEAD', '--', |
| 23 | 'backend', 'tests/amd', 'tests/amd-kfd', 'tests/amd-exec', |
| 24 | 'tests/multitarget', 'Makefile').decode().splitlines() |
| 25 | assert paths, 'empty committed input set' |
| 26 | result = {} |
| 27 | for path in paths: |
| 28 | data = git(root, 'show', 'HEAD:' + path) |
| 29 | digest = hashlib.sha256(data).hexdigest() |
| 30 | actual = root / path |
| 31 | assert not actual.is_symlink(), f'symlink input: {path}' |
| 32 | assert actual.read_bytes() == data, f'dirty input: {path}' |
| 33 | result[path] = digest |
| 34 | # Untracked source can affect glob-based builds and Python imports too. |
| 35 | extra = git(root, 'ls-files', '--others', '--exclude-standard', '--', |
| 36 | 'backend', 'tests/amd', 'tests/amd-kfd', 'tests/amd-exec', |
| 37 | 'tests/multitarget').decode().splitlines() |
| 38 | assert not extra, f'untracked inputs: {extra}' |
| 39 | return result |
| 40 | |
| 41 | |
| 42 | def check(root, out, expected=None): |
| 43 | head = git(root, 'rev-parse', 'HEAD').decode().strip() |
| 44 | assert (out / 'source-head').read_text() == head + '\n', 'receipt is not this HEAD' |
| 45 | expected = inputs(root) if expected is None else expected |
| 46 | rows = {} |
| 47 | for line in (out / 'inputs.sha256').read_text().splitlines(): |
| 48 | match = re.fullmatch(r'([0-9a-f]{64}) ([^\s]+)', line) |
| 49 | assert match, f'malformed receipt line: {line!r}' |
| 50 | digest, path = match.groups() |
| 51 | assert not path.startswith('/') and '$' not in path and '\\' not in path, f'non-relative path: {path}' |
| 52 | assert all(p not in ('', '.', '..') for p in path.split('/')), f'path alias: {path}' |
| 53 | assert path not in rows, f'duplicate input: {path}' |
| 54 | rows[path] = digest |
| 55 | assert rows == expected, 'receipt differs from complete committed HEAD input bytes' |
| 56 | for path, digest in rows.items(): |
| 57 | print(f'SOURCE sha256={digest} path={path}') |
| 58 | print(f'RECEIPT PASS head={head} inputs={len(rows)} skipped=0') |
| 59 | |
| 60 | |
| 61 | def main(): |
| 62 | mode, root, out = sys.argv[1:] |
| 63 | root, out = pathlib.Path(root).resolve(), pathlib.Path(out) |
| 64 | assert mode in ('write', 'check'), mode |
| 65 | expected = inputs(root) |
| 66 | if mode == 'write': |
| 67 | # Never overwrite a receipt: an existing output must first prove this HEAD. |
| 68 | if (out / 'inputs.sha256').exists() or (out / 'source-head').exists(): |
| 69 | check(root, out, expected) |
| 70 | return |
| 71 | out.mkdir(parents=True, exist_ok=True) |
| 72 | (out / 'source-head').write_text(git(root, 'rev-parse', 'HEAD').decode()) |
| 73 | (out / 'inputs.sha256').write_text(''.join(f'{digest} {path}\n' for path, digest in expected.items())) |
| 74 | check(root, out, expected) |
| 75 | |
| 76 | |
| 77 | if __name__ == '__main__': |
| 78 | try: |
| 79 | main() |
| 80 | except (AssertionError, OSError, ValueError, subprocess.SubprocessError) as error: |
| 81 | print(f'RECEIPT FAIL {error}', file=sys.stderr) |
| 82 | sys.exit(1) |