aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / gate_hold_selftest.sh
| 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. |
| 42 | set -uo pipefail |
| 43 | |
| 44 | here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 45 | lib="${here}/lib_gate_hold.sh" |
| 46 | tmp="$(mktemp -d)" |
| 47 | trap 'rm -rf "${tmp}"' EXIT |
| 48 | export AIENOS_QUIET_FLAG="${tmp}/spark-quiet" |
| 49 | export AIENOS_GATE_LOCK="${tmp}/qemu-gate-lock" |
| 50 | unset QUIETLOCK_HOLD |
| 51 | case "${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 ;; |
| 53 | esac |
| 54 | |
| 55 | pass=0 fail=0 |
| 56 | ok() { pass=$((pass + 1)); echo "ok $1"; } |
| 57 | bad() { fail=$((fail + 1)); echo "FAIL $1"; } |
| 58 | check() { 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. |
| 62 | gate="${tmp}/fake_gate.sh" |
| 63 | cat >"${gate}" <<EOF |
| 64 | #!/usr/bin/env bash |
| 65 | set -euo pipefail |
| 66 | . "${lib}" |
| 67 | name="\$1"; shift |
| 68 | if ! gh_quiet_check || ! gh_lock_take "\${name}" "\${GATE_MINUTES:-30}"; then |
| 69 | echo "NOT_RUN \${gh_why}" |
| 70 | exit 3 |
| 71 | fi |
| 72 | trap gh_lock_release EXIT |
| 73 | "\$@" |
| 74 | EOF |
| 75 | chmod +x "${gate}" |
| 76 | |
| 77 | dead_pid() { sleep 0 & local p=$!; wait "${p}" 2>/dev/null; echo "${p}"; } |
| 78 | iso() { date -u -d "$1" +%Y-%m-%dT%H:%M:%SZ; } |
| 79 | wait_lock() { local i; for i in $(seq 1 100); do [[ -s "${AIENOS_GATE_LOCK}" ]] && return 0; sleep 0.05; done; return 1; } |
| 80 | |
| 81 | if [[ ! -r "${lib}" ]]; then |
| 82 | bad "lib_gate_hold.sh exists" |
| 83 | fi |
| 84 | |
| 85 | # ---- 1: two competing gates |
| 86 | "${gate}" gateA sleep 30 >"${tmp}/a.out" 2>&1 & a=$! |
| 87 | wait_lock |
| 88 | out="$("${gate}" gateB true 2>&1)"; rc=$? |
| 89 | check "1 second gate refuses (exit 3)" '[[ ${rc} == 3 ]]' |
| 90 | check "1 refusal names the holder" '[[ ${out} == *"gateA"* ]]' |
| 91 | out="$("${gate}" gateA true 2>&1)"; rc=$? |
| 92 | check "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 |
| 95 | kill -KILL "${a}" 2>/dev/null; wait "${a}" 2>/dev/null |
| 96 | check "2 SIGKILL leaves the hold behind" '[[ -s ${AIENOS_GATE_LOCK} ]]' |
| 97 | before="$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)" |
| 98 | out="$("${gate}" gateB true 2>&1)"; rc=$? |
| 99 | check "2 another gate does not remove a dead hold before expected_end" \ |
| 100 | '[[ ${rc} == 3 && "$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)" == "${before}" ]]' |
| 101 | out="$("${gate}" gateA true 2>&1)"; rc=$? |
| 102 | check "2 the same gate removes its own dead hold and runs" '[[ ${rc} == 0 ]]' |
| 103 | check "2 and releases it at exit" '[[ ! -e ${AIENOS_GATE_LOCK} ]]' |
| 104 | p="$(dead_pid)" |
| 105 | printf '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}" |
| 107 | out="$("${gate}" gateA true 2>&1)"; rc=$? |
| 108 | check "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 |
| 111 | for 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}" ]]' |
| 116 | done |
| 117 | rm -f "${AIENOS_GATE_LOCK}" |
| 118 | printf 'qemu_ck_boot_test 12345\n' >"${AIENOS_QUIET_FLAG}" |
| 119 | out="$("${gate}" gateA true 2>&1)"; rc=$? |
| 120 | check "3 unreadable quiet flag is held" '[[ ${rc} == 3 && -e ${AIENOS_QUIET_FLAG} ]]' |
| 121 | rm -f "${AIENOS_QUIET_FLAG}" |
| 122 | |
| 123 | # ---- 4: quiet flag of a live owner |
| 124 | printf 'gb10 t4fix measure start=%s expected_end=%s pid=%s hold=q-1\n' \ |
| 125 | "$(iso 'now')" "$(iso '+20 minutes')" "$$" >"${AIENOS_QUIET_FLAG}" |
| 126 | before="$(cat "${AIENOS_QUIET_FLAG}")" |
| 127 | out="$("${gate}" gateA true 2>&1)"; rc=$? |
| 128 | check "4 gate refuses while a live owner holds the machine" '[[ ${rc} == 3 ]]' |
| 129 | check "4 refusal names the machine holder" '[[ ${out} == *"gb10 t4fix measure"* ]]' |
| 130 | check "4 quiet flag unchanged" '[[ "$(cat "${AIENOS_QUIET_FLAG}")" == "${before}" && ! -e ${AIENOS_GATE_LOCK} ]]' |
| 131 | |
| 132 | # ---- 5: inside the caller's own quietlock hold |
| 133 | out="$(QUIETLOCK_HOLD=q-1 "${gate}" gateA true 2>&1)"; rc=$? |
| 134 | check "5 gate runs inside the caller's quietlock hold" '[[ ${rc} == 0 ]]' |
| 135 | check "5 quiet flag left alone" '[[ "$(cat "${AIENOS_QUIET_FLAG}")" == "${before}" ]]' |
| 136 | out="$(QUIETLOCK_HOLD=q-2 "${gate}" gateA true 2>&1)"; rc=$? |
| 137 | check "5 a different hold id does not count" '[[ ${rc} == 3 ]]' |
| 138 | rm -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=$! |
| 142 | wait_lock |
| 143 | other="aienos qemu-gate gateZ start=$(iso now) expected_end=$(iso '+5 minutes') pid=$$ hold=z" |
| 144 | printf '%s\n' "${other}" >"${AIENOS_GATE_LOCK}" |
| 145 | wait "${a}" |
| 146 | check "6 a replaced record is left alone at exit" '[[ "$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)" == "${other}" ]]' |
| 147 | rm -f "${AIENOS_GATE_LOCK}" |
| 148 | |
| 149 | # ---- 7: record format |
| 150 | "${gate}" gateA sleep 30 >/dev/null 2>&1 & a=$! |
| 151 | wait_lock |
| 152 | rec="$(head -n1 "${AIENOS_GATE_LOCK}")" |
| 153 | check "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._-]+ ]]' |
| 155 | if 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"* ]]' |
| 160 | else |
| 161 | echo "skip 7 quietlock not installed" |
| 162 | fi |
| 163 | kill -TERM "${a}" 2>/dev/null; wait "${a}" 2>/dev/null |
| 164 | check "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 |
| 168 | mkdir -p "${tmp}/globdir" && touch "${tmp}/globdir/gateA" |
| 169 | p="$(dead_pid)" |
| 170 | printf 'aienos qemu-gate * start=%s expected_end=%s pid=%s hold=g\n' "$(iso '-1 minute')" "$(iso '+30 minutes')" "${p}" >"${AIENOS_GATE_LOCK}" |
| 171 | before="$(cat "${AIENOS_GATE_LOCK}")" |
| 172 | out="$(cd "${tmp}/globdir" && "${gate}" gateA true 2>&1)"; rc=$? |
| 173 | check "9a holder '*' is not read as a filename (dead, before expected_end: held)" \ |
| 174 | '[[ ${rc} == 3 && "$(cat "${AIENOS_GATE_LOCK}")" == "${before}" ]]' |
| 175 | rm -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) |
| 178 | printf 'aienos qemu-gate gateA start=%s expected_end=%s pid=%s pidstart=1 hold=g\n' "$(iso now)" "$(iso '+30 minutes')" "$$" >"${AIENOS_GATE_LOCK}" |
| 179 | out="$("${gate}" gateA true 2>&1)"; rc=$? |
| 180 | check "9b live pid with a different start time counts as dead" '[[ ${rc} == 0 && ! -e ${AIENOS_GATE_LOCK} ]]' |
| 181 | printf 'aienos qemu-gate gateA start=%s expected_end=%s pid=%s pidstart=1 hold=g\n' "$(iso now)" "$(iso '+30 minutes')" "$$" >"${AIENOS_GATE_LOCK}" |
| 182 | out="$("${gate}" gateB true 2>&1)"; rc=$? |
| 183 | check "9b and another gate still waits for its expected_end" '[[ ${rc} == 3 && -e ${AIENOS_GATE_LOCK} ]]' |
| 184 | rm -f "${AIENOS_GATE_LOCK}" |
| 185 | # 9c no clock or no flock: refuse, write nothing |
| 186 | mkdir -p "${tmp}/nodate" "${tmp}/noflock" |
| 187 | for t in bash cat flock mkdir dirname rm sleep true; do ln -sf "$(command -v "${t}")" "${tmp}/nodate/${t}"; done |
| 188 | for t in bash cat date mkdir dirname rm sleep true; do ln -sf "$(command -v "${t}")" "${tmp}/noflock/${t}"; done |
| 189 | out="$(PATH="${tmp}/nodate" "${gate}" gateA true 2>&1)"; rc=$? |
| 190 | check "9c no date: refuses and writes no record" '[[ ${rc} == 3 && ${out} == *"clock"* && ! -e ${AIENOS_GATE_LOCK} ]]' |
| 191 | out="$(PATH="${tmp}/noflock" "${gate}" gateA true 2>&1)"; rc=$? |
| 192 | check "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. |
| 198 | writers="$(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)" |
| 200 | check "8 no script writes or removes the quiet flag" '[[ -z ${writers} ]]' |
| 201 | [[ -n ${writers} ]] && printf ' %s\n' ${writers} |
| 202 | reassign="$(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)" |
| 204 | check "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 | |
| 207 | echo "gate_hold_selftest: ${pass} passed, ${fail} failed" |
| 208 | [[ ${fail} == 0 ]] |