#!/usr/bin/env python3 """Report Apple-silicon translation evidence without inventing a completion denominator.""" import argparse from collections import Counter from datetime import datetime, timezone import hashlib import json import os from pathlib import Path import struct import subprocess import tempfile from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM from evidence import write_evidence import a64_checklist import apple_isa ROOT = Path(__file__).resolve().parents[1] STATUSES = ('implemented', 'partial', 'unsupported') RET = 0xd65f03c0 BLOCK_ABI = 'x86_64-sysv-guest-state-v2-block-exit-direct-ram' SERVICE_ABI = BLOCK_ABI + '-apple-arm64e-software-pauth-clock-v1' def sha256(path): return hashlib.sha256(Path(path).read_bytes()).hexdigest() def repository_path(value): if not isinstance(value, str) or not value: raise ValueError('Evidence/source paths must be nonempty repository-relative paths') path = Path(value) if path.is_absolute() or '..' in path.parts: raise ValueError('Evidence/source paths must stay inside the repository') return path def digest(value): if not isinstance(value, str) or len(value) != 64 or any(c not in '0123456789abcdef' for c in value): raise ValueError('Expected a lowercase SHA-256 identity') return value def validate_registry(registry): if registry.get('schema_version') != 2 or registry.get('kind') != 'reviewed_apple_silicon_instruction_forms': raise ValueError('Unsupported Apple-silicon reviewed-form registry') target = registry.get('target_profile', {}) if (target.get('vendor') != 'Apple' or target.get('guest_architectures') != ['arm64', 'arm64e'] or target.get('host_architecture') != 'x86_64'): raise ValueError('Registry must target Apple arm64/arm64e to native x86_64') if target.get('required_isa_inventory_complete') is not False or target.get('required_isa_form_count') is not None: raise ValueError('An incomplete reviewed list cannot establish a required Apple ISA denominator') if not registry.get('registry_version') or not registry.get('instruction_sources') or not target.get('inventory_limits'): raise ValueError('Reviewed forms require versioned source facts and inventory limits') profile = registry.get('profile', {}).get('id') if not profile or not registry.get('profile', {}).get('outside_scope'): raise ValueError('Support needs an explicit bounded execution profile') evidence = registry.get('evidence_groups', {}) rows = {} for row in registry.get('forms', []): identity = row.get('id') if not isinstance(identity, str) or not identity or identity in rows: raise ValueError('Reviewed form IDs must be unique and nonempty') if row.get('alias') is not False: raise ValueError('Aliases cannot inflate reviewed support counts') if not row.get('category') or not row.get('source_file') or not row.get('source_sha256'): raise ValueError('Reviewed forms need their instruction source facts') digest(row['source_sha256']) mask, value = int(row['encoding_mask'], 16), int(row['encoding_value'], 16) if not 0 <= mask <= 0xffffffff or not 0 <= value <= 0xffffffff or value & ~mask: raise ValueError('Invalid reviewed encoding mask/value') if row.get('status') not in STATUSES: raise ValueError('Unknown support status') if row.get('profile') != profile or not row.get('scope'): raise ValueError('Form support scope/profile missing') if row['status'] != 'unsupported': if not row.get('review') or not row.get('compiler_sources') or not row.get('smoke_samples'): raise ValueError('Support requires reviewed implementation and smoke samples') if not row.get('evidence_groups') or any(group not in evidence for group in row['evidence_groups']): raise ValueError('Support requires a native differential evidence group') for source in row['compiler_sources']: repository_path(source) for sample in row['smoke_samples']: validate_sample(row, sample) if row['status'] == 'partial' and not row.get('rejected_samples'): raise ValueError('Partial forms need a rejected scope-boundary sample') rows[identity] = row return rows def validate_sample(form, sample): word = int(sample['word'], 16) if not 0 <= word <= 0xffffffff or word & int(form['encoding_mask'], 16) != int(form['encoding_value'], 16): raise ValueError(f'Smoke does not match canonical form: {form["id"]}') # Official byte-register forms distinguish an extended operand from LSL, # although the extended form's !=011 constraint cannot fit a fixed mask. reguse = form.get('docvars', {}).get('reguse') if form['id'].endswith('_ldst_regoff') else None if reguse == 'ext-reg' and ((word >> 13) & 7) not in (2, 6, 7): raise ValueError('Extended-register smoke has the wrong operand form') if reguse == 'shifted-reg' and ((word >> 13) & 7) != 3: raise ValueError('Shifted-register smoke has the wrong operand form') expected = sample.get('expected_mnemonics') if not isinstance(expected, list) or not expected: raise ValueError('Smoke needs an independent decoded mnemonic expectation') consumed = sample.get('consumed_source_bytes', 4) if type(consumed) is not int or consumed not in (4, 8): raise ValueError('Smoke must declare a valid instruction/RET boundary') return word def summarize(rows): categories = {} overall = Counter({status: 0 for status in STATUSES}) for row in rows.values(): counts = categories.setdefault(row['category'], Counter({status: 0 for status in STATUSES})) counts[row['status']] += 1 overall[row['status']] += 1 def finish(counts): return {**dict(counts), 'reviewed_forms': sum(counts.values())} return {'categories': {key: finish(counts) for key, counts in sorted(categories.items())}, 'reviewed': finish(overall), 'required_isa_forms': None, 'required_isa_completion_percentage': None, 'overall_completion_percentage': None, 'complete_binaries': {'verified': 0, 'full_binary_harness_available': False, 'required_total': None, 'completion_percentage': None, 'evidence_requirement': 'Stela full Mach-O load and native execution with validated process behavior; leaf fixtures do not qualify.'}} def compile_probe(cli, word, directory, expect_failure=None, run=subprocess.run, mode='leaf', expected_source_bytes=4): if mode not in ('leaf', 'block', 'service'): raise ValueError('Unknown compiler probe mode') if type(expected_source_bytes) is not int or expected_source_bytes not in (4, 8): raise ValueError('Invalid declared compiler probe boundary') source = directory / 'source.arm64' source.write_bytes(struct.pack('' if text.count(start) != 1 or text.count(end) != 1: raise ValueError('README needs exactly one progress marker pair') first, last = text.index(start) + len(start), text.index(end) if first > last: raise ValueError('README progress markers reversed') updated = text[:first] + '\n' + fragment.rstrip() + '\n' + text[last:] if path.read_text() != text: raise ValueError('README changed during update') write_markdown(path, updated) def previous_checkpoint(path, root=ROOT): """Smoke-only updates preserve an archived verified baseline without advancing it.""" path = Path(path) if not path.exists(): return None data = path.read_bytes() previous = json.loads(data) digest = hashlib.sha256(data).hexdigest() archived = root / '.stela/evidence' / path.stem / (digest + '.json') if not archived.exists() or archived.read_bytes() != data: raise ValueError('Previous checkpoint is not bound to its evidence archive') if previous.get('implementation_only'): return previous.get('last_verified_checkpoint') if previous.get('kind') != 'apple_silicon_translation_progress' or not previous.get('native_differential_evidence'): return None row = previous['apple_codegen_candidates']['summary']['union'] return {'implemented': row['implemented'], 'total': row['total'], 'compiler_source_sha256': previous['compiler_source_sha256'], 'measured_at_utc': previous['measured_at_utc'], 'report_sha256': digest} def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--registry', type=Path, default=ROOT / 'tests/isa/apple-silicon-reviewed-forms.json') parser.add_argument('--block-registry', type=Path, default=ROOT / 'tests/isa/apple-silicon-block-forms.json') parser.add_argument('--service-registry', type=Path, default=ROOT / 'tests/isa/a64-service-forms.json') parser.add_argument('--checklist', type=Path, default=ROOT / 'tests/isa/a64-documented-checklist.json') parser.add_argument('--apple-profiles', type=Path, default=ROOT / 'tests/isa/apple-codegen-profiles.json') parser.add_argument('--cli', type=Path, default=ROOT / 'target/debug/stela') parser.add_argument('--output', type=Path, default=ROOT / '.stela/progress/apple-silicon.json') parser.add_argument('--markdown-output', type=Path) parser.add_argument('--update-readme', action='store_true') parser.add_argument('--implementation-only', action='store_true', help='Refresh implementation/emission counts while retaining the full verification checkpoint') args = parser.parse_args() checkpoint = previous_checkpoint(args.output) if args.implementation_only else None registry_data = args.registry.read_bytes() registry = json.loads(registry_data) rows = validate_registry(registry) block_data = args.block_registry.read_bytes() block_registry = json.loads(block_data) block_rows = validate_registry(block_registry) service_data = args.service_registry.read_bytes() service_registry = json.loads(service_data) service_rows = validate_registry(service_registry) catalog_data = args.checklist.read_bytes() catalog = json.loads(catalog_data) canonical_facts = a64_checklist.validate(catalog) apple_profiles_data = args.apple_profiles.read_bytes() apple_profiles = json.loads(apple_profiles_data) apple = json.loads((ROOT / 'tests/isa/apple-specific-extensions.json').read_bytes()) if apple.get('kind') != 'apple_specific_extension_ledger' or apple.get('inventory_complete') is not False: raise ValueError('Apple additions need a separate explicitly open inventory') if len({r['id'] for r in apple['extensions']}) != len(apple['extensions']) or any(r['status'] not in STATUSES for r in apple['extensions']): raise ValueError('Invalid Apple extension identity/status') if (block_registry['profile'].get('compiler_command') != 'compile-block' or block_registry['profile'].get('abi') != BLOCK_ABI): raise ValueError('Block registry must declare the native block-exit compiler ABI') executable_hash = sha256(args.cli) with tempfile.TemporaryDirectory(prefix='stela-apple-progress-') as temporary: compiler, probes = smoke_forms(args.cli, rows, rows, Path(temporary), canonical_facts=canonical_facts) block_directory = Path(temporary) / 'blocks' block_directory.mkdir() block_compiler, block_probes = smoke_forms(args.cli, block_rows, block_rows, block_directory, mode='block', canonical_facts=canonical_facts) service_directory = Path(temporary) / 'services' service_directory.mkdir() service_compiler, service_probes = smoke_forms(args.cli, service_rows, service_rows, service_directory, mode='service', canonical_facts=canonical_facts) if block_compiler != compiler or service_compiler != compiler: raise ValueError('Leaf and block compiler identities differ') if args.implementation_only: evidence, block_evidence, service_evidence = {}, {}, {} sources, block_sources, service_sources = [ {path: sha256(ROOT / path) for row in group.values() for path in row['compiler_sources']} for group in (rows, block_rows, service_rows)] else: evidence, sources = validate_evidence(registry, rows, compiler) block_evidence, block_sources = validate_evidence(block_registry, block_rows, compiler, executable_hash=executable_hash) service_evidence, service_sources = validate_evidence(service_registry, service_rows, compiler, executable_hash=executable_hash) assessed, documented_summary = a64_checklist.assess(catalog, [registry, block_registry, service_registry]) apple_summary = apple_isa.assess(apple_profiles, assessed) assessed['compiler_source_sha256'] = compiler assessed['assessment_basis'] = 'reviewed_implementation_and_emission_smoke' if args.implementation_only else 'current_native_differential_evidence' assessed['last_verified_checkpoint'] = checkpoint assessed['evidence_reports'] = [{k: row[k] for k in ('report', 'report_sha256')} for group in (evidence, block_evidence, service_evidence) for row in group.values()] fingerprint_sources = validate_current_compiler(registry, compiler) if validate_current_compiler(block_registry, compiler) != fingerprint_sources: raise ValueError('Leaf and block fingerprint recipes differ') if (sha256(args.cli) != executable_hash or args.registry.read_bytes() != registry_data or args.block_registry.read_bytes() != block_data or args.service_registry.read_bytes() != service_data or args.checklist.read_bytes() != catalog_data or args.apple_profiles.read_bytes() != apple_profiles_data or validate_current_compiler(registry, compiler) != fingerprint_sources or validate_current_compiler(block_registry, compiler) != fingerprint_sources): raise ValueError('Compiler/registry changed during measurement') report = {'schema_version': 2, 'kind': 'apple_silicon_translation_progress', 'implementation_only': args.implementation_only, 'last_verified_checkpoint': checkpoint, 'measured_at_utc': datetime.now(timezone.utc).isoformat(timespec='seconds').replace('+00:00', 'Z'), 'registry_version': registry['registry_version'], 'target_profile': registry['target_profile'], 'instruction_sources': registry['instruction_sources'], 'registry_sha256': hashlib.sha256(registry_data).hexdigest(), 'compiler_source_sha256': compiler, 'compiler_executable_sha256': executable_hash, 'harness_source_sha256': sha256(__file__), 'compiler_sources_sha256': sources, 'compiler_fingerprint_sources_sha256': fingerprint_sources, 'profile': registry['profile'], 'summary': summarize(rows), 'documented_isa': {'version': catalog['version'], 'facts_sha256': catalog['facts_sha256'], 'summary': documented_summary}, 'apple_codegen_candidates': {'version': apple_profiles['version'], 'facts_sha256': apple_profiles['facts_sha256'], 'inventory_sha256': hashlib.sha256(apple_profiles_data).hexdigest(), 'harness_source_sha256': sha256(apple_isa.__file__), 'summary': apple_summary, 'inventory_complete': False, 'hardware_membership_verified': False}, 'apple_specific_extensions': {'tracked': len(apple['extensions']), 'implemented': sum(r['status'] == 'implemented' for r in apple['extensions']), 'inventory_complete': False}, 'service_profile_evidence': {'registry_sha256': hashlib.sha256(service_data).hexdigest(), 'native_differential_evidence': service_evidence, 'compiler_sources_sha256': service_sources, 'compiler_smoke_probes': service_probes}, 'block_profile_evidence': { 'registry_version': block_registry['registry_version'], 'registry_sha256': hashlib.sha256(block_data).hexdigest(), 'profile': block_registry['profile'], 'summary': summarize(block_rows), 'overlap_with_leaf_form_ids': sorted(set(rows) & set(block_rows)), 'counts_are_not_additive': True, 'compiler_sources_sha256': block_sources, 'native_differential_evidence': block_evidence, 'compiler_smoke_probes': block_probes, 'reviewed_forms': [{'id': identity, 'status': row['status'], 'scope': row['scope']} for identity, row in block_rows.items()]}, 'native_differential_evidence': evidence, 'compiler_smoke_probes': probes, 'reviewed_forms': [{'id': identity, 'status': row['status'], 'scope': row['scope']} for identity, row in rows.items()], 'limits': 'Headline instruction percentage uses pinned public LLVM Apple M1–M5 code-generation candidate forms mapped to the June2026 Arm reference, deduplicated across reviewed execution profiles. Full Arm reference coverage remains secondary. Partial forms earn no credit. These models do not prove complete Apple hardware/mode/operand availability; M6, Apple-private additions and full CPU/VM conformance remain open. Differential tests establish evidence, not exhaustive correctness or complete application/macOS execution.'} archive = write_evidence(args.output, report) if args.markdown_output: write_markdown(args.markdown_output, markdown(report)) if args.update_readme: a64_checklist.write(args.checklist, assessed) update_readme(ROOT / 'README.md', markdown(report)) print(json.dumps({'report': str(args.output), 'archive': str(archive), 'compiler_source_sha256': compiler, 'summary': report['summary'], 'block_profile_summary': report['block_profile_evidence']['summary']}, indent=2)) if __name__ == '__main__': main()