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 / m3_receipt.sh
| 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 | # m3_receipt.sh: produce one reproducible M3 evidence receipt for the commit |
| 18 | # that is checked out. |
| 19 | # |
| 20 | # It refuses to run on a dirty tree, records the commit and the toolchain, |
| 21 | # runs `AIENOS_STRICT=1 bash scripts/verify_all.sh` (so no step may be |
| 22 | # skipped) with every serial log captured, and writes |
| 23 | # evidence/m3_receipt_<YYYY-MM-DD>.md with the PASS lines grouped by proof, |
| 24 | # the sha256 and line count of every raw log, the limitations, and an operator |
| 25 | # sign-off line that stays "pending" until the operator writes "M3 accepted". |
| 26 | # |
| 27 | # Raw logs are kept outside the tree (printed at the end) and are never |
| 28 | # committed; the receipt carries only their hashes and line counts. |
| 29 | # |
| 30 | # Usage: bash scripts/m3_receipt.sh [--allow-missing] |
| 31 | # --allow-missing write the receipt and exit 0 even when some proofs have |
| 32 | # no PASS line yet (they are listed as MISSING). |
| 33 | # Environment: |
| 34 | # AIENOS_RECEIPT_LOG_DIR where raw logs go (default: a new mktemp dir). |
| 35 | # AIENOS_QEMU_TIMEOUT passed through to the QEMU scripts. |
| 36 | # |
| 37 | # Exit status: 0 all proofs present (or --allow-missing), 1 verification |
| 38 | # failed or the run changed tracked files, 2 usage or dirty tree, 3 proofs |
| 39 | # MISSING without --allow-missing. |
| 40 | set -euo pipefail |
| 41 | |
| 42 | allow_missing=0 |
| 43 | for arg in "$@"; do |
| 44 | case "${arg}" in |
| 45 | --allow-missing) allow_missing=1 ;; |
| 46 | -h|--help) sed -n '2,24p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;; |
| 47 | *) echo "unknown argument: ${arg}" >&2; exit 2 ;; |
| 48 | esac |
| 49 | done |
| 50 | |
| 51 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 52 | # Resolve a caller-supplied log dir against the caller's directory, before cd. |
| 53 | requested_log_dir="" |
| 54 | if [[ -n "${AIENOS_RECEIPT_LOG_DIR:-}" ]]; then |
| 55 | requested_log_dir="$(realpath -m -- "${AIENOS_RECEIPT_LOG_DIR}")" |
| 56 | case "${requested_log_dir}/" in |
| 57 | "$(realpath -m -- "${repo_root}")/"*) |
| 58 | echo "m3_receipt: AIENOS_RECEIPT_LOG_DIR must be outside the tree: ${requested_log_dir}" >&2 |
| 59 | exit 2 ;; |
| 60 | esac |
| 61 | fi |
| 62 | cd "${repo_root}" |
| 63 | |
| 64 | # 1. The receipt must describe exactly one commit: refuse a dirty tree, |
| 65 | # including untracked files that a build could pick up. |
| 66 | if [[ -n "$(git status --porcelain --untracked-files=normal)" ]]; then |
| 67 | echo "m3_receipt: refusing to run on a dirty tree. Commit, stash or remove these first:" >&2 |
| 68 | git status --short --untracked-files=normal >&2 |
| 69 | exit 2 |
| 70 | fi |
| 71 | |
| 72 | head_sha="$(git rev-parse HEAD)" |
| 73 | head_subject="$(git log -1 --format=%s HEAD)" |
| 74 | branch="$(git rev-parse --abbrev-ref HEAD)" |
| 75 | origin_main_local="$(git rev-parse --verify -q refs/remotes/origin/main || echo unknown)" |
| 76 | origin_main_live="$(timeout 20 git ls-remote origin refs/heads/main 2>/dev/null | awk '{print $1}' || true)" |
| 77 | [[ -n "${origin_main_live}" ]] || origin_main_live="unreachable" |
| 78 | equals() { [[ "$1" == "${head_sha}" ]] && echo yes || echo no; } |
| 79 | |
| 80 | date_utc="$(date -u +%F)" |
| 81 | started_utc="$(date -u +%FT%TZ)" |
| 82 | receipt="evidence/m3_receipt_${date_utc}.md" |
| 83 | |
| 84 | log_dir="${requested_log_dir:-$(mktemp -d "${TMPDIR:-/tmp}/aienos-m3-receipt.XXXXXX")}" |
| 85 | mkdir -p "${log_dir}/serial" "${log_dir}/shim" |
| 86 | log_dir="$(cd "${log_dir}" && pwd)" |
| 87 | if [[ -n "$(ls -A "${log_dir}/serial")" ]]; then |
| 88 | echo "m3_receipt: ${log_dir}/serial is not empty; use a fresh AIENOS_RECEIPT_LOG_DIR" >&2 |
| 89 | exit 2 |
| 90 | fi |
| 91 | : >"${log_dir}/commands.log" |
| 92 | |
| 93 | # 2. Record every cargo and qemu-system-aarch64 invocation with its exact |
| 94 | # arguments. A thin wrapper on PATH logs the argv, plus the build-time |
| 95 | # AIENOS_* variables the image embeds (option_env!), then execs the real tool. |
| 96 | make_shim() { # tool name |
| 97 | local real |
| 98 | real="$(command -v "$1" || true)" |
| 99 | [[ -n "${real}" ]] || return 0 |
| 100 | cat >"${log_dir}/shim/$1" <<EOF |
| 101 | #!/usr/bin/env bash |
| 102 | { |
| 103 | env | grep -E '^AIENOS_(COMMIT|RESTART_SECS|KEYBOARD_SECS)=' | sort | tr '\\n' ' ' |
| 104 | printf '%q ' "$1" "\$@" |
| 105 | echo |
| 106 | } >>"${log_dir}/commands.log" |
| 107 | exec "${real}" "\$@" |
| 108 | EOF |
| 109 | chmod +x "${log_dir}/shim/$1" |
| 110 | } |
| 111 | make_shim cargo |
| 112 | make_shim qemu-system-aarch64 |
| 113 | |
| 114 | first_line() { "$@" 2>/dev/null | head -n 1 || true; } |
| 115 | sha_of() { [[ -r "$1" ]] && sha256sum "$1" | awk '{print $1}' || echo "not readable"; } |
| 116 | os_name="$( (. /etc/os-release 2>/dev/null && echo "${PRETTY_NAME:-unknown}") || echo unknown)" |
| 117 | rustc_v="$(first_line rustc -V)" |
| 118 | cargo_v="$(first_line cargo -V)" |
| 119 | qemu_v="$(first_line qemu-system-aarch64 --version)" |
| 120 | swtpm_v="$(first_line swtpm --version)" |
| 121 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 122 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 123 | |
| 124 | harness_cmd="AIENOS_STRICT=1 AIENOS_LOG_DIR=${log_dir}/serial bash scripts/verify_all.sh" |
| 125 | [[ -z "${AIENOS_QEMU_TIMEOUT:-}" ]] || harness_cmd="AIENOS_QEMU_TIMEOUT=${AIENOS_QEMU_TIMEOUT} ${harness_cmd}" |
| 126 | |
| 127 | # 3. Run the strict harness. |
| 128 | echo "m3_receipt: commit ${head_sha}" |
| 129 | echo "m3_receipt: raw logs in ${log_dir}" |
| 130 | echo "m3_receipt: running ${harness_cmd}" |
| 131 | set +e |
| 132 | PATH="${log_dir}/shim:${PATH}" AIENOS_STRICT=1 AIENOS_LOG_DIR="${log_dir}/serial" \ |
| 133 | bash scripts/verify_all.sh 2>&1 | tee "${log_dir}/verify_all.log" |
| 134 | verify_status=${PIPESTATUS[0]} |
| 135 | set -e |
| 136 | finished_utc="$(date -u +%FT%TZ)" |
| 137 | |
| 138 | tree_after="clean" |
| 139 | if [[ -n "$(git status --porcelain --untracked-files=normal)" ]]; then |
| 140 | tree_after="DIRTY (the run changed the tree)" |
| 141 | fi |
| 142 | |
| 143 | # 4. Collect PASS lines with the verify_all.sh section they came from. |
| 144 | pass_tsv="${log_dir}/pass_lines.tsv" |
| 145 | awk ' |
| 146 | { sub(/\r$/, "") } |
| 147 | /^--- \[.*\] ---$/ { sec = $0; sub(/^--- \[/, "", sec); sub(/\] ---$/, "", sec); next } |
| 148 | /^PASS[[:space:]]|: PASS|ALL TESTS PASS/ { print sec "\t" $0 } |
| 149 | ' "${log_dir}/verify_all.log" >"${pass_tsv}" |
| 150 | |
| 151 | results="${log_dir}/results.md" |
| 152 | claimed="${log_dir}/claimed.tsv" |
| 153 | : >"${results}" |
| 154 | : >"${claimed}" |
| 155 | missing=() |
| 156 | # proof name, section regex, PASS line regex (both matched lower-cased). |
| 157 | proof() { |
| 158 | local name="$1" sec_re="$2" line_re="$3" hits |
| 159 | hits="$(awk -F '\t' -v s="${sec_re}" -v l="${line_re}" \ |
| 160 | 'tolower($1) ~ s && tolower($2) ~ l' "${pass_tsv}")" |
| 161 | { |
| 162 | echo "### ${name}" |
| 163 | echo "" |
| 164 | if [[ -z "${hits}" ]]; then |
| 165 | echo " MISSING" |
| 166 | else |
| 167 | printf '%s\n' "${hits}" | awk -F '\t' '{ printf " [%s] %s\n", $1, $2 }' |
| 168 | fi |
| 169 | echo "" |
| 170 | } >>"${results}" |
| 171 | if [[ -z "${hits}" ]]; then |
| 172 | missing+=("${name}") |
| 173 | else |
| 174 | printf '%s\n' "${hits}" >>"${claimed}" |
| 175 | fi |
| 176 | } |
| 177 | boot_sec="^qemu aarch64 uefi boot" |
| 178 | smmu_sec="smmu" |
| 179 | proof "Formatting" "^formatting" "formatting" |
| 180 | proof "Host tests" "^host component" "host tests" |
| 181 | proof "Clippy" "^clippy" "clippy" |
| 182 | proof "AArch64 build" "bare-metal|aarch64 uefi image" "aarch64 build" |
| 183 | proof "QEMU boot" "${boot_sec}" "pre-exit report|image is this commit|entered the kernel|kernel entered el1h|final report|qemu_boot: pass" |
| 184 | proof "MMU" "${boot_sec}" "page table" |
| 185 | proof "GIC/timer" "${boot_sec}" "gic|timer irq" |
| 186 | proof "Preemption" "${boot_sec}" "preempt|placement" |
| 187 | proof "EL0 isolation" "." "el0" |
| 188 | proof "Capability" "${boot_sec}" "capabilit" |
| 189 | proof "IPC" "." "(^|[^a-z])ipc([^a-z]|$)" |
| 190 | proof "SMMU positive DMA" "${smmu_sec}" "iort stream|dma window translated|typed text echoed" |
| 191 | proof "SMMU negative DMA" "${smmu_sec}" "negative|(dma|stream).*(block|denied|deny|fault|reject|abort)|(block|denied|deny|fault|reject|abort).*dma" |
| 192 | proof "Device revocation" "." "revok|revoc" |
| 193 | proof "Recovery/security regressions" "^trust-1 gate 4|^trust-1 gate 1|^recovery media tooling" "." |
| 194 | proof "ABI conformance" "." "(^|[^a-z])abi([^a-z]|$)" |
| 195 | |
| 196 | other="$(grep -vxF -f "${claimed}" "${pass_tsv}" || true)" |
| 197 | |
| 198 | # 5. Hash every raw log. |
| 199 | hashes="${log_dir}/hashes.txt" |
| 200 | : >"${hashes}" |
| 201 | for f in "${log_dir}/verify_all.log" "${log_dir}/commands.log" "${log_dir}"/serial/*.log; do |
| 202 | [[ -f "${f}" ]] || continue |
| 203 | printf '%s %s (%s lines)\n' "$(sha256sum "${f}" | awk '{print $1}')" \ |
| 204 | "${f#"${log_dir}/"}" "$(wc -l <"${f}" | tr -d ' ')" >>"${hashes}" |
| 205 | done |
| 206 | serial_count="$(find "${log_dir}/serial" -name '*.log' | wc -l | tr -d ' ')" |
| 207 | |
| 208 | # Debug-only build features seen in the recorded commands. |
| 209 | debug_features="$(grep -oE -- '--features [^ ]+' "${log_dir}/commands.log" | tr ',' '\n' \ |
| 210 | | grep -oE 'debug[a-z0-9_-]*' | sort -u | tr '\n' ' ' || true)" |
| 211 | |
| 212 | if [[ "${verify_status}" != 0 || "${tree_after}" != "clean" ]]; then |
| 213 | overall="FAIL (verify_all.sh exit ${verify_status}, tree after run: ${tree_after})" |
| 214 | exit_code=1 |
| 215 | elif [[ "${#missing[@]}" -gt 0 ]]; then |
| 216 | if [[ "${allow_missing}" == 1 ]]; then |
| 217 | overall="INCOMPLETE: ${#missing[@]} proof(s) MISSING, accepted only because --allow-missing was given" |
| 218 | exit_code=0 |
| 219 | else |
| 220 | overall="INCOMPLETE: ${#missing[@]} proof(s) MISSING" |
| 221 | exit_code=3 |
| 222 | fi |
| 223 | else |
| 224 | overall="PASS: every listed proof has at least one PASS line" |
| 225 | exit_code=0 |
| 226 | fi |
| 227 | |
| 228 | # 6. Write the receipt. |
| 229 | { |
| 230 | echo "# M3 receipt ${date_utc}" |
| 231 | echo "" |
| 232 | echo "Generated by scripts/m3_receipt.sh$([[ "${allow_missing}" == 1 ]] && echo " --allow-missing")." |
| 233 | echo "Overall: ${overall}" |
| 234 | echo "" |
| 235 | echo "## Commit" |
| 236 | echo "" |
| 237 | echo "- HEAD: ${head_sha}" |
| 238 | echo "- Subject: ${head_subject}" |
| 239 | echo "- Branch: ${branch}" |
| 240 | echo "- Tree before run: clean (checked with git status --porcelain)" |
| 241 | echo "- Tree after run: ${tree_after}" |
| 242 | echo "- origin/main (local tracking ref): ${origin_main_local} (HEAD equal: $(equals "${origin_main_local}"))" |
| 243 | echo "- origin/main (live, git ls-remote): ${origin_main_live} (HEAD equal: $(equals "${origin_main_live}"))" |
| 244 | echo "- Run: ${started_utc} to ${finished_utc}" |
| 245 | echo "" |
| 246 | echo "## Environment" |
| 247 | echo "" |
| 248 | echo "- Host: $(uname -n)" |
| 249 | echo "- Arch: $(uname -m)" |
| 250 | echo "- OS: ${os_name}, kernel $(uname -r)" |
| 251 | echo "- rustc: ${rustc_v:-not found}" |
| 252 | echo "- cargo: ${cargo_v:-not found}" |
| 253 | echo "- QEMU: ${qemu_v:-not found}" |
| 254 | echo "- swtpm: ${swtpm_v:-not found}" |
| 255 | echo "- AAVMF code: ${code_fd} sha256 $(sha_of "${code_fd}")" |
| 256 | echo "- AAVMF vars: ${vars_fd} sha256 $(sha_of "${vars_fd}")" |
| 257 | echo "" |
| 258 | echo "## Commands" |
| 259 | echo "" |
| 260 | echo "Harness (exit ${verify_status}):" |
| 261 | echo "" |
| 262 | echo " ${harness_cmd}" |
| 263 | echo "" |
| 264 | echo "Every cargo invocation, in first-use order, with the build-time AIENOS_*" |
| 265 | echo "variables that were set (recorded by a PATH wrapper, not reconstructed):" |
| 266 | echo "" |
| 267 | grep -E "(^| )cargo " "${log_dir}/commands.log" | sed 's/ *$//' | awk '!seen[$0]++' | sed 's/^/ /' || true |
| 268 | echo "" |
| 269 | echo "Every qemu-system-aarch64 invocation (temporary paths differ per run):" |
| 270 | echo "" |
| 271 | grep -E "(^| )qemu-system-aarch64 " "${log_dir}/commands.log" | sed 's/ *$//' | sed 's/^/ /' || true |
| 272 | echo "" |
| 273 | echo "## Results" |
| 274 | echo "" |
| 275 | echo "PASS lines from the harness output, grouped by proof. Each line shows the" |
| 276 | echo "verify_all.sh section it came from. One line may support more than one proof." |
| 277 | echo "" |
| 278 | cat "${results}" |
| 279 | echo "### Other PASS lines" |
| 280 | echo "" |
| 281 | if [[ -n "${other}" ]]; then |
| 282 | printf '%s\n' "${other}" | awk -F '\t' '{ printf " [%s] %s\n", $1, $2 }' |
| 283 | else |
| 284 | echo " none" |
| 285 | fi |
| 286 | echo "" |
| 287 | echo "## Log hashes" |
| 288 | echo "" |
| 289 | echo "Raw logs are not committed. They were kept on the run host in ${log_dir}" |
| 290 | echo "(${serial_count} serial logs)." |
| 291 | echo "" |
| 292 | sed 's/^/ /' "${hashes}" |
| 293 | echo "" |
| 294 | echo "## Limitations" |
| 295 | echo "" |
| 296 | echo "- QEMU only (virt machine, TCG). Nothing here is proven on Machine 1 (GB10) hardware." |
| 297 | echo "- One SMMUv3 instance (QEMU virt iommu=smmuv3) guarding one device stream (xHCI)." |
| 298 | echo " Machine 1 has several SMMUs; none of them is exercised here." |
| 299 | if [[ -n "${debug_features}" ]]; then |
| 300 | echo "- Debug-only build features used by some test images: ${debug_features% }." |
| 301 | echo " The QEMU keyboard test (no SMMU) enables identity xHCI DMA through a debug bypass;" |
| 302 | echo " the confined path is the separate SMMU run." |
| 303 | else |
| 304 | echo "- No debug-only build features were used." |
| 305 | fi |
| 306 | echo "- The recovery boots run the host's Linux kernel from /boot with the AIENOS recovery" |
| 307 | echo " initrd; they are not AIENOS kernel boots." |
| 308 | if [[ "${#missing[@]}" -gt 0 ]]; then |
| 309 | echo "- Not yet implemented or not yet reported by any test (MISSING above):" |
| 310 | for m in "${missing[@]}"; do echo " - ${m}"; done |
| 311 | fi |
| 312 | if [[ "$(equals "${origin_main_live}")" != yes ]]; then |
| 313 | echo "- HEAD is not the live origin/main tip, so this receipt is not for main." |
| 314 | fi |
| 315 | echo "" |
| 316 | echo "## Operator sign-off" |
| 317 | echo "" |
| 318 | echo "Operator sign-off: pending" |
| 319 | } >"${receipt}" |
| 320 | |
| 321 | echo "" |
| 322 | echo "m3_receipt: wrote ${receipt}" |
| 323 | echo "m3_receipt: ${overall}" |
| 324 | if [[ "${#missing[@]}" -gt 0 ]]; then |
| 325 | echo "m3_receipt: MISSING proofs: ${missing[*]}" |
| 326 | fi |
| 327 | exit "${exit_code}" |