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 / aienux / crates / aienos-capability-ffi / cap_ffi.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-capability-ffi/cap_ffi.om: Pure-Omega 32-bit-generation |
| 3 | note capability authority ABI (aienos_cap32_*), layout contract, and split Admin/View handles. |
| 4 | note Converted from aienux crates/aienos-capability-ffi/src/lib.rs. |
| 5 | note Compose with: si/directives/om2.om aienux/crates/aienos-capability-ffi/cap_ffi.om ... |
| 6 | |
| 7 | layout Cap32Ref 8 |
| 8 | field cap_id uint at 0 little |
| 9 | field gen uint at 4 little |
| 10 | end |
| 11 | |
| 12 | layout Cap32Ent 64 |
| 13 | field cap_id uint at 0 little |
| 14 | field gen uint at 4 little |
| 15 | field state uint at 8 little |
| 16 | field issuer uint at 12 little |
| 17 | field subject uint at 16 little |
| 18 | field rights uint at 20 little |
| 19 | field res ulong at 24 little |
| 20 | field epoch ulong at 32 little |
| 21 | field lease ulong at 40 little |
| 22 | field par_id uint at 48 little |
| 23 | field par_gen uint at 52 little |
| 24 | field mnt_id uint at 56 little |
| 25 | field mnt_gen uint at 60 little |
| 26 | end |
| 27 | |
| 28 | layout Cap32Mnt 48 |
| 29 | field issuer uint at 0 little |
| 30 | field subject uint at 4 little |
| 31 | field res ulong at 8 little |
| 32 | field rights uint at 16 little |
| 33 | field lease ulong at 24 little |
| 34 | field par_id uint at 32 little |
| 35 | field par_gen uint at 36 little |
| 36 | field auth_id uint at 40 little |
| 37 | field auth_gen uint at 44 little |
| 38 | end |
| 39 | |
| 40 | const CAP32_ABI_VERSION uint 1 |
| 41 | const CAP32_GEN_BITS uint 32 |
| 42 | const CAP32_REF_SIZE uint 8 |
| 43 | const CAP32_REF_GEN_OFF uint 4 |
| 44 | const CAP32_ENT_SIZE uint 64 |
| 45 | const CAP32_MNT_SIZE uint 48 |
| 46 | |
| 47 | const CAP32_OK int 0 |
| 48 | const CAP32_ERR_STATE int -1 |
| 49 | const CAP32_ERR_NOT_FOUND int -2 |
| 50 | const CAP32_ERR_STALE_GEN int -3 |
| 51 | const CAP32_ERR_REVOKED int -4 |
| 52 | const CAP32_ERR_EXPIRED int -5 |
| 53 | const CAP32_ERR_SUBJECT int -6 |
| 54 | const CAP32_ERR_RESOURCE int -7 |
| 55 | const CAP32_ERR_RIGHTS int -8 |
| 56 | const CAP32_ERR_UNAUTH int -9 |
| 57 | const CAP32_ERR_EXHAUSTED int -10 |
| 58 | const CAP32_ERR_CAPACITY int -11 |
| 59 | |
| 60 | const CAP32_STATE_FREE uint 0 |
| 61 | const CAP32_STATE_ACTIVE uint 1 |
| 62 | const CAP32_STATE_REVOKED uint 2 |
| 63 | |
| 64 | const CAP32_RIGHT_READ uint 1 |
| 65 | const CAP32_RIGHT_WRITE uint 2 |
| 66 | const CAP32_RIGHT_ADMIN uint 0x80000000 |
| 67 | |
| 68 | const CAP32_MAX_SLOTS ulong 32 |
| 69 | |
| 70 | note Global authority table |
| 71 | note meta: 0=active, 1=writer_alive, 2=boot_gen, 3=gen_floor, 4=clock, 5=epoch, 6=office_gen |
| 72 | global cap32_meta ulong 7 |
| 73 | global cap32_s_gen uint 32 |
| 74 | global cap32_s_state uint 32 |
| 75 | global cap32_s_issuer uint 32 |
| 76 | global cap32_s_subj uint 32 |
| 77 | global cap32_s_rights uint 32 |
| 78 | global cap32_s_res ulong 32 |
| 79 | global cap32_s_epoch ulong 32 |
| 80 | global cap32_s_lease ulong 32 |
| 81 | global cap32_s_pid uint 32 |
| 82 | global cap32_s_pgen uint 32 |
| 83 | global cap32_s_mid uint 32 |
| 84 | global cap32_s_mgen uint 32 |
| 85 | |
| 86 | note Report layout contract [REF_SIZE, REF_GEN_OFF, 4, ENT_SIZE] -> returns ABI version 1 |
| 87 | fn cap32_abi_layout words edit uint * to uint |
| 88 | if lt len words 4 |
| 89 | ret 0 |
| 90 | end |
| 91 | put words 0 CAP32_REF_SIZE |
| 92 | put words 1 CAP32_REF_GEN_OFF |
| 93 | put words 2 4 |
| 94 | put words 3 CAP32_ENT_SIZE |
| 95 | ret CAP32_ABI_VERSION |
| 96 | end |
| 97 | |
| 98 | fn cap32_gen_advance g uint dst edit uint * to int |
| 99 | if eq g 0xffffffff |
| 100 | ret CAP32_ERR_EXHAUSTED |
| 101 | end |
| 102 | put dst 0 add g 1 |
| 103 | ret CAP32_OK |
| 104 | end |
| 105 | |
| 106 | fn cap32_bootstrap boot_gen uint to int |
| 107 | if eq boot_gen 0 |
| 108 | ret CAP32_ERR_STATE |
| 109 | end |
| 110 | for i ulong CAP32_MAX_SLOTS |
| 111 | put cap32_s_gen i boot_gen |
| 112 | put cap32_s_state i CAP32_STATE_FREE |
| 113 | put cap32_s_issuer i 0 |
| 114 | put cap32_s_subj i 0 |
| 115 | put cap32_s_rights i 0 |
| 116 | put cap32_s_res i 0 |
| 117 | put cap32_s_epoch i 0 |
| 118 | put cap32_s_lease i 0 |
| 119 | put cap32_s_pid i 0 |
| 120 | put cap32_s_pgen i 0 |
| 121 | put cap32_s_mid i 0 |
| 122 | put cap32_s_mgen i 0 |
| 123 | end |
| 124 | note Slot 0 is the Office Authority capability |
| 125 | put cap32_s_state 0 CAP32_STATE_ACTIVE |
| 126 | put cap32_s_gen 0 boot_gen |
| 127 | put cap32_s_issuer 0 0 |
| 128 | put cap32_s_subj 0 0 |
| 129 | put cap32_s_rights 0 CAP32_RIGHT_ADMIN |
| 130 | put cap32_s_res 0 0 |
| 131 | put cap32_s_epoch 0 1 |
| 132 | put cap32_s_lease 0 0 |
| 133 | put cap32_meta 0 1 |
| 134 | put cap32_meta 1 1 |
| 135 | put cap32_meta 2 ulong boot_gen |
| 136 | put cap32_meta 3 ulong boot_gen |
| 137 | put cap32_meta 4 0 |
| 138 | put cap32_meta 5 1 |
| 139 | put cap32_meta 6 ulong boot_gen |
| 140 | ret CAP32_OK |
| 141 | end |
| 142 | |
| 143 | fn cap32_start to int |
| 144 | ret cap32_bootstrap 1 |
| 145 | end |
| 146 | |
| 147 | fn cap32_office dst edit Cap32Ref to int |
| 148 | if eq get cap32_meta 0 0 |
| 149 | ret CAP32_ERR_STATE |
| 150 | end |
| 151 | put dst cap_id 0 |
| 152 | put dst gen uint get cap32_meta 6 |
| 153 | ret CAP32_OK |
| 154 | end |
| 155 | |
| 156 | fn cap32_check_admin auth_id uint auth_gen uint to int |
| 157 | if eq get cap32_meta 0 0 |
| 158 | ret CAP32_ERR_STATE |
| 159 | end |
| 160 | if eq get cap32_meta 1 0 |
| 161 | ret CAP32_ERR_STATE |
| 162 | end |
| 163 | if ne auth_id 0 |
| 164 | ret CAP32_ERR_UNAUTH |
| 165 | end |
| 166 | if ne auth_gen uint get cap32_meta 6 |
| 167 | ret CAP32_ERR_STALE_GEN |
| 168 | end |
| 169 | if ne get cap32_s_state 0 CAP32_STATE_ACTIVE |
| 170 | ret CAP32_ERR_REVOKED |
| 171 | end |
| 172 | ret CAP32_OK |
| 173 | end |
| 174 | |
| 175 | fn cap32_mint req view Cap32Mnt dst edit Cap32Ref to int |
| 176 | rc = cap32_check_admin get req auth_id get req auth_gen |
| 177 | if ne rc CAP32_OK |
| 178 | ret rc |
| 179 | end |
| 180 | slot = ulong CAP32_MAX_SLOTS |
| 181 | for i ulong sub CAP32_MAX_SLOTS 1 |
| 182 | idx = add i 1 |
| 183 | if eq get cap32_s_state idx CAP32_STATE_FREE |
| 184 | slot = idx |
| 185 | break |
| 186 | end |
| 187 | end |
| 188 | if eq slot CAP32_MAX_SLOTS |
| 189 | ret CAP32_ERR_CAPACITY |
| 190 | end |
| 191 | g = get cap32_s_gen slot |
| 192 | clk = get cap32_meta 4 |
| 193 | l_ticks = get req lease |
| 194 | exp = ulong 0 |
| 195 | if gt l_ticks 0 |
| 196 | exp = add clk l_ticks |
| 197 | end |
| 198 | put cap32_s_state slot CAP32_STATE_ACTIVE |
| 199 | put cap32_s_issuer slot get req issuer |
| 200 | put cap32_s_subj slot get req subject |
| 201 | put cap32_s_rights slot get req rights |
| 202 | put cap32_s_res slot get req res |
| 203 | put cap32_s_epoch slot get cap32_meta 5 |
| 204 | put cap32_s_lease slot exp |
| 205 | put cap32_s_pid slot get req par_id |
| 206 | put cap32_s_pgen slot get req par_gen |
| 207 | put cap32_s_mid slot get req auth_id |
| 208 | put cap32_s_mgen slot get req auth_gen |
| 209 | put dst cap_id uint slot |
| 210 | put dst gen g |
| 211 | ret CAP32_OK |
| 212 | end |
| 213 | |
| 214 | fn cap32_revoke auth view Cap32Ref tgt view Cap32Ref to int |
| 215 | rc = cap32_check_admin get auth cap_id get auth gen |
| 216 | if ne rc CAP32_OK |
| 217 | ret rc |
| 218 | end |
| 219 | id = ulong get tgt cap_id |
| 220 | if or eq id 0 ge id CAP32_MAX_SLOTS |
| 221 | ret CAP32_ERR_NOT_FOUND |
| 222 | end |
| 223 | if ne get cap32_s_gen id get tgt gen |
| 224 | ret CAP32_ERR_STALE_GEN |
| 225 | end |
| 226 | if ne get cap32_s_state id CAP32_STATE_ACTIVE |
| 227 | ret CAP32_ERR_REVOKED |
| 228 | end |
| 229 | put cap32_s_state id CAP32_STATE_REVOKED |
| 230 | ret CAP32_OK |
| 231 | end |
| 232 | |
| 233 | fn cap32_reclaim auth view Cap32Ref cid uint to int |
| 234 | rc = cap32_check_admin get auth cap_id get auth gen |
| 235 | if ne rc CAP32_OK |
| 236 | ret rc |
| 237 | end |
| 238 | id = ulong cid |
| 239 | if or eq id 0 ge id CAP32_MAX_SLOTS |
| 240 | ret CAP32_ERR_NOT_FOUND |
| 241 | end |
| 242 | if ne get cap32_s_state id CAP32_STATE_REVOKED |
| 243 | ret CAP32_ERR_STATE |
| 244 | end |
| 245 | cur_g = get cap32_s_gen id |
| 246 | nxt = new uint 1 0 |
| 247 | rc = cap32_gen_advance cur_g nxt |
| 248 | if ne rc CAP32_OK |
| 249 | ret rc |
| 250 | end |
| 251 | ng = get nxt 0 |
| 252 | put cap32_s_gen id ng |
| 253 | put cap32_s_state id CAP32_STATE_FREE |
| 254 | if gt ulong ng get cap32_meta 3 |
| 255 | put cap32_meta 3 ulong ng |
| 256 | end |
| 257 | ret CAP32_OK |
| 258 | end |
| 259 | |
| 260 | fn cap32_advance_clock auth view Cap32Ref ticks ulong to int |
| 261 | rc = cap32_check_admin get auth cap_id get auth gen |
| 262 | if ne rc CAP32_OK |
| 263 | ret rc |
| 264 | end |
| 265 | put cap32_meta 4 add get cap32_meta 4 ticks |
| 266 | ret CAP32_OK |
| 267 | end |
| 268 | |
| 269 | fn cap32_bump_epoch auth view Cap32Ref to int |
| 270 | rc = cap32_check_admin get auth cap_id get auth gen |
| 271 | if ne rc CAP32_OK |
| 272 | ret rc |
| 273 | end |
| 274 | put cap32_meta 5 add get cap32_meta 5 1 |
| 275 | ret CAP32_OK |
| 276 | end |
| 277 | |
| 278 | fn cap32_kill to int |
| 279 | if eq get cap32_meta 0 0 |
| 280 | ret CAP32_ERR_STATE |
| 281 | end |
| 282 | put cap32_meta 1 0 |
| 283 | ret CAP32_OK |
| 284 | end |
| 285 | |
| 286 | fn cap32_restart to int |
| 287 | if eq get cap32_meta 0 0 |
| 288 | ret CAP32_ERR_STATE |
| 289 | end |
| 290 | flr = uint get cap32_meta 3 |
| 291 | nxt = new uint 1 0 |
| 292 | rc = cap32_gen_advance flr nxt |
| 293 | if ne rc CAP32_OK |
| 294 | ret rc |
| 295 | end |
| 296 | ret cap32_bootstrap get nxt 0 |
| 297 | end |
| 298 | |
| 299 | fn cap32_fill_ent id ulong dst edit Cap32Ent to void |
| 300 | put dst cap_id uint id |
| 301 | put dst gen get cap32_s_gen id |
| 302 | put dst state get cap32_s_state id |
| 303 | put dst issuer get cap32_s_issuer id |
| 304 | put dst subject get cap32_s_subj id |
| 305 | put dst rights get cap32_s_rights id |
| 306 | put dst res get cap32_s_res id |
| 307 | put dst epoch get cap32_s_epoch id |
| 308 | put dst lease get cap32_s_lease id |
| 309 | put dst par_id get cap32_s_pid id |
| 310 | put dst par_gen get cap32_s_pgen id |
| 311 | put dst mnt_id get cap32_s_mid id |
| 312 | put dst mnt_gen get cap32_s_mgen id |
| 313 | end |
| 314 | |
| 315 | fn cap32_validate ref view Cap32Ref subj uint res ulong rights uint dst edit Cap32Ent to int |
| 316 | if eq get cap32_meta 0 0 |
| 317 | ret CAP32_ERR_STATE |
| 318 | end |
| 319 | id = ulong get ref cap_id |
| 320 | if ge id CAP32_MAX_SLOTS |
| 321 | ret CAP32_ERR_NOT_FOUND |
| 322 | end |
| 323 | if ne get cap32_s_gen id get ref gen |
| 324 | ret CAP32_ERR_STALE_GEN |
| 325 | end |
| 326 | st = get cap32_s_state id |
| 327 | if eq st CAP32_STATE_FREE |
| 328 | ret CAP32_ERR_NOT_FOUND |
| 329 | end |
| 330 | if eq st CAP32_STATE_REVOKED |
| 331 | ret CAP32_ERR_REVOKED |
| 332 | end |
| 333 | exp = get cap32_s_lease id |
| 334 | if and gt exp 0 ge get cap32_meta 4 exp |
| 335 | ret CAP32_ERR_EXPIRED |
| 336 | end |
| 337 | if ne get cap32_s_subj id subj |
| 338 | ret CAP32_ERR_SUBJECT |
| 339 | end |
| 340 | if ne get cap32_s_res id res |
| 341 | ret CAP32_ERR_RESOURCE |
| 342 | end |
| 343 | if ne and get cap32_s_rights id rights rights |
| 344 | ret CAP32_ERR_RIGHTS |
| 345 | end |
| 346 | cap32_fill_ent id dst |
| 347 | ret CAP32_OK |
| 348 | end |
| 349 | |
| 350 | fn cap32_inspect ref view Cap32Ref dst edit Cap32Ent to int |
| 351 | if eq get cap32_meta 0 0 |
| 352 | ret CAP32_ERR_STATE |
| 353 | end |
| 354 | id = ulong get ref cap_id |
| 355 | if ge id CAP32_MAX_SLOTS |
| 356 | ret CAP32_ERR_NOT_FOUND |
| 357 | end |
| 358 | if ne get cap32_s_gen id get ref gen |
| 359 | ret CAP32_ERR_STALE_GEN |
| 360 | end |
| 361 | cap32_fill_ent id dst |
| 362 | ret CAP32_OK |
| 363 | end |
| 364 | |
| 365 | fn cap32_clock to ulong |
| 366 | ret get cap32_meta 4 |
| 367 | end |
| 368 | |
| 369 | note View-side cognition attempts are unconditionally unauthorized |
| 370 | fn cap32_cognition_mint req view Cap32Mnt to int |
| 371 | if eq get cap32_meta 0 0 |
| 372 | ret CAP32_ERR_STATE |
| 373 | end |
| 374 | ret CAP32_ERR_UNAUTH |
| 375 | end |
| 376 | |
| 377 | fn cap32_cognition_admin op uint auth view Cap32Ref tgt view Cap32Ref to int |
| 378 | if eq get cap32_meta 0 0 |
| 379 | ret CAP32_ERR_STATE |
| 380 | end |
| 381 | ret CAP32_ERR_UNAUTH |
| 382 | end |
| 383 | |
| 384 | fn cap32_force_gen cid uint g uint to int |
| 385 | id = ulong cid |
| 386 | if ge id CAP32_MAX_SLOTS |
| 387 | ret CAP32_ERR_NOT_FOUND |
| 388 | end |
| 389 | put cap32_s_gen id g |
| 390 | if gt ulong g get cap32_meta 3 |
| 391 | put cap32_meta 3 ulong g |
| 392 | end |
| 393 | ret CAP32_OK |
| 394 | end |