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

230 lines · 10131 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# M4 continuity qualification in QEMU (ADR 0016, Proposed): the same AIEN
18# identity and committed memory survive cold restarts over System Store v1 on
19# the native NVMe driver, and no boot path ever mints a replacement identity.
20#
21# Geometry: 4096-byte LBA (atomic-root qualified namespace). Every boot is a
22# fresh QEMU process (cold restart); only the NVMe image persists.
23#
24# Emits CONTINUITY_QEMU: PASS only when every step passes. QEMU only; no
25# Machine 1 claim. The continuity encodings are format version 0 (Proposed).
26set -uo pipefail
27
28repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
29cd "${repo_root}"
30
31code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
32vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
33command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; }
34[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; exit 2; }
35
36boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}"
37img_bytes=67108864
38lba_bytes=4096
39cfg_offset=$(( 32 * lba_bytes ))
40store_offset=$(( 64 * lba_bytes ))
41machine="virt,virtualization=on,gic-version=3,iommu=smmuv3"
42target_dir="target/qemu-continuity"
43
44echo "=== Building aienos-handoff (continuity-qual) and aienos-store-tool ==="
45commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
46git diff --quiet HEAD 2>/dev/null || commit="${commit}-dirty"
47AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \
48 -p aienos-boot --target aarch64-unknown-uefi --features continuity-qual --bin aienos-handoff \
49 --target-dir "${target_dir}" || { echo "build failed"; exit 2; }
50cargo build --quiet --release -p aienos-store-tool || { echo "store tool build failed"; exit 2; }
51tool="target/release/aienos-store-tool"
52
53work="$(mktemp -d)"
54qemu_pid=""
55cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null || true; rm -rf "${work}"; }
56trap cleanup EXIT
57mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
58touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
59cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
60
61image="${work}/nvme.img"
62serial="${work}/serial.log"
63fail=0
64
65fresh_image() { rm -f "${image}"; truncate -s "${img_bytes}" "${image}"; }
66write_cfg() { "${tool}" cfg "${image}" "${cfg_offset}" "$1" 0 >/dev/null || { echo "cfg write failed"; exit 2; }; }
67digest() { sha256sum "${image}" | cut -d' ' -f1; }
68pass() { echo "PASS $1"; }
69bad() { echo "FAIL $1"; fail=1; }
70
71# run_qemu <marker> [kill]: one cold boot; returns when QEMU exits (or is killed).
72run_qemu() {
73 cp -f "${vars_fd}" "${work}/vars.fd"
74 rm -f "${serial}"
75 qemu-system-aarch64 \
76 -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
77 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
78 -drive if=pflash,format=raw,file="${work}/vars.fd" \
79 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \
80 -device virtio-blk-pci,drive=esp \
81 -drive if=none,id=nvme0,format=raw,file="${image}" \
82 -device nvme,drive=nvme0,serial=aienos-continuity,logical_block_size=${lba_bytes},physical_block_size=${lba_bytes} \
83 -display none -nic none -serial file:"${serial}" -no-reboot &
84 qemu_pid=$!
85 local deadline=$(( $(date +%s) + boot_timeout ))
86 while (( $(date +%s) < deadline )); do
87 grep -q -- "$1" "${serial}" 2>/dev/null && break
88 kill -0 "${qemu_pid}" 2>/dev/null || break
89 sleep 0.1
90 done
91 if [[ "${2:-}" == "kill" ]]; then
92 kill -9 "${qemu_pid}" 2>/dev/null || true
93 fi
94 wait "${qemu_pid}" 2>/dev/null || true
95 qemu_pid=""
96}
97
98# boot_mode <mode>: cfg + cold boot until the guest resets. "Writes nothing"
99# checks hash the image after write_cfg, so only guest writes can change it.
100boot_mode() { write_cfg "$1"; run_qemu "AIENOS_NEVER_PRINTED_MARKER"; }
101
102# cont <WORD>: the last "CONTINUITY: WORD ..." line of this boot (empty if none).
103cont() { tr -d '\r' <"${serial}" | grep -oE "CONTINUITY: $1( .*)?$" | tail -1; }
104field() { sed -nE "s/.* $1=([0-9a-f]+).*/\1/p" <<<"$2"; }
105
106echo "=== 1. Refusals: no boot path mints an identity ==="
107fresh_image
108write_cfg 6; before=$(digest); run_qemu "AIENOS_NEVER_PRINTED_MARKER"; after=$(digest)
109if [[ -n "$(cont 'STOP \(store Unformatted\)')" && "${before}" == "${after}" ]]; then
110 pass "resume on blank media stops (store Unformatted) and writes nothing"
111else
112 bad "resume on blank media: $(tr -d '\r' <"${serial}" | grep 'CONTINUITY:' | head -2)"
113fi
114
115fresh_image
116write_cfg 1; run_qemu "STORE_CHECKPOINT_QEMU" # formatted Store with data, no identity
117grep -q "STORE_CHECKPOINT_QEMU: PASS" "${serial}" || bad "could not build a formatted, unprovisioned store"
118write_cfg 6; before=$(digest); run_qemu "AIENOS_NEVER_PRINTED_MARKER"; after=$(digest)
119if [[ -n "$(cont UNPROVISIONED)" && "${before}" == "${after}" ]]; then
120 pass "resume on a formatted but unprovisioned store stops UNPROVISIONED and writes nothing"
121else
122 bad "unprovisioned resume: $(tr -d '\r' <"${serial}" | grep 'CONTINUITY:' | head -2)"
123fi
124
125echo "=== 2. Provisioning happens once ==="
126fresh_image
127boot_mode 5
128line=$(cont PROVISIONED)
129agent=$(field agent "${line}")
130if [[ ${#agent} == 64 && "${line}" == *"incarnation=1 sequence=1 cortex=0 branches=1 "* ]]; then
131 pass "provisioned agent ${agent:0:16}... (incarnation 1, empty memory, root branch)"
132else
133 bad "provision: ${line:-$(tr -d '\r' <"${serial}" | grep 'CONTINUITY:' | head -2)}"
134fi
135provisioned_img="${work}/provisioned.img"; cp "${image}" "${provisioned_img}"
136
137write_cfg 5; before=$(digest); run_qemu "AIENOS_NEVER_PRINTED_MARKER"; after=$(digest)
138if [[ -n "$(cont 'STOP \(AlreadyProvisioned\)')" && "${before}" == "${after}" ]]; then
139 pass "a second provisioning request is refused and writes nothing"
140else
141 bad "re-provision: $(tr -d '\r' <"${serial}" | grep 'CONTINUITY:' | head -2)"
142fi
143
144cp "${image}" "${work}/keep.img"; fresh_image; boot_mode 5
145other=$(field agent "$(cont PROVISIONED)")
146if [[ ${#other} == 64 && "${other}" != "${agent}" ]]; then
147 pass "independent provisioning draws a different identity from RNDR"
148else
149 bad "second fresh provisioning gave '${other}'"
150fi
151cp "${work}/keep.img" "${image}"
152
153echo "=== 3. Memory and lineage survive cold restarts ==="
154boot_mode 7
155r=$(cont RESUMED); m=$(cont REMEMBERED)
156memory=$(field memory "${m}")
157if [[ "$(field agent "${r}")" == "${agent}" && "${r}" == *"incarnation=2 sequence=2 cortex=0 branches=1 "* && \
158 "$(field agent "${m}")" == "${agent}" && "${m}" == *"incarnation=2 sequence=3 cortex=1 branches=2 "* ]]; then
159 pass "resume + remember: incarnation 2, one Cortex fact and one forked branch committed"
160else
161 bad "remember: resumed='${r}' remembered='${m}'"
162fi
163
164inc=3; seq=4
165for n in 1 2; do
166 boot_mode 6
167 r=$(cont RESUMED)
168 if [[ "$(field agent "${r}")" == "${agent}" && "$(field memory "${r}")" == "${memory}" && \
169 "${r}" == *"incarnation=${inc} sequence=${seq} cortex=1 branches=2 "* ]]; then
170 pass "cold restart ${n}: same agent, same memory ${memory}, same lineage, incarnation ${inc}"
171 else
172 bad "cold restart ${n}: '${r}'"
173 fi
174 inc=$(( inc + 1 )); seq=$(( seq + 1 ))
175done
176base="${work}/remembered.img"; cp "${image}" "${base}"
177
178echo "=== 4. SIGKILL at every Store checkpoint of a continuity commit ==="
179cp "${base}" "${image}"; boot_mode 8
180new_memory=$(field memory "$(cont COMMITTED)")
181[[ -n "${new_memory}" && "${new_memory}" != "${memory}" ]] || bad "reference commit did not produce new memory"
182checkpoints=(before_first_write after_payloads after_catalog after_commit_record \
183 after_first_flush after_superblock_write after_final_flush)
184for cp in "${checkpoints[@]}"; do
185 cp "${base}" "${image}"; write_cfg 8
186 run_qemu "CHECKPOINT: ${cp}" kill
187 saw=0; grep -q -- "CHECKPOINT: ${cp}" "${serial}" && saw=1
188 boot_mode 6
189 r=$(cont RESUMED); got=$(field memory "${r}")
190 case "${cp}" in
191 after_final_flush) want="${new_memory}" ;;
192 after_superblock_write) want="either" ;;
193 *) want="${memory}" ;;
194 esac
195 ok=0
196 if [[ "${saw}" == 1 && "$(field agent "${r}")" == "${agent}" ]]; then
197 if [[ "${want}" == either ]]; then
198 [[ "${got}" == "${memory}" || "${got}" == "${new_memory}" ]] && ok=1
199 else
200 [[ "${got}" == "${want}" ]] && ok=1
201 fi
202 fi
203 state="old"; [[ "${got}" == "${new_memory}" ]] && state="new"
204 if [[ "${ok}" == 1 ]]; then
205 pass "kill at ${cp}: same agent, ${state} memory"
206 else
207 bad "kill at ${cp}: saw=${saw} '${r}'"
208 fi
209done
210
211echo "=== 5. Degraded mount resumes read-only ==="
212cp "${base}" "${image}"
213"${tool}" inject "${image}" "${store_offset}" inactive seeded_garbage >/dev/null || bad "inject failed"
214write_cfg 6; before=$(digest); run_qemu "AIENOS_NEVER_PRINTED_MARKER"; after=$(digest)
215r=$(cont RESUMED_READONLY)
216if [[ "$(field agent "${r}")" == "${agent}" && "$(field memory "${r}")" == "${memory}" && "${before}" == "${after}" ]]; then
217 pass "malformed peer superblock: same agent and memory, read-only, nothing written"
218else
219 bad "degraded: '${r:-$(tr -d '\r' <"${serial}" | grep 'CONTINUITY:' | head -2)}'"
220fi
221
222echo
223echo "=== Summary (commit ${commit}) ==="
224if [[ "${fail}" == 0 ]]; then
225 echo "CONTINUITY_QEMU: PASS"
226else
227 echo "CONTINUITY_QEMU: FAIL"
228 exit 1
229fi
230echo "CONTINUITY_NATIVE: NOT CLAIMED (Machine 1 waits on TRUST-1)"