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 / qemu_ck_store_crash_test.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 | # Store crash gate for the AIENOS C kernel: CK gate M4_STORE_CRASH. |
| 18 | # C-kernel port of the Rust campaigns scripts/qemu_store_crash_test.sh |
| 19 | # (4096-byte LBA) and scripts/qemu_store_512b_crash_test.sh (512-byte LBA). |
| 20 | # QEMU is not hardware: a PASS here qualifies nothing physical. |
| 21 | # |
| 22 | # Images (native/kernel/Makefile): |
| 23 | # - make full default image: every setup and verify boot |
| 24 | # - make full CK_TEST_STORE_CRASH=1 |
| 25 | # TEST-ONLY crash hook image (svc/store_crash.h): |
| 26 | # only the crash boots. Never counts toward a |
| 27 | # PASS of M4_STORE; refused with |
| 28 | # CK_HARDWARE_STAGING and CK_QEMU_UNSAFE_DMA. |
| 29 | # |
| 30 | # Power-cut model. The host writes a crash plan into the last 4096-byte unit |
| 31 | # of the AIENOS partition (the rw-probe scratch unit, restored by the devices |
| 32 | # stage; the kernel sees only that partition, dev/disk_part.h): |
| 33 | # "AIENCRSH v1 cp=<checkpoint> policy=<drop|all|newest|torn>". The TEST image |
| 34 | # then runs the Store on a volatile write cache: nothing reaches the disk until |
| 35 | # the Store flushes. At the planned checkpoint it lands, per policy, none / all |
| 36 | # / only the newest write call / the first half of the blocks of the newest |
| 37 | # write call of the unflushed writes, flushes, prints "store_crash: HALT at" |
| 38 | # and stops; the host kills QEMU (SIGKILL). policy=all is the Rust tier 1 |
| 39 | # model (QEMU killed, every accepted write is in the host file); drop, newest |
| 40 | # and torn model a lost or reordered volatile cache and a root write cut |
| 41 | # between sectors, which the Rust campaign does not boot. |
| 42 | # |
| 43 | # Commit stages of one boot commit (native/store/store_engine.c st_transact and |
| 44 | # store_sealed.c ss_commit_prepared), checkpoint name -> what it models: |
| 45 | # before_first_write .. after_commit_record before the data flush |
| 46 | # after_first_flush after data, before the root |
| 47 | # after_inactive_superblock during/after the root write, |
| 48 | # before the root flush |
| 49 | # after_final_flush, before_anchor root durable, anchor behind |
| 50 | # after_anchor commit and anchor durable |
| 51 | # |
| 52 | # Campaign, per geometry (512 B and 4096 B namespaces, fresh 64 MiB GPT image |
| 53 | # from native/kernel/tools/ck_gpt_image.c, AIENOS partition [9,57) MiB, as in |
| 54 | # scripts/qemu_ck_store_test.sh; all Store offsets below are partition offsets |
| 55 | # plus the partition start) and |
| 56 | # per settle (0: the crash boot formats a blank disk; 3: three default boots |
| 57 | # first, as the Rust settle 3): every checkpoint with policy=all, plus |
| 58 | # after_commit_record/newest and after_inactive_superblock drop/newest/torn. |
| 59 | # Each case: crash boot (TEST image), plan cleared, verify boot (default |
| 60 | # image). N = the generation the crash boot opened (must be 1 + settle). |
| 61 | # The verify boot must pass the M1 checks, exit 0, print "stage store: ok", no |
| 62 | # "store: REFUSED", no reformat, and open: |
| 63 | # N before the root write landed (Rust: "N") |
| 64 | # N + 1 after the final flush (Rust: "NP1"), and for policy=all at |
| 65 | # after_inactive_superblock (QEMU kill: the root write is in the file) |
| 66 | # N or N+1 newest/torn at after_inactive_superblock (Rust: "N_NP1") |
| 67 | # with boot_count and prev_commit of that generation, anchor |
| 68 | # prepared-advance while the anchor is behind (native/store/README.md "Commit |
| 69 | # order"), then commit generation + 1. |
| 70 | # Root tear closure (Rust tier 2a, here on the QEMU images): for settle 3 the |
| 71 | # after_inactive_superblock/all crash changes exactly one superblock slot, |
| 72 | # that slot then holds generation N + 1, and every changed byte lies in the |
| 73 | # slot's first 512 bytes, so any sector-granular tear of the root write |
| 74 | # leaves either the old or the new slot bytes. |
| 75 | # Injected root (Rust tier 2b, one case): that crash image with one byte of the |
| 76 | # new root's CRC flipped must not open generation N + 1: Store refused, nothing |
| 77 | # committed, disk left as found (image sha256 unchanged). |
| 78 | # Hardware refusal: make must refuse CK_TEST_STORE_CRASH with |
| 79 | # CK_HARDWARE_STAGING and with CK_QEMU_UNSAFE_DMA; the default image must |
| 80 | # carry no crash hook string; the TEST image must announce itself. |
| 81 | # Not covered: Rust STORE_SLOT_REUSE_QEMU (the C Store is append-only, no |
| 82 | # reclaim) is printed NOT_RUN and is not part of this gate. |
| 83 | # |
| 84 | # Usage: bash scripts/qemu_ck_store_crash_test.sh the gate |
| 85 | # bash scripts/qemu_ck_store_crash_test.sh --self-test host only, no QEMU: |
| 86 | # judges, expectation table, plan bytes, tear-closure check and |
| 87 | # make refusals against canned inputs and counterexamples |
| 88 | # bash scripts/qemu_ck_store_crash_test.sh --mutant skip_root_flush |
| 89 | # bash scripts/qemu_ck_store_crash_test.sh --mutant accept_bad_root_crc |
| 90 | # builds the TEST-ONLY broken Store (CK_TEST_STORE_CRASH_MUTANT) and |
| 91 | # runs the cases that must catch it, on that image only. Prints |
| 92 | # AIENOS_CK_STORE_CRASH_MUTANT_<NAME>: KILLED (exit 0) when the |
| 93 | # gate's checks FAIL on it, SURVIVED (exit 1) otherwise. Never |
| 94 | # prints the AIENOS_CK_M4_STORE_CRASH line. |
| 95 | # Reads the quiet flag and takes the QEMU gate lock like qemu_ck_store_test.sh |
| 96 | # (not in --self-test). |
| 97 | # Final line (gate): AIENOS_CK_M4_STORE_CRASH: PASS|FAIL|NOT_RUN. |
| 98 | # Exit: 0 PASS, 1 FAIL, 2 missing tools/build failure, 3 quiet flag held. |
| 99 | set -uo pipefail |
| 100 | |
| 101 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 102 | cd "${repo_root}" |
| 103 | |
| 104 | mode=gate; mutant="" |
| 105 | case "${1:-}" in |
| 106 | "") ;; |
| 107 | --self-test) mode=selftest ;; |
| 108 | --mutant) |
| 109 | mode=mutant; mutant="${2:-}" |
| 110 | [[ "${mutant}" == skip_root_flush || "${mutant}" == accept_bad_root_crc ]] \ |
| 111 | || { echo "usage: $0 [--self-test | --mutant skip_root_flush|accept_bad_root_crc]" >&2; exit 2; } ;; |
| 112 | *) echo "usage: $0 [--self-test | --mutant skip_root_flush|accept_bad_root_crc]" >&2; exit 2 ;; |
| 113 | esac |
| 114 | |
| 115 | verdict() { |
| 116 | if [[ "${mode}" == mutant ]]; then |
| 117 | echo "AIENOS_CK_STORE_CRASH_MUTANT_${mutant^^}: $1" |
| 118 | else |
| 119 | echo "AIENOS_CK_M4_STORE_CRASH: $1" |
| 120 | fi |
| 121 | } |
| 122 | |
| 123 | img_bytes=67108864 |
| 124 | unit=4096 |
| 125 | units=$(( img_bytes / unit )) |
| 126 | plan_unit=$(( units - 1 )) |
| 127 | store_off=16384 # 4 anchor units (dev/disk_layout.h; qemu_ck_store_test.sh checks it) |
| 128 | # set_part BS FIRST_LBA LAST_LBA: the AIENOS partition of a GPT image. Sets |
| 129 | # sb_base (image byte offset of the Store region: partition start + store_off) |
| 130 | # and plan_unit (the last 4096-byte unit of the partition). Default: the whole |
| 131 | # image is the partition (self-test synthetic images). |
| 132 | sb_base=${store_off} |
| 133 | set_part() { |
| 134 | sb_base=$(( $2 * $1 + store_off )) |
| 135 | plan_unit=$(( ($3 + 1) * $1 / unit - 1 )) |
| 136 | } |
| 137 | sb_gen_off=56 # superblock generation (native/store/store_engine.c st_open gen_of) |
| 138 | sb_crc_off=168 # SV1_SB_CRC_OFFSET (native/store/store_v1.h) |
| 139 | cps=(before_first_write after_payload_objects after_catalog after_commit_record after_first_flush |
| 140 | after_inactive_superblock after_final_flush before_anchor after_anchor) |
| 141 | # Gate cases beyond "every checkpoint, policy all" (per geometry and settle). |
| 142 | gate_extra=("after_commit_record newest" "after_inactive_superblock drop" "after_inactive_superblock newest" "after_inactive_superblock torn") |
| 143 | gate_geos=(512 4096); gate_settles=(0 3) |
| 144 | # Result rows a complete gate campaign prints: 2 x 2 x (9 + 4) = 52. |
| 145 | gate_rows_want=$(( ${#gate_geos[@]} * ${#gate_settles[@]} * (${#cps[@]} + ${#gate_extra[@]}) )) |
| 146 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 147 | |
| 148 | # rows_all_ok FILE WANT: port of rows_all_ok in scripts/qemu_store_crash_test.sh |
| 149 | # lines 19-31 (main e4f1513 and later). True only when FILE holds exactly WANT |
| 150 | # result rows (lines with " -> ") and every one ends in "-> OK". A missing |
| 151 | # file, an empty or short campaign, an extra row or any BAD row is false, so a |
| 152 | # loop that never ran (or stopped early) cannot read as PASS. |
| 153 | rows_all_ok() { |
| 154 | local f="$1" want="$2" total=0 ok=0 |
| 155 | rows_why="" |
| 156 | [[ -f "${f}" ]] || { rows_why="results file ${f##*/} missing (want ${want} OK rows)"; return 1; } |
| 157 | total=$(grep -c ' -> ' "${f}" || true) |
| 158 | ok=$(grep -c ' -> OK$' "${f}" || true) |
| 159 | if [[ "${want}" -gt 0 && "${total}" == "${want}" && "${ok}" == "${want}" ]]; then return 0; fi |
| 160 | rows_why="${f##*/} has ${total} rows, ${ok} OK; want exactly ${want} OK" |
| 161 | return 1 |
| 162 | } |
| 163 | |
| 164 | # --------------------------------------------------------------- pure logic |
| 165 | # expect_for CHECKPOINT POLICY -> N | NP1 | N_NP1 (generation the verify boot opens) |
| 166 | expect_for() { |
| 167 | case "$1" in |
| 168 | after_inactive_superblock) |
| 169 | case "$2" in all) echo NP1 ;; drop) echo N ;; *) echo N_NP1 ;; esac ;; |
| 170 | after_final_flush|before_anchor|after_anchor) echo NP1 ;; |
| 171 | *) echo N ;; |
| 172 | esac |
| 173 | } |
| 174 | # anchor_ok CHECKPOINT OPENED_GEN N ANCHOR -> 0 if the anchor state fits |
| 175 | anchor_ok() { |
| 176 | local cp="$1" g="$2" n="$3" a="$4" |
| 177 | if [[ "${g}" == "${n}" ]]; then [[ "${a}" == valid-resume || "${a}" == genesis ]]; return; fi |
| 178 | case "${cp}" in |
| 179 | after_anchor) [[ "${a}" == valid-resume ]] ;; |
| 180 | *) [[ "${a}" == prepared-advance ]] ;; |
| 181 | esac |
| 182 | } |
| 183 | |
| 184 | # plan_bytes CP POLICY: the 4096-byte plan unit on stdout. |
| 185 | plan_bytes() { |
| 186 | { printf 'AIENCRSH v1 cp=%s policy=%s\n' "$1" "$2"; head -c "${unit}" /dev/zero; } | head -c "${unit}" |
| 187 | } |
| 188 | write_plan() { plan_bytes "$2" "$3" | dd of="$1" bs="${unit}" seek="${plan_unit}" conv=notrunc status=none; } |
| 189 | clear_plan() { head -c "${unit}" /dev/zero | dd of="$1" bs="${unit}" seek="${plan_unit}" conv=notrunc status=none; } |
| 190 | |
| 191 | # judge_crash SERIAL CP POLICY SETTLE: sets crash_n and crash_why; returns 0 if |
| 192 | # the crash boot halted exactly at CP with the planned policy. |
| 193 | judge_crash() { |
| 194 | local s="$1" cp="$2" pol="$3" settle="$4" re seq want="" c |
| 195 | crash_n=""; crash_why="" |
| 196 | grep -qF "store_crash: TEST-ONLY Store crash hook image (CK_TEST_STORE_CRASH=1)" "${s}" || { crash_why="no TEST-ONLY banner"; return 1; } |
| 197 | grep -qxF "store_crash: plan halt_at=${cp} policy=${pol}; Store runs on a volatile write cache" "${s}" \ |
| 198 | || { crash_why="plan not armed"; return 1; } |
| 199 | re="^store_crash: HALT at ${cp} policy=${pol} generation_open=([0-9]+) target=([0-9]+) pending_writes=[0-9]+ pending_blocks=[0-9]+ landed_blocks=[0-9]+ rc=0 " |
| 200 | local line; line="$(grep -E "^store_crash: HALT at " "${s}" || true)" |
| 201 | [[ "$(grep -c . <<<"${line}")" == 1 && "${line}" =~ ${re} ]] || { crash_why="no single HALT at ${cp} with rc=0"; return 1; } |
| 202 | crash_n="${BASH_REMATCH[1]}" |
| 203 | [[ "${BASH_REMATCH[2]}" == $(( crash_n + 1 )) ]] || { crash_why="target is not generation_open + 1"; return 1; } |
| 204 | [[ "${crash_n}" == $(( 1 + settle )) ]] || { crash_why="opened generation ${crash_n}, expected $(( 1 + settle ))"; return 1; } |
| 205 | seq="$(sed -nE 's/^store_crash: checkpoint ([a-z_]+) generation_open=([0-9]+) .*/\1:\2/p' "${s}" | paste -sd' ' -)" |
| 206 | for c in "${cps[@]}"; do want+="${want:+ }${c}:${crash_n}"; [[ "${c}" == "${cp}" ]] && break; done |
| 207 | [[ "${seq}" == "${want}" ]] || { crash_why="checkpoint order '${seq}', expected '${want}'"; return 1; } |
| 208 | grep -q "^store: committed" "${s}" && { crash_why="Store reported a commit before the halt"; return 1; } |
| 209 | return 0 |
| 210 | } |
| 211 | |
| 212 | # judge_verify SERIAL CP POLICY SETTLE N: sets v_gen and v_why; 0 if the |
| 213 | # verify boot opened the right committed generation and committed on top. |
| 214 | judge_verify() { |
| 215 | local s="$1" cp="$2" pol="$3" settle="$4" n="$5" exp ore cre oline cline g bc prev anc cg cbc |
| 216 | v_gen=""; v_why="" |
| 217 | exp="$(expect_for "${cp}" "${pol}")" |
| 218 | grep -q "^stage store: ok" "${s}" || { v_why="stage store not ok"; return 1; } |
| 219 | grep -q "store: REFUSED" "${s}" && { v_why="Store refused"; return 1; } |
| 220 | grep -q "formatted TEST store" "${s}" && { v_why="Store reformatted after the crash"; return 1; } |
| 221 | grep -q "^store_crash: HALT" "${s}" && { v_why="verify boot halted"; return 1; } |
| 222 | ore='^store: opened generation=([0-9]+) boot_count=([0-9]+) prev_commit=([^ ]+) anchor=([a-z?-]+)$' |
| 223 | cre='^store: committed generation=([0-9]+) boot_count=([0-9]+)$' |
| 224 | oline="$(grep -E "${ore}" "${s}" || true)"; cline="$(grep -E "${cre}" "${s}" || true)" |
| 225 | [[ "$(grep -c . <<<"${oline}")" == 1 && "${oline}" =~ ${ore} ]] || { v_why="no single opened line"; return 1; } |
| 226 | g="${BASH_REMATCH[1]}"; bc="${BASH_REMATCH[2]}"; prev="${BASH_REMATCH[3]}"; anc="${BASH_REMATCH[4]}" |
| 227 | v_gen="${g}" |
| 228 | [[ "$(grep -c . <<<"${cline}")" == 1 && "${cline}" =~ ${cre} ]] || { v_why="no single committed line"; return 1; } |
| 229 | cg="${BASH_REMATCH[1]}"; cbc="${BASH_REMATCH[2]}" |
| 230 | case "${exp}" in |
| 231 | N) [[ "${g}" == "${n}" ]] ;; |
| 232 | NP1) [[ "${g}" == $(( n + 1 )) ]] ;; |
| 233 | N_NP1) [[ "${g}" == "${n}" || "${g}" == $(( n + 1 )) ]] ;; |
| 234 | esac || { v_why="opened generation ${g}, expected ${exp} (N=${n})"; return 1; } |
| 235 | local want_bc=$(( settle + (g == n + 1 ? 1 : 0) )) |
| 236 | [[ "${bc}" == "${want_bc}" ]] || { v_why="boot_count ${bc} at generation ${g}, expected ${want_bc}"; return 1; } |
| 237 | if [[ "${bc}" == 0 ]]; then [[ "${prev}" == none ]]; else [[ "${prev}" == "${commit}" ]]; fi \ |
| 238 | || { v_why="prev_commit ${prev}"; return 1; } |
| 239 | anchor_ok "${cp}" "${g}" "${n}" "${anc}" || { v_why="anchor ${anc} at generation ${g} (cp ${cp})"; return 1; } |
| 240 | [[ "${cg}" == $(( g + 1 )) && "${cbc}" == $(( bc + 1 )) ]] \ |
| 241 | || { v_why="committed generation ${cg} boot_count ${cbc} after opening ${g}/${bc}"; return 1; } |
| 242 | return 0 |
| 243 | } |
| 244 | |
| 245 | # judge_injected SERIAL N: 0 if the bad-CRC root (generation N+1) was not |
| 246 | # opened and nothing was committed (the image hash is checked by the caller). |
| 247 | judge_injected() { |
| 248 | local s="$1" n="$2" |
| 249 | inj_why="" |
| 250 | grep -q "^store: opened generation=$(( n + 1 )) " "${s}" && { inj_why="opened the corrupt root generation $(( n + 1 ))"; return 1; } |
| 251 | grep -q "^store: committed" "${s}" && { inj_why="committed on top of a corrupt root"; return 1; } |
| 252 | grep -q "^store: REFUSED " "${s}" || { inj_why="no Store refusal"; return 1; } |
| 253 | grep -q "disk left as found, not reformatted" "${s}" || { inj_why="no 'disk left as found'"; return 1; } |
| 254 | grep -q "formatted TEST store" "${s}" && { inj_why="reformatted"; return 1; } |
| 255 | return 0 |
| 256 | } |
| 257 | |
| 258 | u64_at() { od --endian=little -An -tu8 -j "$2" -N 8 "$1" | tr -d ' '; } |
| 259 | |
| 260 | # tear_closure BEFORE AFTER N: 0 if exactly one superblock slot changed, it |
| 261 | # holds generation N+1 and every changed byte is in its first 512 bytes. |
| 262 | tear_closure() { |
| 263 | local b="$1" a="$2" n="$3" diffs slots s off |
| 264 | tc_why=""; tc_slot="" |
| 265 | diffs="$(cmp -l "${b}" "${a}" 2>/dev/null | awk -v lo="${sb_base}" -v hi="$(( sb_base + 2 * unit ))" '$1-1 >= lo && $1-1 < hi {print $1-1}')" |
| 266 | [[ -n "${diffs}" ]] || { tc_why="no superblock slot changed"; return 1; } |
| 267 | slots="$(awk -v lo="${sb_base}" -v u="${unit}" '{print int(($1 - lo) / u)}' <<<"${diffs}" | sort -u)" |
| 268 | [[ "$(grep -c . <<<"${slots}")" == 1 ]] || { tc_why="both superblock slots changed"; return 1; } |
| 269 | s="${slots}"; off=$(( sb_base + s * unit )) |
| 270 | awk -v o="${off}" '$1 - o >= 512 {bad=1} END {exit bad}' <<<"${diffs}" \ |
| 271 | || { tc_why="slot ${s} changed beyond its first 512-byte sector"; return 1; } |
| 272 | [[ "$(u64_at "${a}" $(( off + sb_gen_off )))" == $(( n + 1 )) ]] \ |
| 273 | || { tc_why="changed slot ${s} does not hold generation $(( n + 1 ))"; return 1; } |
| 274 | tc_slot="${s}" |
| 275 | return 0 |
| 276 | } |
| 277 | |
| 278 | # make_refusals: the TEST hook must be refused with hardware staging and the |
| 279 | # unsafe DMA build; a mutant needs the hook; unknown mutants refused. Parse |
| 280 | # only (make -n): nothing is built. |
| 281 | make_refusals() { |
| 282 | local out f=0 |
| 283 | mk() { make -n -s -C native/kernel OUT="${1}" full "${@:2}" 2>&1; } |
| 284 | local tmpo; tmpo="$(mktemp -d)" |
| 285 | out="$(mk "${tmpo}" CK_TEST_STORE_CRASH=1 CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS=/dev/null CK_MACHINE_ID=/dev/null)" |
| 286 | grep -q "CK_TEST_STORE_CRASH (TEST-ONLY Store crash hook) cannot be combined with CK_HARDWARE_STAGING" <<<"${out}" \ |
| 287 | && echo "PASS make refuses the crash hook with CK_HARDWARE_STAGING" || { echo "FAIL hardware staging accepted the crash hook"; f=1; } |
| 288 | out="$(mk "${tmpo}" CK_TEST_STORE_CRASH=1 CK_QEMU_UNSAFE_DMA=1)" |
| 289 | grep -q "cannot be combined with CK_QEMU_UNSAFE_DMA" <<<"${out}" \ |
| 290 | && echo "PASS make refuses the crash hook with CK_QEMU_UNSAFE_DMA" || { echo "FAIL unsafe DMA build accepted the crash hook"; f=1; } |
| 291 | out="$(mk "${tmpo}" CK_TEST_STORE_CRASH_MUTANT=skip_root_flush)" |
| 292 | grep -q "CK_TEST_STORE_CRASH_MUTANT needs CK_TEST_STORE_CRASH=1" <<<"${out}" \ |
| 293 | && echo "PASS make refuses a mutant without the TEST hook" || { echo "FAIL mutant built without the TEST hook"; f=1; } |
| 294 | out="$(mk "${tmpo}" CK_TEST_STORE_CRASH=1 CK_TEST_STORE_CRASH_MUTANT=bogus)" |
| 295 | grep -q "CK_TEST_STORE_CRASH_MUTANT must be skip_root_flush or accept_bad_root_crc" <<<"${out}" \ |
| 296 | && echo "PASS make refuses an unknown mutant" || { echo "FAIL unknown mutant accepted"; f=1; } |
| 297 | out="$(mk "${tmpo}" CK_TEST_STORE_CRASH=1)" |
| 298 | grep -qE "^\*\*\*|Error|error:" <<<"${out}" && { echo "FAIL plain TEST crash build refused: $(head -3 <<<"${out}")"; f=1; } \ |
| 299 | || echo "PASS make accepts the plain TEST crash build (control)" |
| 300 | rm -rf "${tmpo}" |
| 301 | return "${f}" |
| 302 | } |
| 303 | |
| 304 | # ----------------------------------------------------------------- self-test |
| 305 | self_test() { |
| 306 | local st=0 t s n |
| 307 | t="$(mktemp -d)"; trap 'rm -rf "${t}"' RETURN |
| 308 | ok() { echo "PASS $*"; } |
| 309 | bad() { echo "FAIL $*"; st=1; } |
| 310 | [[ "$(expect_for after_commit_record all)" == N && "$(expect_for after_first_flush all)" == N \ |
| 311 | && "$(expect_for after_inactive_superblock all)" == NP1 && "$(expect_for after_inactive_superblock drop)" == N \ |
| 312 | && "$(expect_for after_inactive_superblock torn)" == N_NP1 && "$(expect_for after_final_flush all)" == NP1 \ |
| 313 | && "$(expect_for before_anchor all)" == NP1 && "$(expect_for after_anchor all)" == NP1 \ |
| 314 | && "$(expect_for before_first_write newest)" == N ]] && ok "expectation table" || bad "expectation table" |
| 315 | |
| 316 | # plan bytes: exact line, zero padded to one unit |
| 317 | plan_bytes after_catalog torn >"${t}/plan" |
| 318 | [[ "$(stat -c %s "${t}/plan")" == "${unit}" && "$(head -c 40 "${t}/plan")" == "AIENCRSH v1 cp=after_catalog policy=torn" \ |
| 319 | && "$(tail -c +41 "${t}/plan" | tr -d '\0' | od -An -c | tr -d ' ')" == '\n' ]] && ok "plan unit bytes" || bad "plan unit bytes" |
| 320 | truncate -s "${img_bytes}" "${t}/img"; write_plan "${t}/img" after_anchor all |
| 321 | cmp -s <(dd if="${t}/img" bs="${unit}" skip="${plan_unit}" count=1 status=none) <(plan_bytes after_anchor all) \ |
| 322 | && [[ "$(stat -c %s "${t}/img")" == "${img_bytes}" ]] && ok "plan written into the last unit only" || bad "plan placement" |
| 323 | clear_plan "${t}/img"; cmp -s "${t}/img" <(head -c "${img_bytes}" /dev/zero) && ok "plan cleared" || bad "plan clear" |
| 324 | # GPT disks: the plan goes into the last unit of the AIENOS partition |
| 325 | # [9,57) MiB and the Store region starts store_off into it. |
| 326 | ( set_part 512 18432 116735; [[ "${plan_unit}" == 14591 && "${sb_base}" == $(( 9 * 1048576 + store_off )) ]] ) \ |
| 327 | && ( set_part 4096 2304 14591; [[ "${plan_unit}" == 14591 && "${sb_base}" == $(( 9 * 1048576 + store_off )) ]] ) \ |
| 328 | && ok "partition plan unit and Store base (512 B and 4096 B GPT layouts)" || bad "set_part offsets" |
| 329 | |
| 330 | # crash judge |
| 331 | n=4; s="${t}/crash.txt" |
| 332 | { echo "store_crash: TEST-ONLY Store crash hook image (CK_TEST_STORE_CRASH=1); never counts toward a PASS of M4_STORE" |
| 333 | echo "store_crash: plan halt_at=after_catalog policy=newest; Store runs on a volatile write cache" |
| 334 | for c in before_first_write after_payload_objects after_catalog; do echo "store_crash: checkpoint ${c} generation_open=${n} pending_writes=1"; done |
| 335 | echo "store_crash: HALT at after_catalog policy=newest generation_open=${n} target=5 pending_writes=2 pending_blocks=2 landed_blocks=1 rc=0 (power cut: the host kills QEMU now)"; } >"${s}" |
| 336 | judge_crash "${s}" after_catalog newest 3 && ok "crash judge accepts a halt at the planned checkpoint" || bad "crash judge: ${crash_why}" |
| 337 | judge_crash "${s}" after_catalog newest 0 && bad "crash judge accepted the wrong settle" || ok "crash judge refuses N != 1 + settle (${crash_why})" |
| 338 | judge_crash "${s}" after_commit_record newest 3 && bad "crash judge accepted another checkpoint" || ok "crash judge refuses a halt elsewhere (${crash_why})" |
| 339 | grep -v "checkpoint after_payload_objects" "${s}" >"${t}/c2" |
| 340 | judge_crash "${t}/c2" after_catalog newest 3 && bad "crash judge accepted a skipped checkpoint" || ok "crash judge refuses a gap in the checkpoint order (${crash_why})" |
| 341 | sed 's/rc=0/rc=-1/' "${s}" >"${t}/c3" |
| 342 | judge_crash "${t}/c3" after_catalog newest 3 && bad "crash judge accepted rc!=0" || ok "crash judge refuses a failed landing (${crash_why})" |
| 343 | grep -v "TEST-ONLY Store crash hook image" "${s}" >"${t}/c4" |
| 344 | judge_crash "${t}/c4" after_catalog newest 3 && bad "crash judge accepted no banner" || ok "crash judge refuses an image without the banner" |
| 345 | |
| 346 | # verify judge |
| 347 | vf() { # file gen bc prev anchor cgen cbc [extra line] |
| 348 | { echo "stage store: ok"; echo "store: opened generation=$2 boot_count=$3 prev_commit=$4 anchor=$5" |
| 349 | echo "store: committed generation=$6 boot_count=$7"; [[ -z "${8:-}" ]] || echo "$8"; } >"$1"; } |
| 350 | vf "${t}/v1" 4 3 "${commit}" valid-resume 5 4 |
| 351 | judge_verify "${t}/v1" after_catalog all 3 4 && ok "verify judge: N at after_catalog" || bad "verify judge N: ${v_why}" |
| 352 | judge_verify "${t}/v1" after_final_flush all 3 4 && bad "verify judge accepted N where N+1 is required" || ok "verify judge refuses N after the final flush (${v_why})" |
| 353 | vf "${t}/v2" 5 4 "${commit}" prepared-advance 6 5 |
| 354 | judge_verify "${t}/v2" after_final_flush all 3 4 && ok "verify judge: N+1 prepared-advance after the final flush" || bad "verify judge NP1: ${v_why}" |
| 355 | judge_verify "${t}/v2" after_first_flush all 3 4 && bad "verify judge accepted N+1 before the root write" || ok "verify judge refuses N+1 before the root (${v_why})" |
| 356 | judge_verify "${t}/v2" after_inactive_superblock torn 3 4 && ok "verify judge: N+1 allowed for a torn root" || bad "verify judge torn: ${v_why}" |
| 357 | judge_verify "${t}/v2" after_anchor all 3 4 && bad "verify judge accepted prepared-advance after the anchor" || ok "verify judge refuses an anchor behind after_anchor (${v_why})" |
| 358 | vf "${t}/v3" 5 3 "${commit}" prepared-advance 6 4 |
| 359 | judge_verify "${t}/v3" after_final_flush all 3 4 && bad "verify judge accepted a wrong boot_count" || ok "verify judge refuses a wrong boot_count (${v_why})" |
| 360 | vf "${t}/v4" 4 3 "${commit}" valid-resume 5 4 "store: blank disk (all-zero anchor + Store head): formatted TEST store" |
| 361 | judge_verify "${t}/v4" after_catalog all 3 4 && bad "verify judge accepted a reformat" || ok "verify judge refuses a reformat (${v_why})" |
| 362 | vf "${t}/v5" 1 0 none valid-resume 2 1 |
| 363 | judge_verify "${t}/v5" before_first_write all 0 1 && ok "verify judge: settle 0, prev_commit none" || bad "verify judge settle 0: ${v_why}" |
| 364 | vf "${t}/v6" 4 3 "${commit}" valid-resume 6 4 |
| 365 | judge_verify "${t}/v6" after_catalog all 3 4 && bad "verify judge accepted a commit that skipped a generation" || ok "verify judge refuses a skipped commit generation (${v_why})" |
| 366 | { echo "stage store: FAIL"; echo "store: REFUSED proof=commit step=\"ss_transact\" rc=-203 (x); disk left as found, not reformatted"; } >"${t}/v7" |
| 367 | judge_verify "${t}/v7" after_catalog all 3 4 && bad "verify judge accepted a refused Store" || ok "verify judge refuses a refused Store (${v_why})" |
| 368 | |
| 369 | # injected-root judge |
| 370 | judge_injected "${t}/v7" 4 && ok "injected judge: refusal accepted" || bad "injected judge: ${inj_why}" |
| 371 | judge_injected "${t}/v2" 4 && bad "injected judge accepted the corrupt root" || ok "injected judge refuses an opened corrupt root (${inj_why})" |
| 372 | |
| 373 | # tear closure on synthetic images |
| 374 | truncate -s 65536 "${t}/b"; cp "${t}/b" "${t}/a" |
| 375 | printf 'SBMAGIC!' | dd of="${t}/a" bs=1 seek=$(( store_off + unit )) conv=notrunc status=none |
| 376 | printf '\005\0\0\0\0\0\0\0' | dd of="${t}/a" bs=1 seek=$(( store_off + unit + sb_gen_off )) conv=notrunc status=none |
| 377 | tear_closure "${t}/b" "${t}/a" 4 && [[ "${tc_slot}" == 1 ]] && ok "tear closure: one slot, sector 0, generation N+1" || bad "tear closure: ${tc_why}" |
| 378 | tear_closure "${t}/b" "${t}/a" 3 && bad "tear closure accepted the wrong generation" || ok "tear closure refuses the wrong generation (${tc_why})" |
| 379 | cp "${t}/a" "${t}/a2"; printf 'x' | dd of="${t}/a2" bs=1 seek=$(( store_off + unit + 600 )) conv=notrunc status=none |
| 380 | tear_closure "${t}/b" "${t}/a2" 4 && bad "tear closure accepted a change past sector 0" || ok "tear closure refuses a change past sector 0 (${tc_why})" |
| 381 | cp "${t}/a" "${t}/a3"; printf 'x' | dd of="${t}/a3" bs=1 seek=$(( store_off + 3 )) conv=notrunc status=none |
| 382 | tear_closure "${t}/b" "${t}/a3" 4 && bad "tear closure accepted two changed slots" || ok "tear closure refuses two changed slots (${tc_why})" |
| 383 | |
| 384 | # Row tally (mirrors the Rust --verdict-self-test, scripts/qemu_store_crash_test.sh |
| 385 | # lines 33-53): only a complete all-OK table passes. |
| 386 | [[ "${gate_rows_want}" == 52 ]] && ok "gate campaign wants 52 rows (2 geometries x 2 settles x 13 cases)" \ |
| 387 | || bad "gate campaign row count ${gate_rows_want}, expected 52" |
| 388 | want_rows() { # NAME WANT EXPECT(0 pass|1 fail) LINES... |
| 389 | local name="$1" want="$2" expect="$3" got=0; shift 3 |
| 390 | : >"${t}/rows"; for l in "$@"; do printf '%s\n' "${l}" >>"${t}/rows"; done |
| 391 | rows_all_ok "${t}/rows" "${want}" || got=1 |
| 392 | [[ "${got}" == "${expect}" ]] && ok "row tally: ${name} -> $([[ ${got} == 0 ]] && echo PASS || echo FAIL)" \ |
| 393 | || bad "row tally: ${name} gave the wrong verdict" |
| 394 | } |
| 395 | want_rows "empty campaign" 3 1 |
| 396 | want_rows "one row short" 3 1 "a -> OK" "b -> OK" |
| 397 | want_rows "one BAD row" 3 1 "a -> OK" "b -> BAD (x)" "c -> OK" |
| 398 | want_rows "one extra row" 3 1 "a -> OK" "b -> OK" "c -> OK" "d -> OK" |
| 399 | want_rows "zero rows wanted is never a pass" 0 1 |
| 400 | want_rows "complete, all OK" 3 0 "a -> OK" "b -> OK" "c -> OK" |
| 401 | rm -f "${t}/rows"; rows_all_ok "${t}/rows" 3 && bad "row tally: missing file read as PASS" || ok "row tally: missing results file -> FAIL" |
| 402 | # The gate's own row format: a real OK and BAD line from run_case's printf. |
| 403 | printf 'bs=%-4s settle=%s cp=%-26s policy=%-6s saw=%s N=%-2s opened=%-3s expect=%-5s -> %s%s\n' \ |
| 404 | 512 0 after_catalog all 1 1 1 N OK "" 4096 3 after_anchor all 1 4 5 NP1 BAD " (verify: x)" >"${t}/rows" |
| 405 | rows_all_ok "${t}/rows" 2 && bad "row tally: a BAD gate row passed" || ok "row tally: gate-format BAD row -> FAIL (${rows_why})" |
| 406 | rows_all_ok "${t}/rows" 1 && bad "row tally: over-full gate rows passed" || ok "row tally: gate-format extra row -> FAIL" |
| 407 | |
| 408 | if command -v make >/dev/null; then |
| 409 | make_refusals || st=1 |
| 410 | else |
| 411 | bad "make not installed: refusal checks not run" |
| 412 | fi |
| 413 | [[ "${st}" == 0 ]] && echo "CK_STORE_CRASH_SELFTEST: PASS" || echo "CK_STORE_CRASH_SELFTEST: FAIL" |
| 414 | return "${st}" |
| 415 | } |
| 416 | |
| 417 | if [[ "${mode}" == selftest ]]; then |
| 418 | self_test; exit $? |
| 419 | fi |
| 420 | |
| 421 | # ------------------------------------------------------------------ QEMU run |
| 422 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 423 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 424 | command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; verdict NOT_RUN; exit 2; } |
| 425 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; verdict NOT_RUN; exit 2; } |
| 426 | |
| 427 | cross="" |
| 428 | if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi |
| 429 | out="${repo_root}/target/native-kernel" |
| 430 | mk_full() { make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full "$@" >/dev/null; } |
| 431 | if [[ "${mode}" == mutant ]]; then |
| 432 | mk_full CK_TEST_STORE_CRASH=1 CK_TEST_STORE_CRASH_MUTANT="${mutant}" || { echo "mutant build failed"; verdict NOT_RUN; exit 2; } |
| 433 | crash_efi="${out}/full-test-store-crash-mutant-${mutant}/BOOTAA64.EFI" |
| 434 | plain_efi="${crash_efi}" # every boot of a mutant run uses the broken Store |
| 435 | else |
| 436 | mk_full || { echo "build failed"; verdict NOT_RUN; exit 2; } |
| 437 | mk_full CK_TEST_STORE_CRASH=1 || { echo "TEST crash build failed"; verdict NOT_RUN; exit 2; } |
| 438 | crash_efi="${out}/full-test-store-crash/BOOTAA64.EFI" |
| 439 | plain_efi="${out}/full/BOOTAA64.EFI" |
| 440 | fi |
| 441 | # GPT boot disk tool (native/kernel/tools/ck_gpt_image.c): the kernel binds |
| 442 | # NVMe only when it finds exactly one AIENOS partition (dev/disk_part.h). |
| 443 | if ! make -s -C native/kernel OUT="${out}" gpt-image >/dev/null; then |
| 444 | echo "GPT image tool build failed"; verdict NOT_RUN; exit 2 |
| 445 | fi |
| 446 | |
| 447 | # Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): |
| 448 | # scripts/lib_gate_hold.sh (aienos#278). |
| 449 | . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" |
| 450 | if ! gh_quiet_check || ! gh_lock_take "qemu_ck_store_crash_test" "${AIENOS_GATE_MINUTES:-60}"; then |
| 451 | echo "NOT_RUN ${gh_why}" |
| 452 | verdict NOT_RUN |
| 453 | exit 3 |
| 454 | fi |
| 455 | # Release only this run's own gate lock record, at most once. |
| 456 | release_flag() { gh_lock_release; } |
| 457 | top="$(mktemp -d)" |
| 458 | qemu_pid="" |
| 459 | cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null; rm -rf "${top}"; release_flag; } |
| 460 | trap cleanup EXIT |
| 461 | |
| 462 | # shellcheck source=scripts/lib_ck_m1_checks.sh |
| 463 | source "${repo_root}/scripts/lib_ck_m1_checks.sh" |
| 464 | sha() { sha256sum "$1" | cut -d' ' -f1; } |
| 465 | fail=0; m1_fail=0 |
| 466 | |
| 467 | qemu_args() { # efi-dir image bs |
| 468 | local nvme_dev="nvme,drive=nvme0,serial=aienos-store-crash" |
| 469 | [[ "$3" == 512 ]] || nvme_dev="${nvme_dev},logical_block_size=$3,physical_block_size=$3" |
| 470 | qargs=(-M "virt,virtualization=on,gic-version=3,iommu=smmuv3" -accel tcg,thread=single -cpu max -smp 4 -m 2048 |
| 471 | -drive if=pflash,format=raw,readonly=on,file="${code_fd}" |
| 472 | -drive if=pflash,format=raw,file="${work}/vars.fd" |
| 473 | -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" |
| 474 | -device virtio-blk-pci,drive=esp |
| 475 | -drive if=none,id=nvme0,format=raw,file="$2" |
| 476 | -device "${nvme_dev}" |
| 477 | -device ramfb -display none -nic none |
| 478 | -serial file:"${work}/serial.log" -no-reboot) |
| 479 | } |
| 480 | prep() { # name efi |
| 481 | work="${top}/$1" |
| 482 | mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" |
| 483 | touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" |
| 484 | cp "$2" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" |
| 485 | cp "${vars_fd}" "${work}/vars.fd" |
| 486 | } |
| 487 | # boot_full NAME EFI IMAGE BS: a boot that runs to its end (setup/verify). |
| 488 | boot_full() { |
| 489 | prep "$1" "$2"; qemu_args "$1" "$3" "$4" |
| 490 | timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 "${qargs[@]}" |
| 491 | qemu_status=$? |
| 492 | tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" |
| 493 | [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_store_crash_$1.log" |
| 494 | failed=0 |
| 495 | ck_m1_checks >"${work}/m1.txt" |
| 496 | if [[ "${failed}" != 0 || "${qemu_status}" != 0 ]]; then |
| 497 | echo "FAIL boot $1: M1 checks or QEMU exit ${qemu_status}"; grep '^FAIL' "${work}/m1.txt" | sed 's/^/ /' |
| 498 | m1_fail=1; return 1 |
| 499 | fi |
| 500 | if [[ "${mode}" == gate ]] && grep -q "store_crash:" "${work}/serial.txt"; then |
| 501 | echo "FAIL boot $1: the default image printed crash hook lines"; m1_fail=1; return 1 |
| 502 | fi |
| 503 | return 0 |
| 504 | } |
| 505 | # boot_crash NAME EFI IMAGE BS: runs until the HALT line, then SIGKILL. |
| 506 | boot_crash() { |
| 507 | prep "$1" "$2"; qemu_args "$1" "$3" "$4" |
| 508 | : >"${work}/serial.log" |
| 509 | qemu-system-aarch64 "${qargs[@]}" & |
| 510 | qemu_pid=$! |
| 511 | local deadline=$(( $(date +%s) + ${AIENOS_QEMU_TIMEOUT:-180} )) |
| 512 | while (( $(date +%s) < deadline )); do |
| 513 | grep -qE '^store_crash: (HALT at |plan REFUSED)' "${work}/serial.log" 2>/dev/null && break |
| 514 | kill -0 "${qemu_pid}" 2>/dev/null || break |
| 515 | sleep 0.1 |
| 516 | done |
| 517 | kill -9 "${qemu_pid}" 2>/dev/null |
| 518 | wait "${qemu_pid}" 2>/dev/null |
| 519 | qemu_pid="" |
| 520 | tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" |
| 521 | [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_store_crash_$1.log" |
| 522 | } |
| 523 | |
| 524 | results="${top}/results.txt"; : >"${results}" |
| 525 | closure_ok=1; inject_ok=1; inject_ran=0; control_ok=1 |
| 526 | declare -A geo_ok=([512]=1 [4096]=1) |
| 527 | |
| 528 | # run_case BS SETTLE CP POLICY BASE_IMAGE: one crash + verify; leaves the |
| 529 | # crashed image in ${top}/crashed.img. |
| 530 | run_case() { |
| 531 | local bs="$1" settle="$2" cp="$3" pol="$4" base="$5" name="${1}-s${2}-${3}-${4}" img saw=0 ok=0 why="" |
| 532 | img="${top}/case.img"; cp "${base}" "${img}" |
| 533 | write_plan "${img}" "${cp}" "${pol}" |
| 534 | boot_crash "${name}-crash" "${crash_efi}" "${img}" "${bs}" |
| 535 | if judge_crash "${work}/serial.txt" "${cp}" "${pol}" "${settle}"; then saw=1; else why="crash: ${crash_why}"; fi |
| 536 | grep -q "^kernel: alive" "${work}/serial.txt" && grep -qF "aienos_commit: ${commit}" "${work}/serial.txt" \ |
| 537 | || { saw=0; why="crash boot is not this commit's kernel"; } |
| 538 | clear_plan "${img}" |
| 539 | cp "${img}" "${top}/crashed.img" |
| 540 | local n="${crash_n:-0}" |
| 541 | v_gen="" |
| 542 | if boot_full "${name}-verify" "${plain_efi}" "${img}" "${bs}"; then |
| 543 | if [[ "${saw}" == 1 ]]; then |
| 544 | judge_verify "${work}/serial.txt" "${cp}" "${pol}" "${settle}" "${n}" && ok=1 || why="verify: ${v_why}" |
| 545 | fi |
| 546 | else |
| 547 | why="${why:+${why}; }verify boot failed M1/exit" |
| 548 | fi |
| 549 | printf 'bs=%-4s settle=%s cp=%-26s policy=%-6s saw=%s N=%-2s opened=%-3s expect=%-5s -> %s%s\n' \ |
| 550 | "${bs}" "${settle}" "${cp}" "${pol}" "${saw}" "${n}" "${v_gen:-none}" "$(expect_for "${cp}" "${pol}")" \ |
| 551 | "$([[ "${ok}" == 1 ]] && echo OK || echo BAD)" "${why:+ (${why})}" | tee -a "${results}" |
| 552 | case_saw="${saw}"; case_ok="${ok}"; case_n="${n}" |
| 553 | [[ "${ok}" == 1 ]] || { fail=1; geo_ok[${bs}]=0; } |
| 554 | } |
| 555 | |
| 556 | # new_disk BS: the fresh GPT template image for geometry BS (sentinel-a, AIENOS |
| 557 | # all zero, sentinel-b), and set_part from the range the tool WROTE. |
| 558 | new_disk() { |
| 559 | local l re='^gpt_image bs=[0-9]+ blocks=[0-9]+ aienos_first_lba=([0-9]+) aienos_last_lba=([0-9]+)$' |
| 560 | gpt_tmpl="${top}/gpt-$1.img" |
| 561 | l=$("${out}/host/ck_gpt_image" create "${gpt_tmpl}" "$1" $(( img_bytes / 1048576 )) aienos-middle) || l="" |
| 562 | if [[ "${l}" =~ ${re} ]]; then set_part "$1" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"; return 0; fi |
| 563 | echo "FAIL GPT boot disk image not created (bs $1: ${l})"; fail=1; geo_ok[$1]=0; return 1 |
| 564 | } |
| 565 | # setup_image BS SETTLE -> path of an image after SETTLE default-image boots |
| 566 | setup_image() { |
| 567 | local bs="$1" settle="$2" img="${top}/setup-${1}-${2}.img" k |
| 568 | cp "${gpt_tmpl}" "${img}" |
| 569 | for (( k = 1; k <= settle; k++ )); do |
| 570 | if ! boot_full "${bs}-setup${settle}-${k}" "${plain_efi}" "${img}" "${bs}" \ |
| 571 | || ! grep -q "^store: committed generation=$(( k + 1 )) boot_count=${k}$" "${work}/serial.txt"; then |
| 572 | echo "FAIL setup boot ${k} (bs ${bs}) did not commit generation $(( k + 1 ))"; fail=1; geo_ok[${bs}]=0 |
| 573 | fi |
| 574 | done |
| 575 | echo "${img}" |
| 576 | } |
| 577 | |
| 578 | # injected_case BS BASE_SETUP CRASHED N: tear closure + bad-CRC root. |
| 579 | injected_case() { |
| 580 | local bs="$1" before="$2" crashed="$3" n="$4" img off s0 s1 |
| 581 | if tear_closure "${before}" "${crashed}" "${n}"; then |
| 582 | echo "PASS bs=${bs} root tear closure: only slot ${tc_slot} changed, inside its first 512 bytes, generation $(( n + 1 ))" |
| 583 | else |
| 584 | echo "FAIL bs=${bs} root tear closure: ${tc_why}"; closure_ok=0 |
| 585 | return |
| 586 | fi |
| 587 | img="${top}/inject.img"; cp "${crashed}" "${img}" |
| 588 | off=$(( sb_base + tc_slot * unit + sb_crc_off )) |
| 589 | printf "\\$(printf '%03o' $(( $(od -An -tu1 -j "${off}" -N 1 "${img}") ^ 0x01 )))" \ |
| 590 | | dd of="${img}" bs=1 seek="${off}" conv=notrunc status=none |
| 591 | cmp -s "${img}" "${crashed}" && { echo "FAIL bs=${bs} CRC byte flip did not change the image"; inject_ok=0; return; } |
| 592 | s0="$(sha "${img}")" |
| 593 | inject_ran=1 |
| 594 | if boot_full "${bs}-inject-bad-crc" "${plain_efi}" "${img}" "${bs}"; then |
| 595 | s1="$(sha "${img}")" |
| 596 | if judge_injected "${work}/serial.txt" "${n}" && [[ "${s1}" == "${s0}" ]]; then |
| 597 | echo "PASS bs=${bs} root with a bad CRC (generation $(( n + 1 ))) not opened, nothing committed, image unchanged" |
| 598 | else |
| 599 | echo "FAIL bs=${bs} bad-CRC root: ${inj_why:-image changed by the boot}"; inject_ok=0 |
| 600 | grep -E '^store: ' "${work}/serial.txt" | sed 's/^/ /' |
| 601 | fi |
| 602 | else |
| 603 | echo "FAIL bs=${bs} bad-CRC root boot failed M1/exit"; inject_ok=0 |
| 604 | fi |
| 605 | } |
| 606 | |
| 607 | hw_ok=1 |
| 608 | if [[ "${mode}" == gate ]]; then |
| 609 | make_refusals || hw_ok=0 |
| 610 | grep -aqF "store_crash:" "${plain_efi}" && { echo "FAIL default image carries crash hook strings"; hw_ok=0; } \ |
| 611 | || echo "PASS default image carries no crash hook strings" |
| 612 | grep -aqF "TEST-ONLY Store crash hook image" "${crash_efi}" && echo "PASS TEST crash image announces itself" \ |
| 613 | || { echo "FAIL TEST crash image lacks its announcement"; hw_ok=0; } |
| 614 | fi |
| 615 | |
| 616 | mutant_hit=0; mutant_cases=0 |
| 617 | for bs in "${gate_geos[@]}"; do |
| 618 | echo "=== ${bs}-byte LBA ===" |
| 619 | new_disk "${bs}" || continue |
| 620 | for settle in "${gate_settles[@]}"; do |
| 621 | if [[ "${mode}" == mutant && "${mutant}" == accept_bad_root_crc && "${settle}" == 0 ]]; then continue; fi |
| 622 | base="$(setup_image "${bs}" "${settle}")" |
| 623 | plan=() |
| 624 | if [[ "${mode}" == gate ]]; then |
| 625 | for cp in "${cps[@]}"; do plan+=("${cp} all"); done |
| 626 | plan+=("${gate_extra[@]}") |
| 627 | elif [[ "${mutant}" == skip_root_flush ]]; then |
| 628 | plan=("after_inactive_superblock newest" "after_inactive_superblock torn") |
| 629 | else |
| 630 | plan=("after_inactive_superblock all") |
| 631 | fi |
| 632 | for p in "${plan[@]}"; do |
| 633 | read -r cp pol <<<"${p}" |
| 634 | run_case "${bs}" "${settle}" "${cp}" "${pol}" "${base}" |
| 635 | if [[ "${mode}" == mutant ]]; then |
| 636 | mutant_cases=$(( mutant_cases + 1 )) |
| 637 | [[ "${mutant}" == skip_root_flush && "${case_saw}" == 1 && "${case_ok}" == 0 ]] && mutant_hit=1 |
| 638 | # accept_bad_root_crc: the uninjected crash case is the control; it must pass. |
| 639 | [[ "${mutant}" == accept_bad_root_crc && "${case_ok}" != 1 ]] && control_ok=0 |
| 640 | fi |
| 641 | if [[ "${settle}" == 3 && "${cp}" == after_inactive_superblock && "${pol}" == all \ |
| 642 | && ( "${mode}" == gate || "${mutant}" == accept_bad_root_crc ) && "${case_saw}" == 1 ]]; then |
| 643 | injected_case "${bs}" "${base}" "${top}/crashed.img" "${case_n}" |
| 644 | fi |
| 645 | done |
| 646 | done |
| 647 | done |
| 648 | release_flag |
| 649 | |
| 650 | echo "---- crash campaign results ----" |
| 651 | cat "${results}" |
| 652 | if [[ "${fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then |
| 653 | for s in "${top}"/*/serial.txt; do |
| 654 | echo "---- serial $(basename "$(dirname "${s}")") (store lines) ----" |
| 655 | grep -E '^(stage store|store:|store_crash:)' "${s}" | head -30 || true |
| 656 | done |
| 657 | fi |
| 658 | |
| 659 | if [[ "${mode}" == mutant ]]; then |
| 660 | if [[ "${mutant}" == skip_root_flush ]]; then |
| 661 | [[ "${mutant_hit}" == 1 ]] && { verdict "KILLED (a crash case after the root write recovered wrong on the Store without its pre-root flush)"; exit 0; } |
| 662 | verdict "SURVIVED (${mutant_cases} cases, none failed)"; exit 1 |
| 663 | fi |
| 664 | if [[ "${control_ok}" != 1 || "${inject_ran}" != 1 || "${closure_ok}" != 1 ]]; then |
| 665 | verdict "SURVIVED (inconclusive: control crash case or tear closure failed, injection ran=${inject_ran})"; exit 1 |
| 666 | fi |
| 667 | [[ "${inject_ok}" == 0 ]] && { verdict "KILLED (the bad-CRC root was opened)"; exit 0; } |
| 668 | verdict "SURVIVED (bad-CRC root still refused)"; exit 1 |
| 669 | fi |
| 670 | |
| 671 | pf() { [[ "$1" == 1 && "${m1_fail}" == 0 ]] && echo PASS || echo FAIL; } |
| 672 | echo |
| 673 | echo "CK_STORE_CRASH_512B_QEMU: $(pf "${geo_ok[512]}")" |
| 674 | echo "CK_STORE_CRASH_4096_QEMU: $(pf "${geo_ok[4096]}")" |
| 675 | echo "CK_STORE_ROOT_TEAR_CLOSURE_QEMU: $(pf "${closure_ok}")" |
| 676 | echo "CK_STORE_INJECTED_ROOT_QEMU: $(pf "$(( inject_ok & inject_ran ))")" |
| 677 | echo "CK_STORE_CRASH_HOOK_REFUSED_ON_HARDWARE: $(pf "${hw_ok}")" |
| 678 | echo "CK_STORE_SLOT_REUSE: NOT_RUN (MISSING_IMPLEMENTATION: the C Store is append-only, no reclaim/slot reuse)" |
| 679 | [[ "${m1_fail}" != 0 ]] && echo "M1 checks or QEMU exit failed on at least one setup/verify boot: no verdict can pass" |
| 680 | # Complete campaign: exactly gate_rows_want rows, all OK (rows_all_ok, as in |
| 681 | # the Rust scripts); an empty, short or over-full table is FAIL. |
| 682 | rows_ok=0 |
| 683 | if rows_all_ok "${results}" "${gate_rows_want}"; then |
| 684 | rows_ok=1; echo "CK_STORE_CRASH_ROWS: PASS (${gate_rows_want} of ${gate_rows_want} rows OK)" |
| 685 | else |
| 686 | echo "CK_STORE_CRASH_ROWS: FAIL (${rows_why})" |
| 687 | fi |
| 688 | if [[ "${rows_ok}" == 1 && "${fail}" == 0 && "${m1_fail}" == 0 && "${closure_ok}" == 1 && "${inject_ok}" == 1 && "${inject_ran}" == 1 \ |
| 689 | && "${hw_ok}" == 1 && "${geo_ok[512]}" == 1 && "${geo_ok[4096]}" == 1 ]]; then |
| 690 | verdict PASS; exit 0 |
| 691 | fi |
| 692 | verdict FAIL |
| 693 | exit 1 |