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 / omc / tests / struct-bool / check.sh
| 1 | #!/bin/sh |
| 2 | # check.sh -- structs with bool fields (regression for: struct W a uint f bool end, new W 1 false, |
| 3 | # which omc accepted and compiled to a program that dies by SIGILL). The compiler under test is |
| 4 | # seed/out/omc (argument 1 or OMC overrides it). Every row of cases.tsv must compile and the program |
| 5 | # must give its exact exit status. Controls, which must pass even on a broken compiler, keep the rows |
| 6 | # honest: bool logic without a struct, the uchar twin of each row, source mutants of the twins, and a |
| 7 | # self-test that gives the row runner two fake broken compilers and requires it to fail every row. |
| 8 | # Exit 0: all rows and controls pass. Exit 1: controls pass, some row fails (the bug is present). |
| 9 | # Exit 2: a control failed, so the result of the rows cannot be trusted. Host only, no GPU, no build. |
| 10 | # Run as: nice -n19 taskset -c 0 sh seed/omc/tests/struct-bool/check.sh |
| 11 | set -u |
| 12 | R=$(CDPATH= cd -- "$(dirname "$0")/../../../.." && pwd) |
| 13 | cd "$R" |
| 14 | D=seed/omc/tests/struct-bool |
| 15 | mkdir -p build/seed |
| 16 | B=$(mktemp -d build/seed/struct-bool.XXXXXX) |
| 17 | S=${1:-${OMC:-seed/out/omc}} |
| 18 | case "$S" in /*) ;; *) S="$R/$S";; esac |
| 19 | [ -x "$S" ] || { printf 'FAIL: %s missing; run sh seed/omega-from-seed.sh first\n' "$S" >&2; exit 2; } |
| 20 | printf "STRUCTBOOL compiler %s %s scratch %s\n" "$S" "$(sha256sum "$S" | cut -d' ' -f1)" "$B" |
| 21 | ulimit -c 0 |
| 22 | say() { printf '%s\n' "$*"; } |
| 23 | |
| 24 | # run_status BIN -> prints the exit status (132 = SIGILL), nothing else, no shell job messages |
| 25 | run_status() { timeout 15 sh -c '"$1" >/dev/null 2>&1; echo $?' sh "$1" 2>/dev/null; } |
| 26 | |
| 27 | # twin SRC DST: bool -> uchar, true -> 1, false -> 0 |
| 28 | twin() { sed -E 's/\bbool\b/uchar/g; s/\btrue\b/1/g; s/\bfalse\b/0/g' "$1" > "$2"; } |
| 29 | |
| 30 | # compile COMPILER SRC OUT -> sets crc; OUT is the object, OUT.err the diagnostics |
| 31 | compile() { timeout 60 "$1" < "$2" > "$3" 2> "$3.err"; crc=$?; [ "$crc" != 0 ] || chmod +x "$3"; } |
| 32 | |
| 33 | # rows COMPILER PREFIX: run every row of cases.tsv, one PASS/FAIL line each into PREFIX.log |
| 34 | rows() { |
| 35 | : > "$2.log" |
| 36 | grep -v '^#' "$D/cases.tsv" | while IFS=' ' read -r name want tw kind; do |
| 37 | [ -n "$name" ] || continue |
| 38 | o="$2.$name" |
| 39 | compile "$1" "$D/$name.om" "$o" |
| 40 | if [ "$crc" != 0 ]; then echo "FAIL $name compiler exit $crc ($(head -c 80 "$o.err"))"; continue; fi |
| 41 | rc=$(run_status "$o") |
| 42 | if [ "$rc" = "$want" ]; then echo "PASS $name exit $rc" |
| 43 | elif [ "$rc" = 132 ]; then echo "FAIL $name SIGILL (exit 132), wanted $want" |
| 44 | else echo "FAIL $name exit $rc, wanted $want"; fi |
| 45 | done >> "$2.log" |
| 46 | } |
| 47 | |
| 48 | controls=0; cfail=0 |
| 49 | cbad() { cfail=$((cfail+1)); say "FAIL control: $*"; } |
| 50 | cok() { controls=$((controls+1)); say "PASS control: $*"; } |
| 51 | |
| 52 | # 1. bool logic without a struct runs correctly |
| 53 | compile "$S" "$D/control_nostruct.om" "$B/nostruct" |
| 54 | if [ "$crc" != 0 ]; then cbad "control_nostruct does not compile ($(head -c 80 "$B/nostruct.err"))" |
| 55 | else rc=$(run_status "$B/nostruct") |
| 56 | if [ "$rc" = 15 ]; then cok "bool locals and function without a struct give 15"; else cbad "control_nostruct exit $rc, wanted 15"; fi; fi |
| 57 | |
| 58 | # 2. the uchar twin of each row gives the same status |
| 59 | grep -v '^#' "$D/cases.tsv" | while IFS=' ' read -r name want tw kind; do |
| 60 | [ "$tw" = yes ] || continue |
| 61 | twin "$D/$name.om" "$B/$name.tw.om" |
| 62 | cmp -s "$D/$name.om" "$B/$name.tw.om" && { echo "BAD $name twin equals source"; continue; } |
| 63 | compile "$S" "$B/$name.tw.om" "$B/$name.tw" |
| 64 | if [ "$crc" != 0 ]; then echo "BAD $name twin does not compile ($(head -c 80 "$B/$name.tw.err"))"; continue; fi |
| 65 | rc=$(run_status "$B/$name.tw") |
| 66 | [ "$rc" = "$want" ] && echo "OK $name twin exit $rc" || echo "BAD $name twin exit $rc, wanted $want" |
| 67 | done > "$B/twins.log" |
| 68 | nt=$(grep -c '^OK' "$B/twins.log"); nb=$(grep -c '^BAD' "$B/twins.log") |
| 69 | expected_twins=$(awk -F '\t' '$3 == "yes" {n++} END {print n+0}' "$D/cases.tsv") |
| 70 | if [ "$nb" = 0 ] && [ "$nt" = "$expected_twins" ] && [ "$nt" -gt 0 ]; then cok "$nt uchar twins give the expected status" |
| 71 | else cat "$B/twins.log"; cbad "uchar twins: $nt ok, $nb bad"; fi |
| 72 | |
| 73 | # 3. mutants of the twins change the status (each site occurs exactly once) |
| 74 | grep -v '^#' "$D/mutants.tsv" | while IFS=' ' read -r name expr; do |
| 75 | [ -n "$name" ] || continue |
| 76 | want=$(grep "^$name " "$D/cases.tsv" | cut -f2) |
| 77 | twin "$D/$name.om" "$B/$name.tw.om" |
| 78 | sed -E "$expr" "$B/$name.tw.om" > "$B/$name.mut.om" |
| 79 | cmp -s "$B/$name.tw.om" "$B/$name.mut.om" && { echo "BAD $name mutant site not found"; continue; } |
| 80 | compile "$S" "$B/$name.mut.om" "$B/$name.mut" |
| 81 | if [ "$crc" != 0 ]; then echo "BAD $name mutant does not compile"; continue; fi |
| 82 | rc=$(run_status "$B/$name.mut") |
| 83 | if [ "$rc" = "$want" ]; then echo "BAD $name mutant not detected (exit $rc)" |
| 84 | elif [ "$rc" -ge 100 ]; then echo "BAD $name mutant only crashed (exit $rc)" |
| 85 | else echo "OK $name mutant exit $rc, not $want"; fi |
| 86 | done > "$B/mutants.log" |
| 87 | nm=$(grep -c '^OK' "$B/mutants.log"); nmb=$(grep -c '^BAD' "$B/mutants.log") |
| 88 | expected_mutants=$(grep -v '^#' "$D/mutants.tsv" | grep -c .) |
| 89 | if [ "$nmb" = 0 ] && [ "$nm" = "$expected_mutants" ] && [ "$nm" -gt 0 ]; then cok "$nm twin mutants change the status" |
| 90 | else cat "$B/mutants.log"; cbad "mutants: $nm detected, $nmb not"; fi |
| 91 | |
| 92 | # 4. the row runner fails every row on fake broken compilers (the control that catches a broken one) |
| 93 | total=$(grep -v '^#' "$D/cases.tsv" | grep -c .) |
| 94 | nonzero=$(grep -v '^#' "$D/cases.tsv" | awk -F '\t' 'NF && $2 != "0"' | wc -l) |
| 95 | printf '#!/bin/sh\ncat >/dev/null\nprintf "#!/bin/sh\\nkill -ILL \\$\\$\\n"\n' > "$B/fake-sigill" |
| 96 | printf '#!/bin/sh\ncat >/dev/null\nprintf "#!/bin/sh\\nexit 0\\n"\n' > "$B/fake-zero" |
| 97 | chmod +x "$B/fake-sigill" "$B/fake-zero" |
| 98 | rows "$B/fake-sigill" "$B/fs"; rows "$B/fake-zero" "$B/fz" |
| 99 | fs=$(grep -c '^FAIL' "$B/fs.log"); fz=$(grep -c '^FAIL' "$B/fz.log") |
| 100 | if [ "$fs" = "$total" ] && grep -q 'SIGILL' "$B/fs.log"; then cok "a compiler whose programs die by SIGILL fails all $total rows" |
| 101 | else cbad "fake SIGILL compiler failed $fs of $total rows"; fi |
| 102 | if [ "$fz" = "$nonzero" ]; then cok "a compiler whose programs always exit 0 fails the $nonzero rows that want a non-zero status" |
| 103 | else cbad "fake zero compiler failed $fz rows, wanted $nonzero"; fi |
| 104 | |
| 105 | # 5. the row runner passes every twin-able row on a stand-in for a fixed compiler: the real compiler |
| 106 | # fed the uchar twin of the source. A pass path that never fires would hide a runner that cannot pass. |
| 107 | cat > "$B/fake-twin" <<TWIN |
| 108 | #!/bin/sh |
| 109 | sed -E 's/\\bbool\\b/uchar/g; s/\\btrue\\b/1/g; s/\\bfalse\\b/0/g' | exec "$S" |
| 110 | TWIN |
| 111 | chmod +x "$B/fake-twin" |
| 112 | rows "$B/fake-twin" "$B/ft" |
| 113 | twins=$(grep -v '^#' "$D/cases.tsv" | awk -F '\t' '$3 == "yes"' | wc -l) |
| 114 | bad=0 |
| 115 | for name in $(grep -v '^#' "$D/cases.tsv" | awk -F '\t' '$3 == "yes" {print $1}'); do |
| 116 | grep -q "^PASS $name " "$B/ft.log" || { bad=$((bad+1)); grep "^FAIL $name " "$B/ft.log"; } |
| 117 | done |
| 118 | if [ "$bad" = 0 ]; then cok "a stand-in fixed compiler (uchar twin) passes all $twins twin-able rows" |
| 119 | else cbad "stand-in fixed compiler failed $bad of $twins twin-able rows"; fi |
| 120 | |
| 121 | # the rows against the compiler under test |
| 122 | rows "$S" "$B/real" |
| 123 | cat "$B/real.log" |
| 124 | pass=$(grep -c '^PASS' "$B/real.log"); fail=$(grep -c '^FAIL' "$B/real.log") |
| 125 | if [ "$cfail" != 0 ]; then |
| 126 | say "STRUCTBOOL INVALID controls=$controls failed=$cfail rows pass=$pass fail=$fail (the rows cannot be trusted)" |
| 127 | exit 2 |
| 128 | fi |
| 129 | if [ "$fail" != 0 ] || [ "$pass" != "$total" ]; then |
| 130 | say "STRUCTBOOL FAIL rows pass=$pass fail=$fail of $total (controls PASS $controls): struct bool field is broken" |
| 131 | exit 1 |
| 132 | fi |
| 133 | say "STRUCTBOOL PASS rows=$pass controls=$controls" |