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