#!/usr/bin/env python3 """Which Metal API does the unchanged target's graphics stack actually send? (static, read-only) The native Adreno Metal plugin has to supply a real _MTLDevice and the objects hanging off it. How much of Metal that means depends on the clients, so this measures it: the selectors declared by the Metal protocols (from the iPhoneOS SDK headers, via the clang AST) against the selector references (__objc_selrefs) of the compositor and its neighbours in the target's unchanged dyld shared cache. A selector reference proves the image can send that message, not that it does on the paths the system UI takes; the list is an upper bound of the surface that compositing needs and a lower bound of what any client may call. Output: build/gpu-analysis/metal-api-surface.json (ignored) and a summary on stdout. The SDK is newer than iPadOS 17.7.11; selectors the SDK declares but the target never references simply do not appear, and selectors only the target knows are listed as 'unmatched'.""" import json import mmap from pathlib import Path import re import struct import subprocess import sys import tempfile ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'kits/XnuPrelink')) from native_prelink import macho PROTOCOLS = ['MTLDevice', 'MTLCommandQueue', 'MTLCommandBuffer', 'MTLCommandEncoder', 'MTLRenderCommandEncoder', 'MTLBlitCommandEncoder', 'MTLComputeCommandEncoder', 'MTLParallelRenderCommandEncoder', 'MTLResource', 'MTLTexture', 'MTLBuffer', 'MTLLibrary', 'MTLFunction', 'MTLRenderPipelineState', 'MTLComputePipelineState', 'MTLSamplerState', 'MTLDepthStencilState', 'MTLEvent', 'MTLSharedEvent', 'MTLFence', 'MTLHeap', 'MTLDrawable', 'MTLCaptureScope', 'MTLArgumentEncoder', 'MTLIndirectCommandBuffer', 'MTLCounterSampleBuffer', 'MTLBinaryArchive'] IMAGES = ['/System/Library/Frameworks/QuartzCore.framework/QuartzCore', '/System/Library/Frameworks/Metal.framework/Metal', '/System/Library/Frameworks/MetalKit.framework/MetalKit', '/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore', '/System/Library/Frameworks/CoreImage.framework/CoreImage', '/System/Library/PrivateFrameworks/IOMobileFramebuffer.framework/IOMobileFramebuffer'] def sdk_selectors(): sdk = subprocess.check_output(['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'], text=True).strip() result = {} with tempfile.TemporaryDirectory() as td: source = Path(td) / 'm.m' source.write_text('#import \n#import \n') for name in PROTOCOLS: out = subprocess.run(['xcrun', 'clang', '-fsyntax-only', '-fobjc-arc', '-isysroot', sdk, '-target', 'arm64-apple-ios17.0', '-Xclang', '-ast-dump=json', '-Xclang', '-ast-dump-filter', '-Xclang', name, str(source)], capture_output=True, text=True).stdout selectors = set() decoder, at = json.JSONDecoder(), 0 while True: at = out.find('{', at) if at < 0: break node, at = decoder.raw_decode(out, at) if node.get('kind') != 'ObjCProtocolDecl' or node.get('name') != name: continue for child in node.get('inner', []): if child.get('kind') == 'ObjCMethodDecl': selectors.add(child['name']) elif child.get('kind') == 'ObjCPropertyDecl': prop = child['name'] selectors.add(prop) if not child.get('readonly') and 'readonly' not in json.dumps(child.get('inner', [])): selectors.add('set' + prop[0].upper() + prop[1:] + ':') result[name] = sorted(selectors) return result def cache(): directory = ROOT / '.cache/firmware/target-volumes/systemos-mount/System/Library/Caches/com.apple.dyld' files, mappings, slides = [], [], [] for path in sorted(directory.glob('dyld_shared_cache_arm64*')): if path.suffix == '.symbols': continue stream = path.open('rb') data = mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ) files.append((stream, data)) if data[:16].rstrip(b'\0') != b'dyld_v1 arm64': raise SystemExit('Require the original ordinary ARM64 cache') at, count = struct.unpack_from('<2I', data, 16) for i in range(count): va, size, off, _, _ = struct.unpack_from('<3Q2I', data, at + 32 * i) mappings.append((va, va + size, off, data)) at, count = struct.unpack_from('<2I', data, 312) for i in range(count): va, size, _, off, length, _, _, _ = struct.unpack_from('<6Q2I', data, at + 56 * i) if length: mask, base = struct.unpack_from('<2Q', data, off + 24) slides.append((va, va + size, mask, base)) main = files[0][1] if main[88:104].hex() != '590d839a82d230f1a17d0cc053420f73': raise SystemExit('Not the exact target cache') def read(va, size): for start, end, off, data in mappings: if start <= va and va + size <= end: return data[off + va - start:off + va - start + size] raise SystemExit('Unbacked cache extent ' + hex(va)) def pointer(va): raw, = struct.unpack('