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 / seed / json / tests / field.py

206 lines · 8213 bytes Raw Blame History
1#!/usr/bin/env python3
2# Test instrument only. Invoke raw function-mode Omega using its documented pointer/length slots.
3# Sparse anonymous mappings supply real 4 GiB source views, touching only first/last pages. This is
4# host-ABI evidence, not a claim that a seeded-Omega caller can construct/pass that source today.
5def gate_require(condition, detail):
6 if not condition:
7 raise RuntimeError(detail)
8
9import argparse
10import ctypes as C
11import json
12import mmap
13from pathlib import Path
14import subprocess
15import sys
16
17MAX = 4294967295 # uint tape field width, from json.om/jsmaxn
18WRAPPERS = {
19'wsrc': '''fn qb_wsrc s view uchar * p ulong to long
20ob = new uchar 1045 0
21r = js_winit ob
22js_wsrc ob s p
23ret js_wdone ob
24end
25''',
26'stop-copy': '''fn qb_stop s view uchar * to long
27ob = new uchar 1041 0
28r = js_winit ob
29js_wcopy ob s 0 len s
30ret js_wdone ob
31end
32''',
33'stop-string': '''fn qb_stop s view uchar * to long
34ob = new uchar 1041 0
35r = js_winit ob
36js_wsbuf ob s 0 len s
37ret js_wdone ob
38end
39''',
40'parse': '''fn qb_parse s view uchar * t edit uint * n ulong to long
41ret js_parse s t n
42end
43''',
44'reset': '''fn qb_reset t edit uint * n ulong to long
45js_reset t n
46ret long js_hg t jsherr
47end
48''',
49'end': '''fn qb_end s view uchar * p ulong to ulong
50ret js_numend s p len s
51end
52''',
53'float': '''fn qb_float s view uchar * p ulong to ulong
54fs = new long jsfs 0
55x = js_f64 s p fs
56if ne js_f64st fs 0
57ret 0
58end
59ret bitcast ulong x
60end
61''',
62'write': '''fn qb_write s view uchar * p ulong to long
63ob = new uchar 1042 0
64r = js_winit ob
65js_wlex ob s p
66r = js_wdone ob
67if ne r 1
68ret r
69end
70ret long get ob jswh
71end
72''',
73}
74
75def native(binary, op):
76 code = Path(binary).read_bytes()
77 executable = mmap.mmap(-1, len(code), prot=mmap.PROT_READ|mmap.PROT_WRITE|mmap.PROT_EXEC)
78 executable.write(code)
79 address = C.addressof(C.c_char.from_buffer(executable))
80 cache = C.CDLL('libgcc_s.so.1').__clear_cache
81 cache.argtypes = [C.c_void_p,C.c_void_p]
82 cache(C.c_void_p(address),C.c_void_p(address+len(code)))
83 if op.startswith('stop-'):
84 # Guard-page read probe: the view spans an actual mapping, but all except its first
85 # two source bytes are PROT_NONE. Exhausted output must stop before reading them.
86 src = mmap.mmap(-1, 2097152, prot=mmap.PROT_READ|mmap.PROT_WRITE)
87 pointer = C.addressof(C.c_char.from_buffer(src))
88 page = mmap.PAGESIZE
89 src[page-2:page] = b'xx'
90 libc = C.CDLL(None)
91 libc.mprotect.argtypes = [C.c_void_p,C.c_size_t,C.c_int]
92 gate_require(libc.mprotect(pointer+page,2097152-page,0) == 0, 'gate condition failed')
93 f = C.CFUNCTYPE(C.c_int64,C.c_void_p,C.c_uint64)(address)
94 print(f(pointer+page-2,1000001),flush=True)
95 return
96 if op in ('esc','uesc','esc-closed'):
97 # The independent reviewer's full reader probe: one real MAX-byte string, not a
98 # synthetic helper result. Each byte is initialized; the parse must reach the field end.
99 src = mmap.mmap(-1,MAX+1,prot=mmap.PROT_READ|mmap.PROT_WRITE)
100 pointer = C.addressof(C.c_char.from_buffer(src))
101 libc = C.CDLL(None)
102 libc.memset.argtypes = [C.c_void_p,C.c_int,C.c_size_t]
103 libc.memset.restype = C.c_void_p
104 libc.memset(pointer,ord('a'),MAX)
105 src[0] = ord('"')
106 if op=='esc':
107 src[MAX-2:MAX] = b'\\n'
108 elif op=='uesc':
109 src[MAX-6:MAX] = b'\\u0041'
110 else:
111 src[MAX-3:MAX] = b'\\n"'
112 tape=(C.c_uint32*20)()
113 tape[14],tape[8],tape[12],tape[15]=MAX,1,1,788
114 f=C.CFUNCTYPE(C.c_int64,C.c_void_p,C.c_uint64,C.c_void_p,C.c_uint64,C.c_uint64)(address)
115 result=f(pointer,MAX+1,tape,20,MAX)
116 print(json.dumps([result,tape[0],tape[1],tape[3],tape[6],tape[7]]),flush=True)
117 return
118 if op.startswith('wsrc'):
119 # The reviewer's sparse writer probe, at MAX and one byte earlier.
120 src=mmap.mmap(-1,MAX+1,prot=mmap.PROT_READ|mmap.PROT_WRITE)
121 pointer=C.addressof(C.c_char.from_buffer(src))
122 off=MAX-3 if op=='wsrc' else MAX-4
123 src[off:off+4]=b'"\\n"'
124 f=C.CFUNCTYPE(C.c_int64,C.c_void_p,C.c_uint64,C.c_uint64)(address)
125 print(f(pointer,MAX+1,off),flush=True)
126 return
127 if op.startswith('parse') or op.startswith('reset'):
128 tape = (C.c_uint32*20)()
129 tape[14],tape[8],tape[12],tape[15] = MAX,1,1,788
130 # Deliberately stale successful state must be invalidated on every refusal.
131 tape[0],tape[3] = 1,1
132 if op.startswith('reset'):
133 f = C.CFUNCTYPE(C.c_int64,C.c_void_p,C.c_uint64,C.c_uint64)(address)
134 result = f(tape,20,MAX+(op=='reset-above'))
135 else:
136 src = mmap.mmap(-1,MAX+1,prot=mmap.PROT_READ|mmap.PROT_WRITE)
137 src[0] = ord('x')
138 pointer = C.addressof(C.c_char.from_buffer(src))
139 f = C.CFUNCTYPE(C.c_int64,C.c_void_p,C.c_uint64,C.c_void_p,C.c_uint64,C.c_uint64)(address)
140 result = f(pointer,MAX+1,tape,20,MAX+(op=='parse-above'))
141 print(json.dumps([result,tape[0],tape[1],tape[3],tape[6],tape[7]]),flush=True)
142 return
143 src = mmap.mmap(-1,MAX,prot=mmap.PROT_READ|mmap.PROT_WRITE)
144 src[MAX-1] = ord('1') if not op.endswith('bad') else ord('-')
145 pointer = C.addressof(C.c_char.from_buffer(src))
146 f = C.CFUNCTYPE(C.c_uint64,C.c_void_p,C.c_uint64,C.c_uint64)(address)
147 print(f(pointer,MAX,MAX-1),flush=True)
148
149
150def field_cases(large_scans=True):
151 cases = [
152 ('stop-copy','stop-copy',-21),('stop-string','stop-string',-21),
153 ('wsrc','wsrc',4),('wsrc-earlier','wsrc',4),
154 ('parse-at','parse',[-2,0,MAX,0,2,0]),
155 ('parse-above','parse',[-5,0,0,0,5,MAX]),
156 ('reset-at','reset',[0,0,MAX,0,0,0]),
157 ('reset-above','reset',[5,0,0,0,5,MAX]),
158 ('end','end',MAX),('end-bad','end',0),
159 ('float','float',4607182418800017408),('write','write',49),
160 ]
161 if large_scans:
162 cases += [('esc','parse',[-1,0,MAX,0,1,MAX]),
163 ('uesc','parse',[-1,0,MAX,0,1,MAX]),
164 ('esc-closed','parse',[1,1,MAX,1,0,0])]
165 return cases
166
167
168def check(omc, directory, units, wrong_expectation=False, large_scans=True):
169 directory = Path(directory)
170 directory.mkdir(parents=True,exist_ok=True)
171 for name,wrapper in WRAPPERS.items():
172 program = 'om2\n'+''.join(units[u] for u in ('json.om','jsonw.om','jsonf.om'))+wrapper
173 (directory/(name+'.om')).write_text(program)
174 r = subprocess.run([str(Path(omc).resolve())],input=program.encode(),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
175 (directory/(name+'.err')).write_bytes(r.stderr)
176 gate_require(r.returncode == 0, (name,r.returncode,r.stderr))
177 (directory/name).write_bytes(r.stdout)
178 cases = field_cases(large_scans)
179 if wrong_expectation:
180 cases[0]=('stop-copy','stop-copy',-22)
181 failures=[]
182 for op,unit,want in cases:
183 r = subprocess.run([sys.executable,__file__,'--native',str(directory/unit),op],stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=120)
184 got = json.loads(r.stdout) if r.returncode == 0 else None
185 ok = r.returncode == 0 and got == want
186 print(('PASS' if ok else 'FAIL')+' field/'+op+' status='+str(r.returncode)+' got='+repr(got)+' want='+repr(want),flush=True)
187 if not ok:
188 failures.append(op)
189 return failures
190
191if __name__ == '__main__':
192 if len(sys.argv)==4 and sys.argv[1]=='--native':
193 native(sys.argv[2],sys.argv[3])
194 else:
195 p=argparse.ArgumentParser()
196 p.add_argument('omc');p.add_argument('out');p.add_argument('--ref')
197 p.add_argument('--wrong-expectation',action='store_true')
198 p.add_argument('--no-large-scans',action='store_true')
199 a=p.parse_args()
200 j=Path(__file__).resolve().parent.parent
201 if a.ref:
202 units={u:subprocess.check_output(['git','-C',str(j),'show',a.ref+':seed/json/'+u],text=True) for u in ('json.om','jsonw.om','jsonf.om')}
203 else:
204 units={u:(j/u).read_text() for u in ('json.om','jsonw.om','jsonf.om')}
205 raise SystemExit(1 if check(a.omc,a.out,units,a.wrong_expectation,not (a.no_large_scans or a.wrong_expectation)) else 0)
206