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 / cap_abi_isolation_test.sh

181 lines · 8776 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# 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
41set -euo pipefail
42
43ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
44WORK="$(mktemp -d)"
45trap 'rm -rf "$WORK"' EXIT
46CC="${CC:-cc}"
47CFLAGS="-std=gnu11 -O2 -Wall -Wextra -Werror"
48fails=0
49
50die() { echo "cap-abi-isolation FAIL: $*" >&2; echo "AIENOS_CAP_ABI_ISOLATION: FAIL"; exit 1; }
51check() { # 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
56for tool in "$CC" ar nm cargo; do
57 command -v "$tool" > /dev/null || die "missing tool: $tool"
58done
59
60# ---- build both archives (any failure here is a test failure) -------------
61make -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"; }
63C_LIB="$WORK/c/libaienos_capability.a"
64
65TARGET_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"; }
68R_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 ------------------------------------------------------------
73syms() { # 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}
77syms "$C_LIB" > "$WORK/c.syms"
78syms "$R_LIB" > "$WORK/r.syms"
79echo "C archive: $(wc -l < "$WORK/c.syms") aienos_cap* text symbols"
80echo "Rust archive: $(wc -l < "$WORK/r.syms") aienos_cap* text symbols"
81
82has() { grep -qx "$2" "$1"; }
83for 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"
85done
86for 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"
89done
90
91comm -12 "$WORK/c.syms" "$WORK/r.syms" > "$WORK/common.syms"
92check "no aienos_cap* symbol defined by both archives" test ! -s "$WORK/common.syms"
93[ -s "$WORK/common.syms" ] && sed 's/^/ shared: /' "$WORK/common.syms"
94check "C archive defines no aienos_cap32_*" bash -c "! grep -q '^aienos_cap32_' '$WORK/c.syms'"
95check "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 ---------------------------
98cat > "$WORK/probe.c" << 'C'
99#include <stdio.h>
100#include "aienos_capability.h"
101int 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}
113C
114"$CC" $CFLAGS -I "$ROOT/native/capability" -c -o "$WORK/probe.o" "$WORK/probe.c" ||
115 die "probe compile against aienos_capability.h"
116
117RLINK="-lpthread -ldl -lm"
118check "C-ABI probe links against the C archive" \
119 "$CC" -o "$WORK/probe_c" "$WORK/probe.o" "$C_LIB" -pthread
120check "C-ABI probe runs against the C archive" "$WORK/probe_c"
121
122# shellcheck disable=SC2086
123if "$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))
126elif 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_*)"
129else
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))
133fi
134
135# ---- control: the same link line works for a real cap32 caller -------------
136cat > "$WORK/probe32.c" << 'C'
137#include <stdint.h>
138#include <stdio.h>
139uint32_t aienos_cap32_abi_layout(uint32_t *out);
140int 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}
146C
147"$CC" $CFLAGS -c -o "$WORK/probe32.o" "$WORK/probe32.c" || die "cap32 probe compile"
148# shellcheck disable=SC2086
149check "control: cap32 caller links against the Rust archive" \
150 "$CC" -o "$WORK/probe32" "$WORK/probe32.o" "$R_LIB" $RLINK
151check "control: aienos_cap32_abi_layout reports v1 8/4/4/64" "$WORK/probe32"
152check "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 ----------------------------
156drift() { # 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}
172drift 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;/'
174drift 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
177if [ "$fails" -ne 0 ]; then
178 echo "AIENOS_CAP_ABI_ISOLATION: FAIL ($fails checks failed)"
179 exit 1
180fi
181echo "AIENOS_CAP_ABI_ISOLATION: PASS"