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/aienux/blob/main/native/kernel/tests/test_continuity_commit.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tests / test_continuity_commit.c` 802 lines ยท 33339 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 /* test_continuity_commit.c -- host tests of svc/continuity_commit.c (C 18 * continuity port, cut 4: provision, commit, resume; contract 19 * native/kernel/CONTINUITY_RECOVERY_CONTRACT.md, aienos#222). Oracle: 20 * crates/aienos-kernel/src/continuity.rs `provision`/`commit`/`resume` 21 * (:712-868), continuity_tests.rs, scripts/qemu_continuity_test.sh. 22 * 23 * Storage: the REAL sealed Store (native/store, M5 envelopes, anchor) in a 24 * file, at 512 and 4096 byte blocks. Entropy: the real ck_rng over a mocked 25 * RNDR (core/entropy.c). 26 * 27 * 1. Provision, resume N times, remember (INV-8..INV-10, markers). 28 * 2. Refusals: Unprovisioned resume, provision with a root / two roots / 29 * orphans, degraded mount, K-1 size, no entropy. 30 * 3. Power cut at every checkpoint (ST_CP_* 0-6, SS_CP_BEFORE_ANCHOR, 31 * SS_CP_AFTER_ANCHOR) AND at every block boundary, for commit and for 32 * provisioning: the store resolves to exactly the old or the new state. 33 * Mutants (make continuity-mutants) must each turn this test FAIL. */ 34 #include 35 #include 36 #include 37 #include 38 #include 39 40 #include "ck_test.h" 41 #include "continuity_resolve_sealed.h" 42 #include "disk_file.h" 43 #include "../argus/sha256.h" 44 45 #define CK_(c, ...) \ 46 do { \ 47 ck_t_run++; \ 48 if (!(c)) { \ 49 ck_t_fail++; \ 50 printf(" FAIL %s:%d: %s: ", __FILE__, __LINE__, #c); \ 51 printf(__VA_ARGS__); \ 52 printf("\n"); \ 53 } \ 54 } while (0) 55 56 #define A_UNITS 4u 57 #define S_UNITS 512u 58 #define T_UNITS (A_UNITS + S_UNITS) 59 static uint32_t g_su = S_UNITS; /* Store region units (K-2 full run enlarges it) */ 60 #define IMG_BYTES ((size_t)T_UNITS * 4096u) 61 62 static ss_workspace g_ws; 63 static ss_store g_s; 64 static disk_file g_f; 65 static disk_dev g_d; 66 static st_disk g_sd; 67 static st_dev g_sdev; 68 static ts_device g_tdev; 69 static ss_keys g_k; 70 static uint32_t g_bs; 71 static char g_path[256]; 72 static struct cr_work g_w; 73 static struct cr_txwork g_tw; 74 static struct cr_view g_v, g_v2; 75 static struct cr_source g_src; 76 static struct cr_sink g_snk; 77 static struct cr_sealed_sink g_sk; 78 static const uint8_t RUUID[16] = {0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 79 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a}; 80 static uint8_t base_img[IMG_BYTES], snap_img[IMG_BYTES]; 81 82 static int g_open; 83 static void dev_close(void) 84 { 85 if (g_open) disk_file_close(&g_f); 86 g_open = 0; 87 } 88 static int dev_open(int create) 89 { 90 dev_close(); 91 uint32_t bpu = 4096u / g_bs; 92 if (create) unlink(g_path); 93 memset(&g_f, 0, sizeof g_f); 94 if (disk_file_open(&g_f, &g_d, g_path, g_bs, (uint64_t)(A_UNITS + g_su) * bpu, create) != 0) return -1; 95 g_open = 1; 96 if (st_disk_bind(&g_sd, &g_d, (uint64_t)A_UNITS * bpu, g_su, &g_sdev) != 0) return -2; 97 ss_ts_device(&g_d, &g_tdev); 98 return 0; 99 } 100 static void make_keys(void) 101 { 102 uint8_t kvol[32]; 103 memset(kvol, 0x77, 32); 104 m5_subkeys sk; 105 m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); 106 memset(&g_k, 0, sizeof g_k); 107 g_k.identity_class = M5_ID_TEST; 108 g_k.key_generation = 1; 109 memcpy(g_k.k_root_auth, sk.k_root_auth, 32); 110 memcpy(g_k.k_domain, sk.k_artifact, 32); 111 m5_subkeys_wipe(&sk); 112 } 113 static void bind(st_hook hook) 114 { 115 g_sk.s = &g_s; 116 g_sk.hook = hook; 117 g_sk.hook_arg = NULL; 118 cr_bind_sealed(&g_src, &g_s); 119 cr_bind_sealed_sink(&g_snk, &g_sk); 120 } 121 static void fresh(void) 122 { 123 CK_(dev_open(1) == 0, "rig create"); 124 CK_(ss_format(&g_sdev, &g_tdev, 0, RUUID, &g_k, &g_ws) == 0, "format"); 125 CK_(ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) == 0, "open genesis"); 126 bind(NULL); 127 } 128 static int reopen(void) 129 { 130 if (dev_open(0) != 0) return -9999; 131 memset(&g_s, 0, sizeof g_s); 132 int rc = ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws); 133 bind(NULL); 134 return rc; 135 } 136 static int file_load(uint8_t *dst) 137 { 138 FILE *f = fopen(g_path, "rb"); 139 if (!f) return -1; 140 size_t n = fread(dst, 1, IMG_BYTES, f); 141 fclose(f); 142 return n == IMG_BYTES ? 0 : -2; 143 } 144 static int file_store(const uint8_t *src) 145 { 146 FILE *f = fopen(g_path, "wb"); 147 if (!f) return -1; 148 size_t n = fwrite(src, 1, IMG_BYTES, f); 149 fclose(f); 150 return n == IMG_BYTES ? 0 : -2; 151 } 152 /* The image must be byte for byte what it was (a refusal writes nothing). */ 153 static void snapshot(void) { CK_(file_load(snap_img) == 0, "snapshot"); } 154 static int unchanged(void) 155 { 156 static uint8_t now[IMG_BYTES]; 157 return file_load(now) == 0 && memcmp(now, snap_img, IMG_BYTES) == 0; 158 } 159 160 /* ---- mocked RNDR ---- */ 161 struct mock { 162 int present; 163 uint64_t ctr; 164 }; 165 static struct mock g_m; 166 static struct ck_rng_ops g_ops; 167 static struct ck_rng g_rng; 168 static int m_present(void *c) { return ((struct mock *)c)->present; } 169 static int m_read(void *c, uint64_t *out) 170 { 171 struct mock *m = c; 172 uint64_t z = (m->ctr += 0x9e3779b97f4a7c15ull); 173 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; 174 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; 175 *out = z ^ (z >> 31); 176 return 1; 177 } 178 static void rng_init(uint64_t seed, int present) 179 { 180 g_m.present = present; 181 g_m.ctr = seed; 182 g_ops.present = m_present; 183 g_ops.read = m_read; 184 g_ops.ctx = &g_m; 185 memset(&g_rng, 0, sizeof g_rng); 186 (void)ck_rng_probe(&g_rng, &g_ops); 187 } 188 189 static int do_provision(const char **why, int *rc) 190 { 191 return cr_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, &g_v, why, rc); 192 } 193 static int do_resume(int *committed) 194 { 195 return cr_resume(&g_src, &g_snk, &g_w, &g_tw, &g_v, committed, NULL, NULL); 196 } 197 static int do_resolve(void) 198 { 199 return cr_resolve(&g_src, &g_w, &g_v2, NULL, NULL); 200 } 201 static void hex(const uint8_t *p, size_t n, char *o) 202 { 203 static const char H[] = "`0123456789abcdef`"; 204 for (size_t i = 0; i < n; i++) { 205 o[2 * i] = H[p[i] >> 4]; 206 o[2 * i + 1] = H[p[i] & 15]; 207 } 208 o[2 * n] = 0; 209 } 210 211 static void check_marker(const char *word, const struct cr_view *v) 212 { 213 char got[300], want[300], ag[65], mem[17]; 214 hex(v->root.agent_id, 32, ag); 215 hex(v->memory, 8, mem); 216 snprintf(want, sizeof want, "CONTINUITY: %s agent=%s incarnation=%llu sequence=%llu cortex=%llu branches=%u memory=%s", 217 word, ag, (unsigned long long)v->manifest.incarnation, (unsigned long long)v->manifest.sequence, 218 (unsigned long long)v->cortex_count, v->state.n, mem); 219 size_t n = cr_marker_view(got, sizeof got, word, v); 220 CK_(n == strlen(want) && strcmp(got, want) == 0, "marker: got '%s' want '%s'", got, want); 221 CK_(cr_marker_view(got, 20, word, v) == 0, "marker too small buffer refused"); 222 } 223 224 /* ---- 1. provision, resume, remember ---- */ 225 226 static struct cc_record fact(const char *s) 227 { 228 struct cc_record r; 229 memset(&r, 0, sizeof r); 230 r.status = CC_EPI_DIRECT_OBSERVATION; 231 r.len = (uint16_t)strlen(s); 232 r.statement = (const uint8_t *)s; 233 return r; 234 } 235 236 static uint8_t g_agent_a[32]; 237 238 static void t_provision_resume(void) 239 { 240 fresh(); 241 rng_init(1001, 1); 242 uint64_t gen0 = ss_generation(&g_s); 243 const char *why = NULL; 244 int rc = 0; 245 int o = do_provision(&why, &rc); 246 CK_(o == CR_RESOLVED, "bs=%u provision: %s %s", g_bs, cr_outcome_name(o), why ? why : "-"); 247 CK_(g_v.manifest.incarnation == 1 && g_v.manifest.sequence == 1 && g_v.cortex_count == 0 && g_v.state.n == 1, 248 "provisioned view"); 249 CK_(g_v.state.written_at == 1 && g_v.root.source == CC_SOURCE_QUALIFICATION, "genesis written_at 1, source"); 250 CK_(g_v.root.provisioned_generation == gen0 + 1 && ss_generation(&g_s) == gen0 + 1, 251 "provisioned_generation = generation + 1, one transaction"); 252 CK_(memcmp(g_v.root.store_uuid, RUUID, 16) == 0, "store uuid"); 253 static const uint8_t Z[32]; 254 CK_(memcmp(g_v.root.agent_id, Z, 32) != 0, "agent id is not zero"); 255 /* contract 1.7: the agent id is the four RNDR words, little-endian, word 0 first (UNVERIFIED on 256 * hardware; here the mocked words are what ck_rng_fill returned). */ 257 { 258 struct mock m2 = {1, 1001}; 259 struct ck_rng_ops o2 = {m_present, m_read, &m2}; 260 struct ck_rng r2; 261 memset(&r2, 0, sizeof r2); 262 ck_rng_probe(&r2, &o2); 263 uint8_t want[32]; 264 CK_(ck_rng_fill(&r2, want, 32) == 0 && memcmp(want, g_v.root.agent_id, 32) == 0, 265 "agent id = ck_rng_fill output"); 266 } 267 check_marker("PROVISIONED", &g_v); 268 { 269 char mem[17]; 270 hex(g_v.memory, 8, mem); 271 CK_(strcmp(mem, "`e3b0c44298fc1c14`") == 0, "no records: memory= is SHA-256 of nothing, got %s", mem); 272 } 273 memcpy(g_agent_a, g_v.root.agent_id, 32); 274 275 /* Second provision: AlreadyProvisioned, nothing written (83a). */ 276 snapshot(); 277 rng_init(2002, 1); 278 o = do_provision(&why, &rc); 279 CK_(o == CR_ALREADY_PROVISIONED, "second provision: %s", cr_outcome_name(o)); 280 CK_(unchanged(), "second provision changed the image"); 281 char mk[200]; 282 cr_marker_outcome(mk, sizeof mk, o, why, rc); 283 CK_(strcmp(mk, "CONTINUITY: STOP (AlreadyProvisioned)") == 0, "marker '%s'", mk); 284 285 /* Cold restart, resume N times: same agent, incarnation +1 each, memory preserved (84, 85, 85a). */ 286 CK_(reopen() == 0, "reopen"); 287 int committed = 0; 288 CK_(do_resume(&committed) == CR_RESOLVED && committed == 1, "resume 1"); 289 CK_(g_v.manifest.incarnation == 2 && g_v.manifest.sequence == 2 && g_v.cortex_count == 0 && g_v.state.n == 1, 290 "RESUMED incarnation=2 sequence=2"); 291 CK_(memcmp(g_v.root.agent_id, g_agent_a, 32) == 0, "same agent"); 292 check_marker("RESUMED", &g_v); 293 294 /* remember: fork the root branch and append one record, in ONE transaction (REMEMBERED). */ 295 static struct cc_state st; 296 memcpy(&st, &g_v.state, sizeof st); 297 uint8_t child[32]; 298 CK_(cc_state_fork(&st, g_v.root.root_branch, child, NULL) == CC_OK, "fork"); 299 struct cc_record r1 = fact("qemu: the operator asked AIEN to remember this"); 300 struct cr_update up = {&st, &r1, 1}; 301 static struct cr_view cur; 302 memcpy(&cur, &g_v, sizeof cur); 303 uint64_t g1 = ss_generation(&g_s); 304 o = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &up, 0, &g_v, &why, &rc); 305 CK_(o == CR_RESOLVED, "remember: %s %s", cr_outcome_name(o), why ? why : "-"); 306 CK_(ss_generation(&g_s) == g1 + 1, "remember is ONE transaction (INV-8): gen %llu -> %llu", 307 (unsigned long long)g1, (unsigned long long)ss_generation(&g_s)); 308 CK_(g_v.manifest.incarnation == 2 && g_v.manifest.sequence == 3 && g_v.cortex_count == 1 && g_v.state.n == 2, 309 "REMEMBERED incarnation=2 sequence=3 cortex=1 branches=2"); 310 CK_(g_v.state.written_at == 3, "state.written_at = new sequence (INV-9)"); 311 check_marker("REMEMBERED", &g_v); 312 uint8_t mem1[32]; 313 memcpy(mem1, g_v.memory, 32); 314 315 uint64_t inc = 2, seq = 3; 316 for (int i = 0; i < 6; i++) { 317 CK_(reopen() == 0, "cold restart %d", i); 318 CK_(do_resume(&committed) == CR_RESOLVED && committed == 1, "resume %d", i); 319 inc++; 320 seq++; 321 CK_(g_v.manifest.incarnation == inc && g_v.manifest.sequence == seq, "cold restart %d: inc %llu seq %llu", i, 322 (unsigned long long)g_v.manifest.incarnation, (unsigned long long)g_v.manifest.sequence); 323 CK_(memcmp(g_v.root.agent_id, g_agent_a, 32) == 0 && memcmp(g_v.memory, mem1, 32) == 0 && 324 g_v.cortex_count == 1 && g_v.state.n == 2, 325 "same agent, same memory, same branches"); 326 CK_(do_resolve() == CR_RESOLVED && g_v2.manifest.incarnation == inc, "stored view agrees"); 327 } 328 /* Independent provisioning draws another agent (83b). */ 329 fresh(); 330 rng_init(3003, 1); 331 CK_(do_provision(&why, &rc) == CR_RESOLVED, "provision B"); 332 CK_(memcmp(g_v.root.agent_id, g_agent_a, 32) != 0, "independent provisioning gives a different agent (MC-10)"); 333 } 334 335 /* ---- 2. refusals ---- */ 336 337 static void t_unprovisioned_resume(void) 338 { 339 fresh(); 340 CK_(reopen() == 0, "reopen"); 341 snapshot(); 342 uint64_t gen0 = ss_generation(&g_s); 343 int committed = 7; 344 int o = do_resume(&committed); 345 CK_(o == CR_UNPROVISIONED && committed == 0, "bs=%u resume on Unprovisioned: %s", g_bs, cr_outcome_name(o)); 346 CK_(ss_generation(&g_s) == gen0 && unchanged(), "resume minted nothing (INV-4, MC-1)"); 347 CK_(reopen() == 0 && do_resolve() == CR_UNPROVISIONED, "still Unprovisioned after reopen"); 348 char mk[100]; 349 cr_marker_outcome(mk, sizeof mk, o, NULL, 0); 350 CK_(strcmp(mk, "CONTINUITY: UNPROVISIONED") == 0, "marker '%s'", mk); 351 } 352 353 /* An object committed straight to the Store, to build hostile states. */ 354 static int raw_commit(uint16_t kind, const uint8_t *b, size_t n) 355 { 356 ss_object so = {kind, 1, b, n}; 357 return ss_transact(&g_s, &so, 1, NULL, NULL, NULL); 358 } 359 360 static void t_provision_refusals(void) 361 { 362 const char *why = NULL; 363 int rc = 0, o; 364 /* with a root (MC-7) was covered in t_provision_resume; here: two roots and orphans. */ 365 fresh(); 366 rng_init(11, 1); 367 CK_(do_provision(&why, &rc) == CR_RESOLVED, "provision"); 368 /* a second, foreign root written behind the layer's back -> Conflict -> provision refuses */ 369 struct cc_root r; 370 memset(&r, 0, sizeof r); 371 memset(r.agent_id, 0x77, 32); 372 cc_root_branch_id(r.agent_id, r.root_branch); 373 r.provisioned_generation = 9; 374 r.source = CC_SOURCE_OPERATOR; 375 static uint8_t b[CC_MAX_OBJECT_BYTES]; 376 size_t n = 0; 377 CK_(cc_root_encode(&r, b, sizeof b, &n, NULL) == CC_OK && raw_commit(CC_KIND_AGENT_ROOT, b, n) == 0, "second root"); 378 CK_(do_resolve() == CR_CONFLICT, "two roots resolve to Conflict"); 379 snapshot(); 380 rng_init(12, 1); 381 o = do_provision(&why, &rc); 382 CK_(o == CR_ALREADY_PROVISIONED, "provision on Conflict: %s", cr_outcome_name(o)); 383 CK_(unchanged(), "refusal wrote nothing"); 384 385 /* orphan manifest only: provision must refuse (never provision over orphans). */ 386 fresh(); 387 struct cc_manifest m; 388 memset(&m, 0, sizeof m); 389 memset(m.root, 0x55, 32); 390 m.sequence = 1; 391 m.incarnation = 1; 392 memset(m.agent_state, 0x66, 32); 393 CK_(cc_manifest_encode(&m, b, sizeof b, &n, NULL) == CC_OK && raw_commit(CC_KIND_MANIFEST, b, n) == 0, "orphan manifest"); 394 snapshot(); 395 rng_init(13, 1); 396 o = do_provision(&why, &rc); 397 CK_(o == CR_CORRUPT && why && !strcmp(why, "continuity objects without an agent root"), 398 "provision over orphans: %s %s", cr_outcome_name(o), why ? why : "-"); 399 CK_(unchanged(), "orphans: nothing written"); 400 int committed = 0; 401 CK_(do_resume(&committed) == CR_CORRUPT && committed == 0, "resume over orphans is Corrupt, not Unprovisioned"); 402 CK_(unchanged(), "resume over orphans wrote nothing"); 403 404 /* no entropy: absent, then never probed; nothing written, no fallback */ 405 fresh(); 406 snapshot(); 407 rng_init(14, 0); 408 o = do_provision(&why, &rc); 409 CK_(o == CR_NO_ENTROPY && unchanged(), "no RNDR: %s", cr_outcome_name(o)); 410 char mk[100]; 411 cr_marker_outcome(mk, sizeof mk, o, NULL, 0); 412 CK_(strcmp(mk, "CONTINUITY: NO_ENTROPY") == 0, "marker '%s'", mk); 413 memset(&g_rng, 0, sizeof g_rng); 414 o = do_provision(&why, &rc); 415 CK_(o == CR_NO_ENTROPY && unchanged(), "unprobed rng: %s", cr_outcome_name(o)); 416 CK_(do_resolve() == CR_UNPROVISIONED, "still Unprovisioned"); 417 418 /* caller bugs */ 419 CK_(cr_provision(NULL, &g_snk, &g_w, &g_tw, &g_rng, RUUID, 1, &g_v, NULL, NULL) == CR_E_ARG, "NULL src"); 420 rng_init(15, 1); 421 CK_(cr_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, 0, &g_v, NULL, NULL) == CR_E_ARG, "bad source"); 422 CK_(cr_provision(&g_src, &g_snk, &g_w, &g_tw, NULL, RUUID, 1, &g_v, NULL, NULL) == CR_E_ARG, "NULL rng"); 423 CK_(unchanged(), "caller bugs wrote nothing"); 424 } 425 426 static void junk_inactive(void) 427 { 428 uint32_t active = st_active_slot(&g_s.st); 429 dev_close(); 430 FILE *f = fopen(g_path, "r+b"); 431 CK_(f != NULL, "open image"); 432 static uint8_t junk[4096]; 433 for (size_t i = 0; i < sizeof junk; i++) junk[i] = (uint8_t)(i * 37u + 11u); 434 if (f) { 435 fseek(f, (long)((A_UNITS + (1u - active)) * 4096u), SEEK_SET); 436 CK_(fwrite(junk, 1, sizeof junk, f) == sizeof junk, "write junk"); 437 fclose(f); 438 } 439 } 440 441 static void t_degraded(void) 442 { 443 const char *why = NULL; 444 int rc = 0, o; 445 /* provisioned store, malformed peer: resume is read only (INV-11), commit and provision are ReadOnly */ 446 fresh(); 447 rng_init(21, 1); 448 CK_(do_provision(&why, &rc) == CR_RESOLVED, "provision"); 449 int committed = 0; 450 CK_(do_resume(&committed) == CR_RESOLVED && committed == 1, "resume"); 451 uint8_t agent[32], mem[32]; 452 memcpy(agent, g_v.root.agent_id, 32); 453 memcpy(mem, g_v.memory, 32); 454 uint64_t inc = g_v.manifest.incarnation; 455 junk_inactive(); 456 CK_(reopen() == 0 && g_s.st.state == ST_DEGRADED_RECOVERY, "degraded mount (K-5)"); 457 snapshot(); 458 uint64_t gen = ss_generation(&g_s); 459 o = do_resume(&committed); 460 CK_(o == CR_RESOLVED && committed == 0, "bs=%u degraded resume: %s committed=%d", g_bs, cr_outcome_name(o), committed); 461 CK_(memcmp(g_v.root.agent_id, agent, 32) == 0 && memcmp(g_v.memory, mem, 32) == 0 && g_v.manifest.incarnation == inc, 462 "RESUMED_READONLY: same agent, memory, incarnation"); 463 check_marker("RESUMED_READONLY", &g_v); 464 struct cc_record r1 = fact("x"); 465 struct cr_update up = {NULL, &r1, 1}; 466 static struct cr_view cur; 467 memcpy(&cur, &g_v, sizeof cur); 468 o = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &up, 1, &g_v2, &why, &rc); 469 CK_(o == CR_READ_ONLY, "commit on a degraded mount: %s (MC-9)", cr_outcome_name(o)); 470 CK_(ss_generation(&g_s) == gen && unchanged(), "nothing written"); 471 cr_marker_outcome((char[64]){0}, 64, o, NULL, 0); 472 char mk[100]; 473 cr_marker_outcome(mk, sizeof mk, o, NULL, 0); 474 CK_(strcmp(mk, "CONTINUITY: STOP (ReadOnly)") == 0, "marker '%s'", mk); 475 476 /* degraded and unprovisioned: provision refuses ReadOnly (never mints on a degraded mount) */ 477 fresh(); 478 static uint8_t b[16]; 479 memset(b, 1, sizeof b); 480 CK_(raw_commit(0x0B01, b, sizeof b) == 0, "unrelated object (second generation)"); 481 junk_inactive(); 482 CK_(reopen() == 0 && g_s.st.state == ST_DEGRADED_RECOVERY, "degraded"); 483 snapshot(); 484 rng_init(22, 1); 485 o = do_provision(&why, &rc); 486 CK_(o == CR_READ_ONLY, "provision on a degraded mount: %s (MC-9)", cr_outcome_name(o)); 487 CK_(unchanged(), "provision wrote nothing"); 488 CK_(do_resolve() == CR_UNPROVISIONED, "still Unprovisioned"); 489 } 490 491 static void t_size_bound(void) 492 { 493 const char *why = NULL; 494 int rc = 0, o; 495 fresh(); 496 rng_init(31, 1); 497 CK_(do_provision(&why, &rc) == CR_RESOLVED, "provision"); 498 static struct cr_view cur; 499 memcpy(&cur, &g_v, sizeof cur); 500 501 /* K-1: 205 branches = 16464 bytes is Limit, nothing written; 204 = 16384 fits. */ 502 static struct cc_state st; 503 memcpy(&st, &cur.state, sizeof st); 504 uint8_t child[32]; 505 for (int i = 0; i < 203; i++) CK_(cc_state_fork(&st, cur.root.root_branch, child, NULL) == CC_OK, "fork"); 506 CK_(st.n == 204, "204 branches"); 507 snapshot(); 508 uint64_t gen0 = ss_generation(&g_s); 509 struct cr_update up = {&st, NULL, 0}; 510 o = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &up, 0, &g_v, &why, &rc); 511 CK_(o == CR_RESOLVED && ss_generation(&g_s) == gen0 + 1, "204 branches commit: %s %s", cr_outcome_name(o), why ? why : "-"); 512 memcpy(&cur, &g_v, sizeof cur); 513 memcpy(&st, &cur.state, sizeof st); 514 CK_(cc_state_fork(&st, cur.root.root_branch, child, NULL) == CC_OK && st.n == 205, "205 branches"); 515 snapshot(); 516 gen0 = ss_generation(&g_s); 517 o = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &up, 0, &g_v, &why, &rc); 518 CK_(o == CR_LIMIT && why && !strcmp(why, "continuity object exceeds the 16384-byte sealed Store cap"), 519 "205 branches: %s %s (K-1)", cr_outcome_name(o), why ? why : "-"); 520 CK_(ss_generation(&g_s) == gen0 && unchanged(), "K-1: nothing written"); 521 522 /* 64 records of 1024 bytes = 67872 bytes: Limit, never truncated (MC-11). */ 523 static uint8_t stmt[1024]; 524 memset(stmt, 'm', sizeof stmt); 525 static struct cc_record recs[64]; 526 for (int i = 0; i < 64; i++) { 527 recs[i] = fact(""); 528 recs[i].len = 1024; 529 recs[i].statement = stmt; 530 } 531 struct cr_update w64 = {NULL, recs, 64}; 532 snapshot(); 533 o = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &w64, 0, &g_v2, &why, &rc); 534 CK_(o == CR_LIMIT, "64 x 1024 byte WAL: %s (K-1, MC-11)", cr_outcome_name(o)); 535 CK_(unchanged(), "oversize WAL: nothing written, nothing truncated"); 536 struct cr_update w65 = {NULL, recs, 65}; 537 o = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &w65, 0, &g_v2, &why, &rc); 538 CK_(o == CR_LIMIT && why && !strcmp(why, "WAL segment record count"), "65 records: %s", cr_outcome_name(o)); 539 /* 15 records of 1024 bytes = 15.9 KiB fits and reads back whole. */ 540 struct cr_update w15 = {NULL, recs, 15}; 541 o = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &w15, 0, &g_v2, &why, &rc); 542 CK_(o == CR_RESOLVED && g_v2.cortex_count == 15, "15 x 1024 fits and is read back whole: %s %llu", cr_outcome_name(o), 543 (unsigned long long)g_v2.cortex_count); 544 } 545 546 /* K-2: the catalog bound. */ 547 static void t_catalog_bound(void) 548 { 549 const char *why = NULL; 550 int rc = 0; 551 fresh(); 552 rng_init(41, 1); 553 CK_(do_provision(&why, &rc) == CR_RESOLVED, "provision"); 554 uint32_t n; 555 st_catalog(&g_s.st, &n); 556 uint32_t after_prov = n; 557 struct cc_record r1 = fact("k2"); 558 struct cr_update up = {NULL, &r1, 1}; 559 static struct cr_view cur; 560 int ok = 1; 561 for (int i = 0; i < 64 && ok; i++) { 562 memcpy(&cur, &g_v, sizeof cur); 563 ok = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &up, 0, &g_v2, &why, &rc) == CR_RESOLVED; 564 memcpy(&g_v, &g_v2, sizeof g_v); 565 } 566 CK_(ok && g_v.manifest.n_wal == 64, "64 WAL commits"); 567 st_catalog(&g_s.st, &n); 568 printf("K-2 (bs=%u): catalog entries after provision = %u; after provision + 64 one-record WAL commits = %u " 569 "(%.2f entries per transaction; bound %u)\n", 570 g_bs, after_prov, n, (double)(n - after_prov) / 64.0, (unsigned)SV1_MAX_CATALOG_ENTRIES); 571 CK_(n == after_prov + 64u * 3u, "each WAL commit adds WAL + manifest + transaction record (3 entries), got %u", n); 572 memcpy(&cur, &g_v, sizeof cur); 573 uint64_t gen0 = ss_generation(&g_s); 574 int o = cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &up, 0, &g_v2, &why, &rc); 575 CK_(o == CR_LIMIT && why && !strcmp(why, "cortex WAL needs compaction"), "65th WAL segment: %s", cr_outcome_name(o)); 576 CK_(ss_generation(&g_s) == gen0, "nothing written"); 577 } 578 579 /* K-2 run to exhaustion (slow, CK_K2_FULL=1): resumes add manifest + transaction record = 2 entries. */ 580 static void t_catalog_full(void) 581 { 582 const char *why = NULL; 583 int rc = 0, committed = 0, oo; 584 uint32_t n; 585 g_su = 16384; 586 fresh(); 587 rng_init(51, 1); 588 CK_(do_provision(&why, &rc) == CR_RESOLVED, "provision"); 589 unsigned resumes = 0; 590 while ((oo = cr_resume(&g_src, &g_snk, &g_w, &g_tw, &g_v2, &committed, &why, &rc)) == CR_RESOLVED) resumes++; 591 st_catalog(&g_s.st, &n); 592 printf("K-2 full (bs=4096, %u-unit Store region): %u resumes after provisioning, then %s rc=%d (%s); catalog entries %u of %u\n", 593 g_su, resumes, cr_outcome_name(oo), rc, rc ? st_strerror(rc) : "-", n, (unsigned)SV1_MAX_CATALOG_ENTRIES); 594 CK_(oo == CR_STORE && (rc == ST_E_CATALOG_FULL || rc == ST_E_NO_SPACE), "stops with a Store refusal (catalog full or no space), not a wrong result"); 595 g_su = S_UNITS; 596 } 597 598 /* ---- 3. power cuts ---- */ 599 600 static int g_cp_target, g_cp_fired; 601 static void cp_hook(void *arg, int cp) 602 { 603 (void)arg; 604 if (cp == g_cp_target && !g_cp_fired) { 605 g_cp_fired = 1; 606 disk_file_arm_cut(&g_f, 0); 607 } 608 } 609 enum { CUT_CHECKPOINTS = 9 }; 610 static const int CPS[CUT_CHECKPOINTS] = {ST_CP_BEFORE_FIRST_WRITE, ST_CP_AFTER_PAYLOAD_OBJECTS, ST_CP_AFTER_CATALOG, 611 ST_CP_AFTER_COMMIT_RECORD, ST_CP_AFTER_FIRST_FLUSH, 612 ST_CP_AFTER_INACTIVE_SUPERBLOCK, ST_CP_AFTER_FINAL_FLUSH, 613 SS_CP_BEFORE_ANCHOR, SS_CP_AFTER_ANCHOR}; 614 static const char *cp_name(int cp) 615 { 616 return cp >= 100 ? (cp == SS_CP_BEFORE_ANCHOR ? "before_anchor" : "after_anchor") : st_checkpoint_name(cp); 617 } 618 619 struct pc_stats { 620 int old, neu, other_bad, degraded; 621 }; 622 623 /* The operation under test. */ 624 enum { OP_COMMIT, OP_PROVISION }; 625 static uint64_t g_gen_old; 626 static uint8_t g_agent_exp[32]; /* provisioning: the agent the mocked RNDR yields */ 627 static struct cr_view g_old_view, g_new_view; 628 629 static int run_op(int op) 630 { 631 const char *why = NULL; 632 int rc = 0; 633 if (op == OP_PROVISION) { 634 rng_init(777, 1); 635 return do_provision(&why, &rc); 636 } 637 static struct cc_state st; 638 memcpy(&st, &g_old_view.state, sizeof st); 639 uint8_t child[32]; 640 (void)cc_state_fork(&st, g_old_view.root.root_branch, child, NULL); 641 struct cc_record r1 = fact("remember across the crash"); 642 struct cr_update up = {&st, &r1, 1}; 643 return cr_commit(&g_src, &g_snk, &g_w, &g_tw, &g_old_view, &up, 1, &g_v, &why, &rc); 644 } 645 646 /* Verdict after the cut: exactly the old state or exactly the new one. Returns 1 old, 2 new, 0 bad. */ 647 static int classify(int op, const char *what) 648 { 649 int rc = reopen(); 650 CK_(rc == 0, "%s: reopen after the cut: %s (%d)", what, rc ? ss_strerror(rc) : "ok", rc); 651 if (rc != 0) return 0; 652 uint64_t gen = ss_generation(&g_s); 653 int o = do_resolve(); 654 if (op == OP_PROVISION) { 655 if (o == CR_UNPROVISIONED) { 656 CK_(gen == g_gen_old, "%s: old state but generation %llu", what, (unsigned long long)gen); 657 return gen == g_gen_old ? 1 : 0; 658 } 659 int ok = o == CR_RESOLVED && gen == g_gen_old + 1 && memcmp(g_v2.root.agent_id, g_agent_exp, 32) == 0 && 660 g_v2.manifest.sequence == 1 && g_v2.manifest.incarnation == 1 && g_v2.state.n == 1; 661 CK_(ok, "%s: neither Unprovisioned nor the full genesis: %s gen %llu", what, cr_outcome_name(o), (unsigned long long)gen); 662 return ok ? 2 : 0; 663 } 664 if (o != CR_RESOLVED) { 665 CK_(0, "%s: resolve after cut: %s", what, cr_outcome_name(o)); 666 return 0; 667 } 668 CK_(memcmp(g_v2.root.agent_id, g_old_view.root.agent_id, 32) == 0, "%s: identity changed", what); 669 if (g_v2.manifest.sequence == g_old_view.manifest.sequence) { 670 int ok = gen == g_gen_old && g_v2.manifest.incarnation == g_old_view.manifest.incarnation && 671 g_v2.cortex_count == g_old_view.cortex_count && memcmp(g_v2.memory, g_old_view.memory, 32) == 0 && 672 g_v2.state.n == g_old_view.state.n; 673 CK_(ok, "%s: sequence old but content mixed (gen %llu)", what, (unsigned long long)gen); 674 return ok ? 1 : 0; 675 } 676 int ok = gen == g_gen_old + 1 && g_v2.manifest.sequence == g_new_view.manifest.sequence && 677 g_v2.manifest.incarnation == g_new_view.manifest.incarnation && g_v2.cortex_count == g_new_view.cortex_count && 678 memcmp(g_v2.memory, g_new_view.memory, 32) == 0 && g_v2.state.n == g_new_view.state.n; 679 CK_(ok, "%s: third state: seq %llu inc %llu gen %llu", what, (unsigned long long)g_v2.manifest.sequence, 680 (unsigned long long)g_v2.manifest.incarnation, (unsigned long long)gen); 681 return ok ? 2 : 0; 682 } 683 684 /* After a cut the store must keep working: provision again (old) / resume (any). */ 685 static void still_works(int op, int verdict, const char *what) 686 { 687 if (!verdict) return; 688 if (g_s.st.state != ST_VALID) return; /* degraded: read only, the Recovery Core's case, not this layer's */ 689 int committed = 0; 690 if (op == OP_PROVISION && verdict == 1) { 691 const char *why = NULL; 692 int rc = 0; 693 rng_init(888, 1); 694 int o = do_provision(&why, &rc); 695 CK_(o == CR_RESOLVED, "%s: provisioning again after a cut that left the old state: %s %s", what, cr_outcome_name(o), 696 why ? why : "-"); 697 return; 698 } 699 int o = do_resume(&committed); 700 CK_(o == CR_RESOLVED && committed == 1, "%s: resume after the cut: %s", what, cr_outcome_name(o)); 701 } 702 703 static void cut_campaign(int op, const char *label) 704 { 705 /* Base state for this op. */ 706 fresh(); 707 if (op == OP_COMMIT) { 708 rng_init(999, 1); 709 const char *why = NULL; 710 int rc = 0; 711 CK_(do_provision(&why, &rc) == CR_RESOLVED, "provision (base)"); 712 int committed = 0; 713 CK_(do_resume(&committed) == CR_RESOLVED, "resume (base)"); 714 memcpy(&g_old_view, &g_v, sizeof g_old_view); 715 } 716 dev_close(); 717 CK_(file_load(base_img) == 0, "save base"); 718 719 /* Learn the new state and the block count with an uncut run. */ 720 CK_(reopen() == 0, "reopen base"); 721 g_gen_old = ss_generation(&g_s); 722 uint64_t w0 = g_f.blocks_written; 723 int o = run_op(op); 724 CK_(o == CR_RESOLVED, "%s uncut: %s", label, cr_outcome_name(o)); 725 uint64_t W = g_f.blocks_written - w0; 726 memcpy(&g_new_view, &g_v, sizeof g_new_view); 727 if (op == OP_PROVISION) memcpy(g_agent_exp, g_v.root.agent_id, 32); 728 dev_close(); 729 730 struct pc_stats cp = {0}, bl = {0}; 731 /* Every checkpoint. */ 732 for (int i = 0; i < CUT_CHECKPOINTS; i++) { 733 char what[96]; 734 snprintf(what, sizeof what, "%s bs=%u cp %s", label, g_bs, cp_name(CPS[i])); 735 CK_(file_store(base_img) == 0, "restore"); 736 CK_(reopen() == 0, "reopen base"); 737 g_cp_target = CPS[i]; 738 g_cp_fired = 0; 739 bind(cp_hook); 740 (void)run_op(op); 741 CK_(g_cp_fired == 1, "%s: checkpoint never reached", what); 742 disk_file_disarm(&g_f); 743 int v = classify(op, what); 744 /* Expected by the Rust oracle and the sealed-store rule (contract 6.1). */ 745 if (CPS[i] <= ST_CP_AFTER_FIRST_FLUSH) CK_(v == 1, "%s: expected the old state", what); 746 if (CPS[i] == ST_CP_AFTER_FINAL_FLUSH || CPS[i] >= SS_CP_BEFORE_ANCHOR) 747 CK_(v == 2, "%s: expected the new state", what); 748 if (CPS[i] == SS_CP_BEFORE_ANCHOR) CK_(g_s.rb == SS_RB_PREPARED_ADVANCE, "%s: anchor one behind (rb=%d)", what, g_s.rb); 749 cp.old += v == 1; 750 cp.neu += v == 2; 751 cp.other_bad += v == 0; 752 cp.degraded += g_s.st.state == ST_DEGRADED_RECOVERY; 753 still_works(op, v, what); 754 } 755 /* Every block boundary of the write sequence. */ 756 for (uint64_t k = 0; k <= W; k++) { 757 char what[96]; 758 snprintf(what, sizeof what, "%s bs=%u block cut %llu/%llu", label, g_bs, (unsigned long long)k, (unsigned long long)W); 759 CK_(file_store(base_img) == 0, "restore"); 760 CK_(reopen() == 0, "reopen base"); 761 disk_file_arm_cut(&g_f, k); 762 (void)run_op(op); 763 disk_file_disarm(&g_f); 764 int v = classify(op, what); 765 if (k == 0) CK_(v == 1, "%s: cut 0 must be old", what); 766 if (k == W) CK_(v == 2, "%s: full write must be new", what); 767 bl.old += v == 1; 768 bl.neu += v == 2; 769 bl.other_bad += v == 0; 770 bl.degraded += g_s.st.state == ST_DEGRADED_RECOVERY; 771 still_works(op, v, what); 772 } 773 printf("power cut, %s, %4u-byte blocks: %d checkpoints (%d old, %d new, %d bad), %llu block boundaries " 774 "(%d old, %d new, %d bad, %d read-only degraded)\n", 775 label, g_bs, CUT_CHECKPOINTS, cp.old, cp.neu, cp.other_bad, (unsigned long long)W + 1, bl.old, bl.neu, 776 bl.other_bad, bl.degraded); 777 CK_(cp.other_bad == 0 && bl.other_bad == 0, "no mixed state"); 778 CK_(cp.old + cp.neu == CUT_CHECKPOINTS, "every checkpoint classified"); 779 CK_(bl.old + bl.neu == (int)W + 1, "every block cut classified"); 780 } 781 782 int main(void) 783 { 784 make_keys(); 785 snprintf(g_path, sizeof g_path, "/tmp/ck_cm_test_%d.img", (int)getpid()); 786 static const uint32_t geos[2] = {4096u, 512u}; 787 for (int g = 0; g < 2; g++) { 788 g_bs = geos[g]; 789 t_provision_resume(); 790 t_unprovisioned_resume(); 791 t_provision_refusals(); 792 t_degraded(); 793 t_size_bound(); 794 t_catalog_bound(); 795 if (g_bs == 4096 && getenv("CK_K2_FULL")) t_catalog_full(); 796 cut_campaign(OP_COMMIT, "commit"); 797 cut_campaign(OP_PROVISION, "provision"); 798 } 799 dev_close(); 800 unlink(g_path); 801 return ck_t_verdict("test_continuity_commit"); 802 }