#!/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 re import struct import subprocess import tempfile from capstone import Cs,CS_ARCH_ARM64,CS_MODE_ARM from differential import ROOT,State,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", "csel","csinc","csinv","csneg","cinc","cinv","cneg","cset","csetm", "madd","msub","mul","mneg","smaddl","smsubl","smull","smnegl", "umaddl","umsubl","umull","umnegl","smulh","umulh"} ALLOWED.update({"bfm", "sbfm", "ubfm", "asr", "lsl", "lsr", "sbfiz", "sbfx", "sxtb", "sxth", "sxtw", "bfi", "bfxil", "bfc", "ubfiz", "ubfx", "uxtb", "uxth", "uxtw"}) def equal_register_pair(value): """Parse two distinct real X registers; SP and XZR are not state slots.""" names=value.split(",") if len(names)!=2 or any(re.fullmatch(r"x(?:[0-9]|[12][0-9]|30)",name,re.IGNORECASE) is None for name in names): raise argparse.ArgumentTypeError("Expected two registers X0..X30 separated by one comma, e.g. X0,X1") pair=tuple(int(name[1:]) for name in names) if pair[0]==pair[1]: raise argparse.ArgumentTypeError("Equality inputs need two distinct registers") return pair def input_states(equal_registers=None): initial=states(all_flags=True) if equal_registers is not None: left,right=equal_registers matched=[] for original in initial: state=State.from_buffer_copy(original) state.x[right]=state.x[left] matched.append(state) initial+=matched return initial 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"]<=address1: raise AssertionError("Kernel leaf verification mixed compiler identities") report["compiler_executable_sha256"]=executable_hash report["harness_source_sha256"]=harness_hash report["shared_runner_sha256"]=hashlib.sha256((ROOT/"dev/differential.py").read_bytes()).hexdigest() if args.symbol: report["selected_exact_symbols"]=sorted(set(args.symbol)) if args.equal_registers is not None: report["additional_equal_register_inputs"]={"copied_from":f"X{args.equal_registers[0]}","copied_to":f"X{args.equal_registers[1]}","extra_states":len(initial_states)//2} write_evidence(args.output,report) print(json.dumps({"report":str(args.output),"function_starts":len(addresses),"filtered_candidates":len(eligible), "verified":len(verified),"declined":len(declined),"state_comparisons":report["state_comparisons"], "verified_sizes":dict(Counter(r["source_bytes"] for r in verified)), "verified_name_sample":[r["names"] for r in verified[:12]]},indent=2)) if __name__=="__main__": main()