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 / tools / codebook / mutate.sh

75 lines · 4051 bytes Raw Blame History
1#!/bin/sh
2# mutate.sh -- mutation check: each mutant of codebook.om must make test.sh fail.
3# Run from the repo root: sh tools/codebook/mutate.sh (OMC defaults to seed/out/omc)
4set -u
5R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd); cd "$R"
6OMC=${OMC:-$R/seed/out/omc}
7case $OMC in /*) ;; *) OMC=$R/$OMC ;; esac
8export OMC
9[ -f "$OMC" ] && [ -x "$OMC" ] || { echo "compiler unavailable: $OMC" >&2; exit 2; }
10if [ -z "${M:-}" ]; then M=$(mktemp -d "${TMPDIR:-/tmp}/codebook-mut.XXXXXX") || exit 2
11else
12 if [ -e "$M" ] || [ -L "$M" ]; then echo "work directory already exists: $M (preserved)" >&2; exit 2; fi
13 mkdir -p "$M" || exit 2
14fi
15case $M in /*) ;; *) M=$R/$M ;; esac
16# Only completed assertion failures are kills. Preserve full logs and raw exits.
17check_test() {
18 BIN=$1 FIX=$2 sh tools/codebook/test.sh > "$3" 2>&1; rc=$?
19 printf '%s\n' "$rc" > "$3.rc"
20 total=$(grep '^TOTAL pass=[0-9]* fail=[0-9]* skip=[0-9]*$' "$3" | tail -1)
21 failed=${total#*fail=}; failed=${failed%% *}
22 case $total in
23 *' skip=0')
24 if [ "$rc" = 0 ] && [ "$failed" = 0 ]; then echo survived
25 elif [ "$rc" = 1 ] && [ "$failed" -ge 1 ] 2>/dev/null && grep -q '^FAIL ' "$3"; then echo killed
26 else echo harness-error; fi ;;
27 *) echo harness-error ;;
28 esac
29}
30B="$M/base" sh tools/codebook/build.sh > "$M/base.build.log" 2>&1 || { echo 'BASELINE build failed'; exit 5; }
31base=$(check_test "$M/base/codebook" "$M/base/fix" "$M/base.test.log")
32[ "$base" = survived ] || { echo "BASELINE did not pass: $base"; exit 5; }
33KEYDIR=$M/key; export KEYDIR
34SRC=tools/codebook/codebook.om
35# name|sed expression (applied to one copy of the source each)
36cat > "$M/list" <<'L'
37escape-threshold-800|/^fn decl /,/^$/s/^if lt g 900$/if lt g 800/
38no-dangling-escape-check|/^fn decl /,/^$/s/^ret 3$/ret 0/
39encoder-no-structure-check|/^fn encv /,/^$/s/^ret 5$/ret 0/
40o3-digit-order|/^fn o3 /,/^$/s/rem div v 100 10/rem v 10/
41hmac-ipad-55|s/mbyte uchar xor get kz i 54/mbyte uchar xor get kz i 55/
42long-key-not-hashed|/^fn mkk0 /,/^$/s/^if gt n 64$/if gt n 65/
43mac-compare-always-true|/^fn maceq /,/^$/s/^ret eq x 0$/ret true/
44mac-compare-first-byte-only|/^fn maceq /,/^$/s/^for i ulong 64$/for i ulong 1/
45replay-accepted|/^fn verl4 /,/^$/s/^if le ctr rdctr s sn$/if lt ctr rdctr s sn/
46counter-not-stored|/^fn verl4 /,/^$/s/^wrctr s sn c cn$//
47cbsha-not-checked|/^fn verl3 /,/^$/s/^bad vcb$/put mo 0 get mo 0/
48trit-ignores-verification|/^fn ltrit /,/^$/s/^if and ok em$/if em/
49emergency-lower-bound-100|/^fn hasem /,/^$/s/ge g 300/ge g 100/
50bademd-trit-ignores-ok|/^fn ltrit /,/^$/s/^if and ok em$/if or ok em/
51emergency-range-to-499|/^fn hasem /,/^$/s/lt g 400/lt g 500/
52codebook-dup-allowed|/^fn cbent2 /,/^$/s/^if sameb get eo b cs0 cl$/if false/
53unknown-codes-accepted|/^fn known /,/^$/s/^if eq lookup a n false$/if false/
54missing-mac-not-unsigned|/^fn verl /,/^$/s/^if eq get fs 0 get fs 1$/if false/
55malformed-counter-fails-open|/^fn rdctf /,/^$/s/^ret BIG$/ret 0/
56L
57n=0; caught=0; invalid=0; errors=0
58while IFS='|' read -r name expr; do
59 n=$((n+1)); d=$M/$name; mkdir -p "$d"
60 sed "$expr" "$SRC" > "$d/codebook.om"
61 if cmp -s "$d/codebook.om" "$SRC"; then echo "MUTANT $name: sed did not change the source (BAD LIST)"; invalid=$((invalid+1)); continue; fi
62 { printf 'om2\n'; grep '^abstract ' "$d/codebook.om"; sed '/^fn sha256 /,$d' seed/trace/sha256.om; grep -v "^abstract " "$d/codebook.om"; cat tools/codebook/main.om; } > "$d/full.om"
63 if ! "$OMC" < "$d/full.om" > "$d/bin" 2>"$d/omc.err"; then echo "MUTANT $name: does not compile ($(cat "$d/omc.err"))"; invalid=$((invalid+1)); continue; fi
64 chmod +x "$d/bin"
65 out=$(check_test "$d/bin" "$d/fix" "$d/test.log")
66 case "$out" in
67 survived) echo "MUTANT $name: SURVIVED" ;;
68 killed) caught=$((caught+1)); echo "MUTANT $name: caught (assertions failed)" ;;
69 *) errors=$((errors+1)); echo "MUTANT $name: HARNESS ERROR (not a kill)" ;;
70 esac
71done < "$M/list"
72echo "MUTANTS $n CAUGHT $caught"
73[ "$errors" = 0 ] || exit 4
74[ "$invalid" = 0 ] || exit 3
75[ "$n" -gt 0 ] && [ "$caught" -eq "$n" ]