omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / seeded.py
| 1 | """Additive e5b32567 frontend: native/C/Python differential and pin mutants. |
| 2 | Run after the baseline tools/test.sh; this does not replace baseline coverage. |
| 3 | No GPU, CUDA runtime, driver or hardware access. |
| 4 | """ |
| 5 | import os |
| 6 | import pathlib |
| 7 | import shutil |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | root = pathlib.Path(__file__).resolve().parents[1] |
| 12 | os.chdir(root) |
| 13 | b = root / "build/seeded" |
| 14 | b.mkdir(parents=True, exist_ok=True) |
| 15 | seed = os.environ.get("OMEGA_SEEDED_OMC") |
| 16 | if not seed: |
| 17 | sys.exit("NOT_RUN: set OMEGA_SEEDED_OMC to the pinned e5b32567 compiler") |
| 18 | |
| 19 | def run(args, data=None, env=None): |
| 20 | return subprocess.run([str(a) for a in args], input=data, capture_output=True, |
| 21 | timeout=60, env=env) |
| 22 | |
| 23 | def checked(args, data=None): |
| 24 | p = run(args, data) |
| 25 | assert p.returncode == 0, (args, p.returncode, p.stderr.decode()) |
| 26 | return p.stdout |
| 27 | |
| 28 | def result(p): |
| 29 | return p.returncode, p.stdout, p.stderr |
| 30 | |
| 31 | source = "const N ulong 16777216\n" |
| 32 | source += "fn sum6 a ulong b ulong c ulong d ulong e ulong f ulong to ulong\nret add add add a b add c d add e f\nend\n" |
| 33 | source += "fn big a view uchar N to ulong\nret len a\nend\n" |
| 34 | for i in range(40): |
| 35 | body = "add x 1" if i == 0 else f"chain{i-1} add x 1" |
| 36 | source += f"fn chain{i} x ulong to ulong\nret {body}\nend\n" |
| 37 | source += """fn first a view ulong 4 to ulong |
| 38 | ret get a 0 |
| 39 | end |
| 40 | fn main to int |
| 41 | n = sum6 1 2 3 4 5 6 |
| 42 | for i ulong 64 |
| 43 | if gt i 7 |
| 44 | break |
| 45 | end |
| 46 | n = add n i |
| 47 | end |
| 48 | ignored = chain0 n |
| 49 | a = new ulong 4 0 |
| 50 | put a 0 chain39 n |
| 51 | ret int first view a |
| 52 | end |
| 53 | """ |
| 54 | fixture = b / "expanded.om" |
| 55 | fixture.write_text(source) |
| 56 | # A wrong compiler refuses before semantic checking or producing target output. |
| 57 | wrong = dict(os.environ, OMEGA_SEEDED_OMC=os.environ.get("OMEGA_SEED", "build/bootstrap/omc")) |
| 58 | p = run(["sh", "tools/bridge-seeded", "c", fixture], env=wrong) |
| 59 | assert p.returncode == 65 and not p.stdout and b"fingerprint mismatch" in p.stderr |
| 60 | # Baseline mode remains unable to adopt new capabilities implicitly. |
| 61 | p = run(["sh", "tools/bridge", "c", fixture]) |
| 62 | assert p.returncode != 0 and not p.stdout |
| 63 | native = b / "native" |
| 64 | native.write_bytes(checked([seed], source.encode())) |
| 65 | native.chmod(0o755) |
| 66 | expected = result(run([native])) |
| 67 | assert expected[0] not in range(101, 115), expected |
| 68 | outputs = {} |
| 69 | for target, ext in [("c", "c"), ("python", "py")]: |
| 70 | text = checked(["sh", "tools/bridge-seeded", target, fixture]) |
| 71 | path = b / ("expanded." + ext) |
| 72 | path.write_bytes(text) |
| 73 | if target == "c": |
| 74 | exe = b / "portable" |
| 75 | checked(["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", path, "-o", exe]) |
| 76 | got = run([exe]) |
| 77 | else: |
| 78 | got = run([sys.executable, path]) |
| 79 | assert result(got) == expected, (target, expected, result(got)) |
| 80 | outputs[target] = text |
| 81 | # Strict-host control: losing the unused-source-local annotation must fail. |
| 82 | assert b"__attribute__((unused))" in outputs["c"] |
| 83 | warning_mutant = b / "warning-mutant.c" |
| 84 | warning_mutant.write_bytes(outputs["c"].replace(b"__attribute__((unused)) ", b"")) |
| 85 | p = run(["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", |
| 86 | warning_mutant, "-o", b / "warning-mutant"]) |
| 87 | assert p.returncode != 0 and b"unused variable" in p.stderr |
| 88 | # Control: the same differential rejects an actual arithmetic emission defect. |
| 89 | assert b"(a+b)&mask" in outputs["c"] |
| 90 | mutant = b / "mutant.c" |
| 91 | mutant.write_bytes(outputs["c"].replace(b"(a+b)&mask", b"(a-b)&mask")) |
| 92 | checked(["gcc", "-std=gnu11", "-O2", mutant, "-o", b / "mutant"]) |
| 93 | assert result(run([b / "mutant"])) != expected |
| 94 | # Loop guards: generated C and Python must trap 4 (exit 104) exactly where omc does. |
| 95 | loops = { |
| 96 | "while": "fn main to int\nn = ulong 0\nwhile lt n {k}\nn = add n 1\nend\nret 7\nend\n", |
| 97 | "for": "fn count k ulong to ulong\ns = ulong 0\nfor i ulong k\ns = add s 1\nend\nret s\n" |
| 98 | "end\nfn main to int\nr = count {k}\nret int and r 255\nend\n", |
| 99 | "forcontinue": "fn count k ulong to ulong\ns = ulong 0\nfor i ulong k\ns = add s 1\n" |
| 100 | "if gt s 0\ncontinue\nend\nend\nret s\nend\nfn main to int\n" |
| 101 | "r = count {k}\nret int and r 255\nend\n", |
| 102 | } |
| 103 | for name, shape in loops.items(): |
| 104 | for k, want in [(999999, False), (1000000, False), (1000001, True), (1500000, True)]: |
| 105 | src = b / f"loop-{name}-{k}.om" |
| 106 | src.write_text(shape.replace("{k}", str(k))) |
| 107 | exe = b / f"loop-{name}-{k}.native" |
| 108 | exe.write_bytes(checked([seed], src.read_bytes())) |
| 109 | exe.chmod(0o755) |
| 110 | native_loop = result(run([exe])) |
| 111 | assert (native_loop[0] == 104) == want, (name, k, native_loop) |
| 112 | c = b / f"loop-{name}-{k}.c" |
| 113 | c.write_bytes(checked(["sh", "tools/bridge-seeded", "c", src])) |
| 114 | checked(["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", c, "-o", b / "loop-c"]) |
| 115 | assert result(run([b / "loop-c"])) == native_loop, (name, k, "c", native_loop) |
| 116 | if name == "forcontinue": # seeded `continue` is C-only; Python refuses it |
| 117 | p = run(["sh", "tools/bridge-seeded", "python", src]) |
| 118 | assert p.returncode != 0 and not p.stdout, (name, k, "python", result(p)) |
| 119 | continue |
| 120 | py = b / f"loop-{name}-{k}.py" |
| 121 | py.write_bytes(checked(["sh", "tools/bridge-seeded", "python", src])) |
| 122 | assert result(run([sys.executable, py])) == native_loop, (name, k, "python", native_loop) |
| 123 | # Explicit native x86 instruction-set execution, not AArch64 emulation. |
| 124 | if not shutil.which("x86_64-linux-gnu-gcc") or not shutil.which("qemu-x86_64"): |
| 125 | sys.exit("NOT_RUN: install x86_64-linux-gnu-gcc and qemu-x86_64 for x86 differential") |
| 126 | checked(["x86_64-linux-gnu-gcc", "-static", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", |
| 127 | b / "expanded.c", "-o", b / "portable-x86"]) |
| 128 | assert result(run(["qemu-x86_64", b / "portable-x86"])) == expected |
| 129 | unit = b / "unit.om" |
| 130 | unit.write_text("""const L ulong 4 |
| 131 | fn seedunit a view ulong L o edit ulong L to ulong |
| 132 | put o 0 get a 0 |
| 133 | ret get o 0 |
| 134 | end |
| 135 | """) |
| 136 | (b / "unit.c").write_bytes(checked(["sh", "tools/bridge-seeded", "c", unit])) |
| 137 | header = checked(["sh", "tools/bridge-seeded", "h", unit]) |
| 138 | assert b"omega_seedunit" in header and b"const void *" in header and b"{" not in header |
| 139 | (b / "unit.h").write_bytes(b"#include <stdint.h>\n" + header) |
| 140 | (b / "caller.c").write_text('''#include "unit.h" |
| 141 | int main(void) { |
| 142 | const uint64_t a[4] = {42}; |
| 143 | uint64_t out[4] = {0}, result = 0; |
| 144 | if (omega_seedunit(0,a,4,out,4) != 32) return 1; |
| 145 | if (omega_seedunit(&result,a,3,out,4) != 32) return 2; |
| 146 | if (omega_seedunit(&result,out,4,out,4) != 32) return 3; |
| 147 | if (omega_seedunit(&result,a,4,out,4) != 0 || result != 42 || out[0] != 42) return 4; |
| 148 | return 0; |
| 149 | } |
| 150 | ''') |
| 151 | checked(["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", b / "caller.c", b / "unit.c", "-o", b / "caller"]) |
| 152 | assert result(run([b / "caller"])) == (0, b"", b"") |
| 153 | print("SEEDED CONVERTER differential C/Python/native/x86 and pin/arithmetic/ABI controls PASS; device NOT_RUN") |