Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/runtime/aienos_cap_om2.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / runtime / aienos_cap_om2.c` 396 lines ยท 13632 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Pure seeded OM-2 backed implementation of aienos_cap.h (seed/authority/cap_table_om2.om). 5 * Severs the external libaienos_capability.a / aienos.lock C dependency. 6 */ 7 #include "aienos_cap.h" 8 #include "omega_native.h" 9 #include "rx_caproot.h" 10 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 19 struct AienosCapView { 20 pthread_mutex_t mu; 21 uint64_t hdr[4]; 22 uint64_t slots[RX_CAP_MAX * 13]; 23 uint8_t delivered[RX_CAP_MAX]; 24 uint8_t token[RX_CAP_TOKEN_LEN]; 25 AienosCapObserver obs; 26 void *obs_ctx; 27 int alive; 28 }; 29 30 struct AienosCapAdmin { 31 struct AienosCapView *view; 32 }; 33 34 static _Atomic uint64_t g_om2_boot_gen = 0; 35 36 static uint64_t om2_boot_seed(void) { 37 struct timespec ts; 38 if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) return 0; 39 uint64_t ns = (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 40 if (ns > (UINT64_MAX >> 9)) return 0; 41 return ns << 8; 42 } 43 44 static int om2_take_boot_gen(uint64_t *out) { 45 uint64_t seed = om2_boot_seed(); 46 uint64_t cur = atomic_load_explicit(&g_om2_boot_gen, memory_order_relaxed); 47 for (;;) { 48 if (cur == UINT64_MAX) return RX_CAP_ERR_EXHAUSTED; 49 uint64_t next = cur + 1u; 50 if (seed > next) next = seed; 51 if (next == UINT64_MAX) return RX_CAP_ERR_EXHAUSTED; 52 if (atomic_compare_exchange_weak_explicit(&g_om2_boot_gen, &cur, next, 53 memory_order_acq_rel, memory_order_relaxed)) { 54 *out = next; 55 return RX_CAP_OK; 56 } 57 } 58 } 59 60 static void unpack_entry(const uint64_t s[13], AienosCapEntry *e) { 61 e->cap_id = (uint32_t)s[0]; 62 e->generation = s[1]; 63 e->state = (uint32_t)s[2]; 64 e->issuer = (uint32_t)s[3]; 65 e->subject = (uint32_t)s[4]; 66 e->rights = (uint32_t)s[5]; 67 e->resource = s[6]; 68 e->epoch = s[7]; 69 e->lease_expiry = s[8]; 70 e->parent_id = (uint32_t)s[9]; 71 e->parent_generation = s[10]; 72 e->minted_by_id = (uint32_t)s[11]; 73 e->minted_by_generation = s[12]; 74 } 75 76 static void fill_rand(uint8_t *buf, size_t len) { 77 size_t n = 0; 78 while (n < len) { 79 ssize_t r = getrandom(buf + n, len - n, 0); 80 if (r < 0 && errno == EINTR) continue; 81 if (r <= 0) break; 82 n += (size_t)r; 83 } 84 } 85 86 int aienos_cap_start(AienosCapAdmin **admin, AienosCapView **view) { 87 if (!admin || !view) return RX_CAP_ERR_STATE; 88 uint64_t boot = 0; 89 int brc = om2_take_boot_gen(&boot); 90 if (brc != RX_CAP_OK) return brc; 91 AienosCapView *v = calloc(1, sizeof *v); 92 AienosCapAdmin *a = calloc(1, sizeof *a); 93 if (!v || !a) { free(v); free(a); return RX_CAP_ERR_IO; } 94 pthread_mutex_init(&v->mu, NULL); 95 uint64_t rc = 0; 96 (void)omega2_cap_table_init(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, 97 v->delivered, RX_CAP_MAX, boot); 98 if ((int64_t)rc != RX_CAP_OK) { 99 pthread_mutex_destroy(&v->mu); 100 free(v); 101 free(a); 102 return (int)(int64_t)rc; 103 } 104 fill_rand(v->token, sizeof v->token); 105 v->alive = 1; 106 a->view = v; 107 *admin = a; 108 *view = v; 109 return RX_CAP_OK; 110 } 111 112 void aienos_cap_stop(AienosCapAdmin *admin, AienosCapView *view) { 113 AienosCapView *v = view ? view : (admin ? admin->view : NULL); 114 if (admin) free(admin); 115 if (v) { 116 memset(v->token, 0, sizeof v->token); 117 pthread_mutex_destroy(&v->mu); 118 free(v); 119 } 120 } 121 122 int aienos_cap_office(const AienosCapAdmin *admin, AienosCapRef *out) { 123 if (!admin || !admin->view || !out) return RX_CAP_ERR_STATE; 124 AienosCapView *v = admin->view; 125 pthread_mutex_lock(&v->mu); 126 if (v->hdr[3] != 0) { 127 pthread_mutex_unlock(&v->mu); 128 return RX_CAP_ERR_IO; 129 } 130 out->cap_id = (uint32_t)v->slots[0]; 131 out->generation = v->slots[1]; 132 pthread_mutex_unlock(&v->mu); 133 return RX_CAP_OK; 134 } 135 136 int aienos_cap_mint(AienosCapAdmin *admin, const AienosCapMint *request, AienosCapRef *out) { 137 if (!admin || !admin->view || !request) return RX_CAP_ERR_STATE; 138 AienosCapView *v = admin->view; 139 uint64_t req[9] = { 140 request->issuer, request->subject, request->resource, request->rights, 141 request->lease_ticks, request->parent.cap_id, request->parent.generation, 142 request->authority.cap_id, request->authority.generation 143 }; 144 uint64_t ref[2] = { 0, 0 }, rc = 0; 145 AienosCapEntry ent; 146 int have_ent = 0; 147 AienosCapObserver obs = NULL; 148 void *obs_ctx = NULL; 149 150 pthread_mutex_lock(&v->mu); 151 (void)omega2_cap_table_mint(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, 152 v->delivered, RX_CAP_MAX, req, 9, ref, 2); 153 int res = (int)(int64_t)rc; 154 obs = v->obs; 155 obs_ctx = v->obs_ctx; 156 if (res == RX_CAP_OK) { 157 if (out) { out->cap_id = (uint32_t)ref[0]; out->generation = ref[1]; } 158 unpack_entry(&v->slots[ref[0] * 13], &ent); 159 have_ent = 1; 160 } 161 pthread_mutex_unlock(&v->mu); 162 163 if (obs) obs(obs_ctx, AIENOS_CAP_OBS_MINT, have_ent ? &ent : NULL, res); 164 return res; 165 } 166 167 int aienos_cap_revoke(AienosCapAdmin *admin, AienosCapRef authority, AienosCapRef target) { 168 if (!admin || !admin->view) return RX_CAP_ERR_STATE; 169 AienosCapView *v = admin->view; 170 uint64_t args[4] = { authority.cap_id, authority.generation, target.cap_id, target.generation }; 171 uint64_t rev_list[257] = { 0 }, rc = 0; 172 AienosCapEntry ents[RX_CAP_MAX]; 173 uint64_t n_rev = 0; 174 AienosCapObserver obs = NULL; 175 void *obs_ctx = NULL; 176 177 pthread_mutex_lock(&v->mu); 178 (void)omega2_cap_table_revoke(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, 179 v->delivered, RX_CAP_MAX, args, 4, rev_list, 257); 180 int res = (int)(int64_t)rc; 181 obs = v->obs; 182 obs_ctx = v->obs_ctx; 183 if (res == RX_CAP_OK && obs) { 184 n_rev = rev_list[0] <= RX_CAP_MAX ? rev_list[0] : RX_CAP_MAX; 185 for (uint64_t i = 0; i < n_rev; i++) 186 unpack_entry(&v->slots[rev_list[i + 1] * 13], &ents[i]); 187 } 188 pthread_mutex_unlock(&v->mu); 189 190 if (obs) { 191 if (res == RX_CAP_OK) { 192 for (uint64_t i = 0; i < n_rev; i++) obs(obs_ctx, AIENOS_CAP_OBS_REVOKE, &ents[i], res); 193 } else { 194 obs(obs_ctx, AIENOS_CAP_OBS_REVOKE, NULL, res); 195 } 196 } 197 return res; 198 } 199 200 int aienos_cap_reclaim(AienosCapAdmin *admin, AienosCapRef authority, uint32_t cap_id) { 201 if (!admin || !admin->view) return RX_CAP_ERR_STATE; 202 AienosCapView *v = admin->view; 203 uint64_t args[3] = { authority.cap_id, authority.generation, cap_id }; 204 uint64_t rc = 0; 205 AienosCapEntry ent; 206 int have_ent = 0; 207 AienosCapObserver obs = NULL; 208 void *obs_ctx = NULL; 209 210 pthread_mutex_lock(&v->mu); 211 (void)omega2_cap_table_reclaim(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, 212 v->delivered, RX_CAP_MAX, args, 3); 213 int res = (int)(int64_t)rc; 214 obs = v->obs; 215 obs_ctx = v->obs_ctx; 216 if (res == RX_CAP_OK && cap_id < RX_CAP_MAX) { 217 unpack_entry(&v->slots[(size_t)cap_id * 13], &ent); 218 have_ent = 1; 219 } 220 pthread_mutex_unlock(&v->mu); 221 222 if (obs) obs(obs_ctx, AIENOS_CAP_OBS_RECLAIM, have_ent ? &ent : NULL, res); 223 return res; 224 } 225 226 int aienos_cap_advance_clock(AienosCapAdmin *admin, AienosCapRef authority, uint64_t ticks) { 227 if (!admin || !admin->view) return RX_CAP_ERR_STATE; 228 AienosCapView *v = admin->view; 229 uint64_t args[3] = { authority.cap_id, authority.generation, ticks }; 230 uint64_t rc = 0; 231 AienosCapEntry ent; 232 int have_ent = 0; 233 AienosCapObserver obs = NULL; 234 void *obs_ctx = NULL; 235 236 pthread_mutex_lock(&v->mu); 237 (void)omega2_cap_table_clock(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, 238 v->delivered, RX_CAP_MAX, args, 3); 239 int res = (int)(int64_t)rc; 240 obs = v->obs; 241 obs_ctx = v->obs_ctx; 242 if (res == RX_CAP_OK && authority.cap_id < RX_CAP_MAX) { 243 unpack_entry(&v->slots[(size_t)authority.cap_id * 13], &ent); 244 have_ent = 1; 245 } 246 pthread_mutex_unlock(&v->mu); 247 248 if (obs) obs(obs_ctx, AIENOS_CAP_OBS_CLOCK, have_ent ? &ent : NULL, res); 249 return res; 250 } 251 252 int aienos_cap_bump_epoch(AienosCapAdmin *admin, AienosCapRef authority) { 253 if (!admin || !admin->view) return RX_CAP_ERR_STATE; 254 AienosCapView *v = admin->view; 255 uint64_t rc = 0; 256 AienosCapEntry ent; 257 int have_ent = 0; 258 AienosCapObserver obs = NULL; 259 void *obs_ctx = NULL; 260 261 pthread_mutex_lock(&v->mu); 262 (void)omega2_cap_table_epoch(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, 263 v->delivered, RX_CAP_MAX, authority.cap_id, authority.generation); 264 int res = (int)(int64_t)rc; 265 obs = v->obs; 266 obs_ctx = v->obs_ctx; 267 if (res == RX_CAP_OK && authority.cap_id < RX_CAP_MAX) { 268 unpack_entry(&v->slots[(size_t)authority.cap_id * 13], &ent); 269 have_ent = 1; 270 } 271 pthread_mutex_unlock(&v->mu); 272 273 if (obs) obs(obs_ctx, AIENOS_CAP_OBS_EPOCH, have_ent ? &ent : NULL, res); 274 return res; 275 } 276 277 int aienos_cap_kill(AienosCapAdmin *admin) { 278 if (!admin || !admin->view) return RX_CAP_ERR_STATE; 279 AienosCapView *v = admin->view; 280 pthread_mutex_lock(&v->mu); 281 v->hdr[3] = 1; 282 AienosCapObserver obs = v->obs; 283 void *obs_ctx = v->obs_ctx; 284 pthread_mutex_unlock(&v->mu); 285 if (obs) obs(obs_ctx, AIENOS_CAP_OBS_KILL, NULL, RX_CAP_OK); 286 return RX_CAP_OK; 287 } 288 289 int aienos_cap_restart(AienosCapAdmin *admin) { 290 if (!admin || !admin->view) return RX_CAP_ERR_STATE; 291 uint64_t boot = 0; 292 int brc = om2_take_boot_gen(&boot); 293 if (brc != RX_CAP_OK) return brc; 294 AienosCapView *v = admin->view; 295 uint64_t rc = 0; 296 AienosCapEntry ent; 297 AienosCapObserver obs = NULL; 298 void *obs_ctx = NULL; 299 300 pthread_mutex_lock(&v->mu); 301 (void)omega2_cap_table_init(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, 302 v->delivered, RX_CAP_MAX, boot); 303 int res = (int)(int64_t)rc; 304 fill_rand(v->token, sizeof v->token); 305 obs = v->obs; 306 obs_ctx = v->obs_ctx; 307 if (res == RX_CAP_OK) unpack_entry(&v->slots[0], &ent); 308 pthread_mutex_unlock(&v->mu); 309 310 if (obs) obs(obs_ctx, AIENOS_CAP_OBS_RESTART, res == RX_CAP_OK ? &ent : NULL, res); 311 return res; 312 } 313 314 int aienos_cap_validate(const AienosCapView *view, AienosCapRef cap, uint32_t subject, 315 uint64_t resource, uint32_t rights, AienosCapEntry *out) { 316 if (!view) return RX_CAP_ERR_STATE; 317 AienosCapView *v = (AienosCapView *)view; 318 uint64_t args[5] = { cap.cap_id, cap.generation, subject, resource, rights }; 319 uint64_t slot[13] = { 0 }, rc = 0; 320 pthread_mutex_lock(&v->mu); 321 (void)omega2_cap_table_validate(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, args, 5, slot, 13); 322 pthread_mutex_unlock(&v->mu); 323 int res = (int)(int64_t)rc; 324 if (res == RX_CAP_OK && out) unpack_entry(slot, out); 325 return res; 326 } 327 328 int aienos_cap_inspect(const AienosCapView *view, AienosCapRef cap, AienosCapEntry *out) { 329 if (!view) return RX_CAP_ERR_STATE; 330 AienosCapView *v = (AienosCapView *)view; 331 uint64_t slot[13] = { 0 }, rc = 0; 332 pthread_mutex_lock(&v->mu); 333 (void)omega2_cap_table_inspect(&rc, v->hdr, 4, v->slots, RX_CAP_MAX * 13, 334 cap.cap_id, cap.generation, slot, 13); 335 pthread_mutex_unlock(&v->mu); 336 int res = (int)(int64_t)rc; 337 if (res == RX_CAP_OK && out) unpack_entry(slot, out); 338 return res; 339 } 340 341 uint64_t aienos_cap_clock(const AienosCapView *view) { 342 if (!view) return 0; 343 AienosCapView *v = (AienosCapView *)view; 344 pthread_mutex_lock(&v->mu); 345 uint64_t clk = v->hdr[2]; 346 pthread_mutex_unlock(&v->mu); 347 return clk; 348 } 349 350 int aienos_cap_cognition_mint(const AienosCapView *view, const AienosCapMint *request, 351 const uint8_t *token) { 352 (void)view; (void)request; (void)token; 353 return RX_CAP_ERR_UNAUTHORIZED; 354 } 355 356 int aienos_cap_cognition_admin(const AienosCapView *view, uint32_t op, AienosCapRef authority, 357 AienosCapRef target) { 358 (void)view; (void)op; (void)authority; (void)target; 359 return RX_CAP_ERR_UNAUTHORIZED; 360 } 361 362 int aienos_cap_force_generation(AienosCapAdmin *admin, uint32_t cap_id, uint64_t generation) { 363 if (!admin || !admin->view || cap_id >= RX_CAP_MAX) return RX_CAP_ERR_BOUNDS; 364 AienosCapView *v = admin->view; 365 pthread_mutex_lock(&v->mu); 366 v->slots[(size_t)cap_id * 13 + 1] = generation; 367 pthread_mutex_unlock(&v->mu); 368 return RX_CAP_OK; 369 } 370 371 int aienos_cap_generation_advance(uint64_t generation, uint64_t *out) { 372 uint64_t next = 0, rc = 0; 373 (void)omega2_cap_gen_advance(&rc, generation, &next, 1); 374 if ((int64_t)rc == RX_CAP_OK && out) *out = next; 375 return (int)(int64_t)rc; 376 } 377 378 int aienos_cap_set_observer(AienosCapAdmin *admin, AienosCapObserver fn, void *ctx) { 379 if (!admin || !admin->view) return RX_CAP_ERR_STATE; 380 AienosCapView *v = admin->view; 381 pthread_mutex_lock(&v->mu); 382 v->obs = fn; 383 v->obs_ctx = ctx; 384 pthread_mutex_unlock(&v->mu); 385 return RX_CAP_OK; 386 } 387 388 int aienos_cap_authorize(const AienosCapAdmin *admin, const uint8_t *presented) { 389 if (!admin || !admin->view || !presented) return RX_CAP_ERR_UNAUTHORIZED; 390 AienosCapView *v = admin->view; 391 uint64_t eq = 0; 392 pthread_mutex_lock(&v->mu); 393 (void)omega2_cap_token_eq(&eq, v->token, RX_CAP_TOKEN_LEN, presented, RX_CAP_TOKEN_LEN); 394 pthread_mutex_unlock(&v->mu); 395 return eq ? RX_CAP_OK : RX_CAP_ERR_UNAUTHORIZED; 396 }