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 / seed / semhash / check.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # seed/semhash/check.sh: build seed/semhash/semhash.om with the pinned omc and check it. |
| 4 | # 1 keyword parity tool's table == genkw.awk word:code lists + omc.om fn kw2 (decoded here) |
| 5 | # 2 oracle omc(canon(x)) == omc(x) byte for byte, for every unit omc accepts |
| 6 | # 3 invariance alpha-renaming, notes/blanks/spaces, reordering: every fn SAME |
| 7 | # 4 controls meaning changes report CHANGED where expected (cases/*.exp) |
| 8 | # 5 mutant a copy with one keyword dropped must fail check 1 and check 2 |
| 9 | # 6 rename a fn-name-only rename reports RENAMED with an equal fn hash |
| 10 | # 7 dups dups.sh over seed/semhash/dups matches dups.exp; a hash changed past the |
| 11 | # 16-digit key must not group (full-hash compare) |
| 12 | # Prints SEMHASH PASS cases=N controls=M (exit 0), SEMHASH FAIL ... (exit 1), or |
| 13 | # SEMHASH NOT_RUN ... (exit 3) when the pinned compiler is missing or differs. |
| 14 | # Run from anywhere; paths are relative to the repository root. |
| 15 | set -u |
| 16 | cd "$(dirname "$0")/../.." || exit 3 |
| 17 | OMC=${OMC:-seed/out/omc} |
| 18 | PIN=${SEMHASH_OMC_PIN:-26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991} |
| 19 | W=${SEMHASH_OUT:-build/semhash-check} |
| 20 | C=seed/semhash/cases |
| 21 | |
| 22 | notrun() { printf 'SEMHASH NOT_RUN %s\n' "$1"; exit 3; } |
| 23 | [ -x "$OMC" ] || notrun "no compiler at $OMC" |
| 24 | GOT=$(sha256sum "$OMC" | cut -d ' ' -f 1) |
| 25 | [ "$GOT" = "$PIN" ] || notrun "compiler $OMC sha256 $GOT differs from pin $PIN" |
| 26 | printf 'omc %s sha256 %s\n' "$OMC" "$GOT" |
| 27 | |
| 28 | rm -rf "$W" |
| 29 | mkdir -p "$W/or" "$W/mut" || exit 3 |
| 30 | FAILS=0 |
| 31 | CASES=0 |
| 32 | CONTROLS=0 |
| 33 | fail() { printf 'FAIL %s\n' "$1"; FAILS=$((FAILS + 1)); } |
| 34 | |
| 35 | # build: sha256-om2 (exports made plain fns) followed by the tool; semhash.om has no om2 line |
| 36 | build() { # $1 = tool source, $2 = output binary |
| 37 | { sed 's/^export fn /fn /' om/sha256/sha256-om2.om; cat "$1"; } > "$2.full.om" || return 1 |
| 38 | "$OMC" < "$2.full.om" > "$2" 2> "$2.err" || return 1 |
| 39 | chmod +x "$2" |
| 40 | } |
| 41 | build seed/semhash/semhash.om "$W/semhash" || { printf 'SEMHASH FAIL build: %s\n' "$(cat "$W/semhash.err")"; exit 1; } |
| 42 | SH=$W/semhash |
| 43 | printf 'built %s sha256 %s\n' "$SH" "$(sha256sum "$SH" | cut -d ' ' -f 1)" |
| 44 | |
| 45 | # ---- 1 keyword parity |
| 46 | # expected list: every `n=split("word:code ...")` list of genkw.awk (two today; found by content, not |
| 47 | # line number, so a longer header comment cannot shift them) plus omc.om fn kw2 (packed word -> code) |
| 48 | awk '/^[[:space:]]*n=split\("[a-z]/{ s=$0; sub(/^[^"]*"/,"",s); sub(/".*$/,"",s); n=split(s,a," "); |
| 49 | for(i=1;i<=n;i++){ split(a[i],p,":"); print p[1], p[2] } }' seed/omc/genkw.awk > "$W/kw.exp" |
| 50 | [ "$(grep -c '^[[:space:]]*n=split("[a-z]' seed/omc/genkw.awk)" = 2 ] || |
| 51 | fail "keyword parity: genkw.awk no longer holds exactly two word:code lists" |
| 52 | unpack() { # little-endian packed word -> bytes |
| 53 | v=$1 |
| 54 | while [ "$v" -gt 0 ]; do |
| 55 | printf "\\$(printf '%03o' $((v % 256)))" |
| 56 | v=$((v / 256)) |
| 57 | done |
| 58 | } |
| 59 | awk '/^fn kw2 /{on=1; next} !on{next} /^if eq w /{w=$4; inif=1; next} |
| 60 | inif&&/^ret /{print w, $2; next} /^end$/{if(inif){inif=0; next} exit}' seed/omc/omc.om > "$W/kw2.raw" |
| 61 | [ -s "$W/kw2.raw" ] || fail "keyword parity: no fn kw2 table decoded from seed/omc/omc.om" |
| 62 | while read -r w c; do printf '%s %s\n' "$(unpack "$w")" "$c"; done < "$W/kw2.raw" >> "$W/kw.exp" |
| 63 | kwcheck() { # $1 = tool binary, $2 = tag; returns 1 on mismatch |
| 64 | "$1" keywords > "$W/$2.kw.got" 2>&1 || return 1 |
| 65 | sort -u "$W/kw.exp" > "$W/$2.kw.e" |
| 66 | sort -u "$W/$2.kw.got" > "$W/$2.kw.g" |
| 67 | cmp -s "$W/$2.kw.e" "$W/$2.kw.g" |
| 68 | } |
| 69 | if kwcheck "$SH" tool; then |
| 70 | printf 'keywords: %s words match genkw.awk + omc.om kw2\n' "$(wc -l < "$W/tool.kw.e" | tr -d ' ')" |
| 71 | else |
| 72 | fail "keyword parity: $(diff "$W/tool.kw.e" "$W/tool.kw.g" | tr '\n' ' ')" |
| 73 | fi |
| 74 | |
| 75 | # ---- 2 oracle |
| 76 | ORACLE_OK=0 |
| 77 | ORACLE_SKIP=0 |
| 78 | ORACLE_FMT=0 |
| 79 | ORACLE_PRES=0 |
| 80 | SEM_REFUSED=0 |
| 81 | oracle() { # $1 = unit, $2 = tool binary; prints a reason on failure, returns 1 |
| 82 | if ! "$OMC" < "$1" > "$W/or/a" 2> "$W/or/ae"; then |
| 83 | # refused by omc: canon must not turn it into an accepted unit |
| 84 | if "$2" canon < "$1" > "$W/or/c.om" 2> /dev/null; then |
| 85 | if "$OMC" < "$W/or/c.om" > /dev/null 2>&1; then |
| 86 | printf 'refused unit became accepted after canon'; return 1 |
| 87 | fi |
| 88 | ORACLE_PRES=$((ORACLE_PRES + 1)) |
| 89 | fi |
| 90 | ORACLE_SKIP=$((ORACLE_SKIP + 1)); return 0 |
| 91 | fi |
| 92 | if [ "$(grep -v '^[[:space:]]*note\([[:space:]()]\|$\)' "$1" | grep -v '^[[:space:]]*$' | sed -n 1p)" = 'om2 fmt' ]; then |
| 93 | ORACLE_FMT=$((ORACLE_FMT + 1)); return 0 # fmt output is the formatted source text, not code |
| 94 | fi |
| 95 | "$2" canon < "$1" > "$W/or/c.om" 2> "$W/or/ce" |
| 96 | rc=$? |
| 97 | if [ "$rc" = 3 ]; then |
| 98 | SEM_REFUSED=$((SEM_REFUSED + 1)); printf 'SEMREFUSED %s %s\n' "$1" "$(cat "$W/or/ce")" >> "$W/semrefused" |
| 99 | return 0 |
| 100 | fi |
| 101 | [ "$rc" = 0 ] || { printf 'canon exit %s' "$rc"; return 1; } |
| 102 | "$OMC" < "$W/or/c.om" > "$W/or/b" 2> "$W/or/be" || { printf 'canon output refused: %s' "$(cat "$W/or/be")"; return 1; } |
| 103 | cmp -s "$W/or/a" "$W/or/b" || { printf 'compile(canon(x)) != compile(x)'; return 1; } |
| 104 | ORACLE_OK=$((ORACLE_OK + 1)) |
| 105 | } |
| 106 | J=seed/json |
| 107 | { printf 'om2\n'; cat "$J/json.om" "$J/jsonw.om" "$J/jsonf.om"; printf 'fn main to int\nret 0\nend\n'; } > "$W/json-lib.om" |
| 108 | { printf 'om2\n'; cat "$J/json.om" "$J/jsonw.om" "$J/jsonf.om" "$J/tests/jsdrv.om"; } > "$W/json-drv.om" |
| 109 | { printf 'om2\n'; cat "$J/json.om" "$J/jsonw.om"; printf 'fn main to int\nret 0\nend\n'; } > "$W/json-nof.om" |
| 110 | find seed/rung4/tests seed/omc/contracts seed/omc/tests seed/omc/om2 seed/host/tests -name '*.om' -type f | LC_ALL=C sort > "$W/units" |
| 111 | for u in "$W/json-lib.om" "$W/json-drv.om" "$W/json-nof.om" om/blake3/blake3.om om/sha256/sha256-om2.om \ |
| 112 | seed/qualification/om2/leak/leakscan.om "$SH.full.om" $C/*.om; do |
| 113 | printf '%s\n' "$u" >> "$W/units" |
| 114 | done |
| 115 | : > "$W/semrefused" |
| 116 | while read -r u; do |
| 117 | # a redirected function call runs in this shell, so the counters stay visible |
| 118 | oracle "$u" "$SH" > "$W/or/why" || fail "oracle $u: $(cat "$W/or/why")" |
| 119 | done < "$W/units" |
| 120 | NUNITS=$(wc -l < "$W/units" | tr -d ' ') |
| 121 | printf 'oracle: units=%s identical=%s omc-refused=%s (canon kept %s of them refused) fmt-skipped=%s semhash-refused=%s\n' \ |
| 122 | "$NUNITS" "$ORACLE_OK" "$ORACLE_SKIP" "$ORACLE_PRES" "$ORACLE_FMT" "$SEM_REFUSED" |
| 123 | cat "$W/semrefused" |
| 124 | [ "$ORACLE_OK" -gt 0 ] || fail "oracle compared no units" |
| 125 | CASES=$((CASES + ORACLE_OK)) |
| 126 | |
| 127 | # ---- 3, 4, 6 fixtures: diff base against each case; stdout and exit code must match |
| 128 | for x in $C/*.om; do |
| 129 | n=$(basename "$x" .om) |
| 130 | [ "$n" = base ] && continue |
| 131 | "$SH" diff $C/base.om "$x" > "$W/$n.got" 2>&1 |
| 132 | rc=$? |
| 133 | want=1 |
| 134 | case $n in inv_*) want=0 ;; esac |
| 135 | if [ "$rc" != "$want" ]; then fail "case $n: exit $rc, want $want"; continue; fi |
| 136 | if ! cmp -s "$C/$n.exp" "$W/$n.got"; then fail "case $n: $(diff "$C/$n.exp" "$W/$n.got" | tr '\n' ' ')"; continue; fi |
| 137 | printf 'case %s: %s\n' "$n" "$(tail -n 1 "$W/$n.got")" |
| 138 | case $n in neg_*) CONTROLS=$((CONTROLS + 1)) ;; *) CASES=$((CASES + 1)) ;; esac |
| 139 | done |
| 140 | unit() { "$SH" < "$1" | sed -n 's/^UNIT \([0-9a-f]*\) .*/\1/p'; } |
| 141 | UB=$(unit $C/base.om) |
| 142 | for n in inv_alpha inv_notes; do |
| 143 | [ "$(unit $C/$n.om)" = "$UB" ] || fail "unit hash of $n differs from base" |
| 144 | done |
| 145 | [ "$(unit $C/inv_reorder.om)" != "$UB" ] || fail "unit hash of inv_reorder equals base (order is part of the unit)" |
| 146 | fh() { "$SH" < "$1" | awk -v f="$2" '$1=="FN" && $2==f {print $3}'; } |
| 147 | H1=$(fh $C/base.om other) |
| 148 | H2=$(fh $C/ren_fn.om other2) |
| 149 | if [ -n "$H1" ] && [ "$H1" = "$H2" ]; then printf 'rename: other -> other2 fn hash equal %s\n' "$H1" |
| 150 | else fail "rename: fn hash other=$H1 other2=$H2"; fi |
| 151 | |
| 152 | # ---- refusals and usage |
| 153 | printf 'fn main to int\r\nret 0\nend\n' > "$W/cr.om" |
| 154 | "$SH" < "$W/cr.om" > /dev/null 2>&1; rc=$? |
| 155 | [ "$rc" = 3 ] || fail "CR input: exit $rc, want 3" |
| 156 | printf 'fn main to int\n$v0 = 1\nret 0\nend\n' > "$W/rsv.om" |
| 157 | "$SH" < "$W/rsv.om" > /dev/null 2>&1; rc=$? |
| 158 | [ "$rc" = 3 ] || fail "reserved-byte input: exit $rc, want 3" |
| 159 | "$SH" nosuchmode < /dev/null > /dev/null 2>&1; rc=$? |
| 160 | [ "$rc" = 2 ] || fail "unknown mode: exit $rc, want 2" |
| 161 | CONTROLS=$((CONTROLS + 3)) |
| 162 | |
| 163 | # ---- 7 dups: duplicate-function report (#827). dups.sh end to end over the fixtures, then a |
| 164 | # control on the raw records: one hash digit changed past the 16-digit key must split the group. |
| 165 | D=seed/semhash/dups |
| 166 | sh seed/semhash/dups.sh "$SH" $D > "$W/dups.got" 2>&1 |
| 167 | rc=$? |
| 168 | if [ "$rc" = 0 ] && cmp -s $D/dups.exp "$W/dups.got"; then |
| 169 | printf 'dups: %s\n' "$(tail -n 1 "$W/dups.got")"; CASES=$((CASES + 1)) |
| 170 | else |
| 171 | fail "dups: exit $rc $(diff $D/dups.exp "$W/dups.got" | tr '\n' ' ')" |
| 172 | fi |
| 173 | for f in $D/a.om $D/b.om; do "$SH" < "$f" | sed -n "s|^FN |$f FN |p"; done > "$W/dups.in" |
| 174 | awk '$2 == "FN" && $3 == "sq2" { d = substr($4, 41, 1) == "0" ? "1" : "0"; $4 = substr($4, 1, 40) d substr($4, 42) } { print }' \ |
| 175 | "$W/dups.in" > "$W/dups.mut" |
| 176 | got=$("$SH" dups < "$W/dups.mut" | tail -n 1) |
| 177 | if [ "$got" = 'DUPS groups=0 fns=0 records=6 skipped=0' ]; then CONTROLS=$((CONTROLS + 1)) |
| 178 | else fail "dups control: a hash differing past the key: $got"; fi |
| 179 | |
| 180 | # ---- 5 mutant: drop one keyword from the table; check 1 and check 2 must both fail for it |
| 181 | sed 's/\\nwhile 69\\n/\\n/' seed/semhash/semhash.om > "$W/mut/semhash.om" |
| 182 | if cmp -s seed/semhash/semhash.om "$W/mut/semhash.om"; then |
| 183 | fail "mutant: sed did not change the keyword table" |
| 184 | elif ! build "$W/mut/semhash.om" "$W/mut/semhash"; then |
| 185 | fail "mutant does not build: $(cat "$W/mut/semhash.err")" |
| 186 | else |
| 187 | k1=alive |
| 188 | kwcheck "$W/mut/semhash" mut || k1=killed |
| 189 | k2=alive |
| 190 | while read -r u; do |
| 191 | oracle "$u" "$W/mut/semhash" > /dev/null || { k2=killed; break; } |
| 192 | done < "$W/units" |
| 193 | printf 'mutant (while dropped): keyword-parity %s, oracle %s\n' "$k1" "$k2" |
| 194 | if [ "$k1" = killed ] && [ "$k2" = killed ]; then printf 'mutant killed\n'; CONTROLS=$((CONTROLS + 1)) |
| 195 | else fail "mutant survived (keyword-parity $k1, oracle $k2)"; fi |
| 196 | fi |
| 197 | |
| 198 | if [ "$FAILS" = 0 ]; then |
| 199 | printf 'SEMHASH PASS cases=%s controls=%s\n' "$CASES" "$CONTROLS" |
| 200 | exit 0 |
| 201 | fi |
| 202 | printf 'SEMHASH FAIL failures=%s cases=%s controls=%s\n' "$FAILS" "$CASES" "$CONTROLS" |
| 203 | exit 1 |