interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / native / kinds.sh

63 lines · 4188 bytes Raw Blame History
1#!/bin/sh
2# f32 and ushort array kinds of the native call ABI (docs/NATIVE-ABI.md): two Omega units with f32/ushort
3# view and edit arrays, called from C and compared to C-computed values, the refusals for those kinds,
4# and a negative control (fsum's add turned into sub) that must fail. Same pinned compiler as run.sh.
5# The generator's one argument names the output (c, python, rust); a missing argument is a refusal.
6set -eu
7PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61
8ROOT=$(cd "$(dirname "$0")/../.." && pwd)
9T=$ROOT/tests/native
10O=$ROOT/build/native-kinds
11OMC=${OMEGA_NATIVE_OMC:?set OMEGA_NATIVE_OMC to the seeded omc (omega seed/out/omc, sha256 $PIN)}
12got=$(sha256sum < "$OMC" | cut -d' ' -f1)
13[ "$got" = "$PIN" ] || { echo "FAIL omc sha256 $got, pinned $PIN"; exit 1; }
14rm -rf "$O"; mkdir -p "$O/mut"
15"$OMC" < "$ROOT/src/native.om" > "$O/native"; chmod +x "$O/native"
16frame() { # frame <dir> <fsum source>
17 "$OMC" < "$2" > "$1/fsum.bin"; "$OMC" < "$T/halve.om" > "$1/halve.bin"
18 { cat "$2"; printf '\0'; printf '%08d' "$(wc -c < "$1/fsum.bin")"; cat "$1/fsum.bin"
19 cat "$T/halve.om"; printf '\0'; printf '%08d' "$(wc -c < "$1/halve.bin")"; cat "$1/halve.bin"; }
20}
21pylib() { # pylib <dir> <fsum source>: the python word's binding and its library, side by side
22 mkdir -p "$1/py"; frame "$1" "$2" | "$O/native" python > "$1/py/omega_native.py"
23 frame "$1" "$2" | "$O/native" c > "$1/py/omega_native.c"
24 cc -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -shared -o "$1/py/libomega_native.so" "$1/py/omega_native.c"
25}
26build() { # build <dir> <fsum source>
27 frame "$1" "$2" | "$O/native" c > "$1/omega_native.c"
28 cc -std=gnu11 -O2 -Wall -Wextra -Werror -o "$1/kinds" "$T/kinds.c" "$1/omega_native.c"
29}
30build "$O" "$T/fsum.om"
31"$O/kinds"
32# Declaration texts for the same frame. Python names the element types. Rust types f32/ushort as
33# byte pointers (docs/NATIVE-ABI.md); a fragment-index overrun would not contain these markers.
34frame "$O" "$T/fsum.om" | "$O/native" python > "$O/omega_native.py"
35grep -q '_a7 = ctypes.POINTER(ctypes.c_float)' "$O/omega_native.py"
36grep -q '_a10 = ctypes.POINTER(ctypes.c_uint16)' "$O/omega_native.py"
37grep -q 'omega_fsum.argtypes = \[_res, _a8, _u64, _a10, _u64, _a7, _u64\]' "$O/omega_native.py"
38frame "$O" "$T/fsum.om" | "$O/native" rust > "$O/omega_native.rs"
39grep -q 'o_a: \*const u8, o_a_len: u64, o_h: \*const u8, o_h_len: u64, o_d: \*mut u8, o_d_len: u64' "$O/omega_native.rs"
40# The f32/ushort kinds called through the generated Python binding, with refusal controls.
41pylib "$O" "$T/fsum.om"
42if python3 -I "$T/pykinds.py" "$O/py" > "$O/py.log" 2>&1; then echo "PASS python binding: f32/ushort calls and refusals ($(grep -c '^PASS' "$O/py.log") checks)"
43else echo "FAIL python binding"; cat "$O/py.log"; exit 1; fi
44# A uint array is still outside the subset: exit 110, and the previous C file is untouched.
45cp "$O/omega_native.c" "$O/keep.c"
46printf 'fn bad a view uint 1 to ulong\nret 0\nend\n' > "$O/bad.om"
47"$OMC" < "$O/bad.om" > "$O/bad.bin"
48rc=0
49{ cat "$O/bad.om"; printf '\0'; printf '%08d' "$(wc -c < "$O/bad.bin")"; cat "$O/bad.bin"; } | "$O/native" c > "$O/bad.out" 2>"$O/bad.err" || rc=$?
50if [ "$rc" = 110 ] && [ ! -s "$O/bad.out" ] && cmp -s "$O/omega_native.c" "$O/keep.c"; then
51 echo "PASS view uint refused (exit 110), previous output untouched"
52else
53 echo "FAIL view uint refusal (exit $rc)"; cat "$O/bad.err"; exit 1
54fi
55sed 's/^put d i add get a i bitcast f32 h2b get h i$/put d i sub get a i bitcast f32 h2b get h i/' "$T/fsum.om" > "$O/mut/fsum.om"
56grep -q '^put d i sub' "$O/mut/fsum.om" || { echo "FAIL mutant not applied"; exit 1; }
57build "$O/mut" "$O/mut/fsum.om"
58if "$O/mut/kinds" > "$O/mut/log" 2>&1; then echo "FAIL mutant fsum (add -> sub) not caught"; exit 1; fi
59echo "PASS mutant fsum (add -> sub) caught ($(grep -c '^FAIL' "$O/mut/log") failing checks)"
60pylib "$O/mut" "$O/mut/fsum.om"
61if python3 -I "$T/pykinds.py" "$O/mut/py" > "$O/mut/py.log" 2>&1; then echo "FAIL mutant fsum not caught through the python binding"; exit 1; fi
62echo "PASS mutant fsum (add -> sub) caught through the python binding ($(grep -c '^FAIL' "$O/mut/py.log") failing checks)"
63echo "NATIVE-KINDS PASS"