#!/usr/bin/env python3 """Execute two unchanged, PAC-free windows from the pinned Darwin kernel. This generates the original-byte reference for the optional Rust Darwin ABI replay. It exercises return formatting, not a complete function or syscall. The kernel is supplied locally; no Apple instruction bytes are embedded here. """ import argparse import hashlib import json from pathlib import Path import struct import unicorn from unicorn import ( Uc, UC_ARCH_ARM64, UC_MODE_ARM, UC_PROT_READ, UC_PROT_WRITE, UC_PROT_EXEC, UC_HOOK_CODE, UC_HOOK_MEM_WRITE, ) from unicorn import arm64_const as arm from evidence import write_evidence from differential import State, state_dict, capture_arm, initialize_extended_arm ROOT = Path(__file__).resolve().parents[1] KERNEL_SHA256 = "ca55ea8c4c86170e870380ae5501b8b43adb2d2d3db28be4531b3040653be203" REFERENCE_COMMIT = "f6217f891ac0bb64f3d375211650a4c1ff8ca1ea" PAGE_PC = 0xfffffe000a633000 PAGE_OFFSET = 0x362f000 ENTRY_START = 0xfffffe000a633688 ENTRY_END = 0xfffffe000a633694 RETURN_START = 0xfffffe000a63386c RETURN_END = 0xfffffe000a633980 FUNCTION_START = 0xfffffe000a633828 FUNCTION_END = 0xfffffe000a6339fc RESTART_HELPER = 0xfffffe000a633884 SYSENT, SAVED, UTHREAD = 0x220000000000, 0x220000010000, 0x220000020000 CPU_RESERVED = 0xa5c39e71 MASK64 = (1 << 64) - 1 RETURN_TYPES = [ "none", "int", "unsigned_int", "offset", "address", "size", "signed_size", "unsigned64", ] RETURN_PAIRS = [ (0, 0), (1, 0), (0x7fffffff, 0x80000000), (0x80000000, 0x7fffffff), (0xffffffff, 0xffffffff), (0xffffffff, 1), (0, 0xffffffff), (0x89abcdef, 0x01234567), (0x01234567, 0x89abcdef), ] ERRNOS = [1, 4, 9, 22, 78, 0x7fffffff] def sha256(data): return hashlib.sha256(data).hexdigest() def u32(data, offset): return struct.unpack_from("> ((64 - index) % 64))) & MASK64, ((high >> index) | (high << ((64 - index) % 64))) & MASK64] extended.fpcr, extended.fpsr = 0x03c00000, 0x0800001f initialize_extended_arm(self.uc, extended) self.uc.reg_write(arm.UC_ARM64_REG_SP, 0x40400000) self.uc.reg_write(arm.UC_ARM64_REG_PSTATE, 0x3c5) self.uc.reg_write(arm.UC_ARM64_REG_X19, SAVED) self.uc.reg_write(arm.UC_ARM64_REG_PC, ENTRY_START) cpu_before_entry = cpu_state(self.uc) self.clear_trace() self.uc.emu_start(ENTRY_START, ENTRY_END, count=16) assert self.uc.reg_read(arm.UC_ARM64_REG_PC) == ENTRY_END assert self.trace == list(range(ENTRY_START, ENTRY_END, 4)) cpu_after_entry = cpu_state(self.uc) after_entry = bytes(self.uc.mem_read(SAVED, len(before))) expected_entry = bytearray(before) put(expected_entry, "