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