"""Unit tests for tools/int3_cause.py: from an int3 back to the call whose result was tested, on an image built here. The image is tests/test_import_call_sites.py's PE32+ (xmem.dll imports XMemFoo, XMemBar and #7 behind thunks) with hand-assembled functions and a `.pdata` directory added: - an import call, `test eax, eax` and a `js` to a fail block at the end of the function; - a vtable slot call on an object loaded from a global, its result copied to ebx before the test; - a cold chunk with its own `.pdata` entry, reached by a `jne` from another function; - a handler that ends in int3 on every path, and a caller that calls it when a function returns nonzero in al. It needs no game files and no third-party packages. python -I tests/test_int3_cause.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__))) sys.path.insert(0, os.path.join(ROOT, "tools")) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import int3_cause # noqa: E402 from gdk_runtime_calls import decode # noqa: E402 from test_import_call_sites import (IDATA_RVA, IMAGE_BASE, THUNK_BAR, THUNK_FOO, build_image, # noqa: E402 rel32) PDATA_RVA = IDATA_RVA + 0x400 GLOBAL_DEVICE = IDATA_RVA + 0x800 GLOBAL_FLAG = IDATA_RVA + 0x808 IMPORT_CHECK = 0x1100 # call XMemFoo; test eax, eax; js fail ... fail: lea; call XMemBar; int3 IMPORT_STOP = 0x1122 SLOT_CHECK = 0x1200 # this = [global]; call [vtable+0x180]; mov ebx, eax; test ebx, ebx; jns ok; int3 SLOT_STOP = 0x121D HOT = 0x1300 # call 0x1600; cmp eax, 2; jne cold HOT_JNE = 0x130C COLD = 0x1400 # cold: mov ecx, 5; call XMemBar; int3 COLD_STOP = 0x140A HANDLER = 0x1500 # cmp byte [flag], 1; jne stop; call XMemBar; stop: int3 HANDLER_STOP = 0x1512 STATUS = 0x1600 # xor eax, eax; ret CALLER = 0x1700 # call 0x1600; test al, al; je skip; call HANDLER; skip: ... CALLER_JE = 0x170B CALLER_CALL = 0x170D CHECK = 0x1800 # returns its argument; ends in HANDLER2 when (argument + 0x10) is 0 or 2 CHECK_JAE = 0x1823 CHECK_CALL = 0x1825 HANDLER2 = 0x1900 # call XMemBar; int3 HANDLER2_STOP = 0x1905 USER = 0x1A00 # CHECK(device slot 8 result); CHECK(XMemFoo result) FIELD = 0x1B00 # p = XMemFoo(); if (p->field_10 == 0) int3 FIELD_STOP = 0x1B10 GLOBAL_PTR = IDATA_RVA + 0x810 INIT = 0x1C00 # GLOBAL_PTR = XMemFoo() RESET = 0x1C20 # GLOBAL_PTR = 0 USE = 0x1D00 # if (GLOBAL_PTR == 0) int3 USE_STOP = 0x1D0C BOOL = 0x1E00 # ok = 0; if (XMemFoo() >= 0) ok = 1; return ok def call(at, target): return b"\xe8" + rel32(at + 5, target) def rip(at, length, target): """disp32 of a RIP-relative operand in an instruction of `length` bytes at `at` (the disp ends at length-tail).""" return rel32(at + length, target) def functions(): """{start: (code bytes, end)}: the functions the image holds, each with its .pdata entry.""" f = {} a = IMPORT_CHECK f[a] = (b"\x48\x83\xec\x28" + call(a + 4, THUNK_FOO) + b"\x85\xc0" # sub rsp; call; test eax, eax + b"\x0f\x88" + rel32(a + 0x11, a + 0x16) # js fail + b"\x48\x83\xc4\x28\xc3" # add rsp; ret + b"\x48\x8d\x0d" + rip(a + 0x16, 7, GLOBAL_FLAG) # fail: lea rcx, [rip+x] + call(a + 0x1D, THUNK_BAR) + b"\xcc") # call XMemBar; int3 a = SLOT_CHECK f[a] = (b"\x53\x48\x83\xec\x20" # push rbx; sub rsp, 0x20 + b"\x48\x8b\x0d" + rip(a + 5, 7, GLOBAL_DEVICE) # mov rcx, [rip+device] + b"\x48\x8b\x01" # mov rax, [rcx] + b"\xff\x90\x80\x01\x00\x00" # call [rax+0x180] + b"\x8b\xd8\x33\xc9\x85\xdb" # mov ebx, eax; xor; test + b"\x79\x01\xcc" # jns ok; int3 + b"\x48\x83\xc4\x20\x5b\xc3") # ok: add rsp; pop rbx; ret a = HOT f[a] = (b"\x48\x83\xec\x28" + call(a + 4, STATUS) + b"\x83\xf8\x02" # call STATUS; cmp eax, 2 + b"\x0f\x85" + rel32(a + 0x12, COLD) # jne cold + b"\x48\x83\xc4\x28\xc3") a = COLD f[a] = b"\xb9\x05\x00\x00\x00" + call(a + 5, THUNK_BAR) + b"\xcc" a = HANDLER f[a] = (b"\x48\x83\xec\x28" + b"\x80\x3d" + rip(a + 4, 7, GLOBAL_FLAG) + b"\x01" # cmp byte [flag], 1 + b"\x75\x05" + call(a + 0xD, THUNK_BAR) + b"\xcc") # jne stop; call; stop: int3 f[STATUS] = b"\x33\xc0\xc3" a = CALLER f[a] = (b"\x48\x83\xec\x28" + call(a + 4, STATUS) + b"\x84\xc0" # call STATUS; test al, al + b"\x74\x05" + call(a + 0xD, HANDLER) # je skip; call HANDLER + b"\x48\x83\xc4\x28\xc3") # skip: add rsp; ret a = CHECK f[a] = (b"\x56\x48\x83\xec\x20\x8b\xf1" # push rsi; sub rsp; mov esi, ecx + b"\x85\xc9\x78\x08" # test ecx, ecx; js high + b"\x8b\xc6\x48\x83\xc4\x20\x5e\xc3" # out: mov eax, esi; ...; ret + b"\x8d\x46\x10\x83\xf8\x03\x77\xf0" # high: lea eax, [rsi+0x10]; cmp; ja out + b"\xb9\x05\x00\x00\x00\x0f\xa3\xc1\x73\xe6" # mov ecx, 5; bt ecx, eax; jae out + call(a + 0x25, HANDLER2) + b"\xeb\xdf") # call HANDLER2; jmp out f[HANDLER2] = call(HANDLER2, THUNK_BAR) + b"\xcc" a = USER f[a] = (b"\x48\x83\xec\x28" + b"\x48\x8b\x0d" + rip(a + 4, 7, GLOBAL_DEVICE) # mov rcx, [rip+device] + b"\x48\x8b\x01\xff\x50\x40\x8b\xc8" # mov rax, [rcx]; call [rax+0x40] + call(a + 0x13, CHECK) # mov ecx, eax; call CHECK + call(a + 0x18, THUNK_FOO) + b"\x8b\xc8" + call(a + 0x1F, CHECK) # call XMemFoo; mov; call + b"\x48\x83\xc4\x28\xc3") a = FIELD f[a] = (b"\x48\x83\xec\x28" + call(a + 4, THUNK_FOO) # call XMemFoo + b"\x8b\x48\x10\x85\xc9\x75\x01\xcc" # mov ecx, [rax+0x10]; test; jne; int3 + b"\x48\x83\xc4\x28\xc3") a = INIT f[a] = (b"\x48\x83\xec\x28" + call(a + 4, THUNK_FOO) + b"\x48\x89\x05" + rip(a + 9, 7, GLOBAL_PTR) + b"\x48\x83\xc4\x28\xc3") # mov [rip+ptr], rax f[RESET] = b"\x48\xc7\x05" + rip(RESET, 11, GLOBAL_PTR) + b"\x00\x00\x00\x00\xc3" # mov qword [rip+ptr], 0 a = USE f[a] = (b"\x48\x8b\x05" + rip(a, 7, GLOBAL_PTR) # mov rax, [rip+ptr] + b"\x48\x85\xc0\x75\x01\xcc\xc3") # test rax, rax; jne; int3; ret a = BOOL f[a] = (b"\x53\x48\x83\xec\x20\x33\xdb" + call(a + 7, THUNK_FOO) # push; sub; xor ebx, ebx; call + b"\x85\xc0\x78\x02\xb3\x01" # test eax, eax; js done; mov bl, 1 + b"\x8b\xc3\x48\x83\xc4\x20\x5b\xc3") # done: mov eax, ebx; ...; ret return {start: (code, start + len(code)) for start, code in f.items()} def build(): buf = bytearray(build_image([])) entries = sorted(functions().items()) for start, (code, end) in entries: buf[start:end] = code for i, (start, (_, end)) in enumerate(entries): struct.pack_into(" len(data): continue to = base + m.end() + 4 + {0xC6: 1, 0xC7: 4}.get(op, 0) + struct.unpack_from("= 0")) self.assertEqual(g["registers"][0]["import"], "xmem.dll!XMemFoo") self.assertEqual((ok["stop"], ok["never_returns"], ok["reached_from_start"], ok["callers"]), ("instruction", False, False, [])) start = out[hex(IMPORT_CHECK)] # the function's first instruction self.assertEqual((start["guards"], start["reached_from_start"]), ([], True)) self.assertIn("no guard: every path from the function start reaches it", int3_cause.to_text({"x": start})) def test_returns_lists_each_value_with_the_branches_that_set_it(self): out = int3_cause.cause(self.data, [hex(BOOL + 0x13)], returned=True)[hex(BOOL + 0x13)] self.assertEqual((out["stop"], out["function"][0], out["rets"], out["complete"]), ("returns", BOOL, 1, True)) by_value = {x["constant"]: x for x in out["returns"]} self.assertEqual(sorted(by_value), [0, 1]) one, zero = by_value[1], by_value[0] self.assertEqual((one["at"], one["copied"], zero["at"]), (BOOL + 0x10, True, BOOL + 5)) [g] = one["guards"] self.assertEqual((g["jump"], g["condition"]), ("js", "eax >= 0")) self.assertEqual(g["registers"][0]["import"], "xmem.dll!XMemFoo") self.assertTrue(zero["reached_from_start"]) text = int3_cause.to_text({"x": out}) self.assertIn(f"a copy of a constant (1) at {BOOL + 0x10:#x}:", text) self.assertIn(f"js at {BOOL + 0xE:#x}, fall-through side: runs when eax >= 0", text) self.assertIn("on every path from the function start", text) def test_wine_reports_the_address_after_the_int3(self): r = self.one(f"exe+{IMPORT_STOP + 1:#x}") self.assertEqual((r["rva"], r["at"]), (IMPORT_STOP + 1, IMPORT_STOP)) self.assertEqual(r["guards"][0]["condition"], "eax < 0") def test_comparisons_read_as_conditions(self): def cond(code, jcc_cc): insn = decode(code, 0, 0) self.assertEqual(insn.size, len(code)) return int3_cause.condition(int3_cause.read_test(insn, code), jcc_cc) self.assertEqual(cond(b"\x83\xf8\xff", 0x2), "eax rax, not rsp. f1 = 0x1100 code1 = call(f1, THUNK_FOO) + b"\x0f\xb6\xcc\x85\xc9\x75\x01\xcc\xc3" # 2. mov cl, ah (0x88 E1) after call XMemFoo must follow ah -> rax, not rsp. f2 = 0x1200 code2 = call(f2, THUNK_FOO) + b"\x88\xe1\x84\xc9\x75\x01\xcc\xc3" # 3. mov ah, byte [rcx+0x10] (8A 61 10) writes rax (not rsp), so test ah, 1 traces to the load. f3 = 0x1300 code3 = b"\x8a\x61\x10\xf6\xc4\x01\x75\x01\xcc\xc3" # 4. mov byte [rip+GLOBAL_FLAG], ah (88 25 disp32) traces ah -> rax (XMemFoo), not rsp. f4 = 0x1400 code4 = call(f4, THUNK_FOO) + b"\x88\x25" + rip(f4 + 5, 6, GLOBAL_FLAG) + b"\xc3" # 5. movzx eax, spl (40 0F B6 C4, WITH REX) still reads rsp, not rax. f5 = 0x1500 code5 = call(f5, THUNK_FOO) + b"\x40\x0f\xb6\xc4\x85\xc0\x75\x01\xcc\xc3" data = self._build_custom({f1: code1, f2: code2, f3: code3, f4: code4, f5: code5}) r1 = int3_cause.cause(data, [hex(f1 + 12)])[hex(f1 + 12)] self.assertEqual(r1["guards"][0]["registers"][0]["import"], "xmem.dll!XMemFoo") self.assertTrue(r1["guards"][0]["registers"][0].get("copied")) r2 = int3_cause.cause(data, [hex(f2 + 11)])[hex(f2 + 11)] self.assertEqual(r2["guards"][0]["registers"][0]["import"], "xmem.dll!XMemFoo") r3 = int3_cause.cause(data, [hex(f3 + 8)])[hex(f3 + 8)] self.assertEqual(r3["guards"][0]["registers"][0]["value"], "loaded from memory") self.assertEqual(r3["guards"][0]["registers"][0]["from"], "byte [rcx+0x10]") gw = int3_cause.global_writers(int3_cause.Image(data), GLOBAL_FLAG) self.assertEqual(len(gw["writers"]), 1) self.assertEqual(gw["writers"][0]["source"]["import"], "xmem.dll!XMemFoo") r5 = int3_cause.cause(data, [hex(f5 + 13)])[hex(f5 + 13)] self.assertEqual(r5["guards"][0]["registers"][0]["value"], "set before the function is entered") def test_global_writers_handles_16bit_imm_and_rejects_false_byte_pattern(self): target = GLOBAL_PTR # Real writer: 66 C7 05 disp32 imm16 -> mov word [rip+target], 0x1234 (9 bytes total) f_real = 0x1100 code_real = b"\x66\xc7\x05" + rip(f_real, 9, target) + struct.pack("` pointing to target, # so `at - 2` lands cleanly on `48 BA` (start of a real instruction that is NOT a RIP store!). f_decoy = 0x1200 fake_disp = struct.pack("= 2^31 (2*34 - 2 = 66 bits > uint64) target = 0x90000000 packed = image._pack([target << image._shift | 0x1100]) self.assertIsInstance(packed, list) self.assertEqual(image._lookup(packed, target), [target << image._shift | 0x1100]) if __name__ == "__main__": unittest.main()