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 / ng / a64 / tests / macho / lib / libback.om
| 1 | target macos |
| 2 | note libback: library code calls back into Omega. qsort of libSystem sorts eight 64-bit numbers; its comparison |
| 3 | note function is callback entry 5 (libfn 7), so the function libback runs with k = 5 and the addresses of two |
| 4 | note elements. It reads them with memcpy: a libcall inside a callback, on the C stack below qsort's frames. |
| 5 | note Status 1 when the numbers come out sorted, the callback ran at least seven times with k = 5 and no other k, |
| 6 | note libfn gives nothing for 18 and different entries for 2 and 3, and a libcall after the sort still works. |
| 7 | note 2 to 8 name the step that failed. |
| 8 | text s_qs "qsort" |
| 9 | text s_mc "memcpy" |
| 10 | text s_in "53819274" |
| 11 | global g_f ulong 4 |
| 12 | global g_rg uchar 128 |
| 13 | global g_a uchar 16 |
| 14 | |
| 15 | fn p64 b edit uchar 128 o ulong v ulong to void |
| 16 | for i ulong 8 |
| 17 | put b add o i uchar and shr v mul i 8 255 |
| 18 | end |
| 19 | end |
| 20 | |
| 21 | fn pr o ulong v ulong to void |
| 22 | for i ulong 8 |
| 23 | put g_rg add o i uchar and shr v mul i 8 255 |
| 24 | end |
| 25 | end |
| 26 | |
| 27 | fn ga o ulong to ulong |
| 28 | v = ulong 0 |
| 29 | for i ulong 8 |
| 30 | v = or v shl ulong get g_a add o i mul i 8 |
| 31 | end |
| 32 | ret v |
| 33 | end |
| 34 | |
| 35 | fn sym rg edit uchar 128 nm edit uchar 64 to ulong |
| 36 | p64 rg 0 18446744073709551614 |
| 37 | p64 rg 8 ulong libptr nm 0 64 |
| 38 | ret ulong libcall ulong libfn 1 rg 0 128 |
| 39 | end |
| 40 | |
| 41 | fn libback k ulong a ulong b ulong c ulong d ulong e ulong to ulong |
| 42 | put g_f 1 add get g_f 1 1 |
| 43 | if ne k 5 |
| 44 | put g_f 2 1 |
| 45 | end |
| 46 | pr 0 ulong libptr g_a 0 16 |
| 47 | pr 8 a |
| 48 | pr 16 8 |
| 49 | r = libcall get g_f 0 g_rg 0 128 |
| 50 | pr 0 ulong libptr g_a 8 8 |
| 51 | pr 8 b |
| 52 | pr 16 8 |
| 53 | r = libcall get g_f 0 g_rg 0 128 |
| 54 | x = ga 0 |
| 55 | y = ga 8 |
| 56 | if lt x y |
| 57 | ret 18446744073709551615 |
| 58 | end |
| 59 | if gt x y |
| 60 | ret 1 |
| 61 | end |
| 62 | ret 0 |
| 63 | end |
| 64 | |
| 65 | fn main to int |
| 66 | rg = new uchar 128 0 |
| 67 | nm = new uchar 64 0 |
| 68 | arr = new uchar 64 0 |
| 69 | out = new uchar 64 0 |
| 70 | for i ulong 8 |
| 71 | put arr mul i 8 uchar sub get s_in i 48 |
| 72 | end |
| 73 | for i ulong len s_qs |
| 74 | put nm i get s_qs i |
| 75 | end |
| 76 | put nm len s_qs 0 |
| 77 | qs = sym rg nm |
| 78 | for i ulong len s_mc |
| 79 | put nm i get s_mc i |
| 80 | end |
| 81 | put nm len s_mc 0 |
| 82 | mc = sym rg nm |
| 83 | if or eq qs 0 eq mc 0 |
| 84 | ret 2 |
| 85 | end |
| 86 | put g_f 0 mc |
| 87 | cb = ulong libfn 7 |
| 88 | if or eq cb 0 ne ulong libfn 18 0 |
| 89 | ret 3 |
| 90 | end |
| 91 | if or eq ulong libfn 2 ulong libfn 3 ne sub ulong libfn 3 ulong libfn 2 8 |
| 92 | ret 4 |
| 93 | end |
| 94 | p64 rg 0 ulong libptr arr 0 64 |
| 95 | p64 rg 8 8 |
| 96 | p64 rg 16 8 |
| 97 | p64 rg 24 cb |
| 98 | r = libcall qs rg 0 128 |
| 99 | if or ne get g_f 2 0 lt get g_f 1 7 |
| 100 | ret 5 |
| 101 | end |
| 102 | p64 rg 0 ulong libptr out 0 64 |
| 103 | p64 rg 8 ulong libptr arr 0 64 |
| 104 | p64 rg 16 64 |
| 105 | r = libcall mc rg 0 128 |
| 106 | want = ulong 1 |
| 107 | for i ulong 8 |
| 108 | if or ne ulong get out mul i 8 want ne ulong get out add mul i 8 1 0 |
| 109 | ret 6 |
| 110 | end |
| 111 | want = add want 1 |
| 112 | if eq want 6 |
| 113 | want = ulong 7 |
| 114 | end |
| 115 | end |
| 116 | ret 1 |
| 117 | end |