#!/home/geodd/.local/opt/rea-python/bin/python """Execute original deferred service and queue pop instructions with handler mocks.""" import hashlib,json,struct,warnings from pathlib import Path warnings.filterwarnings('ignore',category=UserWarning,module='unicorn') from elftools.elf.elffile import ELFFile from unicorn import Uc,UC_ARCH_X86,UC_MODE_64,UC_HOOK_CODE from unicorn.x86_const import * ROOT=Path(__file__).resolve().parents[1] ARTIFACT=Path('/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary') TEXT=0x10000000;DATA=0x30000000;STACK=0x40000000;STUB=0x50000000;STOP=0x60000000 GPU=DATA;MMU=DATA+0x10000;PACKET=DATA+0x20000;PARSED=DATA+0x21000 names=['_nv040870rm','_nv040954rm','_nv040810rm','_nv040826rm'];hooks={} with ARTIFACT.open('rb') as stream: elf=ELFFile(stream);symbols=list(elf.get_section_by_name('.symtab').iter_symbols());defined={s.name:s for s in symbols};text=bytearray(elf.get_section_by_name('.text').data());ti=elf.get_section_index('.text');functions={name:(defined[name]['st_value'],defined[name]['st_size']) for name in names};undef={} for rel in elf.get_section_by_name('.rela.text').iter_relocations(): offset=rel['r_offset'] if not any(start<=offset15 else poison_status if fault_type==14 and poison_enabled and poison_status else service_status assert status==expected,(fault_type,status,expected) assert any(e['event']=='service' for e in events)==(fault_type<16) assert any(e['event']=='poison_event' for e in events)==(fault_type==14 and poison_enabled) handler_rows.append({'fault_type':fault_type,'service_status':service_status,'poison_enabled':poison_enabled,'poison_status':poison_status,'status':status,'packet':packet,'events':events.copy()}) report={'passed':True,'artifact_sha256':hashlib.sha256(ARTIFACT.read_bytes()).hexdigest(),'parser_cases':len(rows),'handler_cases':handler_rows,'scope':'Original parser, selected sign extension/fault type and handler instructions; OS CPU shift, print, service/RC and poison callbacks mocked. No hardware/reset/notification success claimed. Defined parsed fields and all initialized exception bytes compared.'} (ROOT/'reports/fault-decode-binary.json').write_text(json.dumps(report,indent=2)+'\n') lines=['// Original machine instructions; mocks and padding limits in report.','pub type Vector = (u8,u8,u64,[u32;8],u32,[u8;64]);','#[rustfmt::skip]','pub const VECTORS:&[Vector]=&['] for arch,cpu,maxva,packet,status,parsed in rows:lines.append('(%d,%d,%d,%s,%d,%s),'%(arch,cpu,maxva,str(packet),status,str(parsed))) lines.append('];') lines+=['pub type HandlerVector = ([u32;8],u32,bool,u32,u32,[u32;22]);','#[rustfmt::skip]','pub const HANDLERS:&[HandlerVector]=&['] for row in handler_rows: exception=next((e['exception'] for e in row['events'] if e['event']=='print'),bytes(88).hex());words=struct.unpack('<22I',bytes.fromhex(exception)) lines.append('(%s,%d,%s,%d,%d,%s),'%(str(row['packet']),row['service_status'],str(row['poison_enabled']).lower(),row['poison_status'],row['status'],str(list(words)))) lines.append('];');(ROOT/'src/nvidia/src/mmu/fault_decode_vectors.rs').write_text('\n'.join(lines)+'\n') print(len(rows),'original parser cases;',len(handler_rows),'handler cases passed')