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/argus/tests/test_argus_integration.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 / argus / tests / test_argus_integration.c` 1227 lines ยท 56698 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_argus_integration.c -- ARGUS-0 integration gates (orchestrator-owned). 19 * 20 * Links ONLY the real lane objects (argus_event.c, argus_ring.c, sha256.c, 21 * argus_core.c, argus_detect.c) plus lane E's tests/stub_state.c, used here 22 * solely for its corpus generators (argus_corpus_benign / _hostile). No lane D 23 * stub. Prints one line per gate: GATE PASS|FAIL 24 * 25 * Run from native/argus (the secret-negative scan walks "." recursively). 26 * test_argus_integration [--bench] [object.o ...] 27 * Object files given on the command line are checked with `nm -u` for heap, 28 * I/O and clock imports (ARGUS_MEMORY). --bench runs only the ingest timing, 29 * longer. 30 */ 31 #include "argus_abi.h" 32 #include "argus_core.h" 33 #include "argus_detect.h" 34 #include "sha256.h" 35 #include "stub_state.h" 36 #include "../capability/aienos_capability.h" /* header only: ARGUS-1 mirror constants (never linked) */ 37 38 #include 39 #include 40 #include 41 #include 42 #include 43 #include 44 #include 45 #include 46 #include 47 #include 48 #include 49 #include 50 #include 51 #include 52 #include 53 #include 54 55 /* Advance an snprintf append offset without ever passing the end of the buffer: 56 * on truncation (or error) the offset is clamped so later appends get size 0. */ 57 static size_t snprintf_advance(size_t off, size_t cap, int n) 58 { 59 if (n < 0 || off >= cap) return off < cap ? off : cap; 60 return ((size_t)n >= cap - off) ? cap : off + (size_t)n; 61 } 62 63 #define N_CORPUS 5000u /* lane E validated the hostile corpus at this size */ 64 #define N_PROBE 10000u /* capacity probe only (reported, not a gate) */ 65 #define RING_LOG2 10u 66 #define RING_CAP (1u << RING_LOG2) 67 #define FCAP ARGUS_CORE_MAX_FINDINGS /* never truncate findings */ 68 69 /* ---- secret-negative, layout half (same assertions as lane B) ---- */ 70 #define NOT32(f) _Static_assert(sizeof(((ArgusEvent *)0)->f) != 32, "32-byte field: " #f) 71 NOT32(version); NOT32(class_); NOT32(kind); NOT32(effect_class); NOT32(outcome); NOT32(flags); 72 NOT32(sequence); NOT32(tick); NOT32(principal); NOT32(code); NOT32(cap_id); NOT32(object_id); 73 NOT32(cap_generation); NOT32(world_generation); NOT32(resource); 74 _Static_assert(sizeof(((ArgusEvent *)0)->machine_id) == 32, "machine_id"); 75 _Static_assert(sizeof(((ArgusEvent *)0)->evidence_digest) == 32, "evidence_digest"); 76 _Static_assert(1 + 1 + 2 + 1 + 1 + 2 + 8 + 8 + 4 + 4 + 4 + 4 + 8 + 8 + 8 + 32 + 32 == ARGUS_EVENT_SIZE, 77 "field list complete"); 78 _Static_assert(sizeof(ArgusEvent) == ARGUS_EVENT_SIZE, "event is 128 bytes, no padding"); 79 80 static int gates_failed; 81 82 static void gate(const char *name, int pass, const char *fmt, ...) __attribute__((format(printf, 3, 4))); 83 static void gate(const char *name, int pass, const char *fmt, ...) 84 { 85 char buf[2048]; 86 va_list ap; 87 va_start(ap, fmt); 88 vsnprintf(buf, sizeof buf, fmt, ap); 89 va_end(ap); 90 printf("GATE %s %s %s\n", name, pass ? "PASS" : "FAIL", buf); 91 fflush(stdout); 92 if (!pass) gates_failed++; 93 } 94 95 static void *xalloc(size_t bytes) 96 { 97 size_t r = (bytes + 63u) & ~(size_t)63u; 98 if (r == 0) r = 64; 99 void *p = aligned_alloc(64, r); 100 if (!p) { fprintf(stderr, "out of memory\n"); exit(2); } 101 memset(p, 0, r); 102 return p; 103 } 104 105 static void hex(char *out, const uint8_t *b, size_t n) 106 { 107 for (size_t i = 0; i < n; i++) sprintf(out + 2 * i, "%02x", b[i]); 108 out[2 * n] = 0; 109 } 110 111 static uint64_t now_ns(void) 112 { 113 struct timespec ts; 114 clock_gettime(CLOCK_MONOTONIC, &ts); 115 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 116 } 117 118 /* ---- a core run: all findings of a stream, in order ---------------------- */ 119 typedef struct { 120 ArgusCore *core; 121 void *mem; 122 ArgusFinding *f; /* all findings, in emission order */ 123 size_t nf, fmax; 124 uint64_t rc_full, rc_overflow, rc_malformed, rc_other, ingested; 125 uint64_t per_code[ARGUS_F_MAX + 1]; 126 } Run; 127 128 static void run_init(Run *r, size_t fmax) 129 { 130 memset(r, 0, sizeof *r); 131 r->mem = xalloc(argus_core_footprint()); 132 if (argus_core_init(&r->core, r->mem, argus_core_footprint()) != ARGUS_OK) { 133 fprintf(stderr, "argus_core_init failed\n"); 134 exit(2); 135 } 136 r->fmax = fmax; 137 r->f = xalloc(fmax * sizeof(ArgusFinding)); 138 } 139 140 static void run_free(Run *r) { free(r->mem); free(r->f); } 141 142 static void run_ingest(Run *r, const ArgusEvent *ev) 143 { 144 ArgusFinding out[FCAP]; 145 size_t n = 0; 146 int rc = argus_core_ingest(r->core, ev, out, FCAP, &n); 147 r->ingested++; 148 if (rc == ARGUS_ERR_FULL) r->rc_full++; 149 else if (rc == ARGUS_ERR_OVERFLOW) r->rc_overflow++; 150 else if (rc == ARGUS_ERR_MALFORMED) r->rc_malformed++; 151 else if (rc != ARGUS_OK) r->rc_other++; 152 for (size_t i = 0; i < n; i++) { 153 if (out[i].code <= ARGUS_F_MAX) r->per_code[out[i].code]++; 154 if (r->nf < r->fmax) r->f[r->nf] = out[i]; 155 r->nf++; 156 } 157 } 158 159 static void run_digests(const Run *r, uint8_t state[32], uint8_t chain[32]) 160 { 161 ArgusCoreHealth h; 162 argus_core_state_digest(r->core, state); 163 argus_core_health(r->core, &h); 164 memcpy(chain, h.chain, 32); 165 } 166 167 /* ---- corpora (static buffers; the generators are not reentrant) ----------- */ 168 static ArgusEvent benign[N_CORPUS]; 169 static ArgusEvent hostile[N_CORPUS]; 170 static ArgusExpectedFinding expect[N_CORPUS]; 171 static size_t n_benign, n_hostile, n_expect; 172 173 /* ======================================================================= */ 174 /* ARGUS_EVENT_ABI_PASS */ 175 /* ======================================================================= */ 176 static ArgusEvent fixed_event(void) /* identical to lane B tests/test_argus_event.c */ 177 { 178 ArgusEvent e; 179 memset(&e, 0, sizeof e); 180 e.version = ARGUS_ABI_VERSION; 181 e.class_ = ARGUS_CLASS_SECURITY; 182 e.kind = ARGUS_EV_CAPABILITY_USED; 183 e.effect_class = ARGUS_EFFECT_EXTERNAL; 184 e.outcome = ARGUS_OUTCOME_OK; 185 e.flags = ARGUS_FLAG_SYNTHETIC; 186 e.sequence = 0x0102030405060708ull; 187 e.tick = 0x1112131415161718ull; 188 e.principal = 0x21222324u; 189 e.code = -7; 190 e.cap_id = 0x31323334u; 191 e.object_id = 0x41424344u; 192 e.cap_generation = 0x5152535455565758ull; 193 e.world_generation = 0x6162636465666768ull; 194 e.resource = 0x8182838485868788ull; 195 for (unsigned i = 0; i < 32; i++) { 196 e.machine_id[i] = (uint8_t)(0xA0 + i); 197 e.evidence_digest[i] = (uint8_t)(0xC0 + i); 198 } 199 return e; 200 } 201 202 static ArgusFinding fixed_finding(void) /* identical to lane B */ 203 { 204 ArgusFinding f; 205 memset(&f, 0, sizeof f); 206 f.code = ARGUS_F_REVOKED_CAPABILITY_USED; 207 f.severity = ARGUS_SEV_HIGH; 208 f.confidence = ARGUS_CONF_DETERMINISTIC; 209 f.sync_allowed = 1; 210 f.containment = ARGUS_CONTAIN_REVOKE_CAPABILITY; 211 f.detector = ARGUS_F_REVOKED_CAPABILITY_USED; 212 f.sequence = 42; 213 f.prior_sequence = 17; 214 f.principal = 7; 215 f.cap_id = 9; 216 f.cap_generation = 3; 217 for (unsigned i = 0; i < 32; i++) { f.machine_id[i] = (uint8_t)i; f.event_digest[i] = (uint8_t)(0xFF - i); } 218 return f; 219 } 220 221 static size_t round_trip(const ArgusEvent *ev, size_t n, size_t *bad) 222 { 223 size_t ok = 0; 224 for (size_t i = 0; i < n; i++) { 225 uint8_t b[ARGUS_EVENT_SIZE], b2[ARGUS_EVENT_SIZE], d1[32], d2[32]; 226 ArgusEvent out; 227 memset(&out, 0xA5, sizeof out); 228 if (argus_event_encode(&ev[i], b) != ARGUS_OK || argus_event_decode(b, &out) != ARGUS_OK || 229 memcmp(&out, &ev[i], sizeof out) != 0 || argus_event_encode(&out, b2) != ARGUS_OK || 230 memcmp(b, b2, sizeof b) != 0) { (*bad)++; continue; } 231 argus_event_digest(&ev[i], d1); 232 sha256_hash(b, sizeof b, d2); 233 if (memcmp(d1, d2, 32) != 0) { (*bad)++; continue; } 234 ok++; 235 } 236 return ok; 237 } 238 239 static void gate_abi(void) 240 { 241 static const char *K_EVENT = "`1be29450b792d28426444fceb5e44174e69bb631d5ef6063a870c21929574303`"; 242 static const char *K_CHAIN = "`09ea598e432197cc2d6ff341ec57ab403c06312bad2992480b72c6ce87a6d747`"; 243 static const char *K_CHAIN2 = "`21b19c9ba36f86074263f49952b64ee8dea9b5ff02ed215555cb1a03bfda78e0`"; 244 static const char *K_FINDING = "`b56e133cc6f6cfefe642ce0f1ec74e67c3fee57db2f114257ba04d3d3ee7680a`"; 245 char h[65]; 246 int ka = 0; 247 uint8_t d[32], chain[32] = {0}; 248 ArgusEvent e = fixed_event(); 249 argus_event_digest(&e, d); hex(h, d, 32); ka += strcmp(h, K_EVENT) == 0; 250 argus_chain_extend(chain, &e); hex(h, chain, 32); ka += strcmp(h, K_CHAIN) == 0; 251 e.sequence++; 252 argus_chain_extend(chain, &e); hex(h, chain, 32); ka += strcmp(h, K_CHAIN2) == 0; 253 ArgusFinding f = fixed_finding(); 254 argus_finding_digest(&f, d); hex(h, d, 32); ka += strcmp(h, K_FINDING) == 0; 255 256 size_t bad = 0; 257 size_t ok_b = round_trip(benign, n_benign, &bad); 258 size_t ok_h = round_trip(hostile, n_hostile, &bad); 259 /* version byte 2 must be refused as VERSION */ 260 uint8_t b[ARGUS_EVENT_SIZE]; 261 ArgusEvent out; 262 e = fixed_event(); 263 argus_event_encode(&e, b); 264 b[0] = 2; 265 int v2 = argus_event_decode(b, &out) == ARGUS_ERR_VERSION; 266 gate("ARGUS_EVENT_ABI_PASS", ka == 4 && bad == 0 && ok_b == n_benign && ok_h == n_hostile && v2, 267 "known-answer %d/4 (event `1be29450`.., chain `09ea598e`.., chain2 `21b19c9b`.., finding `b56e133c`..); " 268 "round trip encode->decode->re-encode byte-identical + digest==sha256(bytes): benign %zu/%zu, hostile %zu/%zu, " 269 "bad %zu; v2 byte rejected=%d", 270 ka, ok_b, n_benign, ok_h, n_hostile, bad, v2); 271 } 272 273 /* ======================================================================= */ 274 /* ARGUS_EVENT_SECRET_NEGATIVE_PASS: recursive scan (re-implementation of */ 275 /* lane B's scan_tree: every regular file under ".", except ./out). */ 276 /* ======================================================================= */ 277 static int contains_ci(const char *hay, const char *needle) 278 { 279 size_t n = strlen(needle); 280 for (; *hay; hay++) if (strncasecmp(hay, needle, n) == 0) return 1; 281 return 0; 282 } 283 284 static int scan_tree(const char *dir, const char *word, const char *cap_len, int *files, int *hits, int *dirs) 285 { 286 DIR *d = opendir(dir); 287 if (!d) { fprintf(stderr, "cannot open %s (run from native/argus)\n", dir); return -1; } 288 (*dirs)++; 289 int err = 0; 290 struct dirent *de; 291 while ((de = readdir(d)) != NULL) { 292 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; 293 if (strcmp(dir, ".") == 0 && strcmp(de->d_name, "out") == 0) continue; 294 char path[1024]; 295 snprintf(path, sizeof path, "%s/%s", dir, de->d_name); 296 /* Open first, then fstat the descriptor (no check-then-use race). 297 * O_NOFOLLOW: a symlink fails with ELOOP and is skipped, as before; 298 * O_NONBLOCK: a FIFO cannot hang the scan (it is skipped below). */ 299 int fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK); 300 if (fd < 0) { 301 if (errno == ELOOP || errno == ENXIO) continue; /* symlink / socket: not scanned */ 302 err = -1; continue; 303 } 304 struct stat st; 305 if (fstat(fd, &st) != 0) { close(fd); err = -1; continue; } 306 if (S_ISDIR(st.st_mode)) { close(fd); if (scan_tree(path, word, cap_len, files, hits, dirs)) err = -1; continue; } 307 if (!S_ISREG(st.st_mode)) { close(fd); continue; } 308 FILE *fp = fdopen(fd, "r"); 309 if (!fp) { close(fd); err = -1; continue; } 310 (*files)++; 311 char line[4096]; 312 int lineno = 0; 313 while (fgets(line, sizeof line, fp)) { 314 lineno++; 315 if (contains_ci(line, word) || strstr(line, cap_len)) { 316 (*hits)++; 317 printf(" secret-negative hit: %s:%d\n", path, lineno); 318 } 319 } 320 fclose(fp); 321 } 322 closedir(d); 323 return err; 324 } 325 326 static void gate_secret(void) 327 { 328 char word[8], cap_len[32]; 329 snprintf(word, sizeof word, "%s%s", "to", "ken"); 330 snprintf(cap_len, sizeof cap_len, "%s%s%s", "AIENOS_CAP_", "TO", "KEN_LEN"); 331 int files = 0, hits = 0, dirs = 0; 332 int err = scan_tree(".", word, cap_len, &files, &hits, &dirs); 333 gate("ARGUS_EVENT_SECRET_NEGATIVE_PASS", err == 0 && hits == 0 && files >= 20, 334 "%d files in %d dirs under native/argus (recursive incl. docs/ tests/, out/ excluded), %d hits, scan errors %d; " 335 "static: 15 event fields != 32 bytes, only machine_id/evidence_digest are 32, field sum == 128", 336 files, dirs, hits, err ? 1 : 0); 337 } 338 339 /* ======================================================================= */ 340 /* Ring path: producer thread -> ring -> consumer thread -> core */ 341 /* ======================================================================= */ 342 typedef struct { 343 ArgusRing *ring; 344 const ArgusEvent *ev; 345 size_t n; 346 uint32_t throttle_depth; /* 0 = unthrottled burst */ 347 uint64_t accepted, refused_rc, other_rc; 348 _Atomic int done; 349 } Producer; 350 351 typedef struct { 352 ArgusRing *ring; 353 _Atomic int *prod_done; 354 Run *run; 355 uint64_t popped, drop_events, next_seq; 356 uint64_t dropped_by_class[ARGUS_CLASS_MAX + 1]; /* sum of TELEMETRY_DROPPED resource */ 357 uint64_t drop_events_by_class[ARGUS_CLASS_MAX + 1]; 358 uint64_t loss_expected, loss_seen, loss_wrong_sev; 359 unsigned sleep_us; /* slow consumer: sleep after each batch */ 360 size_t batch; 361 } Consumer; 362 363 static void *producer_main(void *arg) 364 { 365 Producer *p = arg; 366 for (size_t i = 0; i < p->n; i++) { 367 if (p->throttle_depth) { 368 for (;;) { 369 ArgusRingStats s; 370 argus_ring_stats(p->ring, &s); 371 if (s.depth < p->throttle_depth) break; 372 sched_yield(); 373 } 374 } 375 int rc = argus_ring_push(p->ring, &p->ev[i]); 376 if (rc == ARGUS_OK) p->accepted++; 377 else if (rc == ARGUS_ERR_FULL) p->refused_rc++; 378 else p->other_rc++; 379 } 380 atomic_store_explicit(&p->done, 1, memory_order_release); 381 return NULL; 382 } 383 384 /* Ingest one synthesized drop event and check the core's TELEMETRY_LOSS answer. */ 385 static void consume_drop(Consumer *c, const ArgusEvent *d) 386 { 387 size_t before = c->run->nf; 388 run_ingest(c->run, d); 389 c->drop_events++; 390 if (d->object_id <= ARGUS_CLASS_MAX) { 391 c->dropped_by_class[d->object_id] += d->resource; 392 c->drop_events_by_class[d->object_id]++; 393 } 394 int want = d->object_id == ARGUS_CLASS_CRITICAL || d->object_id == ARGUS_CLASS_SECURITY; 395 uint8_t want_sev = d->object_id == ARGUS_CLASS_CRITICAL ? ARGUS_SEV_CRITICAL : ARGUS_SEV_HIGH; 396 c->loss_expected += (uint64_t)want; 397 for (size_t i = before; i < c->run->nf && i < c->run->fmax; i++) { 398 if (c->run->f[i].code == ARGUS_F_TELEMETRY_LOSS) { 399 c->loss_seen++; 400 if (c->run->f[i].severity != want_sev) c->loss_wrong_sev++; 401 } 402 } 403 } 404 405 static void consumer_drain(Consumer *c) 406 { 407 ArgusEvent drops[ARGUS_CLASS_MAX]; 408 size_t nd = argus_ring_drain_drops(c->ring, drops, ARGUS_CLASS_MAX, &c->next_seq); 409 for (size_t i = 0; i < nd; i++) consume_drop(c, &drops[i]); 410 } 411 412 /* One consumer step: pop a batch, ingest it, drain drops. Returns events popped. */ 413 static size_t consumer_step(Consumer *c, ArgusEvent *buf) 414 { 415 size_t n = argus_ring_pop_batch(c->ring, buf, c->batch); 416 for (size_t i = 0; i < n; i++) run_ingest(c->run, &buf[i]); 417 c->popped += n; 418 consumer_drain(c); 419 return n; 420 } 421 422 static void *consumer_main(void *arg) 423 { 424 Consumer *c = arg; 425 ArgusEvent buf[64]; 426 for (;;) { 427 int done = atomic_load_explicit(c->prod_done, memory_order_acquire); 428 size_t n = consumer_step(c, buf); 429 if (n == 0 && done) break; /* producer finished before this empty pop: empty for good */ 430 if (c->sleep_us) { 431 struct timespec ts = { 0, (long)c->sleep_us * 1000L }; 432 nanosleep(&ts, NULL); 433 } else if (n == 0) { 434 sched_yield(); 435 } 436 } 437 return NULL; 438 } 439 440 typedef struct { 441 Producer p; 442 Consumer c; 443 ArgusRingStats stats; 444 } RingRun; 445 446 static void ring_run(RingRun *rr, Run *run, const ArgusEvent *ev, size_t n, uint32_t throttle) 447 { 448 memset(rr, 0, sizeof *rr); 449 size_t bytes = argus_ring_footprint(RING_CAP); 450 void *mem = xalloc(bytes); 451 ArgusRing *ring; 452 if (argus_ring_init(&ring, mem, bytes, RING_CAP) != ARGUS_OK) { fprintf(stderr, "ring init\n"); exit(2); } 453 rr->p.ring = ring; rr->p.ev = ev; rr->p.n = n; rr->p.throttle_depth = throttle; 454 atomic_init(&rr->p.done, 0); 455 rr->c.ring = ring; rr->c.prod_done = &rr->p.done; rr->c.run = run; rr->c.batch = 64; 456 rr->c.next_seq = 1; 457 pthread_t tp, tc; 458 pthread_create(&tc, NULL, consumer_main, &rr->c); 459 pthread_create(&tp, NULL, producer_main, &rr->p); 460 pthread_join(tp, NULL); 461 pthread_join(tc, NULL); 462 argus_ring_stats(ring, &rr->stats); 463 free(mem); 464 } 465 466 static uint64_t sum_refused(const ArgusRingStats *s) 467 { 468 uint64_t t = s->critical_overflow; 469 for (unsigned c = 1; c <= ARGUS_CLASS_MAX; c++) t += s->refused[c]; 470 return t; 471 } 472 473 /* Identity checks shared by transport runs. */ 474 static int ring_identity(const RingRun *rr, char *why, size_t whylen) 475 { 476 const ArgusRingStats *s = &rr->stats; 477 uint64_t refused = sum_refused(s); 478 int ok = 1; 479 ok &= rr->p.other_rc == 0; 480 ok &= s->pushed == rr->p.accepted; 481 ok &= s->pushed + refused == rr->p.n; 482 ok &= rr->p.refused_rc == refused; 483 ok &= s->popped == s->pushed && rr->c.popped == s->pushed; 484 ok &= s->refused[ARGUS_CLASS_CRITICAL] == 0; 485 /* every refusal is reported by a TELEMETRY_DROPPED, per class */ 486 ok &= rr->c.dropped_by_class[ARGUS_CLASS_CRITICAL] == s->critical_overflow; 487 for (unsigned c = 2; c <= ARGUS_CLASS_MAX; c++) ok &= rr->c.dropped_by_class[c] == s->refused[c]; 488 ok &= rr->c.loss_seen == rr->c.loss_expected && rr->c.loss_wrong_sev == 0; 489 snprintf(why, whylen, 490 "attempts %zu = pushed %llu + refused %llu (SEC %llu AUD %llu INF %llu, crit_overflow %llu); popped %llu; " 491 "drop events %llu reporting %llu lost; TELEMETRY_LOSS findings %llu/%llu expected", 492 rr->p.n, (unsigned long long)s->pushed, (unsigned long long)refused, 493 (unsigned long long)s->refused[2], (unsigned long long)s->refused[3], (unsigned long long)s->refused[4], 494 (unsigned long long)s->critical_overflow, (unsigned long long)s->popped, 495 (unsigned long long)rr->c.drop_events, 496 (unsigned long long)(rr->c.dropped_by_class[1] + rr->c.dropped_by_class[2] + rr->c.dropped_by_class[3] + 497 rr->c.dropped_by_class[4]), 498 (unsigned long long)rr->c.loss_seen, (unsigned long long)rr->c.loss_expected); 499 return ok; 500 } 501 502 /* Direct and ring-path hostile runs, shared by TRANSPORT, DETERMINISM, HARD_INVARIANTS. */ 503 static Run direct_h, ring_h, burst_h; 504 static RingRun rr_fast, rr_burst; 505 506 static void gate_transport(void) 507 { 508 char w1[512], w2[512]; 509 run_init(&ring_h, (size_t)N_CORPUS * FCAP); 510 ring_run(&rr_fast, &ring_h, hostile, n_hostile, RING_CAP / 4); 511 int id1 = ring_identity(&rr_fast, w1, sizeof w1); 512 int none_refused = sum_refused(&rr_fast.stats) == 0 && rr_fast.c.drop_events == 0; 513 514 run_init(&burst_h, (size_t)N_CORPUS * FCAP + 64); 515 ring_run(&rr_burst, &burst_h, hostile, n_hostile, 0); 516 int id2 = ring_identity(&rr_burst, w2, sizeof w2); 517 518 gate("ARGUS_EVENT_TRANSPORT_PASS", id1 && none_refused && id2 && ring_h.ingested == n_hostile, 519 "hostile seed 1, ring %u. fast consumer (producer keeps depth < %u): %s; nothing refused=%d. " 520 "burst (unthrottled producer): %s; identity=%d", 521 RING_CAP, RING_CAP / 4, w1, none_refused, w2, id2); 522 } 523 524 /* ======================================================================= */ 525 /* ARGUS_TRANSPORT_SATURATION_PASS */ 526 /* ======================================================================= */ 527 /* Each class is driven by a kind whose floor (argus_event_min_class) is that 528 * class, so every watermark is exercised by rule-abiding traffic: 529 * INFORMATIONAL: no v1 kind has an INFORMATIONAL floor (design fact), so a 530 * non-v1 kind (0x7FFF) is used. The ring is transport and has 531 * no floor for it; the core rejects it (MALFORMED_EVENT, 16), 532 * and the gate counts those findings exactly. 533 * SECURITY: CAPABILITY_DENIED (SECURITY floor), no finding. 534 * CRITICAL: CREDENTIAL_LEASE_REVOKED of an unknown lease (CRITICAL floor), 535 * not applied, no finding, no state growth. */ 536 #define FLOOD_KIND_INF 0x7FFFu 537 static uint16_t flood_kind(uint8_t cls) 538 { 539 return cls == ARGUS_CLASS_INFORMATIONAL ? FLOOD_KIND_INF 540 : cls == ARGUS_CLASS_CRITICAL ? ARGUS_EV_CREDENTIAL_LEASE_REVOKED 541 : ARGUS_EV_CAPABILITY_DENIED; 542 } 543 544 static ArgusEvent flood_event(uint64_t seq, uint8_t cls) 545 { 546 ArgusEvent e; 547 memset(&e, 0, sizeof e); 548 e.version = ARGUS_ABI_VERSION; 549 e.class_ = cls; 550 e.kind = flood_kind(cls); 551 e.effect_class = ARGUS_EFFECT_NONE; 552 e.outcome = ARGUS_OUTCOME_DENIED; 553 e.flags = ARGUS_FLAG_SYNTHETIC; 554 e.sequence = seq; 555 e.tick = seq; 556 e.principal = 1; 557 e.code = -7; /* authority: rights */ 558 e.cap_id = ARGUS_CAP_NONE; /* v1.1: no capability (cap 0 is the office slot) */ 559 return e; 560 } 561 562 /* class pattern: 5 INFORMATIONAL, 2 SECURITY, 1 CRITICAL per 8 */ 563 static uint8_t flood_class(size_t i) 564 { 565 unsigned k = (unsigned)(i & 7u); 566 return k < 5 ? ARGUS_CLASS_INFORMATIONAL : k < 7 ? ARGUS_CLASS_SECURITY : ARGUS_CLASS_CRITICAL; 567 } 568 569 typedef struct { 570 ArgusRing *ring; 571 const ArgusEvent *ev; 572 size_t n; 573 uint64_t first_refusal_idx[ARGUS_CLASS_MAX + 1]; /* attempt index + 1, 0 = never */ 574 uint64_t refused_below_limit[ARGUS_CLASS_MAX + 1]; /* refused although depth-before < class limit */ 575 uint64_t sec_refused_while_info_accepting; 576 uint64_t crit_refused_below_full; 577 uint64_t max_depth_seen; 578 _Atomic int done; 579 } SatProducer; 580 581 static void *sat_producer_main(void *arg) 582 { 583 SatProducer *p = arg; 584 uint32_t lim[ARGUS_CLASS_MAX + 1] = {0}; 585 for (unsigned c = 1; c <= ARGUS_CLASS_MAX; c++) lim[c] = argus_ring_saturation_point(RING_CAP, (uint8_t)c); 586 for (size_t i = 0; i < p->n; i++) { 587 ArgusRingStats s; 588 argus_ring_stats(p->ring, &s); /* depth can only fall between this read and the push */ 589 if (s.depth > p->max_depth_seen) p->max_depth_seen = s.depth; 590 unsigned cls = p->ev[i].class_; 591 int rc = argus_ring_push(p->ring, &p->ev[i]); 592 if (rc == ARGUS_ERR_FULL) { 593 if (!p->first_refusal_idx[cls]) p->first_refusal_idx[cls] = i + 1; 594 if (s.depth < lim[cls]) p->refused_below_limit[cls]++; 595 if (cls == ARGUS_CLASS_SECURITY && s.depth < lim[ARGUS_CLASS_INFORMATIONAL]) 596 p->sec_refused_while_info_accepting++; 597 if (cls == ARGUS_CLASS_CRITICAL && s.depth < RING_CAP) p->crit_refused_below_full++; 598 } 599 } 600 atomic_store_explicit(&p->done, 1, memory_order_release); 601 return NULL; 602 } 603 604 typedef struct { 605 Consumer c; 606 _Atomic int stall; /* consumer pops nothing while set */ 607 } SatConsumer; 608 609 static void *sat_consumer_main(void *arg) 610 { 611 SatConsumer *sc = arg; 612 Consumer *c = &sc->c; 613 ArgusEvent buf[64]; 614 for (;;) { 615 int done = atomic_load_explicit(c->prod_done, memory_order_acquire); 616 if (atomic_load_explicit(&sc->stall, memory_order_acquire) && !done) { sched_yield(); continue; } 617 size_t n = consumer_step(c, buf); 618 if (n == 0 && done) break; 619 struct timespec ts = { 0, (long)c->sleep_us * 1000L }; 620 nanosleep(&ts, NULL); 621 } 622 return NULL; 623 } 624 625 static void gate_saturation(void) 626 { 627 const size_t N = 200000; 628 ArgusEvent *ev = xalloc(N * sizeof *ev); 629 for (size_t i = 0; i < N; i++) ev[i] = flood_event(i + 1, flood_class(i)); 630 631 size_t n_inf = 0; 632 for (size_t i = 0; i < N; i++) n_inf += ev[i].class_ == ARGUS_CLASS_INFORMATIONAL; 633 634 /* pre-check: through a real core, the SEC/CRIT flood kinds yield no finding 635 * and each INF (non-v1) event yields exactly one MALFORMED_EVENT */ 636 Run pre; 637 run_init(&pre, 8192); 638 size_t pre_inf = 0; 639 for (size_t i = 0; i < 4096; i++) { run_ingest(&pre, &ev[i]); pre_inf += ev[i].class_ == ARGUS_CLASS_INFORMATIONAL; } 640 size_t pre_findings = pre.nf - pre.per_code[ARGUS_F_MALFORMED_EVENT]; 641 int pre_malformed_ok = pre.per_code[ARGUS_F_MALFORMED_EVENT] == pre_inf; 642 run_free(&pre); 643 644 Run run; 645 run_init(&run, N * 2 + 4096); 646 size_t bytes = argus_ring_footprint(RING_CAP); 647 void *mem = xalloc(bytes); 648 ArgusRing *ring; 649 argus_ring_init(&ring, mem, bytes, RING_CAP); 650 651 SatProducer p; 652 memset(&p, 0, sizeof p); 653 p.ring = ring; p.ev = ev; p.n = N; 654 atomic_init(&p.done, 0); 655 SatConsumer sc; 656 memset(&sc, 0, sizeof sc); 657 sc.c.ring = ring; sc.c.prod_done = &p.done; sc.c.run = &run; sc.c.batch = 8; sc.c.sleep_us = 20; 658 sc.c.next_seq = 1; 659 atomic_init(&sc.stall, 1); /* phase 1: consumer stalled until the ring is full */ 660 661 pthread_t tp, tc; 662 pthread_create(&tc, NULL, sat_consumer_main, &sc); 663 pthread_create(&tp, NULL, sat_producer_main, &p); 664 /* release the consumer once the producer has driven the ring to 100% (or finished) */ 665 for (;;) { 666 ArgusRingStats s; 667 argus_ring_stats(ring, &s); 668 if (s.depth >= RING_CAP || atomic_load_explicit(&p.done, memory_order_acquire)) break; 669 sched_yield(); 670 } 671 atomic_store_explicit(&sc.stall, 0, memory_order_release); 672 pthread_join(tp, NULL); 673 pthread_join(tc, NULL); 674 ArgusRingStats s; 675 argus_ring_stats(ring, &s); 676 677 uint64_t loss_total = run.per_code[ARGUS_F_TELEMETRY_LOSS]; 678 uint64_t malformed = run.per_code[ARGUS_F_MALFORMED_EVENT]; 679 uint64_t other_findings = run.nf - loss_total - malformed; 680 681 int ok = 1; 682 ok &= pre_findings == 0 && pre_malformed_ok; 683 ok &= s.refused[0] == 0; /* every flood event met its class floor */ 684 ok &= malformed == n_inf - s.refused[ARGUS_CLASS_INFORMATIONAL]; /* each accepted INF -> one code 16 */ 685 ok &= p.refused_below_limit[1] == 0 && p.refused_below_limit[2] == 0 && p.refused_below_limit[4] == 0; 686 ok &= p.sec_refused_while_info_accepting == 0 && p.crit_refused_below_full == 0; 687 ok &= p.first_refusal_idx[ARGUS_CLASS_INFORMATIONAL] != 0; 688 ok &= p.first_refusal_idx[ARGUS_CLASS_SECURITY] == 0 || 689 p.first_refusal_idx[ARGUS_CLASS_SECURITY] > p.first_refusal_idx[ARGUS_CLASS_INFORMATIONAL]; 690 ok &= p.max_depth_seen == RING_CAP; 691 ok &= s.pushed + sum_refused(&s) == N && s.popped == s.pushed; 692 ok &= sc.c.dropped_by_class[1] == s.critical_overflow && sc.c.dropped_by_class[2] == s.refused[2] && 693 sc.c.dropped_by_class[3] == s.refused[3] && sc.c.dropped_by_class[4] == s.refused[4]; 694 ok &= sc.c.loss_seen == sc.c.loss_expected && sc.c.loss_wrong_sev == 0 && loss_total == sc.c.loss_seen; 695 ok &= other_findings == 0; 696 gate("ARGUS_TRANSPORT_SATURATION_PASS", ok, 697 "%zu attempts (5 INF 0x7FFF : 2 SEC CAPABILITY_DENIED : 1 CRIT LEASE_REVOKED), ring %u, consumer stalled to 100%% then 8/20us; limits INF %u SEC %u CRIT %u; " 698 "max depth seen %llu; refused INF %llu SEC %llu CRIT(overflow) %llu; first refusal idx INF %llu SEC %llu CRIT %llu; " 699 "refused below limit INF/SEC/CRIT %llu/%llu/%llu; SEC refused while INF accepting %llu; CRIT refused below 100%% %llu; " 700 "drop events %llu (CRIT-class %llu SEC-class %llu INF-class %llu) -> TELEMETRY_LOSS %llu/%llu expected, wrong sev %llu; " 701 "INF (non-v1 kind) delivered -> MALFORMED_EVENT %llu/%llu expected; ring malformed refusals %llu; " 702 "other findings %llu (flood pre-check %zu)", 703 N, RING_CAP, argus_ring_saturation_point(RING_CAP, 4), argus_ring_saturation_point(RING_CAP, 2), 704 argus_ring_saturation_point(RING_CAP, 1), (unsigned long long)p.max_depth_seen, 705 (unsigned long long)s.refused[4], (unsigned long long)s.refused[2], (unsigned long long)s.critical_overflow, 706 (unsigned long long)p.first_refusal_idx[4], (unsigned long long)p.first_refusal_idx[2], 707 (unsigned long long)p.first_refusal_idx[1], (unsigned long long)p.refused_below_limit[4], 708 (unsigned long long)p.refused_below_limit[2], (unsigned long long)p.refused_below_limit[1], 709 (unsigned long long)p.sec_refused_while_info_accepting, (unsigned long long)p.crit_refused_below_full, 710 (unsigned long long)sc.c.drop_events, (unsigned long long)sc.c.drop_events_by_class[1], 711 (unsigned long long)sc.c.drop_events_by_class[2], (unsigned long long)sc.c.drop_events_by_class[4], 712 (unsigned long long)sc.c.loss_seen, (unsigned long long)sc.c.loss_expected, 713 (unsigned long long)sc.c.loss_wrong_sev, (unsigned long long)malformed, 714 (unsigned long long)(n_inf - s.refused[ARGUS_CLASS_INFORMATIONAL]), (unsigned long long)s.refused[0], 715 (unsigned long long)other_findings, pre_findings); 716 run_free(&run); 717 free(mem); 718 free(ev); 719 } 720 721 /* ======================================================================= */ 722 /* ARGUS_CORE_DETERMINISM_PASS */ 723 /* ======================================================================= */ 724 static void gate_determinism(void) 725 { 726 /* direct_h was filled in main() before the ring runs */ 727 uint8_t s1[32], c1[32], s2[32], c2[32]; 728 run_digests(&direct_h, s1, c1); 729 run_digests(&ring_h, s2, c2); 730 int zero_refused = sum_refused(&rr_fast.stats) == 0 && rr_fast.c.drop_events == 0; 731 int same_n = direct_h.nf == ring_h.nf && direct_h.nf <= direct_h.fmax; 732 int same_f = same_n && memcmp(direct_h.f, ring_h.f, direct_h.nf * sizeof(ArgusFinding)) == 0; 733 int same_s = memcmp(s1, s2, 32) == 0, same_c = memcmp(c1, c2, 32) == 0; 734 /* a third, fresh direct core: repeatability */ 735 Run again; 736 run_init(&again, (size_t)N_CORPUS * FCAP); 737 for (size_t i = 0; i < n_hostile; i++) run_ingest(&again, &hostile[i]); 738 uint8_t s3[32], c3[32]; 739 run_digests(&again, s3, c3); 740 int same3 = again.nf == direct_h.nf && memcmp(again.f, direct_h.f, again.nf * sizeof(ArgusFinding)) == 0 && 741 memcmp(s3, s1, 32) == 0 && memcmp(c3, c1, 32) == 0; 742 run_free(&again); 743 uint8_t fd[32]; 744 sha256_hash((const uint8_t *)direct_h.f, direct_h.nf * sizeof(ArgusFinding), fd); 745 char hs[65], hc[65], hf[65]; 746 hex(hs, s1, 32); hex(hc, c1, 32); hex(hf, fd, 32); 747 gate("ARGUS_CORE_DETERMINISM_PASS", zero_refused && same_f && same_s && same_c && same3 && direct_h.nf > 0, 748 "hostile seed 1 (%zu events): direct vs ring path (0 refused=%d): findings %zu vs %zu byte-identical=%d, " 749 "state digest equal=%d, chain equal=%d; third fresh direct core identical=%d; state %.16s.. chain %.16s.. " 750 "findings-bytes sha256 %.16s..", 751 n_hostile, zero_refused, direct_h.nf, ring_h.nf, same_f, same_s, same_c, same3, hs, hc, hf); 752 } 753 754 /* ======================================================================= */ 755 /* sequence analysis for code 12: streams keyed like the core, */ 756 /* (machine_id, ARGUS_STREAM_OF(flags), CONSUMER bit) (v1.1) */ 757 /* ======================================================================= */ 758 static int per_stream_strictly_increasing(const ArgusEvent *ev, size_t n, size_t *streams) 759 { 760 enum { MAXS = 4096 }; 761 static uint8_t ids[MAXS][ARGUS_MACHINE_ID_LEN]; 762 static uint8_t cons[MAXS]; 763 static uint16_t strm[MAXS]; 764 static uint64_t last[MAXS]; 765 size_t ns = 0; 766 int ok = 1; 767 for (size_t i = 0; i < n; i++) { 768 uint8_t cb = (ev[i].flags & ARGUS_FLAG_CONSUMER) ? 1 : 0; 769 uint16_t st = ARGUS_STREAM_OF(ev[i].flags); 770 size_t k; 771 for (k = 0; k < ns; k++) 772 if (cons[k] == cb && strm[k] == st && memcmp(ids[k], ev[i].machine_id, ARGUS_MACHINE_ID_LEN) == 0) break; 773 if (k == ns) { 774 if (ns == MAXS) return -1; 775 memcpy(ids[ns], ev[i].machine_id, ARGUS_MACHINE_ID_LEN); 776 cons[ns] = cb; 777 strm[ns] = st; 778 last[ns] = ev[i].sequence; 779 ns++; 780 continue; 781 } 782 if (ev[i].sequence <= last[k]) ok = 0; 783 else last[k] = ev[i].sequence; 784 } 785 *streams = ns; 786 return ok; 787 } 788 789 /* ======================================================================= */ 790 /* ARGUS_HARD_INVARIANTS_PASS */ 791 /* ======================================================================= */ 792 static int expected_has(uint64_t seq, uint16_t code) 793 { 794 for (size_t i = 0; i < n_expect; i++) if (expect[i].sequence == seq && expect[i].code == code) return 1; 795 return 0; 796 } 797 798 /* Explain findings that are not in the expected list (all findings, for a 799 * benign stream): re-ingest through a fresh core, then attribute each 800 * unexpected code 4 / code 6 / code 11 to a known integration cause. */ 801 static void explain_unexpected(const char *label, const ArgusEvent *ev, size_t n, int use_expect) 802 { 803 static uint8_t full_at[N_PROBE]; 804 Run r; 805 run_init(&r, (size_t)n * FCAP); 806 size_t *first = xalloc((n + 1) * sizeof *first); /* index of the first finding of event i */ 807 for (size_t i = 0; i < n; i++) { 808 uint64_t before = r.rc_full; 809 first[i] = r.nf; 810 run_ingest(&r, &ev[i]); 811 full_at[i] = r.rc_full != before; 812 } 813 first[n] = r.nf; 814 uint64_t c6 = 0, c6_revive = 0, c4 = 0, c4_full = 0, c11 = 0, other = 0; 815 uint64_t full_kind[ARGUS_EV_KIND_MAX + 1] = {0}; 816 for (size_t i = 0; i < n; i++) { 817 if (full_at[i] && ev[i].kind <= ARGUS_EV_KIND_MAX) full_kind[ev[i].kind]++; 818 for (size_t j = first[i]; j < first[i + 1] && j < r.fmax; j++) { 819 uint16_t code = r.f[j].code; 820 if (use_expect && expected_has(r.f[j].sequence, code)) continue; 821 if (code == ARGUS_F_CREDENTIAL_SCOPE_VIOLATION) { 822 int revoked = 0, recreated = 0; 823 for (size_t p = 0; p < i; p++) { 824 if (ev[p].object_id != ev[i].object_id || ev[p].outcome != ARGUS_OUTCOME_OK) continue; 825 if (ev[p].kind == ARGUS_EV_CREDENTIAL_LEASE_REVOKED) revoked = 1; 826 if (ev[p].kind == ARGUS_EV_CREDENTIAL_LEASE_CREATED && revoked) recreated = 1; 827 } 828 c6++; 829 c6_revive += (uint64_t)recreated; 830 } else if (code == ARGUS_F_ARTIFACT_DIGEST_UNEXPECTED) { 831 int adm_full = 0; 832 for (size_t p = 0; p < i && !adm_full; p++) 833 adm_full = full_at[p] && (ev[p].kind == ARGUS_EV_ARTIFACT_ADMITTED || 834 ev[p].kind == ARGUS_EV_ARTIFACT_REJECTED) && 835 memcmp(ev[p].evidence_digest, ev[i].evidence_digest, ARGUS_DIGEST_LEN) == 0; 836 c4++; 837 c4_full += (uint64_t)adm_full; 838 } else if (code == ARGUS_F_TELEMETRY_LOSS) { 839 c11++; 840 } else { 841 other++; 842 } 843 } 844 } 845 char fk[256] = ""; 846 size_t off = 0; 847 for (unsigned k = 0; k <= ARGUS_EV_KIND_MAX; k++) 848 if (full_kind[k]) 849 off = snprintf_advance(off, sizeof fk, snprintf(fk + off, sizeof fk - off, " kind%u=%llu", k, (unsigned long long)full_kind[k])); 850 printf("EXPLAIN %s: unexpected code 6 %llu, of which %llu on a lease id REVOKED then CREATED again earlier " 851 "(core keeps it REVOKED, argus_core.c:427; corpus reuses lease ids); unexpected code 4 %llu, of which %llu " 852 "on an artifact whose ADMITTED/REJECTED hit ARGUS_ERR_FULL (ARGUS_CORE_ARTIFACTS 64 < corpus G_ARTIFACTS 96); " 853 "code 11 %llu (== ingest rc FULL %llu, by kind:%s); other unexpected %llu\n", 854 label, (unsigned long long)c6, (unsigned long long)c6_revive, (unsigned long long)c4, 855 (unsigned long long)c4_full, (unsigned long long)c11, (unsigned long long)r.rc_full, fk[0] ? fk : " none", 856 (unsigned long long)other); 857 free(first); 858 run_free(&r); 859 } 860 861 /* Hard codes: every finding code except 11 (telemetry loss) and 12 (sequence 862 * anomaly), which have their own checks below. Since the hostile-review round 863 * this includes 13..16 (authority replay, subject mismatch, trust escalation, 864 * malformed event); lane E's corpus injects 14 and 15. */ 865 static int hard_code(unsigned c) 866 { 867 return c >= 1 && c <= ARGUS_F_MAX && c != ARGUS_F_TELEMETRY_LOSS && c != ARGUS_F_SEQUENCE_ANOMALY; 868 } 869 870 static void gate_hard_invariants(void) 871 { 872 uint64_t exp_by[ARGUS_F_MAX + 1] = {0}, det_by[ARGUS_F_MAX + 1] = {0}, unexp_by[ARGUS_F_MAX + 1] = {0}; 873 for (size_t i = 0; i < n_expect; i++) { 874 uint16_t code = expect[i].code; 875 if (code > ARGUS_F_MAX) continue; 876 exp_by[code]++; 877 int found = 0; 878 for (size_t j = 0; j < direct_h.nf && !found; j++) 879 found = direct_h.f[j].sequence == expect[i].sequence && direct_h.f[j].code == code; 880 if (found) det_by[code]++; 881 else printf(" missed expected finding: seq %llu code %u\n", (unsigned long long)expect[i].sequence, code); 882 } 883 uint64_t unexpected_hard = 0; 884 for (size_t j = 0; j < direct_h.nf; j++) { 885 uint16_t code = direct_h.f[j].code; 886 if (hard_code(code) && !expected_has(direct_h.f[j].sequence, code)) { 887 unexpected_hard++; 888 unexp_by[code]++; 889 if (unexpected_hard <= 10) 890 printf(" unexpected finding: seq %llu code %u\n", (unsigned long long)direct_h.f[j].sequence, code); 891 } 892 } 893 size_t streams = 0; 894 int incr = per_stream_strictly_increasing(hostile, n_hostile, &streams); 895 uint64_t seq12 = direct_h.per_code[ARGUS_F_SEQUENCE_ANOMALY]; 896 uint64_t loss11 = direct_h.per_code[ARGUS_F_TELEMETRY_LOSS]; 897 int missing = 0; 898 printf(" hard invariants, hostile seed 1 (%zu events, %zu injected):\n", n_hostile, n_expect); 899 printf(" code detected/expected unexpected\n"); 900 for (unsigned c = 1; c <= ARGUS_F_MAX; c++) { 901 if (!hard_code(c)) continue; 902 printf(" %4u %8llu/%-8llu %llu\n", c, (unsigned long long)det_by[c], (unsigned long long)exp_by[c], 903 (unsigned long long)unexp_by[c]); 904 if (det_by[c] != exp_by[c]) missing = 1; 905 } 906 char table[512]; 907 size_t off = 0; 908 for (unsigned c = 1; c <= ARGUS_F_MAX; c++) 909 if (hard_code(c)) 910 off = snprintf_advance(off, sizeof table, snprintf(table + off, sizeof table - off, "%s%u:%llu/%llu", c > 1 ? " " : "", c, 911 (unsigned long long)det_by[c], (unsigned long long)exp_by[c])); 912 /* 12 on strictly increasing per-stream sequences is an integration bug; 11 means shadow tables overflowed */ 913 int seq_ok = !(seq12 > 0 && incr == 1); 914 if (unexpected_hard || loss11 || seq12) explain_unexpected("hostile seed 1", hostile, n_hostile, 1); 915 gate("ARGUS_HARD_INVARIANTS_PASS", 916 !missing && unexpected_hard == 0 && seq_ok && loss11 == 0 && direct_h.rc_full == 0 && direct_h.rc_overflow == 0, 917 "real core + real detectors, hostile seed 1: code detected/expected [%s]; all %zu injected found=%d; " 918 "unexpected hard (1..16 except 11/12) %llu; code 12 findings %llu (corpus sequences strictly increasing per " 919 "(machine_id,stream,consumer) stream: %s, %zu streams); code 11 findings %llu; ingest rc FULL %llu OVERFLOW %llu", 920 table, n_expect, !missing, (unsigned long long)unexpected_hard, (unsigned long long)seq12, 921 incr == 1 ? "yes" : incr == 0 ? "NO" : "unknown", streams, (unsigned long long)loss11, 922 (unsigned long long)direct_h.rc_full, (unsigned long long)direct_h.rc_overflow); 923 } 924 925 /* ======================================================================= */ 926 /* ARGUS_FALSE_POSITIVE_BASELINE_PASS */ 927 /* ======================================================================= */ 928 static void gate_false_positive(void) 929 { 930 uint64_t total = 0, by[ARGUS_F_MAX + 1] = {0}, full = 0, events = 0; 931 int incr_all = 1; 932 char per_seed[256]; 933 size_t off = 0; 934 for (uint64_t seed = 1; seed <= 9; seed++) { 935 size_t n = argus_corpus_benign(benign, N_CORPUS, seed); 936 size_t streams; 937 if (per_stream_strictly_increasing(benign, n, &streams) != 1) incr_all = 0; 938 Run r; 939 run_init(&r, (size_t)N_CORPUS * FCAP); 940 for (size_t i = 0; i < n; i++) run_ingest(&r, &benign[i]); 941 for (unsigned c = 0; c <= ARGUS_F_MAX; c++) by[c] += r.per_code[c]; 942 for (size_t j = 0; j < r.nf && j < 3; j++) 943 printf(" benign seed %llu finding: seq %llu code %u\n", (unsigned long long)seed, 944 (unsigned long long)r.f[j].sequence, r.f[j].code); 945 if (r.nf) { 946 char label[32]; 947 snprintf(label, sizeof label, "benign seed %llu", (unsigned long long)seed); 948 explain_unexpected(label, benign, n, 0); 949 } 950 total += r.nf; 951 full += r.rc_full; 952 events += n; 953 off = snprintf_advance(off, sizeof per_seed, snprintf(per_seed + off, sizeof per_seed - off, "%s%zu", seed > 1 ? "," : "", r.nf)); 954 run_free(&r); 955 } 956 /* restore seed-1 benign corpus for the timing below */ 957 n_benign = argus_corpus_benign(benign, N_CORPUS, 1); 958 gate("ARGUS_FALSE_POSITIVE_BASELINE_PASS", total == 0 && full == 0, 959 "benign seeds 1..9, %llu events through real core: findings per seed [%s], total %llu " 960 "(code 11: %llu, code 12: %llu; sequences strictly increasing per stream: %s); ingest rc FULL %llu", 961 (unsigned long long)events, per_seed, (unsigned long long)total, (unsigned long long)by[11], 962 (unsigned long long)by[12], incr_all ? "yes" : "NO", (unsigned long long)full); 963 } 964 965 /* ======================================================================= */ 966 /* ARGUS_MEMORY */ 967 /* ======================================================================= */ 968 static int allowed_import(const char *s) 969 { 970 static const char *prefix[] = { "argus_", "sha256_", "__aarch64_", "__stack_chk_", "__memcpy_chk", 971 "__memset_chk", "__memmove_chk" }; 972 static const char *exact[] = { "memset", "memcpy", "memcmp", "memmove", "bzero", "__bzero" }; 973 for (size_t i = 0; i < sizeof prefix / sizeof *prefix; i++) 974 if (strncmp(s, prefix[i], strlen(prefix[i])) == 0) return 1; 975 for (size_t i = 0; i < sizeof exact / sizeof *exact; i++) 976 if (strcmp(s, exact[i]) == 0) return 1; 977 return 0; 978 } 979 980 static void gate_memory(int nobj, char **objs) 981 { 982 size_t core_fp = argus_core_footprint(); 983 size_t ring_fp = argus_ring_footprint(RING_CAP); 984 int bad = 0, checked = 0, nm_err = 0; 985 char imports[1024] = ""; 986 size_t ioff = 0; 987 for (int i = 0; i < nobj; i++) { 988 char cmd[1200]; 989 snprintf(cmd, sizeof cmd, "nm -u '%s' 2>/dev/null", objs[i]); 990 FILE *p = popen(cmd, "r"); 991 if (!p) { nm_err++; continue; } 992 char line[512]; 993 while (fgets(line, sizeof line, p)) { 994 line[strcspn(line, "\r\n")] = 0; 995 char *sym = strrchr(line, ' '); 996 sym = sym ? sym + 1 : line; 997 if (!*sym || strchr(sym, ':')) continue; 998 const char *s = sym; 999 #ifdef __APPLE__ 1000 if (*s == '_') s++; 1001 #endif 1002 if (!allowed_import(s)) { 1003 bad++; 1004 printf(" forbidden import in %s: %s\n", objs[i], s); 1005 } 1006 char key[520]; 1007 snprintf(key, sizeof key, " %.500s ", s); 1008 char probe[1100]; 1009 snprintf(probe, sizeof probe, " %s ", imports); 1010 if (!strstr(probe, key) && ioff + strlen(s) + 2 < sizeof imports) 1011 ioff = snprintf_advance(ioff, sizeof imports, snprintf(imports + ioff, sizeof imports - ioff, "%s%s", ioff ? " " : "", s)); 1012 } 1013 if (pclose(p) != 0) nm_err++; 1014 checked++; 1015 } 1016 gate("ARGUS_MEMORY", nobj >= 4 && checked == nobj && nm_err == 0 && bad == 0 && core_fp > 0 && ring_fp > 0, 1017 "argus_core_footprint %zu B; ring(%u) footprint %zu B (%zu B/event slot); %d objects checked with nm -u, " 1018 "forbidden imports %d (allowed: argus_/sha256_ cross-calls, mem*, stack protector, aarch64 outline atomics); " 1019 "imports seen: %s", 1020 core_fp, RING_CAP, ring_fp, sizeof(ArgusEvent), checked, bad, imports[0] ? imports : "(none)"); 1021 } 1022 1023 /* ======================================================================= */ 1024 /* ingest timing: benign corpus through the real core, real SHA-256 */ 1025 /* ======================================================================= */ 1026 static int cmp_u64(const void *a, const void *b) 1027 { 1028 uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; 1029 return x < y ? -1 : x > y; 1030 } 1031 1032 static void ingest_timing(unsigned reps) 1033 { 1034 size_t total = (size_t)reps * n_benign; 1035 uint64_t *ns = xalloc(total * sizeof *ns); 1036 uint64_t *ov = xalloc(10000 * sizeof *ov); 1037 for (int i = 0; i < 10000; i++) { uint64_t a = now_ns(), b = now_ns(); ov[i] = b - a; } 1038 qsort(ov, 10000, sizeof *ov, cmp_u64); 1039 size_t k = 0; 1040 ArgusFinding out[FCAP]; 1041 for (unsigned r = 0; r < reps; r++) { 1042 void *mem = xalloc(argus_core_footprint()); 1043 ArgusCore *core; 1044 argus_core_init(&core, mem, argus_core_footprint()); 1045 for (size_t i = 0; i < n_benign; i++) { 1046 size_t n; 1047 uint64_t a = now_ns(); 1048 argus_core_ingest(core, &benign[i], out, FCAP, &n); 1049 ns[k++] = now_ns() - a; 1050 } 1051 free(mem); 1052 } 1053 /* whole-stream pass without per-event timers, for a clean mean */ 1054 uint64_t clean_ns = 0; 1055 for (unsigned r = 0; r < reps; r++) { 1056 void *mem = xalloc(argus_core_footprint()); 1057 ArgusCore *core; 1058 argus_core_init(&core, mem, argus_core_footprint()); 1059 uint64_t b0 = now_ns(); 1060 for (size_t i = 0; i < n_benign; i++) { size_t n; argus_core_ingest(core, &benign[i], out, FCAP, &n); } 1061 clean_ns += now_ns() - b0; 1062 free(mem); 1063 } 1064 qsort(ns, total, sizeof *ns, cmp_u64); 1065 printf("INGEST ns/event (benign seed 1, %zu events x %u fresh cores, real core + detectors + SHA-256): " 1066 "p50 %llu p99 %llu p99.9 %llu max %llu; mean without per-event timers %.1f; timer overhead p50 %llu ns " 1067 "(included in p50/p99)\n", 1068 n_benign, reps, (unsigned long long)ns[total / 2], (unsigned long long)ns[(total * 99) / 100], 1069 (unsigned long long)ns[(total * 999) / 1000], (unsigned long long)ns[total - 1], 1070 (double)clean_ns / (double)total, (unsigned long long)ov[5000]); 1071 free(ns); 1072 free(ov); 1073 } 1074 1075 /* capacity probe: does the hostile corpus at N_PROBE overflow the core's tables? */ 1076 static void capacity_probe(void) 1077 { 1078 static ArgusEvent big[N_PROBE]; 1079 static ArgusExpectedFinding bexp[N_PROBE]; 1080 size_t ne = 0; 1081 size_t n = argus_corpus_hostile(big, N_PROBE, 1, bexp, N_PROBE, &ne); 1082 Run r; 1083 run_init(&r, (size_t)N_PROBE * FCAP); 1084 uint64_t first_full = 0; 1085 for (size_t i = 0; i < n; i++) { 1086 uint64_t before = r.rc_full; 1087 run_ingest(&r, &big[i]); 1088 if (r.rc_full != before && !first_full) first_full = big[i].sequence; 1089 } 1090 printf("NOTE capacity probe (not a gate): hostile seed 1 at %zu events, %zu injected: findings %zu, " 1091 "code 11 %llu, code 12 %llu, ingest rc FULL %llu (first at seq %llu)\n", 1092 n, ne, r.nf, (unsigned long long)r.per_code[11], (unsigned long long)r.per_code[12], 1093 (unsigned long long)r.rc_full, (unsigned long long)first_full); 1094 run_free(&r); 1095 } 1096 1097 /* ---- ARGUS-1 (ABI v1.2) header checks: lane L0 -------------------------------- 1098 * Exercises the header only: v1.2 is strictly additive over v1.1. The encode/ 1099 * decode/digest functions (lane B) and argus_contain (lane C) are declared but not 1100 * called here until they land. */ 1101 #define ARGUS1_FSIZE(T, f) sizeof(((T *)0)->f) 1102 _Static_assert(ARGUS_ABI_MINOR == 2u && ARGUS_ABI_VERSION == 1u, "v1.2 minor, event version byte unchanged"); 1103 _Static_assert(ARGUS_CONTAIN_REQUEST_SIZE == 152u, "request encoding is 152 bytes"); 1104 _Static_assert(ARGUS_CONTAIN_REQUEST_VERSION == 2u, "request version 2"); 1105 _Static_assert(ARGUS1_FSIZE(ArgusContainmentRequest, incident_id) + ARGUS1_FSIZE(ArgusContainmentRequest, containment) + 1106 ARGUS1_FSIZE(ArgusContainmentRequest, severity) + ARGUS1_FSIZE(ArgusContainmentRequest, finding_code) + 1107 ARGUS1_FSIZE(ArgusContainmentRequest, principal) + sizeof(uint32_t) + sizeof(uint64_t) /* target, packed */ + 1108 ARGUS1_FSIZE(ArgusContainmentRequest, machine_id) + ARGUS1_FSIZE(ArgusContainmentRequest, finding_digest) + 1109 ARGUS1_FSIZE(ArgusContainmentRequest, request_id) + ARGUS1_FSIZE(ArgusContainmentRequest, finding_sequence) + 1110 ARGUS1_FSIZE(ArgusContainmentRequest, target_object) + ARGUS1_FSIZE(ArgusContainmentRequest, target_rights) + 1111 ARGUS1_FSIZE(ArgusContainmentRequest, target_digest) + ARGUS1_FSIZE(ArgusContainmentRequest, flags) + 1112 ARGUS1_FSIZE(ArgusContainmentRequest, version) + ARGUS1_FSIZE(ArgusContainmentRequest, reserved) == 1113 ARGUS_CONTAIN_REQUEST_SIZE, "request field sizes sum to the wire size"); 1114 _Static_assert(offsetof(ArgusContainmentRequest, request_id) > offsetof(ArgusContainmentRequest, finding_digest), 1115 "v1.2 fields are appended after the ARGUS-0 fields"); 1116 _Static_assert(ARGUS_EV_KIND_MAX == 93 && ARGUS_F_MAX == 21, "v1.2 maxima"); 1117 _Static_assert(ARGUS_EV_CAPABILITY_USE_SUMMARY == 81 && ARGUS_EV_TELEMETRY_DROPPED == 80 && 1118 ARGUS_F_MALFORMED_EVENT == 16, "v1.1 values unchanged"); 1119 _Static_assert(ARGUS_EV_CONTAINMENT_PROPOSED == 90 && ARGUS_EV_CONTAINMENT_DECIDED == 91 && 1120 ARGUS_EV_CONTAINMENT_EXECUTED == 92 && ARGUS_EV_AUTHORITY_ESCALATED == 93, "kinds 90-93"); 1121 _Static_assert(ARGUS_F_CONTAINMENT_DECISION_UNMATCHED == 17 && ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED == 18 && 1122 ARGUS_F_CONTAINMENT_EXECUTION_UNCONFIRMED == 19 && ARGUS_F_CONTAINMENT_UNANSWERED == 20 && 1123 ARGUS_F_CONTAINMENT_SCOPE_EXCEEDED == 21, "codes 17-21"); 1124 _Static_assert(ARGUS_CONTAIN_MAX == 10 && ARGUS_CONTAIN_LIVE_TYPE == ARGUS_CONTAIN_REVOKE_CAPABILITY && 1125 ARGUS_CONTAIN_REVOKE_CAPABILITY == 1, "request types 1..10 unchanged; one LIVE type"); 1126 _Static_assert(ARGUS_CREQ_KNOWN == (ARGUS_CREQ_SYNTHETIC | ARGUS_CREQ_SATURATION), "request flags"); 1127 _Static_assert(ARGUS_CSTATUS_MAX == 8 && ARGUS_CSTATUS_FAILED_SCOPE == 8, "status 0..8"); 1128 _Static_assert(sizeof(ArgusContainHealth) == 17 * sizeof(uint64_t), "spec 4.3: 17 counters"); 1129 _Static_assert(ARGUS_CONTAIN_PENDING == 32 && ARGUS_CONTAIN_RECENT == 128 && ARGUS_CONTAIN_WINDOW_EVENTS == 4096 && 1130 ARGUS_CONTAIN_WINDOW_MAX == 16 && ARGUS_CONTAIN_TIMEOUT_EVENTS == 16384 && 1131 ARGUS_CONTAIN_COOLDOWN_EVENTS == 65536 && ARGUS_CONTAIN_RETRIES == 1, "spec 4 table constants"); 1132 _Static_assert(ARGUS_CAP_RIGHT_PRIVILEGED == AIENOS_CAP_RIGHT_PRIVILEGED && 1133 ARGUS_CAP_RIGHT_REVOKE == AIENOS_CAP_RIGHT_REVOKE, "ARGUS rights mirrors equal the authority's"); 1134 _Static_assert(ARGUS_CAP_MAX == AIENOS_CAP_MAX, "cap table size"); 1135 1136 static void gate_abi_v12(void) 1137 { 1138 /* kinds 90-93: distinct, strictly above every v1.1 kind (max 81) */ 1139 static const uint16_t v12k[] = { ARGUS_EV_CONTAINMENT_PROPOSED, ARGUS_EV_CONTAINMENT_DECIDED, 1140 ARGUS_EV_CONTAINMENT_EXECUTED, ARGUS_EV_AUTHORITY_ESCALATED }; 1141 static const uint16_t v12f[] = { ARGUS_F_CONTAINMENT_DECISION_UNMATCHED, ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED, 1142 ARGUS_F_CONTAINMENT_EXECUTION_UNCONFIRMED, ARGUS_F_CONTAINMENT_UNANSWERED, 1143 ARGUS_F_CONTAINMENT_SCOPE_EXCEEDED }; 1144 int kinds_ok = 1, codes_ok = 1; 1145 for (size_t i = 0; i < 4; i++) { 1146 if (v12k[i] <= ARGUS_EV_CAPABILITY_USE_SUMMARY || v12k[i] > ARGUS_EV_KIND_MAX) kinds_ok = 0; 1147 for (size_t j = 0; j < i; j++) if (v12k[i] == v12k[j]) kinds_ok = 0; 1148 } 1149 for (size_t i = 0; i < 5; i++) { 1150 if (v12f[i] <= ARGUS_F_MALFORMED_EVENT || v12f[i] > ARGUS_F_MAX) codes_ok = 0; 1151 for (size_t j = 0; j < i; j++) if (v12f[i] == v12f[j]) codes_ok = 0; 1152 } 1153 /* no ARGUS-0 detector raises a containment code (17-21 never come from detection) */ 1154 int det_ok = 1; 1155 for (size_t i = 0; i < argus_hard_detector_count; i++) 1156 if (argus_hard_detectors[i].id > ARGUS_F_MALFORMED_EVENT) det_ok = 0; 1157 /* object_id packing round trip over every type x status x containment code */ 1158 int pack_ok = 1; 1159 for (unsigned t = 1; t <= ARGUS_CONTAIN_MAX; t++) 1160 for (unsigned s = 0; s <= ARGUS_CSTATUS_MAX; s++) 1161 for (unsigned c = 0; c <= ARGUS_F_MAX; c++) { 1162 uint32_t o = ARGUS_CONTAIN_PACK(t, s, c); 1163 if (ARGUS_CONTAIN_TYPE_OF(o) != t || ARGUS_CONTAIN_STATUS_OF(o) != s || ARGUS_CONTAIN_CODE_OF(o) != c) 1164 pack_ok = 0; 1165 } 1166 /* G11 (table half): the ratified sync mask (spec section 7) equals argus_hard_detectors[] */ 1167 uint32_t mask = 0; 1168 for (size_t i = 0; i < argus_hard_detector_count; i++) 1169 if (argus_hard_detectors[i].sync_allowed) mask |= 1u << argus_hard_detectors[i].id; 1170 int sync_ok = mask == ARGUS_SYNC_RATIFIED_MASK; 1171 /* G11 symbol direction is checked by check_argus1_symbols.sh from the 1172 * test-argus1 target; this table half must agree with that result. */ 1173 /* TODO(L0, G12, wave 3): full `make test` + `make sanitize` on the Spark incl. test-argus1 1174 * and sanitize-argus1; `make test` on the Mac. */ 1175 /* TODO(L0): G6 needs an independent offline checker over a complete chain 1176 * transcript plus authority observer records before evidence is auditable. */ 1177 gate("ARGUS1_ABI_V12_HEADER", kinds_ok && codes_ok && det_ok && pack_ok && sync_ok, 1178 "static: event 128 B + 17 v1.1 offsets pinned, request wire %u B (field sum), request v%u, ABI minor %u; " 1179 "kinds 90-93 above 81 distinct=%d; codes 17-21 above 16 distinct=%d; no detector raises 17-21=%d; " 1180 "object_id pack round trip=%d; sync mask 0x%x vs ratified 0x%x=%d", 1181 ARGUS_CONTAIN_REQUEST_SIZE, ARGUS_CONTAIN_REQUEST_VERSION, ARGUS_ABI_MINOR, kinds_ok, codes_ok, det_ok, 1182 pack_ok, mask, (unsigned)ARGUS_SYNC_RATIFIED_MASK, sync_ok); 1183 } 1184 1185 int main(int argc, char **argv) 1186 { 1187 int bench = 0; 1188 int nobj = 0; 1189 char **objs = xalloc((size_t)(argc + 1) * sizeof *objs); 1190 for (int i = 1; i < argc; i++) { 1191 if (strcmp(argv[i], "--bench") == 0) bench = 1; 1192 else objs[nobj++] = argv[i]; 1193 } 1194 n_benign = argus_corpus_benign(benign, N_CORPUS, 1); 1195 n_hostile = argus_corpus_hostile(hostile, N_CORPUS, 1, expect, N_CORPUS, &n_expect); 1196 if (n_benign != N_CORPUS || n_hostile != N_CORPUS || n_expect == 0) { 1197 fprintf(stderr, "corpus generation failed (%zu %zu %zu)\n", n_benign, n_hostile, n_expect); 1198 return 2; 1199 } 1200 if (bench) { 1201 ingest_timing(200); 1202 free(objs); 1203 return 0; 1204 } 1205 1206 run_init(&direct_h, (size_t)N_CORPUS * FCAP); 1207 for (size_t i = 0; i < n_hostile; i++) run_ingest(&direct_h, &hostile[i]); 1208 1209 gate_abi(); 1210 gate_abi_v12(); 1211 gate_secret(); 1212 gate_transport(); 1213 gate_saturation(); 1214 gate_determinism(); 1215 gate_hard_invariants(); 1216 gate_false_positive(); 1217 gate_memory(nobj, objs); 1218 ingest_timing(20); 1219 capacity_probe(); 1220 1221 run_free(&direct_h); 1222 run_free(&ring_h); 1223 run_free(&burst_h); 1224 free(objs); 1225 printf("test_argus_integration: %s (%d gate(s) failed)\n", gates_failed ? "FAIL" : "PASS", gates_failed); 1226 return gates_failed ? 1 : 0; 1227 }