aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / specs / pi-spark-task / tools / check-vectors.sh
| 1 | #!/bin/bash |
| 2 | # check-vectors.sh: build the PST1 reference checker and check the vectors. |
| 3 | # |
| 4 | # Usage: check-vectors.sh [CC] (default CC: cc) |
| 5 | # |
| 6 | # 1. Builds tools/pst_verify.c with warnings as errors (C99, libc only). |
| 7 | # 2. Regenerates the vectors into a scratch directory (pst_verify |
| 8 | # --make-vectors) and requires them byte-identical to vectors/, so a stale |
| 9 | # or hand-edited vector fails here. |
| 10 | # 3. Runs every vectors/expected.txt and vectors/sessions/expected.txt line |
| 11 | # (re-encoding every accepted message, which must give the same bytes), |
| 12 | # plus an ASan/UBSan build when the compiler supports it. |
| 13 | # 4. Runs the seeded random session test twice (same seed, same output). |
| 14 | # 5. Runs tools/mutate-verifier.sh: every check must be caught by a vector. |
| 15 | # Host-only check: no hardware. Shell and C only. Found by the PROTO-C-VECTORS |
| 16 | # gate (tests/gates/run.sh c-vectors) because its name matches check-*.sh. |
| 17 | set -eu |
| 18 | export LC_ALL=C |
| 19 | here=$(cd "$(dirname "$0")" && pwd) |
| 20 | vec="$here/../vectors" |
| 21 | CC=${1:-cc} |
| 22 | tmp=$(mktemp -d) |
| 23 | trap 'rm -rf "$tmp"' EXIT |
| 24 | |
| 25 | "$CC" -std=c99 -O2 -Wall -Wextra -Werror -pedantic -o "$tmp/pst_verify" "$here/pst_verify.c" |
| 26 | |
| 27 | mkdir -p "$tmp/regen/sessions" |
| 28 | "$tmp/pst_verify" --make-vectors "$tmp/regen" |
| 29 | ignore="-x .crumb -x .crumb.local -x .gitattributes" |
| 30 | if ! diff -r $ignore "$vec" "$tmp/regen" >/dev/null; then |
| 31 | echo "PST1_VECTORS_STALE: vectors/ differs from pst_verify --make-vectors output" >&2 |
| 32 | diff -rq $ignore "$vec" "$tmp/regen" >&2 || true |
| 33 | exit 1 |
| 34 | fi |
| 35 | echo "PST1_VECTORS_REGEN identical" |
| 36 | |
| 37 | "$tmp/pst_verify" --corpus "$vec" |
| 38 | |
| 39 | if "$CC" -std=c99 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all \ |
| 40 | -o "$tmp/pst_verify_san" "$here/pst_verify.c" 2>/dev/null; then |
| 41 | "$tmp/pst_verify_san" --corpus "$vec" | sed 's/^PST1_CONFORMANCE impl=c/PST1_CONFORMANCE impl=c-asan-ubsan/' |
| 42 | test "${PIPESTATUS[0]}" -eq 0 |
| 43 | else |
| 44 | echo "PST1_SANITIZER NOT_RUN (compiler lacks -fsanitize=address,undefined)" |
| 45 | fi |
| 46 | |
| 47 | |
| 48 | # Seeded random sessions (spec section 10, aien-protocols-mv#32 item 4): |
| 49 | # reorder, drop and duplicate records of every accepted session. Each result |
| 50 | # is accepted or refused with a code; the same seed must give the same counts |
| 51 | # and the same digest of all verdict lines on two runs and under ASan/UBSan. |
| 52 | seed=${PST1_FUZZ_SEED:-20261016} |
| 53 | count=${PST1_FUZZ_COUNT:-20000} |
| 54 | "$tmp/pst_verify" --session-fuzz "$seed" "$count" "$vec"/sessions/ok-*.pss >"$tmp/fuzz1" |
| 55 | "$tmp/pst_verify" --session-fuzz "$seed" "$count" "$vec"/sessions/ok-*.pss >"$tmp/fuzz2" |
| 56 | cat "$tmp/fuzz1" |
| 57 | cmp -s "$tmp/fuzz1" "$tmp/fuzz2" || { echo "PST1_SESSION_FUZZ_NONDETERMINISTIC" >&2; exit 1; } |
| 58 | if [ -x "$tmp/pst_verify_san" ]; then |
| 59 | "$tmp/pst_verify_san" --session-fuzz "$seed" "$count" "$vec"/sessions/ok-*.pss >"$tmp/fuzz3" |
| 60 | cmp -s "$tmp/fuzz1" "$tmp/fuzz3" || { echo "PST1_SESSION_FUZZ_SANITIZER_DIFFERS" >&2; exit 1; } |
| 61 | echo "PST1_SESSION_FUZZ repeat=identical sanitizer=identical" |
| 62 | else |
| 63 | echo "PST1_SESSION_FUZZ repeat=identical sanitizer=NOT_RUN" |
| 64 | fi |
| 65 | |
| 66 | bash "$here/mutate-verifier.sh" "$CC" |
| 67 | echo "PST1_VECTORS_PASS" |