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 / v7 / test-oracle.sh

208 lines · 8227 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Self-test of the independent OSC IR v7 oracle (seed/wasm/v7/oracle.js).
4#
5# 1. writer.js emits every fixture unit into $BUILD/v7-oracle/units.
6# 2. oracle.js check / run results are compared with the hand-written
7# expectations in seed/wasm/v7/expect/ (check.txt, run/*.script + .out).
8# 3. Usage/exit-code controls and a source-independence check.
9# 4. Mutants: patched copies of oracle.js in $BUILD/v7-oracle/mutants/ (each
10# replaces the one line tagged `// M:<marker>`); every mutant must produce
11# at least one mismatch against the same expectations.
12#
13# Exit 0 PASS, 1 FAIL.
14set -euo pipefail
15
16here=$(cd "$(dirname "$0")" && pwd)
17root=$(cd "$here/../../.." && pwd)
18out=${BUILD:-$root/build}/v7-oracle
19units=$out/units
20expect=$here/expect
21fail=0
22note() { printf '%s\n' "$*"; }
23bad() { note "FAIL: $*"; fail=1; }
24
25rm -rf "$out"
26mkdir -p "$units" "$out/runs"
27node "$here/writer.js" "$units" >/dev/null
28
29# suite ORACLE STOP_AT_FIRST -> prints mismatches to stdout, returns count via $mism
30suite() {
31 local oracle=$1 first=$2 name want got rc wantrc u s base
32 mism=0
33 checks=0
34 runs=0
35 while IFS=$'\t' read -r name want; do
36 case $name in ''|'#'*) continue ;; esac
37 checks=$((checks + 1))
38 set +e
39 got=$(node "$oracle" check "$units/$name.unit" 2>&1)
40 rc=$?
41 set -e
42 wantrc=1
43 [ "${want%% *}" = ACCEPT ] && wantrc=0
44 if [ "$(printf '%s' "$got" | cut -d' ' -f1-2)" != "$want" ] || [ "$rc" != "$wantrc" ]; then
45 note " mismatch check $name: want '$want' rc $wantrc, got '$got' rc $rc"
46 mism=$((mism + 1))
47 [ "$first" = 1 ] && return 0
48 fi
49 done < "$expect/check.txt"
50 for s in "$expect"/run/*.script; do
51 base=$(basename "$s" .script)
52 u=$units/$base.unit
53 runs=$((runs + 1))
54 set +e
55 node "$oracle" run --codes "$u" "$s" > "$out/runs/$base.got" 2> "$out/runs/$base.err"
56 rc=$?
57 set -e
58 if [ "$rc" != 0 ] || ! cmp -s "$out/runs/$base.got" "$expect/run/$base.out"; then
59 note " mismatch run $base (rc $rc)"
60 mism=$((mism + 1))
61 [ "$first" = 1 ] && return 0
62 fi
63 if [ -f "$expect/run/$base.plain" ]; then
64 runs=$((runs + 1))
65 set +e
66 node "$oracle" run "$u" "$s" > "$out/runs/$base.plain.got" 2>&1
67 rc=$?
68 set -e
69 if [ "$rc" != 0 ] || ! cmp -s "$out/runs/$base.plain.got" "$expect/run/$base.plain"; then
70 note " mismatch run (plain) $base (rc $rc)"
71 mism=$((mism + 1))
72 [ "$first" = 1 ] && return 0
73 fi
74 fi
75 done
76 return 0
77}
78
79# --- fixture set == expectation set (no silently skipped unit)
80comm -3 <(cd "$units" && ls -- *.unit | sed 's/\.unit$//' | sort) \
81 <(grep -v '^#' "$expect/check.txt" | cut -f1 | sed '/^$/d' | sort) > "$out/setdiff"
82if [ -s "$out/setdiff" ]; then bad "fixture set differs from expect/check.txt:"; cat "$out/setdiff"; fi
83for s in "$expect"/run/*.script; do
84 [ -f "$units/$(basename "$s" .script).unit" ] || bad "run script without unit: $s"
85 [ -f "${s%.script}.out" ] || bad "run script without .out: $s"
86done
87
88# --- unpatched oracle: zero mismatches
89suite "$here/oracle.js" 0
90[ "$mism" = 0 ] || bad "oracle: $mism mismatches"
91nchecks=$checks nruns=$runs
92note "oracle: checks=$nchecks runs=$nruns mismatches=$mism"
93
94# --- usage and exit-code controls
95expect_rc() { # WANT_RC DESCRIPTION CMD...
96 local want=$1 what=$2 rc
97 shift 2
98 set +e
99 "$@" > "$out/ctl.out" 2> "$out/ctl.err"
100 rc=$?
101 set -e
102 [ "$rc" = "$want" ] || bad "control '$what': rc $rc, want $want"
103}
104ctl=$out/ctl
105mkdir -p "$ctl"
106printf 'call nosuch\n' > "$ctl/unknown.s"
107printf 'call get\n' > "$ctl/arity.s"
108printf 'call set 0 256\n' > "$ctl/range.s"
109printf 'call set 0 -1\n' > "$ctl/neg.s"
110printf 'call set 0x1 0x3f8000\n' > "$ctl/floatarg.s"
111printf 'jump get 0\n' > "$ctl/verb.s"
112printf 'region nosuch\n' > "$ctl/region.s"
113printf 'call get 0\n' > "$ctl/ok.s"
114O=$here/oracle.js
115expect_rc 2 'check without unit' node "$O" check
116expect_rc 2 'unknown command' node "$O" frob "$units/rt_u8.unit"
117expect_rc 2 'run without script' node "$O" run --codes "$units/rt_u8.unit"
118expect_rc 2 'missing unit file' node "$O" check "$units/does-not-exist.unit"
119expect_rc 2 'unknown function' node "$O" run "$units/rt_u8.unit" "$ctl/unknown.s"
120expect_rc 2 'wrong arity' node "$O" run "$units/rt_u8.unit" "$ctl/arity.s"
121expect_rc 2 'u8 argument 256' node "$O" run "$units/rt_u8.unit" "$ctl/range.s"
122expect_rc 2 'u8 argument -1' node "$O" run "$units/rt_u8.unit" "$ctl/neg.s"
123expect_rc 2 'short float argument' node "$O" run "$units/rt_f32.unit" "$ctl/floatarg.s"
124expect_rc 2 'unknown script verb' node "$O" run "$units/rt_u8.unit" "$ctl/verb.s"
125expect_rc 2 'unknown region' node "$O" run "$units/rt_u8.unit" "$ctl/region.s"
126expect_rc 1 'run of refused unit' node "$O" run "$units/inv_missing_init.unit" "$ctl/ok.s"
127[ -s "$out/ctl.out" ] && bad 'run of refused unit wrote stdout'
128grep -q '^REFUSE INVALID ' "$out/ctl.err" || bad 'run of refused unit: no REFUSE on stderr'
129expect_rc 0 '--codes before verb' node "$O" --codes run "$units/rt_u8.unit" "$ctl/ok.s"
130[ "$(cat "$out/ctl.out")" = 'get = 0' ] || bad "--codes before verb printed '$(cat "$out/ctl.out")'"
131
132# --- independence: neither file loads the other or any repo producer/backend
133for f in oracle.js writer.js; do
134 reqs=$(grep -v '^ *//' "$here/$f" | grep -o "require([^)]*)" | sort -u | tr '\n' ' ')
135 case $f in
136 oracle.js) [ "$reqs" = "require('fs') " ] || bad "$f requires: $reqs" ;;
137 writer.js) [ "$reqs" = "require('fs') require('path') " ] || bad "$f requires: $reqs" ;;
138 esac
139done
140# Outside comments, neither file names a sibling implementation.
141for pair in 'oracle.js:writer\.js' 'writer.js:oracle\.js'; do
142 f=${pair%%:*}
143 if grep -v '^ *//' "$here/$f" | grep -q "irwasm\|omc\.om\|osc_ir\|${pair#*:}"; then
144 bad "$f names a sibling implementation outside comments"
145 fi
146done
147
148# A mutant "caught" against a failing baseline proves nothing.
149if [ "$fail" != 0 ]; then
150 note "V7ORACLE FAIL checks=$nchecks runs=$nruns mutants=NOT_RUN (baseline failed)"
151 exit 1
152fi
153
154# --- mutants
155mutants=$(cat <<'EOF'
156bounds if (i < 0n) trap(3);
157bounds:off1 if (i < 0n || i > BigInt(this.rg.count)) trap(3);
158initcheck if (false) {
159meet for (let k = 0; k < n; k++) st[k] |= o[k];
160satnan if (Number.isNaN(x)) return intMax(dt);
161satlow if (t < intMin(dt)) return 0n;
162sathigh if (t > intMax(dt)) return wrapInt(dt, t);
163owner void rg;
164textro void rg;
165callsite const r = new Instance(u).call(fi, vals);
166callsite:rollback const snap = inst.module.map((g) => g && g.mem.slice()); let r; try { r = inst.call(fi, vals); } catch (e) { inst.module.forEach((g, k) => { if (g) g.mem.set(snap[k]); }); throw e; }
167gratuitous void 0;
168f32upper if (t === T_F32) return true;
169i2f32 x = Number(m);
170bcsign return bits;
171export const exp = () => {};
172trailing void 0;
173fneg if (t === T_F32) regs[ins.dst] = f32bits(-f32val(a));
174cast5 if (false) trap(5);
175EOF
176)
177nm=0 caught=0
178mdir=$out/mutants
179while IFS=$'\t' read -r tag repl; do
180 marker=${tag%%:*}
181 mname=${tag/:/_}
182 nm=$((nm + 1))
183 hits=$(grep -c "// M:$marker\$" "$O" || true)
184 if [ "$hits" != 1 ]; then bad "mutant $mname: marker M:$marker found $hits times"; continue; fi
185 mkdir -p "$mdir/$mname"
186 MREPL=$repl MMARK="// M:$marker" node -e '
187 const fs = require("fs");
188 const lines = fs.readFileSync(process.argv[1], "utf8").split("\n");
189 const i = lines.findIndex((l) => l.endsWith(process.env.MMARK));
190 lines[i] = process.env.MREPL;
191 fs.writeFileSync(process.argv[2], lines.join("\n"));
192 ' "$O" "$mdir/$mname/oracle.js"
193 if cmp -s "$O" "$mdir/$mname/oracle.js"; then bad "mutant $mname: patch changed nothing"; continue; fi
194 suite "$mdir/$mname/oracle.js" 1 > "$mdir/$mname/log"
195 if [ "$mism" -gt 0 ]; then
196 caught=$((caught + 1))
197 note "mutant $mname: caught ($(sed 's/^ mismatch //' "$mdir/$mname/log" | head -n1))"
198 else
199 bad "mutant $mname: SURVIVED"
200 fi
201done <<< "$mutants"
202
203if [ "$fail" = 0 ] && [ "$caught" = "$nm" ]; then
204 note "V7ORACLE PASS checks=$nchecks runs=$nruns mutants=$caught/$nm"
205 exit 0
206fi
207note "V7ORACLE FAIL checks=$nchecks runs=$nruns mutants=$caught/$nm"
208exit 1