#!/usr/bin/env python3 """Unchanged Apple-linked main/dylib imports versus original ARM and native dispatch. Independent SDK-layout decoders resolve the dylib exports and genuine chained binds. Original ARM and native x86 run the same code at two distinct placements; no guest import is redirected to a host stub. Full macOS process launch remains unverified: dependency discovery, initializer calls, PAC and system ABI are pending. """ import argparse import importlib import json from pathlib import Path import platform import struct import subprocess import tempfile from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM from unicorn import Uc, UC_ARCH_ARM64, UC_MODE_ARM, UC_HOOK_CODE from unicorn import arm64_const as arm from differential import capture_arm, initialize_arm, state_dict as complete_state from differential import ROOT, ARM_REGISTERS, MASK, states from evidence import write_evidence chained = importlib.import_module('chained-image-differential') require, digest, snapshot = chained.require, chained.digest, chained.snapshot STOP, PAGE, SEED = chained.STOP, chained.PAGE, chained.SEED STACK_BASE, STACK_BYTES = 0x80000000000, 65536 STACK_TOP = STACK_BASE + STACK_BYTES PLACEMENTS = ((0x40000000000, 0x50000000000), (0x60000000000, 0x70000000000)) INSTALL_NAME = '@rpath/libstela-fixture.dylib' MAIN_SOURCE = ROOT / 'tests/fixtures/bound-image-arm64.S' LIBRARY_SOURCE = ROOT / 'tests/fixtures/bound-library-arm64.S' def commands(data): require(len(data) >= 32, 'truncated image header') magic, cpu, subtype, kind, count, size, flags, reserved = struct.unpack_from('<8I', data) require((magic, cpu, subtype, reserved) == (0xfeedfacf, 0x100000c, 0, 0) and kind in (2, 6), 'unsupported fixture image header') end, position, rows = 32 + size, 32, [] require(end <= len(data), 'truncated load commands') for _ in range(count): require(position + 8 <= end, 'truncated command header') command, length = struct.unpack_from('= 8 and length % 8 == 0 and position + length <= end, 'invalid command extent') rows.append((position, command, data[position:position + length])) position += length require(position == end, 'command count/size mismatch') return rows def command_string(body): offset = struct.unpack_from('= 72, 'truncated segment') _, _, name, address, size, fileoff, filesize, maximum, protection, count, flags = struct.unpack_from(' cursor, 'missing/empty export edge') edge = trie[cursor:terminator].decode('utf-8') child, cursor = uleb(trie, terminator + 1, len(trie)) walk(child, prefix + edge, active) walk(0, '', set()) return symbols def signed(value, bits): return value - (1 << bits) if value & (1 << (bits - 1)) else value def decode_fixups(data, info, load_base): require(load_base % PAGE == 0 and 0 < load_base < (1 << 47), 'invalid load base') meta = info['chained_payload'] payload = data[meta['offset']:meta['offset'] + meta['bytes']] require(len(payload) >= 28, 'truncated fixup header') version, starts, imports_offset, symbols_offset, import_count, import_format, symbols_format = struct.unpack_from('<7I', payload) require(version == 0 and import_format == 1 and symbols_format == 0, 'unsupported fixup-header format') require(28 <= starts < imports_offset <= symbols_offset <= len(payload) and imports_offset + 4 * import_count <= symbols_offset, 'invalid fixup metadata extents') imports = [] for index in range(import_count): word = struct.unpack_from('> 9) require(name_offset < len(payload) and b'\0' in payload[name_offset:], 'invalid import name') name = payload[name_offset:].split(b'\0', 1)[0].decode('utf-8') imports.append({'library_ordinal': signed(word & 0xff, 8), 'weak': bool(word & 0x100), 'name': name, 'addend': 0}) segment_count = struct.unpack_from('= 22 + pages * 2 and node + size <= imports_offset and page_size in (4096, 16384) and maximum == 0, 'invalid segment-start node') require(pointer_format in (2, 6), 'fixture decoder supports ordinary format2/6 only') formats.add(pointer_format) segment = info['segments'][segment_index] require(segment_offset == segment['address'] - info['preferred_base'] and pages * page_size <= segment['bytes'], 'segment offset/page count mismatch') for page in range(pages): within = struct.unpack_from('> 63: import_index = raw & 0xffffff require(import_index < len(imports) and (raw >> 32) & 0x7ffff == 0, 'invalid bind ordinal/reserved bits') inline = signed((raw >> 24) & 0xff, 8) binds.append({**row, 'import_index': import_index, 'inline_addend': inline, 'addend': inline + imports[import_index]['addend']}) else: require((raw >> 44) & 0x7f == 0 and (raw >> 36) & 0xff == 0, 'unsupported reserved/tagged rebase') target = raw & ((1 << 36) - 1) value = load_base + target if pointer_format == 6 else target + load_base - info['preferred_base'] rebases.append({**row, 'value': value}) next_words = (raw >> 51) & 0xfff if not next_words: break within += next_words * 4 return {'preferred_base': info['preferred_base'], 'load_base': load_base, 'pointer_formats': sorted(formats), 'rebases': rebases, 'imports': imports, 'binds': binds} def resolve_bindings(main, library, decoded, library_exports, library_base): rows = [] for bind in decoded['binds']: imported = decoded['imports'][bind['import_index']] ordinal = imported['library_ordinal'] require(ordinal > 0 and ordinal <= len(main['dependencies']), 'unsupported nonpositive or missing library ordinal') require(not imported['weak'] and main['dependencies'][ordinal - 1] == library['install_name'], 'missing/weak/mismatched declared provider') require(imported['name'] in library_exports, 'missing export') exported = library_exports[imported['name']] address = library_base + exported['value'] rows.append({**{key: bind[key] for key in ('segment_index', 'file_offset', 'guest_address', 'raw', 'pointer_format', 'import_index')}, 'library_ordinal': ordinal, 'name': imported['name'], 'provider_image': 1, 'address': address, 'addend': bind['addend'], 'value': address + bind['addend']}) return rows def reference_memories(data, info, fixups, bindings=()): memories = chained.initialized_segments(data, info, fixups) slide = fixups['load_base'] - info['preferred_base'] for row in bindings: segment = info['segments'][row['segment_index']] base = segment['address'] + slide struct.pack_into('= 0, 'function export edge changed') changed[position + len('function') - 1] = ord('X') rows.append({'name': 'missing_trie_export_with_original_symtab', 'main': main, 'library': bytes(changed)}) terminals = {} exports(library, infos[1], terminals) function_terminal = terminals['_bound_function'] require(function_terminal['bytes'] == 3, 'function terminal changed; review reexport fixture') changed = bytearray(library) offset = function_terminal['offset'] changed[offset:offset + 3] = bytes((8, 1, 0)) # Valid reexport: ordinal1, same symbol name. rows.append({'name': 'unsupported_regular_symbol_reexport', 'main': main, 'library': bytes(changed)}) changed = bytearray(main) starts = meta + struct.unpack_from('