native_callback_frontend.py
verification/tools/native_callback_frontend.py
1#!/usr/bin/env python32"""Source-frozen actual callback frontend diagnostic; no physical proof ticket."""3from pathlib import Path4import datetime5import fcntl6import json7import re8import subprocess9import uuid1011import creusot12import creusot_frontend13import provider_resource14import run_receipt1516ROOT = creusot.ROOT17PROOF = ROOT / 'verification/proofs/provider/creusot/native_callback_frontend.rs'18MEMORY = provider_resource.MEMORY19REGION = MEMORY.parent / 'memory/region.rs'20ERROR = MEMORY.parent / 'memory/error.rs'21POOL = provider_resource.POOL222324def annotate(source):25 marker = '#[creusot_std::prelude::trusted]'26 creusot.require(marker not in source, 'Product source already annotated')27 staged = re.sub(r'(?m)^([ \t]*)((?:pub(?:\([^)]*\))? )?(?:unsafe )?(?:const )?fn )',28 lambda m: m[1] + marker + '\n' + m[1] + m[2], source)29 staged = re.sub(r'(?m)^([ \t]*)((?:pub )?const (?!fn\b))',30 lambda m: m[1] + marker + '\n' + m[1] + '#[creusot_std::prelude::constant(eval)]\n' + m[1] + m[2], staged)31 staged = re.sub(r'(#\[derive\([^\n]*PartialEq[^\n]*)(\)\])', r'\1, creusot_std::model::DeepModel\2', staged)32 recovered = staged.replace(', creusot_std::model::DeepModel', '')33 recovered = re.sub(r'(?m)^[ \t]*#\[creusot_std::prelude::(?:trusted|constant\(eval\))\]\n', '', recovered)34 creusot.require(recovered == source, 'Frontend staging changed SDK bytes')35 return staged363738def stage(output):39 (output / 'src/memory').mkdir(parents=True)40 (output / 'src/lib.rs').write_text('#![feature(const_destruct)]\n#![allow(dead_code,unexpected_cfgs)]\nmod memory;\n')41 (output / 'src/memory.rs').write_text(annotate(MEMORY.read_text())42 + '\n#[path="native_callback_frontend.rs"] mod native_callback_frontend;\n')43 (output / 'src/native_callback_frontend.rs').write_bytes(PROOF.read_bytes())44 for source in (REGION, ERROR, POOL):45 (output / 'src/memory' / source.name).write_text(annotate(source.read_text()))46 for name in ('Cargo.toml', 'Cargo.lock', 'why3find.json'):47 (output / name).write_bytes((creusot.PROOF_DIR / name).read_bytes())48 return {str(p.relative_to(output)): creusot.sha(p) for p in output.rglob('*') if p.is_file()}495051def validate_rejection(log, code):52 creusot_frontend.require_compile_rejection(log, code,53 'forbidden dyn type: dyn memory::region::RetainedBounds '54 '(dyn support is currently minimal, please open an issue to improve this feature)')555657def run():58 run_receipt.checkout(ROOT)59 creusot.validate_source()60 binaries = creusot.validate_binaries()61 inputs = [PROOF, MEMORY, REGION, ERROR, POOL, Path(__file__), Path(creusot.__file__),62 Path(provider_resource.__file__), *run_receipt.INPUTS,63 Path(creusot.__file__).with_suffix('.toml'), Path(creusot.__file__).with_suffix('.nix'),64 *creusot.FRONTEND_PATCHES,65 *[creusot.PROOF_DIR / name for name in ('Cargo.toml', 'Cargo.lock', 'why3find.json')]]66 inputs += [p for p in (ROOT / 'verification/tools').iterdir()67 if p.is_file() and p.suffix in ('.py', '.toml', '.nix', '.patch')]68 inputs = list(dict.fromkeys(inputs))69 before = {str(p.relative_to(ROOT)): creusot.sha(p) for p in inputs}70 output = creusot.TEMP / ('native-callback-frontend-' + datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ') + '-' + uuid.uuid4().hex[:8])71 output.mkdir()72 staged = stage(output)73 receipt = run_receipt.write(ROOT, output, {74 'sources': before, 'staged': staged, 'binaries': binaries,75 'scope': 'actual VecMemory/RetainedRegion/dyn RetainedBounds and concrete closure strict-erasure frontend feasibility; no proof ticket',76 })77 command = [creusot.TOOL / 'bin/cargo-creusot', 'creusot', '--erasure-check=error',78 '--only=coma', 'memory/native_callback_frontend', '--', '--locked', '--offline']79 print('Native callback frontend: ' + str(output), flush=True)80 with (output / 'run.log').open('w') as log:81 result = subprocess.run([str(x) for x in command], cwd=output,82 env=creusot.environment(output), stdout=log, stderr=subprocess.STDOUT)83 receipt.verify()84 creusot.require(before == {str(p.relative_to(ROOT)): creusot.sha(p) for p in inputs}, 'Frontend inputs changed')85 creusot.require(creusot.validate_binaries() == binaries, 'Frontend pinned tools changed')86 validate_rejection((output / 'run.log').read_text(), result.returncode)87 print(json.dumps({'status': 'BLOCKED_FRONTEND', 'compileExit': result.returncode,88 'output': str(output)}, indent=2), flush=True)89 return output, result.returncode909192if __name__ == '__main__':93 with creusot.cache_lease():94 with (creusot.TEMP / '.creusot-runner.lock').open('w') as lock:95 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)96 run()