#!/usr/bin/env python3 """Check native fault PC mapping, state/writeback and architectural RAM effects. 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_HOOK_CODE, UC_PROT_READ from unicorn import arm64_const as arm from differential import initialize_arm, capture_arm, parse_fault_record, compiler_cli, 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, guest_base=BASE, allow_completion=False, retain_error=False, cpu_model=None): machine = Uc(UC_ARCH_ARM64, UC_MODE_ARM) if cpu_model is not None: machine.ctl_set_cpu_model(cpu_model) code_page = guest_base // 4096 * 4096 machine.mem_map(code_page, 0x10000) machine.mem_write(guest_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) initialize_arm(machine, initial) 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) if allow_completion: if source[-4:] != struct.pack('= 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"}) for tail in (1, 2, 4): if tail >= 1 << size: continue for rt in (1, 31): result.append({"size": size, "opcode": 0, "rn": 0, "rt": rt, "offset": 0, "mode": "unsigned", "protection": "crossing", "crossing_bytes": tail}) 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(): cli = compiler_cli(__doc__) 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("