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 / core / cortex / tests / parity / pdrive.om

529 lines · 9123 bytes Raw Blame History
1note pdrive.om: parity and load driver. usage: pdrive CORPUS PORT OUT [LAT]
2note Sends every corpus request, in order, as its own HTTP/1.1 connection to 127.0.0.1:PORT and
3note stores the raw answers. CORPUS lines are CLASS TAB METHOD TAB PATH TAB FLAGS TAB BODY (format
4note in spec/PARITY.md). OUT gets one record per request: "#<index> <bytes>\n", the raw answer
5note bytes and "\n"; a failed exchange is "#<index> ERR\n". LAT (optional) gets the latency of each
6note request in nanoseconds, one per line, measured from socket creation to the peer's close.
7note Flag a sends "authorization: Bearer paritytok", or the value of PDRIVE_TOKEN when that
8note environment variable is set (shadow replay against a server with a real token).
9global pa uchar 4096
10global cb uchar 16777216
11global bb uchar 4194304
12global rq uchar 4194304
13global rb uchar 16777216
14global sa uchar 16
15global ob uchar 64
16note pv: 0 corpus bytes, 1 port, 2 out fd, 3 lat fd (0 none), 4 body len, 5 request len, 6 answer len,
17note 7 length of the PDRIVE_TOKEN header in tk (0 none)
18global pv ulong 8
19global ev uchar 4096
20global tk uchar 4200
21text h_auth "authorization: Bearer "
22text e_tok "PDRIVE_TOKEN="
23note fl: one byte per flag letter present on the current line
24global fl uchar 128
25text u_use "usage: pdrive CORPUS PORT OUT [LAT]\n"
26text u_open "pdrive: cannot open a file\n"
27text u_big "pdrive: corpus too large\n"
28text u_bad "pdrive: malformed corpus line\n"
29text h_ver " HTTP/1.1"
30text h_host "host: 127.0.0.1:"
31text h_ok "authorization: Bearer paritytok"
32text h_bad "authorization: Bearer wrongtok"
33text h_sp "authorization: Bearer paritytok "
34text h_lc "authorization: bearer paritytok"
35text h_js "content-type: application/json"
36text h_tx "content-type: text/plain"
37text h_vnd "content-type: application/vnd.x+json"
38text h_o1 "origin: http://localhost:18095"
39text h_o2 "origin: http://127.0.0.1:18095"
40text h_ev "origin: http://evil"
41text h_pf "access-control-request-method: POST"
42text h_pf2 "access-control-request-headers: authorization,content-type"
43text h_cl "content-length: "
44text h_cc "connection: close"
45text h_err " ERR\n"
46
47fn say t view uchar * to void
48w = write 2 t 0 len t
49if lt w 0
50ret
51end
52end
53note copy argument i into pa; returns its length
54fn argp i ulong to ulong
55n = argread i pa 0 len pa
56if lt n 0
57ret 0
58end
59ret ulong n
60end
61fn opnr n ulong to int
62f = fopen pa 0 n 524288 0
63ret int f
64end
65fn opnw n ulong to int
66f = fopen pa 0 n 524865 420
67ret int f
68end
69fn rdall fd int to bool
70n = ulong 0
71while true bound 999999
72if ge n len cb
73say u_big
74ret false
75end
76r = pread fd cb n sub len cb n long n
77if lt r 0
78ret false
79end
80if eq r 0
81put pv 0 n
82ret true
83end
84n = add n ulong r
85end
86ret false
87end
88fn wall fd int a ulong n ulong to bool
89k = a
90e = add a n
91while lt k e bound 999999
92w = write fd rb k sub e k
93if le w 0
94ret false
95end
96k = add k ulong w
97end
98ret true
99end
100fn wob fd int n ulong to bool
101w = write fd ob 0 n
102ret eq w long n
103end
104fn obdec o ulong v ulong to ulong
105k = o
106if ge v 10
107k = obdec o div v 10
108end
109put ob k uchar add rem v 10 48
110ret add k 1
111end
112
113fn rqt o ulong t view uchar * to ulong
114for i ulong len t
115put rq add o i get t i
116end
117ret add o len t
118end
119fn rqcr o ulong to ulong
120put rq o 13
121put rq add o 1 10
122ret add o 2
123end
124fn rqh o ulong t view uchar * to ulong
125ret rqcr rqt o t
126end
127fn rqa o ulong to ulong
128if eq get pv 7 0
129ret rqh o h_ok
130end
131for i ulong get pv 7
132put rq add o i get tk i
133end
134ret rqcr add o get pv 7
135end
136note tokenv: find PDRIVE_TOKEN=VALUE in the environment; tk gets "authorization: Bearer VALUE"
137fn tokenv to void
138c = ulong envcnt
139for i ulong c
140m = envread i ev 0 len ev
141if lt m 14
142continue
143end
144if tokpre
145tokcp sub ulong m len e_tok
146end
147end
148end
149fn tokpre to bool
150for j ulong len e_tok
151if ne get ev j get e_tok j
152ret false
153end
154end
155ret true
156end
157fn tokcp v ulong to void
158for q ulong len h_auth
159put tk q get h_auth q
160end
161for r ulong v
162put tk add len h_auth r get ev add len e_tok r
163end
164put pv 7 add len h_auth v
165end
166fn rqdec o ulong v ulong to ulong
167k = o
168if ge v 10
169k = rqdec o div v 10
170end
171put rq k uchar add rem v 10 48
172ret add k 1
173end
174fn hexv c ulong to ulong
175if le c 57
176ret sub c 48
177end
178if le c 70
179ret sub c 55
180end
181ret sub c 87
182end
183fn hexd v ulong to uchar
184if lt v 10
185ret uchar add v 48
186end
187ret uchar add v 55
188end
189note path bytes cb[a..b) into rq at o; "~" starts hex pairs sent as %HH up to "&" or the end
190fn rqpath o ulong a ulong b ulong to ulong
191k = o
192i = a
193hx = false
194while lt i b bound 999999
195c = get cb i
196if eq c 126
197hx = true
198i = add i 1
199continue
200end
201if eq c 38
202hx = false
203end
204if hx
205v = add mul hexv ulong c 16 hexv ulong get cb add i 1
206put rq k 37
207put rq add k 1 hexd shr v 4
208put rq add k 2 hexd and v 15
209k = add k 3
210i = add i 2
211continue
212end
213put rq k c
214k = add k 1
215i = add i 1
216end
217ret k
218end
219note body bytes cb[a..b) into bb (flag e: \n \t \\ escapes); sets pv 4
220fn mkbody a ulong b ulong to void
221k = ulong 0
222es = eq get fl 101 1
223sk = false
224for j ulong sub b a
225if sk
226sk = false
227continue
228end
229i = add a j
230c = get cb i
231if and es eq c 92
232d = get cb add i 1
233if eq d 110
234c = 10
235end
236if eq d 116
237c = 9
238end
239if eq d 92
240c = 92
241end
242sk = true
243end
244put bb k c
245k = add k 1
246end
247put pv 4 k
248end
249fn rqflag o ulong to ulong
250k = o
251if eq get fl 97 1
252k = rqa k
253end
254if eq get fl 98 1
255k = rqh k h_bad
256end
257if eq get fl 115 1
258k = rqh k h_sp
259end
260if eq get fl 99 1
261k = rqh k h_lc
262end
263if eq get fl 106 1
264k = rqh k h_js
265end
266if eq get fl 116 1
267k = rqh k h_tx
268end
269if eq get fl 118 1
270k = rqh k h_vnd
271end
272ret rqflg2 k
273end
274fn rqflg2 o ulong to ulong
275k = o
276if eq get fl 111 1
277k = rqh k h_o1
278end
279if eq get fl 79 1
280k = rqh k h_o2
281end
282if eq get fl 120 1
283k = rqh k h_ev
284end
285if eq get fl 112 1
286k = rqh rqh k h_pf h_pf2
287end
288ret k
289end
290fn mkflg a ulong b ulong to void
291for i ulong 128
292put fl i 0
293end
294for i ulong sub b a
295c = get cb add a i
296put fl and ulong c 127 1
297end
298end
299note method cb[a..b) and path cb[b+1..p) as the request line plus host; returns its length
300fn mkline a ulong b ulong p ulong to ulong
301k = ulong 0
302for i ulong sub b a
303put rq k get cb add a i
304k = add k 1
305end
306put rq k 32
307k = rqpath add k 1 add b 1 p
308k = rqcr rqt k h_ver
309k = rqt k h_host
310ret rqcr rqdec k get pv 1
311end
312note build the request for a line with tabs at t1..t4 ending at e; sets pv 5
313fn mkreq t1 ulong t2 ulong t3 ulong t4 ulong e ulong to void
314mkflg add t3 1 t4
315mkbody add t4 1 e
316k = rqflag mkline add t1 1 t2 t3
317bl = get pv 4
318ispo = eq get cb add t1 1 80
319if or gt bl 0 or ispo eq get fl 108 1
320k = rqcr rqdec rqt k h_cl bl
321end
322k = rqcr rqh k h_cc
323for i ulong bl
324put rq add k i get bb i
325end
326put pv 5 add k bl
327end
328
329fn xsend fd int dl long to bool
330k = ulong 0
331e = get pv 5
332while lt k e bound 999999
333w = waitio fd 4 sub dl monons
334if le w 0
335ret false
336end
337s = send fd rq k sub e k 0
338if le s 0
339ret false
340end
341k = add k ulong s
342end
343ret true
344end
345fn xrecv fd int dl long to bool
346n = ulong 0
347while true bound 999999
348now = monons
349if ge now dl
350ret false
351end
352w = waitio fd 1 sub dl now
353if le w 0
354ret false
355end
356if ge n len rb
357ret false
358end
359r = recv fd rb n sub len rb n 0
360if lt r 0
361ret false
362end
363if eq r 0
364put pv 6 n
365ret true
366end
367n = add n ulong r
368end
369ret false
370end
371fn xchg to bool
372dl = add monons 60000000000
373sk = socknew 2 524289 0
374fd = int sk
375if lt fd 0
376ret false
377end
378p = get pv 1
379put sa 0 2
380put sa 1 0
381put sa 2 uchar shr p 8
382put sa 3 uchar and p 255
383put sa 4 127
384put sa 5 0
385put sa 6 0
386put sa 7 1
387r = connect fd sa 0 16
388ok = false
389if eq r 0
390if xsend fd dl
391ok = xrecv fd dl
392end
393end
394x = fclose fd
395ret ok
396end
397
398note emit the record for request ix
399fn emit ix ulong ok bool lat ulong to bool
400fd = int get pv 2
401put ob 0 35
402o = obdec 1 ix
403if not ok
404for i ulong len h_err
405put ob add o i get h_err i
406end
407ret wob fd add o len h_err
408end
409put ob o 32
410o = obdec add o 1 get pv 6
411put ob o 10
412if not wob fd add o 1
413ret false
414end
415n = get pv 6
416put rb n 10
417if not wall fd 0 add n 1
418ret false
419end
420lf = int get pv 3
421if eq lf 0
422ret true
423end
424o = obdec 0 lat
425put ob o 10
426ret wob lf add o 1
427end
428note tab position at or after i on the line ending at e (e when none)
429fn tabat i ulong e ulong to ulong
430for j ulong sub e i
431if eq get cb add i j 9
432ret add i j
433end
434end
435ret e
436end
437fn eolat i ulong to ulong
438n = get pv 0
439for j ulong sub n i
440if eq get cb add i j 10
441ret add i j
442end
443end
444ret n
445end
446fn runone s ulong e ulong ix ulong to int
447t1 = tabat s e
448t2 = tabat add t1 1 e
449t3 = tabat add t2 1 e
450t4 = tabat add t3 1 e
451if ge t4 e
452say u_bad
453ret 4
454end
455mkreq t1 t2 t3 t4 e
456t0 = monons
457ok = xchg
458lat = ulong sub monons t0
459if not emit ix ok lat
460ret 5
461end
462ret 0
463end
464fn runall to int
465i = ulong 0
466ix = ulong 0
467n = get pv 0
468while lt i n bound 999999
469e = eolat i
470if gt e i
471r = runone i e ix
472if ne r 0
473ret r
474end
475ix = add ix 1
476end
477i = add e 1
478end
479ret 0
480end
481fn parg n ulong to ulong
482v = ulong 0
483for k ulong n
484c = get pa k
485if or lt c 48 gt c 57
486ret 0
487end
488v = add mul v 10 ulong sub c 48
489end
490ret v
491end
492fn main to int
493c = ulong argcnt
494if or lt c 4 gt c 5
495say u_use
496ret 2
497end
498tokenv
499fd = opnr argp 1
500if lt fd 0
501say u_open
502ret 3
503end
504if not rdall fd
505ret 3
506end
507x = fclose fd
508put pv 1 parg argp 2
509if or eq get pv 1 0 gt get pv 1 65535
510say u_use
511ret 2
512end
513of = opnw argp 3
514if lt of 0
515say u_open
516ret 3
517end
518put pv 2 ulong of
519put pv 3 0
520if eq c 5
521lf = opnw argp 4
522if lt lf 0
523say u_open
524ret 3
525end
526put pv 3 ulong lf
527end
528ret runall
529end