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 / wasm / gate-irwasm.sh

219 lines · 10046 bytes Raw Blame History
1#!/bin/sh
2# Host-only verification. Node is a read-only wasm oracle, never a generator.
3set -eu
4cd "$(dirname "$0")/../.."
5B=build/seed/wasm
6mkdir -p "$B"
7fail() { printf 'FAIL %s\n' "$*"; exit 1; }
8[ "$(uname -m)" = aarch64 ] || { echo 'NOT_RUN requires AArch64 Linux'; exit 3; }
9command -v node >/dev/null || { echo 'NOT_RUN requires Node WebAssembly oracle'; exit 3; }
10OMC=seed/out/omc
11pin=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991
12[ "$(sha256sum "$OMC" | cut -d ' ' -f 1)" = "$pin" ] || fail 'compiler pin differs'
13printf 'SOURCE %s\n' "$(git rev-parse HEAD)"
14printf 'COMPILER %s\n' "$pin"
15node --version
16uname -sm
17nice -n 19 "$OMC" < seed/wasm/irwasm.om > "$B/irwasm" 2> "$B/compile.err"
18[ ! -s "$B/compile.err" ] || fail 'translator compiler stderr'
19chmod +x "$B/irwasm"
20for source in tests/one_layer/omegese/dev/*.omegese seed/wasm/*.unit.om; do
21 id=$(basename "$source" .omegese)
22 id=${id%.unit.om}
23 { printf 'ir\n'; cat "$source"; } > "$B/$id.input"
24 nice -n 19 "$OMC" < "$B/$id.input" > "$B/$id.ir"
25 nice -n 19 "$B/irwasm" < "$B/$id.ir" > "$B/$id.wasm"
26 nice -n 19 "$B/irwasm" < "$B/$id.ir" > "$B/$id.again.wasm"
27 cmp "$B/$id.wasm" "$B/$id.again.wasm" || fail "$id nondeterministic"
28 printf '%s\n' "$source" > "$B/$id.source"
29 echo "PASS $id deterministic"
30done
31# Each table row gets a real native program calling the same function and inputs.
32# The dumper preserves every result bit, rather than comparing process-status bytes.
33i=0
34tab=$(printf '\t')
35while IFS="$tab" read -r unit function result typed status; do
36 case "$unit" in ''|'#'*) continue ;; esac
37 i=$((i+1))
38 source=$(cat "$B/$unit.source")
39 cat "$source" > "$B/case-$i.om"
40 cat >> "$B/case-$i.om" <<'DUMP'
41global dumpbuf uchar 8
42fn ir_dump x ulong to void
43for j ulong 8
44put dumpbuf j uchar and shr x mul j 8 255
45end
46p = ulong 0
47while lt p 8 bound 8
48k = write 1 dumpbuf p sub ulong 8 p
49if le k 0
50exit 90
51end
52p = add p ulong k
53end
54end
55fn main to int
56DUMP
57 actual=
58 if [ "$typed" != - ]; then
59 for arg in $(printf '%s' "$typed" | tr ',' ' '); do
60 type=${arg%%:*}; value=${arg#*:}
61 if [ "$type" = bool ]; then actual="$actual $value"
62 else actual="$actual $type $value"; fi
63 done
64 fi
65 case "$result" in
66 bool) printf 'if %s %s\nir_dump ulong 1\nelse\nir_dump ulong 0\nend\n' "$function" "$actual" ;;
67 void) printf '%s %s\nir_dump ulong 0\n' "$function" "$actual" ;;
68 byte|short|int|long) printf 'ir_dump bitcast ulong long %s %s\n' "$function" "$actual" ;;
69 uchar|ushort|uint|ulong) printf 'ir_dump ulong %s %s\n' "$function" "$actual" ;;
70 *) fail "unknown result $result" ;;
71 esac >> "$B/case-$i.om"
72 printf 'ret 0\nend\n' >> "$B/case-$i.om"
73 nice -n 19 "$OMC" < "$B/case-$i.om" > "$B/case-$i.native"
74 chmod +x "$B/case-$i.native"
75 set +e
76 timeout 10 "$B/case-$i.native" > "$B/case-$i.out"
77 rc=$?
78 set -e
79 printf '%s\n' "$rc" > "$B/case-$i.status"
80 [ "$rc" = "$status" ] || fail "case $i $unit.$function native exit $rc expected $status"
81done < seed/wasm/cases.tsv
82# The pinned simple fixture has BIN ADD at offsets 38,39. Assert before mutating.
83[ "$(od -An -j 38 -N 2 -t u1 "$B/mutant.ir" | tr -s ' ' | tr -d '\n')" = ' 3 1' ] || fail 'mutant IR layout changed'
84cp "$B/mutant.ir" "$B/mutant-flipped.ir"
85printf '\002' | dd of="$B/mutant-flipped.ir" bs=1 seek=39 conv=notrunc status=none
86"$B/irwasm" < "$B/mutant-flipped.ir" > "$B/mutant-flipped.wasm"
87node seed/wasm/oracle.js "$B" seed/wasm/cases.tsv
88set +e
89node seed/wasm/oracle.js "$B" seed/wasm/cases.tsv --mutant-baseline > "$B/negative.log" 2>&1
90rc=$?
91set -e
92[ "$rc" = 1 ] || fail "mutant comparison exited $rc instead of 1"
93grep -q 'result mismatch' "$B/negative.log" || fail 'mutant failed for unrelated reason'
94echo 'PASS parity checker rejects mutant substituted as baseline (exit 1)'
95refuse() {
96 name=$1; expected=$2; class=$3
97 set +e
98 "$B/irwasm" < "$B/$name.ir" > "$B/$name.refused" 2> "$B/$name.err"
99 rc=$?
100 set -e
101 [ "$rc" = "$expected" ] || fail "$name exit $rc expected $expected"
102 [ ! -s "$B/$name.refused" ] || fail "$name emitted partial module"
103 printf 'irwasm: %s\n' "$class" > "$B/expected.err"
104 cmp "$B/expected.err" "$B/$name.err" || fail "$name wrong refusal name"
105 echo "PASS $name $class exit=$rc stdout=empty"
106}
107bytecase() {
108 name=$1; offset=$2; bytes=$3; code=$4; class=$5
109 cp "$B/mutant.ir" "$B/$name.ir"
110 printf '%b' "$bytes" | dd of="$B/$name.ir" bs=1 seek="$offset" conv=notrunc status=none
111 refuse "$name" "$code" "$class"
112}
113bytecase bad-magic 0 '\130' 20 MAGIC
114bytecase version2 7 '\002' 21 VERSION
115bytecase version6 7 '\006' 21 VERSION
116bytecase unsupported-op 38 '\007' 24 UNSUPPORTED_OP
117bytecase unknown-op 38 '\036' 24 UNSUPPORTED_OP
118bytecase unsupported-tag 18 '\012' 25 UNSUPPORTED_TAG
119bytecase unknown-tag 18 '\015' 25 UNSUPPORTED_TAG
120bytecase bad-register 44 '\003' 26 INVALID
121bytecase undefined-register 42 '\002' 26 INVALID
122bytecase nonascii-name 11 '\377' 26 INVALID
123bytecase oversized-count 8 '\041' 23 CAPACITY
124size=$(wc -c < "$B/mutant.ir")
125dd if="$B/mutant.ir" of="$B/truncated.ir" bs=1 count=$((size-1)) status=none
126refuse truncated 22 TRUNCATED
127: > "$B/empty.ir"
128refuse empty 22 TRUNCATED
129cp "$B/mutant.ir" "$B/trailing.ir"
130printf x >> "$B/trailing.ir"
131refuse trailing 27 TRAILING
132dd if=/dev/zero of="$B/oversized.ir" bs=2620252 count=1 status=none
133refuse oversized 23 CAPACITY
134printf 'PASS IRWASM native parity cases=%s deterministic mutant refusals\n' "$i"
135# OSC IR v7 (spec 10): regions, f32/f64, RLOAD/RSTORE/BITCAST/RINIT. Units come
136# from the independent hand writer seed/wasm/v7/writer.js and seed/wasm/v7cases.js;
137# expected verdicts and outputs come from the independent oracle seed/wasm/v7/oracle.js.
138V="$B/v7"
139rm -rf "$V"
140mkdir -p "$V/fx" "$V/rc"
141[ -f seed/wasm/v7/oracle.js ] && [ -f seed/wasm/v7/writer.js ] || fail 'v7 oracle or writer missing'
142node seed/wasm/v7/writer.js "$V/fx" > /dev/null
143node seed/wasm/v7cases.js "$V/rc" > /dev/null
144code() {
145 case $1 in
146 MAGIC) echo 20 ;; VERSION) echo 21 ;; TRUNCATED) echo 22 ;; CAPACITY) echo 23 ;;
147 UNSUPPORTED_OP) echo 24 ;; UNSUPPORTED_TAG) echo 25 ;; INVALID) echo 26 ;; TRAILING) echo 27 ;;
148 *) fail "oracle class $1" ;;
149 esac
150}
151accepts=0
152refusals=0
153for unit in "$V"/fx/*.unit "$V"/rc/*.unit; do
154 id=${unit%.unit}
155 set +e
156 verdict=$(node seed/wasm/v7/oracle.js check "$unit")
157 "$B/irwasm" < "$unit" > "$id.wasm" 2> "$id.err"
158 rc=$?
159 set -e
160 case $verdict in
161 ACCEPT*)
162 [ "$rc" = 0 ] || fail "v7 $id refused ($(cat "$id.err")) but oracle: $verdict"
163 "$B/irwasm" < "$unit" > "$id.again.wasm"
164 cmp "$id.wasm" "$id.again.wasm" || fail "v7 $id nondeterministic"
165 node -e 'new WebAssembly.Module(require("fs").readFileSync(process.argv[1]))' "$id.wasm" ||
166 fail "v7 $id invalid wasm"
167 accepts=$((accepts + 1)) ;;
168 REFUSE*)
169 class=$(printf '%s\n' "$verdict" | cut -d ' ' -f 2)
170 [ "$rc" = "$(code "$class")" ] || fail "v7 $id exit $rc, oracle: $verdict"
171 [ ! -s "$id.wasm" ] || fail "v7 $id emitted partial module"
172 printf 'irwasm: %s\n' "$class" > "$V/expected.err"
173 cmp "$V/expected.err" "$id.err" || fail "v7 $id wrong refusal name"
174 refusals=$((refusals + 1)) ;;
175 *) fail "v7 $id oracle verdict: $verdict" ;;
176 esac
177done
178[ "$accepts" -gt 0 ] && [ "$refusals" -gt 0 ] || fail 'v7 fixture set empty'
179echo "PASS v7 verdict parity accepts=$accepts (valid, deterministic) refusals=$refusals (class, exit, stdout=empty)"
180runs=0
181for script in "$V"/rc/*.script; do
182 id=${script%.script}
183 node seed/wasm/v7/oracle.js run "$id.unit" "$script" > "$id.oracle"
184 node seed/wasm/v7run.js "$id.wasm" "$script" > "$id.out"
185 [ -s "$id.oracle" ] || fail "v7 $id oracle printed nothing"
186 diff "$id.oracle" "$id.out" || fail "v7 $id run differs from oracle"
187 runs=$((runs + 1))
188done
189lines=$(cat "$V"/rc/*.oracle | wc -l)
190traps=$(cat "$V"/rc/*.oracle | grep -c ' TRAP$')
191echo "PASS v7 run parity cases=$runs lines=$lines traps=$traps"
192# RGBX gradient: the framebuffer bytes after fill() hash the same in wasm and the
193# oracle, and a one-channel mutant and a channel-order mutant hash differently.
194fbsum() {
195 sed -n '3p' "$1" | grep '^region fb ' | cut -d ' ' -f 3 |
196 node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{const b=Buffer.from(s.trim(),"hex");if(b.length!==512)process.exit(1);console.log(require("crypto").createHash("sha256").update(b).digest("hex"))})'
197}
198rgbx=$(fbsum "$V/rc/rgbx.out") || fail 'rgbx framebuffer line'
199[ "$rgbx" = "$(fbsum "$V/rc/rgbx.oracle")" ] || fail 'rgbx framebuffer hash differs'
200for m in rgbx_mut_b rgbx_mut_order; do
201 [ "$(fbsum "$V/rc/$m.out")" != "$rgbx" ] || fail "$m not distinguished"
202done
203echo "PASS v7 RGBX framebuffer sha256=$rgbx; channel and order mutants differ"
204# Out-of-bounds: index == count, count + 1, negative and wide indexes trap in
205# both (case oob above). A translator with the bounds check removed must differ.
206[ "$(grep -c '^boundchk ' seed/wasm/irwasm.om)" = 2 ] || fail 'bounds check call sites changed'
207sed '/^boundchk /d' seed/wasm/irwasm.om > "$V/nobound.om"
208nice -n 19 "$OMC" < "$V/nobound.om" > "$V/nobound" 2> "$V/nobound.err"
209[ ! -s "$V/nobound.err" ] || fail 'bounds mutant compile stderr'
210chmod +x "$V/nobound"
211"$V/nobound" < "$V/rc/oob.unit" > "$V/nobound.wasm"
212node seed/wasm/v7run.js "$V/nobound.wasm" "$V/rc/oob.script" > "$V/nobound.out"
213if diff "$V/rc/oob.oracle" "$V/nobound.out" > "$V/nobound.diff"; then
214 fail 'bounds-check mutant not caught'
215fi
216grep -q '^< .* TRAP$' "$V/nobound.diff" || fail 'bounds mutant differs for an unrelated reason'
217echo "PASS v7 OOB traps=$(grep -c ' TRAP$' "$V/rc/oob.oracle"); no-bounds mutant caught ($(grep -c '^>' "$V/nobound.diff") differing lines)"
218sha256sum seed/wasm/v7cases.js seed/wasm/v7run.js seed/wasm/v7/oracle.js seed/wasm/v7/writer.js
219sha256sum seed/wasm/irwasm.om "$B/irwasm" seed/wasm/cases.tsv