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_osh_core_test.sh

212 lines · 15255 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_OSH_CORE gate (OSH-AIENOS-0): the Omega-native shell core (three compiled OSC units lex_run, parse_run,
18# expand_run, packed by native/kernel/tests/fixtures/osh/make-osh-units.sh from omega's oscc output) runs as admitted EL0
19# tasks under the C kernel and produces exactly the same step trace as on Linux. Differential: the same compiled bytes,
20# two runners (omega: tests/osh/osh_trace.c; here: native/kernel/core/osh_test.c).
21#
22# Label: QEMU (aarch64 virt), TEST signer, not physical. Nothing here ran on a Spark or any real machine.
23# The only key is the spec's THROWAWAY TEST key.
24#
25# Six containers are written into the sealed C Store of a GPT boot disk image: the three units, a copy of
26# osh_lex.unit with one byte of its CODE flipped, a copy with one byte of its Ed25519 signature flipped, and the launch
27# gate's l01_launch_fns.unit (only its spin() is used,
28# for control 4b). One boot of the qualification image built with
29# CK_SEED0B_TEST_ANCHOR=1 CK_OSH_TEST=1. The kernel admits them, runs every embedded fixture script and prints the trace
30# between osh_script_begin / osh_script_end lines. This gate cuts each trace out of the serial log and diffs it, byte for
31# byte, against the committed omega trace (native/kernel/tests/fixtures/osh/trace/*.trace). Negative controls:
32# 1 a 22-page workspace with the pointer range claiming 11456 cells -> REFUSED 41, before the first instruction
33# 2a a workspace passed with len 512 cells (emit_tok, w[17]=127) -> TRAPPED trap_code 3 (BOUNDS), no stray write
34# 2b the entry point with len 512 cells -> RETURNED 213 (the unit's own size check)
35# 3 one flipped byte in a unit's CODE -> admission refuses, nothing of it ran
36# 3b one flipped byte in a unit's signature (last 64 bytes) -> admission refuses BAD_SIGNATURE (26)
37# 4a caller max_ticks 1 on the long shell fixture -> RETURNED inside the tick (ticks=0): LIMIT, see below
38# 4b the same cap through the same launch path on l01 spin() -> OUTCOME_UNKNOWN TICK_OVERRUN
39# LIMIT: every shell loop is bounded and every entry call has a step budget, so no single shell call lasts a whole
40# 10 ms tick under QEMU TCG; the cap is therefore shown to bite on spin(), not on a shell unit.
41# 5 a deliberately wrong expected trace line -> the diff of this script goes red
42# Last line: AIENOS_CK_OSH_CORE: PASS|FAIL|NOT_RUN.
43set -euo pipefail
44
45repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
46cd "${repo_root}"
47verdict=AIENOS_CK_OSH_CORE
48
49code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
50vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
51command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict}: NOT_RUN"; exit 2; }
52[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; }
53
54# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time):
55# scripts/lib_gate_hold.sh (aienos#278).
56. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh"
57if ! gh_quiet_check || ! gh_lock_take "qemu_ck_osh_core_test" "${AIENOS_GATE_MINUTES:-60}"; then
58 echo "NOT_RUN ${gh_why}"
59 echo "${verdict}: NOT_RUN"
60 exit 3
61fi
62# Release only this run's own gate lock record, at most once.
63release_flag() { gh_lock_release; }
64work="$(mktemp -d)"
65cleanup() { rm -rf "${work}"; release_flag; }
66trap cleanup EXIT
67failed=0
68pass() { echo "PASS $1"; }
69fail() { echo "FAIL $1"; failed=1; }
70
71cross=""
72if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
73commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
74out_prod="${AIENOS_CK_OUT_PROD:-${repo_root}/target/native-kernel}"
75out_osh="${AIENOS_CK_OUT_OSH:-${repo_root}/target/native-kernel-osh-core-test}"
76mk() { make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; }
77
78fix="native/kernel/tests/fixtures/osh"
79# the generated header the kernel embeds must be what the committed fixture scripts give
80"${fix}/gen-fixtures-h.sh" "${fix}/trace" "${work}/osh_fixtures.h"
81if cmp -s "${work}/osh_fixtures.h" native/kernel/core/osh_fixtures.h; then pass "core/osh_fixtures.h is exactly what the fixture scripts generate"
82else fail "core/osh_fixtures.h differs from the fixture scripts (run gen-fixtures-h.sh)"; fi
83nsh=$(ls "${fix}"/trace/*.sh | wc -l); ntr=$(ls "${fix}"/trace/*.trace | wc -l)
84[[ "${nsh}" == "${ntr}" && "${nsh}" == 20 ]] && pass "${nsh} fixture scripts, ${ntr} expected traces (omega#343 has 20)" || fail "fixture scripts (${nsh}) and traces (${ntr}): expected 20 each"
85
86mk OUT="${out_osh}" CK_SEED0B_TEST_ANCHOR=1 CK_OSH_TEST=1 full
87make -s -C native/kernel OUT="${out_prod}" store-image gpt-image >/dev/null
88simg="${out_prod}/host/ck_store_image"
89
90# negative control 3 input: osh_lex.unit with one byte of the CODE section flipped. The code offset is read from the
91# container's section table (section 2 entry at 128 + 48, offset field at +4).
92code_off=$(od -An -tu4 -j180 -N4 "${fix}/osh_lex.unit" | tr -d ' ')
93cp "${fix}/osh_lex.unit" "${work}/osh_lex_flipped.unit"
94orig=$(od -An -tx1 -j$((code_off + 100)) -N1 "${fix}/osh_lex.unit" | tr -d ' ')
95flip=$(printf '%02x' $(( 0x${orig} ^ 0x01 )))
96printf "\\x${flip}" | dd of="${work}/osh_lex_flipped.unit" bs=1 seek=$((code_off + 100)) conv=notrunc status=none
97[[ "$(od -An -tx1 -j$((code_off + 100)) -N1 "${work}/osh_lex_flipped.unit" | tr -d ' ')" == "${flip}" ]] && pass "flipped one code byte at offset $((code_off + 100)) (${orig} -> ${flip})" || fail "code byte flip"
98
99# negative control 3b input: osh_lex.unit with one byte of the signature (R half, the last 64 bytes) flipped
100sz=$(stat -c %s "${fix}/osh_lex.unit"); sig_at=$((sz - 64 + 5))
101cp "${fix}/osh_lex.unit" "${work}/osh_lex_badsig.unit"
102orig=$(od -An -tx1 -j${sig_at} -N1 "${fix}/osh_lex.unit" | tr -d ' ')
103flip=$(printf '%02x' $(( 0x${orig} ^ 0x01 )))
104printf "\\x${flip}" | dd of="${work}/osh_lex_badsig.unit" bs=1 seek=${sig_at} conv=notrunc status=none
105[[ "$(od -An -tx1 -j${sig_at} -N1 "${work}/osh_lex_badsig.unit" | tr -d ' ')" == "${flip}" ]] && pass "flipped one signature byte at offset ${sig_at} (${orig} -> ${flip})" || fail "signature byte flip"
106
107files=("${fix}/osh_lex.unit" "${fix}/osh_parse.unit" "${fix}/osh_expand.unit" "${work}/osh_lex_flipped.unit" "${work}/osh_lex_badsig.unit" "${fix}/../osc_unit/launch/l01_launch_fns.unit"
108 "${fix}/../osc_receipt/osh_lex.rcpt" "${fix}/../osc_receipt/osh_parse.rcpt" "${fix}/../osc_receipt/osh_expand.rcpt" "${fix}/../osc_receipt/l01.rcpt") # aienos#383: receipts
109disk="${work}/nvme.img"
110"${out_prod}/host/ck_gpt_image" create "${disk}" 512 64 aienos-middle >/dev/null || fail "GPT boot disk image created"
111if "${simg}" build "${disk}" 512 "${files[@]}" >"${work}/build.txt" 2>&1; then pass "boot disk built: $(head -1 "${work}/build.txt")"
112else fail "boot disk build: $(head -3 "${work}/build.txt" | tr '\n' ' ')"; fi
113"${simg}" list "${disk}" 512 >"${work}/list.txt" 2>&1 || fail "host read-back of the boot disk Store"
114for f in "${files[@]}"; do
115 grep -qxF "$(basename "${f}") bytes=$(stat -c %s "${f}") sha256=$(sha256sum "${f}" | cut -d' ' -f1)" "${work}/list.txt" \
116 || fail "host read-back of $(basename "${f}")"
117done
118
119boot() { # image, serial text output, disk image
120 local esp="${work}/esp" log="${work}/serial.log" started status
121 rm -rf "${esp}"
122 mkdir -p "${esp}/EFI/BOOT" "${esp}/EFI/AIENOS"
123 touch "${esp}/EFI/AIENOS/BOOTREPORT.TXT"
124 cp "$1" "${esp}/EFI/BOOT/BOOTAA64.EFI"
125 cp "${vars_fd}" "${work}/vars.fd"
126 started=$(date +%s)
127 set +e
128 nice -n 10 timeout "${AIENOS_QEMU_TIMEOUT:-900}" qemu-system-aarch64 \
129 -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
130 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
131 -drive if=pflash,format=raw,file="${work}/vars.fd" \
132 -drive if=none,id=esp,format=raw,file=fat:rw:"${esp}" \
133 -device virtio-blk-pci,drive=esp \
134 -drive if=none,id=nvme0,format=raw,file="$3" \
135 -device nvme,drive=nvme0,serial=aienos-nvme-test \
136 -device ramfb -display none -nic none \
137 -serial file:"${log}" -no-reboot
138 status=$?
139 set -e
140 tr -d '\r' <"${log}" >"$2"
141 echo "qemu exit ${status} after $(( $(date +%s) - started )) s (commit ${commit:0:12})"
142 [[ "${status}" == 0 ]] || fail "QEMU exit status ${status} (124 = timeout)"
143}
144has() { grep -qE -- "$2" "$1"; }
145check() { if has "$1" "$3"; then pass "$2"; else fail "$2"; fi; }
146
147serial="${work}/serial.txt"
148cp "${disk}" "${work}/disk-run.img"
149boot "${out_osh}/full/BOOTAA64.EFI" "${serial}" "${work}/disk-run.img"
150release_flag
151
152label='QEMU \(aarch64 virt\), TEST signer, not physical'
153check "${serial}" "left firmware and entered the kernel" "kernel: alive"
154check "${serial}" "kernel read all ten candidates (six units, four receipts) from the boot disk Store" "^artifact_candidates: 10$"
155if has "${serial}" "^report-truncated:"; then fail "kernel report lines were truncated"; else pass "no kernel report line was truncated"; fi
156check "${serial}" "final report reached the console" "report_kind: final"
157if 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"; fi
158for u in osh_lex osh_parse osh_expand; do
159 check "${serial}" "${u} admitted (real oscc output, TEST signer)" "^osc_unit: ${u}\\.unit ACCEPT unit_digest=[0-9a-f]{64} program_id=[0-9a-f]{64} funcs=[0-9]+ caps=0 signer=TEST$"
160done
161check "${serial}" "totals: 6 seen, 4 accepted, 2 refused" "^osc_units: seen=6 accepted=4 refused=2 oversize=0 "
162check "${serial}" "core line: 3 units, 11456 cells, 23 pages, input window 4096" "^osh_core: units=3 ws_cells=11456 ws_pages=23 input_max=4096; ${label}\$"
163check "${serial}" "driver finished every script" "^osh_core_done: scripts=${nsh}; ${label}\$"
164if has "${serial}" "^osh_fault:"; then fail "a launch did not return (osh_fault line)"; else pass "no launch failed during the traces"; fi
165
166# ---- the differential: kernel trace vs committed omega trace, byte for byte ----
167mkdir -p "${work}/got"
168total_lines=0; matched=0
169for t in "${fix}"/trace/*.trace; do
170 n=$(basename "${t}" .trace)
171 awk -v n="${n}" '$1=="osh_script_begin" && $2==n {on=1; next} $1=="osh_script_end" && $2==n {on=0} on' "${serial}" >"${work}/got/${n}.trace"
172 l=$(wc -l <"${t}")
173 if cmp -s "${work}/got/${n}.trace" "${t}"; then pass "trace ${n}: ${l} lines identical to the Linux trace"; matched=$((matched + 1)); total_lines=$((total_lines + l))
174 else
175 fail "trace ${n} differs from the Linux trace; first difference:"
176 diff "${work}/got/${n}.trace" "${t}" | head -4 | cut -c1-200 || true
177 fi
178done
179echo "traces matched: ${matched} of ${nsh} scripts, ${total_lines} trace lines byte-identical"
180[[ "${matched}" == "${nsh}" ]] && pass "every fixture trace is byte-identical (interpreter and native on Linux == AIENOS EL0)" || fail "not every trace matched"
181
182# ---- negative controls ----
183tail_ref="ticks=0 budget=0 pages_mapped=0 page_tables_zeroed=1 slot_free=1"
184check "${serial}" "control 1: 22-page workspace claiming 11456 cells -> REFUSED 41 before the first instruction (ticks=0, nothing mapped)" \
185 "^osh_neg1: .* -> REFUSED_AT_ADMISSION code=41 name=LAUNCH_ARG_SHAPE ${tail_ref}; ${label}\$"
186check "${serial}" "control 2a: workspace len 512 cells -> TRAPPED trap_code 3 (BOUNDS), cell 17 and every cell from 512 up unchanged" \
187 "^osh_neg2a: .* -> TRAPPED trap_code=3 ticks=[0-9]+ budget=[0-9]+ pages_mapped=[1-9][0-9]* page_tables_zeroed=1 slot_free=1 cell17=127 cells_512_up_unchanged=1; ${label}\$"
188check "${serial}" "control 2b: entry point with len 512 cells -> RETURNED 213 (WORKSPACE_SIZE, the unit's own check), nothing written from 512 up" \
189 "^osh_neg2b: .* -> RETURNED value=213 ticks=[0-9]+ budget=[0-9]+ pages_mapped=[1-9][0-9]* page_tables_zeroed=1 slot_free=1 cells_512_up_unchanged=1; ${label}\$"
190check "${serial}" "control 3: one flipped CODE byte -> admission REFUSED code=16 CODE_HASH_MISMATCH" "^osc_unit: osh_lex_flipped\\.unit REFUSED code=16 name=CODE_HASH_MISMATCH$"
191check "${serial}" "control 3: nothing of the flipped unit ran" "^osh_neg3: osh_lex_flipped\\.unit -> NOT_RUN \\(unit was not admitted\\); ${label}\$"
192check "${serial}" "control 3b: one flipped signature byte -> admission REFUSED code=26 BAD_SIGNATURE" "^osc_unit: osh_lex_badsig\\.unit REFUSED code=26 name=BAD_SIGNATURE$"
193check "${serial}" "control 4a (limit): max_ticks 1 on the long shell fixture: finishes inside the first tick (ticks=0) or overruns (1); the units are loop-bounded" \
194 "^osh_neg4a: .* -> (RETURNED value=[0-9]+ ticks=0|OUTCOME_UNKNOWN unknown_reason=2 ticks=1) budget=1 pages_mapped=[1-9][0-9]* page_tables_zeroed=1 slot_free=1 fixture=[a-z0-9_]+ in_len=[0-9]+ max_ticks=1; ${label}\$"
195check "${serial}" "control 4b: the same max_ticks 1 cap on spin() -> OUTCOME_UNKNOWN TICK_OVERRUN (2) after exactly 1 tick" \
196 "^osh_neg4b: .* -> OUTCOME_UNKNOWN unknown_reason=2 ticks=1 budget=1 pages_mapped=[1-9][0-9]* page_tables_zeroed=1 slot_free=1; ${label}\$"
197check "${serial}" "control 4: the next shell launch on the same workspace returns" "^osh_neg4n: .* -> RETURNED value=[0-9]+ "
198# control 5: a deliberately wrong expected line must turn the diff red, then the real one is restored
199first="${fix}/trace/t01_words.trace"
200cp "${first}" "${work}/wrong.trace"
201sed -i '1s/ret=[0-9]*/ret=777/' "${work}/wrong.trace"
202if cmp -s "${work}/got/t01_words.trace" "${work}/wrong.trace"; then fail "control 5: the diff stayed green on a wrong expected line (gate cannot go red)"
203else pass "control 5: a deliberately wrong expected line (ret=777) turns the diff red"; fi
204if cmp -s "${work}/got/t01_words.trace" "${first}"; then pass "control 5: with the real expected line restored the diff is green again"
205else fail "control 5: restored comparison not green"; fi
206
207if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then cp "${serial}" "${AIENOS_LOG_DIR}/qemu_ck_osh_core_serial.log"; fi
208if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
209 echo "---- serial (osc/osh lines, no trace) ----"; grep -E "^(osc_|osh_neg|osh_core|osh_fault|artifact|kernel|report_kind|panic|fault)" "${serial}" | head -80 || true
210fi
211echo "label: QEMU (aarch64 virt), TEST signer, not physical"
212if [[ "${failed}" == 0 ]]; then echo "${verdict}: PASS"; else echo "${verdict}: FAIL"; exit 1; fi