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 / wasm / v7 / test-oracle.sh
| 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. |
| 14 | set -euo pipefail |
| 15 | |
| 16 | here=$(cd "$(dirname "$0")" && pwd) |
| 17 | root=$(cd "$here/../../.." && pwd) |
| 18 | out=${BUILD:-$root/build}/v7-oracle |
| 19 | units=$out/units |
| 20 | expect=$here/expect |
| 21 | fail=0 |
| 22 | note() { printf '%s\n' "$*"; } |
| 23 | bad() { note "FAIL: $*"; fail=1; } |
| 24 | |
| 25 | rm -rf "$out" |
| 26 | mkdir -p "$units" "$out/runs" |
| 27 | node "$here/writer.js" "$units" >/dev/null |
| 28 | |
| 29 | # suite ORACLE STOP_AT_FIRST -> prints mismatches to stdout, returns count via $mism |
| 30 | suite() { |
| 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) |
| 80 | comm -3 <(cd "$units" && ls -- *.unit | sed 's/\.unit$//' | sort) \ |
| 81 | <(grep -v '^#' "$expect/check.txt" | cut -f1 | sed '/^$/d' | sort) > "$out/setdiff" |
| 82 | if [ -s "$out/setdiff" ]; then bad "fixture set differs from expect/check.txt:"; cat "$out/setdiff"; fi |
| 83 | for 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" |
| 86 | done |
| 87 | |
| 88 | # --- unpatched oracle: zero mismatches |
| 89 | suite "$here/oracle.js" 0 |
| 90 | [ "$mism" = 0 ] || bad "oracle: $mism mismatches" |
| 91 | nchecks=$checks nruns=$runs |
| 92 | note "oracle: checks=$nchecks runs=$nruns mismatches=$mism" |
| 93 | |
| 94 | # --- usage and exit-code controls |
| 95 | expect_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 | } |
| 104 | ctl=$out/ctl |
| 105 | mkdir -p "$ctl" |
| 106 | printf 'call nosuch\n' > "$ctl/unknown.s" |
| 107 | printf 'call get\n' > "$ctl/arity.s" |
| 108 | printf 'call set 0 256\n' > "$ctl/range.s" |
| 109 | printf 'call set 0 -1\n' > "$ctl/neg.s" |
| 110 | printf 'call set 0x1 0x3f8000\n' > "$ctl/floatarg.s" |
| 111 | printf 'jump get 0\n' > "$ctl/verb.s" |
| 112 | printf 'region nosuch\n' > "$ctl/region.s" |
| 113 | printf 'call get 0\n' > "$ctl/ok.s" |
| 114 | O=$here/oracle.js |
| 115 | expect_rc 2 'check without unit' node "$O" check |
| 116 | expect_rc 2 'unknown command' node "$O" frob "$units/rt_u8.unit" |
| 117 | expect_rc 2 'run without script' node "$O" run --codes "$units/rt_u8.unit" |
| 118 | expect_rc 2 'missing unit file' node "$O" check "$units/does-not-exist.unit" |
| 119 | expect_rc 2 'unknown function' node "$O" run "$units/rt_u8.unit" "$ctl/unknown.s" |
| 120 | expect_rc 2 'wrong arity' node "$O" run "$units/rt_u8.unit" "$ctl/arity.s" |
| 121 | expect_rc 2 'u8 argument 256' node "$O" run "$units/rt_u8.unit" "$ctl/range.s" |
| 122 | expect_rc 2 'u8 argument -1' node "$O" run "$units/rt_u8.unit" "$ctl/neg.s" |
| 123 | expect_rc 2 'short float argument' node "$O" run "$units/rt_f32.unit" "$ctl/floatarg.s" |
| 124 | expect_rc 2 'unknown script verb' node "$O" run "$units/rt_u8.unit" "$ctl/verb.s" |
| 125 | expect_rc 2 'unknown region' node "$O" run "$units/rt_u8.unit" "$ctl/region.s" |
| 126 | expect_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' |
| 128 | grep -q '^REFUSE INVALID ' "$out/ctl.err" || bad 'run of refused unit: no REFUSE on stderr' |
| 129 | expect_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 |
| 133 | for 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 |
| 139 | done |
| 140 | # Outside comments, neither file names a sibling implementation. |
| 141 | for 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 |
| 146 | done |
| 147 | |
| 148 | # A mutant "caught" against a failing baseline proves nothing. |
| 149 | if [ "$fail" != 0 ]; then |
| 150 | note "V7ORACLE FAIL checks=$nchecks runs=$nruns mutants=NOT_RUN (baseline failed)" |
| 151 | exit 1 |
| 152 | fi |
| 153 | |
| 154 | # --- mutants |
| 155 | mutants=$(cat <<'EOF' |
| 156 | bounds if (i < 0n) trap(3); |
| 157 | bounds:off1 if (i < 0n || i > BigInt(this.rg.count)) trap(3); |
| 158 | initcheck if (false) { |
| 159 | meet for (let k = 0; k < n; k++) st[k] |= o[k]; |
| 160 | satnan if (Number.isNaN(x)) return intMax(dt); |
| 161 | satlow if (t < intMin(dt)) return 0n; |
| 162 | sathigh if (t > intMax(dt)) return wrapInt(dt, t); |
| 163 | owner void rg; |
| 164 | textro void rg; |
| 165 | callsite const r = new Instance(u).call(fi, vals); |
| 166 | callsite: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; } |
| 167 | gratuitous void 0; |
| 168 | f32upper if (t === T_F32) return true; |
| 169 | i2f32 x = Number(m); |
| 170 | bcsign return bits; |
| 171 | export const exp = () => {}; |
| 172 | trailing void 0; |
| 173 | fneg if (t === T_F32) regs[ins.dst] = f32bits(-f32val(a)); |
| 174 | cast5 if (false) trap(5); |
| 175 | EOF |
| 176 | ) |
| 177 | nm=0 caught=0 |
| 178 | mdir=$out/mutants |
| 179 | while 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 |
| 201 | done <<< "$mutants" |
| 202 | |
| 203 | if [ "$fail" = 0 ] && [ "$caught" = "$nm" ]; then |
| 204 | note "V7ORACLE PASS checks=$nchecks runs=$nruns mutants=$caught/$nm" |
| 205 | exit 0 |
| 206 | fi |
| 207 | note "V7ORACLE FAIL checks=$nchecks runs=$nruns mutants=$caught/$nm" |
| 208 | exit 1 |