#!/usr/bin/env python3 """Run the original GV100 OBJMC construct and init stage bodies and write an oracle. Executed original code: the GV100 HAL binder _nv006001rm, the ConstructEngine body _nv043560rm with its bound HAL _nv043558rm, StatePreInitUnlocked _nv043631rm, StateInitLocked _nv043628rm, StateInitUnlocked _nv043629rm and the functions they call directly as part of their own logic. Registry reads, the lock query, other objects' methods and diagnostics are fixture-supplied. """ import bisect from _binary_path import resolve_580_binary import collections 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) ROOT = Path(__file__).resolve().parents[1] ORIGINAL = resolve_580_binary() DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" TEXT, RODATA, DATA, STACK, STOP = 0x10000000, 0x20000000, 0x30000000, 0x40000000, 0x50000000 GPU, MC, CHIP, RM = DATA, DATA + 0x10000, DATA + 0x20000, DATA + 0x21000 OBJ_1EC0, OBJ_20E8, OBJ_2120, OBJ_2068 = (DATA + 0x22000, DATA + 0x23000, DATA + 0x24000, DATA + 0x25000) INDIRECT, SLOT_1B0 = STOP + 0x100, STOP + 0x200 FILL = 0x77 # The binder only stores pointers, so the function it selects is listed. ROOTS = ("_nv043560rm", "_nv043631rm", "_nv043628rm", "_nv043629rm", "_nv006001rm", "_nv043558rm") HOOKED = ("_nv049786rm", "_nv054113rm", "_nv031034rm", "_nv031133rm", "_nv044918rm", "_nv047869rm", "_nv046747rm", "_nv046745rm") KEYS = (b"DisableGrAuto", b"EnableCoreVoltage", b"RmOverrideIdleSlowdownSettings", b"RmEnablePreException", b"RMSkipRCOnExceptions") 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()) rodata = elf.get_section_by_name(".rodata").data() text_section, rodata_section = (elf.get_section_index(name) for name in (".text", ".rodata")) 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()) 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 elif target["st_shndx"] == rodata_section: address = RODATA + target["st_value"] 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("