#!/usr/bin/env python3 """Run the original GV100 OBJMC StateLoad and write a comparison oracle. Executed original code: the class body _nv043630rm, the GV100 HAL binder _nv006001rm that populates the object's function table, and everything they reach: the HAL load, the engine-to-enable-mask translation, the reset leaf and the bit-vector helpers. Register access, the delay and diagnostics are fixture-supplied. No hardware is involved. """ import hashlib import itertools import json from pathlib import Path import struct from elftools.elf.elffile import ELFFile from unicorn import UC_ARCH_X86, UC_HOOK_CODE, UC_MODE_64, Uc from unicorn.x86_const import (UC_X86_REG_RAX, UC_X86_REG_RBP, UC_X86_REG_RCX, UC_X86_REG_RDI, UC_X86_REG_RDX, UC_X86_REG_RIP, UC_X86_REG_RSI, UC_X86_REG_RSP, UC_X86_REG_R8) ROOT = Path(__file__).resolve().parents[1] ORIGINAL = Path("/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary") DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" TEXT, DATA, STACK, STOP = 0x10000000, 0x30000000, 0x40000000, 0x50000000 GPU, MC, CHILD, CHIP, RM, KERNEL_MC = (DATA, DATA + 0x10000, DATA + 0x20000, DATA + 0x30000, DATA + 0x31000, DATA + 0x32000) INDIRECT = STOP + 0x100 # The binder only stores pointers, so the functions it selects are listed. ROOTS = ("_nv043630rm", "_nv006001rm", "_nv043573rm", "_nv043634rm", "_nv043619rm", "_nv043639rm") HOOKED = ("_nv053907rm", "_nv053912rm", "_nv049598rm", "_nv047869rm", "_nv046747rm", "_nv046745rm", "_nv030608rm", "_nv052695rm", "_nv052705rm") assert hashlib.sha256(ORIGINAL.read_bytes()).hexdigest() == DIGEST with ORIGINAL.open("rb") as stream: elf = ELFFile(stream) symbols = list(elf.get_section_by_name(".symtab").iter_symbols()) by_name = {symbol.name: symbol for symbol in symbols} text = bytearray(elf.get_section_by_name(".text").data()) text_section = elf.get_section_index(".text") starts = sorted((symbol["st_value"], symbol["st_size"], symbol.name) for symbol in symbols if symbol["st_shndx"] == text_section and symbol["st_size"]) relocations = sorted((relocation["r_offset"], relocation["r_info_type"], relocation["r_info_sym"], relocation["r_addend"]) for relocation in elf.get_section_by_name(".rela.text").iter_relocations()) import bisect # noqa: E402 offsets = [entry[0] for entry in relocations] executed, pending = [], list(ROOTS) while pending: name = pending.pop() if name in executed or name in HOOKED: continue executed.append(name) start, size = by_name[name]["st_value"], by_name[name]["st_size"] for offset, kind, index, addend in relocations[bisect.bisect_left(offsets, start): bisect.bisect_left(offsets, start + size)]: target = symbols[index] if target.name == "__x86_indirect_thunk_rax": address = INDIRECT else: assert target["st_shndx"] == text_section, (name, target.name) address = TEXT + target["st_value"] # Follow direct calls and tail calls only, not stored pointers. if kind in (2, 4) and text[offset - 1] in (0xe8, 0xe9): entry = target["st_value"] + addend + 4 callee = next((symbol for symbol in starts if symbol[0] == entry), None) if callee and callee[2] not in HOOKED: pending.append(callee[2]) value = address + addend if kind in (2, 4): value -= TEXT + offset else: assert kind == 11, kind struct.pack_into("