#!/usr/bin/env python3 """Run the original GP106/GV100 OBJACR +150 (_nv016745rm) and write an oracle. Executed original code: _nv016745rm and the region-count function _nv016916rm it calls at OBJACR+148, both reached through the pointers the original OBJACR binder _nv005947rm stores with chip HAL 0x20 or 0x23 and RM HAL 2. The default is GV100 (0x23). Use --chip-hal 0x20 for GP106 and its own output paths. Fixture-supplied services: register read _nv053907rm and write _nv053912rm, the check _nv031156rm and the diagnostic _nv047869rm. No hardware is involved. """ import argparse import bisect from _binary_path import resolve_580_binary import hashlib import itertools import json import os 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_R8, UC_X86_REG_R9, 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] parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--chip-hal", type=lambda value: int(value, 0), choices=(0x20, 0x23), default=0x23) parser.add_argument("--output-dir", type=Path) parser.add_argument("--report", type=Path) args = parser.parse_args() if args.chip_hal == 0x20: default_output_dir = ROOT / "target/gp106-memory" default_report = ROOT / "target/gp106-reports/gp106-acr-wpr-region-original.json" else: default_output_dir = ROOT / "target/gpu-stage" default_report = ROOT / "reports/gpu-acr-wpr-region-original.json" args.output_dir = args.output_dir or default_output_dir args.report = args.report or default_report ORIGINAL = resolve_580_binary() DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" TEXT, DATA, STACK, STOP = 0x10000000, 0x30000000, 0x40000000, 0x50000000 GPU, ACR, CHIP, RM, OUT = DATA, DATA + 0x10000, DATA + 0x20000, DATA + 0x21000, DATA + 0x22000 ROOTS = ("_nv016745rm", "_nv016916rm", "_nv005947rm") HOOKED = ("_nv053907rm", "_nv053912rm", "_nv047869rm", "_nv031156rm") 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} 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 = STOP + 0x100 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): callee = entries.get(target["st_value"] + addend + 4) if callee and callee not in HOOKED: pending.append(callee) value = address + addend if kind in (2, 4): value -= TEXT + offset else: assert kind == 11, kind struct.pack_into("> 32 == FILL >> 32 for value in outputs[2:]) paths["read" if reads else "refused"] += 1 rows.append(f"{region:x} {check:x};" + " ".join(f"{value:x}" for value in reads) + f";{status:x};" + " ".join(f"{value:x}" for value in outputs) + ";" + " ".join(trace)) return status, outputs, list(trace) # Physical first-open sequence of the original driver on a V100 (BAR0 # offsets), lines 37714 to 37721 of the per-GPU register transcript. status, outputs, observed = run(1, reads=(0xfffe02, 0x3, 0x8000000, 0x8000001)) assert status == 0 and observed == [ "nv031156:0", "w:100cd4:2", "r:100cd4:fffe02", "w:100cd4:3", "r:100cd4:3", "w:100cd4:0", "r:100cd4:8000000", "w:100cd4:1", "r:100cd4:8000001"], observed assert outputs == (0, 0, FILL & ~0xffffffff, FILL & ~0xffffffff), outputs VALUES = (0, 0xfffe02, 0x3, 0xffffffff, 0x8000000, 0x8000001, 0x12345678, 0xfffe0f, 0x1f) for region in (1, 2): for reads in itertools.product(VALUES, VALUES, (0, 0x8000000, 0xffffffff, 0x12345678, 0xa50), (0, 0x8000001, 0xffffffff, 0x12345678, 0x5a1)): run(region, reads=reads) for region, check in itertools.product((0, 1, 2, 3, 4, 9, 0x80000001, 0xffffffff), (0, 0x56, 0x1f, 0xffff)): if check or region not in (1, 2): run(region, check=check) assert all(paths.values()), paths out = args.output_dir out.mkdir(parents=True, exist_ok=True) oracle = "\n".join(rows) + "\n" (out / "original-acr-wpr-region.txt").write_text(oracle) report = { "original_sha256": DIGEST, "chip_hal": hex(args.chip_hal), "rm_hal": 2, "executed": sorted(executed), "fixture_supplied": list(HOOKED), "bindings": {"OBJACR+0x148": "_nv016916rm", "OBJACR+0x150": "_nv016745rm"}, "cases": len(rows), "cases_by_path": paths, "oracle": os.path.relpath(out / "original-acr-wpr-region.txt", ROOT), "oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(), "scope": "Original OBJACR function at +150 with the selected chip's binder pointers. " "Register values, the check _nv031156rm and the diagnostic are " "fixture-supplied; no hardware executed."} args.report.parent.mkdir(parents=True, exist_ok=True) args.report.write_text(json.dumps(report, indent=1) + "\n") print(f"{len(rows)} original OBJACR +150 cases {paths}; executed {len(executed)} functions")