#!/usr/bin/env python3 """Check native fault PC mapping, unchanged state/writeback and protected RAM. Data-abort cases compare original ARM execution in Unicorn. SP alignment cases check the compiler's explicitly enabled alignment policy against Arm's specified pre-access check. Native signals are evidence boundaries, not guest exceptions. """ import ctypes import hashlib import importlib.util import json from pathlib import Path import signal import struct import subprocess import tempfile from unicorn import Uc, UcError, UC_ARCH_ARM64, UC_MODE_ARM, UC_HOOK_MEM_INVALID, UC_PROT_READ from unicorn import arm64_const as arm from differential import ROOT, BASE, State, ARM_REGISTERS, move from evidence import write_evidence spec = importlib.util.spec_from_file_location("memory_suite", ROOT / "dev/memory-differential.py") memory_suite = importlib.util.module_from_spec(spec) spec.loader.exec_module(memory_suite) def arm_fault(source, initial, ram_address, page_size, readonly, second_readonly=False): machine = Uc(UC_ARCH_ARM64, UC_MODE_ARM) machine.mem_map(BASE, 0x10000) machine.mem_write(BASE, source) observed_bytes = page_size * (2 if second_readonly else 1) machine.mem_map(ram_address, observed_bytes) machine.mem_write(ram_address, b"\xa5" * observed_bytes) if second_readonly: machine.mem_protect(ram_address + page_size, page_size, UC_PROT_READ) if readonly: machine.mem_protect(ram_address, page_size, UC_PROT_READ) for register, value in zip(ARM_REGISTERS, initial.x): machine.reg_write(register, value) machine.reg_write(arm.UC_ARM64_REG_SP, initial.sp) machine.reg_write(arm.UC_ARM64_REG_NZCV, initial.nzcv) faults = [] def fault(machine, access, address, size, value, _): faults.append({"access": access, "address": address, "size": size, "pc": machine.reg_read(arm.UC_ARM64_REG_PC)}) return False machine.hook_add(UC_HOOK_MEM_INVALID, fault) try: machine.emu_start(BASE, BASE + len(source), count=20) except UcError: if not faults: raise if not faults: raise AssertionError("ARM reference did not fault") final = State() for index, register in enumerate(ARM_REGISTERS): final.x[index] = machine.reg_read(register) final.sp = machine.reg_read(arm.UC_ARM64_REG_SP) final.nzcv = machine.reg_read(arm.UC_ARM64_REG_NZCV) return final, bytes(machine.mem_read(ram_address, observed_bytes)), faults def cases(): result = [] for size in range(4): for opcode in range(4): if opcode >= 2 and (size == 3 or (opcode == 3 and size >= 2)): continue for mode, offset in (("unsigned", 0), ("unscaled", -16), ("pre", -16), ("post", 16)): for rn, rt in ((0, 1), (31, 31), (0, 0)): if mode in ("pre", "post") and rn == rt and rn != 31: continue # Unscaled offset is part of the faulting effective address, # just like pre-index, but must never update the base. result.append({"size": size, "opcode": opcode, "rn": rn, "rt": rt, "offset": offset, "mode": mode, "protection": "guard"}) # A store must honor a mapped page's write protection, not just bounds. result.append({"size": size, "opcode": 0, "rn": 0, "rt": 1, "offset": 16, "mode": "post", "protection": "readonly"}) result.append({"size": 3, "opcode": 1, "rn": 0, "rt": 1, "offset": 0, "mode": "unsigned", "protection": "crossing"}) for opcode in (0, 1): for mode, offset in (("unsigned", 0), ("pre", -1), ("post", 16)): result.append({"size": 3, "opcode": opcode, "rn": 31, "rt": 0, "offset": offset, "mode": mode, "protection": "sp_alignment"}) return result def main(): subprocess.run([str(ROOT / "dev/cargo"), "build", "--locked", "-p", "stela-cli"], check=True) cli = ROOT / "target/debug/stela" reports = [] compiler = None with tempfile.TemporaryDirectory(prefix="stela-faults-") as temporary: directory = Path(temporary) observer = directory / "observer" observer_source = ROOT / "dev/native-memory-faults.c" subprocess.run(["clang", "-std=c11", "-Wall", "-Wextra", "-Werror", "-O2", str(observer_source), "-o", str(observer)], check=True) observer_hash = hashlib.sha256(observer.read_bytes()).hexdigest() for index, case in enumerate(cases()): word = memory_suite.encoding(case["size"], case["opcode"], case["rt"], case["rn"], case["offset"], case["mode"]) # Prior completed instructions survive the subsequent fault. source = struct.pack("