interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / semhash / check.sh

203 lines · 10151 bytes Raw Blame History
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.
15set -u
16cd "$(dirname "$0")/../.." || exit 3
17OMC=${OMC:-seed/out/omc}
18PIN=${SEMHASH_OMC_PIN:-26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991}
19W=${SEMHASH_OUT:-build/semhash-check}
20C=seed/semhash/cases
21
22notrun() { printf 'SEMHASH NOT_RUN %s\n' "$1"; exit 3; }
23[ -x "$OMC" ] || notrun "no compiler at $OMC"
24GOT=$(sha256sum "$OMC" | cut -d ' ' -f 1)
25[ "$GOT" = "$PIN" ] || notrun "compiler $OMC sha256 $GOT differs from pin $PIN"
26printf 'omc %s sha256 %s\n' "$OMC" "$GOT"
27
28rm -rf "$W"
29mkdir -p "$W/or" "$W/mut" || exit 3
30FAILS=0
31CASES=0
32CONTROLS=0
33fail() { 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
36build() { # $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}
41build seed/semhash/semhash.om "$W/semhash" || { printf 'SEMHASH FAIL build: %s\n' "$(cat "$W/semhash.err")"; exit 1; }
42SH=$W/semhash
43printf '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)
48awk '/^[[: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"
52unpack() { # 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}
59awk '/^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"
62while read -r w c; do printf '%s %s\n' "$(unpack "$w")" "$c"; done < "$W/kw2.raw" >> "$W/kw.exp"
63kwcheck() { # $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}
69if kwcheck "$SH" tool; then
70 printf 'keywords: %s words match genkw.awk + omc.om kw2\n' "$(wc -l < "$W/tool.kw.e" | tr -d ' ')"
71else
72 fail "keyword parity: $(diff "$W/tool.kw.e" "$W/tool.kw.g" | tr '\n' ' ')"
73fi
74
75# ---- 2 oracle
76ORACLE_OK=0
77ORACLE_SKIP=0
78ORACLE_FMT=0
79ORACLE_PRES=0
80SEM_REFUSED=0
81oracle() { # $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}
106J=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"
110find 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"
111for 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"
114done
115: > "$W/semrefused"
116while 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")"
119done < "$W/units"
120NUNITS=$(wc -l < "$W/units" | tr -d ' ')
121printf '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"
123cat "$W/semrefused"
124[ "$ORACLE_OK" -gt 0 ] || fail "oracle compared no units"
125CASES=$((CASES + ORACLE_OK))
126
127# ---- 3, 4, 6 fixtures: diff base against each case; stdout and exit code must match
128for 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
139done
140unit() { "$SH" < "$1" | sed -n 's/^UNIT \([0-9a-f]*\) .*/\1/p'; }
141UB=$(unit $C/base.om)
142for n in inv_alpha inv_notes; do
143 [ "$(unit $C/$n.om)" = "$UB" ] || fail "unit hash of $n differs from base"
144done
145[ "$(unit $C/inv_reorder.om)" != "$UB" ] || fail "unit hash of inv_reorder equals base (order is part of the unit)"
146fh() { "$SH" < "$1" | awk -v f="$2" '$1=="FN" && $2==f {print $3}'; }
147H1=$(fh $C/base.om other)
148H2=$(fh $C/ren_fn.om other2)
149if [ -n "$H1" ] && [ "$H1" = "$H2" ]; then printf 'rename: other -> other2 fn hash equal %s\n' "$H1"
150else fail "rename: fn hash other=$H1 other2=$H2"; fi
151
152# ---- refusals and usage
153printf '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"
156printf '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"
161CONTROLS=$((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.
165D=seed/semhash/dups
166sh seed/semhash/dups.sh "$SH" $D > "$W/dups.got" 2>&1
167rc=$?
168if [ "$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))
170else
171 fail "dups: exit $rc $(diff $D/dups.exp "$W/dups.got" | tr '\n' ' ')"
172fi
173for f in $D/a.om $D/b.om; do "$SH" < "$f" | sed -n "s|^FN |$f FN |p"; done > "$W/dups.in"
174awk '$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"
176got=$("$SH" dups < "$W/dups.mut" | tail -n 1)
177if [ "$got" = 'DUPS groups=0 fns=0 records=6 skipped=0' ]; then CONTROLS=$((CONTROLS + 1))
178else 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
181sed 's/\\nwhile 69\\n/\\n/' seed/semhash/semhash.om > "$W/mut/semhash.om"
182if cmp -s seed/semhash/semhash.om "$W/mut/semhash.om"; then
183 fail "mutant: sed did not change the keyword table"
184elif ! build "$W/mut/semhash.om" "$W/mut/semhash"; then
185 fail "mutant does not build: $(cat "$W/mut/semhash.err")"
186else
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
196fi
197
198if [ "$FAILS" = 0 ]; then
199 printf 'SEMHASH PASS cases=%s controls=%s\n' "$CASES" "$CONTROLS"
200 exit 0
201fi
202printf 'SEMHASH FAIL failures=%s cases=%s controls=%s\n' "$FAILS" "$CASES" "$CONTROLS"
203exit 1