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/rx_generation.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 / rx_generation.c` 1725 lines ยท 65646 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 * One active generation on disk. A candidate lives beside it until the 19 * pointer file is replaced by a rename. Recovery trusts a complete pointer 20 * and a complete root, or it refuses. It does not splice the two. 21 */ 22 #include "runtime/rx_generation.h" 23 #include "runtime/rx_argus.h" 24 #include "sha256.h" 25 #include "omega_path_check.h" 26 27 #include 28 #include 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 37 #define ROOT_BYTES 512 38 #define ROOT_SUM 480 39 #define ROOT_AUTH_GEN_HI 432 /* after the excluded ids, before the sum */ 40 #define ROOT_WORK_LEN 436 41 #define ROOT_WORK_DIGEST 444 42 #define ROOT_WORK_COUNT 476 43 #define PTR_BYTES 128 44 #define PTR_SUM 96 45 #define JRN_BYTES 128 46 #define JRN_SUM 96 47 #define RCT_BYTES 256 48 #define RCT_SUM 224 49 #define WORK_HEADER_BYTES 16u 50 #define WORK_ROW_BYTES 80u 51 #define WORK_BLOB_BYTES (WORK_HEADER_BYTES + RX_GEN_MAX_WORK * WORK_ROW_BYTES) 52 #define MAX_BLOB 65536u 53 #define MAX_CAND 4 54 55 enum { 56 PHASE_NONE = 0, 57 PHASE_CANDIDATE = 1, 58 PHASE_FLIPPED = 2, 59 PHASE_RECEIPT = 3 60 }; 61 62 enum { 63 DIG_OBJECT = 0, 64 DIG_EVIDENCE, 65 DIG_MODEL, 66 DIG_REALIZATION, 67 DIG_CONFIG, 68 DIG_PROVENANCE, 69 DIG_COUNT 70 }; 71 72 typedef struct { 73 uint64_t id; 74 uint64_t parent; 75 uint64_t lineage; 76 uint32_t state; 77 uint32_t n_objects; 78 uint64_t authority_epoch; 79 uint64_t authority_generation; 80 uint32_t n_external; 81 uint32_t n_excluded; 82 uint32_t flags; 83 uint64_t lengths[DIG_COUNT]; 84 uint8_t digest[DIG_COUNT][32]; 85 uint64_t external_ids[RX_GEN_MAX_EXTERNAL]; 86 uint64_t excluded_ids[RX_GEN_MAX_EXCLUDED]; 87 uint64_t work_len; 88 uint8_t work_digest[32]; 89 uint32_t n_work; 90 } RootView; 91 92 typedef struct { 93 int used; 94 uint64_t id; 95 uint64_t parent_id; 96 uint32_t proposer; 97 int closing; 98 int draining; 99 int proofs_ok; 100 uint64_t authority_epoch; 101 uint64_t authority_generation; 102 RxGenObject objects[RX_GEN_MAX_OBJECTS]; 103 uint32_t n_objects; 104 int observed_set[RX_GEN_MAX_OBJECTS]; 105 uint32_t observed_gen[RX_GEN_MAX_OBJECTS]; 106 uint8_t *blob[DIG_COUNT]; 107 size_t blob_len[DIG_COUNT]; 108 RxGenWork work[RX_GEN_MAX_WORK]; 109 uint32_t n_work; 110 } Candidate; 111 112 struct RxGenStore { 113 char dir[384]; 114 uint64_t active_id; 115 uint64_t active_lineage; 116 uint64_t next_id; 117 int crash_step; 118 int lock_fd; 119 Candidate cand[MAX_CAND]; 120 RxGenDiskHook disk_hook; 121 void *disk_ctx; 122 RxGenPhases phases; 123 uint64_t io_bytes, io_syncs; /* this store's writes and syncs (R15) */ 124 /* Guards active_id/active_lineage, which the durable executor moves while 125 * a reaction may read them. Held only for the copy. */ 126 pthread_mutex_t active_mu; 127 struct RxGenExec *exec; 128 /* R16 C5 (rx_gen_bind_authority): set once, never cleared. */ 129 int bound; 130 RxGenCallerFn caller; 131 void *caller_ctx; 132 RxGenAuthFn bound_auth; 133 void *bound_auth_ctx; 134 }; 135 136 /* R16 C5: bound once (release), read with acquire by every caller. */ 137 static int store_bound(const RxGenStore *store) { 138 return __atomic_load_n(&store->bound, __ATOMIC_ACQUIRE); 139 } 140 141 static void set_active(RxGenStore *store, uint64_t id, uint64_t lineage) { 142 pthread_mutex_lock(&store->active_mu); 143 store->active_id = id; 144 store->active_lineage = lineage; 145 pthread_mutex_unlock(&store->active_mu); 146 } 147 148 static uint64_t monotonic_ns(void) { 149 struct timespec ts; 150 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return 0; 151 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 152 } 153 154 int rx_gen_last_phases(const RxGenStore *store, RxGenPhases *out) { 155 if (!store || !out) return RX_GEN_ERR_ARG; 156 *out = store->phases; 157 return store->phases.enter_ns ? RX_GEN_OK : RX_GEN_ERR_MISSING; 158 } 159 160 static void put_u32(uint8_t *p, uint32_t v) { 161 p[0] = (uint8_t)v; 162 p[1] = (uint8_t)(v >> 8); 163 p[2] = (uint8_t)(v >> 16); 164 p[3] = (uint8_t)(v >> 24); 165 } 166 167 static void put_u64(uint8_t *p, uint64_t v) { 168 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); 169 } 170 171 static uint32_t get_u32(const uint8_t *p) { 172 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | 173 ((uint32_t)p[3] << 24); 174 } 175 176 static uint64_t get_u64(const uint8_t *p) { 177 uint64_t v = 0; 178 for (int i = 0; i < 8; i++) v |= (uint64_t)p[i] << (8 * i); 179 return v; 180 } 181 182 static void checksum(const uint8_t *p, size_t n, uint8_t out[32]) { 183 sha256_hash(p, n, out); 184 } 185 186 /* R15: bytes written and sync calls issued by every store in the process. */ 187 static uint64_t g_io_bytes, g_io_syncs; 188 void rx_gen_io_counters(uint64_t *bytes, uint64_t *syncs) { 189 if (bytes) *bytes = __atomic_load_n(&g_io_bytes, __ATOMIC_RELAXED); 190 if (syncs) *syncs = __atomic_load_n(&g_io_syncs, __ATOMIC_RELAXED); 191 } 192 193 int rx_gen_store_io(const RxGenStore *store, uint64_t *bytes, uint64_t *syncs) { 194 if (!store) return RX_GEN_ERR_ARG; 195 if (bytes) *bytes = __atomic_load_n(&store->io_bytes, __ATOMIC_RELAXED); 196 if (syncs) *syncs = __atomic_load_n(&store->io_syncs, __ATOMIC_RELAXED); 197 return RX_GEN_OK; 198 } 199 200 /* Every write and sync is charged to the store that issued it and to the 201 * process total. */ 202 static int write_full(RxGenStore *store, int fd, const uint8_t *p, size_t n) { 203 size_t off = 0; 204 while (off < n) { 205 ssize_t w = write(fd, p + off, n - off); 206 if (w < 0) { 207 if (errno == EINTR) continue; 208 return -1; 209 } 210 if (w == 0) return -1; 211 off += (size_t)w; 212 __atomic_add_fetch(&g_io_bytes, (uint64_t)w, __ATOMIC_RELAXED); 213 __atomic_add_fetch(&store->io_bytes, (uint64_t)w, __ATOMIC_RELAXED); 214 } 215 return 0; 216 } 217 218 static int read_full(const char *path, uint8_t *buf, size_t n) { 219 int fd = open(path, O_RDONLY); 220 if (fd < 0) return -1; 221 size_t off = 0; 222 while (off < n) { 223 ssize_t r = read(fd, buf + off, n - off); 224 if (r < 0) { 225 if (errno == EINTR) continue; 226 close(fd); 227 return -1; 228 } 229 if (r == 0) { 230 close(fd); 231 return -1; 232 } 233 off += (size_t)r; 234 } 235 close(fd); 236 return 0; 237 } 238 239 static int fsync_fd(RxGenStore *store, int fd) { 240 __atomic_add_fetch(&g_io_syncs, 1, __ATOMIC_RELAXED); 241 __atomic_add_fetch(&store->io_syncs, 1, __ATOMIC_RELAXED); 242 return fsync(fd) == 0 ? 0 : -1; 243 } 244 245 static int fsync_path(RxGenStore *store, const char *path) { 246 int fd = open(path, O_RDONLY); 247 if (fd < 0) return -1; 248 int rc = fsync_fd(store, fd); 249 close(fd); 250 return rc; 251 } 252 253 static int ensure_dir(const char *path) { 254 if (mkdir(path, 0755) == 0 || errno == EEXIST) return 0; 255 return -1; 256 } 257 258 static void crash_now(void) { _exit(86); } 259 260 static int path_join(char *out, size_t n, const char *a, const char *b) { 261 int wrote = snprintf(out, n, "%s/%s", a, b); 262 if (wrote < 0 || (size_t)wrote >= n) return -1; 263 return 0; 264 } 265 266 static Candidate *find_cand(RxGenStore *store, uint64_t id) { 267 for (int i = 0; i < MAX_CAND; i++) 268 if (store->cand[i].used && store->cand[i].id == id) return &store->cand[i]; 269 return NULL; 270 } 271 272 static uint64_t work_resource_value(const RxGenStore *store, const Candidate *c, 273 uint64_t work_id) { 274 static const uint8_t domain[] = "omega.rx.generation.work.finish.v1"; 275 uint8_t ids[24], digest[32]; 276 put_u64(ids, c->parent_id); 277 put_u64(ids + 8, c->id); 278 put_u64(ids + 16, work_id); 279 /* Length-prefix the directory. A raw concatenation aliases a longer path 280 * with a shorter path whose next hashed bytes continue it. */ 281 uint64_t dir_len = strlen(store->dir); 282 uint8_t dir_len_le[8]; 283 put_u64(dir_len_le, dir_len); 284 sha256_ctx ctx; 285 sha256_init(&ctx); 286 sha256_update(&ctx, domain, sizeof domain - 1); 287 sha256_update(&ctx, dir_len_le, sizeof dir_len_le); 288 sha256_update(&ctx, (const uint8_t *)store->dir, (size_t)dir_len); 289 sha256_update(&ctx, ids, sizeof ids); 290 sha256_final(&ctx, digest); 291 uint64_t resource = get_u64(digest); 292 return resource ? resource : UINT64_MAX; 293 } 294 295 int rx_gen_work_resource(const RxGenStore *store, uint64_t candidate, uint64_t work_id, 296 uint64_t *out_resource) { 297 if (!store || !out_resource) return RX_GEN_ERR_ARG; 298 Candidate *c = find_cand((RxGenStore *)store, candidate); 299 if (!c) return RX_GEN_ERR_ARG; 300 *out_resource = work_resource_value(store, c, work_id); 301 return RX_GEN_OK; 302 } 303 304 static int copy_blob(uint8_t **dst, size_t *dst_n, const uint8_t *src, size_t n) { 305 if (n > MAX_BLOB) return RX_GEN_ERR_ARG; 306 uint8_t *p = NULL; 307 if (n) { 308 p = malloc(n); 309 if (!p) return RX_GEN_ERR_IO; 310 memcpy(p, src, n); 311 } 312 free(*dst); 313 *dst = p; 314 *dst_n = n; 315 return RX_GEN_OK; 316 } 317 318 static void encode_root(const RootView *v, uint8_t out[ROOT_BYTES]) { 319 memset(out, 0, ROOT_BYTES); 320 memcpy(out, "R9ROOT3", 7); 321 put_u64(out + 8, v->id); 322 put_u64(out + 16, v->parent); 323 put_u64(out + 24, v->lineage); 324 put_u32(out + 32, v->state); 325 put_u32(out + 36, v->n_objects); 326 put_u64(out + 40, v->authority_epoch); 327 put_u32(out + 48, (uint32_t)v->authority_generation); 328 put_u32(out + 52, v->n_external); 329 put_u32(out + 56, v->n_excluded); 330 put_u32(out + 60, v->flags); 331 for (int i = 0; i < DIG_COUNT; i++) put_u64(out + 64 + (size_t)i * 8, v->lengths[i]); 332 for (int i = 0; i < DIG_COUNT; i++) 333 memcpy(out + 112 + (size_t)i * 32, v->digest[i], 32); 334 for (uint32_t i = 0; i < v->n_external && i < RX_GEN_MAX_EXTERNAL; i++) 335 put_u64(out + 304 + (size_t)i * 8, v->external_ids[i]); 336 for (uint32_t i = 0; i < v->n_excluded && i < RX_GEN_MAX_EXCLUDED; i++) 337 put_u64(out + 368 + (size_t)i * 8, v->excluded_ids[i]); 338 put_u32(out + ROOT_AUTH_GEN_HI, (uint32_t)(v->authority_generation >> 32)); 339 put_u64(out + ROOT_WORK_LEN, v->work_len); 340 memcpy(out + ROOT_WORK_DIGEST, v->work_digest, sizeof v->work_digest); 341 put_u32(out + ROOT_WORK_COUNT, v->n_work); 342 checksum(out, ROOT_SUM, out + ROOT_SUM); 343 } 344 345 static int decode_root(const uint8_t in[ROOT_BYTES], RootView *v) { 346 /* Version 2 keeps the high half of the 64-bit authority generation; 347 * version 3 also commits the durable work-completion provenance. */ 348 int v3 = memcmp(in, "R9ROOT3", 7) == 0; 349 int v2 = v3 || memcmp(in, "R9ROOT2", 7) == 0; 350 if (!v2 && memcmp(in, "R9ROOT1", 7) != 0) return RX_GEN_ERR_TORN; 351 uint8_t sum[32]; 352 checksum(in, ROOT_SUM, sum); 353 if (memcmp(sum, in + ROOT_SUM, 32) != 0) return RX_GEN_ERR_TORN; 354 memset(v, 0, sizeof(*v)); 355 v->id = get_u64(in + 8); 356 v->parent = get_u64(in + 16); 357 v->lineage = get_u64(in + 24); 358 v->state = get_u32(in + 32); 359 v->n_objects = get_u32(in + 36); 360 v->authority_epoch = get_u64(in + 40); 361 v->authority_generation = get_u32(in + 48); 362 if (v2) v->authority_generation |= (uint64_t)get_u32(in + ROOT_AUTH_GEN_HI) << 32; 363 v->n_external = get_u32(in + 52); 364 v->n_excluded = get_u32(in + 56); 365 v->flags = get_u32(in + 60); 366 if (v->n_external > RX_GEN_MAX_EXTERNAL || v->n_excluded > RX_GEN_MAX_EXCLUDED) 367 return RX_GEN_ERR_TORN; 368 for (int i = 0; i < DIG_COUNT; i++) v->lengths[i] = get_u64(in + 64 + (size_t)i * 8); 369 for (int i = 0; i < DIG_COUNT; i++) 370 memcpy(v->digest[i], in + 112 + (size_t)i * 32, 32); 371 for (uint32_t i = 0; i < v->n_external; i++) 372 v->external_ids[i] = get_u64(in + 304 + (size_t)i * 8); 373 for (uint32_t i = 0; i < v->n_excluded; i++) 374 v->excluded_ids[i] = get_u64(in + 368 + (size_t)i * 8); 375 if (v3) { 376 v->work_len = get_u64(in + ROOT_WORK_LEN); 377 memcpy(v->work_digest, in + ROOT_WORK_DIGEST, sizeof v->work_digest); 378 v->n_work = get_u32(in + ROOT_WORK_COUNT); 379 if (v->n_work > RX_GEN_MAX_WORK || 380 v->work_len != WORK_HEADER_BYTES + (uint64_t)v->n_work * WORK_ROW_BYTES) 381 return RX_GEN_ERR_TORN; 382 } 383 return RX_GEN_OK; 384 } 385 386 static void encode_pointer(uint64_t id, uint64_t lineage, const uint8_t digest[32], 387 uint8_t out[PTR_BYTES]) { 388 memset(out, 0, PTR_BYTES); 389 memcpy(out, "R9PTR01", 7); 390 put_u64(out + 8, id); 391 put_u64(out + 16, lineage); 392 memcpy(out + 24, digest, 32); 393 checksum(out, PTR_SUM, out + PTR_SUM); 394 } 395 396 static int decode_pointer(const uint8_t in[PTR_BYTES], uint64_t *id, uint64_t *lineage, 397 uint8_t digest[32]) { 398 if (memcmp(in, "R9PTR01", 7) != 0) return RX_GEN_ERR_TORN; 399 uint8_t sum[32]; 400 checksum(in, PTR_SUM, sum); 401 if (memcmp(sum, in + PTR_SUM, 32) != 0) return RX_GEN_ERR_TORN; 402 *id = get_u64(in + 8); 403 *lineage = get_u64(in + 16); 404 memcpy(digest, in + 24, 32); 405 return RX_GEN_OK; 406 } 407 408 static void encode_journal(uint32_t phase, uint64_t parent, uint64_t candidate, 409 uint64_t parent_lineage, const uint8_t digest[32], 410 uint8_t out[JRN_BYTES]) { 411 memset(out, 0, JRN_BYTES); 412 memcpy(out, "R9JRN01", 7); 413 put_u32(out + 8, phase); 414 put_u64(out + 16, parent); 415 put_u64(out + 24, candidate); 416 put_u64(out + 32, parent_lineage); 417 memcpy(out + 40, digest, 32); 418 checksum(out, JRN_SUM, out + JRN_SUM); 419 } 420 421 static int decode_journal(const uint8_t in[JRN_BYTES], uint32_t *phase, uint64_t *parent, 422 uint64_t *candidate, uint64_t *parent_lineage, uint8_t digest[32]) { 423 if (memcmp(in, "R9JRN01", 7) != 0) return RX_GEN_ERR_TORN; 424 uint8_t sum[32]; 425 checksum(in, JRN_SUM, sum); 426 if (memcmp(sum, in + JRN_SUM, 32) != 0) return RX_GEN_ERR_TORN; 427 *phase = get_u32(in + 8); 428 *parent = get_u64(in + 16); 429 *candidate = get_u64(in + 24); 430 *parent_lineage = get_u64(in + 32); 431 memcpy(digest, in + 40, 32); 432 return RX_GEN_OK; 433 } 434 435 static int commit_file(RxGenStore *store, const char *final_path, const uint8_t *buf, 436 size_t n, int during_step) { 437 char tmp[512]; 438 if (snprintf(tmp, sizeof tmp, "%s/tmp/write.partial", store->dir) >= (int)sizeof tmp) 439 return RX_GEN_ERR_IO; 440 int fd = open(tmp, O_CREAT | O_TRUNC | O_WRONLY, 0644); 441 if (fd < 0) return RX_GEN_ERR_IO; 442 if (during_step != RX_CRASH_NONE && store->crash_step == during_step) { 443 size_t half = n / 2; 444 if (half == 0) half = 1; 445 if (half > n) half = n; 446 if (write_full(store, fd, buf, half) != 0) { 447 close(fd); 448 return RX_GEN_ERR_IO; 449 } 450 fsync_fd(store, fd); 451 close(fd); 452 crash_now(); 453 } 454 if (write_full(store, fd, buf, n) != 0 || fsync_fd(store, fd) != 0) { 455 close(fd); 456 return RX_GEN_ERR_IO; 457 } 458 close(fd); 459 if (rename(tmp, final_path) != 0) return RX_GEN_ERR_IO; 460 char parent[512]; 461 snprintf(parent, sizeof parent, "%s", final_path); 462 char *slash = strrchr(parent, '/'); 463 if (!slash) return RX_GEN_ERR_IO; 464 *slash = 0; 465 if (fsync_path(store, parent) != 0) return RX_GEN_ERR_IO; 466 return RX_GEN_OK; 467 } 468 469 static int gen_dir(const char *dir, uint64_t id, char *out, size_t n) { 470 int wrote = snprintf(out, n, "%s/g/%llu", dir, (unsigned long long)id); 471 if (wrote < 0 || (size_t)wrote >= n) return -1; 472 return 0; 473 } 474 475 static int load_root_file(const char *dir, uint64_t id, RootView *view, uint8_t digest[32]) { 476 char folder[512], path[512]; 477 if (gen_dir(dir, id, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; 478 if (path_join(path, sizeof path, folder, "root") != 0) return RX_GEN_ERR_IO; 479 uint8_t raw[ROOT_BYTES]; 480 if (read_full(path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; 481 int rc = decode_root(raw, view); 482 if (rc != RX_GEN_OK) return rc; 483 checksum(raw, ROOT_BYTES, digest); 484 return RX_GEN_OK; 485 } 486 487 static int file_matches(const char *path, const uint8_t expect[32], uint64_t expect_len) { 488 int fd = open(path, O_RDONLY); 489 if (fd < 0) return RX_GEN_ERR_MISSING; 490 sha256_ctx ctx; 491 sha256_init(&ctx); 492 uint64_t total = 0; 493 uint8_t buf[4096]; 494 for (;;) { 495 ssize_t r = read(fd, buf, sizeof buf); 496 if (r < 0) { 497 if (errno == EINTR) continue; 498 close(fd); 499 return RX_GEN_ERR_IO; 500 } 501 if (r == 0) break; 502 sha256_update(&ctx, buf, (size_t)r); 503 total += (uint64_t)r; 504 } 505 close(fd); 506 uint8_t got[32]; 507 sha256_final(&ctx, got); 508 if (total != expect_len || memcmp(got, expect, 32) != 0) return RX_GEN_ERR_TORN; 509 return RX_GEN_OK; 510 } 511 512 static const char *BLOB_NAME[DIG_COUNT] = { 513 "objects", "evidence", "model", "realization", "config", "provenance" 514 }; 515 516 static int root_reachable(const char *dir, const RootView *view) { 517 char folder[512], path[512]; 518 if (gen_dir(dir, view->id, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; 519 for (int i = 0; i < DIG_COUNT; i++) { 520 if (path_join(path, sizeof path, folder, BLOB_NAME[i]) != 0) return RX_GEN_ERR_IO; 521 int rc = file_matches(path, view->digest[i], view->lengths[i]); 522 if (rc != RX_GEN_OK) return rc; 523 } 524 if (view->work_len) { 525 if (path_join(path, sizeof path, folder, "work") != 0) return RX_GEN_ERR_IO; 526 int rc = file_matches(path, view->work_digest, view->work_len); 527 if (rc != RX_GEN_OK) return rc; 528 } 529 return RX_GEN_OK; 530 } 531 532 static int read_pointer(const char *dir, uint64_t *id, uint64_t *lineage, uint8_t digest[32]) { 533 char path[512]; 534 if (path_join(path, sizeof path, dir, "active") != 0) return RX_GEN_ERR_IO; 535 uint8_t raw[PTR_BYTES]; 536 if (read_full(path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; 537 return decode_pointer(raw, id, lineage, digest); 538 } 539 540 static int read_journal(const char *dir, uint32_t *phase, uint64_t *parent, uint64_t *candidate, 541 uint64_t *parent_lineage, uint8_t digest[32]) { 542 char path[512]; 543 if (path_join(path, sizeof path, dir, "journal") != 0) return RX_GEN_ERR_IO; 544 uint8_t raw[JRN_BYTES]; 545 if (read_full(path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; 546 return decode_journal(raw, phase, parent, candidate, parent_lineage, digest); 547 } 548 549 static int event_has(const char *dir, uint64_t id) { 550 char path[512]; 551 if (path_join(path, sizeof path, dir, "events") != 0) return 0; 552 FILE *f = fopen(path, "r"); 553 if (!f) return 0; 554 unsigned long long seen = 0, lineage = 0; 555 int found = 0; 556 while (fscanf(f, "%llu %llu", &seen, &lineage) == 2) { 557 if (seen == (unsigned long long)id) found = 1; 558 } 559 fclose(f); 560 return found; 561 } 562 563 static int append_event(RxGenStore *store, uint64_t id, uint64_t lineage) { 564 if (event_has(store->dir, id)) return RX_GEN_OK; 565 char path[512]; 566 if (path_join(path, sizeof path, store->dir, "events") != 0) return RX_GEN_ERR_IO; 567 int fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0644); 568 if (fd < 0) return RX_GEN_ERR_IO; 569 char line[64]; 570 int n = snprintf(line, sizeof line, "%llu %llu\n", (unsigned long long)id, 571 (unsigned long long)lineage); 572 if (n < 0 || write_full(store, fd, (const uint8_t *)line, (size_t)n) != 0 || fsync_fd(store, fd) != 0) { 573 close(fd); 574 return RX_GEN_ERR_IO; 575 } 576 close(fd); 577 return fsync_path(store, store->dir) == 0 ? RX_GEN_OK : RX_GEN_ERR_IO; 578 } 579 580 static void fill_record(const RootView *view, int receipt, int event, RxRecoveryRecord *out) { 581 memset(out, 0, sizeof(*out)); 582 out->coherent = 1; 583 out->active_id = view->id; 584 out->lineage = view->lineage; 585 out->parent_id = view->parent; 586 out->receipt_present = receipt; 587 out->event_present = event; 588 out->n_external = view->n_external; 589 out->n_excluded = view->n_excluded; 590 memcpy(out->external_ids, view->external_ids, sizeof view->external_ids); 591 memcpy(out->excluded_ids, view->excluded_ids, sizeof view->excluded_ids); 592 } 593 594 static int receipt_exists(const char *dir, uint64_t id) { 595 char folder[512], path[512]; 596 if (gen_dir(dir, id, folder, sizeof folder) != 0) return 0; 597 if (path_join(path, sizeof path, folder, "receipt") != 0) return 0; 598 return access(path, R_OK) == 0; 599 } 600 601 static int write_receipt_file(RxGenStore *store, const RootView *view, uint32_t subject, 602 uint32_t cap_id, uint64_t cap_gen) { 603 uint8_t raw[RCT_BYTES]; 604 memset(raw, 0, sizeof raw); 605 memcpy(raw, "R9RCT03", 7); 606 put_u64(raw + 8, view->id); 607 put_u64(raw + 16, view->lineage); 608 put_u64(raw + 24, view->parent); 609 uint8_t root_digest[32]; 610 char folder[512], root_path[512], path[512]; 611 if (gen_dir(store->dir, view->id, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; 612 if (path_join(root_path, sizeof root_path, folder, "root") != 0) return RX_GEN_ERR_IO; 613 uint8_t root_raw[ROOT_BYTES]; 614 if (read_full(root_path, root_raw, sizeof root_raw) != 0) return RX_GEN_ERR_MISSING; 615 checksum(root_raw, ROOT_BYTES, root_digest); 616 memcpy(raw + 32, root_digest, 32); 617 put_u32(raw + 64, subject); 618 put_u32(raw + 68, cap_id); 619 put_u32(raw + 72, (uint32_t)cap_gen); 620 put_u32(raw + 76, (uint32_t)(cap_gen >> 32)); 621 put_u64(raw + 80, view->work_len); 622 memcpy(raw + 88, view->work_digest, sizeof view->work_digest); 623 put_u32(raw + 120, view->n_work); 624 checksum(raw, RCT_SUM, raw + RCT_SUM); 625 if (path_join(path, sizeof path, folder, "receipt") != 0) return RX_GEN_ERR_IO; 626 return commit_file(store, path, raw, sizeof raw, RX_CRASH_NONE); 627 } 628 629 static int write_journal(RxGenStore *store, uint32_t phase, uint64_t parent, uint64_t candidate, 630 uint64_t parent_lineage, const uint8_t digest[32]) { 631 uint8_t raw[JRN_BYTES]; 632 encode_journal(phase, parent, candidate, parent_lineage, digest, raw); 633 char path[512]; 634 if (path_join(path, sizeof path, store->dir, "journal") != 0) return RX_GEN_ERR_IO; 635 return commit_file(store, path, raw, sizeof raw, RX_CRASH_NONE); 636 } 637 638 static int write_named_blob(RxGenStore *store, const char *folder, const char *name, 639 const uint8_t *bytes, size_t n, int during) { 640 char path[512]; 641 if (path_join(path, sizeof path, folder, name) != 0) return RX_GEN_ERR_IO; 642 return commit_file(store, path, bytes ? bytes : (const uint8_t *)"", n, during); 643 } 644 645 static int objects_bytes(const Candidate *c, uint8_t **out, size_t *out_n) { 646 size_t n = 4u + (size_t)c->n_objects * 40u; 647 uint8_t *p = malloc(n ? n : 1); 648 if (!p) return RX_GEN_ERR_IO; 649 put_u32(p, c->n_objects); 650 for (uint32_t i = 0; i < c->n_objects; i++) { 651 uint8_t *row = p + 4 + (size_t)i * 40; 652 put_u32(row, c->objects[i].id); 653 put_u32(row + 4, c->objects[i].generation); 654 memcpy(row + 8, c->objects[i].digest, 32); 655 } 656 *out = p; 657 *out_n = n; 658 return RX_GEN_OK; 659 } 660 661 static void hash_buf(const uint8_t *p, size_t n, uint8_t out[32]) { 662 sha256_hash(p ? p : (const uint8_t *)"", n, out); 663 } 664 665 /* R9WORK1 is a root-committed audit record. Every row is fixed-width LE: 666 * id, class, state, required, worker, cap id, completer, cap generation, 667 * scoped finish resource, and the evidence digest observed at completion. */ 668 static size_t encode_work(const Candidate *c, uint8_t out[WORK_BLOB_BYTES]) { 669 memset(out, 0, WORK_BLOB_BYTES); 670 memcpy(out, "R9WORK1", 7); 671 put_u32(out + 8, c->n_work); 672 put_u32(out + 12, WORK_ROW_BYTES); 673 for (uint32_t i = 0; i < c->n_work; i++) { 674 const RxGenWork *w = &c->work[i]; 675 uint8_t *row = out + WORK_HEADER_BYTES + (size_t)i * WORK_ROW_BYTES; 676 put_u64(row, w->id); 677 put_u32(row + 8, w->class); 678 put_u32(row + 12, w->state); 679 put_u32(row + 16, (uint32_t)w->required); 680 put_u32(row + 20, w->worker); 681 put_u32(row + 24, w->finish_cap_id); 682 put_u32(row + 28, w->completer); 683 put_u64(row + 32, w->finish_cap_generation); 684 put_u64(row + 40, w->finish_resource); 685 memcpy(row + 48, w->evidence_digest, sizeof w->evidence_digest); 686 } 687 return WORK_HEADER_BYTES + (size_t)c->n_work * WORK_ROW_BYTES; 688 } 689 690 static int release_lock(RxGenStore *store) { 691 if (store->lock_fd >= 0) { 692 close(store->lock_fd); 693 store->lock_fd = -1; 694 } 695 char path[512]; 696 if (path_join(path, sizeof path, store->dir, "barrier.lock") != 0) return RX_GEN_ERR_IO; 697 if (unlink(path) != 0 && errno != ENOENT) return RX_GEN_ERR_IO; 698 return RX_GEN_OK; 699 } 700 701 static int take_lock(RxGenStore *store) { 702 char path[512]; 703 if (path_join(path, sizeof path, store->dir, "barrier.lock") != 0) return RX_GEN_ERR_IO; 704 int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0644); 705 if (fd < 0) return RX_GEN_ERR_BUSY; 706 char pid[32]; 707 int n = snprintf(pid, sizeof pid, "%ld\n", (long)getpid()); 708 if (n > 0) write_full(store, fd, (const uint8_t *)pid, (size_t)n); 709 fsync_fd(store, fd); 710 store->lock_fd = fd; 711 return RX_GEN_OK; 712 } 713 714 static int compare_u64(const void *a, const void *b) { 715 uint64_t x = *(const uint64_t *)a; 716 uint64_t y = *(const uint64_t *)b; 717 if (x < y) return -1; 718 if (x > y) return 1; 719 return 0; 720 } 721 722 static int install_genesis(RxGenStore *store) { 723 const uint8_t evidence[] = "GENESIS"; 724 uint8_t empty_obj[4]; 725 put_u32(empty_obj, 0); 726 const uint8_t *blobs[DIG_COUNT]; 727 size_t lens[DIG_COUNT]; 728 blobs[DIG_OBJECT] = empty_obj; 729 lens[DIG_OBJECT] = 4; 730 blobs[DIG_EVIDENCE] = evidence; 731 lens[DIG_EVIDENCE] = sizeof evidence - 1; 732 for (int i = DIG_MODEL; i < DIG_COUNT; i++) { 733 blobs[i] = (const uint8_t *)""; 734 lens[i] = 0; 735 } 736 char folder[512]; 737 if (gen_dir(store->dir, 1, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; 738 if (ensure_dir(folder) != 0) return RX_GEN_ERR_IO; 739 RootView view; 740 memset(&view, 0, sizeof view); 741 view.id = 1; 742 view.parent = 0; 743 view.lineage = 1; 744 view.state = 1; 745 view.flags = 1; 746 for (int i = 0; i < DIG_COUNT; i++) { 747 if (write_named_blob(store, folder, BLOB_NAME[i], blobs[i], lens[i], RX_CRASH_NONE) != 748 RX_GEN_OK) 749 return RX_GEN_ERR_IO; 750 view.lengths[i] = lens[i]; 751 hash_buf(blobs[i], lens[i], view.digest[i]); 752 } 753 Candidate empty; 754 uint8_t work[WORK_BLOB_BYTES]; 755 memset(&empty, 0, sizeof empty); 756 view.work_len = encode_work(&empty, work); 757 view.n_work = 0; 758 hash_buf(work, (size_t)view.work_len, view.work_digest); 759 if (write_named_blob(store, folder, "work", work, (size_t)view.work_len, RX_CRASH_NONE) != 760 RX_GEN_OK) 761 return RX_GEN_ERR_IO; 762 uint8_t root[ROOT_BYTES]; 763 encode_root(&view, root); 764 char root_path[512]; 765 if (path_join(root_path, sizeof root_path, folder, "root") != 0) return RX_GEN_ERR_IO; 766 if (commit_file(store, root_path, root, sizeof root, RX_CRASH_NONE) != RX_GEN_OK) 767 return RX_GEN_ERR_IO; 768 uint8_t root_digest[32]; 769 checksum(root, ROOT_BYTES, root_digest); 770 uint8_t pointer[PTR_BYTES]; 771 encode_pointer(1, 1, root_digest, pointer); 772 char active[512]; 773 if (path_join(active, sizeof active, store->dir, "active") != 0) return RX_GEN_ERR_IO; 774 if (commit_file(store, active, pointer, sizeof pointer, RX_CRASH_NONE) != RX_GEN_OK) 775 return RX_GEN_ERR_IO; 776 if (write_journal(store, PHASE_RECEIPT, 0, 1, 0, root_digest) != RX_GEN_OK) 777 return RX_GEN_ERR_IO; 778 store->active_id = 1; 779 store->active_lineage = 1; 780 store->next_id = 2; 781 char nextp[512]; 782 if (path_join(nextp, sizeof nextp, store->dir, "nextid") != 0) return RX_GEN_ERR_IO; 783 char num[32]; 784 int n = snprintf(num, sizeof num, "2\n"); 785 return commit_file(store, nextp, (const uint8_t *)num, (size_t)n, RX_CRASH_NONE); 786 } 787 788 int rx_gen_open(const char *dir, RxGenStore **out) { 789 if (!dir || !out) return RX_GEN_ERR_ARG; 790 RxGenStore *store = calloc(1, sizeof(*store)); 791 if (!store) return RX_GEN_ERR_IO; 792 pthread_mutex_init(&store->active_mu, NULL); 793 /* The caller's directory, checked (src/omega_path_check.h), is the only one this store opens. */ 794 if (omega_path_copy(store->dir, sizeof store->dir, dir) != 0) { 795 free(store); 796 return RX_GEN_ERR_ARG; 797 } 798 store->lock_fd = -1; 799 char gdir[512], tmp[512]; 800 if (path_join(gdir, sizeof gdir, store->dir, "g") != 0 || 801 path_join(tmp, sizeof tmp, store->dir, "tmp") != 0) { 802 free(store); 803 return RX_GEN_ERR_IO; 804 } 805 if (ensure_dir(store->dir) != 0 || ensure_dir(gdir) != 0 || ensure_dir(tmp) != 0) { 806 free(store); 807 return RX_GEN_ERR_IO; 808 } 809 char active[512]; 810 path_join(active, sizeof active, store->dir, "active"); 811 if (access(active, R_OK) != 0) { 812 int rc = install_genesis(store); 813 if (rc != RX_GEN_OK) { 814 free(store); 815 return rc; 816 } 817 } else { 818 uint8_t digest[32]; 819 int rc = read_pointer(store->dir, &store->active_id, &store->active_lineage, digest); 820 if (rc != RX_GEN_OK) { 821 free(store); 822 return rc; 823 } 824 FILE *f = NULL; 825 char nextp[512]; 826 path_join(nextp, sizeof nextp, store->dir, "nextid"); 827 f = fopen(nextp, "r"); 828 unsigned long long next = store->active_id + 1; 829 if (f) { 830 if (fscanf(f, "%llu", &next) != 1) next = store->active_id + 1; 831 fclose(f); 832 } 833 store->next_id = (uint64_t)next; 834 } 835 *out = store; 836 return RX_GEN_OK; 837 } 838 839 static void exec_stop(RxGenStore *store); 840 841 void rx_gen_close(RxGenStore *store) { 842 if (!store) return; 843 exec_stop(store); 844 for (int i = 0; i < MAX_CAND; i++) { 845 for (int b = 0; b < DIG_COUNT; b++) free(store->cand[i].blob[b]); 846 } 847 if (store->lock_fd >= 0) close(store->lock_fd); 848 pthread_mutex_destroy(&store->active_mu); 849 free(store); 850 } 851 852 int rx_gen_active(const RxGenStore *store, uint64_t *id, uint64_t *lineage) { 853 if (!store || !id || !lineage) return RX_GEN_ERR_ARG; 854 RxGenStore *s = (RxGenStore *)store; 855 pthread_mutex_lock(&s->active_mu); 856 *id = s->active_id; 857 *lineage = s->active_lineage; 858 pthread_mutex_unlock(&s->active_mu); 859 return RX_GEN_OK; 860 } 861 862 int rx_gen_read_blob(const RxGenStore *store, uint64_t id, const char *name, uint8_t *buf, 863 size_t cap, size_t *out_len) { 864 if (!store || !name || !out_len || (!buf && cap)) return RX_GEN_ERR_ARG; 865 int which = -1; 866 for (int i = 0; i < DIG_COUNT; i++) 867 if (strcmp(name, BLOB_NAME[i]) == 0) which = i; 868 int work = strcmp(name, "work") == 0; 869 if (which < 0 && !work) return RX_GEN_ERR_ARG; 870 RootView view; 871 uint8_t root_digest[32]; 872 int rc = load_root_file(store->dir, id, &view, root_digest); 873 if (rc != RX_GEN_OK) return rc; 874 if (view.id != id) return RX_GEN_ERR_TORN; 875 uint64_t length = work ? view.work_len : view.lengths[which]; 876 const uint8_t *digest = work ? view.work_digest : view.digest[which]; 877 const char *stored_name = work ? "work" : BLOB_NAME[which]; 878 if (length > cap) return RX_GEN_ERR_ARG; 879 char folder[512], path[512]; 880 /* stored_name comes from a fixed table/literal, never from the input. */ 881 if (gen_dir(store->dir, id, folder, sizeof folder) != 0 || 882 path_join(path, sizeof path, folder, stored_name) != 0) return RX_GEN_ERR_IO; 883 rc = file_matches(path, digest, length); 884 if (rc != RX_GEN_OK) return rc; 885 if (length && read_full(path, buf, (size_t)length) != 0) 886 return RX_GEN_ERR_IO; 887 /* The bytes read must be the bytes the root names, not whatever the file 888 * became after the check above. */ 889 uint8_t got[32]; 890 hash_buf(buf, (size_t)length, got); 891 if (memcmp(got, digest, 32) != 0) return RX_GEN_ERR_TORN; 892 *out_len = (size_t)length; 893 return RX_GEN_OK; 894 } 895 896 /* A bound caller check that refused: an operator stop (R16 G6) is reported as 897 * such, every other refusal is an identity fault. */ 898 static int caller_refusal(int crc) { 899 return crc == RX_CALLER_ERR_HALTED ? RX_GEN_ERR_HALTED : RX_GEN_ERR_IDENTITY; 900 } 901 902 /* R16 G6: the durable mark of an operator stop in the store directory. 0 none, 903 * RX_GEN_ERR_HALTED present, RX_GEN_ERR_IO when it cannot be told (refuse). */ 904 static int halt_marked(const RxGenStore *store) { 905 char p[512]; 906 if (snprintf(p, sizeof p, "%s/%s", store->dir, RX_GEN_HALT_MARK) >= (int)sizeof p) 907 return RX_GEN_ERR_IO; 908 struct stat st; 909 if (lstat(p, &st) == 0) return RX_GEN_ERR_HALTED; 910 return errno == ENOENT ? 0 : RX_GEN_ERR_IO; 911 } 912 913 int rx_gen_bind_authority(RxGenStore *store, RxGenCallerFn caller, void *caller_ctx, 914 RxGenAuthFn auth, void *auth_ctx) { 915 if (!store || !caller || !auth) return RX_GEN_ERR_ARG; 916 if (store_bound(store)) return RX_GEN_ERR_BUSY; 917 store->caller = caller; 918 store->caller_ctx = caller_ctx; 919 store->bound_auth = auth; 920 store->bound_auth_ctx = auth_ctx; 921 __atomic_store_n(&store->bound, 1, __ATOMIC_RELEASE); 922 return RX_GEN_OK; 923 } 924 925 int rx_gen_propose(RxGenStore *store, uint32_t proposer, const RxGenDraft *draft, 926 uint64_t *out_id) { 927 return rx_gen_propose_as(store, proposer, NULL, draft, out_id); 928 } 929 930 int rx_gen_propose_as(RxGenStore *store, uint32_t proposer, const RxCallerCred *cred, 931 const RxGenDraft *draft, uint64_t *out_id) { 932 if (!store || !draft || !out_id) return RX_GEN_ERR_ARG; 933 /* R16 C5: the proposer is who the credential says, or nobody. */ 934 if (store_bound(store)) { 935 int crc = store->caller(store->caller_ctx, proposer, cred, RX_CALLER_OP_CHECK); 936 if (crc != 0) return caller_refusal(crc); 937 } 938 if (draft->n_objects > RX_GEN_MAX_OBJECTS) return RX_GEN_ERR_ARG; 939 if (draft->n_objects && !draft->objects) return RX_GEN_ERR_ARG; 940 Candidate *slot = NULL; 941 for (int i = 0; i < MAX_CAND; i++) 942 if (!store->cand[i].used) { 943 slot = &store->cand[i]; 944 break; 945 } 946 if (!slot) return RX_GEN_ERR_BUSY; 947 memset(slot, 0, sizeof(*slot)); 948 slot->used = 1; 949 slot->id = store->next_id++; 950 slot->parent_id = store->active_id; 951 slot->proposer = proposer; 952 slot->proofs_ok = draft->proofs_ok; 953 slot->authority_epoch = draft->authority_epoch; 954 slot->authority_generation = draft->authority_generation; 955 slot->n_objects = draft->n_objects; 956 if (draft->n_objects) memcpy(slot->objects, draft->objects, draft->n_objects * sizeof(RxGenObject)); 957 const uint8_t *src[DIG_COUNT] = {NULL, draft->evidence, draft->model, draft->realization, 958 draft->config, draft->provenance}; 959 size_t lens[DIG_COUNT] = {0, draft->evidence_len, draft->model_len, draft->realization_len, 960 draft->config_len, draft->provenance_len}; 961 uint8_t *obj = NULL; 962 size_t obj_n = 0; 963 int rc = objects_bytes(slot, &obj, &obj_n); 964 if (rc != RX_GEN_OK) { 965 slot->used = 0; 966 return rc; 967 } 968 src[DIG_OBJECT] = obj; 969 lens[DIG_OBJECT] = obj_n; 970 for (int i = 0; i < DIG_COUNT; i++) { 971 rc = copy_blob(&slot->blob[i], &slot->blob_len[i], src[i], lens[i]); 972 if (rc != RX_GEN_OK) { 973 free(obj); 974 for (int b = 0; b < DIG_COUNT; b++) free(slot->blob[b]); 975 memset(slot, 0, sizeof(*slot)); 976 return rc; 977 } 978 } 979 free(obj); 980 char nextp[512], num[32]; 981 if (path_join(nextp, sizeof nextp, store->dir, "nextid") != 0) return RX_GEN_ERR_IO; 982 int n = snprintf(num, sizeof num, "%llu\n", (unsigned long long)store->next_id); 983 rc = commit_file(store, nextp, (const uint8_t *)num, (size_t)n, RX_CRASH_NONE); 984 if (rc != RX_GEN_OK) return rc; 985 *out_id = slot->id; 986 return RX_GEN_OK; 987 } 988 989 int rx_gen_mutate_object(RxGenStore *store, uint64_t candidate, uint32_t index, 990 uint32_t generation, const uint8_t digest[32]) { 991 if (!store || !digest) return RX_GEN_ERR_ARG; 992 /* R16 C7: these edits carry no credential; a bound store refuses them. */ 993 if (store_bound(store)) return RX_GEN_ERR_IDENTITY; 994 Candidate *c = find_cand(store, candidate); 995 if (!c || index >= c->n_objects) return RX_GEN_ERR_ARG; 996 if (c->closing) return RX_GEN_ERR_CLOSING; 997 c->objects[index].generation = generation; 998 memcpy(c->objects[index].digest, digest, 32); 999 uint8_t *obj = NULL; 1000 size_t obj_n = 0; 1001 int rc = objects_bytes(c, &obj, &obj_n); 1002 if (rc != RX_GEN_OK) return rc; 1003 rc = copy_blob(&c->blob[DIG_OBJECT], &c->blob_len[DIG_OBJECT], obj, obj_n); 1004 free(obj); 1005 return rc; 1006 } 1007 1008 int rx_gen_set_evidence(RxGenStore *store, uint64_t candidate, const uint8_t *bytes, size_t n) { 1009 if (!store) return RX_GEN_ERR_ARG; 1010 if (store_bound(store)) return RX_GEN_ERR_IDENTITY; 1011 Candidate *c = find_cand(store, candidate); 1012 if (!c) return RX_GEN_ERR_ARG; 1013 if (c->closing && !c->draining) return RX_GEN_ERR_CLOSING; 1014 return copy_blob(&c->blob[DIG_EVIDENCE], &c->blob_len[DIG_EVIDENCE], bytes, n); 1015 } 1016 1017 int rx_gen_observe_object(RxGenStore *store, uint64_t candidate, uint32_t index, 1018 uint32_t generation) { 1019 Candidate *c = find_cand(store, candidate); 1020 if (!c || index >= c->n_objects) return RX_GEN_ERR_ARG; 1021 c->observed_set[index] = 1; 1022 c->observed_gen[index] = generation; 1023 return RX_GEN_OK; 1024 } 1025 1026 int rx_gen_add_work(RxGenStore *store, uint64_t candidate, const RxGenWork *work) { 1027 /* R16 C7: no credential, so a bound store refuses it (identity error). */ 1028 return rx_gen_add_work_as(store, 0, NULL, candidate, work); 1029 } 1030 1031 int rx_gen_add_work_as(RxGenStore *store, uint32_t subject, const RxCallerCred *cred, 1032 uint64_t candidate, const RxGenWork *work) { 1033 if (!store || !work) return RX_GEN_ERR_ARG; 1034 /* R16 C7: on a bound store the caller is who the credential says. */ 1035 if (store_bound(store)) { 1036 int crc = store->caller(store->caller_ctx, subject, cred, RX_CALLER_OP_CHECK); 1037 if (crc != 0) return caller_refusal(crc); 1038 } 1039 Candidate *c = find_cand(store, candidate); 1040 if (!c) return RX_GEN_ERR_ARG; 1041 if (c->closing) return RX_GEN_ERR_CLOSING; 1042 if (c->n_work >= RX_GEN_MAX_WORK) return RX_GEN_ERR_BUSY; 1043 if (work->class != RX_WORK_EPHEMERAL && work->class != RX_WORK_EVIDENCE && 1044 work->class != RX_WORK_EXTERNAL) 1045 return RX_GEN_ERR_ARG; 1046 /* R16 C7: work starts pending or issued; only rx_gen_finish_work(_as) 1047 * marks it done, and only classify cancels it. */ 1048 int starts_open = work->state == RX_WORK_PENDING || work->state == RX_WORK_ISSUED; 1049 if (!starts_open) return RX_GEN_ERR_ARG; 1050 for (uint32_t i = 0; i < c->n_work; i++) 1051 if (c->work[i].id == work->id) return RX_GEN_ERR_ARG; 1052 RxGenWork accepted = *work; 1053 accepted.completer = 0; 1054 memset(accepted.evidence_digest, 0, sizeof accepted.evidence_digest); 1055 accepted.finish_resource = 0; 1056 if (store_bound(store)) { 1057 /* Candidate ownership authorizes assignment; it does not let an 1058 * arbitrary enrolled subject manufacture work for someone else's 1059 * candidate. worker_set marks the proposer's explicit delegate. */ 1060 if (subject != c->proposer) return RX_GEN_ERR_AUTHORITY; 1061 accepted.worker = work->worker_set ? work->worker : c->proposer; 1062 accepted.worker_set = 1; 1063 accepted.finish_resource = work_resource_value(store, c, work->id); 1064 int arc = store->bound_auth(store->bound_auth_ctx, work->finish_cap_id, 1065 work->finish_cap_generation, accepted.worker, 1066 accepted.finish_resource, RX_GEN_RIGHT_FINISH); 1067 if (arc != 0) return RX_GEN_ERR_AUTHORITY; 1068 } else { 1069 accepted.worker = 0; 1070 accepted.worker_set = 0; 1071 accepted.finish_cap_id = 0; 1072 accepted.finish_cap_generation = 0; 1073 } 1074 c->work[c->n_work++] = accepted; 1075 return RX_GEN_OK; 1076 } 1077 1078 int rx_gen_finish_work(RxGenStore *store, uint64_t candidate, uint64_t work_id) { 1079 /* R16 C7: no credential, so a bound store refuses it (identity error). */ 1080 return rx_gen_finish_work_as(store, 0, NULL, candidate, work_id); 1081 } 1082 1083 int rx_gen_finish_work_as(RxGenStore *store, uint32_t subject, const RxCallerCred *cred, 1084 uint64_t candidate, uint64_t work_id) { 1085 if (!store) return RX_GEN_ERR_ARG; 1086 if (store_bound(store)) { 1087 int crc = store->caller(store->caller_ctx, subject, cred, RX_CALLER_OP_CHECK); 1088 if (crc != 0) return caller_refusal(crc); 1089 } 1090 Candidate *c = find_cand(store, candidate); 1091 if (!c) return RX_GEN_ERR_ARG; 1092 for (uint32_t i = 0; i < c->n_work; i++) { 1093 if (c->work[i].id == work_id) { 1094 RxGenWork *work = &c->work[i]; 1095 if (store_bound(store)) { 1096 uint64_t resource = work_resource_value(store, c, work_id); 1097 if (work->worker != subject || work->finish_resource != resource) 1098 return RX_GEN_ERR_AUTHORITY; 1099 int arc = store->bound_auth(store->bound_auth_ctx, work->finish_cap_id, 1100 work->finish_cap_generation, subject, resource, 1101 RX_GEN_RIGHT_FINISH); 1102 if (arc != 0) return RX_GEN_ERR_AUTHORITY; 1103 } 1104 work->state = RX_WORK_DONE; 1105 work->completer = subject; 1106 hash_buf(c->blob[DIG_EVIDENCE], c->blob_len[DIG_EVIDENCE], 1107 work->evidence_digest); 1108 return RX_GEN_OK; 1109 } 1110 } 1111 return RX_GEN_ERR_MISSING; 1112 } 1113 1114 void rx_gen_set_crash(RxGenStore *store, int step) { 1115 if (store) store->crash_step = step; 1116 } 1117 1118 void rx_gen_set_disk_hook(RxGenStore *store, RxGenDiskHook fn, void *ctx) { 1119 if (!store) return; 1120 store->disk_hook = fn; 1121 store->disk_ctx = ctx; 1122 } 1123 1124 int rx_gen_hold_barrier(RxGenStore *store) { 1125 if (!store) return RX_GEN_ERR_ARG; 1126 if (store->lock_fd >= 0) return RX_GEN_OK; 1127 return take_lock(store); 1128 } 1129 1130 int rx_gen_release_barrier(RxGenStore *store) { 1131 if (!store) return RX_GEN_ERR_ARG; 1132 return release_lock(store); 1133 } 1134 1135 static int classify(RxGenStore *store, Candidate *c, RxGenDrainFn drain, void *drain_ctx, 1136 uint64_t *external, uint32_t *n_external, uint64_t *excluded, 1137 uint32_t *n_excluded) { 1138 *n_external = 0; 1139 *n_excluded = 0; 1140 c->draining = 1; 1141 for (uint32_t i = 0; i < c->n_work; i++) { 1142 RxGenWork *w = &c->work[i]; 1143 if (w->class == RX_WORK_EPHEMERAL) { 1144 if (w->state != RX_WORK_DONE) w->state = RX_WORK_CANCELLED; 1145 continue; 1146 } 1147 if (w->class == RX_WORK_EVIDENCE) { 1148 if (w->state != RX_WORK_DONE && w->required) { 1149 int drained = drain && drain(w->id, drain_ctx) == 0 && w->state == RX_WORK_DONE; 1150 if (!drained) { 1151 c->draining = 0; 1152 return RX_GEN_ERR_VERIFY; 1153 } 1154 } 1155 if (w->state != RX_WORK_DONE) { 1156 if (*n_excluded >= RX_GEN_MAX_EXCLUDED) { 1157 c->draining = 0; 1158 return RX_GEN_ERR_VERIFY; 1159 } 1160 excluded[(*n_excluded)++] = w->id; 1161 } else { 1162 uint8_t evidence_digest[32]; 1163 hash_buf(c->blob[DIG_EVIDENCE], c->blob_len[DIG_EVIDENCE], evidence_digest); 1164 if (c->blob_len[DIG_EVIDENCE] == 0 || 1165 memcmp(evidence_digest, w->evidence_digest, sizeof evidence_digest) != 0) { 1166 c->draining = 0; 1167 return RX_GEN_ERR_VERIFY; 1168 } 1169 } 1170 continue; 1171 } 1172 if (w->state == RX_WORK_PENDING) { 1173 w->state = RX_WORK_CANCELLED; 1174 continue; 1175 } 1176 if (*n_external >= RX_GEN_MAX_EXTERNAL) { 1177 c->draining = 0; 1178 return RX_GEN_ERR_VERIFY; 1179 } 1180 external[(*n_external)++] = w->id; 1181 } 1182 c->draining = 0; 1183 qsort(external, *n_external, sizeof(uint64_t), compare_u64); 1184 qsort(excluded, *n_excluded, sizeof(uint64_t), compare_u64); 1185 (void)store; 1186 return RX_GEN_OK; 1187 } 1188 1189 static void release_candidate(Candidate *c) { 1190 for (int b = 0; b < DIG_COUNT; b++) free(c->blob[b]); 1191 memset(c, 0, sizeof(*c)); 1192 } 1193 1194 int rx_gen_promote(RxGenStore *store, const RxPromotionRequest *request, RxGenAuthFn auth, 1195 void *auth_ctx, RxGenDrainFn drain, void *drain_ctx, RxGenLiveFn live, 1196 void *live_ctx) { 1197 if (!store || !request) return RX_GEN_ERR_ARG; 1198 /* R16 C5: the request's subject is the caller's only with the credential 1199 * the runtime issued for it; and a bound store validates the promotion 1200 * right with its own authority, never the caller's callback. */ 1201 if (store_bound(store)) { 1202 int crc = store->caller(store->caller_ctx, request->subject, &request->caller, RX_CALLER_OP_CHECK); 1203 if (crc != 0) return caller_refusal(crc); 1204 auth = store->bound_auth; 1205 auth_ctx = store->bound_auth_ctx; 1206 } 1207 /* R16 G6: no promotion while an operator stop is in force, bound or not. */ 1208 int halted = halt_marked(store); 1209 if (halted != 0) return halted; 1210 if (!auth) return RX_GEN_ERR_ARG; 1211 Candidate *c = find_cand(store, request->candidate_id); 1212 if (!c) return RX_GEN_ERR_ARG; 1213 int locked_here = 0; 1214 if (store->lock_fd < 0) { 1215 int rc = take_lock(store); 1216 if (rc != RX_GEN_OK) return rc; 1217 locked_here = 1; 1218 } else { 1219 return RX_GEN_ERR_BUSY; 1220 } 1221 memset(&store->phases, 0, sizeof store->phases); 1222 store->phases.candidate_id = request->candidate_id; 1223 store->phases.enter_ns = monotonic_ns(); 1224 int held = 0; /* R16 C7: the caller check is held across the flip */ 1225 int rc = RX_GEN_OK; 1226 uint64_t disk_id = 0, disk_lineage = 0; 1227 uint8_t disk_digest[32]; 1228 rc = read_pointer(store->dir, &disk_id, &disk_lineage, disk_digest); 1229 if (rc != RX_GEN_OK) goto done; 1230 set_active(store, disk_id, disk_lineage); 1231 if (c->parent_id != store->active_id) { 1232 rc = RX_GEN_ERR_STALE; 1233 goto done; 1234 } 1235 if (request->subject == c->proposer) { 1236 rc = RX_GEN_ERR_AUTHORITY; 1237 goto done; 1238 } 1239 if (request->resource != RX_GEN_RES_PROMOTION || request->rights != RX_GEN_RIGHT_PROMOTE) { 1240 rc = RX_GEN_ERR_AUTHORITY; 1241 goto done; 1242 } 1243 #if RX_ARGUS 1244 /* ARGUS: the promotion authority check (tick unknown here: the view is opaque). */ 1245 uint64_t argus_key = rx_argus_use_begin(); 1246 #endif 1247 int auth_rc = auth(auth_ctx, request->cap_id, request->cap_generation, request->subject, 1248 request->resource, request->rights); 1249 #if RX_ARGUS 1250 rx_argus_use_end(argus_key, NULL, request->subject, request->cap_id, 1251 (uint64_t)request->cap_generation, request->resource, auth_rc); 1252 #endif 1253 if (auth_rc != 0) { 1254 rc = RX_GEN_ERR_AUTHORITY; 1255 goto done; 1256 } 1257 c->closing = 1; 1258 if (live) live(live_ctx); 1259 store->phases.barrier_ns = monotonic_ns(); 1260 uint64_t external[RX_GEN_MAX_EXTERNAL]; 1261 uint64_t excluded[RX_GEN_MAX_EXCLUDED]; 1262 uint32_t n_external = 0, n_excluded = 0; 1263 rc = classify(store, c, drain, drain_ctx, external, &n_external, excluded, &n_excluded); 1264 if (rc != RX_GEN_OK) goto done; 1265 for (uint32_t i = 0; i < c->n_objects; i++) { 1266 if (c->observed_set[i] && c->observed_gen[i] != c->objects[i].generation) { 1267 rc = RX_GEN_ERR_STALE; 1268 goto done; 1269 } 1270 } 1271 if (!c->proofs_ok) { 1272 rc = RX_GEN_ERR_VERIFY; 1273 goto done; 1274 } 1275 store->phases.verified_ns = monotonic_ns(); 1276 if (store->crash_step == RX_CRASH_BEFORE_CANDIDATE_WRITE) crash_now(); 1277 1278 char folder[512]; 1279 if (gen_dir(store->dir, c->id, folder, sizeof folder) != 0) { 1280 rc = RX_GEN_ERR_IO; 1281 goto done; 1282 } 1283 if (ensure_dir(folder) != 0) { 1284 rc = RX_GEN_ERR_IO; 1285 goto done; 1286 } 1287 RootView view; 1288 memset(&view, 0, sizeof view); 1289 view.id = c->id; 1290 view.parent = c->parent_id; 1291 view.lineage = store->active_lineage + 1; 1292 view.state = 2; 1293 view.n_objects = c->n_objects; 1294 view.authority_epoch = c->authority_epoch; 1295 view.authority_generation = c->authority_generation; 1296 view.n_external = n_external; 1297 view.n_excluded = n_excluded; 1298 view.flags = 1; 1299 memcpy(view.external_ids, external, sizeof external); 1300 memcpy(view.excluded_ids, excluded, sizeof excluded); 1301 for (int i = 0; i < DIG_COUNT; i++) { 1302 int during = (i == DIG_OBJECT) ? RX_CRASH_DURING_CANDIDATE_WRITE : RX_CRASH_NONE; 1303 rc = write_named_blob(store, folder, BLOB_NAME[i], c->blob[i], c->blob_len[i], during); 1304 if (rc != RX_GEN_OK) goto done; 1305 view.lengths[i] = c->blob_len[i]; 1306 hash_buf(c->blob[i], c->blob_len[i], view.digest[i]); 1307 } 1308 uint8_t work[WORK_BLOB_BYTES]; 1309 view.work_len = encode_work(c, work); 1310 view.n_work = c->n_work; 1311 hash_buf(work, (size_t)view.work_len, view.work_digest); 1312 rc = write_named_blob(store, folder, "work", work, (size_t)view.work_len, RX_CRASH_NONE); 1313 if (rc != RX_GEN_OK) goto done; 1314 store->phases.blobs_ns = monotonic_ns(); 1315 uint8_t root[ROOT_BYTES]; 1316 encode_root(&view, root); 1317 char root_path[512]; 1318 if (path_join(root_path, sizeof root_path, folder, "root") != 0) { 1319 rc = RX_GEN_ERR_IO; 1320 goto done; 1321 } 1322 rc = commit_file(store, root_path, root, sizeof root, RX_CRASH_NONE); 1323 if (rc != RX_GEN_OK) goto done; 1324 uint8_t root_digest[32]; 1325 checksum(root, ROOT_BYTES, root_digest); 1326 rc = write_journal(store, PHASE_CANDIDATE, c->parent_id, c->id, store->active_lineage, 1327 root_digest); 1328 if (rc != RX_GEN_OK) goto done; 1329 if (store->crash_step == RX_CRASH_AFTER_CANDIDATE_WRITE) crash_now(); 1330 store->phases.candidate_ns = monotonic_ns(); 1331 if (store->disk_hook) store->disk_hook(folder, store->disk_ctx); 1332 rc = root_reachable(store->dir, &view); 1333 if (rc != RX_GEN_OK) goto done; 1334 store->phases.reachable_ns = monotonic_ns(); 1335 if (store->crash_step == RX_CRASH_BEFORE_ROOT_FLIP) crash_now(); 1336 1337 uint8_t pointer[PTR_BYTES]; 1338 encode_pointer(view.id, view.lineage, root_digest, pointer); 1339 char active_path[512]; 1340 if (path_join(active_path, sizeof active_path, store->dir, "active") != 0) { 1341 rc = RX_GEN_ERR_IO; 1342 goto done; 1343 } 1344 /* R16 C7: identity again at the durable commit point. The promoter may 1345 * have been revoked since entry (in the live barrier, during the disk 1346 * writes); refuse before the pointer moves. HOLD keeps any revocation out 1347 * until the flip is in memory too. rx_gen_bind_authority is startup-only 1348 * and lock-free: by contract no bind races an in-flight promotion. */ 1349 int recheck = store_bound(store); 1350 if (recheck) { 1351 int crc = store->caller(store->caller_ctx, request->subject, &request->caller, RX_CALLER_OP_HOLD); 1352 if (crc != 0) { 1353 rc = caller_refusal(crc); 1354 goto done; 1355 } 1356 held = 1; 1357 } 1358 /* R16 G6: a stop taken during the disk writes. A bound store holds the 1359 * caller check, so no world stop lands between here and the flip. */ 1360 rc = halt_marked(store); 1361 if (rc != RX_GEN_OK) goto done; 1362 rc = commit_file(store, active_path, pointer, sizeof pointer, RX_CRASH_DURING_ROOT_FLIP); 1363 if (rc == RX_GEN_OK) { 1364 store->phases.flip_ns = monotonic_ns(); 1365 set_active(store, view.id, view.lineage); 1366 } 1367 if (held) { 1368 store->caller(store->caller_ctx, request->subject, NULL, RX_CALLER_OP_RELEASE); 1369 held = 0; 1370 } 1371 if (rc != RX_GEN_OK) goto done; 1372 /* ARGUS: the World is committed at the flip. object_id = this store's identity 1373 * (hash of its directory: a reopened store keeps it), world_generation = lineage 1374 * (exactly parent+1 within a store). */ 1375 RX_ARGUS_EMIT(rx_argus_emit_world_committed(rx_argus_store_id(store->dir), request->subject, 1376 request->cap_id, (uint64_t)request->cap_generation, 1377 view.lineage, root_digest, 0)); 1378 if (store->crash_step == RX_CRASH_AFTER_ROOT_FLIP) crash_now(); 1379 rc = write_journal(store, PHASE_FLIPPED, c->parent_id, c->id, view.lineage - 1, root_digest); 1380 if (rc != RX_GEN_OK) goto done; 1381 store->phases.flipped_ns = monotonic_ns(); 1382 if (store->crash_step == RX_CRASH_BEFORE_RECEIPT) crash_now(); 1383 rc = write_receipt_file(store, &view, request->subject, request->cap_id, 1384 request->cap_generation); 1385 if (rc != RX_GEN_OK) goto done; 1386 store->phases.receipt_file_ns = monotonic_ns(); 1387 rc = append_event(store, view.id, view.lineage); 1388 if (rc != RX_GEN_OK) goto done; 1389 store->phases.event_ns = monotonic_ns(); 1390 if (store->crash_step == RX_CRASH_AFTER_RECEIPT) crash_now(); 1391 rc = write_journal(store, PHASE_RECEIPT, c->parent_id, c->id, view.lineage - 1, root_digest); 1392 if (rc == RX_GEN_OK) store->phases.receipt_ns = monotonic_ns(); 1393 1394 done: 1395 store->phases.result = rc; 1396 if (held) store->caller(store->caller_ctx, request->subject, NULL, RX_CALLER_OP_RELEASE); 1397 /* The draft is finished: it is the active generation now, or it can never 1398 * be promoted (its parent is gone or an object it observed moved). Its 1399 * slot is free for the next proposal; the committed bytes live on disk. */ 1400 if (store->active_id == c->id || rc == RX_GEN_ERR_STALE) release_candidate(c); 1401 if (locked_here) release_lock(store); 1402 return rc; 1403 } 1404 1405 int rx_gen_recover(const char *dir, RxRecoveryRecord *out) { 1406 if (!dir || !out) return RX_GEN_ERR_ARG; 1407 memset(out, 0, sizeof(*out)); 1408 /* From here dir is the checked copy (src/omega_path_check.h): no other path is opened. */ 1409 char checked[sizeof ((RxGenStore *)0)->dir]; 1410 if (omega_path_copy(checked, sizeof checked, dir) != 0) return RX_GEN_ERR_ARG; 1411 dir = checked; 1412 char lock[512]; 1413 if (path_join(lock, sizeof lock, dir, "barrier.lock") == 0) unlink(lock); 1414 1415 uint64_t id = 0, lineage = 0; 1416 uint8_t pointer_digest[32]; 1417 int pointer_rc = read_pointer(dir, &id, &lineage, pointer_digest); 1418 uint32_t phase = PHASE_NONE; 1419 uint64_t parent = 0, candidate = 0, parent_lineage = 0; 1420 uint8_t journal_digest[32]; 1421 int journal_rc = 1422 read_journal(dir, &phase, &parent, &candidate, &parent_lineage, journal_digest); 1423 1424 if (pointer_rc != RX_GEN_OK && journal_rc == RX_GEN_OK && phase >= PHASE_FLIPPED) { 1425 RootView repaired; 1426 uint8_t got[32]; 1427 int rc = load_root_file(dir, candidate, &repaired, got); 1428 if (rc != RX_GEN_OK) return rc; 1429 if (memcmp(got, journal_digest, 32) != 0) return RX_GEN_ERR_TORN; 1430 if (root_reachable(dir, &repaired) != RX_GEN_OK) return RX_GEN_ERR_TORN; 1431 RxGenStore scratch; 1432 memset(&scratch, 0, sizeof scratch); 1433 scratch.lock_fd = -1; 1434 snprintf(scratch.dir, sizeof scratch.dir, "%s", dir); 1435 uint8_t pointer[PTR_BYTES]; 1436 encode_pointer(repaired.id, repaired.lineage, got, pointer); 1437 char active[512]; 1438 if (path_join(active, sizeof active, dir, "active") != 0) return RX_GEN_ERR_IO; 1439 rc = commit_file(&scratch, active, pointer, sizeof pointer, RX_CRASH_NONE); 1440 if (rc != RX_GEN_OK) return rc; 1441 id = repaired.id; 1442 lineage = repaired.lineage; 1443 memcpy(pointer_digest, got, 32); 1444 pointer_rc = RX_GEN_OK; 1445 } 1446 if (pointer_rc != RX_GEN_OK) return pointer_rc; 1447 1448 RootView view; 1449 uint8_t root_digest[32]; 1450 int rc = load_root_file(dir, id, &view, root_digest); 1451 if (rc != RX_GEN_OK) return rc; 1452 if (view.id != id || view.lineage != lineage || memcmp(root_digest, pointer_digest, 32) != 0) 1453 return RX_GEN_ERR_TORN; 1454 rc = root_reachable(dir, &view); 1455 if (rc != RX_GEN_OK) return rc; 1456 1457 int receipt = receipt_exists(dir, id); 1458 if (view.parent != 0 && journal_rc == RX_GEN_OK && candidate == id && !receipt) { 1459 RxGenStore scratch; 1460 memset(&scratch, 0, sizeof scratch); 1461 scratch.lock_fd = -1; 1462 scratch.crash_step = RX_CRASH_NONE; 1463 snprintf(scratch.dir, sizeof scratch.dir, "%s", dir); 1464 rc = write_receipt_file(&scratch, &view, 0, 0, 0); 1465 if (rc != RX_GEN_OK) return rc; 1466 rc = append_event(&scratch, view.id, view.lineage); 1467 if (rc != RX_GEN_OK) return rc; 1468 receipt = 1; 1469 } 1470 fill_record(&view, receipt, event_has(dir, id) || view.parent == 0, out); 1471 if (view.parent != 0 && !out->event_present) { 1472 RxGenStore scratch; 1473 memset(&scratch, 0, sizeof scratch); 1474 scratch.lock_fd = -1; 1475 snprintf(scratch.dir, sizeof scratch.dir, "%s", dir); 1476 if (append_event(&scratch, view.id, view.lineage) != RX_GEN_OK) return RX_GEN_ERR_IO; 1477 out->event_present = 1; 1478 } 1479 return RX_GEN_OK; 1480 } 1481 1482 int rx_gen_reject_replay(const char *dir, uint64_t effect_id) { 1483 RxRecoveryRecord rec; 1484 int rc = rx_gen_recover(dir, &rec); 1485 if (rc != RX_GEN_OK) return rc; 1486 for (uint32_t i = 0; i < rec.n_external; i++) 1487 if (rec.external_ids[i] == effect_id) return RX_GEN_ERR_REPLAY; 1488 return RX_GEN_OK; 1489 } 1490 1491 /* ---- durable executor --------------------------------------------------- */ 1492 1493 typedef struct { 1494 int state; 1495 uint64_t seq; /* post order; the lower pending seq runs first */ 1496 uint64_t key; 1497 RxGenDoneFn done; 1498 void *done_ctx; 1499 RxGenJobResult result; 1500 /* proposal: an owned copy of the draft */ 1501 uint32_t proposer; 1502 RxCallerCred proposer_cred; /* R16 C5; wiped when the job has run */ 1503 RxGenDraft draft; 1504 RxGenObject *objects; 1505 uint8_t *bytes; 1506 /* promotion */ 1507 RxPromotionRequest request; 1508 RxGenAuthFn auth; 1509 void *auth_ctx; 1510 } Job; 1511 1512 struct RxGenExec { 1513 RxGenStore *store; 1514 pthread_t thread; 1515 pthread_mutex_t mu; 1516 pthread_cond_t cv; 1517 int stop; 1518 uint64_t seq; 1519 Job job[RX_GEN_JOB_KINDS]; 1520 }; 1521 1522 static void job_free(Job *j) { 1523 free(j->objects); 1524 free(j->bytes); 1525 j->objects = NULL; 1526 j->bytes = NULL; 1527 } 1528 1529 static void *exec_main(void *arg) { 1530 struct RxGenExec *x = arg; 1531 pthread_mutex_lock(&x->mu); 1532 for (;;) { 1533 Job *next = NULL; 1534 for (int k = 0; k < RX_GEN_JOB_KINDS; k++) 1535 if (x->job[k].state == RX_GEN_JOB_PENDING && (!next || x->job[k].seq < next->seq)) 1536 next = &x->job[k]; 1537 if (!next) { 1538 if (x->stop) break; 1539 pthread_cond_wait(&x->cv, &x->mu); 1540 continue; 1541 } 1542 pthread_mutex_unlock(&x->mu); 1543 RxGenJobResult r; 1544 memset(&r, 0, sizeof r); 1545 r.key = next->key; 1546 uint64_t t0 = monotonic_ns(); 1547 if (next == &x->job[RX_GEN_JOB_PROPOSE]) 1548 r.rc = rx_gen_propose_as(x->store, next->proposer, &next->proposer_cred, 1549 &next->draft, &r.id); 1550 else 1551 r.rc = rx_gen_promote(x->store, &next->request, next->auth, next->auth_ctx, 1552 NULL, NULL, NULL, NULL); 1553 r.ns = monotonic_ns() - t0; 1554 rx_gen_active(x->store, &r.active, &r.lineage); 1555 rx_caller_wipe(&next->proposer_cred); 1556 rx_caller_wipe(&next->request.caller); 1557 job_free(next); 1558 pthread_mutex_lock(&x->mu); 1559 next->result = r; 1560 next->state = RX_GEN_JOB_DONE; 1561 RxGenDoneFn done = next->done; 1562 void *ctx = next->done_ctx; 1563 pthread_mutex_unlock(&x->mu); 1564 if (done) done(ctx); 1565 pthread_mutex_lock(&x->mu); 1566 } 1567 pthread_mutex_unlock(&x->mu); 1568 return NULL; 1569 } 1570 1571 int rx_gen_exec_start(RxGenStore *store) { 1572 if (!store) return RX_GEN_ERR_ARG; 1573 if (store->exec) return RX_GEN_OK; 1574 struct RxGenExec *x = calloc(1, sizeof *x); 1575 if (!x) return RX_GEN_ERR_IO; 1576 x->store = store; 1577 pthread_mutex_init(&x->mu, NULL); 1578 pthread_cond_init(&x->cv, NULL); 1579 if (pthread_create(&x->thread, NULL, exec_main, x) != 0) { 1580 pthread_cond_destroy(&x->cv); 1581 pthread_mutex_destroy(&x->mu); 1582 free(x); 1583 return RX_GEN_ERR_IO; 1584 } 1585 store->exec = x; 1586 return RX_GEN_OK; 1587 } 1588 1589 int rx_gen_exec_running(const RxGenStore *store) { 1590 return store && store->exec != NULL; 1591 } 1592 1593 /* Runs every job already posted, then stops. A posted job is a promise the 1594 * caller is waiting on; it is never dropped. */ 1595 static void exec_stop(RxGenStore *store) { 1596 struct RxGenExec *x = store->exec; 1597 if (!x) return; 1598 pthread_mutex_lock(&x->mu); 1599 x->stop = 1; 1600 pthread_cond_signal(&x->cv); 1601 pthread_mutex_unlock(&x->mu); 1602 pthread_join(x->thread, NULL); 1603 for (int k = 0; k < RX_GEN_JOB_KINDS; k++) job_free(&x->job[k]); 1604 pthread_cond_destroy(&x->cv); 1605 pthread_mutex_destroy(&x->mu); 1606 free(x); 1607 store->exec = NULL; 1608 } 1609 1610 static Job *claim_slot(struct RxGenExec *x, int kind) { 1611 Job *j = &x->job[kind]; 1612 if (x->stop || j->state != RX_GEN_JOB_IDLE) return NULL; 1613 return j; 1614 } 1615 1616 static void post_locked(struct RxGenExec *x, Job *j, uint64_t key, RxGenDoneFn done, 1617 void *done_ctx) { 1618 j->key = key; 1619 j->done = done; 1620 j->done_ctx = done_ctx; 1621 j->seq = ++x->seq; 1622 memset(&j->result, 0, sizeof j->result); 1623 j->state = RX_GEN_JOB_PENDING; 1624 pthread_cond_signal(&x->cv); 1625 } 1626 1627 int rx_gen_post_propose(RxGenStore *store, uint64_t key, uint32_t proposer, 1628 const RxGenDraft *draft, RxGenDoneFn done, void *done_ctx) { 1629 return rx_gen_post_propose_as(store, key, proposer, NULL, draft, done, done_ctx); 1630 } 1631 1632 int rx_gen_post_propose_as(RxGenStore *store, uint64_t key, uint32_t proposer, 1633 const RxCallerCred *cred, const RxGenDraft *draft, 1634 RxGenDoneFn done, void *done_ctx) { 1635 if (!store || !store->exec || !draft) return RX_GEN_ERR_ARG; 1636 if (draft->n_objects > RX_GEN_MAX_OBJECTS || (draft->n_objects && !draft->objects)) 1637 return RX_GEN_ERR_ARG; 1638 const uint8_t *src[5] = {draft->evidence, draft->model, draft->realization, 1639 draft->config, draft->provenance}; 1640 size_t len[5] = {draft->evidence_len, draft->model_len, draft->realization_len, 1641 draft->config_len, draft->provenance_len}; 1642 size_t total = 0; 1643 for (int i = 0; i < 5; i++) { 1644 if (len[i] && !src[i]) return RX_GEN_ERR_ARG; 1645 total += len[i]; 1646 } 1647 RxGenObject *objs = NULL; 1648 uint8_t *bytes = NULL; 1649 if (draft->n_objects) { 1650 objs = malloc(draft->n_objects * sizeof *objs); 1651 if (!objs) return RX_GEN_ERR_IO; 1652 memcpy(objs, draft->objects, draft->n_objects * sizeof *objs); 1653 } 1654 if (total) { 1655 bytes = malloc(total); 1656 if (!bytes) { 1657 free(objs); 1658 return RX_GEN_ERR_IO; 1659 } 1660 } 1661 RxGenDraft copy = *draft; 1662 copy.objects = objs; 1663 const uint8_t **dst[5] = {©.evidence, ©.model, ©.realization, ©.config, 1664 ©.provenance}; 1665 size_t at = 0; 1666 for (int i = 0; i < 5; i++) { 1667 *dst[i] = len[i] ? bytes + at : NULL; 1668 if (len[i]) memcpy(bytes + at, src[i], len[i]); 1669 at += len[i]; 1670 } 1671 struct RxGenExec *x = store->exec; 1672 pthread_mutex_lock(&x->mu); 1673 Job *j = claim_slot(x, RX_GEN_JOB_PROPOSE); 1674 if (!j) { 1675 pthread_mutex_unlock(&x->mu); 1676 free(objs); 1677 free(bytes); 1678 return RX_GEN_ERR_BUSY; 1679 } 1680 j->proposer = proposer; 1681 if (cred) j->proposer_cred = *cred; 1682 else memset(&j->proposer_cred, 0, sizeof j->proposer_cred); 1683 j->draft = copy; 1684 j->objects = objs; 1685 j->bytes = bytes; 1686 post_locked(x, j, key, done, done_ctx); 1687 pthread_mutex_unlock(&x->mu); 1688 return RX_GEN_OK; 1689 } 1690 1691 int rx_gen_post_promote(RxGenStore *store, uint64_t key, const RxPromotionRequest *request, 1692 RxGenAuthFn auth, void *auth_ctx, RxGenDoneFn done, void *done_ctx) { 1693 if (!store || !store->exec || !request || (!auth && !store_bound(store))) return RX_GEN_ERR_ARG; 1694 struct RxGenExec *x = store->exec; 1695 pthread_mutex_lock(&x->mu); 1696 Job *j = claim_slot(x, RX_GEN_JOB_PROMOTE); 1697 if (!j) { 1698 pthread_mutex_unlock(&x->mu); 1699 return RX_GEN_ERR_BUSY; 1700 } 1701 j->request = *request; 1702 j->auth = auth; 1703 j->auth_ctx = auth_ctx; 1704 post_locked(x, j, key, done, done_ctx); 1705 pthread_mutex_unlock(&x->mu); 1706 return RX_GEN_OK; 1707 } 1708 1709 int rx_gen_job_state(RxGenStore *store, int kind, RxGenJobResult *out) { 1710 if (!store || !store->exec || kind < 0 || kind >= RX_GEN_JOB_KINDS) return RX_GEN_ERR_ARG; 1711 struct RxGenExec *x = store->exec; 1712 pthread_mutex_lock(&x->mu); 1713 int st = x->job[kind].state; 1714 if (st == RX_GEN_JOB_DONE && out) *out = x->job[kind].result; 1715 pthread_mutex_unlock(&x->mu); 1716 return st; 1717 } 1718 1719 void rx_gen_job_take(RxGenStore *store, int kind) { 1720 if (!store || !store->exec || kind < 0 || kind >= RX_GEN_JOB_KINDS) return; 1721 struct RxGenExec *x = store->exec; 1722 pthread_mutex_lock(&x->mu); 1723 if (x->job[kind].state == RX_GEN_JOB_DONE) x->job[kind].state = RX_GEN_JOB_IDLE; 1724 pthread_mutex_unlock(&x->mu); 1725 }