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
omega-mv / tests / universal / translation / cpp_selftest.py
| 1 | #!/usr/bin/env python3 |
| 2 | """C++ lane (#739) author self-test: TESTED evidence only (translation-v1 section 10), never QUALIFIED. |
| 3 | |
| 4 | For every F2-cpp fixture: C++ import, independent decode (v2 header, fields, sidecar binding, spans), #621 v2 |
| 5 | codec `sa identity|decode|replay` (build/L832, NOT_RUN when absent), cross-importer identity against the C |
| 6 | import of the F1 source, export of the unit and of R(U, M) semantic-only for all six family members (identical |
| 7 | bytes), same-language re-import with identical semantic bytes, and g++ DIFFERENTIAL (original and exported |
| 8 | C++) plus cc on the F1 C against a bigint evaluation of the records. Compositions C->C++ and C++->C |
| 9 | (translation-compose-v2 section 3: the v1-core G1 C exporter gets R(U, C); with --retarget the #624 retarget tool |
| 10 | produces every R(U, M) and must equal the test-code R byte for byte, otherwise the test-code R is used) with identity under N and the DIFFERENTIAL check including the native realization |
| 11 | lower(N(U_A)) -> omc 734f0a39 -> bridge binding. |
| 12 | """ |
| 13 | from pathlib import Path |
| 14 | import argparse,hashlib,json,os,random,shutil,struct,subprocess,sys,tempfile |
| 15 | ROOT=Path(__file__).resolve().parents[3] |
| 16 | MASK=(1<<64)-1 |
| 17 | U=65535 |
| 18 | SEMDOM=b'omega.source.semantic.v1' |
| 19 | CONTRACT=hashlib.sha256((ROOT/'spec/universal/semantics.md').read_bytes()).digest() |
| 20 | # translation-compose-v2 section 1: language, profile, arithmetic obligations, OMSA version |
| 21 | MEMBERS={'C':(1,1,1,1),'Rust':(2,2,1,1),'Python':(3,4,5,1),'Cpp':(5,7,1,2),'Zig':(6,8,1,2),'Go':(7,9,1,2)} |
| 22 | BOUNDARY=[0,1,2,(1<<32)-1,1<<32,(1<<63)-1,1<<63,MASK-1,MASK] |
| 23 | IMPORT=ROOT/'tools/adapters/cpp-import';EXPORT=ROOT/'tools/adapters/cpp-export' |
| 24 | CEXPORT=ROOT/'tools/adapters/c-export';LOWER=ROOT/'tools/adapters/lower' |
| 25 | CXXFLAGS=['-std=c++17','-O2','-Wall','-Wextra','-Werror','-Wconversion','-Wsign-conversion','-pedantic-errors','-Wno-unused-parameter'] |
| 26 | CFLAGS=['-std=c17','-O2','-Wall','-Wextra','-Werror','-Wno-unused-parameter'] |
| 27 | OMC_PIN='734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61' |
| 28 | BRIDGE_COMMIT='012860dbf26ee62a9838594b9dfd1e3e94a540a9' |
| 29 | NATIVE_OM_PIN='2c2c53f61d4ab302f1a2092c3c32189335dd53f6cf2b388f47a475f99f2c3940' |
| 30 | T='std::uint64_t' |
| 31 | |
| 32 | class Fail(Exception):pass |
| 33 | def check(c,msg): |
| 34 | if not c:raise Fail(msg) |
| 35 | |
| 36 | def run(cmd,data,cwd=None,timeout=60,env=None): |
| 37 | return subprocess.run(cmd,input=data,capture_output=True,timeout=timeout,cwd=cwd,env=env) |
| 38 | |
| 39 | def py(tool,data,*args):return run([sys.executable,str(tool),*args],data) |
| 40 | |
| 41 | # ---- independent envelope model (not the #621 codec) ---- |
| 42 | def split(env): |
| 43 | check(len(env)>=13 and env[:5]==b'OMSB\x01','bad envelope magic') |
| 44 | ns,np_=struct.unpack_from('<II',env,5);check(len(env)==13+ns+np_,'bad envelope length') |
| 45 | return env[13:13+ns],env[13+ns:] |
| 46 | |
| 47 | def ops_of(sem): |
| 48 | count=struct.unpack_from('<H',sem,10)[0];check(len(sem)==44+20*count,'bad semantic length') |
| 49 | return [struct.unpack_from('<8BHHQ',sem,44+20*i) for i in range(count)] |
| 50 | |
| 51 | def decode(env,member): |
| 52 | lang,prof,arob,ver=MEMBERS[member] |
| 53 | sem,side=split(env) |
| 54 | check(sem[:4]==b'OMSA' and sem[4]==ver and sem[5]==lang and sem[6]==prof and sem[7:10]==b'\x01\x00\x01','wrong %s header'%member) |
| 55 | check(sem[12:44]==CONTRACT,'wrong parent semantics hash') |
| 56 | ops=[];slots=[] |
| 57 | for i,(d,v,op,t,attr,eff,alias,obl,a,b,imm) in enumerate(ops_of(sem)): |
| 58 | check(d==lang and v==1 and t==1 and eff==0 and alias==0,'op %d fields'%i) |
| 59 | if op==9:check(attr==0 and obl==1 and a==b==U,'input op');slots.append(imm) |
| 60 | elif op==1:check(attr==1 and obl==0 and a==b==U,'const op') |
| 61 | else:check(op in (2,3,4) and attr==1 and obl==arob and a<i and b<i and imm==0,'arith op %d'%i) |
| 62 | ops.append((op,a,b,imm)) |
| 63 | check(sorted(slots)==list(range(len(slots))),'input slots not a permutation') |
| 64 | return sem,side,ops |
| 65 | |
| 66 | def sidecar(sem,side,src,count): |
| 67 | """C and C++ importers store raw bytes (OMSP encoding 0).""" |
| 68 | check(side[:6]==b'OMSP\x01\x00','sidecar must be v1 encoding 0 (raw bytes)') |
| 69 | check(struct.unpack_from('<I',side,6)[0]==len(src) and side[10:42]==hashlib.sha256(sem).digest() and side[42:74]==hashlib.sha256(src).digest(),'bad sidecar binding') |
| 70 | check(len(side)==74+8*count+len(src) and side[74+8*count:]==src,'sidecar source mismatch') |
| 71 | return [struct.unpack_from('<II',side,74+8*i) for i in range(count)] |
| 72 | |
| 73 | def evaluate(ops,args): |
| 74 | vals=[] |
| 75 | for op,a,b,imm in ops: |
| 76 | v=args[imm] if op==9 else imm if op==1 else vals[a]+vals[b] if op==2 else vals[a]-vals[b] if op==3 else vals[a]*vals[b] |
| 77 | vals.append(v&MASK) |
| 78 | return vals[-1] |
| 79 | |
| 80 | def normalized(ops): |
| 81 | n=len(ops) |
| 82 | return n>=3 and ops[n-2][0]==1 and ops[n-2][3]==0 and ops[n-1][0]==2 and ops[n-1][2]==n-2 and ops[n-1][1]<n-2 |
| 83 | |
| 84 | def retarget(sem,member): |
| 85 | """R(U, M) on the semantic bytes (translation-compose-v2 section 2): version, language, profile, dialects, obligations.""" |
| 86 | lang,prof,arob,ver=MEMBERS[member];b=bytearray(sem);b[4]=ver;b[5]=lang;b[6]=prof |
| 87 | for i,(_,_,op,*_) in enumerate(ops_of(sem)): |
| 88 | b[44+20*i]=lang |
| 89 | if op in (2,3,4):b[44+20*i+7]=arob |
| 90 | return bytes(b) |
| 91 | |
| 92 | def N(sem):return retarget(sem,'C') |
| 93 | |
| 94 | def strip(sem): |
| 95 | ops=ops_of(sem) |
| 96 | if not normalized([(o[2],o[8],o[9],o[10]) for o in ops]):return sem |
| 97 | n=len(ops)-2;b=bytearray(sem[:44+20*n]);struct.pack_into('<H',b,10,n);return bytes(b) |
| 98 | |
| 99 | def semonly(sem):return b'OMSB\x01'+struct.pack('<II',len(sem),0)+sem |
| 100 | def identity(sem):return hashlib.sha256(SEMDOM+sem).hexdigest() |
| 101 | def recs(sem):return [(o[2],o[8],o[9],o[10]) for o in ops_of(sem)] |
| 102 | |
| 103 | # ---- oracles ---- |
| 104 | class Oracles: |
| 105 | def __init__(s,out,cxx,cc):s.out=out;s.cxx=cxx;s.cc=cc;s.cache={};s.compiles=0 |
| 106 | def build(s,kind,text,name,nargs): |
| 107 | key=hashlib.sha256(kind.encode()+b'\0'+name.encode()+b'\0'+text).hexdigest() |
| 108 | if key in s.cache:return s.cache[key] |
| 109 | d=s.out/'oracle';d.mkdir(exist_ok=True);exe=d/key[:24] |
| 110 | call=name+'('+', '.join('x[%d]'%j for j in range(nargs))+')' |
| 111 | if kind=='cpp': |
| 112 | f=d/(key[:24]+'.cpp') |
| 113 | loop=('std::uint64_t x[6] = {};\n while (std::scanf("'+' '.join(['%" SCNu64 "']*nargs)+'", '+', '.join('&x[%d]'%j for j in range(nargs))+') == %d)\n '%nargs |
| 114 | if nargs else '') |
| 115 | f.write_bytes(text+('\n#include <cstdio>\n#include <cinttypes>\nint main() {\n '+loop+'std::printf("%" PRIu64 "\\n", '+call+');\n return 0;\n}\n').encode()) |
| 116 | cmd=['nice','-n','19',s.cxx,*CXXFLAGS,str(f),'-o',str(exe)] |
| 117 | else: |
| 118 | f=d/(key[:24]+'.c') |
| 119 | body=('uint64_t x[6]={0};while(scanf("'+' '.join(['%" SCNu64 "']*nargs)+'",'+','.join('&x[%d]'%j for j in range(nargs))+')==%d)'%nargs+ |
| 120 | 'printf("%" PRIu64 "\\n",'+call+');') if nargs else 'printf("%" PRIu64 "\\n",'+name+'());' |
| 121 | f.write_bytes(text+('\n#include <stdio.h>\n#include <inttypes.h>\nint main(void){'+body+'return 0;}\n').encode()) |
| 122 | cmd=['nice','-n','19',s.cc,*CFLAGS,str(f),'-o',str(exe)] |
| 123 | r=run(cmd,None,timeout=180);s.compiles+=1 |
| 124 | check(r.returncode==0,kind+' oracle compile failed: '+r.stderr.decode(errors='replace')[-800:]) |
| 125 | s.cache[key]=exe;return exe |
| 126 | def values(s,kind,text,name,vectors): |
| 127 | nargs=len(vectors[0]);exe=s.build(kind,text,name,nargs) |
| 128 | r=run([str(exe)],''.join(' '.join(map(str,v))+'\n' for v in vectors).encode(),timeout=60) |
| 129 | check(r.returncode==0,kind+' oracle run failed: '+r.stderr.decode(errors='replace')[-400:]) |
| 130 | return [int(x) for x in r.stdout.split()] |
| 131 | |
| 132 | def caller_c(n): |
| 133 | params=''.join(', uint64_t i%d'%i for i in range(n));args=''.join(', a[%d]'%i for i in range(n)) |
| 134 | return ('#include <inttypes.h>\n#include <stdio.h>\n#include <stdlib.h>\nuint32_t omega_calc(uint64_t *result%s);\n' |
| 135 | 'int main(void)\n{\n char line[512];\n while (fgets(line, sizeof line, stdin)) {\n char *p = line;\n' |
| 136 | ' uint64_t a[6] = {0};\n for (int i = 0; i < %d; i++)\n a[i] = strtoull(p, &p, 10);\n' |
| 137 | ' (void)a;\n (void)p;\n uint64_t r = UINT64_C(6148914691236517205);\n uint32_t s = omega_calc(&r%s);\n' |
| 138 | ' printf("%%" PRIu32 " %%" PRIu64 "\\n", s, r);\n }\n return 0;\n}\n')%(params,n,args) |
| 139 | |
| 140 | class Native: |
| 141 | """Native realization (translation-v1 section 5): lower(N(U)) -> pinned omc function mode -> pinned bridge binding -> cc caller.""" |
| 142 | def __init__(s,out,omc,bridge,cc): |
| 143 | s.out=out/'native';s.out.mkdir();s.omc=omc;s.cc=cc;s.cache={};s.vectors=0;s.status='NOT_RUN';s.reason='' |
| 144 | try: |
| 145 | check(Path(omc).is_file() and hashlib.sha256(Path(omc).read_bytes()).hexdigest()==OMC_PIN,'omc 734f0a39 pin') |
| 146 | src=subprocess.run(['git','-C',bridge,'show',BRIDGE_COMMIT+':src/native.om'],capture_output=True,check=True).stdout |
| 147 | check(hashlib.sha256(src).hexdigest()==NATIVE_OM_PIN,'bridge native.om pin') |
| 148 | r=run([omc],src);check(r.returncode==0,'bridge native.om compile') |
| 149 | s.gen=s.out/'native-gen';s.gen.write_bytes(r.stdout);s.gen.chmod(0o700);s.status='RUN' |
| 150 | except (Fail,OSError,subprocess.CalledProcessError) as e:s.reason=str(e) |
| 151 | def values(s,sem,vectors): |
| 152 | key=identity(N(sem)) |
| 153 | if key not in s.cache: |
| 154 | d=s.out/key[:24];d.mkdir() |
| 155 | r=py(LOWER,semonly(N(sem)));check(r.returncode==0,'lower refused N(U) rc=%d %s'%(r.returncode,r.stderr)) |
| 156 | unit=r.stdout;(d/'unit.om').write_bytes(unit) |
| 157 | r=run([s.omc],unit);check(r.returncode==0 and r.stdout,'omc function-mode compile rc=%d'%r.returncode) |
| 158 | code=r.stdout |
| 159 | r=run([str(s.gen)],unit+b'\0'+b'%08d'%len(code)+code);check(r.returncode==0 and r.stdout,'bridge binding rc=%d'%r.returncode) |
| 160 | n=sum(1 for o in recs(sem) if o[0]==9) |
| 161 | (d/'omega_native.c').write_bytes(r.stdout);(d/'caller.c').write_text(caller_c(n)) |
| 162 | r=run(['nice','-n','19',s.cc,'-std=gnu11','-O2','-Wall','-Wextra','-Werror','-pthread',str(d/'caller.c'),str(d/'omega_native.c'),'-o',str(d/'caller')],None,timeout=180) |
| 163 | check(r.returncode==0,'cc caller: '+r.stderr.decode(errors='replace')[-400:]) |
| 164 | s.cache[key]=d/'caller' |
| 165 | r=run([str(s.cache[key])],''.join(' '.join(map(str,v))+'\n' for v in vectors).encode()) |
| 166 | lines=r.stdout.decode().split('\n')[:-1];check(r.returncode==0 and len(lines)==len(vectors),'native caller run') |
| 167 | out=[] |
| 168 | for line in lines: |
| 169 | st,val=line.split();check(st=='0','native status '+st);out.append(int(val)) |
| 170 | s.vectors+=len(vectors);return out |
| 171 | |
| 172 | def vectors(nargs,rng): |
| 173 | if nargs==0:return [()] |
| 174 | if nargs<=2:base=[(x,) for x in BOUNDARY] if nargs==1 else [(x,y) for x in BOUNDARY for y in BOUNDARY] |
| 175 | else:base=[tuple(BOUNDARY[(j+k)%len(BOUNDARY)] for k in range(nargs)) for j in range(len(BOUNDARY))] |
| 176 | return base+[tuple(rng.getrandbits(64) for _ in range(nargs)) for _ in range(32)] |
| 177 | |
| 178 | def unit(lang,prof,runtime,abi,ops,typ=1,ver=1): |
| 179 | sem=b'OMSA'+bytes([ver,lang,prof,1,runtime,abi])+struct.pack('<H',len(ops))+CONTRACT |
| 180 | for op,attr,eff,alias,obl,a,b,imm in ops:sem+=struct.pack('<8BHHQ',lang,1,op,typ,attr,eff,alias,obl,a,b,imm) |
| 181 | return semonly(sem) |
| 182 | |
| 183 | def main(): |
| 184 | ap=argparse.ArgumentParser() |
| 185 | ap.add_argument('--out',required=True) |
| 186 | ap.add_argument('--c-import',default=str(ROOT/'build/L589/c-import')) |
| 187 | ap.add_argument('--sa-build',default='build/L832',help='v2 codec build directory under this checkout (SA_BUILD)') |
| 188 | ap.add_argument('--cxx',default=os.environ.get('CXX') or shutil.which('g++') or '',help='g++ oracle (default $CXX, then g++ on PATH)') |
| 189 | ap.add_argument('--cc',default=os.environ.get('CC') or shutil.which('cc') or '',help='C oracle (default $CC, then cc on PATH)') |
| 190 | ap.add_argument('--omc',default=os.environ.get('OMEGA_NATIVE_OMC',''),help='pinned omc 734f0a39 (required; $OMEGA_NATIVE_OMC)') |
| 191 | ap.add_argument('--bridge',default=os.environ.get('OMEGA_BRIDGE_REPO',''),help='omega-bridge clone holding 012860db (required; $OMEGA_BRIDGE_REPO)') |
| 192 | intree=ROOT/'tools/adapters/retarget' |
| 193 | ap.add_argument('--retarget',default=os.environ.get('CPP_SELFTEST_RETARGET') or (str(intree) if (ROOT/'build/L833/build.json').is_file() and intree.is_file() else ''), |
| 194 | help='public tools/adapters/retarget (#624), default the built in-tree one; cross-checked against the test-code R') |
| 195 | a=ap.parse_args() |
| 196 | missing=[w for w,v in (('g++ ($CXX or PATH)',a.cxx),('cc ($CC or PATH)',a.cc)) if not (v and shutil.which(v))] |
| 197 | missing+=[w for w,v in (('$OMEGA_NATIVE_OMC',a.omc),('$OMEGA_BRIDGE_REPO',a.bridge)) if not v] |
| 198 | if missing:print('NOT_RUN cpp self-test: missing '+', '.join(missing));return 3 |
| 199 | out=Path(a.out).resolve();out.mkdir(parents=True,exist_ok=False) |
| 200 | cimp=Path(a.c_import) |
| 201 | O=Oracles(out,a.cxx,a.cc);NAT=Native(out,a.omc,a.bridge,a.cc);rng=random.Random(739);failures=[];counts={} |
| 202 | if NAT.status!='RUN':print('FAIL native realization setup: '+NAT.reason);return 1 |
| 203 | def bump(k,n=1):counts[k]=counts.get(k,0)+n |
| 204 | sa_exe=ROOT/a.sa_build/'sa';codec='RUN' if sa_exe.is_file() and (ROOT/'src/source-aware/core-v2.om').is_file() else 'NOT_RUN' |
| 205 | sa_env=dict(os.environ,SA_BUILD=a.sa_build) |
| 206 | def sa(cmd,env): |
| 207 | r=run(['sh','tools/source-aware/sa',cmd],env,cwd=ROOT,env=sa_env);bump('sa_v2_requests');return r |
| 208 | def cimport(src): |
| 209 | r=run([str(cimp)],src);bump('c_import_runs');return r |
| 210 | def ximport(src): |
| 211 | r=py(IMPORT,src);bump('cpp_import_runs');return r |
| 212 | def xexport(env): |
| 213 | r=py(EXPORT,env);bump('cpp_export_runs');return r |
| 214 | RMEMBER={'C':'c','Rust':'rust','Python':'python','Cpp':'cpp','Zig':'zig','Go':'go'} |
| 215 | def R(env,sem,m): |
| 216 | """R(U, M) semantic-only: the retarget tool when given (must equal the test-code R byte for byte), else the test-code R.""" |
| 217 | want=semonly(retarget(sem,m)) |
| 218 | if not a.retarget:return want |
| 219 | r=py(Path(a.retarget),env,RMEMBER[m]);bump('retarget_runs') |
| 220 | check(r.returncode==0 and r.stdout==want,'retarget %s differs from the test-code R (rc=%d %s)'%(m,r.returncode,r.stderr));return r.stdout |
| 221 | def native_check(sem,vec,ref): |
| 222 | if NAT.status!='RUN':return 'NOT_RUN ('+NAT.reason+')' |
| 223 | check(NAT.values(sem,vec)==ref,'native realization lower(N(U_A)) != reference');return 'PASS' |
| 224 | |
| 225 | f1=json.loads((ROOT/'tests/universal/translation/fixtures-f1.json').read_text()) |
| 226 | f2=json.loads((ROOT/'tests/universal/translation/fixtures-f2-cpp.json').read_text()) |
| 227 | f1pos={p['id']:p for p in f1['positives']} |
| 228 | check(len(f2['positives'])+len(f2['not_in_profile'])==len(f1pos),'F2-cpp does not cover every F1 positive') |
| 229 | check({p['f1_id'] for p in f2['positives']}|{p['f1_id'] for p in f2['not_in_profile']}==set(f1pos),'F2-cpp F1 id set') |
| 230 | |
| 231 | # Oracle type control: a ULL literal is not std::uint64_t on this target, the braced form and the operators are. |
| 232 | ctl_src=('#include <cstdint>\n#include <type_traits>\nstatic_assert(!std::is_same_v<decltype(1ULL), std::uint64_t>, "ULL");\n' |
| 233 | 'static_assert(std::is_same_v<decltype(std::uint64_t{1ULL}), std::uint64_t>, "braced");\n' |
| 234 | 'static_assert(std::is_same_v<decltype(std::uint64_t{1ULL} * std::uint64_t{2ULL} - std::uint64_t{3ULL}), std::uint64_t>, "ops");\n' |
| 235 | 'static_assert(sizeof(std::uint64_t) == 8 && sizeof(unsigned long long) == 8 && sizeof(int) <= 8, "widths");\n' |
| 236 | 'std::uint64_t calc() { return std::uint64_t{18446744073709551615ULL} * std::uint64_t{18446744073709551615ULL}; }\n').encode() |
| 237 | check(O.values('cpp',ctl_src,'calc',[()])==[1],'oracle type control');counts['oracle_type_control']='PASS' |
| 238 | over=b'#include <cstdint>\nstd::uint64_t calc() { return std::uint64_t{18446744073709551616ULL}; }\n' |
| 239 | r=run([a.cxx,*CXXFLAGS,'-fsyntax-only','-x','c++','-'],over);check(r.returncode!=0,'oracle accepted an out-of-range braced literal') |
| 240 | counts['oracle_overflow_control']='PASS' |
| 241 | |
| 242 | def export_checks(sem,env_with_sidecar,tag): |
| 243 | """Exporter: identical bytes for every family member with or without sidecar; returns src_B.""" |
| 244 | r=xexport(env_with_sidecar);check(r.returncode==0 and r.stderr==b'',tag+' export refused rc=%d %s'%(r.returncode,r.stderr)) |
| 245 | text=r.stdout |
| 246 | for m in MEMBERS: |
| 247 | u=R(env_with_sidecar,sem,m) |
| 248 | if codec=='RUN':check(sa('decode',u).returncode==0,tag+' R(U,%s) not a valid v2-codec unit'%m) |
| 249 | q=xexport(u);check(q.returncode==0 and q.stdout==text,tag+' export differs for member '+m+' (semantic-only)') |
| 250 | bump('member_exports',len(MEMBERS)) |
| 251 | check(text.startswith(b'#include <cstdint>\nstd::uint64_t calc(') and text.endswith(b'; }\n') and text.count(b'\n')==2,tag+' export form') |
| 252 | check(b'ULL' not in text.replace(b'ULL}',b''),tag+' unbraced literal in export') |
| 253 | return text |
| 254 | |
| 255 | def reimport_identity(sem_a,src_b,tag): |
| 256 | r=ximport(src_b);check(r.returncode==0,tag+' re-import refused rc=%d %s'%(r.returncode,r.stderr)) |
| 257 | sem_b,side_b,ops_b=decode(r.stdout,'Cpp');sidecar(sem_b,side_b,src_b,len(ops_b)) |
| 258 | check(N(strip(sem_b))==N(strip(sem_a)),tag+' identity N(strip(U_B)) != N(strip(U_A))') |
| 259 | return sem_b,ops_b |
| 260 | |
| 261 | def codec_checks(env,sem,src): |
| 262 | if codec!='RUN':bump('codec_not_run');return 'NOT_RUN' |
| 263 | ident=sa('identity',env);check(ident.returncode==0 and ident.stdout.hex()==identity(sem),'sa v2 identity refused or differs') |
| 264 | check(sa('decode',env).stdout==env,'sa v2 decode changed unit');check(sa('replay',env).stdout==src,'sa v2 replay differs from source') |
| 265 | lg=sa('legality',b'\x02\x00\x01\x00'+env);obl=[o[7] for o in ops_of(sem)] |
| 266 | check(lg.returncode==0 and lg.stdout==bytes(x for ob in obl for x in (3,ob)),'sa v2 legality is not LOWERABLE_WITH_OBLIGATIONS per op');bump('codec_accepted');return 'PASS' |
| 267 | |
| 268 | rows=[] |
| 269 | for fx in f2['positives']: |
| 270 | tag=fx['id'];item={'id':tag} |
| 271 | try: |
| 272 | src=fx['source'].encode();check(hashlib.sha256(src).hexdigest()==fx['sha256'],'fixture sha') |
| 273 | p1=f1pos[fx['f1_id']];check(p1['sha256']==fx['f1_sha256'] and p1['inputs']==fx['inputs'] and p1['name']==fx['name'],'F1 link') |
| 274 | r=ximport(src);check(r.returncode==0 and r.stderr==b'','import refused rc=%d %s'%(r.returncode,r.stderr)) |
| 275 | env=r.stdout;sem,side,ops=decode(env,'Cpp');spans=sidecar(sem,side,src,len(ops)) |
| 276 | check(normalized(ops),'importer must append the normalization pair') |
| 277 | check(spans[-1]==spans[-2]==(0,0),'synthetic pair spans') |
| 278 | for (op,_,_,imm),(lo,hi) in zip(ops[:-2],spans[:-2]): |
| 279 | t=src[lo:hi] |
| 280 | if op==1:check(t.startswith(b'std::uint64_t{') and t.endswith(b'}') and b'%dULL'%imm in t,'literal span') |
| 281 | elif op==9:check(len(t)>0 and t.isascii() and t.decode().isidentifier(),'input span') |
| 282 | else:check(t==b'+-*'[op-2:op-1],'operator span') |
| 283 | item['codec']=codec_checks(env,sem,src) |
| 284 | c=cimport(p1['source'].encode());check(c.returncode==0,'C import of F1 refused') |
| 285 | csem,_,_=decode(c.stdout,'C') |
| 286 | check(N(sem)==N(csem),'cross-importer identity: N(import_Cpp(F2)) != N(import_C(F1))') |
| 287 | exported=export_checks(sem,env,tag) |
| 288 | sem_b,ops_b=reimport_identity(sem,exported,tag) |
| 289 | check(sem_b==sem,'same-language round trip changed the semantic bytes') |
| 290 | vec=vectors(fx['inputs'],rng);ref=[evaluate(ops,v) for v in vec] |
| 291 | check(O.values('cpp',src,fx['name'],vec)==ref,'g++(original C++) != reference') |
| 292 | check(O.values('cpp',exported,'calc',vec)==ref,'g++(exported C++) != reference') |
| 293 | check(O.values('c',p1['source'].encode(),p1['name'],vec)==ref,'cc(F1 C) != reference') |
| 294 | # Non-NORMALIZED input: the exporter emits the last result; re-import gains one pair (recorded). |
| 295 | s2=strip(sem);q=xexport(semonly(s2));check(q.returncode==0,'export of stripped unit refused') |
| 296 | r2=ximport(q.stdout);check(r2.returncode==0,'re-import of stripped export refused') |
| 297 | sem2,_,ops2=decode(r2.stdout,'Cpp') |
| 298 | check([evaluate(ops2,v) for v in vec]==[evaluate(recs(s2),v) for v in vec],'stripped round trip evaluation differs') |
| 299 | item.update(result='PASS',ops=len(ops),vectors=len(vec),semantic_identity=identity(sem),N_identity=identity(N(sem)),exported=exported.decode(), |
| 300 | stripped_reimport_identity_equal=N(strip(sem2))==N(s2)) |
| 301 | bump('gxx_observations',2*len(vec));bump('cc_observations',len(vec)) |
| 302 | except (Fail,ValueError,KeyError,IndexError,struct.error,subprocess.TimeoutExpired) as e: |
| 303 | item.update(result='FAIL',failure=str(e));failures.append(item) |
| 304 | rows.append(item) |
| 305 | |
| 306 | # ---- C++ import refusals ---- |
| 307 | refusals=[] |
| 308 | for fx in f2['refusals']: |
| 309 | src=bytes.fromhex(fx['source_hex']) if 'source_hex' in fx else fx['source'].encode() |
| 310 | r=ximport(src);ok=hashlib.sha256(src).hexdigest()==fx['sha256'] and r.returncode==fx['status'] and r.stdout==b'' and r.stderr.startswith(b'CPP_IMPORT_') |
| 311 | it={'id':fx['id'],'name':fx['name'],'want':fx['status'],'exit':r.returncode,'stderr':r.stderr.decode(errors='replace').strip(),'result':'PASS' if ok else 'FAIL'} |
| 312 | if not ok:failures.append(it) |
| 313 | refusals.append(it) |
| 314 | |
| 315 | # ---- import boundary controls (exact and over capacity); each accepted one exported, re-imported and compiled ---- |
| 316 | head=('#include <cstdint>\n'+T+' calc('+T+' a, '+T+' b) { return ').encode() |
| 317 | base=head+b'a + b; }' |
| 318 | chain=lambda k:head+b'a'+b' + a'*k+b'; }' |
| 319 | parens=lambda k:head+b'('*k+b'a'+b')'*k+b'; }' |
| 320 | lits=lambda k:head+b' + '.join([b'std::uint64_t{18446744073709551615ULL}']*k)+b'; }' |
| 321 | controls=[('exact-source-capacity',base+b' '*(4096-len(base)),0,12),('comment-at-capacity',base+b'//'+b'x'*(4096-len(base)-2),0,12), |
| 322 | ('operation-capacity',chain(60),0,61*5),('nesting-limit',parens(16),0,5),('literal-capacity',lits(30),0,(30*MASK)&MASK), |
| 323 | ('over-source-capacity',base+b' '*(4097-len(base)),67,None),('over-operation-capacity',chain(61),67,None), |
| 324 | ('over-nesting-limit',parens(17),67,None),('over-literal-capacity',lits(31),67,None)] |
| 325 | ctl=[] |
| 326 | for name,src,want,value in controls: |
| 327 | it={'name':name,'source_bytes':len(src)} |
| 328 | try: |
| 329 | r=ximport(src) |
| 330 | if want:check(r.returncode==want and not r.stdout and r.stderr==b'CPP_IMPORT_LIMIT\n','%s rc=%d'%(name,r.returncode)) |
| 331 | else: |
| 332 | check(r.returncode==0,'%s refused rc=%d %s'%(name,r.returncode,r.stderr));sem,side,ops=decode(r.stdout,'Cpp');sidecar(sem,side,src,len(ops)) |
| 333 | check(evaluate(ops,[5,7])==value,name+' value');exported=export_checks(sem,r.stdout,name) |
| 334 | sem_b,_=reimport_identity(sem,exported,name);check(sem_b==sem,name+' round trip') |
| 335 | it['codec']=codec_checks(r.stdout,sem,src) |
| 336 | vec=vectors(2,rng);ref=[evaluate(ops,v) for v in vec] |
| 337 | check(O.values('cpp',src,'calc',vec)==ref and O.values('cpp',exported,'calc',vec)==ref,name+' g++ differential') |
| 338 | it.update(ops=len(ops),exported_bytes=len(exported)) |
| 339 | it.update(result='PASS',exit=r.returncode) |
| 340 | except (Fail,ValueError,struct.error) as e:it.update(result='FAIL',failure=str(e));failures.append(it) |
| 341 | ctl.append(it) |
| 342 | for nm in ('cpp-import','cpp-export'): |
| 343 | r=subprocess.run([str(ROOT/'build/L739'/nm)],stdin=subprocess.DEVNULL,capture_output=True,preexec_fn=lambda:os.close(0),timeout=10) |
| 344 | want=b'CPP_IMPORT_IO\n' if nm=='cpp-import' else b'CPP_EXPORT_IO\n' |
| 345 | ctl.append({'name':nm+'-closed-stdin','exit':r.returncode,'result':'PASS' if r.returncode==77 and not r.stdout and r.stderr==want else 'FAIL'}) |
| 346 | for tool in (IMPORT,EXPORT): |
| 347 | r=py(tool,base,'unexpected');ctl.append({'name':tool.name+'-arguments-refused','exit':r.returncode,'result':'PASS' if r.returncode==64 and not r.stdout and b'BAD_COMMAND' in r.stderr else 'FAIL'}) |
| 348 | r=py(IMPORT,base);d=run([str(ROOT/'build/L739/cpp-import')],base) |
| 349 | ctl.append({'name':'public-import-identical-to-native','result':'PASS' if r.returncode==0 and r.stdout==d.stdout else 'FAIL'}) |
| 350 | good=r.stdout |
| 351 | e1=py(EXPORT,good);e2=run([str(ROOT/'build/L739/cpp-export')],good) |
| 352 | ctl.append({'name':'public-export-identical-to-native','result':'PASS' if e1.returncode==0 and e1.stdout==e2.stdout else 'FAIL'}) |
| 353 | files=['src/source-aware/vendor/sha256.om','src/source-aware/core-v2.om','src/adapters/cpp-u64-import.om','src/adapters/cpp-u64-export.om','build/L739/build.json','build/L739/cpp-import','build/L739/cpp-export'] |
| 354 | for tool,victims,data in ((IMPORT,['src/source-aware/vendor/sha256.om','src/adapters/cpp-u64-import.om','build/L739/cpp-import','build/L739/build.json'],base), |
| 355 | (EXPORT,['src/source-aware/core-v2.om','src/adapters/cpp-u64-export.om','build/L739/cpp-export'],good)): |
| 356 | for victim in victims: |
| 357 | with tempfile.TemporaryDirectory(prefix='closed-build-',dir=out) as td: |
| 358 | t=Path(td) |
| 359 | for rel in ['tools/adapters/'+tool.name,*files]: |
| 360 | p=t/rel;p.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(ROOT/rel,p);p.chmod(0o700) |
| 361 | with (t/victim).open('ab') as f:f.write(b'changed') |
| 362 | r=py(t/'tools/adapters'/tool.name,data) |
| 363 | ok=r.returncode==78 and not r.stdout and b'STALE_OR_MISSING_BUILD' in r.stderr |
| 364 | ctl.append({'name':tool.name+'-stale-'+victim,'exit':r.returncode,'result':'PASS' if ok else 'FAIL'}) |
| 365 | failures+=[c for c in ctl if c['result']=='FAIL' and c not in failures] |
| 366 | |
| 367 | # ---- exporter refusals ---- |
| 368 | gsem,gside=split(good) |
| 369 | def shared(k): # t_j = add(t_{j-1}, t_{j-1}): tree size 2^k, depth k |
| 370 | return unit(5,7,0,1,[(9,0,0,0,1,U,U,0)]+[(2,1,0,0,1,j,j,0) for j in range(k)],ver=2) |
| 371 | deep=[(9,0,0,0,1,U,U,0)]+[(2,1,0,0,1,0,j,0) for j in range(18)] |
| 372 | flipped=bytearray(good);flipped[-3]^=1 |
| 373 | v3=bytearray(good);v3[17]=3 |
| 374 | xr=[('truncated',good[:-1],65),('bad-magic',b'X'+good[1:],65),('sidecar-source-mismatch',bytes(flipped),65),('oversize',good+b'\0'*(8193-len(good)),65), |
| 375 | ('unknown-semantic-version-3',bytes(v3),65), |
| 376 | ('profile3-bigint',unit(3,3,1,1,[(9,0,0,0,1,U,U,0),(9,0,0,0,1,U,U,1),(2,2,20,255,8,0,1,0)],typ=2),68), |
| 377 | ('cpp-div-op5',unit(5,7,0,1,[(9,0,0,0,1,U,U,0),(9,0,0,0,1,U,U,1),(5,1,0,0,3,0,1,0)],ver=2),68), |
| 378 | ('go-div-op5',unit(7,9,0,1,[(9,0,0,0,1,U,U,0),(9,0,0,0,1,U,U,1),(5,1,0,0,3,0,1,0)],ver=2),68), |
| 379 | ('c-div-op5',unit(1,1,0,1,[(9,0,0,0,1,U,U,0),(9,0,0,0,1,U,U,1),(5,1,0,0,3,0,1,0)]),68), |
| 380 | ('slot-gap',unit(5,7,0,1,[(9,0,0,0,1,U,U,1)],ver=2),68), |
| 381 | ('v1-header-profile7',unit(5,7,0,1,[(9,0,0,0,1,U,U,0)],ver=1),68), |
| 382 | ('v2-header-profile1',unit(1,1,0,1,[(9,0,0,0,1,U,U,0)],ver=2),68), |
| 383 | ('language-profile-mismatch',semonly(bytes(gsem[:6])+b'\x08'+gsem[7:]),68), |
| 384 | ('cpp-obligation-5-decoder-72',unit(5,7,0,1,[(9,0,0,0,1,U,U,0),(2,1,0,0,5,0,0,0)],ver=2),65), |
| 385 | ('shared-ops-over-capacity',shared(6),67),('depth-over-limit',unit(5,7,0,1,deep,ver=2),67),('size-over-limit',shared(9),67)] |
| 386 | xrows=[] |
| 387 | for name,env,want in xr: |
| 388 | it={'name':name,'want':want} |
| 389 | if name in ('profile3-bigint','cpp-div-op5','go-div-op5','c-div-op5','slot-gap','shared-ops-over-capacity','depth-over-limit','size-over-limit'): |
| 390 | it['codec_decode']=('PASS' if sa('decode',env).returncode==0 else 'FAIL') if codec=='RUN' else 'NOT_RUN' |
| 391 | r=xexport(env);ok=r.returncode==want and not r.stdout and r.stderr.startswith(b'CPP_EXPORT_') and it.get('codec_decode','PASS')!='FAIL' |
| 392 | it.update(exit=r.returncode,stderr=r.stderr.decode(errors='replace').strip(),result='PASS' if ok else 'FAIL') |
| 393 | if not ok:failures.append(it) |
| 394 | xrows.append(it) |
| 395 | # Accepted exporter boundaries: shared SSA expanded to exactly 64 re-import operations; a right-nested chain of 17 adds |
| 396 | # exports with parenthesis depth exactly 16 (18 adds: depth 17, refused above). |
| 397 | exact=[(9,0,0,0,1,U,U,0)]+[(2,1,0,0,1,j,j,0) for j in range(5)]+[(2,1,0,0,1,5,4,0),(2,1,0,0,1,6,3,0),(2,1,0,0,1,7,2,0),(2,1,0,0,1,8,1,0)] |
| 398 | for name,env,want_ops in (('shared-ops-at-capacity',unit(5,7,0,1,exact,ver=2),64),('depth-at-limit',unit(5,7,0,1,deep[:18],ver=2),20), |
| 399 | ('zig-member-depth-at-limit',unit(6,8,0,1,deep[:18],ver=2),20),('python-member-shared',unit(3,4,0,1,[(9,0,0,0,1,U,U,0)]+[(2,1,0,0,5,j,j,0) for j in range(4)]),18)): |
| 400 | it={'name':name} |
| 401 | try: |
| 402 | sem,_=split(env);ops=recs(sem);r=xexport(env);check(r.returncode==0,name+' refused rc=%d %s'%(r.returncode,r.stderr)) |
| 403 | q=ximport(r.stdout);check(q.returncode==0,name+' re-import refused');_,_,ops2=decode(q.stdout,'Cpp') |
| 404 | check(len(ops2)==want_ops,name+' re-import operation count %d'%len(ops2)) |
| 405 | vec=vectors(1,rng);ref=[evaluate(ops,v) for v in vec] |
| 406 | check([evaluate(ops2,v) for v in vec]==ref and O.values('cpp',r.stdout,'calc',vec)==ref,name+' evaluation') |
| 407 | it.update(result='PASS',reimport_ops=len(ops2),identity_equal=N(strip(split(q.stdout)[0]))==N(strip(sem))) |
| 408 | except (Fail,ValueError,struct.error) as e:it.update(result='FAIL',failure=str(e));failures.append(it) |
| 409 | xrows.append(it) |
| 410 | |
| 411 | # ---- composition C -> C++: F1 positives and the four accepted C import boundary controls ---- |
| 412 | ch=b'#include <stdint.h>\nuint64_t calc(uint64_t a,uint64_t b){return a+b;}' |
| 413 | cctl=[('C-exact-source-capacity',ch+b' '*(4096-len(ch)),'calc',2),('C-comment-at-capacity',ch+b'//'+b'x'*(4096-len(ch)-2),'calc',2), |
| 414 | ('C-operation-capacity',b'#include <stdint.h>\nuint64_t calc(uint64_t a,uint64_t b){return '+b'+'.join([b'a']*61)+b';}','calc',2), |
| 415 | ('C-nesting-limit',b'#include <stdint.h>\nuint64_t calc(uint64_t a,uint64_t b){return '+b'('*16+b'a'+b')'*16+b';}','calc',2)] |
| 416 | comp=[] |
| 417 | for tag,csrc,name,nargs in [(p['id'],p['source'].encode(),p['name'],p['inputs']) for p in f1['positives']]+cctl: |
| 418 | it={'id':'C->C++ '+tag} |
| 419 | try: |
| 420 | c=cimport(csrc);check(c.returncode==0,'C import refused') |
| 421 | sem_a,_,ops_a=decode(c.stdout,'C') |
| 422 | src_b=export_checks(sem_a,c.stdout,tag) |
| 423 | sem_b,ops_b=reimport_identity(sem_a,src_b,tag) |
| 424 | vec=vectors(nargs,rng);ref=[evaluate(ops_a,v) for v in vec] |
| 425 | check(O.values('c',csrc,name,vec)==ref,'cc(original C) != reference(U_A)') |
| 426 | check(O.values('cpp',src_b,'calc',vec)==ref,'g++(src_B) != reference(U_A)') |
| 427 | check([evaluate(ops_b,v) for v in vec]==ref,'reference(U_B) != reference(U_A)') |
| 428 | it.update(native=native_check(sem_a,vec,ref)) |
| 429 | it.update(result='PASS',identity=identity(N(strip(sem_a))),vectors=len(vec)) |
| 430 | except (Fail,ValueError,struct.error) as e:it.update(result='FAIL',failure=str(e));failures.append(it) |
| 431 | comp.append(it) |
| 432 | |
| 433 | # ---- composition C++ -> C: export_C(R(U_A, C)) with the v1-core G1 C exporter (translation-compose-v2 section 3) ---- |
| 434 | rc_rows=[] |
| 435 | ctl_cpp=[(c['name'],src) for (c,(name,src,want,_)) in zip(ctl,controls) if want==0] |
| 436 | for tag,src,nargs,orig_name in [(fx['id'],fx['source'].encode(),fx['inputs'],fx['name']) for fx in f2['positives']]+[('cpp-'+n,s,2,'calc') for n,s in ctl_cpp]: |
| 437 | it={'id':'C++->C '+tag} |
| 438 | try: |
| 439 | r=ximport(src);check(r.returncode==0,'C++ import refused');sem_a,_,ops_a=decode(r.stdout,'Cpp') |
| 440 | direct=py(CEXPORT,r.stdout);check(direct.returncode!=0 and not direct.stdout,'v1-core c-export accepted a v2 unit');bump('c_export_v2_refusal_status_%d'%direct.returncode) |
| 441 | q=py(CEXPORT,R(r.stdout,sem_a,'C'));check(q.returncode==0,'c-export refused R(U,C) rc=%d %s'%(q.returncode,q.stderr)) |
| 442 | c=cimport(q.stdout);check(c.returncode==0,'C import of exported C refused') |
| 443 | sem_b,_,ops_b=decode(c.stdout,'C');check(N(strip(sem_b))==N(strip(sem_a)),'identity N(strip(U_B)) != N(strip(U_A))') |
| 444 | vec=vectors(nargs,rng);ref=[evaluate(ops_a,v) for v in vec] |
| 445 | check(O.values('cpp',src,orig_name,vec)==ref,'g++(original C++) != reference(U_A)') |
| 446 | check(O.values('c',q.stdout,'calc',vec)==ref,'cc(src_B) != reference(U_A)') |
| 447 | check([evaluate(ops_b,v) for v in vec]==ref,'reference(U_B) != reference(U_A)') |
| 448 | it.update(native=native_check(sem_a,vec,ref)) |
| 449 | it.update(result='PASS',vectors=len(vec),exported_c=q.stdout.decode()[:400]) |
| 450 | except (Fail,ValueError,struct.error) as e:it.update(result='FAIL',failure=str(e));failures.append(it) |
| 451 | rc_rows.append(it) |
| 452 | |
| 453 | def sha(p): |
| 454 | p=Path(p);return hashlib.sha256(p.read_bytes()).hexdigest() if p.is_file() else None |
| 455 | nat_pass=sum(1 for r in comp+rc_rows if r.get('native')=='PASS') |
| 456 | report={'schema':'omega.cpp-u64.selftest.v1','result':'FAIL' if failures else 'PASS','status_flags':'TESTED (author self-test), not QUALIFIED', |
| 457 | 'evidence':'REFERENCE + DIFFERENTIAL (g++/cc and the native realization, NATIVE AArch64); no PROOF','source_commit':subprocess.check_output(['git','-C',str(ROOT),'rev-parse','HEAD'],text=True).strip(), |
| 458 | 'build':json.loads((ROOT/'build/L739/build.json').read_text()),'gxx':subprocess.check_output([a.cxx,'--version'],text=True).splitlines()[0],'gxx_flags':CXXFLAGS, |
| 459 | 'cc':subprocess.check_output([a.cc,'--version'],text=True).splitlines()[0], |
| 460 | 'c_import_sha256':sha(cimp),'c_export_sha256':sha(ROOT/'build/L622/c-export'),'lower_sha256':sha(ROOT/'build/L831/lower'), |
| 461 | 'retarget':('TOOL %s (cross-checked against test-code R)'%a.retarget) if a.retarget else 'test-code R only (retarget tool not given)', |
| 462 | 'retarget_build':json.loads((Path(a.retarget).resolve().parents[2]/'build/L833/build.json').read_text()) if a.retarget else None, |
| 463 | 'codec_v2':codec,'codec_v2_sa_sha256':sha(sa_exe),'codec_v2_source_sha256':sha(ROOT/'src/source-aware/core-v2.om'), |
| 464 | 'native_realization':NAT.status,'native_realization_reason':NAT.reason,'native_realized_units':len(NAT.cache),'native_vectors':NAT.vectors, |
| 465 | 'positives':len(rows),'positives_passed':sum(r['result']=='PASS' for r in rows),'not_in_profile':len(f2['not_in_profile']), |
| 466 | 'refusals':len(refusals),'refusals_passed':sum(r['result']=='PASS' for r in refusals), |
| 467 | 'controls':len(ctl),'controls_passed':sum(r['result']=='PASS' for r in ctl),'export_cases':len(xrows),'export_cases_passed':sum(r['result']=='PASS' for r in xrows), |
| 468 | 'c_to_cpp':len(comp),'c_to_cpp_passed':sum(r['result']=='PASS' for r in comp),'cpp_to_c':len(rc_rows),'cpp_to_c_passed':sum(r['result']=='PASS' for r in rc_rows), |
| 469 | 'native_checks_passed':nat_pass,'oracle_compiles':O.compiles,'counts':counts, |
| 470 | 'cases':rows,'refusal_cases':refusals,'control_cases':ctl,'export_rows':xrows,'c_to_cpp_cases':comp,'cpp_to_c_cases':rc_rows,'failures':failures} |
| 471 | (out/'result.json').write_text(json.dumps(report,indent=1)+'\n') |
| 472 | keys=['positives','positives_passed','not_in_profile','refusals','refusals_passed','controls','controls_passed','export_cases','export_cases_passed', |
| 473 | 'c_to_cpp','c_to_cpp_passed','cpp_to_c','cpp_to_c_passed','native_realization','native_checks_passed','native_vectors','codec_v2','retarget','oracle_compiles'] |
| 474 | print(report['result'],' '.join('%s=%s'%(k,report[k]) for k in keys),' '.join('%s=%s'%kv for kv in counts.items())) |
| 475 | for f in failures[:20]:print('FAIL',json.dumps(f)[:600]) |
| 476 | return 1 if failures else 0 |
| 477 | |
| 478 | if __name__=='__main__':raise SystemExit(main()) |