omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / runtime / rx_compose_attach_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * COMPOSITION-2 attach hygiene (rx_compose.h, attach and close in attach |
| 19 | * mode), in one caller-owned World that also runs an outside reaction: |
| 20 | * |
| 21 | * two per World two compositions attach to one World as instances 0 and 1 |
| 22 | * and run goals at the same time; each keeps the winner it |
| 23 | * verified (its own state, results, journal); a step under |
| 24 | * one instance's subject cannot write the other's objects |
| 25 | * with its own or the other's capabilities; a third attach, |
| 26 | * or a second one on a directory in use, is refused |
| 27 | * (RX_ERR_EXISTS) and changes nothing; after one closes, a |
| 28 | * new attach takes its instance and reuses its slots. |
| 29 | * close a candidate step is held mid-run and close is called: |
| 30 | * close revokes first and waits for the step (no cutoff); |
| 31 | * released, the step's write is REJECTED; close returns |
| 32 | * only after it finished; durable state and the Cortex |
| 33 | * record count are unchanged; the RxCompose is freed right |
| 34 | * after close (heap), so an ASan build catches any late use. |
| 35 | * reclaim 2000 attach/close rounds (a goal on every 50th): after every round the |
| 36 | * World's footprint (rx_world_footprint) is what it was |
| 37 | * before the round, the reaction table does not grow after |
| 38 | * the first round, and every attach gets the same 13 |
| 39 | * Aienux capability slots back (close reclaimed them). |
| 40 | * full a World with no room for four more reactions refuses the |
| 41 | * attach (RX_ERR_FULL) before building anything. |
| 42 | */ |
| 43 | #include "rx_compose_fixture.h" |
| 44 | |
| 45 | #include <ftw.h> |
| 46 | #include <pthread.h> |
| 47 | #include <sys/stat.h> |
| 48 | #include <time.h> |
| 49 | #include <unistd.h> |
| 50 | |
| 51 | static int g_checks, g_fail; |
| 52 | |
| 53 | #define CHECK(cond, ...) do { \ |
| 54 | g_checks++; \ |
| 55 | if (!(cond)) { \ |
| 56 | g_fail++; \ |
| 57 | fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ |
| 58 | fprintf(stderr, __VA_ARGS__); \ |
| 59 | fputc('\n', stderr); \ |
| 60 | } \ |
| 61 | } while (0) |
| 62 | |
| 63 | #define EXT_SUBJ 100u |
| 64 | #define OUT_SUBJ 300u |
| 65 | #define RES_OUT_IN 0xD1000001ull |
| 66 | #define RES_OUT_OUT 0xD1000002ull |
| 67 | |
| 68 | static Fx g_fx; |
| 69 | static RxWorld g_w; /* large: static */ |
| 70 | static RxCompose g_c1, g_c2, g_c3; |
| 71 | static RxWorld g_w2; /* the full-table World */ |
| 72 | static AienosCapAdmin *g_admin; |
| 73 | static AienosCapView *g_view; |
| 74 | static char g_base[128]; |
| 75 | static RxObjRef g_oin, g_oout; |
| 76 | static RxCapRef g_cap_ext, g_cap_rin, g_cap_wout; |
| 77 | static uint32_t g_rx_out; |
| 78 | |
| 79 | static int rm_one(const char *p, const struct stat *sb, int flag, struct FTW *ftw) { |
| 80 | (void)sb; (void)flag; (void)ftw; |
| 81 | return remove(p); |
| 82 | } |
| 83 | static void rmtree(const char *p) { nftw(p, rm_one, 16, FTW_DEPTH | FTW_PHYS); } |
| 84 | static void dir_for(char *out, size_t n, const char *name) { |
| 85 | snprintf(out, n, "%s/%s", g_base, name); |
| 86 | rmtree(out); |
| 87 | } |
| 88 | static int ref_eq(JsBranchRef a, JsBranchRef b) { return a.id == b.id && a.gen == b.gen; } |
| 89 | static void sleep_us(long us) { |
| 90 | struct timespec ts = { us / 1000000, (us % 1000000) * 1000 }; |
| 91 | nanosleep(&ts, NULL); |
| 92 | } |
| 93 | |
| 94 | static int mint(uint32_t subject, uint64_t resource, uint32_t rights, RxCapRef *out) { |
| 95 | AienosCapRef office, ref = { UINT32_MAX, 0 }; |
| 96 | if (aienos_cap_office(g_admin, &office) != 0) return -1; |
| 97 | AienosCapMint m = { 3u, subject, resource, rights, 0, { UINT32_MAX, 0 }, office }; |
| 98 | if (aienos_cap_mint(g_admin, &m, &ref) != 0) return -1; |
| 99 | *out = (RxCapRef){ ref.cap_id, ref.generation }; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* The outside reaction: out.field0 = in.field0 + 1. */ |
| 104 | static int outside_fn(RxCtx *x) { |
| 105 | for (uint32_t i = 0; i < x->n_in; i++) |
| 106 | if (x->in[i].obj.id == g_oin.id && x->n_out < RX_MAX_MUTATIONS) |
| 107 | x->out[x->n_out++] = (RxMutation){ g_oout, 0, x->in[i].field[0] + 1 }; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int world_start(void) { |
| 112 | if (aienos_cap_start(&g_admin, &g_view) != 0) return -1; |
| 113 | if (rx_world_init_native(&g_w, g_view, 2, 1u << 20) != RX_OK) return -2; |
| 114 | g_w.external_subject = EXT_SUBJ; |
| 115 | uint64_t z[RX_MAX_FIELDS] = { 0 }; |
| 116 | if (rx_world_create(&g_w, 1, RX_PERSIST_RESIDENT, RES_OUT_IN, z, &g_oin) != RX_OK || |
| 117 | rx_world_create(&g_w, 1, RX_PERSIST_RESIDENT, RES_OUT_OUT, z, &g_oout) != RX_OK) |
| 118 | return -3; |
| 119 | if (mint(EXT_SUBJ, RES_OUT_IN, RX_RIGHT_WRITE, &g_cap_ext) || |
| 120 | mint(OUT_SUBJ, RES_OUT_IN, RX_RIGHT_READ, &g_cap_rin) || |
| 121 | mint(OUT_SUBJ, RES_OUT_OUT, RX_RIGHT_WRITE, &g_cap_wout)) |
| 122 | return -4; |
| 123 | RxReactionDesc d; |
| 124 | memset(&d, 0, sizeof d); |
| 125 | d.name = "outside.copy"; |
| 126 | d.faculty = RX_FACULTY_OMEGA; |
| 127 | d.subject = OUT_SUBJ; |
| 128 | d.priority = RX_PRIO_FOREGROUND; |
| 129 | d.triggers[d.n_triggers++] = (RxDep){ g_oin, RX_ALL_FIELDS }; |
| 130 | d.writes[d.n_writes++] = (RxDep){ g_oout, RX_ALL_FIELDS }; |
| 131 | d.caps[d.n_caps++] = (RxCapNeed){ g_cap_rin, RES_OUT_IN, RX_RIGHT_READ }; |
| 132 | d.caps[d.n_caps++] = (RxCapNeed){ g_cap_wout, RES_OUT_OUT, RX_RIGHT_WRITE }; |
| 133 | d.fn = outside_fn; |
| 134 | return rx_world_add_reaction(&g_w, &d, &g_rx_out) == RX_OK ? 0 : -5; |
| 135 | } |
| 136 | |
| 137 | /* Outside activity: publish v, the outside reaction answers v + 1. */ |
| 138 | static int outside_poke(uint64_t v) { |
| 139 | RxMutation m = { g_oin, 0, v }; |
| 140 | if (rx_world_publish_external(&g_w, g_cap_ext, &m, 1) < 0) return 0; |
| 141 | if (rx_world_wait_quiescent(&g_w, 10000) != RX_OK) return 0; |
| 142 | RxObject o; |
| 143 | return rx_world_read(&g_w, g_oout, &o) == RX_OK && o.field[0] == v + 1; |
| 144 | } |
| 145 | |
| 146 | static int attach(RxCompose *c, const char *dir) { |
| 147 | return rx_compose_attach(c, &g_w, NULL, dir, &g_fx.self, FX_SESSION, &g_fx.router, |
| 148 | fx_contract, g_admin); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static void footprint(RxFootprint *fp) { rx_world_footprint(&g_w, fp); } |
| 153 | |
| 154 | /* Every counter of the footprint equal; `slots` 0 skips the reaction-table |
| 155 | * size (the first attach of an instance grows it once, by design). */ |
| 156 | static int fp_same(const RxFootprint *a, const RxFootprint *b, int slots) { |
| 157 | return (!slots || (a->reaction_slots == b->reaction_slots && |
| 158 | a->reactions_removed == b->reactions_removed)) && |
| 159 | a->reactions_active == b->reactions_active && a->subscriptions == b->subscriptions && |
| 160 | a->objects_live == b->objects_live && a->binders == b->binders && |
| 161 | a->bound_fields == b->bound_fields && a->in_flight == b->in_flight && |
| 162 | a->fanout_backlog == b->fanout_backlog && a->deferred == b->deferred && |
| 163 | a->used_slots == b->used_slots && a->used_memory == b->used_memory && |
| 164 | a->used_energy == b->used_energy; |
| 165 | } |
| 166 | static void fp_print(const char *tag, const RxFootprint *f) { |
| 167 | fprintf(stderr, " %s: slots %u active %u removed %u subs %llu objs %u binders %u bound %u " |
| 168 | "inflight %u backlog %u deferred %u used %u/%llu/%llu\n", tag, f->reaction_slots, |
| 169 | f->reactions_active, f->reactions_removed, (unsigned long long)f->subscriptions, |
| 170 | f->objects_live, f->binders, f->bound_fields, f->in_flight, f->fanout_backlog, |
| 171 | f->deferred, f->used_slots, (unsigned long long)f->used_memory, |
| 172 | (unsigned long long)f->used_energy); |
| 173 | } |
| 174 | |
| 175 | /* ---- two compositions per World ------------------------------------------ */ |
| 176 | |
| 177 | typedef struct { |
| 178 | RxCompose *c; |
| 179 | uint64_t base; |
| 180 | uint32_t n, ok, chain_ok; |
| 181 | JsBranchRef last; |
| 182 | } Runner; |
| 183 | |
| 184 | /* n goals in a row on one composition; each must commit input*3+1 and build |
| 185 | * on the branch the previous goal committed. */ |
| 186 | static void *runner(void *arg) { |
| 187 | Runner *r = arg; |
| 188 | for (uint32_t i = 0; i < r->n; i++) { |
| 189 | RxcResult o; |
| 190 | if (fx_run(&g_fx, r->c, r->base + i, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && |
| 191 | o.result == (r->base + i) * 3 + 1 && o.winner < RXC_K) |
| 192 | r->ok++; |
| 193 | if (ref_eq(o.old_ref, r->last)) r->chain_ok++; |
| 194 | r->last = o.new_ref; |
| 195 | } |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | /* A rogue step: triggered by the outside object, writes field `field` of |
| 200 | * `target`, as `subject`, holding `cap` claimed on `resource`. */ |
| 201 | static uint64_t g_rogue_value; |
| 202 | static RxObjRef g_rogue_target; |
| 203 | static uint32_t g_rogue_field; |
| 204 | static int rogue_fn(RxCtx *x) { |
| 205 | if (x->n_out < RX_MAX_MUTATIONS) |
| 206 | x->out[x->n_out++] = (RxMutation){ g_rogue_target, g_rogue_field, g_rogue_value }; |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /* RX_OK if the rogue's write was refused (at registration or at run) and the |
| 211 | * target field kept its value; otherwise a description in *why. */ |
| 212 | static int rogue_refused(uint32_t subject, RxCapRef cap, uint64_t resource, RxObjRef target, |
| 213 | uint32_t field, const char **why) { |
| 214 | static char buf[96]; |
| 215 | RxObject before, after; |
| 216 | if (rx_world_read(&g_w, target, &before) != RX_OK) { *why = "target unreadable"; return -1; } |
| 217 | /* The rogue's trigger read is honest (its own READ right on the outside |
| 218 | * object), so the only authority in question is the write. */ |
| 219 | RxCapRef rd; |
| 220 | if (mint(subject, RES_OUT_IN, RX_RIGHT_READ, &rd)) { *why = "mint read right"; return -1; } |
| 221 | RxReactionDesc d; |
| 222 | memset(&d, 0, sizeof d); |
| 223 | d.name = "compose.cross.rogue"; |
| 224 | d.faculty = RX_FACULTY_OMEGA; |
| 225 | d.subject = subject; |
| 226 | d.priority = RX_PRIO_FOREGROUND; |
| 227 | d.triggers[d.n_triggers++] = (RxDep){ g_oin, RX_ALL_FIELDS }; |
| 228 | d.writes[d.n_writes++] = (RxDep){ target, RX_FIELD(field) }; |
| 229 | d.caps[d.n_caps++] = (RxCapNeed){ rd, RES_OUT_IN, RX_RIGHT_READ }; |
| 230 | d.caps[d.n_caps++] = (RxCapNeed){ cap, resource, RX_RIGHT_WRITE }; |
| 231 | d.fn = rogue_fn; |
| 232 | g_rogue_target = target; |
| 233 | g_rogue_field = field; |
| 234 | g_rogue_value = before.field[field] ^ 0x5A5Aull; |
| 235 | uint32_t rid; |
| 236 | int rc = rx_world_add_reaction(&g_w, &d, &rid), kind = 0, reason = 0; |
| 237 | if (rc == RX_OK) { |
| 238 | static uint64_t poke = 9000; /* a new value each time: a change wakes */ |
| 239 | (void)outside_poke(poke++); |
| 240 | pthread_mutex_lock(&g_w.mu); |
| 241 | const RxCrumb *k = rx_world_crumb(&g_w, g_w.reactions[rid].last_crumb); |
| 242 | kind = k ? (int)k->kind : -1; |
| 243 | reason = k ? (int)k->reason : 0; |
| 244 | pthread_mutex_unlock(&g_w.mu); |
| 245 | while (rx_world_remove_reaction(&g_w, rid) == RX_ERR_BUSY) sleep_us(1000); |
| 246 | } |
| 247 | AienosCapRef office; |
| 248 | if (aienos_cap_office(g_admin, &office) == 0) { |
| 249 | (void)aienos_cap_revoke(g_admin, office, (AienosCapRef){ rd.cap_id, rd.generation }); |
| 250 | (void)aienos_cap_reclaim(g_admin, office, rd.cap_id); |
| 251 | } |
| 252 | if (rx_world_read(&g_w, target, &after) != RX_OK) { *why = "target gone"; return -1; } |
| 253 | if (after.field[field] != before.field[field]) { *why = "target field changed"; return -1; } |
| 254 | if (rc != RX_OK) { |
| 255 | snprintf(buf, sizeof buf, "refused at registration (%d)", rc); |
| 256 | printf(" rogue as subject %u: %s\n", subject, buf); |
| 257 | *why = buf; |
| 258 | return RX_OK; |
| 259 | } |
| 260 | snprintf(buf, sizeof buf, "crumb kind %d reason %d", kind, reason); |
| 261 | printf(" rogue as subject %u: %s\n", subject, buf); |
| 262 | *why = buf; |
| 263 | return kind == RX_CRUMB_BLOCKED_AUTHORITY || kind == RX_CRUMB_REJECTED ? RX_OK : -1; |
| 264 | } |
| 265 | |
| 266 | static void t_two_per_world(void) { |
| 267 | printf("[*] two compositions per World: isolated, concurrent, third refused\n"); |
| 268 | char d1[256], d2[256], d3[256]; |
| 269 | dir_for(d1, sizeof d1, "one"); |
| 270 | dir_for(d2, sizeof d2, "two"); |
| 271 | dir_for(d3, sizeof d3, "three"); |
| 272 | RxFootprint fp0, fp1, fp2; |
| 273 | footprint(&fp0); |
| 274 | RxcResult o; |
| 275 | CHECK(attach(&g_c1, d1) == RX_OK, "first attach"); |
| 276 | CHECK(attach(&g_c2, d2) == RX_OK, "second attach (other dir) runs alongside"); |
| 277 | CHECK(g_c1.inst == 0 && g_c2.inst == 1, "instances 0 and 1 (%u, %u)", g_c1.inst, g_c2.inst); |
| 278 | CHECK(g_c1.subj_commit == RXC_SUBJ_COMMIT && g_c2.subj_commit == RXC_SUBJ_COMMIT + 8u && |
| 279 | g_c2.res[4] == RXC_RES_STATE + 0x100u, "instance 1 has its own subjects and resources"); |
| 280 | footprint(&fp1); |
| 281 | CHECK(fp1.binders == 2 && fp1.bound_fields == 2 && fp1.reactions_active == fp0.reactions_active + 8 && |
| 282 | fp1.objects_live == fp0.objects_live + 10, "two binders, 8 reactions, 10 objects"); |
| 283 | CHECK(attach(&g_c3, d3) == RX_ERR_EXISTS, "third attach refused"); |
| 284 | CHECK(attach(&g_c3, d1) == RX_ERR_EXISTS, "attach on a directory in use refused"); |
| 285 | CHECK(g_c3.world == NULL, "refused attach left its RxCompose unattached"); |
| 286 | footprint(&fp2); |
| 287 | CHECK(fp_same(&fp1, &fp2, 1), "refused attaches changed nothing"); |
| 288 | |
| 289 | /* Goals at the same time on both; the outside reaction runs too. */ |
| 290 | Runner ra = { &g_c1, 1000, 40, 0, 0, rx_compose_state(&g_c1) }; |
| 291 | Runner rb = { &g_c2, 5000, 40, 0, 0, rx_compose_state(&g_c2) }; |
| 292 | pthread_t ta, tb; |
| 293 | CHECK(pthread_create(&ta, NULL, runner, &ra) == 0 && pthread_create(&tb, NULL, runner, &rb) == 0, |
| 294 | "runner threads"); |
| 295 | uint32_t pokes = 0; |
| 296 | for (uint32_t i = 0; i < 20; i++) pokes += (uint32_t)outside_poke(600 + i); |
| 297 | pthread_join(ta, NULL); |
| 298 | pthread_join(tb, NULL); |
| 299 | CHECK(pokes == 20, "outside reaction answered during both runs (%u/20)", pokes); |
| 300 | CHECK(ra.ok == ra.n && rb.ok == rb.n, "every concurrent goal committed its own result (%u, %u)", |
| 301 | ra.ok, rb.ok); |
| 302 | CHECK(ra.chain_ok == ra.n && rb.chain_ok == rb.n, |
| 303 | "each goal built on its own composition's last commit (%u, %u)", ra.chain_ok, rb.chain_ok); |
| 304 | CHECK(ref_eq(rx_compose_state(&g_c1), ra.last) && ref_eq(rx_compose_state(&g_c2), rb.last), |
| 305 | "each composition keeps the winner it verified"); |
| 306 | RxObject s1, s2; |
| 307 | CHECK(rx_world_read(&g_w, g_c1.state, &s1) == RX_OK && rx_world_read(&g_w, g_c2.state, &s2) == RX_OK && |
| 308 | s1.field[RXC_S_REF] == fx_pack(ra.last) && s2.field[RXC_S_REF] == fx_pack(rb.last), |
| 309 | "state objects name their own branch"); |
| 310 | CHECK(fx_count(&g_c1.cx, CX_K_PROMOTION, UINT64_MAX) == ra.n && fx_count(&g_c2.cx, CX_K_PROMOTION, UINT64_MAX) == rb.n, |
| 311 | "each journal holds only its own promotions (%u, %u)", |
| 312 | fx_count(&g_c1.cx, CX_K_PROMOTION, UINT64_MAX), fx_count(&g_c2.cx, CX_K_PROMOTION, UINT64_MAX)); |
| 313 | |
| 314 | /* Cross-talk: instance 1's steps against instance 0's objects. */ |
| 315 | const char *why = ""; |
| 316 | CHECK(rogue_refused(g_c2.subj_cand[0], g_c2.cap_cand[0][2], g_c2.res[1], g_c1.cand[0], RXC_C_REF, &why) == RX_OK, |
| 317 | "B candidate with its own right on its own resource cannot write A's candidate: %s", why); |
| 318 | CHECK(rogue_refused(g_c2.subj_cand[0], g_c2.cap_cand[0][2], g_c1.res[1], g_c1.cand[0], RXC_C_REF, &why) == RX_OK, |
| 319 | "B candidate claiming A's resource with its own right refused: %s", why); |
| 320 | CHECK(rogue_refused(g_c2.subj_cand[0], g_c1.cap_cand[0][2], g_c1.res[1], g_c1.cand[0], RXC_C_REF, &why) == RX_OK, |
| 321 | "B candidate presenting A's capability refused: %s", why); |
| 322 | CHECK(rogue_refused(g_c2.subj_commit, g_c1.cap_commit[1], g_c1.res[4], g_c1.state, RXC_S_REF, &why) == RX_OK, |
| 323 | "B commit presenting A's commit capability cannot move A's state: %s", why); |
| 324 | CHECK(rogue_refused(g_c1.subj_commit, g_c1.cap_commit[1], g_c2.res[4], g_c2.state, RXC_S_REF, &why) == RX_OK, |
| 325 | "A commit cannot move B's state: %s", why); |
| 326 | RxFootprint fpr; /* rogues removed: their slots wait for reuse */ |
| 327 | footprint(&fpr); |
| 328 | CHECK(fp_same(&fp1, &fpr, 0), "removed rogues left nothing active behind"); |
| 329 | CHECK(fx_run(&g_fx, &g_c1, 7, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && o.result == 22 && |
| 330 | ref_eq(o.old_ref, ra.last), "A still commits after the rogues (%d)", o.outcome); |
| 331 | JsBranchRef a_last = o.new_ref; |
| 332 | CHECK(fx_run(&g_fx, &g_c2, 8, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && o.result == 25 && |
| 333 | ref_eq(o.old_ref, rb.last), "B still commits after the rogues (%d)", o.outcome); |
| 334 | |
| 335 | /* Close A while B keeps running; a new attach takes instance 0 and its |
| 336 | * removed reaction slots, and recovers A's last commit. */ |
| 337 | rx_compose_close(&g_c1); |
| 338 | footprint(&fp2); |
| 339 | CHECK(fp2.binders == 1 && fp2.reactions_active == fp0.reactions_active + 4 && |
| 340 | fp2.objects_live == fp0.objects_live + 5 && fp2.reactions_removed == fpr.reactions_removed + 4, |
| 341 | "close of A took back its binder, reactions and objects"); |
| 342 | CHECK(fx_run(&g_fx, &g_c2, 9, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && o.result == 28, |
| 343 | "B commits after A closed (%d)", o.outcome); |
| 344 | CHECK(attach(&g_c3, d1) == RX_OK && g_c3.inst == 0, "reattach on A's directory gets instance 0"); |
| 345 | footprint(&fp2); |
| 346 | CHECK(fp2.reaction_slots == fpr.reaction_slots, "reattach reused the removed slots (%u vs %u)", |
| 347 | fp2.reaction_slots, fpr.reaction_slots); |
| 348 | CHECK(ref_eq(rx_compose_state(&g_c3), a_last), "reattach recovers A's last commit"); |
| 349 | CHECK(fx_run(&g_fx, &g_c3, 10, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && o.result == 31 && |
| 350 | ref_eq(o.old_ref, a_last), "goal after reattach commits (%d)", o.outcome); |
| 351 | rx_compose_close(&g_c3); |
| 352 | rx_compose_close(&g_c2); |
| 353 | footprint(&fp2); |
| 354 | CHECK(fp_same(&fp0, &fp2, 0), "after both close the footprint is what it was before"); |
| 355 | if (!fp_same(&fp0, &fp2, 0)) { fp_print("before", &fp0); fp_print("after", &fp2); } |
| 356 | CHECK(outside_poke(42), "World still works after close"); |
| 357 | rmtree(d1); |
| 358 | rmtree(d2); |
| 359 | rmtree(d3); |
| 360 | } |
| 361 | |
| 362 | /* ---- close waits for the composition's own steps -------------------------- */ |
| 363 | |
| 364 | static int g_closed; |
| 365 | static void *closer(void *arg) { |
| 366 | rx_compose_close(arg); |
| 367 | __atomic_store_n(&g_closed, 1, __ATOMIC_RELEASE); |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | static uint32_t in_flight(void) { |
| 372 | pthread_mutex_lock(&g_w.mu); |
| 373 | uint32_t n = g_w.in_flight; |
| 374 | pthread_mutex_unlock(&g_w.mu); |
| 375 | return n; |
| 376 | } |
| 377 | |
| 378 | /* Admitted activations parked behind a running writer of their inputs (I11: |
| 379 | * that writer's wake merges into them instead of a second commit). They count |
| 380 | * in in_flight but nothing of theirs runs. */ |
| 381 | static uint32_t upstream_held(void) { |
| 382 | pthread_mutex_lock(&g_w.mu); |
| 383 | uint32_t n = 0; |
| 384 | for (uint32_t i = 0; i < g_w.n_reactions; i++) |
| 385 | if (g_w.reactions[i].upstream_held) n++; |
| 386 | pthread_mutex_unlock(&g_w.mu); |
| 387 | return n; |
| 388 | } |
| 389 | |
| 390 | static void t_close_waits(void) { |
| 391 | printf("[*] close: revoke first, wait for the held step, its late write refused\n"); |
| 392 | char d[256]; |
| 393 | dir_for(d, sizeof d, "hold"); |
| 394 | RxCompose *c = calloc(1, sizeof *c); /* heap: freed right after close */ |
| 395 | if (!c) { CHECK(0, "calloc"); return; } |
| 396 | RxcResult o; |
| 397 | CHECK(attach(c, d) == RX_OK, "attach"); |
| 398 | CHECK(fx_run(&g_fx, c, 5, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED, "goal commits"); |
| 399 | JsBranchRef S1 = o.new_ref; |
| 400 | |
| 401 | /* Next goal straight into the World (as rx_compose_run publishes it), |
| 402 | * with candidate 0 held after its Skill ran, before it proposes. */ |
| 403 | c->test.hold_k1 = 1; |
| 404 | uint64_t sk[RXC_K] = { 0, 0 }; |
| 405 | for (uint32_t k = 0; k < c->n_routes && k < RXC_K; k++) |
| 406 | sk[k] = ((uint64_t)c->run_route[k].skill_version << 32) | c->run_route[k].chosen.skill_id; |
| 407 | uint64_t seq = ++c->seq; |
| 408 | RxMutation g[5] = { { c->goal, RXC_G_INPUT, 6 }, { c->goal, RXC_G_OP, FX_OP_SCALE }, |
| 409 | { c->goal, RXC_G_SKILL0, sk[0] }, { c->goal, RXC_G_SKILL1, sk[1] }, |
| 410 | { c->goal, RXC_G_SEQ, seq } }; |
| 411 | CHECK(rx_world_publish_external(&g_w, c->cap_ext, g, 5) >= 0, "goal published"); |
| 412 | int waited = 0; |
| 413 | while (!__atomic_load_n(&c->test.held, __ATOMIC_ACQUIRE) && waited++ < 10000) sleep_us(1000); |
| 414 | CHECK(c->test.held, "candidate 0 is held mid-run"); |
| 415 | /* Everything else of this goal settles: the held step is the only one |
| 416 | * running. The step that reads its proposal is woken by the other |
| 417 | * candidate and (I11) waits parked behind the held writer instead of |
| 418 | * committing on the stale value; it is admitted, so in_flight counts it. */ |
| 419 | for (waited = 0; in_flight() != 1 + upstream_held() && waited < 10000; waited++) sleep_us(1000); |
| 420 | CHECK(in_flight() == 1 + upstream_held(), "only the held step running (in_flight %u, held behind it %u)", |
| 421 | in_flight(), upstream_held()); |
| 422 | CHECK(upstream_held() == 1, "exactly one step parked behind the held writer (%u)", upstream_held()); |
| 423 | |
| 424 | uint32_t rid = c->rx_cand[0]; |
| 425 | uint64_t cx_before = c->cx.n; |
| 426 | RxCapRef cand_w = c->cap_cand[0][2]; |
| 427 | RxObjRef cand_obj = c->cand[0]; |
| 428 | RxObject before_obj; |
| 429 | CHECK(rx_world_read(&g_w, cand_obj, &before_obj) == RX_OK, "candidate object reads"); |
| 430 | |
| 431 | pthread_t th; |
| 432 | g_closed = 0; |
| 433 | CHECK(pthread_create(&th, NULL, closer, c) == 0, "closer thread"); |
| 434 | /* close revokes first ... */ |
| 435 | for (waited = 0; waited < 5000 && |
| 436 | rx_world_validate_cap(&g_w, cand_w, RXC_SUBJ_CAND0, RXC_RES_CAND0, RX_RIGHT_WRITE, |
| 437 | NULL) == RX_CAP_OK; waited++) |
| 438 | sleep_us(1000); |
| 439 | CHECK(rx_world_validate_cap(&g_w, cand_w, RXC_SUBJ_CAND0, RXC_RES_CAND0, RX_RIGHT_WRITE, |
| 440 | NULL) != RX_CAP_OK, "close revoked the step's write right"); |
| 441 | /* ... and does not return while the step runs (no cutoff). */ |
| 442 | sleep_us(200000); |
| 443 | CHECK(!__atomic_load_n(&g_closed, __ATOMIC_ACQUIRE), "close still waiting for the held step"); |
| 444 | CHECK(!__atomic_load_n(&c->test.hold_done, __ATOMIC_ACQUIRE), "step still held"); |
| 445 | |
| 446 | __atomic_store_n(&c->test.release, 1, __ATOMIC_RELEASE); |
| 447 | pthread_join(th, NULL); |
| 448 | CHECK(g_closed, "close returned"); |
| 449 | CHECK(__atomic_load_n(&c->test.hold_done, __ATOMIC_ACQUIRE), "step finished before close returned"); |
| 450 | pthread_mutex_lock(&g_w.mu); |
| 451 | const RxReaction *r = &g_w.reactions[rid]; |
| 452 | RxState st = r->state; |
| 453 | uint64_t acts = r->activations; |
| 454 | const RxCrumb *k = rx_world_crumb(&g_w, r->last_crumb); |
| 455 | int kind = k ? (int)k->kind : -1; |
| 456 | pthread_mutex_unlock(&g_w.mu); |
| 457 | bool removed = g_w.reactions[rid].removed; |
| 458 | CHECK(st == RX_DORMANT, "held step DORMANT after close (%s)", rx_state_name(st)); |
| 459 | CHECK(removed, "close removed the held step from the World"); |
| 460 | CHECK(kind == RX_CRUMB_REJECTED, "its late write was REJECTED (crumb kind %d)", kind); |
| 461 | CHECK(rx_world_read(&g_w, cand_obj, &before_obj) != RX_OK, "candidate object retired"); |
| 462 | free(c); /* any later use of c is a use-after-free */ |
| 463 | |
| 464 | CHECK(outside_poke(77), "World works after close (outside reaction)"); |
| 465 | CHECK(rx_world_wait_quiescent(&g_w, 10000) == RX_OK, "World quiet"); |
| 466 | pthread_mutex_lock(&g_w.mu); |
| 467 | uint64_t acts_after = g_w.reactions[rid].activations; |
| 468 | pthread_mutex_unlock(&g_w.mu); |
| 469 | CHECK(acts_after == acts, "closed step never ran again (%llu vs %llu)", |
| 470 | (unsigned long long)acts_after, (unsigned long long)acts); |
| 471 | |
| 472 | /* Durable state and the record are what they were before close. */ |
| 473 | CHECK(attach(&g_c1, d) == RX_OK, "reattach"); |
| 474 | CHECK(ref_eq(rx_compose_state(&g_c1), S1), "durable state unchanged (names S1)"); |
| 475 | /* Attach records the creation of its five fresh objects (the scoped |
| 476 | * link replays their CREATE crumbs); nothing else may have been added: |
| 477 | * the refused late write never entered the journal. */ |
| 478 | uint32_t created = 0; |
| 479 | for (uint64_t id = cx_before + 1; id <= g_c1.cx.n; id++) { |
| 480 | const CxObject *x = cx_get(&g_c1.cx, id); |
| 481 | if (x && x->kind == CX_K_ENTITY_CREATED) created++; |
| 482 | } |
| 483 | CHECK(g_c1.cx.n == cx_before + 5 && created == 5, |
| 484 | "Cortex: only the reattach's 5 creation records were added (%llu -> %llu, %u created)", |
| 485 | (unsigned long long)cx_before, (unsigned long long)g_c1.cx.n, created); |
| 486 | CHECK(g_c1.rolled_back == 0, "nothing to roll back"); |
| 487 | CHECK(fx_live_branches(&g_c1.js) == 1, "one durable branch"); |
| 488 | CHECK(fx_run(&g_fx, &g_c1, 7, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && |
| 489 | ref_eq(o.old_ref, S1) && o.result == 22, "goal after reattach commits (%d)", o.outcome); |
| 490 | rx_compose_close(&g_c1); |
| 491 | rmtree(d); |
| 492 | } |
| 493 | |
| 494 | /* ---- close reclaims everything: 2000 rounds -------------------------------- */ |
| 495 | |
| 496 | #ifndef ROUNDS |
| 497 | #define ROUNDS 2000u |
| 498 | #endif |
| 499 | |
| 500 | static void cap_ids(const RxCompose *c, uint32_t out[13]) { |
| 501 | uint32_t n = 0; |
| 502 | out[n++] = c->cap_ext.cap_id; |
| 503 | for (uint32_t k = 0; k < RXC_K; k++) |
| 504 | for (uint32_t j = 0; j < 3; j++) out[n++] = c->cap_cand[k][j].cap_id; |
| 505 | for (uint32_t j = 0; j < 4; j++) out[n++] = c->cap_verify[j].cap_id; |
| 506 | for (uint32_t j = 0; j < 2; j++) out[n++] = c->cap_commit[j].cap_id; |
| 507 | for (uint32_t i = 1; i < 13; i++) /* sorted: the set, not the order */ |
| 508 | for (uint32_t j = i; j > 0 && out[j - 1] > out[j]; j--) { |
| 509 | uint32_t t = out[j]; out[j] = out[j - 1]; out[j - 1] = t; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | static void t_reclaim_rounds(void) { |
| 514 | printf("[*] reclaim: %u attach/close rounds, footprint identical after each\n", ROUNDS); |
| 515 | char d[256]; |
| 516 | dir_for(d, sizeof d, "rounds"); |
| 517 | RxFootprint before, after, first_after, during; |
| 518 | footprint(&before); |
| 519 | uint32_t caps0[13], caps[13]; |
| 520 | uint32_t bad_fp = 0, bad_slots = 0, bad_caps = 0, bad_goal = 0, bad_attach = 0, bad_during = 0; |
| 521 | uint32_t bad_removed = 0, reused = 0, pokes = 0, poked = 0, goals = 0; |
| 522 | RxObjRef first_goal = { UINT32_MAX, 0 }; |
| 523 | for (uint32_t r = 0; r < ROUNDS; r++) { |
| 524 | RxFootprint pre; |
| 525 | footprint(&pre); |
| 526 | if (attach(&g_c1, d) != RX_OK) { bad_attach++; break; } |
| 527 | if (r == 0) first_goal = g_c1.goal; |
| 528 | else if (g_c1.goal.id == first_goal.id && g_c1.goal.generation != first_goal.generation) |
| 529 | reused++; |
| 530 | footprint(&during); |
| 531 | if (during.reactions_active != pre.reactions_active + 4 || during.binders != pre.binders + 1 || |
| 532 | during.objects_live != pre.objects_live + 5 || during.bound_fields != pre.bound_fields + 1) |
| 533 | bad_during++; |
| 534 | cap_ids(&g_c1, caps); |
| 535 | if (r == 0) memcpy(caps0, caps, sizeof caps0); |
| 536 | else if (memcmp(caps0, caps, sizeof caps)) bad_caps++; |
| 537 | /* A goal on the first rounds, every 50th and the last two: the |
| 538 | * leak check is about attach/close; goals show the World still works. */ |
| 539 | if (r < 3 || r % 50 == 0 || r + 2 >= ROUNDS) { |
| 540 | RxcResult o; |
| 541 | goals++; |
| 542 | if (fx_run(&g_fx, &g_c1, 100 + r, &o) != RX_OK || o.outcome != RXC_OUT_COMMITTED || |
| 543 | o.result != (100 + r) * 3 + 1) |
| 544 | bad_goal++; |
| 545 | } |
| 546 | uint32_t ids[4] = { g_c1.rx_cand[0], g_c1.rx_cand[1], g_c1.rx_verify, g_c1.rx_commit }; |
| 547 | rx_compose_close(&g_c1); |
| 548 | pthread_mutex_lock(&g_w.mu); |
| 549 | for (uint32_t i = 0; i < 4; i++) |
| 550 | if (!g_w.reactions[ids[i]].removed || g_w.reactions[ids[i]].desc.fn) bad_removed++; |
| 551 | pthread_mutex_unlock(&g_w.mu); |
| 552 | footprint(&after); |
| 553 | if (!fp_same(&pre, &after, r > 0)) { |
| 554 | if (!bad_fp) { fp_print("pre", &pre); fp_print("post", &after); } |
| 555 | bad_fp++; |
| 556 | } |
| 557 | if (r == 0) first_after = after; |
| 558 | else if (after.reaction_slots != first_after.reaction_slots) bad_slots++; |
| 559 | if (r % 100 == 0) { poked++; pokes += (uint32_t)outside_poke(20000 + r); } |
| 560 | } |
| 561 | CHECK(bad_attach == 0, "every attach succeeded"); |
| 562 | CHECK(bad_goal == 0, "every goal committed (%u of %u failed)", bad_goal, goals); |
| 563 | CHECK(bad_during == 0, "each attach held exactly 4 reactions, 5 objects, 1 binder (%u off)", |
| 564 | bad_during); |
| 565 | CHECK(bad_fp == 0, "footprint after close identical to before the round (%u rounds differ)", |
| 566 | bad_fp); |
| 567 | CHECK(fp_same(&before, &first_after, 0) && |
| 568 | first_after.reaction_slots <= before.reaction_slots + 4u && |
| 569 | first_after.reactions_removed <= before.reactions_removed + 4u, |
| 570 | "first round: only the reaction table kept its (at most 4) removed slots for reuse"); |
| 571 | CHECK(bad_slots == 0, "reaction table did not grow after the first round (%u rounds grew)", |
| 572 | bad_slots); |
| 573 | CHECK(bad_caps == 0, "every attach got the same 13 capability slots back (%u differ)", bad_caps); |
| 574 | CHECK(bad_removed == 0, "every closed reaction removed and unreachable (%u not)", bad_removed); |
| 575 | CHECK(reused > 0, "later attaches reused the retired object slots (%u)", reused); |
| 576 | CHECK(pokes == poked, "outside reaction kept working (%u/%u)", pokes, poked); |
| 577 | footprint(&after); |
| 578 | printf(" %u rounds, %u goals: reaction slots %u (before %u), active %u, objects %u, binders %u\n", |
| 579 | ROUNDS, goals, after.reaction_slots, before.reaction_slots, after.reactions_active, |
| 580 | after.objects_live, after.binders); |
| 581 | rmtree(d); |
| 582 | } |
| 583 | |
| 584 | /* ---- a full reaction table refuses the attach ------------------------------- */ |
| 585 | |
| 586 | static int filler_fn(RxCtx *x) { (void)x; return 0; } |
| 587 | |
| 588 | static void t_full_refused(void) { |
| 589 | printf("[*] full: no room for four reactions -> RX_ERR_FULL, nothing built\n"); |
| 590 | char d[256]; |
| 591 | dir_for(d, sizeof d, "full"); |
| 592 | if (rx_world_init_native(&g_w2, g_view, 1, 1u << 16) != RX_OK) { CHECK(0, "second World"); return; } |
| 593 | g_w2.external_subject = EXT_SUBJ; |
| 594 | uint64_t z[RX_MAX_FIELDS] = { 0 }; |
| 595 | RxObjRef o; |
| 596 | CHECK(rx_world_create(&g_w2, 1, RX_PERSIST_RESIDENT, RES_OUT_IN, z, &o) == RX_OK, "object"); |
| 597 | RxReactionDesc fd; |
| 598 | memset(&fd, 0, sizeof fd); |
| 599 | fd.name = "filler"; |
| 600 | fd.faculty = RX_FACULTY_OMEGA; |
| 601 | fd.subject = OUT_SUBJ; |
| 602 | fd.priority = RX_PRIO_BACKGROUND; |
| 603 | fd.triggers[fd.n_triggers++] = (RxDep){ o, RX_ALL_FIELDS }; |
| 604 | fd.caps[fd.n_caps++] = (RxCapNeed){ g_cap_rin, RES_OUT_IN, RX_RIGHT_READ }; |
| 605 | fd.fn = filler_fn; |
| 606 | uint32_t rid, n = 0; |
| 607 | while (g_w2.n_reactions < RX_MAX_REACTIONS - 3u && rx_world_add_reaction(&g_w2, &fd, &rid) == RX_OK) |
| 608 | n++; |
| 609 | CHECK(g_w2.n_reactions == RX_MAX_REACTIONS - 3u, "table filled to 3 free (%u)", g_w2.n_reactions); |
| 610 | RxFootprint a, b; |
| 611 | rx_world_footprint(&g_w2, &a); |
| 612 | int rc = rx_compose_attach(&g_c1, &g_w2, NULL, d, &g_fx.self, FX_SESSION, &g_fx.router, |
| 613 | fx_contract, g_admin); |
| 614 | rx_world_footprint(&g_w2, &b); |
| 615 | CHECK(rc == RX_ERR_FULL, "attach refused with RX_ERR_FULL (%d)", rc); |
| 616 | CHECK(g_c1.world == NULL && fp_same(&a, &b, 1), "refused attach built nothing"); |
| 617 | /* Removing other subjects' reactions does not make room: a removed slot |
| 618 | * is reused only by the same subject and faculty. */ |
| 619 | CHECK(rx_world_remove_reaction(&g_w2, rid) == RX_OK, "filler removed"); |
| 620 | CHECK(rx_world_remove_reaction(&g_w2, rid) == RX_ERR_NOT_FOUND, "second remove refused"); |
| 621 | CHECK(rx_world_add_reaction(&g_w2, &fd, &n) == RX_OK && n == rid, "same subject reuses the slot"); |
| 622 | rx_world_destroy(&g_w2); |
| 623 | rmtree(d); |
| 624 | } |
| 625 | |
| 626 | /* ---- the settle wait is configurable and reset by attach ------------------- */ |
| 627 | |
| 628 | static void t_wait_ms(void) { |
| 629 | printf("[*] wait_ms: default 30000, set 1..600000, reset by attach\n"); |
| 630 | char d[256]; |
| 631 | dir_for(d, sizeof d, "waitms"); |
| 632 | CHECK(attach(&g_c1, d) == RX_OK, "attach"); |
| 633 | CHECK(g_c1.wait_ms == RXC_WAIT_MS_DEFAULT && RXC_WAIT_MS_DEFAULT == 30000u, "default 30000 (%u)", |
| 634 | g_c1.wait_ms); |
| 635 | CHECK(rx_compose_set_wait_ms(&g_c1, 0) == RX_ERR_ARG, "0 refused"); |
| 636 | CHECK(rx_compose_set_wait_ms(&g_c1, RXC_WAIT_MS_MAX + 1u) == RX_ERR_ARG, "600001 refused"); |
| 637 | CHECK(rx_compose_set_wait_ms(NULL, 1000) == RX_ERR_ARG, "NULL refused"); |
| 638 | CHECK(g_c1.wait_ms == RXC_WAIT_MS_DEFAULT, "refused calls change nothing"); |
| 639 | CHECK(rx_compose_set_wait_ms(&g_c1, 45000) == RX_OK && g_c1.wait_ms == 45000u, "45000 set"); |
| 640 | RxcResult o; |
| 641 | CHECK(fx_run(&g_fx, &g_c1, 7, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && o.result == 22, |
| 642 | "a run still commits with the longer wait"); |
| 643 | rx_compose_close(&g_c1); |
| 644 | CHECK(attach(&g_c1, d) == RX_OK, "reattach"); |
| 645 | CHECK(g_c1.wait_ms == RXC_WAIT_MS_DEFAULT, "reset to 30000 by attach (%u)", g_c1.wait_ms); |
| 646 | rx_compose_close(&g_c1); |
| 647 | rmtree(d); |
| 648 | } |
| 649 | |
| 650 | int main(void) { |
| 651 | snprintf(g_base, sizeof g_base, "/tmp/rx_compose_attach_test.XXXXXX"); |
| 652 | if (!mkdtemp(g_base)) { perror("mkdtemp"); return 1; } |
| 653 | if (fx_init(&g_fx) != 0) { fprintf(stderr, "fixture init failed\n"); return 1; } |
| 654 | int rc = world_start(); |
| 655 | if (rc != 0) { fprintf(stderr, "world start failed (%d)\n", rc); return 1; } |
| 656 | t_two_per_world(); |
| 657 | t_close_waits(); |
| 658 | t_reclaim_rounds(); |
| 659 | t_full_refused(); |
| 660 | t_wait_ms(); |
| 661 | rx_world_destroy(&g_w); |
| 662 | aienos_cap_stop(g_admin, g_view); |
| 663 | fx_free(&g_fx); |
| 664 | rmtree(g_base); |
| 665 | printf("%s: %d checks, %d failed\n", g_fail ? "FAIL" : "PASS", g_checks, g_fail); |
| 666 | return g_fail ? 1 : 0; |
| 667 | } |