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_store_crash_test.sh

234 lines · 11117 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# System Store v1 over the native NVMe driver: QEMU crash/reboot campaign.
18#
19# Geometry: 4096-byte LBA (the atomic-root qualified namespace). The Store root
20# write is one logical block, satisfying the atomic-root predicate.
21#
22# Chain exercised in-guest: NvmeController -> BoundedNvme -> StoreDeviceAdapter
23# -> Store. For each canonical checkpoint the guest begins generation N+1, the
24# host kills QEMU abruptly at the checkpoint marker, then reboots and reopens
25# from media; recovery must be generation N or N+1 with a valid graph.
26#
27# Test-only. QEMU only. No ADR 0015, recovery, Store-format, NVMe-policy, or
28# Machine 1 changes.
29set -uo pipefail
30
31repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
32cd "${repo_root}"
33
34# rows_all_ok FILE WANT: true only when FILE holds exactly WANT result rows
35# (lines with " -> ") and every one ends in "-> OK". A missing file, a short
36# campaign, an extra row or any BAD row is false, so a loop that never ran
37# (or stopped early) cannot read as PASS.
38rows_all_ok() {
39 local f="$1" want="$2" total=0 ok=0
40 [[ -f "${f}" ]] || { echo " rows: ${f##*/} missing (want ${want})"; return 1; }
41 total=$(grep -c ' -> ' "${f}" || true)
42 ok=$(grep -c ' -> OK$' "${f}" || true)
43 if [[ "${want}" -gt 0 && "${total}" == "${want}" && "${ok}" == "${want}" ]]; then return 0; fi
44 echo " rows: ${f##*/} has ${total} rows, ${ok} OK; want exactly ${want} OK"
45 return 1
46}
47
48# --verdict-self-test: feed the verdict check broken result files (no QEMU,
49# no build) and require FAIL for each, PASS only for the complete table.
50if [[ "${1:-}" == --verdict-self-test ]]; then
51 t="$(mktemp -d)"; st=0
52 want_rows() { # NAME WANT EXPECT(0 pass|1 fail) LINES...
53 local name="$1" want="$2" expect="$3"; shift 3
54 printf '%s' "" > "${t}/r"; for l in "$@"; do printf '%s\n' "${l}" >> "${t}/r"; done
55 local got=0; rows_all_ok "${t}/r" "${want}" >/dev/null || got=1
56 if [[ "${got}" == "${expect}" ]]; then echo "PASS ${name} -> $([ "${got}" = 0 ] && echo PASS || echo FAIL)"
57 else echo "FAIL ${name} gave the wrong verdict"; st=1; fi
58 }
59 want_rows "empty results (campaign never ran)" 3 1
60 want_rows "one row short (campaign stopped early)" 3 1 "a -> OK" "b -> OK"
61 want_rows "one BAD row" 3 1 "a -> OK" "b -> BAD" "c -> OK"
62 want_rows "one extra row" 3 1 "a -> OK" "b -> OK" "c -> OK" "d -> OK"
63 want_rows "zero rows wanted is never a pass" 0 1
64 want_rows "complete, all OK" 3 0 "a -> OK" "b -> OK" "c -> OK"
65 rm -f "${t}/r"; rows_all_ok "${t}/r" 3 >/dev/null && { echo "FAIL missing file read as PASS"; st=1; } || echo "PASS missing results file -> FAIL"
66 rm -rf "${t}"
67 [[ "${st}" == 0 ]] && { echo "STORE_CRASH_VERDICT_SELF_TEST: PASS"; exit 0; }
68 echo "STORE_CRASH_VERDICT_SELF_TEST: FAIL"; exit 1
69fi
70
71code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
72vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
73command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; }
74[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; exit 2; }
75
76boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}"
77img_bytes=67108864
78lba_bytes=4096
79bounds_lba=$(( img_bytes / lba_bytes ))
80cfg_lba=32
81machine="virt,virtualization=on,gic-version=3,iommu=smmuv3"
82target_dir="target/qemu-store-crash"
83
84commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
85tree_dirty=unknown
86if [[ "${commit}" != unknown ]]; then
87 if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then tree_dirty=true; commit="${commit}-dirty"; else tree_dirty=false; fi
88fi
89AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \
90 -p aienos-boot --target aarch64-unknown-uefi --features store-qual --bin aienos-handoff \
91 --target-dir "${target_dir}" || { echo "build failed"; exit 2; }
92cargo build --quiet --release -p aienos-store-tool || { echo "store tool build failed"; exit 2; }
93tool="target/release/aienos-store-tool"
94
95work="$(mktemp -d)"
96qemu_pid=""
97cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null || true; rm -rf "${work}"; }
98trap cleanup EXIT
99mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
100touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
101cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
102
103image="${work}/nvme.img"
104serial="${work}/serial.log"
105
106fresh_image() { rm -f "${image}"; truncate -s "${img_bytes}" "${image}"; }
107
108write_cfg() { # mode settle
109 "${tool}" cfg "${image}" "$(( cfg_lba * lba_bytes ))" "$1" "$2" >/dev/null || { echo "cfg write failed"; exit 2; }
110}
111
112# run_qemu <marker> [kill]
113run_qemu() {
114 cp -f "${vars_fd}" "${work}/vars.fd"
115 rm -f "${serial}"
116 qemu-system-aarch64 \
117 -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
118 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
119 -drive if=pflash,format=raw,file="${work}/vars.fd" \
120 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \
121 -device virtio-blk-pci,drive=esp \
122 -drive if=none,id=nvme0,format=raw,file="${image}" \
123 -device nvme,drive=nvme0,serial=aienos-store-crash,logical_block_size=${lba_bytes},physical_block_size=${lba_bytes} \
124 -display none -nic none -serial file:"${serial}" -no-reboot &
125 qemu_pid=$!
126 local deadline=$(( $(date +%s) + boot_timeout ))
127 while (( $(date +%s) < deadline )); do
128 grep -q -- "$1" "${serial}" 2>/dev/null && break
129 kill -0 "${qemu_pid}" 2>/dev/null || break
130 sleep 0.1
131 done
132 if [[ "${2:-}" == "kill" ]]; then
133 kill -9 "${qemu_pid}" 2>/dev/null || true
134 fi
135 wait "${qemu_pid}" 2>/dev/null || true
136 qemu_pid=""
137}
138
139checkpoints=(before_first_write after_payloads after_catalog after_commit_record \
140 after_first_flush after_superblock_write after_final_flush)
141
142fail=0
143integration_pass=0
144crash_pass=0
145reopen_pass=0
146slot_pass=0
147: > "${work}/results.txt"
148
149# --- integration smoke (no crash): provision, transact, reopen -------------
150fresh_image; write_cfg 1 0
151run_qemu "STORE_CHECKPOINT_QEMU"
152if grep -q "STORE_NVME_INTEGRATION_QEMU: PASS" "${serial}" && \
153 grep -q "STORE_CHECKPOINT_QEMU: PASS" "${serial}"; then
154 integration_pass=1; echo "PASS integration smoke (provision + transact)"
155else
156 echo "FAIL integration smoke"; fail=1
157 grep -E "STORE_NVME_INTEGRATION_QEMU|STORE_CHECKPOINT_QEMU" "${serial}" | sed 's/^/ /'
158fi
159write_cfg 2 0
160run_qemu "STORE_REOPEN_QEMU:"
161if grep -q "STORE_REOPEN_QEMU: PASS" "${serial}"; then echo "PASS reopen smoke"; else echo "FAIL reopen smoke"; fail=1; fi
162
163# --- crash campaign --------------------------------------------------------
164# Per-checkpoint expectation from the implementation: the inactive-superblock
165# write is the durable commit point. Before it, recovery must be N; at it the
166# Store contract permits N or N+1 (emulator durability); after the final flush
167# it must be N+1.
168expect_for() { # checkpoint settle
169 case "$1" in
170 after_superblock_write) echo "N_NP1" ;;
171 after_final_flush) echo "NP1" ;;
172 *) echo "N" ;;
173 esac
174}
175
176for settle in 0 3; do
177 N=$(( 1 + settle )); NP1=$(( 2 + settle ))
178 for cp in "${checkpoints[@]}"; do
179 fresh_image; write_cfg 1 "${settle}"
180 run_qemu "CHECKPOINT: ${cp}" kill
181 saw_cp=0; grep -q -- "CHECKPOINT: ${cp}" "${serial}" && saw_cp=1
182 write_cfg 2 "${settle}"
183 run_qemu "STORE_REOPEN_QEMU:"
184 guest_reopen=0; grep -q "STORE_REOPEN_QEMU: PASS" "${serial}" && guest_reopen=1
185 guest_slot=1
186 if [[ "${settle}" -ge 3 ]]; then
187 guest_slot=0; grep -q "STORE_SLOT_REUSE_QEMU: PASS" "${serial}" && guest_slot=1
188 fi
189 gen="$(grep -oE "STORE_REOPEN_QEMU: generation=[0-9]+" "${serial}" | tail -1 | grep -oE '[0-9]+$')"
190 st="$(grep -oE "STORE_REOPEN_QEMU: generation=[0-9]+ state=[A-Za-z]+" "${serial}" | tail -1 | sed 's/.*state=//')"
191 exp="$(expect_for "${cp}")"
192 gen_ok=0
193 case "${exp}" in
194 N) [[ "${gen}" == "${N}" ]] && gen_ok=1 ;;
195 NP1) [[ "${gen}" == "${NP1}" ]] && gen_ok=1 ;;
196 N_NP1) [[ "${gen}" == "${N}" || "${gen}" == "${NP1}" ]] && gen_ok=1 ;;
197 esac
198 ok=0
199 if [[ "${saw_cp}" == "1" && "${gen_ok}" == "1" && "${guest_reopen}" == "1" && "${guest_slot}" == "1" ]]; then ok=1; fi
200 printf 'settle=%s cp=%-18s saw=%s guest_reopen=%s guest_slot=%s recovered=%s state=%s expect=%s -> %s\n' \
201 "${settle}" "${cp}" "${saw_cp}" "${guest_reopen}" "${guest_slot}" "${gen:-none}" "${st:-none}" "${exp}" \
202 "$([ "${ok}" = 1 ] && echo OK || echo BAD)" >> "${work}/results.txt"
203 if [[ "${ok}" != "1" ]]; then fail=1; fi
204 done
205done
206
207# Exactly one OK row per (settle, checkpoint); a short or empty table is FAIL.
208# Slot reuse is checked on the settle=3 rows: all of them must be present and OK.
209crash_pass=0; reopen_pass=0; slot_pass=0
210if rows_all_ok "${work}/results.txt" $(( 2 * ${#checkpoints[@]} )); then crash_pass=1; reopen_pass=1; else fail=1; fi
211grep '^settle=3 ' "${work}/results.txt" > "${work}/results_settle3.txt" 2>/dev/null || true
212rows_all_ok "${work}/results_settle3.txt" ${#checkpoints[@]} && slot_pass=1
213
214echo "---- crash campaign results ----"
215cat "${work}/results.txt"
216if [[ "${fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
217 echo "---- final serial ----"; cat "${serial}"
218fi
219
220echo
221echo "=== Summary (commit ${commit}, tree_dirty=${tree_dirty}, evidence level: QEMU only) ==="
222[[ "${integration_pass}" == 1 ]] && echo "STORE_NVME_INTEGRATION_QEMU: PASS" || echo "STORE_NVME_INTEGRATION_QEMU: FAIL"
223[[ "${crash_pass}" == 1 && "${fail}" == 0 ]] && echo "STORE_CHECKPOINT_CRASH_QEMU: PASS" || echo "STORE_CHECKPOINT_CRASH_QEMU: FAIL"
224[[ "${reopen_pass}" == 1 && "${fail}" == 0 ]] && echo "STORE_REOPEN_QEMU: PASS" || echo "STORE_REOPEN_QEMU: FAIL"
225[[ "${slot_pass}" == 1 ]] && echo "STORE_SLOT_REUSE_QEMU: PASS" || echo "STORE_SLOT_REUSE_QEMU: FAIL"
226
227if [[ "${fail}" == 0 && "${integration_pass}" == 1 && "${crash_pass}" == 1 && "${reopen_pass}" == 1 && "${slot_pass}" == 1 ]]; then
228 echo "STORE_V1_QEMU: PASS"
229else
230 echo "STORE_V1_QEMU: NOT EARNED"
231 echo "P3_STORE_QEMU: NOT CLAIMED (QEMU campaign; native persistence conditions separate)"
232 exit 1
233fi
234echo "P3_STORE_QEMU: NOT CLAIMED (QEMU campaign; native persistence conditions separate)"