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 / check_session_mutants.py

100 lines · 6639 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Mutate hosted default-path Omega units and platform glue, never the C oracle.
4
5A missing/ambiguous site, converter failure, compile failure, signal or timeout
6is a harness error, NOT a killed mutant. Each kill requires rc=1 and FAIL output
7from an assertion in the same tests that pass unmodified.
8"""
9import os
10from pathlib import Path
11import shlex
12import subprocess
13import sys
14
15root = Path(__file__).resolve().parents[1]
16work = root / 'build/gpu_session_mut'
17work.mkdir(parents=True, exist_ok=True)
18cc = shlex.split(os.environ.get('CC', 'cc'))
19flags = ['-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-D_GNU_SOURCE', '-Isrc']
20objects = [p for p in shlex.split(os.environ['SESSION_FS_OBJECTS']) if not p.endswith('/omega_gpu_session.o')]
21units = {'session_admit': 'om/session/admit.om', 'session_words': 'om/session/words.om',
22 'session_park': 'om/session/park.om', 'cmdlist_book': 'om/cmdlist/book.om'}
23# name, source, exact replacements (old, new, expected occurrence count)
24mutants = [
25 ('free-while-latched', 'session_admit', [('if ne and blocked WMASK 0\nret 0', 'if ne and blocked WMASK 0\nret 2', 1)]),
26 ('open-while-latched', 'session_admit', [('fn opendec blocked ulong epoch ulong plat ulong opened ulong to ulong\nif ne and blocked WMASK 0\nret 1', 'fn opendec blocked ulong epoch ulong plat ulong opened ulong to ulong\nif ne and blocked WMASK 0\nret 0', 1)]),
27 ('recover-without-latch', 'session_admit', [('if eq and latched WMASK 0\nret 2', 'if eq and latched WMASK 0\nret 0', 1)]),
28 ('marker-payload', 'session_words', [('const MARKER1 ulong 1145324612', 'const MARKER1 ulong 0', 1)]),
29 ('parked-generation', 'session_park', [('if eq get gen i wgen', 'if ne get gen i wgen', 1)]),
30 ('stale-epoch', 'cmdlist_book', [('if eq get a 2 get a 3', 'if ne get a 2 get a 3', 1)]),
31 ('latch-generation', 'cmdlist_book', [('and add get a 1 1 WMASK', 'get a 1', 1)]),
32 ('ignore-cancel', 'shell', [('if (LOAD(g_cancel)) return WAIT_CANCELLED;', ';', 3)]),
33 ('no-latch-on-timeout', 'shell', [('latch(w == WAIT_TIMED_OUT', 'omega_gpu_session_set_error(w == WAIT_TIMED_OUT', 1)]),
34 ('stale-not-counted', 'shell', [('else { BUMP(g_stats.stale_refusals, 1u); fail_plat("dma_free", rc); }', 'else {}', 1)]),
35 ('marker-not-polled', 'shell', [('RIGHTS_MARKER, PAGE', 'RIGHTS_DATA, PAGE', 1)]),
36 ('close-while-latched', 'shell', [('if (g_open && !LOAD(g_blocked) && p) {', 'if (g_open && p) {', 1), ('if (!LOAD(g_blocked)) { /* a hook', 'if (1) { /* a hook', 1)]),
37 ('recover-keeps-latch', 'shell', [('STORE(g_blocked, 0u);', ';', 1)]),
38 ('recover-without-close', 'shell', [('const int prc = p->device_close(p->ctx, g_s.dev);', 'const int prc = OMEGA_GPU_PLAT_OK;', 1)]),
39 ('recover-ignores-teardown-fault', 'shell', [('if (prc != OMEGA_GPU_PLAT_OK) {', 'if (0) {', 1)]),
40 ('recover-ignores-module-latch', 'shell', [('const int latched = LOAD(g_blocked) || LOAD(g_module_latched);', 'const int latched = LOAD(g_blocked);', 1)]),
41]
42
43def exercise(directory, unit=None, changed=None):
44 bindings = {name: str(root / ('src/cudapp_om_' + name + '.c')) for name in units}
45 shell = 'src/omega_gpu_session.c'
46 with (directory / 'build.log').open('w') as log:
47 if unit == 'shell':
48 shell = str(changed)
49 elif unit is not None:
50 env = dict(os.environ, OMEGA_UNITS=unit + '=' + str(changed), OMEGA_UNITS_DEST=str(directory))
51 subprocess.run(['sh', 'om/units.sh'], cwd=root, env=env, stdout=log, stderr=log, check=True)
52 bindings[unit] = str(directory / ('cudapp_om_' + unit + '.c'))
53 subprocess.run(cc + flags + ['-c', shell, '-o', str(directory/'session.o')], cwd=root, stdout=log, stderr=log, check=True)
54 subprocess.run(cc + flags + ['-DOMEGA_GPU_NO_POSIX', '-Ibuild/no_posix/native', 'tests/test_omega_gpu_session_platform.c',
55 'tests/session_no_default.c', 'src/omega_ampere_qmd.c', 'src/cudapp_om_amp_runtime.c',
56 'src/cudapp_om_streams.c', str(directory/'session.o')] + objects +
57 [bindings[n] for n in ('session_admit', 'session_words', 'session_park')] +
58 ['-lpthread', '-o', str(directory/'platform')], cwd=root, stdout=log, stderr=log, check=True)
59 subprocess.run(cc + flags + ['tests/session_policy_test.c', bindings['session_admit'], bindings['cmdlist_book'],
60 '-o', str(directory/'policy')], cwd=root, stdout=log, stderr=log, check=True)
61 results = []
62 for test in ('platform', 'policy'):
63 run = subprocess.run([str(directory/test)], cwd=root, stdout=subprocess.PIPE,
64 stderr=subprocess.STDOUT, text=True, timeout=60)
65 (directory/(test+'.log')).write_text(run.stdout)
66 if run.returncode not in (0, 1) or (run.returncode == 1 and 'FAIL' not in run.stdout):
67 sys.exit('FAIL harness: ' + str(directory) + '/' + test + '\n' + run.stdout)
68 results.append(run.returncode)
69 return results
70
71baseline = work/'baseline'
72baseline.mkdir(exist_ok=True)
73if any(exercise(baseline)):
74 sys.exit('FAIL unmodified hosted default path; see ' + str(baseline))
75print('PASS unmodified hosted Omega session and policy boundaries', flush=True)
76# Unit mutants regenerate .om through om/units.sh, which executes the pinned
77# AArch64 omc. make sets SESSION_UNIT_MUTANTS=1 only where that compiler runs
78# (CUDAPP_OMC_HOST); elsewhere those mutants are reported NOT_RUN, never killed.
79run_units = os.environ['SESSION_UNIT_MUTANTS'] == '1'
80killed = not_run = 0
81for name, unit, replacements in mutants:
82 directory = work/name
83 directory.mkdir(exist_ok=True)
84 source = root / ('src/omega_gpu_session.c' if unit == 'shell' else units[unit])
85 text = source.read_text()
86 for old, new, count in replacements:
87 if text.count(old) != count:
88 sys.exit('FAIL harness: missing/ambiguous mutant site ' + name)
89 text = text.replace(old, new)
90 if unit != 'shell' and not run_units:
91 print('NOT_RUN Omega-unit mutant: ' + name + ' [' + unit + '] (requires the pinned AArch64 omc)', flush=True)
92 not_run += 1
93 continue
94 changed = directory/source.name
95 changed.write_text(text)
96 if not any(exercise(directory, unit, changed)):
97 sys.exit('FAIL mutant SURVIVED: ' + name)
98 print('PASS behavioral mutant killed: ' + name + ' [' + unit + ']', flush=True)
99 killed += 1
100print('test-gpu-session-mutants: %d of %d mutants killed; %d Omega-unit mutants NOT_RUN' % (killed, killed, not_run))