#!/usr/bin/env python3 """Compare integer LDP/STP/LDPSW native code with ARM and all private RAM.""" 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(opcode, loading, rt, rt2, rn, offset, mode): element_bytes = 8 if opcode == 2 else 4 assert offset % element_bytes == 0 and -64 <= offset // element_bytes <= 63 index = {"post": 1, "offset": 2, "pre": 3}[mode] return (opcode << 30) | 0x28000000 | (index << 23) | (int(loading) << 22) | (((offset // element_bytes) & 127) << 15) | (rt2 << 10) | (rn << 5) | rt def cases(): result = [] decoder = Cs(CS_ARCH_ARM64, CS_MODE_ARM) for opcode, loading in ((0, False), (0, True), (2, False), (2, True), (1, True)): element_bytes = 8 if opcode == 2 else 4 for mode in ("offset", "pre", "post"): for scaled_offset in (-64, -1, 0, 1, 63): for rn, rt, rt2 in ((0, 1, 2), (7, 29, 30), (31, 0, 30), (31, 31, 7), (31, 7, 31), (0, 31, 1), (0, 1, 31), (0, 0, 2), (0, 1, 0), (7, 29, 29), (31, 31, 31)): if (loading and rt == rt2) or (mode != "offset" and rn != 31 and rn in (rt, rt2)): continue offset = scaled_offset * element_bytes word = encoding(opcode, loading, rt, rt2, rn, offset, mode) instruction = next(decoder.disasm(struct.pack("= deadline: raise AssertionError(f"Native pair 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"Pair 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-pairs-") 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 pair suite") compiler = manifest["compiler_source_sha256"] case["native"] = (output / "native.bin").read_bytes() result = bounded_cases(compiled) report = {"kind": "integer_pairs_arm_vs_native_x86", "passed": True, **result, "reference": "Unicorn ARM64 2.1.4", "native": "direct x86-64 calls in isolated worker", "tested_state": ["V0..V31, FPCR and FPSR preservation", "x0..x30", "SP", "all 16 NZCV combinations", "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": "Private ordinary RAM with identical guest/native addresses and enabled SP alignment checks. No concurrency or FEAT_LSE2 single-copy atomicity, MMIO, SIMD instructions, guest exception delivery or OS boot. Fault tests are separate."} write_evidence(ROOT / ".stela/pair-differential-report.json", report) print(json.dumps(report, indent=2)) if __name__ == "__main__": main()