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 / c_import_gate.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Independent development gates for the bounded native C expression importer.""" |
| 3 | from pathlib import Path |
| 4 | import argparse,hashlib,json,os,random,struct,subprocess,tempfile |
| 5 | ROOT=Path(__file__).resolve().parents[2] |
| 6 | HEADER='#include <stdint.h>\n' |
| 7 | EXPRESSIONS=['a','b','a+b','a-b','a*b','(a+b)*(a-b)','a+0ULL','a-1ULL','a*2ULL','0ULL-a','18446744073709551615ULL+a','a*b+3ULL','a+a+b','a+(a+b)','a-b-b','a-(b-b)','a*b*a','a*(b*a)','123ULL','((a))','(a+b)*(b+1ULL)','a+b*2ULL','(a+b)*2ULL','a-2ULL*b','(a-2ULL)*b','a+18446744073709551615ULL*b','2ULL*3ULL+4ULL','2ULL*(3ULL+4ULL)','a-b+a*b','(a+a)*(b+b)'] |
| 8 | POSITIVES=[(HEADER+f'uint64_t calc(uint64_t a, uint64_t b) {{ return {e}; }}\n','calc',2) for e in EXPRESSIONS] |
| 9 | POSITIVES += [(HEADER+'uint64_t calc(void) { return 7ULL; }\n','calc',0),(HEADER+'uint64_t calc(uint64_t x) { return x; }\n','calc',1),(HEADER+'uint64_t calc(uint64_t a,uint64_t b,uint64_t c,uint64_t d,uint64_t e,uint64_t f) { return a+b+c+d+e+f; }\n','calc',6),(HEADER+'/* α comment retained */\nuint64_t calc(uint64_t left,uint64_t right) { /* keep */ return left*right; }\r\n','calc',2)] |
| 10 | POSITIVES += [(HEADER+'uint64_t omega_fold(uint64_t a,uint64_t b){return a*b+42ULL;}\n','omega_fold',2)] |
| 11 | NEGATIVE_BODIES=['uint32_t calc(uint64_t a,uint64_t b){return a+b;}','int64_t calc(uint64_t a,uint64_t b){return a+b;}','uint8_t calc(uint8_t a,uint8_t b){return a+b;}','uint64_t calc(uint64_t *a){return *a;}','uint64_t calc(uint64_t a,uint64_t b){return a/b;}','uint64_t calc(uint64_t a,uint64_t b){return a%b;}','uint64_t calc(uint64_t a,uint64_t b){return a<<b;}','uint64_t calc(uint64_t a,uint64_t b){return a>>b;}','uint64_t calc(uint64_t a,uint64_t b){return a&b;}','uint64_t calc(uint64_t a,uint64_t b){return a|b;}','uint64_t calc(uint64_t a,uint64_t b){return a^b;}','uint64_t calc(uint64_t a,uint64_t b){return a==b;}','uint64_t calc(uint64_t a,uint64_t b){return a?b:a;}','uint64_t calc(uint64_t a,uint64_t b){return -a;}','uint64_t calc(uint64_t a,uint64_t b){return a++;}','uint64_t calc(uint64_t a,uint64_t b){return ~a;}','uint64_t calc(uint64_t a,uint64_t b){if(a)return b;return a;}','uint64_t calc(uint64_t a,uint64_t b){for(;;){}return a;}','uint64_t calc(uint64_t a,uint64_t b){volatile uint64_t c=a;return c;}','uint64_t calc(uint64_t a,uint64_t b){return other(a);}','uint64_t calc(uint64_t a,uint64_t b){return sizeof(a);}','uint64_t calc(uint64_t a,uint64_t b){return (uint32_t)a;}','uint64_t calc(uint64_t a,uint64_t b){return 18446744073709551616ULL;}','uint64_t calc(uint64_t a,uint64_t b){return 1.0;}','uint64_t calc(uint64_t a,uint64_t b){return "abc";}','uint64_t calc(uint64_t a,uint64_t b){return unknown;}','uint64_t calc(uint64_t a,uint64_t a){return a;}','uint64_t calc(){return 1ULL;}','uint64_t calc(uint64_t a,uint64_t b){return a+b;} uint64_t extra(void){return 1ULL;}','uint64_t calc(uint64_t a,uint64_t b){return 2;}','uint64_t calc(uint64_t a,uint64_t b){return 1ull;}','uint64_t calc(uint64_t a,uint64_t b){return a+b','uint64_t calc(uint64_t return){return 1ULL;}','uint64_t calc(uint64_t _private){return _private;}','uint64_t calc(uint64_t a,uint64_t b,uint64_t c,uint64_t d,uint64_t e,uint64_t f,uint64_t g){return a;}'] |
| 12 | NEGATIVES=[(HEADER+b).encode() for b in NEGATIVE_BODIES]+[b'#include <stdio.h>\nuint64_t calc(void){return 1ULL;}\n',b'#define X 1\n'+HEADER.encode()+b'uint64_t calc(void){return X;}',HEADER.encode()+b'/*unterminated',HEADER.encode()+b'uint64_t calc(void){return 1ULL;}\x00',b'x'*4097] |
| 13 | # Source spelling must not smuggle octal or pre-lexical C translation phases. |
| 14 | NEGATIVES += [(HEADER+'uint64_t calc(void){'+body+'}\n').encode() for body in ['return 010ULL;','return 00ULL;','return 09ULL;','return 1ULL // comment '+chr(92)+'\n +2ULL\n;','return 1ULL // comment ??/\n +2ULL\n;']] |
| 15 | NEGATIVES += [(HEADER+body).encode() for body in ['uint64_t calc(uint64_t UINT64_MAX){return 1ULL;}','uint64_t uint32_t(void){return 1ULL;}','uint64_t malloc(void){return 1ULL;}']] |
| 16 | MASK=(1<<64)-1 |
| 17 | |
| 18 | def decode(buf,src,arity): |
| 19 | assert len(buf)>=13 and buf[:5]==b'OMSB\x01','bad envelope' |
| 20 | ns,np=struct.unpack_from('<II',buf,5);assert len(buf)==13+ns+np |
| 21 | s=buf[13:13+ns];p=buf[13+ns:] |
| 22 | assert s[:10]==b'OMSA\x01\x01\x01\x01\x00\x01','wrong C profile' |
| 23 | count=struct.unpack_from('<H',s,10)[0];assert 1<=count<=64 and len(s)==44+20*count |
| 24 | assert s[12:44]==hashlib.sha256((ROOT/'spec/universal/semantics.md').read_bytes()).digest(),'wrong semantic contract' |
| 25 | assert p[:6]==b'OMSP\x01\x00' and struct.unpack_from('<I',p,6)[0]==len(src) |
| 26 | assert p[10:42]==hashlib.sha256(s).digest() and p[42:74]==hashlib.sha256(src).digest(),'bad provenance binding' |
| 27 | assert len(p)==74+8*count+len(src) and p[74+8*count:]==src,'source replay mismatch' |
| 28 | for i in range(count): |
| 29 | lo,hi=struct.unpack_from('<II',p,74+8*i);assert 0<=lo<=hi<=len(src),'bad span' |
| 30 | ops=[];slots=[] |
| 31 | for i in range(count): |
| 32 | d,v,op,t,attr,effects,alias,obligations,aa,bb,imm=struct.unpack_from('<8BHHQ',s,44+20*i) |
| 33 | assert d==1 and v==1 and t==1 and effects==alias==0 |
| 34 | if op==9: |
| 35 | assert attr==0 and obligations==1 and aa==bb==65535 and imm<arity |
| 36 | slots.append(imm) |
| 37 | elif op==1:assert attr==1 and obligations==0 and aa==bb==65535 |
| 38 | else:assert op in [2,3,4] and attr==1 and obligations==1 and aa<i and bb<i and imm==0 |
| 39 | ops.append((op,aa,bb,imm)) |
| 40 | assert sorted(slots)==list(range(arity)),'parameter boundary changed' |
| 41 | return ops |
| 42 | |
| 43 | def evaluate(ops,args): |
| 44 | values=[] |
| 45 | for op,a,b,imm in ops: |
| 46 | if op==9:v=args[imm] |
| 47 | elif op==1:v=imm |
| 48 | elif op==2:v=values[a]+values[b] |
| 49 | elif op==3:v=values[a]-values[b] |
| 50 | else:v=values[a]*values[b] |
| 51 | values.append(v&MASK) |
| 52 | return values[-1] |
| 53 | |
| 54 | def main(): |
| 55 | ap=argparse.ArgumentParser();ap.add_argument('--importer',required=True);ap.add_argument('--out',required=True);a=ap.parse_args() |
| 56 | out=Path(a.out).resolve();out.mkdir(parents=True,exist_ok=False);exe=Path(a.importer).resolve();assert exe.read_bytes()[:4]==b'\x7fELF' |
| 57 | rows=[];failures=[];rng=random.Random(622);cc=os.environ.get('CC','cc');observations=0;sourceaware_requests=0 |
| 58 | for i,(text,name,nargs) in enumerate(POSITIVES): |
| 59 | src=text.encode();r=subprocess.run([str(exe)],input=src,capture_output=True,timeout=10) |
| 60 | item={'case':i,'source_sha256':hashlib.sha256(src).hexdigest(),'exit':r.returncode,'stderr':r.stderr.decode(errors='replace')} |
| 61 | try: |
| 62 | assert r.returncode==0, f'import refused rc={r.returncode}' |
| 63 | ops=decode(r.stdout,src,nargs) |
| 64 | legality=b''.join(bytes([3,0 if op==1 else 1]) for op,aa,bb,imm in ops) |
| 65 | for cmd,expected_bytes in [('decode',r.stdout),('replay',src),('legality',legality)]: |
| 66 | payload=(bytes([2,0,1,0]) if cmd=='legality' else b'')+r.stdout |
| 67 | sa=subprocess.run([str(ROOT/'tools/source-aware/sa'),cmd],input=payload,capture_output=True,timeout=10);sourceaware_requests+=1 |
| 68 | assert sa.returncode==0 and sa.stdout==expected_bytes,'existing source-aware '+cmd+' refused or changed imported unit: '+sa.stderr.decode(errors='replace') |
| 69 | # The independent native C compiler evaluates the actual original source. |
| 70 | c=out/f'oracle-{i}.c';binary=out/f'oracle-{i}';c.write_text(text+'\n#include <stdio.h>\n#include <inttypes.h>\nint main(void){\n'+('uint64_t x[6]={0};while(scanf("'+' '.join(['%" SCNu64 "']*nargs)+'",'+','.join('&x['+str(j)+']' for j in range(nargs))+')=='+str(nargs)+')printf("%" PRIu64 "\\n",'+name+'('+','.join('x['+str(j)+']' for j in range(nargs))+'));' if nargs else 'printf("%" PRIu64 "\\n",'+name+'());')+'return 0;}\n') |
| 71 | # Unused parameters are valid C17 and are deliberately retained to |
| 72 | # test the imported function boundary; they are not a source refusal. |
| 73 | compile_run=subprocess.run([cc,'-std=c17','-O2','-Wall','-Wextra','-Werror','-Wno-unused-parameter',str(c),'-o',str(binary)],capture_output=True,timeout=30) |
| 74 | assert compile_run.returncode==0, 'oracle compile failed: '+compile_run.stderr.decode() |
| 75 | boundary=[0,1,2,(1<<32)-1,1<<32,1<<63,MASK] |
| 76 | vectors=[tuple(boundary[(j+k)%len(boundary)] for k in range(nargs)) for j in range(len(boundary))]+[tuple(rng.getrandbits(64) for _ in range(nargs)) for _ in range(17)] if nargs else [()] |
| 77 | payload=''.join(' '.join(map(str,v))+'\n' for v in vectors).encode();native=subprocess.run([str(binary)],input=payload,capture_output=True,timeout=10) |
| 78 | assert native.returncode==0,native.stderr.decode() |
| 79 | expected=[evaluate(ops,v) for v in vectors];actual=[int(x) for x in native.stdout.splitlines()] |
| 80 | if actual!=expected: |
| 81 | item['mismatched_rows']=[{'input':list(v),'native_C':x,'upper_reference':y} for v,x,y in zip(vectors,actual,expected) if x!=y] |
| 82 | assert actual==expected,'native C versus imported semantic arithmetic mismatch' |
| 83 | item.update({'result':'PASS','ops':len(ops),'observations':len(vectors),'upper_sha256':hashlib.sha256(r.stdout).hexdigest(),'native_stdout_sha256':hashlib.sha256(native.stdout).hexdigest(),'oracle_source_sha256':hashlib.sha256(c.read_bytes()).hexdigest(),'oracle_binary_sha256':hashlib.sha256(binary.read_bytes()).hexdigest()});observations+=len(vectors) |
| 84 | (out/f'case-{i}.native.out').write_bytes(native.stdout) |
| 85 | (out/f'case-{i}.upper-values.out').write_text(''.join(str(v)+'\n' for v in expected)) |
| 86 | (out/f'case-{i}.source.c').write_bytes(src);(out/f'case-{i}.upper').write_bytes(r.stdout) |
| 87 | except (AssertionError,ValueError,IndexError,struct.error) as exc: |
| 88 | item['result']='FAIL';item['failure']=str(exc);failures.append(item) |
| 89 | rows.append(item) |
| 90 | negatives=[] |
| 91 | for i,src in enumerate(NEGATIVES): |
| 92 | r=subprocess.run([str(exe)],input=src,capture_output=True,timeout=10) |
| 93 | ok=r.returncode!=0 and r.stdout==b'' and b'C_IMPORT_' in r.stderr |
| 94 | item={'case':i,'source_sha256':hashlib.sha256(src).hexdigest(),'exit':r.returncode,'stdout_bytes':len(r.stdout),'stderr':r.stderr.decode(errors='replace'),'result':'PASS' if ok else 'FAIL'} |
| 95 | if not ok:failures.append(item) |
| 96 | negatives.append(item) |
| 97 | report={'schema':'omega.c-u64-import.development.v1','result':'FAIL' if failures else 'PASS','source_commit':subprocess.check_output(['git','-C',str(ROOT),'rev-parse','HEAD'],text=True).strip(),'classification':'DIFFERENTIAL_COMPATIBILITY','proof':False,'source_replay_verified':not failures,'positive_executed':len(rows),'positive_passed':sum(r['result']=='PASS' for r in rows),'negative_executed':len(negatives),'negative_passed':sum(r['result']=='PASS' for r in negatives),'source_aware_requests':sourceaware_requests,'source_aware_binary_sha256':hashlib.sha256((ROOT/'build/L574/sa').read_bytes()).hexdigest(),'native_C_observations':observations,'cases':rows,'negative_cases':negatives,'failures':failures,'importer_sha256':hashlib.sha256(exe.read_bytes()).hexdigest(),'test_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),'cc_flags':['-std=c17','-O2','-Wall','-Wextra','-Werror','-Wno-unused-parameter'],'cc_version':subprocess.check_output([cc,'--version'],text=True),'upstream_621_qualification':'SEPARATE_REQUIRED_GATE','not_implemented':['C exporter','u8/u16/u32','control flow','division','OSC lowering','checker-bound translation proof','x86/RV64 execution']} |
| 98 | (out/'result.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps({k:report[k] for k in ['result','positive_executed','positive_passed','negative_executed','negative_passed','native_C_observations']},indent=2));return 1 if failures else 0 |
| 99 | if __name__=='__main__':raise SystemExit(main()) |