interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / runtime / rx_caproot.c

607 lines · 21408 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * rx_caproot.c -- host reference capability root. See rx_caproot.h.
19 *
20 * The writable table stays in a separate non-dumpable process. The control
21 * socket is not authority. Every privileged operation must present a
22 * capability this process previously handed out (the delivered set lives only
23 * here, not in the readable table). R7 is not claimed: AIENOS is not this
24 * process, and this Linux mint is not the permanent root.
25 */
26#include "rx_caproot.h"
27#include "omega_native.h"
28
29#include <errno.h>
30#include <fcntl.h>
31#include <signal.h>
32#include <stddef.h>
33#include <string.h>
34#include <sys/mman.h>
35#include <sys/prctl.h>
36#include <sys/random.h>
37#include <sys/socket.h>
38#include <sys/wait.h>
39#include <time.h>
40#include <unistd.h>
41
42#ifndef F_SEAL_FUTURE_WRITE
43#define F_SEAL_FUTURE_WRITE 0x0010
44#endif
45
46typedef struct {
47 int32_t status;
48 uint32_t pad;
49 RxCapRef ref;
50} RxCapReply;
51
52typedef struct {
53 uint32_t ready;
54 uint32_t office_id;
55 uint32_t pad;
56 uint64_t office_generation;
57 int32_t status;
58 uint8_t token[RX_CAP_TOKEN_LEN];
59} RxCapHello;
60
61/* Next table's starting generation. Taken atomically so two roots in one
62 * process cannot share a generation. A later root starts higher, so a
63 * reference from a dead root cannot validate against the new table: the
64 * start is the larger of one past the last start and the time since boot in
65 * nanoseconds shifted left by 8. A slot of the dead root would need more
66 * than 256 reclaims per nanosecond to reach it. Same rule as AIENOS. */
67static _Atomic uint64_t g_last_boot_gen = 0;
68
69static uint64_t boot_time_seed(void) {
70 struct timespec ts;
71 if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) return 0;
72 uint64_t ns = (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
73 if (ns > (UINT64_MAX >> 9)) return 0;
74 return ns << 8;
75}
76
77static int take_boot_gen(uint64_t *out) {
78 uint64_t seed = boot_time_seed();
79 uint64_t cur = atomic_load_explicit(&g_last_boot_gen, memory_order_relaxed);
80 for (;;) {
81 if (cur == UINT64_MAX) return RX_CAP_ERR_EXHAUSTED;
82 uint64_t next = cur + 1u;
83 if (seed > next) next = seed;
84 if (next == UINT64_MAX) return RX_CAP_ERR_EXHAUSTED;
85 if (atomic_compare_exchange_weak_explicit(
86 &g_last_boot_gen, &cur, next,
87 memory_order_acq_rel, memory_order_relaxed)) {
88 *out = next;
89 return RX_CAP_OK;
90 }
91 }
92}
93
94static int fill_token(uint8_t token[RX_CAP_TOKEN_LEN]) {
95 size_t n = 0;
96 while (n < RX_CAP_TOKEN_LEN) {
97 ssize_t r = getrandom(token + n, RX_CAP_TOKEN_LEN - n, 0);
98 if (r < 0 && errno == EINTR) continue;
99 if (r <= 0) return -1;
100 n += (size_t)r;
101 }
102 return 0;
103}
104
105static int token_ok(const uint8_t *a, const uint8_t *b) {
106 uint64_t eq = 0;
107 (void)omega2_cap_token_eq(&eq, a, RX_CAP_TOKEN_LEN, b, RX_CAP_TOKEN_LEN);
108 return eq != 0;
109}
110
111/* ---- root process (the only writer; logic in seed/authority/cap_table_om2.om) ---- */
112
113static void root_begin(RxCapTable *t) {
114 atomic_fetch_add_explicit(&t->seq, 1, memory_order_acq_rel);
115 atomic_thread_fence(memory_order_release);
116}
117
118static void root_end(RxCapTable *t) {
119 atomic_thread_fence(memory_order_release);
120 atomic_fetch_add_explicit(&t->seq, 1, memory_order_acq_rel);
121}
122
123static void table_to_om2(const RxCapTable *t, uint64_t hdr[4], uint64_t slots[RX_CAP_MAX * 13]) {
124 hdr[0] = t->capacity;
125 hdr[1] = atomic_load_explicit(&t->epoch, memory_order_relaxed);
126 hdr[2] = atomic_load_explicit(&t->clock, memory_order_relaxed);
127 hdr[3] = 0;
128 for (uint32_t i = 0; i < RX_CAP_MAX; i++) {
129 const RxCapEntry *e = &t->entries[i];
130 uint64_t *s = &slots[(size_t)i * 13];
131 s[0] = e->cap_id;
132 s[1] = e->generation;
133 s[2] = e->state;
134 s[3] = e->issuer;
135 s[4] = e->subject;
136 s[5] = e->rights;
137 s[6] = e->resource;
138 s[7] = e->epoch;
139 s[8] = e->lease_expiry;
140 s[9] = e->parent_id;
141 s[10] = e->parent_generation;
142 s[11] = e->minted_by_id;
143 s[12] = e->minted_by_generation;
144 }
145}
146
147static void om2_to_table(RxCapTable *t, const uint64_t hdr[4], const uint64_t slots[RX_CAP_MAX * 13]) {
148 atomic_store_explicit(&t->epoch, hdr[1], memory_order_relaxed);
149 atomic_store_explicit(&t->clock, hdr[2], memory_order_relaxed);
150 for (uint32_t i = 0; i < RX_CAP_MAX; i++) {
151 RxCapEntry *e = &t->entries[i];
152 const uint64_t *s = &slots[(size_t)i * 13];
153 e->cap_id = (uint32_t)s[0];
154 e->generation = s[1];
155 e->state = (uint32_t)s[2];
156 e->issuer = (uint32_t)s[3];
157 e->subject = (uint32_t)s[4];
158 e->rights = (uint32_t)s[5];
159 e->resource = s[6];
160 e->epoch = s[7];
161 e->lease_expiry = s[8];
162 e->parent_id = (uint32_t)s[9];
163 e->parent_generation = s[10];
164 e->minted_by_id = (uint32_t)s[11];
165 e->minted_by_generation = s[12];
166 }
167}
168
169static void om2_unpack_entry(const uint64_t s[13], RxCapEntry *e) {
170 e->cap_id = (uint32_t)s[0];
171 e->generation = s[1];
172 e->state = (uint32_t)s[2];
173 e->issuer = (uint32_t)s[3];
174 e->subject = (uint32_t)s[4];
175 e->rights = (uint32_t)s[5];
176 e->resource = s[6];
177 e->epoch = s[7];
178 e->lease_expiry = s[8];
179 e->parent_id = (uint32_t)s[9];
180 e->parent_generation = s[10];
181 e->minted_by_id = (uint32_t)s[11];
182 e->minted_by_generation = s[12];
183}
184
185static int auth_use(RxCapTable *t, const uint8_t *delivered, RxCapRef a,
186 uint32_t need, const RxCapEntry **out) {
187 uint64_t hdr[4], slots[RX_CAP_MAX * 13], rc = 0;
188 table_to_om2(t, hdr, slots);
189 uint64_t args[3] = { a.cap_id, a.generation, need };
190 (void)omega2_cap_table_auth_use(&rc, hdr, 4, slots, RX_CAP_MAX * 13, delivered, RX_CAP_MAX,
191 args, 3);
192 int res = (int)(int64_t)rc;
193 if (res == RX_CAP_OK && out) *out = &t->entries[a.cap_id];
194 return res;
195}
196
197static int root_mint(RxCapTable *t, const uint8_t *delivered, uint8_t *delivered_mut,
198 const RxCapMint *m, RxCapRef *out) {
199 (void)delivered;
200 uint64_t hdr[4], slots[RX_CAP_MAX * 13], ref[2] = { 0, 0 }, rc = 0;
201 table_to_om2(t, hdr, slots);
202 uint64_t req[9] = {
203 m->issuer, m->subject, m->resource, m->rights, m->lease_ticks,
204 m->parent.cap_id, m->parent.generation, m->authority.cap_id, m->authority.generation
205 };
206 (void)omega2_cap_table_mint(&rc, hdr, 4, slots, RX_CAP_MAX * 13, delivered_mut, RX_CAP_MAX,
207 req, 9, ref, 2);
208 int res = (int)(int64_t)rc;
209 if (res == RX_CAP_OK) {
210 root_begin(t);
211 om2_to_table(t, hdr, slots);
212 root_end(t);
213 out->cap_id = (uint32_t)ref[0];
214 out->generation = ref[1];
215 }
216 return res;
217}
218
219static int root_revoke(RxCapTable *t, const uint8_t *delivered, RxCapRef authority, RxCapRef r) {
220 uint64_t hdr[4], slots[RX_CAP_MAX * 13], rev_list[257] = { 0 }, rc = 0;
221 table_to_om2(t, hdr, slots);
222 uint64_t args[4] = { authority.cap_id, authority.generation, r.cap_id, r.generation };
223 (void)omega2_cap_table_revoke(&rc, hdr, 4, slots, RX_CAP_MAX * 13, delivered, RX_CAP_MAX,
224 args, 4, rev_list, 257);
225 int res = (int)(int64_t)rc;
226 if (res == RX_CAP_OK) {
227 root_begin(t);
228 om2_to_table(t, hdr, slots);
229 root_end(t);
230 }
231 return res;
232}
233
234static int root_reclaim(RxCapTable *t, const uint8_t *delivered, uint8_t *delivered_mut,
235 RxCapRef authority, uint32_t id) {
236 (void)delivered;
237 uint64_t hdr[4], slots[RX_CAP_MAX * 13], rc = 0;
238 table_to_om2(t, hdr, slots);
239 uint64_t args[3] = { authority.cap_id, authority.generation, id };
240 (void)omega2_cap_table_reclaim(&rc, hdr, 4, slots, RX_CAP_MAX * 13, delivered_mut, RX_CAP_MAX,
241 args, 3);
242 int res = (int)(int64_t)rc;
243 if (res == RX_CAP_OK) {
244 root_begin(t);
245 om2_to_table(t, hdr, slots);
246 root_end(t);
247 }
248 return res;
249}
250
251static int root_clock(RxCapTable *t, const uint8_t *delivered, RxCapRef authority, uint64_t ticks) {
252 uint64_t hdr[4], slots[RX_CAP_MAX * 13], rc = 0;
253 table_to_om2(t, hdr, slots);
254 uint64_t args[3] = { authority.cap_id, authority.generation, ticks };
255 (void)omega2_cap_table_clock(&rc, hdr, 4, slots, RX_CAP_MAX * 13, delivered, RX_CAP_MAX,
256 args, 3);
257 int res = (int)(int64_t)rc;
258 if (res == RX_CAP_OK) {
259 root_begin(t);
260 atomic_store_explicit(&t->clock, hdr[2], memory_order_relaxed);
261 root_end(t);
262 }
263 return res;
264}
265
266static int root_epoch(RxCapTable *t, const uint8_t *delivered, RxCapRef authority) {
267 uint64_t hdr[4], slots[RX_CAP_MAX * 13], rc = 0;
268 table_to_om2(t, hdr, slots);
269 (void)omega2_cap_table_epoch(&rc, hdr, 4, slots, RX_CAP_MAX * 13, delivered, RX_CAP_MAX,
270 authority.cap_id, authority.generation);
271 int res = (int)(int64_t)rc;
272 if (res == RX_CAP_OK) {
273 root_begin(t);
274 atomic_store_explicit(&t->epoch, hdr[1], memory_order_relaxed);
275 root_end(t);
276 }
277 return res;
278}
279
280static int read_full(int fd, void *buf, size_t n) {
281 uint8_t *p = buf;
282 while (n) {
283 ssize_t r = read(fd, p, n);
284 if (r < 0 && errno == EINTR) continue;
285 if (r <= 0) return -1;
286 p += r;
287 n -= (size_t)r;
288 }
289 return 0;
290}
291
292static int write_full(int fd, const void *buf, size_t n) {
293 const uint8_t *p = buf;
294 while (n) {
295 ssize_t r = write(fd, p, n);
296 if (r < 0 && errno == EINTR) continue;
297 if (r <= 0) return -1;
298 p += r;
299 n -= (size_t)r;
300 }
301 return 0;
302}
303
304static void root_main(int memfd, int sock) {
305 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
306 prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
307 uint64_t boot_gen = 0;
308 if (read_full(sock, &boot_gen, sizeof(boot_gen)) != 0) _exit(2);
309 if (boot_gen == 0 || boot_gen == UINT64_MAX) _exit(2);
310
311 RxCapTable *t = mmap(NULL, sizeof(RxCapTable), PROT_READ | PROT_WRITE,
312 MAP_SHARED, memfd, 0);
313 close(memfd);
314 if (t == MAP_FAILED) _exit(2);
315 memset(t, 0, sizeof(*t));
316 t->magic = RX_CAP_TABLE_MAGIC;
317 t->capacity = RX_CAP_MAX;
318 atomic_store(&t->epoch, 1);
319 for (uint32_t i = 0; i < RX_CAP_MAX; i++) {
320 t->entries[i].cap_id = i;
321 t->entries[i].generation = boot_gen;
322 t->entries[i].state = RX_CAP_FREE;
323 t->entries[i].parent_id = UINT32_MAX;
324 }
325
326 /* Bootstrap office. Not requested over the socket. Privileged rights only,
327 * no DELEGATE, so they cannot be handed on by attenuation. */
328 uint8_t delivered[RX_CAP_MAX];
329 memset(delivered, 0, sizeof(delivered));
330 RxCapEntry *office = &t->entries[0];
331 office->state = RX_CAP_LIVE;
332 office->issuer = 0;
333 office->subject = 0;
334 office->resource = RX_CAP_RES_AUTHORITY;
335 office->rights = RX_RIGHT_PRIVILEGED;
336 office->epoch = 1;
337 office->parent_id = UINT32_MAX;
338 delivered[0] = 1;
339
340 RxCapHello hello;
341 memset(&hello, 0, sizeof(hello));
342 hello.ready = 1;
343 hello.office_id = office->cap_id;
344 hello.office_generation = office->generation;
345 hello.status = RX_CAP_OK;
346 if (fill_token(hello.token) != 0) _exit(2);
347 if (write_full(sock, &hello, sizeof(hello)) != 0) _exit(3);
348 uint8_t token[RX_CAP_TOKEN_LEN];
349 memcpy(token, hello.token, sizeof(token));
350
351 RxCapRequest q;
352 while (read_full(sock, &q, sizeof(q)) == 0) {
353 RxCapReply r;
354 memset(&r, 0, sizeof(r));
355 if (!token_ok(q.token, token)) {
356 r.status = RX_CAP_ERR_UNAUTHORIZED;
357 if (write_full(sock, &r, sizeof(r)) != 0) break;
358 continue;
359 }
360 switch (q.op) {
361 case RX_OP_MINT:
362 r.status = root_mint(t, delivered, delivered, &q.mint, &r.ref);
363 break;
364 case RX_OP_REVOKE:
365 r.status = root_revoke(t, delivered, q.authority, q.ref);
366 break;
367 case RX_OP_RECLAIM:
368 r.status = root_reclaim(t, delivered, delivered, q.authority, q.ref.cap_id);
369 break;
370 case RX_OP_CLOCK:
371 r.status = root_clock(t, delivered, q.authority, q.arg);
372 break;
373 case RX_OP_EPOCH:
374 r.status = root_epoch(t, delivered, q.authority);
375 break;
376 case RX_OP_SHUTDOWN:
377 r.status = auth_use(t, delivered, q.authority, RX_RIGHT_MINT, NULL);
378 if (write_full(sock, &r, sizeof(r)) != 0) _exit(4);
379 if (r.status == RX_CAP_OK) _exit(0);
380 continue;
381 default:
382 r.status = RX_CAP_ERR_STATE;
383 break;
384 }
385 if (write_full(sock, &r, sizeof(r)) != 0) break;
386 }
387 _exit(0);
388}
389
390/* ---- runtime side ------------------------------------------------------- */
391
392int rx_caproot_start(RxCapRoot *root, RxCapAdmin *admin) {
393 if (!root || !admin) return RX_CAP_ERR_STATE;
394 memset(root, 0, sizeof(*root));
395 memset(admin, 0, sizeof(*admin));
396 root->ro_fd = -1;
397 admin->ctl_fd = -1;
398 admin->office.cap_id = UINT32_MAX;
399 uint64_t boot = 0;
400 int brc = take_boot_gen(&boot);
401 if (brc != RX_CAP_OK) return brc;
402 int memfd = memfd_create("aien-capability-root", MFD_ALLOW_SEALING | MFD_CLOEXEC);
403 if (memfd < 0) return RX_CAP_ERR_IO;
404 if (ftruncate(memfd, sizeof(RxCapTable)) != 0) { close(memfd); return RX_CAP_ERR_IO; }
405 int sv[2];
406 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) != 0) {
407 close(memfd);
408 return RX_CAP_ERR_IO;
409 }
410 pid_t pid = fork();
411 if (pid < 0) { close(memfd); close(sv[0]); close(sv[1]); return RX_CAP_ERR_IO; }
412 if (pid == 0) {
413 close(sv[0]);
414 root_main(memfd, sv[1]);
415 }
416 close(sv[1]);
417 if (write_full(sv[0], &boot, sizeof(boot)) != 0) goto fail;
418 RxCapHello hello;
419 memset(&hello, 0, sizeof(hello));
420 if (read_full(sv[0], &hello, sizeof(hello)) != 0 || hello.ready != 1 ||
421 hello.status != RX_CAP_OK)
422 goto fail;
423 if (fcntl(memfd, F_ADD_SEALS,
424 F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_FUTURE_WRITE | F_SEAL_SEAL) != 0)
425 goto fail;
426 const RxCapTable *t = mmap(NULL, sizeof(RxCapTable), PROT_READ, MAP_SHARED, memfd, 0);
427 if (t == MAP_FAILED) goto fail;
428 if (t->magic != RX_CAP_TABLE_MAGIC) { munmap((void *)t, sizeof(RxCapTable)); goto fail; }
429 root->table = t;
430 root->ro_fd = memfd;
431 root->root_pid = pid;
432 root->running = true;
433 admin->ctl_fd = sv[0];
434 admin->root_pid = pid;
435 admin->running = true;
436 admin->office.cap_id = hello.office_id;
437 admin->office.generation = hello.office_generation;
438 memcpy(admin->token, hello.token, RX_CAP_TOKEN_LEN);
439 return RX_CAP_OK;
440fail:
441 kill(pid, SIGKILL);
442 waitpid(pid, NULL, 0);
443 close(memfd);
444 close(sv[0]);
445 return RX_CAP_ERR_IO;
446}
447
448static int request(RxCapAdmin *admin, RxCapRequest *q, RxCapReply *r) {
449 if (!admin || !admin->running) return RX_CAP_ERR_IO;
450 memcpy(q->token, admin->token, RX_CAP_TOKEN_LEN);
451 if (write_full(admin->ctl_fd, q, sizeof(*q)) != 0) return RX_CAP_ERR_IO;
452 if (read_full(admin->ctl_fd, r, sizeof(*r)) != 0) return RX_CAP_ERR_IO;
453 return r->status;
454}
455
456void rx_caproot_stop(RxCapRoot *root, RxCapAdmin *admin) {
457 if (!root || !admin) return;
458 if (admin->running) {
459 RxCapRequest q;
460 memset(&q, 0, sizeof(q));
461 q.op = RX_OP_SHUTDOWN;
462 q.authority = admin->office;
463 RxCapReply r;
464 if (request(admin, &q, &r) != RX_CAP_OK && admin->root_pid > 0)
465 kill(admin->root_pid, SIGKILL);
466 }
467 if (admin->root_pid > 0) waitpid(admin->root_pid, NULL, 0);
468 if (root->table) munmap((void *)root->table, sizeof(RxCapTable));
469 if (admin->ctl_fd >= 0) close(admin->ctl_fd);
470 if (root->ro_fd >= 0) close(root->ro_fd);
471 memset(admin->token, 0, sizeof(admin->token));
472 root->table = NULL;
473 root->ro_fd = -1;
474 root->running = false;
475 admin->ctl_fd = -1;
476 admin->running = false;
477}
478
479RxCapRef rx_capadmin_office(const RxCapAdmin *admin) {
480 return admin ? admin->office : (RxCapRef){ UINT32_MAX, 0 };
481}
482
483int rx_capadmin_mint(RxCapAdmin *admin, const RxCapMint *req, RxCapRef *out) {
484 RxCapRequest q;
485 memset(&q, 0, sizeof(q));
486 q.op = RX_OP_MINT;
487 q.mint = *req;
488 RxCapReply r;
489 int rc = request(admin, &q, &r);
490 if (rc == RX_CAP_OK && out) *out = r.ref;
491 return rc;
492}
493
494int rx_capadmin_revoke(RxCapAdmin *admin, RxCapRef authority, RxCapRef ref) {
495 RxCapRequest q;
496 memset(&q, 0, sizeof(q));
497 q.op = RX_OP_REVOKE;
498 q.authority = authority;
499 q.ref = ref;
500 RxCapReply r;
501 return request(admin, &q, &r);
502}
503
504int rx_capadmin_reclaim(RxCapAdmin *admin, RxCapRef authority, uint32_t cap_id) {
505 RxCapRequest q;
506 memset(&q, 0, sizeof(q));
507 q.op = RX_OP_RECLAIM;
508 q.authority = authority;
509 q.ref.cap_id = cap_id;
510 RxCapReply r;
511 return request(admin, &q, &r);
512}
513
514int rx_capadmin_advance_clock(RxCapAdmin *admin, RxCapRef authority, uint64_t ticks) {
515 RxCapRequest q;
516 memset(&q, 0, sizeof(q));
517 q.op = RX_OP_CLOCK;
518 q.authority = authority;
519 q.arg = ticks;
520 RxCapReply r;
521 return request(admin, &q, &r);
522}
523
524int rx_capadmin_bump_epoch(RxCapAdmin *admin, RxCapRef authority) {
525 RxCapRequest q;
526 memset(&q, 0, sizeof(q));
527 q.op = RX_OP_EPOCH;
528 q.authority = authority;
529 RxCapReply r;
530 return request(admin, &q, &r);
531}
532
533static int writer_dead(const RxCapRoot *root) {
534 if (!root->running || root->root_pid <= 0) return 1;
535 if (kill(root->root_pid, 0) == 0) return 0;
536 return errno == ESRCH;
537}
538
539int rx_caproot_inspect(const RxCapRoot *root, RxCapRef ref, RxCapEntry *out) {
540 const RxCapTable *t = root->table;
541 if (!t || ref.cap_id >= RX_CAP_MAX) return RX_CAP_ERR_BOUNDS;
542 for (;;) {
543 uint64_t s0 = atomic_load_explicit(&t->seq, memory_order_acquire);
544 if (s0 & 1u) {
545 if (writer_dead(root)) return RX_CAP_ERR_IO;
546 continue;
547 }
548 uint64_t hdr[4], slots[RX_CAP_MAX * 13];
549 table_to_om2(t, hdr, slots);
550 atomic_thread_fence(memory_order_acquire);
551 if (atomic_load_explicit(&t->seq, memory_order_relaxed) != s0) continue;
552 uint64_t ent[13] = { 0 }, rc = 0;
553 (void)omega2_cap_table_inspect(&rc, hdr, 4, slots, RX_CAP_MAX * 13,
554 ref.cap_id, ref.generation, ent, 13);
555 int res = (int)(int64_t)rc;
556 if (res == RX_CAP_OK && out) om2_unpack_entry(ent, out);
557 return res;
558 }
559}
560
561int rx_caproot_validate(const RxCapRoot *root, RxCapRef ref,
562 uint32_t subject, uint64_t resource, uint32_t rights,
563 RxCapEntry *out_entry) {
564 const RxCapTable *t = root->table;
565 if (!t || ref.cap_id >= RX_CAP_MAX) return RX_CAP_ERR_BOUNDS;
566 for (;;) {
567 uint64_t s0 = atomic_load_explicit(&t->seq, memory_order_acquire);
568 if (s0 & 1u) {
569 if (writer_dead(root)) return RX_CAP_ERR_IO;
570 continue;
571 }
572 uint64_t hdr[4], slots[RX_CAP_MAX * 13];
573 table_to_om2(t, hdr, slots);
574 atomic_thread_fence(memory_order_acquire);
575 if (atomic_load_explicit(&t->seq, memory_order_relaxed) != s0) continue;
576 uint64_t args[5] = { ref.cap_id, ref.generation, subject, resource, rights };
577 uint64_t ent[13] = { 0 }, rc = 0;
578 (void)omega2_cap_table_validate(&rc, hdr, 4, slots, RX_CAP_MAX * 13, args, 5, ent, 13);
579 int res = (int)(int64_t)rc;
580 if (res == RX_CAP_OK && out_entry) om2_unpack_entry(ent, out_entry);
581 return res;
582 }
583}
584
585const char *rx_cap_strerror(int code) {
586 switch (code) {
587 case RX_CAP_OK: return "ok";
588 case RX_CAP_ERR_BOUNDS: return "bounds";
589 case RX_CAP_ERR_STALE_GEN: return "stale-generation";
590 case RX_CAP_ERR_REVOKED: return "revoked";
591 case RX_CAP_ERR_EPOCH: return "epoch";
592 case RX_CAP_ERR_SUBJECT: return "wrong-subject";
593 case RX_CAP_ERR_RESOURCE: return "wrong-resource";
594 case RX_CAP_ERR_RIGHTS: return "insufficient-rights";
595 case RX_CAP_ERR_EXPIRED: return "lease-expired";
596 case RX_CAP_ERR_CHAIN: return "delegation-chain-invalid";
597 case RX_CAP_ERR_AMPLIFY: return "rights-amplification";
598 case RX_CAP_ERR_NOT_DELEGABLE: return "not-delegable";
599 case RX_CAP_ERR_FULL: return "table-full";
600 case RX_CAP_ERR_IO: return "root-io";
601 case RX_CAP_ERR_STATE: return "bad-state";
602 case RX_CAP_ERR_UNAUTHORIZED: return "unauthorized";
603 case RX_CAP_ERR_OVERFLOW: return "overflow";
604 case RX_CAP_ERR_EXHAUSTED: return "generation-exhausted";
605 default: return "unknown";
606 }
607}