diff --git a/abi/hardware/provenance.json b/abi/hardware/provenance.json index a2bf5e59..1ffb6d15 100644 --- a/abi/hardware/provenance.json +++ b/abi/hardware/provenance.json @@ -14,7 +14,7 @@ "fixtures": { "chip-tuples.json": "da500258801cbd7be5d7a7ce66ab295743e324624a929d0f3507763983f53a64", "pci-blacklist.json": "1e31a889e49eb8b20a361b7728ccda1a70a7716739cf5dfda2d0537ec1496c6b", - "product-matrix.json": "23204deceaef74014aa9f4c79671045f9f13b3b7abbbf261f0115a72759b20e2" + "product-matrix.json": "1dd424c2e0706f286980cd5cd80ab81b69acefaaafe09e8822c41ddb689137a8" }, "evidence": "analysis-580.178.04/reports/deep-hardware-init; exact original580.178.04 binary, not open-driver inferred replacement" } diff --git a/tests/offline/test_generate_hardware.py b/tests/offline/test_generate_hardware.py new file mode 100644 index 00000000..783ebeae --- /dev/null +++ b/tests/offline/test_generate_hardware.py @@ -0,0 +1,38 @@ +import shutil +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[2] + + +class HardwareTableCheckTests(unittest.TestCase): + def test_optimized_python_checks_fixtures_and_output(self): + with tempfile.TemporaryDirectory() as td: + root = Path(td) + tool = root / "tools/generate_hardware.py" + tool.parent.mkdir() + shutil.copyfile(ROOT / "tools/generate_hardware.py", tool) + fixtures = root / "abi/hardware" + shutil.copytree(ROOT / "abi/hardware", fixtures) + table = root / "src/nvidia/src/hardware/generated.rs" + table.parent.mkdir(parents=True) + shutil.copyfile(ROOT / table.relative_to(root), table) + + command = [sys.executable, "-O", str(tool), "--check"] + subprocess.run(command, check=True, capture_output=True) + for file in (table, fixtures / "chip-tuples.json"): + with self.subTest(file=file.name): + original = file.read_bytes() + file.write_bytes(original + b"\n") + result = subprocess.run(command, capture_output=True, text=True) + file.write_bytes(original) + self.assertNotEqual(result.returncode, 0) + self.assertIn(file.name, result.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/generate_hardware.py b/tools/generate_hardware.py index 70c3cad4..18d7622e 100644 --- a/tools/generate_hardware.py +++ b/tools/generate_hardware.py @@ -3,9 +3,15 @@ from pathlib import Path import hashlib,json,sys R=Path(__file__).resolve().parents[1];D=R/'abi/hardware';p=json.loads((D/'provenance.json').read_text()) -assert p['original_elf_sha256']=='5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b' -for name,digest in p['fixtures'].items():assert hashlib.sha256((D/name).read_bytes()).hexdigest()==digest,name -matrix=json.loads((D/'product-matrix.json').read_text());rows=matrix['rows'];assert len(rows)==1129;assert len({r["devid"] for r in rows})==450 +if p['original_elf_sha256'] != '5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b': + sys.exit("unexpected original ELF digest in provenance.json") +for name, digest in p['fixtures'].items(): + if hashlib.sha256((D/name).read_bytes()).hexdigest() != digest: + sys.exit(f"fixture digest mismatch: {name}") +matrix=json.loads((D/'product-matrix.json').read_text()) +rows=matrix['rows'] +if len(rows) != 1129 or len({row["devid"] for row in rows}) != 450: + sys.exit("unexpected product or device ID count in product-matrix.json") q=lambda x:json.dumps(x,ensure_ascii=True) lines=['// Generated by tools/generate_hardware.py; do not edit.','use super::{Product, Route, Legacy, ChipTuple};','pub static PRODUCTS: [Product; 1129] = ['] for r in rows: @@ -17,6 +23,9 @@ for r in json.loads((D/'pci-blacklist.json').read_text()):lines.append(f'Legacy lines+= ['];','pub static CHIPS: [ChipTuple; 113] = ['] for r in json.loads((D/'chip-tuples.json').read_text())['tuples']:lines.append(f'ChipTuple {{ architecture: {r["architecture"]}, implementation: {r["implementation"]}, soc: {r["soc_combined"]} }},') lines+= ['];'];out='\n'.join(lines)+'\n';dest=R/'src/nvidia/src/hardware/generated.rs' -if '--check' in sys.argv:assert dest.read_text()==out,'generated hardware tables stale' -else:dest.write_text(out) +if '--check' in sys.argv: + if dest.read_text() != out: + sys.exit(f"generated hardware tables stale: {dest.name}") +else: + dest.write_text(out) print('hardware tables verified:1129products,450IDs,1388blacklist records,113chip tuples')