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 / trit / mutate.sh
| 1 | #!/bin/sh |
| 2 | # mutate.sh -- each mutant breaks one round-4 rule in trit.om or the shared codebook.om; the fixture |
| 3 | # suite (test.sh) must kill every mutant, and the Lean differential test (difftest.sh, N=200 seed 3) |
| 4 | # must kill every mutant whose behaviour it models (read mode; see "ack" below). |
| 5 | # Usage from the repo root: sh tools/trit/mutate.sh (needs perl, python3, lean) |
| 6 | # OMC compiler, default <repo>/seed/out/omc (a relative path is made absolute) |
| 7 | # LEAN lean binary, default lean |
| 8 | # W work directory, default a fresh mktemp -d; per-mutant logs are kept under $W/logs |
| 9 | # Verdicts, one line per mutant, from three independent observations: |
| 10 | # NOT APPLIED the mutation expression changed nothing: the mutant is not a mutant |
| 11 | # BUILD INVALID the mutant (or the baseline) did not compile |
| 12 | # test.sh killed only when it ran to its own "TOTAL pass=P fail=F skip=S" line with F>=1 and exit 1; |
| 13 | # exit 0 = survived; anything else (exit 2 "no binary", crash, timeout, no TOTAL line) = |
| 14 | # HARNESS ERROR, never counted as a kill |
| 15 | # difftest killed only on its own "DIFF disagree=" line with exit 1; "DIFF agree=" = survived; |
| 16 | # anything else (python3/lean failure, no output) = HARNESS ERROR |
| 17 | # The unmutated tree is built and must pass both first; otherwise no mutant is run. |
| 18 | # difftest models read mode only (no --ack, no fsync), so the "ack" mutants are judged by test.sh alone |
| 19 | # and the differential outcome is recorded, not required. |
| 20 | # Exit: 0 every mutant applied, built, killed by test.sh, and killed by difftest where modelled; |
| 21 | # 1 a mutant survived; 2 a prerequisite is missing; 3 a mutant is not applied or invalid; |
| 22 | # 4 a harness error; 5 the baseline did not build or did not pass. |
| 23 | # Prerequisites are checked first; a failed baseline exits next. Mutant result precedence: 4, 3, 1. |
| 24 | set -u |
| 25 | R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd) |
| 26 | cd "$R" || exit 2 |
| 27 | OMC=${OMC:-$R/seed/out/omc} |
| 28 | case $OMC in /*) ;; *) OMC=$(pwd)/$OMC ;; esac |
| 29 | LEAN=${LEAN:-lean} |
| 30 | export OMC LEAN |
| 31 | for t in perl python3 cmp timeout; do |
| 32 | command -v "$t" >/dev/null 2>&1 || { echo "mutate.sh: prerequisite missing: $t" >&2; exit 2; } |
| 33 | done |
| 34 | [ -f "$OMC" ] && [ -x "$OMC" ] || { echo "mutate.sh: prerequisite missing: compiler $OMC is not an executable file" >&2; exit 2; } |
| 35 | command -v "$LEAN" >/dev/null 2>&1 || { echo "mutate.sh: prerequisite missing: lean ($LEAN)" >&2; exit 2; } |
| 36 | if [ -z "${W:-}" ]; then W=$(mktemp -d "${TMPDIR:-/tmp}/trit-mut.XXXXXX") || exit 2; else |
| 37 | if [ -e "$W" ] || [ -L "$W" ]; then echo "work directory already exists: $W (preserved)" >&2; exit 2; fi |
| 38 | mkdir -p "$W" || exit 2 |
| 39 | fi |
| 40 | case $W in /*) ;; *) W=$(pwd)/$W ;; esac |
| 41 | LOGS=$W/logs; mkdir -p "$LOGS" |
| 42 | echo "mutate.sh: omc=$OMC sha256=$(sha256sum < "$OMC" | cut -c1-64) logs=$LOGS" |
| 43 | |
| 44 | # stage DIR: a private copy of the sources the build and both tests read. |
| 45 | stage() { |
| 46 | rm -rf "$1"; mkdir -p "$1/tools" "$1/seed/trace" |
| 47 | cp -r tools/trit tools/codebook "$1/tools/"; cp seed/trace/sha256.om "$1/seed/trace/" |
| 48 | rm -rf "$1/tools/trit/trit.bin" "$1/tools/codebook/codebook.bin" |
| 49 | } |
| 50 | # build DIR TAG: both binaries with $OMC; logs kept. |
| 51 | build() { (cd "$1" && sh tools/trit/build.sh && sh tools/codebook/build.sh) > "$LOGS/$2.build.log" 2>&1; rc=$?; printf '%s\n' "$rc" > "$LOGS/$2.build.rc"; return "$rc"; } |
| 52 | # run_test DIR TAG: prints killed|survived|harness-error. |
| 53 | run_test() { |
| 54 | (cd "$1" && FIX="$W/fix-$2" timeout 300 sh tools/trit/test.sh) > "$LOGS/$2.test.log" 2>&1; rc=$? |
| 55 | printf '%s\n' "$rc" > "$LOGS/$2.test.rc" |
| 56 | tot=$(grep '^TOTAL pass=[0-9]* fail=[0-9]* skip=[0-9]*$' "$LOGS/$2.test.log" | tail -1) |
| 57 | f=${tot#*fail=}; f=${f%% *} |
| 58 | case $tot in |
| 59 | '') echo harness-error ;; |
| 60 | *) if [ "$rc" = 0 ] && [ "$f" = 0 ]; then echo survived |
| 61 | elif [ "$rc" = 1 ] && [ "$f" -ge 1 ] 2>/dev/null && grep -q '^FAIL ' "$LOGS/$2.test.log"; then echo killed |
| 62 | else echo harness-error; fi ;; |
| 63 | esac |
| 64 | } |
| 65 | run_diff() { |
| 66 | (cd "$1" && N=200 SEED=3 W="$W/diff-$2" timeout 600 sh tools/trit/proof/difftest.sh) > "$LOGS/$2.diff.log" 2>&1; rc=$? |
| 67 | printf '%s\n' "$rc" > "$LOGS/$2.diff.rc" |
| 68 | if [ "$rc" = 0 ] && grep -q '^DIFF agree=' "$LOGS/$2.diff.log"; then echo survived |
| 69 | elif [ "$rc" = 1 ] && grep -q '^DIFF disagree=' "$LOGS/$2.diff.log"; then echo killed |
| 70 | else echo harness-error; fi |
| 71 | } |
| 72 | |
| 73 | # Baseline: the unmutated tree must build and pass both, or a "kill" proves nothing. |
| 74 | stage "$W/base" |
| 75 | if ! build "$W/base" base; then echo "BASELINE build failed (see $LOGS/base.build.log)"; exit 5; fi |
| 76 | bt=$(run_test "$W/base" base); bd=$(run_diff "$W/base" base) |
| 77 | echo "BASELINE test.sh $bt, difftest $bd" |
| 78 | if [ "$bt" != survived ] || [ "$bd" != survived ]; then |
| 79 | echo "BASELINE did not pass (test.sh $bt, difftest $bd); see $LOGS/base.*.log"; exit 5 |
| 80 | fi |
| 81 | |
| 82 | k1=0; k2=0; n=0; surv=0; inval=0; herr=0 |
| 83 | # m FILE NAME EXPR SCOPE: SCOPE "read" = difftest must kill it; "ack" = outside the differential model. |
| 84 | m() { |
| 85 | n=$((n+1)); f=$1; name=$2; expr=$3; scope=$4 |
| 86 | md="$W/m$n" |
| 87 | stage "$md" |
| 88 | perl -0pi -e "$expr" "$md/tools/$f" |
| 89 | if cmp -s "$md/tools/$f" "tools/$f"; then echo "MUTANT $n $name: NOT APPLIED"; inval=$((inval+1)); return; fi |
| 90 | if ! build "$md" "m$n"; then echo "MUTANT $n $name: BUILD INVALID (see $LOGS/m$n.build.log)"; inval=$((inval+1)); return; fi |
| 91 | t=$(run_test "$md" "m$n"); d=$(run_diff "$md" "m$n") |
| 92 | verdict=ok |
| 93 | case $t in |
| 94 | killed) k1=$((k1+1)) ;; |
| 95 | survived) verdict=SURVIVED; surv=$((surv+1)) ;; |
| 96 | *) verdict="HARNESS ERROR (test.sh)"; herr=$((herr+1)) ;; |
| 97 | esac |
| 98 | case $d in |
| 99 | killed) k2=$((k2+1)) ;; |
| 100 | survived) if [ "$scope" = read ] && [ "$verdict" = ok ]; then verdict="SURVIVED (difftest)"; surv=$((surv+1)); fi ;; |
| 101 | *) verdict="HARNESS ERROR (difftest)"; herr=$((herr+1)) ;; |
| 102 | esac |
| 103 | echo "MUTANT $n $name [$scope]: test.sh $t, difftest $d => $verdict" |
| 104 | } |
| 105 | m trit/trit.om lvl-ignores-3xx 's/if and eq get vf 0 1 eq get ve 0 1/if eq get vf 0 1/' read |
| 106 | m trit/trit.om verify-disabled 's/put vk 0 1\n/put vk 0 0\n/' read |
| 107 | m trit/trit.om quota-counts-forged 's/if eq get vm 0 0\nret true\nend\n//' read |
| 108 | m trit/trit.om quota-counts-replays 's/if seenc s n v\nret true\nend\n//' read |
| 109 | m trit/trit.om unsigned-emergency-jumps 's/if eq open te 2\nmktok 1/if gt open te 0\nmktok 1/' read |
| 110 | m trit/trit.om ack-no-counter-advance 's/ackv cur off\n//' ack |
| 111 | m trit/trit.om ack-advances-past-token 's/while lt p off bound BIG/while lt p get fbn 0 bound BIG/' ack |
| 112 | m trit/trit.om ack-no-lock 's/lk = lockst\n/lk = int 99\n/' ack |
| 113 | m trit/trit.om sig-not-stripped 's/ee = lp\nnf = sub nf 1\n//' read |
| 114 | m trit/trit.om cursor-no-dir-fsync 's/ptext tcur\nsyncdir\n/ptext tcur\n/' ack |
| 115 | m codebook/codebook.om no-batch-table 's/vtput s sn ctr\n//' read |
| 116 | m codebook/codebook.om counter-file-ignored 's/v = rdctf s n\n/v = ulong 0\n/' read |
| 117 | m codebook/codebook.om stale-allows-equal 's/if le ctr rdctr s sn/if lt ctr rdctr s sn/' read |
| 118 | m codebook/codebook.om counter-no-dir-fsync 's/ctrpath s n\nsyncdir\n//' ack |
| 119 | m codebook/codebook.om mac-not-checked 's/ret eq x 0\n/ret true\n/' read |
| 120 | m codebook/codebook.om 1xx-is-emergency 's/if and ge g 300 lt g 400\nret true/if and ge g 100 lt g 400\nret true/' read |
| 121 | echo "MUTANTS $n killed-by-test.sh=$k1 killed-by-difftest=$k2 survived=$surv invalid=$inval harness-errors=$herr" |
| 122 | if [ "$herr" -gt 0 ]; then echo "mutate.sh: FAIL harness error (not a kill); logs $LOGS"; exit 4; fi |
| 123 | if [ "$inval" -gt 0 ]; then echo "mutate.sh: FAIL mutant not applied or invalid; logs $LOGS"; exit 3; fi |
| 124 | if [ "$surv" -gt 0 ]; then echo "mutate.sh: FAIL survivor; logs $LOGS"; exit 1; fi |
| 125 | if [ "$n" -eq 0 ] || [ "$k1" -ne "$n" ]; then echo "mutate.sh: FAIL not every mutant killed by test.sh ($k1 of $n)"; exit 1; fi |
| 126 | echo "mutate.sh: PASS $k1/$n killed by test.sh; difftest killed $k2 (the ack mutants are outside its read-mode model)" |
| 127 | exit 0 |