interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / third_party / omega / rx_cortex.c

616 lines · 21078 bytes Raw Blame History
1/*
2 * rx_cortex.c -- Cortex, canonical (M20). See rx_cortex.h.
3 */
4#include "rx_cortex.h"
5
6#include "sha256.h"
7
8#include <string.h>
9
10/* Allocation hooks: a freestanding host names a config header through
11 * CX_CONFIG_H that defines CX_MALLOC(n), CX_REALLOC(p, n) and CX_FREE(p). */
12#ifdef CX_CONFIG_H
13#include CX_CONFIG_H
14#endif
15#ifndef CX_MALLOC
16#include <stdlib.h>
17#define CX_MALLOC(n) malloc(n)
18#define CX_REALLOC(p, n) realloc((p), (n))
19#define CX_FREE(p) free(p)
20#endif
21
22#ifndef CX_NO_POSIX
23#include <errno.h>
24#include <fcntl.h>
25#include <sys/file.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#endif
29
30static void put_u64(sha256_ctx *c, uint64_t v) {
31 uint8_t b[8];
32 for (int i = 0; i < 8; i++) b[i] = (uint8_t)(v >> (8 * i));
33 sha256_update(c, b, 8);
34}
35
36void cx_digest(const CxObject *o, const uint64_t *payload, uint8_t out[32]) {
37 static const uint8_t domain[] = "AIEN.CORTEX.OBJECT.V1";
38 sha256_ctx c;
39 sha256_init(&c);
40 sha256_update(&c, domain, sizeof domain - 1);
41 put_u64(&c, o->id);
42 put_u64(&c, ((uint64_t)o->cls << 32) | o->kind);
43 put_u64(&c, o->subject);
44 put_u64(&c, o->t);
45 put_u64(&c, o->generation);
46 put_u64(&c, ((uint64_t)o->branch << 32) | o->protect);
47 put_u64(&c, o->tag);
48 for (uint32_t i = 0; i < CX_LINKS; i++) put_u64(&c, o->links[i]);
49 put_u64(&c, o->n);
50 for (uint32_t i = 0; i < o->n; i++) put_u64(&c, payload[i]);
51 sha256_final(&c, out);
52}
53
54static void chain_step(uint8_t chain[32], const uint8_t digest[32]) {
55 uint8_t buf[64];
56 memcpy(buf, chain, 32);
57 memcpy(buf + 32, digest, 32);
58 sha256_hash(buf, sizeof buf, chain);
59}
60
61int cx_init(CxStore *s, uint64_t n_subjects) {
62 memset(s, 0, sizeof(*s));
63 size_t nb = (size_t)(n_subjects ? n_subjects : 1) * sizeof(CxIdList);
64 s->by_subject = CX_MALLOC(nb);
65 if (!s->by_subject) return CX_ERR_NOMEM;
66 memset(s->by_subject, 0, nb);
67 s->n_subjects = n_subjects;
68 return CX_OK;
69}
70
71void cx_free(CxStore *s) {
72 for (uint64_t i = 0; i < s->n_subjects && s->by_subject; i++) CX_FREE(s->by_subject[i].ids);
73 CX_FREE(s->by_subject);
74 CX_FREE(s->obj);
75 CX_FREE(s->arena);
76 if (s->jops) s->jops->close(s->jctx, s->jh); /* closing drops the writer lock */
77 memset(s, 0, sizeof(*s));
78}
79
80void cx_close(CxStore *s) { cx_free(s); }
81
82static int grow(void **p, uint64_t *cap, uint64_t need, size_t elem) {
83 if (need <= *cap) return 0;
84 uint64_t c = *cap ? *cap : 1024;
85 while (c < need) c *= 2;
86 void *q = CX_REALLOC(*p, c * elem);
87 if (!q) return -1;
88 *p = q;
89 *cap = c;
90 return 0;
91}
92
93/* ---- journal encoding ---------------------------------------------------- */
94
95#define CX_J_MAGIC 0x0131584e45494141ull /* journal magic */
96#define CX_J_VERSION 1ull
97#define CX_J_HDR_WORDS 4u
98#define CX_R_MAGIC 0x4443524f43584355ull /* record marker */
99#define CX_R_FIXED 13u /* marker + 12 header words */
100#define CX_R_DIGEST 4u
101
102static void le_put(uint8_t *p, uint64_t v) {
103 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i));
104}
105static uint64_t le_get(const uint8_t *p) {
106 uint64_t v = 0;
107 for (int i = 0; i < 8; i++) v |= (uint64_t)p[i] << (8 * i);
108 return v;
109}
110
111/* Encode one object as a journal record into a fresh buffer. */
112static uint8_t *encode_record(const CxObject *o, const uint64_t *payload, size_t *out_len) {
113 size_t words = CX_R_FIXED + o->n + CX_R_DIGEST;
114 uint8_t *b = CX_MALLOC(words * 8);
115 if (!b) return NULL;
116 uint64_t w[CX_R_FIXED] = {
117 CX_R_MAGIC, o->id, ((uint64_t)o->cls << 32) | o->kind, o->subject, o->t,
118 o->generation, ((uint64_t)o->branch << 32) | o->protect, o->tag,
119 o->links[0], o->links[1], o->links[2], o->links[3], o->n
120 };
121 size_t k = 0;
122 for (uint32_t i = 0; i < CX_R_FIXED; i++, k += 8) le_put(b + k, w[i]);
123 for (uint32_t i = 0; i < o->n; i++, k += 8) le_put(b + k, payload[i]);
124 memcpy(b + k, o->digest, 32);
125 *out_len = words * 8;
126 return b;
127}
128
129/* ---- append -------------------------------------------------------------- */
130
131/* Append into memory. `expect_id` nonzero: replay, the id must match. */
132static int append_mem(CxStore *s, const CxHeader *h, const uint64_t *payload, uint32_t n,
133 uint64_t expect_id, uint64_t *out_id) {
134 if (!h || h->cls == 0 || h->cls >= CX_CLASS_END || h->subject >= s->n_subjects ||
135 (n && !payload))
136 return CX_ERR_ARG;
137 if (expect_id && expect_id != s->n + 1) return CX_ERR_FORMAT;
138 CxIdList *l = &s->by_subject[h->subject];
139 if (l->n && s->obj[l->ids[l->n - 1] - 1].t > h->t) return CX_ERR_ORDER;
140 if (grow((void **)&s->obj, &s->cap, s->n + 1, sizeof(CxObject)) ||
141 grow((void **)&s->arena, &s->arena_cap, s->arena_n + n, sizeof(uint64_t)))
142 return CX_ERR_NOMEM;
143 if (l->n == l->cap) {
144 uint32_t c = l->cap ? l->cap * 2 : 64;
145 uint64_t *q = CX_REALLOC(l->ids, (size_t)c * sizeof(uint64_t));
146 if (!q) return CX_ERR_NOMEM;
147 l->ids = q;
148 l->cap = c;
149 }
150 CxObject *o = &s->obj[s->n];
151 memset(o, 0, sizeof(*o));
152 o->id = s->n + 1;
153 o->cls = h->cls;
154 o->kind = h->kind;
155 o->subject = h->subject;
156 o->t = h->t;
157 o->generation = h->generation;
158 o->branch = h->branch;
159 o->protect = h->protect & CX_PROT_ALL;
160 o->tag = h->tag;
161 memcpy(o->links, h->links, sizeof o->links);
162 o->n = n;
163 o->off = s->arena_n;
164 if (n) memcpy(s->arena + s->arena_n, payload, (size_t)n * sizeof(uint64_t));
165 s->arena_n += n;
166 cx_digest(o, s->arena + o->off, o->digest);
167 chain_step(s->chain, o->digest);
168 l->ids[l->n++] = o->id;
169 s->n++;
170 if (out_id) *out_id = o->id;
171 return CX_OK;
172}
173
174/* Undo the newest in-memory append (journal write failed). */
175static void unappend_mem(CxStore *s, const uint8_t prev_chain[32]) {
176 CxObject *o = &s->obj[s->n - 1];
177 s->by_subject[o->subject].n--;
178 s->arena_n = o->off;
179 s->n--;
180 memcpy(s->chain, prev_chain, 32);
181}
182
183int cx_append_as(CxStore *s, uint64_t token, const CxHeader *h, const uint64_t *payload,
184 uint32_t n, uint64_t *out_id) {
185 if (!s) return CX_ERR_ARG;
186 if (s->writer != token) return CX_ERR_WRITER;
187 if (s->open_flags & CX_OPEN_READONLY) return CX_ERR_WRITER;
188 uint8_t prev[32];
189 memcpy(prev, s->chain, 32);
190 uint64_t id = 0;
191 int rc = append_mem(s, h, payload, n, 0, &id);
192 if (rc != CX_OK) return rc;
193 if (s->jops) {
194 const CxObject *o = &s->obj[id - 1];
195 size_t len = 0;
196 uint8_t *rec = encode_record(o, s->arena + o->off, &len);
197 if (!rec) {
198 unappend_mem(s, prev);
199 return CX_ERR_NOMEM;
200 }
201 /* The backend appends all of the record or none of it; memory never
202 * runs ahead of the journal. */
203 int rc2 = s->jops->append(s->jctx, s->jh, rec, len, (s->open_flags & CX_OPEN_SYNC) != 0);
204 CX_FREE(rec);
205 if (rc2 != CX_OK) {
206 unappend_mem(s, prev);
207 return rc2;
208 }
209 }
210 if (out_id) *out_id = id;
211 return CX_OK;
212}
213
214int cx_append(CxStore *s, const CxHeader *h, const uint64_t *payload, uint32_t n, uint64_t *out_id) {
215 return cx_append_as(s, 0, h, payload, n, out_id);
216}
217
218int cx_claim_writer(CxStore *s, uint64_t token) {
219 if (!s || token == 0) return CX_ERR_ARG;
220 if (s->open_flags & CX_OPEN_READONLY) return CX_ERR_WRITER;
221 if (s->writer && s->writer != token) return CX_ERR_WRITER;
222 s->writer = token;
223 return CX_OK;
224}
225
226void cx_release_writer(CxStore *s, uint64_t token) {
227 if (s && token && s->writer == token) s->writer = 0;
228}
229
230/* ---- journal open / replay ----------------------------------------------- */
231
232static int read_journal(const CxJournalOps *ops, void *ctx, void *h, uint8_t **out, size_t *len) {
233 size_t n = 0;
234 int rc = ops->size(ctx, h, &n);
235 if (rc != CX_OK) return rc;
236 uint8_t *b = CX_MALLOC(n ? n : 1);
237 if (!b) return CX_ERR_NOMEM;
238 if (n) {
239 rc = ops->read(ctx, h, 0, b, n);
240 if (rc != CX_OK) { CX_FREE(b); return rc; }
241 }
242 *out = b;
243 *len = n;
244 return CX_OK;
245}
246
247static int replay(CxStore *s, const uint8_t *b, size_t len, size_t *good_end) {
248 size_t p = CX_J_HDR_WORDS * 8;
249 *good_end = p;
250 uint64_t *payload = NULL;
251 uint32_t payload_cap = 0;
252 int rc = CX_OK;
253 while (p < len) {
254 if (len - p < CX_R_FIXED * 8) { rc = CX_ERR_TORN; break; }
255 uint64_t w[CX_R_FIXED];
256 for (uint32_t i = 0; i < CX_R_FIXED; i++) w[i] = le_get(b + p + 8 * i);
257 if (w[0] != CX_R_MAGIC || w[12] > UINT32_MAX) { rc = CX_ERR_FORMAT; break; }
258 uint32_t n = (uint32_t)w[12];
259 size_t rec = ((size_t)CX_R_FIXED + n + CX_R_DIGEST) * 8;
260 if (len - p < rec) { rc = CX_ERR_TORN; break; }
261 if (n > payload_cap) {
262 uint64_t *q = CX_REALLOC(payload, (size_t)n * sizeof(uint64_t));
263 if (!q) { rc = CX_ERR_NOMEM; break; }
264 payload = q;
265 payload_cap = n;
266 }
267 for (uint32_t i = 0; i < n; i++) payload[i] = le_get(b + p + 8 * (CX_R_FIXED + i));
268 CxHeader h;
269 memset(&h, 0, sizeof h);
270 h.cls = (uint32_t)(w[2] >> 32);
271 h.kind = (uint32_t)w[2];
272 h.subject = w[3];
273 h.t = w[4];
274 h.generation = w[5];
275 h.branch = (uint32_t)(w[6] >> 32);
276 h.protect = (uint32_t)w[6];
277 h.tag = w[7];
278 for (uint32_t i = 0; i < CX_LINKS; i++) h.links[i] = w[8 + i];
279 uint64_t id = 0;
280 rc = append_mem(s, &h, payload, n, w[1], &id);
281 if (rc != CX_OK) break;
282 if (memcmp(s->obj[id - 1].digest, b + p + 8 * (CX_R_FIXED + n), 32) != 0) {
283 rc = CX_ERR_DIGEST;
284 break;
285 }
286 p += rec;
287 *good_end = p;
288 }
289 CX_FREE(payload);
290 return rc;
291}
292
293/* The single integrity seam: see the guarantee in rx_cortex.h. */
294int cx_open_with(CxStore *s, const CxJournalOps *ops, void *ctx, const char *name,
295 uint64_t n_subjects, uint32_t flags) {
296 if (!s || !ops || !name || !ops->open || !ops->size || !ops->read || !ops->append ||
297 !ops->truncate || !ops->close)
298 return CX_ERR_ARG;
299 int ro = (flags & CX_OPEN_READONLY) != 0;
300 void *jh = NULL;
301 int rc = ops->open(ctx, name, ro, &jh);
302 if (rc != CX_OK) return rc;
303 uint8_t *b = NULL;
304 size_t len = 0;
305 rc = read_journal(ops, ctx, jh, &b, &len);
306 if (rc != CX_OK) { ops->close(ctx, jh); return rc; }
307 if (len == 0) {
308 if (ro || n_subjects == 0) { CX_FREE(b); ops->close(ctx, jh); return CX_ERR_FORMAT; }
309 uint8_t h[CX_J_HDR_WORDS * 8] = { 0 };
310 le_put(h, CX_J_MAGIC);
311 le_put(h + 8, CX_J_VERSION);
312 le_put(h + 16, n_subjects);
313 le_put(h + 24, 0); /* reserved: canonical machine identity (M20 A1) */
314 rc = ops->append(ctx, jh, h, sizeof h, 1);
315 if (rc != CX_OK) { CX_FREE(b); ops->close(ctx, jh); return rc; }
316 } else {
317 if (len < CX_J_HDR_WORDS * 8 || le_get(b) != CX_J_MAGIC || le_get(b + 8) != CX_J_VERSION) {
318 CX_FREE(b); ops->close(ctx, jh); return CX_ERR_FORMAT;
319 }
320 uint64_t file_subjects = le_get(b + 16);
321 if (n_subjects && n_subjects != file_subjects) {
322 CX_FREE(b); ops->close(ctx, jh); return CX_ERR_FORMAT;
323 }
324 n_subjects = file_subjects;
325 }
326 rc = cx_init(s, n_subjects);
327 if (rc != CX_OK) { CX_FREE(b); ops->close(ctx, jh); return rc; }
328 if (len) {
329 size_t good = 0;
330 rc = replay(s, b, len, &good);
331 if (rc == CX_ERR_TORN && !ro && (flags & CX_OPEN_REPAIR_TAIL)) {
332 /* Only an incomplete trailing record is dropped. A complete record
333 * whose digest differs is never repaired. */
334 rc = ops->truncate(ctx, jh, good);
335 }
336 }
337 CX_FREE(b);
338 if (rc != CX_OK) {
339 ops->close(ctx, jh);
340 cx_free(s);
341 return rc;
342 }
343 s->jops = ops;
344 s->jctx = ctx;
345 s->jh = jh;
346 s->open_flags = flags;
347 return CX_OK;
348}
349
350#ifndef CX_NO_POSIX
351/* ---- the file backend ---------------------------------------------------- */
352
353static int write_all(int fd, const uint8_t *b, size_t n) {
354 while (n) {
355 ssize_t w = write(fd, b, n);
356 if (w < 0) {
357 if (errno == EINTR) continue;
358 return -1;
359 }
360 b += w;
361 n -= (size_t)w;
362 }
363 return 0;
364}
365
366static int jp_open(void *ctx, const char *name, int ro, void **h) {
367 (void)ctx;
368 int fd = open(name, ro ? O_RDONLY | O_CLOEXEC : O_RDWR | O_CREAT | O_CLOEXEC, 0644);
369 if (fd < 0) return CX_ERR_IO;
370 if (!ro && flock(fd, LOCK_EX | LOCK_NB) != 0) {
371 int e = errno;
372 close(fd);
373 return e == EWOULDBLOCK ? CX_ERR_WRITER : CX_ERR_IO;
374 }
375 *h = (void *)(intptr_t)fd;
376 return CX_OK;
377}
378
379static int jp_size(void *ctx, void *h, size_t *len) {
380 (void)ctx;
381 struct stat st;
382 if (fstat((int)(intptr_t)h, &st) != 0) return CX_ERR_IO;
383 *len = (size_t)st.st_size;
384 return CX_OK;
385}
386
387static int jp_read(void *ctx, void *h, size_t off, uint8_t *out, size_t n) {
388 (void)ctx;
389 int fd = (int)(intptr_t)h;
390 size_t got = 0;
391 while (got < n) {
392 ssize_t r = pread(fd, out + got, n - got, (off_t)(off + got));
393 if (r < 0 && errno == EINTR) continue;
394 if (r <= 0) return CX_ERR_IO;
395 got += (size_t)r;
396 }
397 return CX_OK;
398}
399
400static int jp_append(void *ctx, void *h, const uint8_t *b, size_t n, int sync) {
401 (void)ctx;
402 int fd = (int)(intptr_t)h;
403 off_t end = lseek(fd, 0, SEEK_END);
404 if (end < 0) return CX_ERR_IO;
405 if (write_all(fd, b, n) != 0 || (sync && fdatasync(fd) != 0)) {
406 /* Never leave a partial record behind. */
407 if (ftruncate(fd, end) != 0) { /* reopen will see a torn tail */ }
408 return CX_ERR_IO;
409 }
410 return CX_OK;
411}
412
413static int jp_truncate(void *ctx, void *h, size_t len) {
414 (void)ctx;
415 int fd = (int)(intptr_t)h;
416 return ftruncate(fd, (off_t)len) == 0 && fdatasync(fd) == 0 ? CX_OK : CX_ERR_IO;
417}
418
419static void jp_close(void *ctx, void *h) {
420 (void)ctx;
421 close((int)(intptr_t)h); /* closing drops the flock */
422}
423
424const CxJournalOps cx_journal_posix = { jp_open, jp_size, jp_read, jp_append, jp_truncate, jp_close };
425
426int cx_open(CxStore *s, const char *path, uint64_t n_subjects, uint32_t flags) {
427 return cx_open_with(s, &cx_journal_posix, NULL, path, n_subjects, flags);
428}
429#endif /* !CX_NO_POSIX */
430
431/* ---- read ---------------------------------------------------------------- */
432
433const CxObject *cx_get(const CxStore *s, uint64_t id) {
434 return id >= 1 && id <= s->n ? &s->obj[id - 1] : NULL;
435}
436
437const uint64_t *cx_payload(const CxStore *s, const CxObject *o) { return s->arena + o->off; }
438
439int cx_verify(const CxStore *s, uint64_t id) {
440 const CxObject *o = cx_get(s, id);
441 if (!o) return CX_ERR_ARG;
442 uint8_t d[32];
443 cx_digest(o, cx_payload(s, o), d);
444 return memcmp(d, o->digest, 32) == 0 ? CX_OK : CX_ERR_DIGEST;
445}
446
447int cx_verify_chain(const CxStore *s) {
448 uint8_t chain[32] = { 0 };
449 for (uint64_t i = 0; i < s->n; i++) {
450 uint8_t d[32];
451 cx_digest(&s->obj[i], cx_payload(s, &s->obj[i]), d);
452 if (memcmp(d, s->obj[i].digest, 32) != 0) return CX_ERR_DIGEST;
453 chain_step(chain, d);
454 }
455 return memcmp(chain, s->chain, 32) == 0 ? CX_OK : CX_ERR_DIGEST;
456}
457
458int cx_promote(CxStore *s, uint64_t token, uint64_t candidate, uint64_t evidence,
459 uint64_t t, uint64_t *out_id) {
460 const CxObject *c = cx_get(s, candidate);
461 const CxObject *e = cx_get(s, evidence);
462 if (!c || !e || c->cls != CX_CLAIM || c->kind != CX_K_CANDIDATE || e->cls != CX_EVIDENCE)
463 return CX_ERR_ARG;
464 if (cx_verify(s, candidate) != CX_OK || cx_verify(s, evidence) != CX_OK) return CX_ERR_DIGEST;
465 CxHeader h;
466 memset(&h, 0, sizeof h);
467 h.cls = CX_EVIDENCE;
468 h.kind = CX_K_PROMOTION;
469 h.subject = c->subject;
470 h.t = t;
471 h.generation = c->generation;
472 h.branch = c->branch;
473 h.protect = CX_PROT_VERIFY_EVIDENCE;
474 h.links[0] = candidate;
475 h.links[1] = evidence;
476 uint64_t words[8];
477 for (int i = 0; i < 4; i++) {
478 words[i] = le_get(c->digest + 8 * i);
479 words[4 + i] = le_get(e->digest + 8 * i);
480 }
481 return cx_append_as(s, token, &h, words, 8, out_id);
482}
483
484static int matches(const CxObject *o, const CxFilter *f) {
485 if (!f) return 1;
486 if (f->cls && o->cls != f->cls) return 0;
487 if (f->kind && o->kind != f->kind) return 0;
488 if (!f->branch_any && o->branch != f->branch) return 0;
489 return 1;
490}
491
492/* First index in the subject list with t >= t0. */
493static uint32_t lower_bound(const CxStore *s, const CxIdList *l, uint64_t t0) {
494 uint32_t lo = 0, hi = l->n;
495 while (lo < hi) {
496 uint32_t mid = lo + (hi - lo) / 2;
497 if (s->obj[l->ids[mid] - 1].t < t0) lo = mid + 1;
498 else hi = mid;
499 }
500 return lo;
501}
502
503uint64_t cx_range(CxStore *s, uint64_t subject, uint64_t t0, uint64_t t1,
504 const CxFilter *f, CxVisit visit, void *user) {
505 if (subject >= s->n_subjects) return 0;
506 const CxIdList *l = &s->by_subject[subject];
507 uint64_t visited = 0;
508 for (uint32_t i = lower_bound(s, l, t0); i < l->n; i++) {
509 const CxObject *o = &s->obj[l->ids[i] - 1];
510 if (o->t > t1) break;
511 s->examined++;
512 if (!matches(o, f)) continue;
513 visited++;
514 if (visit && visit(user, o)) break;
515 }
516 return visited;
517}
518
519uint64_t cx_latest(CxStore *s, uint64_t subject, uint64_t t_max, const CxFilter *f) {
520 if (subject >= s->n_subjects) return 0;
521 const CxIdList *l = &s->by_subject[subject];
522 uint32_t i = lower_bound(s, l, t_max == UINT64_MAX ? UINT64_MAX : t_max + 1);
523 while (i > 0) {
524 const CxObject *o = &s->obj[l->ids[--i] - 1];
525 s->examined++;
526 if (matches(o, f)) return o->id;
527 }
528 return 0;
529}
530
531uint64_t cx_count_before(const CxStore *s, uint64_t subject, uint64_t t0) {
532 if (subject >= s->n_subjects) return 0;
533 return lower_bound(s, &s->by_subject[subject], t0);
534}
535
536uint64_t cx_count_subject(const CxStore *s, uint64_t subject) {
537 return subject < s->n_subjects ? s->by_subject[subject].n : 0;
538}
539
540/* ---- recall -------------------------------------------------------------- */
541
542int cx_recall_id(const CxStore *s, uint64_t id, CxRecord *out) {
543 const CxObject *o = cx_get(s, id);
544 if (!o || !out) return CX_ERR_ARG;
545 out->hdr = *o;
546 out->payload = cx_payload(s, o);
547 out->verified = cx_verify(s, id) == CX_OK;
548 return out->verified ? CX_OK : CX_ERR_DIGEST;
549}
550
551typedef struct { const CxStore *s; CxRecord *out; uint32_t n, max; } RecallCtx;
552
553static int recall_visit(void *user, const CxObject *o) {
554 RecallCtx *c = user;
555 cx_recall_id(c->s, o->id, &c->out[c->n++]);
556 return c->n >= c->max;
557}
558
559uint32_t cx_recall(CxStore *s, uint64_t subject, uint64_t t0, uint64_t t1,
560 const CxFilter *f, CxRecord *out, uint32_t max) {
561 if (!s || !out || max == 0) return 0;
562 RecallCtx c = { s, out, 0, max };
563 cx_range(s, subject, t0, t1, f, recall_visit, &c);
564 return c.n;
565}
566
567uint32_t cx_provenance(const CxStore *s, uint64_t id, uint64_t *out, uint32_t max) {
568 if (!s || !out || !cx_get(s, id)) return 0;
569 uint32_t n = 0, head = 0;
570 /* Breadth first over `out` itself; links always point to older ids. */
571 uint64_t cur = id;
572 for (;;) {
573 const CxObject *o = cx_get(s, cur);
574 for (uint32_t i = 0; o && i < CX_LINKS && n < max; i++) {
575 uint64_t l = o->links[i];
576 if (l == 0 || l >= cur || l == id) continue;
577 int seen = 0;
578 for (uint32_t j = 0; j < n && !seen; j++) seen = out[j] == l;
579 if (!seen) out[n++] = l;
580 }
581 if (head >= n || n >= max) break;
582 cur = out[head++];
583 }
584 return n;
585}
586
587int cx_world_decode(const CxRecord *r, CxWorldRecord *out) {
588 if (!r || !out || r->hdr.kind < CX_K_WORK_ACCEPTED || r->hdr.kind > CX_K_EXEC_FAILED ||
589 r->hdr.n != CX_WREC_WORDS)
590 return CX_ERR_ARG;
591 const uint64_t *p = r->payload;
592 memset(out, 0, sizeof(*out));
593 out->cx_id = r->hdr.id;
594 out->kind = r->hdr.kind;
595 out->session = p[CX_WREC_SESSION];
596 out->crumb = p[CX_WREC_CRUMB];
597 out->crumb_kind = (uint32_t)p[CX_WREC_CRUMB_KIND];
598 out->reaction = (uint32_t)p[CX_WREC_REACTION];
599 out->faculty = (uint32_t)p[CX_WREC_FACULTY];
600 out->episode = p[CX_WREC_EPISODE];
601 out->reason = (int32_t)(int64_t)p[CX_WREC_REASON];
602 out->obj = p[CX_WREC_OBJ];
603 out->obj_gen = p[CX_WREC_OBJ_GEN];
604 out->obj_version = p[CX_WREC_OBJ_VERSION];
605 for (int i = 0; i < 8; i++) out->field[i] = p[CX_WREC_FIELD0 + i];
606 for (int i = 0; i < 4; i++) le_put(out->crumb_digest + 8 * i, p[CX_WREC_DIGEST0 + i]);
607 out->n_inputs = (uint32_t)p[CX_WREC_N_INPUTS];
608 out->n_outputs = (uint32_t)p[CX_WREC_N_OUTPUTS];
609 out->cause = r->hdr.links[0];
610 return CX_OK;
611}
612
613void cx_tamper(CxStore *s, uint64_t id, uint32_t word, uint64_t value) {
614 const CxObject *o = cx_get(s, id);
615 if (o && word < o->n) s->arena[o->off + word] = value;
616}