interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / qemu_ck_osc_launch_test.sh

220 lines · 17267 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# AIENOS_CK_OSC_LAUNCH gate: task launch of an admitted OSC unit (C3-3a) in the C kernel, booted in
18# QEMU AArch64 + UEFI (AAVMF). Contract: OSC_UNIT_ARTIFACT.md section 9 (aien-protocols PR #17).
19#
20# Label: QEMU (aarch64 virt), TEST signer, not physical. Nothing here ran on a Spark or any real machine.
21# The only key is the spec's THROWAWAY TEST key. Every launch line the kernel prints carries the label.
22#
23# Two TEST-signed units are written into the sealed C Store of a GPT boot disk image and read back by the
24# kernel's store stage (the path of qemu_ck_osc_unit_test.sh):
25# a01_valid_min.unit the spec's vector unit (compiled by oscc): add, first_byte
26# l01_launch_fns.unit hand-assembled test functions (tests/fixtures/osc_unit/launch/l01.S, built by
27# make-l01.sh with the spec's own generator helpers): trap, spin, peek0, poke_rt,
28# bare_brk, exec_stack, overflow, counter
29# One boot of the qualification image built with CK_SEED0B_TEST_ANCHOR=1 CK_OSC_LAUNCH_TEST=1. After
30# admission the kernel launches a fixed script of calls as EL0 tasks and prints one "osc_launch:" line each.
31# This gate checks:
32# RETURNED with the exact value; refused bad-argument, unknown-name and out-of-window launches (nothing
33# ran); TRAPPED through the runtime trap service; a trap code outside 1..14 and a brk reached directly
34# are OUTCOME_UNKNOWN, never TRAPPED; budget exhaustion is OUTCOME_UNKNOWN (TICK_OVERRUN); a null read,
35# a write to OscRt, a jump into the stack (W^X) and a stack overflow are contained faults and a launch
36# right after each succeeds in the same boot; the task is torn down every time (no page left mapped,
37# slot reusable); a caller-owned workspace keeps its state across calls.
38# Last line: AIENOS_CK_OSC_LAUNCH: PASS|FAIL|NOT_RUN.
39# Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64).
40set -euo pipefail
41
42repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
43cd "${repo_root}"
44verdict=AIENOS_CK_OSC_LAUNCH
45
46code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
47vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
48command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict}: NOT_RUN"; exit 2; }
49[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; }
50
51# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time):
52# scripts/lib_gate_hold.sh (aienos#278).
53. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh"
54if ! gh_quiet_check || ! gh_lock_take "qemu_ck_osc_launch_test" "${AIENOS_GATE_MINUTES:-60}"; then
55 echo "NOT_RUN ${gh_why}"
56 echo "${verdict}: NOT_RUN"
57 exit 3
58fi
59# Release only this run's own gate lock record, at most once.
60release_flag() { gh_lock_release; }
61work="$(mktemp -d)"
62cleanup() { rm -rf "${work}"; release_flag; }
63trap cleanup EXIT
64failed=0
65pass() { echo "PASS $1"; }
66fail() { echo "FAIL $1"; failed=1; }
67
68cross=""
69if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
70commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
71out_prod="${AIENOS_CK_OUT_PROD:-${repo_root}/target/native-kernel}"
72out_launch="${AIENOS_CK_OUT_LAUNCH:-${repo_root}/target/native-kernel-osc-launch-test}"
73mk() { make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; }
74mk OUT="${out_launch}" CK_SEED0B_TEST_ANCHOR=1 CK_OSC_LAUNCH_TEST=1 full
75make -s -C native/kernel OUT="${out_prod}" store-image gpt-image >/dev/null
76simg="${out_prod}/host/ck_store_image"
77
78fix="native/kernel/tests/fixtures/osc_unit"
79rfix="native/kernel/tests/fixtures/osc_receipt"
80# aienos#383: a launch needs a valid realization receipt; a06 shares a01's, l02 shares l01's (same IR and code)
81files=("${fix}/vectors/a01_valid_min.unit" "${fix}/launch/l01_launch_fns.unit" "${fix}/vectors/a06_valid_limits_at_max.unit" "${fix}/launch/l02_stack_over.unit" "${rfix}/a01.rcpt" "${rfix}/l01.rcpt")
82a01_line=$(grep -E '^a01_valid_min\.unit ' "${fix}/vectors/expected.txt")
83a01_digest=$(sed -E 's/.* unit_digest=([0-9a-f]{64}) .*/\1/' <<<"${a01_line}")
84[[ ${#a01_digest} == 64 ]] || fail "expected UnitDigest read from the frozen expected.txt"
85
86disk="${work}/nvme.img"
87"${out_prod}/host/ck_gpt_image" create "${disk}" 512 64 aienos-middle >/dev/null || fail "GPT boot disk image created"
88if "${simg}" build "${disk}" 512 "${files[@]}" >"${work}/build.txt" 2>&1; then pass "boot disk built: $(head -1 "${work}/build.txt")"
89else fail "boot disk build: $(head -3 "${work}/build.txt" | tr '\n' ' ')"; fi
90"${simg}" list "${disk}" 512 >"${work}/list.txt" 2>&1 || fail "host read-back of the boot disk Store"
91for f in "${files[@]}"; do
92 grep -qxF "$(basename "${f}") bytes=$(stat -c %s "${f}") sha256=$(sha256sum "${f}" | cut -d' ' -f1)" "${work}/list.txt" \
93 || fail "host read-back of $(basename "${f}")"
94done
95
96boot() { # image, serial text output, disk image
97 local esp="${work}/esp" log="${work}/serial.log" started status
98 rm -rf "${esp}"
99 mkdir -p "${esp}/EFI/BOOT" "${esp}/EFI/AIENOS"
100 touch "${esp}/EFI/AIENOS/BOOTREPORT.TXT"
101 cp "$1" "${esp}/EFI/BOOT/BOOTAA64.EFI"
102 cp "${vars_fd}" "${work}/vars.fd"
103 started=$(date +%s)
104 set +e
105 nice -n 10 timeout "${AIENOS_QEMU_TIMEOUT:-300}" qemu-system-aarch64 \
106 -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
107 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
108 -drive if=pflash,format=raw,file="${work}/vars.fd" \
109 -drive if=none,id=esp,format=raw,file=fat:rw:"${esp}" \
110 -device virtio-blk-pci,drive=esp \
111 -drive if=none,id=nvme0,format=raw,file="$3" \
112 -device nvme,drive=nvme0,serial=aienos-nvme-test \
113 -device ramfb -display none -nic none \
114 -serial file:"${log}" -no-reboot
115 status=$?
116 set -e
117 tr -d '\r' <"${log}" >"$2"
118 echo "qemu exit ${status} after $(( $(date +%s) - started )) s (commit ${commit:0:12})"
119 [[ "${status}" == 0 ]] || fail "QEMU exit status ${status} (124 = timeout)"
120}
121has() { grep -qE -- "$2" "$1"; }
122check() { if has "$1" "$3"; then pass "$2"; else fail "$2"; fi; }
123
124serial="${work}/serial.txt"
125cp "${disk}" "${work}/disk-run.img"
126boot "${out_launch}/full/BOOTAA64.EFI" "${serial}" "${work}/disk-run.img"
127release_flag
128
129check "${serial}" "left firmware and entered the kernel" "kernel: alive"
130check "${serial}" "kernel read all six candidates (four units, two receipts) from the boot disk Store" "^artifact_candidates: 6$"
131if has "${serial}" "^report-truncated:"; then fail "kernel report lines were truncated"; else pass "no kernel report line was truncated"; fi
132check "${serial}" "final report reached the console" "report_kind: final"
133if has "${serial}" "report_kind: (panic|fault)"; then fail "panic or fault reported (a unit fault must stay contained)"; else pass "no kernel panic or fault report: unit faults stayed contained"; fi
134check "${serial}" "a01 admitted with the frozen UnitDigest" "^osc_unit: a01_valid_min\\.unit ACCEPT unit_digest=${a01_digest} "
135check "${serial}" "l01 admitted (every hand-assembled word is in the OSC subset)" "^osc_unit: l01_launch_fns\\.unit ACCEPT .* funcs=14 caps=0 signer=TEST$"
136check "${serial}" "totals: 4 seen, 4 accepted" "^osc_units: seen=4 accepted=4 refused=0 oversize=0 "
137check "${serial}" "receipt policy line (aienos#383): launch requires a valid receipt" \
138 "^osc_receipt_policy: mode=qualification receipt_test_anchors=1 .* launch requires a valid receipt; QEMU, not physical$"
139check "${serial}" "a01.rcpt stored from the Store" "^osc_receipt: a01\\.rcpt STORED bytes=416 id=$(sha256sum native/kernel/tests/fixtures/osc_receipt/a01.rcpt | cut -c1-64) "
140check "${serial}" "l01.rcpt stored from the Store" "^osc_receipt: l01\\.rcpt STORED bytes=416 id=$(sha256sum native/kernel/tests/fixtures/osc_receipt/l01.rcpt | cut -c1-64) "
141check "${serial}" "receipt totals: 2 seen, 2 stored" "^osc_receipts: seen=2 stored=2 dropped=0 "
142check "${serial}" "launch policy line, labelled" "^osc_launch_policy: units=4 code_max=65536 stack_max=65536 in_max=4096 ws_max=32 pages; 1 tick = 10 ms; QEMU \\(aarch64 virt\\), TEST signer, not physical$"
143
144label='QEMU \(aarch64 virt\), TEST signer, not physical'
145tail_run="ticks=[0-9]+ budget=[0-9]+ pages_mapped=[1-9][0-9]* page_tables_zeroed=1 slot_free=1; ${label}\$"
146tail_ref="ticks=0 budget=0 pages_mapped=0 page_tables_zeroed=1 slot_free=1; ${label}\$"
147# lline NAME UNIT CALL RESULT-REGEX [ref]: one launch line
148lline() {
149 local t="${tail_run}"
150 [[ "${5:-}" == ref ]] && t="${tail_ref}"
151 check "${serial}" "$1" "^osc_launch: $2 $3 -> $4 ${t}"
152}
153a=a01_valid_min.unit
154l=l01_launch_fns.unit
155lline "RETURNED with the exact value: add(1000000007,998244353) = 1998244360" $a 'add\(1000000007,998244353\)' 'RETURNED value=1998244360'
156lline "refused bad argument count: add(5) is LAUNCH_ARG_SHAPE (41), nothing ran" $a 'add\(5\)' 'REFUSED_AT_ADMISSION code=41 name=LAUNCH_ARG_SHAPE' ref
157lline "refused unknown name: nosuch is LAUNCH_BAD_ENTRY (40)" $a 'nosuch\(\)' 'REFUSED_AT_ADMISSION code=40 name=LAUNCH_BAD_ENTRY' ref
158lline "refused wrong case: ADD is LAUNCH_BAD_ENTRY (40)" $a 'ADD\(1,2\)' 'REFUSED_AT_ADMISSION code=40 name=LAUNCH_BAD_ENTRY' ref
159lline "RETURNED: first_byte over the read-only input window = 90 ('Z')" $a 'first_byte\(in\+0,4\)' 'RETURNED value=90'
160lline "refused slice outside the caller's input window (ownership), LAUNCH_ARG_SHAPE (41)" $a 'first_byte\(4096,16\)' 'REFUSED_AT_ADMISSION code=41 name=LAUNCH_ARG_SHAPE' ref
161lline "TRAPPED code 3 through the runtime trap service" $l 'trap\(3\)' 'TRAPPED trap_code=3'
162lline "TRAPPED code 14 (the top of the table)" $l 'trap\(14\)' 'TRAPPED trap_code=14'
163lline "trap code 15 is OUTCOME_UNKNOWN (TRAP_CODE_UNKNOWN=4), never TRAPPED" $l 'trap\(15\)' 'OUTCOME_UNKNOWN unknown_reason=4'
164lline "a brk reached directly is OUTCOME_UNKNOWN (FAULT=1), never TRAPPED" $l 'bare_brk\(\)' 'OUTCOME_UNKNOWN unknown_reason=1'
165check "${serial}" "budget exhaustion: spin() is OUTCOME_UNKNOWN (TICK_OVERRUN=2) after exactly 5 ticks of budget 5" \
166 "^osc_launch: ${l} spin\\(\\) -> OUTCOME_UNKNOWN unknown_reason=2 ticks=5 budget=5 pages_mapped=[1-9][0-9]* page_tables_zeroed=1 slot_free=1; ${label}\$"
167lline "after the budget kill, the next launch succeeds: add(40,2) = 42" $a 'add\(40,2\)' 'RETURNED value=42'
168lline "contained fault: a null read is OUTCOME_UNKNOWN (FAULT=1)" $l 'peek0\(\)' 'OUTCOME_UNKNOWN unknown_reason=1'
169lline "after the fault, the next launch succeeds in the same boot: add(20,22) = 42" $a 'add\(20,22\)' 'RETURNED value=42'
170lline "contained fault: a write to OscRt (read-only to the unit) is OUTCOME_UNKNOWN (FAULT=1)" $l 'poke_rt\(\)' 'OUTCOME_UNKNOWN unknown_reason=1'
171lline "W^X: a jump into the stack (written code) is OUTCOME_UNKNOWN (FAULT=1), not RETURNED 42" $l 'exec_stack\(\)' 'OUTCOME_UNKNOWN unknown_reason=1'
172lline "contained fault: a stack overflow is OUTCOME_UNKNOWN (FAULT=1)" $l 'overflow\(\)' 'OUTCOME_UNKNOWN unknown_reason=1'
173lline "after four faults, the next launch succeeds: add(6,36) = 42" $a 'add\(6,36\)' 'RETURNED value=42'
174# order: the successful launch comes after the fault it follows
175ln() { grep -nE -- "^osc_launch: $1 $2 " "${serial}" | head -1 | cut -d: -f1; }
176n_peek=$(ln "${l}" 'peek0\(\)'); n_add=$(ln "${a}" 'add\(20,22\)')
177if [[ -n "${n_peek}" && -n "${n_add}" && "${n_add}" -gt "${n_peek}" ]]; then pass "the launch after the contained fault was run after it (line ${n_peek} then ${n_add})"
178else fail "the launch after the contained fault was run after it"; fi
179# workspace: first workspace counts 1,2,3 across three calls, a fresh one starts at 1 again
180vals=$(grep -E -- "^osc_launch: ${l} counter\\(ws\\+0,1\\) -> RETURNED value=" "${serial}" | sed -E 's/.*value=([0-9]+) .*/\1/' | tr '\n' ' ')
181if [[ "${vals}" == "1 2 3 1 " ]]; then pass "caller-owned workspace persists across calls: values 1 2 3, a fresh workspace starts again at 1"
182else fail "workspace persistence (got: ${vals})"; fi
183lline "refused misaligned cells pointer (ws+4): LAUNCH_ARG_SHAPE (41)" $l 'counter\(ws\+4,1\)' 'REFUSED_AT_ADMISSION code=41 name=LAUNCH_ARG_SHAPE' ref
184lline "re-entry with a 23-page workspace, last cell (ws+94200) touched: counter 1" $l 'counter\(ws\+94200,1\)' 'RETURNED value=1'
185lline "re-entry, same 23-page workspace: counter state preserved, 2" $l 'counter\(ws\+94200,1\)' 'RETURNED value=2'
186lline "refused 33-page workspace (over the 32-page cap): RESOURCE_UNAVAILABLE (30), nothing ran" $l 'counter\(ws\+0,1\)' 'REFUSED_AT_ADMISSION code=30 name=RESOURCE_UNAVAILABLE' ref
187# review additions: limits, memory protection, register clearing, workspace edges
188a6=a06_valid_limits_at_max.unit; l2=l02_stack_over.unit
189lline "a06 (declared stack 1 MiB, over the 64 KiB launcher maximum): LIMIT_EXCEEDED (14), before any reservation" $a6 'add\(1,2\)' 'REFUSED_AT_ADMISSION code=14 name=LIMIT_EXCEEDED' ref
190lline "stack just over the maximum (65552 = 65536 + 16, the smallest legal value above it): LIMIT_EXCEEDED (14)" $l2 'peek0\(\)' 'REFUSED_AT_ADMISSION code=14 name=LIMIT_EXCEEDED' ref
191lline "after the limit refusals, a launch succeeds: add(3,4) = 7" $a 'add\(3,4\)' 'RETURNED value=7'
192lline "contained fault: a read of a kernel virtual address (not address 0) is OUTCOME_UNKNOWN (FAULT=1)" $l 'peek_kernel\(kernel\)' 'OUTCOME_UNKNOWN unknown_reason=1'
193lline "after the kernel-address read, a launch succeeds: add(5,6) = 11" $a 'add\(5,6\)' 'RETURNED value=11'
194lline "UXN: calling a valid ret pre-loaded in the caller workspace (ws+512) FAULTs (OUTCOME_UNKNOWN FAULT=1) instead of returning 42" $l 'jump_ws\(ws\+512,1\)' 'OUTCOME_UNKNOWN unknown_reason=1'
195lline "after the workspace branch, a launch succeeds: add(7,8) = 15" $a 'add\(7,8\)' 'RETURNED value=15'
196lline "UXN: calling valid ret bytes (c0 03 5f d6) placed in the input window FAULTs (OUTCOME_UNKNOWN FAULT=1) instead of returning 42" $l 'jump_in\(in\+0,4\)' 'OUTCOME_UNKNOWN unknown_reason=1'
197lline "after the input-window branch, a launch succeeds: add(9,10) = 19" $a 'add\(9,10\)' 'RETURNED value=19'
198lline "a task dirties x1..x29 and returns 1" $l 'dirty\(\)' 'RETURNED value=1'
199lline "the next task sees x0..x29 (except x7, the OscRt pointer) all zero: OR of them = 0" $l 'regs_or\(\)' 'RETURNED value=0'
200lline "the first byte past the workspace (ws + pages*4096) is OUTCOME_UNKNOWN (FAULT=1), contained" $l 'touch_past\(ws\+16376,1\)' 'OUTCOME_UNKNOWN unknown_reason=1'
201lline "after the past-the-end read, a launch succeeds: add(11,12) = 23" $a 'add\(11,12\)' 'RETURNED value=23'
202lline "workspace with null memory and pages>0: RESOURCE_UNAVAILABLE (30), nothing mapped" $l 'counter\(ws\+8,1\)' 'REFUSED_AT_ADMISSION code=30 name=RESOURCE_UNAVAILABLE' ref
203lline "workspace with unaligned memory: RESOURCE_UNAVAILABLE (30), nothing mapped" $l 'counter\(ws\+16,1\)' 'REFUSED_AT_ADMISSION code=30 name=RESOURCE_UNAVAILABLE' ref
204lline "workspace overlapping the launcher's own state: RESOURCE_UNAVAILABLE (30), nothing mapped" $l 'counter\(ws\+24,1\)' 'REFUSED_AT_ADMISSION code=30 name=RESOURCE_UNAVAILABLE' ref
205lline "workspace overlapping the input buffer: RESOURCE_UNAVAILABLE (30), nothing mapped" $l 'counter\(ws\+32,1\)' 'REFUSED_AT_ADMISSION code=30 name=RESOURCE_UNAVAILABLE' ref
206lline "after the workspace refusals, a launch succeeds: add(13,14) = 27" $a 'add\(13,14\)' 'RETURNED value=27'
207check "${serial}" "summary: 19 returned, 2 trapped, 12 refused, 11 unknown" \
208 "^osc_launches: returned=19 trapped=2 refused=12 unknown=11; ${label}\$"
209# every launch line carries the label; the teardown columns never show a page left behind
210nl=$(grep -c '^osc_launch: ' "${serial}" || true)
211nlab=$(grep -cE "^osc_launch: .*; ${label}\$" "${serial}" || true)
212[[ "${nl}" == 44 && "${nlab}" == 44 ]] && pass "all 44 launch lines carry the label" || fail "launch lines labelled (${nlab} of ${nl})"
213if grep -E '^osc_launch: ' "${serial}" | grep -vqE 'page_tables_zeroed=1 slot_free=1;'; then fail "a task left pages mapped or its slot busy"; else pass "every task was torn down: page_tables_zeroed=1, slot_free=1"; fi
214
215if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then cp "${serial}" "${AIENOS_LOG_DIR}/qemu_ck_osc_launch_serial.log"; fi
216if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
217 echo "---- serial (osc lines) ----"; grep -E "^(osc_|artifact|kernel|report_kind|panic|fault)" "${serial}" | head -80 || true
218fi
219echo "label: QEMU (aarch64 virt), TEST signer, not physical"
220if [[ "${failed}" == 0 ]]; then echo "${verdict}: PASS"; else echo "${verdict}: FAIL"; exit 1; fi