interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tools / runtime_api_check.sh

232 lines · 17202 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# runtime_api_check.sh -- host check of the Omega Runtime API manifest against the C headers (no GPU, no physics).
4#
5# tools/runtime_api_check.sh [--bless] MANIFEST ROOT WORK_DIR [API_MD]
6#
7# MANIFEST docs/runtime/omega_runtime_api.manifest (line format: docs/runtime/API.md, "The manifest")
8# ROOT directory the manifest's `header` paths are relative to (the omega checkout; a copy in the negative controls)
9# WORK_DIR scratch directory for the generated probe
10# API_MD optional: also check that API.md states the version and has a section for every STABLE entry
11#
12# What it does: writes a C probe from the manifest and compiles it against the headers named there.
13# - every `fn` line becomes a compile-time type-equality check on the function itself, so a removed function, a changed argument
14# type or a changed return type is a compile error (-Werror=incompatible-pointer-types);
15# - every `struct`/`field`/`const` line becomes a printf of sizeof/alignof/offsetof/value, compared with the manifest;
16# - the version macros OMEGA_RUNTIME_API_VERSION_MAJOR/MINOR/PATCH must exist (#error otherwise) and equal the manifest;
17# - surface: every function, object-like macro and enumerator declared in a manifest header must be a manifest `fn`/`const`
18# or be listed, with a reason, in the INTERNAL list next to the manifest (MANIFEST with .manifest -> .internal); an
19# INTERNAL entry the headers no longer declare, or one the manifest also lists, fails too. Needs gcc (-aux-info).
20# It does not link the library and does not touch the GPU. A field added inside struct padding is not seen
21# (the struct size would not change); sovereign-core's Rust checker, which reads field names, covers that later (R12).
22# Whether a manifest change came with the right version bump is tools/runtime_api_semver.sh (against the base branch).
23# --bless prints the manifest to stdout with struct/field/const numbers taken from the headers (review the diff).
24# Exit 0 = PASS, 1 = FAIL (each mismatch printed as "FAIL: ..."), 2 = usage.
25set -u
26BLESS=0
27if [ "${1:-}" = "--bless" ]; then BLESS=1; shift; fi
28[ $# -ge 3 ] || { echo "usage: $0 [--bless] MANIFEST ROOT WORK_DIR [API_MD]" >&2; exit 2; }
29MANIFEST=$1; ROOT=$(cd "$2" && pwd) || exit 2; WORK=$3; APIMD=${4:-}
30CC=${CC:-cc}
31mkdir -p "$WORK" || exit 2
32PROBE=$WORK/probe.c; ITEMS=$WORK/items.txt; GOT=$WORK/got.txt; WANT=$WORK/want.txt
33fail=0
34say_fail() { echo "FAIL: $*"; fail=1; }
35
36# ---- 1. manifest -> probe source -------------------------------------------------------------------------
37awk -v root="$ROOT" -v items="$ITEMS" '
38function trim(s) { gsub(/^[ \t]+|[ \t]+$/, "", s); return s }
39function base(t) {
40 if (t == "u8") return "uint8_t"; if (t == "u16") return "uint16_t"; if (t == "u32") return "uint32_t"
41 if (t == "u64") return "uint64_t"; if (t == "i8") return "int8_t"; if (t == "i16") return "int16_t"
42 if (t == "i32") return "int32_t"; if (t == "i64") return "int64_t"; if (t == "f32") return "float"
43 if (t == "f64") return "double"; if (t == "usize") return "size_t"; if (t == "c_char") return "char"
44 if (t == "c_void" || t == "()" || t == "") return "void"
45 return t
46}
47function ctype(t, r) {
48 t = trim(t)
49 if (t ~ /^\*const /) { r = ctype(substr(t, 8)); return (r ~ /\*$/) ? r " const *" : "const " r " *" }
50 if (t ~ /^\*mut /) { return ctype(substr(t, 6)) " *" }
51 if (t ~ /^fn\(/) { nfp++; fptd = fptd "typedef " fnsig(t, "(*FP" nfp ")") ";\n"; return "FP" nfp }
52 return base(t)
53}
54# "fn(a, b) -> r" as a C declarator named nm
55function fnsig(s, nm, depth, i, c, cl, params, ret, n, parts, k, out) {
56 s = trim(s); sub(/^fn/, "", s); s = trim(s)
57 depth = 0; cl = 0
58 for (i = 1; i <= length(s); i++) { c = substr(s, i, 1)
59 if (c == "(") depth++
60 if (c == ")") { depth--; if (depth == 0) { cl = i; break } } }
61 params = substr(s, 2, cl - 2); ret = trim(substr(s, cl + 1)); sub(/^->/, "", ret)
62 n = splitparams(params, parts); out = ""
63 for (k = 1; k <= n; k++) out = out (k > 1 ? ", " : "") ctype(parts[k])
64 if (n == 0) out = "void"
65 return ctype(ret) " " nm "(" out ")"
66}
67function splitparams(s, parts, i, c, depth, cur, n) {
68 n = 0; cur = ""; depth = 0
69 for (i = 1; i <= length(s); i++) { c = substr(s, i, 1)
70 if (c == "(") depth++
71 if (c == ")") depth--
72 if (c == "," && depth == 0) { parts[++n] = trim(cur); cur = ""; continue }
73 cur = cur c }
74 if (trim(cur) != "") parts[++n] = trim(cur)
75 return n
76}
77function item(line) { k++; print k "\t" line > items; return sprintf("#if !defined(ONLY) || ONLY == %d\n", k) }
78BEGIN { k = 0; nfp = 0; fptd = "" }
79{ line = $0; sub(/#.*/, "", line); line = trim(line); if (line == "") next
80 nw = split(line, w, /[ \t]+/)
81 if (w[1] == "header") { inc = inc "#include \"" root "/" w[2] "\"\n"; next }
82 if (w[1] == "abi") next
83 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 }
84 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 }
85 if (w[1] == "const") { body = body item(line) " printf(\"const " w[2] " %lld\\n\", (long long)(" w[2] "));\n#endif\n"; next }
86 if (w[1] == "fn") { rest = line; sub(/^fn[ \t]+[^ \t]+[ \t]+/, "", rest)
87 decl = decl item(line) "_Static_assert(__builtin_types_compatible_p(__typeof__(" w[2] "), " fnsig(rest, "") "), \"signature of " w[2] "\");\n#endif\n"; next }
88 print "manifest: unknown or malformed line: " $0 > "/dev/stderr"; bad = 1 }
89END {
90 print "#include <stdio.h>\n#include <stddef.h>\n#include <stdint.h>\n#include <stdbool.h>"
91 print "#include \"" root "/src/omega_runtime_api.h\""
92 print "#ifndef OMEGA_RUNTIME_API_VERSION_MAJOR\n#error \"version macro missing: OMEGA_RUNTIME_API_VERSION_MAJOR\"\n#endif"
93 print "#ifndef OMEGA_RUNTIME_API_VERSION_MINOR\n#error \"version macro missing: OMEGA_RUNTIME_API_VERSION_MINOR\"\n#endif"
94 print "#ifndef OMEGA_RUNTIME_API_VERSION_PATCH\n#error \"version macro missing: OMEGA_RUNTIME_API_VERSION_PATCH\"\n#endif"
95 printf "%s", inc
96 printf "%s", fptd
97 printf "%s", decl
98 print "int main(void) {\n printf(\"const OMEGA_RUNTIME_API_VERSION_MAJOR %lld\\n\", (long long)(OMEGA_RUNTIME_API_VERSION_MAJOR));"
99 print " printf(\"const OMEGA_RUNTIME_API_VERSION_MINOR %lld\\n\", (long long)(OMEGA_RUNTIME_API_VERSION_MINOR));"
100 print " printf(\"const OMEGA_RUNTIME_API_VERSION_PATCH %lld\\n\", (long long)(OMEGA_RUNTIME_API_VERSION_PATCH));"
101 printf "%s", body
102 print " return 0;\n}"
103 exit bad
104}' "$MANIFEST" > "$PROBE" || { echo "FAIL: manifest could not be turned into a probe"; exit 1; }
105
106# ---- 2. compile (all at once; on failure name each failing manifest line) --------------------------------
107CFL="-std=gnu11 -Wall -Wextra -Werror -Werror=implicit-function-declaration -D_GNU_SOURCE -I$ROOT/src"
108if ! $CC $CFL -o "$WORK/probe" "$PROBE" 2> "$WORK/cc.err"; then
109 named=0
110 if ! $CC $CFL -DONLY=0 -fsyntax-only "$PROBE" 2> "$WORK/cc0.err"; then
111 say_fail "version macros or headers do not compile:"; grep -m3 error "$WORK/cc0.err" | sed "s/^/ /"
112 else
113 while IFS=" " read -r n text; do
114 if ! $CC $CFL -DONLY="$n" -fsyntax-only "$PROBE" 2> /dev/null; then say_fail "header disagrees with manifest: $text"; named=1; fi
115 done < "$ITEMS"
116 if [ $named = 0 ]; then say_fail "probe does not compile:"; grep -m5 'error' "$WORK/cc.err" | sed 's/^/ /'; fi
117 fi
118 echo "RESULT: FAIL (runtime API check, compile stage)"; exit 1
119fi
120"$WORK/probe" > "$GOT" || { echo "FAIL: probe crashed"; exit 1; }
121
122# ---- 3. bless or compare ---------------------------------------------------------------------------------
123if [ $BLESS = 1 ]; then
124 awk 'NR == FNR { g[$1 " " $2 (($1 == "field") ? " " $3 : "")] = $0; next }
125 { raw = $0; line = raw; sub(/#.*/, "", line); n = split(line, w, /[ \t]+/); cm = ""
126 if (match(raw, /#.*/)) cm = " " substr(raw, RSTART)
127 if (w[1] == "struct") { split(g["struct " w[2]], v, " "); print "struct " w[2] " " v[3] " " v[4] cm; next }
128 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 }
129 if (w[1] == "const") { split(g["const " w[2]], v, " "); print "const " w[2] " " v[3] cm; next }
130 print raw }' "$GOT" "$MANIFEST"
131 exit 0
132fi
133
134# manifest facts without kind/count and without comments, in file order
135awk '{ sub(/#.*/, ""); n = split($0, w, /[ \t]+/); if (w[1] == "struct") print "struct " w[2] " " w[3] " " w[4]
136 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"
137while IFS= read -r l; do
138 grep -qxF "$l" "$GOT" || { key=$(echo "$l" | awk '{ print $1 " " $2 (($1 == "field") ? " " $3 : "") }')
139 have=$(grep -F "$key " "$GOT" | head -1); say_fail "manifest says \`$l\` but the header gives \`${have:-(absent)}\`"; }
140done < "$WANT"
141
142# manifest sanity: tier tag on every struct/fn line, field size matches its kind, version consts present
143awk '{ raw = $0; line = raw; sub(/#.*/, "", line); n = split(line, w, /[ \t]+/)
144 if (w[1] == "struct" || w[1] == "fn") { if (raw !~ /#[ \t]*(STABLE|EXPERIMENTAL)([ \t]|$)/) print "FAIL: " w[1] " " w[2] " has no STABLE/EXPERIMENTAL tag" }
145 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
146 ks["enum"]=4; ks["u64"]=8; ks["i64"]=8; ks["f64"]=8; ks["ptr"]=8
147 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"
148if [ -s "$WORK/sanity.txt" ]; then cat "$WORK/sanity.txt"; fail=1; fi
149vmaj=$(awk '$1 == "const" && $2 == "OMEGA_RUNTIME_API_VERSION_MAJOR" { print $3 }' "$MANIFEST")
150vmin=$(awk '$1 == "const" && $2 == "OMEGA_RUNTIME_API_VERSION_MINOR" { print $3 }' "$MANIFEST")
151vpat=$(awk '$1 == "const" && $2 == "OMEGA_RUNTIME_API_VERSION_PATCH" { print $3 }' "$MANIFEST")
152[ -n "$vmaj" ] && [ -n "$vmin" ] && [ -n "$vpat" ] || say_fail "manifest does not list OMEGA_RUNTIME_API_VERSION_MAJOR, _MINOR and _PATCH"
153abiv=$(awk '$1 == "abi" { print $3 }' "$MANIFEST")
154[ "$abiv" = "$vmaj.$vmin.$vpat" ] || say_fail "manifest abi line says ${abiv:-(none)} but the version consts say $vmaj.$vmin.$vpat"
155
156# API.md: states the version, has a section for every STABLE entry and mentions every EXPERIMENTAL one
157if [ -n "$APIMD" ]; then
158 grep -q "^Omega Runtime API version: $vmaj\.$vmin\.$vpat\$" "$APIMD" || say_fail "$APIMD must contain the line \"Omega Runtime API version: $vmaj.$vmin.$vpat\""
159 awk '{ raw = $0; line = raw; sub(/#.*/, "", line); split(line, w, /[ \t]+/)
160 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" |
161 while read -r name tier; do
162 if [ "$tier" = STABLE ]; then grep -q "^#### \`$name\`" "$APIMD" || echo "FAIL: STABLE entry $name has no \"#### \`$name\`\" section in $APIMD"
163 else grep -qF "\`$name\`" "$APIMD" || echo "FAIL: EXPERIMENTAL entry $name is not mentioned in $APIMD"; fi
164 done > "$WORK/apimd.txt"
165 if [ -s "$WORK/apimd.txt" ]; then cat "$WORK/apimd.txt"; fail=1; fi
166 # every C signature quoted in API.md (lines inside a plain ```c fence that end in `);`) must be in a header, comments removed
167 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"
168 awk '/^```c$/ { f = 1; next } /^```/ { f = 0 } f && /\);[ \t]*$/ { print }' "$APIMD" | sed 's/[[:space:]][[:space:]]*/ /g; s/^ //; s/ $//' |
169 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"
170 if [ -s "$WORK/sigs.txt" ]; then cat "$WORK/sigs.txt"; fail=1; fi
171 # and back: every "#### `name`" section names a manifest fn or struct, so a section left for a removed entry fails
172 awk '{ sub(/#.*/, ""); if ($1 == "fn" || $1 == "struct") print $2 }' "$MANIFEST" | LC_ALL=C sort -u > "$WORK/apimd-entries.txt"
173 sed -n 's/^#### `\([A-Za-z_][A-Za-z0-9_]*\)`.*/\1/p' "$APIMD" | LC_ALL=C sort -u | LC_ALL=C comm -23 - "$WORK/apimd-entries.txt" |
174 sed 's/^/FAIL: stale API.md section (no manifest fn or struct): /' > "$WORK/apimd-stale.txt"
175 if [ -s "$WORK/apimd-stale.txt" ]; then cat "$WORK/apimd-stale.txt"; fail=1; fi
176fi
177
178# ---- 5. surface: nothing in the manifest headers is public by accident ---------------------------------------
179INTERNAL=${MANIFEST%.manifest}.internal
180SURF=$WORK/surface
181mkdir -p "$SURF"
182if [ ! -f "$INTERNAL" ]; then say_fail "INTERNAL list $INTERNAL is missing"
183else
184 : > "$SURF/tu.c"
185 for h in $(awk '$1 == "header" { print $2 }' "$MANIFEST"); do echo "$ROOT/$h"; done > "$SURF/headers.txt"
186 { echo "#include <stddef.h>"; echo "#include <stdint.h>"; echo "#include <stdbool.h>"
187 while IFS= read -r h; do echo "#include \"$h\""; done < "$SURF/headers.txt"; } > "$SURF/tu.c"
188 if ! $CC -std=gnu11 -D_GNU_SOURCE -I"$ROOT/src" -aux-info "$SURF/aux.txt" -c -o "$SURF/tu.o" "$SURF/tu.c" 2> "$SURF/aux.err" \
189 || [ ! -s "$SURF/aux.txt" ]; then
190 say_fail "surface: $CC could not list the header declarations (-aux-info needs gcc):"; head -3 "$SURF/aux.err" | sed 's/^/ /'
191 elif ! $CC -std=gnu11 -D_GNU_SOURCE -I"$ROOT/src" -E -dD "$SURF/tu.c" > "$SURF/pp.txt" 2> "$SURF/pp.err"; then
192 say_fail "surface: preprocessing the headers failed"
193 else
194 # functions: aux-info lines from a manifest header; the name is the first identifier followed by " (" where that
195 # "(" does not open a declarator "(*" (a function returning a function pointer reads "int (*name (void)) (int)")
196 perl -ne 'BEGIN { open(H, "<", shift) or die; while (<H>) { chomp; $h{$_} = 1 } }
197 next unless m{^/\* (\S+):\d+:\S+ \*/ (.*)$}; next unless $h{$1}; print "fn $1\n" if $2 =~ /(\w+) \((?!\s*\*)/' \
198 "$SURF/headers.txt" "$SURF/aux.txt" > "$SURF/fns.raw"
199 # object-like macros with a value, and enumerators, from preprocessed text that came from a manifest header
200 perl -e 'open(H, "<", $ARGV[0]) or die; while (<H>) { chomp; $h{$_} = 1 } close H;
201 open(P, "<", $ARGV[1]) or die; my ($cur, $text) = ("", "");
202 while (<P>) {
203 if (/^# \d+ "([^"]+)"/) { $cur = $1; next }
204 next unless $h{$cur};
205 if (/^#define (\w+)(\(?)(.*)$/) { my ($n, $p, $v) = ($1, $2, $3); print "const $n\n" if $p eq "" && $v =~ /\S/; next }
206 next if /^#/;
207 $text .= $_;
208 }
209 while ($text =~ /\benum\b\s*\w*\s*\{([^}]*)\}/g) { for my $e (split /,/, $1) { print "const $1\n" if $e =~ /^\s*([A-Za-z_]\w*)/ } }' \
210 "$SURF/headers.txt" "$SURF/pp.txt" > "$SURF/consts.raw"
211 LC_ALL=C sort -u "$SURF/consts.raw" > "$SURF/consts.txt"
212 LC_ALL=C sort -u "$SURF/fns.raw" > "$SURF/fns.txt"
213 cat "$SURF/fns.txt" "$SURF/consts.txt" | LC_ALL=C sort -u > "$SURF/surface.txt"
214 awk '{ sub(/#.*/, ""); if ($1 == "fn" || $1 == "const") print $1 " " $2 }' "$MANIFEST" | LC_ALL=C sort -u > "$SURF/manifest.txt"
215 awk '{ raw = $0; line = raw; sub(/#.*/, "", line); n = split(line, w); if (n == 0) next
216 if ((w[1] != "fn" && w[1] != "const") || n != 2) { print "FAIL: INTERNAL list line is not `fn NAME # reason` or `const NAME # reason`: " raw > "/dev/stderr"; next }
217 if (raw !~ /#[ \t]*[^ \t]/) { print "FAIL: INTERNAL entry " w[1] " " w[2] " gives no reason" > "/dev/stderr"; next }
218 print w[1] " " w[2] }' "$INTERNAL" 2> "$SURF/internal.err" | LC_ALL=C sort > "$SURF/internal.txt"
219 if [ -s "$SURF/internal.err" ]; then cat "$SURF/internal.err"; fail=1; fi
220 LC_ALL=C uniq -d "$SURF/internal.txt" | sed 's/^/FAIL: INTERNAL entry listed twice: /' > "$SURF/out.txt"
221 LC_ALL=C sort -u "$SURF/manifest.txt" "$SURF/internal.txt" > "$SURF/declared.txt"
222 LC_ALL=C comm -23 "$SURF/surface.txt" "$SURF/declared.txt" | sed 's/^/FAIL: undeclared public entry (in a manifest header, not in the manifest or the INTERNAL list): /' >> "$SURF/out.txt"
223 LC_ALL=C sort -u "$SURF/internal.txt" | LC_ALL=C comm -23 - "$SURF/surface.txt" | sed 's/^/FAIL: stale INTERNAL entry (no manifest header declares it): /' >> "$SURF/out.txt"
224 LC_ALL=C sort -u "$SURF/internal.txt" | LC_ALL=C comm -12 - "$SURF/manifest.txt" | sed 's/^/FAIL: entry is both in the manifest and INTERNAL: /' >> "$SURF/out.txt"
225 if [ -s "$SURF/out.txt" ]; then cat "$SURF/out.txt"; fail=1; fi
226 nsurf=$(wc -l < "$SURF/surface.txt"); nint=$(wc -l < "$SURF/internal.txt")
227 fi
228fi
229
230nfn=$(grep -c '^fn ' "$MANIFEST"); nst=$(grep -c '^struct ' "$MANIFEST"); nfl=$(grep -c '^field ' "$MANIFEST"); nco=$(grep -c '^const ' "$MANIFEST")
231if [ $fail = 0 ]; then echo "RESULT: PASS (runtime API $vmaj.$vmin.$vpat: $nfn functions, $nst structs, $nfl fields, $nco constants match the headers; surface $nsurf names, $nint INTERNAL)"; exit 0; fi
232echo "RESULT: FAIL (runtime API check)"; exit 1