#!/usr/bin/env python3 """Run the original producers of the PMU InitLocked surface sizes and write an oracle. PMU InitLocked _nv051858rm makes three framebuffer requests through the surface allocator _nv052601rm; their byte counts come from three functions: instance block _nv052270rm asks _nv052299rm(object, &size, &second, &third) +f08 surface _nv052623rm stores _nv052618rm() when its predicates hold super surface _nv052583rm -> _nv052522rm asks the dispatcher _nv052179rm (gpu, &surface, &second), which selects one of _nv052174rm, _nv052175rm, _nv052176rm, _nv052177rm, _nv052180rm by the chip word at *(gpu+80)+10 (0x23 on a GV100 -> _nv052177rm) Executed original code: those eight functions, for every chip word below 0x100 and a few above, with valid and null output pointers. Fixture-supplied services: the predicate _nv054300rm that _nv052175rm asks (both answers per chip word that reaches it), the diagnostics _nv047869rm and _nv047868rm, and the status reporter _nv046747rm. No hardware is involved; no register is read. """ import bisect from _binary_path import resolve_580_binary import hashlib 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, DATA, STACK, STOP = 0x10000000, 0x30000000, 0x40000000, 0x50000000 GPU, CHIP, OUT = DATA, DATA + 0x10000, DATA + 0x11000 ROOTS = ("_nv052179rm", "_nv052299rm", "_nv052618rm") VARIANTS = ("_nv052174rm", "_nv052175rm", "_nv052176rm", "_nv052177rm", "_nv052180rm") HOOKED = ("_nv054300rm", "_nv047869rm", "_nv047868rm", "_nv046747rm") FILL = 0x7777777777777777 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()) offsets = [entry[0] for entry in relocations] entries = {symbol[0]: symbol[2] for symbol in starts} loaded, pending = [], list(ROOTS) while pending: name = pending.pop() if name in loaded or name in HOOKED: continue loaded.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] assert target["st_shndx"] == text_section, (name, target.name) # Direct calls, tail calls and the branch to the cold part only. assert kind in (2, 4), kind if text[offset - 1] in (0xe8, 0xe9): callee = entries.get(target["st_value"] + addend + 4) if callee and callee not in HOOKED: pending.append(callee) struct.pack_into("