omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / max-interop / test_contract_check.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # Test for tools/max-interop/contract_check.om (#680). |
| 4 | # Builds the checker with seed/out/omc, then: |
| 5 | # - every frozen contract in spec/max-interop/*.contract is accepted (mode=check) |
| 6 | # and its printed id equals its sha256 and the id frozen in the checker; |
| 7 | # - a contract with one byte changed is refused; |
| 8 | # - a manifest naming a frozen contract is accepted, one with a changed |
| 9 | # contract hash is refused; |
| 10 | # - mutants that skip the frozen-hash comparison are caught by the checks above. |
| 11 | # Usage: sh tests/max-interop/test_contract_check.sh (from any directory) |
| 12 | set -eu |
| 13 | cd "$(dirname "$0")/../.." |
| 14 | OMC=${OMC:-seed/out/omc} |
| 15 | W=$(mktemp -d "${TMPDIR:-/tmp}/cc680.XXXXXX") |
| 16 | trap 'rm -rf "$W"' EXIT HUP INT TERM |
| 17 | SRC=tools/max-interop/contract_check.om |
| 18 | fail=0 |
| 19 | ok() { echo "PASS $*"; } |
| 20 | bad() { echo "FAIL $*"; fail=1; } |
| 21 | |
| 22 | build() { # src out |
| 23 | "$OMC" < "$1" > "$2" && [ -s "$2" ] && chmod +x "$2" |
| 24 | } |
| 25 | |
| 26 | frozen_id() { # contract basename -> id compiled into the checker |
| 27 | case $1 in |
| 28 | matmul-bf16-v1.contract) sed -n 's/^text mid "\([0-9a-f]*\)"$/\1/p' "$SRC" ;; |
| 29 | gqa-causal-v1.contract) sed -n 's/^text gid "\([0-9a-f]*\)"$/\1/p' "$SRC" ;; |
| 30 | esac |
| 31 | } |
| 32 | |
| 33 | manifest() { # op contract-id |
| 34 | printf 'mode=manifest\nexpect=%s\nomega-input-manifest/1\nop=%s\ncontract=%s\nfixture=f1\ninput_sha256=%s\nseed=1\ncount=4\n' \ |
| 35 | "$2" "$1" "$2" e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 |
| 36 | } |
| 37 | |
| 38 | flip() { # hex string -> same string with its first digit changed |
| 39 | first=$(printf %.1s "$1"); rest=${1#?} |
| 40 | if [ "$first" = 0 ]; then printf '1%s' "$rest"; else printf '0%s' "$rest"; fi |
| 41 | } |
| 42 | |
| 43 | # suite BIN: returns 0 when every check passes |
| 44 | suite() { |
| 45 | cc=$1; s=0 |
| 46 | n=0 |
| 47 | for f in spec/max-interop/*.contract; do |
| 48 | n=$((n + 1)) |
| 49 | b=$(basename "$f"); h=$(sha256sum "$f" | cut -d' ' -f1); fz=$(frozen_id "$b") |
| 50 | out=$( { echo mode=check; cat "$f"; } | "$cc") && rc=0 || rc=$? |
| 51 | if [ "$rc" = 0 ] && [ "$out" = "OK $h" ] && [ "$h" = "$fz" ]; then ok "accept $b $h"; else bad "accept $b rc=$rc out=$out sha=$h frozen=$fz"; s=1; fi |
| 52 | # one byte changed: last value byte before the final newline |
| 53 | { echo mode=check; head -c -2 "$f"; printf 'X\n'; } > "$W/m.in" |
| 54 | out=$("$cc" < "$W/m.in") && rc=0 || rc=$? |
| 55 | case "$rc:$out" in 1:REFUSED*) ok "refuse one-byte-changed $b ($out)";; *) bad "one-byte-changed $b accepted rc=$rc out=$out"; s=1;; esac |
| 56 | op=$(sed -n 's/^op=//p' "$f") |
| 57 | out=$(manifest "$op" "$h" | "$cc") && rc=0 || rc=$? |
| 58 | if [ "$rc" = 0 ] && [ "$out" = "OK $h" ]; then ok "manifest frozen $b"; else bad "manifest frozen $b rc=$rc out=$out"; s=1; fi |
| 59 | hx=$(flip "$h") |
| 60 | out=$(manifest "$op" "$hx" | "$cc") && rc=0 || rc=$? |
| 61 | case "$rc:$out" in 1:REFUSED*) ok "refuse changed-hash $b ($out)";; *) bad "changed-hash $b accepted rc=$rc out=$out"; s=1;; esac |
| 62 | done |
| 63 | [ "$n" -ge 2 ] || { bad "expected >=2 frozen contracts, found $n"; s=1; } |
| 64 | return $s |
| 65 | } |
| 66 | |
| 67 | build "$SRC" "$W/cc" || { echo "FAIL build $SRC"; exit 1; } |
| 68 | echo "omc=$(sha256sum "$OMC" | cut -d' ' -f1) checker=$(sha256sum "$W/cc" | cut -d' ' -f1)" |
| 69 | suite "$W/cc" || fail=1 |
| 70 | |
| 71 | # Mutants: skip the frozen-id byte comparison (compare the value with itself). |
| 72 | for site in mid gid; do |
| 73 | from="if ne vbyte i j get $site j" |
| 74 | c=$(grep -c -F -x "$from" "$SRC" || true) |
| 75 | [ "$c" = 1 ] || { bad "mutant $site: site found $c times"; continue; } |
| 76 | awk -v f="$from" '$0==f{print "if ne vbyte i j vbyte i j"; next} {print}' "$SRC" > "$W/mut.om" |
| 77 | build "$W/mut.om" "$W/mut" || { bad "mutant $site: build failed"; continue; } |
| 78 | if (suite "$W/mut") > "$W/mut.log" 2>&1; then bad "mutant skip-hash-$site SURVIVED"; else ok "mutant skip-hash-$site caught: $(grep -c ^FAIL "$W/mut.log") check(s) failed"; fi |
| 79 | done |
| 80 | |
| 81 | [ "$fail" = 0 ] && echo "CONTRACT-CHECK PASS" || { echo "CONTRACT-CHECK FAIL"; exit 1; } |