interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / runtime / aienos_cap_abi.sh

134 lines · 7448 bytes Raw Blame History
1#!/bin/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# OMEGA_AIENOS_CAP_ABI: src/runtime/aienos_cap.h is a hand-kept copy of aienos
18# native/capability/aienos_capability.h. The runtime compiles against the copy and
19# links libaienos_capability.a built from the aienos.lock commit, so the copy must
20# match the pinned header: struct layouts field by field, macro values, prototypes
21# (tests/runtime/aienos_cap_abi_test.c says how each is checked).
22#
23# usage: aienos_cap_abi.sh <dir holding the pinned aienos_capability.h>
24#
25# The copy must PASS; each mutant below (a temporary copy of aienos_cap.h with one
26# change) must be refused with a diagnostic naming what changed. A mutant whose
27# pattern no longer matches is a failure of this script, never a silent skip.
28# Shell, sed, awk and the C compiler only.
29set -u
30PIN=${1:?usage: aienos_cap_abi.sh <pinned aienos native/capability dir>}
31HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
32COPY=$HERE/src/runtime/aienos_cap.h
33TEST=$HERE/tests/runtime/aienos_cap_abi_test.c
34CC=${CC:-cc}
35CFLAGS=${CFLAGS:--std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2}
36export LC_ALL=C
37[ -f "$PIN/aienos_capability.h" ] || { echo "error: no aienos_capability.h in $PIN"; exit 1; }
38W=$(mktemp -d "${TMPDIR:-/tmp}/aienos-cap-abi.XXXXXX")
39trap 'rm -rf "$W"' EXIT
40
41# gen <header> <dir>: the files aienos_cap_abi_test.c includes (see its comment).
42gen() {
43 mkdir -p "$2" && cp "$1" "$2/omega_view.h" || return 1
44 sed -e '/^typedef struct {/,/^}/d' -e '/^#ifndef /d' -e '/^#endif/d' \
45 -e '/^#define [A-Za-z0-9_]*[ \t]*$/d' -e '/^#include /d' "$1" >"$2/omega_protos.h" || return 1
46 awk -v D="$2" '
47 function bad(m) { printf "aienos_cap_abi: %s:%d: %s: %s\n", FILENAME, FNR, m, $0 > "/dev/stderr"; err = 1; exit 1 }
48 function ident_before(s, re) { if (!match(s, re)) bad("no name"); s = substr(s, 1, RSTART - 1); match(s, /[A-Za-z_][A-Za-z0-9_]*$/); return substr(s, RSTART, RLENGTH) }
49 /^typedef struct \{[ \t]*$/ { ins = 1; nf = 0; next }
50 ins && /^\}/ {
51 if (!match($0, /^\}[ \t]*[A-Za-z_][A-Za-z0-9_]*[ \t]*;[ \t]*$/)) bad("struct end")
52 t = $0; sub(/^\}[ \t]*/, "", t); sub(/[ \t]*;.*$/, "", t)
53 structs = structs " X(" t ", " (++ns) ")"; ren[++nr] = t; pre[nr] = "O_"
54 for (i = 1; i <= nf; i++) fields = fields " X(" t ", " f[i] ")"
55 ins = 0; next
56 }
57 ins && /^[ \t]*$/ { next }
58 ins && /^[ \t]*(\/\*|\*)/ { next }
59 ins {
60 if ($0 !~ /^[ \t]+[A-Za-z_][A-Za-z0-9_ \t*]*[ \t*][A-Za-z_][A-Za-z0-9_]*[ \t]*(\[[^]]*\])?[ \t]*;[ \t]*$/) bad("field")
61 s = $0; sub(/[ \t]*;[ \t]*$/, "", s); sub(/[ \t]*\[[^]]*\]$/, "", s)
62 match(s, /[A-Za-z_][A-Za-z0-9_]*$/); f[++nf] = substr(s, RSTART, RLENGTH); next
63 }
64 /^typedef[^;]*\(\*[ \t]*[A-Za-z_][A-Za-z0-9_]*[ \t]*\)/ {
65 ren[++nr] = ident_before($0, "[ \t]*\\)[ \t]*\\("); pre[nr] = "O_"; next
66 }
67 /^#define[ \t]+[A-Za-z_][A-Za-z0-9_]*[ \t]+[^ \t]/ { macros[++nm] = $2; next }
68 /^[A-Za-z_][A-Za-z0-9_ \t*]*[ \t*][A-Za-z_][A-Za-z0-9_]*[ \t]*\(/ && $1 != "typedef" {
69 fn = ident_before($0, "[ \t]*\\("); funcs = funcs " X(" fn ")"; ren[++nr] = fn; pre[nr] = "o_"; ++nfn; next
70 }
71 END {
72 if (err) exit 1
73 if (ins) { print "aienos_cap_abi: unterminated struct" > "/dev/stderr"; exit 1 }
74 if (!ns || !nfn) { print "aienos_cap_abi: no struct or no function found" > "/dev/stderr"; exit 1 }
75 L = D "/aca_list.h"
76 print "#define ACA_STRUCTS(X)" structs > L; print "#define ACA_FIELDS(X)" fields > L
77 print "#define ACA_FUNCS(X)" funcs > L
78 print "#define ACA_FUNC_COUNT " nfn > L; print "#define ACA_MACRO_COUNT " nm > L
79 for (i = 1; i <= nm; i++)
80 printf "#ifndef %s\n#error \"%s is not defined by the pinned header\"\n#endif\n", macros[i], macros[i] > (D "/aca_exist.h")
81 printf "" > (D "/aca_exist.h")
82 for (i = 1; i <= nr; i++) {
83 printf "#define %s %s%s\n", ren[i], pre[i], ren[i] > (D "/aca_rename.h")
84 printf "#undef %s\n", ren[i] > (D "/aca_unrename.h")
85 }
86 }' "$1"
87}
88
89# check <header> <dir>: 0 when the header matches the pinned one; output in <dir>/log.
90check() {
91 gen "$1" "$2" >"$2.gen.log" 2>&1 || { mkdir -p "$2"; cat "$2.gen.log" >"$2/log"; return 1; }
92 # shellcheck disable=SC2086
93 $CC $CFLAGS -Werror -I"$2" -I"$PIN" -o "$2/t" "$TEST" >"$2/log" 2>&1 </dev/null || return 1
94 "$2/t" >>"$2/log" 2>&1 </dev/null
95}
96
97if ! check "$COPY" "$W/copy"; then
98 echo "FAIL: src/runtime/aienos_cap.h does not match $PIN/aienos_capability.h:"
99 grep -E "error|FAIL|aienos_cap_abi:" "$W/copy/log" | head -20
100 echo "GATE OMEGA_AIENOS_CAP_ABI_PASS FAIL"
101 exit 1
102fi
103grep '^GATE ' "$W/copy/log"
104
105# name|sed program applied to the copy|text the refusal must name
106MUTANTS='M1 two same-size AienosCapEntry fields swapped (issuer, subject)|0,/^ uint32_t issuer;$/{/^ uint32_t issuer;$/{N;s/^\(.*\)\n\(.*\)$/\2\n\1/}}|offsetof AienosCapEntry.issuer
107M2 AienosCapEntry.rights signed, same size and offset|0,/^ uint32_t rights;$/s// int32_t rights;/|kind AienosCapEntry.rights
108M3 aienos_cap_reclaim cap_id widened to 64 bits|s/^\(int aienos_cap_reclaim(.*\)uint32_t cap_id);$/\1uint64_t cap_id);/|aienos_cap_reclaim
109M4 aienos_cap_clock returns 32 bits|s/^uint64_t aienos_cap_clock(/uint32_t aienos_cap_clock(/|aienos_cap_clock
110M5 observer op parameter widened|s/^\(typedef void (\*AienosCapObserver)(void \*ctx, \)uint32_t op,/\1uint64_t op,/|AienosCapObserver
111M6 AIENOS_CAP_OBS_KILL value changed|s/^#define AIENOS_CAP_OBS_KILL 6u$/#define AIENOS_CAP_OBS_KILL 9u/|AIENOS_CAP_OBS_KILL
112M7 a function the pinned header does not declare|/^int aienos_cap_kill(/a int aienos_cap_unpinned(AienosCapAdmin *admin);|aienos_cap_unpinned
113M8 a macro the pinned header does not define|/^#define AIENOS_CAP_OBS_KILL /a #define AIENOS_CAP_OBS_UNPINNED 9u|AIENOS_CAP_OBS_UNPINNED'
114
115total=0 caught=0
116while IFS='|' read -r name prog expect; do
117 total=$((total + 1)) id=${name%% *}
118 mkdir -p "$W/$id"
119 sed -e "$prog" "$COPY" >"$W/$id/aienos_cap.h"
120 if cmp -s "$COPY" "$W/$id/aienos_cap.h"; then
121 echo "MUTANT $name: FAIL (pattern no longer matches src/runtime/aienos_cap.h)"
122 elif check "$W/$id/aienos_cap.h" "$W/$id/case"; then
123 echo "MUTANT $name: FAIL (accepted)"
124 elif ! grep -q -- "$expect" "$W/$id/case/log"; then
125 echo "MUTANT $name: FAIL (refused, but not for \"$expect\"):"
126 grep -E 'error' "$W/$id/case/log" | head -3
127 else
128 caught=$((caught + 1))
129 echo "MUTANT $name: refused ($(grep -m1 -- "$expect" "$W/$id/case/log" | grep -o 'error: .*' | cut -c1-90))"
130 fi
131done <<<"$MUTANTS"
132echo "GATE OMEGA_AIENOS_CAP_ABI_MUTANTS_PASS $([ "$caught" = "$total" ] && echo PASS || echo FAIL) $caught/$total"
133[ "$caught" = "$total" ] || exit 1
134echo "OMEGA_AIENOS_CAP_ABI_PASS"