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 / trace / cli-gate.sh

169 lines · 6513 bytes Raw Blame History
1#!/bin/sh
2# seed/trace/cli-gate.sh: omega-mv #610 offline trace CLI.
3# Run from the repository root: sh seed/trace/cli-gate.sh
4# Checks seed/out/omc against seed/out/omc.sha256, compiles tracegen and tracecli,
5# regenerates fixtures and pins their SHA-256, runs cli.cases against cli-golden,
6# refuses a mode-0000 segment, and requires four source mutants to fail their own
7# checks. The read-only mutant (ropen flags 32768 -> 32770) is caught by a literal
8# scan of the exact return line in tracecli.om. An O_RDWR open of a mode-0400 file
9# is not a reliable refusal for the owner, and verify uses segment.om's own open,
10# so a functional open check would not catch that mutant.
11set -eu
12cd "$(dirname "$0")/../.."
13
14bad() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
15
16sha256sum -c seed/out/omc.sha256 >/dev/null || bad "omc sha256"
17[ "$(id -u)" != 0 ] || bad "mode-0000 check is meaningless as root"
18
19ROPEN='ret fopen tpath 0 get tlen 0 32768 0'
20ROPEN_MUT='ret fopen tpath 0 get tlen 0 32770 0'
21scan_ropen() {
22 c=$(grep -c -x -F "$ROPEN" "$1" || true)
23 [ "$c" = 1 ]
24}
25scan_ropen seed/trace/tracecli.om || bad "ropen flags constant is not 32768 exactly once"
26if grep -q -x -F "$ROPEN_MUT" seed/trace/tracecli.om; then
27 bad "ropen flags 32770 present in the good source"
28fi
29[ "$(grep -c -x 'ret 3' seed/trace/tracecli.om || true)" = 1 ] || bad "partial site not unique"
30[ "$(grep -c -x 'ret same' seed/trace/tracecli.om || true)" = 1 ] || bad "same site not unique"
31[ "$(grep -c -x -F 'if and ne get qtype 0 65536 ne t get qtype 0' seed/trace/tracecli.om || true)" = 1 ] || bad "qtype site not unique"
32
33if [ -n "${OUT_DIR:-}" ]; then
34 B="$(pwd)/$OUT_DIR/trace-cli"
35else
36 B="$(pwd)/build/seed/trace-cli"
37fi
38mkdir -p "$B"
39compile() {
40 cat seed/trace/segment.om seed/trace/tsha.om "$1" | seed/out/omc > "$2"
41 [ "$(od -An -tx1 -N4 "$2" | tr -d ' ')" = 7f454c46 ] || bad "compile did not emit ELF: $1"
42 chmod 700 "$2"
43}
44compile seed/trace/tracegen.om "$B/tracegen"
45compile seed/trace/tracecli.om "$B/tracecli"
46
47ROOT=$(pwd)
48D=$(mktemp -d "${TMPDIR:-/tmp}/trace-cli.XXXXXX")
49cleanup() {
50 chmod -R u+rwx "$D" 2>/dev/null || true
51 rm -rf "$D"
52}
53trap cleanup EXIT
54
55( cd "$D" && "$B/tracegen" ) || bad "tracegen failed"
56if ! ( cd "$D" && sha256sum -c "$ROOT/seed/trace/fixtures.sha256" > "$D/sha.out" ); then
57 cat "$D/sha.out" >&2
58 bad "fixture sha"
59fi
60[ "$(readlink "$D/fx/link.seg")" = a.seg ] || bad "link target"
61for f in a b c crc torn magic; do
62 m=$(stat -c %a "$D/fx/$f.seg")
63 [ "$m" = 400 ] || bad "fixture $f mode $m want 400"
64done
65
66n=0
67while IFS= read -r line || [ -n "$line" ]; do
68 case "$line" in ''|\#*) continue ;; esac
69 set -f
70 # shellcheck disable=SC2086
71 set -- $line
72 set +f
73 name=$1
74 want=$2
75 shift 2
76 out="$D/case.out"
77 err="$D/case.err"
78 set +e
79 ( cd "$D" && "$B/tracecli" "$@" ) > "$out" 2>"$err"
80 rc=$?
81 set -e
82 [ ! -s "$err" ] || { cat "$err" >&2; bad "case $name wrote stderr"; }
83 cmp -s "$out" "seed/trace/cli-golden/$name.txt" || bad "case $name stdout"
84 [ "$rc" = "$want" ] || bad "case $name exit $rc want $want"
85 n=$((n + 1))
86done < seed/trace/cli.cases
87[ "$n" = 28 ] || bad "ran $n cases, need 28"
88
89( cd "$D" && "$B/tracecli" export fx/a.seg ) > "$D/export.out"
90got=$(sha256sum "$D/fx/a.seg" | cut -d' ' -f1)
91grep -q -F "# source sha256=$got " "$D/export.out" || bad "export hash != sha256sum"
92
93cp "$D/fx/a.seg" "$D/fx/zero.seg"
94chmod 000 "$D/fx/zero.seg"
95for cmd in verify show events stats export; do
96 set +e
97 ( cd "$D" && "$B/tracecli" "$cmd" fx/zero.seg ) > "$D/zero.out" 2>"$D/zero.err"
98 rc=$?
99 set -e
100 [ ! -s "$D/zero.err" ] || bad "mode-0000 $cmd wrote stderr"
101 [ "$rc" = 4 ] || bad "mode-0000 $cmd exit $rc want 4"
102 cmp -s "$D/zero.out" seed/trace/cli-golden/zero-read.txt || bad "mode-0000 $cmd stdout"
103done
104chmod 644 "$D/fx/zero.seg"
105rm -f "$D/fx/zero.seg"
106printf 'mode-0000 refused by verify show events stats export\n'
107
108# $1 dest $2 exact old line $3 exact new line. The old line must occur once
109# and be gone afterwards. The new line may already occur; its count must rise by 1.
110mut_swap() {
111 cp seed/trace/tracecli.om "$1"
112 o=$(grep -c -x -F "$2" "$1" || true)
113 before=$(grep -c -x -F "$3" "$1" || true)
114 [ "$o" = 1 ] || bad "mutant site count $o for: $2"
115 awk -v old="$2" -v new="$3" '($0 == old) { print new; next } { print }' "$1" > "$1.tmp"
116 mv "$1.tmp" "$1"
117 o=$(grep -c -x -F "$2" "$1" || true)
118 nn=$(grep -c -x -F "$3" "$1" || true)
119 [ "$o" = 0 ] || bad "mutant site survived: $2"
120 [ "$nn" = "$((before + 1))" ] || bad "mutant replacement count $nn want $((before + 1)) for: $3"
121}
122
123mut_swap "$D/mut-partial.om" 'ret 3' 'ret 0'
124compile "$D/mut-partial.om" "$B/mut-partial"
125set +e
126( cd "$D" && "$B/mut-partial" verify fx/crc.seg ) > "$D/mut.out" 2>"$D/mut.err"
127rc=$?
128set -e
129[ ! -s "$D/mut.err" ] || bad "partial mutant wrote stderr"
130cmp -s "$D/mut.out" seed/trace/cli-golden/verify-crc.txt || bad "partial mutant stdout"
131[ "$rc" = 0 ] || bad "partial mutant exit $rc want 0"
132printf 'caught mutant partial: verify crc exit 0 (need 3)\n'
133
134mut_swap "$D/mut-same.om" 'ret same' 'ret true'
135compile "$D/mut-same.om" "$B/mut-same"
136set +e
137( cd "$D" && "$B/mut-same" compare fx/a.seg fx/b.seg ) > "$D/mut.out" 2>"$D/mut.err"
138rc=$?
139set -e
140[ ! -s "$D/mut.err" ] || bad "same mutant wrote stderr"
141grep -q -F 'compare differ at=0' "$D/mut.out" || bad "same mutant did not differ at 0"
142if grep -q -F 'compare differ at=17' "$D/mut.out"; then
143 bad "same mutant still differs at 17"
144fi
145[ "$rc" = 1 ] || bad "same mutant exit $rc want 1"
146printf 'caught mutant same: compare a b differ at=0 (need 17)\n'
147
148mut_swap "$D/mut-qtype.om" 'if and ne get qtype 0 65536 ne t get qtype 0' 'if and ne get qtype 0 65536 eq t get qtype 0'
149compile "$D/mut-qtype.om" "$B/mut-qtype"
150set +e
151( cd "$D" && "$B/mut-qtype" events fx/a.seg --type 7 ) > "$D/mut.out" 2>"$D/mut.err"
152rc=$?
153set -e
154[ ! -s "$D/mut.err" ] || bad "qtype mutant wrote stderr"
155grep -q -F 'matched=40 of=40' "$D/mut.out" || bad "qtype mutant did not match all"
156if grep -q -F 'matched=0 of=40' "$D/mut.out"; then
157 bad "qtype mutant still matched none"
158fi
159[ "$rc" = 0 ] || bad "qtype mutant exit $rc want 0"
160printf 'caught mutant qtype: events --type 7 matched=40 (need 0)\n'
161
162mut_swap "$D/mut-ropen.om" "$ROPEN" "$ROPEN_MUT"
163compile "$D/mut-ropen.om" "$B/mut-ropen"
164if scan_ropen "$D/mut-ropen.om"; then
165 bad "ropen mutant was not caught by the flag scan"
166fi
167printf 'caught mutant ropen: flag scan rejected 32770\n'
168
169printf 'PASS tracecli %s cases, mode-0000 refused, 4 mutants caught\n' "$n"