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 / test_capacity.py

80 lines · 4246 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Finite frame/output pools: compiler-built positive and hostile boundary controls."""
3import hashlib
4import os
5from pathlib import Path
6import subprocess
7import tempfile
8
9ROOT = Path(__file__).resolve().parents[2]
10OMC = os.environ["OMEGA_NATIVE_OMC"]
11PIN = "734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61"
12
13
14def checked(args, **kwargs):
15 return subprocess.run(args, check=True, capture_output=True, **kwargs)
16
17
18def build(source, out):
19 out.write_bytes(checked([OMC], input=source).stdout)
20 out.chmod(0o755)
21 return str(out)
22
23
24def refused(generator, frame):
25 result = subprocess.run([generator, "c"], input=frame, capture_output=True)
26 assert result.returncode == 106 and result.stdout == b"", (result.returncode, result.stderr)
27
28
29def main():
30 assert hashlib.sha256(Path(OMC).read_bytes()).hexdigest() == PIN, "compiler pin mismatch"
31 source = (ROOT / "src/native.om").read_bytes()
32 with tempfile.TemporaryDirectory(prefix="omega-capacity-") as tmp:
33 tmp = Path(tmp)
34 generator = build(source, tmp / "native")
35 # Every positive image is independently compiled from its matching source.
36 # The straight-line body gives substantial real compiler output, not padded code.
37 frames = []
38 for i in range(64):
39 unit = f"fn cap{i:02d} a ulong to ulong\n".encode() + b"a = xor a 1\n" * 8192 + b"ret ulong 7\nend\n"
40 code = checked([OMC], input=unit).stdout
41 frames.append(unit + b"\0" + f"{len(code):08d}".encode() + code)
42 frame = b"".join(frames)
43 assert 4194304 < len(frame) < 16777216
44 result = checked([generator, "c"], input=frame)
45 assert 16777216 < len(result.stdout) < 33554432
46 cfile = tmp / "capacity.c"
47 cfile.write_bytes(result.stdout)
48 caller = tmp / "caller.c"
49 # cap63's code is emitted past the first 16777216-byte output pool, i.e. from `outc`;
50 # calling it executes bytes stored in the second pool.
51 assert result.stdout.find(b'OMBR_SYM(ombr_code_cap63) ":') >= 16777216, "cap63 not in second output pool"
52 caller.write_text("#include <stdint.h>\nuint32_t omega_cap00(uint64_t *, uint64_t);\nuint32_t omega_cap15(uint64_t *, uint64_t);\nuint32_t omega_cap63(uint64_t *, uint64_t);\nint main(void) { uint64_t r=0; if(omega_cap00(&r,0)!=0 || r!=7) return 1; r=0; if(omega_cap15(&r,0)!=0 || r!=7) return 1; r=0; return omega_cap63(&r,0)!=0 || r!=7; }\n")
53 checked([os.environ.get("CC", "cc"), "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", "-pthread", str(cfile), str(caller), "-o", str(tmp / "caller")])
54 checked([str(tmp / "caller")])
55 for pool, old in ((b"inb uchar 16777216", b"inb uchar 4194304"),
56 (b"outc uchar 16777216", b"outc uchar 1")):
57 mutant = source.replace(b"global " + pool, b"global " + old)
58 assert mutant != source
59 refused(build(mutant, tmp / ("old-" + pool.split()[0].decode())), frame)
60 # Hostile protocol inputs exercise each real upper boundary separately.
61 refused(generator, b"\0" * 16777216)
62 print("CAPACITY PASS: current input pool exhaustion exits 106 with no output")
63 # The output probe deliberately has unmatched source/code custody: it is
64 # never executed and is solely an emitter-resource refusal control.
65 oversized = []
66 for i in range(8):
67 unit = f"fn flood{i} to ulong\nret ulong 7\nend\n".encode()
68 code = checked([OMC], input=unit).stdout
69 oversized_code = code + b"\xff" * (875000 - len(code))
70 oversized.append(unit + b"\0" + f"{len(oversized_code):08d}".encode() + oversized_code)
71 oversized_frame = b"".join(oversized)
72 assert len(oversized_frame) < 16777216
73 refused(generator, oversized_frame)
74 print("CAPACITY PASS: current output pool exhaustion exits 106 with no output")
75 print(f"CAPACITY PASS: compiler-built frame {len(frame)} bytes, C {len(result.stdout)} bytes, both endpoint calls")
76 print("CAPACITY PASS: old input/output bound mutants and new input/output overflow controls refuse with no output")
77
78
79if __name__ == "__main__":
80 main()