#!/usr/bin/env python3 """Inspect retained real constructors under production flags. Never load code.""" import hashlib import json from pathlib import Path import re import subprocess ROOT = Path(__file__).resolve().parents[1] obj = ROOT / 'target/codegen/audit.o' def run(*args): return subprocess.check_output(args, text=True) assembly = run('objdump', '-drC', str(obj)) relocations = run('readelf', '-rW', str(obj)) sections = run('readelf', '-SW', str(obj)) symbols = run('nm', '--defined-only', str(obj)) 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 required = ['SMALL_VM_PREPARE', 'SMALL_VM_FREEZE', 'SMALL_VM_CLOSE', 'NATIVE_POPULATE', 'QMD', 'CONSTANTS', 'PUSHBUFFER'] checks = { 'actual_constructor_and_payload_roots_present': all('NV_RS_AUDIT_' + name in symbols for name in required), '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': hashlib.sha256(obj.read_bytes()).hexdigest(), 'undefined_symbols': run('nm', '-u', str(obj)).splitlines(), 'scope': 'Compile-only retained production SmallVm prepare/freeze/close and native payload composition/writers, 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.'} (ROOT / 'reports/native-path-codegen-validation.json').write_text(json.dumps(report, indent=2) + '\n') print(json.dumps({'passed': report['passed'], 'checks': checks, 'largest_functions': report['largest_functions'][:4]})) raise SystemExit(0 if report['passed'] else 1)