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 / seed / omc / tests / objects / check.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Independent native-object parser/oracle. This is a test instrument, not a linker.""" |
| 3 | import os, struct, subprocess, sys, tempfile |
| 4 | from pathlib import Path |
| 5 | ROOT = Path(__file__).resolve().parents[4] |
| 6 | OMC = Path(sys.argv[1]).resolve() if len(sys.argv)>1 else ROOT/"seed/out/omc" |
| 7 | def compile_source(source, status=0): |
| 8 | p = subprocess.run([OMC], input=source.encode(), capture_output=True) |
| 9 | assert p.returncode == status, (status,p.returncode,p.stderr.decode()) |
| 10 | if status: |
| 11 | assert not p.stdout, ("refusal emitted object",p.stdout[:32]) |
| 12 | return p.stdout |
| 13 | def parse(data): |
| 14 | def u64(off): |
| 15 | assert 0 <= off <= len(data)-8, ("truncated u64",off) |
| 16 | return struct.unpack_from("<Q",data,off)[0] |
| 17 | assert data[:8] == b"OMOBJ001" |
| 18 | version,target,release,start,end,nf,nc,ne = [u64(x) for x in range(8,72,8)] |
| 19 | assert version==1 and target==1 and release in (0,1), "object-target-contract" |
| 20 | assert start==176 and start<=end<=len(data) and end%4==0 |
| 21 | assert data[72:176] == bytes(104) |
| 22 | p=end; funcs=[] |
| 23 | for f in range(nf): |
| 24 | flags,addr,ret,np,stack,nlen=[u64(p+i*8) for i in range(6)]; p+=48 |
| 25 | assert flags in (0,1,2) and 1<=ret<=13 and np<=6 |
| 26 | assert stack%16==0 and nlen>0 |
| 27 | assert p+nlen<=len(data) |
| 28 | name=data[p:p+nlen].decode("ascii");p+=nlen |
| 29 | pad=(-p)%8;assert data[p:p+pad]==bytes(pad);p+=pad |
| 30 | params=[] |
| 31 | for j in range(np): |
| 32 | t,k,n,layout,fresh=[u64(p+i*8) for i in range(5)];p+=40 |
| 33 | assert t in tuple(range(1,12))+(13,) and k in (0,2,3) and layout==0 |
| 34 | assert fresh in (0,1) |
| 35 | params.append((t,k,n,layout,fresh)) |
| 36 | assert (addr==0 and stack==0) if flags==2 else (start<=addr<end and addr%4==0) |
| 37 | funcs.append(dict(flags=flags,addr=addr,ret=ret,stack=stack,name=name,params=params)) |
| 38 | assert len({f["name"] for f in funcs})==nf |
| 39 | calls=[] |
| 40 | for c in range(nc): |
| 41 | site,src,dst,cost=[u64(p+i*8) for i in range(4)];p+=32 |
| 42 | assert start<=site<=end-4 and site%4==0 and src<nf and dst<nf and cost%16==0 |
| 43 | assert funcs[src]["flags"]!=2 |
| 44 | word=struct.unpack_from("<I",data,site)[0] |
| 45 | assert word>>26==0b100101 |
| 46 | if funcs[dst]["flags"]==2: |
| 47 | assert word==0x94000000, ("external call must be unresolved",hex(word)) |
| 48 | else: |
| 49 | disp=word&0x3ffffff |
| 50 | if disp&(1<<25):disp-=1<<26 |
| 51 | assert site+4*disp==funcs[dst]["addr"] |
| 52 | calls.append((site,src,dst,cost)) |
| 53 | assert p==len(data),("trailing bytes",p,len(data)) |
| 54 | exports=[f for f in funcs if f["flags"]==1] |
| 55 | assert len(exports)==ne |
| 56 | for i,f in enumerate(exports): |
| 57 | site=start+4*i;word=struct.unpack_from("<I",data,site)[0] |
| 58 | assert word>>26==0b000101 |
| 59 | disp=word&0x3ffffff |
| 60 | if disp&(1<<25):disp-=1<<26 |
| 61 | assert site+disp*4==f["addr"] |
| 62 | return funcs,calls,(release,start,end) |
| 63 | scalar="""om2 |
| 64 | fn helper a int to int |
| 65 | ret add a 1 |
| 66 | end |
| 67 | export fn run a int to int |
| 68 | ret helper a |
| 69 | end |
| 70 | """ |
| 71 | array="""om2 |
| 72 | export fn readone a view uchar * to uchar |
| 73 | ret get a 0 |
| 74 | end |
| 75 | export fn writeone a edit uchar * x uchar to void |
| 76 | put a 0 x |
| 77 | ret |
| 78 | end |
| 79 | """ |
| 80 | stack="""om2 |
| 81 | fn leaf a view uchar * to uchar |
| 82 | ret get a 0 |
| 83 | end |
| 84 | export fn run to uchar |
| 85 | a = new uchar 33 7 |
| 86 | ret leaf a |
| 87 | end |
| 88 | """ |
| 89 | count=0 |
| 90 | for source in [scalar,array,stack,scalar.replace("om2","om2 release")]: |
| 91 | obj=compile_source("object\n"+source) |
| 92 | funcs,calls,h=parse(obj) |
| 93 | assert obj[h[1]:h[2]]==compile_source(source), "object payload differs from existing native compiler" |
| 94 | assert obj==compile_source("object\n"+source), "nondeterministic object" |
| 95 | count+=1 |
| 96 | fs,cs,h=parse(compile_source("object\n"+stack)) |
| 97 | # A function's record is stkbytes plus its largest transient push (save areas, host buffers), an |
| 98 | # upper bound on the deepest SP its own code reaches, callees excluded; here: frame 48 plus the call's save area. |
| 99 | assert cs[0][3]>48 and fs[1]["stack"]==cs[0][3] and fs[0]["stack"]==0, "object-stack-contract" |
| 100 | imports="""object |
| 101 | om2 |
| 102 | extern fn dep a int to int |
| 103 | export fn run a int to int |
| 104 | ret dep a |
| 105 | end |
| 106 | """ |
| 107 | fs,cs,h=parse(compile_source(imports)) |
| 108 | assert [f["flags"] for f in fs]==[2,1] and fs[0]["name"]=="dep" |
| 109 | assert fs[0]["params"]==[(8,0,0,0,0)] and cs[0][1:3]==(1,0) |
| 110 | count+=1 |
| 111 | many="object\nom2\n"+"".join("export fn entry%d to int\nret %d\nend\n"%(i,i) for i in range(80)) |
| 112 | assert len(parse(compile_source(many))[0])==80 |
| 113 | count+=1 |
| 114 | # These boundaries must refuse rather than encoding untyped or effectful imports. |
| 115 | refusals=[ |
| 116 | ("object\nom2\nexport fn run to long\nret mapdrop 1\nend\n",9), |
| 117 | ("object\nfn main to int\nret 0\nend\n",16), |
| 118 | (imports.replace("dep a","dep a b",1),16), |
| 119 | (imports.replace("extern fn dep a int to int","extern fn dep a int to int inline"),9), |
| 120 | ("object\nom2\nextern fn dep a edit uchar 4 fresh to void\nexport fn run to int\nret 0\nend\n",27), |
| 121 | ("object\nom2\nextern fn dep a int to int\nextern fn dep a int to int\n",10), |
| 122 | ("object\nom2\nglobal g uchar 1\nexport fn run to uchar\nret get g 0\nend\n",9), |
| 123 | ("object\nom2\ntext g \"a\"\nexport fn run to uchar\nret get g 0\nend\n",9), |
| 124 | ("object\nom2\nexport fn run to int\nret run\nend\n",9), |
| 125 | ("object\nom2\nextern fn dep a int to int\nexport fn run to int\nret dep ulong 3\nend\n",7), |
| 126 | ("object\nom2\nextern fn dep a view Missing to int\n",16), |
| 127 | ] |
| 128 | for source,status in refusals: |
| 129 | compile_source(source,status);count+=1 |
| 130 | # Independent parser control: bad target, trailing bytes, changed relocation, and truncation all fail. |
| 131 | data=compile_source(imports) |
| 132 | fs,cs,h=parse(data) |
| 133 | mutants=[] |
| 134 | m=bytearray(data);struct.pack_into("<Q",m,16,99);mutants.append(m) |
| 135 | mutants.extend([data+b"\0",data[:-1]]) |
| 136 | m=bytearray(data);struct.pack_into("<I",m,cs[0][0],0x94000001);mutants.append(m) |
| 137 | for m in mutants: |
| 138 | try:parse(m) |
| 139 | except (AssertionError,UnicodeDecodeError):pass |
| 140 | else:raise AssertionError("invalid object survived parser control") |
| 141 | print("OBJECT-ORACLE PASS cases=%d controls=%d"%(count,len(mutants))) |