omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / v2 / test_single_edit.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """CPU-only qualification of all three zero-initialized native span templates.""" |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import subprocess |
| 7 | import sys |
| 8 | |
| 9 | ROOT = Path(__file__).resolve().parents[3] |
| 10 | FIX = Path(__file__).resolve().parent |
| 11 | OUT = Path(sys.argv[1] if len(sys.argv) > 1 else ROOT / 'build/native-single-edit').resolve() |
| 12 | OUT.mkdir(parents=True, exist_ok=True) |
| 13 | CC = os.environ.get('CC', 'cc') |
| 14 | OLD = 'ombr_span sp[7];' |
| 15 | NEW = 'ombr_span sp[7] = {{0}};' |
| 16 | |
| 17 | def run(args, expected=0): |
| 18 | p = subprocess.run([str(x) for x in args], cwd=ROOT, capture_output=True) |
| 19 | if p.returncode != expected: |
| 20 | sys.stderr.buffer.write(p.stdout + p.stderr) |
| 21 | raise RuntimeError(f'{args}: rc={p.returncode}, expected={expected}') |
| 22 | return p |
| 23 | |
| 24 | source = (FIX / 'oneedit.om').read_text() |
| 25 | assert source.count('om2\n') == 1 and source.count('export fn ') == 1 |
| 26 | v1 = OUT / 'oneedit.om' |
| 27 | # OM1 has fixed-size views, not the OM2 dynamic '*' view. |
| 28 | v1.write_text(source.replace('om2\n', '', 1).replace('export fn ', 'fn ', 1) |
| 29 | .replace('edit uchar *', 'edit uchar 3', 1) |
| 30 | .replace('ret long len b', 'ret long 3', 1)) |
| 31 | profiles = [ |
| 32 | ('v1', v1, False, 1, False), |
| 33 | ('v2', FIX / 'oneedit.om', False, 1, False), |
| 34 | ('v2-device', FIX / 'dev/onehost.om', True, 4, False), |
| 35 | ('v2-context', FIX / 'dev/onehost.om', True, 4, True), |
| 36 | ] |
| 37 | for name, unit, host, count, context in profiles: |
| 38 | directory = OUT / name |
| 39 | run(['tools/native', *(['--host'] if host else []), directory, unit]) |
| 40 | text = (directory / 'omega_native.c').read_text() |
| 41 | assert text.count(NEW) == count and OLD not in text, name + ' declaration count' |
| 42 | caller = directory / 'singleedit' |
| 43 | flags = ['-DSINGLE_OM1'] if name == 'v1' else [] |
| 44 | if context: |
| 45 | flags.append('-DSINGLE_DEVICE') |
| 46 | run([CC, '-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-pthread', *flags, |
| 47 | '-I' + str(directory), FIX / 'singleedit.c', '-L' + str(directory), |
| 48 | '-Wl,-rpath,' + str(directory), '-lomega_native', '-o', caller]) |
| 49 | print(run([caller]).stdout.decode(), end='') |
| 50 | print(f'PASS {name}: exact zero-init declarations={count}') |
| 51 | |
| 52 | # Revert only the pure-v2 declaration. GCC13.3 must reproduce this reported |
| 53 | # false-positive warning, not an arbitrary compiler/link/launch failure. |
| 54 | directory = OUT / 'v2' |
| 55 | text = (directory / 'omega_native.c').read_text() |
| 56 | assert text.count(NEW) == 1 |
| 57 | mutant = directory / 'uninitialized.c' |
| 58 | mutant.write_text(text.replace(NEW, OLD)) |
| 59 | p = run([CC, '-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', |
| 60 | '-Wno-unused-variable', '-fPIC', '-shared', '-pthread', mutant, |
| 61 | '-o', directory / 'uninitialized.so'], expected=1) |
| 62 | assert b'maybe-uninitialized' in p.stderr and b'sp[1].lo' in p.stderr, p.stderr |
| 63 | (directory / 'uninitialized.stderr').write_bytes(p.stderr) |
| 64 | print('PASS single-edit warning mutant: GCC refuses sp[1].lo with maybe-uninitialized, rc=1') |
| 65 | print('SINGLE-EDIT PASS (v1, v2, device-default, device-context; four declaration counts; GCC warning mutant)') |