Files
nvdriver/tools/summarize_native_hello.py
2026-12-01 12:23:47 -05:00

83 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""Check a retained REA acceptance-workload capture and summarize observed setup.
Observed commands include discovery/optional probes; this is not a requirement
minimizer or a replay implementation. It does not execute any target.
"""
import argparse
from collections import Counter
import hashlib
import json
from pathlib import Path
import struct
def require_capture(condition, message):
if not condition:
raise SystemExit(f"Rejected capture: {message}")
parser = argparse.ArgumentParser()
parser.add_argument("capture", type=Path)
parser.add_argument("output", type=Path)
args = parser.parse_args()
evidence = json.loads(args.capture.read_text())
result = evidence["normalized_result"]
require_capture(result["state"] == "detached" and result["cleanup_error"] is None,
"capture did not detach cleanly")
require_capture(result["messages_truncated"] is False, "messages were truncated")
require_capture(all(message["type"] == "send" for message in result["messages"]),
"unexpected message type")
messages = [message["payload"] for message in result["messages"]]
require_capture(not any(any("error" in key for key in message) for message in messages),
"capture included an instrumentation error")
expected = ["cuInit", "cuDeviceGetByPCIBusId", "cuCtxCreate_v2", "cuModuleLoad",
"cuMemAlloc_v2", "cuMemsetD8_v2", "cuLaunchKernel", "cuCtxSynchronize",
"cuMemcpyDtoH_v2", "cuMemFree_v2", "cuModuleUnload", "cuCtxDestroy_v2"]
completed = [message for message in messages if message.get("kind") == "cuda_leave"]
require_capture([message["name"] for message in completed] == expected,
"completed CUDA calls differ from the expected sequence")
require_capture(all(message["result"] == 0 for message in completed),
"a CUDA call failed")
stdout = "".join(message["text"] for message in messages if message.get("kind") == "stdout")
workload = json.loads(stdout)
require_capture(workload["passed"] is True and workload["verified_words"] == 64,
"CUDA workload did not verify all 64 words")
require_capture(workload["context_destroyed"] is True, "CUDA context was not destroyed")
calls = [message for message in messages if "request" in message]
def word(data, offset):
return struct.unpack_from("<I", bytes.fromhex(data), offset)[0]
allocations = [call for call in calls if call["request"] == 0xc030462b]
controls = [call for call in calls if call["request"] == 0xc020462a]
classes = Counter(word(call["before"], 12) for call in allocations)
commands = Counter(word(call["before"], 8) for call in controls)
summary = {
"evidence_id": evidence["evidence_id"],
"capture_sha256": hashlib.sha256(args.capture.read_bytes()).hexdigest(),
"instrumentation_sha256": result["source_sha256"],
"captured_workload_passed": True, "observed_workload": workload,
"actual_cuda_module": [message for message in messages if message.get("kind") == "cuda_module"],
"cuda_calls": completed,
"device_ioctls": len(calls),
"ioctls_by_synchronous_api": dict(Counter(call["synchronous_api"] or "unattributed" for call in calls)),
"resource_classes": [{"class": hex(key), "observed_allocations": count} for key, count in classes.items()],
"control_commands": [{"command": hex(key), "observed_calls": count} for key, count in commands.items()],
"device_allocations": [call for call in allocations if word(call["before"], 12) == 0x80],
"full_replacement_parity_proven": False,
"limits": [
"This capture runs the original proprietary driver; it is not replacement execution.",
"Observed probes include other boards and optional operations, even with CUDA visibility restricted.",
"Synchronous API labels identify intercepted calls on the same thread, not arbitrary worker causality.",
"Only libc ioctl is intercepted; zero observed ioctls does not rule out direct syscalls.",
"No GPU pushbuffer, MMIO or doorbell writes were captured by this instrumentation.",
"NV0080/NV2080 zero-size wire parameters use exact580 metadata-inferred56/4-byte capture lengths.",
"Other zero-size implicit payloads remain uncaptured; payloads are capped at4096 bytes.",
],
}
args.output.write_text(json.dumps(summary, indent=2) + "\n")
print(json.dumps({"device_ioctls": len(calls), "cuda_calls_passed": len(completed),
"capture_evidence_id": evidence["evidence_id"]}))