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 / protocol / synapse / pulse / src / 00base.om
| 1 | note synapse 00base.om: string pool, packed argv, child exec with memfd capture, decimal and time helpers. |
| 2 | note The same file is copied, not shared, into synapse-mcp: each repo builds on its own (Unix rule). |
| 3 | global gsp uchar 8388608 |
| 4 | global gspn ulong 1 |
| 5 | global gav uchar 4096 |
| 6 | global gavs ulong 4 |
| 7 | global gxs long 4 |
| 8 | global gerrb uchar 8192 |
| 9 | global gerrn ulong 1 |
| 10 | text tmemfd "synapse-cap" |
| 11 | text tdevnul "/dev/null" |
| 12 | text tnl "\n" |
| 13 | text tfull "synapse: string pool full" |
| 14 | text tavfull "synapse: argument list over 64 records or 4096 bytes" |
| 15 | text tavz "synapse: argument holds a zero byte" |
| 16 | text targlong "synapse: argument over 4095 bytes" |
| 17 | text ttmp ".tmp" |
| 18 | |
| 19 | fn syn_die msg view uchar * code int to void |
| 20 | w = write 2 msg 0 len msg |
| 21 | w = write 2 tnl 0 1 |
| 22 | exit code |
| 23 | end |
| 24 | |
| 25 | fn syn_pb c uchar to void |
| 26 | n = get gspn 0 |
| 27 | if ge n len gsp |
| 28 | syn_die tfull 70 |
| 29 | end |
| 30 | put gsp n c |
| 31 | put gspn 0 add n 1 |
| 32 | end |
| 33 | |
| 34 | fn syn_pt t view uchar * to void |
| 35 | for i ulong len t |
| 36 | syn_pb get t i |
| 37 | end |
| 38 | end |
| 39 | |
| 40 | fn syn_pa a view uchar * o ulong n ulong to void |
| 41 | for i ulong n |
| 42 | syn_pb get a add o i |
| 43 | end |
| 44 | end |
| 45 | |
| 46 | fn syn_ps o ulong n ulong to void |
| 47 | for i ulong n |
| 48 | syn_pb get gsp add o i |
| 49 | end |
| 50 | end |
| 51 | |
| 52 | fn syn_pd v ulong to void |
| 53 | d = new uchar 24 0 |
| 54 | k = ulong 0 |
| 55 | x = v |
| 56 | while true bound 24 |
| 57 | put d k uchar add 48 rem x 10 |
| 58 | k = add k 1 |
| 59 | x = div x 10 |
| 60 | if eq x 0 |
| 61 | break |
| 62 | end |
| 63 | end |
| 64 | for i ulong k |
| 65 | syn_pb get d sub sub k i 1 |
| 66 | end |
| 67 | end |
| 68 | |
| 69 | fn syn_mark to ulong |
| 70 | ret get gspn 0 |
| 71 | end |
| 72 | |
| 73 | fn syn_rew m ulong to void |
| 74 | put gspn 0 m |
| 75 | end |
| 76 | |
| 77 | fn syn_len m ulong to ulong |
| 78 | ret sub get gspn 0 m |
| 79 | end |
| 80 | |
| 81 | note syn_seq: pool string (o,n) equals text t |
| 82 | fn syn_seq o ulong n ulong t view uchar * to bool |
| 83 | if ne n len t |
| 84 | ret false |
| 85 | end |
| 86 | for i ulong n |
| 87 | if ne get gsp add o i get t i |
| 88 | ret false |
| 89 | end |
| 90 | end |
| 91 | ret true |
| 92 | end |
| 93 | |
| 94 | note syn_peq: two pool strings are equal |
| 95 | fn syn_peq a ulong an ulong b ulong bn ulong to bool |
| 96 | if ne an bn |
| 97 | ret false |
| 98 | end |
| 99 | for i ulong an |
| 100 | if ne get gsp add a i get gsp add b i |
| 101 | ret false |
| 102 | end |
| 103 | end |
| 104 | ret true |
| 105 | end |
| 106 | |
| 107 | note syn_pre: pool string (o,n) starts with text t |
| 108 | fn syn_pre o ulong n ulong t view uchar * to bool |
| 109 | if lt n len t |
| 110 | ret false |
| 111 | end |
| 112 | for i ulong len t |
| 113 | if ne get gsp add o i get t i |
| 114 | ret false |
| 115 | end |
| 116 | end |
| 117 | ret true |
| 118 | end |
| 119 | |
| 120 | note syn_find: first index >= from of byte c in pool string (o,n), or n |
| 121 | fn syn_find o ulong n ulong from ulong c uchar to ulong |
| 122 | i = from |
| 123 | while lt i n bound 999999 |
| 124 | if eq get gsp add o i c |
| 125 | ret i |
| 126 | end |
| 127 | i = add i 1 |
| 128 | end |
| 129 | ret n |
| 130 | end |
| 131 | |
| 132 | note syn_pdec: parse a decimal pool string; gxs 2 is 1 when it was all digits |
| 133 | fn syn_pdec o ulong n ulong to ulong |
| 134 | v = ulong 0 |
| 135 | put gxs 2 1 |
| 136 | if eq n 0 |
| 137 | put gxs 2 0 |
| 138 | end |
| 139 | for i ulong n |
| 140 | c = ulong get gsp add o i |
| 141 | if or lt c 48 gt c 57 |
| 142 | put gxs 2 0 |
| 143 | ret v |
| 144 | end |
| 145 | v = add mul v 10 sub c 48 |
| 146 | end |
| 147 | ret v |
| 148 | end |
| 149 | |
| 150 | note packed argument list: u16 count, then per record u16 length and the bytes |
| 151 | fn syn_ainit to void |
| 152 | put gav 0 0 |
| 153 | put gav 1 0 |
| 154 | put gavs 0 2 |
| 155 | put gavs 1 0 |
| 156 | end |
| 157 | |
| 158 | fn syn_ahead n ulong to ulong |
| 159 | u = get gavs 0 |
| 160 | c = get gavs 1 |
| 161 | if or ge c 64 gt add add u 2 n len gav |
| 162 | syn_die tavfull 64 |
| 163 | end |
| 164 | put gav u uchar and n 255 |
| 165 | put gav add u 1 uchar shr n 8 |
| 166 | put gavs 1 add c 1 |
| 167 | put gav 0 uchar and add c 1 255 |
| 168 | put gav 1 uchar shr add c 1 8 |
| 169 | ret add u 2 |
| 170 | end |
| 171 | |
| 172 | fn syn_at t view uchar * to void |
| 173 | u = syn_ahead len t |
| 174 | for i ulong len t |
| 175 | put gav add u i get t i |
| 176 | end |
| 177 | put gavs 0 add u len t |
| 178 | end |
| 179 | |
| 180 | note syn_ap: add pool string (o,n) as one record; a zero byte is refused (execprog cannot carry it) |
| 181 | fn syn_ap o ulong n ulong to void |
| 182 | u = syn_ahead n |
| 183 | for i ulong n |
| 184 | c = get gsp add o i |
| 185 | if eq c 0 |
| 186 | syn_die tavz 65 |
| 187 | end |
| 188 | put gav add u i c |
| 189 | end |
| 190 | put gavs 0 add u n |
| 191 | end |
| 192 | |
| 193 | fn syn_rdall fd int obf edit uchar * cap ulong to ulong |
| 194 | k = ulong 0 |
| 195 | while lt k cap bound 999999 |
| 196 | r = read fd obf k sub cap k |
| 197 | if le r 0 |
| 198 | break |
| 199 | end |
| 200 | k = add k ulong r |
| 201 | end |
| 202 | ret k |
| 203 | end |
| 204 | |
| 205 | note syn_rderr: read fd into the global gerrb (a global cannot be an argument, error 19); returns bytes read |
| 206 | fn syn_rderr fd int to ulong |
| 207 | k = ulong 0 |
| 208 | while lt k len gerrb bound 999999 |
| 209 | r = read fd gerrb k sub len gerrb k |
| 210 | if le r 0 |
| 211 | break |
| 212 | end |
| 213 | k = add k ulong r |
| 214 | end |
| 215 | ret k |
| 216 | end |
| 217 | |
| 218 | note syn_run: run the packed list in gav (first record an absolute path, never a shell) |
| 219 | note child stdin /dev/null, stdout and stderr captured in memfds; out gets up to len out bytes. |
| 220 | note gxs 0: exit code 0..255, 300+signal, 400 timeout (killed), 401 fork or memfd failure |
| 221 | note gxs 1: full stdout size (may exceed len out); gerrb holds the first stderr bytes |
| 222 | fn syn_run obf edit uchar * tmo ulong to ulong |
| 223 | put gerrn 0 0 |
| 224 | m = int memfd tmemfd 0 len tmemfd 1 |
| 225 | e = int memfd tmemfd 0 len tmemfd 1 |
| 226 | if or lt m 0 lt e 0 |
| 227 | put gxs 0 401 |
| 228 | ret 0 |
| 229 | end |
| 230 | pid = fork |
| 231 | if lt pid 0 |
| 232 | put gxs 0 401 |
| 233 | ret 0 |
| 234 | end |
| 235 | if eq pid 0 |
| 236 | r = dupfd m 1 0 |
| 237 | r = dupfd e 2 0 |
| 238 | nf = fopen tdevnul 0 len tdevnul 0 0 |
| 239 | if ge nf 0 |
| 240 | r = dupfd int nf 0 0 |
| 241 | end |
| 242 | r = execprog gav 0 get gavs 0 |
| 243 | exit 127 |
| 244 | end |
| 245 | st = new uchar 4 0 |
| 246 | t0 = monons |
| 247 | code = long 400 |
| 248 | while true bound 999999 |
| 249 | r = waitpid int pid st 0 4 1 |
| 250 | if eq r long pid |
| 251 | s0 = and get st 0 127 |
| 252 | if ne s0 0 |
| 253 | code = add 300 long s0 |
| 254 | else |
| 255 | code = long get st 1 |
| 256 | end |
| 257 | break |
| 258 | end |
| 259 | if lt r 0 |
| 260 | code = 401 |
| 261 | break |
| 262 | end |
| 263 | if gt sub monons t0 long tmo |
| 264 | r = killpid int pid 9 |
| 265 | r = waitpid int pid st 0 4 0 |
| 266 | code = 400 |
| 267 | break |
| 268 | end |
| 269 | r = sleepns 2000000 |
| 270 | end |
| 271 | put gxs 0 code |
| 272 | sz = fseek m 0 2 |
| 273 | put gxs 1 sz |
| 274 | r = fseek m 0 0 |
| 275 | n = syn_rdall m obf len obf |
| 276 | r = fseek e 0 0 |
| 277 | put gerrn 0 syn_rderr e |
| 278 | r = fclose m |
| 279 | r = fclose e |
| 280 | ret n |
| 281 | end |
| 282 | |
| 283 | note civil date (proleptic Gregorian, years 1970..9999), Hinnant's days_from_civil and back |
| 284 | fn syn_days y ulong m ulong d ulong to ulong |
| 285 | yy = y |
| 286 | if le m 2 |
| 287 | yy = sub y 1 |
| 288 | end |
| 289 | era = div yy 400 |
| 290 | yoe = sub yy mul era 400 |
| 291 | mm = add m 9 |
| 292 | if gt m 2 |
| 293 | mm = sub m 3 |
| 294 | end |
| 295 | doy = add div add mul 153 mm 2 5 sub d 1 |
| 296 | doe = add mul yoe 365 div yoe 4 |
| 297 | doe = add sub doe div yoe 100 doy |
| 298 | ret sub add mul era 146097 doe 719468 |
| 299 | end |
| 300 | |
| 301 | note syn_mdays: days in month m (1..12) of year y, proleptic Gregorian leap rule (4, not 100, but 400) |
| 302 | fn syn_mdays y ulong m ulong to ulong |
| 303 | if eq m 2 |
| 304 | if and eq rem y 4 0 or ne rem y 100 0 eq rem y 400 0 |
| 305 | ret 29 |
| 306 | end |
| 307 | ret 28 |
| 308 | end |
| 309 | if or or eq m 4 eq m 6 or eq m 9 eq m 11 |
| 310 | ret 30 |
| 311 | end |
| 312 | ret 31 |
| 313 | end |
| 314 | |
| 315 | note syn_tsep: the six fixed bytes of YYYY-MM-DDTHH:MM:SSZ at pool offset o ('-' 4 and 7, 'T' 10, ':' 13 and 16, 'Z' 19) |
| 316 | fn syn_tsep o ulong to bool |
| 317 | if ne get gsp add o 4 45 |
| 318 | ret false |
| 319 | end |
| 320 | if ne get gsp add o 7 45 |
| 321 | ret false |
| 322 | end |
| 323 | if ne get gsp add o 10 84 |
| 324 | ret false |
| 325 | end |
| 326 | if ne get gsp add o 13 58 |
| 327 | ret false |
| 328 | end |
| 329 | if ne get gsp add o 16 58 |
| 330 | ret false |
| 331 | end |
| 332 | if ne get gsp add o 19 90 |
| 333 | ret false |
| 334 | end |
| 335 | ret true |
| 336 | end |
| 337 | |
| 338 | note syn_tsp: parse strict UTC YYYY-MM-DDTHH:MM:SSZ at pool (o,n) to epoch seconds; gxs 2 is 1 when well formed. |
| 339 | note Contract (the one synapse-claims ts_parse enforces): exactly 20 bytes, separators as in syn_tsep (uppercase T |
| 340 | note and Z only), year 1970..9999, month 1..12, day 1..syn_mdays, hour 0..23, minute 0..59, second 0..59. |
| 341 | note No leap second: :60 is refused, because Unix seconds cannot name it and synapse-claims never writes one. |
| 342 | fn syn_tsp o ulong n ulong to ulong |
| 343 | put gxs 2 0 |
| 344 | if ne n 20 |
| 345 | ret 0 |
| 346 | end |
| 347 | if eq syn_tsep o false |
| 348 | ret 0 |
| 349 | end |
| 350 | y = syn_pdec o 4 |
| 351 | ok = get gxs 2 |
| 352 | mo = syn_pdec add o 5 2 |
| 353 | ok = and ok get gxs 2 |
| 354 | d = syn_pdec add o 8 2 |
| 355 | ok = and ok get gxs 2 |
| 356 | h = syn_pdec add o 11 2 |
| 357 | ok = and ok get gxs 2 |
| 358 | mi = syn_pdec add o 14 2 |
| 359 | ok = and ok get gxs 2 |
| 360 | s = syn_pdec add o 17 2 |
| 361 | ok = and ok get gxs 2 |
| 362 | put gxs 2 0 |
| 363 | if or eq ok 0 or lt y 1970 or lt mo 1 gt mo 12 |
| 364 | ret 0 |
| 365 | end |
| 366 | if lt d 1 |
| 367 | ret 0 |
| 368 | end |
| 369 | if gt d syn_mdays y mo |
| 370 | ret 0 |
| 371 | end |
| 372 | if gt h 23 |
| 373 | ret 0 |
| 374 | end |
| 375 | if gt mi 59 |
| 376 | ret 0 |
| 377 | end |
| 378 | if gt s 59 |
| 379 | ret 0 |
| 380 | end |
| 381 | put gxs 2 1 |
| 382 | dd = syn_days y mo d |
| 383 | tt = add mul dd 86400 mul h 3600 |
| 384 | ret add add tt mul mi 60 s |
| 385 | end |
| 386 | |
| 387 | note syn_min: the smaller of two ulong values (Omegese has no min word) |
| 388 | fn syn_min a ulong b ulong to ulong |
| 389 | if lt a b |
| 390 | ret a |
| 391 | end |
| 392 | ret b |
| 393 | end |
| 394 | |
| 395 | fn syn_p2 v ulong to void |
| 396 | syn_pb uchar add 48 div v 10 |
| 397 | syn_pb uchar add 48 rem v 10 |
| 398 | end |
| 399 | |
| 400 | note syn_tsw: append epoch seconds t as YYYY-MM-DDTHH:MM:SSZ to the pool |
| 401 | fn syn_tsw t ulong to void |
| 402 | z = add div t 86400 719468 |
| 403 | sd = rem t 86400 |
| 404 | era = div z 146097 |
| 405 | doe = sub z mul era 146097 |
| 406 | yoe = div sub add sub doe div doe 1460 div doe 36524 div doe 146096 365 |
| 407 | y = add yoe mul era 400 |
| 408 | doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100 |
| 409 | mp = div add mul 5 doy 2 153 |
| 410 | d = add sub doy div add mul 153 mp 2 5 1 |
| 411 | m = add mp 3 |
| 412 | if ge mp 10 |
| 413 | m = sub mp 9 |
| 414 | end |
| 415 | if le m 2 |
| 416 | y = add y 1 |
| 417 | end |
| 418 | syn_pd y |
| 419 | syn_pb 45 |
| 420 | syn_p2 m |
| 421 | syn_pb 45 |
| 422 | syn_p2 d |
| 423 | syn_pb 84 |
| 424 | syn_p2 div sd 3600 |
| 425 | syn_pb 58 |
| 426 | syn_p2 div rem sd 3600 60 |
| 427 | syn_pb 58 |
| 428 | syn_p2 rem sd 60 |
| 429 | syn_pb 90 |
| 430 | end |
| 431 | |
| 432 | note syn_arg: copy argument i into the pool; returns its offset, gxs 2 its length |
| 433 | fn syn_arg i ulong to ulong |
| 434 | b = new uchar 4096 0 |
| 435 | n = argread i b 0 len b |
| 436 | if lt n 0 |
| 437 | put gxs 2 0 |
| 438 | ret get gspn 0 |
| 439 | end |
| 440 | if ge n long len b |
| 441 | syn_die targlong 64 |
| 442 | end |
| 443 | o = get gspn 0 |
| 444 | syn_pa b 0 ulong n |
| 445 | put gxs 2 n |
| 446 | ret o |
| 447 | end |
| 448 | |
| 449 | note syn_wbeg: open (po,pn).tmp for writing; descriptor or negative errno. Finish with syn_wend. |
| 450 | fn syn_wbeg po ulong pn ulong to long |
| 451 | mk = syn_mark |
| 452 | syn_ps po pn |
| 453 | syn_pt ttmp |
| 454 | fd = fopen gsp mk add pn 4 524865 420 |
| 455 | syn_rew mk |
| 456 | ret fd |
| 457 | end |
| 458 | |
| 459 | note syn_wend: sync and close fd, then rename (po,pn).tmp over (po,pn); 0 or negative errno |
| 460 | fn syn_wend fd long po ulong pn ulong to long |
| 461 | pk = new uchar 8200 0 |
| 462 | r = fdsync int fd |
| 463 | c = fclose int fd |
| 464 | put pk 0 2 |
| 465 | put pk 1 0 |
| 466 | put pk 2 uchar and add pn 4 255 |
| 467 | put pk 3 uchar shr add pn 4 8 |
| 468 | for i ulong pn |
| 469 | put pk add 4 i get gsp add po i |
| 470 | end |
| 471 | for i ulong 4 |
| 472 | put pk add add 4 pn i get ttmp i |
| 473 | end |
| 474 | q = add add 4 pn 4 |
| 475 | put pk q uchar and pn 255 |
| 476 | put pk add q 1 uchar shr pn 8 |
| 477 | for i ulong pn |
| 478 | put pk add add q 2 i get gsp add po i |
| 479 | end |
| 480 | tl = add q add 2 pn |
| 481 | if lt r 0 |
| 482 | ret r |
| 483 | end |
| 484 | r = renames pk 0 tl 0 |
| 485 | ret r |
| 486 | end |
| 487 | |
| 488 | note syn_rfile: read the file at pool path (po,pn) into out; byte count or negative errno; gxs 1 holds the file size |
| 489 | fn syn_rfile po ulong pn ulong obf edit uchar * to long |
| 490 | fd = fopen gsp po pn 524288 0 |
| 491 | if lt fd 0 |
| 492 | ret fd |
| 493 | end |
| 494 | sz = fseek int fd 0 2 |
| 495 | put gxs 1 sz |
| 496 | r = fseek int fd 0 0 |
| 497 | n = syn_rdall int fd obf len obf |
| 498 | c = fclose int fd |
| 499 | ret long n |
| 500 | end |
| 501 | |
| 502 | note syn_mkdir: make directory (po,pn); an existing one is fine |
| 503 | fn syn_mkdir po ulong pn ulong to long |
| 504 | r = makedir gsp po pn 493 |
| 505 | if eq r neg long 17 |
| 506 | ret 0 |
| 507 | end |
| 508 | ret r |
| 509 | end |