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 / tools / runtime_api_check.sh

195 lines · 14437 bytes Raw Blame History
1#!/bin/sh
2# runtime_api_check.sh -- host check of the Omega Runtime API manifest against the C headers (no GPU, no physics).
3#
4# tools/runtime_api_check.sh [--bless] MANIFEST ROOT WORK_DIR [API_MD]
5#
6# MANIFEST docs/runtime/omega_runtime_api.manifest (line format: docs/runtime/API.md, "The manifest")
7# ROOT directory the manifest's `header` paths are relative to (the omega checkout; a copy in the negative controls)
8# WORK_DIR scratch directory for the generated probe
9# API_MD optional: also check that API.md states the version and has a section for every STABLE entry
10#
11# What it does: writes a C probe from the manifest and compiles it against the headers named there.
12# - every `fn` line becomes a compile-time type-equality check on the function itself, so a removed function, a changed argument
13# type or a changed return type is a compile error (-Werror=incompatible-pointer-types);
14# - every `struct`/`field`/`const` line becomes a printf of sizeof/alignof/offsetof/value, compared with the manifest;
15# - the version macros OMEGA_RUNTIME_API_VERSION_MAJOR/MINOR must exist (#error otherwise) and equal the manifest.
16# With API_MD it also scans the headers back (gcc -aux-info): every external function a listed header declares must be a
17# manifest `fn` or be a leading name of a bullet in API.md section 10 (INTERNAL), never both, and every such name must still be declared. So a new public
18# function is seen. Not seen: a new struct or constant (no manifest line, nothing to compare), a field added inside struct
19# padding that the manifest does not list (the struct size would not change), and a field that changes type but keeps its
20# size (int -> unsigned). sovereign-core's Rust checker, which reads field names, covers the field cases later (R12).
21# It does not link the library and does not touch the GPU.
22# --bless prints the manifest to stdout with struct/field/const numbers taken from the headers (review the diff).
23# Exit 0 = PASS, 1 = FAIL (each mismatch printed as "FAIL: ..."), 2 = usage.
24set -u
25BLESS=0
26if [ "${1:-}" = "--bless" ]; then BLESS=1; shift; fi
27[ $# -ge 3 ] || { echo "usage: $0 [--bless] MANIFEST ROOT WORK_DIR [API_MD]" >&2; exit 2; }
28MANIFEST=$1; ROOT=$(cd "$2" && pwd) || exit 2; WORK=$3; APIMD=${4:-}
29CC=${CC:-cc}
30mkdir -p "$WORK" || exit 2
31PROBE=$WORK/probe.c; ITEMS=$WORK/items.txt; GOT=$WORK/got.txt; WANT=$WORK/want.txt
32fail=0
33say_fail() { echo "FAIL: $*"; fail=1; }
34
35# ---- 1. manifest -> probe source -------------------------------------------------------------------------
36awk -v root="$ROOT" -v items="$ITEMS" '
37function trim(s) { gsub(/^[ \t]+|[ \t]+$/, "", s); return s }
38function base(t) {
39 if (t == "u8") return "uint8_t"; if (t == "u16") return "uint16_t"; if (t == "u32") return "uint32_t"
40 if (t == "u64") return "uint64_t"; if (t == "i8") return "int8_t"; if (t == "i16") return "int16_t"
41 if (t == "i32") return "int32_t"; if (t == "i64") return "int64_t"; if (t == "f32") return "float"
42 if (t == "f64") return "double"; if (t == "usize") return "size_t"; if (t == "c_char") return "char"
43 if (t == "c_void" || t == "()" || t == "") return "void"
44 return t
45}
46function ctype(t, r) {
47 t = trim(t)
48 if (t ~ /^\*const /) { r = ctype(substr(t, 8)); return (r ~ /\*$/) ? r " const *" : "const " r " *" }
49 if (t ~ /^\*mut /) { return ctype(substr(t, 6)) " *" }
50 if (t ~ /^fn\(/) { nfp++; fptd = fptd "typedef " fnsig(t, "(*FP" nfp ")") ";\n"; return "FP" nfp }
51 return base(t)
52}
53# "fn(a, b) -> r" as a C declarator named nm
54function fnsig(s, nm, depth, i, c, cl, params, ret, n, parts, k, out) {
55 s = trim(s); sub(/^fn/, "", s); s = trim(s)
56 depth = 0; cl = 0
57 for (i = 1; i <= length(s); i++) { c = substr(s, i, 1)
58 if (c == "(") depth++
59 if (c == ")") { depth--; if (depth == 0) { cl = i; break } } }
60 params = substr(s, 2, cl - 2); ret = trim(substr(s, cl + 1)); sub(/^->/, "", ret)
61 n = splitparams(params, parts); out = ""
62 for (k = 1; k <= n; k++) out = out (k > 1 ? ", " : "") ctype(parts[k])
63 if (n == 0) out = "void"
64 return ctype(ret) " " nm "(" out ")"
65}
66function splitparams(s, parts, i, c, depth, cur, n) {
67 n = 0; cur = ""; depth = 0
68 for (i = 1; i <= length(s); i++) { c = substr(s, i, 1)
69 if (c == "(") depth++
70 if (c == ")") depth--
71 if (c == "," && depth == 0) { parts[++n] = trim(cur); cur = ""; continue }
72 cur = cur c }
73 if (trim(cur) != "") parts[++n] = trim(cur)
74 return n
75}
76function item(line) { k++; print k "\t" line > items; return sprintf("#if !defined(ONLY) || ONLY == %d\n", k) }
77BEGIN { k = 0; nfp = 0; fptd = "" }
78{ line = $0; sub(/#.*/, "", line); line = trim(line); if (line == "") next
79 nw = split(line, w, /[ \t]+/)
80 if (w[1] == "header") { inc = inc "#include \"" root "/" w[2] "\"\n"; next }
81 if (w[1] == "abi") next
82 if (w[1] == "struct") { body = body item(line) " printf(\"struct " w[2] " %zu %zu\\n\", sizeof(" w[2] "), _Alignof(" w[2] "));\n#endif\n"; next }
83 if (w[1] == "field") { body = body item(line) " printf(\"field " w[2] " " w[3] " %zu %zu\\n\", offsetof(" w[2] ", " w[3] "), sizeof(((" w[2] " *)0)->" w[3] "));\n#endif\n"; next }
84 if (w[1] == "const") { body = body item(line) " printf(\"const " w[2] " %lld\\n\", (long long)(" w[2] "));\n#endif\n"; next }
85 if (w[1] == "fn") { rest = line; sub(/^fn[ \t]+[^ \t]+[ \t]+/, "", rest)
86 decl = decl item(line) "_Static_assert(__builtin_types_compatible_p(__typeof__(" w[2] "), " fnsig(rest, "") "), \"signature of " w[2] "\");\n#endif\n"; next }
87 print "manifest: unknown or malformed line: " $0 > "/dev/stderr"; bad = 1 }
88END {
89 print "#include <stdio.h>\n#include <stddef.h>\n#include <stdint.h>\n#include <stdbool.h>"
90 print "#include \"" root "/src/omega_runtime_api.h\""
91 print "#ifndef OMEGA_RUNTIME_API_VERSION_MAJOR\n#error \"version macro missing: OMEGA_RUNTIME_API_VERSION_MAJOR\"\n#endif"
92 print "#ifndef OMEGA_RUNTIME_API_VERSION_MINOR\n#error \"version macro missing: OMEGA_RUNTIME_API_VERSION_MINOR\"\n#endif"
93 printf "%s", inc
94 printf "%s", fptd
95 printf "%s", decl
96 print "int main(void) {\n printf(\"const OMEGA_RUNTIME_API_VERSION_MAJOR %lld\\n\", (long long)(OMEGA_RUNTIME_API_VERSION_MAJOR));"
97 print " printf(\"const OMEGA_RUNTIME_API_VERSION_MINOR %lld\\n\", (long long)(OMEGA_RUNTIME_API_VERSION_MINOR));"
98 printf "%s", body
99 print " return 0;\n}"
100 exit bad
101}' "$MANIFEST" > "$PROBE" || { echo "FAIL: manifest could not be turned into a probe"; exit 1; }
102
103# ---- 2. compile (all at once; on failure name each failing manifest line) --------------------------------
104CFL="-std=gnu11 -Wall -Wextra -Werror -Werror=implicit-function-declaration -D_GNU_SOURCE -I$ROOT/src"
105if ! $CC $CFL -o "$WORK/probe" "$PROBE" 2> "$WORK/cc.err"; then
106 named=0
107 if ! $CC $CFL -DONLY=0 -fsyntax-only "$PROBE" 2> "$WORK/cc0.err"; then
108 say_fail "version macros or headers do not compile:"; grep -m3 error "$WORK/cc0.err" | sed "s/^/ /"
109 else
110 while IFS=" " read -r n text; do
111 if ! $CC $CFL -DONLY="$n" -fsyntax-only "$PROBE" 2> /dev/null; then say_fail "header disagrees with manifest: $text"; named=1; fi
112 done < "$ITEMS"
113 if [ $named = 0 ]; then say_fail "probe does not compile:"; grep -m5 'error' "$WORK/cc.err" | sed 's/^/ /'; fi
114 fi
115 echo "RESULT: FAIL (runtime API check, compile stage)"; exit 1
116fi
117"$WORK/probe" > "$GOT" || { echo "FAIL: probe crashed"; exit 1; }
118
119# ---- 3. bless or compare ---------------------------------------------------------------------------------
120if [ $BLESS = 1 ]; then
121 awk 'NR == FNR { g[$1 " " $2 (($1 == "field") ? " " $3 : "")] = $0; next }
122 { raw = $0; line = raw; sub(/#.*/, "", line); n = split(line, w, /[ \t]+/); cm = ""
123 if (match(raw, /#.*/)) cm = " " substr(raw, RSTART)
124 if (w[1] == "struct") { split(g["struct " w[2]], v, " "); print "struct " w[2] " " v[3] " " v[4] cm; next }
125 if (w[1] == "field") { split(g["field " w[2] " " w[3]], v, " "); print "field " w[2] " " w[3] " " v[4] " " v[5] " " w[6] " " w[7] cm; next }
126 if (w[1] == "const") { split(g["const " w[2]], v, " "); print "const " w[2] " " v[3] cm; next }
127 print raw }' "$GOT" "$MANIFEST"
128 exit 0
129fi
130
131# manifest facts without kind/count and without comments, in file order
132awk '{ sub(/#.*/, ""); n = split($0, w, /[ \t]+/); if (w[1] == "struct") print "struct " w[2] " " w[3] " " w[4]
133 else if (w[1] == "field") print "field " w[2] " " w[3] " " w[4] " " w[5]; else if (w[1] == "const") print "const " w[2] " " w[3] }' "$MANIFEST" > "$WANT"
134while IFS= read -r l; do
135 grep -qxF "$l" "$GOT" || { key=$(echo "$l" | awk '{ print $1 " " $2 (($1 == "field") ? " " $3 : "") }')
136 have=$(grep -F "$key " "$GOT" | head -1); say_fail "manifest says \`$l\` but the header gives \`${have:-(absent)}\`"; }
137done < "$WANT"
138
139# manifest sanity: tier tag on every struct/fn line, field size matches its kind, version consts present
140awk '{ raw = $0; line = raw; sub(/#.*/, "", line); n = split(line, w, /[ \t]+/)
141 if (w[1] == "struct" || w[1] == "fn") { if (raw !~ /#[ \t]*(STABLE|EXPERIMENTAL)([ \t]|$)/) print "FAIL: " w[1] " " w[2] " has no STABLE/EXPERIMENTAL tag" }
142 if (w[1] == "field") { ks["u8"]=1; ks["i8"]=1; ks["bool"]=1; ks["c_char"]=1; ks["u16"]=2; ks["i16"]=2; ks["u32"]=4; ks["i32"]=4; ks["f32"]=4
143 ks["enum"]=4; ks["u64"]=8; ks["i64"]=8; ks["f64"]=8; ks["ptr"]=8
144 if (w[6] in ks && w[5] != ks[w[6]] * w[7]) print "FAIL: field " w[2] "." w[3] " size " w[5] " does not match kind " w[6] " x " w[7] } }' "$MANIFEST" > "$WORK/sanity.txt"
145if [ -s "$WORK/sanity.txt" ]; then cat "$WORK/sanity.txt"; fail=1; fi
146vmaj=$(awk '$1 == "const" && $2 == "OMEGA_RUNTIME_API_VERSION_MAJOR" { print $3 }' "$MANIFEST")
147vmin=$(awk '$1 == "const" && $2 == "OMEGA_RUNTIME_API_VERSION_MINOR" { print $3 }' "$MANIFEST")
148[ -n "$vmaj" ] && [ -n "$vmin" ] || say_fail "manifest does not list OMEGA_RUNTIME_API_VERSION_MAJOR and _MINOR"
149
150# API.md: states the version, has a section for every STABLE entry and mentions every EXPERIMENTAL one
151if [ -n "$APIMD" ]; then
152 grep -q "^Omega Runtime API version: $vmaj\.$vmin\$" "$APIMD" || say_fail "$APIMD must contain the line \"Omega Runtime API version: $vmaj.$vmin\""
153 awk '{ raw = $0; line = raw; sub(/#.*/, "", line); split(line, w, /[ \t]+/)
154 if ((w[1] == "fn" || w[1] == "struct") && match(raw, /#[ \t]*(STABLE|EXPERIMENTAL)/)) { t = substr(raw, RSTART); print w[2], (t ~ /STABLE/ && t !~ /EXPERIMENTAL/) ? "STABLE" : "EXPERIMENTAL" } }' "$MANIFEST" |
155 while read -r name tier; do
156 if [ "$tier" = STABLE ]; then grep -q "^#### \`$name\`" "$APIMD" || echo "FAIL: STABLE entry $name has no \"#### \`$name\`\" section in $APIMD"
157 else grep -qF "\`$name\`" "$APIMD" || echo "FAIL: EXPERIMENTAL entry $name is not mentioned in $APIMD"; fi
158 done > "$WORK/apimd.txt"
159 if [ -s "$WORK/apimd.txt" ]; then cat "$WORK/apimd.txt"; fail=1; fi
160 # every C signature quoted in API.md (lines inside a plain ```c fence that end in `);`) must be in a header, comments removed
161 for h in $(awk '$1 == "header" { print $2 }' "$MANIFEST"); do perl -0777 -pe 's{/\*.*?\*/}{}gs' "$ROOT/$h"; done | tr '\n' ' ' | sed 's/[[:space:]][[:space:]]*/ /g' | sed 's/^/ /' > "$WORK/headers.norm"
162 awk '/^```c$/ { f = 1; next } /^```/ { f = 0 } f && /\);[ \t]*$/ { print }' "$APIMD" | sed 's/[[:space:]][[:space:]]*/ /g; s/^ //; s/ $//' |
163 while IFS= read -r sig; do grep -qF -- " $sig" "$WORK/headers.norm" || echo "FAIL: signature quoted in $APIMD but in no header: $sig"; done > "$WORK/sigs.txt"
164 if [ -s "$WORK/sigs.txt" ]; then cat "$WORK/sigs.txt"; fail=1; fi
165 # headers -> manifest: every external function a listed header declares is a manifest `fn` or is named INTERNAL in
166 # API.md section 10, and every name section 10 lists is still declared. gcc -aux-info writes each declaration with the
167 # file it came from, so a function added to a header without a manifest line or an INTERNAL entry fails here.
168 # A name is INTERNAL only as a leading token of a section-10 bullet ("- `a`, `b` (prose `c`)": a and b, not c), so a
169 # manifest function mentioned in the prose is not taken as INTERNAL; a name that is both a manifest fn and INTERNAL fails.
170 if $CC $CFL -fsyntax-only -aux-info "$WORK/aux.txt" "$PROBE" 2> "$WORK/aux.err"; then
171 awk '/^## 10\./ { s = 1; next } /^## / { s = 0 } s && /^- `/ { t = substr($0, 3)
172 while (match(t, /^`omega_[a-z0-9_]+`/)) { print substr(t, 2, RLENGTH - 2); t = substr(t, RLENGTH + 1); if (t !~ /^, `/) break; t = substr(t, 3) } }' "$APIMD" | sort -u > "$WORK/internal.txt"
173 awk -v internal="$WORK/internal.txt" 'BEGIN { while ((getline l < internal) > 0) in10[l] = 1 }
174 FNR == NR { if ($1 == "header") hdr[$2] = 1; if ($1 == "fn") fn[$2] = 1; next }
175 /^\/\* .* \*\/ extern / { f = $2; sub(/:[0-9]+:[A-Z]+$/, "", f); listed = 0
176 for (h in hdr) if (f == h || substr(f, length(f) - length(h)) == "/" h) { listed = 1; hf = h }
177 if (!listed) next
178 # the name is the identifier before the first "(" that does not open a declarator "(*" (a function returning a
179 # function pointer is written "int (*name (void)) (int)")
180 s = $0; sub(/^\/\* [^*]* \*\/ extern /, "", s); pre = ""
181 while ((i = index(s, "(")) > 0) { if (substr(s, i + 1) ~ /^ *\*/) { pre = pre substr(s, 1, i); s = substr(s, i + 1); continue }
182 pre = pre substr(s, 1, i - 1); break }
183 sub(/ +$/, "", pre); n = split(pre, a, /[^A-Za-z0-9_]+/); nm = a[n]; seen[nm] = 1
184 if (!(nm in fn) && !(nm in in10)) print "FAIL: " hf " declares " nm ", which is neither a manifest fn nor named INTERNAL in API.md section 10" }
185 END { for (l in in10) { if (!(l in seen)) print "FAIL: API.md section 10 names " l " as INTERNAL but no listed header declares it"
186 if (l in fn) print "FAIL: API.md section 10 names " l " as INTERNAL but the manifest lists it as a fn" } }' "$MANIFEST" "$WORK/aux.txt" | sort > "$WORK/decls.txt"
187 if [ -s "$WORK/decls.txt" ]; then cat "$WORK/decls.txt"; fail=1; fi
188 else
189 say_fail "the header declaration scan needs a compiler with -aux-info (gcc; set CC=gcc):"; grep -m3 . "$WORK/aux.err" | sed 's/^/ /'
190 fi
191fi
192
193nfn=$(grep -c '^fn ' "$MANIFEST"); nst=$(grep -c '^struct ' "$MANIFEST"); nfl=$(grep -c '^field ' "$MANIFEST"); nco=$(grep -c '^const ' "$MANIFEST")
194if [ $fail = 0 ]; then echo "RESULT: PASS (runtime API $vmaj.$vmin: $nfn functions, $nst structs, $nfl fields, $nco constants match the headers)"; exit 0; fi
195echo "RESULT: FAIL (runtime API check)"; exit 1