omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / test_private_symbols.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Host-only linkage regression; no device or application is opened.""" |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import subprocess |
| 6 | import tempfile |
| 7 | |
| 8 | ROOT = Path(__file__).resolve().parents[2] |
| 9 | OMC = os.environ["OMEGA_NATIVE_OMC"] |
| 10 | CC = os.environ.get("CC", "cc") |
| 11 | PIN = "734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61" |
| 12 | |
| 13 | |
| 14 | def run(args, **kwargs): |
| 15 | return subprocess.run(args, check=True, capture_output=True, **kwargs) |
| 16 | |
| 17 | |
| 18 | def build(generator, out): |
| 19 | out.mkdir() |
| 20 | objects = [] |
| 21 | for name in ("madd", "smadd"): |
| 22 | source = (ROOT / "tests/native" / (name + ".om")).read_bytes() |
| 23 | code = run([OMC], input=source).stdout |
| 24 | frame = source + b"\0" + f"{len(code):08d}".encode() + code |
| 25 | cfile = out / (name + ".c") |
| 26 | cfile.write_bytes(run([str(generator), "c"], input=frame).stdout) |
| 27 | obj = out / (name + ".o") |
| 28 | run([CC, "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", "-c", str(cfile), "-o", str(obj)]) |
| 29 | objects.append(obj) |
| 30 | result = subprocess.run([CC, "-pthread", str(ROOT / "tests/native/private_symbols.c"), |
| 31 | *map(str, objects), "-o", str(out / "caller")], capture_output=True) |
| 32 | (out / "link.log").write_bytes(result.stdout + result.stderr) |
| 33 | return result, objects |
| 34 | |
| 35 | |
| 36 | def generator(source, out): |
| 37 | out.write_bytes(run([OMC], input=source).stdout) |
| 38 | out.chmod(0o755) |
| 39 | return out |
| 40 | |
| 41 | |
| 42 | def main(): |
| 43 | import hashlib |
| 44 | assert hashlib.sha256(Path(OMC).read_bytes()).hexdigest() == PIN, "compiler pin mismatch" |
| 45 | # Optional source override also permits replaying the pre-fix generator. |
| 46 | source = Path(os.environ.get("OMEGA_NATIVE_SOURCE", ROOT / "src/native.om")).read_bytes() |
| 47 | with tempfile.TemporaryDirectory(prefix="omega-private-symbols-") as tmp: |
| 48 | tmp = Path(tmp) |
| 49 | gen = generator(source, tmp / "native") |
| 50 | linked, objects = build(gen, tmp / "normal") |
| 51 | if linked.returncode: |
| 52 | print(linked.stderr.decode(), end="") |
| 53 | raise AssertionError("separately generated bindings must link") |
| 54 | for name, obj in zip(("madd", "smadd"), objects): |
| 55 | symbols = run(["nm", "--defined-only", str(obj)], text=True).stdout.splitlines() |
| 56 | public = {line.split()[-1] for line in symbols if line.split()[-2].isupper()} |
| 57 | assert public == {"omega_" + name}, (obj, public) |
| 58 | private = {line.split()[-1]: line.split()[-2] for line in symbols |
| 59 | if line.split()[-1].startswith("ombr_")} |
| 60 | for helper in ("ombr_enter", "ombr_trap", "ombr_no_entry", "ombr_code_" + name, |
| 61 | "ombr_entry_" + name, "ombr_active", "ombr_stk", "ombr_rt_table"): |
| 62 | assert helper in private and private[helper].islower(), (helper, private) |
| 63 | print(run([str(tmp / "normal/caller")], text=True).stdout, end="") |
| 64 | # Restore just the original defect, not arbitrary code or public wrappers: ombr_enter's ELF |
| 65 | # spelling OMBR_LOCAL(ombr_enter) becomes a literal .globl directive. |
| 66 | mutant = source.replace(b"OMBR_LOCAL(ombr_enter)", b'\\".globl ombr_enter\\\\n\\"') |
| 67 | assert source.count(b"OMBR_LOCAL(ombr_enter)") == 1 and mutant != source, "missing local helper directive" |
| 68 | linked, _ = build(generator(mutant, tmp / "mutant"), tmp / "global") |
| 69 | assert linked.returncode != 0 and b"multiple definition" in linked.stderr and b"ombr_enter" in linked.stderr, linked.stderr |
| 70 | print("PRIVATE-SYMBOLS PASS: nm public ABI unchanged, private locals; .globl mutant rejected") |
| 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | main() |