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_infer_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 | # Boot the AIENOS C kernel with the inference unit linked in (QEMU AArch64, |
| 18 | # UEFI/AAVMF, EL2 like Machine 1, -m 4096), let the kernel ingest the |
| 19 | # Llama-3.2-1B-Instruct Q4_K_M GGUF (807,694,368 bytes) into its own RAM over |
| 20 | # QEMU fw_cfg DMA, then check that the Rust crate crates/aienos-infer, called |
| 21 | # from the general-regs-only C kernel through crates/aienos-infer-kernel, |
| 22 | # parsed the header, bound the model, tokenized the fixed chat prompt |
| 23 | # "What is the capital of France?", ran prefill + greedy decode to the exact |
| 24 | # llama.cpp token sequence, decoded it to text containing "Paris", unloaded |
| 25 | # the model (every frame returned) and went on to its normal exit |
| 26 | # (aienos#34 lane 4 cut 1 + lane 5 cut 2). |
| 27 | # Last line: AIENOS_CK_INFER: PASS|FAIL|NOT_RUN (read by scripts/ck_gates.sh). |
| 28 | # NOT_RUN (never a silent pass) when the model file, cargo, the Rust target |
| 29 | # aarch64-unknown-none, qemu or AAVMF is missing, the QEMU gate lock |
| 30 | # (~/workspace/.qemu-gate-lock) is held by another gate run, or the machine |
| 31 | # quiet flag (~/workspace/.spark-quiet) exists. This script never creates or |
| 32 | # removes the quiet flag (Drake's rule: no agent raises it without approval). |
| 33 | # QEMU is not hardware: a PASS here qualifies nothing physical. |
| 34 | # |
| 35 | # FAIL when the script re-checks the serial lines itself and finds: the |
| 36 | # kernel did not ingest exactly the host file's size in bytes, the kernel's |
| 37 | # SHA-256 of the ingested bytes differs from the host's sha256sum (and, in the |
| 38 | # gate, the pinned model hash), no or several "infer: probe" lines, rc != 0, |
| 39 | # tensor count != 147, vocab != 128256, layers != 16, the first four prompt |
| 40 | # ids differ from the first four of the "ids" line of |
| 41 | # crates/aienos-infer/tests/fixtures/ref_fr.txt (128000 128006 882 128007), |
| 42 | # prompt length differs from that line's id count, the generated ids differ |
| 43 | # from that fixture's "step" tokens (791 6864 315 9822 374 12366 13 128009, |
| 44 | # exact, no near-tie tolerance: same code path as the host test), the decoded |
| 45 | # text does not contain "Paris", no "infer: unloaded" line or free frames |
| 46 | # after the unload != before the ingest or a frame return refused, no |
| 47 | # per-token timing line, a kernel PASS the script does not reproduce, a |
| 48 | # panic/fault report, no "kernel: alive", no final report, or a non-zero |
| 49 | # QEMU exit (PSCI reset = 0). After the real run the script also re-checks |
| 50 | # the SAME serial log against a mutated expected-id list and requires that |
| 51 | # check to FAIL (a checker that cannot fail makes the gate FAIL). |
| 52 | # |
| 53 | # Usage: bash scripts/qemu_ck_infer_test.sh the gate |
| 54 | # bash scripts/qemu_ck_infer_test.sh --negative-control corrupt the GGUF |
| 55 | # magic; the gate checks must FAIL (rc=-2) -> prints |
| 56 | # AIENOS_CK_INFER_NEGATIVE_CONTROL: PASS when they do |
| 57 | # bash scripts/qemu_ck_infer_test.sh --disk model on a GPT/NVMe |
| 58 | # image via MODEL.MAP + firmware Block I/O (the hardware path) |
| 59 | # bash scripts/qemu_ck_infer_test.sh --disk-negative-control corrupt the |
| 60 | # declared sha256 in MODEL.MAP; the kernel must refuse to decode -> |
| 61 | # AIENOS_CK_INFER_DISK_NEGATIVE_CONTROL: PASS when it does |
| 62 | # bash scripts/qemu_ck_infer_test.sh --self-test canned logs, no QEMU/build |
| 63 | # Environment: AIENOS_MODEL (the GGUF), AIENOS_QEMU_TIMEOUT (3600 s: TCG runs |
| 64 | # the 25 forward passes at roughly 20-30 s each), AIENOS_GATE_LOCK, |
| 65 | # AIENOS_GATE_MINUTES, AIENOS_QUIET_FLAG (read only), AIENOS_LOG_DIR, |
| 66 | # AIENOS_QEMU_VERBOSE. |
| 67 | set -euo pipefail |
| 68 | |
| 69 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 70 | cd "${repo_root}" |
| 71 | verdict_name=AIENOS_CK_INFER |
| 72 | source_mode=fwcfg disk_negctl=0 |
| 73 | model="${AIENOS_MODEL:-${HOME}/models/aien-mail/Llama-3.2-1B-Instruct-Q4_K_M.gguf}" |
| 74 | ref_file=crates/aienos-infer/tests/fixtures/ref_fr.txt |
| 75 | # Pinned model hash: evidence/config_a_reference_bundle.json and |
| 76 | # crates/aienos-infer/tests/fixtures/PROVENANCE.md. |
| 77 | pinned_sha=3f5a22426976ab26cfe84dba63c1d08391717abb1af893e10f1b2968d862dcc1 |
| 78 | # Tensor count: crates/aienos-infer/tests/golden.rs:112; vocab/layers: golden.rs:61,56. |
| 79 | want_tensors=147 want_vocab=128256 want_layers=16 |
| 80 | want_text=Paris |
| 81 | |
| 82 | # infer_check_log FILE WANT_SIZE WANT_SHA WANT_IDS("a b c d ...") PIN(1|0) WANT_GEN("t1 t2 ..."): |
| 83 | # PASS/FAIL line per check; sets infer_failed=1 on failure. |
| 84 | infer_check_log() { |
| 85 | local f="$1" want_size="$2" want_sha="$3" want_ids="$4" pin="$5" want_gen="$6" |
| 86 | local line rc ids_got first4 plen |
| 87 | ok() { echo "PASS $*"; } |
| 88 | bad() { echo "FAIL $*"; infer_failed=1; } |
| 89 | local re_fw='^infer: fw_cfg base=0x[0-9a-f]+ dma=yes file=opt/aienos/model size=([0-9]+)$' |
| 90 | local re_in='^infer: ingest bytes=([0-9]+) dma_us=([0-9]+)$' |
| 91 | local re_sha='^infer: sha256=([0-9a-f]{64}) sha_us=[0-9]+$' |
| 92 | local re_p='^infer: probe rc=(-?[0-9]+) tensors=([0-9]+) vocab=([0-9]+) ids=([0-9]+),([0-9]+),([0-9]+),([0-9]+) prompt_len=([0-9]+) layers=([0-9]+) heap_peak_kib=([0-9]+) probe_us=[0-9]+$' |
| 93 | local re_tok='^infer: tokens=([0-9 ]*)$' |
| 94 | local re_tus='^infer: decode_tokens=([0-9]+) mean_tok_us=([0-9]+)$' |
| 95 | local re_txt='^infer: text=(.*)$' |
| 96 | local re_un='^infer: unloaded heap_live_kib=([0-9]+) frames_free_before=([0-9]+) frames_free_after=([0-9]+) frames_returned=([0-9]+) rc=(-?[0-9]+),(-?[0-9]+)$' |
| 97 | local n sz line plen="" |
| 98 | local -a m; g() { echo "${m[$1]}"; } |
| 99 | if [[ "${source_mode:-fwcfg}" == disk ]]; then |
| 100 | local re_dk='^infer: disk base=0x[0-9a-f]+ size=([0-9]+) extents=([0-9]+) read_us=([0-9]+) block_size=(512|4096)$' |
| 101 | n="$(grep -cE "${re_dk}" "${f}" || true)" |
| 102 | if [[ "${n}" == 1 ]]; then |
| 103 | local dl; dl="$(grep -E "${re_dk}" "${f}")"; [[ "${dl}" =~ ${re_dk} ]]; sz="${BASH_REMATCH[1]}" |
| 104 | [[ "${sz}" == "${want_size}" ]] && ok "boot-disk model size ${sz} = host file size" || bad "boot-disk model size ${sz}, host file is ${want_size}" |
| 105 | grep -qE '^model_map: ok len='"${want_size}"' extents=[0-9]+ sector=(512|4096) disk=[0-9a-f]{32}$' "${f}" && ok "stub accepted MODEL.MAP (len ${want_size})" || bad "no well-formed model_map: ok line from the stub" |
| 106 | grep -qE '^model: base=0x[0-9a-f]+ len='"${want_size}"' extents=[0-9]+ read_us=[0-9]+ declared_sha256=[0-9a-f]{64}$' "${f}" && ok "stub read the model through firmware Block I/O" || bad "no well-formed model: base line from the stub" |
| 107 | else bad "exactly one well-formed infer: disk line (found ${n})"; fi |
| 108 | if grep -qE "${re_fw}|${re_in}|^infer_source_mode: TEST-ONLY" "${f}"; then bad "the TEST-ONLY fw_cfg path ran in disk mode"; else ok "TEST-ONLY fw_cfg path did not run"; fi |
| 109 | else |
| 110 | n="$(grep -cE "${re_fw}" "${f}" || true)" |
| 111 | if [[ "${n}" == 1 ]]; then |
| 112 | sz="$(sed -nE "s|${re_fw}|\\1|p" "${f}")" |
| 113 | [[ "${sz}" == "${want_size}" ]] && ok "fw_cfg file size ${sz} = host file size" || bad "fw_cfg file size ${sz}, host file is ${want_size}" |
| 114 | else bad "exactly one well-formed fw_cfg line (found ${n})"; fi |
| 115 | n="$(grep -cE "${re_in}" "${f}" || true)" |
| 116 | if [[ "${n}" == 1 ]]; then |
| 117 | sz="$(sed -nE "s|${re_in}|\\1|p" "${f}")" |
| 118 | [[ "${sz}" == "${want_size}" ]] && ok "kernel ingested ${sz} bytes = host file size" || bad "kernel ingested ${sz} bytes, host file is ${want_size}" |
| 119 | else bad "exactly one well-formed ingest line (found ${n})"; fi |
| 120 | fi |
| 121 | n="$(grep -cE "${re_sha}" "${f}" || true)" |
| 122 | if [[ "${n}" == 1 ]]; then |
| 123 | sz="$(sed -nE "s|${re_sha}|\\1|p" "${f}")" |
| 124 | [[ "${sz}" == "${want_sha}" ]] && ok "kernel SHA-256 of the ingested bytes = host sha256sum" || bad "kernel SHA-256 ${sz} differs from host ${want_sha}" |
| 125 | if [[ "${pin}" == 1 ]]; then |
| 126 | [[ "${sz}" == "${pinned_sha}" ]] && ok "SHA-256 = pinned model hash" || bad "SHA-256 is not the pinned model hash" |
| 127 | fi |
| 128 | else bad "exactly one well-formed sha256 line (found ${n})"; fi |
| 129 | if [[ "$(grep -cE "${re_p}" "${f}" || true)" != 1 ]]; then |
| 130 | bad "exactly one well-formed infer: probe line" |
| 131 | else |
| 132 | line="$(grep -E "${re_p}" "${f}")" |
| 133 | [[ "${line}" =~ ${re_p} ]]; m=("${BASH_REMATCH[@]}") |
| 134 | rc="$(g 1)" |
| 135 | [[ "${rc}" == 0 ]] && ok "probe returned 0" || bad "probe returned ${rc}" |
| 136 | [[ "$(g 2)" == "${want_tensors}" ]] && ok "tensor count ${want_tensors}" || bad "tensor count $(g 2), expected ${want_tensors}" |
| 137 | [[ "$(g 3)" == "${want_vocab}" ]] && ok "vocab size ${want_vocab}" || bad "vocab size $(g 3), expected ${want_vocab}" |
| 138 | [[ "$(g 9)" == "${want_layers}" ]] && ok "layers ${want_layers}" || bad "layers $(g 9), expected ${want_layers}" |
| 139 | ids_got="$(g 4) $(g 5) $(g 6) $(g 7)" |
| 140 | first4="$(awk '{print $1,$2,$3,$4}' <<<"${want_ids}")" |
| 141 | [[ "${ids_got}" == "${first4}" ]] && ok "first four prompt ids ${ids_got}" || bad "first four prompt ids '${ids_got}', expected '${first4}'" |
| 142 | plen="$(wc -w <<<"${want_ids}")" |
| 143 | [[ "$(g 8)" == "${plen}" ]] && ok "prompt length ${plen} tokens" || bad "prompt length $(g 8), expected ${plen}" |
| 144 | local hp; hp="$(g 10)" |
| 145 | (( hp > 0 && hp < 262144 )) && ok "allocator peak ${hp} KiB within the 256 MiB region" || bad "allocator peak ${hp} KiB outside (0, 262144)" |
| 146 | fi |
| 147 | # Generated ids: exact match with the fixture's step tokens (EOT included). |
| 148 | if [[ "$(grep -cE "${re_tok}" "${f}" || true)" != 1 ]]; then |
| 149 | bad "exactly one well-formed infer: tokens line" |
| 150 | else |
| 151 | local got; got="$(sed -nE "s|${re_tok}|\\1|p" "${f}" | tr -s ' ' | sed 's/ *$//')" |
| 152 | [[ "${got}" == "${want_gen}" ]] && ok "generated ids '${got}' = llama.cpp greedy sequence" || bad "generated ids '${got}', expected '${want_gen}'" |
| 153 | fi |
| 154 | if [[ "$(grep -cE "${re_tus}" "${f}" || true)" != 1 ]]; then |
| 155 | bad "exactly one well-formed decode timing line" |
| 156 | else |
| 157 | line="$(grep -E "${re_tus}" "${f}")"; [[ "${line}" =~ ${re_tus} ]] |
| 158 | local nd="${BASH_REMATCH[1]}" mu="${BASH_REMATCH[2]}" |
| 159 | local want_nd=$(( $(wc -w <<<"${want_gen}") - 1 )) |
| 160 | [[ "${nd}" == "${want_nd}" && "${mu}" -gt 0 ]] && ok "decode timing: ${nd} forwards, mean ${mu} us per token (QEMU TCG, not hardware)" \ |
| 161 | || bad "decode timing line: ${nd} forwards (expected ${want_nd}), mean ${mu} us" |
| 162 | fi |
| 163 | if [[ "$(grep -cE "${re_txt}" "${f}" || true)" != 1 ]]; then |
| 164 | bad "exactly one infer: text line" |
| 165 | else |
| 166 | line="$(grep -E "${re_txt}" "${f}")" |
| 167 | grep -qF -- "${want_text}" <<<"${line#infer: text=}" && ok "decoded text contains '${want_text}': ${line#infer: text=}" || bad "decoded text lacks '${want_text}': ${line#infer: text=}" |
| 168 | fi |
| 169 | if [[ "$(grep -cE "${re_un}" "${f}" || true)" != 1 ]]; then |
| 170 | bad "exactly one well-formed infer: unloaded line" |
| 171 | else |
| 172 | line="$(grep -E "${re_un}" "${f}")"; [[ "${line}" =~ ${re_un} ]] |
| 173 | local fb="${BASH_REMATCH[2]}" fa="${BASH_REMATCH[3]}" r1="${BASH_REMATCH[5]}" r2="${BASH_REMATCH[6]}" |
| 174 | [[ "${fb}" == "${fa}" && "${fb}" -gt 0 && "${r1}" == 0 && "${r2}" == 0 ]] \ |
| 175 | && ok "unload returned every frame (free frames ${fa} = ${fb} before ingest), kernel heap live ${BASH_REMATCH[1]} KiB" \ |
| 176 | || bad "unload: free frames after ${fa} vs before ${fb}, frame-return rc ${r1},${r2}" |
| 177 | fi |
| 178 | grep -qx 'AIENOS_CK_INFER: PASS' "${f}" && ok "kernel reported AIENOS_CK_INFER: PASS" || bad "kernel did not report AIENOS_CK_INFER: PASS" |
| 179 | # native v1 P06: the bounded session over the same model bytes. Open with |
| 180 | # explicit limits, three cheap refusals, a request cancelled during its |
| 181 | # prefill, the golden prompt through the step API (reply must equal the |
| 182 | # one-shot probe's), a second cancel after a completed request, close, |
| 183 | # and every session heap frame returned. |
| 184 | local re_so='^infer: session open rc=(-?[0-9]+) tensors=([0-9]+) vocab=([0-9]+) layers=([0-9]+) kv_capacity=([0-9]+) max_new=([0-9]+) max_prompt_bytes=([0-9]+) heap_reserve_kib=([0-9]+) heap_used_kib=([0-9]+) open_us=[0-9]+$' |
| 185 | local re_sr='^infer: session refusals open_twice=(-?[0-9]+) prompt_bytes=(-?[0-9]+) step_idle=(-?[0-9]+)$' |
| 186 | local re_sc='^infer: session prompt=([0-9]+) ids=([0-9]+) cancel_after_steps=([0-9]+) steps=([0-9]+) prefill=([0-9]+) produced=([0-9]+) idle_step=done heap_used_kib=[0-9]+ rc=(-?[0-9]+)$' |
| 187 | local re_sg='^infer: session prompt=2 ids=([0-9]+) steps=([0-9]+) prefill=([0-9]+) produced=([0-9]+) last=(yes|no) prefill_us=([0-9]+) mean_tok_us=([0-9]+) tokens=([0-9 ]*) text=(.*) heap_used_kib=[0-9]+ request_us=[0-9]+ rc=(-?[0-9]+)$' |
| 188 | local re_sx='^infer: session close rc=(-?[0-9]+) heap_used_kib=([0-9]+) heap_peak_kib=([0-9]+) frames_free_before=([0-9]+) frames_free_after=([0-9]+) rc=(-?[0-9]+)$' |
| 189 | if [[ "$(grep -cE "${re_so}" "${f}" || true)" != 1 ]]; then |
| 190 | bad "exactly one well-formed infer: session open line" |
| 191 | else |
| 192 | line="$(grep -E "${re_so}" "${f}")"; [[ "${line}" =~ ${re_so} ]]; m=("${BASH_REMATCH[@]}") |
| 193 | [[ "$(g 1)" == 0 && "$(g 2)" == "${want_tensors}" && "$(g 3)" == "${want_vocab}" && "$(g 4)" == "${want_layers}" ]] \ |
| 194 | && ok "session open: rc 0, same model shape ($(g 2) tensors, vocab $(g 3), $(g 4) layers)" \ |
| 195 | || bad "session open: rc $(g 1), tensors $(g 2), vocab $(g 3), layers $(g 4)" |
| 196 | [[ "$(g 5)" == 64 && "$(g 6)" == 16 && "$(g 7)" == 256 && "$(g 8)" == 64 ]] \ |
| 197 | && ok "session limits: kv 64, max_new 16, prompt 256 bytes, heap reserve 64 KiB" \ |
| 198 | || bad "session limits: kv $(g 5), max_new $(g 6), prompt bytes $(g 7), reserve $(g 8) KiB" |
| 199 | fi |
| 200 | if [[ "$(grep -cE "${re_sr}" "${f}" || true)" != 1 ]]; then |
| 201 | bad "exactly one well-formed infer: session refusals line" |
| 202 | else |
| 203 | line="$(grep -E "${re_sr}" "${f}")"; [[ "${line}" =~ ${re_sr} ]] |
| 204 | [[ "${BASH_REMATCH[1]}" == -12 && "${BASH_REMATCH[2]}" == -16 && "${BASH_REMATCH[3]}" == 0 ]] \ |
| 205 | && ok "session refusals: second open -12, over-long prompt -16, idle step 0" \ |
| 206 | || bad "session refusals: second open ${BASH_REMATCH[1]}, over-long prompt ${BASH_REMATCH[2]}, idle step ${BASH_REMATCH[3]}" |
| 207 | fi |
| 208 | if [[ "$(grep -cE "${re_sc}" "${f}" || true)" != 2 ]]; then |
| 209 | bad "exactly two well-formed infer: session cancel lines (prompt 1 and 3)" |
| 210 | else |
| 211 | local which |
| 212 | for which in 1 3; do |
| 213 | line="$(grep -E "${re_sc}" "${f}" | grep "prompt=${which} " || true)" |
| 214 | if [[ "${line}" =~ ${re_sc} ]]; then |
| 215 | m=("${BASH_REMATCH[@]}") |
| 216 | [[ "$(g 2)" -gt 0 && "$(g 4)" == "$(g 3)" && "$(g 6)" == $(( $(g 4) - $(g 5) )) && "$(g 7)" -ge 0 ]] \ |
| 217 | && ok "session cancel ${which}: $(g 2) prompt ids, stopped after $(g 4) of its steps ($(g 5) prefill, $(g 6) produced), idle afterwards" \ |
| 218 | || bad "session cancel ${which}: ids $(g 2), cancel_after $(g 3), steps $(g 4), prefill $(g 5), produced $(g 6), rc $(g 7)" |
| 219 | else bad "session cancel line for prompt ${which} missing"; fi |
| 220 | done |
| 221 | fi |
| 222 | if [[ "$(grep -cE "${re_sg}" "${f}" || true)" != 1 ]]; then |
| 223 | bad "exactly one well-formed infer: session prompt=2 reply line" |
| 224 | else |
| 225 | line="$(grep -E "${re_sg}" "${f}")"; [[ "${line}" =~ ${re_sg} ]]; m=("${BASH_REMATCH[@]}") |
| 226 | local got; got="$(tr -s ' ' <<<"$(g 8)" | sed 's/ *$//')" |
| 227 | local want_n; want_n="$(wc -w <<<"${want_gen}")" |
| 228 | [[ "${got}" == "${want_gen}" ]] && ok "session reply ids '${got}' = llama.cpp greedy sequence" || bad "session reply ids '${got}', expected '${want_gen}'" |
| 229 | [[ "$(g 1)" == "${plen}" && "$(g 3)" == "${plen}" && "$(g 5)" == yes && "$(g 4)" == "${want_n}" && "$(g 2)" == $(( plen + want_n )) && "$(g 7)" -gt 0 ]] \ |
| 230 | && ok "session reply: ${plen} prompt steps then ${want_n} token steps to the reply's end (mean $(g 7) us per token, QEMU TCG)" \ |
| 231 | || bad "session reply: ids $(g 1), steps $(g 2), prefill $(g 3), produced $(g 4), last $(g 5), mean_tok_us $(g 7)" |
| 232 | grep -qF -- "${want_text}" <<<"$(g 9)" && ok "session reply text contains '${want_text}': $(g 9)" || bad "session reply text lacks '${want_text}': $(g 9)" |
| 233 | fi |
| 234 | # PI5-6 agent: the chat template the tokenizer built is checked in the session path too (the first four prompt ids |
| 235 | # of the golden prompt; the one-shot probe checks the same four, and the Pi image has no one-shot probe). |
| 236 | local re_sh='^infer: session prompt=2 prompt_head=([0-9]+),([0-9]+),([0-9]+),([0-9]+) expect=(match|MISMATCH)$' |
| 237 | if [[ "$(grep -cE "${re_sh}" "${f}" || true)" != 1 ]]; then |
| 238 | bad "exactly one well-formed infer: session prompt=2 prompt_head line" |
| 239 | else |
| 240 | line="$(grep -E "${re_sh}" "${f}")"; [[ "${line}" =~ ${re_sh} ]]; m=("${BASH_REMATCH[@]}") |
| 241 | local want_head; want_head="$(awk '{print $1,$2,$3,$4}' <<<"${want_ids}")" |
| 242 | [[ "$(g 1) $(g 2) $(g 3) $(g 4)" == "${want_head}" && "$(g 5)" == match ]] \ |
| 243 | && ok "session prompt head $(g 1),$(g 2),$(g 3),$(g 4) = the golden chat-template head" \ |
| 244 | || bad "session prompt head $(g 1),$(g 2),$(g 3),$(g 4) (${want_head} expected), kernel said $(g 5)" |
| 245 | fi |
| 246 | if [[ "$(grep -cE "${re_sx}" "${f}" || true)" != 1 ]]; then |
| 247 | bad "exactly one well-formed infer: session close line" |
| 248 | else |
| 249 | line="$(grep -E "${re_sx}" "${f}")"; [[ "${line}" =~ ${re_sx} ]]; m=("${BASH_REMATCH[@]}") |
| 250 | [[ "$(g 1)" == 0 && "$(g 6)" == 0 && "$(g 4)" == "$(g 5)" && "$(g 4)" -gt 0 && "$(g 3)" -gt 0 && "$(g 3)" -lt 262144 ]] \ |
| 251 | && ok "session close: rc 0, session heap peak $(g 3) KiB within 256 MiB, every frame returned (free frames $(g 5) = $(g 4) before)" \ |
| 252 | || bad "session close: rc $(g 1), peak $(g 3) KiB, free frames after $(g 5) vs before $(g 4), frame-return rc $(g 6)" |
| 253 | fi |
| 254 | grep -qx 'AIENOS_CK_INFER_SESSION: PASS' "${f}" && ok "kernel reported AIENOS_CK_INFER_SESSION: PASS" || bad "kernel did not report AIENOS_CK_INFER_SESSION: PASS" |
| 255 | # The session runs between the one-shot verdict and the unload line. |
| 256 | if [[ "$(grep -nx 'AIENOS_CK_INFER: PASS' "${f}" | cut -d: -f1 | head -1)" -lt "$(grep -nE "${re_sx}" "${f}" | cut -d: -f1 | head -1)" \ |
| 257 | && "$(grep -nE "${re_sx}" "${f}" | cut -d: -f1 | head -1)" -lt "$(grep -nE "${re_un}" "${f}" | cut -d: -f1 | head -1)" ]] 2>/dev/null; then |
| 258 | ok "session ran after the one-shot verdict and before the unload" |
| 259 | else bad "session close line not between the one-shot verdict and the unload"; fi |
| 260 | grep -q "kernel: alive" "${f}" && ok "kernel alive" || bad "kernel never reached kernel: alive" |
| 261 | if grep -qE 'report_kind: (panic|fault)' "${f}"; then bad "panic or fault report"; else ok "no panic or fault"; fi |
| 262 | grep -q '^report_kind: final' "${f}" && ok "kernel reached its final report" || bad "no final report" |
| 263 | # The unload line must come before the final report (unload, then the OS goes on). |
| 264 | if [[ "$(grep -nE "${re_un}" "${f}" | cut -d: -f1 | head -1)" -lt "$(grep -n '^report_kind: final' "${f}" | cut -d: -f1 | head -1)" ]] 2>/dev/null; then |
| 265 | ok "kernel continued to its final report after the unload" |
| 266 | else bad "unload line not before the final report"; fi |
| 267 | } |
| 268 | |
| 269 | # The expected ids come from the golden reference, not from this script. |
| 270 | read_want_ids() { sed -n 's/^ids //p' "${ref_file}" | head -1; } |
| 271 | read_want_gen() { awk '$1=="step"{printf "%s%s", (n++?" ":""), $3} END{print ""}' "${ref_file}"; } |
| 272 | # mutate_gen "a b c": the same list with the second-to-last id changed by one. |
| 273 | mutate_gen() { awk '{ $(NF-1) = $(NF-1) + 1; print }' <<<"$1"; } |
| 274 | |
| 275 | if [[ "${1:-}" == --self-test ]]; then |
| 276 | tmp="$(mktemp -d)"; trap 'rm -rf "${tmp}"' EXIT |
| 277 | [[ -r "${ref_file}" ]] || { echo "FAIL ${ref_file} missing"; echo "AIENOS_CK_INFER_SELF_TEST: FAIL"; exit 1; } |
| 278 | ids="$(read_want_ids)"; gen="$(read_want_gen)" |
| 279 | [[ "$(wc -w <<<"${ids}")" == 17 && "$(awk '{print $1,$2,$3,$4}' <<<"${ids}")" == "128000 128006 882 128007" ]] \ |
| 280 | && echo "PASS reference ids line parsed (17 ids, first four 128000 128006 882 128007)" || { echo "FAIL reference ids line"; echo "AIENOS_CK_INFER_SELF_TEST: FAIL"; exit 1; } |
| 281 | [[ "${gen}" == "791 6864 315 9822 374 12366 13 128009" ]] \ |
| 282 | && echo "PASS reference step tokens parsed (${gen})" || { echo "FAIL reference step tokens '${gen}'"; echo "AIENOS_CK_INFER_SELF_TEST: FAIL"; exit 1; } |
| 283 | sz=807694368 |
| 284 | good() { printf 'kernel: alive\ninfer: fw_cfg base=0x9020000 dma=yes file=opt/aienos/model size=%s\ninfer: ingest bytes=%s dma_us=42340\ninfer: sha256=%s sha_us=8568801\ninfer: probe rc=0 tensors=147 vocab=128256 ids=128000,128006,882,128007 prompt_len=17 layers=16 heap_peak_kib=24695 probe_us=1116277\ninfer: prefill_us=400000000 prompt_len=17\ninfer: tokens=791 6864 315 9822 374 12366 13 128009\ninfer: tok_us=23000000,23000000,23000000,23000000,23000000,23000000,23000000\ninfer: decode_tokens=7 mean_tok_us=23000000\ninfer: text=The capital of France is Paris.\nAIENOS_CK_INFER: PASS\ninfer: session open rc=0 tensors=147 vocab=128256 layers=16 kv_capacity=64 max_new=16 max_prompt_bytes=256 heap_reserve_kib=64 heap_used_kib=20000 open_us=1000000\ninfer: session refusals open_twice=-12 prompt_bytes=-16 step_idle=0\ninfer: session prompt=1 ids=14 cancel_after_steps=3 steps=3 prefill=3 produced=0 idle_step=done heap_used_kib=20010 rc=1\ninfer: session prompt=2 prompt_head=128000,128006,882,128007 expect=match\ninfer: session prompt=2 ids=17 steps=25 prefill=17 produced=8 last=yes prefill_us=400000000 mean_tok_us=23000000 tokens=791 6864 315 9822 374 12366 13 128009 text=The capital of France is Paris. heap_used_kib=20020 request_us=600000000 rc=3\ninfer: session prompt=3 ids=14 cancel_after_steps=1 steps=1 prefill=1 produced=0 idle_step=done heap_used_kib=20030 rc=1\ninfer: session close rc=0 heap_used_kib=20030 heap_peak_kib=24695 frames_free_before=900000 frames_free_after=900000 rc=0\nAIENOS_CK_INFER_SESSION: PASS\ninfer: unloaded heap_live_kib=1234 frames_free_before=900000 frames_free_after=900000 frames_returned=262705 rc=0,0\nreport_kind: final\n' "${sz}" "${sz}" "${pinned_sha}"; } |
| 285 | st=0 |
| 286 | chk() { infer_failed=0; out="$(infer_check_log "$3" "${sz}" "${pinned_sha}" "${ids}" 1 "${gen}")"; grep -q '^FAIL' <<<"${out}" && infer_failed=1 |
| 287 | if [[ "$2" == pass && ${infer_failed} == 0 ]] || [[ "$2" == fail && ${infer_failed} == 1 && -n "$4" ]] && { [[ "$2" == pass ]] || grep -q "^FAIL.*$4" <<<"${out}"; }; then echo "PASS self-test $1"; else echo "FAIL self-test $1"; st=1; fi; } |
| 288 | good >"${tmp}/g"; chk good pass "${tmp}/g" |
| 289 | sed 's/rc=0 /rc=-2 /' "${tmp}/g" >"${tmp}/a"; chk bad-header-rc fail "${tmp}/a" "probe returned -2" |
| 290 | sed 's/tensors=147/tensors=146/' "${tmp}/g" >"${tmp}/b"; chk wrong-tensors fail "${tmp}/b" "tensor count 146" |
| 291 | sed 's/vocab=128256/vocab=128255/' "${tmp}/g" >"${tmp}/c"; chk wrong-vocab fail "${tmp}/c" "vocab size 128255" |
| 292 | sed 's/ids=128000,/ids=128001,/' "${tmp}/g" >"${tmp}/d"; chk wrong-id0 fail "${tmp}/d" "first four prompt ids" |
| 293 | sed 's/,128007 prompt_len/,128008 prompt_len/' "${tmp}/g" >"${tmp}/e"; chk wrong-id3 fail "${tmp}/e" "first four prompt ids" |
| 294 | sed 's/prompt_len=17 layers/prompt_len=16 layers/' "${tmp}/g" >"${tmp}/f"; chk wrong-prompt-len fail "${tmp}/f" "prompt length 16" |
| 295 | sed 's/layers=16/layers=15/' "${tmp}/g" >"${tmp}/h"; chk wrong-layers fail "${tmp}/h" "layers 15" |
| 296 | sed 's/ingest bytes=807694368/ingest bytes=807694367/' "${tmp}/g" >"${tmp}/i"; chk short-ingest fail "${tmp}/i" "kernel ingested" |
| 297 | sed 's/size=807694368/size=807694367/' "${tmp}/g" >"${tmp}/i2"; chk wrong-fwcfg-size fail "${tmp}/i2" "fw_cfg file size" |
| 298 | sed "s/sha256=${pinned_sha}/sha256=0${pinned_sha:1}/" "${tmp}/g" >"${tmp}/j"; chk wrong-sha fail "${tmp}/j" "kernel SHA-256" |
| 299 | sed 's/heap_peak_kib=24695/heap_peak_kib=0/' "${tmp}/g" >"${tmp}/k"; chk zero-heap fail "${tmp}/k" "allocator peak" |
| 300 | # a log whose sha equals the host value but is not the pinned model hash |
| 301 | sed "s|${pinned_sha}|0${pinned_sha:1}|" "${tmp}/g" >"${tmp}/g2" |
| 302 | out="$(infer_check_log "${tmp}/g2" "${sz}" "0${pinned_sha:1}" "${ids}" 1 "${gen}")" |
| 303 | grep -q '^FAIL SHA-256 is not the pinned' <<<"${out}" && ! grep -q '^FAIL kernel SHA-256' <<<"${out}" \ |
| 304 | && echo "PASS self-test not-the-pinned-model" || { echo "FAIL self-test not-the-pinned-model"; st=1; } |
| 305 | grep -v '^AIENOS_CK_INFER' "${tmp}/g" >"${tmp}/l"; chk no-kernel-verdict fail "${tmp}/l" "kernel did not report" |
| 306 | grep -v '^infer: probe' "${tmp}/g" >"${tmp}/m"; chk no-probe-line fail "${tmp}/m" "infer: probe line" |
| 307 | grep -v '^kernel: alive' "${tmp}/g" >"${tmp}/n"; chk not-alive fail "${tmp}/n" "never reached kernel: alive" |
| 308 | { cat "${tmp}/g"; echo "report_kind: fault"; } >"${tmp}/o"; chk fault fail "${tmp}/o" "panic or fault" |
| 309 | grep -v '^report_kind: final' "${tmp}/g" >"${tmp}/p"; chk no-final fail "${tmp}/p" "no final report" |
| 310 | { cat "${tmp}/g"; grep '^infer: probe' "${tmp}/g"; } >"${tmp}/q"; chk two-probe-lines fail "${tmp}/q" "infer: probe line" |
| 311 | # cut 2: generation, text, timing, unload |
| 312 | sed 's/tokens=791 6864 315 9822 374 12366 13 128009/tokens=791 6864 315 9822 374 12367 13 128009/' "${tmp}/g" >"${tmp}/r"; chk wrong-token fail "${tmp}/r" "generated ids" |
| 313 | sed 's/ 13 128009$/ 13/' "${tmp}/g" >"${tmp}/s"; chk short-tokens fail "${tmp}/s" "generated ids" |
| 314 | sed 's/tokens=791 6864/tokens=6864 791/' "${tmp}/g" >"${tmp}/s2"; chk swapped-tokens fail "${tmp}/s2" "generated ids" |
| 315 | grep -v '^infer: tokens=' "${tmp}/g" >"${tmp}/t"; chk no-tokens-line fail "${tmp}/t" "infer: tokens line" |
| 316 | sed 's/France is Paris\./France is Lyon./' "${tmp}/g" >"${tmp}/u"; chk missing-paris fail "${tmp}/u" "decoded text lacks" |
| 317 | grep -v '^infer: text=' "${tmp}/g" >"${tmp}/v"; chk no-text-line fail "${tmp}/v" "infer: text line" |
| 318 | grep -v '^infer: unloaded' "${tmp}/g" >"${tmp}/w"; chk no-unload-line fail "${tmp}/w" "infer: unloaded line" |
| 319 | sed 's/frames_free_after=900000/frames_free_after=899999/' "${tmp}/g" >"${tmp}/x"; chk frames-leaked fail "${tmp}/x" "unload: free frames" |
| 320 | sed 's/rc=0,0$/rc=0,-1/' "${tmp}/g" >"${tmp}/y"; chk frame-return-refused fail "${tmp}/y" "unload: free frames" |
| 321 | sed 's/decode_tokens=7 mean_tok_us=23000000/decode_tokens=6 mean_tok_us=23000000/' "${tmp}/g" >"${tmp}/z"; chk wrong-decode-count fail "${tmp}/z" "decode timing line" |
| 322 | sed 's/mean_tok_us=23000000/mean_tok_us=0/' "${tmp}/g" >"${tmp}/z2"; chk zero-tok-time fail "${tmp}/z2" "decode timing line" |
| 323 | grep -v '^infer: decode_tokens=' "${tmp}/g" >"${tmp}/z3"; chk no-timing-line fail "${tmp}/z3" "decode timing line" |
| 324 | # unload line after the final report: the OS did not continue after the unload |
| 325 | { grep -v '^infer: unloaded' "${tmp}/g"; grep '^infer: unloaded' "${tmp}/g"; } >"${tmp}/z4"; chk unload-after-final fail "${tmp}/z4" "not before the final report" |
| 326 | # native v1 P06: the bounded session |
| 327 | grep -v '^AIENOS_CK_INFER_SESSION' "${tmp}/g" >"${tmp}/s1"; chk no-session-verdict fail "${tmp}/s1" "AIENOS_CK_INFER_SESSION" |
| 328 | grep -v '^infer: session open' "${tmp}/g" >"${tmp}/s2"; chk no-session-open fail "${tmp}/s2" "session open line" |
| 329 | sed 's/kv_capacity=64/kv_capacity=65/' "${tmp}/g" >"${tmp}/s3"; chk session-limits-drift fail "${tmp}/s3" "session limits" |
| 330 | sed 's/open_twice=-12/open_twice=0/' "${tmp}/g" >"${tmp}/s4"; chk session-second-open-allowed fail "${tmp}/s4" "session refusals" |
| 331 | sed 's/prompt_bytes=-16/prompt_bytes=14/' "${tmp}/g" >"${tmp}/s5"; chk session-long-prompt-accepted fail "${tmp}/s5" "session refusals" |
| 332 | sed 's/cancel_after_steps=3 steps=3/cancel_after_steps=3 steps=4/' "${tmp}/g" >"${tmp}/s6"; chk session-cancel-overran fail "${tmp}/s6" "session cancel 1" |
| 333 | sed 's/idle_step=done heap_used_kib=20030/idle_step=busy heap_used_kib=20030/' "${tmp}/g" >"${tmp}/s7"; chk session-not-idle-after-cancel fail "${tmp}/s7" "session cancel lines" |
| 334 | sed 's/session prompt=2 ids=17 steps=25 prefill=17 produced=8 last=yes/session prompt=2 ids=17 steps=24 prefill=17 produced=7 last=no/' "${tmp}/g" >"${tmp}/s8"; chk session-reply-unfinished fail "${tmp}/s8" "session reply" |
| 335 | sed 's/128009 text=The capital/128010 text=The capital/' "${tmp}/g" >"${tmp}/s9"; chk session-wrong-token fail "${tmp}/s9" "session reply ids" |
| 336 | sed 's/Paris\. heap_used_kib/Lyon. heap_used_kib/' "${tmp}/g" >"${tmp}/s10"; chk session-missing-paris fail "${tmp}/s10" "session reply text lacks" |
| 337 | sed 's/prompt_head=128000,128006,882,128007 expect=match/prompt_head=128000,128006,883,128007 expect=match/' "${tmp}/g" >"${tmp}/s15"; chk session-wrong-prompt-head fail "${tmp}/s15" "session prompt head" |
| 338 | sed 's/expect=match/expect=MISMATCH/' "${tmp}/g" >"${tmp}/s16"; chk session-head-mismatch-flag fail "${tmp}/s16" "session prompt head" |
| 339 | grep -v 'session prompt=2 prompt_head=' "${tmp}/g" >"${tmp}/s17"; chk session-no-head-line fail "${tmp}/s17" "prompt_head line" |
| 340 | sed 's/session close rc=0 heap_used_kib=20030 heap_peak_kib=24695 frames_free_before=900000 frames_free_after=900000/session close rc=0 heap_used_kib=20030 heap_peak_kib=24695 frames_free_before=900000 frames_free_after=899999/' "${tmp}/g" >"${tmp}/s11"; chk session-frames-leaked fail "${tmp}/s11" "session close" |
| 341 | sed 's/session close rc=0/session close rc=-12/' "${tmp}/g" >"${tmp}/s12"; chk session-close-refused fail "${tmp}/s12" "session close" |
| 342 | { grep -v '^AIENOS_CK_INFER: PASS' "${tmp}/g"; } >"${tmp}/s13"; chk no-oneshot-verdict-still fail "${tmp}/s13" "kernel did not report AIENOS_CK_INFER: PASS" |
| 343 | # session close after the unload: the resident model was unloaded under a live session |
| 344 | { grep -v '^infer: session close' "${tmp}/g" | sed '/^report_kind: final/i infer: session close rc=0 heap_used_kib=20030 heap_peak_kib=24695 frames_free_before=900000 frames_free_after=900000 rc=0'; } >"${tmp}/s14"; chk session-close-after-unload fail "${tmp}/s14" "not between" |
| 345 | # the mutated expected list must make the good log FAIL (used as the live control) |
| 346 | infer_failed=0; out="$(infer_check_log "${tmp}/g" "${sz}" "${pinned_sha}" "${ids}" 1 "$(mutate_gen "${gen}")")" |
| 347 | grep -q '^FAIL generated ids' <<<"${out}" && echo "PASS self-test mutated-expected-list" || { echo "FAIL self-test mutated-expected-list"; st=1; } |
| 348 | [[ ${st} == 0 ]] && { echo "AIENOS_CK_INFER_SELF_TEST: PASS"; exit 0; } |
| 349 | echo "AIENOS_CK_INFER_SELF_TEST: FAIL"; exit 1 |
| 350 | fi |
| 351 | |
| 352 | negctl=0 |
| 353 | [[ "${1:-}" == --negative-control ]] && negctl=1 |
| 354 | # Model source: fwcfg (default, TEST-ONLY QEMU channel) or disk (MODEL.MAP on a |
| 355 | # GPT image, read by the stub through the firmware Block I/O: the hardware |
| 356 | # path, proven here under QEMU). --disk-negative-control corrupts the declared |
| 357 | # SHA-256 in MODEL.MAP: the stub reads fine, the kernel must refuse to decode. |
| 358 | case "${1:-}" in |
| 359 | --disk) source_mode=disk ;; |
| 360 | --disk-negative-control) source_mode=disk disk_negctl=1 ;; |
| 361 | esac |
| 362 | if [[ ${negctl} == 1 ]]; then verdict_name=AIENOS_CK_INFER_NEGATIVE_CONTROL; fi |
| 363 | if [[ ${disk_negctl} == 1 ]]; then verdict_name=AIENOS_CK_INFER_DISK_NEGATIVE_CONTROL; fi |
| 364 | |
| 365 | notrun() { echo "NOT_RUN $*"; echo "${verdict_name}: NOT_RUN ($*)"; exit 2; } |
| 366 | [[ -r "${model}" ]] || notrun "model file ${model} not found" |
| 367 | [[ -r "${ref_file}" ]] || notrun "${ref_file} not found" |
| 368 | command -v qemu-system-aarch64 >/dev/null || notrun "qemu-system-aarch64 not installed" |
| 369 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 370 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 371 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || notrun "AAVMF firmware not found" |
| 372 | command -v cargo >/dev/null || notrun "cargo not installed" |
| 373 | if ! rustc --print target-list 2>/dev/null | grep -qx aarch64-unknown-none \ |
| 374 | || ! [[ -d "$(rustc --print sysroot)/lib/rustlib/aarch64-unknown-none/lib" ]]; then |
| 375 | notrun "Rust target aarch64-unknown-none not installed (rustup target add aarch64-unknown-none)" |
| 376 | fi |
| 377 | |
| 378 | cross="" |
| 379 | if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi |
| 380 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 381 | out="${repo_root}/target/native-kernel-infer" |
| 382 | lib="${repo_root}/target/infer-kernel/aarch64-unknown-none/release/libaienos_infer_kernel.a" |
| 383 | CARGO_TARGET_DIR="${repo_root}/target/infer-kernel" cargo build -q --release --target aarch64-unknown-none \ |
| 384 | --manifest-path crates/aienos-infer-kernel/Cargo.toml |
| 385 | make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" CK_INFER_LIB="${lib}" >/dev/null |
| 386 | [[ "${source_mode}" != disk ]] || make -s -C native/kernel OUT="${out}" gpt-image model-map >/dev/null |
| 387 | |
| 388 | # Machine-wide quiet flag: read only, never written here. If someone holds |
| 389 | # it, a CPU-heavy TCG run would disturb their measurement: NOT_RUN. |
| 390 | # Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): |
| 391 | # scripts/lib_gate_hold.sh (aienos#278). |
| 392 | . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" |
| 393 | if ! gh_quiet_check; then |
| 394 | echo "NOT_RUN ${gh_why}" |
| 395 | echo "${verdict_name}: NOT_RUN" |
| 396 | exit 3 |
| 397 | fi |
| 398 | if ! gh_lock_take qemu_ck_infer_test "${AIENOS_GATE_MINUTES:-60}"; then |
| 399 | echo "NOT_RUN ${gh_why}" |
| 400 | echo "${verdict_name}: NOT_RUN" |
| 401 | exit 3 |
| 402 | fi |
| 403 | # Release only this run's own gate lock record, at most once. |
| 404 | release_lock() { gh_lock_release; } |
| 405 | work="$(mktemp -d)" |
| 406 | cleanup() { rm -rf "${work}"; release_lock; } |
| 407 | trap cleanup EXIT |
| 408 | |
| 409 | use_model="${model}" |
| 410 | if [[ ${negctl} == 1 ]]; then |
| 411 | # Same bytes, first four (the GGUF magic "GGUF") overwritten: the header |
| 412 | # parser must refuse it. Host-side copy; the original is never touched. |
| 413 | use_model="${work}/corrupt.gguf" |
| 414 | cp "${model}" "${use_model}" |
| 415 | printf 'XXXX' | dd of="${use_model}" bs=1 count=4 conv=notrunc status=none |
| 416 | fi |
| 417 | want_size="$(stat -c %s "${use_model}")" |
| 418 | want_sha="$(sha256sum "${use_model}" | cut -d' ' -f1)" |
| 419 | want_ids="$(read_want_ids)" |
| 420 | want_gen="$(read_want_gen)" |
| 421 | |
| 422 | mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" |
| 423 | touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" |
| 424 | cp "${out}/BOOTAA64.EFI" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" |
| 425 | cp "${vars_fd}" "${work}/vars.fd" |
| 426 | # Boot media. disk: a GPT image with the ESP (BOOTAA64.EFI + MODEL.MAP) and the |
| 427 | # model in three out-of-order extents, on an NVMe controller; the stub reads |
| 428 | # the model through the firmware Block I/O driver (native/boot/efi_model.c). |
| 429 | # fwcfg: the TEST-ONLY channel, unchanged. |
| 430 | if [[ "${source_mode}" == disk ]]; then |
| 431 | img="${work}/disk.img" |
| 432 | esp_mib=64 |
| 433 | model_bytes="$(stat -c %s "${use_model}")" |
| 434 | total_mib=$(( (model_bytes + 1048575) / 1048576 + esp_mib + 16 )) |
| 435 | gpt_line="$("${out}/host/ck_gpt_image" create "${img}" 512 "${total_mib}" esp-model "${esp_mib}" "${use_model}")" |
| 436 | echo "${gpt_line}" |
| 437 | esp_first="$(sed -nE 's/.* esp_first_lba=([0-9]+).*/\1/p' <<<"${gpt_line}")" |
| 438 | extents="$(sed -nE 's/.* model_extents=([0-9:,]+).*/\1/p' <<<"${gpt_line}")" |
| 439 | [[ -n "${esp_first}" && -n "${extents}" ]] || { echo "FAIL ck_gpt_image esp-model gave no geometry"; echo "${verdict_name}: FAIL"; exit 1; } |
| 440 | mkfs.vfat -F 32 -s 1 -S 512 --offset "${esp_first}" "${img}" $(( esp_mib * 1024 )) >/dev/null 2>&1 |
| 441 | mo="${img}@@$(( esp_first * 512 ))" |
| 442 | mmd -i "${mo}" ::/EFI ::/EFI/BOOT ::/EFI/AIENOS |
| 443 | mcopy -i "${mo}" "${out}/BOOTAA64.EFI" ::/EFI/BOOT/BOOTAA64.EFI |
| 444 | map_args=() |
| 445 | IFS=, read -ra exts <<<"${extents}" |
| 446 | for e in "${exts[@]}"; do map_args+=(--extent "${e}"); done |
| 447 | corrupt=() |
| 448 | [[ "${disk_negctl}" == 1 ]] && corrupt=(--corrupt-sha) |
| 449 | "${out}/host/ck_model_map" make "${work}/MODEL.MAP" --file "${use_model}" --disk "${img}" "${map_args[@]}" "${corrupt[@]}" |
| 450 | if [[ "${disk_negctl}" == 0 ]]; then |
| 451 | "${out}/host/ck_model_map" check "${work}/MODEL.MAP" --disk "${img}" || { echo "FAIL host-side map check"; echo "${verdict_name}: FAIL"; exit 1; } |
| 452 | fi |
| 453 | mcopy -i "${mo}" "${work}/MODEL.MAP" ::/EFI/AIENOS/MODEL.MAP |
| 454 | drive_args=(-drive if=none,id=disk,format=raw,file="${img}" -device nvme,drive=disk,serial=aienos0001) |
| 455 | else |
| 456 | drive_args=(-drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" -device virtio-blk-pci,drive=esp |
| 457 | -fw_cfg name=opt/aienos/model,file="${use_model}") |
| 458 | fi |
| 459 | log="${work}/serial.log" |
| 460 | |
| 461 | started=$(date +%s) |
| 462 | set +e |
| 463 | timeout "${AIENOS_QEMU_TIMEOUT:-3600}" qemu-system-aarch64 \ |
| 464 | -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 2 -m 4096 \ |
| 465 | -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ |
| 466 | -drive if=pflash,format=raw,file="${work}/vars.fd" \ |
| 467 | "${drive_args[@]}" \ |
| 468 | -device ramfb -display none -nic none \ |
| 469 | -serial file:"${log}" -no-reboot |
| 470 | qemu_status=$? |
| 471 | set -e |
| 472 | elapsed=$(( $(date +%s) - started )) |
| 473 | release_lock |
| 474 | |
| 475 | tr -d '\r' <"${log}" >"${work}/serial.txt" |
| 476 | [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_infer_serial.log" |
| 477 | echo "qemu exit ${qemu_status} after ${elapsed} s (commit ${commit:0:12})" |
| 478 | |
| 479 | infer_failed=0 |
| 480 | results="$(infer_check_log "${work}/serial.txt" "${want_size}" "${want_sha}" "${want_ids}" "$(( 1 - negctl ))" "${want_gen}")" || true |
| 481 | grep -q '^FAIL' <<<"${results}" && infer_failed=1 |
| 482 | printf '%s\n' "${results}" |
| 483 | if [[ "${qemu_status}" == 0 ]]; then echo "PASS QEMU exit 0 (PSCI reset)"; else echo "FAIL QEMU exit ${qemu_status}"; infer_failed=1; fi |
| 484 | grep -E '^infer: ' "${work}/serial.txt" || true |
| 485 | if [[ ${negctl} == 1 ]]; then |
| 486 | # The control passes only when the corrupted header is what made the gate fail. |
| 487 | if [[ ${infer_failed} == 1 ]] && grep -q '^infer: probe rc=-2 ' "${work}/serial.txt" \ |
| 488 | && grep -q '^FAIL probe returned -2' <<<"${results}" && grep -q "kernel: alive" "${work}/serial.txt"; then |
| 489 | echo "AIENOS_CK_INFER_NEGATIVE_CONTROL: PASS (corrupt GGUF magic -> probe rc=-2 -> gate FAIL)"; exit 0 |
| 490 | fi |
| 491 | echo "AIENOS_CK_INFER_NEGATIVE_CONTROL: FAIL (corrupt magic was not caught as probe rc=-2)"; exit 1 |
| 492 | fi |
| 493 | if [[ ${disk_negctl} == 1 ]]; then |
| 494 | # The control passes only when the corrupted declared SHA-256 is what made |
| 495 | # the gate fail: the stub read the model, the kernel hashed it, refused to |
| 496 | # decode (no probe line), and reported FAIL itself. |
| 497 | s="${work}/serial.txt" |
| 498 | if [[ ${infer_failed} == 1 ]] && grep -q '^infer: sha256 mismatch declared=' "${s}" \ |
| 499 | && ! grep -q '^infer: probe' "${s}" && grep -q '^AIENOS_CK_INFER: FAIL' "${s}" \ |
| 500 | && grep -q '^model: base=0x' "${s}" && grep -q 'kernel: alive' "${s}"; then |
| 501 | echo "AIENOS_CK_INFER_DISK_NEGATIVE_CONTROL: PASS (corrupt MODEL.MAP sha256 -> kernel refused to decode -> gate FAIL)"; exit 0 |
| 502 | fi |
| 503 | echo "AIENOS_CK_INFER_DISK_NEGATIVE_CONTROL: FAIL (corrupt declared sha256 was not refused by the kernel)"; exit 1 |
| 504 | fi |
| 505 | # Live control on the same serial log: a mutated expected-id list must FAIL. |
| 506 | mut="$(infer_check_log "${work}/serial.txt" "${want_size}" "${want_sha}" "${want_ids}" 1 "$(mutate_gen "${want_gen}")")" || true |
| 507 | if grep -q '^FAIL generated ids' <<<"${mut}"; then |
| 508 | echo "PASS control: mutated expected ids ($(mutate_gen "${want_gen}")) make the token check FAIL" |
| 509 | else |
| 510 | echo "FAIL control: the token check did not FAIL against a mutated expected list"; infer_failed=1 |
| 511 | fi |
| 512 | if [[ "${infer_failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then |
| 513 | echo "---- serial console ----"; cat "${work}/serial.txt" |
| 514 | fi |
| 515 | if [[ "${infer_failed}" == 0 ]]; then echo "AIENOS_CK_INFER: PASS"; exit 0; fi |
| 516 | echo "AIENOS_CK_INFER: FAIL"; exit 1 |