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/tests/runtime/rx_heartbeat_test.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 / tests / runtime / rx_heartbeat_test.c` 2668 lines ยท 123861 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * rx_heartbeat_test.c -- qualification suite for the first heartbeat of the 19 * resident reaction runtime (ADR 0016; gates R3 and R4, host reference). 20 * 21 * The heartbeat this suite must show, with no central function sequencing it: 22 * 23 * shared object -> dependency change -> reaction becomes ready -> 24 * capability validates -> reaction executes -> atomic publication -> 25 * causal crumb 26 * 27 * The test code only (a) sets up objects and capabilities, (b) injects 28 * external stimuli, and (c) waits for quiescence and inspects. It never calls a 29 * reaction. Ordering comes from the dependency index. 30 * 31 * Receipt: build/qual-runs//R3/rx_heartbeat_receipt.json. A recorded 32 * receipt is committed separately as evidence/R3/.json in an 33 * evidence-only commit. Counts are observed, never literal. The candidate 34 * commit is an input (OMEGA_CANDIDATE_COMMIT), not HEAD. 35 */ 36 #include "runtime/rx_caproot.h" 37 #include "runtime/rx_world.h" 38 #include "omega_evidence.h" 39 #include "sha256.h" 40 41 #include 42 #include 43 #include 44 #include 45 #include 46 #include 47 #include 48 #include 49 #include 50 #include 51 #include 52 #include 53 #include 54 55 /* ---- tiny check framework ----------------------------------------------- */ 56 57 #define MAX_TESTS 48 58 typedef struct { 59 const char *name; 60 const char *invariants; 61 int checks; 62 int failures; 63 } TestRec; 64 65 static TestRec g_tests[MAX_TESTS]; 66 static int g_ntests; 67 static TestRec *g_cur; 68 static uint64_t g_crumbs_verified; 69 static uint64_t g_crumbs_audited; 70 static uint64_t g_illegal_transitions; 71 static uint64_t g_transitions[RX_STATE_COUNT][RX_STATE_COUNT]; 72 73 #define CHECK(cond, ...) do { \ 74 g_cur->checks++; \ 75 if (!(cond)) { \ 76 g_cur->failures++; \ 77 fprintf(stderr, " FAIL %s:%d [%s] ", __FILE__, __LINE__, g_cur->name); \ 78 fprintf(stderr, __VA_ARGS__); \ 79 fputc('\n', stderr); \ 80 } \ 81 } while (0) 82 83 static void begin(const char *name, const char *invariants) { 84 g_cur = &g_tests[g_ntests++]; 85 g_cur->name = name; 86 g_cur->invariants = invariants; 87 printf("[*] %s\n", name); 88 } 89 90 static void sleep_ms(int ms) { 91 struct timespec ts = { ms / 1000, (long)(ms % 1000) * 1000000L }; 92 nanosleep(&ts, NULL); 93 } 94 95 /* ---- principals and resources ------------------------------------------- */ 96 97 enum { SUBJ_AIEN = 1, SUBJ_OMEGA = 2, SUBJ_AEGIS = 3, SUBJ_EXTERNAL = 100, SUBJ_OTHER = 7 }; 98 enum { ISSUER_AEGIS_POLICY = 3 }; 99 enum { 100 RES_SENSOR = 0x10, RES_BELIEF = 0x20, RES_PLAN = 0x30, RES_RISK = 0x40, 101 RES_FAN_SRC = 0x50, RES_FAN_OUT = 0x51, RES_PAIR = 0x60, RES_TICK = 0x61, 102 RES_CHECK = 0x62, RES_SCRATCH = 0x70 103 }; 104 enum { F_TEMP = 0, F_HUMIDITY = 1 }; 105 106 typedef struct { 107 RxCapRoot root; 108 RxCapAdmin admin; 109 RxWorld w; 110 } Env; 111 112 static int env_start(Env *e, uint32_t workers) { 113 if (rx_caproot_start(&e->root, &e->admin) != RX_CAP_OK) return -1; 114 if (rx_world_init(&e->w, &e->root, workers, 1u << 18) != RX_OK) { 115 rx_caproot_stop(&e->root, &e->admin); 116 return -1; 117 } 118 e->w.external_subject = SUBJ_EXTERNAL; 119 return 0; 120 } 121 122 static RxCapRef office_of(const RxCapAdmin *admin) { return rx_capadmin_office(admin); } 123 124 static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { 125 RxCapMint m; 126 memset(&m, 0, sizeof(m)); 127 m.issuer = ISSUER_AEGIS_POLICY; 128 m.subject = subject; 129 m.resource = resource; 130 m.rights = rights; 131 m.parent = (RxCapRef){ UINT32_MAX, 0 }; 132 m.authority = office_of(&e->admin); 133 RxCapRef r = { UINT32_MAX, 0 }; 134 int rc = rx_capadmin_mint(&e->admin, &m, &r); 135 if (rc != RX_CAP_OK) fprintf(stderr, "mint failed: %s\n", rx_cap_strerror(rc)); 136 return r; 137 } 138 139 static int revoke_cap(RxCapAdmin *admin, RxCapRef ref) { 140 return rx_capadmin_revoke(admin, office_of(admin), ref); 141 } 142 143 static RxObjRef mkobj(Env *e, uint64_t resource, uint64_t v0) { 144 uint64_t init[RX_MAX_FIELDS] = { v0 }; 145 RxObjRef r = { UINT32_MAX, 0 }; 146 rx_world_create(&e->w, 1, RX_PERSIST_RESIDENT, resource, init, &r); 147 return r; 148 } 149 150 static uint64_t field(Env *e, RxObjRef r, uint32_t f) { 151 RxObject o; 152 if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX; 153 return o.field[f]; 154 } 155 156 static int64_t stimulus(Env *e, RxCapRef cap, RxObjRef obj, uint32_t f, uint64_t v) { 157 RxMutation m = { obj, f, v }; 158 return rx_world_publish_external(&e->w, cap, &m, 1); 159 } 160 161 static void desc_init(RxReactionDesc *d, const char *name, uint32_t faculty, uint32_t subject, 162 RxFn fn, void *user) { 163 memset(d, 0, sizeof(*d)); 164 d->name = name; 165 d->faculty = faculty; 166 d->subject = subject; 167 d->priority = RX_PRIO_FOREGROUND; 168 d->fn = fn; 169 d->user = user; 170 } 171 172 static void add_trigger(RxReactionDesc *d, RxObjRef o, uint64_t mask) { 173 d->triggers[d->n_triggers++] = (RxDep){ o, mask }; 174 } 175 static void add_write(RxReactionDesc *d, RxObjRef o, uint64_t mask) { 176 d->writes[d->n_writes++] = (RxDep){ o, mask }; 177 } 178 static void add_cap(RxReactionDesc *d, RxCapRef c, uint64_t res, uint32_t rights) { 179 d->caps[d->n_caps++] = (RxCapNeed){ c, res, rights }; 180 } 181 182 /* Find the snapshot entry for an object. */ 183 static const RxSnapshotDep *in_of(const RxCtx *c, RxObjRef o) { 184 for (uint32_t i = 0; i < c->n_in; i++) 185 if (c->in[i].obj.id == o.id) return &c->in[i]; 186 return NULL; 187 } 188 189 /* ---- causal audit: every reaction outcome must be explainable ------------ */ 190 191 static bool reaches_root(const RxWorld *w, uint64_t id, int depth) { 192 const RxCrumb *k = rx_world_crumb(w, id); 193 if (!k || depth > 4096) return false; 194 if (k->kind == RX_CRUMB_EXTERNAL || k->kind == RX_CRUMB_CREATE) return true; 195 for (uint32_t i = 0; i < k->n_parents; i++) 196 if (reaches_root(w, k->parents[i], depth + 1)) return true; 197 return false; 198 } 199 200 static void audit_and_close(Env *e) { 201 RxWorld *w = &e->w; 202 CHECK(rx_world_wait_quiescent(w, 10000) == RX_OK, "world did not quiesce"); 203 uint64_t checked = 0; 204 int vrc = rx_world_verify_crumbs(w, &checked); 205 CHECK(vrc == 0, "crumb digest chain verification failed (%d)", vrc); 206 CHECK(w->stats.crumb_overflow == 0, "crumb log overflowed"); 207 g_crumbs_verified += checked; 208 for (uint64_t id = 1; id <= w->n_crumbs; id++) { 209 const RxCrumb *k = rx_world_crumb(w, id); 210 if (k->reaction == UINT32_MAX) continue; 211 g_crumbs_audited++; 212 const RxReactionDesc *d = &w->reactions[k->reaction].desc; 213 /* A containment record is not an activation: it names the limit and 214 * the crumb that tripped it, which is this reaction's own activation 215 * or the wake it refused. */ 216 if (k->kind == RX_CRUMB_QUARANTINE) { 217 const RxCrumb *by = rx_world_crumb(w, k->wake_cause); 218 CHECK(by && by->id < k->id && k->n_parents == 1 && k->parents[0] == by->id, 219 "crumb %llu: containment without its cause", (unsigned long long)id); 220 CHECK(k->reason >= RX_CONTAIN_BUDGET && k->reason <= RX_CONTAIN_CONFLICT, 221 "crumb %llu: containment names no limit", (unsigned long long)id); 222 CHECK(reaches_root(w, id, 0), "crumb %llu: containment does not reach a stimulus", 223 (unsigned long long)id); 224 continue; 225 } 226 /* trigger explainable: woken by an earlier crumb that wrote a trigger */ 227 const RxCrumb *cause = rx_world_crumb(w, k->wake_cause); 228 bool trig = false; 229 if (cause && cause->id < k->id) 230 for (uint32_t o = 0; o < cause->n_outputs; o++) 231 for (uint32_t t = 0; t < d->n_triggers; t++) 232 if (cause->outputs[o].obj.id == d->triggers[t].obj.id && 233 (cause->outputs[o].mask & d->triggers[t].mask)) trig = true; 234 CHECK(trig, "crumb %llu (%s): trigger not explainable", 235 (unsigned long long)id, d->name); 236 CHECK(k->n_inputs > 0, "crumb %llu: inputs not recorded", (unsigned long long)id); 237 CHECK(k->n_caps == d->n_caps, "crumb %llu: authority not recorded", (unsigned long long)id); 238 if (k->kind == RX_CRUMB_COMMIT) 239 CHECK(k->n_outputs > 0, "crumb %llu: commit without outputs", (unsigned long long)id); 240 CHECK(reaches_root(w, id, 0), "crumb %llu: ancestry does not reach a stimulus/creation", 241 (unsigned long long)id); 242 } 243 g_illegal_transitions += w->stats.illegal_transitions; 244 for (int a = 0; a < RX_STATE_COUNT; a++) 245 for (int b = 0; b < RX_STATE_COUNT; b++) { 246 g_transitions[a][b] += w->stats.transitions[a][b]; 247 if (w->stats.transitions[a][b]) 248 CHECK(rx_state_transition_legal((RxState)a, (RxState)b), 249 "illegal transition %s -> %s", rx_state_name((RxState)a), 250 rx_state_name((RxState)b)); 251 } 252 CHECK(w->stats.illegal_transitions == 0, "illegal lifecycle transitions observed"); 253 rx_world_destroy(w); 254 rx_caproot_stop(&e->root, &e->admin); 255 } 256 257 /* ---- heartbeat reactions -------------------------------------------------- */ 258 259 typedef struct { RxObjRef sensor, belief, plan, risk; } HB; 260 261 static int fn_hypothesis(RxCtx *c) { /* AIEN: sensor -> belief */ 262 HB *h = c->user; 263 const RxSnapshotDep *s = in_of(c, h->sensor); 264 c->out[c->n_out++] = (RxMutation){ h->belief, 0, s->field[F_TEMP] * 2 + 1 }; 265 return 0; 266 } 267 268 static int fn_realize(RxCtx *c) { /* OMEGA: belief -> plan */ 269 HB *h = c->user; 270 const RxSnapshotDep *b = in_of(c, h->belief); 271 c->out[c->n_out++] = (RxMutation){ h->plan, 0, b->field[0] + 1000 }; 272 return 0; 273 } 274 275 static int fn_risk(RxCtx *c) { /* fan-in of two triggers */ 276 HB *h = c->user; 277 const RxSnapshotDep *s = in_of(c, h->sensor); 278 const RxSnapshotDep *b = in_of(c, h->belief); 279 c->out[c->n_out++] = (RxMutation){ h->risk, 0, s->field[F_HUMIDITY] * 100000 + b->field[0] }; 280 return 0; 281 } 282 283 typedef struct { Env e; HB h; uint32_t ra, rb, rc; RxCapRef c_ext; } HBEnv; 284 285 static int hb_setup(HBEnv *x, uint32_t workers) { 286 if (env_start(&x->e, workers) != 0) return -1; 287 Env *e = &x->e; 288 x->h.sensor = mkobj(e, RES_SENSOR, 0); 289 x->h.belief = mkobj(e, RES_BELIEF, 0); 290 x->h.plan = mkobj(e, RES_PLAN, 0); 291 x->h.risk = mkobj(e, RES_RISK, 0); 292 x->c_ext = mint(e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 293 RxCapRef a_rs = mint(e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 294 RxCapRef a_wb = mint(e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE); 295 RxCapRef o_rb = mint(e, SUBJ_OMEGA, RES_BELIEF, RX_RIGHT_READ); 296 RxCapRef o_wp = mint(e, SUBJ_OMEGA, RES_PLAN, RX_RIGHT_WRITE); 297 RxCapRef o_rs = mint(e, SUBJ_OMEGA, RES_SENSOR, RX_RIGHT_READ); 298 RxCapRef o_wr = mint(e, SUBJ_OMEGA, RES_RISK, RX_RIGHT_WRITE); 299 300 RxReactionDesc d; 301 desc_init(&d, "aien.hypothesis", RX_FACULTY_AIEN, SUBJ_AIEN, fn_hypothesis, &x->h); 302 add_trigger(&d, x->h.sensor, RX_FIELD(F_TEMP)); 303 add_write(&d, x->h.belief, RX_FIELD(0)); 304 add_cap(&d, a_rs, RES_SENSOR, RX_RIGHT_READ); 305 add_cap(&d, a_wb, RES_BELIEF, RX_RIGHT_WRITE); 306 if (rx_world_add_reaction(&e->w, &d, &x->ra) != RX_OK) return -1; 307 308 desc_init(&d, "omega.realize", RX_FACULTY_OMEGA, SUBJ_OMEGA, fn_realize, &x->h); 309 add_trigger(&d, x->h.belief, RX_FIELD(0)); 310 add_write(&d, x->h.plan, RX_FIELD(0)); 311 add_cap(&d, o_rb, RES_BELIEF, RX_RIGHT_READ); 312 add_cap(&d, o_wp, RES_PLAN, RX_RIGHT_WRITE); 313 if (rx_world_add_reaction(&e->w, &d, &x->rb) != RX_OK) return -1; 314 315 desc_init(&d, "omega.risk", RX_FACULTY_OMEGA, SUBJ_OMEGA, fn_risk, &x->h); 316 add_trigger(&d, x->h.sensor, RX_FIELD(F_HUMIDITY)); 317 add_trigger(&d, x->h.belief, RX_FIELD(0)); 318 add_write(&d, x->h.risk, RX_FIELD(0)); 319 add_cap(&d, o_rs, RES_SENSOR, RX_RIGHT_READ); 320 add_cap(&d, o_rb, RES_BELIEF, RX_RIGHT_READ); 321 add_cap(&d, o_wr, RES_RISK, RX_RIGHT_WRITE); 322 if (rx_world_add_reaction(&e->w, &d, &x->rc) != RX_OK) return -1; 323 return 0; 324 } 325 326 /* ---- tests ---------------------------------------------------------------- */ 327 328 static char g_heartbeat_trace[2048]; 329 330 static void t_heartbeat(void) { 331 begin("heartbeat_chain", "I3,I4,I6,I7"); 332 HBEnv x; 333 CHECK(hb_setup(&x, 4) == 0, "setup"); 334 Env *e = &x.e; 335 int64_t ext = stimulus(e, x.c_ext, x.h.sensor, F_TEMP, 21); 336 CHECK(ext > 0, "external stimulus refused"); 337 CHECK(rx_world_wait_quiescent(&e->w, 5000) == RX_OK, "quiescence"); 338 CHECK(field(e, x.h.belief, 0) == 43, "belief = %llu", (unsigned long long)field(e, x.h.belief, 0)); 339 CHECK(field(e, x.h.plan, 0) == 1043, "plan = %llu", (unsigned long long)field(e, x.h.plan, 0)); 340 341 /* Walk back from the result: plan <- omega.realize <- belief <- aien.hypothesis <- stimulus */ 342 uint64_t c_plan = rx_world_explain(&e->w, x.h.plan, 0); 343 const RxCrumb *kp = rx_world_crumb(&e->w, c_plan); 344 CHECK(kp && kp->kind == RX_CRUMB_COMMIT && kp->reaction == x.rb, "plan writer is omega.realize"); 345 uint64_t c_bel = rx_world_explain(&e->w, x.h.belief, 0); 346 const RxCrumb *kb = rx_world_crumb(&e->w, c_bel); 347 CHECK(kb && kb->kind == RX_CRUMB_COMMIT && kb->reaction == x.ra, "belief writer is aien.hypothesis"); 348 CHECK(kp && kp->wake_cause == c_bel, "omega.realize was woken by the belief commit"); 349 CHECK(kb && kb->wake_cause == (uint64_t)ext, "aien.hypothesis was woken by the stimulus"); 350 bool parent_ok = false; 351 for (uint32_t i = 0; kp && i < kp->n_parents; i++) if (kp->parents[i] == c_bel) parent_ok = true; 352 CHECK(parent_ok, "plan crumb lists the belief crumb as a causal parent"); 353 CHECK(kb && kb->n_caps == 2 && kp && kp->n_caps == 2, "authority used is recorded"); 354 CHECK(kb && kb->faculty == RX_FACULTY_AIEN && kp && kp->faculty == RX_FACULTY_OMEGA, 355 "faculty contributions recorded"); 356 CHECK(e->w.reactions[x.ra].activations == 1 && e->w.reactions[x.rb].activations == 1, 357 "each stage activated exactly once"); 358 if (kp && kb) { 359 const RxCrumb *ke = rx_world_crumb(&e->w, (uint64_t)ext); 360 char dg[3][17]; 361 const RxCrumb *ks[3] = { ke, kb, kp }; 362 for (int i = 0; i < 3; i++) 363 for (int j = 0; j < 8; j++) sprintf(&dg[i][j * 2], "%02x", ks[i]->digest[j]); 364 snprintf(g_heartbeat_trace, sizeof(g_heartbeat_trace), 365 "[{\"crumb\":%llu,\"kind\":\"EXTERNAL\",\"digest16\":\"%s\"}," 366 "{\"crumb\":%llu,\"kind\":\"COMMIT\",\"reaction\":\"aien.hypothesis\",\"wake_cause\":%llu,\"digest16\":\"%s\"}," 367 "{\"crumb\":%llu,\"kind\":\"COMMIT\",\"reaction\":\"omega.realize\",\"wake_cause\":%llu,\"digest16\":\"%s\"}]", 368 (unsigned long long)ke->id, dg[0], 369 (unsigned long long)kb->id, (unsigned long long)kb->wake_cause, dg[1], 370 (unsigned long long)kp->id, (unsigned long long)kp->wake_cause, dg[2]); 371 } 372 audit_and_close(e); 373 } 374 375 static void t_single_dependency_narrow(void) { 376 begin("single_dependency_field_granularity", "I7"); 377 HBEnv x; 378 CHECK(hb_setup(&x, 2) == 0, "setup"); 379 Env *e = &x.e; 380 stimulus(e, x.c_ext, x.h.sensor, F_TEMP, 5); 381 rx_world_wait_quiescent(&e->w, 5000); 382 uint64_t a0 = e->w.reactions[x.ra].activations; 383 /* Humidity is not in aien.hypothesis's trigger mask: it must not wake. */ 384 stimulus(e, x.c_ext, x.h.sensor, F_HUMIDITY, 9); 385 rx_world_wait_quiescent(&e->w, 5000); 386 CHECK(e->w.reactions[x.ra].activations == a0, "temperature-only reaction woke on humidity"); 387 CHECK(e->w.reactions[x.rc].activations >= 1, "humidity subscriber did wake"); 388 /* Same value again: no change, no wake (fixed point). */ 389 uint64_t b0 = e->w.reactions[x.rb].activations; 390 stimulus(e, x.c_ext, x.h.sensor, F_TEMP, 5); 391 rx_world_wait_quiescent(&e->w, 5000); 392 CHECK(e->w.reactions[x.ra].activations == a0 && e->w.reactions[x.rb].activations == b0, 393 "unchanged value woke reactions"); 394 audit_and_close(e); 395 } 396 397 static void t_multiple_dependency(void) { 398 begin("multiple_dependency_fan_in_two_triggers", "I6,I7"); 399 HBEnv x; 400 CHECK(hb_setup(&x, 4) == 0, "setup"); 401 Env *e = &x.e; 402 stimulus(e, x.c_ext, x.h.sensor, F_HUMIDITY, 3); 403 stimulus(e, x.c_ext, x.h.sensor, F_TEMP, 10); 404 rx_world_wait_quiescent(&e->w, 5000); 405 /* risk = humidity*100000 + belief, belief = 2*temp + 1 */ 406 CHECK(field(e, x.h.risk, 0) == 3 * 100000 + 21, "risk = %llu", 407 (unsigned long long)field(e, x.h.risk, 0)); 408 audit_and_close(e); 409 } 410 411 typedef struct { RxObjRef src; RxObjRef out; uint64_t add; } FanArg; 412 413 static int fn_fan(RxCtx *c) { 414 FanArg *a = c->user; 415 c->out[c->n_out++] = (RxMutation){ a->out, 0, in_of(c, a->src)->field[0] + a->add }; 416 return 0; 417 } 418 419 static void t_fan_out(void) { 420 begin("fan_out_64", "I6,I7"); 421 Env e; 422 CHECK(env_start(&e, 8) == 0, "setup"); 423 RxObjRef src = mkobj(&e, RES_FAN_SRC, 0); 424 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_FAN_SRC, RX_RIGHT_WRITE); 425 RxCapRef r = mint(&e, SUBJ_OMEGA, RES_FAN_SRC, RX_RIGHT_READ); 426 RxCapRef wr = mint(&e, SUBJ_OMEGA, RES_FAN_OUT, RX_RIGHT_WRITE); 427 static FanArg args[64]; 428 uint32_t ids[64]; 429 for (int i = 0; i < 64; i++) { 430 args[i] = (FanArg){ src, mkobj(&e, RES_FAN_OUT, 0), (uint64_t)i }; 431 RxReactionDesc d; 432 desc_init(&d, "fan.out", RX_FACULTY_OMEGA, SUBJ_OMEGA, fn_fan, &args[i]); 433 add_trigger(&d, src, RX_FIELD(0)); 434 add_write(&d, args[i].out, RX_FIELD(0)); 435 add_cap(&d, r, RES_FAN_SRC, RX_RIGHT_READ); 436 add_cap(&d, wr, RES_FAN_OUT, RX_RIGHT_WRITE); 437 CHECK(rx_world_add_reaction(&e.w, &d, &ids[i]) == RX_OK, "add fan reaction %d", i); 438 } 439 stimulus(&e, ext, src, 0, 1000); 440 rx_world_wait_quiescent(&e.w, 5000); 441 int bad = 0, multi = 0; 442 for (int i = 0; i < 64; i++) { 443 if (field(&e, args[i].out, 0) != 1000u + (uint64_t)i) bad++; 444 if (e.w.reactions[ids[i]].activations != 1) multi++; 445 } 446 CHECK(bad == 0, "%d fan-out outputs wrong", bad); 447 CHECK(multi == 0, "%d fan-out reactions ran more or less than once", multi); 448 audit_and_close(&e); 449 } 450 451 typedef struct { RxObjRef src[8]; RxObjRef agg; } FanInArg; 452 453 static int fn_sum(RxCtx *c) { 454 FanInArg *a = c->user; 455 uint64_t s = 0; 456 for (int i = 0; i < 8; i++) s += in_of(c, a->src[i])->field[0]; 457 c->out[c->n_out++] = (RxMutation){ a->agg, 0, s }; 458 return 0; 459 } 460 461 static void t_fan_in(void) { 462 begin("fan_in_8", "I6,I7"); 463 Env e; 464 CHECK(env_start(&e, 4) == 0, "setup"); 465 static FanInArg a; 466 for (int i = 0; i < 8; i++) a.src[i] = mkobj(&e, RES_FAN_SRC, 0); 467 a.agg = mkobj(&e, RES_FAN_OUT, 0); 468 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_FAN_SRC, RX_RIGHT_WRITE); 469 RxCapRef r = mint(&e, SUBJ_OMEGA, RES_FAN_SRC, RX_RIGHT_READ); 470 RxCapRef wr = mint(&e, SUBJ_OMEGA, RES_FAN_OUT, RX_RIGHT_WRITE); 471 RxReactionDesc d; 472 desc_init(&d, "fan.in", RX_FACULTY_OMEGA, SUBJ_OMEGA, fn_sum, &a); 473 for (int i = 0; i < 8; i++) add_trigger(&d, a.src[i], RX_FIELD(0)); 474 add_write(&d, a.agg, RX_FIELD(0)); 475 add_cap(&d, r, RES_FAN_SRC, RX_RIGHT_READ); 476 add_cap(&d, wr, RES_FAN_OUT, RX_RIGHT_WRITE); 477 uint32_t id; 478 CHECK(rx_world_add_reaction(&e.w, &d, &id) == RX_OK, "add"); 479 uint64_t want = 0; 480 for (int round = 0; round < 5; round++) 481 for (int i = 0; i < 8; i++) { 482 uint64_t v = (uint64_t)(round * 10 + i + 1); 483 stimulus(&e, ext, a.src[i], 0, v); 484 } 485 for (int i = 0; i < 8; i++) want += (uint64_t)(40 + i + 1); 486 rx_world_wait_quiescent(&e.w, 5000); 487 CHECK(field(&e, a.agg, 0) == want, "agg=%llu want %llu", 488 (unsigned long long)field(&e, a.agg, 0), (unsigned long long)want); 489 printf(" fan-in: 40 stimuli -> %llu activations (%llu wakes coalesced)\n", 490 (unsigned long long)e.w.reactions[id].activations, 491 (unsigned long long)e.w.stats.coalesced_wakes); 492 audit_and_close(&e); 493 } 494 495 typedef struct { RxObjRef x, y; int delay_ms; } SlowArg; 496 497 static int fn_slow(RxCtx *c) { 498 SlowArg *a = c->user; 499 uint64_t v = in_of(c, a->x)->field[0]; 500 sleep_ms(a->delay_ms); 501 c->out[c->n_out++] = (RxMutation){ a->y, 0, v * 10 }; 502 return 0; 503 } 504 505 static void t_invalidation(void) { 506 begin("invalidation_stale_snapshot", "I2,I6,I7"); 507 Env e; 508 CHECK(env_start(&e, 2) == 0, "setup"); 509 static SlowArg a; 510 a.x = mkobj(&e, RES_SENSOR, 0); 511 a.y = mkobj(&e, RES_BELIEF, 0); 512 a.delay_ms = 80; 513 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 514 RxReactionDesc d; 515 desc_init(&d, "slow.derive", RX_FACULTY_AIEN, SUBJ_AIEN, fn_slow, &a); 516 add_trigger(&d, a.x, RX_FIELD(0)); 517 add_write(&d, a.y, RX_FIELD(0)); 518 add_cap(&d, mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ), RES_SENSOR, RX_RIGHT_READ); 519 add_cap(&d, mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE), RES_BELIEF, RX_RIGHT_WRITE); 520 uint32_t id; 521 CHECK(rx_world_add_reaction(&e.w, &d, &id) == RX_OK, "add"); 522 stimulus(&e, ext, a.x, 0, 1); 523 sleep_ms(20); /* first activation is computing on x=1 */ 524 stimulus(&e, ext, a.x, 0, 2); 525 rx_world_wait_quiescent(&e.w, 5000); 526 CHECK(field(&e, a.y, 0) == 20, "y=%llu (stale result committed?)", 527 (unsigned long long)field(&e, a.y, 0)); 528 CHECK(e.w.stats.invalidations >= 1, "no invalidation recorded"); 529 CHECK(e.w.reactions[id].commits == 1, "commits=%llu; the stale candidate must not commit", 530 (unsigned long long)e.w.reactions[id].commits); 531 bool saw = false; 532 for (uint64_t i = 1; i <= e.w.n_crumbs; i++) 533 if (rx_world_crumb(&e.w, i)->kind == RX_CRUMB_INVALIDATED) saw = true; 534 CHECK(saw, "INVALIDATED crumb missing"); 535 audit_and_close(&e); 536 } 537 538 /* A read-only input changes while the reaction computes. The candidate is 539 * stale and must not commit, but the trigger that woke it is still owed an 540 * answer: the reaction runs again and answers it. (Found by R10: a new Omega 541 * selection landed while the production path was serving a request, and the 542 * request was dropped.) */ 543 typedef struct { RxObjRef x, r, y; int delay_ms; } ReadArg; 544 545 static int fn_slow_read(RxCtx *c) { 546 ReadArg *a = c->user; 547 uint64_t v = in_of(c, a->x)->field[0]; 548 uint64_t k = in_of(c, a->r)->field[0]; 549 sleep_ms(a->delay_ms); 550 c->out[c->n_out++] = (RxMutation){ a->y, 0, v * 10 + k }; 551 return 0; 552 } 553 554 static void t_read_only_invalidation(void) { 555 begin("invalidation_by_read_only_input_reruns", "I2,I6,I7"); 556 Env e; 557 CHECK(env_start(&e, 2) == 0, "setup"); 558 static ReadArg a; 559 a.x = mkobj(&e, RES_SENSOR, 0); 560 a.r = mkobj(&e, RES_PLAN, 0); 561 a.y = mkobj(&e, RES_BELIEF, 0); 562 a.delay_ms = 80; 563 RxCapRef ext_x = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 564 RxCapRef ext_r = mint(&e, SUBJ_EXTERNAL, RES_PLAN, RX_RIGHT_WRITE); 565 RxReactionDesc d; 566 desc_init(&d, "slow.read", RX_FACULTY_AIEN, SUBJ_AIEN, fn_slow_read, &a); 567 add_trigger(&d, a.x, RX_FIELD(0)); 568 d.reads[d.n_reads++] = (RxDep){ a.r, RX_FIELD(0) }; 569 add_write(&d, a.y, RX_FIELD(0)); 570 add_cap(&d, mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ), RES_SENSOR, RX_RIGHT_READ); 571 add_cap(&d, mint(&e, SUBJ_AIEN, RES_PLAN, RX_RIGHT_READ), RES_PLAN, RX_RIGHT_READ); 572 add_cap(&d, mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE), RES_BELIEF, RX_RIGHT_WRITE); 573 uint32_t id; 574 CHECK(rx_world_add_reaction(&e.w, &d, &id) == RX_OK, "add"); 575 stimulus(&e, ext_x, a.x, 0, 1); 576 sleep_ms(20); /* computing on r = 0 */ 577 stimulus(&e, ext_r, a.r, 0, 5); /* not a trigger: wakes nobody */ 578 rx_world_wait_quiescent(&e.w, 5000); 579 CHECK(field(&e, a.y, 0) == 15, "y=%llu; the stimulus was dropped or answered stale", 580 (unsigned long long)field(&e, a.y, 0)); 581 CHECK(e.w.stats.invalidations == 1, "invalidations=%llu", 582 (unsigned long long)e.w.stats.invalidations); 583 CHECK(e.w.reactions[id].commits == 1, "commits=%llu", 584 (unsigned long long)e.w.reactions[id].commits); 585 CHECK(e.w.reactions[id].activations == 2, "activations=%llu", 586 (unsigned long long)e.w.reactions[id].activations); 587 audit_and_close(&e); 588 } 589 590 /* Concurrent publication: 8 incrementers on one PAIR object, driven by 4 591 * publisher threads. a and b must always move together, and no increment may 592 * be lost: a == b == number of increment commits. */ 593 typedef struct { RxObjRef pair, tick; } IncArg; 594 typedef struct { RxObjRef pair, check; } ChkArg; 595 596 static int fn_inc(RxCtx *c) { 597 IncArg *a = c->user; 598 const RxSnapshotDep *p = in_of(c, a->pair); 599 c->out[c->n_out++] = (RxMutation){ a->pair, 0, p->field[0] + 1 }; 600 c->out[c->n_out++] = (RxMutation){ a->pair, 1, p->field[1] + 1 }; 601 return 0; 602 } 603 604 static int fn_chk(RxCtx *c) { 605 ChkArg *a = c->user; 606 const RxSnapshotDep *p = in_of(c, a->pair); 607 const RxSnapshotDep *k = in_of(c, a->check); 608 if (p->field[0] != p->field[1]) 609 c->out[c->n_out++] = (RxMutation){ a->check, 0, k->field[0] + 1 }; 610 return 0; 611 } 612 613 typedef struct { Env *e; RxCapRef cap; RxObjRef ticks[8]; int base; } PubArg; 614 615 static void *publisher(void *arg) { 616 PubArg *p = arg; 617 for (int n = 0; n < 250; n++) 618 for (int i = 0; i < 2; i++) 619 stimulus(p->e, p->cap, p->ticks[p->base + i], 0, (uint64_t)(n + 1)); 620 return NULL; 621 } 622 623 static void t_concurrent_publication(void) { 624 begin("concurrent_publication_no_torn_no_lost", "I6,I7"); 625 Env e; 626 CHECK(env_start(&e, 8) == 0, "setup"); 627 RxObjRef pair = mkobj(&e, RES_PAIR, 0); 628 RxObjRef check = mkobj(&e, RES_CHECK, 0); 629 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_TICK, RX_RIGHT_WRITE); 630 RxCapRef rt = mint(&e, SUBJ_OMEGA, RES_TICK, RX_RIGHT_READ); 631 RxCapRef rwp = mint(&e, SUBJ_OMEGA, RES_PAIR, RX_RIGHT_READ | RX_RIGHT_WRITE); 632 RxCapRef rwc = mint(&e, SUBJ_AEGIS, RES_CHECK, RX_RIGHT_READ | RX_RIGHT_WRITE); 633 RxCapRef rp = mint(&e, SUBJ_AEGIS, RES_PAIR, RX_RIGHT_READ); 634 static IncArg inc[8]; 635 static ChkArg chk; 636 uint32_t ids[8], cid; 637 PubArg pa[4]; 638 RxObjRef ticks[8]; 639 for (int i = 0; i < 8; i++) { 640 ticks[i] = mkobj(&e, RES_TICK, 0); 641 inc[i] = (IncArg){ pair, ticks[i] }; 642 RxReactionDesc d; 643 desc_init(&d, "pair.increment", RX_FACULTY_OMEGA, SUBJ_OMEGA, fn_inc, &inc[i]); 644 add_trigger(&d, ticks[i], RX_FIELD(0)); 645 d.reads[d.n_reads++] = (RxDep){ pair, RX_FIELD(0) | RX_FIELD(1) }; 646 add_write(&d, pair, RX_FIELD(0) | RX_FIELD(1)); 647 add_cap(&d, rt, RES_TICK, RX_RIGHT_READ); 648 add_cap(&d, rwp, RES_PAIR, RX_RIGHT_READ | RX_RIGHT_WRITE); 649 CHECK(rx_world_add_reaction(&e.w, &d, &ids[i]) == RX_OK, "add inc"); 650 } 651 chk = (ChkArg){ pair, check }; 652 RxReactionDesc d; 653 desc_init(&d, "aegis.pair_invariant", RX_FACULTY_AEGIS, SUBJ_AEGIS, fn_chk, &chk); 654 add_trigger(&d, pair, RX_FIELD(0) | RX_FIELD(1)); 655 d.reads[d.n_reads++] = (RxDep){ check, RX_FIELD(0) }; 656 add_write(&d, check, RX_FIELD(0)); 657 add_cap(&d, rp, RES_PAIR, RX_RIGHT_READ); 658 add_cap(&d, rwc, RES_CHECK, RX_RIGHT_READ | RX_RIGHT_WRITE); 659 CHECK(rx_world_add_reaction(&e.w, &d, &cid) == RX_OK, "add checker"); 660 661 pthread_t th[4]; 662 for (int i = 0; i < 4; i++) { 663 pa[i].e = &e; 664 pa[i].cap = ext; 665 memcpy(pa[i].ticks, ticks, sizeof(ticks)); 666 pa[i].base = i * 2; 667 pthread_create(&th[i], NULL, publisher, &pa[i]); 668 } 669 for (int i = 0; i < 4; i++) pthread_join(th[i], NULL); 670 rx_world_wait_quiescent(&e.w, 20000); 671 uint64_t commits = 0; 672 for (int i = 0; i < 8; i++) commits += e.w.reactions[ids[i]].commits; 673 uint64_t a = field(&e, pair, 0), b = field(&e, pair, 1); 674 CHECK(a == b, "torn pair a=%llu b=%llu", (unsigned long long)a, (unsigned long long)b); 675 CHECK(a == commits, "lost update: a=%llu commits=%llu", (unsigned long long)a, 676 (unsigned long long)commits); 677 CHECK(field(&e, check, 0) == 0, "invariant checker observed %llu torn states", 678 (unsigned long long)field(&e, check, 0)); 679 printf(" concurrent: 2000 ticks -> %llu increments committed, %llu invalidations, a=b=%llu\n", 680 (unsigned long long)commits, (unsigned long long)e.w.stats.invalidations, 681 (unsigned long long)a); 682 audit_and_close(&e); 683 } 684 685 typedef struct { RxObjRef x, target; } StaleArg; 686 687 static int fn_write_target(RxCtx *c) { 688 StaleArg *a = c->user; 689 c->out[c->n_out++] = (RxMutation){ a->target, 0, in_of(c, a->x)->field[0] }; 690 return 0; 691 } 692 693 static int fn_read_target(RxCtx *c) { (void)c; return 0; } 694 695 static void t_stale_input(void) { 696 begin("stale_generation_rejected", "I2,I7"); 697 Env e; 698 CHECK(env_start(&e, 2) == 0, "setup"); 699 static StaleArg a; 700 a.x = mkobj(&e, RES_SENSOR, 0); 701 a.target = mkobj(&e, RES_SCRATCH, 7); 702 RxObjRef reader_dep = a.target; 703 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 704 RxCapRef ext_s = mint(&e, SUBJ_EXTERNAL, RES_SCRATCH, RX_RIGHT_WRITE); 705 RxCapRef rs = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 706 RxCapRef ws = mint(&e, SUBJ_AIEN, RES_SCRATCH, RX_RIGHT_READ | RX_RIGHT_WRITE); 707 RxReactionDesc d; 708 desc_init(&d, "stale.writer", RX_FACULTY_AIEN, SUBJ_AIEN, fn_write_target, &a); 709 add_trigger(&d, a.x, RX_FIELD(0)); 710 add_write(&d, a.target, RX_FIELD(0)); 711 add_cap(&d, rs, RES_SENSOR, RX_RIGHT_READ); 712 add_cap(&d, ws, RES_SCRATCH, RX_RIGHT_READ | RX_RIGHT_WRITE); 713 uint32_t wid, rid; 714 CHECK(rx_world_add_reaction(&e.w, &d, &wid) == RX_OK, "add writer"); 715 desc_init(&d, "stale.reader", RX_FACULTY_AIEN, SUBJ_AIEN, fn_read_target, NULL); 716 add_trigger(&d, a.x, RX_FIELD(0)); 717 d.reads[d.n_reads++] = (RxDep){ reader_dep, RX_FIELD(0) }; 718 add_cap(&d, rs, RES_SENSOR, RX_RIGHT_READ); 719 add_cap(&d, ws, RES_SCRATCH, RX_RIGHT_READ | RX_RIGHT_WRITE); 720 CHECK(rx_world_add_reaction(&e.w, &d, &rid) == RX_OK, "add reader"); 721 722 /* Retire the target and reuse its slot: same id, next generation. */ 723 CHECK(rx_world_retire(&e.w, a.target) == RX_OK, "retire"); 724 RxObjRef reborn = mkobj(&e, RES_SCRATCH, 99); 725 CHECK(reborn.id == a.target.id && reborn.generation == a.target.generation + 1, 726 "slot reuse did not advance generation"); 727 CHECK(stimulus(&e, ext_s, a.target, 0, 5) == RX_ERR_STALE_GEN, 728 "external write through a stale reference was accepted"); 729 stimulus(&e, ext, a.x, 0, 1234); 730 rx_world_wait_quiescent(&e.w, 5000); 731 CHECK(field(&e, reborn, 0) == 99, "stale writer mutated the reborn object"); 732 CHECK(e.w.reactions[wid].commits == 0, "stale writer committed"); 733 CHECK(e.w.stats.rejected >= 1, "stale write not rejected"); 734 CHECK(e.w.stats.invalidations >= 1, "reader of retired object not invalidated"); 735 audit_and_close(&e); 736 } 737 738 typedef struct { RxObjRef x, declared, undeclared; int mode; } WsArg; 739 740 static int fn_outside(RxCtx *c) { 741 WsArg *a = c->user; 742 if (a->mode == 0) /* an object it holds WRITE authority for, but never declared */ 743 c->out[c->n_out++] = (RxMutation){ a->undeclared, 0, 555 }; 744 else /* the declared object, but a field outside its write mask */ 745 c->out[c->n_out++] = (RxMutation){ a->declared, 3, 555 }; 746 c->out[c->n_out++] = (RxMutation){ a->declared, 0, 1 }; 747 return 0; 748 } 749 750 static void t_write_set_enforced(void) { 751 begin("write_outside_declared_set_rejected", "I6,I9"); 752 Env e; 753 CHECK(env_start(&e, 2) == 0, "setup"); 754 static WsArg a[2]; 755 RxObjRef x = mkobj(&e, RES_SENSOR, 0); 756 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 757 RxCapRef rs = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 758 RxCapRef wb = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE); 759 uint32_t ids[2]; 760 for (int m = 0; m < 2; m++) { 761 a[m] = (WsArg){ x, mkobj(&e, RES_BELIEF, 0), mkobj(&e, RES_BELIEF, 0), m }; 762 RxReactionDesc d; 763 desc_init(&d, m ? "outside.field_mask" : "outside.object", RX_FACULTY_AIEN, SUBJ_AIEN, 764 fn_outside, &a[m]); 765 add_trigger(&d, x, RX_FIELD(0)); 766 add_write(&d, a[m].declared, RX_FIELD(0)); 767 add_cap(&d, rs, RES_SENSOR, RX_RIGHT_READ); 768 add_cap(&d, wb, RES_BELIEF, RX_RIGHT_WRITE); 769 CHECK(rx_world_add_reaction(&e.w, &d, &ids[m]) == RX_OK, "add"); 770 } 771 stimulus(&e, ext, x, 0, 1); 772 rx_world_wait_quiescent(&e.w, 5000); 773 for (int m = 0; m < 2; m++) { 774 RxObject o; 775 rx_world_read(&e.w, a[m].declared, &o); 776 CHECK(o.field[0] == 0 && o.field[3] == 0, "mode %d: partial publication leaked", m); 777 CHECK(field(&e, a[m].undeclared, 0) == 0, "mode %d: undeclared object written", m); 778 CHECK(e.w.reactions[ids[m]].commits == 0, "mode %d: committed", m); 779 } 780 CHECK(e.w.stats.rejected == 2, "rejected=%llu", (unsigned long long)e.w.stats.rejected); 781 audit_and_close(&e); 782 } 783 784 typedef struct { RxObjRef d, out; } DupArg; 785 786 static int fn_dup(RxCtx *c) { 787 DupArg *a = c->user; 788 uint64_t v = in_of(c, a->d)->field[0]; 789 sleep_ms(2); 790 c->out[c->n_out++] = (RxMutation){ a->out, 0, v }; 791 return 0; 792 } 793 794 static void t_duplicate_wake(void) { 795 begin("duplicate_wake_coalesced", "I7,I13"); 796 Env e; 797 CHECK(env_start(&e, 4) == 0, "setup"); 798 static DupArg a; 799 a.d = mkobj(&e, RES_SENSOR, 0); 800 a.out = mkobj(&e, RES_BELIEF, 0); 801 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 802 RxReactionDesc d; 803 desc_init(&d, "dup.derive", RX_FACULTY_AIEN, SUBJ_AIEN, fn_dup, &a); 804 add_trigger(&d, a.d, RX_FIELD(0)); 805 add_write(&d, a.out, RX_FIELD(0)); 806 add_cap(&d, mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ), RES_SENSOR, RX_RIGHT_READ); 807 add_cap(&d, mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE), RES_BELIEF, RX_RIGHT_WRITE); 808 uint32_t id; 809 CHECK(rx_world_add_reaction(&e.w, &d, &id) == RX_OK, "add"); 810 for (uint64_t v = 1; v <= 1000; v++) stimulus(&e, ext, a.d, 0, v); 811 rx_world_wait_quiescent(&e.w, 10000); 812 uint64_t acts = e.w.reactions[id].activations; 813 CHECK(field(&e, a.out, 0) == 1000, "final output %llu != last input", 814 (unsigned long long)field(&e, a.out, 0)); 815 CHECK(acts < 1000, "no coalescing: %llu activations for 1000 wakes", (unsigned long long)acts); 816 CHECK(e.w.stats.coalesced_wakes > 0, "coalesced counter is zero"); 817 printf(" duplicate wake: 1000 wakes -> %llu activations, %llu commits\n", 818 (unsigned long long)acts, (unsigned long long)e.w.reactions[id].commits); 819 audit_and_close(&e); 820 } 821 822 /* ---- authority ------------------------------------------------------------ */ 823 824 static int fn_forbidden(RxCtx *c) { 825 HB *h = c->user; 826 c->out[c->n_out++] = (RxMutation){ h->belief, 0, 666 }; 827 return 0; 828 } 829 830 static uint64_t blocked_with(Env *e, RxObjRef sensor, RxObjRef belief, RxCapRef ext, 831 uint32_t subject, RxCapRef rcap, RxCapRef wcap, uint64_t trigger_v, 832 int *out_reason) { 833 static HB h; 834 h.sensor = sensor; 835 h.belief = belief; 836 RxReactionDesc d; 837 desc_init(&d, "attack.writer", RX_FACULTY_AIEN, subject, fn_forbidden, &h); 838 add_trigger(&d, sensor, RX_FIELD(F_TEMP)); 839 add_write(&d, belief, RX_FIELD(0)); 840 add_cap(&d, rcap, RES_SENSOR, RX_RIGHT_READ); 841 add_cap(&d, wcap, RES_BELIEF, RX_RIGHT_WRITE); 842 uint32_t id; 843 if (rx_world_add_reaction(&e->w, &d, &id) != RX_OK) return UINT64_MAX; 844 uint64_t first = e->w.n_crumbs + 1; 845 stimulus(e, ext, sensor, F_TEMP, trigger_v); 846 rx_world_wait_quiescent(&e->w, 5000); 847 *out_reason = 0; 848 uint64_t blocked = 0; 849 for (uint64_t i = first; i <= e->w.n_crumbs; i++) { 850 const RxCrumb *k = rx_world_crumb(&e->w, i); 851 if (k->reaction != id) continue; 852 if (k->kind == RX_CRUMB_BLOCKED_AUTHORITY) { *out_reason = k->reason; blocked++; } 853 else blocked += 1000; /* any other outcome is a failure */ 854 } 855 return blocked; 856 } 857 858 static void t_capability_attacks(void) { 859 begin("capability_attacks_all_fail", "I3,I4,I9"); 860 Env e; 861 CHECK(env_start(&e, 2) == 0, "setup"); 862 RxObjRef sensor = mkobj(&e, RES_SENSOR, 0); 863 RxObjRef belief = mkobj(&e, RES_BELIEF, 0); 864 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 865 RxCapRef rs = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 866 RxCapRef wb = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE); 867 int reason = 0; 868 uint64_t v = 1; 869 870 struct { const char *name; RxCapRef w; uint32_t subject; int expect; } cases[8]; 871 int nc = 0; 872 cases[nc++] = (typeof(cases[0])){ "forged_reference", (RxCapRef){ 200, 7 }, SUBJ_AIEN, RX_CAP_ERR_STALE_GEN }; 873 cases[nc++] = (typeof(cases[0])){ "wrong_subject", wb, SUBJ_OTHER, RX_CAP_ERR_SUBJECT }; 874 cases[nc++] = (typeof(cases[0])){ "wrong_resource", mint(&e, SUBJ_AIEN, RES_PLAN, RX_RIGHT_WRITE), SUBJ_AIEN, RX_CAP_ERR_RESOURCE }; 875 cases[nc++] = (typeof(cases[0])){ "insufficient_rights", mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_READ), SUBJ_AIEN, RX_CAP_ERR_RIGHTS }; 876 RxCapRef rev = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE); 877 revoke_cap(&e.admin, rev); 878 cases[nc++] = (typeof(cases[0])){ "revoked", rev, SUBJ_AIEN, RX_CAP_ERR_REVOKED }; 879 /* Generation replay: revoke, reclaim, the slot is re-minted to someone else. */ 880 RxCapRef old = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE); 881 revoke_cap(&e.admin, old); 882 rx_capadmin_reclaim(&e.admin, office_of(&e.admin), old.cap_id); 883 RxCapRef reused = mint(&e, SUBJ_OTHER, RES_BELIEF, RX_RIGHT_WRITE); 884 CHECK(reused.cap_id == old.cap_id && reused.generation == old.generation + 1, 885 "reclaimed slot was not reused with a new generation"); 886 cases[nc++] = (typeof(cases[0])){ "generation_replay", old, SUBJ_AIEN, RX_CAP_ERR_STALE_GEN }; 887 RxCapMint lm; 888 memset(&lm, 0, sizeof(lm)); 889 lm.issuer = ISSUER_AEGIS_POLICY; 890 lm.subject = SUBJ_AIEN; 891 lm.resource = RES_BELIEF; 892 lm.rights = RX_RIGHT_WRITE; 893 lm.lease_ticks = 5; 894 lm.parent = (RxCapRef){ UINT32_MAX, 0 }; 895 lm.authority = office_of(&e.admin); 896 RxCapRef leased; 897 rx_capadmin_mint(&e.admin, &lm, &leased); 898 rx_capadmin_advance_clock(&e.admin, office_of(&e.admin), 10); 899 cases[nc++] = (typeof(cases[0])){ "expired_lease", leased, SUBJ_AIEN, RX_CAP_ERR_EXPIRED }; 900 901 for (int i = 0; i < nc; i++) { 902 uint64_t n = blocked_with(&e, sensor, belief, ext, cases[i].subject, 903 cases[i].subject == SUBJ_AIEN ? rs : mint(&e, cases[i].subject, RES_SENSOR, RX_RIGHT_READ), 904 cases[i].w, v++, &reason); 905 CHECK(n == 1 && reason == cases[i].expect, "%s: blocked=%llu reason=%s (want %s)", 906 cases[i].name, (unsigned long long)n, rx_cap_strerror(reason), 907 rx_cap_strerror(cases[i].expect)); 908 CHECK(field(&e, belief, 0) == 0, "%s: forbidden write reached the world", cases[i].name); 909 } 910 911 /* Delegation may only attenuate. */ 912 RxCapRef parent = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_READ | RX_RIGHT_DELEGATE); 913 RxCapMint amp; 914 memset(&, 0, sizeof(amp)); 915 amp.issuer = SUBJ_AIEN; 916 amp.subject = SUBJ_OMEGA; 917 amp.resource = RES_BELIEF; 918 amp.rights = RX_RIGHT_READ | RX_RIGHT_WRITE; 919 amp.parent = parent; 920 amp.authority = parent; 921 CHECK(rx_capadmin_mint(&e.admin, &, NULL) == RX_CAP_ERR_AMPLIFY, "rights amplification accepted"); 922 RxCapMint other = amp; 923 other.resource = RES_PLAN; 924 other.rights = RX_RIGHT_READ; 925 CHECK(rx_capadmin_mint(&e.admin, &other, NULL) == RX_CAP_ERR_RESOURCE, "resource widening accepted"); 926 RxCapMint nd = amp; 927 nd.resource = RES_SENSOR; 928 nd.rights = RX_RIGHT_READ; 929 nd.parent = rs; 930 nd.authority = rs; 931 CHECK(rx_capadmin_mint(&e.admin, &nd, NULL) == RX_CAP_ERR_NOT_DELEGABLE, "non-delegable parent delegated"); 932 RxCapMint ok = amp; 933 ok.rights = RX_RIGHT_READ; 934 RxCapRef child; 935 CHECK(rx_capadmin_mint(&e.admin, &ok, &child) == RX_CAP_OK, "valid attenuation refused"); 936 CHECK(rx_caproot_validate(&e.root, child, SUBJ_OMEGA, RES_BELIEF, RX_RIGHT_READ, NULL) == RX_CAP_OK, 937 "delegated child invalid"); 938 revoke_cap(&e.admin, parent); 939 int child_after = rx_caproot_validate(&e.root, child, SUBJ_OMEGA, RES_BELIEF, RX_RIGHT_READ, NULL); 940 CHECK(child_after == RX_CAP_ERR_REVOKED || child_after == RX_CAP_ERR_CHAIN, 941 "child survived parent revocation (%s)", rx_cap_strerror(child_after)); 942 943 /* External stimulus with a capability for the wrong subject. */ 944 CHECK(stimulus(&e, rs, sensor, F_TEMP, 77) == RX_ERR_AUTHORITY, "external write with wrong cap accepted"); 945 946 /* Epoch change invalidates everything minted before it. */ 947 rx_capadmin_bump_epoch(&e.admin, office_of(&e.admin)); 948 CHECK(rx_caproot_validate(&e.root, rs, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ, NULL) == RX_CAP_ERR_EPOCH, 949 "pre-epoch capability still valid"); 950 audit_and_close(&e); 951 } 952 953 typedef struct { RxObjRef x, y; } RevArg; 954 955 static int fn_rev(RxCtx *c) { 956 RevArg *a = c->user; 957 sleep_ms(80); 958 c->out[c->n_out++] = (RxMutation){ a->y, 0, in_of(c, a->x)->field[0] + 1 }; 959 return 0; 960 } 961 962 static void t_revoke_during_run(void) { 963 begin("revocation_during_run_rejected", "I4,I7"); 964 Env e; 965 CHECK(env_start(&e, 2) == 0, "setup"); 966 static RevArg a; 967 a.x = mkobj(&e, RES_SENSOR, 0); 968 a.y = mkobj(&e, RES_BELIEF, 0); 969 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 970 RxCapRef w = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE); 971 RxReactionDesc d; 972 desc_init(&d, "revoked.midflight", RX_FACULTY_AIEN, SUBJ_AIEN, fn_rev, &a); 973 add_trigger(&d, a.x, RX_FIELD(0)); 974 add_write(&d, a.y, RX_FIELD(0)); 975 add_cap(&d, mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ), RES_SENSOR, RX_RIGHT_READ); 976 add_cap(&d, w, RES_BELIEF, RX_RIGHT_WRITE); 977 uint32_t id; 978 CHECK(rx_world_add_reaction(&e.w, &d, &id) == RX_OK, "add"); 979 stimulus(&e, ext, a.x, 0, 41); 980 sleep_ms(20); 981 CHECK(revoke_cap(&e.admin, w) == RX_CAP_OK, "revoke"); 982 rx_world_wait_quiescent(&e.w, 5000); 983 CHECK(field(&e, a.y, 0) == 0, "revoked authority executed"); 984 CHECK(e.w.stats.rejected == 1, "publication not rejected"); 985 bool saw = false; 986 for (uint64_t i = 1; i <= e.w.n_crumbs; i++) { 987 const RxCrumb *k = rx_world_crumb(&e.w, i); 988 if (k->kind == RX_CRUMB_REJECTED && k->reason == RX_CAP_ERR_REVOKED) saw = true; 989 } 990 CHECK(saw, "REJECTED(revoked) crumb missing"); 991 audit_and_close(&e); 992 } 993 994 static void t_root_table_unwritable(void) { 995 begin("capability_table_unforgeable_by_runtime", "I3"); 996 RxCapRoot root; 997 RxCapAdmin admin; 998 CHECK(rx_caproot_start(&root, &admin) == RX_CAP_OK, "root"); 999 void *page = (void *)root.table; 1000 errno = 0; 1001 CHECK(mprotect(page, sizeof(RxCapTable), PROT_READ | PROT_WRITE) != 0, 1002 "runtime made the capability table writable"); 1003 void *m = mmap(NULL, sizeof(RxCapTable), PROT_READ | PROT_WRITE, MAP_SHARED, root.ro_fd, 0); 1004 CHECK(m == MAP_FAILED, "runtime obtained a writable mapping"); 1005 if (m != MAP_FAILED) munmap(m, sizeof(RxCapTable)); 1006 uint32_t junk = 0xffffffffu; 1007 CHECK(pwrite(root.ro_fd, &junk, sizeof(junk), offsetof(RxCapTable, entries)) < 0, 1008 "runtime wrote the capability table through its fd"); 1009 CHECK(ftruncate(root.ro_fd, 0) != 0, "runtime resized the capability table"); 1010 /* Reopen through /proc: the seal is on the inode, so every path is sealed. */ 1011 char path[64]; 1012 snprintf(path, sizeof(path), "/proc/self/fd/%d", root.ro_fd); 1013 int fd2 = open(path, O_RDWR); 1014 if (fd2 >= 0) { 1015 CHECK(pwrite(fd2, &junk, sizeof(junk), offsetof(RxCapTable, entries)) < 0, 1016 "reopened fd could write the table"); 1017 close(fd2); 1018 } 1019 /* The root process is non-dumpable: its writable mapping is out of reach. */ 1020 snprintf(path, sizeof(path), "/proc/%d/mem", (int)root.root_pid); 1021 int memfd = open(path, O_RDWR); 1022 CHECK(memfd < 0, "runtime opened the root's memory"); 1023 if (memfd >= 0) close(memfd); 1024 /* A direct store faults. Prove it in a throwaway child. */ 1025 pid_t pid = fork(); 1026 if (pid == 0) { 1027 volatile uint32_t *rights = (volatile uint32_t *)&((RxCapTable *)page)->entries[0].rights; 1028 *rights = 0xffffffffu; 1029 _exit(0); 1030 } 1031 int st = 0; 1032 waitpid(pid, &st, 0); 1033 CHECK(WIFSIGNALED(st) && (WTERMSIG(st) == SIGSEGV || WTERMSIG(st) == SIGBUS), 1034 "direct store into the capability table did not fault"); 1035 rx_caproot_stop(&root, &admin); 1036 } 1037 1038 static void t_crumb_tamper_detected(void) { 1039 begin("causal_record_tamper_detected", "I7,I15"); 1040 HBEnv x; 1041 CHECK(hb_setup(&x, 2) == 0, "setup"); 1042 Env *e = &x.e; 1043 stimulus(e, x.c_ext, x.h.sensor, F_TEMP, 3); 1044 rx_world_wait_quiescent(&e->w, 5000); 1045 uint64_t id = rx_world_explain(&e->w, x.h.belief, 0); 1046 RxCrumb *k = &e->w.crumbs[id - 1]; 1047 k->caps[0].cap_id ^= 1; /* rewrite "which authority allowed it" */ 1048 CHECK(rx_world_verify_crumbs(&e->w, NULL) != 0, "rewritten authority went undetected"); 1049 k->caps[0].cap_id ^= 1; 1050 CHECK(rx_world_verify_crumbs(&e->w, NULL) == 0, "restored record fails verification"); 1051 k->cap_issuer[0] ^= 1; /* rewrite who issued that authority */ 1052 CHECK(rx_world_verify_crumbs(&e->w, NULL) != 0, "rewritten issuer went undetected"); 1053 k->cap_issuer[0] ^= 1; 1054 CHECK(rx_world_verify_crumbs(&e->w, NULL) == 0, "restored issuer fails verification"); 1055 k->caps[0].generation ^= 1ull << 32; /* rewrite only the high half of the generation */ 1056 CHECK(rx_world_verify_crumbs(&e->w, NULL) != 0, "rewritten generation high half went undetected"); 1057 k->caps[0].generation ^= 1ull << 32; 1058 CHECK(rx_world_verify_crumbs(&e->w, NULL) == 0, "restored generation fails verification"); 1059 audit_and_close(e); 1060 } 1061 1062 /* Hostile: two crumbs identical except for the capability generation must not 1063 * share a digest, even when the generations agree in their low 32 bits. */ 1064 static void t_crumb_cap_generation_64(void) { 1065 begin("causal_record_binds_64bit_cap_generation", "I15"); 1066 HBEnv x; 1067 CHECK(hb_setup(&x, 1) == 0, "setup"); 1068 Env *e = &x.e; 1069 static const uint64_t pairs[][2] = { 1070 { 0x5ull, 0x100000005ull }, /* high half only */ 1071 { (uint64_t)UINT32_MAX, (uint64_t)UINT32_MAX + 1u }, /* the 32-bit boundary */ 1072 { (uint64_t)UINT32_MAX, 0x1ffffffffull }, /* boundary, same low half */ 1073 { 0x0ull, 0x8000000000000000ull }, /* top bit only */ 1074 }; 1075 for (size_t i = 0; i < sizeof pairs / sizeof pairs[0]; i++) { 1076 RxCrumb a, b; 1077 memset(&a, 0, sizeof a); 1078 a.id = 1; 1079 a.kind = RX_CRUMB_COMMIT; 1080 a.n_caps = 1; 1081 a.caps[0] = (RxCapRef){ 7, pairs[i][0] }; 1082 a.cap_issuer[0] = 3; 1083 b = a; 1084 b.caps[0].generation = pairs[i][1]; 1085 uint8_t da[32], db[32], da2[32]; 1086 rx_world_crumb_digest(&e->w, &a, da); 1087 rx_world_crumb_digest(&e->w, &b, db); 1088 rx_world_crumb_digest(&e->w, &a, da2); 1089 CHECK(memcmp(da, da2, 32) == 0, "crumb digest is not deterministic"); 1090 CHECK(memcmp(da, db, 32) != 0, "crumbs differing only in cap generation share a digest"); 1091 } 1092 audit_and_close(e); 1093 } 1094 1095 /* No orchestrator, and the order does not matter: for a pure DAG the committed 1096 * world is identical whatever the worker count or thread interleaving. */ 1097 static void t_schedule_independence(void) { 1098 begin("schedule_independent_result", "I16"); 1099 uint8_t ref[32]; 1100 int differ = 0, runs = 0; 1101 for (uint32_t workers = 1; workers <= 8; workers++) 1102 for (int rep = 0; rep < 6; rep++) { 1103 HBEnv x; 1104 if (hb_setup(&x, workers) != 0) { differ++; continue; } 1105 Env *e = &x.e; 1106 for (uint64_t v = 1; v <= 20; v++) { 1107 stimulus(e, x.c_ext, x.h.sensor, F_TEMP, v * 3); 1108 stimulus(e, x.c_ext, x.h.sensor, F_HUMIDITY, v); 1109 } 1110 rx_world_wait_quiescent(&e->w, 5000); 1111 uint8_t dg[32]; 1112 rx_world_digest(&e->w, dg); 1113 if (runs == 0) memcpy(ref, dg, 32); 1114 else if (memcmp(ref, dg, 32) != 0) differ++; 1115 runs++; 1116 audit_and_close(e); 1117 } 1118 CHECK(differ == 0, "%d of %d runs committed a different world", differ, runs); 1119 printf(" %d runs across 1..8 workers, identical committed world\n", runs); 1120 } 1121 1122 /* ---- authority hardening (host). Not R7. -------------------------------- */ 1123 1124 static RxCapMint delegated(RxCapRef parent, uint32_t subject, uint64_t resource, uint32_t rights) { 1125 RxCapMint m; 1126 memset(&m, 0, sizeof(m)); 1127 m.issuer = ISSUER_AEGIS_POLICY; 1128 m.subject = subject; 1129 m.resource = resource; 1130 m.rights = rights; 1131 m.parent = parent; 1132 m.authority = parent; 1133 return m; 1134 } 1135 1136 static void t_authority_office(void) { 1137 begin("authority_office_required", "I3"); 1138 RxCapRoot root; 1139 RxCapAdmin admin; 1140 CHECK(rx_caproot_start(&root, &admin) == RX_CAP_OK, "root"); 1141 RxCapRef office = office_of(&admin); 1142 CHECK(office.cap_id != UINT32_MAX, "office was not delivered"); 1143 1144 RxCapMint bare; 1145 memset(&bare, 0, sizeof(bare)); 1146 bare.issuer = ISSUER_AEGIS_POLICY; 1147 bare.subject = SUBJ_AIEN; 1148 bare.resource = RES_SENSOR; 1149 bare.rights = RX_RIGHT_READ; 1150 bare.parent = (RxCapRef){ UINT32_MAX, 0 }; 1151 bare.authority = (RxCapRef){ UINT32_MAX, 0 }; 1152 CHECK(rx_capadmin_mint(&admin, &bare, NULL) == RX_CAP_ERR_UNAUTHORIZED, 1153 "socket holder minted without a delivered authority"); 1154 1155 RxCapMint ok = bare; 1156 ok.authority = office; 1157 RxCapRef world; 1158 CHECK(rx_capadmin_mint(&admin, &ok, &world) == RX_CAP_OK, "office mint refused"); 1159 CHECK(rx_capadmin_revoke(&admin, world, world) == RX_CAP_ERR_UNAUTHORIZED, 1160 "ordinary capability revoked"); 1161 CHECK(rx_capadmin_advance_clock(&admin, world, 1) == RX_CAP_ERR_UNAUTHORIZED, 1162 "ordinary capability advanced the clock"); 1163 CHECK(rx_capadmin_bump_epoch(&admin, world) == RX_CAP_ERR_UNAUTHORIZED, 1164 "ordinary capability changed the epoch"); 1165 CHECK(rx_capadmin_reclaim(&admin, world, world.cap_id) == RX_CAP_ERR_UNAUTHORIZED, 1166 "ordinary capability reclaimed a slot"); 1167 1168 RxCapMint both = ok; 1169 both.rights = RX_RIGHT_REVOKE | RX_RIGHT_DELEGATE; 1170 CHECK(rx_capadmin_mint(&admin, &both, NULL) == RX_CAP_ERR_NOT_DELEGABLE, 1171 "privileged right combined with delegate"); 1172 RxCapMint revoker = ok; 1173 revoker.rights = RX_RIGHT_REVOKE; 1174 revoker.resource = RX_CAP_RES_AUTHORITY; 1175 RxCapRef revcap; 1176 CHECK(rx_capadmin_mint(&admin, &revoker, &revcap) == RX_CAP_OK, "limited revoker refused"); 1177 RxCapMint pass = delegated(revcap, SUBJ_OMEGA, RX_CAP_RES_AUTHORITY, RX_RIGHT_REVOKE); 1178 CHECK(rx_capadmin_mint(&admin, &pass, NULL) == RX_CAP_ERR_NOT_DELEGABLE, 1179 "privileged right was delegated"); 1180 CHECK(rx_capadmin_revoke(&admin, revcap, world) == RX_CAP_OK, "limited revoker could not revoke"); 1181 1182 /* Office MINT must not attenuate an unrelated capability. */ 1183 RxCapRef parent = office; 1184 (void)parent; 1185 RxCapMint sneak = bare; 1186 sneak.authority = office; 1187 sneak.parent = world; /* revoked, and not the office */ 1188 sneak.rights = RX_RIGHT_READ; 1189 CHECK(rx_capadmin_mint(&admin, &sneak, NULL) != RX_CAP_OK, 1190 "office minted a delegation of a capability it does not hold"); 1191 1192 RxCapRef cur; 1193 RxCapMint base = ok; 1194 base.rights = RX_RIGHT_READ | RX_RIGHT_DELEGATE; 1195 base.resource = RES_BELIEF; 1196 CHECK(rx_capadmin_mint(&admin, &base, &cur) == RX_CAP_OK, "delegable parent refused"); 1197 int depth_ok = 0; 1198 for (int i = 0; i < 8; i++) { 1199 RxCapMint step = delegated(cur, SUBJ_OMEGA, RES_BELIEF, RX_RIGHT_READ | RX_RIGHT_DELEGATE); 1200 RxCapRef child; 1201 if (rx_capadmin_mint(&admin, &step, &child) != RX_CAP_OK) break; 1202 cur = child; 1203 depth_ok++; 1204 } 1205 CHECK(depth_ok == 8, "delegation depth stopped early (%d)", depth_ok); 1206 RxCapMint too_deep = delegated(cur, SUBJ_OMEGA, RES_BELIEF, RX_RIGHT_READ | RX_RIGHT_DELEGATE); 1207 CHECK(rx_capadmin_mint(&admin, &too_deep, NULL) == RX_CAP_ERR_CHAIN, 1208 "delegation deeper than the limit was accepted"); 1209 1210 /* A narrower revoker cannot strike the office, and revocation cascades. */ 1211 RxCapMint chain = ok; 1212 chain.rights = RX_RIGHT_READ | RX_RIGHT_DELEGATE; 1213 chain.resource = RES_SENSOR; 1214 RxCapRef link; 1215 CHECK(rx_capadmin_mint(&admin, &chain, &link) == RX_CAP_OK, "chain parent"); 1216 RxCapMint step1 = delegated(link, SUBJ_OMEGA, RES_SENSOR, RX_RIGHT_READ | RX_RIGHT_DELEGATE); 1217 RxCapRef mid; 1218 CHECK(rx_capadmin_mint(&admin, &step1, &mid) == RX_CAP_OK, "chain child"); 1219 RxCapMint step2 = delegated(mid, SUBJ_OMEGA, RES_SENSOR, RX_RIGHT_READ); 1220 RxCapRef grand; 1221 CHECK(rx_capadmin_mint(&admin, &step2, &grand) == RX_CAP_OK, "chain grandchild"); 1222 CHECK(rx_capadmin_revoke(&admin, revcap, office) == RX_CAP_ERR_UNAUTHORIZED, 1223 "narrow revoker struck the office"); 1224 CHECK(rx_caproot_validate(&root, office, 0, RX_CAP_RES_AUTHORITY, RX_RIGHT_MINT, NULL) == RX_CAP_OK, 1225 "office died after a refused revoke"); 1226 CHECK(rx_capadmin_revoke(&admin, office, link) == RX_CAP_OK, "parent revoke"); 1227 RxCapEntry seen; 1228 CHECK(rx_caproot_inspect(&root, grand, &seen) == RX_CAP_OK, "grandchild disappeared"); 1229 CHECK(seen.state == RX_CAP_REVOKED, "grandchild stayed live after its ancestor was revoked"); 1230 CHECK(rx_caproot_validate(&root, grand, SUBJ_OMEGA, RES_SENSOR, RX_RIGHT_READ, NULL) != RX_CAP_OK, 1231 "grandchild of a revoked capability still validated"); 1232 CHECK(seen.minted_by_id == mid.cap_id, "grandchild did not record who minted it"); 1233 1234 /* The office id is visible in the readable table. Without the token the 1235 * socket must still refuse. */ 1236 RxCapRequest forged; 1237 memset(&forged, 0, sizeof(forged)); 1238 forged.op = RX_OP_MINT; 1239 forged.mint = bare; 1240 forged.mint.authority = office; 1241 forged.authority = office; 1242 CHECK(write(admin.ctl_fd, &forged, sizeof(forged)) == (ssize_t)sizeof(forged), 1243 "could not send a forged mint"); 1244 struct { int32_t status; uint32_t pad; RxCapRef ref; } reply; 1245 memset(&reply, 0, sizeof(reply)); 1246 CHECK(read(admin.ctl_fd, &reply, sizeof(reply)) == (ssize_t)sizeof(reply), 1247 "mint did not answer the forged request"); 1248 CHECK(reply.status == RX_CAP_ERR_UNAUTHORIZED, 1249 "table-visible office minted without the office token (%d)", reply.status); 1250 rx_caproot_stop(&root, &admin); 1251 } 1252 1253 static void t_counter_fail_closed(void) { 1254 begin("counters_fail_closed", "I3"); 1255 uint64_t gen = 0; 1256 CHECK(rx_cap_generation_advance(UINT64_MAX, &gen) == RX_CAP_ERR_EXHAUSTED, 1257 "generation wrap was accepted"); 1258 CHECK(gen == 0, "exhausted generation was written"); 1259 CHECK(rx_cap_generation_advance(UINT64_MAX - 1, &gen) == RX_CAP_OK, "last generation refused"); 1260 CHECK(gen == UINT64_MAX, "last generation value"); 1261 CHECK(rx_cap_generation_advance(UINT32_MAX, &gen) == RX_CAP_OK && gen == (uint64_t)UINT32_MAX + 1, 1262 "generation stopped at 32 bits"); 1263 uint64_t sum = 1; 1264 CHECK(rx_cap_add_u64(UINT64_MAX, 1, &sum) == RX_CAP_ERR_OVERFLOW, "u64 wrap was accepted"); 1265 CHECK(sum == 1, "overflow wrote a sum"); 1266 CHECK(rx_cap_add_u64(UINT64_MAX - 5, 5, &sum) == RX_CAP_OK, "exact top of range refused"); 1267 CHECK(sum == UINT64_MAX, "exact sum"); 1268 CHECK(rx_cap_add_u64(UINT64_MAX - 5, 6, &sum) == RX_CAP_ERR_OVERFLOW, "one past the top was accepted"); 1269 1270 RxCapRoot root; 1271 RxCapAdmin admin; 1272 CHECK(rx_caproot_start(&root, &admin) == RX_CAP_OK, "root"); 1273 RxCapRef office = office_of(&admin); 1274 RxCapMint huge; 1275 memset(&huge, 0, sizeof(huge)); 1276 huge.issuer = ISSUER_AEGIS_POLICY; 1277 huge.subject = SUBJ_AIEN; 1278 huge.resource = RES_SCRATCH; 1279 huge.rights = RX_RIGHT_READ; 1280 huge.parent = (RxCapRef){ UINT32_MAX, 0 }; 1281 huge.authority = office; 1282 int filled = 0; 1283 for (uint32_t i = 0; i < RX_CAP_MAX + 2; i++) { 1284 int rc = rx_capadmin_mint(&admin, &huge, NULL); 1285 if (rc == RX_CAP_OK) filled++; 1286 else { 1287 CHECK(rc == RX_CAP_ERR_FULL, "table exhaustion returned %s", rx_cap_strerror(rc)); 1288 break; 1289 } 1290 } 1291 CHECK(filled == (int)RX_CAP_MAX - 1, "filled %d slots, want %u", filled, RX_CAP_MAX - 1); 1292 rx_caproot_stop(&root, &admin); 1293 1294 CHECK(rx_caproot_start(&root, &admin) == RX_CAP_OK, "clock root"); 1295 office = office_of(&admin); 1296 huge.authority = office; 1297 CHECK(rx_capadmin_advance_clock(&admin, office, UINT64_MAX - 8) == RX_CAP_OK, "clock near the top"); 1298 CHECK(rx_capadmin_advance_clock(&admin, office, 100) == RX_CAP_ERR_OVERFLOW, "clock wrap was accepted"); 1299 huge.lease_ticks = 100; 1300 huge.resource = RES_SENSOR; 1301 CHECK(rx_capadmin_mint(&admin, &huge, NULL) == RX_CAP_ERR_OVERFLOW, "lease wrap was accepted"); 1302 huge.lease_ticks = 1; 1303 RxCapRef leased; 1304 CHECK(rx_capadmin_mint(&admin, &huge, &leased) == RX_CAP_OK, "in-range lease refused"); 1305 CHECK(rx_capadmin_bump_epoch(&admin, office) == RX_CAP_OK, "epoch step refused"); 1306 CHECK(rx_caproot_validate(&root, leased, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ, NULL) == RX_CAP_ERR_EPOCH, 1307 "pre-epoch lease survived"); 1308 CHECK(rx_capadmin_bump_epoch(&admin, office) == RX_CAP_ERR_EPOCH, 1309 "stale office advanced the epoch again"); 1310 rx_caproot_stop(&root, &admin); 1311 } 1312 1313 static void t_root_death_restart(void) { 1314 begin("root_death_and_restart", "I3"); 1315 RxCapRoot root; 1316 RxCapAdmin admin; 1317 CHECK(rx_caproot_start(&root, &admin) == RX_CAP_OK, "root"); 1318 RxCapRef old_office = office_of(&admin); 1319 RxCapMint m; 1320 memset(&m, 0, sizeof(m)); 1321 m.issuer = ISSUER_AEGIS_POLICY; 1322 m.subject = SUBJ_AIEN; 1323 m.resource = RES_SENSOR; 1324 m.rights = RX_RIGHT_READ; 1325 m.parent = (RxCapRef){ UINT32_MAX, 0 }; 1326 m.authority = old_office; 1327 RxCapRef old; 1328 CHECK(rx_capadmin_mint(&admin, &m, &old) == RX_CAP_OK, "mint"); 1329 pid_t pid = root.root_pid; 1330 CHECK(kill(pid, SIGKILL) == 0, "could not stop the mint process"); 1331 int st = 0; 1332 waitpid(pid, &st, 0); 1333 alarm(3); 1334 int still = rx_caproot_validate(&root, old, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ, NULL); 1335 alarm(0); 1336 CHECK(still == RX_CAP_OK || still == RX_CAP_ERR_IO || still == RX_CAP_ERR_STALE_GEN, 1337 "dead root validate returned %s", rx_cap_strerror(still)); 1338 CHECK(rx_capadmin_mint(&admin, &m, NULL) == RX_CAP_ERR_IO, "dead root still minted"); 1339 rx_caproot_stop(&root, &admin); 1340 CHECK(rx_caproot_start(&root, &admin) == RX_CAP_OK, "restart"); 1341 CHECK(rx_caproot_validate(&root, old, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ, NULL) == RX_CAP_ERR_STALE_GEN, 1342 "capability from the dead root validated on the new one"); 1343 CHECK(rx_caproot_validate(&root, old_office, 0, RX_CAP_RES_AUTHORITY, RX_RIGHT_MINT, NULL) != RX_CAP_OK, 1344 "old office still authorizes the new root"); 1345 void *writable = mmap(NULL, sizeof(RxCapTable), PROT_READ | PROT_WRITE, MAP_SHARED, root.ro_fd, 0); 1346 CHECK(writable == MAP_FAILED, "restarted root handed out a writable table"); 1347 if (writable != MAP_FAILED) munmap(writable, sizeof(RxCapTable)); 1348 rx_caproot_stop(&root, &admin); 1349 } 1350 1351 /* ---- R5 resource admission (host) --------------------------------------- */ 1352 1353 static volatile int g_hold = 0; 1354 1355 static int fn_hold(RxCtx *c) { 1356 (void)c; 1357 while (g_hold) sleep_ms(1); 1358 return 0; 1359 } 1360 1361 static int fn_noop(RxCtx *c) { 1362 (void)c; 1363 return 0; 1364 } 1365 1366 static void t_resource_admission(void) { 1367 begin("resource_admission_1000", "R5"); 1368 Env *e = calloc(1, sizeof(*e)); 1369 CHECK(e != NULL, "allocation"); 1370 if (!e) return; 1371 CHECK(env_start(e, 2) == 0, "setup"); 1372 RxResourceBudget b; 1373 memset(&b, 0, sizeof(b)); 1374 b.slots = 4; 1375 b.memory_bytes = 40; 1376 b.offered_accel = 0x1u; 1377 rx_world_set_resources(&e->w, &b); 1378 1379 RxObjRef sensor = mkobj(e, RES_SENSOR, 0); 1380 RxCapRef ext = mint(e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 1381 RxCapRef rd = mint(e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 1382 g_hold = 1; 1383 for (int i = 0; i < 4; i++) { 1384 RxReactionDesc d; 1385 desc_init(&d, "hold.bg", RX_FACULTY_AIEN, SUBJ_AIEN, fn_hold, NULL); 1386 d.priority = RX_PRIO_BACKGROUND; 1387 add_trigger(&d, sensor, RX_FIELD(0)); 1388 add_cap(&d, rd, RES_SENSOR, RX_RIGHT_READ); 1389 CHECK(rx_world_add_reaction(&e->w, &d, NULL) == RX_OK, "bg hold"); 1390 } 1391 for (int i = 0; i < 4; i++) { 1392 RxReactionDesc d; 1393 desc_init(&d, "hold.crit", RX_FACULTY_AIEN, SUBJ_AIEN, fn_hold, NULL); 1394 d.priority = RX_PRIO_CRITICAL; 1395 add_trigger(&d, sensor, RX_FIELD(0)); 1396 add_cap(&d, rd, RES_SENSOR, RX_RIGHT_READ); 1397 CHECK(rx_world_add_reaction(&e->w, &d, NULL) == RX_OK, "crit hold"); 1398 } 1399 stimulus(e, ext, sensor, 0, 1); 1400 int saw = 0; 1401 for (int n = 0; n < 200; n++) { 1402 int crit = 0, bg = 0; 1403 for (uint32_t i = 0; i < e->w.n_reactions; i++) { 1404 RxState s = e->w.reactions[i].state; 1405 if (e->w.reactions[i].desc.priority == RX_PRIO_CRITICAL && 1406 (s == RX_RUNNING || s == RX_READY || s == RX_PUBLISHING)) 1407 crit++; 1408 if (e->w.reactions[i].desc.priority == RX_PRIO_BACKGROUND && 1409 s == RX_BLOCKED_RESOURCE) 1410 bg++; 1411 } 1412 if (crit == 4 && bg == 4) { saw = 1; break; } 1413 sleep_ms(5); 1414 } 1415 g_hold = 0; 1416 CHECK(saw, "critical work did not take the scarce slots ahead of background"); 1417 CHECK(rx_world_wait_quiescent(&e->w, 5000) == RX_OK, "holders did not finish"); 1418 1419 uint32_t base = e->w.n_reactions; 1420 for (uint32_t i = 0; i < 1000; i++) { 1421 RxReactionDesc d; 1422 desc_init(&d, "crowd", RX_FACULTY_AIEN, SUBJ_AIEN, fn_noop, NULL); 1423 d.priority = i % RX_PRIORITY_CLASSES; 1424 d.need.memory_bytes = 10; 1425 add_trigger(&d, sensor, RX_FIELD(0)); 1426 add_cap(&d, rd, RES_SENSOR, RX_RIGHT_READ); 1427 CHECK(rx_world_add_reaction(&e->w, &d, NULL) == RX_OK, "crowd %u", i); 1428 } 1429 RxReactionDesc bad; 1430 desc_init(&bad, "impossible", RX_FACULTY_AIEN, SUBJ_AIEN, fn_noop, NULL); 1431 bad.priority = RX_PRIO_CRITICAL; 1432 bad.need.memory_bytes = 10; 1433 bad.need.accelerator_features = 0x80000000u; 1434 add_trigger(&bad, sensor, RX_FIELD(0)); 1435 add_cap(&bad, rd, RES_SENSOR, RX_RIGHT_READ); 1436 CHECK(rx_world_add_reaction(&e->w, &bad, NULL) == RX_OK, "impossible"); 1437 e->w.peak_slots = 0; 1438 e->w.peak_blocked = 0; 1439 stimulus(e, ext, sensor, 0, 2); 1440 CHECK(rx_world_wait_quiescent(&e->w, 10000) == RX_OK, "crowd did not finish"); 1441 CHECK(e->w.peak_slots <= 4, "admitted %u reactions into 4 slots", e->w.peak_slots); 1442 CHECK(e->w.peak_blocked >= 996, "only %u waited for resources", e->w.peak_blocked); 1443 uint32_t ran = 0; 1444 for (uint32_t i = base; i < base + 1000; i++) { 1445 CHECK(e->w.reactions[i].activations == 1, "crowd reaction %u ran %llu", 1446 i, (unsigned long long)e->w.reactions[i].activations); 1447 ran += (uint32_t)e->w.reactions[i].activations; 1448 } 1449 CHECK(ran == 1000, "ran %u of 1000", ran); 1450 CHECK(e->w.reactions[base + 1000].activations == 0, "unsupported accelerator ran"); 1451 CHECK(e->w.reactions[base + 1000].state == RX_BLOCKED_RESOURCE, "impossible reaction was not blocked"); 1452 g_illegal_transitions += e->w.stats.illegal_transitions; 1453 rx_world_destroy(&e->w); 1454 rx_caproot_stop(&e->root, &e->admin); 1455 free(e); 1456 } 1457 1458 typedef struct { RxObjRef obj; int left; uint32_t prio; } Spin; 1459 1460 static uint32_t g_log[64]; 1461 static uint32_t g_nlog; 1462 1463 static int fn_spin(RxCtx *c) { 1464 Spin *s = c->user; 1465 if (g_nlog < 64) g_log[g_nlog++] = s->prio; 1466 if (s->left > 0) { 1467 s->left--; 1468 uint64_t cur = in_of(c, s->obj)->field[0]; 1469 c->out[c->n_out++] = (RxMutation){ s->obj, 0, cur + 1 }; 1470 } 1471 return 0; 1472 } 1473 1474 static int first_log(uint32_t prio) { 1475 for (uint32_t i = 0; i < g_nlog; i++) 1476 if (g_log[i] == prio) return (int)i; 1477 return -1; 1478 } 1479 1480 static void t_zero_resource_budget(void) { 1481 begin("zero_resource_budget_is_zero", "R5"); 1482 Env e; 1483 CHECK(env_start(&e, 1) == 0, "setup"); 1484 RxObjRef sensor = mkobj(&e, RES_SENSOR, 0); 1485 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 1486 RxCapRef rd = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 1487 1488 RxReactionDesc d; 1489 desc_init(&d, "zero.budget", RX_FACULTY_AIEN, SUBJ_AIEN, fn_noop, NULL); 1490 d.need.memory_bytes = 1; 1491 add_trigger(&d, sensor, RX_FIELD(0)); 1492 add_cap(&d, rd, RES_SENSOR, RX_RIGHT_READ); 1493 uint32_t id; 1494 CHECK(rx_world_add_reaction(&e.w, &d, &id) == RX_OK, "reaction"); 1495 1496 RxResourceBudget b; 1497 memset(&b, 0, sizeof(b)); 1498 rx_world_set_resources(&e.w, &b); 1499 stimulus(&e, ext, sensor, 0, 1); 1500 CHECK(rx_world_wait_quiescent(&e.w, 100) == RX_OK, "zero-budget world did not settle"); 1501 CHECK(e.w.reactions[id].activations == 0, "zero slots/memory admitted work"); 1502 CHECK(e.w.reactions[id].state == RX_BLOCKED_RESOURCE, "reaction not blocked at zero budget"); 1503 1504 b.slots = 1; 1505 b.memory_bytes = 1; 1506 rx_world_set_resources(&e.w, &b); 1507 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "restored budget did not run work"); 1508 CHECK(e.w.reactions[id].activations == 1, "restored budget ran %llu activations", 1509 (unsigned long long)e.w.reactions[id].activations); 1510 audit_and_close(&e); 1511 } 1512 1513 static void t_background_not_starved(void) { 1514 begin("background_not_starved", "R5"); 1515 Env e; 1516 CHECK(env_start(&e, 1) == 0, "setup"); 1517 RxResourceBudget b; 1518 memset(&b, 0, sizeof(b)); 1519 b.slots = 1; 1520 b.starvation_bound = 4; 1521 rx_world_set_resources(&e.w, &b); 1522 static Spin crit, bg; 1523 crit.obj = mkobj(&e, RES_TICK, 0); 1524 bg.obj = crit.obj; 1525 crit.left = 12; 1526 bg.left = 0; 1527 crit.prio = RX_PRIO_CRITICAL; 1528 bg.prio = RX_PRIO_BACKGROUND; 1529 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_TICK, RX_RIGHT_WRITE); 1530 RxCapRef rd = mint(&e, SUBJ_AIEN, RES_TICK, RX_RIGHT_READ); 1531 RxCapRef wr = mint(&e, SUBJ_AIEN, RES_TICK, RX_RIGHT_WRITE); 1532 RxReactionDesc d; 1533 desc_init(&d, "spin.bg", RX_FACULTY_AIEN, SUBJ_AIEN, fn_spin, &bg); 1534 d.priority = RX_PRIO_BACKGROUND; 1535 add_trigger(&d, crit.obj, RX_FIELD(0)); 1536 add_cap(&d, rd, RES_TICK, RX_RIGHT_READ); 1537 add_cap(&d, wr, RES_TICK, RX_RIGHT_WRITE); 1538 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK, "bg"); 1539 desc_init(&d, "spin.crit", RX_FACULTY_AIEN, SUBJ_AIEN, fn_spin, &crit); 1540 d.priority = RX_PRIO_CRITICAL; 1541 add_trigger(&d, crit.obj, RX_FIELD(0)); 1542 add_write(&d, crit.obj, RX_FIELD(0)); 1543 add_cap(&d, rd, RES_TICK, RX_RIGHT_READ); 1544 add_cap(&d, wr, RES_TICK, RX_RIGHT_WRITE); 1545 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK, "crit"); 1546 g_nlog = 0; 1547 stimulus(&e, ext, crit.obj, 0, 1); 1548 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "spin did not finish"); 1549 int at = first_log(RX_PRIO_BACKGROUND); 1550 CHECK(at >= 0, "background never ran"); 1551 CHECK(at <= 5, "background waited %d higher admissions (bound 4)", at); 1552 CHECK(at > 0, "background jumped the critical work"); 1553 audit_and_close(&e); 1554 } 1555 1556 static void t_priority_ladder(void) { 1557 begin("priority_ladder", "R5"); 1558 Env e; 1559 CHECK(env_start(&e, 1) == 0, "setup"); 1560 RxResourceBudget b; 1561 memset(&b, 0, sizeof(b)); 1562 b.slots = 1; 1563 b.starvation_bound = 3; 1564 rx_world_set_resources(&e.w, &b); 1565 g_hold = 1; 1566 RxObjRef gate = mkobj(&e, RES_SCRATCH, 0); 1567 RxObjRef tick = mkobj(&e, RES_TICK, 0); 1568 RxCapRef ext_g = mint(&e, SUBJ_EXTERNAL, RES_SCRATCH, RX_RIGHT_WRITE); 1569 RxCapRef rd_g = mint(&e, SUBJ_AIEN, RES_SCRATCH, RX_RIGHT_READ); 1570 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_TICK, RX_RIGHT_WRITE); 1571 RxCapRef rd = mint(&e, SUBJ_AIEN, RES_TICK, RX_RIGHT_READ); 1572 RxCapRef wr = mint(&e, SUBJ_AIEN, RES_TICK, RX_RIGHT_WRITE); 1573 RxReactionDesc d; 1574 desc_init(&d, "ladder.hold", RX_FACULTY_AIEN, SUBJ_AIEN, fn_hold, NULL); 1575 d.priority = RX_PRIO_CRITICAL; 1576 add_trigger(&d, gate, RX_FIELD(0)); 1577 add_cap(&d, rd_g, RES_SCRATCH, RX_RIGHT_READ); 1578 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK, "hold"); 1579 static Spin spins[RX_PRIORITY_CLASSES]; 1580 for (uint32_t p = 0; p < RX_PRIORITY_CLASSES; p++) { 1581 spins[p].obj = tick; 1582 spins[p].left = (p == RX_PRIO_CRITICAL) ? 8 : 0; 1583 spins[p].prio = p; 1584 desc_init(&d, "ladder", RX_FACULTY_AIEN, SUBJ_AIEN, fn_spin, &spins[p]); 1585 d.priority = p; 1586 add_trigger(&d, tick, RX_FIELD(0)); 1587 add_cap(&d, rd, RES_TICK, RX_RIGHT_READ); 1588 if (p == RX_PRIO_CRITICAL) { 1589 add_write(&d, tick, RX_FIELD(0)); 1590 add_cap(&d, wr, RES_TICK, RX_RIGHT_WRITE); 1591 } 1592 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK, "class %u", p); 1593 } 1594 g_nlog = 0; 1595 stimulus(&e, ext_g, gate, 0, 1); 1596 sleep_ms(20); 1597 stimulus(&e, ext, tick, 0, 1); 1598 sleep_ms(20); 1599 g_hold = 0; 1600 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "ladder did not finish"); 1601 int prev = -1; 1602 for (uint32_t p = 0; p < RX_PRIORITY_CLASSES; p++) { 1603 int at = first_log(p); 1604 CHECK(at >= 0, "class %u never ran", p); 1605 if (at >= 0 && prev >= 0) 1606 CHECK(at > prev, "class %u ran before a more urgent class (%d <= %d)", p, at, prev); 1607 if (at >= 0) prev = at; 1608 } 1609 CHECK(first_log(RX_PRIO_BACKGROUND) <= 12, "background waited %d admissions", 1610 first_log(RX_PRIO_BACKGROUND)); 1611 g_illegal_transitions += e.w.stats.illegal_transitions; 1612 rx_world_destroy(&e.w); 1613 rx_caproot_stop(&e.root, &e.admin); 1614 } 1615 1616 /* ---- R6 stability (host) ------------------------------------------------ */ 1617 1618 static int fn_flip(RxCtx *c) { 1619 RxObjRef *o = c->user; 1620 uint64_t v = in_of(c, *o)->field[0]; 1621 c->out[c->n_out++] = (RxMutation){ *o, 0, v ^ 1ull }; 1622 return 0; 1623 } 1624 1625 static int fn_down(RxCtx *c) { 1626 RxObjRef *o = c->user; 1627 uint64_t v = in_of(c, *o)->field[0]; 1628 if (v > 0) c->out[c->n_out++] = (RxMutation){ *o, 0, v - 1 }; 1629 return 0; 1630 } 1631 1632 static int fn_up_to(RxCtx *c) { 1633 RxObjRef *o = c->user; 1634 uint64_t v = in_of(c, *o)->field[0]; 1635 if (v < 5) c->out[c->n_out++] = (RxMutation){ *o, 0, v + 1 }; 1636 return 0; 1637 } 1638 1639 static int fn_const(RxCtx *c) { 1640 RxObjRef *o = c->user; 1641 c->out[c->n_out++] = (RxMutation){ *o, 0, 1 }; 1642 return 0; 1643 } 1644 1645 static void t_stability(void) { 1646 begin("stability_containment", "R6"); 1647 Env e; 1648 CHECK(env_start(&e, 1) == 0, "setup"); 1649 RxStabilityBudget s; 1650 memset(&s, 0, sizeof(s)); 1651 s.fanout_limit = 5; 1652 s.oscillation_limit = 4; 1653 s.livelock_limit = 3; 1654 rx_world_set_stability(&e.w, &s); 1655 1656 RxObjRef fan = mkobj(&e, RES_FAN_SRC, 0); 1657 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_FAN_SRC, RX_RIGHT_WRITE); 1658 RxCapRef rd = mint(&e, SUBJ_AIEN, RES_FAN_SRC, RX_RIGHT_READ); 1659 uint32_t fan_ids[40]; 1660 for (int i = 0; i < 40; i++) { 1661 RxReactionDesc d; 1662 desc_init(&d, "fan.bomb", RX_FACULTY_AIEN, SUBJ_AIEN, fn_noop, NULL); 1663 d.priority = (uint32_t)(i % RX_PRIORITY_CLASSES); 1664 add_trigger(&d, fan, RX_FIELD(0)); 1665 add_cap(&d, rd, RES_FAN_SRC, RX_RIGHT_READ); 1666 CHECK(rx_world_add_reaction(&e.w, &d, &fan_ids[i]) == RX_OK, "fan"); 1667 } 1668 stimulus(&e, ext, fan, 0, 1); 1669 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "fanout did not settle"); 1670 uint32_t fan_ran = 0, fan_crit = 0; 1671 for (int i = 0; i < 40; i++) { 1672 fan_ran += (uint32_t)e.w.reactions[fan_ids[i]].activations; 1673 if (e.w.reactions[fan_ids[i]].desc.priority == RX_PRIO_CRITICAL) 1674 fan_crit += (uint32_t)e.w.reactions[fan_ids[i]].activations; 1675 } 1676 CHECK(fan_ran == 40, "fanout delivered only %u of 40 dependent reactions", fan_ran); 1677 CHECK(fan_crit >= 5, "critical dependents did not execute (%u)", fan_crit); 1678 CHECK(e.w.stats.deferred_wakes >= 35, "fanout deferred only %llu wakes", 1679 (unsigned long long)e.w.stats.deferred_wakes); 1680 CHECK(e.w.stats.deferred_peak >= 35, "deferred peak %llu", 1681 (unsigned long long)e.w.stats.deferred_peak); 1682 1683 RxObjRef flip = mkobj(&e, RES_PAIR, 0); 1684 RxCapRef ext_f = mint(&e, SUBJ_EXTERNAL, RES_PAIR, RX_RIGHT_WRITE); 1685 RxCapRef rd_f = mint(&e, SUBJ_AIEN, RES_PAIR, RX_RIGHT_READ); 1686 RxCapRef wr_f = mint(&e, SUBJ_AIEN, RES_PAIR, RX_RIGHT_WRITE); 1687 static RxObjRef flip_o, down_o, up_o; 1688 flip_o = flip; 1689 RxReactionDesc d; 1690 desc_init(&d, "osc.flip", RX_FACULTY_AIEN, SUBJ_AIEN, fn_flip, &flip_o); 1691 add_trigger(&d, flip, RX_FIELD(0)); 1692 add_write(&d, flip, RX_FIELD(0)); 1693 add_cap(&d, rd_f, RES_PAIR, RX_RIGHT_READ); 1694 add_cap(&d, wr_f, RES_PAIR, RX_RIGHT_WRITE); 1695 uint32_t flip_id; 1696 CHECK(rx_world_add_reaction(&e.w, &d, &flip_id) == RX_OK, "flip"); 1697 stimulus(&e, ext_f, flip, 0, 1); 1698 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "oscillation did not settle"); 1699 uint64_t flip_acts = e.w.reactions[flip_id].activations; 1700 CHECK(e.w.reactions[flip_id].quarantined, "oscillation was not quarantined"); 1701 CHECK(flip_acts > 0 && flip_acts < 30, "oscillation ran %llu times", 1702 (unsigned long long)flip_acts); 1703 stimulus(&e, ext_f, flip, 0, field(&e, flip, 0) ^ 1ull); 1704 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "lift did not settle"); 1705 CHECK(e.w.reactions[flip_id].activations > flip_acts, "external cause did not resume the reaction"); 1706 1707 down_o = mkobj(&e, RES_CHECK, 0); 1708 RxCapRef ext_d = mint(&e, SUBJ_EXTERNAL, RES_CHECK, RX_RIGHT_WRITE); 1709 RxCapRef rd_d = mint(&e, SUBJ_AIEN, RES_CHECK, RX_RIGHT_READ); 1710 RxCapRef wr_d = mint(&e, SUBJ_AIEN, RES_CHECK, RX_RIGHT_WRITE); 1711 desc_init(&d, "converge", RX_FACULTY_AIEN, SUBJ_AIEN, fn_down, &down_o); 1712 add_trigger(&d, down_o, RX_FIELD(0)); 1713 add_write(&d, down_o, RX_FIELD(0)); 1714 add_cap(&d, rd_d, RES_CHECK, RX_RIGHT_READ); 1715 add_cap(&d, wr_d, RES_CHECK, RX_RIGHT_WRITE); 1716 uint32_t down_id; 1717 CHECK(rx_world_add_reaction(&e.w, &d, &down_id) == RX_OK, "down"); 1718 stimulus(&e, ext_d, down_o, 0, 8); 1719 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "convergent loop did not settle"); 1720 CHECK(field(&e, down_o, 0) == 0, "convergent loop did not reach the fixed point"); 1721 CHECK(!e.w.reactions[down_id].quarantined, "convergent loop was quarantined"); 1722 1723 up_o = mkobj(&e, RES_SCRATCH, 0); 1724 RxCapRef ext_u = mint(&e, SUBJ_EXTERNAL, RES_SCRATCH, RX_RIGHT_WRITE); 1725 RxCapRef rd_u = mint(&e, SUBJ_AIEN, RES_SCRATCH, RX_RIGHT_READ); 1726 RxCapRef wr_u = mint(&e, SUBJ_AIEN, RES_SCRATCH, RX_RIGHT_WRITE); 1727 desc_init(&d, "periodic", RX_FACULTY_AIEN, SUBJ_AIEN, fn_up_to, &up_o); 1728 add_trigger(&d, up_o, RX_FIELD(0)); 1729 add_write(&d, up_o, RX_FIELD(0)); 1730 add_cap(&d, rd_u, RES_SCRATCH, RX_RIGHT_READ); 1731 add_cap(&d, wr_u, RES_SCRATCH, RX_RIGHT_WRITE); 1732 uint32_t up_id; 1733 CHECK(rx_world_add_reaction(&e.w, &d, &up_id) == RX_OK, "up"); 1734 /* activation budget is 6; counting to 5 must still finish */ 1735 stimulus(&e, ext_u, up_o, 0, 1); 1736 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "periodic loop did not settle"); 1737 CHECK(field(&e, up_o, 0) == 5, "periodic loop stopped early at %llu", 1738 (unsigned long long)field(&e, up_o, 0)); 1739 CHECK(!e.w.reactions[up_id].quarantined, "periodic loop was quarantined"); 1740 1741 RxObjRef bell = mkobj(&e, RES_RISK, 0); 1742 RxObjRef sink = mkobj(&e, RES_PLAN, 0); 1743 RxCapRef ext_b = mint(&e, SUBJ_EXTERNAL, RES_RISK, RX_RIGHT_WRITE); 1744 RxCapRef rd_b = mint(&e, SUBJ_AIEN, RES_RISK, RX_RIGHT_READ); 1745 RxCapRef wr_b = mint(&e, SUBJ_AIEN, RES_RISK, RX_RIGHT_WRITE); 1746 RxCapRef rd_p = mint(&e, SUBJ_AIEN, RES_PLAN, RX_RIGHT_READ); 1747 RxCapRef wr_p = mint(&e, SUBJ_AIEN, RES_PLAN, RX_RIGHT_WRITE); 1748 static RxObjRef sink_o; 1749 sink_o = sink; 1750 desc_init(&d, "livelock.spin", RX_FACULTY_AIEN, SUBJ_AIEN, fn_const, &sink_o); 1751 add_trigger(&d, bell, RX_FIELD(0)); 1752 add_write(&d, sink, RX_FIELD(0)); 1753 add_cap(&d, rd_b, RES_RISK, RX_RIGHT_READ); 1754 add_cap(&d, wr_p, RES_PLAN, RX_RIGHT_WRITE); 1755 uint32_t spin_id; 1756 CHECK(rx_world_add_reaction(&e.w, &d, &spin_id) == RX_OK, "spin"); 1757 static Spin metro; 1758 metro.obj = bell; 1759 metro.left = 10; 1760 metro.prio = RX_PRIO_FOREGROUND; 1761 desc_init(&d, "livelock.metro", RX_FACULTY_OMEGA, SUBJ_AIEN, fn_spin, &metro); 1762 d.priority = RX_PRIO_FOREGROUND; 1763 add_trigger(&d, bell, RX_FIELD(0)); 1764 add_write(&d, bell, RX_FIELD(0)); 1765 add_cap(&d, rd_b, RES_RISK, RX_RIGHT_READ); 1766 add_cap(&d, wr_b, RES_RISK, RX_RIGHT_WRITE); 1767 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK, "metro"); 1768 (void)rd_p; 1769 stimulus(&e, ext_b, bell, 0, 1); 1770 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "livelock did not settle"); 1771 CHECK(e.w.reactions[spin_id].quarantined, "noop loop was not quarantined"); 1772 uint64_t spin_acts = e.w.reactions[spin_id].activations; 1773 CHECK(spin_acts > 0 && spin_acts <= 4, "noop loop ran %llu times", 1774 (unsigned long long)spin_acts); 1775 CHECK(e.w.stats.livelock_trips >= 1, "livelock was not counted"); 1776 CHECK(e.w.stats.useful_commits > 0, "useful work was not counted separately from churn"); 1777 1778 s.activation_budget = 4; 1779 rx_world_set_stability(&e.w, &s); 1780 static Spin bounded; 1781 bounded.obj = mkobj(&e, RES_SENSOR, 0); 1782 bounded.left = 30; 1783 bounded.prio = RX_PRIO_FOREGROUND; 1784 RxCapRef ext_s = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 1785 RxCapRef rd_s = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 1786 RxCapRef wr_s = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_WRITE); 1787 desc_init(&d, "budget", RX_FACULTY_AIEN, SUBJ_AIEN, fn_spin, &bounded); 1788 add_trigger(&d, bounded.obj, RX_FIELD(0)); 1789 add_write(&d, bounded.obj, RX_FIELD(0)); 1790 add_cap(&d, rd_s, RES_SENSOR, RX_RIGHT_READ); 1791 add_cap(&d, wr_s, RES_SENSOR, RX_RIGHT_WRITE); 1792 uint32_t budget_id; 1793 CHECK(rx_world_add_reaction(&e.w, &d, &budget_id) == RX_OK, "budget"); 1794 stimulus(&e, ext_s, bounded.obj, 0, 1); 1795 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "budgeted reaction did not settle"); 1796 CHECK(e.w.reactions[budget_id].activations == 4, "activation budget ran %llu", 1797 (unsigned long long)e.w.reactions[budget_id].activations); 1798 CHECK(e.w.reactions[budget_id].quarantined, "activation budget did not quarantine"); 1799 uint64_t before_episode = e.w.reactions[budget_id].activations; 1800 stimulus(&e, ext_s, bounded.obj, 0, field(&e, bounded.obj, 0) + 1); 1801 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "fresh causal episode did not settle"); 1802 CHECK(e.w.reactions[budget_id].activations == before_episode + 4, 1803 "fresh external episode did not receive a fresh activation budget (%llu -> %llu)", 1804 (unsigned long long)before_episode, 1805 (unsigned long long)e.w.reactions[budget_id].activations); 1806 CHECK(e.w.reactions[budget_id].quarantined, 1807 "second activation episode did not enforce its own bound"); 1808 audit_and_close(&e); 1809 } 1810 1811 /* relay: copies the stimulus into a second object. watch: counts it. */ 1812 typedef struct { RxObjRef from, to; } Relay; 1813 static int fn_relay(RxCtx *c) { 1814 Relay *r = c->user; 1815 c->out[c->n_out++] = (RxMutation){ r->to, 0, in_of(c, r->from)->field[0] }; 1816 return 0; 1817 } 1818 static int fn_bump(RxCtx *c) { 1819 Relay *r = c->user; 1820 c->out[c->n_out++] = (RxMutation){ r->to, 0, in_of(c, r->from)->field[0] + 1 }; 1821 return 0; 1822 } 1823 static int fn_count(RxCtx *c) { 1824 RxObjRef *o = c->user; 1825 c->out[c->n_out++] = (RxMutation){ *o, 1, in_of(c, *o)->field[1] + 1 }; 1826 return 0; 1827 } 1828 1829 /* Regression (R14). The activation budget belongs to a causal episode: the 1830 * work one outside publication sets off. A reaction woken one step down the 1831 * chain gets a fresh budget with every new episode; before this fix only a 1832 * direct outside wake reset it, so in the R13 body omega.watch and 1833 * aien.observe were quarantined after 256 activations. A two-node cycle 1834 * inside one episode is still stopped, and the stop leaves a crumb. */ 1835 static void t_episode_budget(void) { 1836 begin("episode_activation_budget", "R6"); 1837 Env e; 1838 CHECK(env_start(&e, 2) == 0, "setup"); 1839 RxStabilityBudget s; 1840 memset(&s, 0, sizeof(s)); 1841 s.activation_budget = 4; 1842 rx_world_set_stability(&e.w, &s); 1843 1844 static Relay relay; 1845 static RxObjRef mid_o; 1846 relay.from = mkobj(&e, RES_SENSOR, 0); 1847 relay.to = mid_o = mkobj(&e, RES_BELIEF, 0); 1848 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 1849 RxCapRef rs = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 1850 RxCapRef rwb = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_READ | RX_RIGHT_WRITE); 1851 RxReactionDesc d; 1852 desc_init(&d, "relay", RX_FACULTY_AIEN, SUBJ_AIEN, fn_relay, &relay); 1853 add_trigger(&d, relay.from, RX_FIELD(0)); 1854 add_write(&d, relay.to, RX_FIELD(0)); 1855 add_cap(&d, rs, RES_SENSOR, RX_RIGHT_READ); 1856 add_cap(&d, rwb, RES_BELIEF, RX_RIGHT_READ | RX_RIGHT_WRITE); 1857 uint32_t relay_id, watch_id; 1858 CHECK(rx_world_add_reaction(&e.w, &d, &relay_id) == RX_OK, "relay"); 1859 desc_init(&d, "watch", RX_FACULTY_OMEGA, SUBJ_AIEN, fn_count, &mid_o); 1860 add_trigger(&d, mid_o, RX_FIELD(0)); 1861 add_write(&d, mid_o, RX_FIELD(1)); 1862 add_cap(&d, rwb, RES_BELIEF, RX_RIGHT_READ | RX_RIGHT_WRITE); 1863 CHECK(rx_world_add_reaction(&e.w, &d, &watch_id) == RX_OK, "watch"); 1864 for (uint64_t i = 1; i <= 20; i++) { 1865 CHECK(stimulus(&e, ext, relay.from, 0, i) > 0, "stimulus %llu", (unsigned long long)i); 1866 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "episode %llu", (unsigned long long)i); 1867 } 1868 CHECK(e.w.reactions[watch_id].activations == 20 && field(&e, mid_o, 1) == 20, 1869 "a reaction one step from the outside ran %llu of 20 episodes", 1870 (unsigned long long)e.w.reactions[watch_id].activations); 1871 CHECK(!e.w.reactions[watch_id].quarantined && !e.w.reactions[relay_id].quarantined, 1872 "an ordinary chain was quarantined across episodes"); 1873 1874 /* ping writes pong + 1, pong writes ping + 1: never converges. */ 1875 static Relay ping, pong; 1876 RxObjRef a = mkobj(&e, RES_PAIR, 0), b = mkobj(&e, RES_TICK, 0); 1877 ping = (Relay){ a, b }; 1878 pong = (Relay){ b, a }; 1879 RxCapRef ext_a = mint(&e, SUBJ_EXTERNAL, RES_PAIR, RX_RIGHT_WRITE); 1880 RxCapRef rwa = mint(&e, SUBJ_AIEN, RES_PAIR, RX_RIGHT_READ | RX_RIGHT_WRITE); 1881 RxCapRef rwt = mint(&e, SUBJ_AIEN, RES_TICK, RX_RIGHT_READ | RX_RIGHT_WRITE); 1882 uint32_t ping_id, pong_id; 1883 desc_init(&d, "cycle.ping", RX_FACULTY_AIEN, SUBJ_AIEN, fn_bump, &ping); 1884 add_trigger(&d, a, RX_FIELD(0)); 1885 add_write(&d, b, RX_FIELD(0)); 1886 add_cap(&d, rwa, RES_PAIR, RX_RIGHT_READ); 1887 add_cap(&d, rwt, RES_TICK, RX_RIGHT_READ | RX_RIGHT_WRITE); 1888 CHECK(rx_world_add_reaction(&e.w, &d, &ping_id) == RX_OK, "ping"); 1889 desc_init(&d, "cycle.pong", RX_FACULTY_AIEN, SUBJ_AIEN, fn_bump, &pong); 1890 add_trigger(&d, b, RX_FIELD(0)); 1891 add_write(&d, a, RX_FIELD(0)); 1892 add_cap(&d, rwt, RES_TICK, RX_RIGHT_READ); 1893 add_cap(&d, rwa, RES_PAIR, RX_RIGHT_READ | RX_RIGHT_WRITE); 1894 CHECK(rx_world_add_reaction(&e.w, &d, &pong_id) == RX_OK, "pong"); 1895 int64_t root = stimulus(&e, ext_a, a, 0, 1); 1896 CHECK(root > 0, "cycle stimulus"); 1897 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "cycle did not settle"); 1898 CHECK(e.w.reactions[ping_id].quarantined || e.w.reactions[pong_id].quarantined, 1899 "a cycle inside one episode was not stopped"); 1900 CHECK(e.w.reactions[ping_id].activations <= 5 && e.w.reactions[pong_id].activations <= 5, 1901 "cycle ran %llu/%llu", (unsigned long long)e.w.reactions[ping_id].activations, 1902 (unsigned long long)e.w.reactions[pong_id].activations); 1903 uint64_t contained = 0; 1904 for (uint64_t id = 1; id <= e.w.n_crumbs; id++) { 1905 const RxCrumb *k = rx_world_crumb(&e.w, id); 1906 if (k->kind != RX_CRUMB_QUARANTINE) continue; 1907 contained++; 1908 CHECK(k->reason == RX_CONTAIN_BUDGET && (k->reaction == ping_id || k->reaction == pong_id) && 1909 k->episode == (uint64_t)root, 1910 "containment crumb %llu names reaction %u reason %d episode %llu", 1911 (unsigned long long)id, k->reaction, k->reason, (unsigned long long)k->episode); 1912 } 1913 CHECK(contained >= 1 && contained <= 2, "%llu containment crumbs", (unsigned long long)contained); 1914 /* The quarantined cycle does not stop the chain that is not part of it. */ 1915 CHECK(stimulus(&e, ext, relay.from, 0, 21) > 0, "stimulus after the cycle"); 1916 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "after the cycle"); 1917 CHECK(field(&e, mid_o, 1) == 21, "the chain stopped with the cycle"); 1918 audit_and_close(&e); 1919 } 1920 1921 typedef struct { RxObjRef obj; int left; } FlipN; 1922 1923 static int fn_flip_n(RxCtx *c) { 1924 FlipN *s = c->user; 1925 if (s->left <= 0) return 0; 1926 s->left--; 1927 uint64_t v = in_of(c, s->obj)->field[0]; 1928 c->out[c->n_out++] = (RxMutation){ s->obj, 0, v ^ 1ull }; 1929 return 0; 1930 } 1931 1932 static void t_periodic_and_backoff(void) { 1933 begin("periodic_versus_oscillation", "R6"); 1934 Env e; 1935 CHECK(env_start(&e, 1) == 0, "setup"); 1936 RxStabilityBudget s; 1937 memset(&s, 0, sizeof(s)); 1938 s.oscillation_limit = 4; 1939 s.livelock_limit = 6; 1940 rx_world_set_stability(&e.w, &s); 1941 static FlipN period; 1942 period.obj = mkobj(&e, RES_TICK, 0); 1943 period.left = 12; 1944 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_TICK, RX_RIGHT_WRITE); 1945 RxCapRef rd = mint(&e, SUBJ_AIEN, RES_TICK, RX_RIGHT_READ); 1946 RxCapRef wr = mint(&e, SUBJ_AIEN, RES_TICK, RX_RIGHT_WRITE); 1947 RxReactionDesc d; 1948 desc_init(&d, "periodic.heartbeat", RX_FACULTY_AIEN, SUBJ_AIEN, fn_flip_n, &period); 1949 d.loop_kind = RX_LOOP_PERIODIC; 1950 add_trigger(&d, period.obj, RX_FIELD(0)); 1951 add_write(&d, period.obj, RX_FIELD(0)); 1952 add_cap(&d, rd, RES_TICK, RX_RIGHT_READ); 1953 add_cap(&d, wr, RES_TICK, RX_RIGHT_WRITE); 1954 uint32_t id; 1955 CHECK(rx_world_add_reaction(&e.w, &d, &id) == RX_OK, "periodic"); 1956 stimulus(&e, ext, period.obj, 0, 1); 1957 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "periodic loop did not settle"); 1958 CHECK(!e.w.reactions[id].quarantined, "a declared periodic loop was quarantined"); 1959 CHECK(e.w.reactions[id].activations > 4, "periodic loop ran %llu times", 1960 (unsigned long long)e.w.reactions[id].activations); 1961 CHECK(e.w.stats.periodic_commits >= 4, "periodic commits %llu", 1962 (unsigned long long)e.w.stats.periodic_commits); 1963 CHECK(e.w.stats.oscillation_trips == 0, "periodic loop was counted as oscillation"); 1964 1965 RxResourceBudget b; 1966 memset(&b, 0, sizeof(b)); 1967 b.slots = 1; 1968 b.starvation_bound = 1; 1969 rx_world_set_resources(&e.w, &b); 1970 s.oscillation_limit = 0; 1971 rx_world_set_stability(&e.w, &s); 1972 g_hold = 1; 1973 RxObjRef gate = mkobj(&e, RES_SCRATCH, 0); 1974 RxCapRef ext_g = mint(&e, SUBJ_EXTERNAL, RES_SCRATCH, RX_RIGHT_WRITE); 1975 RxCapRef rd_g = mint(&e, SUBJ_AIEN, RES_SCRATCH, RX_RIGHT_READ); 1976 desc_init(&d, "backoff.hold", RX_FACULTY_AIEN, SUBJ_AIEN, fn_hold, NULL); 1977 d.priority = RX_PRIO_CRITICAL; 1978 add_trigger(&d, gate, RX_FIELD(0)); 1979 add_cap(&d, rd_g, RES_SCRATCH, RX_RIGHT_READ); 1980 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK, "hold"); 1981 static FlipN hot; 1982 static Spin cool; 1983 hot.obj = mkobj(&e, RES_RISK, 0); 1984 hot.left = 6; 1985 cool.obj = mkobj(&e, RES_PLAN, 0); 1986 cool.left = 4; 1987 cool.prio = RX_PRIO_BACKGROUND; 1988 RxCapRef ext_h = mint(&e, SUBJ_EXTERNAL, RES_RISK, RX_RIGHT_WRITE); 1989 RxCapRef rd_h = mint(&e, SUBJ_AIEN, RES_RISK, RX_RIGHT_READ); 1990 RxCapRef wr_h = mint(&e, SUBJ_AIEN, RES_RISK, RX_RIGHT_WRITE); 1991 RxCapRef ext_c = mint(&e, SUBJ_EXTERNAL, RES_PLAN, RX_RIGHT_WRITE); 1992 RxCapRef rd_c = mint(&e, SUBJ_AIEN, RES_PLAN, RX_RIGHT_READ); 1993 RxCapRef wr_c = mint(&e, SUBJ_AIEN, RES_PLAN, RX_RIGHT_WRITE); 1994 desc_init(&d, "backoff.hot", RX_FACULTY_AIEN, SUBJ_AIEN, fn_flip_n, &hot); 1995 d.priority = RX_PRIO_CRITICAL; 1996 add_trigger(&d, hot.obj, RX_FIELD(0)); 1997 add_write(&d, hot.obj, RX_FIELD(0)); 1998 add_cap(&d, rd_h, RES_RISK, RX_RIGHT_READ); 1999 add_cap(&d, wr_h, RES_RISK, RX_RIGHT_WRITE); 2000 uint32_t hot_id; 2001 CHECK(rx_world_add_reaction(&e.w, &d, &hot_id) == RX_OK, "hot"); 2002 desc_init(&d, "backoff.cool", RX_FACULTY_OMEGA, SUBJ_AIEN, fn_spin, &cool); 2003 d.priority = RX_PRIO_BACKGROUND; 2004 add_trigger(&d, cool.obj, RX_FIELD(0)); 2005 add_write(&d, cool.obj, RX_FIELD(0)); 2006 add_cap(&d, rd_c, RES_PLAN, RX_RIGHT_READ); 2007 add_cap(&d, wr_c, RES_PLAN, RX_RIGHT_WRITE); 2008 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK, "cool"); 2009 stimulus(&e, ext_g, gate, 0, 1); 2010 sleep_ms(20); 2011 stimulus(&e, ext_h, hot.obj, 0, 1); 2012 stimulus(&e, ext_c, cool.obj, 0, 1); 2013 g_hold = 0; 2014 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "backoff case did not settle"); 2015 CHECK(field(&e, cool.obj, 0) == 5, "background work did not finish while the hot loop yielded"); 2016 CHECK(e.w.reactions[hot_id].activations > 0, "hot loop never ran"); 2017 CHECK(e.w.stats.backoffs > 0, "hot loop never yielded"); 2018 audit_and_close(&e); 2019 } 2020 2021 /* ---- one identity, then the CPU cross-engine descriptor ------------------- */ 2022 2023 static uint32_t ld32(const uint8_t *p) { 2024 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); 2025 } 2026 static uint64_t ld64(const uint8_t *p) { 2027 uint64_t v = 0; 2028 for (int i = 0; i < 8; i++) v |= (uint64_t)p[i] << (8 * i); 2029 return v; 2030 } 2031 static void st32(uint8_t *p, uint32_t v) { 2032 p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); 2033 p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); 2034 } 2035 static void st64(uint8_t *p, uint64_t v) { 2036 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); 2037 } 2038 2039 static RxObjRef g_note_out; 2040 2041 static int fn_note(RxCtx *c) { 2042 c->out[c->n_out++] = (RxMutation){ g_note_out, 0, c->in[0].field[0] + 1 }; 2043 return 0; 2044 } 2045 2046 static OmegaSharedWorldDesc make_pub(RxWorld *w, const RxObject *o, RxCapRef cap, uint64_t crumb) { 2047 OmegaSharedWorldDesc d; 2048 memset(&d, 0, sizeof(d)); 2049 d.msg_type = RX_RING_PUBLISH; 2050 d.sequence = rx_world_publication_tail(w); 2051 d.world_epoch = 1; 2052 d.object_id = o->id; 2053 d.object_generation = o->generation; 2054 d.object_offset = 0; 2055 d.object_length = (uint32_t)o->size_bytes; 2056 d.payload_len = RX_CAP_PAYLOAD; 2057 st32(d.payload, cap.cap_id); 2058 st32(d.payload + 4, (uint32_t)cap.generation); 2059 st32(d.payload + RX_CAP_GEN_HI_A, (uint32_t)(cap.generation >> 32)); 2060 st64(d.payload + 8, o->version); 2061 st64(d.payload + 16, crumb); 2062 rx_world_seal_descriptor(&d); 2063 return d; 2064 } 2065 2066 static int inject_take(RxWorld *w, const OmegaSharedWorldDesc *d, uint32_t *fault) { 2067 if (rx_world_inject_descriptor(w, d) != RX_OK) return RX_ERR_FULL; 2068 return rx_world_take_publication(w, NULL, fault); 2069 } 2070 2071 static void t_one_identity(void) { 2072 begin("canonical_object_one_identity", "one id, one generation, one lineage"); 2073 Env e; 2074 CHECK(env_start(&e, 2) == 0, "setup"); 2075 CHECK(sizeof(OmegaSharedWorldObject) == 32, "physical record changed size"); 2076 CHECK(sizeof(OmegaSharedWorldDesc) == 128, "descriptor changed size"); 2077 RxObjRef obj = mkobj(&e, RES_SENSOR, 0); 2078 g_note_out = mkobj(&e, RES_BELIEF, 0); 2079 RxObject sem; 2080 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "read"); 2081 OmegaSharedWorldObject phys; 2082 CHECK(rx_world_physical(&e.w, obj, &phys) == RX_ERR_UNPLACED, "a new object already had a physical copy"); 2083 CHECK(!sem.placed && sem.region_offset == 0 && sem.size_bytes == 0, "semantic object carried a placement"); 2084 CHECK(rx_world_attach_physical(&e.w, obj) == RX_OK, "attach"); 2085 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "read after attach"); 2086 CHECK(sem.generation == obj.generation, "attach advanced the generation"); 2087 CHECK(rx_world_physical(&e.w, obj, &phys) == RX_OK, "physical"); 2088 CHECK(phys.object_id == sem.id && phys.object_id == obj.id, "two ids for one object"); 2089 CHECK(phys.generation == sem.generation && sem.generation == 1, "create opened a second generation"); 2090 CHECK(phys.permissions == 0, "shared record carried a permission bit"); 2091 CHECK(phys.region_offset == sem.region_offset && phys.size_bytes == sem.size_bytes, "placement diverged"); 2092 CHECK(sem.size_bytes == RX_OBJECT_WINDOW, "window length"); 2093 CHECK(sem.region_offset > sem.id && sem.region_offset < e.w.coherent_bytes, "placement is not inside the shared image"); 2094 CHECK(sem.placement == RX_PLACE_COHERENT && sem.locality == RX_LOCALITY_MACHINE, "locality"); 2095 CHECK(sem.coherency == RX_COHERENCY_HOST, "coherency"); 2096 CHECK(sem.cap.cap_id == 0 && sem.cap.generation == 0, "unbound object named a capability"); 2097 2098 RxCapRef cap = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_READ | RX_RIGHT_WRITE); 2099 RxCapRef rd = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 2100 RxCapRef wr = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE); 2101 CHECK(rx_world_bind_capability(&e.w, obj, cap) == RX_OK, "bind"); 2102 RxReactionDesc d; 2103 desc_init(&d, "note", RX_FACULTY_AIEN, SUBJ_AIEN, fn_note, NULL); 2104 add_trigger(&d, obj, RX_FIELD(0)); 2105 add_write(&d, g_note_out, RX_FIELD(0)); 2106 add_cap(&d, rd, RES_SENSOR, RX_RIGHT_READ); 2107 add_cap(&d, wr, RES_BELIEF, RX_RIGHT_WRITE); 2108 uint32_t rid = 0; 2109 CHECK(rx_world_add_reaction(&e.w, &d, &rid) == RX_OK, "add note"); 2110 2111 int64_t cid = stimulus(&e, cap, obj, 0, 5); 2112 CHECK(cid > 0, "stimulus"); 2113 CHECK(rx_world_wait_quiescent(&e.w, 3000) == RX_OK, "quiet"); 2114 CHECK(field(&e, g_note_out, 0) == 6, "host reaction did not wake"); 2115 CHECK(e.w.reactions[rid].commits == 1, "host reaction did not publish"); 2116 2117 OmegaSharedWorldDesc got; 2118 uint32_t fault = 99; 2119 CHECK(rx_world_take_publication(&e.w, &got, &fault) == RX_OK, "publication was not accepted"); 2120 CHECK(fault == 0, "fresh publication raised a fault"); 2121 CHECK(got.msg_type == RX_RING_PUBLISH, "ring was not a publication"); 2122 CHECK(got.object_id == obj.id && got.object_generation == obj.generation, "descriptor named a different object"); 2123 RxObject after; 2124 CHECK(rx_world_read(&e.w, obj, &after) == RX_OK, "reread"); 2125 CHECK(after.generation == obj.generation, "publication bumped the generation"); 2126 CHECK(after.version > sem.version, "publication did not advance the canonical version"); 2127 CHECK(ld64(got.payload + 8) == after.version, "descriptor version differs from the object"); 2128 CHECK(ld64(got.payload + 16) == (uint64_t)cid, "descriptor lost the causal record"); 2129 CHECK(ld32(got.payload) == cap.cap_id && 2130 (ld32(got.payload + 4) | (uint64_t)ld32(got.payload + RX_CAP_GEN_HI_A) << 32) == 2131 cap.generation, 2132 "capability reference differs"); 2133 CHECK(rx_world_explain(&e.w, obj, 0) == (uint64_t)cid, "field history points elsewhere"); 2134 const RxCrumb *k = rx_world_crumb(&e.w, (uint64_t)cid); 2135 CHECK(k && k->kind == RX_CRUMB_EXTERNAL, "stimulus crumb"); 2136 CHECK(k->n_outputs == 1 && k->outputs[0].obj.id == got.object_id && 2137 k->outputs[0].obj.generation == got.object_generation, "lineage broke"); 2138 CHECK(rx_world_take_publication(&e.w, NULL, &fault) == RX_ERR_NOT_FOUND, "a second object was published"); 2139 2140 uint32_t old_gen = obj.generation; 2141 CHECK(rx_world_retire(&e.w, obj) == RX_OK, "retire"); 2142 RxObjRef neu = { obj.id, e.w.objects[obj.id].generation }; 2143 CHECK(neu.generation == old_gen + 1, "one generation counter did not advance"); 2144 OmegaSharedWorldObject retired; 2145 CHECK(rx_world_physical(&e.w, neu, &retired) == RX_OK, "retired projection missing"); 2146 CHECK(retired.generation == neu.generation && retired.object_id == obj.id, "projection has its own generation"); 2147 CHECK(retired.state == OMEGA_SW_OBJ_REVOKED && retired.permissions == 0, "retired record"); 2148 OmegaSharedWorldDesc stale = got; 2149 stale.sequence = rx_world_publication_tail(&e.w); 2150 stale.object_generation = old_gen; 2151 rx_world_seal_descriptor(&stale); 2152 CHECK(inject_take(&e.w, &stale, &fault) == RX_ERR_STALE_GEN, "stale generation was accepted"); 2153 CHECK(rx_world_read(&e.w, obj, &after) == RX_ERR_STALE_GEN, "host still honored the retired generation"); 2154 RxObjRef again = mkobj(&e, RES_SENSOR, 0); 2155 CHECK(again.id == obj.id && again.generation == neu.generation, "reuse opened a second identity"); 2156 CHECK(rx_world_physical(&e.w, obj, &retired) == RX_ERR_STALE_GEN, "old physical name survived reuse"); 2157 CHECK(rx_world_physical(&e.w, again, &retired) == RX_ERR_UNPLACED, "reused object inherited the old placement"); 2158 CHECK(rx_world_read(&e.w, again, &after) == RX_OK && after.generation == again.generation, "reused object"); 2159 audit_and_close(&e); 2160 } 2161 2162 static void t_identity_closed(void) { 2163 begin("identity_space_is_closed", "one identity space; placement is not required"); 2164 Env e; 2165 CHECK(env_start(&e, 1) == 0, "setup"); 2166 RxObjRef refs[RX_MAX_OBJECTS]; 2167 uint32_t n = 0; 2168 for (; n < RX_MAX_OBJECTS; n++) { 2169 refs[n] = mkobj(&e, RES_SCRATCH, n); 2170 if (refs[n].id == UINT32_MAX) break; 2171 } 2172 CHECK(n == RX_MAX_OBJECTS, "identity space is smaller than the host world"); 2173 RxObjRef extra = mkobj(&e, RES_SCRATCH, 0); 2174 CHECK(extra.id == UINT32_MAX, "an object was created with nowhere to stand"); 2175 uint32_t bad = 0; 2176 for (uint32_t i = 0; i < n; i++) { 2177 RxObject sem; 2178 OmegaSharedWorldObject phys; 2179 if (rx_world_read(&e.w, refs[i], &sem) != RX_OK || sem.placed || 2180 sem.generation != refs[i].generation || 2181 rx_world_physical(&e.w, refs[i], &phys) != RX_ERR_UNPLACED) 2182 bad++; 2183 } 2184 CHECK(bad == 0, "a semantic object was born with a physical copy"); 2185 CHECK(rx_world_attach_physical(&e.w, refs[0]) == RX_OK, "attach one"); 2186 RxObject sem; 2187 OmegaSharedWorldObject phys; 2188 CHECK(rx_world_read(&e.w, refs[0], &sem) == RX_OK, "read attached"); 2189 CHECK(rx_world_physical(&e.w, refs[0], &phys) == RX_OK, "physical of one"); 2190 CHECK(phys.object_id == sem.id && phys.generation == sem.generation && phys.permissions == 0, 2191 "attached record used a different name"); 2192 audit_and_close(&e); 2193 } 2194 2195 static void t_semantic_without_transport(void) { 2196 begin("semantic_commit_without_transport", "a reaction publishes with no physical copy"); 2197 Env e; 2198 CHECK(env_start(&e, 2) == 0, "setup"); 2199 RxObjRef obj = mkobj(&e, RES_SENSOR, 0); 2200 g_note_out = mkobj(&e, RES_BELIEF, 0); 2201 RxCapRef cap = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_READ | RX_RIGHT_WRITE); 2202 RxCapRef rd = mint(&e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ); 2203 RxCapRef wr = mint(&e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE); 2204 CHECK(rx_world_bind_capability(&e.w, obj, cap) == RX_OK, "bind"); 2205 RxReactionDesc d; 2206 desc_init(&d, "note-semantic", RX_FACULTY_AIEN, SUBJ_AIEN, fn_note, NULL); 2207 add_trigger(&d, obj, RX_FIELD(0)); 2208 add_write(&d, g_note_out, RX_FIELD(0)); 2209 add_cap(&d, rd, RES_SENSOR, RX_RIGHT_READ); 2210 add_cap(&d, wr, RES_BELIEF, RX_RIGHT_WRITE); 2211 uint32_t rid = 0; 2212 CHECK(rx_world_add_reaction(&e.w, &d, &rid) == RX_OK, "add"); 2213 RxObject before; 2214 CHECK(rx_world_read(&e.w, obj, &before) == RX_OK, "before"); 2215 int64_t cid = stimulus(&e, cap, obj, 0, 5); 2216 CHECK(cid > 0, "stimulus"); 2217 CHECK(rx_world_wait_quiescent(&e.w, 3000) == RX_OK, "quiet"); 2218 CHECK(field(&e, g_note_out, 0) == 6, "host reaction did not see the semantic change"); 2219 CHECK(e.w.reactions[rid].commits == 1, "host reaction did not publish"); 2220 uint32_t fault = 0; 2221 CHECK(rx_world_take_publication(&e.w, NULL, &fault) == RX_ERR_NOT_FOUND, 2222 "a ring slot appeared for an object with no physical copy"); 2223 RxObject after; 2224 CHECK(rx_world_read(&e.w, obj, &after) == RX_OK, "after"); 2225 CHECK(after.id == before.id && after.generation == before.generation && after.version > before.version, 2226 "semantic publication changed the object's name"); 2227 CHECK(!after.placed, "publication invented a placement"); 2228 audit_and_close(&e); 2229 } 2230 2231 static int same_bytes(const uint8_t *a, const uint8_t *b, size_t n) { 2232 return memcmp(a, b, n) == 0; 2233 } 2234 2235 static void t_physical_realization(void) { 2236 begin("physical_realization", "attach, detach, relocate; identity stays"); 2237 Env e; 2238 CHECK(env_start(&e, 1) == 0, "setup"); 2239 RxObjRef obj = mkobj(&e, RES_SENSOR, 4); 2240 RxObject sem; 2241 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "read"); 2242 uint8_t digest[32]; 2243 memcpy(digest, sem.digest, 32); 2244 uint8_t world0[32], world1[32]; 2245 rx_world_digest(&e.w, world0); 2246 RxObjRef stale = obj; 2247 stale.generation = 0; 2248 CHECK(rx_world_attach_physical(&e.w, stale) == RX_ERR_STALE_GEN, "generation 0 was attached"); 2249 stale.generation = obj.generation + 9; 2250 CHECK(rx_world_attach_physical(&e.w, stale) == RX_ERR_STALE_GEN, "future generation was attached"); 2251 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK && !sem.placed, "a refused attach placed the object"); 2252 CHECK(same_bytes(sem.digest, digest, 32), "refused attach changed the content"); 2253 2254 CHECK(rx_world_attach_physical(&e.w, obj) == RX_OK, "attach"); 2255 CHECK(rx_world_attach_physical(&e.w, obj) == RX_ERR_EXISTS, "second physical copy was allowed"); 2256 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "read placed"); 2257 CHECK(sem.generation == obj.generation && same_bytes(sem.digest, digest, 32), 2258 "attach changed identity or content"); 2259 OmegaSharedWorldObject phys; 2260 CHECK(rx_world_physical(&e.w, obj, &phys) == RX_OK, "physical"); 2261 CHECK(phys.object_id == obj.id && phys.generation == obj.generation && phys.permissions == 0, 2262 "projection name"); 2263 uint64_t off0 = phys.region_offset; 2264 2265 CHECK(rx_world_relocate_physical(&e.w, obj) == RX_OK, "relocate"); 2266 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "read moved"); 2267 CHECK(rx_world_physical(&e.w, obj, &phys) == RX_OK, "physical moved"); 2268 CHECK(phys.region_offset != off0 && phys.region_offset == sem.region_offset, "bytes did not move"); 2269 CHECK(phys.object_id == obj.id && phys.generation == obj.generation, "move renamed the object"); 2270 CHECK(sem.version == 1 && same_bytes(sem.digest, digest, 32), "move changed the content"); 2271 rx_world_digest(&e.w, world1); 2272 CHECK(same_bytes(world0, world1, 32), "move changed the world content"); 2273 CHECK(field(&e, obj, 0) == 4, "move changed a field"); 2274 2275 CHECK(rx_world_place_physical(&e.w, obj, UINT64_MAX, RX_OBJECT_WINDOW) == RX_ERR_BOUNDS, 2276 "offset at the top of the range was accepted"); 2277 CHECK(rx_world_place_physical(&e.w, obj, sem.region_offset, UINT64_MAX) == RX_ERR_BOUNDS, 2278 "length at the top of the range was accepted"); 2279 CHECK(rx_world_place_physical(&e.w, obj, UINT64_MAX - 8u, RX_OBJECT_WINDOW) == RX_ERR_BOUNDS, 2280 "wrapping offset plus length was accepted"); 2281 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK && sem.region_offset == phys.region_offset && 2282 same_bytes(sem.digest, digest, 32), 2283 "a refused move changed the object"); 2284 2285 OmegaSharedWorldObject lie; 2286 CHECK(rx_world_physical(&e.w, obj, &lie) == RX_OK, "lie source"); 2287 lie.state = OMEGA_SW_OBJ_REVOKED; 2288 CHECK(rx_world_overwrite_physical(&e.w, obj.id, &lie) == RX_OK, "poke liveness"); 2289 RxCapRef cap = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_READ | RX_RIGHT_WRITE); 2290 CHECK(rx_world_bind_capability(&e.w, obj, cap) == RX_OK, "bind"); 2291 int64_t cid = stimulus(&e, cap, obj, 0, 4); 2292 CHECK(cid > 0, "stimulus"); 2293 CHECK(rx_world_wait_quiescent(&e.w, 2000) == RX_OK, "quiet"); 2294 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "read after poke"); 2295 OmegaSharedWorldDesc bad = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2296 uint32_t fault = 0; 2297 CHECK(inject_take(&e.w, &bad, &fault) == RX_ERR_STALE_GEN, "liveness disagreement was accepted"); 2298 CHECK(fault == RX_FAULT_DIVERGED, "disagreement was not reported"); 2299 RxObject still; 2300 CHECK(rx_world_read(&e.w, obj, &still) == RX_OK, "read after disagreement"); 2301 CHECK(still.generation == sem.generation && still.version == sem.version && 2302 still.field[0] == sem.field[0], 2303 "disagreement changed the object"); 2304 lie.state = OMEGA_SW_OBJ_ACTIVE; 2305 lie.generation = sem.generation; 2306 lie.permissions = 0; 2307 CHECK(rx_world_overwrite_physical(&e.w, obj.id, &lie) == RX_OK, "restore liveness"); 2308 2309 CHECK(rx_world_detach_physical(&e.w, stale) == RX_ERR_STALE_GEN, "stale detach"); 2310 CHECK(rx_world_detach_physical(&e.w, obj) == RX_OK, "detach"); 2311 CHECK(rx_world_detach_physical(&e.w, obj) == RX_ERR_UNPLACED, "second detach"); 2312 CHECK(rx_world_physical(&e.w, obj, &phys) == RX_ERR_UNPLACED, "detached object still resolved"); 2313 CHECK(rx_world_read(&e.w, obj, &still) == RX_OK && still.generation == obj.generation && 2314 still.field[0] == 4, 2315 "detach changed the semantic object"); 2316 OmegaSharedWorldDesc gone = make_pub(&e.w, &still, cap, (uint64_t)cid); 2317 CHECK(inject_take(&e.w, &gone, &fault) == RX_ERR_UNPLACED, "detached realization was accepted"); 2318 CHECK(rx_world_read(&e.w, obj, &still) == RX_OK && still.field[0] == 4 && still.version == sem.version, 2319 "rejected descriptor changed the object"); 2320 CHECK(rx_world_attach_physical(&e.w, obj) == RX_OK, "reattach"); 2321 CHECK(rx_world_physical(&e.w, obj, &phys) == RX_OK && phys.object_id == obj.id && 2322 phys.generation == obj.generation, 2323 "reattach used a new name"); 2324 audit_and_close(&e); 2325 } 2326 2327 static void t_cross_engine(void) { 2328 begin("cross_engine_descriptor_cpu", "pointer-free descriptor, hostile input rejected"); 2329 Env e; 2330 CHECK(env_start(&e, 1) == 0, "setup"); 2331 RxObjRef obj = mkobj(&e, RES_SENSOR, 1); 2332 CHECK(rx_world_attach_physical(&e.w, obj) == RX_OK, "attach"); 2333 RxCapRef cap = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_READ | RX_RIGHT_WRITE); 2334 RxCapRef other = mint(&e, SUBJ_EXTERNAL, RES_BELIEF, RX_RIGHT_READ | RX_RIGHT_WRITE); 2335 CHECK(rx_world_bind_capability(&e.w, obj, cap) == RX_OK, "bind"); 2336 int64_t cid = stimulus(&e, cap, obj, 0, 9); 2337 CHECK(cid > 0, "stimulus"); 2338 CHECK(rx_world_wait_quiescent(&e.w, 2000) == RX_OK, "quiet"); 2339 OmegaSharedWorldDesc good; 2340 uint32_t fault = 99; 2341 CHECK(rx_world_take_publication(&e.w, &good, &fault) == RX_OK, "good descriptor rejected"); 2342 CHECK(good.msg_type == RX_RING_PUBLISH, "message kind"); 2343 CHECK(good.object_offset == 0 && good.object_length == RX_OBJECT_WINDOW, "window"); 2344 CHECK((good.flags & OMEGA_SW_FLAG_CHECKSUM) != 0, "checksum missing"); 2345 2346 RxObject sem; 2347 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "read"); 2348 OmegaSharedWorldObject poked; 2349 CHECK(rx_world_physical(&e.w, obj, &poked) == RX_OK, "physical"); 2350 poked.permissions = OMEGA_SW_PERM_READ | OMEGA_SW_PERM_WRITE; 2351 CHECK(rx_world_overwrite_physical(&e.w, obj.id, &poked) == RX_OK, "poke permissions"); 2352 OmegaSharedWorldDesc again = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2353 CHECK(inject_take(&e.w, &again, &fault) == RX_OK, "permission bits were treated as authority"); 2354 2355 poked.generation = sem.generation + 9; 2356 CHECK(rx_world_overwrite_physical(&e.w, obj.id, &poked) == RX_OK, "poke generation"); 2357 OmegaSharedWorldDesc lied = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2358 CHECK(inject_take(&e.w, &lied, &fault) == RX_ERR_STALE_GEN, "edited generation became a second object"); 2359 poked.generation = sem.generation; 2360 poked.permissions = 0; 2361 CHECK(rx_world_overwrite_physical(&e.w, obj.id, &poked) == RX_OK, "restore"); 2362 2363 OmegaSharedWorldDesc torn = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2364 torn.payload[30] ^= 0x5a; 2365 CHECK(inject_take(&e.w, &torn, &fault) == RX_ERR_TORN, "torn descriptor accepted"); 2366 OmegaSharedWorldDesc oldver = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2367 st64(oldver.payload + 8, 0); 2368 rx_world_seal_descriptor(&oldver); 2369 CHECK(inject_take(&e.w, &oldver, &fault) == RX_ERR_TORN, "stale publication version accepted"); 2370 2371 OmegaSharedWorldDesc replay = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2372 replay.sequence = rx_world_publication_tail(&e.w) - 1; 2373 rx_world_seal_descriptor(&replay); 2374 CHECK(inject_take(&e.w, &replay, &fault) == RX_ERR_REPLAY, "replayed sequence accepted"); 2375 OmegaSharedWorldDesc skip = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2376 skip.sequence += 3; 2377 rx_world_seal_descriptor(&skip); 2378 CHECK(inject_take(&e.w, &skip, &fault) == RX_ERR_REPLAY, "skipped sequence accepted"); 2379 2380 OmegaSharedWorldDesc wide = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2381 wide.object_length = (uint32_t)sem.size_bytes + 1u; 2382 rx_world_seal_descriptor(&wide); 2383 CHECK(inject_take(&e.w, &wide, &fault) == RX_ERR_BOUNDS, "length past the object accepted"); 2384 OmegaSharedWorldDesc call = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2385 call.msg_type = OMEGA_SW_MSG_XFORM_REQ; 2386 rx_world_seal_descriptor(&call); 2387 CHECK(inject_take(&e.w, &call, &fault) == RX_ERR_BAD_DESC, "transform call accepted on the reaction ring"); 2388 OmegaSharedWorldDesc magic = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2389 magic.magic = 0; 2390 CHECK(inject_take(&e.w, &magic, &fault) == RX_ERR_BAD_DESC, "bad magic accepted"); 2391 2392 OmegaSharedWorldDesc wrong = make_pub(&e.w, &sem, other, (uint64_t)cid); 2393 CHECK(inject_take(&e.w, &wrong, &fault) == RX_ERR_AUTHORITY, "a different capability was accepted"); 2394 CHECK(revoke_cap(&e.admin, cap) == RX_CAP_OK, "revoke"); 2395 OmegaSharedWorldDesc dead = make_pub(&e.w, &sem, cap, (uint64_t)cid); 2396 CHECK(inject_take(&e.w, &dead, &fault) == RX_ERR_AUTHORITY, "revoked capability was accepted"); 2397 2398 /* Re-bind a live capability so the remaining refusals are about the 2399 * descriptor, and confirm each refusal leaves the object untouched. */ 2400 RxCapRef fresh = mint(&e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_READ | RX_RIGHT_WRITE); 2401 CHECK(rx_world_bind_capability(&e.w, obj, fresh) == RX_OK, "rebind"); 2402 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "reread for hostile set"); 2403 cid = stimulus(&e, fresh, obj, 0, 11); 2404 CHECK(cid > 0, "second stimulus"); 2405 CHECK(rx_world_wait_quiescent(&e.w, 2000) == RX_OK, "quiet after second stimulus"); 2406 while (rx_world_take_publication(&e.w, NULL, &fault) == RX_OK) {} 2407 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK, "baseline"); 2408 2409 OmegaSharedWorldDesc huge = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2410 huge.object_id = UINT32_MAX; 2411 rx_world_seal_descriptor(&huge); 2412 CHECK(inject_take(&e.w, &huge, &fault) == RX_ERR_BOUNDS, "object id at the top of the range was accepted"); 2413 2414 OmegaSharedWorldDesc zero = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2415 zero.object_generation = 0; 2416 rx_world_seal_descriptor(&zero); 2417 CHECK(inject_take(&e.w, &zero, &fault) == RX_ERR_STALE_GEN, "generation zero was accepted"); 2418 2419 OmegaSharedWorldDesc edge = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2420 edge.object_offset = (uint32_t)sem.size_bytes - 1u; 2421 edge.object_length = 1; 2422 rx_world_seal_descriptor(&edge); 2423 CHECK(inject_take(&e.w, &edge, &fault) == RX_OK, "last byte of the object was refused"); 2424 2425 OmegaSharedWorldDesc past = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2426 past.object_offset = (uint32_t)sem.size_bytes; 2427 past.object_length = 1; 2428 rx_world_seal_descriptor(&past); 2429 CHECK(inject_take(&e.w, &past, &fault) == RX_ERR_BOUNDS, "one byte past the object was accepted"); 2430 2431 OmegaSharedWorldDesc offmax = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2432 offmax.object_offset = UINT32_MAX; 2433 offmax.object_length = 1; 2434 rx_world_seal_descriptor(&offmax); 2435 CHECK(inject_take(&e.w, &offmax, &fault) == RX_ERR_BOUNDS, "maximum offset was accepted"); 2436 2437 OmegaSharedWorldDesc lenmax = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2438 lenmax.object_offset = 0; 2439 lenmax.object_length = UINT32_MAX; 2440 rx_world_seal_descriptor(&lenmax); 2441 CHECK(inject_take(&e.w, &lenmax, &fault) == RX_ERR_BOUNDS, "maximum length was accepted"); 2442 2443 OmegaSharedWorldDesc wrap = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2444 wrap.object_offset = 0xFFFFFFF0u; 2445 wrap.object_length = 0x20u; 2446 rx_world_seal_descriptor(&wrap); 2447 CHECK(inject_take(&e.w, &wrap, &fault) == RX_ERR_BOUNDS, "wrapping offset plus length was accepted"); 2448 2449 OmegaSharedWorldDesc kind = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2450 kind.msg_type = 0x00FFu; 2451 rx_world_seal_descriptor(&kind); 2452 CHECK(inject_take(&e.w, &kind, &fault) == RX_ERR_BAD_DESC, "unknown ring kind was accepted"); 2453 2454 OmegaSharedWorldDesc keep = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2455 keep.msg_type = RX_RING_KEEPALIVE; 2456 rx_world_seal_descriptor(&keep); 2457 uint64_t ver = sem.version; 2458 CHECK(inject_take(&e.w, &keep, &fault) == RX_OK, "keepalive was treated as a call"); 2459 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK && sem.version == ver, "keepalive changed the object"); 2460 2461 OmegaSharedWorldDesc down = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2462 down.msg_type = RX_RING_SHUTDOWN; 2463 rx_world_seal_descriptor(&down); 2464 CHECK(inject_take(&e.w, &down, &fault) == RX_OK, "shutdown notice was refused"); 2465 CHECK(rx_world_read(&e.w, obj, &sem) == RX_OK && sem.version == ver && sem.field[0] == 11, 2466 "shutdown notice changed the object"); 2467 2468 uint32_t old = obj.generation; 2469 uint64_t held = sem.field[0]; 2470 CHECK(rx_world_retire(&e.w, obj) == RX_OK, "retire for revoked descriptor"); 2471 OmegaSharedWorldDesc revoked = make_pub(&e.w, &sem, fresh, (uint64_t)cid); 2472 revoked.object_generation = old; 2473 revoked.sequence = rx_world_publication_tail(&e.w); 2474 rx_world_seal_descriptor(&revoked); 2475 CHECK(inject_take(&e.w, &revoked, &fault) == RX_ERR_STALE_GEN, "revoked generation was accepted"); 2476 OmegaSharedWorldDesc deadobj = revoked; 2477 deadobj.object_generation = e.w.objects[obj.id].generation; 2478 deadobj.sequence = rx_world_publication_tail(&e.w); 2479 rx_world_seal_descriptor(&deadobj); 2480 CHECK(inject_take(&e.w, &deadobj, &fault) == RX_ERR_BOUNDS, "revoked object was accepted"); 2481 CHECK(e.w.objects[obj.id].field[0] == held && e.w.objects[obj.id].generation == old + 1, 2482 "revoked descriptor changed the object"); 2483 2484 RxObject tail; 2485 CHECK(rx_world_read(&e.w, (RxObjRef){ obj.id, old }, &tail) == RX_ERR_STALE_GEN, 2486 "host accepted the retired name after the hostile set"); 2487 audit_and_close(&e); 2488 } 2489 2490 /* ---- receipt -------------------------------------------------------------- */ 2491 2492 static void write_receipt(int total_checks, int total_fail, const char *binary_digest) { 2493 char path[512]; 2494 if (omega_evidence_path("R3/rx_heartbeat_receipt.json", path, sizeof(path)) != 0) { 2495 fprintf(stderr, "receipt: refused (path exists in record mode?)\n"); 2496 return; 2497 } 2498 FILE *f = fopen(path, "w"); 2499 if (!f) { perror("receipt"); return; } 2500 char commit[41] = "unknown"; 2501 omega_evidence_run_commit(commit); 2502 const char *cand = getenv("OMEGA_CANDIDATE_COMMIT"); 2503 struct utsname u; 2504 uname(&u); 2505 int tests_passed = 0; 2506 for (int i = 0; i < g_ntests; i++) if (g_tests[i].failures == 0) tests_passed++; 2507 bool all = total_fail == 0 && tests_passed == g_ntests; 2508 time_t now = time(NULL); 2509 char ts[32]; 2510 strftime(ts, sizeof(ts), "%Y-%m-%dT%H:%M:%SZ", gmtime(&now)); 2511 2512 fprintf(f, "{\n \"schema\": \"AIEN_RX_HEARTBEAT_RECEIPT_V1\",\n"); 2513 fprintf(f, " \"adr\": \"aien-architecture ADR 0016\",\n"); 2514 fprintf(f, " \"run_id\": \"%s\",\n \"timestamp_utc\": \"%s\",\n", omega_evidence_run_id(), ts); 2515 fprintf(f, " \"candidate_commit\": %s%s%s,\n \"candidate_bound\": %s,\n", 2516 cand ? "\"" : "", cand ? cand : "null", cand ? "\"" : "", cand ? "true" : "false"); 2517 fprintf(f, " \"run_commit\": \"%s\",\n \"tree_dirty\": %s,\n", commit, 2518 omega_evidence_tree_dirty() ? "true" : "false"); 2519 fprintf(f, " \"test_binary_sha256\": \"%s\",\n", binary_digest); 2520 fprintf(f, " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\"},\n", 2521 u.sysname, u.release, u.machine); 2522 const char *physics = getenv("PHYSICS_COMMIT"); 2523 fprintf(f, " \"physics_commit\": %s%s%s,\n", 2524 physics ? "\"" : "", physics ? physics : "null", physics ? "\"" : ""); 2525 fprintf(f, " \"hardware_scope\": \"host/reference; no silicon claim\",\n"); 2526 fprintf(f, " \"gates\": {\n"); 2527 int r5_ok = all; 2528 int r6_ok = all; 2529 for (int i = 0; i < g_ntests; i++) { 2530 if (strcmp(g_tests[i].name, "resource_admission_1000") == 0 || 2531 strcmp(g_tests[i].name, "zero_resource_budget_is_zero") == 0 || 2532 strcmp(g_tests[i].name, "background_not_starved") == 0 || 2533 strcmp(g_tests[i].name, "priority_ladder") == 0) 2534 if (g_tests[i].failures) r5_ok = 0; 2535 if ((strcmp(g_tests[i].name, "stability_containment") == 0 || 2536 strcmp(g_tests[i].name, "periodic_versus_oscillation") == 0 || 2537 strcmp(g_tests[i].name, "episode_activation_budget") == 0) && g_tests[i].failures) 2538 r6_ok = 0; 2539 } 2540 fprintf(f, " \"R3_REACTION_CORE\": \"%s\",\n", all ? "PASS (host reference)" : "FAIL"); 2541 fprintf(f, " \"R4_CAUSAL_TRACE\": \"%s\",\n", all ? "PASS (host reference; every reaction crumb in every test audited)" : "FAIL"); 2542 fprintf(f, " \"R5_RESOURCE_ARBITRATION\": \"%s\",\n", 2543 r5_ok ? "PASS (host reference; not silicon)" : "FAIL"); 2544 fprintf(f, " \"R6_REACTION_STABILITY\": \"%s\",\n", 2545 r6_ok ? "PASS (host reference; not silicon)" : "FAIL"); 2546 int r1_ok = all, r2_ok = all; 2547 for (int i = 0; i < g_ntests; i++) { 2548 if ((strcmp(g_tests[i].name, "canonical_object_one_identity") == 0 || 2549 strcmp(g_tests[i].name, "identity_space_is_closed") == 0 || 2550 strcmp(g_tests[i].name, "semantic_commit_without_transport") == 0 || 2551 strcmp(g_tests[i].name, "physical_realization") == 0) && g_tests[i].failures) 2552 r1_ok = 0; 2553 if (strcmp(g_tests[i].name, "cross_engine_descriptor_cpu") == 0 && g_tests[i].failures) 2554 r2_ok = 0; 2555 } 2556 fprintf(f, " \"R1_CANONICAL_WORLD\": \"%s\",\n", 2557 r1_ok ? "PASS (host/reference; no silicon claim)" : "FAIL"); 2558 fprintf(f, " \"R1_CANONICAL_SHARED_WORLD_PASS\": \"%s\",\n", 2559 r1_ok ? "PASS (host/reference; no silicon claim)" : "FAIL"); 2560 fprintf(f, " \"R2_CROSS_ENGINE_ABI\": \"%s\",\n", 2561 r2_ok ? "PASS (CPU/reference; no GB10 claim; no resident GPU claim)" : "FAIL"); 2562 fprintf(f, " \"R2_CROSS_ENGINE_ABI_PASS\": \"%s\",\n", 2563 r2_ok ? "PASS (CPU/reference; no GB10 claim; no resident GPU claim)" : "FAIL"); 2564 fprintf(f, " \"authority_host_reference\": \"%s\",\n", 2565 all ? "hardened; R7 native root NOT claimed" : "FAIL"); 2566 fprintf(f, " \"not_claimed\": [\"R7 native\", \"R8\", \"R9\", \"R10\", \"R11\", \"R12\", \"R13+\"]\n },\n"); 2567 fprintf(f, " \"tests\": [\n"); 2568 for (int i = 0; i < g_ntests; i++) 2569 fprintf(f, " {\"name\": \"%s\", \"invariants\": \"%s\", \"checks\": %d, \"failures\": %d}%s\n", 2570 g_tests[i].name, g_tests[i].invariants, g_tests[i].checks, g_tests[i].failures, 2571 i + 1 < g_ntests ? "," : ""); 2572 fprintf(f, " ],\n"); 2573 fprintf(f, " \"totals\": {\"tests\": %d, \"tests_passed\": %d, \"checks\": %d, \"failures\": %d},\n", 2574 g_ntests, tests_passed, total_checks, total_fail); 2575 fprintf(f, " \"causal\": {\"crumbs_digest_verified\": %llu, \"reaction_crumbs_audited\": %llu},\n", 2576 (unsigned long long)g_crumbs_verified, (unsigned long long)g_crumbs_audited); 2577 fprintf(f, " \"lifecycle\": {\"illegal_transitions\": %llu, \"observed\": [", 2578 (unsigned long long)g_illegal_transitions); 2579 bool first = true; 2580 for (int a = 0; a < RX_STATE_COUNT; a++) 2581 for (int b = 0; b < RX_STATE_COUNT; b++) 2582 if (g_transitions[a][b]) { 2583 fprintf(f, "%s\n {\"from\": \"%s\", \"to\": \"%s\", \"count\": %llu}", first ? "" : ",", 2584 rx_state_name((RxState)a), rx_state_name((RxState)b), 2585 (unsigned long long)g_transitions[a][b]); 2586 first = false; 2587 } 2588 fprintf(f, "\n ]},\n"); 2589 fprintf(f, " \"heartbeat_trace\": %s\n}\n", g_heartbeat_trace[0] ? g_heartbeat_trace : "null"); 2590 fclose(f); 2591 printf("[*] receipt: %s\n", path); 2592 } 2593 2594 static void binary_digest(const char *argv0, char out[65]) { 2595 strcpy(out, "unavailable"); 2596 FILE *f = fopen("/proc/self/exe", "rb"); 2597 if (!f) f = fopen(argv0, "rb"); 2598 if (!f) return; 2599 sha256_ctx c; 2600 sha256_init(&c); 2601 uint8_t buf[65536]; 2602 size_t n; 2603 while ((n = fread(buf, 1, sizeof(buf), f)) > 0) sha256_update(&c, buf, n); 2604 fclose(f); 2605 uint8_t d[32]; 2606 sha256_final(&c, d); 2607 for (int i = 0; i < 32; i++) sprintf(&out[i * 2], "%02x", d[i]); 2608 } 2609 2610 int main(int argc, char **argv) { 2611 (void)argc; 2612 signal(SIGPIPE, SIG_IGN); 2613 printf("AIEN resident reaction runtime -- heartbeat qualification (ADR 0016, R3/R4 host)\n"); 2614 t_heartbeat(); 2615 t_single_dependency_narrow(); 2616 t_multiple_dependency(); 2617 t_fan_out(); 2618 t_fan_in(); 2619 t_invalidation(); 2620 t_read_only_invalidation(); 2621 t_concurrent_publication(); 2622 t_stale_input(); 2623 t_write_set_enforced(); 2624 t_duplicate_wake(); 2625 t_capability_attacks(); 2626 t_revoke_during_run(); 2627 t_root_table_unwritable(); 2628 t_crumb_tamper_detected(); 2629 t_crumb_cap_generation_64(); 2630 t_schedule_independence(); 2631 t_authority_office(); 2632 t_counter_fail_closed(); 2633 t_root_death_restart(); 2634 t_resource_admission(); 2635 t_zero_resource_budget(); 2636 t_background_not_starved(); 2637 t_priority_ladder(); 2638 t_stability(); 2639 t_periodic_and_backoff(); 2640 t_episode_budget(); 2641 t_semantic_without_transport(); 2642 t_one_identity(); 2643 t_identity_closed(); 2644 t_physical_realization(); 2645 t_cross_engine(); 2646 2647 int checks = 0, fails = 0; 2648 for (int i = 0; i < g_ntests; i++) { 2649 checks += g_tests[i].checks; 2650 fails += g_tests[i].failures; 2651 printf(" %-45s %s (%d checks)\n", g_tests[i].name, 2652 g_tests[i].failures ? "FAIL" : "PASS", g_tests[i].checks); 2653 } 2654 char dg[65]; 2655 binary_digest(argv[0], dg); 2656 printf("TOTAL: %d tests, %d checks, %d failures; %llu crumbs verified, %llu reaction crumbs audited, %llu illegal transitions\n", 2657 g_ntests, checks, fails, (unsigned long long)g_crumbs_verified, 2658 (unsigned long long)g_crumbs_audited, (unsigned long long)g_illegal_transitions); 2659 write_receipt(checks, fails, dg); 2660 printf("%s\n", fails == 0 2661 ? "R1_CANONICAL_WORLD: PASS (host CPU, not silicon) " 2662 "R2_CROSS_ENGINE_ABI: PASS (host CPU, not silicon) " 2663 "R3_REACTION_CORE: PASS (host) R4_CAUSAL_TRACE: PASS (host) " 2664 "R5_RESOURCE_ARBITRATION: PASS (host) R6_REACTION_STABILITY: PASS (host) " 2665 "R7: NOT CLAIMED" 2666 : "R1/R2/R3/R4/R5/R6: FAIL"); 2667 return fails == 0 ? 0 : 1; 2668 }