omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / macos / run.sh
| 1 | #!/bin/sh |
| 2 | # Native call ABI v1 on macOS arm64 (docs/NATIVE-ABI.md, "Platforms"). Two steps, because the seeded |
| 3 | # compiler is a Linux AArch64 executable and cannot run on macOS: |
| 4 | # 1. On Linux AArch64: OMEGA_NATIVE_OMC=<seeded omc> sh tests/native/macos/run.sh bundle DIR |
| 5 | # runs tools/native on the v1 suite's units and writes everything the Mac needs into DIR: the |
| 6 | # units' code (.bin), omega_native.c/.h/.py, the add -> sub mutant, the host-word control, |
| 7 | # the callers and this script. |
| 8 | # 2. On macOS arm64: sh DIR/run.sh run DIR |
| 9 | # builds the generated C with clang into libomega_native.dylib and runs the C and Python callers. |
| 10 | # Step 2 needs only clang, the clang tools (nm, otool, llvm objdump) and python3's standard library. |
| 11 | # The last line is NATIVE-CALL-MACOS PASS, FAIL or NOT_RUN; only PASS exits 0. |
| 12 | set -eu |
| 13 | U="madd smadd fill spin mix big hsum" |
| 14 | PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 |
| 15 | mode=${1-}; D=${2-} |
| 16 | [ -n "$D" ] || { echo 'usage: run.sh bundle|run DIR' >&2; exit 64; } |
| 17 | fails=0 |
| 18 | pass() { echo "PASS $1"; } |
| 19 | fail() { echo "FAIL $1"; fails=$((fails + 1)); } |
| 20 | case $mode in |
| 21 | bundle) |
| 22 | ROOT=$(cd "$(dirname "$0")/../../.." && pwd) |
| 23 | T=$ROOT/tests/native |
| 24 | OMC=${OMEGA_NATIVE_OMC:?set OMEGA_NATIVE_OMC to the seeded omc (sha256 $PIN)} |
| 25 | rm -rf "$D"; mkdir -p "$D/mut" "$D/hostw" |
| 26 | # tools/native checks the compiler pin, compiles the units and generates the bindings (it also builds |
| 27 | # the Linux library, which the bundle does not carry). |
| 28 | G=$D/gen; OMEGA_NATIVE_OMC=$OMC "$ROOT/tools/native" "$G" $(for u in $U; do echo "$T/$u.om"; done) > /dev/null |
| 29 | cp "$G/omega_native.c" "$G/omega_native.h" "$G/omega_native.py" "$D/" |
| 30 | for u in $U; do cp "$G/work/$u.bin" "$D/"; done |
| 31 | # Negative control: madd's add turned into sub (the same mutant as run.sh), framed for the same generator. |
| 32 | frame() { cat "$T/$1.om"; printf '\0'; printf '%08d' "$(wc -c < "$2/$1.bin")"; cat "$2/$1.bin"; } |
| 33 | for u in $U; do [ $u = madd ] || cp "$D/$u.bin" "$D/mut/"; done |
| 34 | printf '\003\174\001\233\140\000\002\313\300\003\137\326' > "$D/mut/madd.bin" |
| 35 | for u in $U; do frame $u "$D/mut"; done | "$G/work/native-gen" c > "$D/mut/omega_native.c" |
| 36 | # Host-word control: a unit whose code makes a Linux system call; its binding must not build on macOS. |
| 37 | OMEGA_NATIVE_OMC=$OMC "$ROOT/tools/native" "$G/hostw" "$T/macos/hostw.om" > /dev/null |
| 38 | cp "$G/hostw/omega_native.c" "$G/hostw/omega_native.h" "$D/hostw/" |
| 39 | cp "$T/caller.c" "$T/caller.py" "$T/macos/run.sh" "$D/" |
| 40 | rm -rf "$G" |
| 41 | (cd "$D" && sha256sum *.bin omega_native.c omega_native.h omega_native.py mut/omega_native.c hostw/omega_native.c hostw/omega_native.h) > "$D/SHA256SUMS" |
| 42 | echo "BUNDLE $D (omc $PIN, generated on $(uname -sm))" |
| 43 | exit 0 ;; |
| 44 | run) ;; |
| 45 | *) echo 'usage: run.sh bundle|run DIR' >&2; exit 64 ;; |
| 46 | esac |
| 47 | D=$(cd "$D" && pwd) |
| 48 | if [ "$(uname -s)" != Darwin ] || [ "$(uname -m)" != arm64 ]; then |
| 49 | echo "NATIVE-CALL-MACOS NOT_RUN (host is $(uname -sm), not Darwin arm64)"; exit 1 |
| 50 | fi |
| 51 | for t in clang nm otool objdump python3; do |
| 52 | command -v $t > /dev/null || { echo "NATIVE-CALL-MACOS NOT_RUN ($t not installed)"; exit 1; } |
| 53 | done |
| 54 | echo "host $(uname -sm) $(sw_vers -productVersion 2> /dev/null || true); $(clang --version | head -n 1)" |
| 55 | export LC_ALL=C |
| 56 | (cd "$D" && shasum -a 256 -c SHA256SUMS > /dev/null) || { echo 'NATIVE-CALL-MACOS FAIL (bundle does not match SHA256SUMS)'; exit 1; } |
| 57 | B=$D/macos; rm -rf "$B"; mkdir -p "$B/mut" |
| 58 | lib() { clang -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -shared -install_name @rpath/libomega_native.dylib \ |
| 59 | -include "$D/omega_native.h" -o "$2/libomega_native.dylib" "$1"; } |
| 60 | caller() { clang -std=gnu11 -O2 -Wall -Wextra -Werror -pthread -I "$D" "$D/caller.c" -L"$1" -lomega_native \ |
| 61 | -Wl,-rpath,"$1" -o "$1/caller_c"; } |
| 62 | lib "$D/omega_native.c" "$B" |
| 63 | pass "clang built libomega_native.dylib ($(otool -hv "$B/libomega_native.dylib" | awk 'END{print $2, $5}'))" |
| 64 | # The embedded code must be the compiler's bytes, unchanged. embeq LIB UNIT BIN compares UNIT's bytes in |
| 65 | # LIB with BIN, mapping the symbol address to a file offset through the __TEXT,__text section. |
| 66 | embeq() { |
| 67 | sect=$(otool -l "$1" | awk '/sectname __text$/{t=1} t&&$1=="addr"{a=$2} t&&$1=="offset"{print a, $2; exit}') |
| 68 | s=$(nm "$1" | awk -v n="_ombr_code_$2" '$3==n{print $1}') |
| 69 | [ -n "$s" ] && [ -n "$sect" ] || return 1 |
| 70 | off=$((0x$s - ${sect% *} + ${sect#* })) |
| 71 | dd if="$1" bs=1 skip="$off" count="$(wc -c < "$3")" 2> /dev/null > "$B/emb.bin" |
| 72 | cmp -s "$3" "$B/emb.bin" |
| 73 | } |
| 74 | ok=1 |
| 75 | for u in $U; do embeq "$B/libomega_native.dylib" $u "$D/$u.bin" || { ok=0; echo " $u differs"; }; done |
| 76 | if [ $ok = 1 ]; then pass "embedded code == omc output, per unit ($(cat $(for u in $U; do echo "$D/$u.bin"; done) | shasum -a 256 | cut -c1-16))" |
| 77 | else fail "embedded code differs from omc output"; fi |
| 78 | # macOS reserves x18 (the platform register); the embedded code must neither read nor write it. |
| 79 | # x18dis FILE START LEN prints the instructions of [START, START+LEN) that name x18 or w18. |
| 80 | x18dis() { objdump -d --no-show-raw-insn --start-address="$2" --stop-address=$(($2 + $3)) "$1" | grep -E '^ *[0-9a-f]+:' > "$B/dis.txt" || true |
| 81 | grep -wE '[xw]18' "$B/dis.txt" || true; } |
| 82 | ok=1; n=0 |
| 83 | for u in $U; do |
| 84 | s=$(nm "$B/libomega_native.dylib" | awk -v n="_ombr_code_$u" '$3==n{print $1}') |
| 85 | hit=$(x18dis "$B/libomega_native.dylib" $((0x$s)) "$(wc -c < "$D/$u.bin")") |
| 86 | n=$((n + $(wc -l < "$B/dis.txt"))) |
| 87 | [ -z "$hit" ] || { ok=0; echo " $u: $hit"; } |
| 88 | done |
| 89 | # Control for the check itself: an instruction that writes x18 must be found. |
| 90 | printf '.text\n.globl _x\n_x:\nmov x18, x0\nret\n' > "$B/x18.s" |
| 91 | clang -c -o "$B/x18.o" "$B/x18.s" |
| 92 | ctl=$(x18dis "$B/x18.o" 0 8) |
| 93 | if [ $ok = 1 ] && [ -n "$ctl" ]; then pass "no x18/w18 in the embedded code ($n instructions; control found: $(echo $ctl | sed 's/^[0-9a-f]*: *//'))" |
| 94 | else fail "x18 check (units clean: $ok; control found: ${ctl:-nothing})"; fi |
| 95 | caller "$B" |
| 96 | if "$B/caller_c" > "$B/c.log" 2>&1; then pass "c caller ($(grep -c '^PASS' "$B/c.log") lines)"; else fail "c caller"; cat "$B/c.log"; fi |
| 97 | cp "$D/caller.py" "$D/omega_native.py" "$B/" |
| 98 | if python3 -I "$B/caller.py" > "$B/python.log" 2>&1; then pass "python caller ($(grep -c '^PASS' "$B/python.log") lines)" |
| 99 | else fail "python caller"; cat "$B/python.log"; fi |
| 100 | # Negative control: madd's add turned into sub must be caught. |
| 101 | lib "$D/mut/omega_native.c" "$B/mut"; caller "$B/mut" |
| 102 | if embeq "$B/mut/libomega_native.dylib" madd "$D/madd.bin"; then fail "byte check missed the mutant madd" |
| 103 | elif embeq "$B/mut/libomega_native.dylib" madd "$D/mut/madd.bin"; then pass "byte check: mutant library holds the mutant madd, not omc's" |
| 104 | else fail "mutant madd not embedded as generated"; fi |
| 105 | if "$B/mut/caller_c" > "$B/mut/c.log" 2>&1; then fail "mutant madd (add -> sub) not caught" |
| 106 | else pass "mutant madd (add -> sub) caught ($(grep -c '^FAIL' "$B/mut/c.log") failing checks)"; fi |
| 107 | # Host-word control: Linux system calls in a unit's code must stop the macOS build with the generator's reason. |
| 108 | if clang -std=gnu11 -fPIC -shared -include "$D/hostw/omega_native.h" -o "$B/hostw.dylib" "$D/hostw/omega_native.c" 2> "$B/hostw.log"; then |
| 109 | fail "host-word unit built on macOS" |
| 110 | elif grep -q 'unit hostw makes Linux system calls' "$B/hostw.log"; then pass "host-word unit refused on macOS (#error)" |
| 111 | else fail "host-word unit failed for another reason"; cat "$B/hostw.log"; fi |
| 112 | if [ "$fails" -ne 0 ]; then echo "NATIVE-CALL-MACOS FAIL $fails"; exit 1; fi |
| 113 | echo "NATIVE-CALL-MACOS PASS" |