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_c4_requal.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * rx_c4_requal.c -- C4 local-runtime requalification, interruption and recovery. |
| 19 | * |
| 20 | * ONE repeatable task: the verified-scale ledger. Three goals (inputs 5, 6, 7) |
| 21 | * each go through the whole COMPOSITION-2 path in one World: |
| 22 | * World -> J-Space (fork two staged branches) -> AEGIS (contract check, |
| 23 | * one candidate deliberately breaks the contract) -> World commit (seal) |
| 24 | * -> settle -> Cortex (claims, evidence, promotion), durable on disk. |
| 25 | * The committed state after each goal is a signature (World state ref, J-Space |
| 26 | * content digest, state result, Cortex promotion count, chain verified, exactly |
| 27 | * one live branch). The baseline run records the signature after goal 0..3. |
| 28 | * |
| 29 | * Injections (each in a fresh directory): |
| 30 | * crash : fault point p (one per stage boundary) x goal g: the child exits |
| 31 | * at the point (test-build hook), the parent reopens. |
| 32 | * kill : the child runs the whole task, the parent SIGKILLs it after a |
| 33 | * delay (a sweep across the run), the parent reopens. |
| 34 | * corrupt : the finished directory is damaged (truncate, flip a byte, zero |
| 35 | * fill, delete, append junk) in the Cortex journal, in every |
| 36 | * J-Space file, and in machine.id, then reopened; plus a foreign |
| 37 | * machine identity, and a single-byte flip sweep of the J-Space |
| 38 | * checkpoint, machine.id and (every 7th byte of) the journal. |
| 39 | * Acceptance, written before the run: |
| 40 | * crash/kill : reopen succeeds and the signature equals exactly one baseline |
| 41 | * signature (crash: the one the point implies); the task then |
| 42 | * resumes from it and reaches the baseline final signature, also |
| 43 | * after one more close/reopen. |
| 44 | * corrupt : reopen REFUSES (nonzero, repeatable); or the damage is |
| 45 | * detected when the state is read (J-Space corrupt error); or |
| 46 | * the state and every Cortex record equal the pristine reopen |
| 47 | * (UNCHANGED, benign damage); or a torn-looking tail was cut and |
| 48 | * re-completed with a visible RECOVERED admission and the same |
| 49 | * state and promotions (REPAIRED_VISIBLY). Anything else is a |
| 50 | * silent divergence and FAILS. |
| 51 | * |
| 52 | * Needs -DAIEN_TEST_BUILD=1 (crash points). usage: rx_c4_requal <receipt.json> |
| 53 | */ |
| 54 | #include "rx_compose_fixture.h" |
| 55 | #include "sha256.h" |
| 56 | |
| 57 | #include <dirent.h> |
| 58 | #include <stdarg.h> |
| 59 | #include <errno.h> |
| 60 | #include <fcntl.h> |
| 61 | #include <ftw.h> |
| 62 | #include <signal.h> |
| 63 | #include <sys/stat.h> |
| 64 | #include <sys/wait.h> |
| 65 | #include <time.h> |
| 66 | #include <unistd.h> |
| 67 | |
| 68 | #ifndef RXC_TEST_HOOKS |
| 69 | #error "rx_c4_requal needs -DAIEN_TEST_BUILD=1" |
| 70 | #endif |
| 71 | |
| 72 | #define NGOAL 3 |
| 73 | static const uint64_t GOALS[NGOAL] = { 5, 6, 7 }; |
| 74 | |
| 75 | static Fx g_fx; |
| 76 | static RxCompose g_c; |
| 77 | static char g_base[128]; |
| 78 | |
| 79 | typedef struct { |
| 80 | int ok; /* chain verified, one live branch, digest readable */ |
| 81 | int digest_rc; /* js_branch_content_digest result */ |
| 82 | JsBranchRef ref; |
| 83 | uint8_t dg[32]; |
| 84 | uint64_t result; |
| 85 | uint32_t promos; |
| 86 | uint64_t nrec; |
| 87 | uint32_t radm; /* RECOVERED admissions: records re-completed after a torn tail */ |
| 88 | uint8_t rec[32]; /* Cortex record digest (all records, timing excluded) */ |
| 89 | } Sig; |
| 90 | static Sig BASE[NGOAL + 1]; |
| 91 | static uint8_t REOPEN_REC[32]; /* record digest after ONE reopen of the pristine finished directory */ |
| 92 | static uint64_t REOPEN_NREC; /* and its record count */ |
| 93 | |
| 94 | typedef struct { |
| 95 | char name[96]; |
| 96 | char klass[24]; /* RECOVERED / REFUSED / DETECTED_ON_READ / UNCHANGED / DIVERGED / FAULT_NOT_FIRED */ |
| 97 | int pass; |
| 98 | char detail[200]; |
| 99 | } Case; |
| 100 | #define MAXCASE 400 |
| 101 | static Case g_case[MAXCASE]; |
| 102 | static int g_ncase; |
| 103 | |
| 104 | static int rm_one(const char *p, const struct stat *sb, int fl, struct FTW *w) { |
| 105 | (void)sb; (void)fl; (void)w; |
| 106 | return remove(p); |
| 107 | } |
| 108 | static void rmtree(const char *p) { nftw(p, rm_one, 16, FTW_DEPTH | FTW_PHYS); } |
| 109 | |
| 110 | static void reset_hooks(void) { |
| 111 | g_c.test.fault_point = RXC_FP_NONE; |
| 112 | g_c.test.fault_crash = 0; |
| 113 | g_c.test.fault_k = 0; |
| 114 | g_c.test.rogue_candidate = 0; |
| 115 | } |
| 116 | |
| 117 | static int sig_eq(const Sig *a, const Sig *b) { |
| 118 | /* The branch ref (slot id, generation) is an allocator artifact that differs |
| 119 | * after a crash; the state is its content digest, the World result and the |
| 120 | * Cortex promotion count. */ |
| 121 | return a->result == b->result && |
| 122 | a->promos == b->promos && memcmp(a->dg, b->dg, 32) == 0; |
| 123 | } |
| 124 | |
| 125 | /* Which fields of a differ from b (for failure details). */ |
| 126 | static void sig_diff(const Sig *a, const Sig *b, char *buf, size_t n) { |
| 127 | snprintf(buf, n, "ref %u/%u vs %u/%u; result %llu vs %llu; promos %u vs %u; digest %s; ok=%d", |
| 128 | a->ref.id, a->ref.gen, b->ref.id, b->ref.gen, (unsigned long long)a->result, |
| 129 | (unsigned long long)b->result, a->promos, b->promos, |
| 130 | memcmp(a->dg, b->dg, 32) ? "differs" : "same", a->ok); |
| 131 | } |
| 132 | |
| 133 | static void sig_take(RxCompose *c, Sig *s) { |
| 134 | memset(s, 0, sizeof *s); |
| 135 | s->ref = rx_compose_state(c); |
| 136 | s->digest_rc = js_branch_content_digest(&c->js, s->ref.id, s->dg); |
| 137 | RxObject so; |
| 138 | memset(&so, 0, sizeof so); |
| 139 | rx_world_read(&c->w, c->state, &so); |
| 140 | s->result = so.field[RXC_S_RESULT]; |
| 141 | s->promos = fx_count(&c->cx, CX_K_PROMOTION, UINT64_MAX); |
| 142 | rx_compose_record_digest(&c->cx, s->rec); |
| 143 | s->nrec = c->cx.n; |
| 144 | s->radm = fx_count(&c->cx, CX_K_ADMISSION, RXC_ADMIT_RECOVERED); |
| 145 | s->ok = cx_verify_chain(&c->cx) == CX_OK && fx_live_branches(&c->js) == 1 && s->digest_rc == JS_OK; |
| 146 | } |
| 147 | |
| 148 | static int open_dir(const char *d) { |
| 149 | memset(&g_c, 0, sizeof g_c); |
| 150 | return fx_open(&g_fx, &g_c, d, 1); |
| 151 | } |
| 152 | |
| 153 | /* Index j of the baseline signature equal to s, or -1. */ |
| 154 | static int match_base(const Sig *s) { |
| 155 | for (int j = 0; j <= NGOAL; j++) |
| 156 | if (s->ok && sig_eq(s, &BASE[j])) return j; |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | static void add_case(const char *name, const char *klass, int pass, const char *fmt, ...) { |
| 161 | if (g_ncase >= MAXCASE) { fprintf(stderr, "too many cases\n"); exit(2); } |
| 162 | Case *c = &g_case[g_ncase++]; |
| 163 | snprintf(c->name, sizeof c->name, "%s", name); |
| 164 | snprintf(c->klass, sizeof c->klass, "%s", klass); |
| 165 | c->pass = pass; |
| 166 | va_list ap; |
| 167 | va_start(ap, fmt); |
| 168 | vsnprintf(c->detail, sizeof c->detail, fmt, ap); |
| 169 | va_end(ap); |
| 170 | } |
| 171 | |
| 172 | /* Baseline: run the whole task once in `d`, filling sig[]. */ |
| 173 | static int run_task(const char *d, Sig *sig) { |
| 174 | if (open_dir(d) != RX_OK) return -1; |
| 175 | sig_take(&g_c, &sig[0]); |
| 176 | for (int i = 0; i < NGOAL; i++) { |
| 177 | RxcResult o; |
| 178 | fx_a_bad = 1; |
| 179 | int rc = fx_run(&g_fx, &g_c, GOALS[i], &o); |
| 180 | fx_a_bad = 0; |
| 181 | if (rc != RX_OK || o.outcome != RXC_OUT_COMMITTED || o.result != GOALS[i] * 3 + 1) { |
| 182 | fx_close(&g_fx, &g_c); |
| 183 | return -2; |
| 184 | } |
| 185 | sig_take(&g_c, &sig[i + 1]); |
| 186 | } |
| 187 | fx_close(&g_fx, &g_c); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* After a recovery landed on baseline index j: finish the task from goal j, |
| 192 | * check the final signature, close, reopen, check again. Returns 1 on success. */ |
| 193 | static int resume_and_check(const char *d, int j, char *why, size_t wn) { |
| 194 | for (int i = j; i < NGOAL; i++) { |
| 195 | RxcResult o; |
| 196 | fx_a_bad = 1; |
| 197 | int rc = fx_run(&g_fx, &g_c, GOALS[i], &o); |
| 198 | fx_a_bad = 0; |
| 199 | if (rc != RX_OK || o.outcome != RXC_OUT_COMMITTED) { |
| 200 | snprintf(why, wn, "resume goal %d rc=%d outcome=%d", i, rc, o.outcome); |
| 201 | fx_close(&g_fx, &g_c); |
| 202 | return 0; |
| 203 | } |
| 204 | } |
| 205 | Sig f; |
| 206 | sig_take(&g_c, &f); |
| 207 | fx_close(&g_fx, &g_c); |
| 208 | if (!f.ok || !sig_eq(&f, &BASE[NGOAL])) { |
| 209 | { char dd[160]; sig_diff(&f, &BASE[NGOAL], dd, sizeof dd); snprintf(why, wn, "final differs: %.150s", dd); } |
| 210 | return 0; |
| 211 | } |
| 212 | if (open_dir(d) != RX_OK) { snprintf(why, wn, "second reopen failed"); return 0; } |
| 213 | sig_take(&g_c, &f); |
| 214 | fx_close(&g_fx, &g_c); |
| 215 | if (!f.ok || !sig_eq(&f, &BASE[NGOAL])) { snprintf(why, wn, "second reopen differs"); return 0; } |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | /* Reopen after an interruption; acceptance: recovered signature is baseline |
| 220 | * index `expect` (-1 = any index), then resume reaches the baseline final. */ |
| 221 | static void recover_case(const char *name, const char *d, int expect) { |
| 222 | char why[200] = ""; |
| 223 | int rc = open_dir(d); |
| 224 | if (rc != RX_OK) { |
| 225 | add_case(name, "REFUSED", 0, "interruption must be recoverable, open rc=%d", rc); |
| 226 | return; |
| 227 | } |
| 228 | Sig s; |
| 229 | sig_take(&g_c, &s); |
| 230 | int j = match_base(&s); |
| 231 | if (j < 0) { |
| 232 | fx_close(&g_fx, &g_c); |
| 233 | { char dd[160]; sig_diff(&s, &BASE[0], dd, sizeof dd); add_case(name, "DIVERGED", 0, "matches no baseline; vs genesis: %s", dd); } |
| 234 | return; |
| 235 | } |
| 236 | if (expect >= 0 && j != expect) { |
| 237 | fx_close(&g_fx, &g_c); |
| 238 | add_case(name, "DIVERGED", 0, "recovered to baseline %d, implied %d", j, expect); |
| 239 | return; |
| 240 | } |
| 241 | if (!resume_and_check(d, j, why, sizeof why)) { |
| 242 | add_case(name, "DIVERGED", 0, "%s", why); |
| 243 | return; |
| 244 | } |
| 245 | add_case(name, "RECOVERED", 1, "recovered to baseline %d, resumed to final, 2nd reopen equal", j); |
| 246 | } |
| 247 | |
| 248 | static const char *fp_name[RXC_FP_END] = { "none", "before_fork", "candidate", "before_commit", |
| 249 | "after_validation", "seal", "reclaim", "cortex" }; |
| 250 | |
| 251 | static void mkcasedir(char *out, size_t n, const char *leaf) { |
| 252 | snprintf(out, n, "%s/%s", g_base, leaf); |
| 253 | rmtree(out); |
| 254 | } |
| 255 | |
| 256 | static void crash_case(int point, int g) { |
| 257 | char d[200], name[96], leaf[64]; |
| 258 | snprintf(name, sizeof name, "crash/%s/goal%d", fp_name[point], g); |
| 259 | snprintf(leaf, sizeof leaf, "crash_%d_%d", point, g); |
| 260 | mkcasedir(d, sizeof d, leaf); |
| 261 | fflush(NULL); |
| 262 | pid_t pid = fork(); |
| 263 | if (pid == 0) { |
| 264 | if (open_dir(d) != RX_OK) _exit(2); |
| 265 | for (int i = 0; i < g; i++) { |
| 266 | RxcResult o; |
| 267 | fx_a_bad = 1; |
| 268 | int rc = fx_run(&g_fx, &g_c, GOALS[i], &o); |
| 269 | fx_a_bad = 0; |
| 270 | if (rc != RX_OK || o.outcome != RXC_OUT_COMMITTED) _exit(4); |
| 271 | } |
| 272 | reset_hooks(); |
| 273 | g_c.test.fault_point = point; |
| 274 | g_c.test.fault_crash = 1; |
| 275 | g_c.test.fault_k = 0; |
| 276 | RxcResult o; |
| 277 | fx_a_bad = 1; |
| 278 | fx_run(&g_fx, &g_c, GOALS[g], &o); |
| 279 | _exit(3); /* fault did not fire */ |
| 280 | } |
| 281 | int st = 0; |
| 282 | waitpid(pid, &st, 0); |
| 283 | if (!(WIFEXITED(st) && WEXITSTATUS(st) == RXC_CRASH_EXIT)) { |
| 284 | add_case(name, "FAULT_NOT_FIRED", 0, "child status %d", WIFEXITED(st) ? WEXITSTATUS(st) : -1); |
| 285 | rmtree(d); |
| 286 | return; |
| 287 | } |
| 288 | recover_case(name, d, point == RXC_FP_CORTEX ? g + 1 : g); |
| 289 | rmtree(d); |
| 290 | } |
| 291 | |
| 292 | static double now_s(void) { |
| 293 | struct timespec t; |
| 294 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 295 | return t.tv_sec + t.tv_nsec * 1e-9; |
| 296 | } |
| 297 | |
| 298 | static void child_full_run(const char *d) { |
| 299 | Sig tmp[NGOAL + 1]; |
| 300 | _exit(run_task(d, tmp) == 0 ? 0 : 5); |
| 301 | } |
| 302 | |
| 303 | static void kill_sweep(int n) { |
| 304 | char d[200]; |
| 305 | mkcasedir(d, sizeof d, "kill_t"); |
| 306 | fflush(NULL); |
| 307 | double t0 = now_s(); |
| 308 | pid_t pid = fork(); |
| 309 | if (pid == 0) child_full_run(d); |
| 310 | int st; |
| 311 | waitpid(pid, &st, 0); |
| 312 | double T = now_s() - t0; |
| 313 | rmtree(d); |
| 314 | for (int i = 0; i < n; i++) { |
| 315 | char name[96], leaf[64]; |
| 316 | snprintf(name, sizeof name, "kill/sigkill@%d_of_%d", i + 1, n); |
| 317 | snprintf(leaf, sizeof leaf, "kill_%d", i); |
| 318 | mkcasedir(d, sizeof d, leaf); |
| 319 | fflush(NULL); |
| 320 | pid = fork(); |
| 321 | if (pid == 0) child_full_run(d); |
| 322 | double delay = T * 1.1 * (i + 1) / n; |
| 323 | struct timespec ts = { (time_t)delay, (long)((delay - (time_t)delay) * 1e9) }; |
| 324 | nanosleep(&ts, NULL); |
| 325 | kill(pid, SIGKILL); |
| 326 | waitpid(pid, &st, 0); |
| 327 | /* A child that already finished is a valid (complete) case. */ |
| 328 | recover_case(name, d, -1); |
| 329 | rmtree(d); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* ---- corruption of a finished directory ---------------------------------- */ |
| 334 | |
| 335 | typedef enum { OP_TRUNC_BY, OP_TRUNC_TO, OP_TRUNC_HALF, OP_FLIP_MID, OP_FLIP_FIRST, OP_FLIP_LAST, |
| 336 | OP_ZERO_TAIL, OP_DELETE, OP_APPEND_JUNK, OP_FLIP_PM, OP_FLIP_ABS } Op; |
| 337 | static const char *op_name[] = { "truncate_by", "truncate_to", "truncate_half", "flip_mid", |
| 338 | "flip_first", "flip_last", "zero_tail", "delete", "append_junk", "flip_permille", "flip_byte" }; |
| 339 | |
| 340 | static int apply_op(const char *path, Op op, long arg) { |
| 341 | struct stat sb; |
| 342 | if (stat(path, &sb) != 0) return -1; |
| 343 | off_t n = sb.st_size; |
| 344 | if (op == OP_DELETE) return unlink(path); |
| 345 | int fd = open(path, O_RDWR); |
| 346 | if (fd < 0) return -1; |
| 347 | int rc = 0; |
| 348 | uint8_t b; |
| 349 | switch (op) { |
| 350 | case OP_TRUNC_BY: rc = ftruncate(fd, n > arg ? n - arg : 0); break; |
| 351 | case OP_TRUNC_TO: rc = ftruncate(fd, arg); break; |
| 352 | case OP_TRUNC_HALF: rc = ftruncate(fd, n / 2); break; |
| 353 | case OP_FLIP_MID: case OP_FLIP_FIRST: case OP_FLIP_LAST: case OP_FLIP_PM: case OP_FLIP_ABS: { |
| 354 | off_t at = op == OP_FLIP_MID ? n / 2 : op == OP_FLIP_FIRST ? 0 : op == OP_FLIP_PM ? n * arg / 1000 : op == OP_FLIP_ABS ? arg : n - 1; |
| 355 | if (n == 0 || pread(fd, &b, 1, at) != 1) { rc = -1; break; } |
| 356 | b ^= 0x40; |
| 357 | rc = pwrite(fd, &b, 1, at) == 1 ? 0 : -1; |
| 358 | break; |
| 359 | } |
| 360 | case OP_ZERO_TAIL: { |
| 361 | uint8_t z[64] = { 0 }; |
| 362 | off_t at = n > 64 ? n - 64 : 0; |
| 363 | rc = pwrite(fd, z, n > 64 ? 64 : (size_t)n, at) >= 0 ? 0 : -1; |
| 364 | break; |
| 365 | } |
| 366 | case OP_APPEND_JUNK: { |
| 367 | uint8_t j[64]; |
| 368 | for (int i = 0; i < 64; i++) j[i] = (uint8_t)(0xA5 ^ i); |
| 369 | rc = pwrite(fd, j, sizeof j, n) == (ssize_t)sizeof j ? 0 : -1; |
| 370 | break; |
| 371 | } |
| 372 | default: rc = -1; |
| 373 | } |
| 374 | if (rc == 0) fsync(fd); |
| 375 | close(fd); |
| 376 | return rc; |
| 377 | } |
| 378 | |
| 379 | /* Open a damaged finished directory and classify what happened. Returns 1 if |
| 380 | * acceptable (refused stably, detected on read, or nothing committed changed). */ |
| 381 | static int classify_dir(const char *d, const char **klass, char *detail, size_t dn) { |
| 382 | int rc = open_dir(d); |
| 383 | if (rc != RX_OK) { |
| 384 | int rc2 = open_dir(d); /* a refusal must be stable */ |
| 385 | if (rc2 == RX_OK) fx_close(&g_fx, &g_c); |
| 386 | *klass = "REFUSED"; |
| 387 | snprintf(detail, dn, "open rc=%d, again rc=%d", rc, rc2); |
| 388 | return rc2 == rc; |
| 389 | } |
| 390 | Sig s; |
| 391 | sig_take(&g_c, &s); |
| 392 | fx_close(&g_fx, &g_c); |
| 393 | if (s.digest_rc != JS_OK) { |
| 394 | *klass = "DETECTED_ON_READ"; |
| 395 | snprintf(detail, dn, "open ok, state read refused (js rc=%d)", s.digest_rc); |
| 396 | return 1; |
| 397 | } |
| 398 | if (s.ok && sig_eq(&s, &BASE[NGOAL]) && memcmp(s.rec, REOPEN_REC, 32) == 0) { |
| 399 | *klass = "UNCHANGED"; |
| 400 | snprintf(detail, dn, "open ok, state and every Cortex record equal the committed final state"); |
| 401 | return 1; |
| 402 | } |
| 403 | /* A flip that makes a record look torn gets the log cut there; recovery repairs |
| 404 | * it by re-completing the composition records and leaves a RECOVERED admission |
| 405 | * as visible evidence. Same state and promotions, bounded record count. */ |
| 406 | if (s.ok && sig_eq(&s, &BASE[NGOAL]) && s.radm >= 1 && s.nrec >= REOPEN_NREC && s.nrec <= REOPEN_NREC + 2) { |
| 407 | *klass = "REPAIRED_VISIBLY"; |
| 408 | snprintf(detail, dn, "torn-looking tail cut and re-completed: state and promotions equal, %u RECOVERED admission(s), records %llu vs %llu", |
| 409 | s.radm, (unsigned long long)s.nrec, (unsigned long long)REOPEN_NREC); |
| 410 | return 1; |
| 411 | } |
| 412 | *klass = "DIVERGED"; |
| 413 | snprintf(detail, dn, "open ok but state or Cortex records differ from the committed final state (j=%d result=%llu promos=%u ok=%d records=%llu vs pristine-reopen %llu)", |
| 414 | match_base(&s), (unsigned long long)s.result, s.promos, s.ok, (unsigned long long)s.nrec, |
| 415 | (unsigned long long)REOPEN_NREC); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static void corrupt_case(const char *rel, Op op, long arg) { |
| 420 | char d[200], name[128], path[400], detail[220]; |
| 421 | const char *klass; |
| 422 | if (op == OP_TRUNC_BY || op == OP_TRUNC_TO || op == OP_FLIP_PM || op == OP_FLIP_ABS) snprintf(name, sizeof name, "corrupt/%s/%s_%ld", rel, op_name[op], arg); |
| 423 | else snprintf(name, sizeof name, "corrupt/%s/%s", rel, op_name[op]); |
| 424 | mkcasedir(d, sizeof d, "corrupt"); |
| 425 | Sig tmp[NGOAL + 1]; |
| 426 | if (run_task(d, tmp) != 0) { add_case(name, "DIVERGED", 0, "could not build the finished directory"); return; } |
| 427 | snprintf(path, sizeof path, "%s/%s", d, rel); |
| 428 | /* A torn Cortex tail is what a crash leaves; classify_dir accepts it only as |
| 429 | * REPAIRED_VISIBLY. Every other damage must leave every record byte-identical |
| 430 | * or be refused. */ |
| 431 | if (apply_op(path, op, arg) != 0) { |
| 432 | add_case(name, "NOT_APPLICABLE", 1, "operation not applicable (file is empty or absent in this task)"); |
| 433 | rmtree(d); |
| 434 | return; |
| 435 | } |
| 436 | int ok = classify_dir(d, &klass, detail, sizeof detail); |
| 437 | add_case(name, klass, ok, "%s", detail); |
| 438 | rmtree(d); |
| 439 | } |
| 440 | |
| 441 | /* Exhaustive single-byte sweep of one file: flip every `stride`-th byte (each |
| 442 | * from a pristine copy of the directory), reopen, classify. One summary case. */ |
| 443 | typedef struct { char rel[300]; uint8_t *b; size_t n; } Snap; |
| 444 | static int snap_dir(const char *d, Snap *s, int max) { |
| 445 | static const char *fixed[] = { "machine.id", "cortex.cx" }; |
| 446 | char names[16][300]; |
| 447 | int k = 0; |
| 448 | for (size_t i = 0; i < 2; i++) snprintf(names[k++], sizeof names[0], "%s", fixed[i]); |
| 449 | char jd[260]; |
| 450 | snprintf(jd, sizeof jd, "%s/jspace", d); |
| 451 | DIR *dir = opendir(jd); |
| 452 | struct dirent *e; |
| 453 | while (dir && (e = readdir(dir)) && k < 14) |
| 454 | if (e->d_name[0] != '.') snprintf(names[k++], sizeof names[0], "jspace/%.200s", e->d_name); |
| 455 | if (dir) closedir(dir); |
| 456 | int ns = 0; |
| 457 | for (int i = 0; i < k && ns < max; i++) { |
| 458 | char p[600]; |
| 459 | snprintf(p, sizeof p, "%.200s/%.290s", d, names[i]); |
| 460 | struct stat sb; |
| 461 | if (stat(p, &sb) != 0) continue; |
| 462 | FILE *f = fopen(p, "rb"); |
| 463 | if (!f) continue; |
| 464 | s[ns].b = malloc(sb.st_size ? (size_t)sb.st_size : 1); |
| 465 | s[ns].n = fread(s[ns].b, 1, sb.st_size, f); |
| 466 | fclose(f); |
| 467 | snprintf(s[ns].rel, sizeof s[ns].rel, "%.290s", names[i]); |
| 468 | ns++; |
| 469 | } |
| 470 | return ns; |
| 471 | } |
| 472 | static void restore_dir(const char *d, const Snap *s, int ns) { |
| 473 | for (int i = 0; i < ns; i++) { |
| 474 | char p[600]; |
| 475 | snprintf(p, sizeof p, "%.200s/%.290s", d, s[i].rel); |
| 476 | FILE *f = fopen(p, "wb"); |
| 477 | if (!f) continue; |
| 478 | fwrite(s[i].b, 1, s[i].n, f); |
| 479 | fclose(f); |
| 480 | } |
| 481 | } |
| 482 | static void byte_sweep(const char *rel, size_t stride) { |
| 483 | char d[200], name[128], path[600], detail[220]; |
| 484 | snprintf(name, sizeof name, "sweep/%s/flip_every_%zu_bytes", rel, stride); |
| 485 | mkcasedir(d, sizeof d, "sweep"); |
| 486 | Sig tmp[NGOAL + 1]; |
| 487 | if (run_task(d, tmp) != 0) { add_case(name, "DIVERGED", 0, "could not build the finished directory"); return; } |
| 488 | Snap snap[16]; |
| 489 | int ns = snap_dir(d, snap, 16); |
| 490 | snprintf(path, sizeof path, "%s/%s", d, rel); |
| 491 | struct stat sb; |
| 492 | if (stat(path, &sb) != 0 || sb.st_size == 0) { |
| 493 | add_case(name, "NOT_APPLICABLE", 1, "file is empty or absent in this task"); |
| 494 | } else { |
| 495 | size_t n = (size_t)sb.st_size; |
| 496 | unsigned refused = 0, detected = 0, unchanged = 0, diverged = 0, tried = 0; |
| 497 | char first_bad[120] = ""; |
| 498 | for (size_t i = 0; i < n; i += stride) { |
| 499 | restore_dir(d, snap, ns); |
| 500 | if (apply_op(path, OP_FLIP_ABS, (long)i) != 0) continue; |
| 501 | const char *klass; |
| 502 | int ok = classify_dir(d, &klass, detail, sizeof detail); |
| 503 | tried++; |
| 504 | if (!ok) { diverged++; if (getenv("C4_VERBOSE")) fprintf(stderr, "sweep divergence at %zu: %s\n", i, detail); if (!first_bad[0]) snprintf(first_bad, sizeof first_bad, "first at offset %zu", i); } |
| 505 | else if (!strcmp(klass, "REFUSED")) refused++; |
| 506 | else if (!strcmp(klass, "DETECTED_ON_READ")) detected++; |
| 507 | else unchanged++; |
| 508 | } |
| 509 | add_case(name, diverged ? "DIVERGED" : "SWEEP", diverged == 0, |
| 510 | "%zu-byte file, %u flips: refused %u, detected on read %u, unchanged (benign) %u, silent divergence %u %s", |
| 511 | n, tried, refused, detected, unchanged, diverged, first_bad); |
| 512 | } |
| 513 | for (int i = 0; i < ns; i++) free(snap[i].b); |
| 514 | rmtree(d); |
| 515 | } |
| 516 | |
| 517 | static void corrupt_all(void) { |
| 518 | static const struct { Op op; long arg; } cx_ops[] = { |
| 519 | { OP_TRUNC_BY, 1 }, { OP_TRUNC_BY, 40 }, { OP_TRUNC_BY, 400 }, { OP_TRUNC_HALF, 0 }, |
| 520 | { OP_TRUNC_TO, 0 }, { OP_TRUNC_TO, 24 }, { OP_FLIP_FIRST, 0 }, { OP_FLIP_MID, 0 }, |
| 521 | { OP_FLIP_LAST, 0 }, { OP_ZERO_TAIL, 0 }, { OP_APPEND_JUNK, 0 }, { OP_DELETE, 0 }, |
| 522 | { OP_FLIP_PM, 125 }, { OP_FLIP_PM, 250 }, { OP_FLIP_PM, 375 }, { OP_FLIP_PM, 625 }, |
| 523 | { OP_FLIP_PM, 750 }, { OP_FLIP_PM, 875 } }; |
| 524 | for (size_t i = 0; i < sizeof cx_ops / sizeof cx_ops[0]; i++) |
| 525 | corrupt_case("cortex.cx", cx_ops[i].op, cx_ops[i].arg); |
| 526 | static const struct { Op op; long arg; } mid_ops[] = { { OP_FLIP_FIRST, 0 }, { OP_DELETE, 0 } }; |
| 527 | for (size_t i = 0; i < 2; i++) corrupt_case("machine.id", mid_ops[i].op, mid_ops[i].arg); |
| 528 | /* every J-Space file in a finished directory */ |
| 529 | char d[200], jd[260]; |
| 530 | mkcasedir(d, sizeof d, "corrupt_list"); |
| 531 | Sig tmp[NGOAL + 1]; |
| 532 | if (run_task(d, tmp) != 0) { add_case("corrupt/jspace/list", "DIVERGED", 0, "no directory"); return; } |
| 533 | snprintf(jd, sizeof jd, "%s/jspace", d); |
| 534 | char names[16][256]; |
| 535 | int nn = 0; |
| 536 | DIR *dir = opendir(jd); |
| 537 | struct dirent *e; |
| 538 | while (dir && (e = readdir(dir)) && nn < 16) |
| 539 | if (e->d_name[0] != '.') snprintf(names[nn++], sizeof names[0], "%.200s", e->d_name); |
| 540 | if (dir) closedir(dir); |
| 541 | rmtree(d); |
| 542 | static const struct { Op op; long arg; } js_ops[] = { |
| 543 | { OP_TRUNC_HALF, 0 }, { OP_TRUNC_BY, 1 }, { OP_TRUNC_TO, 0 }, { OP_FLIP_FIRST, 0 }, |
| 544 | { OP_FLIP_MID, 0 }, { OP_FLIP_LAST, 0 }, { OP_ZERO_TAIL, 0 }, { OP_DELETE, 0 }, |
| 545 | { OP_FLIP_ABS, 20 }, { OP_FLIP_ABS, 70 }, { OP_FLIP_ABS, 100 }, { OP_FLIP_ABS, 130 }, |
| 546 | { OP_FLIP_PM, 125 }, { OP_FLIP_PM, 250 }, { OP_FLIP_PM, 375 }, { OP_FLIP_PM, 625 }, |
| 547 | { OP_FLIP_PM, 750 }, { OP_FLIP_PM, 875 } }; |
| 548 | for (int f = 0; f < nn; f++) |
| 549 | for (size_t i = 0; i < sizeof js_ops / sizeof js_ops[0]; i++) { |
| 550 | char rel[300]; |
| 551 | snprintf(rel, sizeof rel, "jspace/%.200s", names[f]); |
| 552 | corrupt_case(rel, js_ops[i].op, js_ops[i].arg); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* A directory made on one machine is refused by another identity, and the |
| 557 | * refusal changes nothing (the owner still opens it and sees the final state). */ |
| 558 | static void identity_case(void) { |
| 559 | char d[200]; |
| 560 | mkcasedir(d, sizeof d, "identity"); |
| 561 | Sig tmp[NGOAL + 1]; |
| 562 | if (run_task(d, tmp) != 0) { add_case("identity/foreign_machine", "DIVERGED", 0, "no directory"); return; } |
| 563 | AienMachineId mine = g_fx.self; |
| 564 | g_fx.self = fx_mid(2); |
| 565 | int rc = open_dir(d); |
| 566 | g_fx.self = mine; |
| 567 | if (rc == RX_OK) fx_close(&g_fx, &g_c); |
| 568 | Sig s; |
| 569 | int rc2 = open_dir(d); |
| 570 | int same = 0; |
| 571 | if (rc2 == RX_OK) { sig_take(&g_c, &s); fx_close(&g_fx, &g_c); same = s.ok && sig_eq(&s, &BASE[NGOAL]); } |
| 572 | add_case("identity/foreign_machine", rc == RX_ERR_IDENTITY ? "REFUSED" : "DIVERGED", |
| 573 | rc == RX_ERR_IDENTITY && same, "foreign open rc=%d (want %d), owner reopen same=%d", rc, |
| 574 | RX_ERR_IDENTITY, same); |
| 575 | rmtree(d); |
| 576 | } |
| 577 | |
| 578 | /* Stale J-Space: put back the J-Space checkpoint from after goal `k` while the |
| 579 | * Cortex journal (and machine.id) are from after the last goal. Cortex has |
| 580 | * already promoted the later goals, which only happens after their state is |
| 581 | * durable, so a checkpoint that lost them is not a crash window: it must be |
| 582 | * refused, never silently rolled back. */ |
| 583 | static void stale_case(int k) { |
| 584 | char d[200], name[96]; |
| 585 | snprintf(name, sizeof name, "stale/jspace_checkpoint_from_after_goal%d", k); |
| 586 | mkcasedir(d, sizeof d, "stale"); |
| 587 | Snap snap[16]; |
| 588 | int ns = 0; |
| 589 | if (open_dir(d) != RX_OK) { add_case(name, "DIVERGED", 0, "open failed"); return; } |
| 590 | for (int pass = 0; pass < 2; pass++) { |
| 591 | for (int i = pass ? k : 0; i < (pass ? NGOAL : k); i++) { |
| 592 | RxcResult o; |
| 593 | fx_a_bad = 1; |
| 594 | int rc = fx_run(&g_fx, &g_c, GOALS[i], &o); |
| 595 | fx_a_bad = 0; |
| 596 | if (rc != RX_OK || o.outcome != RXC_OUT_COMMITTED) { fx_close(&g_fx, &g_c); add_case(name, "DIVERGED", 0, "setup goal %d failed", i); rmtree(d); return; } |
| 597 | } |
| 598 | fx_close(&g_fx, &g_c); |
| 599 | if (pass == 0) { |
| 600 | ns = snap_dir(d, snap, 16); |
| 601 | if (open_dir(d) != RX_OK) { add_case(name, "DIVERGED", 0, "reopen failed"); return; } |
| 602 | } |
| 603 | } |
| 604 | int nj = 0; |
| 605 | for (int i = 0; i < ns; i++) |
| 606 | if (!strncmp(snap[i].rel, "jspace/", 7)) snap[nj++] = snap[i]; |
| 607 | restore_dir(d, snap, nj); |
| 608 | int rc = open_dir(d); |
| 609 | if (rc != RX_OK) { |
| 610 | int rc2 = open_dir(d); |
| 611 | if (rc2 == RX_OK) fx_close(&g_fx, &g_c); |
| 612 | add_case(name, "REFUSED", rc2 == rc, "open rc=%d, again rc=%d", rc, rc2); |
| 613 | } else { |
| 614 | Sig s; |
| 615 | sig_take(&g_c, &s); |
| 616 | fx_close(&g_fx, &g_c); |
| 617 | char dd[160]; |
| 618 | sig_diff(&s, &BASE[NGOAL], dd, sizeof dd); |
| 619 | add_case(name, "DIVERGED", 0, "promoted goals silently rolled back (state matches baseline %d of %d); vs final: %.100s", |
| 620 | match_base(&s), NGOAL, dd); |
| 621 | } |
| 622 | rmtree(d); |
| 623 | } |
| 624 | |
| 625 | /* ---- Cortex journal cut at a record boundary ------------------------------ |
| 626 | * cx_open accepts a journal cut exactly at a record boundary (no count or head |
| 627 | * in its header). rx_compose cross-checks the J-Space checkpoint anchor |
| 628 | * (count + head digest): a cut behind the anchored count must be refused at the |
| 629 | * compose seam; a cut that only drops records written after the last commit |
| 630 | * (count == anchor count) must still open. The test parses the journal and the |
| 631 | * anchor (last 48 bytes of jspace.meta, version 3) itself. */ |
| 632 | static int read_file_all(const char *path, uint8_t **out, size_t *n) { |
| 633 | FILE *f = fopen(path, "rb"); |
| 634 | if (!f) return -1; |
| 635 | fseek(f, 0, SEEK_END); |
| 636 | long sz = ftell(f); |
| 637 | fseek(f, 0, SEEK_SET); |
| 638 | uint8_t *b = malloc(sz > 0 ? (size_t)sz : 1); |
| 639 | if (!b || (sz > 0 && fread(b, 1, (size_t)sz, f) != (size_t)sz)) { free(b); fclose(f); return -1; } |
| 640 | fclose(f); |
| 641 | *out = b; *n = (size_t)sz; |
| 642 | return 0; |
| 643 | } |
| 644 | static uint64_t le64_at(const uint8_t *p) { |
| 645 | uint64_t v = 0; |
| 646 | for (int i = 0; i < 8; i++) v |= (uint64_t)p[i] << (8 * i); |
| 647 | return v; |
| 648 | } |
| 649 | |
| 650 | /* Fill off[0..nrec] with record boundaries (off[0] = journal header end). */ |
| 651 | static int journal_bounds(const uint8_t *j, size_t n, size_t *off, int max, int *nrec) { |
| 652 | size_t pos = 4u * 8u; /* CX_J_HDR_WORDS * 8 */ |
| 653 | int k = 0; |
| 654 | if (n < pos) return -1; |
| 655 | off[0] = pos; |
| 656 | while (pos + 13u * 8u <= n && k + 1 < max) { |
| 657 | uint64_t pw = le64_at(j + pos + 12u * 8u); /* word index 12: payload words */ |
| 658 | size_t len = (size_t)(13u + pw + 4u) * 8u; |
| 659 | if (pos + len > n) return -1; |
| 660 | pos += len; |
| 661 | off[++k] = pos; |
| 662 | } |
| 663 | if (pos != n) return -1; |
| 664 | *nrec = k; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static int cx_alone_opens(const char *path) { |
| 669 | CxStore cx; |
| 670 | int rc = cx_open(&cx, path, RX_CORTEX_SUBJECTS, CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL); |
| 671 | if (rc == CX_OK) cx_close(&cx); |
| 672 | return rc; |
| 673 | } |
| 674 | |
| 675 | static void boundary_cut_cases(void) { |
| 676 | const char *nA = "cortex_cut/boundary_behind_checkpoint_anchor"; |
| 677 | const char *nB = "cortex_cut/boundary_after_last_commit_only"; |
| 678 | char d[200], jp[260], mp[260]; |
| 679 | Sig tmp[NGOAL + 1]; |
| 680 | mkcasedir(d, sizeof d, "cutb"); |
| 681 | if (run_task(d, tmp) != 0) { |
| 682 | add_case(nA, "DIVERGED", 0, "no directory"); |
| 683 | add_case(nB, "DIVERGED", 0, "no directory"); |
| 684 | return; |
| 685 | } |
| 686 | snprintf(jp, sizeof jp, "%s/cortex.cx", d); |
| 687 | snprintf(mp, sizeof mp, "%s/jspace/jspace.meta", d); |
| 688 | uint8_t *j = NULL, *m = NULL; |
| 689 | size_t jn = 0, mn = 0; |
| 690 | static size_t off[4096]; |
| 691 | int nrec = 0; |
| 692 | if (read_file_all(jp, &j, &jn) || read_file_all(mp, &m, &mn) || mn < 176u || |
| 693 | journal_bounds(j, jn, off, 4096, &nrec)) { |
| 694 | add_case(nA, "DIVERGED", 0, "cannot parse journal or checkpoint"); |
| 695 | add_case(nB, "DIVERGED", 0, "cannot parse journal or checkpoint"); |
| 696 | free(j); free(m); rmtree(d); |
| 697 | return; |
| 698 | } |
| 699 | uint64_t cnt = le64_at(m + mn - 48); /* anchor: u64 count first */ |
| 700 | free(m); |
| 701 | if (cnt < 1 || cnt > (uint64_t)nrec) { |
| 702 | add_case(nA, "DIVERGED", 0, "anchor count %llu vs %d records", (unsigned long long)cnt, nrec); |
| 703 | add_case(nB, "DIVERGED", 0, "anchor count %llu vs %d records", (unsigned long long)cnt, nrec); |
| 704 | free(j); rmtree(d); |
| 705 | return; |
| 706 | } |
| 707 | /* (a) cut to cnt-1 records: at least the newest checkpointed record is gone. */ |
| 708 | char da[200], db[200], cmd[640]; |
| 709 | mkcasedir(da, sizeof da, "cutb_a"); |
| 710 | snprintf(cmd, sizeof cmd, "cp -a '%s' '%s'", d, da); |
| 711 | int cp = system(cmd); |
| 712 | char ja[260]; |
| 713 | snprintf(ja, sizeof ja, "%s/cortex.cx", da); |
| 714 | int tr = truncate(ja, (off_t)off[cnt - 1]); |
| 715 | int rc = open_dir(da); |
| 716 | if (rc == RX_OK) fx_close(&g_fx, &g_c); |
| 717 | int rc2 = open_dir(da); |
| 718 | if (rc2 == RX_OK) fx_close(&g_fx, &g_c); |
| 719 | int cxrc = cx_alone_opens(ja); |
| 720 | add_case(nA, rc == RX_ERR_REPLAY ? "REFUSED" : "DIVERGED", |
| 721 | cp == 0 && tr == 0 && rc == RX_ERR_REPLAY && rc2 == RX_ERR_REPLAY && cxrc == CX_OK, |
| 722 | "cut to %llu of %d records (anchor count %llu): compose open rc=%d again rc=%d (want %d), cx_open alone rc=%d (want %d)", |
| 723 | (unsigned long long)(cnt - 1), nrec, (unsigned long long)cnt, rc, rc2, RX_ERR_REPLAY, cxrc, CX_OK); |
| 724 | rmtree(da); |
| 725 | /* (b) cut to exactly the anchored count: only post-commit records dropped. */ |
| 726 | if ((uint64_t)nrec == cnt) { |
| 727 | add_case(nB, "NOT_APPLICABLE", 1, "no records written after the final commit (%d records, anchor count %llu); lower bound not exercised", nrec, (unsigned long long)cnt); |
| 728 | } else { |
| 729 | mkcasedir(db, sizeof db, "cutb_b"); |
| 730 | snprintf(cmd, sizeof cmd, "cp -a '%s' '%s'", d, db); |
| 731 | cp = system(cmd); |
| 732 | char jb[260]; |
| 733 | snprintf(jb, sizeof jb, "%s/cortex.cx", db); |
| 734 | tr = truncate(jb, (off_t)off[cnt]); |
| 735 | rc = open_dir(db); |
| 736 | if (rc == RX_OK) fx_close(&g_fx, &g_c); |
| 737 | add_case(nB, rc == RX_OK ? "RECOVERED" : "DIVERGED", cp == 0 && tr == 0 && rc == RX_OK, |
| 738 | "cut to anchor count %llu of %d records: open rc=%d (want 0)", (unsigned long long)cnt, nrec, rc); |
| 739 | rmtree(db); |
| 740 | } |
| 741 | free(j); |
| 742 | rmtree(d); |
| 743 | } |
| 744 | |
| 745 | /* The format version byte of a J-Space checkpoint file (offset 8), or -1. */ |
| 746 | static int jspace_meta_version(const char *mp) { |
| 747 | uint8_t *m = NULL; |
| 748 | size_t mn = 0; |
| 749 | int v = -1; |
| 750 | if (read_file_all(mp, &m, &mn) == 0 && mn > 8) v = m[8]; |
| 751 | free(m); |
| 752 | return v; |
| 753 | } |
| 754 | |
| 755 | /* C4: a version-2 checkpoint (no anchor) cannot detect a lost journal through |
| 756 | * the anchor, so recover() must still refuse durable J-Space history that has no |
| 757 | * state record in the Cortex. The v2 file is made from the v3 one the way |
| 758 | * rx_jspace_prod does: drop the 48-byte anchor section, patch the version and |
| 759 | * recompute the body and header digests. A control reopen with the journal intact |
| 760 | * proves the conversion is valid; the emptied-journal reopen must be refused. |
| 761 | * |
| 762 | * The control reopen runs on a COPY of the directory. A successful open commits |
| 763 | * the J-Space checkpoint again (recover() ends with commit_js), and that commit |
| 764 | * writes version 3 WITH an anchor; if the control ran on the case directory itself |
| 765 | * the emptied-journal open would be refused by the anchor check in open_home before |
| 766 | * recover() is reached, and the genesis refusal (mutant M02) would not be tested. |
| 767 | * The version of the checkpoint is read again right before the emptied-journal |
| 768 | * open and must still be 2. */ |
| 769 | static void v2_lost_journal_case(void) { |
| 770 | const char *nm = "cortex_cut/v2_checkpoint_journal_emptied"; |
| 771 | char d[200], dc[200], cmd[640], mp[260], jp[260]; |
| 772 | Sig tmp[NGOAL + 1]; |
| 773 | mkcasedir(d, sizeof d, "v2lost"); |
| 774 | if (run_task(d, tmp) != 0) { add_case(nm, "DIVERGED", 0, "no directory"); return; } |
| 775 | snprintf(mp, sizeof mp, "%s/jspace/jspace.meta", d); |
| 776 | snprintf(jp, sizeof jp, "%s/cortex.cx", d); |
| 777 | uint8_t *m = NULL; |
| 778 | size_t mn = 0; |
| 779 | if (read_file_all(mp, &m, &mn) || mn < 176u || m[8] != 3) { |
| 780 | add_case(nm, "DIVERGED", 0, "cannot read a version-3 checkpoint"); |
| 781 | free(m); rmtree(d); |
| 782 | return; |
| 783 | } |
| 784 | size_t nn = mn - 48; |
| 785 | uint64_t bl = (uint64_t)nn - 128; |
| 786 | for (int i = 0; i < 8; i++) m[24 + i] = (uint8_t)(bl >> (8 * i)); |
| 787 | m[8] = 2; |
| 788 | sha256_hash(m + 128, bl, m + 64); |
| 789 | sha256_hash(m, 96, m + 96); |
| 790 | int fd = open(mp, O_WRONLY | O_TRUNC); |
| 791 | int wr = fd >= 0 && write(fd, m, nn) == (ssize_t)nn; |
| 792 | if (fd >= 0) close(fd); |
| 793 | free(m); |
| 794 | /* Control: v2 checkpoint, journal intact, on a copy (the open rewrites it). */ |
| 795 | mkcasedir(dc, sizeof dc, "v2lost_ctl"); |
| 796 | snprintf(cmd, sizeof cmd, "cp -a '%s' '%s'", d, dc); |
| 797 | int cp = system(cmd); |
| 798 | int rc0 = (wr && cp == 0) ? open_dir(dc) : -1; |
| 799 | if (rc0 == RX_OK) fx_close(&g_fx, &g_c); |
| 800 | rmtree(dc); |
| 801 | /* Journal emptied, checkpoint still version 2 (no anchor). */ |
| 802 | int tr = truncate(jp, 0); |
| 803 | int ver = jspace_meta_version(mp); |
| 804 | int rc = (wr && ver == 2) ? open_dir(d) : -1; |
| 805 | if (rc == RX_OK) fx_close(&g_fx, &g_c); |
| 806 | add_case(nm, rc == RX_ERR_REPLAY ? "REFUSED" : "DIVERGED", |
| 807 | wr && cp == 0 && tr == 0 && ver == 2 && rc0 == RX_OK && rc == RX_ERR_REPLAY, |
| 808 | "version-2 checkpoint: intact journal open (on a copy) rc=%d (want 0); checkpoint version before the emptied open=%d (want 2); journal emptied open rc=%d (want %d)", |
| 809 | rc0, ver, rc, RX_ERR_REPLAY); |
| 810 | rmtree(d); |
| 811 | } |
| 812 | |
| 813 | /* C4: a record in the MIDDLE of the journal whose length field is enlarged reads |
| 814 | * as a torn tail in cx_open, and open_home opens with CX_OPEN_REPAIR_TAIL, which |
| 815 | * truncates the file from that record on. Compose open must still refuse, because |
| 816 | * the journal is then shorter than the checkpoint anchor (a refusal, never a |
| 817 | * silent recovery). Recorded in the message: whether the file was shortened by the |
| 818 | * failed open (the truncate is not undone). */ |
| 819 | static void middle_length_case(void) { |
| 820 | const char *nm = "cortex_cut/middle_record_length_enlarged"; |
| 821 | char d[200], jp[260], mp[260]; |
| 822 | Sig tmp[NGOAL + 1]; |
| 823 | mkcasedir(d, sizeof d, "midlen"); |
| 824 | if (run_task(d, tmp) != 0) { add_case(nm, "DIVERGED", 0, "no directory"); return; } |
| 825 | snprintf(jp, sizeof jp, "%s/cortex.cx", d); |
| 826 | snprintf(mp, sizeof mp, "%s/jspace/jspace.meta", d); |
| 827 | uint8_t *j = NULL, *m = NULL; |
| 828 | size_t jn = 0, mn = 0; |
| 829 | static size_t off[4096]; |
| 830 | int nrec = 0; |
| 831 | if (read_file_all(jp, &j, &jn) || read_file_all(mp, &m, &mn) || mn < 176u || |
| 832 | journal_bounds(j, jn, off, 4096, &nrec)) { |
| 833 | add_case(nm, "DIVERGED", 0, "cannot parse journal or checkpoint"); |
| 834 | free(j); free(m); rmtree(d); |
| 835 | return; |
| 836 | } |
| 837 | uint64_t cnt = le64_at(m + mn - 48); |
| 838 | free(m); |
| 839 | free(j); |
| 840 | if (cnt < 2 || cnt > (uint64_t)nrec) { |
| 841 | add_case(nm, "DIVERGED", 0, "anchor count %llu vs %d records", (unsigned long long)cnt, nrec); |
| 842 | rmtree(d); |
| 843 | return; |
| 844 | } |
| 845 | uint64_t victim = cnt / 2; /* 0-based record index, strictly inside the anchored prefix */ |
| 846 | uint8_t big[8] = { 0, 0, 0, 0x10, 0, 0, 0, 0 }; /* payload words = 2^28: past the end of file */ |
| 847 | int fd = open(jp, O_WRONLY); |
| 848 | int wr = fd >= 0 && pwrite(fd, big, 8, (off_t)(off[victim] + 12u * 8u)) == 8; |
| 849 | if (fd >= 0) close(fd); |
| 850 | struct stat s0, s1; |
| 851 | int st0 = stat(jp, &s0); |
| 852 | int rc = wr ? open_dir(d) : -1; |
| 853 | if (rc == RX_OK) fx_close(&g_fx, &g_c); |
| 854 | int st1 = stat(jp, &s1); |
| 855 | int rc2 = wr ? open_dir(d) : -1; |
| 856 | if (rc2 == RX_OK) fx_close(&g_fx, &g_c); |
| 857 | add_case(nm, rc == RX_ERR_REPLAY ? "REFUSED" : "DIVERGED", |
| 858 | wr && st0 == 0 && st1 == 0 && rc == RX_ERR_REPLAY && rc2 == RX_ERR_REPLAY, |
| 859 | "record %llu of %d (anchor count %llu) length enlarged: open rc=%d again rc=%d (want %d); journal %lld -> %lld bytes after the failed open", |
| 860 | (unsigned long long)victim, nrec, (unsigned long long)cnt, rc, rc2, RX_ERR_REPLAY, |
| 861 | (long long)s0.st_size, (long long)s1.st_size); |
| 862 | rmtree(d); |
| 863 | } |
| 864 | |
| 865 | static void jstr(FILE *f, const char *s) { |
| 866 | fputc('"', f); |
| 867 | for (; *s; s++) { |
| 868 | if (*s == '"' || *s == '\\') fputc('\\', f); |
| 869 | fputc(*s, f); |
| 870 | } |
| 871 | fputc('"', f); |
| 872 | } |
| 873 | |
| 874 | int main(int argc, char **argv) { |
| 875 | if (argc != 2) { fprintf(stderr, "usage: %s <receipt.json>\n", argv[0]); return 2; } |
| 876 | snprintf(g_base, sizeof g_base, "/tmp/rx_c4_requal.XXXXXX"); |
| 877 | if (!mkdtemp(g_base)) { perror("mkdtemp"); return 2; } |
| 878 | if (fx_init(&g_fx) != 0) { fprintf(stderr, "fixture init failed\n"); return 2; } |
| 879 | |
| 880 | /* Baseline, twice: must be deterministic and fully committed. */ |
| 881 | char d0[200], d1[200]; |
| 882 | mkcasedir(d0, sizeof d0, "base0"); |
| 883 | mkcasedir(d1, sizeof d1, "base1"); |
| 884 | Sig B1[NGOAL + 1]; |
| 885 | int b0 = run_task(d0, BASE), b1 = run_task(d1, B1); |
| 886 | int base_ok = b0 == 0 && b1 == 0; |
| 887 | for (int i = 0; base_ok && i <= NGOAL; i++) |
| 888 | base_ok = BASE[i].ok && sig_eq(&BASE[i], &B1[i]) && (i == 0 || !sig_eq(&BASE[i], &BASE[i - 1])); |
| 889 | add_case("baseline/two_runs_identical_signatures", "BASELINE", base_ok, "rc=%d,%d", b0, b1); |
| 890 | /* Baseline reopen after clean close names the final state. */ |
| 891 | Sig rb; |
| 892 | int rr = open_dir(d0); |
| 893 | int reopen_ok = 0; |
| 894 | if (rr == RX_OK) { sig_take(&g_c, &rb); fx_close(&g_fx, &g_c); reopen_ok = rb.ok && sig_eq(&rb, &BASE[NGOAL]); memcpy(REOPEN_REC, rb.rec, 32); REOPEN_NREC = rb.nrec; } |
| 895 | add_case("baseline/close_reopen_same_state", "BASELINE", reopen_ok, "rc=%d", rr); |
| 896 | rmtree(d0); |
| 897 | rmtree(d1); |
| 898 | |
| 899 | if (base_ok) { |
| 900 | for (int g = 0; g < NGOAL; g++) |
| 901 | for (int p = RXC_FP_BEFORE_FORK; p < RXC_FP_END; p++) crash_case(p, g); |
| 902 | kill_sweep(24); |
| 903 | corrupt_all(); |
| 904 | identity_case(); |
| 905 | for (int k = 1; k < NGOAL; k++) stale_case(k); |
| 906 | boundary_cut_cases(); |
| 907 | v2_lost_journal_case(); |
| 908 | middle_length_case(); |
| 909 | byte_sweep("jspace/jspace.meta", 1); |
| 910 | byte_sweep("cortex.cx", 7); |
| 911 | byte_sweep("machine.id", 1); |
| 912 | } |
| 913 | |
| 914 | int npass = 0, nfail = 0, nna = 0; |
| 915 | for (int i = 0; i < g_ncase; i++) nna += strcmp(g_case[i].klass, "NOT_APPLICABLE") == 0; |
| 916 | for (int i = 0; i < g_ncase; i++) g_case[i].pass ? npass++ : nfail++; |
| 917 | FILE *f = fopen(argv[1], "w"); |
| 918 | if (!f) { perror(argv[1]); return 2; } |
| 919 | fprintf(f, "{\n \"task\": \"verified-scale ledger: goals 5,6,7, result = 3*input+1, Skill A breaks the contract\",\n"); |
| 920 | fprintf(f, " \"stages\": \"World -> J-Space -> AEGIS verify -> World commit/seal -> settle -> Cortex\",\n"); |
| 921 | fprintf(f, " \"baseline_final\": {\"result\": %llu, \"promotions\": %u},\n", |
| 922 | (unsigned long long)BASE[NGOAL].result, BASE[NGOAL].promos); |
| 923 | fprintf(f, " \"cases\": %d, \"pass\": %d, \"fail\": %d, \"not_applicable\": %d,\n \"verdict\": \"%s\",\n \"results\": [\n", g_ncase, npass, |
| 924 | nfail, nna, nfail == 0 && base_ok ? "PASS" : "FAIL"); |
| 925 | for (int i = 0; i < g_ncase; i++) { |
| 926 | fprintf(f, " {\"name\": "); |
| 927 | jstr(f, g_case[i].name); |
| 928 | fprintf(f, ", \"class\": \"%s\", \"result\": \"%s\", \"detail\": ", g_case[i].klass, |
| 929 | g_case[i].pass ? "PASS" : "FAIL"); |
| 930 | jstr(f, g_case[i].detail); |
| 931 | fprintf(f, "}%s\n", i + 1 < g_ncase ? "," : ""); |
| 932 | } |
| 933 | fprintf(f, " ]\n}\n"); |
| 934 | fclose(f); |
| 935 | for (int i = 0; i < g_ncase; i++) |
| 936 | if (!g_case[i].pass || getenv("C4_VERBOSE")) |
| 937 | printf("%s %-52s %-17s %s\n", g_case[i].pass ? "ok " : "FAIL", g_case[i].name, |
| 938 | g_case[i].klass, g_case[i].detail); |
| 939 | printf("C4 requalification (interruption+recovery): %d cases, %d pass, %d fail: %s\n", g_ncase, npass, nfail, |
| 940 | nfail == 0 && base_ok ? "PASS" : "FAIL"); |
| 941 | fx_free(&g_fx); |
| 942 | rmtree(g_base); |
| 943 | return nfail == 0 && base_ok ? 0 : 1; |
| 944 | } |