mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 22:48:43 +00:00
126 lines
6.2 KiB
Python
126 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Inspect retained real constructors under production flags. Never load code."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
REQUIRED = [
|
|
'SMALL_VM_PREPARE', 'SMALL_VM_FREEZE', 'SMALL_VM_CLOSE', 'NATIVE_POPULATE',
|
|
'VRAM_PREPARE', 'VRAM_CLOSE', 'VRAM_CREATE', 'VRAM_ALLOCATE', 'VRAM_MAP',
|
|
'VRAM_COPY', 'VRAM_UNMAP', 'VRAM_RELEASE', 'VRAM_READ', 'VRAM_READ_LOCKED',
|
|
'VRAM_COPY_LOCKED', 'FIRMWARE_UNPACK', 'FIRMWARE_CLOSE',
|
|
'NATIVE_WPR_POLICY_READ', 'NATIVE_WPR_POLICY_CLOSE', 'NATIVE_WPR_GATHER',
|
|
'COLD_PCI_IRQ_POLICY', 'GV100_STALL_PROGRAM', 'GV100_NONSTALL_SERVICE',
|
|
'GV100_TIMER_ACK', 'GV100_FIFO_DEADLINE', 'GV100_FIFO_NAMESPACE',
|
|
'GV100_FIFO_PTIMER', 'GV100_FIFO_ROTATION', 'GV100_FIFO_WEIGHTED',
|
|
'GV100_RUNLIST_PREEMPT', 'GV100_FIFO_SWITCH', 'GV100_FIFO_CONTEXT',
|
|
'GV100_FIFO_CONTEXT_WRAPPER', 'GV100_FIFO_LPWR', 'GV100_PMU_MUTEX_ACQUIRE',
|
|
'GV100_PMU_MUTEX_RELEASE', 'GV100_LPWR_ENGINE', 'GV100_PMU_COMMAND_MEMORY',
|
|
'GV100_PMU_INIT', 'GV100_PMU_WAIT', 'GV100_PMU_STARTUP',
|
|
'GV100_PMU_STAGE_COMMAND', 'GV100_PMU_PUBLISH_COMMAND',
|
|
'GV100_PMU_SUBMIT_COMMAND', 'GV100_PMU_SUBMIT_SYNC',
|
|
'GV100_PMU_STALL_SCOPE', 'GV100_PMU_DEFERRED', 'GV100_PMU_DEFERRED_WORKER',
|
|
'GV100_STALL_PENDING', 'GV100_ENGINE_DISCOVER', 'GV100_STALL_FROM_FIFO',
|
|
'LSF_IMAGE_PREPARE', 'LSF_IMAGE_CLOSE', 'LSF_ENTRY_HEADERS',
|
|
'LSF_BOOT_DESCRIPTOR', 'SHARED_BAR2_PREPARE', 'SHARED_BAR2_CLOSE',
|
|
'SHARED_BAR2_FREEZE', 'SHARED_BAR2_BIND', 'SHARED_BAR2_RETIRE',
|
|
'SHARED_BAR2_REFERENCE', 'SHARED_BAR2_INSERT', 'SHARED_BAR2_REMOVE',
|
|
'DEVICE_SOFTWARE_PREPARE', 'TIMEOUT_FIELDS', 'TIMEOUT_COMMIT',
|
|
'TIMEOUT_INTERNAL_COMMIT', 'REGISTRY_DWORD_BASE', 'REGISTRY_DWORD_COMPLETE',
|
|
'FAULT_PREPARE_DEFAULT', 'FAULT_CLOSE', 'FAULT_RETIRE', 'QMD', 'CONSTANTS',
|
|
'PUSHBUFFER',
|
|
]
|
|
|
|
|
|
def _run(*args):
|
|
return subprocess.check_output(args, text=True, env=dict(os.environ, LC_ALL='C'))
|
|
|
|
|
|
def main(argv=None) -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--object', type=Path, default=ROOT / 'target/codegen/audit.o')
|
|
parser.add_argument('--report', type=Path, default=ROOT / 'reports/native-path-codegen-validation.json')
|
|
parser.add_argument(
|
|
'--entrypoint',
|
|
action='append',
|
|
help='Explicit alternative retained entrypoint; defaults retain every production audit root',
|
|
)
|
|
parser.add_argument('--scope')
|
|
options = parser.parse_args(argv)
|
|
obj = options.object
|
|
if not obj.is_file():
|
|
raise SystemExit(f'audit object not found: {obj}')
|
|
|
|
# A concurrent rebuild can replace the input between binutils invocations.
|
|
# Inspect and hash the same private snapshot for the entire report.
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
snapshot = Path(directory) / obj.name
|
|
shutil.copyfile(obj, snapshot)
|
|
assembly = _run('objdump', '-drC', str(snapshot))
|
|
relocations = _run('readelf', '-rW', str(snapshot))
|
|
sections = _run('readelf', '-SW', str(snapshot))
|
|
symbols = _run('nm', '--defined-only', str(snapshot))
|
|
undefined_symbols = _run('nm', '-u', str(snapshot)).splitlines()
|
|
object_sha256 = hashlib.sha256(snapshot.read_bytes()).hexdigest()
|
|
|
|
fn = ''
|
|
frames = {}
|
|
for line in assembly.splitlines():
|
|
match = re.search(r'^[0-9a-f]+ <(.+)>:$', line)
|
|
if match:
|
|
fn = match[1]
|
|
match = re.search(r'\bsub\s+\$0x([0-9a-f]+),%rsp', line)
|
|
if match:
|
|
frames[fn] = frames.get(fn, 0) + int(match[1], 16)
|
|
if re.search(r'\bpush\s', line):
|
|
frames[fn] = frames.get(fn, 0) + 8
|
|
|
|
defined_names = {parts[-1] for line in symbols.splitlines() if (parts := line.split())}
|
|
entrypoints = options.entrypoint or ['NV_RS_AUDIT_' + n for n in REQUIRED]
|
|
checks = {
|
|
'actual_constructor_and_payload_roots_present': all(name in defined_names for name in entrypoints),
|
|
'no_unsupported_GOT_relocations': 'GOT' not in relocations,
|
|
'no_SIMD_x87_registers': not re.search(r'%(?:[xyz]mm\d+|mm\d+|st(?:\(|\b))', assembly),
|
|
'bounded_fixed_function_stack': max(frames.values(), default=0) <= 1024,
|
|
'no_dynamic_RSP_subtraction': not re.search(r'\bsub\s+%[^,]+,%rsp', assembly),
|
|
'no_unwind_sections': not re.search(r'\.eh_frame|\.gcc_except_table', sections),
|
|
}
|
|
report = {
|
|
'passed': all(checks.values()),
|
|
'checks': checks,
|
|
'largest_conservative_function_stack_bytes': max(frames.values(), default=0),
|
|
'largest_functions': sorted(frames.items(), key=lambda item: item[1], reverse=True)[:20],
|
|
'object_sha256': object_sha256,
|
|
'undefined_symbols': undefined_symbols,
|
|
'scope': (
|
|
'Compile-only retained production SmallVm prepare/freeze/close and native payload '
|
|
'composition/writers plus VRAM owner methods firmware unpack/close, and native WPR '
|
|
'policy/gather and selected LSF image construction/headers/boot descriptors and '
|
|
'concrete shared BAR2 prepare/close/freeze/bind/retire/reference/insert/remove and '
|
|
'its actual fault prepare/default/close/retire, and actual canonical timeout field '
|
|
'reads/commits and named DWORD base/complete registry composition and cold PCI IRQ '
|
|
'policy and GV100 stall programming/routing and engine discovery, with utils.mk '
|
|
'production flags and no guest memcpy/memset threshold overrides. Conservative '
|
|
'per-function frame accounting is not a complete call-chain stack proof. This object '
|
|
'must never be loaded or executed.'
|
|
),
|
|
}
|
|
if options.scope:
|
|
report['scope'] = options.scope
|
|
report['required_entrypoints'] = entrypoints
|
|
options.report.parent.mkdir(parents=True, exist_ok=True)
|
|
options.report.write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
|
|
print(json.dumps({'passed': report['passed'], 'checks': checks, 'largest_functions': report['largest_functions'][:4]}))
|
|
raise SystemExit(0 if report['passed'] else 1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|