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

80 lines · 4788 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Verification oracle only: vendor layout probe and real Omega source mutants."""
4import hashlib
5import os
6from pathlib import Path
7import shlex
8import subprocess
9import tempfile
10
11ROOT = Path(__file__).resolve().parents[1]
12# Default to the vendored 580.173.02 headers the Makefile builds against; NVRM_VENDOR still overrides.
13VENDOR = Path(os.environ.get("NVRM_VENDOR", str(ROOT / "third_party/nvidia-open-580.173.02")))
14BRIDGE = Path(os.environ["OMEGA_BRIDGE"])
15OMC = Path(os.environ["OMEGA_OMC"])
16# Pins come from om/units.sh, the one place they are recorded (a copied toc.om pin went stale at the bridge repin).
17UNITS_PIN = dict(line.split("=", 1) for line in (ROOT / "om/units.sh").read_text().splitlines()
18 if line.split("=", 1)[0] in ("OMC_PIN", "CONVERTER_PIN", "FRONTEND_PIN"))
19PINS = [(OMC, UNITS_PIN["OMC_PIN"]),
20 (BRIDGE / "src/toc.om", UNITS_PIN["CONVERTER_PIN"]),
21 (BRIDGE / "tools/bridge-seeded", UNITS_PIN["FRONTEND_PIN"])]
22for path, sha in PINS:
23 if hashlib.sha256(path.read_bytes()).hexdigest() != sha:
24 raise SystemExit(f"NOT_RUN unpinned verification input {path}")
25if Path("/dev/nvidiactl").exists():
26 raise SystemExit("REFUSED: tests require isolated /dev")
27INCS = [ROOT / "src", ROOT / "nvrm"] + [VENDOR / p for p in (
28 "src/common/sdk/nvidia/inc", "kernel-open/common/inc", "kernel-open/nvidia-uvm", "src/nvidia/arch/nvalloc/unix/include")]
29CC = shlex.split(os.environ.get("CC", "cc")) + ["-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror"] + ["-I" + str(p) for p in INCS]
30ENV = dict(os.environ, OMEGA_SEEDED_OMC=str(OMC))
31
32def run(cmd, **kw):
33 return subprocess.run([str(c) for c in cmd], check=True, timeout=120, **kw)
34
35# Each mutant changes one real semantic site; compilation errors do not count.
36MUTANTS = [
37 ("gpu-block-size", "if eq k 5\nret 564\n", "if eq k 5\nret 8\n"),
38 ("card-table-size", "ret 4608\n", "ret 4607\n"),
39 ("device-mode-offset", "ret 48\n", "ret 44\n"),
40 ("vas-base-offset", "ret 40\nend\nif eq k 9", "ret 32\nend\nif eq k 9"),
41 ("gid-data-offset", "ret add 12 sub f 3", "ret add 8 sub f 3"),
42 ("memory-limit-offset", "ret 88\n", "ret 80\n"),
43 ("channel-engine-offset", "ret 128\nend\nif eq k 12", "ret 124\nend\nif eq k 12"),
44 ("card-index-reversed", "if ge idx 64", "if lt idx 64"),
45 ("gid-data-width", "if eq k 9\nif ge f 3\nret 1", "if eq k 9\nif ge f 3\nret 2"),
46 ("zero-fill", "for i ulong sz\nput b i 0\n", "for i ulong sz\nput b i 1\n"),
47 ("uvm-status-width", "ret ns_load b ns_off k sub ns_count k 1 4", "ret ns_load b ns_off k sub ns_count k 1 1"),
48 ("heap-free-offset", "ret ns_load b 32 8", "ret ns_load b 24 8"),
49]
50with tempfile.TemporaryDirectory(prefix="nvrm-struct-check-") as work:
51 work = Path(work)
52 probe = work / "probe"
53 run(CC + [ROOT / "tests/nvrm_struct_layout.c", "-o", probe])
54 measured = run([probe], stdout=subprocess.PIPE).stdout
55 if measured != (ROOT / "tests/nvrm_struct_layout.txt").read_bytes():
56 raise SystemExit("FAIL vendor layout probe differs from committed measurement")
57 print("NVRM STRUCT VENDOR PROBE SNAPSHOT PASS", flush=True)
58 normal = work / "test"
59 run(CC + [ROOT / "tests/nvrm_struct_test.c", ROOT / "src/cudapp_om_nvrm_struct.c", "-o", normal])
60 run([normal])
61 red = subprocess.run([str(normal), "--red"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30)
62 if red.returncode == 0 or b"FAIL byte differential" not in red.stderr:
63 raise SystemExit("FAIL red control was not caught by byte oracle")
64 print("NVRM STRUCT RED CONTROL KILLED", flush=True)
65 src = (ROOT / "native/nvrm/uvm_fields.om").read_text() + "\n" + (ROOT / "native/nvrm/struct.om").read_text() + "\n"
66 for name, before, after in MUTANTS:
67 if src.count(before) != 1:
68 raise SystemExit(f"FAIL mutant {name} site count {src.count(before)}")
69 mutated = work / (name + ".om")
70 mutated.write_text(src.replace(before, after))
71 generated = work / (name + ".c")
72 with generated.open("wb") as out:
73 run(["sh", BRIDGE / "tools/bridge-seeded", "--runtime-views", "c", mutated], env=ENV, stdout=out)
74 exe = work / name
75 run(CC + [ROOT / "tests/nvrm_struct_test.c", generated, "-o", exe])
76 result = subprocess.run([str(exe)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30)
77 if result.returncode != 1 or b"FAIL " not in result.stderr:
78 raise SystemExit(f"FAIL mutant {name} did not trigger oracle assertion (rc={result.returncode})")
79 print(f"NVRM STRUCT MUTANT KILLED {name}: {result.stderr.decode().strip()}", flush=True)
80print("NVRM STRUCT MUTANTS PASS (12 semantic mutants; HOST_ONLY)")