omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / spark / run_spark_qemu_e2e.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # aienux/spark/run_spark_qemu_e2e.sh [EFI_OMC]: build the REAL spark_main.om with the QEMU platform profile |
| 4 | # (PLAT=qemu -> SPARK-QEMU.EFI, gate-only) and run it under QEMU virt,iommu=smmuv3 with a read-only NVMe |
| 5 | # disk (GPT + ext4 + synthetic OMFWBND1 bundle) and an edu device standing in for the GPU endpoint. |
| 6 | # Real: Stage 1, Stage 2 (MCFG/ECAM probe, BAR map, quiesce, IORT, mode-1 SMMUv3 bring-up, per-device |
| 7 | # streams, BME enable), Stage 3 disk + bundle (NVMe RO, GPT, ext4, TOC + SHA-256). Stubbed: GB20B |
| 8 | # register access, RTL8127/TCP, Stages 4..6. QEMU-only; touches no hardware. |
| 9 | set -eu |
| 10 | ROOT=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd) |
| 11 | OMC="${1:-${OMC:-$ROOT/seed/out/omc}}" |
| 12 | TMP=$(mktemp -d "${TMPDIR:-/tmp}/spark-qemu-e2e.XXXXXX") |
| 13 | trap 'rm -rf "$TMP"' EXIT |
| 14 | PLAT=qemu OMC="$OMC" sh "$ROOT/aienux/spark/build_spark_efi.sh" "$TMP/SPARK-QEMU.EFI" |
| 15 | python3 "$ROOT/aienux/spark/mk_qemu_disk.py" "$TMP/disk.img" "$TMP" |
| 16 | code=${A64EFI_CODE:-} |
| 17 | for f in /opt/homebrew/share/qemu/edk2-aarch64-code.fd \ |
| 18 | /usr/share/qemu-efi-aarch64/QEMU_EFI.fd \ |
| 19 | /usr/share/edk2/aarch64/QEMU_EFI.fd \ |
| 20 | /usr/share/AAVMF/QEMU_EFI.fd; do |
| 21 | [ -z "$code" ] && [ -f "$f" ] && code=$f |
| 22 | done |
| 23 | [ -n "$code" ] && [ -f "$code" ] || { echo "FAIL: EDK2 firmware not found" >&2; exit 1; } |
| 24 | mkdir -p "$TMP/esp/EFI/BOOT" |
| 25 | cp "$TMP/SPARK-QEMU.EFI" "$TMP/esp/EFI/BOOT/BOOTAA64.EFI" |
| 26 | run_qemu() { # $1 = disk image, $2 = serial log, $3 = ESP dir (default $TMP/esp) |
| 27 | timeout 120 qemu-system-aarch64 \ |
| 28 | -M virt,gic-version=3,its=on,virtualization=on,iommu=smmuv3 -cpu max -smp 20 -m 4096 \ |
| 29 | -display none -bios "$code" \ |
| 30 | -drive if=none,id=d0,format=raw,file=fat:rw:"${3:-$TMP/esp}" \ |
| 31 | -device virtio-blk-device,drive=d0 \ |
| 32 | -drive if=none,id=nv0,format=raw,readonly=on,file="$1" \ |
| 33 | -device nvme,serial=spk0,drive=nv0 \ |
| 34 | -device edu,dma_mask=0xffffffffff \ |
| 35 | -net none -serial file:"$2" -monitor none -no-reboot </dev/null >/dev/null 2>&1 || true |
| 36 | } |
| 37 | run_qemu "$TMP/disk.img" "$TMP/qemu.log" |
| 38 | tr -d '\r' < "$TMP/qemu.log" | grep -E '\[SPARK|SMP_ONLINE|SPARK_QEMU|PANIC' || true |
| 39 | if grep -q 'PANIC' "$TMP/qemu.log"; then echo "FAIL: PANIC" >&2; exit 1; fi |
| 40 | if grep -q '\[SPARK FAIL' "$TMP/qemu.log"; then echo "FAIL: spark_main stage failure under QEMU" >&2; exit 1; fi |
| 41 | grep -q 'SMP_ONLINE online=20 expected=20 failed=0' "$TMP/qemu.log" |
| 42 | grep -q '\[SPARK 2/6\]' "$TMP/qemu.log" |
| 43 | grep -q '\[SPARK QEMU\] stage 2 SMMUv3 streams live' "$TMP/qemu.log" |
| 44 | grep -q '\[SPARK 3/6\]' "$TMP/qemu.log" |
| 45 | grep -q '\[SPARK QEMU\] stage 3 bundle verified' "$TMP/qemu.log" |
| 46 | grep -q 'SPARK_QEMU_E2E_PASS real=stage1,stage2' "$TMP/qemu.log" |
| 47 | # Negative: flip one payload byte of bundle entry 0 -> SHA-256 mismatch must fail closed at stage 3 (304). |
| 48 | python3 - "$TMP/disk.img" "$TMP/disk-bad.img" <<'PY' |
| 49 | import sys |
| 50 | d = bytearray(open(sys.argv[1], "rb").read()) |
| 51 | i = d.find(b"OMFWBND1") |
| 52 | assert i > 0 |
| 53 | d[i + 4096 + 100] ^= 1 |
| 54 | open(sys.argv[2], "wb").write(d) |
| 55 | PY |
| 56 | run_qemu "$TMP/disk-bad.img" "$TMP/qemu-bad.log" |
| 57 | tr -d '\r' < "$TMP/qemu-bad.log" | grep -E '\[SPARK FAIL|SPARK_QEMU_E2E_PASS' || true |
| 58 | grep -q '\[SPARK FAIL stage=3 code=304\]' "$TMP/qemu-bad.log" |
| 59 | if grep -q 'SPARK_QEMU_E2E_PASS' "$TMP/qemu-bad.log"; then echo "FAIL: tampered bundle accepted" >&2; exit 1; fi |
| 60 | # Negative: a platform profile whose SMMU base disagrees with the firmware IORT must fail closed at |
| 61 | # stage 2 with SPK_E_SMMU_BASE (208) before any SMMU is programmed. |
| 62 | sed 's/ret 0x09050000/ret 0x09060000/' "$ROOT/aienux/spark/plat_qemu.om" > "$TMP/plat_qemu_badbase.om" |
| 63 | if cmp -s "$ROOT/aienux/spark/plat_qemu.om" "$TMP/plat_qemu_badbase.om"; then |
| 64 | echo "FAIL: 208 negative control could not alter the QEMU SMMU base" >&2; exit 1 |
| 65 | fi |
| 66 | PLAT=qemu PLAT_OM_OVERRIDE="$TMP/plat_qemu_badbase.om" OMC="$OMC" \ |
| 67 | sh "$ROOT/aienux/spark/build_spark_efi.sh" "$TMP/SPARK-QEMU-BADBASE.EFI" >/dev/null |
| 68 | mkdir -p "$TMP/esp-badbase/EFI/BOOT" |
| 69 | cp "$TMP/SPARK-QEMU-BADBASE.EFI" "$TMP/esp-badbase/EFI/BOOT/BOOTAA64.EFI" |
| 70 | run_qemu "$TMP/disk.img" "$TMP/qemu-badbase.log" "$TMP/esp-badbase" |
| 71 | tr -d '\r' < "$TMP/qemu-badbase.log" | grep -E '\[SPARK FAIL|SPARK_QEMU_E2E_PASS|streams live' || true |
| 72 | grep -q '\[SPARK FAIL stage=2 code=208\]' "$TMP/qemu-badbase.log" |
| 73 | if grep -qE 'SPARK_QEMU_E2E_PASS|stage 2 SMMUv3 streams live' "$TMP/qemu-badbase.log"; then |
| 74 | echo "FAIL: mismatched SMMU base accepted" >&2; exit 1 |
| 75 | fi |
| 76 | echo "PASS spark_main_qemu_e2e (real spark_main Stage 1 + Stage 2 incl. SMMUv3 mode-1 + Stage 3 NVMe/GPT/ext4/bundle; GB20B regs, net, Stages 4-6 stubbed; tampered bundle -> stage 3 code 304; IORT/platform SMMU base mismatch -> stage 2 code 208)" |