Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/runtime/rx_jspace_prod.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / runtime / rx_jspace_prod.c` 943 lines ยท 43481 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* M20 production J-Space mechanics. 18 * 19 * Covers only what M20 added to rx_jspace: slab allocation and reclamation, 20 * generation-safe branch and realization references, deterministic failure at 21 * every limit, concurrent use through the space mutex, durable checkpoints 22 * that reopen to a valid state (including after a crash), refusal of torn or 23 * corrupt metadata, placement metadata and remote refusal, and World commit 24 * compatibility (a World field names a packed branch reference; writers stage 25 * privately and publish through the World's own commit). 26 * 27 * The 4-minute branch-reuse benchmark (make test-branch-reuse) remains the 28 * check that J-Space semantics did not change. */ 29 #include "runtime/rx_caproot.h" 30 #include "runtime/rx_world.h" 31 #include "runtime/rx_jspace.h" 32 #include "sha256.h" 33 34 #include 35 #include 36 #include 37 #include 38 #include 39 #include 40 #include 41 #include 42 #include 43 44 static int g_fail, g_checks; 45 #define CHECK(c, ...) do { g_checks++; if (!(c)) { g_fail++; \ 46 fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); \ 47 fputc('\n', stderr); } } while (0) 48 49 #define UNIT 4096u 50 51 static uint64_t splitmix(uint64_t *x) { 52 uint64_t z = (*x += 0x9e3779b97f4a7c15ull); 53 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; 54 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; 55 return z ^ (z >> 31); 56 } 57 58 /* Pure: same (prev, token) gives the same bytes. */ 59 static void derive_t(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n) { 60 uint64_t x = token; 61 if (!prev) { 62 for (size_t i = 0; i + 8 <= n; i += 8) { uint64_t v = splitmix(&x); memcpy(out + i, &v, 8); } 63 return; 64 } 65 const uint64_t *p = (const uint64_t *)prev; 66 uint64_t *o = (uint64_t *)out; 67 size_t w = n / 8; 68 for (size_t j = 0; j < w; j++) o[j] = (p[j] ^ splitmix(&x)) + p[(j + 1) % w]; 69 } 70 71 static const JsRealizer RZ = { JS_REAL_WORLD_PROJECTION, "test_projection", UNIT, derive_t }; 72 static const JsRealizer RZ2 = { JS_REAL_KV_STATE, "test_kv", UNIT, derive_t }; 73 74 static JsReal *unit_of(JsSpace *s, uint32_t b, uint32_t i) { return s->branches[b]->units[i]; } 75 76 static void digest(JsSpace *s, uint32_t b, uint8_t out[32]) { 77 CHECK(js_branch_content_digest(s, b, out) == JS_OK, "digest of branch %u", b); 78 } 79 80 static JsLimits lim(uint32_t reals, uint32_t branches, uint64_t spill, uint64_t resident) { 81 JsLimits l = { reals, branches, spill, resident }; 82 return l; 83 } 84 85 /* ---- allocation and reclamation ------------------------------------------ */ 86 87 static void test_alloc_reclaim(void) { 88 static JsSpace s; 89 JsLimits l = lim(64, 4, 8 * UNIT, 0); 90 CHECK(js_space_init_limits(&s, "/tmp/jspace_prod_spill", &l) == JS_OK, "init"); 91 uint32_t root; 92 CHECK(js_branch_root(&s, &RZ, 1, &root) == JS_OK, "root"); 93 for (int i = 0; i < 4; i++) CHECK(js_branch_derive(&s, root, 10 + i) == JS_OK, "derive"); 94 uint64_t base = s.stats.live_reals; 95 /* Ten thousand fork/derive/edit/spill/release cycles in a space with room 96 * for 64 realizations, 4 branches and 8 spilled units: only works if every 97 * slot, branch and extent comes back. */ 98 for (int k = 0; k < 10000; k++) { 99 uint32_t c; 100 int rc = js_branch_fork(&s, root, &c); 101 if (rc) { CHECK(0, "fork %d rc %d", k, rc); break; } 102 uint8_t p[16]; 103 memset(p, k & 0xff, sizeof p); 104 CHECK(js_branch_derive(&s, c, 1000 + (uint64_t)k) == JS_OK, "derive %d", k); 105 CHECK(js_branch_edit(&s, c, 5, 64, p, sizeof p) == JS_OK, "edit %d", k); 106 CHECK(js_real_spill(&s, unit_of(&s, c, 5)) == JS_OK, "spill %d", k); 107 CHECK(js_branch_release(&s, c) == JS_OK, "release %d", k); 108 if (g_fail) break; 109 } 110 CHECK(s.stats.live_reals == base, "live realizations back to %lu (now %lu)", 111 (unsigned long)base, (unsigned long)s.stats.live_reals); 112 CHECK(s.real_hw <= 8, "slab high-water %u: slots were not reused", s.real_hw); 113 CHECK(s.n_branches <= 2, "branch high-water %u: slots were not reused", s.n_branches); 114 CHECK(s.spill_end <= UNIT, "spill file grew to %lu", (unsigned long)s.spill_end); 115 CHECK(s.stats.spilled_bytes == 0, "spilled bytes leaked"); 116 js_space_destroy(&s); 117 } 118 119 /* ---- generation safety ---------------------------------------------------- */ 120 121 static void test_generations(void) { 122 static JsSpace s; 123 CHECK(js_space_init(&s, "/tmp/jspace_prod_spill") == JS_OK, "init"); 124 uint32_t root, c; 125 js_branch_root(&s, &RZ, 2, &root); 126 js_branch_derive(&s, root, 3); 127 JsBranchRef rr, cr; 128 CHECK(js_branch_ref(&s, root, &rr) == JS_OK, "root ref"); 129 CHECK(js_branch_fork_staged(&s, rr, 42, &cr) == JS_OK, "staged fork"); 130 c = cr.id; 131 js_branch_derive(&s, c, 4); 132 JsRealId rid; 133 CHECK(js_real_id(&s, c, 2, &rid) == JS_OK, "real id"); 134 CHECK(js_real_lookup(&s, rid) == unit_of(&s, c, 2), "lookup resolves"); 135 136 CHECK(js_branch_release_ref(&s, cr, 7) == JS_ERR_OWNER, "foreign subject released a branch"); 137 CHECK(js_branch_check(&s, cr) == JS_OK, "owner refusal changed state"); 138 CHECK(js_branch_release_ref(&s, cr, 42) == JS_OK, "owner release"); 139 CHECK(js_branch_check(&s, cr) == JS_ERR_STALE, "released ref still valid"); 140 CHECK(js_branch_seal(&s, cr) == JS_ERR_STALE, "seal of stale ref"); 141 CHECK(js_branch_release_ref(&s, cr, 42) == JS_ERR_STALE, "double release"); 142 CHECK(js_real_lookup(&s, rid) == NULL, "reclaimed realization still resolves"); 143 144 /* The slots come back with new generations: old refs never alias. */ 145 JsBranchRef nr; 146 CHECK(js_branch_fork_staged(&s, rr, 0, &nr) == JS_OK, "refork"); 147 CHECK(nr.id == cr.id && nr.gen != cr.gen, "slot %u reused without a new generation", nr.id); 148 CHECK(js_branch_check(&s, cr) == JS_ERR_STALE, "old ref aliases new occupant"); 149 js_branch_derive(&s, nr.id, 5); 150 JsRealId nid; 151 js_real_id(&s, nr.id, 2, &nid); 152 CHECK(nid.slot == rid.slot && nid.gen != rid.gen, "realization slot reuse kept generation"); 153 CHECK(js_real_lookup(&s, rid) == NULL, "old realization id aliases new one"); 154 155 /* Packed form for World fields round-trips. */ 156 JsBranchRef u = js_branch_ref_unpack(js_branch_ref_pack(nr)); 157 CHECK(u.id == nr.id && u.gen == nr.gen, "pack round trip"); 158 CHECK(js_space_reclaim_staged(&s) == 1, "one staged branch to reclaim"); 159 CHECK(js_branch_check(&s, nr) == JS_ERR_STALE, "reclaimed staged branch still valid"); 160 js_space_destroy(&s); 161 } 162 163 /* ---- limits: deterministic failure, nothing changes ------------------------ */ 164 165 static void test_limits(void) { 166 static JsSpace s; 167 uint8_t d0[32], d1[32]; 168 JsLimits l = lim(6, 3, 2 * UNIT, 4 * UNIT); 169 CHECK(js_space_init_limits(&s, "/tmp/jspace_prod_spill", &l) == JS_OK, "init"); 170 uint32_t root, c1, c2, c3; 171 js_branch_root(&s, &RZ, 9, &root); 172 for (int i = 0; i < 3; i++) CHECK(js_branch_derive(&s, root, 20 + i) == JS_OK, "derive"); 173 /* resident limit: 4 units resident */ 174 digest(&s, root, d0); 175 uint64_t live = s.stats.live_reals, res = s.stats.resident_bytes; 176 CHECK(js_branch_derive(&s, root, 99) == JS_ERR_FULL, "resident limit not enforced"); 177 CHECK(s.stats.live_reals == live && s.stats.resident_bytes == res && 178 s.branches[root]->n_units == 4, "failed derive changed state"); 179 digest(&s, root, d1); 180 CHECK(!memcmp(d0, d1, 32), "failed derive changed content"); 181 182 /* spill limit: two units fit, the third fails and stays where it was */ 183 CHECK(js_real_spill(&s, unit_of(&s, root, 0)) == JS_OK, "spill 0"); 184 CHECK(js_real_spill(&s, unit_of(&s, root, 1)) == JS_OK, "spill 1"); 185 CHECK(js_real_spill(&s, unit_of(&s, root, 2)) == JS_ERR_FULL, "spill limit not enforced"); 186 CHECK(unit_of(&s, root, 2)->placement == JS_PLACE_HOT, "failed spill moved the unit"); 187 188 /* realization limit: 6 slots, 4 used */ 189 CHECK(js_branch_derive(&s, root, 30) == JS_OK, "derive 5"); 190 CHECK(js_branch_derive(&s, root, 31) == JS_OK, "derive 6"); 191 live = s.stats.live_reals; 192 CHECK(js_branch_derive(&s, root, 32) == JS_ERR_FULL, "realization limit not enforced"); 193 CHECK(s.stats.live_reals == live && s.branches[root]->n_units == 6, "failed derive changed state"); 194 195 /* branch limit: 3 slots */ 196 CHECK(js_branch_fork(&s, root, &c1) == JS_OK && js_branch_fork(&s, root, &c2) == JS_OK, "forks"); 197 uint32_t nb = s.n_branches; 198 CHECK(js_branch_fork(&s, root, &c3) == JS_ERR_FULL, "branch limit not enforced"); 199 CHECK(s.n_branches == nb, "failed fork took a slot"); 200 /* The same failures repeat identically. */ 201 CHECK(js_branch_fork(&s, root, &c3) == JS_ERR_FULL, "branch limit not deterministic"); 202 CHECK(js_branch_derive(&s, c1, 32) == JS_ERR_FULL, "realization limit not deterministic"); 203 js_space_destroy(&s); 204 205 JsLimits bad = lim(0, 3, 0, 0); 206 CHECK(js_space_init_limits(&s, "/tmp/jspace_prod_spill", &bad) == JS_ERR_ARG, "zero limit accepted"); 207 js_space_destroy(&s); 208 } 209 210 /* ---- concurrency ----------------------------------------------------------- */ 211 212 #define NTHREADS 8 213 #define STEPS 60 214 215 typedef struct { 216 JsSpace *s; 217 JsBranchRef root; 218 unsigned t; 219 uint8_t dig[32]; 220 int rc; 221 } Worker; 222 223 static int script(JsSpace *s, uint32_t b, unsigned t) { 224 for (unsigned k = 0; k < STEPS; k++) { 225 int rc; 226 if (k % 7 == 3) { 227 uint8_t p[24]; 228 memset(p, (int)(t * 31 + k), sizeof p); 229 rc = js_branch_edit(s, b, k % 9, (k * 64) % (UNIT - 24), p, sizeof p); 230 } else { 231 rc = js_branch_derive(s, b, (uint64_t)t * 100000 + k); 232 } 233 if (rc) return rc; 234 uint8_t buf[UNIT]; 235 if ((rc = js_branch_read(s, b, k % 9, buf))) return rc; 236 } 237 return JS_OK; 238 } 239 240 static void *worker(void *arg) { 241 Worker *w = arg; 242 JsBranchRef me; 243 w->rc = js_branch_fork_staged(w->s, w->root, w->t + 1, &me); 244 if (!w->rc) w->rc = script(w->s, me.id, w->t); 245 if (!w->rc) w->rc = js_branch_content_digest(w->s, me.id, w->dig); 246 if (!w->rc) w->rc = js_branch_release_ref(w->s, me, w->t + 1); 247 return NULL; 248 } 249 250 static volatile int g_stop; 251 static void *pressure(void *arg) { 252 JsSpace *s = arg; 253 JsPolicyReport rep; 254 while (!g_stop) js_forge_enforce(s, 6 * UNIT, &rep); 255 return NULL; 256 } 257 258 static void test_concurrency(void) { 259 static JsSpace s, ref; 260 CHECK(js_space_init(&s, "/tmp/jspace_prod_spill") == JS_OK, "init"); 261 CHECK(js_space_init(&ref, "/tmp/jspace_prod_spill2") == JS_OK, "init ref"); 262 uint32_t root, rroot; 263 js_branch_root(&s, &RZ, 77, &root); 264 js_branch_root(&ref, &RZ, 77, &rroot); 265 for (unsigned i = 0; i < 8; i++) { 266 js_branch_derive(&s, root, 500 + i); 267 js_branch_derive(&ref, rroot, 500 + i); 268 } 269 Worker w[NTHREADS]; 270 pthread_t th[NTHREADS], pt; 271 g_stop = 0; 272 pthread_create(&pt, NULL, pressure, &s); 273 for (unsigned t = 0; t < NTHREADS; t++) { 274 w[t] = (Worker){ .s = &s, .t = t }; 275 js_branch_ref(&s, root, &w[t].root); 276 pthread_create(&th[t], NULL, worker, &w[t]); 277 } 278 for (unsigned t = 0; t < NTHREADS; t++) pthread_join(th[t], NULL); 279 g_stop = 1; 280 pthread_join(pt, NULL); 281 /* Same scripts, one thread, no pressure: same content. */ 282 for (unsigned t = 0; t < NTHREADS; t++) { 283 CHECK(w[t].rc == JS_OK, "worker %u rc %d", t, w[t].rc); 284 uint32_t c; 285 js_branch_fork(&ref, rroot, &c); 286 CHECK(script(&ref, c, t) == JS_OK, "reference script %u", t); 287 uint8_t d[32]; 288 digest(&ref, c, d); 289 CHECK(!memcmp(d, w[t].dig, 32), "worker %u content differs from serial reference", t); 290 js_branch_release(&ref, c); 291 } 292 CHECK(s.stats.corrupt_restores == 0, "corrupt restores under concurrency"); 293 CHECK(s.n_free_branch + 1 == s.n_branches, "worker branches not all reclaimed"); 294 uint8_t a[32], b[32]; 295 digest(&s, root, a); 296 digest(&ref, rroot, b); 297 CHECK(!memcmp(a, b, 32), "shared root changed under concurrent use"); 298 js_space_destroy(&s); 299 js_space_destroy(&ref); 300 } 301 302 /* ---- placement metadata, remote refusal ------------------------------------ */ 303 304 static void test_remote(void) { 305 static JsSpace s; 306 CHECK(js_space_init(&s, "/tmp/jspace_prod_spill") == JS_OK, "init"); 307 uint32_t root; 308 js_branch_root(&s, &RZ, 5, &root); 309 js_branch_derive(&s, root, 6); 310 JsBranchRef r; 311 js_branch_ref(&s, root, &r); 312 JsHome h = { .locality = JS_HOME_REMOTE_OWNED }; 313 memset(h.machine, 0xab, sizeof h.machine); 314 CHECK(js_branch_set_home(&s, r, &h) == JS_OK, "set home"); 315 JsHome got; 316 CHECK(js_branch_home(&s, r, &got) == JS_OK && !memcmp(&got, &h, sizeof h), "home round trip"); 317 uint32_t c; 318 uint8_t p[4] = { 1, 2, 3, 4 }, buf[UNIT]; 319 CHECK(js_branch_derive(&s, root, 7) == JS_ERR_REMOTE, "derive on remote-owned branch"); 320 CHECK(js_branch_edit(&s, root, 0, 0, p, 4) == JS_ERR_REMOTE, "edit on remote-owned branch"); 321 CHECK(js_branch_fork(&s, root, &c) == JS_ERR_REMOTE, "fork of remote-owned branch"); 322 CHECK(js_branch_read(&s, root, 1, buf) == JS_OK, "resident read of remote-owned branch"); 323 js_real_evict(&s, unit_of(&s, root, 1)); 324 CHECK(js_branch_read(&s, root, 1, buf) == JS_ERR_REMOTE, "non-resident remote read without transport"); 325 h.locality = JS_HOME_REPLICA; 326 js_branch_set_home(&s, r, &h); 327 CHECK(js_branch_derive(&s, root, 7) == JS_ERR_REMOTE, "derive on replica"); 328 CHECK(js_branch_read(&s, root, 1, buf) == JS_OK, "replica rebuilds from recipe"); 329 h.locality = 9; 330 CHECK(js_branch_set_home(&s, r, &h) == JS_ERR_ARG, "bad locality accepted"); 331 js_space_destroy(&s); 332 } 333 334 /* ---- durability: reopen, crash, torn metadata ------------------------------ */ 335 336 static char g_dir[96]; 337 static const JsRealizer *const RZS[] = { &RZ, &RZ2 }; 338 339 static void path(char *out, const char *name) { snprintf(out, 128, "%s/%s", g_dir, name); } 340 341 static int copy_file(const char *from, const char *to) { 342 int a = open(from, O_RDONLY), b = open(to, O_WRONLY | O_CREAT | O_TRUNC, 0600); 343 int rc = a < 0 || b < 0 ? -1 : 0; 344 char buf[65536]; 345 ssize_t n; 346 while (!rc && (n = read(a, buf, sizeof buf)) > 0) if (write(b, buf, (size_t)n) != n) rc = -1; 347 if (a >= 0) close(a); 348 if (b >= 0) close(b); 349 return rc; 350 } 351 352 static void flip(const char *file, off_t at) { 353 int fd = open(file, O_RDWR); 354 uint8_t c; 355 if (fd < 0 || pread(fd, &c, 1, at) != 1) { CHECK(0, "flip %s", file); if (fd >= 0) close(fd); return; } 356 c ^= 0x5a; 357 if (pwrite(fd, &c, 1, at) != 1) CHECK(0, "flip write"); 358 close(fd); 359 } 360 361 typedef struct { 362 JsBranchRef a, b, staged; 363 uint8_t da[32], db[32]; 364 uint32_t na, nb; 365 } Saved; 366 367 /* Build a durable state, commit it, then keep changing it without committing. */ 368 static void build_and_crash(Saved *sv, int crash) { 369 static JsSpace s; 370 JsHome me = { .locality = JS_HOME_LOCAL }; 371 memset(me.machine, 0x11, sizeof me.machine); 372 CHECK(js_space_open(&s, g_dir, RZS, 2, NULL, &me) == JS_OK, "fresh open"); 373 uint32_t a, b, k; 374 js_branch_root(&s, &RZ, 123, &a); 375 for (unsigned i = 0; i < 12; i++) js_branch_derive(&s, a, 40 + i); 376 js_branch_fork(&s, a, &b); 377 uint8_t p[32]; 378 memset(p, 0xc3, sizeof p); 379 js_branch_edit(&s, b, 3, 100, p, sizeof p); 380 js_branch_edit(&s, b, 9, 4000, p, 32); 381 for (unsigned i = 0; i < 4; i++) js_branch_derive(&s, b, 900 + i); 382 js_branch_root(&s, &RZ2, 55, &k); /* a second representation */ 383 js_branch_derive(&s, k, 56); 384 /* mixed placements: spilled, compressed, evicted, hot */ 385 js_real_spill(&s, unit_of(&s, a, 2)); 386 js_real_spill(&s, unit_of(&s, b, 3)); 387 js_real_compress(&s, unit_of(&s, a, 5)); 388 js_real_evict(&s, unit_of(&s, b, 14)); 389 js_branch_ref(&s, a, &sv->a); 390 js_branch_ref(&s, b, &sv->b); 391 JsHome far = { .locality = JS_HOME_REPLICA }; 392 memset(far.machine, 0x77, sizeof far.machine); 393 js_branch_set_home(&s, sv->b, &far); 394 js_branch_set_owner(&s, sv->b, 9); 395 CHECK(js_branch_fork_staged(&s, sv->b, 9, &sv->staged) == JS_ERR_REMOTE, "fork of replica"); 396 /* staged fork of a: never named by a commit, never persisted */ 397 CHECK(js_branch_fork_staged(&s, sv->a, 3, &sv->staged) == JS_OK, "staged fork"); 398 js_branch_derive(&s, sv->staged.id, 1); 399 digest(&s, a, sv->da); 400 digest(&s, b, sv->db); 401 sv->na = s.branches[a]->n_units; 402 sv->nb = s.branches[b]->n_units; 403 CHECK(js_space_commit(&s) == JS_OK, "commit"); 404 if (!crash) { js_space_destroy(&s); return; } 405 /* After the commit: overwrite-prone work that must not damage it. Units 406 * named by the checkpoint are freed, respilled and edited; nothing is 407 * committed, then the process dies. */ 408 js_real_restore(&s, unit_of(&s, a, 2)); /* frees a named spill extent */ 409 js_real_restore(&s, unit_of(&s, b, 3)); 410 js_branch_release(&s, k); 411 for (unsigned i = 0; i < 6; i++) { 412 js_branch_derive(&s, sv->staged.id, 2 + i); 413 js_real_spill(&s, unit_of(&s, sv->staged.id, s.branches[sv->staged.id]->n_units - 1)); 414 } 415 _exit(0); 416 } 417 418 static void verify_reopen(const Saved *sv, const char *what) { 419 static JsSpace s; 420 int rc = js_space_open(&s, g_dir, RZS, 2, NULL, NULL); 421 CHECK(rc == JS_OK, "%s: reopen rc %d", what, rc); 422 if (rc) return; 423 CHECK(js_branch_check(&s, sv->a) == JS_OK && js_branch_check(&s, sv->b) == JS_OK, 424 "%s: committed refs not valid after reopen", what); 425 CHECK(js_branch_check(&s, sv->staged) == JS_ERR_STALE, "%s: staged branch survived", what); 426 CHECK(s.branches[sv->a.id]->n_units == sv->na && s.branches[sv->b.id]->n_units == sv->nb, 427 "%s: unit counts", what); 428 uint8_t d[32]; 429 digest(&s, sv->a.id, d); 430 CHECK(!memcmp(d, sv->da, 32), "%s: branch a content after reopen", what); 431 digest(&s, sv->b.id, d); 432 CHECK(!memcmp(d, sv->db, 32), "%s: branch b content after reopen", what); 433 CHECK(s.stats.corrupt_restores == 0, "%s: corrupt restores", what); 434 JsHome h; 435 js_branch_home(&s, sv->b, &h); 436 CHECK(h.locality == JS_HOME_REPLICA && h.machine[0] == 0x77, "%s: placement metadata lost", what); 437 CHECK(js_branch_release_ref(&s, sv->b, 1) == JS_ERR_OWNER, "%s: owner lost", what); 438 /* Reopened state is fully usable, and a new staged slot never aliases. */ 439 JsBranchRef n; 440 CHECK(js_branch_fork_staged(&s, sv->a, 0, &n) == JS_OK, "%s: fork after reopen", what); 441 CHECK(!(n.id == sv->staged.id && n.gen == sv->staged.gen), "%s: stale ref aliases", what); 442 CHECK(js_branch_derive(&s, n.id, 5) == JS_OK, "%s: derive after reopen", what); 443 js_space_destroy(&s); 444 } 445 446 static void test_durable(void) { 447 snprintf(g_dir, sizeof g_dir, "/tmp/jspace_prod_XXXXXX"); 448 if (!mkdtemp(g_dir)) { CHECK(0, "mkdtemp"); return; } 449 char meta[128], data[128], tmp[128], keep_meta[128], keep_data[128]; 450 path(meta, "jspace.meta"); path(data, "jspace.data"); path(tmp, "jspace.meta.tmp"); 451 path(keep_meta, "keep.meta"); path(keep_data, "keep.data"); 452 Saved sv; 453 454 /* Crash after commit: a child builds, commits, keeps working, dies. */ 455 pid_t pid = fork(); 456 if (pid == 0) { g_fail = 0; build_and_crash(&sv, 1); _exit(1); } 457 int st; 458 waitpid(pid, &st, 0); 459 CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 0, "crash child"); 460 /* Rebuild the same state in-process (deterministic) for the expectations. */ 461 char dir2[96]; 462 memcpy(dir2, g_dir, sizeof dir2); 463 strcat(g_dir, "/ref"); /* g_dir is a 24-byte mkdtemp name in a 96-byte buffer */ 464 build_and_crash(&sv, 0); 465 memcpy(g_dir, dir2, sizeof g_dir); 466 verify_reopen(&sv, "after crash"); 467 verify_reopen(&sv, "second reopen"); /* reopen without commit is idempotent */ 468 469 copy_file(meta, keep_meta); 470 copy_file(data, keep_data); 471 struct stat ms; 472 stat(meta, &ms); 473 static JsSpace s; 474 struct { const char *what; int mode; off_t at; int want; } cases[] = { 475 { "torn metadata (half written)", 1, 0, JS_ERR_CORRUPT }, 476 { "torn metadata (header only)", 2, 0, JS_ERR_CORRUPT }, 477 { "flipped header byte", 3, 20, JS_ERR_CORRUPT }, 478 { "flipped body byte", 3, 300, JS_ERR_CORRUPT }, 479 { "flipped last byte", 3, -1, JS_ERR_CORRUPT }, 480 { "data file truncated", 4, 0, JS_ERR_CORRUPT }, 481 { "empty metadata", 5, 0, JS_ERR_CORRUPT }, 482 }; 483 for (size_t i = 0; i < sizeof cases / sizeof *cases; i++) { 484 copy_file(keep_meta, meta); 485 copy_file(keep_data, data); 486 switch (cases[i].mode) { 487 case 1: CHECK(truncate(meta, ms.st_size / 2) == 0, "truncate"); break; 488 case 2: CHECK(truncate(meta, 128) == 0, "truncate"); break; 489 case 3: flip(meta, cases[i].at < 0 ? ms.st_size - 1 : cases[i].at); break; 490 case 4: CHECK(truncate(data, 1) == 0, "truncate"); break; 491 case 5: CHECK(truncate(meta, 0) == 0, "truncate"); break; 492 } 493 int rc = js_space_open(&s, g_dir, RZS, 2, NULL, NULL); 494 CHECK(rc == cases[i].want, "%s: open rc %d want %d", cases[i].what, rc, cases[i].want); 495 if (rc == JS_OK) js_space_destroy(&s); 496 } 497 /* Corrupt spilled bytes: metadata is fine, the content check refuses the bytes. */ 498 copy_file(keep_meta, meta); 499 copy_file(keep_data, data); 500 { 501 CHECK(js_space_open(&s, g_dir, RZS, 2, NULL, NULL) == JS_OK, "open"); 502 JsReal *r = unit_of(&s, sv.a.id, 2); 503 CHECK(r->placement == JS_PLACE_SPILLED, "spilled placement persisted"); 504 uint64_t off = r->spill_off; 505 js_space_destroy(&s); 506 flip(data, (off_t)off + 7); 507 CHECK(js_space_open(&s, g_dir, RZS, 2, NULL, NULL) == JS_OK, "open"); 508 uint8_t buf[UNIT]; 509 CHECK(js_branch_read(&s, sv.a.id, 2, buf) == JS_ERR_CORRUPT, "corrupt spilled bytes served"); 510 js_space_destroy(&s); 511 } 512 /* Missing representation: the caller did not supply a persisted type. */ 513 copy_file(keep_meta, meta); 514 copy_file(keep_data, data); 515 CHECK(js_space_open(&s, g_dir, RZS, 1, NULL, NULL) == JS_ERR_ARG, "missing realizer accepted"); 516 /* A leftover temporary checkpoint is ignored and removed. */ 517 int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0600); 518 CHECK(fd >= 0 && write(fd, "OMJSPC01garbage", 15) == 15, "tmp write"); 519 if (fd >= 0) close(fd); 520 verify_reopen(&sv, "leftover temp file"); 521 CHECK(access(tmp, F_OK) != 0, "temp checkpoint not removed"); 522 523 /* Commit, reopen, commit again: sequence advances, state holds; released 524 * extents are reused only after the commit that stops naming them. */ 525 CHECK(js_space_open(&s, g_dir, RZS, 2, NULL, NULL) == JS_OK, "open"); 526 uint64_t seq = s.commit_seq; 527 JsReal *r = unit_of(&s, sv.a.id, 2); 528 uint64_t named = r->spill_off; 529 js_real_restore(&s, r); 530 CHECK(s.n_ext_pending >= 1, "freed named extent not quarantined"); 531 js_real_spill(&s, unit_of(&s, sv.a.id, 7)); 532 CHECK(unit_of(&s, sv.a.id, 7)->spill_off != named, "named extent reused before commit"); 533 CHECK(js_space_commit(&s) == JS_OK && s.commit_seq == seq + 1, "second commit"); 534 CHECK(s.n_ext_pending == 0, "quarantine not released by commit"); 535 js_space_destroy(&s); 536 verify_reopen(&sv, "after second commit"); 537 538 /* Volatile spaces have no checkpoint. */ 539 CHECK(js_space_init(&s, "/tmp/jspace_prod_spill") == JS_OK, "init"); 540 CHECK(js_space_commit(&s) == JS_ERR_ARG, "commit of volatile space"); 541 js_space_destroy(&s); 542 543 char cmd[160]; 544 snprintf(cmd, sizeof cmd, "rm -rf '%s'", g_dir); 545 if (system(cmd)) { /* best effort */ } 546 } 547 548 /* ---- World commit compatibility --------------------------------------------- */ 549 550 enum { SUBJ_WRITER = 1, SUBJ_EXTERNAL = 100, ISSUER_AEGIS_POLICY = 3 }; 551 enum { RES_STATE = 0x90, RES_TICK = 0x91 }; 552 #define STEP_TOKEN 7u 553 554 typedef struct { 555 JsSpace *s; 556 RxObjRef state, tick; 557 unsigned ran; 558 } WorldUser; 559 560 /* A writer: read the committed reference, fork it privately, advance it one 561 * step, propose the new reference. The World's commit decides whether it 562 * becomes visible; a refused proposal stays staged and is reclaimed. */ 563 static int writer_fn(RxCtx *c) { 564 WorldUser *u = c->user; 565 const RxSnapshotDep *in = NULL; 566 for (uint32_t i = 0; i < c->n_in; i++) if (c->in[i].obj.id == u->state.id) in = &c->in[i]; 567 if (!in) return 0; 568 JsBranchRef cur = js_branch_ref_unpack(in->field[0]), next; 569 if (js_branch_fork_staged(u->s, cur, SUBJ_WRITER, &next)) return 0; 570 if (js_branch_derive(u->s, next.id, STEP_TOKEN)) return 0; 571 __atomic_add_fetch(&u->ran, 1, __ATOMIC_RELAXED); 572 c->out[c->n_out++] = (RxMutation){ u->state, 0, js_branch_ref_pack(next) }; 573 return 0; 574 } 575 576 static RxCapRef mint(RxCapAdmin *admin, uint32_t subject, uint64_t resource, uint32_t rights) { 577 RxCapMint m; 578 memset(&m, 0, sizeof m); 579 m.issuer = ISSUER_AEGIS_POLICY; 580 m.subject = subject; 581 m.resource = resource; 582 m.rights = rights; 583 m.parent = (RxCapRef){ UINT32_MAX, 0 }; 584 m.authority = rx_capadmin_office(admin); 585 RxCapRef r = { UINT32_MAX, 0 }; 586 CHECK(rx_capadmin_mint(admin, &m, &r) == RX_CAP_OK, "mint"); 587 return r; 588 } 589 590 static void test_world(void) { 591 static RxCapRoot root; 592 static RxCapAdmin admin; 593 static RxWorld w; 594 static JsSpace s; 595 if (rx_caproot_start(&root, &admin) != RX_CAP_OK) { CHECK(0, "caproot"); return; } 596 if (rx_world_init(&w, &root, 4, 1u << 16) != RX_OK) { CHECK(0, "world"); rx_caproot_stop(&root, &admin); return; } 597 w.external_subject = SUBJ_EXTERNAL; 598 CHECK(js_space_init(&s, "/tmp/jspace_prod_spill") == JS_OK, "init"); 599 600 uint32_t base; 601 js_branch_root(&s, &RZ, 31, &base); 602 JsBranchRef b0; 603 js_branch_ref(&s, base, &b0); 604 uint64_t init_state[RX_MAX_FIELDS] = { js_branch_ref_pack(b0) }, init_tick[RX_MAX_FIELDS] = { 0 }; 605 WorldUser u = { .s = &s }; 606 CHECK(rx_world_create(&w, 1, RX_PERSIST_RESIDENT, RES_STATE, init_state, &u.state) == RX_OK, "state obj"); 607 CHECK(rx_world_create(&w, 1, RX_PERSIST_RESIDENT, RES_TICK, init_tick, &u.tick) == RX_OK, "tick obj"); 608 RxCapRef c_state = mint(&admin, SUBJ_WRITER, RES_STATE, RX_RIGHT_READ | RX_RIGHT_WRITE); 609 RxCapRef c_tick = mint(&admin, SUBJ_WRITER, RES_TICK, RX_RIGHT_READ); 610 RxCapRef c_ext = mint(&admin, SUBJ_EXTERNAL, RES_TICK, RX_RIGHT_WRITE); 611 612 /* Two writers race on the same state field: World re-validation must make 613 * every committed step build on the previously committed one. */ 614 for (int k = 0; k < 2; k++) { 615 RxReactionDesc d; 616 memset(&d, 0, sizeof d); 617 d.name = k ? "jspace_writer_b" : "jspace_writer_a"; 618 d.subject = SUBJ_WRITER; 619 d.priority = RX_PRIO_FOREGROUND; 620 d.fn = writer_fn; 621 d.user = &u; 622 d.triggers[d.n_triggers++] = (RxDep){ u.tick, 1 }; 623 d.reads[d.n_reads++] = (RxDep){ u.state, 1 }; 624 d.writes[d.n_writes++] = (RxDep){ u.state, 1 }; 625 d.caps[d.n_caps++] = (RxCapNeed){ c_state, RES_STATE, RX_RIGHT_READ | RX_RIGHT_WRITE }; 626 d.caps[d.n_caps++] = (RxCapNeed){ c_tick, RES_TICK, RX_RIGHT_READ }; 627 uint32_t id; 628 CHECK(rx_world_add_reaction(&w, &d, &id) == RX_OK, "add reaction %d", k); 629 } 630 631 JsBranchRef first = { UINT32_MAX, 0 }; 632 uint8_t first_dig[32]; 633 for (unsigned t = 1; t <= 40; t++) { 634 RxMutation m = { u.tick, 0, t }; 635 CHECK(rx_world_publish_external(&w, c_ext, &m, 1) > 0, "tick %u", t); 636 if (t % 8 == 0 || t == 1) { 637 CHECK(rx_world_wait_quiescent(&w, 10000) == RX_OK, "quiesce"); 638 RxObject o; 639 CHECK(rx_world_read(&w, u.state, &o) == RX_OK, "read state"); 640 JsBranchRef named = js_branch_ref_unpack(o.field[0]); 641 /* What the World names is a live branch; seal it (the commit hook 642 * requested from the World owner does this on every commit). */ 643 CHECK(js_branch_seal(&s, named) == JS_OK, "World names a dead branch"); 644 js_space_reclaim_staged(&s); 645 CHECK(js_branch_check(&s, named) == JS_OK, "reclaim took the committed branch"); 646 if (first.id == UINT32_MAX && named.id != b0.id) { 647 first = named; 648 digest(&s, first.id, first_dig); 649 } 650 } 651 } 652 CHECK(rx_world_wait_quiescent(&w, 10000) == RX_OK, "quiesce"); 653 RxObject o; 654 rx_world_read(&w, u.state, &o); 655 JsBranchRef fin = js_branch_ref_unpack(o.field[0]); 656 CHECK(js_branch_seal(&s, fin) == JS_OK, "final state names a dead branch"); 657 js_space_reclaim_staged(&s); 658 659 /* No lost update: the committed state is exactly base + n steps. */ 660 uint32_t n = s.branches[fin.id]->n_units - 1; 661 CHECK(n >= 1, "no write committed"); 662 static JsSpace ref; 663 js_space_init(&ref, "/tmp/jspace_prod_spill2"); 664 uint32_t rb; 665 js_branch_root(&ref, &RZ, 31, &rb); 666 for (uint32_t i = 0; i < n; i++) js_branch_derive(&ref, rb, STEP_TOKEN); 667 uint8_t a[32], b[32]; 668 digest(&s, fin.id, a); 669 digest(&ref, rb, b); 670 CHECK(!memcmp(a, b, 32), "committed J-Space state is not a serial history (%u steps)", n); 671 js_space_destroy(&ref); 672 673 /* An older World value still reads the bytes it named. */ 674 if (first.id != UINT32_MAX) { 675 CHECK(js_branch_check(&s, first) == JS_OK, "older committed branch reclaimed"); 676 digest(&s, first.id, a); 677 CHECK(!memcmp(a, first_dig, 32), "older committed bytes changed"); 678 uint8_t p[4] = { 9, 9, 9, 9 }; 679 CHECK(js_branch_edit(&s, first.id, 0, 0, p, 4) == JS_ERR_FROZEN, "committed branch mutable in place"); 680 /* Superseded and released: a stale World value is refused, never aliased. */ 681 CHECK(js_branch_release_ref(&s, first, SUBJ_WRITER) == JS_OK, "release superseded"); 682 CHECK(js_branch_check(&s, first) == JS_ERR_STALE, "stale World value accepted"); 683 } 684 rx_world_destroy(&w); 685 rx_caproot_stop(&root, &admin); 686 js_space_destroy(&s); 687 printf(" world: %u writer activations, %u committed steps\n", u.ran, n); 688 } 689 690 /* ---- Lane 32: generations never wrap (no ABA) ------------------------------ 691 * 692 * A slot is driven to the last live generation its type can hold (standing in 693 * for ~2^64 realization reuses or ~2^32 branch reuses) by writing the public 694 * struct field, then released and reused. The exhausted slot must never be 695 * handed out again and no older reference may validate. The top is computed 696 * from the field's own type so this test also compiles against 32-bit 697 * realization generations, where it fails (the slot wraps to generation 0 and 698 * the first occupant's reference validates again). */ 699 #define GEN_TOP(T) ((T)~(T)0) 700 typedef __typeof__(((JsReal *)0)->gen) RealGen; 701 typedef __typeof__(((JsBranchRef *)0)->gen) BranchGen; 702 703 static void test_gen_exhaustion(void) { 704 static JsSpace s; 705 /* Realization slot. */ 706 CHECK(js_space_init(&s, "/tmp/jspace_prod_spill") == JS_OK, "init"); 707 uint32_t root; 708 js_branch_root(&s, &RZ, 77, &root); 709 JsBranchRef rr, c; 710 js_branch_ref(&s, root, &rr); 711 CHECK(js_branch_fork_staged(&s, rr, 0, &c) == JS_OK, "fork"); 712 CHECK(js_branch_derive(&s, c.id, 700) == JS_OK, "derive"); 713 uint32_t last = s.branches[c.id]->n_units - 1; 714 JsRealId first, top; 715 js_real_id(&s, c.id, last, &first); 716 CHECK(first.gen == 0, "first occupant generation %llu", (unsigned long long)first.gen); 717 JsReal *r = unit_of(&s, c.id, last); 718 r->gen = GEN_TOP(RealGen) - 1; /* the last live generation */ 719 js_real_id(&s, c.id, last, &top); 720 CHECK(js_real_lookup(&s, top) == r, "top-generation reference resolves while live"); 721 CHECK(js_branch_release_ref(&s, c, 0) == JS_OK, "release at top generation"); 722 CHECK(js_real_lookup(&s, top) == NULL, "released top-generation reference resolves"); 723 for (int k = 0; k < 4; k++) { 724 CHECK(js_branch_fork_staged(&s, rr, 0, &c) == JS_OK, "refork %d", k); 725 CHECK(js_branch_derive(&s, c.id, 701 + (uint64_t)k) == JS_OK, "rederive %d", k); 726 JsRealId now; 727 js_real_id(&s, c.id, s.branches[c.id]->n_units - 1, &now); 728 CHECK(now.slot != first.slot, "cycle %d: exhausted realization slot %u handed out again (gen %llu)", 729 k, now.slot, (unsigned long long)now.gen); 730 CHECK(js_real_lookup(&s, first) == NULL, "cycle %d: first occupant's reference validates (ABA)", k); 731 CHECK(js_real_lookup(&s, top) == NULL, "cycle %d: top-generation reference validates", k); 732 CHECK(js_branch_release_ref(&s, c, 0) == JS_OK, "release %d", k); 733 } 734 js_space_destroy(&s); 735 736 /* Branch slot: 32-bit generations (a JsBranchRef packs into a World field). */ 737 CHECK(js_space_init(&s, "/tmp/jspace_prod_spill") == JS_OK, "init"); 738 js_branch_root(&s, &RZ, 78, &root); 739 js_branch_ref(&s, root, &rr); 740 JsBranchRef b0, btop; 741 CHECK(js_branch_fork_staged(&s, rr, 0, &b0) == JS_OK, "fork b0"); 742 CHECK(b0.gen == 0, "first branch occupant generation %u", b0.gen); 743 s.branch_gen[b0.id] = GEN_TOP(BranchGen) - 1; 744 s.branches[b0.id]->gen = GEN_TOP(BranchGen) - 1; 745 btop = (JsBranchRef){ b0.id, GEN_TOP(BranchGen) - 1 }; 746 CHECK(js_branch_check(&s, btop) == JS_OK, "top-generation branch ref valid while live"); 747 CHECK(js_branch_check(&s, b0) == JS_ERR_STALE, "older branch ref valid"); 748 CHECK(js_branch_release_ref(&s, btop, 0) == JS_OK, "release branch at top generation"); 749 for (int k = 0; k < 4; k++) { 750 JsBranchRef nb; 751 CHECK(js_branch_fork_staged(&s, rr, 0, &nb) == JS_OK, "branch refork %d", k); 752 CHECK(nb.id != b0.id, "cycle %d: exhausted branch slot %u handed out again (gen %u)", k, nb.id, nb.gen); 753 CHECK(js_branch_check(&s, b0) == JS_ERR_STALE, "cycle %d: first branch reference validates (ABA)", k); 754 CHECK(js_branch_check(&s, btop) == JS_ERR_STALE, "cycle %d: top branch reference validates", k); 755 CHECK(js_branch_release_ref(&s, nb, 0) == JS_OK, "branch release %d", k); 756 } 757 /* A full space whose only free slot retired refuses a new branch. */ 758 js_space_destroy(&s); 759 JsLimits l = lim(64, 2, 0, 0); 760 CHECK(js_space_init_limits(&s, "/tmp/jspace_prod_spill", &l) == JS_OK, "init 2 branches"); 761 js_branch_root(&s, &RZ, 79, &root); 762 js_branch_ref(&s, root, &rr); 763 CHECK(js_branch_fork_staged(&s, rr, 0, &b0) == JS_OK, "fork into the last slot"); 764 s.branch_gen[b0.id] = GEN_TOP(BranchGen) - 1; 765 s.branches[b0.id]->gen = GEN_TOP(BranchGen) - 1; 766 btop = (JsBranchRef){ b0.id, GEN_TOP(BranchGen) - 1 }; 767 CHECK(js_branch_release_ref(&s, btop, 0) == JS_OK, "release last slot at top generation"); 768 JsBranchRef nb; 769 CHECK(js_branch_fork_staged(&s, rr, 0, &nb) == JS_ERR_FULL, "retired slot reused instead of JS_ERR_FULL"); 770 js_space_destroy(&s); 771 } 772 773 /* Durable: a retired branch slot stays retired after reopen, a realization 774 * generation above 2^32 comes back whole, and a version-1 checkpoint (32-bit 775 * realization generations) is refused with JS_ERR_VERSION. */ 776 static void test_gen_durable(void) { 777 char dir[64], meta[128], data[128]; 778 snprintf(dir, sizeof dir, "/tmp/jspace_gen_XXXXXX"); 779 if (!mkdtemp(dir)) { CHECK(0, "mkdtemp"); return; } 780 snprintf(meta, sizeof meta, "%s/jspace.meta", dir); 781 snprintf(data, sizeof data, "%s/jspace.data", dir); 782 static JsSpace s; 783 JsHome me = { .locality = JS_HOME_LOCAL }; 784 memset(me.machine, 0x11, sizeof me.machine); 785 CHECK(js_space_open(&s, dir, RZS, 2, NULL, &me) == JS_OK, "fresh open"); 786 uint32_t a, b, c; 787 js_branch_root(&s, &RZ, 90, &a); 788 for (unsigned i = 0; i < 3; i++) js_branch_derive(&s, a, 900 + i); 789 CHECK(js_branch_fork(&s, a, &b) == JS_OK, "fork b"); 790 CHECK(js_branch_derive(&s, b, 950) == JS_OK, "derive b"); 791 uint32_t bl = s.branches[b]->n_units - 1; 792 const RealGen wide = sizeof(RealGen) == 8 ? (RealGen)((1ull << 32) + 5) : (RealGen)5; 793 CHECK(sizeof(RealGen) == 8, "realization generations are %zu bytes, not 8", sizeof(RealGen)); 794 unit_of(&s, b, bl)->gen = wide; 795 JsRealId wid; 796 js_real_id(&s, b, bl, &wid); 797 CHECK(js_branch_fork(&s, a, &c) == JS_OK, "fork c"); 798 s.branch_gen[c] = GEN_TOP(BranchGen) - 1; 799 s.branches[c]->gen = GEN_TOP(BranchGen) - 1; 800 CHECK(js_branch_release(&s, c) == JS_OK, "release c at top generation"); 801 CHECK(js_space_commit(&s) == JS_OK, "commit"); 802 js_space_destroy(&s); 803 804 CHECK(js_space_open(&s, dir, RZS, 2, NULL, &me) == JS_OK, "reopen"); 805 JsRealId got; 806 CHECK(js_real_id(&s, b, bl, &got) == JS_OK && got.gen == wid.gen && got.slot == wid.slot, 807 "wide realization generation did not survive reopen (%llu vs %llu)", 808 (unsigned long long)got.gen, (unsigned long long)wid.gen); 809 JsRealId low = { wid.slot, (RealGen)(uint32_t)wid.gen }; 810 CHECK(js_real_lookup(&s, low) == NULL, "truncated generation resolves"); 811 CHECK(s.branch_gen[c] == GEN_TOP(BranchGen), "retired branch slot generation %u after reopen", s.branch_gen[c]); 812 JsBranchRef ar; 813 js_branch_ref(&s, a, &ar); 814 for (int k = 0; k < 3; k++) { 815 JsBranchRef nb; 816 CHECK(js_branch_fork_staged(&s, ar, 0, &nb) == JS_OK, "fork after reopen %d", k); 817 CHECK(nb.id != c, "retired branch slot %u handed out after reopen", c); 818 CHECK(js_branch_check(&s, (JsBranchRef){ c, 0 }) == JS_ERR_STALE, "first ref of retired slot valid"); 819 js_branch_release_ref(&s, nb, 0); 820 } 821 js_space_destroy(&s); 822 823 /* Rewrite the header as version 1 (header hash recomputed): refused. */ 824 int fd = open(meta, O_RDWR); 825 uint8_t h[128]; 826 CHECK(fd >= 0 && pread(fd, h, sizeof h, 0) == (ssize_t)sizeof h, "read header"); 827 h[8] = 1; h[9] = h[10] = h[11] = 0; 828 sha256_hash(h, 96, h + 96); 829 CHECK(fd >= 0 && pwrite(fd, h, sizeof h, 0) == (ssize_t)sizeof h, "write v1 header"); 830 if (fd >= 0) close(fd); 831 int rc = js_space_open(&s, dir, RZS, 2, NULL, &me); 832 CHECK(rc == JS_ERR_VERSION, "version-1 checkpoint: open rc %d, want JS_ERR_VERSION", rc); 833 if (rc == JS_OK) js_space_destroy(&s); 834 unlink(meta); unlink(data); 835 rmdir(dir); 836 } 837 838 /* Client anchor: set -> commit -> reopen returns the same 48 bytes; a commit 839 * with no anchor set reopens absent; a version-2 checkpoint (no anchor section, 840 * made from a v3 one by dropping the section, patching the version and 841 * recomputing the body and header digests) still loads with the anchor absent. */ 842 static void test_anchor(void) { 843 char dir[64], meta[128], data[128]; 844 snprintf(dir, sizeof dir, "/tmp/jspace_anc_XXXXXX"); 845 if (!mkdtemp(dir)) { CHECK(0, "mkdtemp"); return; } 846 snprintf(meta, sizeof meta, "%s/jspace.meta", dir); 847 snprintf(data, sizeof data, "%s/jspace.data", dir); 848 static JsSpace s; 849 JsHome me = { .locality = JS_HOME_LOCAL }; 850 memset(me.machine, 0x22, sizeof me.machine); 851 uint8_t want[48], got[48]; 852 for (int i = 0; i < 48; i++) want[i] = (uint8_t)(i * 7 + 1); 853 CHECK(js_space_open(&s, dir, RZS, 2, NULL, &me) == JS_OK, "fresh open"); 854 CHECK(js_space_anchor(&s, got) == 0, "fresh space reports an anchor"); 855 uint32_t a; 856 js_branch_root(&s, &RZ, 91, &a); 857 CHECK(js_space_commit(&s) == JS_OK, "commit without anchor"); 858 js_space_destroy(&s); 859 CHECK(js_space_open(&s, dir, RZS, 2, NULL, &me) == JS_OK, "reopen no-anchor"); 860 CHECK(js_space_anchor(&s, got) == 0, "never-set anchor reads as present after reopen"); 861 js_space_set_anchor(&s, want); 862 CHECK(js_space_anchor(&s, got) == 1 && !memcmp(got, want, 48), "set anchor not visible before commit"); 863 CHECK(js_space_commit(&s) == JS_OK, "commit with anchor"); 864 js_space_destroy(&s); 865 CHECK(js_space_open(&s, dir, RZS, 2, NULL, &me) == JS_OK, "reopen with anchor"); 866 memset(got, 0, sizeof got); 867 CHECK(js_space_anchor(&s, got) == 1 && !memcmp(got, want, 48), "anchor did not round-trip"); 868 js_space_destroy(&s); 869 870 /* Version-2 fixture from the v3 file. */ 871 int fd = open(meta, O_RDONLY); 872 struct stat st; 873 CHECK(fd >= 0 && !fstat(fd, &st) && st.st_size > 128 + 48, "stat checkpoint"); 874 if (fd < 0 || st.st_size <= 128 + 48) { if (fd >= 0) close(fd); goto done; } 875 size_t n = (size_t)st.st_size; 876 uint8_t *f = malloc(n); 877 CHECK(f && pread(fd, f, n, 0) == (ssize_t)n, "read checkpoint"); 878 close(fd); 879 if (!f) goto done; 880 CHECK(f[8] == 3, "checkpoint version %u, want 3", f[8]); 881 size_t nn = n - 48; 882 uint64_t bl = (uint64_t)nn - 128; 883 for (int i = 0; i < 8; i++) f[24 + i] = (uint8_t)(bl >> (8 * i)); 884 f[8] = 2; 885 sha256_hash(f + 128, bl, f + 64); 886 sha256_hash(f, 96, f + 96); 887 fd = open(meta, O_WRONLY | O_TRUNC); 888 CHECK(fd >= 0 && write(fd, f, nn) == (ssize_t)nn, "write v2 checkpoint"); 889 if (fd >= 0) close(fd); 890 free(f); 891 int rc = js_space_open(&s, dir, RZS, 2, NULL, &me); 892 CHECK(rc == JS_OK, "version-2 checkpoint: open rc %d, want JS_OK", rc); 893 if (rc == JS_OK) { 894 CHECK(js_space_anchor(&s, got) == 0, "version-2 checkpoint reports an anchor"); 895 js_space_destroy(&s); 896 } 897 done: 898 unlink(meta); unlink(data); 899 rmdir(dir); 900 } 901 902 /* js_calibrate on a unit shorter than its 32 repetitions stays inside the 903 * buffer (ASan reports the old a[i] write past the end), and a zero-size unit 904 * is refused instead of dividing by zero. */ 905 static void test_calibrate(void) { 906 static const JsRealizer small = { JS_REAL_WORLD_PROJECTION, "test_small", 16, derive_t }; 907 static const JsRealizer empty = { JS_REAL_WORLD_PROJECTION, "test_empty", 0, derive_t }; 908 JsSpace s; 909 CHECK(js_space_init(&s, "/tmp/jspace_prod_spill") == JS_OK, "init"); 910 CHECK(js_calibrate(&s, &small) == JS_OK, "calibrate 16-byte unit"); 911 CHECK(s.costs.copy_ns_per_byte >= 0 && s.costs.derive_ns >= 1, "calibrated costs are sane"); 912 JsCosts before = s.costs; 913 CHECK(js_calibrate(&s, &empty) == JS_ERR_ARG, "zero-size unit refused"); 914 CHECK(js_calibrate(&s, NULL) == JS_ERR_ARG, "missing realizer refused"); 915 CHECK(memcmp(&before, &s.costs, sizeof before) == 0, "costs unchanged on refusal"); 916 js_space_destroy(&s); 917 } 918 919 int main(void) { 920 struct { const char *name; void (*fn)(void); } t[] = { 921 { "alloc_reclaim", test_alloc_reclaim }, 922 { "generations", test_generations }, 923 { "limits", test_limits }, 924 { "concurrency", test_concurrency }, 925 { "remote", test_remote }, 926 { "durable", test_durable }, 927 { "world_commit", test_world }, 928 { "gen_exhaustion", test_gen_exhaustion }, 929 { "gen_durable", test_gen_durable }, 930 { "anchor", test_anchor }, 931 { "calibrate", test_calibrate }, 932 }; 933 for (size_t i = 0; i < sizeof t / sizeof *t; i++) { 934 int before = g_fail; 935 t[i].fn(); 936 printf("%-14s %s\n", t[i].name, g_fail == before ? "PASS" : "FAIL"); 937 } 938 unlink("/tmp/jspace_prod_spill"); 939 unlink("/tmp/jspace_prod_spill2"); 940 printf("checks=%d failed=%d\n", g_checks, g_fail); 941 printf("M20_JSPACE_PROD=%s\n", g_fail ? "FAIL" : "PASS"); 942 return g_fail ? 1 : 0; 943 }