omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / cabi / proc.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note Ported from CUDApp tests/components/proc.om at 1fae7beb (same licence): the fork, pipefds, execprog and waitpid runner subset. |
| 4 | note Concatenate before tests/cabi/gate.om; this unit has no main. |
| 5 | note Host forms checked with omega lookup at omega-mv e5b32567. |
| 6 | note Compiler SHA256 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f. |
| 7 | global HArg uchar 4096 |
| 8 | global HLen ulong 1 |
| 9 | global HCnt ulong 1 |
| 10 | global HMerge ulong 1 |
| 11 | global HFd3 ulong 1 |
| 12 | text HPFail "cabi gate refused\n" |
| 13 | |
| 14 | text HPExec "cabi gate exec refusal:" |
| 15 | text HPExp " expected=" |
| 16 | text HPSt0 " wait[0]=" |
| 17 | text HPSt1 " wait[1]=" |
| 18 | text HPSt2 " wait[2]=" |
| 19 | text HPSt3 " wait[3]=" |
| 20 | text HPPipe " exec-pipe=" |
| 21 | text HPByte " exec-byte=" |
| 22 | text HPArg " argv0=" |
| 23 | text HPNew "\n" |
| 24 | text HPMinus "-" |
| 25 | |
| 26 | fn hp_require ok bool to void |
| 27 | if not ok |
| 28 | r = write 2 HPFail 0 len HPFail |
| 29 | exit 90 |
| 30 | end |
| 31 | end |
| 32 | |
| 33 | fn hp_path a view uchar * n ulong to void |
| 34 | hp_require and gt n 0 le n len a |
| 35 | hp_require le n 4095 |
| 36 | for i ulong n |
| 37 | hp_require ne get a i 0 |
| 38 | end |
| 39 | end |
| 40 | |
| 41 | fn hp_start a view uchar * n ulong to void |
| 42 | hp_path a n |
| 43 | put HLen 0 2 |
| 44 | put HCnt 0 0 |
| 45 | put HArg 0 0 |
| 46 | put HArg 1 0 |
| 47 | hp_arg a n |
| 48 | end |
| 49 | |
| 50 | fn hp_arg a view uchar * n ulong to void |
| 51 | hp_require le n len a |
| 52 | hp_require le n 4092 |
| 53 | hp_require lt get HCnt 0 64 |
| 54 | p = get HLen 0 |
| 55 | hp_require and ge p 2 le p 4096 |
| 56 | hp_require le add n 2 sub 4096 p |
| 57 | for i ulong n |
| 58 | hp_require ne get a i 0 |
| 59 | end |
| 60 | put HArg p uchar and n 255 |
| 61 | put HArg add p 1 uchar shr n 8 |
| 62 | for i ulong n |
| 63 | put HArg add add p 2 i get a i |
| 64 | end |
| 65 | put HLen 0 add add p 2 n |
| 66 | c = add get HCnt 0 1 |
| 67 | put HCnt 0 c |
| 68 | put HArg 0 uchar and c 255 |
| 69 | put HArg 1 uchar shr c 8 |
| 70 | end |
| 71 | |
| 72 | note Retry budgets are finite; exhausting one refuses instead of reporting success. |
| 73 | fn hp_get fd int b edit uchar * off ulong n ulong to long |
| 74 | for tries ulong 64 |
| 75 | r = read fd b off n |
| 76 | if ne r -4 |
| 77 | ret r |
| 78 | end |
| 79 | end |
| 80 | hp_require false |
| 81 | ret -4 |
| 82 | end |
| 83 | |
| 84 | fn hp_put fd int b view uchar * off ulong n ulong to long |
| 85 | for tries ulong 64 |
| 86 | r = write fd b off n |
| 87 | if ne r -4 |
| 88 | ret r |
| 89 | end |
| 90 | end |
| 91 | hp_require false |
| 92 | ret -4 |
| 93 | end |
| 94 | |
| 95 | fn hp_open p view uchar * n ulong flags int mode int to int |
| 96 | hp_path p n |
| 97 | for tries ulong 64 |
| 98 | r = fopen p 0 n flags mode |
| 99 | if ne r -4 |
| 100 | hp_require ge r 0 |
| 101 | ret int r |
| 102 | end |
| 103 | end |
| 104 | hp_require false |
| 105 | ret -1 |
| 106 | end |
| 107 | |
| 108 | note On Linux close releases the descriptor even when it reports EINTR; never retry it. |
| 109 | fn hp_close fd int to void |
| 110 | r = fclose fd |
| 111 | hp_require eq r 0 |
| 112 | end |
| 113 | |
| 114 | note Fill one chunk; independent short-read boundaries cannot create false mismatches. |
| 115 | fn hp_fill fd int b edit uchar * off ulong n ulong to ulong |
| 116 | hp_require le n 4096 |
| 117 | hp_require le off len b |
| 118 | hp_require le n sub len b off |
| 119 | used = ulong 0 |
| 120 | while lt used n bound 4096 |
| 121 | r = hp_get fd b add off used sub n used |
| 122 | hp_require ge r 0 |
| 123 | if eq r 0 |
| 124 | ret used |
| 125 | end |
| 126 | hp_require le ulong r sub n used |
| 127 | used = add used ulong r |
| 128 | end |
| 129 | ret used |
| 130 | end |
| 131 | |
| 132 | fn hp_wpart fd int b view uchar * off ulong n ulong to void |
| 133 | hp_require le n 4096 |
| 134 | hp_require le off len b |
| 135 | hp_require le n sub len b off |
| 136 | used = ulong 0 |
| 137 | while lt used n bound 4096 |
| 138 | r = hp_put fd b add off used sub n used |
| 139 | hp_require gt r 0 |
| 140 | hp_require le ulong r sub n used |
| 141 | used = add used ulong r |
| 142 | end |
| 143 | end |
| 144 | |
| 145 | fn hp_write path view uchar * n ulong b view uchar * used ulong to void |
| 146 | hp_require le used len b |
| 147 | fd = hp_open path n or int 577 524288 420 |
| 148 | off = ulong 0 |
| 149 | note Each outer pass completes up to 4096 bytes, even under one-byte short writes. |
| 150 | while lt off used bound 999999 |
| 151 | step = sub used off |
| 152 | if gt step 4096 |
| 153 | step = 4096 |
| 154 | end |
| 155 | hp_wpart fd b off step |
| 156 | off = add off step |
| 157 | end |
| 158 | hp_close fd |
| 159 | end |
| 160 | |
| 161 | fn hp_read path view uchar * n ulong b edit uchar * cap ulong to ulong |
| 162 | probe = new uchar 1 0 |
| 163 | hp_require le cap len b |
| 164 | fd = hp_open path n 524288 0 |
| 165 | used = ulong 0 |
| 166 | while lt used cap bound 999999 |
| 167 | step = sub cap used |
| 168 | if gt step 4096 |
| 169 | step = 4096 |
| 170 | end |
| 171 | k = hp_fill fd b used step |
| 172 | used = add used k |
| 173 | if lt k step |
| 174 | hp_close fd |
| 175 | ret used |
| 176 | end |
| 177 | end |
| 178 | note Exact-capacity input is accepted only after a separate EOF probe. |
| 179 | r = hp_get fd probe 0 1 |
| 180 | hp_require eq r 0 |
| 181 | hp_close fd |
| 182 | ret used |
| 183 | end |
| 184 | |
| 185 | fn hp_fdok fd int to void |
| 186 | b = new uchar 128 0 |
| 187 | for tries ulong 64 |
| 188 | r = fstat fd b 0 128 |
| 189 | if ne r -4 |
| 190 | hp_require eq r 0 |
| 191 | ret |
| 192 | end |
| 193 | end |
| 194 | hp_require false |
| 195 | end |
| 196 | |
| 197 | note Copy into an unused descriptor above stderr, without changing the parent's table. |
| 198 | note Called only after fork; this single-threaded child performs no concurrent opens. |
| 199 | fn hp_save fd int to int |
| 200 | b = new uchar 128 0 |
| 201 | for cand ulong 65536 |
| 202 | if ge cand 3 |
| 203 | r = fstat int cand b 0 128 |
| 204 | if eq r -4 |
| 205 | continue |
| 206 | end |
| 207 | if eq r -9 |
| 208 | for tries ulong 64 |
| 209 | r = dupfd fd int cand 524288 |
| 210 | if ne r -4 |
| 211 | if eq r long cand |
| 212 | ret int cand |
| 213 | end |
| 214 | ret -1 |
| 215 | end |
| 216 | end |
| 217 | ret -1 |
| 218 | end |
| 219 | if ne r 0 |
| 220 | ret -1 |
| 221 | end |
| 222 | end |
| 223 | end |
| 224 | ret -1 |
| 225 | end |
| 226 | |
| 227 | note A failed child writes the exec-error pipe and exits 127, never an expected tool exit. |
| 228 | fn hp_childbad errfd int to void |
| 229 | b = new uchar 1 1 |
| 230 | for tries ulong 64 |
| 231 | r = write errfd b 0 1 |
| 232 | if eq r 1 |
| 233 | exit 127 |
| 234 | end |
| 235 | if ne r -4 |
| 236 | break |
| 237 | end |
| 238 | end |
| 239 | note If the error cannot be reported, force a signaled status that the parent refuses. |
| 240 | r = killpid int getpid 9 |
| 241 | exit 127 |
| 242 | end |
| 243 | |
| 244 | fn hp_dupto fd int target int errfd int to void |
| 245 | if eq fd target |
| 246 | ret |
| 247 | end |
| 248 | for tries ulong 64 |
| 249 | r = dupfd fd target 0 |
| 250 | if ne r -4 |
| 251 | if ne r long target |
| 252 | hp_childbad errfd |
| 253 | end |
| 254 | ret |
| 255 | end |
| 256 | end |
| 257 | hp_childbad errfd |
| 258 | end |
| 259 | |
| 260 | fn hp_child infd int outfd int rd int wr int to void |
| 261 | errfd = wr |
| 262 | if lt wr 3 |
| 263 | errfd = hp_save wr |
| 264 | if lt errfd 0 |
| 265 | hp_childbad wr |
| 266 | end |
| 267 | r = fclose wr |
| 268 | if ne r 0 |
| 269 | hp_childbad errfd |
| 270 | end |
| 271 | end |
| 272 | r = fclose rd |
| 273 | if ne r 0 |
| 274 | hp_childbad errfd |
| 275 | end |
| 276 | saved = int -1 |
| 277 | if ge infd 0 |
| 278 | saved = hp_save infd |
| 279 | if lt saved 0 |
| 280 | hp_childbad errfd |
| 281 | end |
| 282 | end |
| 283 | note Preserve stdin first, so even the input=1/output=0 swap is well-defined. |
| 284 | if ge outfd 0 |
| 285 | if eq outfd 1 |
| 286 | temp = hp_save outfd |
| 287 | if lt temp 0 |
| 288 | hp_childbad errfd |
| 289 | end |
| 290 | hp_dupto temp 1 errfd |
| 291 | r = fclose temp |
| 292 | if ne r 0 |
| 293 | hp_childbad errfd |
| 294 | end |
| 295 | else |
| 296 | hp_dupto outfd 1 errfd |
| 297 | end |
| 298 | end |
| 299 | note Port addition: HMerge 1 also sends the child's stderr to its output file, so refusal diagnostics are checked. |
| 300 | if ne get HMerge 0 0 |
| 301 | hp_dupto 1 2 errfd |
| 302 | end |
| 303 | if ge saved 0 |
| 304 | hp_dupto saved 0 errfd |
| 305 | r = fclose saved |
| 306 | if ne r 0 |
| 307 | hp_childbad errfd |
| 308 | end |
| 309 | end |
| 310 | if gt infd 2 |
| 311 | r = fclose infd |
| 312 | if ne r 0 |
| 313 | hp_childbad errfd |
| 314 | end |
| 315 | end |
| 316 | if and gt outfd 2 ne outfd infd |
| 317 | r = fclose outfd |
| 318 | if ne r 0 |
| 319 | hp_childbad errfd |
| 320 | end |
| 321 | end |
| 322 | note Port addition: HFd3 holds a descriptor + 1 (0 none) that the child receives as descriptor 3, |
| 323 | note the generator-source channel tools/bridge-seeded opens for cabi-1 provenance (error 52 without it). |
| 324 | if ne get HFd3 0 0 |
| 325 | if eq errfd 3 |
| 326 | hp_childbad errfd |
| 327 | end |
| 328 | f3 = int sub get HFd3 0 1 |
| 329 | hp_dupto f3 3 errfd |
| 330 | end |
| 331 | r = execprog HArg 0 get HLen 0 |
| 332 | hp_childbad errfd |
| 333 | end |
| 334 | |
| 335 | note Validate the complete canonical list before execprog can trap in a child. |
| 336 | fn hp_args to void |
| 337 | total = get HLen 0 |
| 338 | count = get HCnt 0 |
| 339 | hp_require and ge total 4 le total 4096 |
| 340 | hp_require and ge count 1 le count 64 |
| 341 | head = or ulong get HArg 0 shl ulong get HArg 1 8 |
| 342 | hp_require eq head count |
| 343 | p = ulong 2 |
| 344 | for record ulong count |
| 345 | hp_require le p sub total 2 |
| 346 | n = or ulong get HArg p shl ulong get HArg add p 1 8 |
| 347 | p = add p 2 |
| 348 | hp_require le n sub total p |
| 349 | if eq record 0 |
| 350 | hp_require gt n 0 |
| 351 | end |
| 352 | for i ulong n |
| 353 | hp_require ne get HArg add p i 0 |
| 354 | end |
| 355 | p = add p n |
| 356 | end |
| 357 | hp_require eq p total |
| 358 | end |
| 359 | |
| 360 | |
| 361 | fn hp_u32 b view uchar * off ulong to ulong |
| 362 | v = ulong 0 |
| 363 | for i ulong 4 |
| 364 | v = or v shl ulong get b add off i mul i 8 |
| 365 | end |
| 366 | ret v |
| 367 | end |
| 368 | |
| 369 | fn hp_wait pid long st edit uchar 4 to void |
| 370 | for tries ulong 64 |
| 371 | r = waitpid int pid st 0 4 0 |
| 372 | if ne r -4 |
| 373 | hp_require eq r pid |
| 374 | ret |
| 375 | end |
| 376 | end |
| 377 | hp_require false |
| 378 | end |
| 379 | |
| 380 | fn hp_say label view uchar * to void |
| 381 | hp_wpart 2 label 0 len label |
| 382 | end |
| 383 | |
| 384 | fn hp_diag label view uchar * value long to void |
| 385 | digits = new uchar 32 0 |
| 386 | hp_say label |
| 387 | v = value |
| 388 | if lt v 0 |
| 389 | hp_say HPMinus |
| 390 | v = neg v |
| 391 | end |
| 392 | n = hp_num ulong v digits |
| 393 | hp_wpart 2 digits 0 n |
| 394 | end |
| 395 | |
| 396 | fn hp_argv0 to void |
| 397 | hp_say HPArg |
| 398 | n = or ulong get HArg 2 shl ulong get HArg 3 8 |
| 399 | used = ulong 0 |
| 400 | while lt used n bound 4096 |
| 401 | r = long -4 |
| 402 | for tries ulong 64 |
| 403 | r = write 2 HArg add used 4 sub n used |
| 404 | if ne r -4 |
| 405 | break |
| 406 | end |
| 407 | end |
| 408 | if le r 0 |
| 409 | ret |
| 410 | end |
| 411 | used = add used ulong r |
| 412 | end |
| 413 | hp_say HPNew |
| 414 | end |
| 415 | |
| 416 | fn hp_result expected long st view uchar 4 piperc long marker uchar to void |
| 417 | ok = eq piperc 0 |
| 418 | ok = and ok eq get st 0 0 |
| 419 | ok = and ok eq get st 2 0 |
| 420 | ok = and ok eq get st 3 0 |
| 421 | ok = and ok eq long get st 1 expected |
| 422 | if not ok |
| 423 | hp_say HPExec |
| 424 | hp_diag HPExp expected |
| 425 | hp_diag HPSt0 long get st 0 |
| 426 | hp_diag HPSt1 long get st 1 |
| 427 | hp_diag HPSt2 long get st 2 |
| 428 | hp_diag HPSt3 long get st 3 |
| 429 | hp_diag HPPipe piperc |
| 430 | hp_diag HPByte long marker |
| 431 | hp_argv0 |
| 432 | end |
| 433 | hp_require ok |
| 434 | end |
| 435 | |
| 436 | fn hp_exec infd int outfd int expected long to void |
| 437 | ends = new uchar 8 0 |
| 438 | st = new uchar 4 0 |
| 439 | probe = new uchar 1 0 |
| 440 | hp_require and ge expected 0 le expected 255 |
| 441 | hp_args |
| 442 | if ge infd 0 |
| 443 | hp_fdok infd |
| 444 | end |
| 445 | if ge outfd 0 |
| 446 | hp_fdok outfd |
| 447 | end |
| 448 | r = long -4 |
| 449 | for tries ulong 64 |
| 450 | r = pipefds ends 0 8 524288 |
| 451 | if ne r -4 |
| 452 | break |
| 453 | end |
| 454 | end |
| 455 | hp_require eq r 0 |
| 456 | rd = int hp_u32 ends 0 |
| 457 | wr = int hp_u32 ends 4 |
| 458 | pid = fork |
| 459 | if eq pid 0 |
| 460 | hp_child infd outfd rd wr |
| 461 | exit 127 |
| 462 | end |
| 463 | if lt pid 0 |
| 464 | hp_close rd |
| 465 | hp_close wr |
| 466 | hp_require false |
| 467 | end |
| 468 | hp_close wr |
| 469 | hp_wait pid st |
| 470 | r = hp_get rd probe 0 1 |
| 471 | hp_close rd |
| 472 | note Error-pipe EOF and all four wait-status bytes are checked together. |
| 473 | hp_result expected st r get probe 0 |
| 474 | end |
| 475 | |
| 476 | fn hp_equal a view uchar * b view uchar * used ulong to bool |
| 477 | hp_require le used len a |
| 478 | hp_require le used len b |
| 479 | for i ulong used |
| 480 | if ne get a i get b i |
| 481 | ret false |
| 482 | end |
| 483 | end |
| 484 | ret true |
| 485 | end |
| 486 | |
| 487 | fn hp_cmp a view uchar * an ulong b view uchar * bn ulong to bool |
| 488 | left = new uchar 4096 0 |
| 489 | right = new uchar 4096 0 |
| 490 | af = hp_open a an 524288 0 |
| 491 | bf = hp_open b bn 524288 0 |
| 492 | for chunk ulong 999999 |
| 493 | ac = hp_fill af left 0 4096 |
| 494 | bc = hp_fill bf right 0 4096 |
| 495 | if ne ac bc |
| 496 | hp_close af |
| 497 | hp_close bf |
| 498 | ret false |
| 499 | end |
| 500 | if not hp_equal left right ac |
| 501 | hp_close af |
| 502 | hp_close bf |
| 503 | ret false |
| 504 | end |
| 505 | if lt ac 4096 |
| 506 | hp_close af |
| 507 | hp_close bf |
| 508 | ret true |
| 509 | end |
| 510 | end |
| 511 | hp_require false |
| 512 | ret false |
| 513 | end |
| 514 | |
| 515 | fn hp_num value ulong dst edit uchar 32 to ulong |
| 516 | rev = new uchar 32 0 |
| 517 | v = value |
| 518 | n = ulong 0 |
| 519 | while true bound 32 |
| 520 | put rev n uchar add rem v 10 48 |
| 521 | n = add n 1 |
| 522 | v = div v 10 |
| 523 | if eq v 0 |
| 524 | break |
| 525 | end |
| 526 | end |
| 527 | for i ulong n |
| 528 | put dst i get rev sub sub n 1 i |
| 529 | end |
| 530 | ret n |
| 531 | end |