Files
nvdriver/tools/check_native_shader.py

59 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""Bind the embedded shader to the immutable original-driver execution baseline."""
import hashlib
import json
from pathlib import Path, PurePosixPath
from elftools.elf.elffile import ELFFile
ROOT = Path(__file__).resolve().parents[1]
BASELINE = ROOT / 'reports/checkpoints/native-hello-baseline-2da2fe783377'
cubin = BASELINE / 'target/hardware-hello/native_hello.sm70.cubin'
expected = '2da2fe78337788e5c8103837255d3dd79fe248ae26a3d4f51b5f3e6e05ccb7ac'
assert hashlib.sha256(cubin.read_bytes()).hexdigest() == expected
record = json.loads((BASELINE / 'reports/native-hello-original-580/validation.json').read_text())
if record['passed'] is not True or record['pci_driver_before'] != 'nvidia':
raise SystemExit('archived original-driver validation did not pass')
if not record['runs'] or any(
run['observed']['passed'] is not True or run['observed']['verified_words'] != 64
for run in record['runs']
):
raise SystemExit('archived original-driver runs are missing or incomplete')
def recorded_digest(relative_path):
# The original archive stored absolute paths on its evidence host.
expected_suffix = PurePosixPath(relative_path).parts
matches = [
digest for path, digest in record['artifacts'].items()
if PurePosixPath(path).parts[-len(expected_suffix):] == expected_suffix
]
if len(matches) != 1:
raise SystemExit(f'expected one archived artifact entry for {relative_path}')
return matches[0]
cubin_path = cubin.relative_to(BASELINE).as_posix()
if recorded_digest(cubin_path) != expected:
raise SystemExit('archived validation identifies a different cubin')
source_path = 'tests/hardware/native_hello.cu'
source = ROOT / source_path
if hashlib.sha256(source.read_bytes()).hexdigest() != recorded_digest(source_path):
raise SystemExit('native shader source differs from archived validation')
with cubin.open('rb') as stream:
elf = ELFFile(stream)
text = elf.get_section_by_name('.text.native_hello')
code = text.data()
assert len(code) == 256 and text['sh_addralign'] == 128
assert text['sh_info'] >> 24 == 8
constant = elf.get_section_by_name('.nv.constant0.native_hello').data()
assert len(constant) == 368 and not any(constant)
assert not any(section.name.startswith(('.rel.text.native_hello', '.rela.text.native_hello')) for section in elf.iter_sections())
embedded = ROOT / 'src/nvidia/src/compute/native_hello_sm70.bin'
assert embedded.read_bytes() == code
result = {'passed': True, 'baseline_cubin_sha256': expected,
'code_sha256': hashlib.sha256(code).hexdigest(),
'code_bytes': len(code), 'constant0_bytes': len(constant), 'registers': 8,
'scope': 'Exact shader bytes from recorded original-driver execution. No replacement QMD, firmware, initialization or physical GPU execution is established by this check.'}
(ROOT / 'reports/native-shader-baseline-check.json').write_text(json.dumps(result, indent=2) + '\n')
print(json.dumps(result))