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_service_disk_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 | # SERVICE_DISK gate for the AIENOS C kernel (native v1 P06 / gate N3, |
| 18 | # aienos#34): rows 200-211 of native/kernel/GATES.md "SERVICE_DISK". C-only. |
| 19 | # QEMU is not hardware: a PASS here qualifies nothing physical. |
| 20 | # |
| 21 | # The production service image with resident CPU inference, exactly as built |
| 22 | # for the machine (make full CK_SERVICE=1 CK_HARDWARE_STAGING=1 |
| 23 | # CK_OWNER_PUBKEYS= CK_MACHINE_ID= CK_INFER_LIB=, CK_FB_HOLD_S=120), staged by |
| 24 | # scripts/stage_one_time_boot_ck_service.sh onto ONE emulated NVMe disk that |
| 25 | # holds the ESP (image + MODEL.MAP), the model bytes (three out-of-order |
| 26 | # extents) and the all-zero AIENOS partition for the sealed Store. No fw_cfg |
| 27 | # model, no CK_TEST_* flag, no test-keys image: the owner files (a fresh |
| 28 | # Ed25519 public key, a random machine id and Store uuid) and the 32-byte |
| 29 | # recovery secrets are generated for this run and never stored. |
| 30 | # |
| 31 | # bash scripts/qemu_ck_service_disk_test.sh the whole gate (~30-45 min TCG) |
| 32 | # bash scripts/qemu_ck_service_disk_test.sh --build-only rows 200-201 only (make refusals |
| 33 | # and the accepted line, no QEMU, |
| 34 | # no Rust target needed) |
| 35 | # |
| 36 | # row 200 build refusals: each `make` line must FAIL with its message: |
| 37 | # CK_INFER_LIB + CK_HARDWARE_STAGING without CK_SERVICE (the one-shot |
| 38 | # probe); + CK_SERVICE_TEST_KEYS; + CK_TEST_FWCFG_ARTIFACTS; + CK_RUST_LIBS; |
| 39 | # + CK_QEMU_UNSAFE_DMA; + CK_CONSOLE_SESSION; + CK_TEST_STORE_CRASH; |
| 40 | # + CK_TEST_CONTINUITY; staging without owner files |
| 41 | # row 201 the supported line is accepted by make (parse, make -n) |
| 42 | # row 202 the built image (twice, equal digests, by the staging script) carries |
| 43 | # the resident session and the boot-disk source and no fw_cfg code, |
| 44 | # fw_cfg symbol, TEST Store marker or TEST-ONLY banner; the QEMU |
| 45 | # inference images keep their TEST-ONLY fw_cfg announcement |
| 46 | # row 203 staging --dry-run against the scratch image: every write printed with |
| 47 | # its sha256, "nothing touched", the image byte-identical (cmp); |
| 48 | # --apply then changes it (cmp differs: the check is not vacuous) and |
| 49 | # writes exactly the digests the dry run announced |
| 50 | # row 204 boot 1 (blank Store): the stub reads MODEL.MAP from the ESP and the |
| 51 | # model by firmware Block I/O, the kernel recomputes the pinned |
| 52 | # sha256, `infer: resident open`, no fw_cfg line; the Store stage |
| 53 | # refuses the production key source (LOCKED, owner provisioned at |
| 54 | # build time); `unlock <secret>` formats the production Store with |
| 55 | # the recovery keyslot (genesis) and continuity provisions with |
| 56 | # source=operator and the owner root key |
| 57 | # row 205 boot 1: `prompt What is the capital of France?` -> golden reply ids |
| 58 | # -> `approve` commits it; `remember prefer short notes` -> record 1; |
| 59 | # `facts` lists it; evidence counters; stop order closes the session |
| 60 | # (every frame returned) and the Store; QEMU exit 0; M1 checks |
| 61 | # row 206 boot 2 = cold restart of QEMU on the SAME disk image file: a wrong |
| 62 | # secret is refused, the right one opens the Store (no reformat), |
| 63 | # continuity RESUMES the same agent, `recall` returns the approved |
| 64 | # reply and `facts` the fact from the disk (neither retyped), QEMU |
| 65 | # exit 0, M1 checks |
| 66 | # row 207 secret hygiene: neither secret's hex in any serial log; no TEST |
| 67 | # banner or TEST machine id line in any boot |
| 68 | # row 208 negative control A: the same image with MODEL.MAP deleted and the |
| 69 | # model OFFERED on fw_cfg: `model_map: absent`, `infer: ingest REFUSED |
| 70 | # (no boot-disk model`, no fw_cfg ingest; after unlock `prompt` is |
| 71 | # refused ("no inference unit"), evidence inference=none |
| 72 | # row 209 negative control B: MODEL.MAP with a corrupted declared sha256: the |
| 73 | # stub reads the bytes, the kernel refuses to decode (`sha256 |
| 74 | # mismatch`), no resident session, prompt refused |
| 75 | # row 210 limits (declared): QEMU only; the Spark console receive path, the |
| 76 | # AIENOS partition on the Spark NVMe and the owner key ceremony are |
| 77 | # separate items; int8 stays off |
| 78 | # Final line: AIENOS_CK_SERVICE_DISK: PASS|FAIL|NOT_RUN. No Python. |
| 79 | set -euo pipefail |
| 80 | |
| 81 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 82 | cd "${repo_root}" |
| 83 | final_tag="AIENOS_CK_SERVICE_DISK" |
| 84 | build_only=0 |
| 85 | case "${1:-}" in |
| 86 | "") ;; |
| 87 | --build-only) build_only=1 ;; |
| 88 | *) echo "usage: $0 [--build-only]" >&2; exit 2 ;; |
| 89 | esac |
| 90 | |
| 91 | fail=0 |
| 92 | ok() { echo "ROW $1 PASS $2"; } |
| 93 | bad() { echo "ROW $1 FAIL $2"; fail=1; } |
| 94 | cross="" |
| 95 | if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi |
| 96 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 97 | top="$(mktemp -d)" |
| 98 | qemu_pid="" |
| 99 | drain_pid="" |
| 100 | locked=0 |
| 101 | cleanup() { |
| 102 | [[ -z "${qemu_pid}" ]] || kill "${qemu_pid}" 2>/dev/null || true |
| 103 | [[ -z "${drain_pid}" ]] || kill "${drain_pid}" 2>/dev/null || true |
| 104 | exec 4>&- 2>/dev/null || true |
| 105 | rm -rf "${top}" |
| 106 | [[ "${locked}" == 0 ]] || gh_lock_release |
| 107 | } |
| 108 | trap cleanup EXIT |
| 109 | trap 'echo "${final_tag}: FAIL (script stopped at line ${LINENO})"' ERR |
| 110 | |
| 111 | # --- rows 200-201: build refusals and the accepted line (make -n: parse only) --- |
| 112 | fix="${repo_root}/native/kernel/tests/fixtures/owner" |
| 113 | own_fx="${fix}/TEST-FIXTURE-owner-pubkeys.txt" |
| 114 | mach_fx="${fix}/TEST-FIXTURE-machine-id.txt" |
| 115 | lib_any="${top}/libaienos_infer_kernel.a" # make -n never reads it |
| 116 | prod=(full CK_SERVICE=1 CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="${own_fx}" CK_MACHINE_ID="${mach_fx}" CK_INFER_LIB="${lib_any}") |
| 117 | refused() { # label, expected message, make args... |
| 118 | local label="$1" want="$2" msg st; shift 2 |
| 119 | set +e; msg="$(make -s -n -C native/kernel OUT="${top}/refuse" "$@" 2>&1)"; st=$?; set -e |
| 120 | if [[ "${st}" != 0 ]] && grep -qF -- "${want}" <<<"${msg}"; then ok 200 "refused: ${label}" |
| 121 | else bad 200 "NOT refused (status ${st}): ${label}: $(tail -c 200 <<<"${msg}")"; fi |
| 122 | } |
| 123 | refused "inference + staging without CK_SERVICE (one-shot probe)" \ |
| 124 | "CK_INFER_LIB with CK_HARDWARE_STAGING needs CK_SERVICE=1" \ |
| 125 | full CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="${own_fx}" CK_MACHINE_ID="${mach_fx}" CK_INFER_LIB="${lib_any}" |
| 126 | refused "core probe image + staging" "CK_INFER_LIB with CK_HARDWARE_STAGING needs CK_SERVICE=1" \ |
| 127 | CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="${own_fx}" CK_MACHINE_ID="${mach_fx}" CK_INFER_LIB="${lib_any}" |
| 128 | refused "+ CK_SERVICE_TEST_KEYS" "CK_SERVICE_TEST_KEYS (TEST keys) cannot be combined with CK_HARDWARE_STAGING" \ |
| 129 | "${prod[@]}" CK_SERVICE_TEST_KEYS=1 |
| 130 | refused "+ CK_TEST_FWCFG_ARTIFACTS (fw_cfg test source)" \ |
| 131 | "CK_TEST_FWCFG_ARTIFACTS (QEMU fw_cfg test source) cannot be combined with CK_HARDWARE_STAGING" \ |
| 132 | "${prod[@]}" CK_TEST_FWCFG_ARTIFACTS=1 |
| 133 | refused "+ CK_RUST_LIBS" "CK_RUST_LIBS (FP probe, QEMU gate only) cannot be combined with CK_HARDWARE_STAGING" \ |
| 134 | "${prod[@]}" CK_RUST_LIBS="${top}/libfpu.a" |
| 135 | refused "+ CK_QEMU_UNSAFE_DMA" "CK_QEMU_UNSAFE_DMA (unconfined DMA, QEMU debug only) cannot be combined with CK_HARDWARE_STAGING" \ |
| 136 | "${prod[@]}" CK_QEMU_UNSAFE_DMA=1 |
| 137 | refused "+ CK_CONSOLE_SESSION" "CK_CONSOLE_SESSION (QEMU test console session) cannot be combined with CK_HARDWARE_STAGING" \ |
| 138 | "${prod[@]}" CK_CONSOLE_SESSION=1 |
| 139 | refused "+ CK_TEST_STORE_CRASH" "CK_SERVICE cannot be combined with a CK_TEST_* flag" \ |
| 140 | "${prod[@]}" CK_TEST_STORE_CRASH=1 |
| 141 | refused "+ CK_TEST_CONTINUITY" "CK_SERVICE cannot be combined with a CK_TEST_* flag" \ |
| 142 | "${prod[@]}" CK_TEST_CONTINUITY=1 |
| 143 | refused "staging without owner files" "CK_HARDWARE_STAGING refuses the TEST Store keys and TEST machine id" \ |
| 144 | full CK_SERVICE=1 CK_HARDWARE_STAGING=1 CK_INFER_LIB="${lib_any}" |
| 145 | set +e; msg="$(make -s -n -C native/kernel OUT="${top}/accept" "${prod[@]}" 2>&1)"; st=$?; set -e |
| 146 | if [[ "${st}" == 0 ]] && grep -qF -- "-DCK_INFER_DISK_ONLY=1" <<<"${msg}" && grep -qF -- "-DCK_INFER_RESIDENT=1" <<<"${msg}"; then |
| 147 | ok 201 "accepted: make full CK_SERVICE=1 CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS CK_MACHINE_ID CK_INFER_LIB (-DCK_INFER_DISK_ONLY=1 -DCK_INFER_RESIDENT=1)" |
| 148 | else |
| 149 | bad 201 "the supported line was not accepted with the disk-only flags (status ${st}): $(grep -m1 -i error <<<"${msg}" || true)" |
| 150 | fi |
| 151 | set +e; msg="$(make -s -n -C native/kernel OUT="${top}/accept2" full CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 CK_INFER_LIB="${lib_any}" 2>&1)"; st=$?; set -e |
| 152 | if [[ "${st}" == 0 ]] && ! grep -qF -- "-DCK_INFER_DISK_ONLY" <<<"${msg}"; then ok 201 "QEMU test-keys inference image unchanged (accepted, keeps the fw_cfg source)" |
| 153 | else bad 201 "QEMU test-keys inference image changed (status ${st})"; fi |
| 154 | |
| 155 | if [[ "${build_only}" == 1 ]]; then |
| 156 | if [[ "${fail}" == 0 ]]; then echo "${final_tag}_BUILD: PASS (make only; nothing booted)"; exit 0; fi |
| 157 | echo "${final_tag}_BUILD: FAIL"; exit 1 |
| 158 | fi |
| 159 | |
| 160 | # --- prerequisites (NOT_RUN, never a silent pass) --- |
| 161 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 162 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 163 | model="${AIENOS_MODEL:-${HOME}/models/aien-mail/Llama-3.2-1B-Instruct-Q4_K_M.gguf}" |
| 164 | notrun="" |
| 165 | command -v qemu-system-aarch64 >/dev/null || notrun="qemu-system-aarch64 missing" |
| 166 | [[ -n "${notrun}" || ( -r "${code_fd}" && -r "${vars_fd}" ) ]] || notrun="AAVMF missing" |
| 167 | [[ -n "${notrun}" || -r "${model}" ]] || notrun="model file ${model} missing" |
| 168 | for t in cargo openssl mkfs.vfat mcopy mmd mdel; do [[ -n "${notrun}" ]] || command -v "${t}" >/dev/null || notrun="${t} missing"; done |
| 169 | if [[ -z "${notrun}" ]] && { ! rustc --print target-list 2>/dev/null | grep -qx aarch64-unknown-none \ |
| 170 | || ! [[ -d "$(rustc --print sysroot)/lib/rustlib/aarch64-unknown-none/lib" ]]; }; then |
| 171 | notrun="Rust target aarch64-unknown-none not installed" |
| 172 | fi |
| 173 | if [[ -n "${notrun}" ]]; then echo "NOT_RUN ${notrun}"; echo "${final_tag}: NOT_RUN (${notrun})"; exit 2; fi |
| 174 | |
| 175 | . "${repo_root}/scripts/lib_gate_hold.sh" |
| 176 | if ! gh_quiet_check; then echo "NOT_RUN ${gh_why}"; echo "${final_tag}: NOT_RUN (quiet flag held)"; exit 3; fi |
| 177 | if ! gh_lock_take qemu_ck_service_disk_test "${AIENOS_GATE_MINUTES:-120}"; then echo "NOT_RUN ${gh_why}"; echo "${final_tag}: NOT_RUN (QEMU gate lock held)"; exit 3; fi |
| 178 | locked=1 |
| 179 | source "${repo_root}/scripts/lib_ck_m1_checks.sh" |
| 180 | |
| 181 | # --- generated owner files and recovery secrets (this run only) --- |
| 182 | own="${top}/owner" |
| 183 | mkdir -p "${own}" |
| 184 | openssl genpkey -algorithm ed25519 -out "${own}/k.pem" 2>/dev/null |
| 185 | pk="$(openssl pkey -in "${own}/k.pem" -pubout -outform DER | tail -c 32 | od -An -tx1 | tr -d ' \n')" |
| 186 | rm -f "${own}/k.pem" # the private half is never kept: nothing in this gate signs |
| 187 | rnd() { od -An -tx1 -N "$1" /dev/urandom | tr -d ' \n'; } |
| 188 | printf '# owner file generated by scripts/qemu_ck_service_disk_test.sh for one QEMU run (private half discarded)\nowner_root_ed25519 %s\n' "${pk}" >"${own}/owner-pubkeys.txt" |
| 189 | printf '# machine file generated by scripts/qemu_ck_service_disk_test.sh for one QEMU run\nmachine_id %s\nstore_uuid %s\n' "$(rnd 32)" "$(rnd 16)" >"${own}/machine-id.txt" |
| 190 | secret_ok="$(rnd 32)" |
| 191 | secret_bad="$(rnd 32)" |
| 192 | [[ "${secret_ok}" != "${secret_bad}" && ${#secret_ok} == 64 ]] || { echo "${final_tag}: FAIL (secret generation)"; exit 1; } |
| 193 | |
| 194 | # --- scratch disk: ESP + model (3 out-of-order extents) + AIENOS partition --- |
| 195 | make -s -C native/kernel OUT="${top}/host" gpt-image model-map >/dev/null |
| 196 | img="${top}/disk.img" |
| 197 | model_bytes="$(stat -c %s "${model}")" |
| 198 | esp_mib=64 store_mib=64 |
| 199 | total_mib=$(( (model_bytes + 1048575) / 1048576 + esp_mib + store_mib + 16 )) |
| 200 | gpt_out="$("${top}/host/host/ck_gpt_image" create "${img}" 512 "${total_mib}" esp-model-aienos "${esp_mib}" "${model}" "${store_mib}")" |
| 201 | esp_first="$(sed -nE '1s/.* esp_first_lba=([0-9]+).*/\1/p' <<<"${gpt_out}")" |
| 202 | extents="$(sed -nE '1s/.* model_extents=([0-9:,]+).*/\1/p' <<<"${gpt_out}")" |
| 203 | [[ -n "${esp_first}" && -n "${extents}" ]] || { echo "ck_gpt_image esp-model-aienos gave no geometry"; echo "${final_tag}: FAIL"; exit 1; } |
| 204 | "${top}/host/host/ck_gpt_image" find "${img}" 512 | grep -q '^aienos first_lba=' || { echo "kernel GPT parser found no AIENOS partition"; echo "${final_tag}: FAIL"; exit 1; } |
| 205 | mkfs.vfat -F 32 -s 1 -S 512 --offset "${esp_first}" "${img}" $(( esp_mib * 1024 )) >/dev/null 2>&1 |
| 206 | echo "disk: ${img} ${total_mib} MiB, ESP at LBA ${esp_first}, model extents ${extents}, AIENOS ${store_mib} MiB" |
| 207 | |
| 208 | # --- rows 202-203: staging dry run (cmp) then apply --- |
| 209 | stage=(env AIENOS_OWNER_PUBKEYS="${own}/owner-pubkeys.txt" AIENOS_MACHINE_ID="${own}/machine-id.txt" AIENOS_MODEL="${model}" |
| 210 | bash scripts/stage_one_time_boot_ck_service.sh) |
| 211 | dirty=() |
| 212 | [[ -z "$(git status --porcelain)" ]] || dirty=(--allow-dirty) |
| 213 | cp --sparse=always "${img}" "${top}/disk.before" |
| 214 | set +e |
| 215 | "${stage[@]}" --dry-run --target-image "${img}" --extents "${extents}" "${dirty[@]}" >"${top}/dry.txt" 2>&1; st=$? |
| 216 | set -e |
| 217 | if [[ "${st}" == 0 ]] && grep -q '^Dry run: nothing touched. Pre-flight clean' "${top}/dry.txt"; then ok 203 "dry run completed, pre-flight clean"; else bad 203 "dry run status ${st}: $(tail -3 "${top}/dry.txt" | tr '\n' ' ')"; fi |
| 218 | n_writes="$(grep -cE '^write ESP:/EFI/(AIENOS|BOOT)/[A-Za-z0-9._-]+ bytes=[0-9]+ sha256=[0-9a-f]{64}$' "${top}/dry.txt" || true)" |
| 219 | if [[ "${n_writes}" == 4 ]]; then ok 203 "dry run printed 4 writes, each with bytes and sha256"; else bad 203 "dry run printed ${n_writes} well-formed writes (want 4)"; fi |
| 220 | if cmp -s "${img}" "${top}/disk.before"; then ok 203 "dry run left the scratch image byte-identical (cmp)"; else bad 203 "dry run CHANGED the scratch image"; fi |
| 221 | grep -E '^(image sha256|write )' "${top}/dry.txt" | sed 's/^/ dry: /' |
| 222 | set +e |
| 223 | "${stage[@]}" --apply --target-image "${img}" --extents "${extents}" "${dirty[@]}" >"${top}/apply.txt" 2>&1; st=$? |
| 224 | set -e |
| 225 | if [[ "${st}" == 0 ]] && grep -q '^staged: 4 files written to the ESP' "${top}/apply.txt"; then ok 203 "apply staged 4 files and read them back equal"; else bad 203 "apply status ${st}: $(tail -3 "${top}/apply.txt" | tr '\n' ' ')"; fi |
| 226 | if cmp -s "${img}" "${top}/disk.before"; then bad 203 "apply left the image unchanged (the cmp check would be vacuous)"; else ok 203 "apply changed the image (the dry-run cmp is not vacuous)"; fi |
| 227 | rm -f "${top}/disk.before" |
| 228 | for f in aienos-ck-service.efi MODEL.MAP; do |
| 229 | d="$(grep -E "^write ESP:/EFI/AIENOS/${f} " "${top}/dry.txt" | sed -nE 's/.* sha256=([0-9a-f]{64})$/\1/p')" |
| 230 | a="$(grep -E "^write ESP:/EFI/AIENOS/${f} " "${top}/apply.txt" | sed -nE 's/.* sha256=([0-9a-f]{64})$/\1/p')" |
| 231 | if [[ -n "${d}" && "${d}" == "${a}" ]]; then ok 203 "${f}: apply wrote the digest the dry run announced (${d:0:16})"; else bad 203 "${f}: dry ${d:-none} vs apply ${a:-none}"; fi |
| 232 | done |
| 233 | mt="${img}@@$(( esp_first * 512 ))" |
| 234 | staged_efi="${top}/staged.efi" |
| 235 | mcopy -n -i "${mt}" ::/EFI/BOOT/BOOTAA64.EFI "${staged_efi}" |
| 236 | if grep -q "build A and build B equal" "${top}/apply.txt"; then ok 202 "built twice in clean folders, equal digests"; else bad 202 "no reproducibility line from the staging script"; fi |
| 237 | if grep -aqF "infer: resident open" "${staged_efi}" && grep -aqF "infer_source_mode: boot-disk model" "${staged_efi}"; then ok 202 "staged image carries the resident session and the boot-disk source"; else bad 202 "staged image lacks the resident session or the boot-disk source"; fi |
| 238 | s="$(grep -aoE "fw_cfg|opt/aienos/model|QEMU0002|TEST-ONLY|TEST Store|TEST machine id|AIEN-TEST-BOOT01" "${staged_efi}" | sort -u | tr '\n' ' ' || true)" |
| 239 | if [[ -z "${s}" ]]; then ok 202 "staged image carries no fw_cfg, TEST-ONLY or TEST Store marker"; else bad 202 "staged image carries: ${s}"; fi |
| 240 | staged_elf="${repo_root}/target/native-kernel-service-staging/full-service-hardware-staging/aienos-ck.elf" |
| 241 | y="$("${cross}nm" "${staged_elf}" | grep -E ' (fw_select|fw_read|fw_find|fw_dma_read|fwcfg_from_dsdt|ck_store_test_keys|ck_store_test_uuid)$' | tr '\n' ' ' || true)" |
| 242 | if [[ -z "${y}" ]]; then ok 202 "no fw_cfg or TEST key symbol in the ELF"; else bad 202 "symbols present: ${y}"; fi |
| 243 | |
| 244 | # --- QEMU: one NVMe disk (ESP + model + Store), PL011 serial from outside --- |
| 245 | ser_line() { printf '%s\r' "$1" >&4; sleep 1.2; } |
| 246 | serial_has() { tr -d '\r' 2>/dev/null <"${work}/serial.log" | grep -qE -- "$1"; } |
| 247 | wait_for() { local deadline=$(( $(date +%s) + $1 )); shift |
| 248 | while (( $(date +%s) < deadline )); do for p in "$@"; do serial_has "${p}" && return 0; done |
| 249 | kill -0 "${qemu_pid}" 2>/dev/null || break; sleep 1; done |
| 250 | for p in "$@"; do serial_has "${p}" && return 0; done; return 1; } |
| 251 | ser_wait() { wait_for "${2:-300}" "$1" '^report_kind: (panic|fault|final)' || true; } |
| 252 | has() { grep -qE -- "$1" "${work}/serial.txt"; } |
| 253 | rcheck() { if has "$3"; then ok "$1" "$2"; else bad "$1" "$2"; fi; } |
| 254 | rabsent() { if has "$3"; then bad "$1" "$2 (present)"; else ok "$1" "$2"; fi; } |
| 255 | line_of() { { grep -nE -m1 -- "$1" "${work}/serial.txt" || true; } | cut -d: -f1; } |
| 256 | before() { local a b; a="$(line_of "$3")"; b="$(line_of "$4")" |
| 257 | if [[ -n "${a}" && -n "${b}" && "${a}" -lt "${b}" ]]; then ok "$1" "$2 (lines ${a} < ${b})"; else bad "$1" "$2 (lines ${a:-none} ${b:-none})"; fi; } |
| 258 | boot_to="${AIENOS_QEMU_INFER_TIMEOUT:-2700}" |
| 259 | cp "${vars_fd}" "${top}/vars.fd" # one machine: the same NVRAM file for both main boots |
| 260 | boot() { # name disk-image driver [extra qemu args...] |
| 261 | local name="$1" disk="$2" drv="$3"; shift 3 |
| 262 | work="${top}/${name}" |
| 263 | mkdir -p "${work}" |
| 264 | mkfifo "${work}/ser.in" "${work}/ser.out" |
| 265 | : >"${work}/serial.log" |
| 266 | local vars="${BOOT_VARS:-${top}/vars.fd}" |
| 267 | timeout "${boot_to}" qemu-system-aarch64 \ |
| 268 | -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 4096 \ |
| 269 | -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ |
| 270 | -drive if=pflash,format=raw,file="${vars}" \ |
| 271 | -drive if=none,id=nvme0,format=raw,file="${disk}" \ |
| 272 | -device nvme,drive=nvme0,serial=aienos-service-disk \ |
| 273 | -device ramfb -display none -nic none -monitor none \ |
| 274 | -chardev pipe,id=ser,path="${work}/ser" -serial chardev:ser -no-reboot "$@" 2>"${work}/qemu.err" & |
| 275 | qemu_pid=$! |
| 276 | cat "${work}/ser.out" >>"${work}/serial.log" & |
| 277 | drain_pid=$! |
| 278 | exec 4>"${work}/ser.in" |
| 279 | local t0; t0=$(date +%s) |
| 280 | if wait_for "${boot_to}" '^lifecycle: ready state=' '^report_kind: (panic|fault|final)' && serial_has '^lifecycle: ready state='; then |
| 281 | sleep 1 |
| 282 | "${drv}" |
| 283 | fi |
| 284 | wait_for 600 '^report_kind: (panic|fault|final)' || true |
| 285 | set +e; wait "${qemu_pid}"; qemu_status=$?; set -e |
| 286 | qemu_pid="" |
| 287 | exec 4>&- 2>/dev/null || true |
| 288 | wait "${drain_pid}" 2>/dev/null || true |
| 289 | drain_pid="" |
| 290 | tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" |
| 291 | [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_service_disk_${name}.log" |
| 292 | echo "== boot $(basename "${work}"): qemu exit ${qemu_status} after $(( $(date +%s) - t0 )) s" |
| 293 | } |
| 294 | m1() { |
| 295 | failed=0 |
| 296 | ck_m1_checks >"${work}/m1.txt" 2>&1 || true |
| 297 | if [[ "${failed}" == 0 ]]; then ok "$1" "M1 checks"; else bad "$1" "M1 checks: $(grep -E "^FAIL" "${work}/m1.txt" | head -3 | tr "\n" " ")"; fi |
| 298 | } |
| 299 | infer_wait="${AIENOS_QEMU_INFER_WAIT:-1800}" |
| 300 | drive_main1() { |
| 301 | ser_line status |
| 302 | ser_line "unlock ${secret_ok}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' |
| 303 | ser_line status |
| 304 | ser_line "prompt What is the capital of France?" |
| 305 | wait_for "${infer_wait}" '^lifecycle: (reply pending|generation cancelled|REFUSED verb=prompt)' '^report_kind: (panic|fault|final)' || true |
| 306 | ser_line approve; ser_wait '^lifecycle: (approve committed|REFUSED verb=approve)' |
| 307 | ser_line "remember prefer short notes"; ser_wait '^lifecycle: (remembered|REFUSED verb=remember)' |
| 308 | ser_line facts; ser_wait '^lifecycle: (facts records=|facts none|REFUSED verb=facts)' |
| 309 | ser_line evidence |
| 310 | ser_line shutdown |
| 311 | } |
| 312 | drive_main2() { |
| 313 | ser_line "unlock ${secret_bad}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' |
| 314 | ser_line "unlock ${secret_ok}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' |
| 315 | ser_line status |
| 316 | ser_line recall; ser_wait '^lifecycle: recall' |
| 317 | ser_line facts; ser_wait '^lifecycle: (facts records=|facts none|REFUSED verb=facts)' |
| 318 | ser_line evidence |
| 319 | ser_line shutdown |
| 320 | } |
| 321 | drive_neg() { |
| 322 | ser_line "unlock ${secret_ok}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' |
| 323 | ser_line "prompt What is the capital of France?"; ser_wait '^lifecycle: (REFUSED verb=prompt|reply pending|generating)' |
| 324 | ser_line evidence |
| 325 | ser_line shutdown |
| 326 | } |
| 327 | re_open='^infer: resident open rc=0 tensors=147 vocab=128256 layers=16 kv_capacity=1024 max_new=256 max_prompt_bytes=752 heap_reserve_kib=64 heap_used_kib=[0-9]+ open_us=[0-9]+$' |
| 328 | re_recall='^lifecycle: recall gen=4 kind=reply id=[0-9a-f]{8} bytes=[0-9]+ text="aienos\.reply\.v1.prompt: What is the capital of France\?.reply: The capital of France is Paris\.' |
| 329 | frames_kept() { |
| 330 | local l b a; l="$(grep -E -m1 '^infer: unloaded ' "${work}/serial.txt" || true)" |
| 331 | b="$(sed -nE 's/.* frames_free_before=([0-9]+).*/\1/p' <<<"${l}")"; a="$(sed -nE 's/.* frames_free_after=([0-9]+).*/\1/p' <<<"${l}")" |
| 332 | if [[ -n "${b}" && "${b}" == "${a}" ]] && grep -qE '^infer: unloaded .* rc=0,0$' <<<"${l}"; then ok "$1" "$2 (${b} = ${a})"; else bad "$1" "$2 (before=${b:-none} after=${a:-none})"; fi; } |
| 333 | disk_model_rows() { # row: the model came from the disk, by the map, with the pinned digest |
| 334 | rcheck "$1" "stub read MODEL.MAP from the ESP" "^model_map: ok len=${model_bytes} extents=3 sector=512 disk=[0-9a-f]{32}\$" |
| 335 | rcheck "$1" "boot-disk source declared" '^infer_source_mode: boot-disk model \(MODEL\.MAP read by the UEFI stub' |
| 336 | rcheck "$1" "model handed over from the disk (3 extents)" "^infer: disk base=0x[0-9a-f]+ size=${model_bytes} extents=3 read_us=[0-9]+ block_size=512\$" |
| 337 | rcheck "$1" "kernel recomputed the pinned sha256" '^infer: sha256=3f5a22426976ab26cfe84dba63c1d08391717abb1af893e10f1b2968d862dcc1 ' |
| 338 | rabsent "$1" "no fw_cfg ingest line" '^(infer_source_mode: TEST-ONLY|infer: fw_cfg|infer: ingest bytes=)' |
| 339 | } |
| 340 | |
| 341 | # --- negative control A: no MODEL.MAP, the model offered on fw_cfg (row 208) --- |
| 342 | neg_img="${top}/neg.img" |
| 343 | cp --sparse=always "${img}" "${neg_img}" |
| 344 | mdel -i "${neg_img}@@$(( esp_first * 512 ))" ::/EFI/AIENOS/MODEL.MAP |
| 345 | cp "${vars_fd}" "${top}/vars.neg" |
| 346 | BOOT_VARS="${top}/vars.neg" boot neg-nomap "${neg_img}" drive_neg -fw_cfg "name=opt/aienos/model,file=${model}" |
| 347 | rcheck 208 "stub found no MODEL.MAP" '^model_map: absent' |
| 348 | rcheck 208 "kernel refused: no boot-disk model, no other source" '^infer: ingest REFUSED \(no boot-disk model: MODEL\.MAP absent or refused by the stub; this image has no other model source\)$' |
| 349 | rabsent 208 "the fw_cfg model was NOT ingested although QEMU offered it" '^(infer_source_mode|infer: fw_cfg|infer: ingest bytes=|infer: resident open)' |
| 350 | rcheck 208 "unlock ok (the Store path is unaffected)" '^lifecycle: unlock ok state=ready$' |
| 351 | rcheck 208 "prompt refused: no inference unit" '^lifecycle: REFUSED verb=prompt state=ready reason="no inference unit in this image"$' |
| 352 | rcheck 208 "evidence inference=none" '^lifecycle: evidence .* inference=none ' |
| 353 | rabsent 208 "no panic or fault" '^report_kind: (panic|fault)' |
| 354 | if [[ "${qemu_status}" == 0 ]]; then ok 208 "QEMU exit 0"; else bad 208 "QEMU exit ${qemu_status}"; fi |
| 355 | |
| 356 | # --- negative control B: MODEL.MAP with a corrupted declared sha256 (row 209) --- |
| 357 | cp --sparse=always "${img}" "${neg_img}" |
| 358 | "${top}/host/host/ck_model_map" make "${top}/BAD.MAP" --file "${model}" --disk "${neg_img}" \ |
| 359 | $(IFS=,; for e in ${extents}; do printf -- '--extent %s ' "${e}"; done) --corrupt-sha >/dev/null |
| 360 | mcopy -o -i "${neg_img}@@$(( esp_first * 512 ))" "${top}/BAD.MAP" ::/EFI/AIENOS/MODEL.MAP |
| 361 | cp "${vars_fd}" "${top}/vars.neg" |
| 362 | BOOT_VARS="${top}/vars.neg" boot neg-badsha "${neg_img}" drive_neg |
| 363 | rcheck 209 "stub accepted the map and read the bytes" "^model_map: ok len=${model_bytes} extents=3 " |
| 364 | rcheck 209 "kernel refused to decode: sha256 mismatch" '^infer: sha256 mismatch declared=[0-9a-f]{64}$' |
| 365 | rabsent 209 "no resident session" '^infer: resident open' |
| 366 | rcheck 209 "prompt refused: no inference unit" '^lifecycle: REFUSED verb=prompt state=ready reason="no inference unit in this image"$' |
| 367 | rabsent 209 "no panic or fault" '^report_kind: (panic|fault)' |
| 368 | if [[ "${qemu_status}" == 0 ]]; then ok 209 "QEMU exit 0"; else bad 209 "QEMU exit ${qemu_status}"; fi |
| 369 | rm -f "${neg_img}" |
| 370 | |
| 371 | # --- boot 1: the staged disk, blank Store (rows 204-205) --- |
| 372 | boot main-1 "${img}" drive_main1 |
| 373 | disk_model_rows 204 |
| 374 | rcheck 204 "resident session open" "${re_open}" |
| 375 | before 204 "resident open < lifecycle ready" '^infer: resident open rc=0' '^lifecycle: ready state=locked' |
| 376 | rcheck 204 "owner provisioned at build time (public only)" '^owner: provisioned at build time \(public only\): owner_root_ed25519 sha256=[0-9a-f]{16} store_uuid=' |
| 377 | rcheck 204 "Store stage refused the production key source: LOCKED" '^store: REFUSED proof=keyed step="production Store key source" rc=-?[0-9]+ ' |
| 378 | rcheck 204 "lifecycle came up locked" '^lifecycle: ready state=locked store=locked' |
| 379 | rcheck 204 "blank AIENOS partition formatted as a production store" '^store: blank disk \(all-zero anchor \+ Store head\): formatted production store$' |
| 380 | rcheck 204 "recovery keyslot genesis" '^store: key source=recovery-keyslot genesis=1 ' |
| 381 | rcheck 204 "unlock ok, genesis, generation 2 boot 1" '^store: unlock ok source=recovery-keyslot slot=1 genesis=1 generation=2 boot_count=1$' |
| 382 | rcheck 204 "continuity genesis from the owner root key (operator source)" '^continuity: genesis source=operator provenance=owner-root-pk\+store-uuid ' |
| 383 | rcheck 204 "identity PROVISIONED" '^continuity: PROVISIONED agent=[0-9a-f]{64} incarnation=1 ' |
| 384 | rcheck 204 "lifecycle unlocked to ready" '^lifecycle: unlock ok state=ready$' |
| 385 | rcheck 205 "golden reply ids" '^lifecycle: reply ids=791 6864 315 9822 374 12366 13 128009 tokens=8 prompt_ids=17 steps=25 us=[0-9]+$' |
| 386 | rcheck 205 "reply is the pending proposal" '^lifecycle: reply pending state=serve proposed="reply: The capital of France is Paris\.\??" \(approve \| deny \| cancel\)$' |
| 387 | rcheck 205 "approve committed the reply at generation 4" '^lifecycle: approve committed gen=4 id=[0-9a-f]{8} state=ready$' |
| 388 | rcheck 205 "remembered as record 1 at generation 6" '^lifecycle: remembered id=1 t=1 store_gen=6 bytes=18 text="prefer short notes"$' |
| 389 | rcheck 205 "facts lists it, chain verified" '^lifecycle: facts records=1 shown=1 journal_bytes=192 chain=ok$' |
| 390 | rcheck 205 "evidence counters" '^lifecycle: evidence requests=[0-9]+ refused=0 invalid=0 tasks=0 approved=1 denied=0 cancelled=0 overlong=0 last_commit_gen=4 last_id=[0-9a-f]{8} prompts=1 replies=1 gen_cancelled=0 gen_failed=0 recalls=0 inference=resident remembers=1 facts=1 cortex=open records=1$' |
| 391 | rcheck 205 "resident close" '^infer: resident close rc=0 ' |
| 392 | frames_kept 205 "unload returned every frame" |
| 393 | before 205 "unload < stop order" '^infer: unloaded ' '^lifecycle: stop order store=closed' |
| 394 | rcheck 205 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=[0-9]+$' |
| 395 | rabsent 205 "operator input accepted (no console-input refusal)" '^lifecycle: REFUSED console input' |
| 396 | rabsent 205 "no panic or fault" '^report_kind: (panic|fault)' |
| 397 | if [[ "${qemu_status}" == 0 ]]; then ok 205 "QEMU exit 0"; else bad 205 "QEMU exit ${qemu_status}"; fi |
| 398 | m1 205 |
| 399 | agent="$(grep -oE '^continuity: PROVISIONED agent=[0-9a-f]{64}' "${work}/serial.txt" | head -1 | sed 's/.*agent=//')" |
| 400 | [[ -n "${agent}" ]] || agent="NO_AGENT" |
| 401 | |
| 402 | # --- boot 2: cold restart, SAME disk image file (row 206) --- |
| 403 | boot main-2 "${img}" drive_main2 |
| 404 | disk_model_rows 206 |
| 405 | rcheck 206 "resident session open again" "${re_open}" |
| 406 | rcheck 206 "wrong secret refused by the keyslot tag, still locked" '^store: unlock REFUSED proof=keyed step="recovery secret rejected \(K_vol unwrap tag failed\); disk not opened" rc=-?[0-9]+ \([A-Za-z_]+\); still LOCKED, disk not reformatted$' |
| 407 | rabsent 206 "not reformatted" '^store: blank disk' |
| 408 | rcheck 206 "Store opened at generation 6 (no genesis)" '^store: opened generation=6 boot_count=1 prev_commit=[0-9a-f]+ anchor=[a-z_-]+$' |
| 409 | rcheck 206 "unlock ok, no genesis" '^store: unlock ok source=recovery-keyslot slot=1 genesis=0 generation=7 boot_count=2$' |
| 410 | rcheck 206 "continuity RESUMED the same agent" "^continuity: RESUMED agent=${agent} incarnation=2 " |
| 411 | rabsent 206 "no second genesis" '^continuity: (PROVISIONED|genesis)' |
| 412 | rcheck 206 "recall returns the approved reply from the disk" "${re_recall}" |
| 413 | rcheck 206 "facts answers the fact from the disk" '^lifecycle: fact id=1 t=1 store_gen=6 text="prefer short notes"$' |
| 414 | rabsent 206 "neither the prompt nor the fact retyped" '^> (prompt|remember) ' |
| 415 | rcheck 206 "evidence: one refused unlock, one recall, facts, nothing generated" '^lifecycle: evidence requests=[0-9]+ refused=1 invalid=0 tasks=0 approved=0 denied=0 cancelled=0 overlong=0 last_commit_gen=0 last_id=00000000 prompts=0 replies=0 gen_cancelled=0 gen_failed=0 recalls=1 inference=resident remembers=0 facts=1 cortex=open records=1$' |
| 416 | frames_kept 206 "unload returned every frame" |
| 417 | rcheck 206 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=[0-9]+$' |
| 418 | rabsent 206 "no panic or fault" '^report_kind: (panic|fault)' |
| 419 | if [[ "${qemu_status}" == 0 ]]; then ok 206 "QEMU exit 0"; else bad 206 "QEMU exit ${qemu_status}"; fi |
| 420 | m1 206 |
| 421 | |
| 422 | # --- row 207: secret hygiene, no TEST markers, across every boot --- |
| 423 | for b in neg-nomap neg-badsha main-1 main-2; do |
| 424 | work="${top}/${b}" |
| 425 | rabsent 207 "${b}: right secret hex never in the serial log" "${secret_ok}" |
| 426 | rabsent 207 "${b}: wrong secret hex never in the serial log" "${secret_bad}" |
| 427 | rabsent 207 "${b}: no TEST banner, TEST machine id or TEST Store line" '(TEST-ONLY|TEST machine id|formatted TEST store)' |
| 428 | done |
| 429 | |
| 430 | if [[ "${fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then |
| 431 | for b in neg-nomap neg-badsha main-1 main-2; do echo "---- ${b} serial (selected lines) ----" |
| 432 | grep -E '^(model_map|infer|lifecycle|store|continuity|owner|report_kind|> )' "${top}/${b}/serial.txt" | head -60 || true; done |
| 433 | fi |
| 434 | echo "ROW 210 LIMIT QEMU only (hardware NOT_RUN); Spark console input, the AIENOS partition on the Spark NVMe and the owner key ceremony are separate items; int8 off" |
| 435 | if [[ "${fail}" == 0 ]]; then echo "${final_tag}: PASS (QEMU only; hardware NOT_RUN)"; exit 0; fi |
| 436 | echo "${final_tag}: FAIL"; exit 1 |