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

344 lines · 16926 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: 512-byte-LBA QEMU qualification.
18#
19# Geometry: 512-byte LBA (the native DGX Spark geometry, AWUPF=0: one logical
20# block is the power-fail atomic unit). A Store root write is 4096 bytes
21# (8 logical blocks), so the device does not promise it lands atomically.
22#
23# Tier 1 STORE_512B_CRASH_OBSERVED_QEMU
24# QEMU is killed (SIGKILL) at each engine checkpoint, then reopened through the
25# real NVMe path. This models a VM/process crash: every write QEMU accepted is
26# in the host file, flushed or not. It is not a power-loss model (volatile
27# cache loss is covered by the #133 atomicity qualification).
28# Tier 2a STORE_512B_ROOT_TEAR_CLOSURE
29# Host proof on the Tier 1 images: the image killed at after_first_flush and
30# the image killed at after_final_flush differ only inside one superblock
31# slot, and every sector-granular tear of that write (all 2^8 subsets)
32# reproduces either the old or the new slot bytes. So on this format a torn
33# root write can only leave a state Tier 1 already booted.
34# Tier 2b STORE_512B_INJECTED_ROOT_RECOVERY_QEMU
35# Deterministic media-corruption injection, not a crash: one defect per case
36# is written into the inactive slot of the after_first_flush image by
37# aienos-store-tool (the kernel's own encoder and CRC), then the guest reopens.
38#
39# Emits STORE_512B_CRASH_RECOVERY_QEMU: PASS only when all tiers pass.
40# Test-only. QEMU only. No ADR 0015, recovery, Store-format, NVMe-policy, or
41# Machine 1 changes. P3_STORE_QEMU and P3_STORE_NATIVE remain unclaimed.
42set -uo pipefail
43
44repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
45cd "${repo_root}"
46
47# rows_all_ok FILE WANT: true only when FILE holds exactly WANT result rows
48# (lines with " -> ") and every one ends in "-> OK". A missing file, a short
49# campaign, an extra row or any BAD row is false, so a loop that never ran
50# (or stopped early) cannot read as PASS.
51rows_all_ok() {
52 local f="$1" want="$2" total=0 ok=0
53 [[ -f "${f}" ]] || { echo " rows: ${f##*/} missing (want ${want})"; return 1; }
54 total=$(grep -c ' -> ' "${f}" || true)
55 ok=$(grep -c ' -> OK$' "${f}" || true)
56 if [[ "${want}" -gt 0 && "${total}" == "${want}" && "${ok}" == "${want}" ]]; then return 0; fi
57 echo " rows: ${f##*/} has ${total} rows, ${ok} OK; want exactly ${want} OK"
58 return 1
59}
60
61# --verdict-self-test: feed the verdict check broken result files (no QEMU,
62# no build) and require FAIL for each, PASS only for the complete table.
63if [[ "${1:-}" == --verdict-self-test ]]; then
64 t="$(mktemp -d)"; st=0
65 want_rows() { # NAME WANT EXPECT(0 pass|1 fail) LINES...
66 local name="$1" want="$2" expect="$3"; shift 3
67 printf '%s' "" > "${t}/r"; for l in "$@"; do printf '%s\n' "${l}" >> "${t}/r"; done
68 local got=0; rows_all_ok "${t}/r" "${want}" >/dev/null || got=1
69 if [[ "${got}" == "${expect}" ]]; then echo "PASS ${name} -> $([ "${got}" = 0 ] && echo PASS || echo FAIL)"
70 else echo "FAIL ${name} gave the wrong verdict"; st=1; fi
71 }
72 want_rows "empty results (campaign never ran)" 3 1
73 want_rows "one row short (campaign stopped early)" 3 1 "a -> OK" "b -> OK"
74 want_rows "one BAD row" 3 1 "a -> OK" "b -> BAD" "c -> OK"
75 want_rows "one extra row" 3 1 "a -> OK" "b -> OK" "c -> OK" "d -> OK"
76 want_rows "zero rows wanted is never a pass" 0 1
77 want_rows "complete, all OK" 3 0 "a -> OK" "b -> OK" "c -> OK"
78 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"
79 rm -rf "${t}"
80 [[ "${st}" == 0 ]] && { echo "STORE_CRASH_VERDICT_SELF_TEST: PASS"; exit 0; }
81 echo "STORE_CRASH_VERDICT_SELF_TEST: FAIL"; exit 1
82fi
83
84code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
85vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
86command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; }
87[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; exit 2; }
88
89boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}"
90img_bytes=67108864
91lba_bytes=512
92cfg_offset=$(( 256 * lba_bytes ))
93store_offset=$(( 512 * lba_bytes ))
94
95machine="virt,virtualization=on,gic-version=3,iommu=smmuv3"
96target_dir="target/qemu-store-512b-crash"
97
98echo "=== Building aienos-handoff (store-qual) and aienos-store-tool ==="
99commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
100tree_dirty=unknown
101if [[ "${commit}" != unknown ]]; then
102 if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then tree_dirty=true; commit="${commit}-dirty"; else tree_dirty=false; fi
103fi
104AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \
105 -p aienos-boot --target aarch64-unknown-uefi --features store-qual --bin aienos-handoff \
106 --target-dir "${target_dir}" || { echo "build failed"; exit 2; }
107cargo build --quiet --release -p aienos-store-tool || { echo "store tool build failed"; exit 2; }
108tool="target/release/aienos-store-tool"
109
110work="$(mktemp -d)"
111qemu_pid=""
112cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null || true; rm -rf "${work}"; }
113trap cleanup EXIT
114mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" "${work}/saved"
115touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
116cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
117
118image="${work}/nvme.img"
119serial="${work}/serial.log"
120
121fresh_image() { rm -f "${image}"; truncate -s "${img_bytes}" "${image}"; }
122write_cfg() { "${tool}" cfg "${image}" "${cfg_offset}" "$1" "$2" >/dev/null || { echo "cfg write failed"; exit 2; }; }
123
124# run_qemu <marker> [kill]
125run_qemu() {
126 cp -f "${vars_fd}" "${work}/vars.fd"
127 rm -f "${serial}"
128 qemu-system-aarch64 \
129 -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
130 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
131 -drive if=pflash,format=raw,file="${work}/vars.fd" \
132 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \
133 -device virtio-blk-pci,drive=esp \
134 -drive if=none,id=nvme0,format=raw,file="${image}" \
135 -device nvme,drive=nvme0,serial=aienos-store-512b-crash,logical_block_size=${lba_bytes},physical_block_size=${lba_bytes} \
136 -display none -nic none -serial file:"${serial}" -no-reboot &
137 qemu_pid=$!
138 local deadline=$(( $(date +%s) + boot_timeout ))
139 while (( $(date +%s) < deadline )); do
140 grep -q -- "$1" "${serial}" 2>/dev/null && break
141 kill -0 "${qemu_pid}" 2>/dev/null || break
142 sleep 0.1
143 done
144 if [[ "${2:-}" == "kill" ]]; then
145 kill -9 "${qemu_pid}" 2>/dev/null || true
146 fi
147 wait "${qemu_pid}" 2>/dev/null || true
148 qemu_pid=""
149}
150
151# Fields of this run's single STORE_REOPEN_512B_QEMU line (serial is per run).
152reopen_fields() {
153 local line
154 line="$(grep -oE "STORE_REOPEN_512B_QEMU: generation=[0-9]+ state=[A-Za-z]+ peer_classification=[A-Za-z]+" "${serial}" | tail -1)"
155 gen="$(sed -nE 's/.*generation=([0-9]+).*/\1/p' <<<"${line}")"
156 st="$(sed -nE 's/.*state=([A-Za-z]+).*/\1/p' <<<"${line}")"
157 pclass="$(sed -nE 's/.*peer_classification=([A-Za-z]+).*/\1/p' <<<"${line}")"
158}
159
160checkpoints=(before_first_write after_payloads after_catalog after_commit_record \
161 after_first_flush after_superblock_write after_final_flush)
162
163fail=0
164integration_pass=0
165reopen_pass=0
166tier1_pass=0
167tier2a_pass=0
168tier2b_pass=0
169: > "${work}/results_tier1.txt"
170: > "${work}/results_tier2b.txt"
171
172# --- integration smoke (no crash): provision, transact, reopen -------------
173echo "=== Integration Smoke (512-byte LBA) ==="
174fresh_image; write_cfg 3 0
175run_qemu "STORE_CHECKPOINT_QEMU"
176if grep -q "STORE_512B_NONATOMIC_QUALIFICATION: ACTIVE" "${serial}" && \
177 grep -q "STORE_NVME_INTEGRATION_QEMU: PASS" "${serial}" && \
178 grep -q "STORE_CHECKPOINT_QEMU: PASS" "${serial}"; then
179 integration_pass=1
180 echo "PASS integration smoke (provision + transact, 512b non-atomic qualification active)"
181else
182 echo "FAIL integration smoke"
183 fail=1
184 grep -E "STORE_512B_NONATOMIC_QUALIFICATION|STORE_NVME_INTEGRATION_QEMU|STORE_CHECKPOINT_QEMU" "${serial}" | sed 's/^/ /'
185fi
186
187write_cfg 4 0
188run_qemu "STORE_REOPEN_QEMU:"
189reopen_fields
190if [[ "${gen}" == 2 && "${st}" == Valid && "${pclass}" == Valid ]] && \
191 grep -q "STORE_REOPEN_QEMU: PASS" "${serial}"; then
192 reopen_pass=1
193 echo "PASS reopen smoke (generation=2 state=Valid peer=Valid)"
194else
195 echo "FAIL reopen smoke"
196 fail=1
197 grep -E "STORE_REOPEN_512B_QEMU|STORE_REOPEN_QEMU" "${serial}" | sed 's/^/ /'
198fi
199
200# --- Tier 1: checkpoint SIGKILL campaign ------------------------------------
201echo
202echo "=== Tier 1: Checkpoint SIGKILL Campaign ==="
203expect_for() { # checkpoint
204 case "$1" in
205 after_superblock_write) echo "N_NP1" ;;
206 after_final_flush) echo "NP1" ;;
207 *) echo "N" ;;
208 esac
209}
210
211for settle in 0 3; do
212 N=$(( 1 + settle )); NP1=$(( 2 + settle ))
213 for cp in "${checkpoints[@]}"; do
214 fresh_image; write_cfg 3 "${settle}"
215 run_qemu "CHECKPOINT: ${cp}" kill
216 saw_cp=0; grep -q -- "CHECKPOINT: ${cp}" "${serial}" && saw_cp=1
217 # Keep the crashed image for Tier 2 before the verify boot touches it.
218 cp "${image}" "${work}/saved/s${settle}_${cp}.img"
219 write_cfg 4 "${settle}"
220 run_qemu "STORE_REOPEN_QEMU:"
221 guest_reopen=0; grep -q "STORE_REOPEN_QEMU: PASS" "${serial}" && guest_reopen=1
222 guest_slot=1
223 if [[ "${settle}" -ge 3 ]]; then
224 guest_slot=0; grep -q "STORE_SLOT_REUSE_QEMU: PASS" "${serial}" && guest_slot=1
225 fi
226 reopen_fields
227 exp="$(expect_for "${cp}")"
228 gen_ok=0
229 case "${exp}" in
230 N) [[ "${gen}" == "${N}" ]] && gen_ok=1 ;;
231 NP1) [[ "${gen}" == "${NP1}" ]] && gen_ok=1 ;;
232 N_NP1) [[ "${gen}" == "${N}" || "${gen}" == "${NP1}" ]] && gen_ok=1 ;;
233 esac
234 ok=0
235 if [[ "${saw_cp}" == 1 && "${gen_ok}" == 1 && "${guest_reopen}" == 1 && "${guest_slot}" == 1 \
236 && "${st}" == Valid ]]; then ok=1; fi
237 printf 'settle=%s cp=%-24s saw=%s reopen=%s slot=%s gen=%s state=%-16s peer=%-13s expect=%-5s -> %s\n' \
238 "${settle}" "${cp}" "${saw_cp}" "${guest_reopen}" "${guest_slot}" "${gen:-none}" "${st:-none}" \
239 "${pclass:-none}" "${exp}" "$([ "${ok}" = 1 ] && echo OK || echo BAD)" >> "${work}/results_tier1.txt"
240 if [[ "${ok}" != 1 ]]; then fail=1; fi
241 done
242done
243cat "${work}/results_tier1.txt"
244# Exactly one OK row per (settle, checkpoint); a short or empty table is FAIL.
245tier1_pass=0
246rows_all_ok "${work}/results_tier1.txt" $(( 2 * ${#checkpoints[@]} )) && tier1_pass=1
247[[ "${tier1_pass}" == 1 ]] || fail=1
248[[ "${tier1_pass}" == 1 ]] && echo "STORE_512B_CRASH_OBSERVED_QEMU: PASS" || echo "STORE_512B_CRASH_OBSERVED_QEMU: FAIL"
249
250# --- Tier 2a: root tear closure on the real Tier 1 images -------------------
251echo
252echo "=== Tier 2a: Root Tear Closure (host proof over Tier 1 images) ==="
253tier2a_pass=1
254for settle in 0 3; do
255 # PASS needs exit 0 AND the tool's own PASS line (an empty run is FAIL).
256 if ! "${tool}" tear-closure "${work}/saved/s${settle}_after_first_flush.img" \
257 "${work}/saved/s${settle}_after_final_flush.img" "${store_offset}" "${lba_bytes}" \
258 > "${work}/tear_s${settle}.txt" 2>&1 \
259 || ! grep -q "^STORE_ROOT_TEAR_CLOSURE: PASS" "${work}/tear_s${settle}.txt"; then
260 sed "s/^/settle=${settle} /" "${work}/tear_s${settle}.txt"
261 tier2a_pass=0; fail=1
262 echo "settle=${settle} STORE_ROOT_TEAR_CLOSURE: FAIL"
263 else
264 sed "s/^/settle=${settle} /" "${work}/tear_s${settle}.txt"
265 fi
266done
267[[ "${tier2a_pass}" == 1 ]] && echo "STORE_512B_ROOT_TEAR_CLOSURE: PASS" || echo "STORE_512B_ROOT_TEAR_CLOSURE: FAIL"
268
269# --- Tier 2b: deterministic injected-root recovery --------------------------
270echo
271echo "=== Tier 2b: Injected Root Recovery (media corruption, one defect per case) ==="
272# Base: settle-0 image killed at after_first_flush (gen 1 in slot 0, gen 2
273# objects durable, slot 1 zero). Source: the real gen-2 superblock from the
274# after_final_flush image. graph_bad_newer uses the after_payloads image,
275# where the gen-2 catalog and commit record were never written.
276base_flush="${work}/saved/s0_after_first_flush.img"
277base_payloads="${work}/saved/s0_after_payloads.img"
278source_root="${work}/saved/s0_after_final_flush.img"
279
280# case base tool-case expected-outcome
281inject_cases=(
282 "bad_crc flush bad_crc degraded:Malformed"
283 "wrong_magic flush wrong_magic degraded:Malformed"
284 "nonzero_reserved flush nonzero_reserved degraded:Malformed"
285 "wrong_slot_id flush wrong_slot_id degraded:Malformed"
286 "region_mismatch flush region_mismatch degraded:Malformed"
287 "seeded_garbage flush seeded_garbage degraded:Malformed"
288 "unsupported_version flush unsupported_version refuse:UnsupportedVersion"
289 "graph_bad_newer payloads new_root degraded:GraphBadNewer"
290 "control_new_root flush new_root valid:gen2"
291)
292
293tier2b_pass=1
294for row in "${inject_cases[@]}"; do
295 read -r name base tcase expect <<<"${row}"
296 if [[ "${base}" == flush ]]; then cp "${base_flush}" "${image}"; else cp "${base_payloads}" "${image}"; fi
297 if ! "${tool}" inject "${image}" "${store_offset}" 1 "${tcase}" "${source_root}" >"${work}/inject.txt" 2>&1; then
298 echo "case=${name} injection refused: $(cat "${work}/inject.txt")" >> "${work}/results_tier2b.txt"
299 tier2b_pass=0; fail=1; continue
300 fi
301 write_cfg 4 0
302 run_qemu "STORE_REOPEN_QEMU:"
303 gen=""; st=""; pclass=""
304 reopen_fields
305 active=0; grep -q "STORE_512B_NONATOMIC_QUALIFICATION: ACTIVE" "${serial}" && active=1
306 ro=0; grep -q "STORE_DEGRADED_READONLY_QEMU: PASS" "${serial}" && ro=1
307 ok=0
308 case "${expect}" in
309 degraded:*)
310 [[ "${active}" == 1 && "${gen}" == 1 && "${st}" == DegradedRecovery && \
311 "${pclass}" == "${expect#degraded:}" && "${ro}" == 1 ]] && \
312 grep -q "STORE_REOPEN_QEMU: PASS" "${serial}" && ok=1 ;;
313 refuse:*)
314 grep -q "STORE_REOPEN_QEMU: FAIL (open ${expect#refuse:})" "${serial}" && [[ -z "${gen}" ]] && ok=1 ;;
315 valid:gen2)
316 [[ "${gen}" == 2 && "${st}" == Valid && "${pclass}" == Valid ]] && \
317 grep -q "STORE_REOPEN_QEMU: PASS" "${serial}" && ok=1 ;;
318 esac
319 printf 'case=%-20s %-32s gen=%s state=%-16s peer=%-13s readonly=%s expect=%-28s -> %s\n' \
320 "${name}" "$(cut -d' ' -f4 "${work}/inject.txt")" "${gen:-none}" "${st:-none}" "${pclass:-none}" "${ro}" \
321 "${expect}" "$([ "${ok}" = 1 ] && echo OK || echo BAD)" >> "${work}/results_tier2b.txt"
322 if [[ "${ok}" != 1 ]]; then tier2b_pass=0; fail=1; fi
323done
324rows_all_ok "${work}/results_tier2b.txt" ${#inject_cases[@]} || { tier2b_pass=0; fail=1; }
325cat "${work}/results_tier2b.txt"
326[[ "${tier2b_pass}" == 1 ]] && echo "STORE_512B_INJECTED_ROOT_RECOVERY_QEMU: PASS" || echo "STORE_512B_INJECTED_ROOT_RECOVERY_QEMU: FAIL"
327
328echo
329echo "=== Final Summary (commit ${commit}, tree_dirty=${tree_dirty}, evidence level: QEMU only) ==="
330[[ "${integration_pass}" == 1 && "${reopen_pass}" == 1 ]] && echo "STORE_NVME_INTEGRATION_512B_QEMU: PASS" || echo "STORE_NVME_INTEGRATION_512B_QEMU: FAIL"
331[[ "${tier1_pass}" == 1 ]] && echo "STORE_512B_CRASH_OBSERVED_QEMU: PASS" || echo "STORE_512B_CRASH_OBSERVED_QEMU: FAIL"
332[[ "${tier2a_pass}" == 1 ]] && echo "STORE_512B_ROOT_TEAR_CLOSURE: PASS" || echo "STORE_512B_ROOT_TEAR_CLOSURE: FAIL"
333[[ "${tier2b_pass}" == 1 ]] && echo "STORE_512B_INJECTED_ROOT_RECOVERY_QEMU: PASS" || echo "STORE_512B_INJECTED_ROOT_RECOVERY_QEMU: FAIL"
334
335if [[ "${fail}" == 0 && "${integration_pass}" == 1 && "${reopen_pass}" == 1 && "${tier1_pass}" == 1 \
336 && "${tier2a_pass}" == 1 && "${tier2b_pass}" == 1 ]]; then
337 echo "STORE_512B_CRASH_RECOVERY_QEMU: PASS"
338else
339 echo "STORE_512B_CRASH_RECOVERY_QEMU: FAIL"
340 exit 1
341fi
342
343echo "P3_STORE_QEMU: NOT CLAIMED (QEMU campaign; native persistence conditions separate)"
344echo "P3_STORE_NATIVE: NOT CLAIMED"