interplane.Get Omega
Read in your language

aien-protocols-mv

Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem

Brought by drakestapleton

branch main Branches Tags History

aien-protocols-mv / specs / execution-transcript / tools / check-corpus.sh

65 lines · 2876 bytes Raw Blame History
1#!/bin/bash
2# check-corpus.sh: build the C reference verifier and check the TRN1 corpus.
3#
4# Usage: check-corpus.sh [CC] (default CC: cc)
5#
6# 1. Regenerates the corpus into a temporary directory with make-corpus.sh
7# and requires it to be byte-identical to vectors/, .gitattributes included
8# (a stale or hand-edited corpus fails here).
9# 2. Builds trn1_verify.c with warnings as errors, plus an ASan/UBSan build
10# when the compiler supports it.
11# 3. Runs every expected.txt and compare.txt line; prints
12# TRN1_CONFORMANCE impl=c pass=N fail=M and exits non-zero on any miss.
13# Host-only check: no hardware, no QEMU. Shell and C only.
14set -eu
15export LC_ALL=C
16here=$(cd "$(dirname "$0")" && pwd)
17vec="$here/../vectors"
18CC=${1:-cc}
19tmp=$(mktemp -d)
20trap 'rm -rf "$tmp"' EXIT
21
22bash "$here/make-corpus.sh" "$tmp/regen" >/dev/null
23# The comparison is the whole directory, byte for byte, including
24# vectors/.gitattributes (make-corpus.sh emits it). Before trusting it, prove
25# it catches drift on a copy of the regenerated corpus (counterexamples):
26# a) one bit flipped in one vector must differ;
27# b) a vector missing must differ;
28# c) the line-ending pin (.gitattributes) removed must differ.
29first=$(ls "$tmp/regen"/*.trn | head -n 1)
30test -s "$first" || { echo "TRN1_CORPUS_SELFTEST: no non-empty vector to alter" >&2; exit 2; }
31for cx in flip drop attr; do
32 rm -rf "$tmp/cx" && cp -r "$tmp/regen" "$tmp/cx"
33 f="$tmp/cx/$(basename "$first")"
34 case $cx in
35 flip) b=$(od -An -N1 -tu1 "$f" | tr -d ' ')
36 printf "\\$(printf '%03o' $((b ^ 1)))" | dd of="$f" bs=1 seek=0 count=1 conv=notrunc 2>/dev/null ;;
37 drop) rm -f "$f" ;;
38 attr) rm -f "$tmp/cx/.gitattributes" ;;
39 esac
40 if diff -r "$tmp/cx" "$tmp/regen" >/dev/null; then
41 echo "TRN1_CORPUS_SELFTEST: counterexample '$cx' not detected" >&2
42 exit 2
43 fi
44done
45echo "TRN1_CORPUS_SELFTEST counterexamples=3 detected=3"
46# Only the crumb tool's two exact file names are ignored (RFC-0001 drops
47# .crumb / .crumb.local into directories); any other stray file still differs.
48ignore="-x .crumb -x .crumb.local"
49if ! diff -r $ignore "$vec" "$tmp/regen" >/dev/null; then
50 echo "TRN1_CORPUS_STALE: vectors/ differs from make-corpus.sh output" >&2
51 diff -rq $ignore "$vec" "$tmp/regen" >&2 || true
52 exit 1
53fi
54echo "TRN1_CORPUS_REGEN identical"
55
56"$CC" -std=c99 -O2 -Wall -Wextra -Werror -o "$tmp/trn1_verify" "$here/trn1_verify.c"
57"$tmp/trn1_verify" --corpus "$vec"
58
59if "$CC" -std=c99 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all \
60 -o "$tmp/trn1_verify_san" "$here/trn1_verify.c" 2>/dev/null; then
61 "$tmp/trn1_verify_san" --corpus "$vec" | sed 's/^TRN1_CONFORMANCE impl=c/TRN1_CONFORMANCE impl=c-asan-ubsan/'
62 test "${PIPESTATUS[0]}" -eq 0
63else
64 echo "TRN1_SANITIZER NOT_RUN (compiler lacks -fsanitize=address,undefined)"
65fi