interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / universal / translation / retarget_selftest.py

157 lines · 10414 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Retarget lane self-test (TESTED evidence for translation-compose-v2 section 2, not qualification).
3
4For every F1 positive and accepted c_import_controls boundary (C imports) and every member M of the
5extended S1 family, tools/adapters/retarget M must give exactly the R(U, M) bytes rebuilt here from the
6records; R must compose (R(R(U, A), B) == R(U, B), so N(R(U, M)) == N(U)); R(U, c) of a C import is
7the import with its sidecar stripped; every output decodes unchanged under the v2 codec build/L832/sa;
8the frozen v1 sa accepts the v1 members and refuses v2 members with 66; lower(R(U, c)) == lower(U).
9Refusals check status, empty stdout and the named stderr line. Tools run as separate processes, one at
10a time. A check whose tool is missing is NOT_RUN, never PASS, and the run exits 2.
11"""
12from pathlib import Path
13import argparse,hashlib,json,os,struct,subprocess,sys
14ROOT=Path(__file__).resolve().parents[3]
15MASK=(1<<64)-1
16MEMBERS=['c','rust','python','cpp','zig','go']
17ROW={'c':(1,1,1,1),'rust':(1,2,2,1),'python':(1,3,4,5),'cpp':(2,5,7,1),'zig':(2,6,8,1),'go':(2,7,9,1)}
18CONTRACT=hashlib.sha256((ROOT/'spec/universal/semantics.md').read_bytes()).digest()
19RETARGET=[sys.executable,str(ROOT/'tools/adapters/retarget')]
20LOWER=[sys.executable,str(ROOT/'tools/adapters/lower')]
21IMPORT=[sys.executable,str(ROOT/'tools/adapters/c-import')]
22NAMES={64:'BAD_COMMAND',65:'MALFORMED',66:'UNKNOWN_VERSION',67:'LIMIT',68:'PROFILE_MISMATCH',69:'UNKNOWN_OPERATION',
23 70:'TYPE_MISMATCH',71:'EFFECT_ALIAS_MISMATCH',72:'OBLIGATION_MISMATCH',74:'PROVENANCE_MISMATCH',78:'CONTRACT_MISMATCH'}
24FROZEN_V1_SA='1a127bf032e403b103d12d3140f27e74ab490e9a8b7af46b70233c6f3aaa60ce'
25fails=[];notrun=set()
26
27def check(ok,what):
28 if not ok:
29 fails.append(what);print('FAIL',what,flush=True)
30 return ok
31
32def run(cmd,data,timeout=60):
33 r=subprocess.run(cmd,input=data,capture_output=True,timeout=timeout)
34 return r.returncode,r.stdout,r.stderr
35
36def expected_r(env,m):
37 """Independent R(U, M): header version/language/profile, op dialects, arithmetic obligations; no sidecar."""
38 ns=struct.unpack_from('<I',env,5)[0];s=env[13:13+ns]
39 count=struct.unpack_from('<H',s,10)[0];ver,lang,prof,obl=ROW[m]
40 sem=b'OMSA'+bytes([ver,lang,prof,1,0,1])+struct.pack('<H',count)+s[12:44]
41 for i in range(count):
42 d,v,op,t,attr,eff,al,ob,a,b,imm=struct.unpack_from('<8BHHQ',s,44+20*i)
43 sem+=struct.pack('<8BHHQ',lang,v,op,t,attr,eff,al,obl if op in (2,3,4) else ob,a,b,imm)
44 return b'OMSB\x01'+struct.pack('<II',len(sem),0)+sem
45
46def semantic_only(env):
47 ns=struct.unpack_from('<I',env,5)[0]
48 return env[:9]+b'\0\0\0\0'+env[13:13+ns]
49
50U=65535
51def envelope(ops,ver=1,lang=1,prof=1,runtime=0,abi=1,contract=CONTRACT,count=None):
52 sem=b'OMSA'+bytes([ver,lang,prof,1,runtime,abi])+struct.pack('<H',len(ops) if count is None else count)+contract
53 for o in ops:sem+=struct.pack('<8BHHQ',*o)
54 return b'OMSB\x01'+struct.pack('<II',len(sem),0)+sem
55def inp(s,d=1,t=1,obl=1):return (d,1,9,t,0,0,0,obl,U,U,s)
56def const(v,d=1,t=1,attr=1,eff=0):return (d,1,1,t,attr,eff,0,0,U,U,v)
57def arith(op,a,b,d=1,obl=1,t=1,attr=1,eff=0,al=0):return (d,1,op,t,attr,eff,al,obl,a,b,0)
58
59class Codec:
60 """One sa binary: first byte = command (2 decode), then the envelope."""
61 def __init__(self,path,tag):
62 self.path=Path(path);self.tag=tag;self.ok=self.path.is_file() and os.access(self.path,os.X_OK)
63 self.sha=hashlib.sha256(self.path.read_bytes()).hexdigest() if self.ok else None
64 if not self.ok:notrun.add(f'{tag} ({path} missing)');print('NOT_RUN',tag,path,flush=True)
65 def decode(self,env):return run([str(self.path)],b'\x02'+env)
66
67def main():
68 ap=argparse.ArgumentParser();ap.add_argument('--out',required=True)
69 ap.add_argument('--v2-sa',default=str(ROOT/'build/L832/sa'))
70 ap.add_argument('--v1-sa',default=str(ROOT/'build/L574/sa'))
71 a=ap.parse_args();out=ROOT/a.out;out.mkdir(parents=True)
72 v2=Codec(a.v2_sa,'v2-codec-decode');v1=Codec(a.v1_sa,'frozen-v1-codec')
73 # The frozen #621 v1 codec (tools/source-aware/build -> build/L574/sa); any other bytes are not it.
74 if v1.ok and not check(v1.sha==FROZEN_V1_SA,f'frozen v1 codec {a.v1_sa} sha256 {v1.sha}, pinned {FROZEN_V1_SA}'):v1.ok=False
75 f1=json.loads((ROOT/'tests/universal/translation/fixtures-f1.json').read_text())
76 cases=[]
77 for p in f1['positives']:
78 src=p['source'].encode()
79 check(hashlib.sha256(src).hexdigest()==p['sha256'],p['id']+': fixture sha256')
80 cases.append((p['id'],src,None if p['name']=='calc' else p['name']))
81 header='#include <stdint.h>\n'
82 base=(header+'uint64_t calc(uint64_t a,uint64_t b){return a+b;}').encode()
83 cases+=[('C-exact-source-capacity',base+b' '*(4096-len(base)),None),
84 ('C-comment-at-capacity',base+b'//'+b'x'*(4096-len(base)-2),None),
85 ('C-operation-capacity',(header+'uint64_t calc(uint64_t a,uint64_t b){return '+'+'.join(['a']*61)+';}').encode(),None),
86 ('C-nesting-limit',(header+'uint64_t calc(uint64_t a,uint64_t b){return '+'('*16+'a'+')'*16+';}').encode(),None)]
87 c={'fixtures':0,'retargets':0,'compositions':0,'v2_decodes':0,'v1_accepts':0,'v1_refusals_66':0,'lower_equal':0}
88 p05env=None
89 for tag,src,name in cases:
90 rc,env,err=run(IMPORT,src)
91 if not check(rc==0,f'{tag}: import rc={rc} {err.decode().strip()}'):continue
92 if tag=='F1-P05':p05env=env
93 outs={}
94 for m in MEMBERS:
95 rc,o,err=run(RETARGET+[m],env)
96 if not check(rc==0 and err==b'',f'{tag} -> {m}: retarget rc={rc} {err.decode().strip()}'):continue
97 check(o==expected_r(env,m),f'{tag} -> {m}: output differs from R(U, {m})')
98 rc2,o2,_=run(RETARGET+[m],env);check(rc2==0 and o2==o,f'{tag} -> {m}: not byte-deterministic')
99 outs[m]=o;c['retargets']+=1
100 if tag in ('F1-P05','C-operation-capacity'):(out/f'{tag}.{m}.omsb').write_bytes(o)
101 if len(outs)!=6:continue
102 check(outs['c']==semantic_only(env),f'{tag}: R(U, c) is not the C import with its sidecar stripped')
103 for m,o in outs.items():
104 for m2 in MEMBERS:
105 rc,o3,err=run(RETARGET+[m2],o)
106 if check(rc==0 and o3==outs[m2],f'{tag}: R(R(U, {m}), {m2}) != R(U, {m2}) rc={rc} {err.decode().strip()}'):c['compositions']+=1
107 if v2.ok:
108 rc,d,err=v2.decode(o)
109 if check(rc==0 and d==o,f'{tag} -> {m}: v2 codec decode rc={rc} {err.decode().strip()}'):c['v2_decodes']+=1
110 if v1.ok:
111 rc,d,err=v1.decode(o)
112 if ROW[m][0]==1:
113 if check(rc==0 and d==o,f'{tag} -> {m}: frozen v1 codec decode rc={rc}'):c['v1_accepts']+=1
114 elif check(rc==66 and d==b'',f'{tag} -> {m}: frozen v1 codec gave rc={rc}, want 66'):c['v1_refusals_66']+=1
115 if tag.startswith('F1-'):
116 cmd=LOWER+([name] if name else [])
117 rc,l1,e1=run(cmd,env);rc2,l2,e2=run(cmd,outs['c'])
118 if check(rc==0 and rc2==0 and l1==l2,f'{tag}: lower(R(U, c)) != lower(U) rc={rc}/{rc2} {e1.decode().strip()}{e2.decode().strip()}'):c['lower_equal']+=1
119 c['fixtures']+=1
120 # Refusals. Decoder statuses follow source-aware-v2.md section 5 decode order.
121 def flip(e,off,val):b=bytearray(e);b[off]=val;return bytes(b)
122 so=semantic_only(p05env)
123 cpp=lambda ops,**k:envelope(ops,ver=2,lang=5,prof=7,**k)
124 refusals=[('no-argument',p05env,[],64),('two-arguments',p05env,['c','go'],64),('upper-case-C',p05env,['C'],64),
125 ('unknown-member',p05env,['cxx'],64),('empty-member',p05env,[''],64),('prefix-member',p05env,['pyth'],64),
126 ('over-16-byte-member',p05env,['python'*3],64),
127 ('empty-input',b'',['c'],65),('truncated-envelope',p05env[:-1],['c'],65),('bad-magic',flip(p05env,0,0),['c'],65),
128 ('trailing-byte',p05env+b'\0',['c'],65),('envelope-version-2',flip(p05env,4,2),['c'],66),
129 ('semantic-version-3',flip(so,17,3),['c'],66),('over-8192-bytes',p05env+b'\0'*(8193-len(p05env)),['c'],67),
130 ('zero-operations',envelope([],count=0),['c'],67),('wrong-semantics-hash',envelope([const(1)],contract=b'\0'*32),['c'],78),
131 ('opcode-10',envelope([inp(0),(1,1,10,1,1,0,0,1,0,0,0)]),['c'],69),('forward-operand',envelope([inp(0),arith(2,0,2),const(1)]),['c'],70),
132 ('const-effects',envelope([const(1,eff=1)]),['c'],71),
133 ('python-arith-obligation-1',envelope([inp(0,3),inp(1,3),arith(2,0,1,d=3,obl=1)],lang=3,prof=4),['c'],72),
134 ('sidecar-source-hash',flip(p05env,len(p05env)-2,0x41),['c'],74),
135 ('c-div-op5',envelope([inp(0),inp(1),arith(5,0,1,obl=3)]),['go'],68),
136 ('cpp-div-op5',cpp([inp(0,5),inp(1,5),arith(5,0,1,d=5,obl=3)]),['c'],68),
137 ('version-2-profile-1',flip(so,17,2),['c'],68),('version-1-profile-7',flip(cpp([inp(0,5)]),17,1),['c'],68),
138 ('profile-3-bigint',envelope([inp(0,3,t=2,obl=1),const(5,d=3,t=2,attr=2)],lang=3,prof=3,runtime=1),['c'],68),
139 ('profile-5-osc',envelope([inp(0,4),inp(1,4),arith(2,0,1,d=4)],lang=4,prof=5),['c'],68),
140 ('profile-6-dynamic',envelope([inp(0,3)],lang=3,prof=6,runtime=1,abi=2),['c'],68),
141 ('input-slot-gap',envelope([inp(1),const(3),arith(2,0,1)]),['c'],68)]
142 refused=0
143 for tag,data,argv,want in refusals:
144 rc,o,err=run(RETARGET+argv,data)
145 if check(rc==want and o==b'' and err==f'RETARGET_{NAMES[want]}\n'.encode(),f'refusal {tag}: rc={rc} want {want} stdout={len(o)} stderr={err!r}'):refused+=1
146 c['refusals']=refused;c['refusal_cases']=len(refusals)
147 report={'schema':'omega.upper-retarget.selftest.v1','evidence':'TESTED (lane self-test), byte identity, NATIVE',
148 'v2_sa_sha256':v2.sha,'v1_sa_sha256':v1.sha,'counts':c,'not_run':sorted(notrun),'failures':fails}
149 (out/'report.json').write_text(json.dumps(report,indent=2)+'\n')
150 summary=' '.join(f'{k}={v}' for k,v in c.items() if k!='refusal_cases')+f'/{len(refusals)}'
151 if fails:
152 print(f'FAIL retarget selftest: {len(fails)} failures; {summary}');return 1
153 if notrun:
154 print(f'INCOMPLETE retarget selftest (no failures) {summary}; NOT_RUN: {"; ".join(sorted(notrun))}');return 2
155 print(f'PASS retarget selftest {summary}');return 0
156
157if __name__=='__main__':raise SystemExit(main())