aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / specs / verified-crumb / tools / check-golden.sh
| 1 | #!/bin/bash |
| 2 | # check-golden.sh [CC]: prove the VC1 golden corpus and the C checker agree. |
| 3 | # |
| 4 | # 1. golden/ is byte-identical to make-golden.sh output (ignoring the crumb |
| 5 | # tool's .crumb and .crumb.local files). |
| 6 | # 2. vc1-check builds with warnings as errors (and under ASan/UBSan if the |
| 7 | # compiler has it) and passes every line of golden/expected.txt. |
| 8 | # 3. Every ACCEPT id is recomputed with sha256sum over the raw bytes, a hash |
| 9 | # implementation independent of the C tool. |
| 10 | # 4. Vector mutants (flip a byte, reorder deps, drop the domain tag) must be |
| 11 | # detected: the checker's answer must differ from the golden expectation. |
| 12 | # 5. Checker mutants (-DVC1_MUTANT=1..9) must each make the corpus FAIL. |
| 13 | # Host-only. Shell, xxd, sha256sum and a C99 compiler. No Python. |
| 14 | set -eu |
| 15 | export LC_ALL=C |
| 16 | here=$(cd "$(dirname "$0")" && pwd) |
| 17 | gold="$here/../golden" |
| 18 | CC=${1:-cc} |
| 19 | tmp=$(mktemp -d) |
| 20 | trap 'rm -rf "$tmp"' EXIT |
| 21 | |
| 22 | bash "$here/make-golden.sh" "$tmp/regen" |
| 23 | if ! diff -r -x .crumb -x .crumb.local "$gold" "$tmp/regen" >/dev/null; then |
| 24 | echo "VC1_GOLDEN_STALE: golden/ differs from make-golden.sh output" >&2 |
| 25 | diff -rq -x .crumb -x .crumb.local "$gold" "$tmp/regen" >&2 || true |
| 26 | exit 1 |
| 27 | fi |
| 28 | echo "VC1_GOLDEN_REGEN identical" |
| 29 | |
| 30 | "$CC" -std=c99 -O2 -Wall -Wextra -Werror -o "$tmp/vc1-check" "$here/vc1-check.c" |
| 31 | "$tmp/vc1-check" --corpus "$gold" |
| 32 | if "$CC" -std=c99 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all \ |
| 33 | -o "$tmp/vc1-check-san" "$here/vc1-check.c" 2>/dev/null; then |
| 34 | "$tmp/vc1-check-san" --corpus "$gold" | sed 's/impl=c /impl=c-asan-ubsan /' |
| 35 | else |
| 36 | echo "VC1_SANITIZER NOT_RUN (compiler lacks -fsanitize=address,undefined)" |
| 37 | fi |
| 38 | |
| 39 | # 3. independent hash cross-check |
| 40 | n=0 |
| 41 | while read -r name verb arg; do |
| 42 | [ "$verb" = ACCEPT ] || continue |
| 43 | want=$(xxd -r -p "$gold/$name.hex" | sha256sum | cut -d' ' -f1) |
| 44 | got=$("$tmp/vc1-check" "$gold/$name.hex" | cut -d' ' -f2) |
| 45 | if [ "$want" != "$arg" ] || [ "$got" != "$arg" ]; then |
| 46 | echo "VC1_HASH_MISMATCH $name expected=$arg sha256sum=$want tool=$got" >&2; exit 1 |
| 47 | fi |
| 48 | n=$((n + 1)) |
| 49 | done < "$gold/expected.txt" |
| 50 | echo "VC1_INDEPENDENT_SHA256 ids=$n agree" |
| 51 | # same digest bytes, different digest_kind: ids must differ |
| 52 | id1=$(grep "^v01_minimal " "$gold/expected.txt" | cut -d" " -f3); id4=$(grep "^v04_ir_kind " "$gold/expected.txt" | cut -d" " -f3) |
| 53 | [ "$id1" != "$id4" ] || { echo "VC1_KIND_NOT_IN_ID" >&2; exit 1; } |
| 54 | echo "VC1_DIGEST_KIND_IN_ID v01(source) != v04(ir)" |
| 55 | |
| 56 | # 4. vector mutants, applied to v02_deps (hex string surgery, 2 hex chars per byte) |
| 57 | v=$(tr -d '\n' < "$gold/v02_deps.hex") |
| 58 | exp=$(grep '^v02_deps ' "$gold/expected.txt" | cut -d' ' -f2-) |
| 59 | tag=44 # domain tag is 22 bytes = 44 hex chars |
| 60 | sem=$((tag + 8)) # semantic_id starts after tag and 4-byte version |
| 61 | deps=$((tag + 8 + 3 * 64 + 2 + 8 + 2 * 64 + 8)) # first dep entry: tag, version, 3 ids, digest_kind byte, realization count, 2 realizations, dep count |
| 62 | flip=$(printf '%s' "$v" | sed "s/^\(.\{$sem\}\)../\1ee/") |
| 63 | d1=${v:$deps:128}; d2=${v:$((deps + 128)):128} |
| 64 | swap="${v:0:$deps}$d2$d1${v:$((deps + 256))}" |
| 65 | notag=${v:$tag} |
| 66 | mut=0 |
| 67 | for m in flip swap notag; do |
| 68 | case $m in flip) h=$flip;; swap) h=$swap;; notag) h=$notag;; esac |
| 69 | [ "$h" != "$v" ] || { echo "VC1_MUTANT_NOOP $m" >&2; exit 2; } |
| 70 | printf '%s\n' "$h" > "$tmp/m.hex" |
| 71 | set +e; got=$("$tmp/vc1-check" "$tmp/m.hex" | sed 's/ @.*//'); set -e |
| 72 | if [ "$got" = "$exp" ]; then echo "VC1_VECTOR_MUTANT_SURVIVED $m" >&2; exit 1; fi |
| 73 | echo "vector mutant $m -> $got (golden: ACCEPT ${exp#ACCEPT }) detected" |
| 74 | mut=$((mut + 1)) |
| 75 | done |
| 76 | echo "VC1_VECTOR_MUTANTS detected=$mut/3" |
| 77 | |
| 78 | # 5. checker mutants |
| 79 | surv=0 |
| 80 | for k in 1 2 3 4 5 6 7 8 9; do |
| 81 | "$CC" -std=c99 -O2 -DVC1_MUTANT=$k -o "$tmp/mut$k" "$here/vc1-check.c" |
| 82 | if "$tmp/mut$k" --corpus "$gold" >"$tmp/mut$k.out" 2>&1; then |
| 83 | echo "VC1_CHECKER_MUTANT_SURVIVED $k" >&2; surv=$((surv + 1)) |
| 84 | else |
| 85 | echo "checker mutant $k: $(tail -n 1 "$tmp/mut$k.out") (killed)" |
| 86 | fi |
| 87 | done |
| 88 | [ "$surv" -eq 0 ] || exit 1 |
| 89 | echo "VC1_CHECKER_MUTANTS killed=9/9" |
| 90 | echo "VC1_GOLDEN_CHECK PASS" |