Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/ba93ee1be08c5a63 # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-mv#217 (code only) closed derived `ba93ee1be08c5a63` #217 Imported from GitHub aien-dev/omega-mv#217 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `56811e7ff0f344e904a80ad56900a78da4e20eac` · Head `878bdd234aec4a6ed3a6a1a9cc35f773590fa0cc` ### Apply it ``` git checkout -b patch-ba93ee1be08c5a63 56811e7ff0f344e904a80ad56900a78da4e20eac curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/ba93ee1be08c5a63.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 217 878bdd234aec4a6ed3a6a1a9cc35f773590fa0cc ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":217,"url":"https://github.com/aien-dev/omega-mv/pull/217","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:49Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/src/runtime/rx_compose.c b/src/runtime/rx_compose.c index 532bd2c2a..823d789f9 100644 --- a/src/runtime/rx_compose.c +++ b/src/runtime/rx_compose.c @@ -501,6 +501,20 @@ static int compose_records(RxCompose *c, uint64_t S, uint64_t V, const uint8_t ( /* ---- recovery (OLD-or-NEW), before the World exists ----------------------- */ +/* Checkpoint the J-Space state together with an anchor naming the Cortex store: + * u64 record count (LE) | digest of the newest record (32 zero bytes if none) | + * u64 flags = 1 (non-zero, so the section reads as present). open_home refuses a + * journal that is shorter than, or disagrees with, the anchor. */ +static int commit_js(RxCompose *c) { + uint8_t a[48] = { 0 }; + uint64_t n = c->cx.n; + for (int i = 0; i < 8; i++) a[i] = (uint8_t)(n >> (8 * i)); + if (n) memcpy(a + 8, c->cx.obj[n - 1].digest, 32); + a[40] = 1; + js_space_set_anchor(&c->js, a); + return js_space_commit(&c->js); +} + static int recover(RxCompose *c) { CxStore *s = &c->cx; const CxIdList *l = &s->by_subject[RXC_STATE_SUBJECT]; @@ -571,7 +585,7 @@ static int recover(RxCompose *c) { for (uint32_t b = 0; b < c->js.n_branches; b++) if (c->js.branches[b] && b != ref.id) js_branch_release(&c->js, b); } - if (js_space_commit(&c->js) != JS_OK) return RX_ERR_REPLAY; + if (commit_js(c) != JS_OK) return RX_ERR_REPLAY; c->recovered = ref; c->recovered_record = chosen; @@ -868,6 +882,29 @@ static int open_home(RxCompose *c, const char *dir, const AienMachineId *self, u cx_close(&c->cx); return RX_ERR_REPLAY; } + /* A Cortex journal cut exactly at a record boundary opens cleanly in cx_open + * (the journal header carries no count or head). The checkpoint anchor + * (count + head digest) is the cross-check: a journal shorter than the + * anchor, or whose record at the anchored count differs, is refused. Records + * appended after the last commit only make the journal longer: lower bound. + * KNOWN LIMIT: if both the journal and the checkpoint roll back together, + * nothing local detects it (an external anchor problem). A version-2 + * checkpoint has no anchor: the check is audit only (anchor_unknown) until + * the next commit writes one. */ + c->anchor_unknown = 0; + uint8_t an[48]; + if (js_space_anchor(&c->js, an)) { + uint64_t cnt = 0; + for (int i = 0; i < 8; i++) cnt |= (uint64_t)an[i] << (8 * i); + const CxObject *head = cnt ? cx_get(&c->cx, cnt) : NULL; + if (c->cx.n < cnt || (cnt > 0 && (!head || memcmp(head->digest, an + 8, 32) != 0))) { + js_space_destroy(&c->js); + cx_close(&c->cx); + return RX_ERR_REPLAY; + } + } else { + c->anchor_unknown = 1; + } int rc = recover(c); if (rc != RX_OK) { js_space_destroy(&c->js); @@ -1122,7 +1159,7 @@ int rx_compose_run(RxCompose *c, uint64_t input, const SrRequirement *req, const js_branch_release_ref(&c->js, c->pend_old, 0); c->pend_released = 1; } - if (c->pend_S && js_space_commit(&c->js) != JS_OK) return RX_ERR_REPLAY; + if (c->pend_S && commit_js(c) != JS_OK) return RX_ERR_REPLAY; out->prior_completed = m > 0 ? (uint32_t)m : 0; c->pending = 0; } @@ -1178,7 +1215,7 @@ int rx_compose_run(RxCompose *c, uint64_t input, const SrRequirement *req, const if (committed) { out->new_ref = nw; out->result = so.field[RXC_S_RESULT]; - if (fault(c, RXC_FP_RECLAIM, 0) || js_space_commit(&c->js) != JS_OK) { + if (fault(c, RXC_FP_RECLAIM, 0) || commit_js(c) != JS_OK) { c->pending = 2; out->outcome = RXC_OUT_NOT_DURABLE; return RX_OK; @@ -1192,7 +1229,7 @@ int rx_compose_run(RxCompose *c, uint64_t input, const SrRequirement *req, const return RX_OK; } js_branch_release_ref(&c->js, old, 0); /* the superseded branch */ - if (js_space_commit(&c->js) != JS_OK) { + if (commit_js(c) != JS_OK) { set_pending(c, S, V, 1, old, 1, (const uint8_t (*)[32])cdig); out->outcome = RXC_OUT_RECORD_FAILED; return RX_OK; diff --git a/src/runtime/rx_compose.h b/src/runtime/rx_compose.h index 831424325..62cbd7bd0 100644 --- a/src/runtime/rx_compose.h +++ b/src/runtime/rx_compose.h @@ -182,6 +182,7 @@ typedef struct RxCompose { /* recovery report of the last open */ JsBranchRef recovered; uint64_t recovered_record; /* Cortex id naming it */ + int anchor_unknown; /* 1: opened from a checkpoint with no Cortex anchor (version 2): cut check is audit only */ uint32_t rolled_back; /* newer records refused (rollback admissions) */ uint32_t recovered_completed; /* composition records completed at open (0 = none missing) */ /* in-process state after RECORD_FAILED / NOT_DURABLE (cleared at open) */ diff --git a/src/runtime/rx_cortex.h b/src/runtime/rx_cortex.h index 4785129e2..923d31573 100644 --- a/src/runtime/rx_cortex.h +++ b/src/runtime/rx_cortex.h @@ -214,9 +214,12 @@ enum { * After a CX_OK open, callers may assume the objects in memory equal the * journal, and that cx_verify on an object read from this store cannot fail * except through memory corruption or a later in-memory edit. - * NOT checked: a journal cut exactly at a record boundary looks like a shorter - * valid journal. The file holds no record count or head marker, so such a - * truncation is not detectable at open. */ + * NOT checked by cx_open: a journal cut exactly at a record boundary looks like a + * shorter valid journal (the file holds no record count or head marker). That + * cut is detected by rx_compose, which records the Cortex record count and head + * record digest in the J-Space checkpoint anchor and cross-checks it at open. + * KNOWN LIMIT: if both the Cortex journal and the J-Space checkpoint roll back + * together, nothing local detects it (an external anchor problem). */ int cx_open(CxStore *s, const char *path, uint64_t n_subjects, uint32_t flags); void cx_close(CxStore *s); /* = cx_free */ diff --git a/src/runtime/rx_jspace.c b/src/runtime/rx_jspace.c index ebacd4f23..7b00125e5 100644 --- a/src/runtime/rx_jspace.c +++ b/src/runtime/rx_jspace.c @@ -1307,7 +1307,7 @@ int js_branch_home(JsSpace *s, JsBranchRef ref, JsHome *out) { /* ---- durable checkpoint ------------------------------------------------------ * * jspace.meta, little-endian, all fixed-width fields: - * header (128 B): "OMJSPC01" | u32 version=2 | u32 header_bytes=128 | + * header (128 B): "OMJSPC01" | u32 version=3 | u32 header_bytes=128 | * u64 commit_seq | u64 body_len | u32 n_types | u32 n_reals | * u32 n_branch_slots | u32 n_branches | u64 spill_end | * u64 real_gen_floor | sha256(body) | sha256(header[0..96)) @@ -1322,6 +1322,10 @@ int js_branch_home(JsSpace *s, JsBranchRef ref, JsHome *out) { * u32 divergence | u32 frozen | u32 owner | u32 locality | * u32 type | u32 n_units | machine[32] | * n_units x { u32 slot | u64 gen } } + * Version 3 appends a fixed 48-byte opaque client anchor (u64 count | digest[32] | + * u64 reserved, little-endian) as the last bytes of the body, after the branches, + * covered by the body digest. A version-2 checkpoint (no anchor section) still + * loads with the anchor absent; the next commit writes version 3. * Version 2 widened realization generations to 64 bits (Lane 32). Version 1 * (32-bit realization generations) is refused with JS_ERR_VERSION, never * reinterpreted. No pointer is stored. Placement is SPILLED (bytes at spill_off in @@ -1330,7 +1334,9 @@ int js_branch_home(JsSpace *s, JsBranchRef ref, JsHome *out) { * are left out. */ #define JS_META_MAGIC "OMJSPC01" -#define JS_META_VERSION 2u +#define JS_META_VERSION 3u +#define JS_META_VERSION_V2 2u +#define JS_META_ANCHOR 48u #define JS_META_VERSION_V1 1u #define JS_META_HDR 128u #define JS_META_REAL 136u @@ -1466,6 +1472,7 @@ static int commit_u(JsSpace *s) { put32(&body, b->units[u]->slot); put64(&body, b->units[u]->gen); } } + buf_put(&body, s->anchor_set ? s->anchor : (const uint8_t[JS_META_ANCHOR]){ 0 }, JS_META_ANCHOR); if (body.oom) { free(body.p); return JS_ERR_NOMEM; } /* 3. Header. */ @@ -1515,6 +1522,21 @@ static int commit_u(JsSpace *s) { int js_space_commit(JsSpace *s) { LOCKED_INT(commit_u(s)); } +void js_space_set_anchor(JsSpace *s, const uint8_t anchor[48]) { + LOCK(s); + memcpy(s->anchor, anchor, JS_META_ANCHOR); + s->anchor_set = true; + UNLOCK(s); +} + +int js_space_anchor(const JsSpace *s, uint8_t out[48]) { + LOCK(s); + int set = s->anchor_set ? 1 : 0; + if (set) memcpy(out, s->anchor, JS_META_ANCHOR); + UNLOCK(s); + return set; +} + static int ext_cmp(const void *a, const void *b) { const JsExtent *x = a, *y = b; return (x->off > y->off) - (x->off < y->off); @@ -1530,7 +1552,9 @@ static int load_u(JsSpace *s, const uint8_t *f, size_t fn, if (memcmp(d, f + 96, 32)) return JS_ERR_CORRUPT; /* Version 1 had 32-bit realization generations: refused, never reinterpreted. */ if (rd32(f + 8) == JS_META_VERSION_V1) return JS_ERR_VERSION; - if (rd32(f + 8) != JS_META_VERSION || rd32(f + 12) != JS_META_HDR) return JS_ERR_CORRUPT; + uint32_t ver = rd32(f + 8); + if ((ver != JS_META_VERSION && ver != JS_META_VERSION_V2) || rd32(f + 12) != JS_META_HDR) + return JS_ERR_CORRUPT; uint64_t body_len = rd64(f + 24); if (body_len != fn - JS_META_HDR) return JS_ERR_CORRUPT; const uint8_t *p = f + JS_META_HDR, *end = p + body_len; @@ -1688,6 +1712,16 @@ static int load_u(JsSpace *s, const uint8_t *f, size_t fn, } p = u; } + if (rc == JS_OK && ver == JS_META_VERSION) { + if ((size_t)(end - p) != JS_META_ANCHOR) rc = JS_ERR_CORRUPT; + else { + /* An all-zero section is "never set": absent, like version 2. */ + static const uint8_t zero[JS_META_ANCHOR]; + memcpy(s->anchor, p, JS_META_ANCHOR); + s->anchor_set = memcmp(p, zero, JS_META_ANCHOR) != 0; + p = end; + } + } if (rc == JS_OK && p != end) rc = JS_ERR_CORRUPT; /* Every persisted realization is held by something; nothing dangles. */ for (uint32_t i = 0; i < n_reals && rc == JS_OK; i++) diff --git a/src/runtime/rx_jspace.h b/src/runtime/rx_jspace.h index d0ddfeebb..70469dc4a 100644 --- a/src/runtime/rx_jspace.h +++ b/src/runtime/rx_jspace.h @@ -306,6 +306,8 @@ typedef struct { bool durable; char *dir; uint64_t commit_seq; + uint8_t anchor[48]; /* opaque client anchor (v3 checkpoint) */ + bool anchor_set; JsHome local_home; /* home given to locally created branches */ } JsSpace; @@ -327,13 +329,23 @@ void js_limits_default(JsLimits *lim); * (matched by type and unit_bytes). A torn or corrupt checkpoint fails with * JS_ERR_CORRUPT and leaves no state; a leftover temporary file is ignored. * A version-1 checkpoint (32-bit realization generations) fails with - * JS_ERR_VERSION and leaves no state: it is refused, never reinterpreted. */ + * JS_ERR_VERSION and leaves no state: it is refused, never reinterpreted. + * A version-2 checkpoint (no client anchor) still loads, with the anchor absent + * (js_space_anchor returns 0). Commits write version 3. */ int js_space_open(JsSpace *s, const char *dir, const JsRealizer *const *realizers, uint32_t n_realizers, const JsLimits *lim, const JsHome *local_home); /* Make the current non-staged state the durable checkpoint: data first * (fdatasync), then metadata to a temporary file, fsync, atomic rename, * directory fsync. Only then are quarantined extents reusable. */ int js_space_commit(JsSpace *s); +/* Opaque 48-byte client anchor stored in the checkpoint (format v3), covered + * by the body digest. js_space_set_anchor copies it into the space; the next + * js_space_commit writes it. js_space_anchor copies it out and returns 1 if one + * is loaded or set, 0 if absent (a version-2 checkpoint, never set, or an + * all-zero section: clients must make a real anchor non-zero). J-Space + * never interprets the bytes. */ +void js_space_set_anchor(JsSpace *s, const uint8_t anchor[48]); +int js_space_anchor(const JsSpace *s, uint8_t out[48]); /* Stable references. */ int js_branch_ref(JsSpace *s, uint32_t id, JsBranchRef *out); diff --git a/tests/runtime/rx_c4_requal.c b/tests/runtime/rx_c4_requal.c index 210d0a5ef..63dd99aab 100644 --- a/tests/runtime/rx_c4_requal.c +++ b/tests/runtime/rx_c4_requal.c @@ -36,6 +36,7 @@ * Needs -DAIEN_TEST_BUILD=1 (crash points). usage: rx_c4_requal */ #include "rx_compose_fixture.h" +#include "sha256.h" #include #include @@ -605,6 +606,246 @@ static void stale_case(int k) { rmtree(d); } +/* ---- Cortex journal cut at a record boundary ------------------------------ + * cx_open accepts a journal cut exactly at a record boundary (no count or head + * in its header). rx_compose cross-checks the J-Space checkpoint anchor + * (count + head digest): a cut behind the anchored count must be refused at the + * compose seam; a cut that only drops records written after the last commit + * (count == anchor count) must still open. The test parses the journal and the + * anchor (last 48 bytes of jspace.meta, version 3) itself. */ +static int read_file_all(const char *path, uint8_t **out, size_t *n) { + FILE *f = fopen(path, "rb"); + if (!f) return -1; + fseek(f, 0, SEEK_END); + long sz = ftell(f); + fseek(f, 0, SEEK_SET); + uint8_t *b = malloc(sz > 0 ? (size_t)sz : 1); + if (!b || (sz > 0 && fread(b, 1, (size_t)sz, f) != (size_t)sz)) { free(b); fclose(f); return -1; } + fclose(f); + *out = b; *n = (size_t)sz; + return 0; +} +static uint64_t le64_at(const uint8_t *p) { + uint64_t v = 0; + for (int i = 0; i < 8; i++) v |= (uint64_t)p[i] << (8 * i); + return v; +} + +/* Fill off[0..nrec] with record boundaries (off[0] = journal header end). */ +static int journal_bounds(const uint8_t *j, size_t n, size_t *off, int max, int *nrec) { + size_t pos = 4u * 8u; /* CX_J_HDR_WORDS * 8 */ + int k = 0; + if (n < pos) return -1; + off[0] = pos; + while (pos + 13u * 8u <= n && k + 1 < max) { + uint64_t pw = le64_at(j + pos + 12u * 8u); /* word index 12: payload words */ + size_t len = (size_t)(13u + pw + 4u) * 8u; + if (pos + len > n) return -1; + pos += len; + off[++k] = pos; + } + if (pos != n) return -1; + *nrec = k; + return 0; +} + +static int cx_alone_opens(const char *path) { + CxStore cx; + int rc = cx_open(&cx, path, RX_CORTEX_SUBJECTS, CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL); + if (rc == CX_OK) cx_close(&cx); + return rc; +} + +static void boundary_cut_cases(void) { + const char *nA = "cortex_cut/boundary_behind_checkpoint_anchor"; + const char *nB = "cortex_cut/boundary_after_last_commit_only"; + char d[200], jp[260], mp[260]; + Sig tmp[NGOAL + 1]; + mkcasedir(d, sizeof d, "cutb"); + if (run_task(d, tmp) != 0) { + add_case(nA, "DIVERGED", 0, "no directory"); + add_case(nB, "DIVERGED", 0, "no directory"); + return; + } + snprintf(jp, sizeof jp, "%s/cortex.cx", d); + snprintf(mp, sizeof mp, "%s/jspace/jspace.meta", d); + uint8_t *j = NULL, *m = NULL; + size_t jn = 0, mn = 0; + static size_t off[4096]; + int nrec = 0; + if (read_file_all(jp, &j, &jn) || read_file_all(mp, &m, &mn) || mn < 176u || + journal_bounds(j, jn, off, 4096, &nrec)) { + add_case(nA, "DIVERGED", 0, "cannot parse journal or checkpoint"); + add_case(nB, "DIVERGED", 0, "cannot parse journal or checkpoint"); + free(j); free(m); rmtree(d); + return; + } + uint64_t cnt = le64_at(m + mn - 48); /* anchor: u64 count first */ + free(m); + if (cnt < 1 || cnt > (uint64_t)nrec) { + add_case(nA, "DIVERGED", 0, "anchor count %llu vs %d records", (unsigned long long)cnt, nrec); + add_case(nB, "DIVERGED", 0, "anchor count %llu vs %d records", (unsigned long long)cnt, nrec); + free(j); rmtree(d); + return; + } + /* (a) cut to cnt-1 records: at least the newest checkpointed record is gone. */ + char da[200], db[200], cmd[640]; + mkcasedir(da, sizeof da, "cutb_a"); + snprintf(cmd, sizeof cmd, "cp -a '%s' '%s'", d, da); + int cp = system(cmd); + char ja[260]; + snprintf(ja, sizeof ja, "%s/cortex.cx", da); + int tr = truncate(ja, (off_t)off[cnt - 1]); + int rc = open_dir(da); + if (rc == RX_OK) fx_close(&g_fx, &g_c); + int rc2 = open_dir(da); + if (rc2 == RX_OK) fx_close(&g_fx, &g_c); + int cxrc = cx_alone_opens(ja); + add_case(nA, rc == RX_ERR_REPLAY ? "REFUSED" : "DIVERGED", + cp == 0 && tr == 0 && rc == RX_ERR_REPLAY && rc2 == RX_ERR_REPLAY && cxrc == CX_OK, + "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)", + (unsigned long long)(cnt - 1), nrec, (unsigned long long)cnt, rc, rc2, RX_ERR_REPLAY, cxrc, CX_OK); + rmtree(da); + /* (b) cut to exactly the anchored count: only post-commit records dropped. */ + if ((uint64_t)nrec == cnt) { + 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); + } else { + mkcasedir(db, sizeof db, "cutb_b"); + snprintf(cmd, sizeof cmd, "cp -a '%s' '%s'", d, db); + cp = system(cmd); + char jb[260]; + snprintf(jb, sizeof jb, "%s/cortex.cx", db); + tr = truncate(jb, (off_t)off[cnt]); + rc = open_dir(db); + if (rc == RX_OK) fx_close(&g_fx, &g_c); + add_case(nB, rc == RX_OK ? "RECOVERED" : "DIVERGED", cp == 0 && tr == 0 && rc == RX_OK, + "cut to anchor count %llu of %d records: open rc=%d (want 0)", (unsigned long long)cnt, nrec, rc); + rmtree(db); + } + free(j); + rmtree(d); +} + +/* The format version byte of a J-Space checkpoint file (offset 8), or -1. */ +static int jspace_meta_version(const char *mp) { + uint8_t *m = NULL; + size_t mn = 0; + int v = -1; + if (read_file_all(mp, &m, &mn) == 0 && mn > 8) v = m[8]; + free(m); + return v; +} + +/* C4: a version-2 checkpoint (no anchor) cannot detect a lost journal through + * the anchor, so recover() must still refuse durable J-Space history that has no + * state record in the Cortex. The v2 file is made from the v3 one the way + * rx_jspace_prod does: drop the 48-byte anchor section, patch the version and + * recompute the body and header digests. A control reopen with the journal intact + * proves the conversion is valid; the emptied-journal reopen must be refused. + * + * The control reopen runs on a COPY of the directory. A successful open commits + * the J-Space checkpoint again (recover() ends with commit_js), and that commit + * writes version 3 WITH an anchor; if the control ran on the case directory itself + * the emptied-journal open would be refused by the anchor check in open_home before + * recover() is reached, and the genesis refusal (mutant M02) would not be tested. + * The version of the checkpoint is read again right before the emptied-journal + * open and must still be 2. */ +static void v2_lost_journal_case(void) { + const char *nm = "cortex_cut/v2_checkpoint_journal_emptied"; + char d[200], dc[200], cmd[640], mp[260], jp[260]; + Sig tmp[NGOAL + 1]; + mkcasedir(d, sizeof d, "v2lost"); + if (run_task(d, tmp) != 0) { add_case(nm, "DIVERGED", 0, "no directory"); return; } + snprintf(mp, sizeof mp, "%s/jspace/jspace.meta", d); + snprintf(jp, sizeof jp, "%s/cortex.cx", d); + uint8_t *m = NULL; + size_t mn = 0; + if (read_file_all(mp, &m, &mn) || mn < 176u || m[8] != 3) { + add_case(nm, "DIVERGED", 0, "cannot read a version-3 checkpoint"); + free(m); rmtree(d); + return; + } + size_t nn = mn - 48; + uint64_t bl = (uint64_t)nn - 128; + for (int i = 0; i < 8; i++) m[24 + i] = (uint8_t)(bl >> (8 * i)); + m[8] = 2; + sha256_hash(m + 128, bl, m + 64); + sha256_hash(m, 96, m + 96); + int fd = open(mp, O_WRONLY | O_TRUNC); + int wr = fd >= 0 && write(fd, m, nn) == (ssize_t)nn; + if (fd >= 0) close(fd); + free(m); + /* Control: v2 checkpoint, journal intact, on a copy (the open rewrites it). */ + mkcasedir(dc, sizeof dc, "v2lost_ctl"); + snprintf(cmd, sizeof cmd, "cp -a '%s' '%s'", d, dc); + int cp = system(cmd); + int rc0 = (wr && cp == 0) ? open_dir(dc) : -1; + if (rc0 == RX_OK) fx_close(&g_fx, &g_c); + rmtree(dc); + /* Journal emptied, checkpoint still version 2 (no anchor). */ + int tr = truncate(jp, 0); + int ver = jspace_meta_version(mp); + int rc = (wr && ver == 2) ? open_dir(d) : -1; + if (rc == RX_OK) fx_close(&g_fx, &g_c); + add_case(nm, rc == RX_ERR_REPLAY ? "REFUSED" : "DIVERGED", + wr && cp == 0 && tr == 0 && ver == 2 && rc0 == RX_OK && rc == RX_ERR_REPLAY, + "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)", + rc0, ver, rc, RX_ERR_REPLAY); + rmtree(d); +} + +/* C4: a record in the MIDDLE of the journal whose length field is enlarged reads + * as a torn tail in cx_open, and open_home opens with CX_OPEN_REPAIR_TAIL, which + * truncates the file from that record on. Compose open must still refuse, because + * the journal is then shorter than the checkpoint anchor (a refusal, never a + * silent recovery). Recorded in the message: whether the file was shortened by the + * failed open (the truncate is not undone). */ +static void middle_length_case(void) { + const char *nm = "cortex_cut/middle_record_length_enlarged"; + char d[200], jp[260], mp[260]; + Sig tmp[NGOAL + 1]; + mkcasedir(d, sizeof d, "midlen"); + if (run_task(d, tmp) != 0) { add_case(nm, "DIVERGED", 0, "no directory"); return; } + snprintf(jp, sizeof jp, "%s/cortex.cx", d); + snprintf(mp, sizeof mp, "%s/jspace/jspace.meta", d); + uint8_t *j = NULL, *m = NULL; + size_t jn = 0, mn = 0; + static size_t off[4096]; + int nrec = 0; + if (read_file_all(jp, &j, &jn) || read_file_all(mp, &m, &mn) || mn < 176u || + journal_bounds(j, jn, off, 4096, &nrec)) { + add_case(nm, "DIVERGED", 0, "cannot parse journal or checkpoint"); + free(j); free(m); rmtree(d); + return; + } + uint64_t cnt = le64_at(m + mn - 48); + free(m); + free(j); + if (cnt < 2 || cnt > (uint64_t)nrec) { + add_case(nm, "DIVERGED", 0, "anchor count %llu vs %d records", (unsigned long long)cnt, nrec); + rmtree(d); + return; + } + uint64_t victim = cnt / 2; /* 0-based record index, strictly inside the anchored prefix */ + uint8_t big[8] = { 0, 0, 0, 0x10, 0, 0, 0, 0 }; /* payload words = 2^28: past the end of file */ + int fd = open(jp, O_WRONLY); + int wr = fd >= 0 && pwrite(fd, big, 8, (off_t)(off[victim] + 12u * 8u)) == 8; + if (fd >= 0) close(fd); + struct stat s0, s1; + int st0 = stat(jp, &s0); + int rc = wr ? open_dir(d) : -1; + if (rc == RX_OK) fx_close(&g_fx, &g_c); + int st1 = stat(jp, &s1); + int rc2 = wr ? open_dir(d) : -1; + if (rc2 == RX_OK) fx_close(&g_fx, &g_c); + add_case(nm, rc == RX_ERR_REPLAY ? "REFUSED" : "DIVERGED", + wr && st0 == 0 && st1 == 0 && rc == RX_ERR_REPLAY && rc2 == RX_ERR_REPLAY, + "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", + (unsigned long long)victim, nrec, (unsigned long long)cnt, rc, rc2, RX_ERR_REPLAY, + (long long)s0.st_size, (long long)s1.st_size); + rmtree(d); +} + static void jstr(FILE *f, const char *s) { fputc('"', f); for (; *s; s++) { @@ -646,6 +887,9 @@ int main(int argc, char **argv) { corrupt_all(); identity_case(); for (int k = 1; k < NGOAL; k++) stale_case(k); + boundary_cut_cases(); + v2_lost_journal_case(); + middle_length_case(); byte_sweep("jspace/jspace.meta", 1); byte_sweep("cortex.cx", 7); byte_sweep("machine.id", 1); diff --git a/tests/runtime/rx_compose_test.c b/tests/runtime/rx_compose_test.c index 4cdc51681..793db21a3 100644 --- a/tests/runtime/rx_compose_test.c +++ b/tests/runtime/rx_compose_test.c @@ -392,27 +392,57 @@ static void t_cortex(void) { CHECK(cx_verify_chain(&g_c.cx) == CX_OK, "chain after repair"); fx_close(&g_fx, &g_c); } - /* Tear inside the composition record itself: completed at open. */ + /* Admission cut short by a real crash: the process dies after the first + * composition record and before the loser admission, so the checkpoint + * still holds the previous anchor. Open completes the rest. A winning + * composition is 5 records (2 claims, evidence, promotion, 1 loser + * admission); one was written, so 4 are completed at open. */ char d2[200]; dir_for(d2, sizeof d2, "cortex2"); + fflush(NULL); + pid_t pid = fork(); + if (pid == 0) { + reset_c(); + g_c.test.fault_point = RXC_FP_CORTEX; + g_c.test.fault_crash = 1; + if (fx_open(&g_fx, &g_c, d2, 1) != RX_OK) _exit(2); + RxcResult oc; + fx_run(&g_fx, &g_c, 5, &oc); + _exit(3); /* the fault did not fire */ + } + int cst = 0; + waitpid(pid, &cst, 0); + CHECK(WIFEXITED(cst) && WEXITSTATUS(cst) == RXC_CRASH_EXIT, "crash mid-admission (%d)", + WIFEXITED(cst) ? WEXITSTATUS(cst) : -1); reset_c(); - CHECK(fx_open(&g_fx, &g_c, d2, 1) == RX_OK, "open 2"); - CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED, "commit 2"); - uint64_t last_adm = o.cx_admission[1]; - CHECK(last_adm == g_c.cx.n, "loser admission is the last record"); - fx_close(&g_fx, &g_c); - snprintf(p, sizeof p, "%s/cortex.cx", d2); - CHECK(stat(p, &sb) == 0 && truncate(p, sb.st_size - 3) == 0, "tear the admission"); - reset_c(); - rc = fx_open(&g_fx, &g_c, d2, 1); - CHECK(rc == RX_OK && g_c.recovered_completed == 1, "torn admission rewritten at open (%d, %u)", - rc, g_c.recovered_completed); - if (rc == RX_OK) { + int rc2 = fx_open(&g_fx, &g_c, d2, 1); + CHECK(rc2 == RX_OK && g_c.recovered_completed == 4, "torn admission rewritten at open (%d, %u)", + rc2, g_c.recovered_completed); + if (rc2 == RX_OK) { CHECK(fx_count(&g_c.cx, CX_K_ADMISSION, RXC_ADMIT_LOSER) == 1 && fx_count(&g_c.cx, CX_K_ADMISSION, RXC_ADMIT_RECOVERED) == 1, "completed + marked"); fx_close(&g_fx, &g_c); } + /* Artificial tear: a clean commit anchored the last admission, then the + * record is cut away. That is durable data that vanished, so open refuses + * (the checkpoint anchor is ahead of the journal). */ + char d3[200]; + dir_for(d3, sizeof d3, "cortex3"); + reset_c(); + CHECK(fx_open(&g_fx, &g_c, d3, 1) == RX_OK, "open 3"); + CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED, "commit 3"); + CHECK(o.cx_admission[1] == g_c.cx.n, "loser admission is the last record"); + fx_close(&g_fx, &g_c); + snprintf(p, sizeof p, "%s/cortex.cx", d3); + CHECK(stat(p, &sb) == 0 && truncate(p, sb.st_size - 3) == 0, "tear the anchored admission"); + reset_c(); + rc = fx_open(&g_fx, &g_c, d3, 1); + CHECK(rc == RX_ERR_REPLAY, "anchored record lost is refused (%d)", rc); + if (rc == RX_OK) fx_close(&g_fx, &g_c); + rmtree(d3); + snprintf(p, sizeof p, "%s/cortex.cx", d2); + /* Corrupt middle: one flipped byte in the body is refused, not repaired. */ CHECK(stat(p, &sb) == 0, "stat"); FILE *fp = fopen(p, "r+b"); diff --git a/tests/runtime/rx_jspace_prod.c b/tests/runtime/rx_jspace_prod.c index e2a57b11f..da73de9a0 100644 --- a/tests/runtime/rx_jspace_prod.c +++ b/tests/runtime/rx_jspace_prod.c @@ -819,6 +819,70 @@ static void test_gen_durable(void) { rmdir(dir); } +/* Client anchor: set -> commit -> reopen returns the same 48 bytes; a commit + * with no anchor set reopens absent; a version-2 checkpoint (no anchor section, + * made from a v3 one by dropping the section, patching the version and + * recomputing the body and header digests) still loads with the anchor absent. */ +static void test_anchor(void) { + char dir[64], meta[128], data[128]; + snprintf(dir, sizeof dir, "/tmp/jspace_anc_XXXXXX"); + if (!mkdtemp(dir)) { CHECK(0, "mkdtemp"); return; } + snprintf(meta, sizeof meta, "%s/jspace.meta", dir); + snprintf(data, sizeof data, "%s/jspace.data", dir); + static JsSpace s; + JsHome me = { .locality = JS_HOME_LOCAL }; + memset(me.machine, 0x22, sizeof me.machine); + uint8_t want[48], got[48]; + for (int i = 0; i < 48; i++) want[i] = (uint8_t)(i * 7 + 1); + CHECK(js_space_open(&s, dir, RZS, 2, NULL, &me) == JS_OK, "fresh open"); + CHECK(js_space_anchor(&s, got) == 0, "fresh space reports an anchor"); + uint32_t a; + js_branch_root(&s, &RZ, 91, &a); + CHECK(js_space_commit(&s) == JS_OK, "commit without anchor"); + js_space_destroy(&s); + CHECK(js_space_open(&s, dir, RZS, 2, NULL, &me) == JS_OK, "reopen no-anchor"); + CHECK(js_space_anchor(&s, got) == 0, "never-set anchor reads as present after reopen"); + js_space_set_anchor(&s, want); + CHECK(js_space_anchor(&s, got) == 1 && !memcmp(got, want, 48), "set anchor not visible before commit"); + CHECK(js_space_commit(&s) == JS_OK, "commit with anchor"); + js_space_destroy(&s); + CHECK(js_space_open(&s, dir, RZS, 2, NULL, &me) == JS_OK, "reopen with anchor"); + memset(got, 0, sizeof got); + CHECK(js_space_anchor(&s, got) == 1 && !memcmp(got, want, 48), "anchor did not round-trip"); + js_space_destroy(&s); + + /* Version-2 fixture from the v3 file. */ + int fd = open(meta, O_RDONLY); + struct stat st; + CHECK(fd >= 0 && !fstat(fd, &st) && st.st_size > 128 + 48, "stat checkpoint"); + if (fd < 0 || st.st_size <= 128 + 48) { if (fd >= 0) close(fd); goto done; } + size_t n = (size_t)st.st_size; + uint8_t *f = malloc(n); + CHECK(f && pread(fd, f, n, 0) == (ssize_t)n, "read checkpoint"); + close(fd); + if (!f) goto done; + CHECK(f[8] == 3, "checkpoint version %u, want 3", f[8]); + size_t nn = n - 48; + uint64_t bl = (uint64_t)nn - 128; + for (int i = 0; i < 8; i++) f[24 + i] = (uint8_t)(bl >> (8 * i)); + f[8] = 2; + sha256_hash(f + 128, bl, f + 64); + sha256_hash(f, 96, f + 96); + fd = open(meta, O_WRONLY | O_TRUNC); + CHECK(fd >= 0 && write(fd, f, nn) == (ssize_t)nn, "write v2 checkpoint"); + if (fd >= 0) close(fd); + free(f); + int rc = js_space_open(&s, dir, RZS, 2, NULL, &me); + CHECK(rc == JS_OK, "version-2 checkpoint: open rc %d, want JS_OK", rc); + if (rc == JS_OK) { + CHECK(js_space_anchor(&s, got) == 0, "version-2 checkpoint reports an anchor"); + js_space_destroy(&s); + } +done: + unlink(meta); unlink(data); + rmdir(dir); +} + int main(void) { struct { const char *name; void (*fn)(void); } t[] = { { "alloc_reclaim", test_alloc_reclaim }, @@ -830,6 +894,7 @@ int main(void) { { "world_commit", test_world }, { "gen_exhaustion", test_gen_exhaustion }, { "gen_durable", test_gen_durable }, + { "anchor", test_anchor }, }; for (size_t i = 0; i < sizeof t / sizeof *t; i++) { int before = g_fail; diff --git a/tools/c4_requal.sh b/tools/c4_requal.sh index 141bd8fc3..a775fbc79 100755 --- a/tools/c4_requal.sh +++ b/tools/c4_requal.sh @@ -99,7 +99,7 @@ out="evidence/C4-REQUAL/$sum.json" cp "$tmp" "$out" chmod 0644 "$out" cat "$work/injection.out" | tail -n 5 -[ $want_mut -eq 1 ] && tail -n 2 "$work/mutants.out" +[ $want_mut -eq 1 ] && cat "$work/mutants.out" # one line per mutant (an ERROR line carries the make exit code and last log lines) echo "receipt: $out" echo "verdict: $verdict (omega $commit, binding $binding)" [ "$verdict" = PASS ] diff --git a/tools/c4_requal_mutants.sh b/tools/c4_requal_mutants.sh index d17944b8b..5a83c4474 100755 --- a/tools/c4_requal_mutants.sh +++ b/tools/c4_requal_mutants.sh @@ -31,11 +31,12 @@ M08~src/runtime/rx_compose.c~s/int n = compose_records(c, S, V, NULL, 0, \&tmp); M09~src/runtime/rx_cortex.c~s/if (memcmp(s->obj\[id - 1\].digest, b + p + 8 \* (CX_R_FIXED + n), 32) != 0) {/if (0) {/~each journal record digest is checked at load M10~src/runtime/rx_jspace.c~s/if (memcmp(d, f + 64, 32)) return JS_ERR_CORRUPT;/;/~J-Space checkpoint body digest~redundant~header digest, body digest and the restore-time content check overlap; each alone is covered by the others and by structural validation M11~src/runtime/rx_jspace.c~s/if (memcmp(d, f + 96, 32)) return JS_ERR_CORRUPT;/;/~J-Space checkpoint header digest~redundant~header digest, body digest and the restore-time content check overlap; each alone is covered by the others and by structural validation -M12~src/runtime/rx_compose.c~s/if (js_space_commit(&c->js) != JS_OK) return RX_ERR_REPLAY;/;/~recovered state is made durable before the World starts +M12~src/runtime/rx_compose.c~s/if (commit_js(c) != JS_OK) return RX_ERR_REPLAY;/;/~recovered state is made durable before the World starts M13~src/runtime/rx_cortex.c,src/runtime/rx_cortex.c~s/if (memcmp(s->obj\[id - 1\].digest, b + p + 8 \* (CX_R_FIXED + n), 32) != 0) {/if (0) {/@@s/return memcmp(chain, s->chain, 32) == 0 ? CX_OK : CX_ERR_DIGEST;/return CX_OK;/~Cortex integrity (open-time record digest and the audit chain compare both removed) M14~src/runtime/rx_jspace.c,src/runtime/rx_jspace.c~s/if (memcmp(d, f + 64, 32)) return JS_ERR_CORRUPT;/;/@@s/if (memcmp(d, f + 96, 32)) return JS_ERR_CORRUPT;/;/~J-Space checkpoint integrity (header and body digests both removed)~gap~with every J-Space integrity digest removed all 820 single-byte flips of the task checkpoint are still refused structurally or benign, so the digests are not shown to bite M15~src/runtime/rx_jspace.c,src/runtime/rx_jspace.c,src/runtime/rx_jspace.c~s/if (memcmp(d, f + 64, 32)) return JS_ERR_CORRUPT;/;/@@s/if (memcmp(d, f + 96, 32)) return JS_ERR_CORRUPT;/;/@@s/if (memcmp(chk, r->content, 32)) { s->stats.corrupt_restores++; return JS_ERR_CORRUPT; }/;/~J-Space integrity, all three layers (header digest, body digest, restore-time content check) removed~gap~with every J-Space integrity digest removed all 820 single-byte flips of the task checkpoint are still refused structurally or benign, so the digests are not shown to bite M16~src/runtime/rx_compose.c~s/if (pp \&\& state_ref_promoted(s, pp\[CX_WREC_FIELD0 + RXC_S_REF\])) return RX_ERR_REPLAY;/(void)pp; (void)state_ref_promoted;/~refuse a J-Space checkpoint that lost a state Cortex already promoted +M17~src/runtime/rx_compose.c~s/if (c->cx.n < cnt || (cnt > 0 && (!head || memcmp(head->digest, an + 8, 32) != 0))) {/(void)head; if (0) {/~refuse a Cortex journal cut behind the checkpoint anchor ' # Same copy list as before: the source dirs plus the one authority library. copy="src tests tools Makefile mk aienos.lock physics.lock build/aienos-authority/$(basename "$auth")" ```