Files
nvdriver/tools/run_native_hello.py
morebrackets 09029607c1 tools: harden capture_stock/replay_live, run_native_hello, and bringup_verdict
- tools/ioctl_capture/capture_stock.sh: strictly validate lowercase PCI BDF and sysfs existence, pass BDF via positional arg to sh -c (no shell interpolation), reject path traversal and missing commands in run, link shim with -pthread, create nested output dirs lazily, propagate workload exit code instead of always returning 0, check dev_reload.sh status, and clean all /dev/nvidia[0-9]* nodes on restore.
- tools/ioctl_capture/replay_live.sh: reject unknown CLI flags (exit 2) and verify captures/golden/stock-620.73.04.json exists before running.
- tools/run_native_hello.py: wrap CLI in main(), handle unbound PCI devices cleanly in binding(), verify binary/cubin and libcuda.so.1 ldd resolution, and catch missing trace/strace/invalid JSON stdout cleanly.
- tools/bringup_verdict.py: validate 32-bit hex note values instead of crashing with ValueError, and reject 0xffffffff PCIe bus-error readbacks in sec2_stop and block_1704_after_bind.
- tests/offline/test_capture_hello_and_verdict_tools.py: 8 offline unit tests covering all 4 tools.
2026-12-02 02:36:28 -08:00

118 lines
5.1 KiB
Python

#!/usr/bin/env python3
"""Run a native CUDA acceptance workload, preserving its actual PCI driver.
The original-driver baseline is deliberately not replacement-driver evidence.
No driver binding or system configuration is changed by this runner.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
ROOT = Path(__file__).resolve().parents[1]
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def binding(device):
try:
return (device / "driver").resolve(strict=True).name
except OSError:
return None
def resolve_libcuda(linkage):
match = re.search(r"libcuda\.so\.1 => (\S+)", linkage)
if match is None or match.group(1) == "not":
return None
library = Path(match.group(1)).resolve()
return library if library.is_file() else None
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument("--pci", required=True, help="domain:bus:device.function")
parser.add_argument("--expect-pci-driver", required=True)
parser.add_argument("--label", required=True)
args = parser.parse_args(argv)
if not re.fullmatch(r"[A-Za-z0-9_-]+", args.label):
parser.error("label must contain only letters, digits, underscores or hyphens")
if not re.fullmatch(r"[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}\.[0-7]", args.pci):
parser.error("invalid PCI address")
device = Path("/sys/bus/pci/devices") / args.pci.lower()
before = binding(device)
if before != args.expect_pci_driver:
raise SystemExit(f"refusing mismatched PCI driver: {before}")
binary = ROOT / "target/hardware-hello/native-hello"
cubin = ROOT / "target/hardware-hello/native_hello.sm70.cubin"
for required in (binary, cubin):
if not required.is_file():
raise SystemExit(f"missing required artifact: {required}")
out = ROOT / "reports" / ("native-hello-" + args.label)
out.mkdir(parents=True, exist_ok=True)
linkage = subprocess.check_output(["ldd", str(binary)], text=True)
(out / "linkage.txt").write_text(linkage)
library = resolve_libcuda(linkage)
if library is None:
raise SystemExit(f"failed to resolve libcuda.so.1 from ldd output for {binary}")
artifacts = [binary, cubin, library, ROOT / "tests/hardware/native_hello.rs",
ROOT / "tests/hardware/native_hello.cu"]
report = {
"label": args.label, "pci": args.pci.lower(), "pci_driver_before": before,
"kernel": os.uname().release, "artifacts": {str(p): digest(p) for p in artifacts},
"environment": {key: os.environ.get(key) for key in
("CUDA_VISIBLE_DEVICES", "CUDA_DEVICE_ORDER")},
"nvidia_version": Path("/proc/driver/nvidia/version").read_text()
if Path("/proc/driver/nvidia/version").exists() else None,
"scope": "Native sm70 cubin execution via CUDA Driver API on the recorded driver. An original-driver pass is only a baseline.",
"full_driver_parity_proven": False, "runs": [], "passed": False,
}
for seed in (0x12F04321, 0xD86B9170):
trace = out / f"syscalls-{seed}.log"
command = ["strace", "-f", "-qq", "-s", "128", "-e",
"trace=openat,ioctl,mmap,munmap,close", "-o", str(trace),
str(binary), str(cubin), args.pci, str(seed)]
try:
result = subprocess.run(command, capture_output=True, text=True, timeout=60)
run = {"seed": seed, "exit_code": result.returncode,
"stdout": result.stdout, "stderr": result.stderr,
"command": command,
"trace_sha256": digest(trace) if trace.is_file() else None}
if result.returncode == 0:
try:
observed = json.loads(result.stdout)
except json.JSONDecodeError as error:
run["error"] = f"invalid JSON stdout: {error}"
run["passed"] = False
else:
run["observed"] = observed
run["passed"] = (isinstance(observed, dict)
and observed.get("passed") is True
and observed.get("seed") == seed
and observed.get("verified_words") == 64
and observed.get("context_destroyed") is True)
else:
run["passed"] = False
except subprocess.TimeoutExpired as error:
run = {"seed": seed, "passed": False, "timeout": True, "error": str(error)}
except OSError as error:
run = {"seed": seed, "passed": False, "error": str(error)}
report["runs"].append(run)
if not run["passed"]:
break
report["pci_driver_after"] = binding(device)
report["passed"] = (len(report["runs"]) == 2 and all(run["passed"] for run in report["runs"])
and report["pci_driver_after"] == before)
(out / "validation.json").write_text(json.dumps(report, indent=2) + "\n")
print(json.dumps(report, indent=2))
return 0 if report["passed"] else 1
if __name__ == "__main__":
raise SystemExit(main())