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 / authority / store-gate.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Durable 32-word authority record store. Caller supplies the record and generation. |
| 3 | note Writes a temporary 256-byte file, fsyncs it, then atomically renames it into place. |
| 4 | note Reads require exactly 256 bytes; a short or torn file is refused. Store calls do not lock; the |
| 5 | note dispatcher holds lock.om around its read-check-persist window. |
| 6 | text recp "build/authority-store/rec" |
| 7 | text tmpp "build/authority-store/tmp" |
| 8 | text genp "build/authority-store/gen" |
| 9 | global sb uchar 256 |
| 10 | global stail uchar 1 |
| 11 | global spkt uchar 256 |
| 12 | global sn ulong 1 |
| 13 | global spath uchar 128 |
| 14 | global splen ulong 1 |
| 15 | global sgerr ulong 1 |
| 16 | fn put16 v ulong to void |
| 17 | put spkt get sn 0 uchar and v 255 |
| 18 | put spkt add get sn 0 1 uchar shr v 8 |
| 19 | put sn 0 add get sn 0 2 |
| 20 | end |
| 21 | fn setrec to void |
| 22 | put splen 0 len recp |
| 23 | for i ulong len recp |
| 24 | put spath i get recp i |
| 25 | end |
| 26 | end |
| 27 | fn settmp to void |
| 28 | put splen 0 len tmpp |
| 29 | for i ulong len tmpp |
| 30 | put spath i get tmpp i |
| 31 | end |
| 32 | end |
| 33 | fn setgen to void |
| 34 | put splen 0 len genp |
| 35 | for i ulong len genp |
| 36 | put spath i get genp i |
| 37 | end |
| 38 | end |
| 39 | fn pack2 to ulong |
| 40 | put sn 0 0 |
| 41 | put16 2 |
| 42 | put16 len tmpp |
| 43 | for i ulong len tmpp |
| 44 | put spkt add get sn 0 i get tmpp i |
| 45 | end |
| 46 | put sn 0 add get sn 0 len tmpp |
| 47 | put16 len recp |
| 48 | for i ulong len recp |
| 49 | put spkt add get sn 0 i get recp i |
| 50 | end |
| 51 | put sn 0 add get sn 0 len recp |
| 52 | ret get sn 0 |
| 53 | end |
| 54 | fn enc r view ulong 32 to void |
| 55 | for i ulong 32 |
| 56 | v = get r i |
| 57 | for j ulong 8 |
| 58 | put sb add mul i 8 j uchar and shr v mul j 8 255 |
| 59 | end |
| 60 | end |
| 61 | end |
| 62 | fn dec r edit ulong 32 to void |
| 63 | for i ulong 32 |
| 64 | v = ulong 0 |
| 65 | for j ulong 8 |
| 66 | v = or v shl ulong get sb add mul i 8 j mul j 8 |
| 67 | end |
| 68 | put r i v |
| 69 | end |
| 70 | end |
| 71 | note p10 effect |
| 72 | fn writerec r view ulong 32 to ulong |
| 73 | enc r |
| 74 | settmp |
| 75 | fd = fopen spath 0 get splen 0 577 384 |
| 76 | if lt fd 0 |
| 77 | ret 10 |
| 78 | end |
| 79 | n = write int fd sb 0 256 |
| 80 | if ne n 256 |
| 81 | note p10 allow E1 primary write error wins over cleanup close error |
| 82 | cleanup = fclose int fd |
| 83 | ret 11 |
| 84 | end |
| 85 | if ne fsync int fd 0 |
| 86 | note p10 allow E1 primary sync error wins over cleanup close error |
| 87 | cleanup = fclose int fd |
| 88 | ret 12 |
| 89 | end |
| 90 | if ne fclose int fd 0 |
| 91 | ret 13 |
| 92 | end |
| 93 | pn = pack2 |
| 94 | rr = renames spkt 0 pn 0 |
| 95 | if lt rr 0 |
| 96 | ret 14 |
| 97 | end |
| 98 | ret 0 |
| 99 | end |
| 100 | note p10 effect |
| 101 | fn readrec r edit ulong 32 to ulong |
| 102 | setrec |
| 103 | fd = fopen spath 0 get splen 0 0 0 |
| 104 | if lt fd 0 |
| 105 | ret 20 |
| 106 | end |
| 107 | n = read int fd sb 0 256 |
| 108 | extra = long -1 |
| 109 | if eq n 256 |
| 110 | extra = read int fd stail 0 1 |
| 111 | end |
| 112 | if ne fclose int fd 0 |
| 113 | ret 21 |
| 114 | end |
| 115 | if or ne n 256 ne extra 0 |
| 116 | ret 22 |
| 117 | end |
| 118 | dec r |
| 119 | ret 0 |
| 120 | end |
| 121 | note p10 effect |
| 122 | fn readgen to ulong |
| 123 | put sgerr 0 0 |
| 124 | setgen |
| 125 | fd = fopen spath 0 get splen 0 0 0 |
| 126 | if lt fd 0 |
| 127 | put sgerr 0 30 |
| 128 | ret 0 |
| 129 | end |
| 130 | n = read int fd sb 0 8 |
| 131 | extra = long -1 |
| 132 | if eq n 8 |
| 133 | extra = read int fd stail 0 1 |
| 134 | end |
| 135 | if ne fclose int fd 0 |
| 136 | put sgerr 0 31 |
| 137 | ret 0 |
| 138 | end |
| 139 | if or ne n 8 ne extra 0 |
| 140 | put sgerr 0 32 |
| 141 | ret 0 |
| 142 | end |
| 143 | v = ulong 0 |
| 144 | for j ulong 8 |
| 145 | v = or v shl ulong get sb j mul j 8 |
| 146 | end |
| 147 | ret v |
| 148 | end |
| 149 | note p10 effect |
| 150 | fn writegen v ulong to ulong |
| 151 | setgen |
| 152 | fd = fopen spath 0 get splen 0 577 384 |
| 153 | if lt fd 0 |
| 154 | ret 40 |
| 155 | end |
| 156 | for j ulong 8 |
| 157 | put sb j uchar and shr v mul j 8 255 |
| 158 | end |
| 159 | n = write int fd sb 0 8 |
| 160 | if ne n 8 |
| 161 | note p10 allow E1 primary write error wins over cleanup close error |
| 162 | cleanup = fclose int fd |
| 163 | ret 41 |
| 164 | end |
| 165 | if ne fsync int fd 0 |
| 166 | note p10 allow E1 primary sync error wins over cleanup close error |
| 167 | cleanup = fclose int fd |
| 168 | ret 42 |
| 169 | end |
| 170 | if ne fclose int fd 0 |
| 171 | ret 43 |
| 172 | end |
| 173 | ret 0 |
| 174 | end |
| 175 | note p10 pure |
| 176 | fn storevalid r view ulong 32 g ulong to ulong |
| 177 | if ne get r 0 1 |
| 178 | ret 51 |
| 179 | end |
| 180 | if ne get r 4 g |
| 181 | ret 52 |
| 182 | end |
| 183 | ret 0 |
| 184 | end |
| 185 | note SPDX-License-Identifier: AGPL-3.0-only |
| 186 | note Store checks: exact read/write, torn file refusal, generation mismatch and atomic replace. |
| 187 | global checks ulong 1 |
| 188 | global fails ulong 1 |
| 189 | global report uchar 64 |
| 190 | text title "store checks=" |
| 191 | text dir "build/authority-store" |
| 192 | fn check got ulong want ulong to void |
| 193 | put checks 0 add get checks 0 1 |
| 194 | if ne got want |
| 195 | put fails 0 add get fails 0 1 |
| 196 | end |
| 197 | end |
| 198 | fn fill r edit ulong 32 seed ulong gen ulong to void |
| 199 | for i ulong 32 |
| 200 | put r i add seed i |
| 201 | end |
| 202 | put r 0 1 |
| 203 | put r 1 1 |
| 204 | put r 3 8 |
| 205 | put r 4 gen |
| 206 | end |
| 207 | fn summary to int |
| 208 | for i ulong len title |
| 209 | put report i get title i |
| 210 | end |
| 211 | n = get checks 0 |
| 212 | at = len title |
| 213 | put report at uchar add 48 div n 100 |
| 214 | put report add at 1 uchar add 48 rem div n 10 10 |
| 215 | put report add at 2 uchar add 48 rem n 10 |
| 216 | put report add at 3 32 |
| 217 | put report add at 4 uchar add 48 div get fails 0 100 |
| 218 | put report add at 5 uchar add 48 rem div get fails 0 10 10 |
| 219 | put report add at 6 uchar add 48 rem get fails 0 10 |
| 220 | put report add at 7 10 |
| 221 | w = write 1 report 0 add at 8 |
| 222 | if ne w long add at 8 |
| 223 | ret 2 |
| 224 | end |
| 225 | if ne get fails 0 0 |
| 226 | ret 1 |
| 227 | end |
| 228 | ret 0 |
| 229 | end |
| 230 | fn main to int |
| 231 | if ne makedir dir 0 len dir 448 0 |
| 232 | put sb 0 0 |
| 233 | end |
| 234 | r = new ulong 32 0 |
| 235 | q = new ulong 32 0 |
| 236 | fill r 100 1 |
| 237 | check writerec r 0 |
| 238 | fill q 0 0 |
| 239 | check readrec q 0 |
| 240 | for i ulong 32 |
| 241 | check get q i get r i |
| 242 | end |
| 243 | check storevalid q 1 0 |
| 244 | check writegen 1 0 |
| 245 | check readgen 1 |
| 246 | check storevalid q 2 52 |
| 247 | setrec |
| 248 | fd = fopen spath 0 get splen 0 577 384 |
| 249 | if lt fd 0 |
| 250 | check 0 1 |
| 251 | else |
| 252 | n = write int fd sb 0 3 |
| 253 | if ne fclose int fd 0 |
| 254 | check 0 1 |
| 255 | end |
| 256 | check readrec q 22 |
| 257 | end |
| 258 | check writerec r 0 |
| 259 | check writegen 2 0 |
| 260 | check readgen 2 |
| 261 | fill r 200 2 |
| 262 | check writerec r 0 |
| 263 | check readrec q 0 |
| 264 | for i ulong 32 |
| 265 | check get q i get r i |
| 266 | end |
| 267 | check storevalid q 2 0 |
| 268 | ret summary |
| 269 | end |