#!/usr/bin/env python3 """Compare direct native RAM accesses with original ARM instructions and RAM. Guest pointers equal the native mapping's addresses in both engines. Neither engine maps the other's state or executable code into the guest RAM region. Native execution is isolated in a bounded subprocess; crashes fail the suite. """ import ctypes import hashlib import json import mmap import multiprocessing from pathlib import Path import random import struct import subprocess import tempfile import time from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM from differential import compiler_cli, ROOT, MASK, State, arithmetic, move, native, reference, states from evidence import write_evidence RAM_SIZE = 0x10000 RET = 0xd65f03c0 def encoding(size, opcode, rt, rn, offset, mode): if mode == "unsigned": assert offset % (1 << size) == 0 return (size << 30) | 0x39000000 | (opcode << 22) | ((offset >> size) << 10) | (rn << 5) | rt assert -256 <= offset <= 255 indexing = {"unscaled": 0, "post": 1, "pre": 3}[mode] return (size << 30) | 0x38000000 | (opcode << 22) | ((offset & 511) << 12) | (indexing << 10) | (rn << 5) | rt def cases(): result = [] disassembler = Cs(CS_ARCH_ARM64, CS_MODE_ARM) 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 in ("unsigned", "unscaled", "pre", "post"): offsets = (0, 1 << size, 4095 << size) if mode == "unsigned" else (-256, -1, 0, 255) for offset in offsets: for rn, rt in ((0, 1), (7, 30), (31, 0), (31, 31), (0, 31), (7, 7)): if mode in ("pre", "post") and rn == rt and rn != 31: continue word = encoding(size, opcode, rt, rn, offset, mode) instruction = next(disassembler.disasm(struct.pack("= deadline: raise AssertionError(f"Native memory worker failed or timed out: exit={process.exitcode}") result = receive.recv() process.join(timeout=2) if process.exitcode != 0 or "error" in result: raise AssertionError(f"Memory suite failed: {result}, exit={process.exitcode}") finally: if process.is_alive(): process.kill() process.join() receive.close() return result def main(): cli = compiler_cli(__doc__) compiled = cases() compiler = None corpus = hashlib.sha256() with tempfile.TemporaryDirectory(prefix="stela-memory-") as temporary: directory = Path(temporary) for index, case in enumerate(compiled): source = struct.pack("<" + "I" * len(case["words"]), *case["words"]) corpus.update(source) input_path = directory / "source.bin" input_path.write_bytes(source) output = directory / str(index) manifest = json.loads(subprocess.check_output([str(cli), "compile-leaf", str(input_path), "--output-dir", str(output)])) if compiler is not None and compiler != manifest["compiler_source_sha256"]: raise AssertionError("Compiler changed during suite") compiler = manifest["compiler_source_sha256"] case["native"] = (output / "native.bin").read_bytes() result = bounded_cases(compiled) report = {"kind": "direct_ram_arm_vs_native_x86", "passed": True, **result, "reference": "Unicorn ARM64 2.1.4", "native": "direct x86-64 SysV calls in isolated worker", "tested_state": ["V0..V31, FPCR and FPSR preservation", "x0..x30", "SP", "NZCV", "all 65536 mapped RAM bytes"], "corpus_sha256": corpus.hexdigest(), "compiler_source_sha256": compiler, "compiler_executable_sha256": hashlib.sha256(cli.read_bytes()).hexdigest(), "harness_source_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "shared_runner_sha256": hashlib.sha256((ROOT / "dev/differential.py").read_bytes()).hexdigest(), "limits": "Ordinary private RAM, identical guest/native addresses, aligned incoming SP, tags/PAC disabled. Fault tests are separate. No MMIO, atomics, concurrency, SIMD instructions, guest exception delivery or OS boot."} write_evidence(ROOT / ".stela/memory-differential-report.json", report) print(json.dumps(report, indent=2)) if __name__ == "__main__": main()