"""Unit tests for tools/d3d12_device_calls.py on an x86-64 image assembled here byte by byte. The image imports D3D12XboxCreateDevice from d3d12_xs.dll and has ten .pdata functions: two creator calls (through the import thunk with the out-pointer a global, and `call [rip+IAT]` with it a stack slot), calls through the device global and a copy of it (`call [vtable+disp]`, `call reg`, a CFG-style dispatch with the method in rax and the device in rdx, a guard-check stub that ends in `jmp rax`, a tail jump), a method in rax handed to a helper that only returns (not a slot: the review of old upstream PR 7998), an unrelated object's vtable call, the device passed to another function, and an indexed vtable call no slot can be read from. No game files. python -I tests/test_d3d12_device_calls.py """ import contextlib import io import json import os import struct import sys import tempfile import unittest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOLS_DIR = os.path.join(ROOT, "tools") if TOOLS_DIR not in sys.path: sys.path.insert(0, TOOLS_DIR) import d3d12_device_calls as ddc # noqa: E402 import pe_inspect # noqa: E402 IMAGE_BASE = 0x140000000 TEXT_RVA = 0x1000 IDATA_RVA = 0x2000 DATA_RVA = 0x3000 PDATA_RVA = 0x4000 SIZE = 0x5000 IAT = IDATA_RVA + 0x60 # D3D12XboxCreateDevice's address-table slot DEVICE = DATA_RVA + 0x00 # the creator's out-pointer COPY = DATA_RVA + 0x08 # a global the device is copied to OTHER = DATA_RVA + 0x10 # an unrelated object GUARD = DATA_RVA + 0x18 # the dispatch routine's pointer (to a `jmp rax` stub) EXTRA = TEXT_RVA + 0xC00 # free .text space for the code a test adds class Asm: """Appends instructions at known RVAs; `rip` and `rel` encode a RIP-relative or rel32 operand.""" def __init__(self, rva): self.rva = rva self.code = bytearray() @property def here(self): return self.rva + len(self.code) def raw(self, b): self.code += b def rip(self, prefix, target): end = self.here + len(prefix) + 4 self.code += prefix + struct.pack(" int: if path.endswith(".txt"): raise PermissionError("simulated permission error") return orig_mode(path) err, out = io.StringIO(), io.StringIO() with mock.patch.object(ddc, "published_mode", side_effect=fail_txt_mode), \ redirect_stderr(err), redirect_stdout(out): rc = ddc.main([img, "--out", out_dir]) self.assertEqual(rc, 1) self.assertEqual(out.getvalue(), "") self.assertIn("cannot write the report:", err.getvalue()) with open(json_dest, "rb") as f: self.assertEqual(f.read(), b"old-json\n") with open(txt_dest, "rb") as f: self.assertEqual(f.read(), b"old-txt\n") self.assertEqual(sorted(os.listdir(out_dir)), [ "game.exe.device_calls.json", "game.exe.device_calls.txt", ]) out_ok, err_ok = io.StringIO(), io.StringIO() with redirect_stdout(out_ok), redirect_stderr(err_ok): rc = ddc.main([img, "--out", out_dir]) self.assertEqual(rc, 0) self.assertEqual(err_ok.getvalue(), "") self.assertIn("site(s) across", out_ok.getvalue()) with open(json_dest, "rb") as f: raw_json = f.read() with open(txt_dest, "rb") as f: raw_txt = f.read() self.assertNotIn(b"\r\n", raw_json) self.assertNotIn(b"\r\n", raw_txt) self.assertEqual(sorted(os.listdir(out_dir)), [ "game.exe.device_calls.json", "game.exe.device_calls.txt", ]) if __name__ == "__main__": unittest.main()