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 / rxc_host_abi_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 | * rxc_host_abi_test.c -- NEXT-PHASE-1 cut 1a: the host ABI facade over |
| 19 | * COMPOSITION-2, through build/librx_compose.a only (no test hooks). |
| 20 | * |
| 21 | * T1 open a fresh home, register one Skill that records its task handle, |
| 22 | * run one goal: committed, AEGIS pass, Cortex record ids present |
| 23 | * T2 close, REOPEN the same dir: same AienMachineId, recall returns the |
| 24 | * same record ids with the same digests (restart + recall); a second |
| 25 | * goal commits after the restart |
| 26 | * T3 another machine is refused on that home (identity) |
| 27 | * T4 containment: only machine.id, cortex.cx and jspace* on disk (staged |
| 28 | * J-Space branches leave no file) |
| 29 | * T5 torn tail (cut 2): the default open refuses a torn journal |
| 30 | * (RXC_HOST_E_TORN) or one cut behind its anchor (RXC_HOST_E_REPLAY), |
| 31 | * stably and without writing; rxc_host_recover repairs, records the cut |
| 32 | * (byte range, dropped count, cause) and re-anchors; the next open |
| 33 | * succeeds with the old records an unchanged prefix and a goal commits |
| 34 | * T6 recover is refused while a handle holds the home |
| 35 | * T7 host records (constraint, authorization, effect) through the |
| 36 | * composition writer, recalled with the same ids after restart |
| 37 | */ |
| 38 | #include "runtime/rxc_host_abi.h" |
| 39 | |
| 40 | #include <dirent.h> |
| 41 | #include <stdatomic.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <unistd.h> |
| 47 | |
| 48 | static int fails, checks; |
| 49 | #define CHECK(cond, ...) do { checks++; if (!(cond)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); \ |
| 50 | printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 51 | |
| 52 | static _Atomic uint64_t seen_task, calls; |
| 53 | #define PROPOSE(t) ((t) * 3u + 1u) |
| 54 | |
| 55 | static int skill(void *ctx, uint64_t task, uint64_t *result) { |
| 56 | (void)ctx; |
| 57 | atomic_store(&seen_task, task); |
| 58 | atomic_fetch_add(&calls, 1); |
| 59 | *result = PROPOSE(task); |
| 60 | return 0; |
| 61 | } |
| 62 | static int verify(void *ctx, uint64_t task, uint64_t result) { |
| 63 | (void)ctx; |
| 64 | return result == PROPOSE(task); |
| 65 | } |
| 66 | |
| 67 | static const uint8_t ROOT[] = "np1-host-abi-test-machine"; |
| 68 | static const uint8_t OTHER[] = "np1-some-other-machine"; |
| 69 | |
| 70 | static void hex8(const uint8_t *d, char *o) { |
| 71 | static const char *h = "0123456789abcdef"; |
| 72 | for (int i = 0; i < 8; i++) { o[2 * i] = h[d[i] >> 4]; o[2 * i + 1] = h[d[i] & 15]; } |
| 73 | o[16] = 0; |
| 74 | } |
| 75 | |
| 76 | static RxcHost *open_home(const char *dir, uint32_t flags, RxcHostInfo *info, int *rc_out) { |
| 77 | RxcHost *h = NULL; |
| 78 | int rc = rxc_host_open(dir, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, 0x5E55ull, flags, |
| 79 | &h, info); |
| 80 | if (rc_out) *rc_out = rc; |
| 81 | if (rc != RXC_HOST_OK) return NULL; |
| 82 | int k = rxc_host_register_skill(h, "np1.echo-proposal", NULL, 10, skill, NULL); |
| 83 | CHECK(k == 0, "register_skill -> %d", k); |
| 84 | CHECK(rxc_host_set_verify(h, verify, NULL) == RXC_HOST_OK, "set_verify"); |
| 85 | return h; |
| 86 | } |
| 87 | |
| 88 | static uint32_t collect(RxcHost *h, RxcHostRecord *all, uint32_t max) { |
| 89 | RxcHostInfo in; |
| 90 | if (rxc_host_info(h, &in) != RXC_HOST_OK) return 0; |
| 91 | uint32_t n = 0; |
| 92 | for (uint64_t id = 1; id <= in.records && n < max; id++) |
| 93 | if (rxc_host_record(h, id, &all[n]) == RXC_HOST_OK) n++; |
| 94 | CHECK(n == in.records && n > 0, "every record readable (%u of %llu)", n, |
| 95 | (unsigned long long)in.records); |
| 96 | return n; |
| 97 | } |
| 98 | |
| 99 | /* Byte offset where record `id` starts (journal: 32-byte header, then per |
| 100 | * record 13 header words + payload + 4 digest words). */ |
| 101 | static uint64_t record_start(const RxcHostRecord *all, uint32_t nall, uint64_t id) { |
| 102 | uint64_t off = 32; |
| 103 | for (uint32_t i = 0; i < nall && all[i].id < id; i++) off += (17u + all[i].n_payload) * 8u; |
| 104 | return off; |
| 105 | } |
| 106 | |
| 107 | static uint64_t word_digest_nonzero(const uint64_t *w) { return w[0] | w[1] | w[2] | w[3]; } |
| 108 | |
| 109 | /* `all[0..nall)`: every record of the closed home, read just before close. |
| 110 | * Cut the journal to `cut` bytes (torn: inside the last record; boundary: |
| 111 | * at a record start), then: the default open refuses with a named reason |
| 112 | * and leaves the journal untouched, stably; another machine may not |
| 113 | * repair; rxc_host_recover repairs and records the cut; a second recover |
| 114 | * is a no-op; the next open succeeds with records 1..kept an unchanged |
| 115 | * prefix, the repair record readable, and a goal commits. 0 = all held. */ |
| 116 | static int torn_case(const char *home, const char *label, const RxcHostRecord *all, uint32_t nall, |
| 117 | uint64_t cut, int torn, uint64_t next_task, RxcHostRepair *rep_out) { |
| 118 | int f0 = fails, rc; |
| 119 | char cx[400]; |
| 120 | snprintf(cx, sizeof cx, "%s/cortex.cx", home); |
| 121 | struct stat st; |
| 122 | CHECK(stat(cx, &st) == 0 && (uint64_t)st.st_size > cut, "%s: journal size", label); |
| 123 | const uint64_t size0 = (uint64_t)st.st_size; |
| 124 | CHECK(truncate(cx, (off_t)cut) == 0, "%s: cut %llu -> %llu bytes", label, |
| 125 | (unsigned long long)size0, (unsigned long long)cut); |
| 126 | |
| 127 | for (int pass = 0; pass < 2; pass++) { /* the refusal is stable */ |
| 128 | RxcHostInfo it, it2; |
| 129 | RxcHost *s = NULL; |
| 130 | int orc = rxc_host_open(home, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, 0x5E55ull, |
| 131 | 0, &s, &it); |
| 132 | if (torn) { |
| 133 | CHECK(orc == RXC_HOST_E_TORN && s == NULL && it.tail_torn == 1, |
| 134 | "%s: open %d refuses the torn tail -> %d", label, pass, orc); |
| 135 | } else { |
| 136 | CHECK(orc == RXC_HOST_OK && s && !it.tail_torn, "%s: open %d handle -> %d", label, pass, |
| 137 | orc); |
| 138 | CHECK(s && rxc_host_register_skill(s, "np1.echo-proposal", NULL, 10, skill, NULL) == 0, |
| 139 | "%s: register on the refused home", label); |
| 140 | int irc = s ? rxc_host_info(s, &it2) : -99; |
| 141 | CHECK(irc == RXC_HOST_E_REPLAY && it2.open_rc == -21 && !it2.opened, |
| 142 | "%s: open %d refuses a journal behind its anchor (rc %d open_rc %d)", label, pass, |
| 143 | irc, it2.open_rc); |
| 144 | RxcHostResult rq; |
| 145 | int qrc = s ? rxc_host_run(s, next_task, 6000000, &rq) : -99; |
| 146 | CHECK(qrc == RXC_HOST_E_REPLAY, "%s: no run on a refused home -> %d", label, qrc); |
| 147 | rxc_host_close(s); |
| 148 | } |
| 149 | CHECK(stat(cx, &st) == 0 && (uint64_t)st.st_size == cut, |
| 150 | "%s: refusal %d left the journal untouched (%lld bytes)", label, pass, |
| 151 | (long long)st.st_size); |
| 152 | } |
| 153 | |
| 154 | RxcHostRepair rep, rep2; |
| 155 | CHECK(rxc_host_recover(home, RXC_HOST_ROOT_PROVISIONED, OTHER, sizeof OTHER - 1, &rep) == |
| 156 | RXC_HOST_E_IDENTITY && !rep.repaired, "%s: another machine may not repair", label); |
| 157 | CHECK(stat(cx, &st) == 0 && (uint64_t)st.st_size == cut, "%s: identity refusal wrote nothing", |
| 158 | label); |
| 159 | rc = rxc_host_recover(home, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, &rep); |
| 160 | printf("%s: recover rc %d repaired %u torn %u cause %d cut [%llu,%llu) partial %llu anchor %llu " |
| 161 | "kept %llu dropped %llu event %llu cut_bytes_kept %llu rolled_back %u recovered_completed %u\n", label, rc, rep.repaired, |
| 162 | rep.tail_torn, rep.cause, (unsigned long long)rep.cut_lo, (unsigned long long)rep.cut_hi, |
| 163 | (unsigned long long)rep.partial_records, (unsigned long long)rep.anchor_records, |
| 164 | (unsigned long long)rep.records_kept, (unsigned long long)rep.dropped_records, |
| 165 | (unsigned long long)rep.event_id, (unsigned long long)rep.cut_bytes_kept, rep.rolled_back, |
| 166 | rep.recovered_completed); |
| 167 | CHECK(rc == RXC_HOST_OK && rep.repaired == 1 && rep.event_id == rep.records_kept + 1, |
| 168 | "%s: recover repaired -> %d", label, rc); |
| 169 | CHECK(rep.opens == 1 && rep.open_rc == 0, "%s: recover's trial open succeeded", label); |
| 170 | CHECK(rep.tail_torn == (uint32_t)torn && rep.cause == (torn ? -8 : -21), "%s: cause named", |
| 171 | label); |
| 172 | CHECK(rep.cut_hi == cut && rep.cut_lo <= rep.cut_hi && rep.cut_bytes_kept == rep.cut_hi - rep.cut_lo, |
| 173 | "%s: byte range recorded", label); |
| 174 | CHECK(rep.dropped_records >= 1 && rep.records_kept < nall, "%s: dropped %llu records", label, |
| 175 | (unsigned long long)rep.dropped_records); |
| 176 | CHECK(rep.records_kept + rep.dropped_records == nall || rep.anchor_records < nall, |
| 177 | "%s: kept + dropped = records before the cut (%llu + %llu vs %u)", label, |
| 178 | (unsigned long long)rep.records_kept, (unsigned long long)rep.dropped_records, nall); |
| 179 | if (torn) CHECK(rep.cut_lo == record_start(all, nall, rep.records_kept + 1), |
| 180 | "%s: cut starts at record %llu", label, (unsigned long long)rep.records_kept + 1); |
| 181 | CHECK(rxc_host_recover(home, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, &rep2) == |
| 182 | RXC_HOST_OK && rep2.repaired == 0, "%s: a second recover is a no-op", label); |
| 183 | |
| 184 | RxcHostInfo ir, ir2; |
| 185 | RxcHost *h = open_home(home, 0, &ir, &rc); |
| 186 | CHECK(h && rc == RXC_HOST_OK && !ir.tail_torn, "%s: open after recover -> %d", label, rc); |
| 187 | if (!h) return -1; |
| 188 | int irc = rxc_host_info(h, &ir2); |
| 189 | printf("%s: after recover info rc %d records %llu rolled_back %u recovered_completed %u\n", |
| 190 | label, irc, (unsigned long long)ir2.records, ir2.rolled_back, ir2.recovered_completed); |
| 191 | CHECK(irc == RXC_HOST_OK && ir2.opened, "%s: the composition opens after recover", label); |
| 192 | int prefix = 1; |
| 193 | for (uint32_t i = 0; i < rep.records_kept && i < nall; i++) { |
| 194 | RxcHostRecord x; |
| 195 | if (rxc_host_record(h, all[i].id, &x) != RXC_HOST_OK || x.id != all[i].id || |
| 196 | memcmp(x.digest, all[i].digest, 32) != 0) { prefix = 0; break; } |
| 197 | } |
| 198 | CHECK(prefix, "%s: records 1..%llu are an unchanged prefix", label, |
| 199 | (unsigned long long)rep.records_kept); |
| 200 | RxcHostRecord ev; |
| 201 | uint64_t w[24]; |
| 202 | CHECK(rxc_host_record(h, rep.event_id, &ev) == RXC_HOST_OK && |
| 203 | ev.subject == RXC_HOST_SUBJECT_HOST && ev.tag == RXC_HOST_TAG_REPAIR_TAIL && |
| 204 | ev.links[0] == rep.records_kept, "%s: repair record %llu in the journal", label, |
| 205 | (unsigned long long)rep.event_id); |
| 206 | int pn = rxc_host_payload(h, rep.event_id, w, 24); |
| 207 | CHECK(pn >= (int)RXC_HOST_RP_BYTES && w[RXC_HOST_RP_CUT_LO] == rep.cut_lo && |
| 208 | w[RXC_HOST_RP_CUT_HI] == rep.cut_hi && w[RXC_HOST_RP_DROPPED] == rep.dropped_records && |
| 209 | (int64_t)w[RXC_HOST_RP_CAUSE] == rep.cause && |
| 210 | (!rep.cut_bytes_kept || word_digest_nonzero(w + RXC_HOST_RP_CUT_SHA)), |
| 211 | "%s: repair payload names range, count, cause", label); |
| 212 | RxcHostResult r3; |
| 213 | rc = rxc_host_run(h, next_task, 5000000 + next_task % 1000, &r3); |
| 214 | CHECK(rc == RXC_HOST_OK && r3.committed, "%s: run after recover -> %d outcome %d", label, rc, |
| 215 | r3.outcome); |
| 216 | rxc_host_close(h); |
| 217 | if (rep_out) *rep_out = rep; |
| 218 | return fails == f0 ? 0 : -1; |
| 219 | } |
| 220 | |
| 221 | /* rxc_host_set_wait_ms: validated, accepted before and after the first run. */ |
| 222 | static void t_wait_ms(const char *dir) { |
| 223 | char home[300]; |
| 224 | snprintf(home, sizeof home, "%s/home_wait", dir); |
| 225 | int rc; |
| 226 | RxcHostInfo info; |
| 227 | RxcHost *h = open_home(home, 0, &info, &rc); |
| 228 | CHECK(h && rc == RXC_HOST_OK, "wait_ms: open -> %d", rc); |
| 229 | if (!h) return; |
| 230 | CHECK(rxc_host_set_wait_ms(NULL, 1000) == RXC_HOST_E_ARG, "wait_ms: NULL refused"); |
| 231 | CHECK(rxc_host_set_wait_ms(h, 0) == RXC_HOST_E_ARG, "wait_ms: 0 refused"); |
| 232 | CHECK(rxc_host_set_wait_ms(h, 600001) == RXC_HOST_E_ARG, "wait_ms: 600001 refused"); |
| 233 | CHECK(rxc_host_set_wait_ms(h, 45000) == RXC_HOST_OK, "wait_ms: 45000 before first run"); |
| 234 | RxcHostResult r; |
| 235 | rc = rxc_host_run(h, 0x4E50310000000F01ull, 1000000, &r); |
| 236 | CHECK(rc == RXC_HOST_OK && r.outcome == RXC_HOST_OUT_COMMITTED, "wait_ms: run -> %d", rc); |
| 237 | CHECK(rxc_host_set_wait_ms(h, 60000) == RXC_HOST_OK, "wait_ms: 60000 after the run"); |
| 238 | CHECK(rxc_host_set_wait_ms(h, 600001) == RXC_HOST_E_ARG, "wait_ms: still validated when open"); |
| 239 | rxc_host_close(h); |
| 240 | } |
| 241 | |
| 242 | #define MAXR 64 |
| 243 | int main(void) { |
| 244 | char dir[256]; |
| 245 | const char *tmp = getenv("TMPDIR"); |
| 246 | snprintf(dir, sizeof dir, "%s/rxc_host_abi.XXXXXX", tmp && *tmp ? tmp : "/tmp"); |
| 247 | if (!mkdtemp(dir)) { perror("mkdtemp"); return 2; } |
| 248 | char home[300]; |
| 249 | snprintf(home, sizeof home, "%s/home", dir); |
| 250 | |
| 251 | uint32_t lay[4]; |
| 252 | CHECK(rxc_host_abi_layout(lay) == RXC_HOST_ABI_VERSION && lay[0] == sizeof(RxcHostInfo) && |
| 253 | lay[1] == sizeof(RxcHostResult) && lay[2] == sizeof(RxcHostRecord) && |
| 254 | lay[3] == sizeof(RxcHostRepair), "abi layout"); |
| 255 | printf("abi layout: info %u result %u record %u repair %u\n", lay[0], lay[1], lay[2], lay[3]); |
| 256 | |
| 257 | t_wait_ms(dir); |
| 258 | |
| 259 | /* A dir refused by src/omega_path_check.h is an argument error and creates nothing (Rev1001 on #1006). */ |
| 260 | { |
| 261 | char bad[320]; |
| 262 | snprintf(bad, sizeof bad, "%s/refused dir", dir); |
| 263 | RxcHost *hb = NULL; |
| 264 | RxcHostInfo ib; |
| 265 | int brc = rxc_host_open(bad, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, 0x5E55ull, 0, &hb, &ib); |
| 266 | CHECK(brc == RXC_HOST_E_ARG && !hb, "refused dir -> E_ARG (%d)", brc); |
| 267 | CHECK(access(bad, F_OK) != 0, "refused dir not created"); |
| 268 | } |
| 269 | |
| 270 | /* ---- T1 ---- */ |
| 271 | RxcHostInfo i1; |
| 272 | int rc; |
| 273 | RxcHost *h = open_home(home, 0, &i1, &rc); |
| 274 | CHECK(h && rc == RXC_HOST_OK, "open fresh -> %d", rc); |
| 275 | if (!h) return 1; |
| 276 | CHECK(i1.abi_version == RXC_HOST_ABI_VERSION && !i1.machine_id_was_stored && !i1.tail_torn, |
| 277 | "fresh info"); |
| 278 | uint64_t task1 = 0x4E50310000000001ull; |
| 279 | RxcHostResult r1; |
| 280 | rc = rxc_host_run(h, task1, 1000000, &r1); |
| 281 | CHECK(rc == RXC_HOST_OK, "run 1 -> %d (run_rc %d)", rc, r1.run_rc); |
| 282 | CHECK(r1.outcome == RXC_HOST_OUT_COMMITTED && r1.committed == 1, "run 1 outcome %d", r1.outcome); |
| 283 | CHECK(atomic_load(&seen_task) == task1 && atomic_load(&calls) >= 1, |
| 284 | "Skill saw task %llx", (unsigned long long)atomic_load(&seen_task)); |
| 285 | CHECK(r1.result == PROPOSE(task1), "committed result"); |
| 286 | CHECK(r1.n_branches == 1 && r1.winner == 0 && r1.winner_skill == 0, "branches %u winner %u", |
| 287 | r1.n_branches, r1.winner); |
| 288 | CHECK(r1.aegis_pass_mask & 1u, "AEGIS pass mask %x", r1.aegis_pass_mask); |
| 289 | CHECK(r1.cx_goal && r1.cx_candidate[0] && r1.cx_evidence && r1.cx_promotion, |
| 290 | "cortex ids goal %llu cand %llu ev %llu promo %llu", (unsigned long long)r1.cx_goal, |
| 291 | (unsigned long long)r1.cx_candidate[0], (unsigned long long)r1.cx_evidence, |
| 292 | (unsigned long long)r1.cx_promotion); |
| 293 | uint64_t cited[4] = { r1.cx_goal, r1.cx_candidate[0], r1.cx_evidence, r1.cx_promotion }; |
| 294 | RxcHostRecord cited_rec[4]; |
| 295 | for (int i = 0; i < 4; i++) |
| 296 | CHECK(rxc_host_record(h, cited[i], &cited_rec[i]) == RXC_HOST_OK && cited_rec[i].verified, |
| 297 | "record %llu", (unsigned long long)cited[i]); |
| 298 | static RxcHostRecord before[MAXR], after[MAXR]; |
| 299 | uint32_t nb = 0, na = 0; |
| 300 | uint64_t tb = 0, ta = 0; |
| 301 | CHECK(rxc_host_recall(h, RXC_HOST_SUBJECT_STATE, before, MAXR, &nb, &tb) == RXC_HOST_OK && |
| 302 | nb > 0 && nb == tb, "recall before: %u of %llu", nb, (unsigned long long)tb); |
| 303 | RxcHostInfo i1b; |
| 304 | CHECK(rxc_host_info(h, &i1b) == RXC_HOST_OK && i1b.records > 0, "journal has records"); |
| 305 | uint8_t mid[32]; |
| 306 | memcpy(mid, i1b.machine_id, 32); |
| 307 | rxc_host_close(h); |
| 308 | |
| 309 | /* ---- T2 restart + recall ---- */ |
| 310 | RxcHostInfo i2; |
| 311 | h = open_home(home, 0, &i2, &rc); |
| 312 | CHECK(h && rc == RXC_HOST_OK, "reopen -> %d", rc); |
| 313 | if (!h) return 1; |
| 314 | CHECK(i2.machine_id_was_stored && memcmp(i2.machine_id, mid, 32) == 0 && !i2.tail_torn, |
| 315 | "same AienMachineId after restart"); |
| 316 | CHECK(rxc_host_recall(h, RXC_HOST_SUBJECT_STATE, after, MAXR, &na, &ta) == RXC_HOST_OK, |
| 317 | "recall after"); |
| 318 | /* Reopen appends (the World's objects come to life again at every open): |
| 319 | * the pre-restart records must be an unchanged PREFIX of the recall. */ |
| 320 | CHECK(na >= nb, "recall count %u vs %u", na, nb); |
| 321 | int same = na >= nb; |
| 322 | for (uint32_t i = 0; same && i < nb; i++) |
| 323 | same = after[i].id == before[i].id && memcmp(after[i].digest, before[i].digest, 32) == 0 && |
| 324 | after[i].verified; |
| 325 | CHECK(same, "recall after restart returns the same ids and digests"); |
| 326 | for (uint32_t i = nb; i < na; i++) |
| 327 | printf("appended at reopen: id %llu kind 0x%x tag %llu\n", (unsigned long long)after[i].id, |
| 328 | after[i].kind, (unsigned long long)after[i].tag); |
| 329 | for (int i = 0; i < 4; i++) { |
| 330 | RxcHostRecord x; |
| 331 | CHECK(rxc_host_record(h, cited[i], &x) == RXC_HOST_OK && |
| 332 | memcmp(x.digest, cited_rec[i].digest, 32) == 0, "cited record %llu after restart", |
| 333 | (unsigned long long)cited[i]); |
| 334 | } |
| 335 | RxcHostInfo i2b; |
| 336 | rxc_host_info(h, &i2b); |
| 337 | CHECK(i2b.recovered_record != 0 && i2b.rolled_back == 0, "recovered state record %llu", |
| 338 | (unsigned long long)i2b.recovered_record); |
| 339 | RxcHostResult r2; |
| 340 | uint64_t task2 = 0x4E50310000000002ull; |
| 341 | rc = rxc_host_run(h, task2, 2000000, &r2); |
| 342 | CHECK(rc == RXC_HOST_OK && r2.committed && r2.result == PROPOSE(task2), |
| 343 | "run 2 after restart -> %d outcome %d", rc, r2.outcome); |
| 344 | |
| 345 | /* ---- T3 identity ---- */ |
| 346 | RxcHost *o = NULL; |
| 347 | RxcHostInfo io; |
| 348 | int orc = rxc_host_open(home, RXC_HOST_ROOT_PROVISIONED, OTHER, sizeof OTHER - 1, 1, 0, &o, &io); |
| 349 | CHECK(orc == RXC_HOST_E_IDENTITY && o == NULL, "other machine refused -> %d", orc); |
| 350 | rxc_host_close(h); |
| 351 | |
| 352 | /* ---- T4 containment ---- */ |
| 353 | DIR *d = opendir(home); |
| 354 | int stray = 0, nfiles = 0; |
| 355 | struct dirent *e; |
| 356 | while (d && (e = readdir(d))) { |
| 357 | if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue; |
| 358 | nfiles++; |
| 359 | if (strcmp(e->d_name, "machine.id") && strcmp(e->d_name, "cortex.cx") && |
| 360 | strncmp(e->d_name, "jspace", 6)) { |
| 361 | stray++; |
| 362 | printf("stray file: %s\n", e->d_name); |
| 363 | } |
| 364 | } |
| 365 | if (d) closedir(d); |
| 366 | CHECK(stray == 0 && nfiles >= 3, "home holds only machine.id, cortex.cx, jspace* (%d files)", |
| 367 | nfiles); |
| 368 | |
| 369 | /* ---- T5 torn tail, strict by default, repaired on request ---- |
| 370 | * (a) a goal commits and its own tail is torn (the coordinator's case); |
| 371 | * (b) the torn record lies past the J-Space anchor (written at reopen); |
| 372 | * (c) the journal is cut exactly at a record boundary inside the |
| 373 | * anchored prefix (cx_open sees a shorter valid journal). */ |
| 374 | static RxcHostRecord all[256]; |
| 375 | uint32_t nall; |
| 376 | RxcHostRepair rp; |
| 377 | h = open_home(home, 0, NULL, &rc); |
| 378 | CHECK(h != NULL, "T5a open -> %d", rc); |
| 379 | if (!h) return 1; |
| 380 | RxcHostResult rb; |
| 381 | CHECK(rxc_host_run(h, 0x4E50310000000005ull, 7000000, &rb) == RXC_HOST_OK && rb.committed, |
| 382 | "T5a run"); |
| 383 | nall = collect(h, all, 256); |
| 384 | rxc_host_close(h); |
| 385 | { |
| 386 | struct stat s5; |
| 387 | char cx5[400]; |
| 388 | snprintf(cx5, sizeof cx5, "%s/cortex.cx", home); |
| 389 | CHECK(stat(cx5, &s5) == 0, "T5a stat"); |
| 390 | CHECK(torn_case(home, "T5a torn after a run", all, nall, (uint64_t)s5.st_size - 5, 1, |
| 391 | 0x4E50310000000006ull, &rp) == 0, "T5a held"); |
| 392 | } |
| 393 | |
| 394 | h = open_home(home, 0, NULL, &rc); |
| 395 | CHECK(h != NULL, "T5b open -> %d", rc); |
| 396 | if (!h) return 1; |
| 397 | nall = collect(h, all, 256); |
| 398 | rxc_host_close(h); |
| 399 | h = open_home(home, 0, NULL, &rc); /* this reopen appends past the anchor */ |
| 400 | if (!h) return 1; |
| 401 | nall = collect(h, all, 256); |
| 402 | rxc_host_close(h); |
| 403 | { |
| 404 | struct stat s5; |
| 405 | char cx5[400]; |
| 406 | snprintf(cx5, sizeof cx5, "%s/cortex.cx", home); |
| 407 | CHECK(stat(cx5, &s5) == 0, "T5b stat"); |
| 408 | CHECK(torn_case(home, "T5b torn past anchor", all, nall, (uint64_t)s5.st_size - 5, 1, |
| 409 | 0x4E50310000000007ull, &rp) == 0, "T5b held"); |
| 410 | } |
| 411 | |
| 412 | h = open_home(home, 0, NULL, &rc); |
| 413 | CHECK(h != NULL, "T5c open -> %d", rc); |
| 414 | if (!h) return 1; |
| 415 | CHECK(rxc_host_run(h, 0x4E50310000000008ull, 8000000, &rb) == RXC_HOST_OK && rb.committed, |
| 416 | "T5c run"); |
| 417 | nall = collect(h, all, 256); |
| 418 | rxc_host_close(h); |
| 419 | CHECK(torn_case(home, "T5c cut at a record boundary", all, nall, |
| 420 | record_start(all, nall, all[nall - 1].id), 0, 0x4E50310000000009ull, &rp) == 0, |
| 421 | "T5c held"); |
| 422 | |
| 423 | /* (d) a whole run's records cut at a boundary (fresh home): everything |
| 424 | * the last goal wrote, its state commit included, is gone while |
| 425 | * J-Space holds NEW. MEASURED limit: recover records the cut and |
| 426 | * reports that the composition still refuses (it neither adopts a |
| 427 | * state no record names nor overwrites committed J-Space history); |
| 428 | * the refusal is named and stable and the kept prefix is unchanged. */ |
| 429 | { |
| 430 | char home2[320]; |
| 431 | snprintf(home2, sizeof home2, "%s/home2", dir); |
| 432 | h = open_home(home2, 0, NULL, &rc); |
| 433 | CHECK(h != NULL, "T5d open -> %d", rc); |
| 434 | if (!h) return 1; |
| 435 | CHECK(rxc_host_run(h, 0x4E5031000000000Bull, 8500000, &rb) == RXC_HOST_OK && rb.committed, |
| 436 | "T5d run 1"); |
| 437 | uint32_t npre = collect(h, all, 256); |
| 438 | CHECK(rxc_host_run(h, 0x4E5031000000000Cull, 8600000, &rb) == RXC_HOST_OK && rb.committed, |
| 439 | "T5d run 2"); |
| 440 | nall = collect(h, all, 256); |
| 441 | rxc_host_close(h); |
| 442 | uint64_t cut = record_start(all, nall, npre + 1); |
| 443 | char cx[400]; |
| 444 | snprintf(cx, sizeof cx, "%s/cortex.cx", home2); |
| 445 | static uint8_t keep[1 << 16], now[1 << 16]; |
| 446 | FILE *f = fopen(cx, "rb"); |
| 447 | size_t got = f && cut <= sizeof keep ? fread(keep, 1, cut, f) : 0; |
| 448 | if (f) fclose(f); |
| 449 | CHECK(got == cut && truncate(cx, (off_t)cut) == 0, "T5d cut run 2 (records %u..%u)", npre + 1, |
| 450 | nall); |
| 451 | rc = rxc_host_recover(home2, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, &rp); |
| 452 | printf("T5d whole run cut: recover rc %d repaired %u cause %d anchor %llu kept %llu dropped %llu " |
| 453 | "event %llu opens %u open_rc %d\n", rc, rp.repaired, rp.cause, |
| 454 | (unsigned long long)rp.anchor_records, (unsigned long long)rp.records_kept, |
| 455 | (unsigned long long)rp.dropped_records, (unsigned long long)rp.event_id, rp.opens, |
| 456 | rp.open_rc); |
| 457 | CHECK(rc == RXC_HOST_E_REPLAY && rp.repaired == 1 && !rp.opens && rp.open_rc == -21 && |
| 458 | rp.event_id == npre + 1 && rp.dropped_records == nall - npre, |
| 459 | "T5d recover records the cut and names the refusal"); |
| 460 | RxcHostInfo i5d; |
| 461 | h = open_home(home2, 0, NULL, &rc); |
| 462 | int qrc = h ? rxc_host_info(h, &i5d) : -99; |
| 463 | CHECK(qrc == RXC_HOST_E_REPLAY, "T5d the refusal is stable and named -> %d", qrc); |
| 464 | rxc_host_close(h); |
| 465 | f = fopen(cx, "rb"); |
| 466 | got = f ? fread(now, 1, cut, f) : 0; |
| 467 | if (f) fclose(f); |
| 468 | CHECK(got == cut && memcmp(keep, now, cut) == 0, "T5d records 1..%u unchanged on disk", npre); |
| 469 | } |
| 470 | |
| 471 | /* ---- T6 recover needs the journal: a live handle blocks it ---- */ |
| 472 | h = open_home(home, 0, NULL, &rc); |
| 473 | CHECK(h != NULL, "T6 open -> %d", rc); |
| 474 | if (!h) return 1; |
| 475 | RxcHostInfo i6; |
| 476 | CHECK(rxc_host_info(h, &i6) == RXC_HOST_OK, "T6 info"); |
| 477 | CHECK(rxc_host_recover(home, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, &rp) == |
| 478 | RXC_HOST_E_OPEN && !rp.repaired, "T6 recover refused while a handle holds the home"); |
| 479 | |
| 480 | /* ---- T7 host records through the composition writer, recalled after restart ---- */ |
| 481 | RxcHostResult r7; |
| 482 | CHECK(rxc_host_run(h, 0x4E5031000000000Aull, 9000000, &r7) == RXC_HOST_OK && r7.committed, |
| 483 | "T7 run"); |
| 484 | static const char C7[] = "constraint: change only files under the workspace"; |
| 485 | static const char A7[] = "authorize: write notes.txt (expected approval 1 of 1)"; |
| 486 | static const char E7[] = "effect: wrote notes.txt sha256=..."; |
| 487 | uint64_t c7 = 0, a7 = 0, e7 = 0; |
| 488 | uint64_t lk[4] = { r7.cx_promotion, r7.cx_evidence, 0, 0 }; |
| 489 | CHECK(rxc_host_note(h, RXC_HOST_NOTE_CONSTRAINT, NULL, (const uint8_t *)C7, sizeof C7 - 1, &c7) == |
| 490 | RXC_HOST_OK && c7, "T7 constraint note %llu", (unsigned long long)c7); |
| 491 | lk[2] = c7; |
| 492 | CHECK(rxc_host_note(h, RXC_HOST_NOTE_AUTHORIZATION, lk, (const uint8_t *)A7, sizeof A7 - 1, &a7) == |
| 493 | RXC_HOST_OK && a7 > c7, "T7 authorization note %llu", (unsigned long long)a7); |
| 494 | lk[3] = a7; |
| 495 | CHECK(rxc_host_note(h, RXC_HOST_NOTE_EFFECT, lk, (const uint8_t *)E7, sizeof E7 - 1, &e7) == |
| 496 | RXC_HOST_OK && e7 > a7, "T7 effect note %llu", (unsigned long long)e7); |
| 497 | uint64_t bad[4] = { 1u << 30, 0, 0, 0 }; |
| 498 | uint64_t x7 = 0; |
| 499 | CHECK(rxc_host_note(h, RXC_HOST_NOTE_EFFECT, bad, (const uint8_t *)E7, 3, &x7) == |
| 500 | RXC_HOST_E_NOT_FOUND && x7 == 0, "T7 a link to a missing record is refused"); |
| 501 | CHECK(rxc_host_note(h, 99, NULL, (const uint8_t *)E7, 3, &x7) == RXC_HOST_E_ARG, |
| 502 | "T7 unknown note kind refused"); |
| 503 | static RxcHostRecord hb[MAXR], ha[MAXR]; |
| 504 | uint32_t nhb = 0, nha = 0; |
| 505 | uint64_t thb = 0, tha = 0; |
| 506 | CHECK(rxc_host_recall(h, RXC_HOST_SUBJECT_HOST, hb, MAXR, &nhb, &thb) == RXC_HOST_OK, |
| 507 | "T7 recall host subject"); |
| 508 | RxcHostInfo i7; |
| 509 | rxc_host_info(h, &i7); |
| 510 | rxc_host_close(h); |
| 511 | h = open_home(home, 0, NULL, &rc); |
| 512 | CHECK(h != NULL, "T7 reopen -> %d", rc); |
| 513 | if (!h) return 1; |
| 514 | RxcHostInfo i7b; |
| 515 | CHECK(rxc_host_info(h, &i7b) == RXC_HOST_OK && memcmp(i7b.machine_id, mid, 32) == 0, |
| 516 | "T7 same AienMachineId after restart"); |
| 517 | CHECK(rxc_host_recall(h, RXC_HOST_SUBJECT_HOST, ha, MAXR, &nha, &tha) == RXC_HOST_OK && |
| 518 | nha == nhb, "T7 recall after restart: %u host records (were %u)", nha, nhb); |
| 519 | int same7 = nha == nhb; |
| 520 | for (uint32_t i = 0; same7 && i < nha; i++) |
| 521 | same7 = ha[i].id == hb[i].id && !memcmp(ha[i].digest, hb[i].digest, 32) && ha[i].verified; |
| 522 | CHECK(same7, "T7 host records keep ids and digests across restart"); |
| 523 | uint64_t pw[RXC_HOST_NP_BYTES + 16]; |
| 524 | int pn = rxc_host_payload(h, c7, pw, RXC_HOST_NP_BYTES + 16); |
| 525 | char txt[129] = { 0 }; |
| 526 | if (pn > (int)RXC_HOST_NP_BYTES && pw[RXC_HOST_NP_LEN] < sizeof txt) |
| 527 | for (uint64_t i = 0; i < pw[RXC_HOST_NP_LEN]; i++) |
| 528 | txt[i] = (char)(pw[RXC_HOST_NP_BYTES + i / 8] >> (8 * (i % 8))); |
| 529 | CHECK(strcmp(txt, C7) == 0, "T7 constraint text round-trips: '%s'", txt); |
| 530 | RxcHostRecord ra; |
| 531 | CHECK(rxc_host_record(h, a7, &ra) == RXC_HOST_OK && ra.tag == RXC_HOST_NOTE_AUTHORIZATION && |
| 532 | ra.links[0] == r7.cx_promotion && ra.links[2] == c7, "T7 authorization cites its ids"); |
| 533 | printf("T7 host records: constraint %llu authorization %llu effect %llu; host subject %u\n", |
| 534 | (unsigned long long)c7, (unsigned long long)a7, (unsigned long long)e7, nha); |
| 535 | rxc_host_close(h); |
| 536 | |
| 537 | char m[17]; |
| 538 | hex8(mid, m); |
| 539 | printf("machine %s... records-before-restart %u\n", m, nb); |
| 540 | printf("rxc_host_abi_test: %d checks, %d failures: %s\n", checks, fails, fails ? "FAIL" : "PASS"); |
| 541 | char cmd[400]; |
| 542 | snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir); |
| 543 | if (!fails && system(cmd) != 0) printf("note: could not remove %s\n", dir); |
| 544 | return fails ? 1 : 0; |
| 545 | } |