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 / equiv / gate-irequiv.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Gate for seed/equiv/irequiv.om (issue 826): exhaustive u8/u16 equivalence of |
| 4 | # OSC IR v1 scalar functions. Host CPU only; no C participates in checking. |
| 5 | set -eu |
| 6 | cd "$(dirname "$0")/../.." |
| 7 | [ "$(uname -m)" = aarch64 ] || { echo "SKIP gate-irequiv: needs aarch64 (omc emits AArch64)"; exit 0; } |
| 8 | OMC=seed/out/omc |
| 9 | PIN=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991 |
| 10 | [ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo "FAIL pin: $OMC"; exit 1; } |
| 11 | B=build/seed/equiv |
| 12 | mkdir -p "$B" |
| 13 | fails=0 |
| 14 | bad() { echo "FAIL $*"; fails=$((fails + 1)); } |
| 15 | |
| 16 | # irequiv.om lines 43-496 are the OSC IR v1 reader and validator, byte-identical to irspirv.om. |
| 17 | # irwasm.om forked it for IR v7 (#818): its lines 65-843 must equal that v1 reader with the |
| 18 | # recorded v7 edits of seed/equiv/irwasm-v7.sed applied, byte for byte. A change on either side |
| 19 | # that is not in the recorded edits is drift. |
| 20 | sed -n 43,496p seed/equiv/irequiv.om > "$B/r1" |
| 21 | sed -n 43,496p seed/spirv/irspirv.om > "$B/r2" |
| 22 | cmp -s "$B/r1" "$B/r2" || bad "reader drift vs seed/spirv/irspirv.om" |
| 23 | sed -n 65,843p seed/wasm/irwasm.om > "$B/r2" |
| 24 | sed -f seed/equiv/irwasm-v7.sed "$B/r1" > "$B/r1v7" || bad "apply seed/equiv/irwasm-v7.sed" |
| 25 | cmp -s "$B/r1v7" "$B/r2" || bad "reader drift vs seed/wasm/irwasm.om (v1 reader + seed/equiv/irwasm-v7.sed)" |
| 26 | |
| 27 | "$OMC" < seed/equiv/irequiv.om > "$B/irequiv" 2> "$B/irequiv.err" || { cat "$B/irequiv.err"; echo "FAIL compile irequiv"; exit 1; } |
| 28 | [ -s "$B/irequiv.err" ] && bad "compile stderr not empty" |
| 29 | chmod +x "$B/irequiv" |
| 30 | for u in hexv clamp rot bitrev pop16 wide; do |
| 31 | { printf 'ir\n'; cat "seed/equiv/$u.unit.om"; } | "$OMC" > "$B/$u.ir" || bad "ir $u" |
| 32 | done |
| 33 | |
| 34 | # expect UNIT STATUS "line substring" ARGS... |
| 35 | expect() { |
| 36 | u=$1; want=$2; sub=$3; shift 3 |
| 37 | set +e |
| 38 | out=$("$B/irequiv" "$@" < "$B/$u.ir" 2>&1); st=$? |
| 39 | set -e |
| 40 | echo "$out" |
| 41 | [ "$st" = "$want" ] || bad "$u $*: status $st, want $want" |
| 42 | case "$out" in *"$sub"*) ;; *) bad "$u $*: missing '$sub'";; esac |
| 43 | } |
| 44 | |
| 45 | # Equivalent pairs: PASS over the full domain, exact counts. |
| 46 | expect hexv 0 "EQUIV PASS a=js_hexv b=hhexv params=uchar domain=256 checked=256 differing=0" check js_hexv hhexv |
| 47 | expect hexv 0 "EQUIV PASS a=js_hexv b=hexfold params=uchar domain=256 checked=256 differing=0" check js_hexv hexfold |
| 48 | expect clamp 0 "domain=65536 checked=65536 differing=0 both_trap=0" check clamp_add clamp_wrap |
| 49 | expect bitrev 0 "domain=256 checked=256 differing=0" check bitrev8 bitrev8s |
| 50 | expect rot 0 "domain=65536 checked=65536 differing=0 both_trap=63488" check r8 r8w |
| 51 | expect pop16 0 "params=ushort domain=65536 checked=65536 differing=0" check pop16 pop16s |
| 52 | # Negative controls: single-input mutants report that exact input and both results. |
| 53 | expect hexv 1 "differing=1 first=uchar:70 a_result=value:ulong:15 b_result=value:ulong:16" check js_hexv hexoff1 |
| 54 | expect pop16 1 "differing=1 first=ushort:0 a_result=value:ushort:0 b_result=value:ushort:1" check pop16 pop16z |
| 55 | expect pop16 1 "differing=1 first=ushort:65535 a_result=value:ushort:16 b_result=value:ushort:15" check pop16 pop16e |
| 56 | # Trap codes are part of equality: value-for-trap is a difference. |
| 57 | expect rot 1 "differing=63488 first=uchar:0,uchar:8 a_result=trap:7 b_result=value:uchar:0" check r8 r8m |
| 58 | expect rot 0 "trap:7" eval r8 uchar:1 uchar:9 |
| 59 | expect rot 0 "value:128" eval r8 uchar:1 uchar:1 |
| 60 | # Wider domains: refused for check, sampled (exit 10, NOT_A_PROOF) otherwise. |
| 61 | expect wide 5 "irequiv: DOMAIN" check pop32 pop32s |
| 62 | expect wide 10 "EQUIV SAMPLED" sample pop32 pop32s 100000 7 |
| 63 | expect wide 10 "NOT_A_PROOF" sample pop32 pop32s 100000 7 |
| 64 | expect wide 1 "EQUIV FAIL" sample pop32 pop32t 100000 7 |
| 65 | # Fuel exhaustion is INCONCLUSIVE, never PASS or FAIL. |
| 66 | expect wide 6 "EQUIV INCONCLUSIVE" check spin spin |
| 67 | # Refusals. |
| 68 | expect hexv 3 "UNKNOWN_FUNCTION" check js_hexv nope |
| 69 | expect wide 4 "SIGNATURE" check pop32 spin |
| 70 | expect rot 2 "USAGE" eval r8 uchar:1 ushort:1 |
| 71 | expect rot 2 "USAGE" eval r8 uchar:256 uchar:1 |
| 72 | expect rot 2 "USAGE" bogus r8 |
| 73 | expect rot 7 "DUMP_TRAP" dump r8 |
| 74 | head -c 100 "$B/pop16.ir" > "$B/trunc.ir" |
| 75 | set +e; "$B/irequiv" check pop16 pop16s < "$B/trunc.ir" > /dev/null 2>&1; st=$?; set -e |
| 76 | [ "$st" = 22 ] || bad "truncated IR: status $st, want 22" |
| 77 | # Full-domain dump: bitrev8 over 256 inputs is 2048 bytes and reverses 1 to 128. |
| 78 | "$B/irequiv" dump bitrev8 < "$B/bitrev.ir" > "$B/bitrev.dump" |
| 79 | [ "$(wc -c < "$B/bitrev.dump")" = 2048 ] || bad "dump size" |
| 80 | [ "$(od -An -tu8 -j8 -N8 "$B/bitrev.dump" | tr -d ' ')" = 128 ] || bad "dump bitrev8(1)" |
| 81 | |
| 82 | # Conformance with native code: every row of the irwasm/irspirv tables is run as a |
| 83 | # real native program (the dumper of seed/wasm/gate-irwasm.sh, all 64 result bits) |
| 84 | # and through `irequiv eval` on the same unit's IR. Values must be bit-identical and |
| 85 | # traps must carry the same code; the native exit must also match the table. |
| 86 | for source in tests/one_layer/omegese/dev/*.omegese seed/wasm/*.unit.om seed/spirv/*.unit.om; do |
| 87 | id=$(basename "$source" .omegese) |
| 88 | id=${id%.unit.om} |
| 89 | printf '%s\n' "$source" > "$B/nx-$id.source" |
| 90 | { printf 'ir\n'; cat "$source"; } | "$OMC" > "$B/nx-$id.ir" || bad "ir $source" |
| 91 | done |
| 92 | |
| 93 | # Reader verdicts: irwasm's v7 reader must accept every v1 unit irequiv accepts and refuse the |
| 94 | # malformed ones with the same status; a version-7 header stays irwasm's alone (irequiv: VERSION). |
| 95 | "$OMC" < seed/wasm/irwasm.om > "$B/irwasm" || { echo "FAIL compile irwasm"; exit 1; } |
| 96 | chmod +x "$B/irwasm" |
| 97 | { printf X; tail -c +2 "$B/pop16.ir"; } > "$B/bad-magic.ir" |
| 98 | { head -c 7 "$B/pop16.ir"; printf '\002'; tail -c +9 "$B/pop16.ir"; } > "$B/bad-version.ir" |
| 99 | { head -c 7 "$B/pop16.ir"; printf '\007'; tail -c +9 "$B/pop16.ir"; } > "$B/v7-header.ir" |
| 100 | { cat "$B/pop16.ir"; printf Z; } > "$B/bad-trail.ir" |
| 101 | rd_units=0 |
| 102 | rd_agree=0 |
| 103 | verdict() { |
| 104 | set +e |
| 105 | "$B/irwasm" < "$1" > /dev/null 2>&1; w=$? |
| 106 | "$B/irequiv" eval ir_reader_probe < "$1" > /dev/null 2>&1; e=$? |
| 107 | set -e |
| 108 | [ "$e" = 3 ] && e=0 |
| 109 | rd_units=$((rd_units + 1)) |
| 110 | if [ "$w" = "$2" ] && [ "$e" = "$2" ]; then rd_agree=$((rd_agree + 1)); else bad "reader verdict $1: irwasm $w, irequiv $e, want $2"; fi |
| 111 | } |
| 112 | for u in "$B"/nx-*.ir; do verdict "$u" 0; done |
| 113 | for u in hexv clamp rot bitrev pop16 wide; do verdict "$B/$u.ir" 0; done |
| 114 | verdict "$B/bad-magic.ir" 20 |
| 115 | verdict "$B/bad-version.ir" 21 |
| 116 | verdict "$B/trunc.ir" 22 |
| 117 | verdict "$B/bad-trail.ir" 27 |
| 118 | set +e; "$B/irequiv" eval ir_reader_probe < "$B/v7-header.ir" > /dev/null 2>&1; st=$?; set -e |
| 119 | [ "$st" = 21 ] || bad "v7 header: irequiv status $st, want 21 (VERSION)" |
| 120 | echo "READERS units=$rd_units agree=$rd_agree" |
| 121 | tab=$(printf '\t') |
| 122 | nx_rows=0 |
| 123 | nx_equal=0 |
| 124 | conform() { |
| 125 | table=$1; want=$2; rows=0 |
| 126 | while IFS="$tab" read -r unit function result typed status; do |
| 127 | case "$unit" in ''|'#'*) continue ;; esac |
| 128 | rows=$((rows + 1)); nx_rows=$((nx_rows + 1)) |
| 129 | om=$B/nx-row-$nx_rows.om |
| 130 | cat "$(cat "$B/nx-$unit.source")" > "$om" |
| 131 | cat >> "$om" <<'DUMP' |
| 132 | global dumpbuf uchar 8 |
| 133 | fn ir_dump x ulong to void |
| 134 | for j ulong 8 |
| 135 | put dumpbuf j uchar and shr x mul j 8 255 |
| 136 | end |
| 137 | p = ulong 0 |
| 138 | while lt p 8 bound 8 |
| 139 | k = write 1 dumpbuf p sub ulong 8 p |
| 140 | if le k 0 |
| 141 | exit 90 |
| 142 | end |
| 143 | p = add p ulong k |
| 144 | end |
| 145 | end |
| 146 | fn main to int |
| 147 | DUMP |
| 148 | actual=; eargs= |
| 149 | if [ "$typed" != - ]; then |
| 150 | for a in $(printf '%s' "$typed" | tr ',' ' '); do |
| 151 | ty=${a%%:*}; v=${a#*:} |
| 152 | if [ "$ty" = bool ]; then actual="$actual $v"; else actual="$actual $ty $v"; fi |
| 153 | eargs="$eargs $a" |
| 154 | done |
| 155 | fi |
| 156 | case "$result" in |
| 157 | bool|void|byte|short|int|long|uchar|ushort|uint|ulong) ;; |
| 158 | *) bad "$table row $rows: unknown result $result"; continue ;; |
| 159 | esac |
| 160 | case "$result" in |
| 161 | bool) printf 'if %s %s\nir_dump ulong 1\nelse\nir_dump ulong 0\nend\n' "$function" "$actual" ;; |
| 162 | void) printf '%s %s\nir_dump ulong 0\n' "$function" "$actual" ;; |
| 163 | byte|short|int|long) printf 'ir_dump bitcast ulong long %s %s\n' "$function" "$actual" ;; |
| 164 | uchar|ushort|uint|ulong) printf 'ir_dump ulong %s %s\n' "$function" "$actual" ;; |
| 165 | esac >> "$om" |
| 166 | printf 'ret 0\nend\n' >> "$om" |
| 167 | "$OMC" < "$om" > "$B/nx-row-$nx_rows" || { bad "$table row $rows: native compile"; continue; } |
| 168 | chmod +x "$B/nx-row-$nx_rows" |
| 169 | set +e |
| 170 | timeout 10 "$B/nx-row-$nx_rows" > "$B/nx-row-$nx_rows.out"; rc=$? |
| 171 | ir=$("$B/irequiv" eval "$function" $eargs < "$B/nx-$unit.ir" 2>&1); st=$? |
| 172 | set -e |
| 173 | [ "$rc" = "$status" ] || bad "$table row $rows: native exit $rc, table $status" |
| 174 | if [ "$rc" = 0 ] && [ "$(wc -c < "$B/nx-row-$nx_rows.out")" = 8 ]; then |
| 175 | native="value:$(od -An -tu8 "$B/nx-row-$nx_rows.out" | tr -d ' ')" |
| 176 | elif [ "$rc" -gt 100 ] && [ "$rc" -lt 200 ]; then |
| 177 | native="trap:$((rc - 100))" |
| 178 | else |
| 179 | native="native-exit:$rc" |
| 180 | fi |
| 181 | if [ "$st" = 0 ] && [ "$ir" = "$native" ]; then |
| 182 | nx_equal=$((nx_equal + 1)) |
| 183 | else |
| 184 | bad "$table row $rows $unit.$function $typed: native $native, irequiv $ir (exit $st)" |
| 185 | fi |
| 186 | done < "$table" |
| 187 | [ "$rows" = "$want" ] || bad "$table denominator $rows, want $want" |
| 188 | } |
| 189 | conform seed/wasm/cases.tsv 129 |
| 190 | conform seed/spirv/regression.tsv 8 |
| 191 | conform seed/spirv/demo.tsv 3 |
| 192 | [ "$nx_equal" = 140 ] || bad "native conformance equal=$nx_equal of $nx_rows, want 140" |
| 193 | echo "CONFORM native rows=$nx_rows equal=$nx_equal" |
| 194 | |
| 195 | if [ "$fails" = 0 ]; then echo "GATE-IREQUIV PASS"; else echo "GATE-IREQUIV FAIL ($fails)"; exit 1; fi |