Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-bridge/blob/main/tests/native/test_freestanding_f32.py # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-bridge / tests / native / test_freestanding_f32.py` 151 lines ยท 9382 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 """Private FP32 behind an integer-only native ABI, with no-libc differential and mutants.""" 4 import argparse 5 import hashlib 6 import json 7 import os 8 from pathlib import Path 9 import shlex 10 import shutil 11 12 from test_freestanding_aggregate import ROOT, run 13 14 15 def main(): 16 parser = argparse.ArgumentParser(description=__doc__) 17 parser.add_argument('--out', type=Path, required=True) 18 parser.add_argument('--target', choices=('aarch64linux', 'x86linux'), default='aarch64linux') 19 args = parser.parse_args() 20 out = args.out.resolve() 21 out.mkdir(parents=True, exist_ok=False) 22 unit = ROOT / 'tests/native/freestanding/f32.om' 23 caller = ROOT / 'tests/native/freestanding/f32.c' 24 records = {str(p.relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() 25 for p in (unit, caller, Path(__file__).resolve(), ROOT / 'src/native.om', ROOT / 'tools/native')} 26 (out / 'sources.json').write_text(json.dumps(records, indent=2) + '\n') 27 cc = shlex.split(os.environ.get('CC', 'cc')) 28 nm = shlex.split(os.environ.get('NM', 'nm')) 29 readelf = shlex.split(os.environ.get('READELF', 'readelf')) 30 runner = shlex.split(os.environ.get('FREEST_RUNNER', '')) 31 flags = ['-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-Wmissing-prototypes', 32 '-ffreestanding', '-fno-builtin', '-fno-stack-protector', '-ffp-contract=off'] 33 free, hosted = out / 'freestanding', out / 'hosted' 34 run(out, [ROOT / 'tools/native', '--target', args.target, '--freestanding', free, unit]) 35 run(out, [ROOT / 'tools/native', '--target', args.target, hosted, unit]) 36 reloc = out / 'freestanding.o' 37 run(out, [*cc, '-r', '-nostdlib', free / 'omega_native.o', free / 'omega_api_defs.o', '-o', reloc]) 38 imports = run(out, [*nm, '-u', reloc]).splitlines() 39 assert all(line.split()[-1] == b'_GLOBAL_OFFSET_TABLE_' for line in imports), imports 40 bare = out / 'bare' 41 run(out, [*cc, *flags, '-DOMEGA_F32_BARE=1', '-I', free, caller, reloc, 42 '-nostdlib', '-static', '-no-pie', '-Wl,-e,_start', '-o', bare]) 43 assert not run(out, [*nm, '-u', bare]).strip(), 'bare FP32 runtime import' 44 program_headers = run(out, [*readelf, '-l', bare]) 45 assert b'INTERP' not in program_headers and b'TLS' not in program_headers, program_headers 46 actual = run(out, [*runner, bare], binary=True) 47 reference = out / 'reference' 48 run(out, [*cc, *flags, '-pthread', '-I', hosted, caller, '-L', hosted, 49 '-Wl,-rpath,' + str(hosted), '-lomega_native', '-o', reference]) 50 expected = run(out, [*runner, reference], binary=True) 51 (out / 'bare.bin').write_bytes(actual) 52 (out / 'hosted.bin').write_bytes(expected) 53 assert actual == expected, 'raw integer-ABI FP32 bytes differ from hosted output' 54 wrong = out / 'wrong' 55 run(out, [*cc, *flags, '-DOMEGA_F32_BARE=1', '-DOMEGA_F32_WRONG=1', '-I', free, 56 caller, reloc, '-nostdlib', '-static', '-no-pie', '-Wl,-e,_start', '-o', wrong]) 57 assert not run(out, [*runner, wrong], expected=90), 'wrong-result control wrote output' 58 source = unit.read_text() 59 source_mutants = ( 60 ('addition', 'ret ulong bitcast uint add a b', 'ret ulong bitcast uint sub a b', 2), 61 ('contracting', 'p = mul a b\nret ulong bitcast uint add p c', 62 'p = mul f64 a f64 b\nret ulong bitcast uint f32 add p f64 c', 2), 63 ('integer-nan-rewrite', 'ret ulong bitcast uint a\n', 64 'if ne a a\nret ulong `2147483647`\nend\nret ulong bitcast uint a\n', 3), 65 ) 66 for name, old, new, status in source_mutants: 67 assert source.count(old) == 1, (name, source.count(old)) 68 work = out / ('mutant-' + name) 69 work.mkdir() 70 changed = work / 'f32.om' 71 changed.write_text(source.replace(old, new)) 72 lib = work / 'lib' 73 run(out, [ROOT / 'tools/native', '--target', args.target, '--freestanding', lib, changed]) 74 executable = work / 'bare' 75 run(out, [*cc, *flags, '-DOMEGA_F32_BARE=1', '-I', lib, caller, 76 lib / 'omega_native.o', lib / 'omega_api_defs.o', '-nostdlib', '-static', 77 '-no-pie', '-Wl,-e,_start', '-o', executable]) 78 assert not run(out, [*runner, executable], expected=status), (name, 'unexpected output') 79 print(f'PASS FP32 {name} source mutant caught with exit {status}') 80 generator = (ROOT / 'src/native.om').read_text() 81 suffix = ('b = get inb add s 1\nif eq b 54\nif ne get inb add s 2 52\nret 0\nend\n' 82 'else\nif ne b 51\nret 0\nend\nif ne get inb add s 2 50\nret 0\nend\nend') 83 generator_mutants = ( 84 ('private-type', '|f64|f32|"', '|f64|"'), 85 ('literal-suffix', suffix, 'if not and eq get inb add s 1 54 eq get inb add s 2 52\nret 0\nend'), 86 ) 87 for name, old, new in generator_mutants: 88 assert generator.count(old) == 1, (name, generator.count(old)) 89 tree = out / ('generator-' + name) 90 (tree / 'tools').mkdir(parents=True) 91 (tree / 'src').mkdir() 92 shutil.copy2(ROOT / 'tools/native', tree / 'tools/native') 93 (tree / 'src/native.om').write_text(generator.replace(old, new)) 94 lib = tree / 'refused' 95 run(out, [tree / 'tools/native', '--target', args.target, '--freestanding', lib, unit], expected=110) 96 assert not any(lib.glob('omega_*')), (name, 'refusal installed output') 97 print(f'PASS FP32 {name} generator mutant caught with refusal110 and no installed output') 98 compiler = Path(os.environ['OMEGA_NATIVE_X86_OMC' if args.target == 'x86linux' 99 else 'OMEGA_NATIVE_OMC2']) 100 compiler_pin = hashlib.sha256(compiler.read_bytes()).hexdigest() 101 generator_args = [free / 'work/native-gen', 'freest'] 102 if args.target == 'x86linux': 103 generator_args.append('x86linux') 104 refusals = { 105 'f32-parameter': 'export fn rejected a f32 to ulong\nret ulong bitcast uint a\nend\n', 106 'f64-parameter': 'export fn rejected a f64 to ulong\nret bitcast ulong a\nend\n', 107 'f32-result': 'export fn rejected a ulong to f32\nret bitcast f32 uint a\nend\n', 108 'f64-result': 'export fn rejected a ulong to f64\nret bitcast f64 a\nend\n', 109 'f32-view': 'export fn rejected a view f32 4 to ulong\nret ulong bitcast uint get a 0\nend\n', 110 'f64-view': 'export fn rejected a view f64 4 to ulong\nret bitcast ulong get a 0\nend\n', 111 'f32-edit': 'export fn rejected a edit f32 4 to ulong\nret ulong bitcast uint get a 0\nend\n', 112 'f64-edit': 'export fn rejected a edit f64 4 to ulong\nret bitcast ulong get a 0\nend\n', 113 'f32-out': 'export fn rejected a out f32 4 to ulong\nput a 0 f32 0\nret 0\nend\n', 114 'f64-out': 'export fn rejected a out f64 4 to ulong\nput a 0 f64 0\nret 0\nend\n', 115 'f32-dynamic-view': 'export fn rejected a view f32 * to ulong\nret len a\nend\n', 116 'f64-dynamic-view': 'export fn rejected a view f64 * to ulong\nret len a\nend\n', 117 'f32-dynamic-edit': 'export fn rejected a edit f32 * to ulong\nret len a\nend\n', 118 'f64-dynamic-edit': 'export fn rejected a edit f64 * to ulong\nret len a\nend\n', 119 } 120 assert len(refusals) == 14, 'public float refusal matrix changed' 121 for name, body in refusals.items(): 122 work = out / ('refusal-' + name) 123 work.mkdir() 124 rejected = work / 'rejected.om' 125 rejected.write_text('om2\n' + body) 126 snapshot = rejected.read_bytes() 127 compile_input = (b'target x86linux\n' if args.target == 'x86linux' else b'') + snapshot 128 code = run(out, [compiler], input=compile_input, binary=True) 129 assert code, (name, 'compiler emitted no code') 130 frame = snapshot + b'\0' + f'{len(code):08d}'.encode() + code 131 frame += f'omega-unit rejected {compiler_pin}\n'.encode() 132 assert not run(out, generator_args, input=frame, expected=110), (name, 'generator emitted output') 133 lib = work / 'lib' 134 run(out, [ROOT / 'tools/native', '--target', args.target, '--freestanding', lib, rejected], expected=110) 135 assert not any(lib.glob('omega_*')), (name, 'refusal installed output') 136 # The pure compiler itself rejects function-mode host words before the bridge. 137 # Reach the generator's independent whole-source guard with a scanner-only 138 # frame: reuse clean code and metadata, never execute the mismatched frame. 139 clean, separator, tail = (free / 'work/units.in').read_bytes().partition(b'\0') 140 assert separator and clean == unit.read_bytes() and clean.endswith(b'\n') 141 blocked = clean + b'fn hidden to ulong\nret ulong getuid\nend\n' 142 (out / 'refused-host-helper.om').write_bytes(blocked) 143 assert not run(out, generator_args, 144 input=blocked + b'\0' + tail, expected=110), 'host refusal emitted output' 145 print('PASS FP32 scanner-only unused host helper refused110; deliberately reused clean frame code is never executed') 146 print('PASS private FP32 integer ABI: non-NaNs bit-exact, NaN class only, raw integer outputs preserved; RNE/subnormal/non-contracting controls') 147 print('PASS FP32 bare ELF: no undefined symbols, interpreter, TLS or libc; raw hosted bytes identical; three source and two generator mutants caught; 14 compiled public float interfaces and scanner-only host helper refused110') 148 149 150 if __name__ == '__main__': 151 main()