Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/scripts/qemu_ck_osc_launch_test.sh # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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). 40 set -euo pipefail 41 42 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 43 cd "${repo_root}" 44 verdict=AIENOS_CK_OSC_LAUNCH 45 46 code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" 47 vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" 48 command -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" 54 if ! 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 58 fi 59 # Release only this run's own gate lock record, at most once. 60 release_flag() { gh_lock_release; } 61 work="$(mktemp -d)" 62 cleanup() { rm -rf "${work}"; release_flag; } 63 trap cleanup EXIT 64 failed=0 65 pass() { echo "PASS $1"; } 66 fail() { echo "FAIL $1"; failed=1; } 67 68 cross="" 69 if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi 70 commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" 71 out_prod="${AIENOS_CK_OUT_PROD:-${repo_root}/target/native-kernel}" 72 out_launch="${AIENOS_CK_OUT_LAUNCH:-${repo_root}/target/native-kernel-osc-launch-test}" 73 mk() { make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; } 74 mk OUT="${out_launch}" CK_SEED0B_TEST_ANCHOR=1 CK_OSC_LAUNCH_TEST=1 full 75 make -s -C native/kernel OUT="${out_prod}" store-image gpt-image >/dev/null 76 simg="${out_prod}/host/ck_store_image" 77 78 fix="native/kernel/tests/fixtures/osc_unit" 79 rfix="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) 81 files=("${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") 82 a01_line=$(grep -E '^a01_valid_min\.unit ' "${fix}/vectors/expected.txt") 83 a01_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 86 disk="${work}/nvme.img" 87 "${out_prod}/host/ck_gpt_image" create "${disk}" 512 64 aienos-middle >/dev/null || fail "GPT boot disk image created" 88 if "${simg}" build "${disk}" 512 "${files[@]}" >"${work}/build.txt" 2>&1; then pass "boot disk built: $(head -1 "${work}/build.txt")" 89 else 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" 91 for 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}")" 94 done 95 96 boot() { # 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 } 121 has() { grep -qE -- "$2" "$1"; } 122 check() { if has "$1" "$3"; then pass "$2"; else fail "$2"; fi; } 123 124 serial="${work}/serial.txt" 125 cp "${disk}" "${work}/disk-run.img" 126 boot "${out_launch}/full/BOOTAA64.EFI" "${serial}" "${work}/disk-run.img" 127 release_flag 128 129 check "${serial}" "left firmware and entered the kernel" "kernel: alive" 130 check "${serial}" "kernel read all six candidates (four units, two receipts) from the boot disk Store" "^artifact_candidates: 6$" 131 if has "${serial}" "^report-truncated:"; then fail "kernel report lines were truncated"; else pass "no kernel report line was truncated"; fi 132 check "${serial}" "final report reached the console" "report_kind: final" 133 if 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 134 check "${serial}" "a01 admitted with the frozen UnitDigest" "^osc_unit: a01_valid_min\\.unit ACCEPT unit_digest=${a01_digest} " 135 check "${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$" 136 check "${serial}" "totals: 4 seen, 4 accepted" "^osc_units: seen=4 accepted=4 refused=0 oversize=0 " 137 check "${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$" 139 check "${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) " 140 check "${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) " 141 check "${serial}" "receipt totals: 2 seen, 2 stored" "^osc_receipts: seen=2 stored=2 dropped=0 " 142 check "${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 144 label='QEMU \(aarch64 virt\), TEST signer, not physical' 145 tail_run="ticks=[0-9]+ budget=[0-9]+ pages_mapped=[1-9][0-9]* page_tables_zeroed=1 slot_free=1; ${label}\$" 146 tail_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 148 lline() { 149 local t="${tail_run}" 150 [[ "${5:-}" == ref ]] && t="${tail_ref}" 151 check "${serial}" "$1" "^osc_launch: $2 $3 -> $4 ${t}" 152 } 153 a=a01_valid_min.unit 154 l=l01_launch_fns.unit 155 lline "RETURNED with the exact value: add(`1000000007`,`998244353`) = `1998244360`" $a 'add\(`1000000007`,`998244353`\)' 'RETURNED value=`1998244360`' 156 lline "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 157 lline "refused unknown name: nosuch is LAUNCH_BAD_ENTRY (40)" $a 'nosuch\(\)' 'REFUSED_AT_ADMISSION code=40 name=LAUNCH_BAD_ENTRY' ref 158 lline "refused wrong case: ADD is LAUNCH_BAD_ENTRY (40)" $a 'ADD\(1,2\)' 'REFUSED_AT_ADMISSION code=40 name=LAUNCH_BAD_ENTRY' ref 159 lline "RETURNED: first_byte over the read-only input window = 90 ('Z')" $a 'first_byte\(in\+0,4\)' 'RETURNED value=90' 160 lline "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 161 lline "TRAPPED code 3 through the runtime trap service" $l 'trap\(3\)' 'TRAPPED trap_code=3' 162 lline "TRAPPED code 14 (the top of the table)" $l 'trap\(14\)' 'TRAPPED trap_code=14' 163 lline "trap code 15 is OUTCOME_UNKNOWN (TRAP_CODE_UNKNOWN=4), never TRAPPED" $l 'trap\(15\)' 'OUTCOME_UNKNOWN unknown_reason=4' 164 lline "a brk reached directly is OUTCOME_UNKNOWN (FAULT=1), never TRAPPED" $l 'bare_brk\(\)' 'OUTCOME_UNKNOWN unknown_reason=1' 165 check "${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}\$" 167 lline "after the budget kill, the next launch succeeds: add(40,2) = 42" $a 'add\(40,2\)' 'RETURNED value=42' 168 lline "contained fault: a null read is OUTCOME_UNKNOWN (FAULT=1)" $l 'peek0\(\)' 'OUTCOME_UNKNOWN unknown_reason=1' 169 lline "after the fault, the next launch succeeds in the same boot: add(20,22) = 42" $a 'add\(20,22\)' 'RETURNED value=42' 170 lline "contained fault: a write to OscRt (read-only to the unit) is OUTCOME_UNKNOWN (FAULT=1)" $l 'poke_rt\(\)' 'OUTCOME_UNKNOWN unknown_reason=1' 171 lline "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' 172 lline "contained fault: a stack overflow is OUTCOME_UNKNOWN (FAULT=1)" $l 'overflow\(\)' 'OUTCOME_UNKNOWN unknown_reason=1' 173 lline "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 175 ln() { grep -nE -- "^osc_launch: $1 $2 " "${serial}" | head -1 | cut -d: -f1; } 176 n_peek=$(ln "${l}" 'peek0\(\)'); n_add=$(ln "${a}" 'add\(20,22\)') 177 if [[ -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})" 178 else 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 180 vals=$(grep -E -- "^osc_launch: ${l} counter\\(ws\\+0,1\\) -> RETURNED value=" "${serial}" | sed -E 's/.*value=([0-9]+) .*/\1/' | tr '\n' ' ') 181 if [[ "${vals}" == "1 2 3 1 " ]]; then pass "caller-owned workspace persists across calls: values 1 2 3, a fresh workspace starts again at 1" 182 else fail "workspace persistence (got: ${vals})"; fi 183 lline "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 184 lline "re-entry with a 23-page workspace, last cell (ws+94200) touched: counter 1" $l 'counter\(ws\+94200,1\)' 'RETURNED value=1' 185 lline "re-entry, same 23-page workspace: counter state preserved, 2" $l 'counter\(ws\+94200,1\)' 'RETURNED value=2' 186 lline "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 188 a6=a06_valid_limits_at_max.unit; l2=l02_stack_over.unit 189 lline "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 190 lline "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 191 lline "after the limit refusals, a launch succeeds: add(3,4) = 7" $a 'add\(3,4\)' 'RETURNED value=7' 192 lline "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' 193 lline "after the kernel-address read, a launch succeeds: add(5,6) = 11" $a 'add\(5,6\)' 'RETURNED value=11' 194 lline "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' 195 lline "after the workspace branch, a launch succeeds: add(7,8) = 15" $a 'add\(7,8\)' 'RETURNED value=15' 196 lline "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' 197 lline "after the input-window branch, a launch succeeds: add(9,10) = 19" $a 'add\(9,10\)' 'RETURNED value=19' 198 lline "a task dirties x1..x29 and returns 1" $l 'dirty\(\)' 'RETURNED value=1' 199 lline "the next task sees x0..x29 (except x7, the OscRt pointer) all zero: OR of them = 0" $l 'regs_or\(\)' 'RETURNED value=0' 200 lline "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' 201 lline "after the past-the-end read, a launch succeeds: add(11,12) = 23" $a 'add\(11,12\)' 'RETURNED value=23' 202 lline "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 203 lline "workspace with unaligned memory: RESOURCE_UNAVAILABLE (30), nothing mapped" $l 'counter\(ws\+16,1\)' 'REFUSED_AT_ADMISSION code=30 name=RESOURCE_UNAVAILABLE' ref 204 lline "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 205 lline "workspace overlapping the input buffer: RESOURCE_UNAVAILABLE (30), nothing mapped" $l 'counter\(ws\+32,1\)' 'REFUSED_AT_ADMISSION code=30 name=RESOURCE_UNAVAILABLE' ref 206 lline "after the workspace refusals, a launch succeeds: add(13,14) = 27" $a 'add\(13,14\)' 'RETURNED value=27' 207 check "${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 210 nl=$(grep -c '^osc_launch: ' "${serial}" || true) 211 nlab=$(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})" 213 if 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 215 if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then cp "${serial}" "${AIENOS_LOG_DIR}/qemu_ck_osc_launch_serial.log"; fi 216 if [[ "${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 218 fi 219 echo "label: QEMU (aarch64 virt), TEST signer, not physical" 220 if [[ "${failed}" == 0 ]]; then echo "${verdict}: PASS"; else echo "${verdict}: FAIL"; exit 1; fi