interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / tests / run_forge_gates.sh

327 lines · 17248 bytes Raw Blame History
1#!/bin/bash
2# tests/run_forge_gates.sh -- Gate 3 (FORGE-0) and Gate 4 (FORGE-HWID) runner
3# with a machine-made receipt.
4#
5# tests/run_forge_gates.sh
6# log only, no receipt (the old behaviour)
7# tests/run_forge_gates.sh --omega-dir DIR --omega-candidate SHA
8# --physics-candidate SHA [--evidence-dir DIR --record]
9# also writes build/forge-runs/<run-id>/receipt-preview.json, and with
10# --record the permanent receipt <evidence-dir>/<receipt_digest>.json
11# (created exclusively, mode 0444) plus blobs/<sha256>.log
12#
13# Needs the GB10. The whole build + test section runs under one exclusive
14# lock on /tmp/aien-gb10.lock (fd 9; tests/gb10_lock.sh skips it when a parent such as
15# the judge already holds it); nothing inside takes the lock again.
16#
17# Receipt mode checks: both candidates are explicit full 40-hex SHAs equal to
18# HEAD of a clean tree (this physics tree and --omega-dir), before the run
19# and again at receipt time. A gate is PASS only when its binary exited 0,
20# its "Gate N Results:" line appears once with FAILED=0 and TOTAL=PASSED, and
21# the "[PASS]/[FAIL] ID" lines are exactly the expected ids, each PASS once.
22# Field names follow the omega Gate 1/2 and Gate 5 receipts, so the Gate 14
23# combiner reads one set of names: candidate_git_commit is the OMEGA commit,
24# physics_candidate_git_commit the physics commit.
25#
26# The receipt digest is SHA-256 over the canonical JSON of the receipt body
27# (sorted keys, compact: the form omega tools/json_canon.c and the Gate 14
28# combiner use). forge_body prints the body already in that form, so the
29# digest is plain sha256sum and nothing from omega is compiled or run here;
30# --omega-dir is read only for its git HEAD and clean state. The digest shows
31# the receipt was not altered; it is not a signature. Shell + coreutils +
32# git + gcc + awk. No Python.
33#
34# Exit 0 when both gates pass (and, in receipt mode, the receipt is PASS),
35# 1 on any failure, 2 on bad arguments.
36#
37# The functions can be sourced (tests/test_forge_gates_receipt.sh does);
38# main only runs when the file is executed.
39
40FORGE_ROOT=$(cd -P "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
41FORGE_LOCK=/tmp/aien-gb10.lock
42. "$FORGE_ROOT/tests/gb10_lock.sh"
43FORGE_ERR=
44FORGE_SCHEMA=AIEN_M19R_FORGE_GATES_V1
45FORGE_GATE=M19R_GATES_3_4_FORGE
46FORGE_G3=GATE_3_FORGE_0
47FORGE_G4=GATE_4_FORGE_HWID
48FORGE_G3_IDS="FORGE_REALIZE_API_NAMED AEGIS_VERIFY_API_NAMED FORGE_TYPED_BOUNDARY_ENFORCED
49HISTORICAL_NAMES_PRESERVED NO_EXISTING_FILE_RENAMED NEG_NULL_NVRM_REJECTED
50NEG_UNVERIFIED_REALIZATION_REJECTED NEG_DESCRIPTOR_MISMATCH_REJECTED
51NEG_STALE_REALIZATION_IDENTITY_REJECTED NEG_VERIFICATION_FAILURE_PREVENTS_SUBMISSION
52NEG_POST_VERIFICATION_MUTATION_REFUSED"
53FORGE_G4_IDS="RAW_HARDWARE_PROBE_CAPTURED FORGE_DESCRIPTOR_NORMALIZED DESCRIPTOR_DIGEST_BOUND
54RECEIPT_HARDWARE_DERIVED SERIALIZATION_KAT_VERIFIED NEG_MISSING_REQUIRED_FIELD_REJECTED
55NEG_MALFORMED_PCI_REJECTED NEG_UNSUPPORTED_COMPUTE_CLASS_REJECTED
56NEG_DESCRIPTOR_MUTATION_CAUSES_DIGEST_MISMATCH NEG_UNBOUND_ALIAS_REJECTED"
57
58forge_fail() { FORGE_ERR=$1; return 1; }
59
60# forge_sha_file FILE -- hex sha256 of FILE; fails (prints nothing) when unreadable.
61forge_sha_file() { local s; s=$(sha256sum < "$1") || return 1; printf '%s\n' "${s%% *}"; }
62
63# forge_candidate REPO SHA -- SHA is a full 40-hex id equal to HEAD of a clean tree.
64forge_candidate() {
65 local repo=$1 sha=$2 name head st
66 name=$(basename "$repo")
67 [[ $sha =~ ^[0-9a-fA-F]{40}$ ]] || forge_fail "candidate must be an explicit full 40-hex SHA" || return 1
68 head=$(git -C "$repo" rev-parse HEAD 2>/dev/null) || forge_fail "cannot read $name HEAD" || return 1
69 [ "${head,,}" = "${sha,,}" ] || forge_fail "$name HEAD differs from supplied candidate" || return 1
70 st=$(git -C "$repo" status --porcelain --untracked-files=normal 2>/dev/null) ||
71 forge_fail "cannot read $name status" || return 1
72 [ -z "$st" ] || forge_fail "$name candidate tree is dirty"
73}
74
75# forge_tree_clean REPO -- "true" or "false" from git status right now.
76forge_tree_clean() {
77 local st
78 if st=$(git -C "$1" status --porcelain --untracked-files=normal 2>/dev/null) && [ -z "$st" ]; then
79 echo true
80 else
81 echo false
82 fi
83}
84
85# forge_events LOG SECTION -- "id<TAB>status" for each [PASS]/[FAIL] line of
86# one gate section: from "Running Gate N:" up to "Gate N Results:".
87forge_events() {
88 awk -v n="$2" '
89 index($0, "Running Gate " n ":") { on = 1; next }
90 on && index($0, "Gate " n " Results:") == 1 { on = 0; next }
91 on && match($0, /^\[(PASS|FAIL)\] [A-Z0-9_]+$/) {
92 print substr($0, 8) "\t" substr($0, 2, 4)
93 }' "$1"
94}
95
96# forge_results LOG N -- the "TOTAL PASSED FAILED" numbers of the single
97# "Gate N Results:" line; fails when there is not exactly one.
98forge_results() {
99 local lines
100 lines=$(grep -E "^Gate $2 Results: TOTAL=[0-9]+ PASSED=[0-9]+ FAILED=[0-9]+$" "$1")
101 [ -n "$lines" ] && [ "$(printf '%s\n' "$lines" | wc -l)" -eq 1 ] || return 1
102 printf '%s\n' "$lines" | sed -E 's/.*TOTAL=([0-9]+) PASSED=([0-9]+) FAILED=([0-9]+)$/\1 \2 \3/'
103}
104
105# forge_gate_verdict LOG N EXIT_STATUS EXPECTED_IDS -- PASS or FAIL: <reason>.
106forge_gate_verdict() {
107 local log=$1 n=$2 rc=$3 expected=$4 ev nums t p f want got
108 [ "$rc" = 0 ] || { echo "FAIL: gate $n binary exit status $rc"; return; }
109 nums=$(forge_results "$log" "$n") || { echo "FAIL: gate $n results line missing or repeated"; return; }
110 read -r t p f <<< "$nums"
111 [ "$f" = 0 ] && [ "$t" = "$p" ] || { echo "FAIL: gate $n results TOTAL=$t PASSED=$p FAILED=$f"; return; }
112 ev=$(forge_events "$log" "$n")
113 if printf '%s\n' "$ev" | awk -F'\t' '$2 != "PASS" { f = 1 } END { exit !f }'; then
114 echo "FAIL: gate $n printed a FAIL line"; return
115 fi
116 want=$(printf '%s\n' $expected | sort)
117 got=$(printf '%s\n' "$ev" | cut -f1 | sort)
118 [ "$got" = "$want" ] || { echo "FAIL: gate $n observed ids differ from the expected ids"; return; }
119 [ "$(printf '%s\n' "$ev" | grep -c .)" = "$t" ] || { echo "FAIL: gate $n TOTAL=$t differs from observed lines"; return; }
120 echo PASS
121}
122
123# forge_body LOG G3_RC G4_RC G3_BIN_SHA G4_BIN_SHA OMEGA_DIR OMEGA_SHA PHYSICS_DIR PHYSICS_SHA RUN_ID TS
124# Prints the receipt body (JSON object, no receipt_digest) ALREADY IN
125# CANONICAL FORM: keys sorted bytewise at every level, no whitespace, strings
126# printable ASCII only. So its bytes are exactly what omega's json_canon
127# (Python json.dumps sort_keys, compact separators) would produce, and
128# sha256sum of it is the receipt digest. Keep every printf below in key
129# order. The receipt holds no floats, so no number reformatting arises.
130forge_body() {
131 local log=$1 g3rc=$2 g4rc=$3 g3sha=$4 g4sha=$5 odir=$6 osha=$7 pdir=$8 psha=$9 run_id=${10} ts=${11}
132 local v3 v4 oc pc err= desc alias results np nf nt s3 s4 status
133 v3=$(forge_gate_verdict "$log" 3 "$g3rc" "$FORGE_G3_IDS")
134 v4=$(forge_gate_verdict "$log" 4 "$g4rc" "$FORGE_G4_IDS")
135 desc=$(sed -n -E 's/^\[\*\] Observed Descriptor Digest: ([0-9a-f]{64})$/\1/p' "$log" | tail -n 1)
136 alias=$(sed -n -E 's/^\[\*\] Derived Display Alias: ([A-Za-z0-9_]+)$/\1/p' "$log" | tail -n 1)
137 oc=$(forge_tree_clean "$odir")
138 pc=$(forge_tree_clean "$pdir")
139 s3=${v3%%:*}; s4=${v4%%:*}
140 [ "$s3" = PASS ] || err=${v3#FAIL: }
141 [ "$s4" = PASS ] || err=${err:+$err; }${v4#FAIL: }
142 [ "$s4" != PASS ] || [ -n "$desc" ] || { s4=FAIL; err=${err:+$err; }"gate 4 descriptor digest not printed"; }
143 [[ $g3sha =~ ^[0-9a-f]{64}$ ]] || { s3=FAIL; err=${err:+$err; }"gate 3 binary hash missing"; }
144 [[ $g4sha =~ ^[0-9a-f]{64}$ ]] || { s4=FAIL; err=${err:+$err; }"gate 4 binary hash missing"; }
145 [ "$oc" = true ] && [ "$pc" = true ] || err=${err:+$err; }"candidate trees not clean at receipt time"
146 [ "$(git -C "$odir" rev-parse HEAD 2>/dev/null)" = "${osha,,}" ] || err=${err:+$err; }"omega HEAD moved during the run"
147 [ "$(git -C "$pdir" rev-parse HEAD 2>/dev/null)" = "${psha,,}" ] || err=${err:+$err; }"physics HEAD moved during the run"
148 if [ -z "$err" ]; then status=PASS; else status=FAIL; fi
149 results=$( { forge_events "$log" 3 | sed "s/^/$FORGE_G3\t/"; forge_events "$log" 4 | sed "s/^/$FORGE_G4\t/"; } |
150 awk -F'\t' 'BEGIN { printf "[" } { printf "%s{\"id\":\"%s\",\"status\":\"%s\",\"suite\":\"%s\"}", (NR > 1 ? "," : ""), $2, $3, $1 } END { printf "]" }')
151 np=$( { forge_events "$log" 3; forge_events "$log" 4; } | grep -c $'\tPASS$')
152 nf=$( { forge_events "$log" 3; forge_events "$log" 4; } | grep -c $'\tFAIL$')
153 nt=$((np + nf))
154 printf '{"candidate_git_commit":"%s"' "${osha,,}"
155 printf ',"candidate_trees_clean":{"omega":%s,"physics":%s}' "$oc" "$pc"
156 printf ',"digest_meaning":"integrity only, not authenticity: re-hash blobs/<sha256>.log named here"'
157 [ -z "$err" ] || printf ',"error":"%s"' "$(printf '%s' "$err" | LC_ALL=C tr -cd ' -~' | LC_ALL=C tr -d '"\\')"
158 printf ',"gate":"%s","gate_log_sha256":"%s"' "$FORGE_GATE" "$(forge_sha_file "$log")"
159 printf ',"gates":{"%s":{"candidate_binary_sha256":"%s","gate_binary_exit_status":%d,"status":"%s"}' \
160 "$FORGE_G3" "$g3sha" "$g3rc" "$s3"
161 printf ',"%s":{"candidate_binary_sha256":"%s"' "$FORGE_G4" "$g4sha"
162 [ -z "$alias" ] || printf ',"derived_alias":"%s"' "$alias"
163 printf ',"gate_binary_exit_status":%d' "$g4rc"
164 [ -z "$desc" ] || printf ',"hardware_descriptor_digest":"%s"' "$desc"
165 printf ',"status":"%s"}}' "$s4"
166 [ -z "$desc" ] || printf ',"hardware_descriptor_digest":"%s"' "$desc"
167 printf ',"observed_fail_count":%d,"observed_pass_count":%d,"observed_test_count":%d' "$nf" "$np" "$nt"
168 printf ',"physics_candidate_git_commit":"%s","program":"AIEN_M19R_FOUNDATION"' "${psha,,}"
169 printf ',"run_id":"%s","schema":"%s","status":"%s"' "$run_id" "$FORGE_SCHEMA" "$status"
170 printf ',"test_results":%s,"timestamp_utc":"%s"}' "$results" "$ts"
171}
172
173# forge_receipt BODY -- digest the canonical BODY (sha256 of its bytes). Sets
174# FORGE_DIGEST and FORGE_RECEIPT (the body with receipt_digest inserted in
175# its sorted place, between "program" and "run_id", so the receipt itself
176# stays canonical).
177forge_receipt() {
178 local body=$1
179 case $body in *',"run_id":"'*) ;; *) forge_fail "receipt body has no run_id" || return 1;; esac
180 FORGE_DIGEST=$(printf '%s' "$body" | sha256sum | cut -d' ' -f1)
181 FORGE_RECEIPT="${body%%,\"run_id\":\"*},\"receipt_digest\":\"$FORGE_DIGEST\",\"run_id\":\"${body#*,\"run_id\":\"}"
182}
183
184# forge_status RECEIPT -- the top-level status (the one just before test_results).
185forge_status() {
186 printf '%s' "$1" | grep -o '"status":"[A-Z]*","test_results":' | cut -d'"' -f4
187}
188# forge_under DIR TREE -- DIR is TREE or inside it (after resolving links).
189forge_under() {
190 local d t
191 d=$(realpath -m "$1"); t=$(realpath -m "$2")
192 [ "$d" = "$t" ] || [ "${d#"$t"/}" != "$d" ]
193}
194
195# forge_record RECEIPT LOG EVIDENCE_DIR OMEGA_DIR PHYSICS_DIR -- keep the log as
196# blobs/<sha256>.log (0444) and write <evidence-dir>/<digest>.json exclusively.
197forge_record() {
198 local receipt=$1 log=$2 ev=$3 t sha dst tmp
199 for t in "$4" "$5"; do
200 ! forge_under "$ev" "$t" || forge_fail "evidence dir $ev is inside $t; receipts must live outside the candidate trees" || return 1
201 done
202 mkdir -p "$ev/blobs" || forge_fail "cannot create $ev/blobs" || return 1
203 [ ! -L "$ev/blobs" ] || forge_fail "$ev/blobs is a symlink" || return 1
204 for t in "$4" "$5"; do
205 ! forge_under "$ev/blobs" "$t" || forge_fail "$ev/blobs resolves inside $t" || return 1
206 done
207 sha=$(forge_sha_file "$log") || forge_fail "cannot hash $log" || return 1
208 dst=$ev/blobs/$sha.log
209 if [ -e "$dst" ] || [ -L "$dst" ]; then
210 [ -f "$dst" ] && [ ! -L "$dst" ] || forge_fail "blob $dst is not a regular file" || return 1
211 [ "$(forge_sha_file "$dst")" = "$sha" ] || forge_fail "blob $dst does not hash to its name" || return 1
212 chmod 0444 "$dst" || forge_fail "cannot make blob $dst read-only" || return 1
213 else
214 tmp=$(mktemp "$ev/blobs/.tmp.XXXXXX") || return 1
215 cp "$log" "$tmp" && chmod 0444 "$tmp" && mv -n "$tmp" "$dst" && [ ! -e "$tmp" ] ||
216 { rm -f "$tmp"; forge_fail "cannot keep blob $dst"; return 1; }
217 fi
218 # exclusive and atomic: full content in a temp file, then a hard link,
219 # which fails when the name already exists
220 dst=$ev/$FORGE_DIGEST.json
221 tmp=$(mktemp "$ev/.tmp.XXXXXX") || return 1
222 if printf "%s\n" "$receipt" > "$tmp" && chmod 0444 "$tmp" && sync "$tmp" && ln -T "$tmp" "$dst" 2>/dev/null; then
223 rm -f "$tmp"; sync "$ev"
224 else
225 rm -f "$tmp"; forge_fail "cannot write $dst (exists already or write error)"; return 1
226 fi
227}
228
229# forge_run_section RUN_DIR -- compile and run both gates; exit statuses go to
230# RUN_DIR/gate3.rc and gate4.rc. Called with the GB10 lock held.
231forge_run_section() {
232 local rd=$1 rc
233 local cflags="-O2 -Wall -Wextra -Werror \
234 -I./forge -I./nvrm -I./m16 \
235 -I./third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc \
236 -I./third_party/nvidia-open-580.173.02/kernel-open/common/inc \
237 -I./third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm \
238 -I./third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include"
239 local srcs="forge/forge_descriptor.c forge/forge_realize.c sha256_clean.c nvrm/nvrm.c m16/m16_native.c"
240 echo 127 > "$rd/gate3.rc"; echo 127 > "$rd/gate4.rc"
241
242 echo -e "\n[*] Compiling test_forge_hwid..."
243 # shellcheck disable=SC2086
244 gcc $cflags $srcs tests/test_forge_hwid.c -o tests/test_forge_hwid || return 1
245 echo -e "[*] Compiling test_forge_seam..."
246 # shellcheck disable=SC2086
247 gcc $cflags $srcs tests/test_forge_seam.c -o tests/test_forge_seam || return 1
248
249 echo -e "\n[*] Running Gate 4: FORGE-HWID under GPU lock..."
250 rc=0; ./tests/test_forge_hwid || rc=$?; echo "$rc" > "$rd/gate4.rc"
251 echo -e "\n[*] Running Gate 3: FORGE-0 Seam under GPU lock..."
252 rc=0; ./tests/test_forge_seam || rc=$?; echo "$rc" > "$rd/gate3.rc"
253}
254
255forge_usage() {
256 echo "usage: $0 [--omega-dir DIR --omega-candidate SHA --physics-candidate SHA [--evidence-dir DIR --record]]" >&2
257 exit 2
258}
259
260forge_main() {
261 local odir= osha= psha= ev= record=0 receipt_mode=0 run_id rd log g3 g4 rc body receipt ts status g3sha g4sha
262 while [ $# -gt 0 ]; do
263 case $1 in
264 --omega-dir) [ $# -ge 2 ] || forge_usage; odir=$2; shift 2;;
265 --omega-candidate) [ $# -ge 2 ] || forge_usage; osha=$2; shift 2;;
266 --physics-candidate) [ $# -ge 2 ] || forge_usage; psha=$2; shift 2;;
267 --evidence-dir) [ $# -ge 2 ] || forge_usage; ev=$2; shift 2;;
268 --record) record=1; shift;;
269 *) forge_usage;;
270 esac
271 done
272 if [ -n "$odir$osha$psha$ev" ] || [ "$record" = 1 ]; then
273 [ -n "$odir" ] && [ -n "$osha" ] && [ -n "$psha" ] || forge_usage
274 [ "$record" = 0 ] || [ -n "$ev" ] || forge_usage
275 receipt_mode=1
276 odir=$(realpath -e "$odir") || { echo "no such omega dir" >&2; exit 2; }
277 fi
278 cd "$FORGE_ROOT" || exit 1
279
280 if [ "$receipt_mode" = 1 ]; then
281 forge_candidate "$FORGE_ROOT" "$psha" && forge_candidate "$odir" "$osha" ||
282 { echo "Refused before the run: $FORGE_ERR" >&2; exit 1; }
283 fi
284 run_id=$(date -u +%Y%m%dT%H%M%SZ)-$(head -c 6 /dev/urandom | od -An -tx1 | tr -d ' \n')
285 rd=$FORGE_ROOT/build/forge-runs/$run_id
286 mkdir -p "$rd" || exit 1
287 log=$rd/gate3_4.log
288
289 echo "=== M19R FORGE Gates (Gate 3 & Gate 4) ==="
290 echo "Timestamp: $(date -u +"%Y-%m-%dT%H:%M:%SZ")"
291 echo "Host: $(uname -n) ($(uname -m))"
292 echo "Run: $run_id"
293
294 # fd 9, or nothing when a parent (the judge's gb10 job) already holds it: tests/gb10_lock.sh
295 GB10_LOCK=$FORGE_LOCK gb10_lock_acquire || exit 1
296 ts=$(date -u +%Y-%m-%dT%H:%M:%S.%6NZ)
297 forge_run_section "$rd" 2>&1 | tee "$log"
298 # hash the binaries that ran before another run can rebuild them
299 g3sha=$( [ -f tests/test_forge_seam ] && forge_sha_file tests/test_forge_seam)
300 g4sha=$( [ -f tests/test_forge_hwid ] && forge_sha_file tests/test_forge_hwid)
301 gb10_lock_release
302 g3=$(cat "$rd/gate3.rc"); g4=$(cat "$rd/gate4.rc")
303 echo -e "\n=== M19R FORGE Gates Complete (gate3 exit $g3, gate4 exit $g4) ==="
304 rc=0
305 [ "$g3" = 0 ] && [ "$g4" = 0 ] || rc=1
306 [ "$receipt_mode" = 1 ] || { echo "No receipt written (no candidates given)."; exit "$rc"; }
307
308 body=$(forge_body "$log" "$g3" "$g4" "$g3sha" "$g4sha" \
309 "$odir" "$osha" "$FORGE_ROOT" "$psha" "$run_id" "$ts")
310 forge_receipt "$body" || { echo "Receipt failed: $FORGE_ERR" >&2; exit 1; }
311 receipt=$FORGE_RECEIPT
312 printf "%s\n" "$receipt" > "$rd/receipt-preview.json" || exit 1
313 status=$(forge_status "$receipt")
314 echo "Receipt status $status, digest $FORGE_DIGEST (preview $rd/receipt-preview.json)"
315 if [ "$record" = 1 ]; then
316 forge_record "$receipt" "$log" "$ev" "$odir" "$FORGE_ROOT" || { echo "Record failed: $FORGE_ERR" >&2; exit 1; }
317 echo "Permanent receipt: $ev/$FORGE_DIGEST.json"
318 else
319 echo "No permanent receipt (no --record)."
320 fi
321 [ "$status" = PASS ] || rc=1
322 exit "$rc"
323}
324
325if [ "${BASH_SOURCE[0]}" = "$0" ]; then
326 forge_main "$@"
327fi