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 / cap_abi_isolation_test.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 | # The native C capability authority and the Rust capability crates are two |
| 18 | # separate implementations with incompatible layouts: |
| 19 | # |
| 20 | # native/capability (production ABI) AienosCapRef 16 bytes, u64 generation, |
| 21 | # symbols aienos_cap_* |
| 22 | # crates/aienos-capability-ffi AienosCapRef 8 bytes, u32 generation, |
| 23 | # symbols aienos_cap32_* |
| 24 | # |
| 25 | # This test proves they cannot be swapped by accident. It builds both archives |
| 26 | # and checks, with nm and the linker: |
| 27 | # 1 both archives define their expected entry points (non-empty, positive) |
| 28 | # 2 no aienos_cap* text symbol is defined by both archives |
| 29 | # 3 the C archive defines no aienos_cap32_*; the Rust one no aienos_cap_* |
| 30 | # 4 a C program compiled against aienos_capability.h links and runs against |
| 31 | # the C archive, and FAILS TO LINK against the Rust archive with an |
| 32 | # undefined reference to aienos_cap_* |
| 33 | # 5 control: a C caller of the cap32 ABI links against the Rust archive with |
| 34 | # the same link line, and aienos_cap32_abi_layout reports 8/4/4/64, v1 |
| 35 | # 6 the C layout asserts reject a drifted header: a narrowed generation, and |
| 36 | # an equal-size reorder of AienosCapRef |
| 37 | # Any build failure, or any check that cannot run, fails the test (exit 1). |
| 38 | # No Python. Needs cc, ar, nm, cargo. |
| 39 | # |
| 40 | # bash scripts/cap_abi_isolation_test.sh |
| 41 | set -euo pipefail |
| 42 | |
| 43 | ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 44 | WORK="$(mktemp -d)" |
| 45 | trap 'rm -rf "$WORK"' EXIT |
| 46 | CC="${CC:-cc}" |
| 47 | CFLAGS="-std=gnu11 -O2 -Wall -Wextra -Werror" |
| 48 | fails=0 |
| 49 | |
| 50 | die() { echo "cap-abi-isolation FAIL: $*" >&2; echo "AIENOS_CAP_ABI_ISOLATION: FAIL"; exit 1; } |
| 51 | check() { # name, then a command that must succeed |
| 52 | local name="$1"; shift |
| 53 | if "$@"; then echo "ok $name"; else echo "FAIL $name"; fails=$((fails + 1)); fi |
| 54 | } |
| 55 | |
| 56 | for tool in "$CC" ar nm cargo; do |
| 57 | command -v "$tool" > /dev/null || die "missing tool: $tool" |
| 58 | done |
| 59 | |
| 60 | # ---- build both archives (any failure here is a test failure) ------------- |
| 61 | make -s -C "$ROOT/native/capability" OUT="$WORK/c" "$WORK/c/libaienos_capability.a" \ |
| 62 | > "$WORK/c-build.log" 2>&1 || { tail -20 "$WORK/c-build.log"; die "native C library build"; } |
| 63 | C_LIB="$WORK/c/libaienos_capability.a" |
| 64 | |
| 65 | TARGET_DIR="${CARGO_TARGET_DIR:-$ROOT/target}" |
| 66 | (cd "$ROOT" && cargo build --locked -p aienos-capability-ffi) \ |
| 67 | > "$WORK/rust-build.log" 2>&1 || { tail -20 "$WORK/rust-build.log"; die "Rust staticlib build"; } |
| 68 | R_LIB="$TARGET_DIR/debug/libaienos_capability_ffi.a" |
| 69 | [ -s "$C_LIB" ] || die "C archive missing: $C_LIB" |
| 70 | [ -s "$R_LIB" ] || die "Rust archive missing: $R_LIB" |
| 71 | |
| 72 | # ---- symbol sets ------------------------------------------------------------ |
| 73 | syms() { # defined global text symbols starting with aienos_cap |
| 74 | nm --defined-only -g "$1" 2> /dev/null | awk '$2 == "T" {print $3}' | |
| 75 | grep '^aienos_cap' | sort -u || true |
| 76 | } |
| 77 | syms "$C_LIB" > "$WORK/c.syms" |
| 78 | syms "$R_LIB" > "$WORK/r.syms" |
| 79 | echo "C archive: $(wc -l < "$WORK/c.syms") aienos_cap* text symbols" |
| 80 | echo "Rust archive: $(wc -l < "$WORK/r.syms") aienos_cap* text symbols" |
| 81 | |
| 82 | has() { grep -qx "$2" "$1"; } |
| 83 | for s in aienos_cap_start aienos_cap_stop aienos_cap_mint aienos_cap_validate aienos_cap_restart; do |
| 84 | check "C archive defines $s" has "$WORK/c.syms" "$s" |
| 85 | done |
| 86 | for s in aienos_cap32_start aienos_cap32_stop aienos_cap32_mint aienos_cap32_validate \ |
| 87 | aienos_cap32_restart aienos_cap32_abi_layout; do |
| 88 | check "Rust archive defines $s" has "$WORK/r.syms" "$s" |
| 89 | done |
| 90 | |
| 91 | comm -12 "$WORK/c.syms" "$WORK/r.syms" > "$WORK/common.syms" |
| 92 | check "no aienos_cap* symbol defined by both archives" test ! -s "$WORK/common.syms" |
| 93 | [ -s "$WORK/common.syms" ] && sed 's/^/ shared: /' "$WORK/common.syms" |
| 94 | check "C archive defines no aienos_cap32_*" bash -c "! grep -q '^aienos_cap32_' '$WORK/c.syms'" |
| 95 | check "Rust archive defines no aienos_cap_*" bash -c "! grep -q '^aienos_cap_' '$WORK/r.syms'" |
| 96 | |
| 97 | # ---- link probe: a caller of the production C ABI --------------------------- |
| 98 | cat > "$WORK/probe.c" << 'C' |
| 99 | #include <stdio.h> |
| 100 | #include "aienos_capability.h" |
| 101 | int main(void) { |
| 102 | AienosCapAdmin *admin = NULL; |
| 103 | AienosCapView *view = NULL; |
| 104 | if (aienos_cap_start(&admin, &view) != AIENOS_CAP_OK) return 2; |
| 105 | AienosCapRef office; |
| 106 | if (aienos_cap_office(admin, &office) != AIENOS_CAP_OK) return 3; |
| 107 | int rc = aienos_cap_validate(view, office, 0, AIENOS_CAP_RES_AUTHORITY, |
| 108 | AIENOS_CAP_RIGHT_MINT, NULL); |
| 109 | aienos_cap_stop(admin, view); |
| 110 | printf("probe sizeof(AienosCapRef)=%zu validate=%d\n", sizeof(AienosCapRef), rc); |
| 111 | return rc == AIENOS_CAP_OK && sizeof(AienosCapRef) == 16 ? 0 : 4; |
| 112 | } |
| 113 | C |
| 114 | "$CC" $CFLAGS -I "$ROOT/native/capability" -c -o "$WORK/probe.o" "$WORK/probe.c" || |
| 115 | die "probe compile against aienos_capability.h" |
| 116 | |
| 117 | RLINK="-lpthread -ldl -lm" |
| 118 | check "C-ABI probe links against the C archive" \ |
| 119 | "$CC" -o "$WORK/probe_c" "$WORK/probe.o" "$C_LIB" -pthread |
| 120 | check "C-ABI probe runs against the C archive" "$WORK/probe_c" |
| 121 | |
| 122 | # shellcheck disable=SC2086 |
| 123 | if "$CC" -o "$WORK/probe_r" "$WORK/probe.o" "$R_LIB" $RLINK > "$WORK/probe_r.log" 2>&1; then |
| 124 | echo "FAIL C-ABI probe LINKED against the Rust archive (layouts would be misread)" |
| 125 | fails=$((fails + 1)) |
| 126 | elif grep -q "undefined reference to .aienos_cap_start" "$WORK/probe_r.log" && |
| 127 | grep -q "undefined reference to .aienos_cap_validate" "$WORK/probe_r.log"; then |
| 128 | echo "ok C-ABI probe fails to link against the Rust archive (undefined aienos_cap_*)" |
| 129 | else |
| 130 | echo "FAIL C-ABI probe did not link against the Rust archive, but not for the expected reason:" |
| 131 | tail -5 "$WORK/probe_r.log" |
| 132 | fails=$((fails + 1)) |
| 133 | fi |
| 134 | |
| 135 | # ---- control: the same link line works for a real cap32 caller ------------- |
| 136 | cat > "$WORK/probe32.c" << 'C' |
| 137 | #include <stdint.h> |
| 138 | #include <stdio.h> |
| 139 | uint32_t aienos_cap32_abi_layout(uint32_t *out); |
| 140 | int main(void) { |
| 141 | uint32_t w[4] = {0, 0, 0, 0}; |
| 142 | uint32_t v = aienos_cap32_abi_layout(w); |
| 143 | printf("cap32 abi v%u ref=%u gen_off=%u gen_size=%u entry=%u\n", v, w[0], w[1], w[2], w[3]); |
| 144 | return v == 1 && w[0] == 8 && w[1] == 4 && w[2] == 4 && w[3] == 64 ? 0 : 1; |
| 145 | } |
| 146 | C |
| 147 | "$CC" $CFLAGS -c -o "$WORK/probe32.o" "$WORK/probe32.c" || die "cap32 probe compile" |
| 148 | # shellcheck disable=SC2086 |
| 149 | check "control: cap32 caller links against the Rust archive" \ |
| 150 | "$CC" -o "$WORK/probe32" "$WORK/probe32.o" "$R_LIB" $RLINK |
| 151 | check "control: aienos_cap32_abi_layout reports v1 8/4/4/64" "$WORK/probe32" |
| 152 | check "control: cap32 caller does not link against the C archive" \ |
| 153 | bash -c "! '$CC' -o '$WORK/probe32_c' '$WORK/probe32.o' '$C_LIB' -pthread > /dev/null 2>&1" |
| 154 | |
| 155 | # ---- the C layout asserts reject a drifted header ---------------------------- |
| 156 | drift() { # name, perl substitution on the header; the copy must NOT compile |
| 157 | local name="$1" expr="$2" d="$WORK/drift-$1" |
| 158 | mkdir -p "$d" |
| 159 | cp "$ROOT/native/capability/aienos_capability.c" "$ROOT/native/capability/aienos_capability.h" "$d/" |
| 160 | perl -0pi -e "$expr" "$d/aienos_capability.h" |
| 161 | if cmp -s "$d/aienos_capability.h" "$ROOT/native/capability/aienos_capability.h"; then |
| 162 | echo "FAIL drift $name: substitution did not apply"; fails=$((fails + 1)); return |
| 163 | fi |
| 164 | if "$CC" $CFLAGS -c -o "$d/x.o" "$d/aienos_capability.c" > "$d/log" 2>&1; then |
| 165 | echo "FAIL drift $name: compiled, layout asserts did not fire"; fails=$((fails + 1)) |
| 166 | elif grep -q 'static assertion failed' "$d/log"; then |
| 167 | echo "ok drift $name rejected by _Static_assert" |
| 168 | else |
| 169 | echo "FAIL drift $name: build failed for another reason"; tail -3 "$d/log"; fails=$((fails + 1)) |
| 170 | fi |
| 171 | } |
| 172 | drift narrowed-generation \ |
| 173 | 's/typedef struct \{\n uint32_t cap_id;\n uint64_t generation;\n\} AienosCapRef;/typedef struct {\n uint32_t cap_id;\n uint32_t generation;\n} AienosCapRef;/' |
| 174 | drift reordered-same-size \ |
| 175 | 's/typedef struct \{\n uint32_t cap_id;\n uint64_t generation;\n\} AienosCapRef;/typedef struct {\n uint64_t generation;\n uint32_t cap_id;\n} AienosCapRef;/' |
| 176 | |
| 177 | if [ "$fails" -ne 0 ]; then |
| 178 | echo "AIENOS_CAP_ABI_ISOLATION: FAIL ($fails checks failed)" |
| 179 | exit 1 |
| 180 | fi |
| 181 | echo "AIENOS_CAP_ABI_ISOLATION: PASS" |