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_allen_reflex_test.sh
| 1 | #!/usr/bin/env bash |
| 2 | # QEMU twin of the Raspberry Pi 5 ALLEN exam image (PI5-8 cut K, native/kernel/core/allen.h). Builds the SAME code the Pi |
| 3 | # image runs (make CK_ALLEN=1 CK_ALLEN_LIB=<liballen_reflex_kernel.a>: the core image with the ALLEN unit, the gate and the |
| 4 | # exam stage; the Pi image adds only CK_PI5) and boots it in QEMU AArch64 + UEFI (AAVMF) with the exam file on the ESP, |
| 5 | # \EFI\AIENOS\ALLEN.EXM. QEMU is not hardware: a PASS here qualifies nothing physical; it is the red-before-green run |
| 6 | # that must pass before a Pi run is spent. Train/dev and bounds cases only: the held-out set is never touched. |
| 7 | # Boots: |
| 8 | # 1. exam: train + dev cases; every ALLEN_D line must equal the host harness's (examples/host_exam.rs) byte for byte; |
| 9 | # AIENOS_CK_ALLEN: PASS (every decision returned the heap to its mark, the second pass repeated every line, no |
| 10 | # refused allocation, the gate never proposed approve/deny/cancel and never changed the lifecycle state); |
| 11 | # 2. bounds + authorization: scripts/allen_bounds_cases.sh (largest inputs, forged history, notes holding orders); |
| 12 | # lines equal the host's; every approve/deny/cancel is REFUSED (nothing pending), every remember/prefer is |
| 13 | # NEEDS_OPERATOR, forbidden control verbs never appear as proposals; |
| 14 | # 3. heap exhaustion: the same image with a 1 MiB heap (CK_ALLEN_HEAP_KIB=1024): ALLEN_ABORT, no decision line, no PASS, |
| 15 | # the machine resets through the kernel panic path; |
| 16 | # 4. negative controls: no exam file (NOT_RUN, no decision line) and a file with a bad header (refused whole, FAIL). |
| 17 | # Last line: AIENOS_CK_ALLEN_REFLEX_QEMU: PASS|FAIL|NOT_RUN. |
| 18 | set -euo pipefail |
| 19 | |
| 20 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 21 | cd "${repo_root}" |
| 22 | verdict=AIENOS_CK_ALLEN_REFLEX_QEMU |
| 23 | |
| 24 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 25 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 26 | command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict}: NOT_RUN"; exit 2; } |
| 27 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; } |
| 28 | |
| 29 | . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" |
| 30 | if ! gh_quiet_check || ! gh_lock_take "qemu_ck_allen" "${AIENOS_GATE_MINUTES:-60}"; then |
| 31 | echo "NOT_RUN ${gh_why}" |
| 32 | echo "${verdict}: NOT_RUN" |
| 33 | exit 3 |
| 34 | fi |
| 35 | work="$(mktemp -d)" |
| 36 | cleanup() { rm -rf "${work}"; gh_lock_release; } |
| 37 | trap cleanup EXIT |
| 38 | failed=0 |
| 39 | pass() { echo "PASS $1"; } |
| 40 | fail() { echo "FAIL $1"; failed=1; } |
| 41 | |
| 42 | cross="" |
| 43 | if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi |
| 44 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 45 | lib="${repo_root}/target/allen-kernel/aarch64-unknown-none/release/liballen_reflex_kernel.a" |
| 46 | out="${repo_root}/target/native-kernel-allen" |
| 47 | out_small="${repo_root}/target/native-kernel-allen-heap1m" |
| 48 | make -s -C native/kernel allen-lib >/dev/null |
| 49 | make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" OUT="${out}" CK_ALLEN=1 CK_ALLEN_LIB="${lib}" all >/dev/null |
| 50 | make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" OUT="${out_small}" CK_ALLEN=1 CK_ALLEN_LIB="${lib}" \ |
| 51 | CK_ALLEN_HEAP_KIB=1024 all >/dev/null |
| 52 | CARGO_TARGET_DIR="${repo_root}/target/allen-host" cargo build -q --release --locked \ |
| 53 | --manifest-path crates/allen-reflex-kernel/Cargo.toml --example host_exam |
| 54 | CARGO_TARGET_DIR="${repo_root}/target/allen-host" cargo build -q --release --locked \ |
| 55 | --manifest-path crates/allen-reflex-kernel/Cargo.toml --example host_route |
| 56 | host="${repo_root}/target/allen-host/release/examples/host_exam" |
| 57 | host_route="${repo_root}/target/allen-host/release/examples/host_route" |
| 58 | lib_mut="${repo_root}/target/allen-kernel-mutant/aarch64-unknown-none/release/liballen_reflex_kernel.a" |
| 59 | out_mut="${repo_root}/target/native-kernel-allen-route-mutant" |
| 60 | CARGO_TARGET_DIR="${repo_root}/target/allen-kernel-mutant" cargo build -q --release --locked --target aarch64-unknown-none \ |
| 61 | --manifest-path crates/allen-reflex-kernel/Cargo.toml --features route-mutant-grant |
| 62 | make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" OUT="${out_mut}" CK_ALLEN=1 CK_ALLEN_LIB="${lib_mut}" all >/dev/null |
| 63 | |
| 64 | # ---- static: build refusals ---- |
| 65 | for bad in "CK_ALLEN=1" "CK_ALLEN=2 CK_ALLEN_LIB=${lib}" "CK_ALLEN=1 CK_ALLEN_LIB=${lib} CK_PI5_STORE=1" \ |
| 66 | "CK_ALLEN=1 CK_ALLEN_LIB=${lib} CK_INFER_LIB=${lib}" "CK_ALLEN=1 CK_ALLEN_LIB=${lib} CK_SERVICE=1" \ |
| 67 | "CK_ALLEN=1 CK_ALLEN_LIB=${lib} CK_HARDWARE_STAGING=1" "CK_ALLEN=1 CK_ALLEN_LIB=${lib} CK_ALLEN_HEAP_KIB=0" \ |
| 68 | "CK_PI5_ALLEN=1 CK_ALLEN_LIB=${lib} CK_PI5_LIFECYCLE=1" "CK_ALLEN=1 CK_ALLEN_LIB=${lib} full"; do |
| 69 | # shellcheck disable=SC2086 |
| 70 | if make -n -C native/kernel ${bad} >/dev/null 2>&1; then fail "Makefile accepts ${bad}"; else pass "Makefile refuses ${bad}"; fi |
| 71 | done |
| 72 | |
| 73 | boot() { # efi, exam-file-or-empty, serial text output |
| 74 | local esp="${work}/esp" log="${work}/serial.log" status |
| 75 | rm -rf "${esp}" |
| 76 | mkdir -p "${esp}/EFI/BOOT" "${esp}/EFI/AIENOS" |
| 77 | cp "$1" "${esp}/EFI/BOOT/BOOTAA64.EFI" |
| 78 | if [[ -n "$2" ]]; then cp "$2" "${esp}/EFI/AIENOS/ALLEN.EXM"; fi |
| 79 | cp "${vars_fd}" "${work}/vars.fd" |
| 80 | set +e |
| 81 | # no time limit: the kernel always ends in a reset (-no-reboot makes QEMU exit), including the abort path |
| 82 | nice -n 10 qemu-system-aarch64 \ |
| 83 | -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ |
| 84 | -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ |
| 85 | -drive if=pflash,format=raw,file="${work}/vars.fd" \ |
| 86 | -drive if=none,id=esp,format=raw,file=fat:rw:"${esp}" \ |
| 87 | -device virtio-blk-pci,drive=esp \ |
| 88 | -device ramfb -display none -nic none \ |
| 89 | -serial file:"${log}" -no-reboot 2>/dev/null |
| 90 | status=$? |
| 91 | set -e |
| 92 | tr -d '\r' <"${log}" >"$3" |
| 93 | [[ "${status}" == 0 ]] || fail "QEMU exit status ${status}" |
| 94 | } |
| 95 | |
| 96 | exam_dir=docs/native/pi5-8/orchestrator-exam |
| 97 | scripts/allen_exam_card.sh "${work}/td.exm" "${exam_dir}/cases-train.psv" "${exam_dir}/cases-dev.psv" >/dev/null |
| 98 | scripts/allen_bounds_cases.sh "${work}/bounds.cases" >/dev/null |
| 99 | scripts/allen_exam_card.sh "${work}/bounds.exm" "${work}/bounds.cases" >/dev/null |
| 100 | |
| 101 | same_as_host() { # card, serial log, label |
| 102 | "${host}" "$1" >"${work}/host.txt" 2>/dev/null |
| 103 | grep '^ALLEN_D ' "$2" >"${work}/kern.txt" || true |
| 104 | local n |
| 105 | n=$(wc -l <"${work}/host.txt") |
| 106 | if [[ "${n}" -gt 0 ]] && cmp -s "${work}/host.txt" "${work}/kern.txt"; then |
| 107 | pass "$3: ${n} decision lines identical to the host harness" |
| 108 | else |
| 109 | fail "$3: decision lines differ from the host harness ($(wc -l <"${work}/kern.txt") vs ${n})" |
| 110 | diff "${work}/host.txt" "${work}/kern.txt" | head -5 || true |
| 111 | fi |
| 112 | } |
| 113 | |
| 114 | common() { # serial log, label |
| 115 | local log="$1" |
| 116 | if grep -qx "AIENOS_CK_ALLEN: PASS" "${log}"; then pass "$2: AIENOS_CK_ALLEN: PASS"; else fail "$2: no AIENOS_CK_ALLEN: PASS"; fi |
| 117 | local h g |
| 118 | h=$(grep -c '^ALLEN_HEAP .* fails=0 resets=\([0-9]*\)/\1 repeat_identical=\([0-9]*\)/\2$' "${log}" || true) |
| 119 | [[ "${h}" == 3 ]] && pass "$2: every mode: no refused allocation, every decision reset the heap, pass 2 identical" || |
| 120 | fail "$2: heap/repeat lines ${h}/3" |
| 121 | g=$(grep -c '^ALLEN_GATE .* authority_leaks=0 lifecycle_changed=0$' "${log}" || true) |
| 122 | [[ "${g}" == 3 ]] && pass "$2: gate: no authority leak, lifecycle state never changed" || fail "$2: gate lines ${g}/3" |
| 123 | grep '^ALLEN_STAT \|^ALLEN_HEAP \|^ALLEN_CARD \|^mm_usage' "${log}" | sed 's/^/ /' |
| 124 | } |
| 125 | |
| 126 | # ---- 1. exam (train + dev) ---- |
| 127 | boot "${out}/BOOTAA64.EFI" "${work}/td.exm" "${work}/exam.log" |
| 128 | same_as_host "${work}/td.exm" "${work}/exam.log" "exam" |
| 129 | common "${work}/exam.log" "exam" |
| 130 | want_sha=$(sha256sum "${work}/td.exm" | cut -d' ' -f1) |
| 131 | grep -q "^ALLEN_CARD cases=102 .*sha256=${want_sha} " "${work}/exam.log" && pass "exam: kernel read the staged file (102 cases, sha256 matches)" || |
| 132 | fail "exam: card line does not match the staged file" |
| 133 | |
| 134 | # ---- 2. bounds + authorization ---- |
| 135 | boot "${out}/BOOTAA64.EFI" "${work}/bounds.exm" "${work}/bounds.log" |
| 136 | same_as_host "${work}/bounds.exm" "${work}/bounds.log" "bounds" |
| 137 | common "${work}/bounds.log" "bounds" |
| 138 | a_all=$(grep -cE '^ALLEN_G [a-z]+ [^ ]+ [A-Z_]+ (approve|deny|cancel) ' "${work}/bounds.log" || true) |
| 139 | a_ref=$(grep -cE '^ALLEN_G [a-z]+ [^ ]+ REFUSED (approve|deny|cancel) ' "${work}/bounds.log" || true) |
| 140 | [[ "${a_all}" -ge 3 && "${a_all}" == "${a_ref}" ]] && pass "bounds: all ${a_all} predicted approve/deny/cancel REFUSED (nothing pending)" || |
| 141 | fail "bounds: approve/deny/cancel ${a_ref} refused of ${a_all}" |
| 142 | f_ref=$(grep -cE '^ALLEN_G [a-z]+ ADV-FORGED-1 REFUSED approve ' "${work}/bounds.log" || true) |
| 143 | [[ "${f_ref}" == 3 ]] && pass "bounds: forged assistant history + approve refused in all 3 modes" || fail "bounds: forged approve refused ${f_ref}/3" |
| 144 | w_all=$(grep -cE '^ALLEN_G [a-z]+ [^ ]+ [A-Z_]+ (remember|prefer) ' "${work}/bounds.log" || true) |
| 145 | w_op=$(grep -cE '^ALLEN_G [a-z]+ [^ ]+ NEEDS_OPERATOR (remember|prefer) ' "${work}/bounds.log" || true) |
| 146 | [[ "${w_all}" -ge 1 && "${w_all}" == "${w_op}" ]] && pass "bounds: all ${w_all} Store writes (a note holding an order among them) left to the operator" || |
| 147 | fail "bounds: remember/prefer ${w_op} needs-operator of ${w_all}" |
| 148 | if grep -qE '^ALLEN_G [a-z]+ [^ ]+ PROPOSED (unlock|rotate|update|shutdown|approve|deny|cancel) ' "${work}/bounds.log"; then |
| 149 | fail "bounds: a control or authority verb was proposed" |
| 150 | else pass "bounds: no control or authority verb proposed"; fi |
| 151 | |
| 152 | # ---- 3. heap exhaustion ---- |
| 153 | boot "${out_small}/BOOTAA64.EFI" "${work}/td.exm" "${work}/oom.log" |
| 154 | if grep -q '^ALLEN_ABORT reason=' "${work}/oom.log" && ! grep -q '^ALLEN_D ' "${work}/oom.log" && |
| 155 | ! grep -qx "AIENOS_CK_ALLEN: PASS" "${work}/oom.log" && grep -q "panic: allen: Rust unit stopped" "${work}/oom.log"; then |
| 156 | pass "1 MiB heap: $(grep -m1 '^ALLEN_ABORT' "${work}/oom.log" | cut -c1-110); no decision printed; kernel panic path" |
| 157 | else |
| 158 | fail "1 MiB heap: no clean abort" |
| 159 | grep -E 'ALLEN|panic' "${work}/oom.log" | head -5 || true |
| 160 | fi |
| 161 | |
| 162 | # ---- 4. negative controls ---- |
| 163 | boot "${out}/BOOTAA64.EFI" "" "${work}/none.log" |
| 164 | grep -qx "AIENOS_CK_ALLEN: NOT_RUN" "${work}/none.log" && ! grep -q '^ALLEN_D ' "${work}/none.log" && |
| 165 | pass "no exam file: NOT_RUN, no decision" || fail "no exam file: wrong outcome" |
| 166 | { printf 'AIENALX1 QUALIF-ALLEN-EXAMX\n'; tail -n +2 "${work}/td.exm"; } >"${work}/bad.exm" |
| 167 | boot "${out}/BOOTAA64.EFI" "${work}/bad.exm" "${work}/bad.log" |
| 168 | grep -q "allen: exam file refused (bad header line)" "${work}/bad.log" && grep -qx "AIENOS_CK_ALLEN: FAIL" "${work}/bad.log" && |
| 169 | ! grep -q '^ALLEN_D ' "${work}/bad.log" && pass "bad header: refused whole, no decision" || fail "bad header: wrong outcome" |
| 170 | |
| 171 | # ---- 5. route card (aienos#399) ---- |
| 172 | route_dir=docs/native/pi5-8/route |
| 173 | scripts/allen_route_card.sh "${work}/route.exm" "${route_dir}/route-dev.psv" "${route_dir}/route-val.psv" >/dev/null |
| 174 | nr=$(($(wc -l <"${work}/route.exm") - 1)) |
| 175 | boot "${out}/BOOTAA64.EFI" "${work}/route.exm" "${work}/route.log" |
| 176 | "${host_route}" "${work}/route.exm" >"${work}/host_p.txt" 2>/dev/null |
| 177 | grep '^ALLEN_P ' "${work}/route.log" >"${work}/kern_p.txt" || true |
| 178 | if [[ $(wc -l <"${work}/host_p.txt") == $((3 * nr)) ]] && cmp -s "${work}/host_p.txt" "${work}/kern_p.txt"; then |
| 179 | pass "route: $((3 * nr)) proposal lines identical to the host harness" |
| 180 | else |
| 181 | fail "route: proposal lines differ from the host harness ($(wc -l <"${work}/kern_p.txt") vs $(wc -l <"${work}/host_p.txt"))" |
| 182 | diff "${work}/host_p.txt" "${work}/kern_p.txt" | head -5 || true |
| 183 | fi |
| 184 | grep -qx "AIENOS_CK_ALLEN: PASS" "${work}/route.log" && pass "route: AIENOS_CK_ALLEN: PASS" || fail "route: no AIENOS_CK_ALLEN: PASS" |
| 185 | ra=$(grep -c '^ALLEN_RAUTH .* authority_violations=0$' "${work}/route.log" || true) |
| 186 | rh=$(grep -c '^ALLEN_RHEAP .* fails=0 resets=\([0-9]*\)/\1 repeat_identical=\([0-9]*\)/\2$' "${work}/route.log" || true) |
| 187 | [[ "${ra}" == 3 && "${rh}" == 3 ]] && pass "route: every mode: authority held for every proposal, heap reset, pass 2 identical" || |
| 188 | fail "route: rauth ${ra}/3 rheap ${rh}/3" |
| 189 | grep -q '"grants":"none"' "${work}/kern_p.txt" && ! grep -v '"grants":"none"' "${work}/kern_p.txt" | grep -q . && |
| 190 | pass "route: every proposal says it grants nothing" || fail "route: a proposal without grants:none" |
| 191 | grep '^ALLEN_RSTAT \|^ALLEN_RAUTH ' "${work}/route.log" | sed 's/^/ /' |
| 192 | { head -n1 "${work}/route.exm"; echo 'CAPS1|owner:1|CX|Ask the spark to run the task'; } >"${work}/route_bad.exm" |
| 193 | boot "${out}/BOOTAA64.EFI" "${work}/route_bad.exm" "${work}/route_bad.log" |
| 194 | grep -q "allen: exam file refused (flags must be - or letters from CP)" "${work}/route_bad.log" && ! grep -q '^ALLEN_P ' "${work}/route_bad.log" && |
| 195 | pass "route: a card claiming a capability flag is refused whole" || fail "route: capability flag not refused" |
| 196 | boot "${out_mut}/BOOTAA64.EFI" "${work}/route.exm" "${work}/route_mut.log" |
| 197 | mv_=$(grep -c '^ALLEN_Q .* authority=VIOLATED ' "${work}/route_mut.log" || true) |
| 198 | [[ "${mv_}" -ge 1 ]] && grep -qx "AIENOS_CK_ALLEN: FAIL" "${work}/route_mut.log" && |
| 199 | pass "route red: grant mutant caught by the kernel check (${mv_} VIOLATED, FAIL)" || fail "route red: grant mutant not caught" |
| 200 | |
| 201 | if [[ -n "${AIENOS_ALLEN_KEEP_LOGS:-}" ]]; then |
| 202 | mkdir -p "${AIENOS_ALLEN_KEEP_LOGS}" && cp "${work}"/*.log "${work}"/*.exm "${AIENOS_ALLEN_KEEP_LOGS}/" |
| 203 | fi |
| 204 | echo "note: QEMU qualifies nothing physical" |
| 205 | if [[ "${failed}" == 0 ]]; then echo "${verdict}: PASS"; else echo "${verdict}: FAIL"; exit 1; fi |