Files
nvdriver/tools/check_vram_size_binary.py

65 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""Original selected format/levels/default getter and complete BAR2 instance bytes.
Private Unicorn execution only. Address/aperture/volatility getters are explicit
owned-descriptor models; no native driver, MMIO, root bind or TLB runs.
"""
import argparse,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 *
parser=argparse.ArgumentParser();parser.add_argument('--check',action='store_true');options=parser.parse_args()
R=Path(__file__).resolve().parents[1]
P=Path('/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary')
TEXT=0x10000000;RO=0x18000000;DATA=0x30000000;STACK=0x40000000;STUB=0x50000000;STOP=0x60000000
GPU=DATA;MMU=DATA+0x10000;FAMILY=DATA+0x20000;BUS=DATA+0x30000;INSTANCE=DATA+0x40000;LEVELS=DATA+0x50000;DESC=DATA+0x60000;FMT64=DATA+0x70000;FMT128=DATA+0x71000
names=['_nv041679rm'];hooks={}
with P.open('rb') as stream:
elf=ELFFile(stream);syms=list(elf.get_section_by_name('.symtab').iter_symbols());defined={s.name:s for s in syms}
text=bytearray(elf.get_section_by_name('.text').data());ro=elf.get_section_by_name('.rodata').data();ti=elf.get_section_index('.text');ri=elf.get_section_index('.rodata')
functions={n:(defined[n]['st_value'],defined[n]['st_size']) for n in names};undefined={}
for rel in elf.get_section_by_name('.rela.text').iter_relocations():
offset=rel['r_offset']
if not any(start<=offset<start+size for start,size in functions.values()):continue
symbol=syms[rel['r_info_sym']];kind=rel['r_info_type']
if symbol['st_shndx']==ti:target=TEXT+symbol['st_value']
elif symbol['st_shndx']==ri:target=RO+symbol['st_value']
elif symbol['st_shndx']=='SHN_UNDEF':
target=undefined.setdefault(symbol.name,STUB+0x100+len(undefined)*16);hooks[target]=symbol.name
else:target=STUB+0x8000
assert kind in(2,4,11)
value=target+rel['r_addend']-(TEXT+offset) if kind in(2,4) else target+rel['r_addend'];struct.pack_into('<i',text,offset,value)
hooks[TEXT+defined['_nv053907rm']['st_value']]='read'
u=Uc(UC_ARCH_X86,UC_MODE_64)
for base,size in [(TEXT,len(text)),(RO,len(ro)),(DATA,0x80000),(STACK,0x10000),(STUB,0x10000),(STOP,4096)]:u.mem_map(base,(size+4095)&~4095)
u.mem_write(TEXT,bytes(text));u.mem_write(RO,ro)
OUT=DATA+0x70000
rows=[]
def put(a,v):u.mem_write(a,struct.pack('<Q',v))
def hook(_,address,size,user):
if address not in hooks:return
assert u.reg_read(UC_X86_REG_RCX)==0x100ce0
sp=u.reg_read(UC_X86_REG_RSP);ret=struct.unpack('<Q',u.mem_read(sp,8))[0]
u.reg_write(UC_X86_REG_RAX,raw);u.reg_write(UC_X86_REG_RSP,sp+8);u.reg_write(UC_X86_REG_RIP,ret)
u.hook_add(UC_HOOK_CODE,hook)
for scale in range(16):
for magnitude in range(64):
for ecc in (0,1):
raw=0x85555400|(magnitude<<4)|scale|(ecc<<30)
u.reg_write(UC_X86_REG_RDI,GPU);u.reg_write(UC_X86_REG_RSI,MMU);u.reg_write(UC_X86_REG_RDX,OUT)
u.reg_write(UC_X86_REG_RSP,STACK+0xfff8);put(STACK+0xfff8,STOP);u.reg_write(UC_X86_REG_RBP,STACK+0x8000)
u.emu_start(TEXT+defined['_nv041679rm']['st_value'],STOP,count=1000)
assert u.reg_read(UC_X86_REG_RIP)==STOP and u.reg_read(UC_X86_REG_RAX)==0
result=struct.unpack('<Q',u.mem_read(OUT,8))[0];rows.append((raw,result))
code='// Original580041679 LOCAL_MEMORY_RANGE decode: all64magnitudes/16scales/ECC.\npub const VECTORS:&[(u32,u64)]=&[\n'
for raw,result in rows:code+=f' (0x{raw:x},0x{result:x}),\n'
code+='];\n'
report={'passed':True,'source_sha256':hashlib.sha256(P.read_bytes()).hexdigest(),'function':'_nv041679rm','cases':len(rows),'scope':'Original isolated machine instructions for every field combination. GPU read is a synthetic register value; no physicalGPU access, initializedmemory ownership or allocator proof.'}
for path,data in [(R/'src/nvidia/src/mmu/vram_size_vectors.rs',code),(R/'reports/native-vram-size-oracle.json',json.dumps(report,indent=2)+'\n')]:
if options.check:assert path.read_text()==data,'Stale '+str(path)
else:path.write_text(data)
print(len(rows),'original framebuffer-size vectors')