mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
23 lines
2.5 KiB
Python
23 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Compare captured runtime code to modulefile at existing PT decode errors.
|
|
Reads only cached code, never /proc/kcore; does not decode/capture or mutateperf.
|
|
"""
|
|
from pathlib import Path
|
|
from elftools.elf.elffile import ELFFile
|
|
import json,re,argparse
|
|
p=argparse.ArgumentParser();p.add_argument('--cache',type=Path,required=True);p.add_argument('--errors',type=Path,required=True);p.add_argument('--output',type=Path,required=True);a=p.parse_args()
|
|
M=Path('/lib/modules/5.15.0-202-generic/updates/dkms/nvidia.ko');C=a.cache
|
|
moduleline=next(x for x in(C/'modules').read_text().splitlines()if x.split()[0]=='nvidia');textbase=int(moduleline.split()[5],16)
|
|
with M.open('rb')as f:
|
|
e=ELFFile(f);text=e.get_section_by_name('.text').data();textindex=e.get_section_index('.text');symtab=e.get_section_by_name('.symtab');relocs=[{'offset':r['r_offset'],'type':r['r_info_type'],'symbol':symtab.get_symbol(r['r_info_sym']).name}for r in e.get_section_by_name('.rela.text').iter_relocations()];syms=sorted((s['st_value'],s.name)for s in symtab.iter_symbols()if s['st_shndx']==textindex and s['st_info']['type']=='STT_FUNC')
|
|
with(C/'kcore').open('rb')as f:
|
|
e=ELFFile(f);segments=[p for p in e.iter_segments()if p['p_type']=='PT_LOAD'];rows=[]
|
|
for line in a.errors.read_text().splitlines()[:12]:
|
|
match=re.search(r' ip (0x[0-9a-f]+)',line)
|
|
if not match:continue
|
|
ip=int(match[1],16);ph=next((p for p in segments if p['p_vaddr']<=ip and ip+16<=p['p_vaddr']+p['p_filesz']),None)
|
|
if ph is None:rows.append({'ip':hex(ip),'cached_code_present':False});continue
|
|
f.seek(ph['p_offset']+ip-ph['p_vaddr']);runtime=f.read(16);offset=ip-textbase;disk=text[offset:offset+16];near=max((s for s in syms if s[0]<=offset),default=(0,''));near_reloc=[{'relative_to_ip':r['offset']-offset,'type':r['type'],'symbol':r['symbol']}for r in relocs if offset-8<=r['offset']<offset+16]
|
|
rows.append({'ip':hex(ip),'module_offset':hex(offset),'symbol':near[1],'symbol_offset':hex(offset-near[0]),'cached_code_present':True,'runtime_hex':runtime.hex(),'installed_file_hex':disk.hex(),'equal':runtime==disk,'near_relocations':near_reloc})
|
|
report={'read_only':True,'cache_dir':str(C),'module_line':moduleline,'module_text_base':hex(textbase),'initial_mismatch_code_comparisons':rows,'claim':'Read-only cached runtime instruction comparison; no decoded execution sequence or call hits validated by this comparison.','tracing_changed':False};a.output.write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(rows[:3],indent=2))
|