interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / tools / hwgates / gate.sh

207 lines · 13665 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# tools/hwgates/gate.sh: run the hardware-gate register check (hwgates.om) on its fixtures, kill
4# its compiling behavior mutants, then check docs/hardware-gates.tsv, the receipts it cites, and
5# planted-bad copies of it.
6# AIEN_OMC=<the omc named in PIN> sh tools/hwgates/gate.sh
7# Prints HWGATES-GATE PASS (exit 0) or HWGATES-GATE FAIL (exit 1). Without the pinned compiler it
8# prints HWGATES-GATE NOT_RUN and exits 3. A NOT_RUN is never a pass.
9# Invariants this gate holds:
10# - a PASS or FAIL row names a tracked, regular receipt file under evidence/ holding its
11# HWGATE <id> <tier> <status> line (no receipt, no claim);
12# - every gate id has exactly one PHYSICAL row, and QEMU is never PHYSICAL;
13# - a gate whose receipt still names it cannot be deleted from the register;
14# - the register's gate ids equal those of docs/hardware-gates-needs.tsv, so a gate no receipt
15# names yet cannot leave the inventory silently either;
16# - every HWGATE line in a tracked evidence file agrees with the register row for its id and
17# tier, unless a tracked CORRECTION-*.txt beside it names that line (receipts are never rewritten);
18# - a PASS or FAIL row's source REPO@COMMIT is recorded by files in its receipt's folder, and
19# names a commit of this repository exactly when REPO is this repository.
20set -eu
21ROOT=$(cd "$(dirname "$0")/../.." && pwd)
22cd "$ROOT"
23PIN=PIN
24want=$(sed -n 's/^omc-sha256 //p' "$PIN")
25commit=$(sed -n 's/^omega-mv //p' "$PIN")
26if [ -z "${AIEN_OMC:-}" ] || [ ! -r "$AIEN_OMC" ]; then
27 echo "HWGATES-GATE NOT_RUN: set AIEN_OMC to omc sha256 $want (omega-mv $commit, $PIN)"; exit 3
28fi
29have=$(sha256sum "$AIEN_OMC"); have=${have%% *}
30if [ "$have" != "$want" ]; then
31 echo "HWGATES-GATE NOT_RUN: compiler sha256 $have is not the pin $want ($PIN)"; exit 3
32fi
33W=$(mktemp -d "${TMPDIR:-/tmp}/hwgates.XXXXXX")
34trap 'rm -rf "$W"' EXIT
35SRC=tools/hwgates/hwgates.om
36FX=tools/hwgates/fixtures
37REG=docs/hardware-gates.tsv
38build() { "$AIEN_OMC" < "$1" > "$2" && chmod +x "$2"; }
39# fixtures BIN QUIET: prints one line per fixture unless QUIET; returns the number of wrong verdicts
40fixtures() {
41 bad=0
42 if "$1" "$FX/good.tsv" "$ROOT/$FX" > "$W/out" 2>&1; then
43 [ -n "$2" ] || echo "PASS fixture good.tsv: $(cat "$W/out")"
44 else
45 [ -n "$2" ] || echo "FAIL fixture good.tsv: $(tr '\n' ' ' < "$W/out")"; bad=$((bad + 1))
46 fi
47 for f in "$FX"/bad-R*.tsv; do
48 b=$(basename "$f"); rule=$(echo "$b" | sed 's/^bad-\(R[0-9]*\)-.*/\1/')
49 frc=0; "$1" "$f" "$ROOT/$FX" > "$W/out" 2>&1 || frc=$?
50 if [ "$frc" = 1 ] && grep -q "rule=$rule\$" "$W/out"; then
51 [ -n "$2" ] || echo "PASS fixture $b refused ($rule)"
52 else
53 [ -n "$2" ] || echo "FAIL fixture $b: rc=$frc, expected exit 1 with rule=$rule: $(tr '\n' ' ' < "$W/out")"
54 bad=$((bad + 1))
55 fi
56 done
57 return "$bad"
58}
59# planted NAME RULE FILE: the checker must refuse FILE with exit 1 and rule=RULE
60planted() {
61 rc=0; "$W/hwgates" "$3" "$ROOT" > "$W/out" 2>&1 || rc=$?
62 if ! cmp -s "$REG" "$3" && [ "$rc" = 1 ] && grep -q "rule=$2\$" "$W/out"; then
63 echo "PASS planted $1 refused ($2)"; n=$((n + 1))
64 else echo "FAIL planted $1: rc=$rc, expected exit 1 with rule=$2: $(tr '\n' ' ' < "$W/out")"; fail=1; fi
65}
66fail=0; n=0
67build "$SRC" "$W/hwgates" || { echo "HWGATES-GATE FAIL: compile"; exit 1; }
68rc=0; fixtures "$W/hwgates" "" || rc=$?
69nfx=$(ls "$FX"/good.tsv "$FX"/bad-R*.tsv | wc -l)
70if [ "$rc" = 0 ]; then n=$((n + nfx)); else fail=1; fi
71# compiling behavior mutants of the checker; each must change at least one fixture verdict
72mutant() { # NAME AWK-PROGRAM
73 awk "$2" "$SRC" > "$W/m-$1.om"
74 if cmp -s "$SRC" "$W/m-$1.om"; then echo "FAIL mutant $1 did not apply"; fail=1; return; fi
75 build "$W/m-$1.om" "$W/m-$1" || { echo "FAIL mutant $1 does not compile"; fail=1; return; }
76 k=0; fixtures "$W/m-$1" quiet || k=$?
77 if [ "$k" -gt 0 ]; then echo "PASS mutant $1 killed ($k fixture verdicts changed)"; n=$((n + 1))
78 else echo "FAIL mutant $1 survived"; fail=1; fi
79}
80mutant claim-without-receipt 'prev=="if dash" && $0=="fail ln 3" {prev=$0; next} {prev=$0; print}'
81mutant physical-row-optional '$0=="if ne np 1" {print "if gt np 1"; next} {print}'
82mutant any-line-of-length '$0=="ok = eqneed ls n" {print "ok = true"; next} {print}'
83mutant empty-register-accepted '$0=="fail 0 8" {next} {print}'
84mutant receipt-ids-unchecked '$0=="receiptids" {next} {print}'
85# the register itself
86rc=0; "$W/hwgates" "$REG" "$ROOT" > "$W/reg" 2>&1 || rc=$?
87cat "$W/reg"
88if [ "$rc" = 0 ]; then echo "PASS register $REG"; n=$((n + 1)); else echo "FAIL register $REG rc=$rc"; fail=1; fi
89# the receipts it cites: tracked regular files that resolve inside evidence/
90bad=0
91for r in $(awk -F '\t' '!/^#/ && NF == 7 && $6 != "-" {print $6}' "$REG" | sort -u); do
92 rp=$(realpath -e "$r" 2>/dev/null || true)
93 if [ -L "$r" ] || [ ! -f "$r" ]; then echo "FAIL receipt $r is not a regular file"; bad=1
94 elif [ "${rp#"$ROOT/evidence/"}" = "$rp" ]; then echo "FAIL receipt $r resolves outside evidence/"; bad=1
95 elif ! git ls-files --error-unmatch -- "$r" > /dev/null 2>&1; then echo "FAIL receipt $r is not tracked by git"; bad=1
96 fi
97done
98if [ "$bad" = 0 ]; then echo "PASS receipts are tracked regular files under evidence/"; n=$((n + 1)); else fail=1; fi
99# inventory, receipt claims and source commits (regsh REGISTER NEEDS): one FAIL line per problem,
100# exit 1 if any. Claims are read from every tracked file under evidence/ with HWGATE lines, cited
101# or not; a CORRECTION-*.txt counts only when it is a tracked regular file beside the receipt.
102NEEDS=docs/hardware-gates-needs.tsv
103# this repository's name as the register's source column spells it
104SELF=aienux
105regsh() {
106 rb=0
107 awk -F '\t' '!/^#/ && NF == 7 {print $1}' "$1" | sort -u > "$W/ids-reg"
108 awk -F '\t' '!/^#/ && NF == 2 {print $1}' "$2" | sort -u > "$W/ids-need"
109 for g in $(comm -23 "$W/ids-need" "$W/ids-reg"); do echo "FAIL inventory: gate $g is in $NEEDS but has no rows in the register"; rb=1; done
110 for g in $(comm -13 "$W/ids-need" "$W/ids-reg"); do echo "FAIL inventory: gate $g has rows but no entry in $NEEDS"; rb=1; done
111 for r in $( (git grep -l '^HWGATE ' -- evidence/; awk -F '\t' '!/^#/ && NF == 7 && $6 != "-" {print $6}' "$1") | sort -u); do
112 [ -f "$r" ] || continue
113 grep '^HWGATE ' "$r" > "$W/claims" || true
114 : > "$W/corr"
115 for c in "$(dirname "$r")"/CORRECTION-*.txt; do
116 if [ -f "$c" ] && [ ! -L "$c" ] && git ls-files --error-unmatch -- "$c" > /dev/null 2>&1; then cat "$c" >> "$W/corr"; fi
117 done
118 while read -r _ id tier st; do
119 have=$(awk -F '\t' -v i="$id" -v t="$tier" '!/^#/ && NF == 7 && $1 == i && $4 == t {print $5}' "$1")
120 [ "$have" = "$st" ] && continue
121 if grep -qxF "CORRECTS HWGATE $id $tier $st" "$W/corr"; then continue; fi
122 echo "FAIL claim: $r says HWGATE $id $tier $st, the register says ${have:-no row}, and no tracked CORRECTION-*.txt beside it names the line"; rb=1
123 done < "$W/claims"
124 done
125 # a PASS or FAIL row names REPO@COMMIT (40 hex); a *source-commit.txt in its receipt's folder or
126 # below holds COMMIT as a whole line; COMMIT is a commit of this repository exactly when REPO is
127 # $SELF; for any other REPO a *source-repo.txt there holds REPO@COMMIT as a whole line
128 awk -F '\t' '!/^#/ && NF == 7 && ($5 == "PASS" || $5 == "FAIL") {print $1 " " $3 " " $6}' "$1" > "$W/srcs"
129 while read -r id src r; do
130 repo=${src%%@*}; c=${src#*@}
131 if ! printf '%s\n' "$src" | grep -qxE '[A-Za-z0-9._-]+@[0-9a-f]{40}'; then echo "FAIL source: $id names '$src', not REPO@<40 hex commit>"; rb=1; continue; fi
132 [ -f "$r" ] || continue
133 if ! find "$(dirname "$r")" -name '*source-commit.txt' -type f -exec cat {} + | grep -xF -- "$c" > /dev/null; then
134 echo "FAIL source: $id names $src, which no *source-commit.txt in the folder of $r records"; rb=1; continue; fi
135 own=no; [ "$(git cat-file -t "$c" 2> /dev/null)" = commit ] && own=yes
136 if [ "$repo" = "$SELF" ] && [ "$own" = no ]; then echo "FAIL source: $id names $src, which is not a commit of this repository"; rb=1; fi
137 if [ "$repo" != "$SELF" ] && [ "$own" = yes ]; then echo "FAIL source: $id names $src, a commit of $SELF, not of $repo"; rb=1; continue; fi
138 if [ "$repo" != "$SELF" ] && ! find "$(dirname "$r")" -name '*source-repo.txt' -type f -exec cat {} + | grep -xF -- "$src" > /dev/null; then
139 echo "FAIL source: $id names $src, which no *source-repo.txt in the folder of $r records"; rb=1; fi
140 done < "$W/srcs"
141 return "$rb"
142}
143rc=0; regsh "$REG" "$NEEDS" > "$W/rs" || rc=$?
144cat "$W/rs"
145if [ "$rc" = 0 ]; then echo "PASS inventory, receipt claims and source commits agree with $REG"; n=$((n + 1)); else fail=1; fi
146# plantsh NAME KIND REGISTER [NEEDS]: regsh must refuse the copy with a FAIL KIND line
147plantsh() {
148 pn=${4:-$NEEDS}
149 rc=0; regsh "$3" "$pn" > "$W/out" || rc=$?
150 if { ! cmp -s "$REG" "$3" || ! cmp -s "$NEEDS" "$pn"; } && [ "$rc" = 1 ] && grep -q "^FAIL $2:" "$W/out"; then
151 echo "PASS planted $1 refused ($2)"; n=$((n + 1))
152 else echo "FAIL planted $1: rc=$rc, expected a FAIL $2 line: $(tr '\n' ' ' < "$W/out")"; fail=1; fi
153}
154# planted control 6: a gate listed in the inventory with no rows (a deleted gate that no receipt
155# names, which R9 cannot see), and a gate with rows but no inventory entry -> inventory.
156# ZZ-PLANTED is synthetic, so the control holds whatever receipts the real gates have.
157{ cat "$NEEDS"; printf 'ZZ-PLANTED\tplanted control\n'; } > "$W/p6n.tsv"
158plantsh "gate ZZ-PLANTED listed without rows" inventory "$REG" "$W/p6n.tsv"
159{ cat "$REG"; printf 'ZZ-PLANTED\tplanted\tplanted\tPHYSICAL\tNOT_RUN\t-\tplanted control\n'; } > "$W/p6r.tsv"
160plantsh "gate ZZ-PLANTED with rows but no inventory entry" inventory "$W/p6r.tsv"
161# planted control 7: the first PASS row downgraded to NOT_RUN without a correction -> claim
162awk -F '\t' 'BEGIN{OFS="\t"} !done && !/^#/ && $5=="PASS" {$5="NOT_RUN"; $6="-"; done=1} {print}' "$REG" > "$W/p7.tsv"
163plantsh "PASS row silently downgraded" claim "$W/p7.tsv"
164# planted control 8: the first PASS row names a source commit its receipt does not record, an
165# empty commit, a commit of this repository under another repository's name, or another
166# repository's recorded commit under a misspelt name -> source
167awk -F '\t' 'BEGIN{OFS="\t"} !done && !/^#/ && $5=="PASS" {sub(/@.*/, "@0000000000000000000000000000000000000000", $3); done=1} {print}' "$REG" > "$W/p8.tsv"
168plantsh "PASS row with an unrecorded source commit" source "$W/p8.tsv"
169awk -F '\t' 'BEGIN{OFS="\t"} !done && !/^#/ && $5=="PASS" {sub(/@.*/, "@", $3); done=1} {print}' "$REG" > "$W/p8e.tsv"
170plantsh "PASS row with an empty source commit" source "$W/p8e.tsv"
171selfc=$(awk -F '\t' -v s="$SELF" '!/^#/ && NF == 7 && $5 == "PASS" && index($3, s "@") == 1 {print substr($3, length(s) + 2); exit}' "$REG")
172awk -F '\t' -v s="$SELF" -v c="$selfc" 'BEGIN{OFS="\t"} !done && !/^#/ && $5=="PASS" && index($3, s "@") != 1 {sub(/@.*/, "@" c, $3); done=1} {print}' "$REG" > "$W/p8x.tsv"
173plantsh "another repository's PASS row naming $SELF@$selfc" source "$W/p8x.tsv"
174awk -F '\t' -v s="$SELF" 'BEGIN{OFS="\t"} !done && !/^#/ && $5=="PASS" && index($3, s "@") != 1 {sub(/@/, "x@", $3); done=1} {print}' "$REG" > "$W/p8r.tsv"
175plantsh "another repository's PASS row under a misspelt repository name" source "$W/p8r.tsv"
176# alarm: the checker refuses registers and receipts larger than its tables (CAP bytes, MAXR rows,
177# read from hwgates.om); fail loudly at three quarters of either so the tables grow first
178cap=$(sed -n 's/^const CAP ulong \([0-9]*\)$/\1/p' "$SRC")
179maxr=$(sed -n 's/^const MAXR ulong \([0-9]*\)$/\1/p' "$SRC")
180rows=$(awk -F '\t' '!/^#/ && NF > 0' "$REG" | wc -l)
181big=0
182if [ $((rows * 4)) -gt $((maxr * 3)) ]; then echo "ALARM register has $rows rows; the checker holds MAXR=$maxr ($SRC)"; big=1; fi
183for f in "$REG" $(awk -F '\t' '!/^#/ && NF == 7 && $6 != "-" {print $6}' "$REG" | sort -u); do
184 s=$(wc -c < "$f")
185 if [ $((s * 4)) -gt $((cap * 3)) ]; then echo "ALARM $f is $s bytes; the checker reads CAP=$cap ($SRC)"; big=1; fi
186done
187if [ "$big" = 0 ]; then echo "PASS sizes: $rows rows of MAXR=$maxr, every file under 3/4 of CAP=$cap"; n=$((n + 1)); else fail=1; fi
188# planted control 1: the first PHYSICAL row, whatever its status, claims PASS without a receipt -> R3
189awk -F '\t' 'BEGIN{OFS="\t"} !done && !/^#/ && $4=="PHYSICAL" {$5="PASS"; $6="-"; done=1} {print}' "$REG" > "$W/p1.tsv"
190planted "physical PASS without receipt" R3 "$W/p1.tsv"
191# planted control 2: every PHYSICAL row removed -> R5
192awk -F '\t' '$4!="PHYSICAL"' "$REG" > "$W/p2.tsv"
193planted "register without physical rows" R5 "$W/p2.tsv"
194# planted control 3: every row of the first gate with a receipt removed -> R9
195gid=$(awk -F '\t' '!/^#/ && NF == 7 && $6 != "-" {print $1; exit}' "$REG")
196awk -F '\t' -v g="$gid" '$1 != g' "$REG" > "$W/p3.tsv"
197planted "gate $gid deleted while its receipt names it" R9 "$W/p3.tsv"
198# planted control 4: comments only -> R8
199grep '^#' "$REG" > "$W/p4.tsv" || true
200planted "register without rows" R8 "$W/p4.tsv"
201# planted control 5: one row more than MAXR -> exit 2 naming the row table, never a rule verdict
202awk -v m="$maxr" 'BEGIN{for (i = 0; i <= m; i++) printf "ZZ-%d\tc\ts\tPHYSICAL\tNOT_RUN\t-\to\n", i}' > "$W/p5.tsv"
203rc=0; "$W/hwgates" "$W/p5.tsv" "$ROOT" > "$W/out" 2>&1 || rc=$?
204if [ "$rc" = 2 ] && grep -q 'MAXR' "$W/out"; then echo "PASS planted $((maxr + 1)) rows refused (exit 2, MAXR)"; n=$((n + 1))
205else echo "FAIL planted $((maxr + 1)) rows: rc=$rc: $(tr '\n' ' ' < "$W/out")"; fail=1; fi
206if [ "$fail" = 0 ]; then echo "HWGATES-GATE PASS checks=$n"; exit 0; fi
207echo "HWGATES-GATE FAIL checks_passed=$n"; exit 1