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 / gate_hold_selftest.sh

208 lines · 10787 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# Self-test of scripts/lib_gate_hold.sh, the one way QEMU gate scripts treat
18# the machine quiet flag and the QEMU gate lock (aienos#278).
19#
20# Runs only in a temp folder: AIENOS_QUIET_FLAG and AIENOS_GATE_LOCK point
21# there, never at ~/workspace. No QEMU, no build, no GPU.
22#
23# Cases:
24# 1 two competing gates: the second refuses (exit 3) and names the holder
25# 2 a gate killed mid-run (SIGKILL, no trap): another gate does not remove
26# that dead hold before its expected_end; the same gate removes its own
27# dead hold and runs; a dead hold past expected_end is stale for any gate
28# 3 a record without a readable pid (old "<name> <pid>" or "lane tag" form)
29# is held and never removed, in the gate lock and in the quiet flag
30# 4 the quiet flag held by a live owner: the gate refuses by name, the flag
31# is unchanged
32# 5 the quiet flag of the caller's own quietlock hold (hold=<id> equal to
33# QUIETLOCK_HOLD): the gate runs and leaves the flag alone
34# 6 release removes only this run's own record
35# 7 the gate lock record is in the quietlock record format (holder, start=,
36# expected_end=, pid=, hold=); when quietlock is installed it reads the
37# holder as alive
38# 8 no script under scripts/ writes or removes the machine quiet flag
39# 9 a "*" in a record is not globbed; a reused pid (other start time) is
40# dead; no date or no flock: the gate refuses and writes nothing
41# Exit 0 when every case passes, 1 otherwise.
42set -uo pipefail
43
44here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
45lib="${here}/lib_gate_hold.sh"
46tmp="$(mktemp -d)"
47trap 'rm -rf "${tmp}"' EXIT
48export AIENOS_QUIET_FLAG="${tmp}/spark-quiet"
49export AIENOS_GATE_LOCK="${tmp}/qemu-gate-lock"
50unset QUIETLOCK_HOLD
51case "${AIENOS_QUIET_FLAG}${AIENOS_GATE_LOCK}" in
52 *"${HOME}/workspace/"*) echo "gate_hold_selftest: refusing to run on the real flag paths" >&2; exit 1 ;;
53esac
54
55pass=0 fail=0
56ok() { pass=$((pass + 1)); echo "ok $1"; }
57bad() { fail=$((fail + 1)); echo "FAIL $1"; }
58check() { if eval "$2"; then ok "$1"; else bad "$1"; fi; }
59
60# A fake gate: takes the quiet check and the gate lock like a real gate, then
61# runs its body ("sleep N" or "true"). Exit 3 + "NOT_RUN <why>" when refused.
62gate="${tmp}/fake_gate.sh"
63cat >"${gate}" <<EOF
64#!/usr/bin/env bash
65set -euo pipefail
66. "${lib}"
67name="\$1"; shift
68if ! gh_quiet_check || ! gh_lock_take "\${name}" "\${GATE_MINUTES:-30}"; then
69 echo "NOT_RUN \${gh_why}"
70 exit 3
71fi
72trap gh_lock_release EXIT
73"\$@"
74EOF
75chmod +x "${gate}"
76
77dead_pid() { sleep 0 & local p=$!; wait "${p}" 2>/dev/null; echo "${p}"; }
78iso() { date -u -d "$1" +%Y-%m-%dT%H:%M:%SZ; }
79wait_lock() { local i; for i in $(seq 1 100); do [[ -s "${AIENOS_GATE_LOCK}" ]] && return 0; sleep 0.05; done; return 1; }
80
81if [[ ! -r "${lib}" ]]; then
82 bad "lib_gate_hold.sh exists"
83fi
84
85# ---- 1: two competing gates
86"${gate}" gateA sleep 30 >"${tmp}/a.out" 2>&1 & a=$!
87wait_lock
88out="$("${gate}" gateB true 2>&1)"; rc=$?
89check "1 second gate refuses (exit 3)" '[[ ${rc} == 3 ]]'
90check "1 refusal names the holder" '[[ ${out} == *"gateA"* ]]'
91out="$("${gate}" gateA true 2>&1)"; rc=$?
92check "1 same gate name, live holder: refuses" '[[ ${rc} == 3 && ${out} == *"gateA"* ]]'
93
94# ---- 2: kill mid-gate, then cleanup removes only its own dead hold
95kill -KILL "${a}" 2>/dev/null; wait "${a}" 2>/dev/null
96check "2 SIGKILL leaves the hold behind" '[[ -s ${AIENOS_GATE_LOCK} ]]'
97before="$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)"
98out="$("${gate}" gateB true 2>&1)"; rc=$?
99check "2 another gate does not remove a dead hold before expected_end" \
100 '[[ ${rc} == 3 && "$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)" == "${before}" ]]'
101out="$("${gate}" gateA true 2>&1)"; rc=$?
102check "2 the same gate removes its own dead hold and runs" '[[ ${rc} == 0 ]]'
103check "2 and releases it at exit" '[[ ! -e ${AIENOS_GATE_LOCK} ]]'
104p="$(dead_pid)"
105printf 'aienos qemu-gate gateX start=%s expected_end=%s pid=%s hold=old\n' \
106 "$(iso '-2 hours')" "$(iso '-1 hour')" "${p}" >"${AIENOS_GATE_LOCK}"
107out="$("${gate}" gateA true 2>&1)"; rc=$?
108check "2 dead hold past expected_end is stale for any gate" '[[ ${rc} == 0 && ! -e ${AIENOS_GATE_LOCK} ]]'
109
110# ---- 3: unreadable records are held, never removed
111for rec in "qemu_ck_boot_test 12345" "lane11 qemu_native_nvme_test" "garbage"; do
112 printf '%s\n' "${rec}" >"${AIENOS_GATE_LOCK}"
113 out="$("${gate}" gateA true 2>&1)"; rc=$?
114 check "3 gate lock '${rec}' is held" '[[ ${rc} == 3 ]]'
115 check "3 gate lock '${rec}' is not removed" '[[ "$(cat "${AIENOS_GATE_LOCK}")" == "${rec}" ]]'
116done
117rm -f "${AIENOS_GATE_LOCK}"
118printf 'qemu_ck_boot_test 12345\n' >"${AIENOS_QUIET_FLAG}"
119out="$("${gate}" gateA true 2>&1)"; rc=$?
120check "3 unreadable quiet flag is held" '[[ ${rc} == 3 && -e ${AIENOS_QUIET_FLAG} ]]'
121rm -f "${AIENOS_QUIET_FLAG}"
122
123# ---- 4: quiet flag of a live owner
124printf 'gb10 t4fix measure start=%s expected_end=%s pid=%s hold=q-1\n' \
125 "$(iso 'now')" "$(iso '+20 minutes')" "$$" >"${AIENOS_QUIET_FLAG}"
126before="$(cat "${AIENOS_QUIET_FLAG}")"
127out="$("${gate}" gateA true 2>&1)"; rc=$?
128check "4 gate refuses while a live owner holds the machine" '[[ ${rc} == 3 ]]'
129check "4 refusal names the machine holder" '[[ ${out} == *"gb10 t4fix measure"* ]]'
130check "4 quiet flag unchanged" '[[ "$(cat "${AIENOS_QUIET_FLAG}")" == "${before}" && ! -e ${AIENOS_GATE_LOCK} ]]'
131
132# ---- 5: inside the caller's own quietlock hold
133out="$(QUIETLOCK_HOLD=q-1 "${gate}" gateA true 2>&1)"; rc=$?
134check "5 gate runs inside the caller's quietlock hold" '[[ ${rc} == 0 ]]'
135check "5 quiet flag left alone" '[[ "$(cat "${AIENOS_QUIET_FLAG}")" == "${before}" ]]'
136out="$(QUIETLOCK_HOLD=q-2 "${gate}" gateA true 2>&1)"; rc=$?
137check "5 a different hold id does not count" '[[ ${rc} == 3 ]]'
138rm -f "${AIENOS_QUIET_FLAG}"
139
140# ---- 6: release removes only this run's own record
141"${gate}" gateA sleep 1 >"${tmp}/a6.out" 2>&1 & a=$!
142wait_lock
143other="aienos qemu-gate gateZ start=$(iso now) expected_end=$(iso '+5 minutes') pid=$$ hold=z"
144printf '%s\n' "${other}" >"${AIENOS_GATE_LOCK}"
145wait "${a}"
146check "6 a replaced record is left alone at exit" '[[ "$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)" == "${other}" ]]'
147rm -f "${AIENOS_GATE_LOCK}"
148
149# ---- 7: record format
150"${gate}" gateA sleep 30 >/dev/null 2>&1 & a=$!
151wait_lock
152rec="$(head -n1 "${AIENOS_GATE_LOCK}")"
153check "7 record names holder, start, expected_end, pid and hold" \
154 '[[ ${rec} =~ ^aienos\ qemu-gate\ gateA\ start=[0-9TZ:-]+\ expected_end=[0-9TZ:-]+\ pid=[0-9]+\ .*hold=[A-Za-z0-9._-]+ ]]'
155if command -v quietlock >/dev/null 2>&1; then
156 mkdir -p "${tmp}/ql"
157 cp "${AIENOS_GATE_LOCK}" "${tmp}/ql/.spark-quiet"
158 qout="$(QUIETLOCK_DIR="${tmp}/ql" quietlock check 2>&1)"
159 check "7 quietlock reads the record as alive" '[[ ${qout} == *"pid="[0-9]* && ${qout} == *"alive=yes"* ]]'
160else
161 echo "skip 7 quietlock not installed"
162fi
163kill -TERM "${a}" 2>/dev/null; wait "${a}" 2>/dev/null
164check "7 SIGTERM releases the hold" '[[ ! -e ${AIENOS_GATE_LOCK} ]]'
165
166# ---- 9: review hardening (aienos#281 review)
167# 9a a "*" in a record is a literal token, never a filename glob
168mkdir -p "${tmp}/globdir" && touch "${tmp}/globdir/gateA"
169p="$(dead_pid)"
170printf 'aienos qemu-gate * start=%s expected_end=%s pid=%s hold=g\n' "$(iso '-1 minute')" "$(iso '+30 minutes')" "${p}" >"${AIENOS_GATE_LOCK}"
171before="$(cat "${AIENOS_GATE_LOCK}")"
172out="$(cd "${tmp}/globdir" && "${gate}" gateA true 2>&1)"; rc=$?
173check "9a holder '*' is not read as a filename (dead, before expected_end: held)" \
174 '[[ ${rc} == 3 && "$(cat "${AIENOS_GATE_LOCK}")" == "${before}" ]]'
175rm -f "${AIENOS_GATE_LOCK}"
176# 9b pid reused: the recorded start time differs from the live pid's, so the
177# holder is dead (removed by the same gate name)
178printf 'aienos qemu-gate gateA start=%s expected_end=%s pid=%s pidstart=1 hold=g\n' "$(iso now)" "$(iso '+30 minutes')" "$$" >"${AIENOS_GATE_LOCK}"
179out="$("${gate}" gateA true 2>&1)"; rc=$?
180check "9b live pid with a different start time counts as dead" '[[ ${rc} == 0 && ! -e ${AIENOS_GATE_LOCK} ]]'
181printf 'aienos qemu-gate gateA start=%s expected_end=%s pid=%s pidstart=1 hold=g\n' "$(iso now)" "$(iso '+30 minutes')" "$$" >"${AIENOS_GATE_LOCK}"
182out="$("${gate}" gateB true 2>&1)"; rc=$?
183check "9b and another gate still waits for its expected_end" '[[ ${rc} == 3 && -e ${AIENOS_GATE_LOCK} ]]'
184rm -f "${AIENOS_GATE_LOCK}"
185# 9c no clock or no flock: refuse, write nothing
186mkdir -p "${tmp}/nodate" "${tmp}/noflock"
187for t in bash cat flock mkdir dirname rm sleep true; do ln -sf "$(command -v "${t}")" "${tmp}/nodate/${t}"; done
188for t in bash cat date mkdir dirname rm sleep true; do ln -sf "$(command -v "${t}")" "${tmp}/noflock/${t}"; done
189out="$(PATH="${tmp}/nodate" "${gate}" gateA true 2>&1)"; rc=$?
190check "9c no date: refuses and writes no record" '[[ ${rc} == 3 && ${out} == *"clock"* && ! -e ${AIENOS_GATE_LOCK} ]]'
191out="$(PATH="${tmp}/noflock" "${gate}" gateA true 2>&1)"; rc=$?
192check "9c no flock: refuses by name and writes no record" '[[ ${rc} == 3 && ${out} == *"flock not found"* && ! -e ${AIENOS_GATE_LOCK} ]]'
193
194# ---- 8: no script writes or removes the machine quiet flag
195# trust1_m5_qualify.sh is left out of the grep: it writes and removes a flag
196# only in --self-test, after pointing QUIET_FLAG at a temp file; 8b checks that
197# every reassignment of its QUIET_FLAG is such a temp file or a restore.
198writers="$(grep -n -E '(>|rm -f) *"?\$\{?(quiet_flag|quiet|QUIET_FLAG)\}?"' "${here}"/*.sh 2>/dev/null \
199 | grep -v -E '/(gate_hold_selftest|trust1_m5_qualify)\.sh:' | cut -d: -f1,2 || true)"
200check "8 no script writes or removes the quiet flag" '[[ -z ${writers} ]]'
201[[ -n ${writers} ]] && printf ' %s\n' ${writers}
202reassign="$(grep -n -E '(^|[ ;])QUIET_FLAG=' "${here}/trust1_m5_qualify.sh" \
203 | grep -v -E 'QUIET_FLAG="(\$\{AIENOS_QUIET_FLAG:-\$\{HOME\}/workspace/\.spark-quiet\}|\$\{tmp\}/[A-Za-z0-9_]+|\$\{saved_quiet2?\})"' || true)"
204check "8b trust1_m5_qualify.sh points QUIET_FLAG only at the default, temp files or a restore" '[[ -z ${reassign} ]]'
205[[ -n ${reassign} ]] && printf ' %s\n' "${reassign}"
206
207echo "gate_hold_selftest: ${pass} passed, ${fail} failed"
208[[ ${fail} == 0 ]]