#!/usr/bin/env python3 """Compile and compare bounded integer-only leaves from the real ARM kernel. Function starts and symbols come from the validated fileset manifest. The current subset cannot access guest memory or system registers. Unsupported functions are retained as declines, never patched into successful stubs. """ import argparse from collections import Counter,defaultdict import hashlib import json from pathlib import Path import struct import subprocess import tempfile from capstone import Cs,CS_ARCH_ARM64,CS_MODE_ARM from differential import ROOT,reference,states,bounded_native from evidence import write_evidence ALLOWED={"mov","movz","movn","movk","add","adds","sub","subs","cmp","cmn","and","ands","bic","bics","orr","orn","eor","eon","tst","nop","ret","b","cbz","cbnz","tbz","tbnz"} def starts(data,base): result=[] at=0 address=base while at=len(data) or shift>=64: raise ValueError("Malformed function-start ULEB128") byte=data[at];at+=1 value|=(byte&127)<(1<<64)-1: raise ValueError("Function address overflow") result.append(address) raise ValueError("Missing function-start terminator") def candidates(data,image,allowed=ALLOWED): symbol_names=defaultdict(list) string_offset=image["string_table_file_offset"] string_size=image["string_table_bytes"] strings=data[string_offset:string_offset+string_size] for index in range(image["symbol_count"]): name,kind,_,_,address=struct.unpack_from("=len(strings) or end<0: raise ValueError("Symbol name exceeds string table") if end>name: symbol_names[address].append(strings[name:end].decode()) text=next(s for s in image["segments"] if s["name"]=="__TEXT") offset=image["function_starts_file_offset"] length=image["function_starts_bytes"] addresses=starts(data[offset:offset+length],text["vm_address"]) decoder=Cs(CS_ARCH_ARM64,CS_MODE_ARM) rows=[] for index,address in enumerate(addresses): if address not in symbol_names: continue section=next((s for s in image["sections"] if s["instructions"] and not s["zero_fill"] and s["vm_address"]<=address