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 / scripts / ck_gates.sh

844 lines · 52282 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# ck_gates.sh: one entrypoint for the AIENOS C kernel ("CK", Lane 18) QEMU
18# gates, with a content-addressed receipt.
19#
20# It refuses a dirty tree, runs the C kernel QEMU scripts that exist
21# (scripts/qemu_ck_boot_test.sh, scripts/qemu_ck_store_test.sh,
22# scripts/qemu_ck_net_test.sh, scripts/qemu_ck_artifact_test.sh,
23# scripts/qemu_ck_smp_test.sh, scripts/qemu_ck_store_crash_test.sh,
24# scripts/qemu_ck_disk_layout_test.sh, scripts/qemu_ck_continuity_test.sh,
25# scripts/qemu_ck_recovery_test.sh, scripts/qemu_ck_allen_test.sh,
26# scripts/qemu_ck_keyboard_test.sh, scripts/qemu_ck_fpu_test.sh,
27# scripts/qemu_ck_infer_test.sh, scripts/qemu_ck_screen_test.sh,
28# scripts/qemu_ck_console_test.sh,
29# scripts/qemu_ck_rollback_test.sh, scripts/qemu_ck_osc_unit_test.sh,
30# scripts/qemu_ck_osc_launch_test.sh, scripts/qemu_ck_osc_receipt_test.sh, scripts/qemu_ck_osh_core_test.sh, scripts/qemu_ck_osh_op_test.sh,
31# scripts/qemu_ck_slot_loader_test.sh), and prints
32# one line per gate:
33# AIENOS_CK_<gate>: PASS|FAIL|NOT_RUN [(reason)]
34# for M1 M3 SMMU NVME_SHUTDOWN P2_ARTIFACT M0_ROLLBACK OSC_UNIT OSC_LAUNCH OSC_RECEIPT OSH_CORE OSH_OP M4_NVME M4_STORE M4_STORE_CRASH
35# M4_CONTINUITY M4_RECOVERY M4_ALLEN ARGUS1_REVOKE KEYBOARD NET SMP DISK_LAYOUT FPU INFER SCREEN CONSOLE SLOT_LOADER. Gates with no C
36# implementation print NOT_RUN (MISSING_IMPLEMENTATION: <reason>) and never
37# run anything. A missing child script, a child that reports NOT_RUN, a child
38# that prints no verdict line, or a PASS line contradicted by the child's exit
39# status is never PASS. The parity of each gate with the Rust QEMU gates is
40# in native/kernel/GATES.md.
41#
42# QEMU is an emulator: a PASS here qualifies nothing physical (the receipt
43# says "physical": "NOT_RUN"). The child scripts serialise themselves: every
44# child takes the QEMU gate lock (~/workspace/.qemu-gate-lock, quietlock record
45# format, scripts/lib_gate_hold.sh, aienos#278) and reports NOT_RUN while
46# another gate run holds it or while the machine quiet flag
47# (~/workspace/.spark-quiet) is held, which every child only reads (no agent
48# raises the quiet flag without Drake's approval). This script takes neither.
49#
50# Usage: bash scripts/ck_gates.sh [--out DIR] [--only GATE[,GATE...]]
51# bash scripts/ck_gates.sh --self-test
52# --out DIR where the receipt goes (default: evidence/). The receipt
53# evidence/ck_gates_<sha256 of its content>.json is the only
54# file a run adds to the tree; an existing receipt is never
55# overwritten. Child logs stay in a temp folder outside the tree.
56# --only LIST run only the listed gates (commas or spaces; env AIENOS_CK_ONLY is the
57# same). Only the child scripts those gates need run. Every other gate
58# prints AIENOS_CK_<gate>: NOT_SELECTED (never PASS, never dropped); an
59# unknown name is refused, exit 2. A selected run never reports
60# ALL_GATES_PASS; it ends SELECTED_GATES_PASS_QEMU_ONLY or
61# SELECTED_GATES_NOT_ALL_PASS. No --only: unchanged behavior.
62# --self-test checks the verdict parsing with canned child outputs (PASS,
63# FAIL, NOT_RUN, missing marker, missing script), the receipt
64# naming and the dirty-tree refusal. Runs no QEMU.
65#
66# Exit: 0 no gate FAILed (NOT_RUN allowed), 1 a gate FAILed or the run
67# changed the tree, 2 usage error, dirty tree at start, or receipt clash.
68set -uo pipefail
69
70# ===========================================================================
71# GATE TABLE: gate | source | reason when there is no C implementation
72# source: boot -> verdict line from scripts/qemu_ck_boot_test.sh
73# store -> verdict line from scripts/qemu_ck_store_test.sh
74# net -> verdict line from scripts/qemu_ck_net_test.sh
75# artifact -> verdict line from scripts/qemu_ck_artifact_test.sh
76# smp -> verdict line from scripts/qemu_ck_smp_test.sh
77# crash -> verdict line from scripts/qemu_ck_store_crash_test.sh
78# disk -> verdict line from scripts/qemu_ck_disk_layout_test.sh
79# cont -> verdict line from scripts/qemu_ck_continuity_test.sh
80# recov -> verdict line from scripts/qemu_ck_recovery_test.sh
81# allen -> verdict line from scripts/qemu_ck_allen_test.sh (ALLEN genesis/restore, OS-0018 PROPOSED)
82# kbd -> verdict line from scripts/qemu_ck_keyboard_test.sh (xHCI keyboard, recovery access; keys from QEMU sendkey)
83# fpu -> verdict line from scripts/qemu_ck_fpu_test.sh (NOT_RUN without the Rust target)
84# infer -> verdict line from scripts/qemu_ck_infer_test.sh (NOT_RUN without the model file or the Rust target)
85# screen -> verdict line from scripts/qemu_ck_screen_test.sh (GOP framebuffer console vs a QMP screendump)
86# console -> verdict line from scripts/qemu_ck_console_test.sh (C-only: sustained serial + USB console session in a TEST-ONLY QEMU image; NOT_RUN on gate lock or quiet flag)
87# rollback -> verdict line from scripts/qemu_ck_rollback_test.sh (C image as the one-time BootNext candidate, AAVMF)
88# osc -> verdict line from scripts/qemu_ck_osc_unit_test.sh (OSC unit admission, OSC_UNIT_ARTIFACT v1; QEMU, TEST signer, not physical)
89# osc_launch -> verdict line from scripts/qemu_ck_osc_launch_test.sh (OSC unit task launch, OSC_UNIT_ARTIFACT v1 section 9; QEMU, TEST signer, not physical)
90# osc_receipt -> verdict line from scripts/qemu_ck_osc_receipt_test.sh (launch requires a valid Omega realization receipt, aienos#383; QEMU, TEST signer, not physical)
91# osh_core -> verdict line from scripts/qemu_ck_osh_core_test.sh (OSH shell core as EL0 tasks, step trace equals Linux; QEMU, TEST signer, not physical)
92# osh_op -> verdict line from scripts/qemu_ck_osh_op_test.sh (a real capability-authorized OSH operation, klog; QEMU, TEST signer, not physical)
93# slots -> verdict line from scripts/qemu_ck_slot_loader_test.sh (SLOTLDR.EFI signed A/B slot loader: bounded tries, rollback, refusals; QEMU, TEST signer, not physical)
94# missing -> NOT_RUN (MISSING_IMPLEMENTATION), never run, never PASS
95# ===========================================================================
96CK_GATE_TABLE='
97M1|boot|-
98M3|boot|-
99SMMU|store|-
100NVME_SHUTDOWN|store|-
101P2_ARTIFACT|artifact|-
102M0_ROLLBACK|rollback|-
103OSC_UNIT|osc|-
104OSC_LAUNCH|osc_launch|-
105OSC_RECEIPT|osc_receipt|-
106OSH_CORE|osh_core|-
107OSH_OP|osh_op|-
108M4_NVME|store|-
109M4_STORE|store|-
110M4_STORE_CRASH|crash|-
111M4_CONTINUITY|cont|-
112M4_RECOVERY|recov|-
113M4_ALLEN|allen|-
114ARGUS1_REVOKE|store|-
115KEYBOARD|kbd|-
116NET|net|-
117SMP|smp|-
118DISK_LAYOUT|disk|-
119FPU|fpu|-
120INFER|infer|-
121SCREEN|screen|-
122CONSOLE|console|-
123SLOT_LOADER|slots|-
124'
125CHILDREN=(boot store net artifact smp crash disk cont recov allen kbd fpu infer screen console rollback osc osc_launch osc_receipt osh_core osh_op slots)
126# Gate selection (--only): empty selection means every gate (the default).
127selection_active=0
128declare -A sel_gate=()
129declare -A src_wanted=()
130# ===========================================================================
131
132repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
133# Receipts always boot QEMU -cpu max (FEAT_RNG present): the manual negative
134# override AIENOS_QEMU_CPU of the child scripts never reaches a receipt run.
135unset AIENOS_QEMU_CPU
136QUIET_FLAG="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}"
137AAVMF_CODE_FD="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
138AAVMF_VARS_FD="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
139PHYSICAL_STATEMENT="QEMU emulator runs only. Nothing here was run on Machine 1 or any GB10; no physical qualification is claimed."
140M4_NVME_PASS_NOTE="SMMU-confined NVMe DMA in QEMU (iommu=smmuv3, default build); TEST-ONLY bypass build boot-checked separately"
141NET_PASS_NOTE="QEMU user networking (slirp), virtio-net with ACCESS_PLATFORM required behind the smmuv3 vIOMMU; this PASS covers: attach, UDP TX seen by the host helper, UDP reply RX parsed by M6-A, an out-of-window device DMA refused (F_TRANSLATION, page intact) and recovery by reset, as checked by this run only; QEMU, not hardware"
142
143die() { echo "ck_gates: $*" >&2; exit 2; }
144
145# parse_selection LIST: LIST is gate names separated by commas or spaces. An
146# unknown name is refused (exit 2); an empty list is refused. Selected-out
147# gates are reported NOT_SELECTED, never PASS and never dropped.
148parse_selection() {
149 local -a names=(); local g
150 read -ra names <<<"${1//,/ }"
151 [[ ${#names[@]} -gt 0 ]] || die "--only needs at least one gate name"
152 for g in "${names[@]}"; do
153 awk -F'|' -v g="${g}" '$1==g {f=1} END {exit !f}' <<<"${CK_GATE_TABLE}" || die "unknown gate in --only: ${g}"
154 sel_gate[${g}]=1
155 done
156 selection_active=1
157}
158# want_sources: which child sources the selected gates need.
159want_sources() {
160 local gate src why
161 src_wanted=()
162 while IFS='|' read -r gate src why; do
163 [[ -n "${gate}" && -n "${sel_gate[${gate}]:-}" ]] && src_wanted[${src}]=1
164 done <<<"${CK_GATE_TABLE}"
165 return 0
166}
167json_str() {
168 local s="$1"
169 s="${s//\\/\\\\}"; s="${s//\"/\\\"}"; s="${s//$'\t'/\\t}"; s="${s//$'\r'/}"; s="${s//$'\n'/\\n}"
170 printf '"%s"' "$(printf '%s' "${s}" | tr -d '\000-\010\013\014\016-\037')"
171}
172sha_of() { sha256sum | cut -d' ' -f1; }
173file_sha() { [[ -r "$1" ]] && sha_of <"$1" || echo "not readable"; }
174
175tree_state() { git -C "$1" status --porcelain --untracked-files=normal; }
176refuse_if_dirty() {
177 local st; st="$(tree_state "$1")" || die "git status failed in $1"
178 if [[ -n "${st}" ]]; then
179 echo "ck_gates: refusing to run on a dirty tree. Commit, stash or remove these first:" >&2
180 printf '%s\n' "${st}" >&2
181 exit 2
182 fi
183}
184
185# ------------------------------------------------------------ child scripts
186# set_default_children: the real child scripts (only main calls this; the
187# self-test points the same variables at canned scripts in a temp folder).
188set_default_children() {
189 declare -gA child_script=(
190 [boot]="${repo_root}/scripts/qemu_ck_boot_test.sh"
191 [store]="${repo_root}/scripts/qemu_ck_store_test.sh"
192 [net]="${repo_root}/scripts/qemu_ck_net_test.sh"
193 [artifact]="${repo_root}/scripts/qemu_ck_artifact_test.sh"
194 [smp]="${repo_root}/scripts/qemu_ck_smp_test.sh"
195 [crash]="${repo_root}/scripts/qemu_ck_store_crash_test.sh"
196 [disk]="${repo_root}/scripts/qemu_ck_disk_layout_test.sh"
197 [cont]="${repo_root}/scripts/qemu_ck_continuity_test.sh"
198 [recov]="${repo_root}/scripts/qemu_ck_recovery_test.sh"
199 [allen]="${repo_root}/scripts/qemu_ck_allen_test.sh"
200 [kbd]="${repo_root}/scripts/qemu_ck_keyboard_test.sh"
201 [fpu]="${repo_root}/scripts/qemu_ck_fpu_test.sh"
202 [infer]="${repo_root}/scripts/qemu_ck_infer_test.sh"
203 [screen]="${repo_root}/scripts/qemu_ck_screen_test.sh"
204 [console]="${repo_root}/scripts/qemu_ck_console_test.sh"
205 [rollback]="${repo_root}/scripts/qemu_ck_rollback_test.sh"
206 [osc]="${repo_root}/scripts/qemu_ck_osc_unit_test.sh"
207 [osc_launch]="${repo_root}/scripts/qemu_ck_osc_launch_test.sh"
208 [osc_receipt]="${repo_root}/scripts/qemu_ck_osc_receipt_test.sh"
209 [osh_core]="${repo_root}/scripts/qemu_ck_osh_core_test.sh"
210 [osh_op]="${repo_root}/scripts/qemu_ck_osh_op_test.sh"
211 [slots]="${repo_root}/scripts/qemu_ck_slot_loader_test.sh"
212 )
213}
214
215# run_children: runs each child script that exists, logs outside the tree.
216run_children() {
217 local c script t0 log
218 declare -gA child_present=() child_rc=() child_secs=() child_log=() child_log_sha=() child_lines=() child_script_sha=()
219 for c in "${CHILDREN[@]}"; do
220 script="${child_script[${c}]}"
221 log="${work_dir}/logs/${c}.log"
222 child_log[${c}]="${log}"
223 if [[ ${selection_active} == 1 && -z "${src_wanted[${c}]:-}" ]]; then
224 child_present[${c}]=0; child_rc[${c}]=-; child_secs[${c}]=0
225 child_log_sha[${c}]=-; child_lines[${c}]=0; child_script_sha[${c}]=-
226 echo "--- [${c}] not selected: skipped" >&2
227 continue
228 fi
229 if [[ ! -f "${script}" ]]; then
230 child_present[${c}]=0; child_rc[${c}]=-; child_secs[${c}]=0
231 child_log_sha[${c}]=-; child_lines[${c}]=0; child_script_sha[${c}]=-
232 echo "--- [${c}] ${script#"${repo_root}"/} not present: its gates are NOT_RUN" >&2
233 continue
234 fi
235 child_present[${c}]=1
236 child_script_sha[${c}]="$(file_sha "${script}")"
237 echo "--- [${c}] running ${script#"${repo_root}"/} ..." >&2
238 t0="$(date +%s)"
239 bash "${script}" >"${log}" 2>&1 </dev/null
240 child_rc[${c}]=$?
241 child_secs[${c}]=$(( $(date +%s) - t0 ))
242 child_log_sha[${c}]="$(sha_of <"${log}")"
243 child_lines[${c}]="$(wc -l <"${log}")"
244 echo "--- [${c}] exit ${child_rc[${c}]} after ${child_secs[${c}]} s" >&2
245 # A failing child's own FAIL lines go to stderr (the evidence blob), so a red gate can be read
246 # without rerunning it: the child log itself lives in a temp folder that is removed at exit.
247 if [[ "${child_rc[${c}]}" != 0 ]]; then
248 { grep -aE '^(FAIL|ALLEN: |CONTINUITY: )' "${log}" | tail -n 40 | sed "s/^/ [${c}] /" >&2; } || true
249 { tail -n 5 "${log}" | sed "s/^/ [${c}] tail: /" >&2; } || true
250 fi
251 done
252}
253
254# classify_gate GATE SOURCE REASON -> "VERDICT|reason"
255# A verdict line must be a whole line: ^AIENOS_CK_<gate>: VERDICT( ...)?$
256classify_gate() {
257 local gate="$1" src="$2" why="$3" log rc markers n
258 if [[ "${src}" == missing ]]; then
259 echo "NOT_RUN|MISSING_IMPLEMENTATION: ${why}"; return
260 fi
261 if [[ -z "${child_script[${src}]+x}" ]]; then
262 echo "FAIL|gate table names unknown source ${src}"; return
263 fi
264 if [[ "${child_present[${src}]:-0}" != 1 ]]; then
265 echo "NOT_RUN|child script $(basename "${child_script[${src}]}") not present at this commit"; return
266 fi
267 log="${child_log[${src}]}"; rc="${child_rc[${src}]}"
268 markers="$(sed -nE "s/^AIENOS_CK_${gate}: (PASS|FAIL|NOT_RUN)( .*)?\$/\\1/p" "${log}" | sort -u)"
269 n="$(grep -c . <<<"${markers}")"
270 if [[ -z "${markers}" ]]; then
271 echo "FAIL|child exited ${rc} without an AIENOS_CK_${gate} verdict line"; return
272 fi
273 if [[ "${n}" -gt 1 ]]; then
274 echo "FAIL|contradictory AIENOS_CK_${gate} verdict lines ($(paste -sd, - <<<"${markers}"))"; return
275 fi
276 case "${markers}" in
277 NOT_RUN) echo "NOT_RUN|child reported NOT_RUN (exit ${rc})" ;;
278 FAIL) echo "FAIL|child reported FAIL (exit ${rc})" ;;
279 PASS)
280 if [[ "${rc}" == 0 ]]; then
281 echo "PASS|child reported PASS, exit 0"
282 elif [[ "${rc}" == 1 ]] && grep -qE '^AIENOS_CK_[A-Z0-9_]+: FAIL( .*)?$' "${log}"; then
283 echo "PASS|child reported PASS; its exit 1 comes from another gate's FAIL line"
284 else
285 echo "FAIL|child printed PASS but exited ${rc} with no FAIL line to explain it"
286 fi ;;
287 esac
288}
289
290# nvme_dma_mode: what DMA mode the store child's C NVMe path reported.
291nvme_dma_mode() {
292 local log="${child_log[store]:-}"
293 if [[ "${child_present[store]:-0}" != 1 || ! -f "${log}" ]]; then echo "not run"; return; fi
294 # The store script prints check descriptions, not serial lines; accept
295 # its PASS lines as well as the raw serial lines. Confined is reported
296 # only from the confined PASS line and the negative-test PASS line.
297 if grep -qE '^PASS NVMe DMA granted confined$' "${log}" \
298 && grep -qE '^PASS DMA outside the window faulted, page intact, controller still usable \(differs\)$' "${log}"; then
299 local m="Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted)"
300 grep -qE '^PASS NVMe DMA denied without an SMMU$' "${log}" && m+="; no-SMMU boot denied (NoSmmu)"
301 grep -qE '^PASS NVMe DMA granted through the TEST-ONLY bypass$' "${log}" && m+="; TEST-ONLY bypass image checked separately"
302 echo "${m}"
303 elif grep -qE '^PASS NVMe DMA granted through the unsafe bypass$|dma_gate: nvme granted \(UnsafeBypass\)' "${log}"; then
304 if grep -qE '^PASS NVMe DMA denied without an SMMU$' "${log}"; then
305 echo "UnsafeBypass (QEMU-only build, no SMMU confinement); default image denied (NoSmmu)"
306 else
307 echo "UnsafeBypass (QEMU-only build, no SMMU confinement)"
308 fi
309 elif grep -q 'dma_gate: nvme granted (Confined)' "${log}"; then echo "Confined"
310 elif grep -q 'dma_gate: nvme denied (NoSmmu)' "${log}"; then echo "denied (NoSmmu)"
311 else echo "not reported"; fi
312}
313
314# evaluate_table: fills gate_rows ("gate|source|verdict|reason") and prints
315# one verdict line per gate.
316evaluate_table() {
317 local gate src why out v r
318 gate_rows=()
319 while IFS='|' read -r gate src why; do
320 [[ -n "${gate}" ]] || continue
321 if [[ ${selection_active} == 1 && -z "${sel_gate[${gate}]:-}" ]]; then
322 gate_rows+=("${gate}|${src}|NOT_SELECTED|not in this run's --only list")
323 echo "AIENOS_CK_${gate}: NOT_SELECTED (not in this run's --only list)"
324 continue
325 fi
326 out="$(classify_gate "${gate}" "${src}" "${why}")"
327 v="${out%%|*}"; r="${out#*|}"
328 # M4_NVME PASS means the SMMU-confined mode: say so.
329 [[ "${v}" == PASS && "${gate}" == M4_NVME ]] && r="${M4_NVME_PASS_NOTE}"
330 [[ "${v}" == PASS && "${gate}" == NET ]] && r="${NET_PASS_NOTE}"
331 gate_rows+=("${gate}|${src}|${v}|${r}")
332 if [[ "${v}" == PASS && ( "${gate}" == M4_NVME || "${gate}" == NET ) ]]; then
333 echo "AIENOS_CK_${gate}: PASS (${r})"
334 elif [[ "${v}" == PASS ]]; then
335 echo "AIENOS_CK_${gate}: PASS"
336 else
337 echo "AIENOS_CK_${gate}: ${v} (${r})"
338 fi
339 done <<<"${CK_GATE_TABLE}"
340}
341
342count_rows() {
343 n_pass=0; n_fail=0; n_notrun=0; n_total=0; n_notsel=0
344 local row v
345 for row in "${gate_rows[@]}"; do
346 IFS='|' read -r _ _ v _ <<<"${row}"
347 n_total=$((n_total + 1))
348 case "${v}" in
349 PASS) n_pass=$((n_pass + 1)) ;;
350 NOT_RUN) n_notrun=$((n_notrun + 1)) ;;
351 NOT_SELECTED) n_notsel=$((n_notsel + 1)) ;;
352 *) n_fail=$((n_fail + 1)) ;;
353 esac
354 done
355 if [[ ${n_total} -gt 0 && ${n_pass} == "${n_total}" ]]; then overall=ALL_GATES_PASS_QEMU_ONLY; else overall=NOT_ALL_GATES_PASS; fi
356 if [[ ${selection_active} == 1 ]]; then
357 # A selected run never claims the whole set passed.
358 if [[ ${n_fail} == 0 && ${n_notrun} == 0 && $((n_pass + n_notsel)) == "${n_total}" ]]; then overall=SELECTED_GATES_PASS_QEMU_ONLY; else overall=SELECTED_GATES_NOT_ALL_PASS; fi
359 fi
360}
361
362# --------------------------------------------------------------- receipt
363collect_host() {
364 h_host="$(uname -n)"; h_arch="$(uname -m)"; h_kernel="$(uname -r)"
365 h_cpu="$(lscpu 2>/dev/null | sed -n 's/^Model name: *//p' | sort -u | paste -sd';' -)"
366 [[ -n "${h_cpu}" ]] || h_cpu="not readable"
367 if command -v qemu-system-aarch64 >/dev/null; then
368 q_path="$(command -v qemu-system-aarch64)"
369 q_version="$(qemu-system-aarch64 --version 2>/dev/null | head -1)"
370 else
371 q_path="not installed"; q_version="not installed"
372 fi
373 fw_code_sha="$(file_sha "${AAVMF_CODE_FD}")"; fw_vars_sha="$(file_sha "${AAVMF_VARS_FD}")"
374}
375
376# write_receipt: builds the JSON in work_dir, names it by its sha256 and
377# installs it in out_dir without ever replacing an existing file.
378write_receipt() {
379 local tmp="${work_dir}/receipt.tmp" i c g s v r first
380 {
381 echo "{"
382 echo " \"schema\": \"aienos.ck_gates.v1\","
383 echo " \"started_utc\": $(json_str "${started_utc}"),"
384 echo " \"finished_utc\": $(json_str "$(date -u +%FT%TZ)"),"
385 echo " \"commit\": $(json_str "${head_sha}"),"
386 echo " \"commit_subject\": $(json_str "${head_subject}"),"
387 echo " \"tree_clean_before\": ${tree_clean_before},"
388 echo " \"tree_clean_after\": ${tree_clean_after},"
389 echo " \"quiet_flag\": $(json_str "${QUIET_FLAG}"),"
390 echo " \"quiet_flag_present_at_start\": ${quiet_at_start},"
391 echo " \"host\": {\"hostname\": $(json_str "${h_host}"), \"arch\": $(json_str "${h_arch}"), \"kernel\": $(json_str "${h_kernel}"), \"cpu_model\": $(json_str "${h_cpu}")},"
392 echo " \"qemu\": {\"binary\": $(json_str "${q_path}"), \"version\": $(json_str "${q_version}")},"
393 echo " \"firmware\": {\"aavmf_code\": $(json_str "${AAVMF_CODE_FD}"), \"aavmf_code_sha256\": $(json_str "${fw_code_sha}"), \"aavmf_vars\": $(json_str "${AAVMF_VARS_FD}"), \"aavmf_vars_sha256\": $(json_str "${fw_vars_sha}")},"
394 echo " \"nvme_dma_mode\": $(json_str "${dma_mode}"),"
395 if [[ ${selection_active} == 1 ]]; then echo " \"selection\": $(json_str "$(printf "%s\n" "${!sel_gate[@]}" | sort | paste -sd, -)"),"; fi
396 echo " \"children\": ["
397 first=1
398 for c in "${CHILDREN[@]}"; do
399 [[ ${first} == 1 ]] || echo ","
400 first=0
401 printf ' {"name": %s, "script": %s, "present": %s, "script_sha256": %s, "exit": %s, "seconds": %s, "log_sha256": %s, "log_lines": %s}' \
402 "$(json_str "${c}")" "$(json_str "${child_script[${c}]#"${repo_root}"/}")" \
403 "$([[ ${child_present[${c}]} == 1 ]] && echo true || echo false)" \
404 "$(json_str "${child_script_sha[${c}]}")" "$(json_str "${child_rc[${c}]}")" \
405 "${child_secs[${c}]:-0}" "$(json_str "${child_log_sha[${c}]}")" "${child_lines[${c}]:-0}"
406 done
407 echo ""
408 echo " ],"
409 echo " \"gates\": ["
410 for i in "${!gate_rows[@]}"; do
411 IFS='|' read -r g s v r <<<"${gate_rows[$i]}"
412 printf ' {"id": %s, "marker": %s, "source": %s, "verdict": %s, "reason": %s}' \
413 "$(json_str "${g}")" "$(json_str "AIENOS_CK_${g}")" "$(json_str "${s}")" "$(json_str "${v}")" "$(json_str "${r}")"
414 [[ $i -lt $((${#gate_rows[@]} - 1)) ]] && echo "," || echo ""
415 done
416 echo " ],"
417 if [[ ${selection_active} == 1 ]]; then
418 echo " \"counts\": {\"total\": ${n_total}, \"pass\": ${n_pass}, \"fail\": ${n_fail}, \"not_run\": ${n_notrun}, \"not_selected\": ${n_notsel}},"
419 else
420 echo " \"counts\": {\"total\": ${n_total}, \"pass\": ${n_pass}, \"fail\": ${n_fail}, \"not_run\": ${n_notrun}},"
421 fi
422 echo " \"verdict\": $(json_str "${overall}"),"
423 echo " \"parity\": \"native/kernel/GATES.md\","
424 echo " \"physical\": \"NOT_RUN\","
425 echo " \"physical_statement\": $(json_str "${PHYSICAL_STATEMENT}")"
426 echo "}"
427 } >"${tmp}"
428 receipt_sha="$(sha_of <"${tmp}")"
429 receipt_path="${out_dir}/ck_gates_${receipt_sha}.json"
430 install_receipt "${tmp}" "${receipt_path}"
431}
432
433# install_receipt SRC DEST: noclobber copy; refuses an existing DEST.
434install_receipt() {
435 if [[ -e "$2" ]]; then
436 echo "ck_gates: receipt $2 already exists; never overwritten" >&2
437 return 2
438 fi
439 ( set -C; cat "$1" >"$2" ) 2>/dev/null || { echo "ck_gates: could not create $2 (exists or unwritable)" >&2; return 2; }
440}
441
442# ----------------------------------------------------------------- self-test
443self_test() {
444 local st_fail=0 tmp
445 tmp="$(mktemp -d "${TMPDIR:-/tmp}/ck-gates-selftest.XXXXXX")" || exit 2
446 work_dir="${tmp}/work"; mkdir -p "${work_dir}/logs" "${tmp}/kids"
447 ok() { echo "PASS $*"; }
448 bad() { echo "FAIL $*"; st_fail=1; }
449 # fake NAME RC LINES...: a canned child script.
450 fake() {
451 local f="${tmp}/kids/$1.sh" rc="$2"; shift 2
452 { echo '#!/usr/bin/env bash'; printf 'printf "%%s\\n"'; printf ' %q' "$@"; echo; echo "exit ${rc}"; } >"${f}"
453 echo "${f}"
454 }
455 expect() { # GATE VERDICT
456 local row g v
457 for row in "${gate_rows[@]}"; do
458 IFS='|' read -r g _ v _ <<<"${row}"
459 if [[ "${g}" == "$1" ]]; then
460 [[ "${v}" == "$2" ]] && ok "${scen}: $1 -> $2" || bad "${scen}: $1 -> ${v}, expected $2"
461 return
462 fi
463 done
464 bad "${scen}: $1 has no row"
465 }
466 expect_missing_all() {
467 local g
468 for g in P2_ARTIFACT M0_ROLLBACK OSC_UNIT OSC_LAUNCH OSC_RECEIPT OSH_CORE OSH_OP M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY M4_ALLEN KEYBOARD DISK_LAYOUT FPU INFER SCREEN CONSOLE; do expect "${g}" NOT_RUN; done # P2_ARTIFACT: no artifact child in A..G; M4_STORE_CRASH: no crash child in A..J and R; DISK_LAYOUT: no disk child in A..S
469 }
470 scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT [NET_SCRIPT] [ARTIFACT_SCRIPT] [SMP_SCRIPT] [CRASH_SCRIPT] [DISK_SCRIPT] (absent: missing)
471 scen="$1"
472 declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}" [crash]="${7:-${tmp}/kids/no_crash_child.sh}" [disk]="${8:-${tmp}/kids/no_disk_child.sh}" [cont]="${tmp}/kids/no_cont_child.sh" [recov]="${tmp}/kids/no_recov_child.sh" [allen]="${tmp}/kids/no_allen_child.sh" [kbd]="${kbd_kid:-${tmp}/kids/no_kbd_child.sh}" [fpu]="${tmp}/kids/no_fpu_child.sh" [infer]="${tmp}/kids/no_infer_child.sh" [screen]="${tmp}/kids/no_screen_child.sh" [console]="${console_kid:-${tmp}/kids/no_console_child.sh}" [rollback]="${rollback_kid:-${tmp}/kids/no_rollback_child.sh}" [osc]="${osc_kid:-${tmp}/kids/no_osc_child.sh}" [osc_launch]="${osc_launch_kid:-${tmp}/kids/no_osc_launch_child.sh}" [osc_receipt]="${osc_receipt_kid:-${tmp}/kids/no_osc_receipt_child.sh}" [osh_core]="${osh_core_kid:-${tmp}/kids/no_osh_core_child.sh}" [osh_op]="${osh_op_kid:-${tmp}/kids/no_osh_op_child.sh}" [slots]="${slots_kid:-${tmp}/kids/no_slots_child.sh}")
473 run_children 2>/dev/null
474 evaluate_table >"${tmp}/${scen}.out"
475 }
476
477 # A: all present gates PASS; the store child also claims PASS for gates
478 # with no C implementation, which must stay NOT_RUN.
479 scenario A "$(fake bootA 0 'PASS x' 'AIENOS_CK_M3: PASS' 'AIENOS_CK_M1: PASS')" \
480 "$(fake storeA 0 'AIENOS_CK_M4_NVME: PASS' 'AIENOS_CK_M4_STORE: PASS' 'AIENOS_CK_M4_STORE_CRASH: PASS' 'AIENOS_CK_ARGUS1_REVOKE: PASS (narrow)' \
481 'AIENOS_CK_SMMU: PASS' 'AIENOS_CK_NVME_SHUTDOWN: PASS' 'AIENOS_CK_KEYBOARD: PASS' 'PASS NVMe DMA granted confined' \
482 'PASS DMA outside the window faulted, page intact, controller still usable (differs)' 'PASS NVMe DMA denied without an SMMU' \
483 'PASS NVMe DMA granted through the TEST-ONLY bypass')" \
484 "$(fake netA 0 'PASS UDP reply received' 'AIENOS_CK_NET: PASS')" "" \
485 "$(fake smpA 0 'PASS 3 secondary cores checked in' 'AIENOS_CK_SMP_MUTATION: FAIL' 'AIENOS_CK_SMP: PASS')"
486 expect M1 PASS; expect M3 PASS; expect M4_NVME PASS; expect M4_STORE PASS; expect ARGUS1_REVOKE PASS; expect SMMU PASS; expect NVME_SHUTDOWN PASS; expect NET PASS; expect SMP PASS; expect_missing_all
487 count_rows
488 [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "9/0/18/27" && ${overall} == NOT_ALL_GATES_PASS ]] \
489 && ok "A: counts 9/0/18 of 27, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}"
490 [[ "$(nvme_dma_mode)" == "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately" ]] && ok "A: confined DMA mode recorded from the store PASS lines" || bad "A: dma mode '$(nvme_dma_mode)'"
491 grep -qxF "AIENOS_CK_M4_NVME: PASS (${M4_NVME_PASS_NOTE})" "${tmp}/A.out" \
492 && ok "A: M4_NVME PASS line carries the confined-mode note" || bad "A: M4_NVME PASS line lacks the note"
493 grep -qxF "AIENOS_CK_NET: PASS (${NET_PASS_NOTE})" "${tmp}/A.out" \
494 && ok "A: NET PASS line carries the QEMU slirp / SMMU fence note" || bad "A: NET PASS line lacks the note"
495 grep -qxF 'AIENOS_CK_M0_ROLLBACK: NOT_RUN (child script no_rollback_child.sh not present at this commit)' "${tmp}/A.out" \
496 && ok "A: M0_ROLLBACK without its child prints NOT_RUN (child script not present)" || bad "A: M0_ROLLBACK line wrong"
497 [[ "$(classify_gate X missing 'parked')" == "NOT_RUN|MISSING_IMPLEMENTATION: parked" ]] \
498 && ok "a missing-source row is NOT_RUN (MISSING_IMPLEMENTATION: reason)" || bad "missing-source row not NOT_RUN"
499 grep -q '^AIENOS_CK_KEYBOARD: NOT_RUN' "${tmp}/A.out" \
500 && ok "A: KEYBOARD claimed PASS by the store child stays NOT_RUN without its own child" || bad "A: KEYBOARD line wrong"
501 [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 27 ]] && ok "A: exactly 27 verdict lines" || bad "A: verdict line count"
502
503 # B: boot FAIL; store script missing -> its three gates NOT_RUN, never PASS.
504 scenario B "$(fake bootB 1 'FAIL kernel: alive' 'AIENOS_CK_M3: FAIL' 'AIENOS_CK_M1: FAIL')" "${tmp}/kids/does_not_exist.sh"
505 expect M1 FAIL; expect M3 FAIL; expect M4_NVME NOT_RUN; expect M4_STORE NOT_RUN; expect ARGUS1_REVOKE NOT_RUN; expect SMMU NOT_RUN; expect NVME_SHUTDOWN NOT_RUN; expect NET NOT_RUN; expect SMP NOT_RUN; expect_missing_all
506 [[ "$(nvme_dma_mode)" == "not run" ]] && ok "B: dma mode 'not run' when the store script is missing" || bad "B: dma mode"
507
508 # C: boot NOT_RUN (quiet flag held, exit 3); store mixed with exit 1.
509 scenario C "$(fake bootC 3 'NOT_RUN quiet flag held' 'AIENOS_CK_M3: NOT_RUN' 'AIENOS_CK_M1: NOT_RUN')" \
510 "$(fake storeC 1 'AIENOS_CK_M4_NVME: PASS' 'AIENOS_CK_M4_STORE: FAIL' 'AIENOS_CK_ARGUS1_REVOKE: PASS')"
511 expect M1 NOT_RUN; expect M3 NOT_RUN; expect M4_NVME PASS; expect M4_STORE FAIL; expect ARGUS1_REVOKE PASS
512
513 # D: no marker at all (exit 0); PASS lines with an unexplained exit 2.
514 scenario D "$(fake bootD 0 'all good, honest')" \
515 "$(fake storeD 2 'AIENOS_CK_M4_NVME: PASS' 'AIENOS_CK_M4_STORE: PASS' 'AIENOS_CK_ARGUS1_REVOKE: PASS')"
516 expect M1 FAIL; expect M3 FAIL; expect M4_NVME FAIL; expect M4_STORE FAIL; expect ARGUS1_REVOKE FAIL
517
518 # E: contradictory lines; a marker inside a line (echoed serial) does not
519 # count; NOT_RUN with exit 3.
520 scenario E "$(fake bootE 0 'AIENOS_CK_M1: PASS' 'AIENOS_CK_M1: FAIL')" \
521 "$(fake storeE 3 'serial: AIENOS_CK_M4_NVME: PASS' 'AIENOS_CK_M4_STORE: NOT_RUN' 'AIENOS_CK_ARGUS1_REVOKE: NOT_RUN')"
522 expect M1 FAIL; expect M4_NVME FAIL; expect M4_STORE NOT_RUN; expect ARGUS1_REVOKE NOT_RUN
523
524 # F: PASS line but exit 3 (e.g. killed after printing) -> FAIL.
525 scenario F "$(fake bootF 3 'AIENOS_CK_M1: PASS')" "$(fake storeF 0 'AIENOS_CK_M4_NVME: PASSED' 'AIENOS_CK_M4_STORE: PASS')"
526 expect M1 FAIL; expect M4_NVME FAIL; expect M4_STORE PASS; expect ARGUS1_REVOKE FAIL
527
528 # G: M3 FAIL with M1 PASS in one boot (exit 1 explained by the M3 FAIL
529 # line); an M3 PASS claimed by the store child is ignored (M3 is a boot gate).
530 scenario G "$(fake bootG 1 'FAIL typed IPC' 'AIENOS_CK_M3: FAIL' 'AIENOS_CK_M1: PASS')" \
531 "$(fake storeG 0 'AIENOS_CK_M3: PASS' 'AIENOS_CK_M4_NVME: PASS')"
532 expect M1 PASS; expect M3 FAIL; expect M4_NVME PASS
533
534 # H: artifact child PASS -> P2_ARTIFACT PASS; its claim for M0_ROLLBACK
535 # (no C implementation) stays NOT_RUN. I: artifact child NOT_RUN (quiet
536 # flag held, exit 3) -> NOT_RUN; J: FAIL -> FAIL.
537 scenario H "$(fake bootH 0 'AIENOS_CK_M1: PASS')" "$(fake storeH 0 'AIENOS_CK_M4_NVME: PASS')" "" \
538 "$(fake artH 0 'PASS x' 'AIENOS_CK_M0_ROLLBACK: PASS' 'AIENOS_CK_P2_ARTIFACT: PASS')"
539 expect P2_ARTIFACT PASS; expect M0_ROLLBACK NOT_RUN; expect M1 PASS
540 scenario I "$(fake bootI 0 'AIENOS_CK_M1: PASS')" "$(fake storeI 0 'AIENOS_CK_M4_NVME: PASS')" "" \
541 "$(fake artI 3 'NOT_RUN quiet flag held' 'AIENOS_CK_P2_ARTIFACT: NOT_RUN')"
542 expect P2_ARTIFACT NOT_RUN
543 scenario J "$(fake bootJ 0 'AIENOS_CK_M1: PASS')" "$(fake storeJ 0 'AIENOS_CK_M4_NVME: PASS')" "" \
544 "$(fake artJ 1 'FAIL P25WX' 'AIENOS_CK_P2_ARTIFACT: FAIL')"
545 expect P2_ARTIFACT FAIL
546 # O: the crash child (scripts/qemu_ck_store_crash_test.sh) PASS -> M4_STORE_CRASH PASS,
547 # and only that child's line counts (the store child's claim in A was ignored).
548 # P: crash child FAIL -> FAIL. Q: crash child prints PASS but exits 1 with no
549 # FAIL line -> FAIL. S: crash child quiet flag held (exit 3) -> NOT_RUN.
550 scenario O "$(fake bootO 0 'AIENOS_CK_M1: PASS')" "$(fake storeO 0 'AIENOS_CK_M4_NVME: PASS')" "" "" "" \
551 "$(fake crashO 0 'CK_STORE_CRASH_512B_QEMU: PASS' 'AIENOS_CK_M4_STORE_CRASH: PASS')"
552 expect M4_STORE_CRASH PASS; expect M4_CONTINUITY NOT_RUN; expect M4_RECOVERY NOT_RUN
553 scenario P "$(fake bootP 0 'AIENOS_CK_M1: PASS')" "$(fake storeP 0 'AIENOS_CK_M4_STORE_CRASH: PASS')" "" "" "" \
554 "$(fake crashP 1 'bs=4096 settle=3 cp=after_final_flush -> BAD' 'AIENOS_CK_M4_STORE_CRASH: FAIL')"
555 expect M4_STORE_CRASH FAIL
556 scenario Q "$(fake bootQ 0 'AIENOS_CK_M1: PASS')" "$(fake storeQ 0 'AIENOS_CK_M4_NVME: PASS')" "" "" "" \
557 "$(fake crashQ 1 'AIENOS_CK_M4_STORE_CRASH: PASS')"
558 expect M4_STORE_CRASH FAIL
559 scenario S "$(fake bootS 0 'AIENOS_CK_M1: PASS')" "$(fake storeS 0 'AIENOS_CK_M4_NVME: PASS')" "" "" "" \
560 "$(fake crashS 3 'NOT_RUN quiet flag held' 'AIENOS_CK_M4_STORE_CRASH: NOT_RUN')"
561 expect M4_STORE_CRASH NOT_RUN
562
563 # K: SMP child FAIL (a core never checked in) -> FAIL; L: quiet flag held
564 # (exit 3) -> NOT_RUN; M: PASS line with an unexplained exit 2 -> FAIL;
565 # N: only the mutation verdict line (AIENOS_CK_SMP_MUTATION) -> no SMP
566 # verdict -> FAIL, never PASS.
567 scenario K "$(fake bootK 0 'AIENOS_CK_M1: PASS')" "$(fake storeK 0 'AIENOS_CK_M4_NVME: PASS')" "" "" \
568 "$(fake smpK 1 'FAIL core never checked in: smp_cpu: target=0x3 checked_in=no' 'AIENOS_CK_SMP: FAIL')"
569 expect SMP FAIL; expect M1 PASS
570 scenario L "$(fake bootL 0 'AIENOS_CK_M1: PASS')" "$(fake storeL 0 'AIENOS_CK_M4_NVME: PASS')" "" "" \
571 "$(fake smpL 3 'NOT_RUN quiet flag held' 'AIENOS_CK_SMP: NOT_RUN')"
572 expect SMP NOT_RUN
573 scenario M "$(fake bootM 0 'AIENOS_CK_M1: PASS')" "$(fake storeM 0 'AIENOS_CK_M4_NVME: PASS')" "" "" \
574 "$(fake smpM 2 'AIENOS_CK_SMP: PASS')"
575 expect SMP FAIL
576 scenario N "$(fake bootN 0 'AIENOS_CK_M1: PASS')" "$(fake storeN 0 'AIENOS_CK_M4_NVME: PASS')" "" "" \
577 "$(fake smpN 0 'AIENOS_CK_SMP_MUTATION: PASS (gate checks FAIL on the CPU_ON-skip image; mutation killed)')"
578 expect SMP FAIL
579
580 # T: disk child (scripts/qemu_ck_disk_layout_test.sh) PASS -> DISK_LAYOUT PASS;
581 # a store gate it claims is ignored. U: disk child FAIL (mutant survived)
582 # -> FAIL; V: PASS line, exit 1 -> FAIL.
583 scenario T "$(fake bootT 0 'AIENOS_CK_M1: PASS')" "$(fake storeT 3 'AIENOS_CK_M4_STORE: NOT_RUN')" "" "" "" "" \
584 "$(fake diskT 0 'PASS translation bypass makes DISK_LAYOUT fail (mutant killed)' 'AIENOS_CK_M4_STORE: PASS' 'AIENOS_CK_DISK_LAYOUT: PASS')"
585 expect DISK_LAYOUT PASS; expect M4_STORE NOT_RUN
586 scenario U "$(fake bootU 0 'AIENOS_CK_M1: PASS')" "$(fake storeU 0 'AIENOS_CK_M4_NVME: PASS')" "" "" "" "" \
587 "$(fake diskU 1 'FAIL translation bypass mutant survived' 'AIENOS_CK_DISK_LAYOUT: FAIL')"
588 expect DISK_LAYOUT FAIL
589 scenario V "$(fake bootV 0 'AIENOS_CK_M1: PASS')" "$(fake storeV 0 'AIENOS_CK_M4_NVME: PASS')" "" "" "" "" \
590 "$(fake diskV 1 'AIENOS_CK_DISK_LAYOUT: PASS')"
591 expect DISK_LAYOUT FAIL
592 # W, X, Y: KEYBOARD comes only from its own child (scripts/qemu_ck_keyboard_test.sh):
593 # PASS with exit 0 is PASS, a FAIL verdict is FAIL, exit 3 (lock or quiet flag) is NOT_RUN.
594 kbd_kid="$(fake kbdW 0 'ROW 30 PASS smmu: overflow line refused' 'AIENOS_CK_KEYBOARD_DMA: PASS' 'AIENOS_CK_KEYBOARD: PASS (rows 25-32)')"
595 scenario W "$(fake bootW 0 'AIENOS_CK_M1: PASS')" "$(fake storeW 0 'AIENOS_CK_KEYBOARD: FAIL')"
596 expect KEYBOARD PASS
597 kbd_kid="$(fake kbdX 1 'ROW 29 FAIL smmu: shell line missing' 'AIENOS_CK_KEYBOARD: FAIL')"
598 scenario X "$(fake bootX 0 'AIENOS_CK_M1: PASS')" "$(fake storeX 0 'AIENOS_CK_KEYBOARD: PASS')"
599 expect KEYBOARD FAIL
600 kbd_kid="$(fake kbdY 3 'NOT_RUN QEMU gate lock held' 'AIENOS_CK_KEYBOARD: NOT_RUN (gate lock held)')"
601 scenario Y "$(fake bootY 0 'AIENOS_CK_M1: PASS')" "$(fake storeY 0 'AIENOS_CK_M4_NVME: PASS')"
602 expect KEYBOARD NOT_RUN
603 unset kbd_kid
604 # CA, CB, CC: CONSOLE comes only from its own child (scripts/qemu_ck_console_test.sh), like KEYBOARD.
605 console_kid="$(fake conA 0 'CONSOLE_ROW 147: PASS' 'AIENOS_CK_CONSOLE: PASS (rows 146-153)')"
606 scenario CA "$(fake bootCA 0 'AIENOS_CK_M1: PASS')" "$(fake storeCA 0 'AIENOS_CK_CONSOLE: FAIL')"
607 expect CONSOLE PASS
608 console_kid="$(fake conB 1 'CONSOLE_ROW 148: FAIL' 'AIENOS_CK_CONSOLE: FAIL')"
609 scenario CB "$(fake bootCB 0 'AIENOS_CK_M1: PASS')" "$(fake storeCB 0 'AIENOS_CK_CONSOLE: PASS')"
610 expect CONSOLE FAIL
611 console_kid="$(fake conC 3 'NOT_RUN QEMU gate lock held' 'AIENOS_CK_CONSOLE: NOT_RUN (gate lock held)')"
612 scenario CC "$(fake bootCC 0 'AIENOS_CK_M1: PASS')" "$(fake storeCC 0 'AIENOS_CK_M4_NVME: PASS')"
613 expect CONSOLE NOT_RUN
614 unset console_kid
615 # RA, RB, RC: M0_ROLLBACK comes only from its own child (scripts/qemu_ck_rollback_test.sh).
616 rollback_kid="$(fake rbA 0 'PASS normal: attempt evidence in boot 2' 'AIENOS_CK_M0_ROLLBACK: PASS (QEMU only, C kernel image as candidate; physical NOT_RUN)')"
617 scenario RA "$(fake bootRA 0 'AIENOS_CK_M1: PASS')" "$(fake storeRA 0 'AIENOS_CK_M0_ROLLBACK: FAIL')"
618 expect M0_ROLLBACK PASS
619 rollback_kid="$(fake rbB 1 'FAIL hang: attempt evidence in boot 2' 'AIENOS_CK_M0_ROLLBACK: FAIL')"
620 scenario RB "$(fake bootRB 0 'AIENOS_CK_M1: PASS')" "$(fake storeRB 0 'AIENOS_CK_M0_ROLLBACK: PASS')"
621 expect M0_ROLLBACK FAIL
622 rollback_kid="$(fake rbC 3 'NOT_RUN QEMU gate lock held' 'AIENOS_CK_M0_ROLLBACK: NOT_RUN')"
623 scenario RC "$(fake bootRC 0 'AIENOS_CK_M1: PASS')" "$(fake storeRC 0 'AIENOS_CK_M4_NVME: PASS')"
624 expect M0_ROLLBACK NOT_RUN
625 unset rollback_kid
626 # OA, OB: OSC_UNIT comes only from its own child (scripts/qemu_ck_osc_unit_test.sh).
627 osc_kid="$(fake oscA 0 'PASS x' 'AIENOS_CK_OSC_UNIT: PASS')"
628 scenario OA "$(fake bootOA 0 'AIENOS_CK_M1: PASS')" "$(fake storeOA 0 'AIENOS_CK_OSC_UNIT: FAIL')"
629 expect OSC_UNIT PASS
630 osc_kid="$(fake oscB 1 'FAIL y' 'AIENOS_CK_OSC_UNIT: FAIL')"
631 scenario OB "$(fake bootOB 0 'AIENOS_CK_M1: PASS')" "$(fake storeOB 0 'AIENOS_CK_OSC_UNIT: PASS')"
632 expect OSC_UNIT FAIL
633 unset osc_kid
634 # LA, LB: OSC_LAUNCH comes only from its own child (scripts/qemu_ck_osc_launch_test.sh).
635 osc_launch_kid="$(fake olA 0 'PASS x' 'AIENOS_CK_OSC_LAUNCH: PASS')"
636 scenario LA "$(fake bootLA 0 'AIENOS_CK_M1: PASS')" "$(fake storeLA 0 'AIENOS_CK_OSC_LAUNCH: FAIL')"
637 expect OSC_LAUNCH PASS
638 osc_launch_kid="$(fake olB 1 'FAIL y' 'AIENOS_CK_OSC_LAUNCH: FAIL')"
639 scenario LB "$(fake bootLB 0 'AIENOS_CK_M1: PASS')" "$(fake storeLB 0 'AIENOS_CK_OSC_LAUNCH: PASS')"
640 expect OSC_LAUNCH FAIL
641 unset osc_launch_kid
642 # EA, EB, EC: OSC_RECEIPT comes only from its own child (scripts/qemu_ck_osc_receipt_test.sh, aienos#383).
643 osc_receipt_kid="$(fake orA 0 'PASS x' 'AIENOS_CK_OSC_RECEIPT: PASS')"
644 scenario EA "$(fake bootEA 0 'AIENOS_CK_M1: PASS')" "$(fake storeEA 0 'AIENOS_CK_OSC_RECEIPT: FAIL')"
645 expect OSC_RECEIPT PASS
646 osc_receipt_kid="$(fake orB 1 'FAIL y' 'AIENOS_CK_OSC_RECEIPT: FAIL')"
647 scenario EB "$(fake bootEB 0 'AIENOS_CK_M1: PASS')" "$(fake storeEB 0 'AIENOS_CK_OSC_RECEIPT: PASS')"
648 expect OSC_RECEIPT FAIL
649 osc_receipt_kid="$(fake orC 3 'NOT_RUN QEMU gate lock held' 'AIENOS_CK_OSC_RECEIPT: NOT_RUN')"
650 scenario EC "$(fake bootEC 0 'AIENOS_CK_M1: PASS')" "$(fake storeEC 0 'AIENOS_CK_OSC_RECEIPT: PASS')"
651 expect OSC_RECEIPT NOT_RUN
652 unset osc_receipt_kid
653 # HA, HB: OSH_CORE comes only from its own child (scripts/qemu_ck_osh_core_test.sh).
654 osh_core_kid="$(fake ohA 0 'PASS x' 'AIENOS_CK_OSH_CORE: PASS')"
655 scenario HA "$(fake bootHA 0 'AIENOS_CK_M1: PASS')" "$(fake storeHA 0 'AIENOS_CK_OSH_CORE: FAIL')"
656 expect OSH_CORE PASS
657 osh_core_kid="$(fake ohB 1 'FAIL y' 'AIENOS_CK_OSH_CORE: FAIL')"
658 scenario HB "$(fake bootHB 0 'AIENOS_CK_M1: PASS')" "$(fake storeHB 0 'AIENOS_CK_OSH_CORE: PASS')"
659 expect OSH_CORE FAIL
660 unset osh_core_kid
661
662 # SEL: gate selection (--only). Selected-out gates are NOT_SELECTED, their
663 # child scripts do not run, and a selection never reads as ALL_GATES_PASS.
664 sel_gate=(); sel_gate[M1]=1; sel_gate[M3]=1; selection_active=1; want_sources
665 scenario SEL1 "$(fake bootS1 0 'AIENOS_CK_M1: PASS' 'AIENOS_CK_M3: PASS')" \
666 "$(fake storeS1 0 'AIENOS_CK_SMMU: PASS' 'AIENOS_CK_M4_NVME: PASS')"
667 expect M1 PASS; expect M3 PASS; expect SMMU NOT_SELECTED; expect M4_NVME NOT_SELECTED; expect SMP NOT_SELECTED
668 [[ "${child_present[store]}" == 0 && "${child_rc[store]}" == - && "${child_present[boot]}" == 1 ]] \
669 && ok "SEL1: the store child was not run, the boot child was" || bad "SEL1: child selection wrong"
670 grep -qxF "AIENOS_CK_SMMU: NOT_SELECTED (not in this run's --only list)" "${tmp}/SEL1.out" \
671 && ok "SEL1: selected-out gate prints an explicit NOT_SELECTED line" || bad "SEL1: no NOT_SELECTED line"
672 [[ "$(grep -c '^AIENOS_CK_' "${tmp}/SEL1.out")" == 27 ]] && ok "SEL1: all 27 gates still reported (none dropped)" || bad "SEL1: gate lines dropped"
673 count_rows
674 [[ "${n_pass}/${n_fail}/${n_notrun}/${n_notsel}/${n_total}" == "2/0/0/25/27" && ${overall} == SELECTED_GATES_PASS_QEMU_ONLY ]] \
675 && ok "SEL1: counts 2 pass, 25 not selected, never ALL_GATES_PASS" || bad "SEL1: counts ${n_pass}/${n_fail}/${n_notrun}/${n_notsel}/${n_total} ${overall}"
676 # SEL2: a selected gate that FAILs fails the run; a selected gate with no
677 # verdict is FAIL, not dropped.
678 scenario SEL2 "$(fake bootS2 1 'AIENOS_CK_M1: FAIL' 'AIENOS_CK_M3: PASS')" "$(fake storeS2 0 'AIENOS_CK_SMMU: PASS')"
679 expect M1 FAIL; expect M3 PASS; count_rows
680 [[ ${n_fail} == 1 && ${overall} == SELECTED_GATES_NOT_ALL_PASS ]] && ok "SEL2: a selected FAIL counts as FAIL" || bad "SEL2: ${n_fail} ${overall}"
681 # SEL3: the complementary selection; the two together report every gate exactly once.
682 sel_gate=(); selection_active=1
683 local gx sx wx
684 while IFS='|' read -r gx sx wx; do [[ -n "${gx}" && "${gx}" != M1 && "${gx}" != M3 ]] && sel_gate[${gx}]=1; done <<<"${CK_GATE_TABLE}"
685 want_sources
686 scenario SEL3 "$(fake bootS3 0 'AIENOS_CK_M1: PASS')" "$(fake storeS3 0 'AIENOS_CK_SMMU: PASS')"
687 expect M1 NOT_SELECTED; expect SMMU PASS
688 local once=1 nsel
689 while IFS='|' read -r gx sx wx; do
690 [[ -n "${gx}" ]] || continue
691 nsel=$(cat "${tmp}/SEL1.out" "${tmp}/SEL3.out" | grep -c "^AIENOS_CK_${gx}: \(PASS\|FAIL\|NOT_RUN\)")
692 [[ "${nsel}" == 1 ]] || { once=0; echo " ${gx} reported ${nsel} times"; }
693 done <<<"${CK_GATE_TABLE}"
694 [[ ${once} == 1 ]] && ok "SEL3: two complementary selections report every gate exactly once" || bad "SEL3: a gate was dropped or doubled"
695 # Negative control: an unknown gate name is refused (exit 2), an empty list too.
696 sel_gate=(); selection_active=0
697 ( parse_selection "M1,NO_SUCH_GATE" ) >/dev/null 2>&1; rc=$?
698 [[ ${rc} == 2 ]] && ok "negative control: unknown gate name refused, exit 2" || bad "unknown gate name gave exit ${rc}"
699 ( parse_selection " " ) >/dev/null 2>&1; rc=$?
700 [[ ${rc} == 2 ]] && ok "negative control: empty gate list refused, exit 2" || bad "empty list gave exit ${rc}"
701 bash "${BASH_SOURCE[0]}" --only "M1,m3" >/dev/null 2>&1; rc=$?
702 [[ ${rc} == 2 ]] && ok "negative control: --only with a wrong-case name refused end to end, exit 2" || bad "--only wrong case gave exit ${rc}"
703 bash "${BASH_SOURCE[0]}" --only "" >/dev/null 2>&1; rc=$?
704 [[ ${rc} == 2 ]] && ok "negative control: --only with an empty value refused end to end, exit 2" || bad "--only '' gave exit ${rc}"
705 sel_gate=(); src_wanted=(); selection_active=0
706
707 # Receipt: named by its content hash, valid JSON, physical NOT_RUN, never overwritten.
708 scenario R "$(fake bootR 0 'AIENOS_CK_M1: PASS')" "$(fake storeR 0 'AIENOS_CK_M4_NVME: PASS')"
709 count_rows
710 out_dir="${tmp}/out"; mkdir -p "${out_dir}"
711 started_utc=x; head_sha=x; head_subject='quote " backslash \ tab end'; tree_clean_before=true; tree_clean_after=true; quiet_at_start=false
712 h_host=h; h_arch=a; h_kernel=k; h_cpu=c; q_path=q; q_version=v; fw_code_sha=x; fw_vars_sha=y; dma_mode="$(nvme_dma_mode)"
713 write_receipt
714 if [[ -f "${receipt_path}" && "$(sha_of <"${receipt_path}")" == "${receipt_sha}" && "${receipt_path}" == *"/ck_gates_${receipt_sha}.json" ]]; then
715 ok "receipt name is the sha256 of its content"
716 else
717 bad "receipt name does not match its content hash"
718 fi
719 # tree_clean_before comes from the pre-run tree state, never a literal.
720 grep -q '"tree_clean_before": true,' "${receipt_path}" && ok "receipt records tree_clean_before true when the tree was clean" || bad "tree_clean_before not true in a clean-tree receipt"
721 tree_clean_before=false; out_dir="${tmp}/out_dirty"; mkdir -p "${out_dir}"
722 write_receipt
723 grep -q '"tree_clean_before": false,' "${receipt_path}" && ok "mutant: dirty tree before the run -> receipt says tree_clean_before false" || bad "mutant survived: tree_clean_before stayed true"
724 tree_clean_before=true; out_dir="${tmp}/out"
725 # Restore the clean-tree receipt as the one the checks below look at.
726 write_receipt
727 if command -v jq >/dev/null; then
728 jq -e '.physical == "NOT_RUN" and (.gates | length) == 27 and .verdict == "NOT_ALL_GATES_PASS"
729 and ([.gates[] | select(.id == "M1")][0].verdict == "PASS")
730 and ([.gates[] | select(.id == "ARGUS1_REVOKE")][0].verdict == "FAIL")
731 and (.children | length) == 22 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \
732 && ok "receipt is valid JSON (jq) with expected fields" || bad "receipt JSON invalid or fields wrong"
733 else
734 echo "NOT_RUN jq not installed; JSON validity not machine-checked"; jq_skipped=1
735 fi
736 local before_sha; before_sha="$(sha_of <"${receipt_path}")"
737 echo junk >"${work_dir}/other.tmp"
738 if ! install_receipt "${work_dir}/other.tmp" "${receipt_path}" 2>/dev/null && [[ "$(sha_of <"${receipt_path}")" == "${before_sha}" ]]; then
739 ok "an existing receipt is never overwritten"
740 else
741 bad "an existing receipt was overwritten or the clash was not refused"
742 fi
743
744 # The gate table parses: 27 rows, known sources, reasons only on missing rows.
745 local g s w rows=0 tbad=0
746 while IFS='|' read -r g s w; do
747 [[ -n "${g}" ]] || continue
748 rows=$((rows + 1))
749 case "${s}" in
750 boot|store|net|artifact|smp|crash|disk|cont|recov|allen|kbd|fpu|infer|screen|console|rollback|osc|osc_launch|osc_receipt|osh_core|osh_op|slots) [[ "${w}" == - ]] || tbad=1 ;;
751 missing) [[ -n "${w}" && "${w}" != - ]] || tbad=1 ;;
752 *) tbad=1 ;;
753 esac
754 done <<<"${CK_GATE_TABLE}"
755 [[ ${tbad} == 0 && ${rows} == 27 ]] && ok "gate table: 27 rows well-formed" || bad "gate table malformed (${rows} rows)"
756
757 # Dirty-tree refusal, end to end, on a private clone (never this tree).
758 # Both runs stop before any child script (no QEMU).
759 local clone="${tmp}/clone" rc
760 if git clone -q --shared "${repo_root}" "${clone}" 2>/dev/null; then
761 cp "${BASH_SOURCE[0]}" "${clone}/scripts/ck_gates.sh"
762 git -C "${clone}" add scripts/ck_gates.sh
763 git -C "${clone}" -c user.name=t -c user.email=t@t commit -qm selftest --allow-empty
764 : >"${clone}/untracked_junk"
765 bash "${clone}/scripts/ck_gates.sh" --out "${tmp}/o1" >/dev/null 2>&1; rc=$?
766 [[ ${rc} == 2 && ! -e "${tmp}/o1" ]] && ok "dirty tree (untracked file) refused, exit 2, no receipt" || bad "untracked junk gave exit ${rc}"
767 rm -f "${clone}/untracked_junk"
768 echo x >>"${clone}/README.md" 2>/dev/null || echo x >"${clone}/README.md"
769 bash "${clone}/scripts/ck_gates.sh" --out "${tmp}/o2" >/dev/null 2>&1; rc=$?
770 [[ ${rc} == 2 && ! -e "${tmp}/o2" ]] && ok "dirty tree (modified tracked file) refused, exit 2, no receipt" || bad "modified tree gave exit ${rc}"
771 else
772 bad "could not make a private clone for the dirty-tree test"
773 fi
774 bash "${BASH_SOURCE[0]}" --bogus >/dev/null 2>&1; rc=$?
775 [[ ${rc} == 2 ]] && ok "unknown argument -> exit 2" || bad "unknown argument gave exit ${rc}"
776
777 rm -rf --one-file-system "${tmp}"
778 if [[ ${st_fail} == 0 ]]; then echo "AIENOS_CK_GATES_SELF_TEST: PASS${jq_skipped:+ (jq NOT_RUN: JSON validity of the receipt not machine-checked)}"; exit 0; fi
779 echo "AIENOS_CK_GATES_SELF_TEST: FAIL"; exit 1
780}
781
782# ---------------------------------------------------------------------- main
783main() {
784 local out_arg="" mode=full only_arg=""
785 while [[ $# -gt 0 ]]; do
786 case "$1" in
787 --out) [[ $# -ge 2 ]] || die "--out needs a directory"; out_arg="$2"; shift ;;
788 --self-test) mode=self ;;
789 --only) [[ $# -ge 2 && -n "$2" ]] || die "--only needs a gate list"; only_arg="$2"; shift ;;
790 -h|--help) sed -n '2,44p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;;
791 *) die "unknown argument: $1" ;;
792 esac
793 shift
794 done
795 if [[ ${mode} == self ]]; then self_test; fi
796 [[ -n "${only_arg}" || -z "${AIENOS_CK_ONLY:-}" ]] || only_arg="${AIENOS_CK_ONLY}"
797 if [[ -n "${only_arg}" ]]; then parse_selection "${only_arg}"; want_sources; fi
798
799 refuse_if_dirty "${repo_root}"
800 local before; before="$(tree_state "${repo_root}")"
801 tree_clean_before=true
802 [[ -z "${before}" ]] || tree_clean_before=false
803 head_sha="$(git -C "${repo_root}" rev-parse HEAD)"
804 head_subject="$(git -C "${repo_root}" log -1 --format=%s HEAD)"
805 started_utc="$(date -u +%FT%TZ)"
806 quiet_at_start="$([[ -e ${QUIET_FLAG} ]] && echo true || echo false)"
807 # Resolved now, created only after the tree re-check, so an in-tree DIR
808 # gets the receipt and nothing else.
809 out_dir="$(realpath -m -- "${out_arg:-${repo_root}/evidence}")"
810 work_dir="$(mktemp -d "${TMPDIR:-/tmp}/ck-gates-work.XXXXXX")" || die "mktemp failed"
811 mkdir -p "${work_dir}/logs"
812
813 echo "AIENOS C kernel gates at ${head_sha} (${head_subject}); QEMU only, qualifies nothing physical"
814 collect_host
815 set_default_children
816 run_children
817 dma_mode="$(nvme_dma_mode)"
818 evaluate_table
819 count_rows
820
821 local after; after="$(tree_state "${repo_root}")"
822 tree_clean_after=true
823 if [[ "${after}" != "${before}" ]]; then
824 tree_clean_after=false
825 echo "ck_gates: the run changed the tree:" >&2
826 printf '%s\n' "${after}" >&2
827 fi
828
829 mkdir -p "${out_dir}" || die "cannot create ${out_dir}"
830 write_receipt || exit 2
831 echo "NVMe DMA mode: ${dma_mode}"
832 echo "Child logs: ${work_dir}/logs"
833 echo "Receipt: ${receipt_path}"
834 if [[ ${selection_active} == 1 ]]; then
835 echo "AIENOS_CK_GATES: ${overall} (pass=${n_pass} fail=${n_fail} not_run=${n_notrun} not_selected=${n_notsel} of ${n_total}; physical NOT_RUN)"
836 else
837 echo "AIENOS_CK_GATES: ${overall} (pass=${n_pass} fail=${n_fail} not_run=${n_notrun} of ${n_total}; physical NOT_RUN)"
838 fi
839 [[ ${n_fail} == 0 && ${tree_clean_after} == true ]] && exit 0 || exit 1
840}
841
842if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
843 main "$@"
844fi