omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / test_freestanding_aggregate.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Qualify explicit CUDApp portable/native compositions; no production implementation lives here. |
| 4 | |
| 5 | Run this entire test under the device-free bwrap wrapper documented in NATIVE-ABI.md. |
| 6 | The three portable allocator roots become separate OM2 inputs by an exact `om2\n` prefix; |
| 7 | CoreUtil's already-composed OM2 roots are copied unchanged. Source/input hashes are recorded. |
| 8 | """ |
| 9 | import argparse |
| 10 | import hashlib |
| 11 | import json |
| 12 | import os |
| 13 | from pathlib import Path |
| 14 | import shlex |
| 15 | import subprocess |
| 16 | |
| 17 | ROOT = Path(__file__).resolve().parents[2] |
| 18 | |
| 19 | |
| 20 | def run(out, args, *, binary=False, expected=0, **kwargs): |
| 21 | args = [str(a) for a in args] |
| 22 | print('+', shlex.join(args), flush=True) |
| 23 | p = subprocess.run(args, capture_output=True, **kwargs) |
| 24 | with (out / 'commands.log').open('ab') as log: |
| 25 | log.write((shlex.join(args) + f'\nrc {p.returncode}\n').encode()) |
| 26 | log.write(b'stdout_hex ' + p.stdout.hex().encode() + b'\n' if binary else p.stdout) |
| 27 | log.write(p.stderr + b'\n') |
| 28 | assert p.returncode == expected, (args, p.returncode, expected, p.stdout, p.stderr) |
| 29 | return p.stdout |
| 30 | |
| 31 | |
| 32 | def generate(out, regalloc, core, target, api=None): |
| 33 | out.mkdir(parents=True, exist_ok=True) |
| 34 | inputs = out / 'sources' |
| 35 | inputs.mkdir(exist_ok=True) |
| 36 | records = [] |
| 37 | units = [] |
| 38 | groups = [ |
| 39 | (regalloc / 'om/codegen', ('regalloc.om', 'regalloc_ir.om', 'regalloc_live.om'), b'om2\n'), |
| 40 | (core / 'om/core', ('native.om', 'cubin.om', 'ampere.om'), b''), |
| 41 | ] |
| 42 | if api is not None: |
| 43 | groups.append((api / 'om/components', |
| 44 | ('api_policy.om', 'api_mm_plan.om', 'api_at_plan.om'), b'om2\n')) |
| 45 | for parent, names, prefix in groups: |
| 46 | for name in names: |
| 47 | source = parent / name |
| 48 | portable = source.read_bytes() |
| 49 | if prefix: |
| 50 | assert b'om2' not in portable.splitlines(), source |
| 51 | else: |
| 52 | assert b'om2' in portable.splitlines(), source |
| 53 | native = prefix + portable |
| 54 | dest = inputs / name |
| 55 | dest.write_bytes(native) |
| 56 | records.append({'source': str(source), |
| 57 | 'source_sha256': hashlib.sha256(portable).hexdigest(), |
| 58 | 'input_sha256': hashlib.sha256(native).hexdigest(), |
| 59 | 'prefix': prefix.decode()}) |
| 60 | units.append(dest) |
| 61 | (out / 'sources.json').write_text(json.dumps(records, indent=2) + '\n') |
| 62 | for mode in ('freestanding', 'hosted'): |
| 63 | command = [ROOT / 'tools/native', '--target', target] |
| 64 | if mode == 'freestanding': |
| 65 | command.append('--freestanding') |
| 66 | run(out, [*command, out / mode, *units]) |
| 67 | return units |
| 68 | |
| 69 | |
| 70 | def main(): |
| 71 | parser = argparse.ArgumentParser(description=__doc__) |
| 72 | parser.add_argument('--regalloc', type=Path, required=True) |
| 73 | parser.add_argument('--core', type=Path, required=True) |
| 74 | parser.add_argument('--api', type=Path, help='also qualify whole policy/mm/attention plans, including private f32/f64') |
| 75 | parser.add_argument('--out', type=Path, required=True) |
| 76 | parser.add_argument('--target', choices=('aarch64linux', 'x86linux'), default='aarch64linux') |
| 77 | args = parser.parse_args() |
| 78 | out = args.out.resolve() |
| 79 | units = generate(out, args.regalloc.resolve(), args.core.resolve(), args.target, |
| 80 | args.api.resolve() if args.api else None) |
| 81 | cc = shlex.split(os.environ.get('CC', 'cc')) |
| 82 | nm = shlex.split(os.environ.get('NM', 'nm')) |
| 83 | readelf = shlex.split(os.environ.get('READELF', 'readelf')) |
| 84 | runner = shlex.split(os.environ.get('FREEST_RUNNER', '')) |
| 85 | free = out / 'freestanding' |
| 86 | host = out / 'hosted' |
| 87 | flags = ['-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', |
| 88 | '-ffreestanding', '-fno-builtin', '-fno-stack-protector'] |
| 89 | if args.api: |
| 90 | flags.append('-DOMEGA_AGGREGATE_API_PLAN=1') |
| 91 | reloc = out / 'freestanding.o' |
| 92 | run(out, [*cc, '-r', '-nostdlib', free / 'omega_native.o', |
| 93 | free / 'omega_api_defs.o', '-o', reloc]) |
| 94 | imports = run(out, [*nm, '-u', reloc]).splitlines() |
| 95 | assert all(line.split()[-1] == b'_GLOBAL_OFFSET_TABLE_' for line in imports), imports |
| 96 | source = ROOT / 'tests/native/freestanding/aggregate.c' |
| 97 | bare = out / 'bare' |
| 98 | run(out, [*cc, *flags, '-DOMEGA_AGGREGATE_BARE=1', '-I', free, |
| 99 | source, reloc, '-nostdlib', '-static', '-no-pie', '-Wl,-e,_start', '-o', bare]) |
| 100 | assert not run(out, [*nm, '-u', bare]).strip(), 'bare ELF imports' |
| 101 | assert b'INTERP' not in run(out, [*readelf, '-lW', bare]), 'bare ELF interpreter' |
| 102 | sections = run(out, [*readelf, '-SW', bare]) |
| 103 | assert b'.tdata' not in sections and b'.tbss' not in sections, 'bare ELF TLS' |
| 104 | observed = run(out, [*runner, bare], binary=True) |
| 105 | assert observed, 'bare caller produced no differential bytes' |
| 106 | (out / 'bare.bin').write_bytes(observed) |
| 107 | hosted = out / 'hosted-caller' |
| 108 | run(out, [*cc, *flags, '-I', host, source, '-L', host, |
| 109 | '-Wl,-rpath,' + str(host), '-lomega_native', '-o', hosted]) |
| 110 | reference = run(out, [*runner, hosted], binary=True) |
| 111 | (out / 'hosted.bin').write_bytes(reference) |
| 112 | assert observed == reference, 'aggregate hosted/freestanding differential' |
| 113 | # The caller's wrong expected transport result must fail under the same bare link. |
| 114 | wrong = out / 'wrong' |
| 115 | run(out, [*cc, *flags, '-DOMEGA_AGGREGATE_BARE=1', '-DOMEGA_AGGREGATE_WRONG=1', |
| 116 | '-I', free, source, reloc, '-nostdlib', '-static', '-no-pie', '-Wl,-e,_start', |
| 117 | '-o', wrong]) |
| 118 | assert not run(out, [*runner, wrong], expected=90), 'wrong-result control wrote stdout' |
| 119 | if args.api: |
| 120 | for name, target, before, after, expected in ( |
| 121 | ('policy-rem', 'api_policy.om', b'ne rem b 128 0', b'eq rem b 128 0', 14), |
| 122 | ('mm-view', 'api_mm_plan.om', b'put a 8 m\n', b'put a 8 add m 1\n', 15), |
| 123 | ('at-scale', 'api_at_plan.om', b'if not ge sc f32 0\n', b'if not le sc f32 0\n', 19), |
| 124 | ('at-pack', 'api_at_plan.om', b'put a 8 ulong bitcast uint f32 v\n', |
| 125 | b'put a 8 add ulong bitcast uint f32 v 1\n', 20), |
| 126 | ): |
| 127 | work = out / ('mutant-' + name) |
| 128 | work.mkdir(exist_ok=True) |
| 129 | copies = [] |
| 130 | for unit in units: |
| 131 | content = unit.read_bytes() |
| 132 | if unit.name == target: |
| 133 | assert content.count(before) == 1, (name, 'mutation count') |
| 134 | content = content.replace(before, after) |
| 135 | copy = work / unit.name |
| 136 | copy.write_bytes(content) |
| 137 | copies.append(copy) |
| 138 | library = work / 'lib' |
| 139 | run(out, [ROOT / 'tools/native', '--target', args.target, '--freestanding', |
| 140 | library, *copies]) |
| 141 | executable = work / 'bare' |
| 142 | run(out, [*cc, *flags, '-DOMEGA_AGGREGATE_BARE=1', '-I', library, source, |
| 143 | library / 'omega_native.o', library / 'omega_api_defs.o', |
| 144 | '-nostdlib', '-static', '-no-pie', '-Wl,-e,_start', '-o', executable]) |
| 145 | assert not run(out, [*runner, executable], expected=expected), (name, 'stdout') |
| 146 | print('PASS actual API source mutant', name, 'exits', expected) |
| 147 | print('PASS aggregate bare ELF: no undefined symbols, interpreter, TLS or libc; hosted bytes identical') |
| 148 | print('PASS allocator, IR, liveness, graph, private f64, padded NOP and CUBIN refusal; bad span untouched; wrong-result control exits 90') |
| 149 | if args.api: |
| 150 | print('PASS whole policy/mm/attention without libc: views/rem/private f32/f64, RNE conversion/Newton outputs, bad span untouched; four source mutants caught') |
| 151 | |
| 152 | |
| 153 | if __name__ == '__main__': |
| 154 | main() |