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 / om / codec / parity / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # om/codec parity: compiles codec.om with the pinned seeded omc, regenerates the corpus with corpus.py, |
| 4 | # runs every stream through the .om and compares each stream's sha256 with the C oracle's recorded |
| 5 | # hashes in expected.txt. This script does not build or run any C: the C oracle (the product's |
| 6 | # omega_codec.c, omega_canonical.c, omega_validate.c, omega_core.c, sha256.c, built with the Makefile |
| 7 | # CFLAGS) was run once by a driver outside the tree; README.md names it and its rerun command. |
| 8 | # usage: sh om/codec/parity/run.sh [--mutants] |
| 9 | # OMEGA_OMC the seed-born omc (default seed/out/omc in this checkout), sha256 pinned below |
| 10 | # Exit 0 PASS, 1 FAIL, 3 NOT_RUN (no pinned omc or no python3). |
| 11 | set -eu |
| 12 | OMC_PIN=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991 |
| 13 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 14 | ROOT=$(cd "$HERE/../../.." && pwd) |
| 15 | OMC=${OMEGA_OMC:-$ROOT/seed/out/omc} |
| 16 | [ -x "$OMC" ] || { echo "NOT_RUN: no omc at $OMC (set OMEGA_OMC)"; exit 3; } |
| 17 | [ "$(sha256sum < "$OMC" | cut -c1-64)" = "$OMC_PIN" ] || { echo "NOT_RUN: $OMC is not the pinned omc"; exit 3; } |
| 18 | command -v python3 >/dev/null || { echo "NOT_RUN: python3 missing"; exit 3; } |
| 19 | T=$(mktemp -d); trap 'rm -rf "$T"' EXIT |
| 20 | STREAMS="decode validate serialize format parse hexid header" |
| 21 | |
| 22 | # run SRC OUTDIR: compile SRC and write every stream into OUTDIR |
| 23 | run() { |
| 24 | mkdir -p "$2" |
| 25 | nice -n 19 "$OMC" < "$1" > "$2/codec" || { echo "FAIL: omc refused $1"; return 1; } |
| 26 | chmod +x "$2/codec" |
| 27 | ( cd "$2" && nice -n 19 ./codec decode < "$T/corpus/decode.pack" && nice -n 19 ./codec text < "$T/corpus/text.pack" \ |
| 28 | && nice -n 19 ./codec hexid < "$T/corpus/hexid.pack" && nice -n 19 ./codec header ) || { echo "FAIL: codec exited $?"; return 1; } |
| 29 | for s in $STREAMS; do printf '%s %s\n' "$(sha256sum < "$2/$s.out" | cut -c1-64)" "$s"; done > "$2/got.txt" |
| 30 | } |
| 31 | |
| 32 | nice -n 19 python3 "$HERE/corpus.py" "$T/corpus" > "$T/corpus.log" |
| 33 | cat "$T/corpus.log" |
| 34 | run "$ROOT/om/codec/codec.om" "$T/om" |
| 35 | fail=0 |
| 36 | for s in $STREAMS; do |
| 37 | want=$(awk -v s="$s" '$2 == s { print $1 }' "$HERE/expected.txt") |
| 38 | got=$(awk -v s="$s" '$2 == s { print $1 }' "$T/om/got.txt") |
| 39 | n=$(wc -l < "$T/om/$s.out") |
| 40 | if [ "$want" = "$got" ]; then echo "PASS $s ($n lines) equals the recorded C hash"; else echo "FAIL $s ($n lines): $got, C recorded $want"; fail=1; fi |
| 41 | done |
| 42 | echo "records: $(wc -l < "$T/om/decode.out") wire inputs ($(awk '$2 == 0' "$T/om/decode.out" | wc -l) decoded), $(grep -c '^[0-9]* [0-9-]* [0-9]*\( [01]:[0-9a-f]*\)*$' "$T/om/parse.out") text inputs, $(wc -l < "$T/om/hexid.out") hexid records, 65536 header pairs" |
| 43 | |
| 44 | # The WCAP limit (README): a graph whose bytes run past 2 MiB. The C decodes it; the port refuses with -2. |
| 45 | python3 - "$T/big.pack" <<'EOF' |
| 46 | import struct, sys |
| 47 | o0 = b"OMG0\x01\x06" + bytes(14) # KIND_MEMORY, no fields, then junk inside clen |
| 48 | o0 += bytes(2097140 - len(o0)) |
| 49 | o1 = b"OMG0\x01\x06" + bytes(14) |
| 50 | g = b"OMGG\x00\x02" + struct.pack(">I", len(o0)) + o0 + struct.pack(">I", len(o1)) + o1 |
| 51 | open(sys.argv[1], "wb").write(struct.pack("<I", len(g)) + g) |
| 52 | EOF |
| 53 | mkdir -p "$T/big" && ( cd "$T/big" && nice -n 19 "$T/om/codec" decode < "$T/big.pack" ) |
| 54 | echo "limit: a $(($(wc -c < "$T/big.pack") - 4))-byte graph past WCAP decodes to: $(cut -d' ' -f2 "$T/big/decode.out") (the C gives 0)" |
| 55 | |
| 56 | # Every .omega/.osc/.bin file under tests/ and examples/ (the spec's program corpus). Not hashed: |
| 57 | # it follows the tree. At acb3f15c the C decoder accepted 0 of 248 (all refused at the OMGG magic). |
| 58 | nice -n 19 python3 "$HERE/corpus.py" "$T/corpus" --tree "$ROOT" > /dev/null |
| 59 | mkdir -p "$T/tree" && ( cd "$T/tree" && nice -n 19 "$T/om/codec" decode < "$T/corpus/tree.pack" ) |
| 60 | echo "tree: $(wc -l < "$T/tree/decode.out") files under tests/ and examples/, accepted by the port: $(awk '$2 == 0' "$T/tree/decode.out" | wc -l)" |
| 61 | |
| 62 | if [ "${1:-}" = --mutants ]; then |
| 63 | # Each mutant is one edit inside one function of codec.om; it must change its stream's hash. |
| 64 | python3 - "$ROOT/om/codec/codec.om" "$T" > "$T/mlist" <<'EOF' |
| 65 | import sys |
| 66 | src, t = sys.argv[1], sys.argv[2] |
| 67 | s = open(src).read() |
| 68 | M = [ # name, function, old, new, stream that must change |
| 69 | ("attr sort reversed", "acmp", "if lt ci cj\nret -1", "if lt ci cj\nret 1", "decode"), |
| 70 | ("key past its NUL", "ekl", "ret i\n", "ret kl\n", "decode"), |
| 71 | ("relation kind order reversed", "rcmp", "if lt ki kj\nret -1", "if lt ki kj\nret 1", "decode"), |
| 72 | ("constraint length tie reversed", "ccmp", "if lt li lj\nret -1", "if lt li lj\nret 1", "decode"), |
| 73 | ("klen 64 accepted", "dattrs", "if ge kl 64", "if gt kl 64", "decode"), |
| 74 | ("effect padding unchecked", "effchk", "if ge i prm", "if gt i prm", "decode"), |
| 75 | ("id limit 4000", "ccanon", "if gt n 4096", "if gt n 4000", "decode"), |
| 76 | ("unsigned width 255", "vtype", "if and eq tag 3 or lt wd 1 gt wd 256", "if and eq tag 3 or lt wd 1 gt wd 255", "validate"), |
| 77 | ("value bytes rounding", "vvalue", "ex = div add wd 7 8", "ex = div add wd 8 8", "validate"), |
| 78 | ("operand window offset", "dtgt", "p = add 33 mul a 32", "p = add 32 mul a 32", "validate"), |
| 79 | ("struct value offset", "sattr", "ret ulong get w add add o kl add 2 v", "ret ulong get w add add o kl add 3 v", "validate"), |
| 80 | ("serialize length check", "cserial", "if gt add add pos 4 L mx", "if gt add pos L mx", "serialize"), |
| 81 | ("stale line length", "cformat", "if ge wv sub cap pos\nret -1", "if gt wv sub cap pos\nret -1", "format"), |
| 82 | ("value last 8 bytes", "fline", "s = sub bl 8", "s = sub bl 7", "format"), |
| 83 | ("no binary prefix", "spref", "bs = 2\n", "bs = 8\n", "parse"), |
| 84 | ("overflow ignored", "sdig", "put r 1 1\n", "put r 1 0\n", "parse"), |
| 85 | ("strstr to line end", "seol", "if eq get s i 10", "if eq get s i 11", "parse"), |
| 86 | ("negative pair", "hx2", "ret and neg v 255", "ret v", "hexid"), |
| 87 | ("memcmp sign", "cidcmp", "if lt p q\nret -1", "if lt p q\nret 1", "hexid"), |
| 88 | ("v1 effect code", "cchkhdr", "ret -2", "ret -1", "header"), |
| 89 | ("sha K[63]", "shak", "put k 63 3329325298", "put k 63 3329325299", "decode"), |
| 90 | ] |
| 91 | for i, (name, fn, old, new, stream) in enumerate(M): |
| 92 | a = s.index("\nfn %s " % fn) |
| 93 | b = s.index("\nfn ", a + 1) |
| 94 | body = s[a:b] |
| 95 | if body.count(old) != 1: |
| 96 | sys.exit("mutant %s: '%s' is not unique in %s" % (name, old, fn)) |
| 97 | open("%s/m%d.om" % (t, i), "w").write(s[:a] + body.replace(old, new) + s[b:]) |
| 98 | print("%d %s %s" % (i, stream, name)) |
| 99 | EOF |
| 100 | mfail=0 |
| 101 | n=0 |
| 102 | while read -r i stream name; do |
| 103 | n=$((n + 1)) |
| 104 | if ! run "$T/m$i.om" "$T/mo" > /dev/null; then echo "FAIL mutant $i ($name): did not build or run"; mfail=1; continue; fi |
| 105 | changed="" |
| 106 | for s in $STREAMS; do |
| 107 | a=$(awk -v s="$s" '$2 == s { print $1 }' "$T/om/got.txt"); b=$(awk -v s="$s" '$2 == s { print $1 }' "$T/mo/got.txt") |
| 108 | [ "$a" = "$b" ] || changed="$changed $s" |
| 109 | done |
| 110 | case " $changed " in |
| 111 | *" $stream "*) echo "killed mutant $i ($name): changes$changed" ;; |
| 112 | *) echo "FAIL mutant $i ($name): $stream unchanged (changes:${changed:- nothing})"; mfail=1 ;; |
| 113 | esac |
| 114 | rm -rf "$T/mo" |
| 115 | done < "$T/mlist" |
| 116 | [ $mfail -eq 0 ] && echo "PASS all $n mutants change their stream's hash" || { echo "FAIL a mutant changed no stream"; fail=1; } |
| 117 | fi |
| 118 | [ $fail -eq 0 ] && echo "PASS om/codec parity: every stream equals the C oracle's recorded hash" || { echo "FAIL om/codec parity"; exit 1; } |