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 / g1 / run.sh

164 lines · 10463 bytes Raw Blame History
1#!/bin/sh
2# G1 sample (omega-mv #624, translation-v1 section 5 form): calc(i0, i1) = (i0 + i1) * (i0 - i1) modulo
3# 2^64 as a function-mode .om unit with a "new ulong" SSA array, compiled by the pinned omc, bound by
4# tools/native, and called from C, C++, Python, Rust, Zig and Go. Every caller's output on the boundary
5# vectors must equal the Python bigint reference line for line. Writes build/g1/manifest.json.
6# OMEGA_G1_UNIT names the unit (default tests/native/g1/calc.om, hand-written; calc.lowered.om is the one
7# the omega-mv pipeline generated). The bound function's name and arity come from the unit's last fn
8# line; the callers and the reference implement calc with two inputs, so any other is a FAIL. A first
9# line "note omega.universal.lower.v1 semantic <64 hex>" is recorded as the semantic identity, apart from
10# the source identity (sha256 of the unit's bytes).
11# A missing toolchain is NOT_RUN, never PASS. Exit 0 only when every caller that ran passed.
12set -eu
13PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61
14ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
15T=$ROOT/tests/native/g1
16O=$ROOT/build/g1
17OMC=${OMEGA_NATIVE_OMC:?set OMEGA_NATIVE_OMC to the seeded omc (omega seed/out/omc, sha256 $PIN)}
18got=$(sha256sum < "$OMC" | cut -d' ' -f1)
19[ "$got" = "$PIN" ] || { echo "FAIL omc sha256 $got, pinned $PIN"; exit 1; }
20command -v python3 > /dev/null || { echo "FAIL python3 (the reference) is not installed"; exit 1; }
21rm -rf "$O"; mkdir -p "$O/mut" "$O/unit"
22fails=0
23pass() { echo "PASS $1"; }
24fail() { echo "FAIL $1"; fails=$((fails + 1)); }
25UNIT=${OMEGA_G1_UNIT:-$T/calc.om}
26[ -f "$UNIT" ] || { echo "FAIL unit $UNIT not found"; exit 1; }
27FNL=$(grep '^fn ' "$UNIT" | tail -n 1)
28NAME=$(printf '%s\n' "$FNL" | awk '{ print $2 }')
29ARITY=$(printf '%s\n' "$FNL" | awk '{ n = 0; for (i = 3; i < NF; i += 2) { if ($i == "to") break; n++ } print n }')
30SEM=$(head -n 1 "$UNIT" | awk '$1 == "note" && $2 == "omega.universal.lower.v1" && $3 == "semantic" { print $4 }')
31if head -n 1 "$UNIT" | grep -q '^note omega.universal.lower.v1 '; then
32 if printf '%s\n' "$SEM" | grep -qx '[0-9a-f]\{64\}'; then pass "unit carries semantic identity $SEM"
33 else fail "unit's lower.v1 note line has no 64-hex semantic identity"; SEM=; fi
34fi
35if [ "$NAME" = calc ] && [ "$ARITY" = 2 ]; then pass "bound function $NAME, $ARITY inputs ($FNL)"
36else echo "FAIL bound function '$FNL': the callers and the reference implement calc with 2 inputs"; exit 1; fi
37# tools/native names a unit's code after its file, so the unit is bound from a copy named after its function.
38cp "$UNIT" "$O/unit/$NAME.om"
39OMEGA_NATIVE_OMC="$OMC" "$ROOT/tools/native" "$O" "$O/unit/$NAME.om" > "$O/tools-native.log" 2>&1 \
40 || { echo "FAIL tools/native"; cat "$O/tools-native.log"; exit 1; }
41"$OMC" < "$UNIT" > "$O/calc.bin"
42if cmp -s "$O/calc.bin" "$O/work/calc.bin"; then pass "calc compiled ($(wc -c < "$O/calc.bin") bytes of code)"
43else fail "tools/native compiled calc differently from omc"; fi
44# The embedded code must be omc's bytes: read at ombr_code_calc, mapped to its file offset.
45s=$(objdump -t "$O/libomega_native.so" | awk '$NF=="ombr_code_calc"{print $1}')
46off=$(objdump -h "$O/libomega_native.so" | awk '$2 ~ /^\./ {print $3, $4, $6}' | while read -r sz vma fo; do
47 if [ $((0x$vma)) -le $((0x$s)) ] && [ $((0x$s)) -lt $((0x$vma + 0x$sz)) ]; then echo $((0x$s - 0x$vma + 0x$fo)); break; fi
48done)
49dd if="$O/libomega_native.so" bs=1 skip="${off:-0}" count="$(wc -c < "$O/calc.bin")" 2> /dev/null > "$O/calc.emb"
50if [ -n "$off" ] && cmp -s "$O/calc.bin" "$O/calc.emb"; then pass "embedded code == omc output"
51else fail "embedded code differs from omc output"; fi
52python3 -I "$T/reference.py" vectors > "$O/vectors.txt"
53python3 -I "$T/reference.py" expect < "$O/vectors.txt" > "$O/expect.txt"
54N=$(wc -l < "$O/vectors.txt")
55cp "$T"/caller.* "$O/"
56: > "$O/status.txt"
57# rec LANG STATUS MATCHED TOOLCHAIN: one line per language for the manifest.
58rec() { printf '%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$4" >> "$O/status.txt"; }
59cmpout() {
60 lang=$1; tc=$2
61 m=$(awk 'NR==FNR { e[FNR] = $0; next } e[FNR] == $0 { n++ } END { print n + 0 }' "$O/expect.txt" "$O/$lang.out")
62 if cmp -s "$O/expect.txt" "$O/$lang.out"; then pass "$lang caller ($m/$N vectors)"; rec "$lang" PASS "$m" "$tc"
63 else fail "$lang caller ($m/$N vectors)"; rec "$lang" FAIL "$m" "$tc"; fi
64}
65try() {
66 lang=$1; tc=$2; shift 2
67 if "$@" > "$O/$lang-build.log" 2>&1; then return 0; fi
68 fail "$lang build"; cat "$O/$lang-build.log"; rec "$lang" FAIL 0 "$tc"; return 1
69}
70notrun() { echo "NOT_RUN $1 caller: $2 not installed"; rec "$1" NOT_RUN 0 "-"; }
71# ver TOOL ARGS: the tool's resolved path, then the first line of its version output.
72ver() { printf '%s: ' "$(command -v "$1")"; "$@" 2>&1 | head -n 1; }
73if command -v cc > /dev/null; then
74 tc=$(ver cc --version)
75 try c "$tc" cc -std=c17 -O2 -Wall -Wextra -Werror "$O/caller.c" -L"$O" -lomega_native -Wl,-rpath,"$O" -o "$O/caller_c" \
76 && { "$O/caller_c" < "$O/vectors.txt" > "$O/c.out" 2> "$O/c.err" || true; cmpout c "$tc"; }
77else notrun c cc; fi
78if command -v c++ > /dev/null; then
79 tc=$(ver c++ --version)
80 try cpp "$tc" c++ -std=c++17 -O2 -Wall -Wextra -Werror "$O/caller.cpp" -L"$O" -lomega_native -Wl,-rpath,"$O" -o "$O/caller_cpp" \
81 && { "$O/caller_cpp" < "$O/vectors.txt" > "$O/cpp.out" 2> "$O/cpp.err" || true; cmpout cpp "$tc"; }
82else notrun cpp c++; fi
83tc=$(ver python3 --version)
84python3 -I "$O/caller.py" < "$O/vectors.txt" > "$O/python.out" 2> "$O/python.err" || true; cmpout python "$tc"
85if command -v rustc > /dev/null; then
86 tc=$(ver rustc --version)
87 try rust "$tc" rustc -O --edition 2021 "$O/caller.rs" -L "$O" -C link-arg=-Wl,-rpath,"$O" -o "$O/caller_rs" \
88 && { "$O/caller_rs" < "$O/vectors.txt" > "$O/rust.out" 2> "$O/rust.err" || true; cmpout rust "$tc"; }
89else notrun rust rustc; fi
90if command -v zig > /dev/null; then
91 tc=$(ver zig version)
92 try zig "$tc" sh -c 'cd "$1" && zig build-exe caller.zig -L. -lomega_native -lc -rpath "$1" -femit-bin=caller_zig' _ "$O" \
93 && { "$O/caller_zig" < "$O/vectors.txt" > "$O/zig.out" 2> "$O/zig.err" || true; cmpout zig "$tc"; }
94else notrun zig zig; fi
95if command -v go > /dev/null; then
96 tc=$(ver go version)
97 mkdir -p "$O/go/omeganative"; cp "$O/omega_native.go" "$O/omega_native.h" "$O/libomega_native.so" "$O/go/omeganative/"
98 cp "$O/caller.go" "$O/go/"
99 try go "$tc" sh -c 'cd "$1" && go mod init omegacaller > /dev/null 2>&1 && CGO_LDFLAGS="-Wl,-rpath,$1/omeganative" go build -o caller_go .' _ "$O/go" \
100 && { "$O/go/caller_go" < "$O/vectors.txt" > "$O/go.out" 2> "$O/go.err" || true; cmpout go "$tc"; }
101else notrun go go; fi
102# Negative control: the same unit with its (first) sub turned into add, bound and called from C, must
103# disagree with the reference on these vectors.
104sed '0,/^\(put v [0-9]* \)sub /s//\1add /' "$UNIT" > "$O/mut/calc.om"
105if cmp -s "$O/mut/calc.om" "$UNIT"; then fail "mutant calc (sub -> add) did not change the unit"
106elif OMEGA_NATIVE_OMC="$OMC" "$ROOT/tools/native" "$O/mut" "$O/mut/calc.om" > "$O/mut/tools-native.log" 2>&1 \
107 && cc -std=c17 -O2 "$O/caller.c" -I"$O/mut" -L"$O/mut" -lomega_native -Wl,-rpath,"$O/mut" -o "$O/mut/caller_c" \
108 && "$O/mut/caller_c" < "$O/vectors.txt" > "$O/mut/c.out"; then
109 m=$(awk 'NR==FNR { e[FNR] = $0; next } e[FNR] == $0 { n++ } END { print n + 0 }' "$O/expect.txt" "$O/mut/c.out")
110 if cmp -s "$O/expect.txt" "$O/mut/c.out"; then fail "mutant calc (sub -> add) not caught"
111 else pass "mutant calc (sub -> add) caught ($((N - m))/$N vectors differ)"; fi
112else fail "mutant calc (sub -> add) did not build"; fi
113# The manifest: identities of the inputs and the artifact, the ELF header, and each caller's status.
114readelf -h "$O/libomega_native.so" > "$O/readelf-h.txt"
115python3 -I - "$O" "$ROOT" "$N" "$got" "$UNIT" "$NAME" "$ARITY" "$SEM" > "$O/manifest.json" << 'EOF'
116import hashlib, json, os, subprocess, sys
117O, ROOT, N, OMC_SHA, UNIT, NAME, ARITY, SEM = sys.argv[1:9]
118N, ARITY = int(N), int(ARITY)
119def sha(p):
120 return hashlib.sha256(open(p, "rb").read()).hexdigest()
121elf = {}
122for line in open(O + "/readelf-h.txt"):
123 k, _, v = line.partition(":")
124 if k.strip() in ("Class", "Machine", "OS/ABI", "Type"):
125 elf[k.strip().lower().replace("/", "_")] = v.strip()
126git = lambda *a: subprocess.run(["git", "-C", ROOT] + list(a), capture_output=True, text=True).stdout.strip()
127callers = {}
128for line in open(O + "/status.txt"):
129 lang, status, matched, tc = line.rstrip("\n").split("\t")
130 callers[lang] = {"status": status, "vectors": N if status != "NOT_RUN" else 0,
131 "matched": int(matched), "toolchain": tc}
132uname = subprocess.run(["uname", "-s", "-m"], capture_output=True, text=True).stdout.split()
133unit = os.path.realpath(UNIT)
134m = {
135 "schema": "omega-bridge.native-artifact-manifest.v1",
136 "unit": {"file": os.path.relpath(unit, ROOT) if unit.startswith(ROOT + "/") else unit,
137 "function": NAME, "inputs": ARITY},
138 "source_identity": {"algorithm": "sha256", "of": "the unit's .om bytes", "value": sha(unit)},
139 "semantic_identity": ({"value": SEM, "from": "the unit's first line: note omega.universal.lower.v1 semantic",
140 "definition": "spec/universal/translation-v1.md section 4 (omega-mv)"}
141 if SEM else None),
142 "omc_sha256": OMC_SHA,
143 "native_om_sha256": sha(ROOT + "/src/native.om"),
144 "tools_native_sha256": sha(ROOT + "/tools/native"),
145 "code_sha256": sha(O + "/calc.bin"),
146 "artifact": {"file": "libomega_native.so", "sha256": sha(O + "/libomega_native.so"),
147 "binding_sha256": {f: sha(O + "/omega_native." + f) for f in ("c", "h", "py", "rs", "zig", "go")}},
148 "isa": "aarch64",
149 "elf": elf,
150 "abi": "omega-native-v1",
151 "os": "Linux",
152 "host": {"uname_s": uname[0], "uname_m": uname[1]},
153 "bridge_commit": git("rev-parse", "HEAD"),
154 "bridge_tree_dirty": bool(git("status", "--porcelain", "--", "src", "tools", "tests")),
155 "vectors": {"count": N, "sha256": sha(O + "/vectors.txt"), "expect_sha256": sha(O + "/expect.txt"),
156 "reference": "tests/native/g1/reference.py (Python bigint, reduced mod 2^64)"},
157 "callers": callers,
158}
159print(json.dumps(m, indent=2, sort_keys=True))
160EOF
161echo "manifest $O/manifest.json"
162nr=$(awk -F'\t' '$2 == "NOT_RUN" { printf " %s", $1 }' "$O/status.txt")
163if [ "$fails" -ne 0 ]; then echo "G1-CALC FAIL $fails"; exit 1; fi
164echo "G1-CALC PASS (not run:${nr:- none})"