#!/usr/bin/env python3 """Run the original base GPU PreInit pass _nv031220rm and write an oracle. Only _nv031220rm itself executes. Every callee is fixture-supplied: the oracle records call order, arguments and the GPU fields the pass writes. It proves the pass's control flow, not the behavior of any callee or engine. """ import hashlib from _binary_path import resolve_580_binary import json from pathlib import Path import random 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, UC_X86_REG_R9, ) ROOT = Path(__file__).resolve().parents[1] ORIGINAL = resolve_580_binary() DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" TEXT, BSS, DATA, STACK, STOP = 0x10000000, 0x20000000, 0x30000000, 0x40000000, 0x50000000 GPU, SYS, FIRMWARE, GUEST = DATA, DATA + 0x8000, DATA + 0x10000, DATA + 0x70000 RMAPI, CHIP, LIST, DESCRIPTORS, CLASSES = (DATA + 0x71000, DATA + 0x72000, DATA + 0x73000, DATA + 0x74000, DATA + 0x75000) ENGINES, VTABLE = DATA + 0x76000, DATA + 0x7f000 CLIENT_OBJECT, SUBDEVICE_OBJECT = 0x1111222233334444, 0x5555666677778888 INDIRECT = STOP + 0x100 SLOTS = {name: STOP + 0x200 + 0x10 * index for index, name in enumerate( ("brand", "rtd3", "selfhost", "sink", "ctrl", "falcons", "mig", "present", "retained", "optimus"))} GPU_SLOTS = {0x118: "brand", 0x120: "rtd3", 0x450: "selfhost", 0x4298: "ctrl", 0x148: "falcons", 0x460: "mig", 0x130: "retained", 0x468: "optimus"} DESTROY_OFFSET = 0x5d4250 HOOKED = ("_nv006750rm", "_nv030279rm", "_nv054113rm", "_nv054215rm", "_nv054239rm", "_nv047869rm", "_nv046752rm", "_nv052687rm", "_nv052705rm", "_nv030236rm", "_nv030184rm", "_nv042366rm", "_nv030522rm", "_nv030262rm", "_nv030260rm", "_nv046747rm", "_nv046745rm", "_nv026840rm", "_nv026850rm", "_nv026839rm", "_nv030928rm", "_nv054281rm", "_nv056331rm", "_nv058711rm", "_nv031235rm", "_nv047868rm", "_nv030463rm", "_nv030258rm", "_nv052701rm", "_nv013534rm") INITIAL = {0x423c: 0x70000001, 0x4240: 0x70000002, 0x4244: 0x70000003, 0x4248: 0x70000004, 0xcec: 0x70000005, 0xcf4: 0x70000006, 0xcf8: 0x70000007} 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") bss_section = elf.get_section_index(".bss") bss_size = elf.get_section_by_name(".bss")["sh_size"] start, size = by_name["_nv031220rm"]["st_value"], by_name["_nv031220rm"]["st_size"] for relocation in elf.get_section_by_name(".rela.text").iter_relocations(): offset = relocation["r_offset"] if not start <= offset < start + size: continue kind = relocation["r_info_type"] target = symbols[relocation["r_info_sym"]] if target.name == "__x86_indirect_thunk_rax": address = INDIRECT elif target["st_shndx"] == bss_section: address = BSS + target["st_value"] else: assert target["st_shndx"] == text_section, target.name address = TEXT + target["st_value"] value = address + relocation["r_addend"] if kind in (2, 4): value -= TEXT + offset else: assert kind == 11, kind struct.pack_into("