tools: validate native shader receipts across checkout paths (#111)

This commit is contained in:
Rudy Mizrahi Celekli
2026-12-01 11:25:40 -06:00
committed by GitHub
parent 4d2ff2339e
commit d236e8be31
2 changed files with 113 additions and 5 deletions

View File

@ -0,0 +1,84 @@
"""Exercise the native shader receipt checks with files in a relocated checkout."""
import contextlib
import io
import json
from pathlib import Path
import runpy
import shutil
import sys
import tempfile
import types
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
SCRIPT = "tools/check_native_shader.py"
CHECKPOINT = Path("reports/checkpoints/native-hello-baseline-2da2fe783377")
RECEIPT = CHECKPOINT / "reports/native-hello-original-580/validation.json"
CUBIN = CHECKPOINT / "target/hardware-hello/native_hello.sm70.cubin"
SOURCE = Path("tests/hardware/native_hello.cu")
EMBEDDED = Path("src/nvidia/src/compute/native_hello_sm70.bin")
class ReceiptTests(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name) / "different checkout"
for fixture in (SCRIPT, RECEIPT, CUBIN, SOURCE, EMBEDDED):
destination = self.root / fixture
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(ROOT / fixture, destination)
self.receipt = json.loads((self.root / RECEIPT).read_text())
def audit(self):
def elf_fixture(_stream):
# Receipt checks are independent of ELF parsing. Stub that
# optional dependency while keeping the real file hashes.
embedded = (self.root / EMBEDDED).read_bytes()
class TextSection(dict):
def data(self):
return embedded
sections = {
".text.native_hello": TextSection(sh_addralign=128, sh_info=8 << 24),
".nv.constant0.native_hello": types.SimpleNamespace(data=lambda: bytes(368)),
}
return types.SimpleNamespace(get_section_by_name=sections.get, iter_sections=lambda: [])
(self.root / RECEIPT).write_text(json.dumps(self.receipt))
module = types.SimpleNamespace(ELFFile=elf_fixture)
with (
patch.dict(sys.modules, {"elftools.elf.elffile": module}),
contextlib.redirect_stdout(io.StringIO()),
):
runpy.run_path(str(self.root / SCRIPT), run_name="__main__")
return json.loads((self.root / "reports/native-shader-baseline-check.json").read_text())
def test_archive_receipt_survives_checkout_relocation(self):
self.assertIs(self.audit()["passed"], True)
def use_local_source_path(self):
artifacts = self.receipt["artifacts"]
source_key = next(path for path in artifacts if path.endswith(str(SOURCE)))
artifacts[str((self.root / SOURCE).resolve())] = artifacts.pop(source_key)
def test_receipt_without_runs_cannot_pass(self):
self.use_local_source_path()
self.receipt["runs"] = []
with self.assertRaisesRegex(SystemExit, "runs are missing or incomplete"):
self.audit()
def test_receipt_must_identify_the_same_cubin(self):
self.use_local_source_path()
artifacts = self.receipt["artifacts"]
cubin_key = next(path for path in artifacts if path.endswith(CUBIN.name))
artifacts[cubin_key] = "0" * 64
with self.assertRaisesRegex(SystemExit, "identifies a different cubin"):
self.audit()
if __name__ == "__main__":
unittest.main()

View File

@ -2,7 +2,7 @@
"""Bind the embedded shader to the immutable original-driver execution baseline."""
import hashlib
import json
from pathlib import Path
from pathlib import Path, PurePosixPath
from elftools.elf.elffile import ELFFile
ROOT = Path(__file__).resolve().parents[1]
@ -11,10 +11,34 @@ 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())
assert record['passed'] and record['pci_driver_before'] == 'nvidia'
assert all(run['observed']['verified_words'] == 64 for run in record['runs'])
source = ROOT / 'tests/hardware/native_hello.cu'
assert hashlib.sha256(source.read_bytes()).hexdigest() == record['artifacts'][str(source)]
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')