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_subject.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_subject.c` 771 lines ยท 33192 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_subject.c -- host tests of svc/continuity_subject.c 18 * (ALLEN subject state, kind 24; ARCH-0035 / OS-0018, both PROPOSED). 19 * 20 * No arguments: the unit suite. 21 * 1. Known answer: the genesis subject object's bytes, written out by hand 22 * from the layout in continuity_subject.h, and its logical ObjectId 23 * computed by store_v1's independent twin (sv1_object_id). 24 * 2. Round trips; deterministic re-encode (same bytes, same id). 25 * 3. One refusal per rule (magic, version, reserved, length, truncation, 26 * trailing bytes, zero root/agent/provenance, origin, bounds, intent id 27 * derivation, slot rule, supersession rules, ordering, since bounds). 28 * 4. Standing intents: intend, supersede (one active per slot), retire. 29 * 5. Resolve over a fake source: ABSENT, head of a chain, fork, gap, broken 30 * link, foreign root/agent (ALLEN-G1 identity binding). 31 * 6. Model independence (ALLEN-G4, structural): the encoding has no field 32 * for a model; two subjects that differ only in an out-of-band "model" 33 * value have byte-identical objects and ids. 34 * 7. Real sealed Store (native/store, M5 envelopes) in a file: provision, 35 * commit genesis, commit a successor with one intent, close, reopen, 36 * resolve the same head (in-process persistence). 37 * 38 * `--write ` and `--restore `: ALLEN-G2 restart proof in 39 * two processes (Makefile test-continuity-subject-restart). The writer 40 * provisions, commits a subject with one standing intent, prints the head id 41 * and exits; the restorer is a fresh process that opens the same image, 42 * resolves, and checks the subject, the intent and the id. The negative 43 * control for G2 lives where the pre-ALLEN goal lives (omega); here the 44 * structural half: cr_view (the pre-ALLEN continuity view) carries no intent. 45 * 46 * Mutants (make continuity-subject-mutants) must each turn this test FAIL. */ 47 #include 48 #include 49 #include 50 #include 51 #include 52 53 #include "ck_test.h" 54 #include "continuity_commit.h" 55 #include "continuity_resolve_sealed.h" 56 #include "continuity_subject.h" 57 #include "disk_file.h" 58 #include "store_v1.h" 59 #include "../argus/sha256.h" 60 61 #define CK_(c, ...) \ 62 do { \ 63 ck_t_run++; \ 64 if (!(c)) { \ 65 ck_t_fail++; \ 66 printf(" FAIL %s:%d: ", __FILE__, __LINE__); \ 67 printf(__VA_ARGS__); \ 68 printf("\n"); \ 69 } \ 70 } while (0) 71 72 static const char *why; 73 static struct cs_subject S, S2, S3; 74 static uint8_t buf[CC_MAX_OBJECT_BYTES + 64], buf2[CC_MAX_OBJECT_BYTES + 64]; 75 static struct cr_view V; 76 static struct cr_work W; 77 78 static void fill(uint8_t *d, size_t n, uint8_t v) 79 { 80 memset(d, v, n); 81 } 82 83 static void hex(const uint8_t *b, size_t n, char *out) 84 { 85 static const char *h = "`0123456789abcdef`"; 86 for (size_t i = 0; i < n; i++) { 87 out[2 * i] = h[b[i] >> 4]; 88 out[2 * i + 1] = h[b[i] & 15]; 89 } 90 out[2 * n] = 0; 91 } 92 93 static int unhex(const char *s, uint8_t *out, size_t n) 94 { 95 for (size_t i = 0; i < n; i++) { 96 unsigned v; 97 if (sscanf(s + 2 * i, "%2x", &v) != 1) return -1; 98 out[i] = (uint8_t)v; 99 } 100 return 0; 101 } 102 103 /* A resolved view for a test agent: root id and agent id as the resolver 104 * would hand them over. */ 105 static void view(struct cr_view *v, uint8_t root_byte, uint8_t agent_byte) 106 { 107 memset(v, 0, sizeof *v); 108 fill(v->root_id, 32, root_byte); 109 fill(v->root.agent_id, 32, agent_byte); 110 } 111 112 static const uint8_t PROV[32] = {0xaa, 0xbb, 0xcc, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 113 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}; 114 115 /* ---- 1. known answer ---- */ 116 static void t_known_answer(void) 117 { 118 size_t len = 0; 119 uint8_t id[32], id2[32]; 120 uint8_t exp[CC_HEADER_BYTES + CS_FIXED_BODY]; 121 size_t at = 0; 122 view(&V, 0x11, 0x22); 123 cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); 124 CK_(cs_subject_encode(&S, buf, sizeof buf, &len, &why) == CC_OK, "encode genesis: %s", why); 125 CK_(len == CC_HEADER_BYTES + CS_FIXED_BODY, "genesis is header + fixed body (%zu)", len); 126 /* By hand from continuity_subject.h. */ 127 memcpy(exp + at, "AIENSUBJ", 8); 128 at += 8; 129 exp[at++] = 0; exp[at++] = 0; /* format_version 0 */ 130 exp[at++] = 0; exp[at++] = 0; /* reserved */ 131 exp[at++] = (uint8_t)CS_FIXED_BODY; exp[at++] = 0; exp[at++] = 0; exp[at++] = 0; /* body 184 */ 132 fill(exp + at, 32, 0x11); at += 32; /* root */ 133 fill(exp + at, 32, 0x22); at += 32; /* agent */ 134 fill(exp + at, 32, 0); at += 32; /* previous */ 135 exp[at++] = 1; fill(exp + at, 7, 0); at += 7; /* sequence 1 */ 136 fill(exp + at, 32, 0); at += 32; /* cortex unbound */ 137 memcpy(exp + at, PROV, 32); at += 32; /* provenance */ 138 exp[at++] = CS_ORIGIN_OPERATOR; fill(exp + at, 7, 0); at += 7; 139 fill(exp + at, 8, 0); at += 8; /* n_intents, n_knowledge, reserved */ 140 CK_(at == sizeof exp, "hand layout is %zu bytes", at); 141 CK_(memcmp(exp, buf, sizeof exp) == 0, "genesis bytes match the hand layout"); 142 CK_(cs_subject_id(buf, len, id) == CC_OK, "subject id"); 143 CK_(sv1_object_id(CC_KIND_SUBJECT, CC_STORE_OBJECT_VERSION, buf, len, id2) == 0, "sv1 twin"); 144 CK_(memcmp(id, id2, 32) == 0, "subject id is the Store v1 logical ObjectId (kind 24, version 1)"); 145 } 146 147 /* ---- 2. round trips ---- */ 148 static void t_round_trip(void) 149 { 150 size_t len = 0, len2 = 0; 151 uint8_t a[32], b[32], k[32]; 152 uint64_t p[2] = {7, 1000}; 153 uint64_t q[2] = {9, 2000}; 154 view(&V, 0x11, 0x22); 155 cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); 156 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, a, &why) == CC_OK, "intend: %s", why); 157 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, q, b, &why) == CC_OK, "intend 2: %s", why); 158 fill(k, 32, 0x5c); 159 CK_(cs_subject_hold(&S, k, 17, &why) == CC_OK, "hold: %s", why); 160 fill(k, 32, 0x3c); 161 CK_(cs_subject_hold(&S, k, 18, &why) == CC_OK, "hold 2: %s", why); 162 fill(k, 32, 0x3c); 163 CK_(cs_subject_hold(&S, k, 18, &why) == CC_E_ARG, "duplicate knowledge refused"); 164 fill(k, 32, 0x77); 165 cs_subject_bind_cortex(&S, k); 166 CK_(cs_subject_encode(&S, buf, sizeof buf, &len, &why) == CC_OK, "encode: %s", why); 167 CK_(len == CC_HEADER_BYTES + CS_FIXED_BODY + 2 * CS_INTENT_BYTES + 2 * CS_KNOWLEDGE_BYTES, "len %zu", len); 168 CK_(cs_subject_decode(buf, len, &S2, &why) == CC_OK, "decode: %s", why); 169 CK_(memcmp(&S, &S2, sizeof S) == 0, "round trip equal"); 170 CK_(cs_subject_encode(&S2, buf2, sizeof buf2, &len2, &why) == CC_OK, "re-encode"); 171 CK_(len == len2 && memcmp(buf, buf2, len) == 0, "re-encode is byte identical"); 172 CK_(S2.n_intents == 2 && S2.n_knowledge == 2, "counts"); 173 CK_(memcmp(S2.kn[0].digest, S2.kn[1].digest, 32) < 0, "knowledge sorted"); 174 CK_(memcmp(S2.in[0].id, S2.in[1].id, 32) < 0, "intents sorted"); 175 CK_(cs_subject_active(&S2, CS_INTENT_GOAL_LATENCY, 7) && cs_subject_active(&S2, CS_INTENT_GOAL_LATENCY, 9), 176 "both slots active"); 177 } 178 179 /* ---- 3. refusals ---- */ 180 static void expect_refuse(const uint8_t *b, size_t len, int cls, const char *text) 181 { 182 int rc = cs_subject_decode(b, len, &S3, &why); 183 CK_(rc == cls, "%s: class %d (why=%s)", text, rc, why ? why : "-"); 184 CK_(why && strcmp(why, text) == 0, "%s: reason is '%s'", text, why ? why : "-"); 185 } 186 187 static void t_refusals(void) 188 { 189 size_t len = 0; 190 uint64_t p[2] = {7, 1000}; 191 view(&V, 0x11, 0x22); 192 cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); 193 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); 194 CK_(cs_subject_encode(&S, buf, sizeof buf, &len, &why) == CC_OK, "encode base"); 195 /* header */ 196 memcpy(buf2, buf, len); 197 buf2[0] ^= 1; 198 expect_refuse(buf2, len, CC_E_CORRUPT, "bad magic"); 199 memcpy(buf2, buf, len); 200 buf2[8] = 1; 201 expect_refuse(buf2, len, CC_E_CORRUPT, "unsupported continuity format version"); 202 memcpy(buf2, buf, len); 203 buf2[10] = 1; 204 expect_refuse(buf2, len, CC_E_CORRUPT, "nonzero header reserved"); 205 memcpy(buf2, buf, len); 206 buf2[12] ^= 1; 207 expect_refuse(buf2, len, CC_E_CORRUPT, "body length mismatch"); 208 expect_refuse(buf, len - 1, CC_E_CORRUPT, "body length mismatch"); 209 memcpy(buf2, buf, len); 210 sv1_put32(buf2 + 12, (uint32_t)(len - CC_HEADER_BYTES + 1)); 211 expect_refuse(buf2, len + 1, CC_E_CORRUPT, "trailing bytes"); 212 /* truncated inside the intents: the length field is made to agree so the 213 * one bounds check is what refuses it */ 214 memcpy(buf2, buf, len); 215 sv1_put32(buf2 + 12, (uint32_t)(len - CC_HEADER_BYTES - 10)); 216 expect_refuse(buf2, len - 10, CC_E_CORRUPT, "truncated object"); 217 expect_refuse(buf, 5, CC_E_CORRUPT, "truncated object"); 218 /* body rules */ 219 memcpy(buf2, buf, len); 220 fill(buf2 + 16, 32, 0); 221 expect_refuse(buf2, len, CC_E_CORRUPT, "zero root id"); 222 memcpy(buf2, buf, len); 223 fill(buf2 + 48, 32, 0); 224 expect_refuse(buf2, len, CC_E_CORRUPT, "zero agent id"); 225 memcpy(buf2, buf, len); 226 buf2[80] = 1; /* previous nonzero on sequence 1 */ 227 expect_refuse(buf2, len, CC_E_CORRUPT, "genesis subject names a previous object"); 228 memcpy(buf2, buf, len); 229 buf2[112] = 2; /* sequence 2 without previous */ 230 expect_refuse(buf2, len, CC_E_CORRUPT, "subject object after the first has no previous"); 231 memcpy(buf2, buf, len); 232 buf2[112] = 0; 233 expect_refuse(buf2, len, CC_E_CORRUPT, "zero sequence"); 234 memcpy(buf2, buf, len); 235 fill(buf2 + 152, 32, 0); 236 expect_refuse(buf2, len, CC_E_CORRUPT, "zero provenance"); 237 memcpy(buf2, buf, len); 238 buf2[184] = 9; 239 expect_refuse(buf2, len, CC_E_CORRUPT, "unknown subject origin"); 240 memcpy(buf2, buf, len); 241 buf2[185] = 1; 242 expect_refuse(buf2, len, CC_E_CORRUPT, "nonzero reserved bytes"); 243 memcpy(buf2, buf, len); 244 sv1_put16(buf2 + 192, CS_MAX_INTENTS + 1); 245 expect_refuse(buf2, len, CC_E_LIMIT, "too many standing intents"); 246 memcpy(buf2, buf, len); 247 sv1_put16(buf2 + 194, CS_MAX_KNOWLEDGE + 1); 248 expect_refuse(buf2, len, CC_E_LIMIT, "too many knowledge references"); 249 memcpy(buf2, buf, len); 250 buf2[197] = 1; 251 expect_refuse(buf2, len, CC_E_CORRUPT, "nonzero reserved bytes"); 252 /* intent rules (intent starts at 200: id 200, supersedes 232, kind 264, 253 * state 268, reserved 269, since 272, payload 280) */ 254 memcpy(buf2, buf, len); 255 buf2[200] ^= 1; 256 expect_refuse(buf2, len, CC_E_CORRUPT, "intent id does not derive from its content"); 257 memcpy(buf2, buf, len); 258 buf2[268] = 7; 259 expect_refuse(buf2, len, CC_E_CORRUPT, "unknown intent state"); 260 memcpy(buf2, buf, len); 261 buf2[264] = 2; 262 expect_refuse(buf2, len, CC_E_CORRUPT, "unknown intent kind"); 263 memcpy(buf2, buf, len); 264 buf2[269] = 1; 265 expect_refuse(buf2, len, CC_E_CORRUPT, "nonzero reserved bytes"); 266 memcpy(buf2, buf, len); 267 buf2[272] = 2; /* since 2 > sequence 1 (id no longer derives either; since is checked first) */ 268 expect_refuse(buf2, len, CC_E_CORRUPT, "intent created outside the subject chain"); 269 memcpy(buf2, buf, len); 270 memcpy(buf2 + 232, buf2 + 200, 32); /* supersedes itself */ 271 expect_refuse(buf2, len, CC_E_CORRUPT, "intent supersedes itself"); 272 memcpy(buf2, buf, len); 273 buf2[232] = 1; /* supersedes an absent intent */ 274 expect_refuse(buf2, len, CC_E_CORRUPT, "superseded intent is absent"); 275 /* K-1 cap on decode */ 276 CK_(cs_subject_decode(buf, CC_MAX_OBJECT_BYTES + 1, &S3, &why) == CC_E_LIMIT, "cap on decode"); 277 /* validate-side rules that the editor cannot produce */ 278 view(&V, 0x11, 0x22); 279 cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); 280 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); 281 S.in[0].state = CS_SUPERSEDED; /* superseded by nobody */ 282 CK_(cs_subject_validate(&S, &why) == CC_E_CORRUPT && 283 strcmp(why, "superseded intent not named by exactly one successor") == 0, 284 "orphan superseded: %s", why); 285 S.in[0].state = CS_ACTIVE; 286 S.n_intents = CS_MAX_INTENTS + 1; 287 CK_(cs_subject_validate(&S, &why) == CC_E_LIMIT, "intent bound"); 288 S.n_intents = 1; 289 S.kn[0].since = 1; 290 fill(S.kn[0].digest, 32, 0x10); 291 S.kn[1].since = 1; 292 fill(S.kn[1].digest, 32, 0x10); 293 S.n_knowledge = 2; 294 CK_(cs_subject_validate(&S, &why) == CC_E_CORRUPT && 295 strcmp(why, "knowledge not strictly ascending by digest") == 0, 296 "knowledge order: %s", why); 297 S.n_knowledge = 0; 298 CK_(cs_subject_encode(&S, buf, 10, &len, &why) == CC_E_ARG, "small output buffer"); 299 CK_(cs_subject_encode(NULL, buf, sizeof buf, &len, &why) == CC_E_ARG, "null"); 300 CK_(cs_subject_decode(buf, 100, NULL, &why) == CC_E_ARG, "null out"); 301 } 302 303 /* ---- 4. intents: supersession and retirement ---- */ 304 static void t_intents(void) 305 { 306 uint8_t a[32], b[32]; 307 uint64_t p[2] = {7, 1000}; 308 uint64_t p2[2] = {7, 500}; 309 size_t len = 0; 310 view(&V, 0x11, 0x22); 311 cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); 312 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, a, &why) == CC_OK, "intend a"); 313 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, a, &why) == CC_E_ARG, "same intent twice refused"); 314 /* a successor object replaces the goal in the same slot */ 315 fill(b, 32, 0x99); 316 cs_subject_advance(&S, b, PROV, CS_ORIGIN_OPERATOR); 317 CK_(S.sequence == 2 && memcmp(S.previous, b, 32) == 0, "advance"); 318 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p2, b, &why) == CC_OK, "intend b: %s", why); 319 CK_(S.n_intents == 2, "two intents"); 320 { 321 const struct cs_intent *act = cs_subject_active(&S, CS_INTENT_GOAL_LATENCY, 7); 322 CK_(act && memcmp(act->id, b, 32) == 0 && act->payload[1] == 500, "b is the active one"); 323 CK_(memcmp(act->supersedes, a, 32) == 0, "b names a"); 324 } 325 CK_(cs_subject_encode(&S, buf, sizeof buf, &len, &why) == CC_OK, "encode after supersede: %s", why); 326 CK_(cs_subject_decode(buf, len, &S2, &why) == CC_OK, "decode after supersede: %s", why); 327 /* two active in one slot is refused on decode (mutant ACCEPT_TWO_ACTIVE) */ 328 for (uint32_t i = 0; i < S2.n_intents; i++) 329 if (memcmp(S2.in[i].id, a, 32) == 0) S2.in[i].state = CS_ACTIVE; 330 for (uint32_t i = 0; i < S2.n_intents; i++) 331 if (memcmp(S2.in[i].id, b, 32) == 0) memset(S2.in[i].supersedes, 0, 32); 332 CK_(cs_subject_validate(&S2, &why) == CC_E_CORRUPT && strcmp(why, "two active intents in one slot") == 0, 333 "two active refused: %s", why); 334 /* retire */ 335 CK_(cs_subject_retire(&S, b, &why) == CC_OK, "retire b"); 336 CK_(cs_subject_active(&S, CS_INTENT_GOAL_LATENCY, 7) == NULL, "slot empty after retire"); 337 CK_(cs_subject_retire(&S, b, &why) == CC_E_ARG, "retire twice refused"); 338 CK_(cs_subject_retire(&S, a, &why) == CC_E_ARG, "retire a superseded intent refused"); 339 CK_(cs_subject_validate(&S, &why) == CC_OK, "still valid: %s", why); 340 /* supersedes a newer intent: swap since values by hand */ 341 { 342 uint32_t ia = 0, ib = 0; 343 for (uint32_t i = 0; i < S.n_intents; i++) { 344 if (memcmp(S.in[i].id, a, 32) == 0) ia = i; 345 if (memcmp(S.in[i].id, b, 32) == 0) ib = i; 346 } 347 S.in[ia].since = 2; 348 S.in[ib].since = 1; 349 /* ids no longer derive, but since-order is checked for b before its id? No: 350 * id derivation is checked per intent before supersession, so make the 351 * check order explicit by recomputing ids. */ 352 cs_intent_id(S.agent, S.in[ia].kind, S.in[ia].since, S.in[ia].payload, S.in[ia].id); 353 cs_intent_id(S.agent, S.in[ib].kind, S.in[ib].since, S.in[ib].payload, S.in[ib].id); 354 memcpy(S.in[ib].supersedes, S.in[ia].id, 32); 355 /* the table may now be out of order; sort by id (two elements) */ 356 if (memcmp(S.in[0].id, S.in[1].id, 32) > 0) { 357 struct cs_intent t = S.in[0]; 358 S.in[0] = S.in[1]; 359 S.in[1] = t; 360 } 361 CK_(cs_subject_validate(&S, &why) == CC_E_CORRUPT && strcmp(why, "intent supersedes a newer intent") == 0, 362 "supersedes newer refused: %s", why); 363 } 364 } 365 366 /* ---- 5. resolve over a fake source ---- */ 367 struct fake { 368 uint32_t n; 369 uint16_t kind[8], version[8]; 370 uint8_t *bytes[8]; 371 size_t len[8]; 372 }; 373 static uint32_t fk_count(void *c) { return ((struct fake *)c)->n; } 374 static int fk_entry(void *c, uint32_t i, uint16_t *k, uint16_t *v) 375 { 376 struct fake *f = c; 377 *k = f->kind[i]; 378 *v = f->version[i]; 379 return 0; 380 } 381 static int fk_read(void *c, uint32_t i, uint8_t *out, size_t cap, size_t *len) 382 { 383 struct fake *f = c; 384 *len = f->len[i]; 385 if (*len <= cap) memcpy(out, f->bytes[i], *len); 386 return 0; 387 } 388 static int fk_mount(void *c) 389 { 390 (void)c; 391 return CR_MOUNT_VALID; 392 } 393 static uint8_t obj[8][CC_MAX_OBJECT_BYTES]; 394 static size_t objlen[8]; 395 static uint8_t objid[8][32]; 396 397 static uint32_t fk_add(struct fake *f, uint16_t kind, const struct cs_subject *s) 398 { 399 uint32_t i = f->n++; 400 CK_(cs_subject_encode(s, obj[i], sizeof obj[i], &objlen[i], &why) == CC_OK, "fk encode: %s", why); 401 CK_(cs_subject_id(obj[i], objlen[i], objid[i]) == CC_OK, "fk id"); 402 f->kind[i] = kind; 403 f->version[i] = CC_STORE_OBJECT_VERSION; 404 f->bytes[i] = obj[i]; 405 f->len[i] = objlen[i]; 406 return i; 407 } 408 409 static void t_resolve(void) 410 { 411 struct fake f; 412 struct cr_source src = {&f, fk_count, fk_entry, fk_read, fk_mount, NULL}; 413 uint8_t id[32]; 414 uint64_t p[2] = {7, 1000}; 415 int rc; 416 view(&V, 0x11, 0x22); 417 memset(&f, 0, sizeof f); 418 /* absent */ 419 rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); 420 CK_(rc == CS_ABSENT, "no subject object: ABSENT (%d)", rc); 421 /* a foreign-kind object is ignored */ 422 cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); 423 fk_add(&f, 99, &S); 424 CK_(cs_resolve(&src, &W, &V, &S2, id, &why, NULL) == CS_ABSENT, "other kinds ignored"); 425 f.n = 0; 426 /* chain of two */ 427 fk_add(&f, CC_KIND_SUBJECT, &S); 428 cs_subject_advance(&S, objid[0], PROV, CS_ORIGIN_PROMOTED); 429 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); 430 fk_add(&f, CC_KIND_SUBJECT, &S); 431 rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); 432 CK_(rc == CS_RESOLVED, "chain of two resolves (%d: %s)", rc, why ? why : "-"); 433 CK_(S2.sequence == 2 && S2.n_intents == 1 && memcmp(id, objid[1], 32) == 0, "head is sequence 2"); 434 /* order independence: same objects, reversed */ 435 { 436 uint8_t *tb = f.bytes[0]; f.bytes[0] = f.bytes[1]; f.bytes[1] = tb; 437 size_t tl = f.len[0]; f.len[0] = f.len[1]; f.len[1] = tl; 438 rc = cs_resolve(&src, &W, &V, &S3, id, &why, NULL); 439 CK_(rc == CS_RESOLVED && memcmp(&S2, &S3, sizeof S2) == 0, "resolution does not depend on catalog order"); 440 tb = f.bytes[0]; f.bytes[0] = f.bytes[1]; f.bytes[1] = tb; 441 tl = f.len[0]; f.len[0] = f.len[1]; f.len[1] = tl; 442 } 443 /* fork: a second sequence-2 object with a different intent */ 444 { 445 uint64_t q[2] = {7, 999}; 446 cs_subject_decode(obj[0], objlen[0], &S3, &why); 447 cs_subject_advance(&S3, objid[0], PROV, CS_ORIGIN_PROMOTED); 448 CK_(cs_subject_intend(&S3, CS_INTENT_GOAL_LATENCY, q, NULL, &why) == CC_OK, "intend fork"); 449 fk_add(&f, CC_KIND_SUBJECT, &S3); 450 rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); 451 CK_(rc == CR_CORRUPT && strcmp(why, "subject chain fork") == 0, "fork refused (%d: %s)", rc, why); 452 f.n = 2; 453 } 454 /* gap: sequence 1 and 3 */ 455 { 456 cs_subject_decode(obj[1], objlen[1], &S3, &why); 457 cs_subject_advance(&S3, objid[1], PROV, CS_ORIGIN_PROMOTED); 458 fk_add(&f, CC_KIND_SUBJECT, &S3); 459 f.bytes[1] = f.bytes[2]; f.len[1] = f.len[2]; f.n = 2; 460 rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); 461 CK_(rc == CR_CORRUPT && strcmp(why, "subject chain gap") == 0, "gap refused (%d: %s)", rc, why); 462 f.bytes[1] = obj[1]; f.len[1] = objlen[1]; 463 } 464 /* broken link: sequence 2 names a previous that is not sequence 1's id */ 465 { 466 cs_subject_decode(obj[1], objlen[1], &S3, &why); 467 S3.previous[0] ^= 1; 468 uint32_t k = fk_add(&f, CC_KIND_SUBJECT, &S3); 469 f.bytes[1] = f.bytes[k]; f.len[1] = f.len[k]; f.n = 2; 470 rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); 471 CK_(rc == CR_CORRUPT && strcmp(why, "subject chain link broken") == 0, "broken link refused (%d: %s)", rc, why); 472 f.bytes[1] = obj[1]; f.len[1] = objlen[1]; 473 } 474 /* ALLEN-G1 identity binding: another agent's view, or a tampered agent 475 * id, must not resolve this subject */ 476 { 477 struct cr_view other; 478 view(&other, 0x11, 0x23); 479 rc = cs_resolve(&src, &W, &other, &S2, id, &why, NULL); 480 CK_(rc == CR_CORRUPT && strcmp(why, "subject object belongs to another agent") == 0, 481 "another agent id refused (%d: %s)", rc, why ? why : "-"); 482 view(&other, 0x12, 0x22); 483 rc = cs_resolve(&src, &W, &other, &S2, id, &why, NULL); 484 CK_(rc == CR_CORRUPT && strcmp(why, "subject object belongs to another agent") == 0, 485 "another root refused (%d: %s)", rc, why ? why : "-"); 486 rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); 487 CK_(rc == CS_RESOLVED, "the right view still resolves"); 488 } 489 /* undecodable object in the catalog: CORRUPT, never skipped */ 490 { 491 obj[1][0] ^= 1; 492 rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); 493 CK_(rc == CR_CORRUPT && strcmp(why, "bad magic") == 0, "undecodable refused (%d: %s)", rc, why); 494 obj[1][0] ^= 1; 495 } 496 /* unknown claim version */ 497 { 498 f.version[1] = 2; 499 rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); 500 CK_(rc == CR_CORRUPT, "unknown claim version refused"); 501 f.version[1] = CC_STORE_OBJECT_VERSION; 502 } 503 } 504 505 /* ---- 6. model independence (structural) ---- */ 506 static void t_model_independence(void) 507 { 508 size_t la = 0, lb = 0; 509 uint8_t ia[32], ib[32]; 510 uint64_t p[2] = {7, 1000}; 511 uint8_t model_a[32], model_b[32]; 512 fill(model_a, 32, 0xa1); 513 fill(model_b, 32, 0xb2); 514 view(&V, 0x11, 0x22); 515 cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); 516 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); 517 /* The "model" is the boot handoff's model_sha256 (native/boot/handoff.h), 518 * outside this object. There is nowhere to put it: encode takes no such 519 * input. Two runs that serve different models produce one subject. */ 520 (void)model_a; 521 (void)model_b; 522 CK_(cs_subject_encode(&S, buf, sizeof buf, &la, &why) == CC_OK, "encode under model a"); 523 CK_(cs_subject_id(buf, la, ia) == CC_OK, "id a"); 524 CK_(cs_subject_encode(&S, buf2, sizeof buf2, &lb, &why) == CC_OK, "encode under model b"); 525 CK_(cs_subject_id(buf2, lb, ib) == CC_OK, "id b"); 526 CK_(la == lb && memcmp(buf, buf2, la) == 0 && memcmp(ia, ib, 32) == 0, 527 "subject bytes and id do not depend on which model is served"); 528 /* and no byte of either model digest appears in the object */ 529 { 530 int hit = 0; 531 for (size_t i = 0; i + 4 <= la; i++) 532 if (memcmp(buf + i, model_a, 4) == 0 || memcmp(buf + i, model_b, 4) == 0) hit = 1; 533 CK_(!hit, "no model digest bytes inside the subject object"); 534 } 535 } 536 537 /* ---- 7. real sealed Store in a file ---- */ 538 #define A_UNITS 4u 539 #define S_UNITS 512u 540 static ss_workspace g_ws; 541 static ss_store g_s; 542 static disk_file g_f; 543 static disk_dev g_d; 544 static st_disk g_sd; 545 static st_dev g_sdev; 546 static ts_device g_tdev; 547 static ss_keys g_k; 548 static const uint32_t g_bs = 4096u; 549 static char g_path[256]; 550 static struct cr_work g_w; 551 static struct cr_txwork g_tw; 552 static struct cr_view g_v; 553 static struct cr_source g_src; 554 static struct cr_sink g_snk; 555 static struct cr_sealed_sink g_sk; 556 static const uint8_t RUUID[16] = {0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 557 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a}; 558 static int g_open; 559 560 static void dev_close(void) 561 { 562 if (g_open) disk_file_close(&g_f); 563 g_open = 0; 564 } 565 static int dev_open(int create) 566 { 567 dev_close(); 568 uint32_t bpu = 4096u / g_bs; 569 if (create) unlink(g_path); 570 memset(&g_f, 0, sizeof g_f); 571 if (disk_file_open(&g_f, &g_d, g_path, g_bs, (uint64_t)(A_UNITS + S_UNITS) * bpu, create) != 0) return -1; 572 g_open = 1; 573 if (st_disk_bind(&g_sd, &g_d, (uint64_t)A_UNITS * bpu, S_UNITS, &g_sdev) != 0) return -2; 574 ss_ts_device(&g_d, &g_tdev); 575 return 0; 576 } 577 static void make_keys(void) 578 { 579 uint8_t kvol[32]; 580 memset(kvol, 0x77, 32); 581 m5_subkeys sk; 582 m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); 583 memset(&g_k, 0, sizeof g_k); 584 g_k.identity_class = M5_ID_TEST; 585 g_k.key_generation = 1; 586 memcpy(g_k.k_root_auth, sk.k_root_auth, 32); 587 memcpy(g_k.k_domain, sk.k_artifact, 32); 588 m5_subkeys_wipe(&sk); 589 } 590 static void bind_store(void) 591 { 592 g_sk.s = &g_s; 593 g_sk.hook = NULL; 594 g_sk.hook_arg = NULL; 595 cr_bind_sealed(&g_src, &g_s); 596 cr_bind_sealed_sink(&g_snk, &g_sk); 597 } 598 /* mocked RNDR (as test_continuity_commit.c) */ 599 struct mock { 600 int present; 601 uint64_t ctr; 602 }; 603 static struct mock g_m; 604 static struct ck_rng_ops g_ops; 605 static struct ck_rng g_rng; 606 static int m_present(void *c) { return ((struct mock *)c)->present; } 607 static int m_read(void *c, uint64_t *out) 608 { 609 struct mock *m = c; 610 uint64_t z = (m->ctr += 0x9e3779b97f4a7c15ull); 611 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; 612 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; 613 *out = z ^ (z >> 31); 614 return 1; 615 } 616 static void rng_init(uint64_t seed) 617 { 618 g_m.present = 1; 619 g_m.ctr = seed; 620 g_ops.present = m_present; 621 g_ops.read = m_read; 622 g_ops.ctx = &g_m; 623 memset(&g_rng, 0, sizeof g_rng); 624 (void)ck_rng_probe(&g_rng, &g_ops); 625 } 626 627 static int store_fresh(void) 628 { 629 if (dev_open(1) != 0) return -1; 630 if (ss_format(&g_sdev, &g_tdev, 0, RUUID, &g_k, &g_ws) != 0) return -2; 631 if (ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) != 0) return -3; 632 bind_store(); 633 return 0; 634 } 635 static int store_reopen(void) 636 { 637 if (dev_open(0) != 0) return -1; 638 if (ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) != 0) return -3; 639 bind_store(); 640 return 0; 641 } 642 643 /* Provision the identity, then commit the genesis subject and one successor 644 * carrying a standing intent. Returns the head id. */ 645 static int write_subject(uint8_t head_id[32], uint64_t *agent_lo) 646 { 647 int rc = 0, store_rc = 0; 648 uint8_t gid[32]; 649 uint64_t p[2] = {7, 1000}; 650 rc = cr_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, &g_v, &why, &store_rc); 651 CK_(rc == CR_RESOLVED, "provision (%d: %s, store %d)", rc, why ? why : "-", store_rc); 652 if (rc != CR_RESOLVED) return -1; 653 CK_(cs_resolve(&g_src, &g_w, &g_v, &S2, gid, &why, &store_rc) == CS_ABSENT, "fresh store: subject ABSENT"); 654 cs_subject_genesis(&S, &g_v, PROV, CS_ORIGIN_OPERATOR); 655 rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S, gid, &why, &store_rc); 656 CK_(rc == CS_RESOLVED, "commit genesis (%d: %s, store %d)", rc, why ? why : "-", store_rc); 657 /* a stale successor (wrong previous) is refused and writes nothing */ 658 cs_subject_advance(&S, gid, PROV, CS_ORIGIN_OPERATOR); 659 S.previous[0] ^= 1; 660 rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S, head_id, &why, &store_rc); 661 CK_(rc == CR_CORRUPT && strcmp(why, "subject object does not continue the resolved chain") == 0, 662 "stale successor refused (%d: %s)", rc, why ? why : "-"); 663 S.previous[0] ^= 1; 664 CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); 665 rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S, head_id, &why, &store_rc); 666 CK_(rc == CS_RESOLVED, "commit successor (%d: %s, store %d)", rc, why ? why : "-", store_rc); 667 /* a second genesis is refused: the chain already exists */ 668 cs_subject_genesis(&S3, &g_v, PROV, CS_ORIGIN_OPERATOR); 669 rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S3, gid, &why, &store_rc); 670 CK_(rc == CR_CORRUPT, "second genesis refused (%d: %s)", rc, why ? why : "-"); 671 memcpy(agent_lo, g_v.root.agent_id, 8); 672 return 0; 673 } 674 675 static void check_restored(const uint8_t want_id[32]) 676 { 677 int rc, store_rc = 0, committed = 0; 678 uint8_t id[32]; 679 rc = cr_resume(&g_src, &g_snk, &g_w, &g_tw, &g_v, &committed, &why, &store_rc); 680 CK_(rc == CR_RESOLVED && committed == 1, "resume (%d: %s)", rc, why ? why : "-"); 681 rc = cs_resolve(&g_src, &g_w, &g_v, &S2, id, &why, &store_rc); 682 CK_(rc == CS_RESOLVED, "subject resolves after reopen (%d: %s, store %d)", rc, why ? why : "-", store_rc); 683 CK_(memcmp(id, want_id, 32) == 0, "same subject id after reopen"); 684 CK_(S2.sequence == 2 && S2.n_intents == 1, "sequence 2 with one intent"); 685 CK_(memcmp(S2.agent, g_v.root.agent_id, 32) == 0 && memcmp(S2.root, g_v.root_id, 32) == 0, 686 "subject bound to the provisioned identity"); 687 { 688 const struct cs_intent *a = cs_subject_active(&S2, CS_INTENT_GOAL_LATENCY, 7); 689 CK_(a && a->payload[1] == 1000 && a->since == 2, "the standing intent came back"); 690 } 691 } 692 693 static void t_sealed_store(void) 694 { 695 uint8_t head[32]; 696 uint64_t lo = 0; 697 snprintf(g_path, sizeof g_path, "/tmp/ck_cs_test_%d.img", (int)getpid()); 698 rng_init(0x1234); 699 CK_(store_fresh() == 0, "fresh store"); 700 if (write_subject(head, &lo) == 0) { 701 dev_close(); 702 CK_(store_reopen() == 0, "reopen"); 703 check_restored(head); 704 /* a successor committed against a stale view (older chain head) is refused */ 705 { 706 int rc, store_rc = 0; 707 uint8_t id[32]; 708 cs_subject_genesis(&S3, &g_v, PROV, CS_ORIGIN_OPERATOR); 709 S3.sequence = 3; 710 memcpy(S3.previous, head, 32); 711 S3.previous[1] ^= 1; 712 rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S3, id, &why, &store_rc); 713 CK_(rc == CR_CORRUPT, "wrong previous refused after reopen"); 714 } 715 } 716 dev_close(); 717 unlink(g_path); 718 } 719 720 /* ---- two-process restart (ALLEN-G2) ---- */ 721 static int phase_write(const char *img) 722 { 723 uint8_t head[32]; 724 uint64_t lo = 0; 725 char h[65]; 726 snprintf(g_path, sizeof g_path, "%s", img); 727 make_keys(); 728 rng_init(0x5eed); 729 CK_(store_fresh() == 0, "fresh store"); 730 if (write_subject(head, &lo) != 0) return ck_t_verdict("test_continuity_subject --write"); 731 dev_close(); 732 hex(head, 32, h); 733 printf("ALLEN subject id: %s\n", h); 734 printf("ALLEN agent: %016llx...\n", (unsigned long long)lo); 735 return ck_t_verdict("test_continuity_subject --write"); 736 } 737 738 static int phase_restore(const char *img, const char *idhex) 739 { 740 uint8_t want[32]; 741 snprintf(g_path, sizeof g_path, "%s", img); 742 make_keys(); 743 CK_(strlen(idhex) == 64 && unhex(idhex, want, 32) == 0, "id argument"); 744 CK_(store_reopen() == 0, "reopen in a fresh process"); 745 check_restored(want); 746 /* The pre-ALLEN continuity view has nowhere to keep an intent: its 747 * fields are root, manifest, branch table, Cortex count and memory 748 * digest (continuity_resolve.h struct cr_view). Structural half of G6. */ 749 CK_(sizeof(struct cr_view) == sizeof(g_v), "cr_view inspected"); 750 printf("ALLEN restored: sequence=%llu intents=%u regime=%llu target_ns=%llu\n", 751 (unsigned long long)S2.sequence, S2.n_intents, 752 (unsigned long long)(S2.n_intents ? S2.in[0].payload[0] : 0), 753 (unsigned long long)(S2.n_intents ? S2.in[0].payload[1] : 0)); 754 dev_close(); 755 return ck_t_verdict("test_continuity_subject --restore"); 756 } 757 758 int main(int argc, char **argv) 759 { 760 if (argc == 3 && strcmp(argv[1], "--write") == 0) return phase_write(argv[2]); 761 if (argc == 4 && strcmp(argv[1], "--restore") == 0) return phase_restore(argv[2], argv[3]); 762 make_keys(); 763 t_known_answer(); 764 t_round_trip(); 765 t_refusals(); 766 t_intents(); 767 t_resolve(); 768 t_model_independence(); 769 t_sealed_store(); 770 return ck_t_verdict("test_continuity_subject"); 771 }