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 / seed / host / tests / netpeer.om
| 1 | note Parent and child connect independently; SO_PEERCRED must return the other process PID. |
| 2 | text pname "netpeer.sock" |
| 3 | text pass "PASS independent Unix peer identity and offset sentinels\n" |
| 4 | global pa uchar 110 |
| 5 | global pc uchar 16 |
| 6 | global ps uchar 4 |
| 7 | global pb uchar 8 |
| 8 | global pp uchar 16 |
| 9 | fn pcheck yes bool to void |
| 10 | if not yes |
| 11 | exit 1 |
| 12 | end |
| 13 | end |
| 14 | fn pget off ulong to long |
| 15 | v = ulong 0 |
| 16 | for i ulong 4 |
| 17 | v = or v shl ulong get pc add off i mul i 8 |
| 18 | end |
| 19 | ret long v |
| 20 | end |
| 21 | fn pcreds fd int pid long to void |
| 22 | for i ulong len pc |
| 23 | put pc i 165 |
| 24 | end |
| 25 | pcheck eq add peercred fd pc 2 12 9 9 |
| 26 | pcheck eq pget 2 pid |
| 27 | pcheck eq pget 6 getuid |
| 28 | pcheck eq pget 10 getgid |
| 29 | pcheck eq get pc 0 165 |
| 30 | pcheck eq get pc 1 165 |
| 31 | pcheck eq get pc 14 165 |
| 32 | pcheck eq get pc 15 165 |
| 33 | end |
| 34 | fn pchild to int |
| 35 | fd = socknew 1 524289 0 |
| 36 | pcheck ge fd 0 |
| 37 | pcheck eq connect int fd pa 0 len pa 0 |
| 38 | pcreds int fd getppid |
| 39 | put pb 3 42 |
| 40 | pcheck eq sendmsg int fd pb 3 1 0 1 |
| 41 | pcheck gt waitio int fd 1 1000000000 0 |
| 42 | pcheck eq recv int fd pb 4 1 0 1 |
| 43 | pcheck eq get pb 4 99 |
| 44 | pcheck eq fclose int fd 0 |
| 45 | ret 0 |
| 46 | end |
| 47 | fn pserve fd int pid long to void |
| 48 | pcheck gt waitio fd 1 1000000000 0 |
| 49 | conn = accept fd 524288 |
| 50 | pcheck ge conn 0 |
| 51 | pcreds int conn pid |
| 52 | for i ulong len pp |
| 53 | put pp i 0 |
| 54 | end |
| 55 | for i ulong 4 |
| 56 | put pp add i 4 uchar and shr ulong conn mul i 8 255 |
| 57 | end |
| 58 | put pp 8 1 |
| 59 | put pp 0 165 |
| 60 | put pp 15 165 |
| 61 | pcheck eq add pollfds pp 4 8 neg long 1 9 10 |
| 62 | pcheck ne and get pp 10 1 0 |
| 63 | pcheck eq get pp 0 165 |
| 64 | pcheck eq get pp 15 165 |
| 65 | pcheck eq recv int conn pb 2 1 0 1 |
| 66 | pcheck eq get pb 2 42 |
| 67 | put pb 3 99 |
| 68 | pcheck eq add sendmsg int conn pb 3 1 0 9 10 |
| 69 | pcheck eq fclose int conn 0 |
| 70 | end |
| 71 | fn main to int |
| 72 | put pa 0 1 |
| 73 | for i ulong len pname |
| 74 | put pa add i 2 get pname i |
| 75 | end |
| 76 | fd = socknew 1 524289 0 |
| 77 | pcheck ge fd 0 |
| 78 | pcheck eq bind int fd pa 0 len pa 0 |
| 79 | pcheck eq listen int fd 1 0 |
| 80 | pid = fork |
| 81 | pcheck ge pid 0 |
| 82 | if eq pid 0 |
| 83 | pcheck eq fclose int fd 0 |
| 84 | ret pchild |
| 85 | end |
| 86 | pserve int fd pid |
| 87 | pcheck eq waitpid int pid ps 0 4 0 pid |
| 88 | for i ulong 4 |
| 89 | pcheck eq get ps i 0 |
| 90 | end |
| 91 | pcheck eq fclose int fd 0 |
| 92 | pcheck eq unlink pname 0 len pname 0 |
| 93 | pcheck eq write 1 pass 0 len pass long len pass |
| 94 | ret 0 |
| 95 | end |