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/effect/test_effect_cap64.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 / effect / test_effect_cap64.c` 704 lines ยท 35459 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 /* 18 * test_effect_cap64.c -- hostile tests for the 64-bit effect capability 19 * generation migration (spec/effect-cap64-migration.md). 20 * 21 * Gates (each prints "GATE PASS|FAIL /"): 22 * OMEGA_EFFECT_CAP64_ROUNDTRIP_PASS full generation survives build, canonical 23 * encoding, OMGG serialize/decode, validate, 24 * Visor inspection (struct, text, JSON) 25 * OMEGA_EFFECT_CAP64_IDENTITY_PASS the SemanticId binds all 64 generation bits 26 * OMEGA_EFFECT_CAP64_STALE_REJECT_PASS old / truncated / malformed bytes refused 27 * with the specific reason 28 * OMEGA_EFFECT_CAP64_AUTHORITY_PASS the real Aienux authority (pinned lib, 29 * aienos.lock) accepts honest references 30 * carried through effect objects and refuses 31 * truncated, stale, wrong-slot, wrong-right 32 * and pre-restart ones 33 * 34 * The authority side uses the pinned Aienux header and library directly 35 * (aienos_capability.h / libaienos_capability.a); the Omega side uses only 36 * the physics-free core and the Visor effect-request adapter. 37 */ 38 #include "aienos_capability.h" 39 40 #include "omega_canonical.h" 41 #include "omega_codec.h" 42 #include "omega_core.h" 43 #include "omega_validate.h" 44 #include "sha256.h" 45 #include "visor_effect_request.h" 46 47 #include 48 #include 49 #include 50 #include 51 52 enum { G_ROUNDTRIP, G_IDENTITY, G_STALE, G_AUTHORITY, G_COUNT }; 53 static const char *const GATE_NAME[G_COUNT] = { 54 "OMEGA_EFFECT_CAP64_ROUNDTRIP_PASS", "OMEGA_EFFECT_CAP64_IDENTITY_PASS", 55 "OMEGA_EFFECT_CAP64_STALE_REJECT_PASS", "OMEGA_EFFECT_CAP64_AUTHORITY_PASS", 56 }; 57 static int g_pass[G_COUNT], g_total[G_COUNT]; 58 59 #define CHECK(gate, cond, ...) \ 60 do { \ 61 g_total[gate]++; \ 62 if (cond) g_pass[gate]++; \ 63 else { \ 64 fprintf(stderr, "FAIL [%s] %s:%d ", GATE_NAME[gate], __FILE__, __LINE__); \ 65 fprintf(stderr, __VA_ARGS__); \ 66 fputc('\n', stderr); \ 67 } \ 68 } while (0) 69 70 #define U32MAX 0xFFFFFFFFull 71 72 /* ---- helpers ------------------------------------------------------------ */ 73 74 static OmegaObject *effect(OmegaGraph *g, uint16_t cls, uint16_t op, uint32_t slot, uint64_t gen, 75 const char *params) { 76 OmegaObject *o = omega_build_effect(g, cls, op, slot, gen); 77 if (!o) return NULL; 78 if (params) { 79 EffectPayload e; 80 if (omega_effect_read(o, &e) != OMEGA_EFFECT_OK) return NULL; 81 e.param_len = (uint16_t)strlen(params); 82 memcpy(e.param_bytes, params, e.param_len); 83 omega_effect_write(o, &e); 84 omega_compute_semantic_id(o); 85 } 86 return o; 87 } 88 89 static int canon(const OmegaObject *o, uint8_t *buf, size_t cap, size_t *len) { 90 return omega_canonical_encode(o, buf, cap, len); 91 } 92 93 /* Offset of the kind-specific payload inside a canonical encoding of an 94 * object with no attributes, relations or constraints. */ 95 #define BARE_PAYLOAD_OFF (6 + 2 + 2 + 2 + 4) 96 97 /* One-object OMGG stream from raw canonical bytes. */ 98 static size_t omgg1(const uint8_t *c, size_t clen, uint8_t *out) { 99 out[0] = 'O'; out[1] = 'M'; out[2] = 'G'; out[3] = 'G'; 100 out[4] = 0; out[5] = 1; 101 out[6] = (uint8_t)(clen >> 24); out[7] = (uint8_t)(clen >> 16); 102 out[8] = (uint8_t)(clen >> 8); out[9] = (uint8_t)clen; 103 memcpy(out + 10, c, clen); 104 return 10 + clen; 105 } 106 107 /* Append one more object to an OMGG stream built by omgg1/omggn. */ 108 static size_t omgg_append(uint8_t *s, size_t slen, const uint8_t *c, size_t clen) { 109 uint16_t n = (uint16_t)((s[4] << 8) | s[5]); 110 n++; 111 s[4] = (uint8_t)(n >> 8); s[5] = (uint8_t)n; 112 s[slen] = (uint8_t)(clen >> 24); s[slen + 1] = (uint8_t)(clen >> 16); 113 s[slen + 2] = (uint8_t)(clen >> 8); s[slen + 3] = (uint8_t)clen; 114 memcpy(s + slen + 4, c, clen); 115 return slen + 4 + clen; 116 } 117 118 static void put_be64(uint8_t *p, uint64_t v) { 119 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (56 - 8 * i)); 120 } 121 122 static int hex_eq(const SemanticId *id, const char *hex) { 123 char h[65]; 124 omega_hex_semantic_id(id, h); 125 return strcmp(h, hex) == 0; 126 } 127 128 /* The old (v1, origin/main `193a7e7`) canonical encoding of an effect with no 129 * params and zero capability_ref: header version 0x01, 176-byte little-endian 130 * host-struct payload with a 32-bit generation. Built by hand from the old 131 * layout so the test does not depend on the old code. */ 132 static size_t v1_effect_bytes(uint16_t cls, uint16_t op, uint32_t slot, uint32_t gen32, uint8_t *out) { 133 size_t p = 0; 134 out[p++] = 'O'; out[p++] = 'M'; out[p++] = 'G'; out[p++] = '0'; 135 out[p++] = 0x01; out[p++] = KIND_EFFECT; 136 for (int i = 0; i < 6; i++) out[p++] = 0; /* attr, rel, const counts */ 137 out[p++] = 0; out[p++] = 0; out[p++] = 0; out[p++] = 176; 138 uint8_t s[176]; 139 memset(s, 0, sizeof s); 140 s[0] = (uint8_t)cls; s[1] = (uint8_t)(cls >> 8); 141 s[2] = (uint8_t)op; s[3] = (uint8_t)(op >> 8); 142 for (int i = 0; i < 4; i++) s[4 + i] = (uint8_t)(slot >> (8 * i)); 143 for (int i = 0; i < 4; i++) s[8 + i] = (uint8_t)(gen32 >> (8 * i)); 144 memcpy(out + p, s, sizeof s); 145 return p + sizeof s; 146 } 147 148 static const uint64_t GENS[] = { 149 0ull, 1ull, U32MAX, U32MAX + 1ull, 0x0123456789ABCDEFull, 1ull << 63, 150 0xFFFFFFFFFFFFFFFEull, UINT64_MAX, 151 }; 152 #define NGENS (sizeof GENS / sizeof GENS[0]) 153 154 /* ---- ROUNDTRIP -------------------------------------------------------------- */ 155 156 static void roundtrip_one(uint64_t gen, uint32_t slot, const char *params) { 157 OmegaGraph *g = omega_graph_create(); 158 OmegaGraph *d = calloc(1, sizeof *d); 159 static uint8_t buf[1 << 20]; 160 OmegaObject *o = effect(g, 7, 2, slot, gen, params); 161 CHECK(G_ROUNDTRIP, o && o->payload_len == OMEGA_EFFECT_PAYLOAD_LEN, "build gen=%" PRIu64, gen); 162 if (!o) goto out; 163 SemanticId id = o->id; 164 165 /* canonical header version and the big-endian generation bytes */ 166 uint8_t c[4096]; 167 size_t clen = 0; 168 CHECK(G_ROUNDTRIP, canon(o, c, sizeof c, &clen) == 0 && c[4] == OMEGA_EFFECT_VERSION && 169 c[5] == KIND_EFFECT, "canonical header v2 gen=%" PRIu64, gen); 170 uint8_t be[8]; 171 put_be64(be, gen); 172 CHECK(G_ROUNDTRIP, clen == BARE_PAYLOAD_OFF + OMEGA_EFFECT_PAYLOAD_LEN && 173 memcmp(c + BARE_PAYLOAD_OFF + 8, be, 8) == 0, 174 "wire carries all 8 generation bytes big-endian gen=%" PRIu64, gen); 175 176 /* in-memory decode */ 177 EffectPayload e; 178 CHECK(G_ROUNDTRIP, omega_effect_read(o, &e) == OMEGA_EFFECT_OK && e.capability_generation == gen && 179 e.capability_slot == slot && e.resource_class == 7 && e.operation_code == 2, 180 "decode gen=%" PRIu64, gen); 181 182 /* OMGG graph serialize -> deserialize */ 183 size_t n = 0; 184 CHECK(G_ROUNDTRIP, omega_graph_serialize_binary(g, buf, sizeof buf, &n) == 0, "serialize"); 185 CHECK(G_ROUNDTRIP, omega_graph_deserialize_binary(buf, n, d) == 0 && d->object_count == 1, 186 "deserialize gen=%" PRIu64, gen); 187 const OmegaObject *o2 = omega_graph_find_object_const(d, &id); 188 EffectPayload e2; 189 CHECK(G_ROUNDTRIP, o2 && omega_effect_read(o2, &e2) == OMEGA_EFFECT_OK && 190 e2.capability_generation == gen && e2.capability_slot == slot && 191 memcmp(&e2, &e, sizeof e) == 0, "decoded object equal gen=%" PRIu64, gen); 192 char err[200]; 193 CHECK(G_ROUNDTRIP, omega_validate_graph(d, err, sizeof err) == 0, "validate: %s", err); 194 195 /* Visor inspection */ 196 VisorEffectRequest r; 197 CHECK(G_ROUNDTRIP, visor_effect_request_build(d, &id, &r) == 0 && r.capability_generation == gen && 198 r.capability_slot == slot && !r.authorized, "visor request gen=%" PRIu64, gen); 199 char text[2048], json[2048], want[96]; 200 CHECK(G_ROUNDTRIP, visor_effect_request_format_text(&r, text, sizeof text) == 0, "text"); 201 snprintf(want, sizeof want, "generation %" PRIu64 " ref", gen); 202 CHECK(G_ROUNDTRIP, strstr(text, want) != NULL, "text prints exact generation %s", want); 203 CHECK(G_ROUNDTRIP, visor_effect_request_format_json(&r, json, sizeof json) == 0, "json"); 204 snprintf(want, sizeof want, "\"capability_generation\":%" PRIu64 ",", gen); 205 CHECK(G_ROUNDTRIP, strstr(json, want) != NULL, "json prints exact generation %s", want); 206 out: 207 free(d); 208 omega_graph_destroy(g); 209 } 210 211 static void test_roundtrip(void) { 212 printf("-- roundtrip\n"); 213 for (size_t i = 0; i < NGENS; i++) roundtrip_one(GENS[i], 3, NULL); 214 roundtrip_one(U32MAX + 1ull, UINT32_MAX, "set=42"); 215 roundtrip_one(1ull << 63, 255, "`0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef`" 216 "`0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef`"); 217 218 /* Exact v2 layout of one fixture (spec/effect-cap64-migration.md 2.2). */ 219 OmegaGraph *g = omega_graph_create(); 220 OmegaObject *o = effect(g, 0x0102, 0x0304, 0x05060708u, 0x090A0B0C0D0E0F10ull, "ab"); 221 uint8_t want[OMEGA_EFFECT_PAYLOAD_LEN]; 222 memset(want, 0, sizeof want); 223 const uint8_t head[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 224 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 }; 225 memcpy(want, head, sizeof head); 226 want[0x30] = 0x00; want[0x31] = 0x02; want[0x32] = 'a'; want[0x33] = 'b'; 227 CHECK(G_ROUNDTRIP, o && o->payload_len == sizeof want && memcmp(o->payload, want, sizeof want) == 0, 228 "fixture payload bytes match the spec layout exactly"); 229 omega_graph_destroy(g); 230 } 231 232 /* ---- IDENTITY --------------------------------------------------------------- */ 233 234 /* New ids (spec/effect-cap64-migration.md section 3.1) and the old ids they replace. */ 235 #define OLD_ID_OMEGATOOL "`ff7aa9701f39e226ac7a8babaafd74c052b169e986f03bac8555d4d6bf4fc46e`" 236 #define OLD_ID_VERIFY "`94d5b66158ff57354be51e44b3867b810c7a2afde8749a95722877ed01195bfb`" 237 #define NEW_ID_OMEGATOOL "`13f2c858be4dd0dcde1f458cf6e87d468a4d2bac2f8355254c32a977bc4692eb`" 238 #define NEW_ID_VERIFY "`d3873a85a31eaa1b33018edebbf7e6dfce8a36a49703217505555a884fde7988`" 239 240 static SemanticId id_of(uint64_t gen) { 241 OmegaGraph *g = omega_graph_create(); 242 OmegaObject *o = effect(g, 1, 2, 9, gen, "p"); 243 SemanticId id = o->id; 244 omega_graph_destroy(g); 245 return id; 246 } 247 248 static void test_identity(void) { 249 printf("-- identity\n"); 250 for (size_t i = 0; i < NGENS; i++) { 251 uint64_t x = GENS[i]; 252 SemanticId a = id_of(x), b = id_of(x); 253 CHECK(G_IDENTITY, omega_compare_semantic_id(&a, &b) == 0, "deterministic gen=%" PRIu64, x); 254 SemanticId hi = id_of(x ^ (1ull << 32)), top = id_of(x ^ (1ull << 63)), lo = id_of(x ^ 1ull); 255 CHECK(G_IDENTITY, omega_compare_semantic_id(&a, &hi) != 0, "bit 32 changes the id"); 256 CHECK(G_IDENTITY, omega_compare_semantic_id(&a, &top) != 0, "bit 63 changes the id"); 257 CHECK(G_IDENTITY, omega_compare_semantic_id(&a, &lo) != 0, "bit 0 changes the id"); 258 if (x > U32MAX) { 259 SemanticId cut = id_of(x & U32MAX); 260 CHECK(G_IDENTITY, omega_compare_semantic_id(&a, &cut) != 0, 261 "truncated generation has a different id gen=%" PRIu64, x); 262 } 263 } 264 SemanticId m = id_of(U32MAX), m1 = id_of(U32MAX + 1ull), z = id_of(0); 265 CHECK(G_IDENTITY, omega_compare_semantic_id(&m, &m1) != 0 && omega_compare_semantic_id(&m1, &z) != 0, 266 "UINT32_MAX, UINT32_MAX+1 and 0 are three identities"); 267 268 /* id == sha256(canonical encoding) */ 269 OmegaGraph *g = omega_graph_create(); 270 OmegaObject *o = effect(g, 1, 2, 9, 1ull << 63, "set=42"); 271 uint8_t c[4096], h[32]; 272 size_t clen = 0; 273 canon(o, c, sizeof c, &clen); 274 sha256_hash(c, clen, h); 275 CHECK(G_IDENTITY, memcmp(h, o->id.bytes, 32) == 0, "id is sha256 of the canonical bytes"); 276 277 /* Altering the generation after the id was computed. */ 278 SemanticId named = o->id; 279 EffectPayload e; 280 omega_effect_read(o, &e); 281 const uint64_t variants[] = { e.capability_generation ^ (1ull << 40), e.capability_generation & U32MAX, 282 e.capability_generation + 1, e.capability_generation - 1 }; 283 for (size_t i = 0; i < sizeof variants / sizeof variants[0]; i++) { 284 EffectPayload t = e; 285 t.capability_generation = variants[i]; 286 omega_effect_write(o, &t); 287 VisorEffectRequest r; 288 CHECK(G_IDENTITY, visor_effect_request_build(g, &named, &r) == -1 && !r.authorized, 289 "edited generation without re-id refused (variant %zu)", i); 290 omega_compute_semantic_id(o); 291 CHECK(G_IDENTITY, omega_compare_semantic_id(&o->id, &named) != 0, "re-id gives a new id (variant %zu)", i); 292 SemanticId nid = o->id; 293 CHECK(G_IDENTITY, visor_effect_request_build(g, &nid, &r) == 0 && 294 r.capability_generation == variants[i] && memcmp(r.request_digest, named.bytes, 32) != 0, 295 "re-identified object is a different request (variant %zu)", i); 296 omega_effect_write(o, &e); 297 omega_compute_semantic_id(o); 298 CHECK(G_IDENTITY, omega_compare_semantic_id(&o->id, &named) == 0, "restore gives the old id back"); 299 } 300 omega_graph_destroy(g); 301 302 /* Golden ids of the two in-tree effects (old -> new, spec section 3). */ 303 g = omega_graph_create(); 304 OmegaObject *t1 = omega_build_effect(g, 1, 1, 0, 1); 305 OmegaObject *t2 = omega_build_effect(g, 1, 1, 3, 1); 306 char hx[65]; 307 omega_hex_semantic_id(&t1->id, hx); 308 printf(" id (1,1,0,1) = %s\n", hx); 309 omega_hex_semantic_id(&t2->id, hx); 310 printf(" id (1,1,3,1) = %s\n", hx); 311 CHECK(G_IDENTITY, hex_eq(&t1->id, NEW_ID_OMEGATOOL), "golden new id (1,1,0,1)"); 312 CHECK(G_IDENTITY, hex_eq(&t2->id, NEW_ID_VERIFY), "golden new id (1,1,3,1)"); 313 CHECK(G_IDENTITY, !hex_eq(&t1->id, OLD_ID_OMEGATOOL) && !hex_eq(&t2->id, OLD_ID_VERIFY), 314 "new ids differ from the old ids"); 315 /* The old ids are exactly the hash of the hand-built v1 bytes (so the 316 * refusal tests below refuse the real old format, not a guess). */ 317 uint8_t v1[256]; 318 SemanticId old; 319 size_t v1n = v1_effect_bytes(1, 1, 0, 1, v1); 320 sha256_hash(v1, v1n, old.bytes); 321 CHECK(G_IDENTITY, hex_eq(&old, OLD_ID_OMEGATOOL), "hand-built v1 bytes hash to the old id (1,1,0,1)"); 322 v1n = v1_effect_bytes(1, 1, 3, 1, v1); 323 sha256_hash(v1, v1n, old.bytes); 324 CHECK(G_IDENTITY, hex_eq(&old, OLD_ID_VERIFY), "hand-built v1 bytes hash to the old id (1,1,3,1)"); 325 omega_graph_destroy(g); 326 } 327 328 /* ---- STALE_REJECT (hand-crafted bytes) ------------------------------------ */ 329 330 static int decode_stream(const uint8_t *s, size_t n) { 331 OmegaGraph *d = calloc(1, sizeof *d); 332 int rc = omega_graph_deserialize_binary(s, n, d); 333 free(d); 334 return rc; 335 } 336 337 static void test_stale_reject(void) { 338 printf("-- stale / truncated / legacy bytes\n"); 339 static uint8_t s[16384]; 340 uint8_t c[4096], v1[256]; 341 size_t clen = 0, n; 342 343 /* header version rule */ 344 CHECK(G_STALE, omega_canonical_check_header(0x01, KIND_EFFECT) == OMEGA_CANON_ERR_EFFECT_V1, "v1 effect header"); 345 CHECK(G_STALE, omega_canonical_check_header(0x02, KIND_EFFECT) == OMEGA_CANON_OK, "v2 effect header"); 346 CHECK(G_STALE, omega_canonical_check_header(0x03, KIND_EFFECT) == OMEGA_CANON_ERR_VERSION, "v3 effect header"); 347 CHECK(G_STALE, omega_canonical_check_header(0x00, KIND_EFFECT) == OMEGA_CANON_ERR_VERSION, "v0 effect header"); 348 CHECK(G_STALE, omega_canonical_check_header(0x01, KIND_TYPE) == OMEGA_CANON_OK, "v1 type header (control)"); 349 CHECK(G_STALE, omega_canonical_check_header(0x02, KIND_TYPE) == OMEGA_CANON_ERR_VERSION, "v2 type header"); 350 CHECK(G_STALE, omega_canonical_check_header(0x02, KIND_VALUE) == OMEGA_CANON_ERR_VERSION, "v2 value header"); 351 352 /* A real old (v1) effect, carrying a generation that was truncated on the way in. */ 353 const uint64_t live = (1ull << 40) | 0x1234u; 354 n = omgg1(v1, v1_effect_bytes(1, 2, 3, (uint32_t)(live & U32MAX), v1), s); 355 CHECK(G_STALE, decode_stream(s, n) == -1, "old v1 effect with a truncated generation: refused"); 356 n = omgg1(v1, v1_effect_bytes(1, 1, 0, 1, v1), s); 357 CHECK(G_STALE, decode_stream(s, n) == -1, "old v1 effect (omegatool fixture): refused"); 358 359 /* control: an unchanged v1 non-effect object still decodes; mixed stream is refused whole */ 360 OmegaGraph *g = omega_graph_create(); 361 OmegaObject *t = omega_build_type_uint(g, 64); 362 canon(t, c, sizeof c, &clen); 363 CHECK(G_STALE, c[4] == 0x01, "non-effect objects keep version 0x01"); 364 n = omgg1(c, clen, s); 365 CHECK(G_STALE, decode_stream(s, n) == 0, "control: v1 type object decodes"); 366 size_t v1n = v1_effect_bytes(1, 1, 0, 1, v1); 367 n = omgg_append(s, n, v1, v1n); 368 CHECK(G_STALE, decode_stream(s, n) == -1, "stream with one v1 effect is refused as a whole"); 369 370 /* v2 header written in front of an unchanged non-effect object */ 371 c[4] = 0x02; 372 n = omgg1(c, clen, s); 373 CHECK(G_STALE, decode_stream(s, n) == -1, "v2 header on a type object: refused (no second identity)"); 374 375 /* v2 header in front of the old 176-byte struct payload */ 376 v1[4] = 0x02; 377 n = omgg1(v1, v1n, s); 378 CHECK(G_STALE, decode_stream(s, n) == -1, "v2 header + old 176-byte payload: refused"); 379 CHECK(G_STALE, omega_effect_payload_decode(v1 + BARE_PAYLOAD_OFF, 176, &(EffectPayload){0}) == 380 OMEGA_EFFECT_ERR_LEGACY_V1, "176-byte payload reported as legacy v1"); 381 382 /* v2 bytes with the high 32 bits of the generation cut out (174-byte payload) */ 383 OmegaObject *e = effect(g, 1, 2, 3, live, "x"); 384 canon(e, c, sizeof c, &clen); 385 { 386 uint8_t cut[4096]; 387 size_t po = BARE_PAYLOAD_OFF, k = 0; 388 memcpy(cut, c, po + 8); /* header + first 8 payload bytes */ 389 k = po + 8; 390 memcpy(cut + k, c + po + 12, clen - (po + 12)); /* drop generation bytes 0..3 (high word) */ 391 k += clen - (po + 12); 392 uint32_t pl = OMEGA_EFFECT_PAYLOAD_LEN - 4; 393 cut[po - 4] = (uint8_t)(pl >> 24); cut[po - 3] = (uint8_t)(pl >> 16); 394 cut[po - 2] = (uint8_t)(pl >> 8); cut[po - 1] = (uint8_t)pl; 395 n = omgg1(cut, k, s); 396 CHECK(G_STALE, decode_stream(s, n) == -1, "generation with the high 32 bits cut out (174 bytes): refused"); 397 CHECK(G_STALE, omega_effect_payload_decode(cut + po, pl, &(EffectPayload){0}) == OMEGA_EFFECT_ERR_LENGTH, 398 "174-byte payload: LENGTH"); 399 } 400 /* stream cut in the middle of the generation field */ 401 n = omgg1(c, clen, s); 402 CHECK(G_STALE, decode_stream(s, n) == 0, "control: intact v2 effect decodes"); 403 CHECK(G_STALE, decode_stream(s, 10 + BARE_PAYLOAD_OFF + 12) == -1, "stream truncated inside the generation: refused"); 404 CHECK(G_STALE, decode_stream(s, n - 1) == -1, "stream missing its last byte: refused"); 405 406 /* malformed v2 payloads, in memory and on the wire */ 407 struct { const char *what; size_t off; uint8_t val; int want; } bad[] = { 408 { "resource_class 0", 0x00, 0x00, OMEGA_EFFECT_ERR_RESOURCE }, 409 { "param_len 129", 0x30, 0x00, OMEGA_EFFECT_ERR_PARAM_LEN }, 410 { "non-zero padding after params", 0x32 + 5, 0x7F, OMEGA_EFFECT_ERR_PADDING }, 411 { "non-zero last padding byte", OMEGA_EFFECT_PAYLOAD_LEN - 1, 0x01, OMEGA_EFFECT_ERR_PADDING }, 412 }; 413 for (size_t i = 0; i < sizeof bad / sizeof bad[0]; i++) { 414 uint8_t p[OMEGA_EFFECT_PAYLOAD_LEN]; 415 memcpy(p, e->payload, sizeof p); 416 if (bad[i].off == 0x00) { p[0] = 0; p[1] = 0; } 417 else if (bad[i].off == 0x30) { p[0x30] = 0; p[0x31] = 129; } 418 else p[bad[i].off] = bad[i].val; 419 EffectPayload out; 420 CHECK(G_STALE, omega_effect_payload_decode(p, sizeof p, &out) == bad[i].want, "decode: %s", bad[i].what); 421 uint8_t cc[4096]; 422 memcpy(cc, c, clen); 423 memcpy(cc + BARE_PAYLOAD_OFF, p, sizeof p); 424 n = omgg1(cc, clen, s); 425 CHECK(G_STALE, decode_stream(s, n) == -1, "wire: %s refused", bad[i].what); 426 OmegaObject *m = omega_graph_add_object(g, KIND_EFFECT); 427 memcpy(m->payload, p, sizeof p); 428 m->payload_len = sizeof p; 429 omega_compute_semantic_id(m); 430 char err[200]; 431 SemanticId mid = m->id; 432 VisorEffectRequest r; 433 CHECK(G_STALE, omega_validate_object(g, m, err, sizeof err) != 0, "validate: %s refused", bad[i].what); 434 CHECK(G_STALE, visor_effect_request_build(g, &mid, &r) == -1 && !r.authorized, "visor: %s refused", bad[i].what); 435 } 436 /* wrong lengths, and a raw memcpy of the (new) in-memory struct */ 437 const size_t lens[] = { 0, 8, 175, 176, 177, 179, 184, 256 }; 438 for (size_t i = 0; i < sizeof lens / sizeof lens[0]; i++) { 439 OmegaObject *m = omega_graph_add_object(g, KIND_EFFECT); 440 memset(m->payload, 0, lens[i] ? lens[i] : 1); 441 memcpy(m->payload, e->payload, lens[i] < OMEGA_EFFECT_PAYLOAD_LEN ? lens[i] : OMEGA_EFFECT_PAYLOAD_LEN); 442 m->payload_len = (uint32_t)lens[i]; 443 omega_compute_semantic_id(m); 444 EffectPayload out; 445 int want = lens[i] == 176 ? OMEGA_EFFECT_ERR_LEGACY_V1 : OMEGA_EFFECT_ERR_LENGTH; 446 char err[200]; 447 CHECK(G_STALE, omega_effect_read(m, &out) == want && omega_validate_object(g, m, err, sizeof err) != 0, 448 "payload length %zu refused", lens[i]); 449 } 450 { 451 EffectPayload host; 452 omega_effect_read(e, &host); 453 OmegaObject *m = omega_graph_add_object(g, KIND_EFFECT); 454 memcpy(m->payload, &host, sizeof host); /* host-struct image, not the wire layout */ 455 m->payload_len = sizeof host; 456 omega_compute_semantic_id(m); 457 EffectPayload out; 458 CHECK(G_STALE, sizeof host != OMEGA_EFFECT_PAYLOAD_LEN && omega_effect_read(m, &out) != OMEGA_EFFECT_OK, 459 "raw host-struct image (%zu bytes) refused", sizeof host); 460 } 461 omega_graph_destroy(g); 462 } 463 464 /* ---- AUTHORITY (real Aienux, pinned lib) ----------------------------------- */ 465 466 #define SUBJ 7u 467 #define OTHER 9u 468 #define RES 0x7101ull 469 470 typedef struct { 471 AienosCapAdmin *admin; 472 AienosCapView *view; 473 } Auth; 474 475 static AienosCapRef office(Auth *a) { 476 AienosCapRef o = { AIENOS_CAP_PARENT_NONE, 0 }; 477 aienos_cap_office(a->admin, &o); 478 return o; 479 } 480 481 /* Put the next free slot on generation `gen` (0 = leave it) and mint there. */ 482 static int mint_at(Auth *a, uint32_t next_free, uint64_t gen, uint32_t subject, uint32_t rights, 483 AienosCapRef *out) { 484 if (gen) { 485 int rc = aienos_cap_force_generation(a->admin, next_free, gen); 486 if (rc != AIENOS_CAP_OK) return rc; 487 } 488 AienosCapMint m; 489 memset(&m, 0, sizeof m); 490 m.issuer = 3; 491 m.subject = subject; 492 m.resource = RES; 493 m.rights = rights; 494 m.parent = (AienosCapRef){ AIENOS_CAP_PARENT_NONE, 0 }; 495 m.authority = office(a); 496 return aienos_cap_mint(a->admin, &m, out); 497 } 498 499 /* Carry a reference through an effect object: build, serialize, decode, 500 * Visor request; return the reference the authority is shown. */ 501 static int carry(AienosCapRef ref, uint16_t op, uint8_t *stream, size_t *stream_len, AienosCapRef *shown) { 502 OmegaGraph *g = omega_graph_create(); 503 OmegaGraph *d = calloc(1, sizeof *d); 504 int ok = -1; 505 OmegaObject *o = effect(g, 1, op, ref.cap_id, ref.generation, "set=1"); 506 size_t n = 0; 507 if (o && omega_graph_serialize_binary(g, stream, 8192, &n) == 0 && 508 omega_graph_deserialize_binary(stream, n, d) == 0) { 509 VisorEffectRequest r; 510 if (visor_effect_request_build(d, &o->id, &r) == 0) { 511 shown->cap_id = r.capability_slot; 512 shown->generation = r.capability_generation; 513 ok = 0; 514 } 515 } 516 if (stream_len) *stream_len = n; 517 free(d); 518 omega_graph_destroy(g); 519 return ok; 520 } 521 522 /* Re-read a stored stream (e.g. saved before an authority restart). */ 523 static int reread(const uint8_t *stream, size_t n, AienosCapRef *shown) { 524 OmegaGraph *d = calloc(1, sizeof *d); 525 int ok = -1; 526 if (omega_graph_deserialize_binary(stream, n, d) == 0 && d->object_count == 1) { 527 VisorEffectRequest r; 528 if (visor_effect_request_build(d, &d->objects[0].id, &r) == 0) { 529 shown->cap_id = r.capability_slot; 530 shown->generation = r.capability_generation; 531 ok = 0; 532 } 533 } 534 free(d); 535 return ok; 536 } 537 538 static int val(Auth *a, AienosCapRef r, uint32_t subject, uint64_t res, uint32_t rights) { 539 return aienos_cap_validate(a->view, r, subject, res, rights, NULL); 540 } 541 542 static void test_authority(void) { 543 printf("-- authority (AIENOS native capability library)\n"); 544 Auth a = { 0 }; 545 int rc = aienos_cap_start(&a.admin, &a.view); 546 CHECK(G_AUTHORITY, rc == AIENOS_CAP_OK, "aienos_cap_start %d", rc); 547 if (rc != AIENOS_CAP_OK) return; 548 const uint32_t RW = AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE; 549 static uint8_t st[8][8192]; 550 size_t stn[8]; 551 AienosCapRef shown; 552 553 /* 1. natural generation (boot-time seeded by aienos#158) */ 554 AienosCapRef nat; 555 CHECK(G_AUTHORITY, mint_at(&a, 1, 0, SUBJ, RW, &nat) == AIENOS_CAP_OK && nat.cap_id == 1, "natural mint"); 556 printf(" natural generation %" PRIu64 " (%s 2^32)\n", nat.generation, 557 nat.generation > U32MAX ? "above" : "not above"); 558 CHECK(G_AUTHORITY, carry(nat, 2, st[0], &stn[0], &shown) == 0 && shown.generation == nat.generation && 559 shown.cap_id == nat.cap_id, "natural reference carried intact"); 560 CHECK(G_AUTHORITY, val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_OK, 561 "natural reference validates after the round trip"); 562 563 /* 2. forced generations at the 32-bit boundary and at bit 63 */ 564 const uint64_t forced[] = { U32MAX, U32MAX + 1ull, 1ull << 63 }; 565 AienosCapRef ref[3]; 566 for (int i = 0; i < 3; i++) { 567 uint32_t slot = 2u + (uint32_t)i; 568 rc = mint_at(&a, slot, forced[i], SUBJ, RW, &ref[i]); 569 CHECK(G_AUTHORITY, rc == AIENOS_CAP_OK && ref[i].cap_id == slot && ref[i].generation == forced[i], 570 "mint at forced generation %" PRIu64 " (rc %d)", forced[i], rc); 571 CHECK(G_AUTHORITY, carry(ref[i], 2, st[1 + i], &stn[1 + i], &shown) == 0 && 572 shown.generation == forced[i] && shown.cap_id == slot, "carried %" PRIu64, forced[i]); 573 CHECK(G_AUTHORITY, val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_OK, 574 "honest reference at %" PRIu64 " validates", forced[i]); 575 AienosCapRef t = shown; 576 t.generation = shown.generation & U32MAX; /* what the old format kept */ 577 if (t.generation != shown.generation) 578 CHECK(G_AUTHORITY, val(&a, t, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_STALE_GEN, 579 "high 32 bits cut: STALE_GEN at %" PRIu64, forced[i]); 580 t.generation = shown.generation + 1; 581 CHECK(G_AUTHORITY, val(&a, t, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_STALE_GEN, 582 "generation+1: STALE_GEN"); 583 t.generation = shown.generation - 1; 584 CHECK(G_AUTHORITY, val(&a, t, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_STALE_GEN, 585 "generation-1: STALE_GEN"); 586 /* wrong right / wrong resource / wrong subject on the carried reference */ 587 CHECK(G_AUTHORITY, val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_EFFECT) == AIENOS_CAP_ERR_RIGHTS, 588 "wrong right: RIGHTS"); 589 CHECK(G_AUTHORITY, val(&a, shown, SUBJ, RES + 1, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_RESOURCE, 590 "wrong resource: RESOURCE"); 591 CHECK(G_AUTHORITY, val(&a, shown, OTHER, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_SUBJECT, 592 "wrong subject: SUBJECT"); 593 } 594 595 /* 3. hand-crafted wire: the v2 object with its high generation word zeroed 596 * decodes as a different (valid-looking) object and the authority refuses it */ 597 { 598 OmegaGraph *d = calloc(1, sizeof *d); 599 uint8_t w[8192]; 600 memcpy(w, st[3], stn[3]); /* the 1<<63 reference */ 601 size_t goff = 10 + BARE_PAYLOAD_OFF + 8; 602 CHECK(G_AUTHORITY, w[goff] == 0x80, "located the generation bytes"); 603 memset(w + goff, 0, 4); 604 CHECK(G_AUTHORITY, omega_graph_deserialize_binary(w, stn[3], d) == 0, "zeroed-high-word object decodes"); 605 EffectPayload e; 606 CHECK(G_AUTHORITY, omega_effect_read(&d->objects[0], &e) == OMEGA_EFFECT_OK && 607 e.capability_generation == ((1ull << 63) & U32MAX), "it carries the truncated generation"); 608 OmegaGraph *d0 = calloc(1, sizeof *d0); 609 omega_graph_deserialize_binary(st[3], stn[3], d0); 610 CHECK(G_AUTHORITY, omega_compare_semantic_id(&d->objects[0].id, &d0->objects[0].id) != 0, 611 "and a different SemanticId"); 612 AienosCapRef t = { e.capability_slot, e.capability_generation }; 613 CHECK(G_AUTHORITY, val(&a, t, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_STALE_GEN, 614 "authority refuses it: STALE_GEN"); 615 free(d0); 616 free(d); 617 } 618 619 /* 4. wrong slot */ 620 AienosCapRef oth; 621 CHECK(G_AUTHORITY, mint_at(&a, 5, (1ull << 62) + 7, OTHER, RW, &oth) == AIENOS_CAP_OK, "mint other subject"); 622 CHECK(G_AUTHORITY, carry(oth, 2, st[4], &stn[4], &shown) == 0 && 623 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_SUBJECT, 624 "someone else's live slot: SUBJECT"); 625 AienosCapRef ws = { ref[2].cap_id + 1000u, ref[2].generation }; 626 CHECK(G_AUTHORITY, carry(ws, 2, st[5], &stn[5], &shown) == 0 && 627 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_BOUNDS, "slot out of range: BOUNDS"); 628 AienosCapRef sw = { ref[1].cap_id, ref[2].generation }; /* right generation, wrong slot */ 629 CHECK(G_AUTHORITY, carry(sw, 2, st[5], &stn[5], &shown) == 0 && 630 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_STALE_GEN, 631 "generation of slot 4 presented on slot 3: STALE_GEN"); 632 633 /* 5. stale: revoke, reclaim, re-mint in the same slot */ 634 AienosCapRef of = office(&a); 635 CHECK(G_AUTHORITY, aienos_cap_revoke(a.admin, of, ref[0]) == AIENOS_CAP_OK, "revoke UINT32_MAX ref"); 636 CHECK(G_AUTHORITY, reread(st[1], stn[1], &shown) == 0 && 637 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_REVOKED, "revoked: REVOKED"); 638 CHECK(G_AUTHORITY, aienos_cap_reclaim(a.admin, of, ref[0].cap_id) == AIENOS_CAP_OK, "reclaim (generation advances)"); 639 CHECK(G_AUTHORITY, reread(st[1], stn[1], &shown) == 0 && 640 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_STALE_GEN, "reclaimed: STALE_GEN"); 641 AienosCapRef again; 642 CHECK(G_AUTHORITY, mint_at(&a, ref[0].cap_id, 0, SUBJ, RW, &again) == AIENOS_CAP_OK && 643 again.cap_id == ref[0].cap_id && again.generation == U32MAX + 1ull, 644 "re-mint in the slot crosses 2^32 (gen %" PRIu64 ")", again.generation); 645 CHECK(G_AUTHORITY, reread(st[1], stn[1], &shown) == 0 && 646 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_STALE_GEN, 647 "old effect replayed against the re-grant: STALE_GEN"); 648 CHECK(G_AUTHORITY, carry(again, 2, st[6], &stn[6], &shown) == 0 && 649 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_OK, "control: the re-grant validates"); 650 651 /* 6. replay after authority restart (restart-safe table, aienos#158) */ 652 CHECK(G_AUTHORITY, aienos_cap_kill(a.admin) == AIENOS_CAP_OK, "kill"); 653 rc = aienos_cap_restart(a.admin); 654 CHECK(G_AUTHORITY, rc == AIENOS_CAP_OK, "restart (%d)", rc); 655 for (int i = 0; i < 7; i++) { 656 if (i == 5) continue; /* slot-out-of-range probe */ 657 CHECK(G_AUTHORITY, reread(st[i], stn[i], &shown) == 0, "pre-restart effect %d re-reads", i); 658 int v = val(&a, shown, i == 4 ? OTHER : SUBJ, RES, AIENOS_CAP_RIGHT_WRITE); 659 CHECK(G_AUTHORITY, v == AIENOS_CAP_ERR_STALE_GEN, 660 "pre-restart reference (gen %" PRIu64 ") refused by the new table: %d", shown.generation, v); 661 } 662 AienosCapRef fresh; 663 CHECK(G_AUTHORITY, mint_at(&a, 1, 0, SUBJ, RW, &fresh) == AIENOS_CAP_OK, "mint on the new table"); 664 printf(" post-restart generation %" PRIu64 "\n", fresh.generation); 665 CHECK(G_AUTHORITY, fresh.generation > (1ull << 63), "new table starts above every old generation"); 666 CHECK(G_AUTHORITY, carry(fresh, 2, st[7], &stn[7], &shown) == 0 && shown.generation == fresh.generation && 667 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_OK, 668 "restart-safe generation round-trips and validates"); 669 670 /* 7. near the top: 0xFFFFFFFFFFFFFFFE validates; a further restart refuses (EXHAUSTED) */ 671 AienosCapRef top; 672 rc = mint_at(&a, 2, 0xFFFFFFFFFFFFFFFEull, SUBJ, RW, &top); 673 CHECK(G_AUTHORITY, rc == AIENOS_CAP_OK && top.generation == 0xFFFFFFFFFFFFFFFEull, "mint at 2^64-2 (%d)", rc); 674 CHECK(G_AUTHORITY, carry(top, 2, st[0], &stn[0], &shown) == 0 && shown.generation == top.generation && 675 val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_OK, "2^64-2 round-trips and validates"); 676 AienosCapRef t = shown; 677 t.generation = U32MAX - 1; /* 0xFFFFFFFE: the old format's view */ 678 CHECK(G_AUTHORITY, val(&a, t, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_ERR_STALE_GEN, 679 "2^64-2 cut to 32 bits: STALE_GEN"); 680 rc = aienos_cap_restart(a.admin); 681 CHECK(G_AUTHORITY, rc == AIENOS_CAP_ERR_EXHAUSTED, 682 "restart above 2^64-2 is refused (EXHAUSTED by design, never wraps): %d", rc); 683 CHECK(G_AUTHORITY, val(&a, shown, SUBJ, RES, AIENOS_CAP_RIGHT_WRITE) == AIENOS_CAP_OK, 684 "the table was not replaced by the refused restart"); 685 aienos_cap_stop(a.admin, a.view); 686 } 687 688 int main(void) { 689 test_roundtrip(); 690 test_identity(); 691 test_stale_reject(); 692 test_authority(); 693 int all_p = 0, all_t = 0, ok = 1; 694 for (int i = 0; i < G_COUNT; i++) { 695 int pass = g_total[i] > 0 && g_pass[i] == g_total[i]; 696 printf("GATE %s %s %d/%d\n", GATE_NAME[i], pass ? "PASS" : "FAIL", g_pass[i], g_total[i]); 697 all_p += g_pass[i]; 698 all_t += g_total[i]; 699 ok &= pass; 700 } 701 printf("%s %d/%d\n", ok ? "PASS" : "FAIL", all_p, all_t); 702 if (ok) printf("OMEGA_EFFECT_CAP64_PASS\n"); 703 return ok ? 0 : 1; 704 }