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 / x86 / test_build_failures.py

114 lines · 6204 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Issue #34 controls: no missing/failed language can produce aggregate PASS.
3Runs the complete x86 boundary suite for each control and the positive case.
4Requires the same real toolchains/interpreter as run.sh; never emulates a build.
5OMEGA_NATIVE_X86_BAD_QEMU, when set, names an emulator known to break the callers
6(for example Ubuntu's QEMU 8.2.2); the suite must refuse it with
7QEMU-ORACLE-UNUSABLE before any caller runs. Unset, that control is NOT_RUN.
8OMEGA_NATIVE_X86_EXEC=native runs every case natively (see suite.py); the
9known-bad QEMU control is then NOT_RUN and the positive case must print NATIVE.
10
11Optional EMPTY_OUTPUT retains every case's stdout, command/status and numbered
12build/run logs. By default a fresh timestamped directory is created under
13build/native-x86-controls. Evidence is never automatically deleted.
14"""
15import os
16from datetime import datetime, timezone
17from pathlib import Path
18import shutil
19import shlex
20import subprocess
21import sys
22
23HERE = Path(__file__).resolve().parent
24
25
26def main():
27 if len(sys.argv) > 2:
28 print("usage: test_build_failures.py [EMPTY_OUTPUT]")
29 return 2
30 default = HERE.parents[2] / "build/native-x86-controls" / (
31 datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") + "-" + str(os.getpid()))
32 out = (Path(sys.argv[1]) if len(sys.argv) == 2 else default).resolve()
33 if out.exists() and (not out.is_dir() or any(out.iterdir())):
34 print("FAIL output directory must be empty: " + str(out))
35 return 1
36 out.mkdir(parents=True, exist_ok=True)
37 print("INFO control evidence: " + str(out), flush=True)
38 env = dict(os.environ)
39 native = env.get("OMEGA_NATIVE_X86_EXEC", "qemu") == "native"
40 how = b"NATIVE" if native else b"EMULATED"
41 # No control may print an aggregate PASS line, in either execution mode.
42 aggregate_pass = b" PASS (six languages built and run"
43 # The positive run must resolve actual compilers, not control scripts.
44 for key, default_tool in (("ZIG", "zig"), ("GO", "go")):
45 value = env.get(key, default_tool)
46 if not shutil.which(value):
47 print("NATIVE-X86 CONTROLS NOT_RUN: real " + key + " required")
48 return 1
49 env[key] = shutil.which(value)
50
51 def run_case(name, case_env):
52 args = [sys.executable, str(HERE / "suite.py"), str(out / name)]
53 run = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=case_env)
54 (out / (name + ".log")).write_bytes(run.stdout)
55 (out / (name + ".command")).write_text(
56 shlex.join(args) + "\nexit=" + str(run.returncode) + "\n")
57 return run
58
59 for key, status in (("ZIG", 42), ("GO", 43)):
60 fake = out / (key.lower() + "-fail")
61 fake.write_text(f"#!/bin/sh\nprintf 'CONTROL: {key} build refused\\n' >&2\nexit {status}\n")
62 fake.chmod(0o755)
63 run = run_case(key, dict(env, **{key: str(fake)}))
64 # The failing compiler's signal is in a numbered command log, and
65 # the aggregate must be FAIL, not a silent skip or green run.
66 signals = b"".join(p.read_bytes() for p in (out / key).glob("*.log"))
67 if run.returncode == 0 or b"NATIVE-X86 FAIL" not in run.stdout or aggregate_pass in run.stdout or f"CONTROL: {key} build refused".encode() not in signals:
68 sys.stdout.buffer.write(run.stdout)
69 print("FAIL control " + key + ": failed build was not refused; logs: " + str(out))
70 return 1
71 print("PASS failed-toolchain control " + key, flush=True)
72 missing = dict(env, ZIG=str(out / "absent-zig"), GO=str(out / "absent-go"))
73 run = run_case("missing", missing)
74 if run.returncode == 0 or b"NATIVE-X86 NOT_RUN" not in run.stdout or b"NOT_RUN zig" not in run.stdout or b"NOT_RUN go" not in run.stdout or aggregate_pass in run.stdout:
75 sys.stdout.buffer.write(run.stdout)
76 print("FAIL missing-toolchain control; logs: " + str(out))
77 return 1
78 print("PASS missing Zig and Go refuse aggregate PASS (#34)", flush=True)
79 run = run_case("bad-mode", dict(env, OMEGA_NATIVE_X86_EXEC="hardware"))
80 if run.returncode == 0 or b"NATIVE-X86 FAIL" not in run.stdout or b" caller " in run.stdout or aggregate_pass in run.stdout:
81 sys.stdout.buffer.write(run.stdout)
82 print("FAIL unknown execution mode control: mode was not refused; logs: " + str(out))
83 return 1
84 print("PASS unknown OMEGA_NATIVE_X86_EXEC refused before any caller ran", flush=True)
85 bad = env.get("OMEGA_NATIVE_X86_BAD_QEMU")
86 if native:
87 print("INFO known-bad QEMU control NOT_RUN: native execution does not use QEMU", flush=True)
88 elif bad:
89 run = run_case("bad-qemu", dict(env, QEMU_X86_64=bad))
90 if run.returncode == 0 or b"NOT_RUN QEMU-ORACLE-UNUSABLE" not in run.stdout or b"NATIVE-X86 NOT_RUN" not in run.stdout or b"caller EMULATED" in run.stdout or b"EMULATED PASS" in run.stdout:
91 sys.stdout.buffer.write(run.stdout)
92 print("FAIL known-bad QEMU control: emulator was not refused; logs: " + str(out))
93 return 1
94 print("PASS known-bad QEMU refused before any caller ran", flush=True)
95 else:
96 print("INFO known-bad QEMU control NOT_RUN: OMEGA_NATIVE_X86_BAD_QEMU unset", flush=True)
97 run = run_case("positive", env)
98 sys.stdout.buffer.write(run.stdout)
99 positive = (b"NATIVE-X86 NATIVE PASS (six languages built and run on the host x86-64 CPU; virtualization="
100 if native else b"NATIVE-X86 EMULATED PASS (six languages built and run; native x86 NOT_RUN)")
101 if run.returncode != 0 or positive not in run.stdout:
102 print("FAIL real-toolchain positive control; logs: " + str(out))
103 return 1
104 for language in ("c", "cpp", "python", "rust", "zig", "go"):
105 if ("PASS " + language + " caller ").encode() + how not in run.stdout:
106 print("FAIL positive caller missing: " + language)
107 return 1
108 print("NATIVE-X86 CONTROLS PASS (NATIVE; see the positive case's virtualization line)" if native
109 else "NATIVE-X86 CONTROLS PASS (EMULATED; native x86 NOT_RUN)")
110 return 0
111
112
113if __name__ == "__main__":
114 sys.exit(main())