Files
nvdriver/tools/check_kernel_module.py
arabek 94e9bbfeba Nightly -Z cf-protection=branch so the Rust object gets the ENDBR objtool demands
- utils.mk RUST_KERNEL_FLAGS (x86 kernel target) gains -Z cf-protection=branch; the ARM64 branch be left alone — IBT be an x86 CET feature and no BTI was ordered.
- rust-toolchain.toml moved from pinned stable 1.100.0 to nightly, since the flag be unstable. Verified against the tool: rustc --print=codegen-options on 1.100.0 lists nothing for CF protection, and the unstable book lists none/branch/full for the LLVM backend.
- Make RUSTC explicit so the pinned toolchain is what actually runs:
  - utils.mk: RUSTC ?= $(or $(shell rustup which rustc),$(shell command -v rustc)). Resolves the toolchain pinned in rust-toolchain.toml instead of trusting PATH order; still overridable with RUSTC=..., and no hardcoded ~/.cargo path that may not exist on another box.
  - The gotcha that made this necessary is recorded in the scroll: plain rustc here be the system stable 1.100.0 and rejects -Z. Boxes without rustup still fail - honest limit, no magic.
2026-11-30 17:39:55 +01:00

19 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""Check the built diagnostic module before any load; no hardware access."""
import hashlib,json,re,subprocess
from pathlib import Path
root=Path(__file__).resolve().parents[1]
obj=root/'kernel-open/nvidia/nv-rust.o_binary';module=root/'kernel-open/nvidia_rs.ko'
def run(*args):return subprocess.check_output(args,text=True)
assembly=run('objdump','-drC',str(obj));relocs=run('readelf','-rW',str(module));sections=run('readelf','-SW',str(obj));symbols=run('nm','--defined-only',str(obj));undefined=run('nm','-u',str(module));frames={};fn='';first_instr={};entry=False
for line in assembly.splitlines():
m=re.search(r'^[0-9a-f]+ <(.+)>:$',line)
if m:fn=m[1];entry=True
if entry and re.match(r'^\s+[0-9a-f]+:\t',line):first_instr[fn]=line.split('\t')[-1].strip().split()[0];entry=False
m=re.search(r'\bsub\s+\$0x([0-9a-f]+),%rsp',line)
if m:frames[fn]=frames.get(fn,0)+int(m[1],16)
if re.search(r'\bpush\s',line):frames[fn]=frames.get(fn,0)+8
checks={'no_unresolved_private_or_Rust_runtime_symbols':not re.search(r'\b(?:nv_rs_|_RNv|_ZN|__rust_|rust_eh_personality)',undefined),'no_unsupported_GOT_relocations':'GOT'not in relocs,'no_SIMD_x87_registers':not re.search(r'%(?:[xyz]mm\d+|mm\d+|st(?:\(|\b))',assembly),'no_Rust_unwind_sections':not re.search(r'\.eh_frame|\.gcc_except_table',sections),'bounded_fixed_function_stack':max(frames.values(),default=0)<=1024,'no_dynamic_RSP_subtraction':not re.search(r'\bsub\s+%[^,]+,%rsp',assembly),'no_RSP_redzone_reference':not re.search(r'-0x[\da-f]+\(%rsp\)',assembly),'endbr64_at_every_function_entry':bool(first_instr) and all(v=='endbr64' for v in first_instr.values()),'protocol_exports_present':all('nv_rs_'+s in symbols for s in ['protocol_version','gpu_capabilities','event_selftest','gpu_prepare_owned','gpu_set_probe_identity','gpu_registry_publish','gpu_registry_retire','sysmem_prepare','sysmem_close']),'distinct_diagnostic_module_name':run('modinfo','-F','name',str(module)).strip()=='nvidia_rs','no_PCI_aliases':not run('modinfo','-F','alias',str(module)).strip()}
report={'passed':all(checks.values()),'checks':checks,'largest_conservative_function_stack_bytes':max(frames.values(),default=0),'functions_without_endbr':sum(1 for v in first_instr.values() if v!='endbr64'),'object_sha256':hashlib.sha256(obj.read_bytes()).hexdigest(),'module_sha256':hashlib.sha256(module.read_bytes()).hexdigest(),'undefined_kernel_symbols':undefined.splitlines(),'scope':'Static codegen/export checks for the current diagnostic module only. Per-function stack accounting is not a full call-chain proof; this does not prove GPU initialization, CUDA, hardware ownership or parity.'}
(root/'reports/kernel-module-codegen-validation.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps({'passed':report['passed'],'checks':checks,'largest_stack':report['largest_conservative_function_stack_bytes']}));raise SystemExit(0 if report['passed']else 1)