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 / tools / codebook / mutate.sh
| 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) |
| 4 | set -u |
| 5 | R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd); cd "$R" |
| 6 | OMC=${OMC:-$R/seed/out/omc} |
| 7 | case $OMC in /*) ;; *) OMC=$R/$OMC ;; esac |
| 8 | export OMC |
| 9 | [ -f "$OMC" ] && [ -x "$OMC" ] || { echo "compiler unavailable: $OMC" >&2; exit 2; } |
| 10 | if [ -z "${M:-}" ]; then M=$(mktemp -d "${TMPDIR:-/tmp}/codebook-mut.XXXXXX") || exit 2 |
| 11 | else |
| 12 | if [ -e "$M" ] || [ -L "$M" ]; then echo "work directory already exists: $M (preserved)" >&2; exit 2; fi |
| 13 | mkdir -p "$M" || exit 2 |
| 14 | fi |
| 15 | case $M in /*) ;; *) M=$R/$M ;; esac |
| 16 | # Only completed assertion failures are kills. Preserve full logs and raw exits. |
| 17 | check_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 | } |
| 30 | B="$M/base" sh tools/codebook/build.sh > "$M/base.build.log" 2>&1 || { echo 'BASELINE build failed'; exit 5; } |
| 31 | base=$(check_test "$M/base/codebook" "$M/base/fix" "$M/base.test.log") |
| 32 | [ "$base" = survived ] || { echo "BASELINE did not pass: $base"; exit 5; } |
| 33 | KEYDIR=$M/key; export KEYDIR |
| 34 | SRC=tools/codebook/codebook.om |
| 35 | # name|sed expression (applied to one copy of the source each) |
| 36 | cat > "$M/list" <<'L' |
| 37 | escape-threshold-800|/^fn decl /,/^$/s/^if lt g 900$/if lt g 800/ |
| 38 | no-dangling-escape-check|/^fn decl /,/^$/s/^ret 3$/ret 0/ |
| 39 | encoder-no-structure-check|/^fn encv /,/^$/s/^ret 5$/ret 0/ |
| 40 | o3-digit-order|/^fn o3 /,/^$/s/rem div v 100 10/rem v 10/ |
| 41 | hmac-ipad-55|s/mbyte uchar xor get kz i 54/mbyte uchar xor get kz i 55/ |
| 42 | long-key-not-hashed|/^fn mkk0 /,/^$/s/^if gt n 64$/if gt n 65/ |
| 43 | mac-compare-always-true|/^fn maceq /,/^$/s/^ret eq x 0$/ret true/ |
| 44 | mac-compare-first-byte-only|/^fn maceq /,/^$/s/^for i ulong 64$/for i ulong 1/ |
| 45 | replay-accepted|/^fn verl4 /,/^$/s/^if le ctr rdctr s sn$/if lt ctr rdctr s sn/ |
| 46 | counter-not-stored|/^fn verl4 /,/^$/s/^wrctr s sn c cn$// |
| 47 | cbsha-not-checked|/^fn verl3 /,/^$/s/^bad vcb$/put mo 0 get mo 0/ |
| 48 | trit-ignores-verification|/^fn ltrit /,/^$/s/^if and ok em$/if em/ |
| 49 | emergency-lower-bound-100|/^fn hasem /,/^$/s/ge g 300/ge g 100/ |
| 50 | bademd-trit-ignores-ok|/^fn ltrit /,/^$/s/^if and ok em$/if or ok em/ |
| 51 | emergency-range-to-499|/^fn hasem /,/^$/s/lt g 400/lt g 500/ |
| 52 | codebook-dup-allowed|/^fn cbent2 /,/^$/s/^if sameb get eo b cs0 cl$/if false/ |
| 53 | unknown-codes-accepted|/^fn known /,/^$/s/^if eq lookup a n false$/if false/ |
| 54 | missing-mac-not-unsigned|/^fn verl /,/^$/s/^if eq get fs 0 get fs 1$/if false/ |
| 55 | malformed-counter-fails-open|/^fn rdctf /,/^$/s/^ret BIG$/ret 0/ |
| 56 | L |
| 57 | n=0; caught=0; invalid=0; errors=0 |
| 58 | while 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 |
| 71 | done < "$M/list" |
| 72 | echo "MUTANTS $n CAUGHT $caught" |
| 73 | [ "$errors" = 0 ] || exit 4 |
| 74 | [ "$invalid" = 0 ] || exit 3 |
| 75 | [ "$n" -gt 0 ] && [ "$caught" -eq "$n" ] |