interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / argus / tests / test_argus_determinism.c

684 lines · 32930 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_determinism.c -- lane D determinism gate + provisional ingest
19 * microbenchmark.
20 *
21 * A 10,000-event synthetic stream (xorshift64*, fixed seed, covers every event
22 * kind, non-OK outcomes, sequence repeats, malformed events, table overflow)
23 * (cap_id up to 260, so ids >= ARGUS_CAP_MAX exercise the malformed path; replays and
24 * stale grants/revokes exercise AUTHORITY_REPLAY and not-applied) is fed to two fresh cores in lockstep: return codes and finding bytes must be
25 * identical per event, and final state digests and chains equal. The same
26 * stream is then fed with two other chunkings (one event at a time with
27 * digest/health queries interleaved; and split at event 3333 with the core's
28 * memory byte-copied to a different buffer), plus once with a 1-finding output
29 * buffer, and all must reach the same state digest.
30 *
31 * Timing numbers use the STUB hash (FNV), not SHA-256: provisional, Mac only.
32 *
33 * v1.2 (ARGUS-1): after the ARGUS-0 part (unchanged, same stream, same output
34 * lines), argus1_containment_determinism() replays recorded containment fixtures
35 * (kinds 90-93, one per code 17-21 plus a happy path; see the fixture block) and
36 * the ARGUS-0 stream + all fixtures, with known-answer digests (stub hash).
37 */
38#define _GNU_SOURCE
39#include "test_common_d.h"
40#include <stdlib.h>
41#include <time.h>
42
43#define N_EVENTS 10000u
44#define N_MACH 20u
45
46static ArgusEvent stream[N_EVENTS];
47static _Alignas(16) uint8_t memA[1 << 17], memB[1 << 17], memC[1 << 17], memD[1 << 17], memD2[1 << 17], memE[1 << 17];
48
49static uint64_t rs = 0x9E3779B97F4A7C15ull;
50static uint64_t rnd(void)
51{
52 rs ^= rs >> 12; rs ^= rs << 25; rs ^= rs >> 27;
53 return rs * 0x2545F4914F6CDD1Dull;
54}
55static uint32_t rn(uint32_t n) { return (uint32_t)(rnd() % n); }
56
57static const uint16_t kinds[] = {
58 1, 2, 3, 4, 10, 11, 12, 20, 21, 22, 30, 31, 32, 40, 41, 42, 43,
59 50, 51, 52, 60, 61, 62, 63, 70, 71, 72, 80, 81
60};
61#define N_STREAMS 3u /* v1.1: stream ids per machine */
62#define N_STORES 12u /* v1.1: World stores; more than ARGUS_WORLD_STORES so the table fills */
63#define N_KINDS (sizeof kinds / sizeof kinds[0])
64
65static void gen(void)
66{
67 static uint64_t seqs[N_MACH][N_STREAMS][2];
68 static uint64_t world[N_STORES];
69 for (uint32_t i = 0; i < N_EVENTS; i++) {
70 ArgusEvent *e = &stream[i];
71 memset(e, 0, sizeof *e);
72 uint16_t kind = kinds[rn(N_KINDS)];
73 uint32_t m = rn(100) < 90 ? rn(4) : rn(N_MACH);
74 uint32_t cons = kind == ARGUS_EV_TELEMETRY_DROPPED ? 1u : 0u;
75 e->version = ARGUS_ABI_VERSION;
76 e->class_ = (uint8_t)(1 + rn(4));
77 e->kind = kind;
78 e->effect_class = (uint8_t)rn(4);
79 uint32_t o = rn(100);
80 e->outcome = o < 80 ? ARGUS_OUTCOME_OK : o < 95 ? ARGUS_OUTCOME_DENIED : ARGUS_OUTCOME_ERROR;
81 uint32_t st = rn(N_STREAMS);
82 e->flags = (uint16_t)(ARGUS_FLAG_SYNTHETIC | (cons ? ARGUS_FLAG_CONSUMER : 0) | (st << ARGUS_FLAG_STREAM_SHIFT));
83 uint64_t *sq = &seqs[m][st][cons];
84 uint32_t r = rn(100);
85 if (r == 0 && *sq > 0)
86 e->sequence = *sq; /* replay */
87 else if (r == 1 && *sq > 1)
88 e->sequence = *sq - 1; /* reorder */
89 else
90 e->sequence = ++*sq;
91 e->tick = i;
92 e->principal = 1 + rn(12);
93 e->code = (int32_t)rn(3) - 1;
94 e->cap_id = rn(50) == 0 ? ARGUS_CAP_NONE : rn(261);
95 e->cap_generation = 1 + rn(4);
96 mid(e->machine_id, m);
97 switch (kind) {
98 case ARGUS_EV_CAPABILITY_GRANTED: e->object_id = rn(0x400); e->resource = rnd(); break;
99 case ARGUS_EV_CREDENTIAL_LEASE_CREATED: case ARGUS_EV_CREDENTIAL_LEASE_USED:
100 case ARGUS_EV_CREDENTIAL_LEASE_REVOKED: e->object_id = rn(90); e->resource = rn(16); break;
101 case ARGUS_EV_ARTIFACT_ADMITTED: case ARGUS_EV_ARTIFACT_REJECTED:
102 case ARGUS_EV_ARTIFACT_ACTIVATED: dg(e->evidence_digest, rn(90)); break;
103 case ARGUS_EV_MACHINE_TRUST_CHANGED: e->object_id = rn(8); break;
104 case ARGUS_EV_PROVIDER_DISCOVERED: case ARGUS_EV_PROVIDER_CHANGED:
105 case ARGUS_EV_PROVIDER_QUARANTINED: case ARGUS_EV_PROVIDER_USED: dg(e->evidence_digest, 1000 + rn(45)); break;
106 case ARGUS_EV_WORLD_COMMITTED: {
107 uint32_t s = rn(10) < 8 ? rn(3) : rn(N_STORES);
108 world[s] = rn(10) == 0 && world[s] > 2 ? world[s] - 2 : world[s] + 1;
109 e->object_id = s;
110 e->world_generation = world[s]; dg(e->evidence_digest, 5000 + 100 * s + (uint32_t)world[s]); break;
111 }
112 case ARGUS_EV_CAPABILITY_USE_SUMMARY: e->resource = 1 + rn(4096); e->world_generation = e->cap_generation - (e->cap_generation ? rn(2) : 0); break;
113 case ARGUS_EV_POLICY_CHANGED: case ARGUS_EV_RUNTIME_BUILD_CHANGED: dg(e->evidence_digest, 7000 + rn(5)); break;
114 case ARGUS_EV_TELEMETRY_DROPPED: e->object_id = 1 + rn(4); e->resource = 1 + rn(50); break;
115 default: break;
116 }
117 if (rn(100) == 0)
118 e->version = 0; /* malformed */
119 }
120}
121
122typedef struct {
123 uint64_t fold, nfind, rc_fold, full, overflow, malformed, seq_anom, det_findings, replay, malformed_f;
124 uint8_t state[32], chain[32];
125} RunSummary;
126
127static void fold_bytes(uint64_t *h, const void *p, size_t n)
128{
129 const uint8_t *b = p;
130 for (size_t i = 0; i < n; i++) { *h ^= b[i]; *h *= 0x100000001b3ull; }
131}
132
133static void account(RunSummary *s, int rc, const ArgusFinding *f, size_t n)
134{
135 int32_t r = rc;
136 fold_bytes(&s->rc_fold, &r, sizeof r);
137 if (rc == ARGUS_ERR_FULL) s->full++;
138 if (rc == ARGUS_ERR_OVERFLOW) s->overflow++;
139 if (rc == ARGUS_ERR_MALFORMED) s->malformed++;
140 for (size_t i = 0; i < n; i++) {
141 fold_bytes(&s->fold, &f[i], sizeof f[i]);
142 if (f[i].code == ARGUS_F_SEQUENCE_ANOMALY) s->seq_anom++;
143 if (f[i].code == ARGUS_F_AUTHORITY_REPLAY) s->replay++;
144 if (f[i].code == ARGUS_F_MALFORMED_EVENT) s->malformed_f++;
145 if (f[i].code != ARGUS_F_SEQUENCE_ANOMALY && f[i].code != ARGUS_F_TELEMETRY_LOSS &&
146 f[i].code != ARGUS_F_AUTHORITY_REPLAY && f[i].code != ARGUS_F_MALFORMED_EVENT) s->det_findings++;
147 }
148 s->nfind += n;
149}
150
151static void finish(RunSummary *s, const ArgusCore *c)
152{
153 ArgusCoreHealth h;
154 argus_core_state_digest(c, s->state);
155 argus_core_health(c, &h);
156 memcpy(s->chain, h.chain, 32);
157}
158
159static void init_sum(RunSummary *s) { memset(s, 0, sizeof *s); s->fold = s->rc_fold = 0xcbf29ce484222325ull; }
160
161static int same(const RunSummary *a, const RunSummary *b, int with_findings)
162{
163 if (memcmp(a->state, b->state, 32) || memcmp(a->chain, b->chain, 32)) return 0;
164 if (with_findings && (a->fold != b->fold || a->nfind != b->nfind || a->rc_fold != b->rc_fold)) return 0;
165 return 1;
166}
167
168static uint64_t now_ns(void)
169{
170#ifdef __APPLE__
171 return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
172#else
173 struct timespec ts;
174 clock_gettime(CLOCK_MONOTONIC, &ts);
175 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
176#endif
177}
178
179static int cmp_u64(const void *a, const void *b)
180{
181 uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b;
182 return x < y ? -1 : x > y;
183}
184
185/* ======================================================================== *
186 * ARGUS-1 (v1.2) containment fixtures, lane D. RECORDED FOR REUSE by lanes
187 * C (argus_contain unit tests), H (hostile) and X (bridge e2e): each fixture is
188 * the event pattern spec section 3/4.1 says argus_contain must turn into the
189 * listed code (fx_expect[]). The core itself raises none of 17-21: it only
190 * validates, sequence-checks, counts and chains 90-93, applies no shadow
191 * change for them and never runs a detector on them (spec section 3, lane D).
192 *
193 * Encoding per spec section 3 so the streams also pass lane B's real v1.2
194 * validate: version 1, class CRITICAL, effect NONE, object_id =
195 * ARGUS_CONTAIN_PACK(type, status, finding_code), outcome paired with status,
196 * world_generation = request_id (!= 0), CONSUMER only on 90, resource =
197 * finding_sequence (90) / decision_id (91) / slots revoked (92); kind 4 class CRITICAL
198 * (its real floor). Lanes C/H/X: treat the EVENT PATTERN as canonical, not the digests
199 * (evidence_digest changes once fx_request_digest becomes lane B's SHA-256).
200 *
201 * Fixture k uses machine 200+k (authority stream 0, bridge stream 1, Omega
202 * stream 2, consumer stream 0 + CONSUMER), caps 200+2k / 201+2k, principal
203 * 100+k, so all fixtures can be concatenated into one coherent stream.
204 * Event 0 joins machine 200+k (so the real code-7 detector stays quiet), events
205 * 1-2 grant the target and a descendant, and the trigger (event 3) is a stale-generation USED: the stub detector raises
206 * code 2 STALE_GENERATION HIGH (the ARGUS-1 LIVE RevokeCapability trigger).
207 *
208 * fx_request_digest is a TEST-ONLY stand-in (152-byte canonical encoding per
209 * spec section 2, folded with the stub FNV-style hash); replace it with lane B's
210 * argus_contain_request_digest (SHA-256) when argus_event.c lands. Known-answer
211 * digests below are under the lane-D STUB hash, not SHA-256; the SHA-256 pins
212 * belong in L0's integration test once lane B merges.
213 * ======================================================================== */
214
215enum { FX_HAPPY = 0, FX_C17, FX_C18, FX_C19, FX_C20, FX_C21, FX_COUNT };
216static const char *const fx_name[FX_COUNT] = {
217 "happy: 90 -> 91 GRANT -> one kind 4 -> 92 DONE (+93)",
218 "code17: 91 GRANT for an unknown request_id",
219 "code18: 92 DONE while the request is only PROPOSED",
220 "code19: 91 GRANT -> 92 DONE with no kind 4",
221 "code20: 91 ESCALATE then no next step for TIMEOUT_EVENTS+1 events",
222 "code21: 91 GRANT -> two kind 4 (cascade) -> 92 FAILED_SCOPE low32=2",
223};
224/* Code argus_contain must raise for the fixture (0 = none) and the index of the
225 * event on which it is due (code 20: the last event, once the timeout elapsed). */
226static const uint16_t fx_expect[FX_COUNT] = { 0, 17, 18, 19, 20, 21 };
227static size_t fx_trigger[FX_COUNT];
228
229#define FX_MAX (ARGUS_CONTAIN_TIMEOUT_EVENTS + 64u)
230static ArgusEvent fx_ev[FX_COUNT][FX_MAX];
231static size_t fx_n[FX_COUNT];
232static ArgusContainmentRequest fx_req[FX_COUNT];
233
234static void fx_put(uint8_t *p, uint64_t v, int n) { for (int i = 0; i < n; i++) p[i] = (uint8_t)(v >> (8 * i)); }
235
236/* TEST-ONLY canonical 152-byte encoding (spec section 2 offsets), to be replaced by lane B's encoder. */
237static void fx_request_encode(const ArgusContainmentRequest *r, uint8_t b[ARGUS_CONTAIN_REQUEST_SIZE])
238{
239 memset(b, 0, ARGUS_CONTAIN_REQUEST_SIZE);
240 fx_put(b + 0, r->incident_id, 8); b[8] = r->containment; b[9] = r->severity;
241 fx_put(b + 10, r->finding_code, 2); fx_put(b + 12, r->principal, 4);
242 fx_put(b + 16, r->target.cap_id, 4); fx_put(b + 20, r->target.generation, 8);
243 memcpy(b + 28, r->machine_id, ARGUS_MACHINE_ID_LEN); memcpy(b + 60, r->finding_digest, ARGUS_DIGEST_LEN);
244 fx_put(b + 92, r->request_id, 8); fx_put(b + 100, r->finding_sequence, 8);
245 fx_put(b + 108, r->target_object, 4); fx_put(b + 112, r->target_rights, 4);
246 memcpy(b + 116, r->target_digest, ARGUS_DIGEST_LEN);
247 fx_put(b + 148, r->flags, 2); b[150] = r->version; b[151] = r->reserved;
248}
249
250/* TEST-ONLY digest stand-in (not SHA-256). */
251static void fx_request_digest(const ArgusContainmentRequest *r, uint8_t out[ARGUS_DIGEST_LEN])
252{
253 uint8_t b[ARGUS_CONTAIN_REQUEST_SIZE];
254 fx_request_encode(r, b);
255 for (int lane = 0; lane < 4; lane++) {
256 uint64_t h = 0xcbf29ce484222325ull ^ ((uint64_t)(lane + 7) * 0x9e3779b97f4a7c15ull);
257 for (size_t i = 0; i < sizeof b; i++) { h ^= b[i]; h *= 0x100000001b3ull; }
258 fx_put(out + 8 * lane, h, 8);
259 }
260}
261
262static ArgusEvent fx_base(uint16_t kind, uint32_t k, uint16_t stream, uint64_t seq)
263{
264 ArgusEvent e = ev_make(kind, seq);
265 mid(e.machine_id, 200u + k);
266 e.flags = (uint16_t)(ARGUS_FLAG_SYNTHETIC | (stream << ARGUS_FLAG_STREAM_SHIFT));
267 return e;
268}
269
270/* 90-92 record for fixture k's request (spec section 3 field table). */
271static ArgusEvent fx_contain(uint16_t kind, uint32_t k, uint64_t seq, uint8_t status, uint8_t outcome,
272 uint64_t request_id, uint64_t resource, int32_t code, uint64_t tick)
273{
274 const ArgusContainmentRequest *r = &fx_req[k];
275 ArgusEvent e = fx_base(kind, k, kind == ARGUS_EV_CONTAINMENT_PROPOSED ? 0 : 1, seq);
276 e.flags = kind == ARGUS_EV_CONTAINMENT_PROPOSED ? ARGUS_FLAG_CONSUMER
277 : (uint16_t)(1u << ARGUS_FLAG_STREAM_SHIFT);
278 e.class_ = ARGUS_CLASS_CRITICAL;
279 e.effect_class = ARGUS_EFFECT_NONE;
280 e.outcome = outcome;
281 e.code = code;
282 e.principal = r->principal;
283 e.cap_id = r->target.cap_id;
284 e.cap_generation = r->target.generation;
285 e.object_id = ARGUS_CONTAIN_PACK(r->containment, status, r->finding_code);
286 e.world_generation = request_id;
287 e.resource = resource;
288 e.tick = tick;
289 memcpy(e.machine_id, r->machine_id, ARGUS_MACHINE_ID_LEN);
290 fx_request_digest(r, e.evidence_digest);
291 return e;
292}
293
294/* Builds fixture k while ingesting it into a scratch core, so the request's
295 * finding_digest / incident_id come from the finding the core really emitted. */
296static void fx_build(uint32_t k, ArgusCore *c)
297{
298 ArgusEvent *s = fx_ev[k];
299 size_t n = 0;
300 uint32_t cap = 200u + 2u * k, child = cap + 1u, P = 100u + k;
301 ArgusFinding f[ARGUS_CORE_MAX_FINDINGS];
302 size_t nf;
303 stub_detect_mode = 0;
304
305 ArgusEvent e = fx_base(ARGUS_EV_MACHINE_JOINED, k, 0, 1); /* joined, so real detectors raise no code 7 */
306 s[n++] = e;
307 e = fx_base(ARGUS_EV_CAPABILITY_GRANTED, k, 0, 2);
308 e.principal = P; e.cap_id = cap; e.cap_generation = 3; e.object_id = 0x1; e.resource = 0x77;
309 s[n++] = e;
310 e = fx_base(ARGUS_EV_CAPABILITY_GRANTED, k, 0, 3); /* a descendant, used by the cascade fixture */
311 e.principal = P; e.cap_id = child; e.cap_generation = 1; e.object_id = 0x1; e.resource = 0x77;
312 s[n++] = e;
313 e = fx_base(ARGUS_EV_CAPABILITY_USED, k, 0, 4); /* stale generation -> code 2 HIGH (stub) */
314 e.principal = P; e.cap_id = cap; e.cap_generation = 2;
315 s[n++] = e;
316 for (size_t i = 0; i < n; i++)
317 argus_core_ingest(c, &s[i], f, ARGUS_CORE_MAX_FINDINGS, &nf);
318 CHECK(nf == 1 && f[0].code == ARGUS_F_STALE_GENERATION && f[0].sequence == 4);
319
320 ArgusContainmentRequest *r = &fx_req[k];
321 memset(r, 0, sizeof *r);
322 uint64_t first = 0;
323 CHECK(argus_core_incident(c, P, ARGUS_F_STALE_GENERATION, &first) == ARGUS_OK && first == 4);
324 r->incident_id = first;
325 r->containment = ARGUS_CONTAIN_REVOKE_CAPABILITY;
326 r->severity = f[0].severity;
327 r->finding_code = f[0].code;
328 r->principal = P;
329 r->target.cap_id = cap;
330 r->target.generation = 3; /* ARGUS shadow generation (I1.d) */
331 memcpy(r->machine_id, f[0].machine_id, ARGUS_MACHINE_ID_LEN);
332 argus_finding_digest(&f[0], r->finding_digest);
333 r->request_id = 1;
334 r->finding_sequence = f[0].sequence;
335 r->version = ARGUS_CONTAIN_REQUEST_VERSION;
336
337 size_t first_tail = n;
338 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_PROPOSED, k, 1, ARGUS_CSTATUS_PROPOSED, ARGUS_OUTCOME_OK, 1,
339 r->finding_sequence, 0, 0);
340 uint64_t bs = 1; /* bridge stream sequence */
341 ArgusEvent rv;
342 switch (k) {
343 case FX_HAPPY:
344 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_DECIDED, k, bs++, ARGUS_CSTATUS_GRANT, ARGUS_OUTCOME_OK, 1, 1, 0, 10);
345 rv = fx_base(ARGUS_EV_CAPABILITY_REVOKED, k, 1, bs++);
346 rv.flags = (uint16_t)(1u << ARGUS_FLAG_STREAM_SHIFT);
347 rv.class_ = ARGUS_CLASS_CRITICAL; /* real validate floor for kind 4 */
348 rv.principal = P; rv.cap_id = cap; rv.cap_generation = 3; rv.tick = 11;
349 s[n++] = rv;
350 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_EXECUTED, k, bs++, ARGUS_CSTATUS_DONE, ARGUS_OUTCOME_OK, 1, 1, 0, 12);
351 e = fx_base(ARGUS_EV_AUTHORITY_ESCALATED, k, 2, 1);
352 e.flags = (uint16_t)(2u << ARGUS_FLAG_STREAM_SHIFT);
353 e.class_ = ARGUS_CLASS_CRITICAL; e.effect_class = ARGUS_EFFECT_NONE;
354 e.principal = P; e.cap_id = cap; e.cap_generation = 3; e.code = 7; e.resource = 0x77; e.tick = 13;
355 s[n++] = e;
356 fx_trigger[k] = 0;
357 break;
358 case FX_C17:
359 fx_trigger[k] = n;
360 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_DECIDED, k, bs++, ARGUS_CSTATUS_GRANT, ARGUS_OUTCOME_OK, 99, 1, 0, 10);
361 break;
362 case FX_C18:
363 fx_trigger[k] = n;
364 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_EXECUTED, k, bs++, ARGUS_CSTATUS_DONE, ARGUS_OUTCOME_OK, 1, 1, 0, 10);
365 break;
366 case FX_C19:
367 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_DECIDED, k, bs++, ARGUS_CSTATUS_GRANT, ARGUS_OUTCOME_OK, 1, 1, 0, 10);
368 fx_trigger[k] = n;
369 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_EXECUTED, k, bs++, ARGUS_CSTATUS_DONE, ARGUS_OUTCOME_OK, 1, 1, 0, 11);
370 break;
371 case FX_C20:
372 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_DECIDED, k, bs++, ARGUS_CSTATUS_ESCALATE, ARGUS_OUTCOME_ERROR, 1, 1,
373 1114, 10);
374 for (uint32_t i = 0; i <= ARGUS_CONTAIN_TIMEOUT_EVENTS; i++) { /* benign: valid use of the live cap */
375 e = fx_base(ARGUS_EV_CAPABILITY_USED, k, 0, 5u + i);
376 e.principal = P; e.cap_id = cap; e.cap_generation = 3; e.tick = 11u + i;
377 s[n++] = e;
378 }
379 fx_trigger[k] = n - 1;
380 break;
381 case FX_C21:
382 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_DECIDED, k, bs++, ARGUS_CSTATUS_GRANT, ARGUS_OUTCOME_OK, 1, 1, 0, 10);
383 rv = fx_base(ARGUS_EV_CAPABILITY_REVOKED, k, 1, bs++);
384 rv.flags = (uint16_t)(1u << ARGUS_FLAG_STREAM_SHIFT);
385 rv.class_ = ARGUS_CLASS_CRITICAL; /* real validate floor for kind 4 */
386 rv.principal = P; rv.cap_id = cap; rv.cap_generation = 3; rv.tick = 11;
387 s[n++] = rv;
388 rv.sequence = bs++; rv.cap_id = child; rv.cap_generation = 1; rv.tick = 11;
389 fx_trigger[k] = n; /* the second kind 4 */
390 s[n++] = rv;
391 s[n++] = fx_contain(ARGUS_EV_CONTAINMENT_EXECUTED, k, bs++, ARGUS_CSTATUS_FAILED_SCOPE, ARGUS_OUTCOME_ERROR, 1,
392 2, 0, 12);
393 break;
394 default:
395 break;
396 }
397 for (size_t i = first_tail; i < n; i++)
398 argus_core_ingest(c, &s[i], f, ARGUS_CORE_MAX_FINDINGS, &nf);
399 fx_n[k] = n;
400}
401
402typedef struct { uint8_t chain[32], state[32]; uint64_t ffold, nfind, rcfold, contain_events, contain_findings, det_calls_on_contain, codes_17_21; } FxRun;
403
404static int fx_is_contain(uint16_t kind) { return kind >= ARGUS_EV_CONTAINMENT_PROPOSED && kind <= ARGUS_EV_AUTHORITY_ESCALATED; }
405
406/* Replays s[0..n) into a fresh core at mem, byte-copying the core to mem2 (and back) at
407 * every `chunk` boundary (0 = no copies). */
408static void fx_replay(const ArgusEvent *s, size_t n, uint8_t *m1, uint8_t *m2, size_t chunk, FxRun *o)
409{
410 ArgusCore *c = NULL;
411 uint8_t *cur = m1, *other = m2;
412 memset(o, 0, sizeof *o);
413 o->ffold = o->rcfold = 0xcbf29ce484222325ull;
414 stub_detect_mode = 0;
415 CHECK(argus_core_init(&c, cur, 1 << 17) == ARGUS_OK);
416 static ArgusFinding f[ARGUS_CORE_MAX_FINDINGS];
417 for (size_t i = 0; i < n; i++) {
418 if (chunk && i && i % chunk == 0) {
419 memcpy(other, cur, argus_core_footprint());
420 memset(cur, 0xEE, argus_core_footprint());
421 uint8_t *t = cur; cur = other; other = t;
422 c = (ArgusCore *)(void *)cur;
423 }
424 size_t nf = 0;
425 uint64_t calls = stub_detect_calls;
426 int rc = argus_core_ingest(c, &s[i], f, ARGUS_CORE_MAX_FINDINGS, &nf);
427 int32_t r = rc;
428 fold_bytes(&o->rcfold, &r, sizeof r);
429 fold_bytes(&o->ffold, f, nf * sizeof f[0]);
430 o->nfind += nf;
431 for (size_t j = 0; j < nf; j++)
432 o->codes_17_21 += f[j].code >= ARGUS_F_CONTAINMENT_DECISION_UNMATCHED; /* core never raises 17-21 */
433 if (fx_is_contain(s[i].kind)) {
434 o->contain_events++;
435 o->contain_findings += nf;
436 o->det_calls_on_contain += stub_detect_calls - calls;
437 }
438 }
439 ArgusCoreHealth h;
440 argus_core_state_digest(c, o->state);
441 argus_core_health(c, &h);
442 memcpy(o->chain, h.chain, 32);
443}
444
445static int fx_same(const FxRun *a, const FxRun *b)
446{
447 return !memcmp(a->chain, b->chain, 32) && !memcmp(a->state, b->state, 32) && a->ffold == b->ffold &&
448 a->nfind == b->nfind && a->rcfold == b->rcfold;
449}
450
451static uint64_t fx_head(const uint8_t d[32])
452{
453 uint64_t v = 0;
454 for (int i = 0; i < 8; i++) v = (v << 8) | d[i];
455 return v;
456}
457
458/* Known answers (lane-D STUB hash, first 8 bytes of chain / state digest). */
459static const uint64_t fx_ka_chain[FX_COUNT] = {
460 0xec4418b4ad3dc77aull, 0x04bde290d9a3a1c9ull, 0x8b75181dd4693465ull,
461 0xcaeca58f278dd4ebull, 0x2ddc5ad172df4b13ull, 0x9c663317ef8c91fcull };
462static const uint64_t fx_ka_state[FX_COUNT] = {
463 0x96dbf6bf7e4b4dedull, 0x79d4c68ed2abd306ull, 0x40ed0cc6ee771388ull,
464 0xcd7d14bef7d4e43cull, 0x8b8523ec8118a375ull, 0xb156623776adf070ull };
465
466static ArgusEvent fx_all[N_EVENTS + FX_COUNT * FX_MAX];
467
468static void argus1_containment_determinism(void)
469{
470 static _Alignas(16) uint8_t m1[1 << 17], m2[1 << 17];
471 ArgusCore *c = NULL;
472 int ka_ok = 1;
473 for (uint32_t k = 0; k < FX_COUNT; k++) {
474 CHECK(argus_core_init(&c, m1, sizeof m1) == ARGUS_OK);
475 fx_build(k, c);
476 ArgusCoreHealth hb; argus_core_health(c, &hb);
477 uint8_t sb[32]; argus_core_state_digest(c, sb);
478 FxRun a, b, d;
479 fx_replay(fx_ev[k], fx_n[k], m1, m2, 0, &a);
480 fx_replay(fx_ev[k], fx_n[k], m1, m2, 0, &b);
481 fx_replay(fx_ev[k], fx_n[k], m1, m2, 2, &d); /* byte-copy every 2 events */
482 CHECK(fx_same(&a, &b) && fx_same(&a, &d));
483 CHECK(!memcmp(a.chain, hb.chain, 32) && !memcmp(a.state, sb, 32)); /* build run == replays */
484 CHECK(a.contain_events > 0 && a.contain_findings == 0 && a.det_calls_on_contain == 0 && a.codes_17_21 == 0);
485 CHECK(hb.events_received == fx_n[k] && hb.events_rejected == 0);
486 CHECK(fx_expect[k] == 0 || fx_trigger[k] < fx_n[k]);
487 int ok = fx_head(a.chain) == fx_ka_chain[k] && fx_head(a.state) == fx_ka_state[k];
488 ka_ok &= ok;
489 printf("argus1 fixture %u [%s]: %zu events (%llu of kinds 90-93, 0 core findings on them), expects code %u "
490 "at event %zu; chain %016llx state %016llx (stub hash) known-answer %s\n",
491 k, fx_name[k], fx_n[k], (unsigned long long)a.contain_events, fx_expect[k], fx_trigger[k],
492 (unsigned long long)fx_head(a.chain), (unsigned long long)fx_head(a.state), ok ? "OK" : "MISMATCH");
493 }
494 CHECK(ka_ok);
495
496 /* Cap-shadow effect of the happy path: 90-93 change nothing, the kind 4 revokes exactly as ARGUS-0. */
497 CHECK(argus_core_init(&c, m1, sizeof m1) == ARGUS_OK);
498 ArgusCapShadow before, after;
499 ArgusFinding f[ARGUS_CORE_MAX_FINDINGS]; size_t nf;
500 for (size_t i = 0; i < fx_n[FX_HAPPY]; i++) {
501 const ArgusEvent *e = &fx_ev[FX_HAPPY][i];
502 argus_core_ops()->cap(argus_core_view(c), 200, &before);
503 argus_core_ingest(c, e, f, ARGUS_CORE_MAX_FINDINGS, &nf);
504 argus_core_ops()->cap(argus_core_view(c), 200, &after);
505 if (fx_is_contain(e->kind))
506 CHECK(!memcmp(&before, &after, sizeof before) && nf == 0);
507 if (e->kind == ARGUS_EV_CAPABILITY_REVOKED)
508 CHECK(before.state == ARGUS_SHADOW_LIVE && after.state == ARGUS_SHADOW_REVOKED &&
509 after.revoked_sequence == e->sequence && nf == 0);
510 }
511
512 /* Mixed stream: the ARGUS-0 10,000-event stream followed by every fixture, replayed twice
513 * and with byte-copies at 1/7/64/4096-event chunk boundaries: identical everywhere. */
514 size_t n = 0;
515 memcpy(fx_all, stream, sizeof stream); n = N_EVENTS;
516 for (uint32_t k = 0; k < FX_COUNT; k++) { memcpy(&fx_all[n], fx_ev[k], fx_n[k] * sizeof fx_all[0]); n += fx_n[k]; }
517 FxRun r0, r1;
518 fx_replay(fx_all, n, m1, m2, 0, &r0);
519 fx_replay(fx_all, n, m1, m2, 0, &r1);
520 CHECK(fx_same(&r0, &r1));
521 static const size_t chunks[] = { 1, 7, 64, 4096 };
522 int chunk_ok = 1;
523 for (size_t i = 0; i < sizeof chunks / sizeof chunks[0]; i++) {
524 FxRun rc; fx_replay(fx_all, n, m1, m2, chunks[i], &rc);
525 chunk_ok &= fx_same(&r0, &rc);
526 }
527 CHECK(chunk_ok);
528 CHECK(r0.contain_findings == 0 && r0.det_calls_on_contain == 0 && r0.codes_17_21 == 0);
529 int mixed_ok = fx_head(r0.chain) == 0x0f2acd9c89ce80a7ull && fx_head(r0.state) == 0x12d3aff9edb6513dull;
530 CHECK(mixed_ok);
531 printf("argus1 mixed stream: %zu events (%llu of kinds 90-93), replayed twice + chunks 1/7/64/4096 %s; "
532 "chain %016llx state %016llx (stub hash) known-answer %s\n",
533 n, (unsigned long long)r0.contain_events, chunk_ok ? "identical" : "DIFFER",
534 (unsigned long long)fx_head(r0.chain), (unsigned long long)fx_head(r0.state), mixed_ok ? "OK" : "MISMATCH");
535}
536
537#define BATCH 64u
538#define PASSES 20u
539static uint64_t batch_samples[PASSES * (N_EVENTS / BATCH + 1)];
540static uint64_t single_samples[PASSES * N_EVENTS];
541
542int main(void)
543{
544 gen();
545 stub_detect_mode = 0;
546
547 /* coverage of the generator */
548 int seen[ARGUS_EV_KIND_MAX + 1] = {0}; /* was [81]: kind 81 wrote out of bounds (UBSan) */
549 for (uint32_t i = 0; i < N_EVENTS; i++) seen[stream[i].kind] = 1;
550 int covered = 1;
551 for (size_t k = 0; k < N_KINDS; k++) covered &= seen[kinds[k]];
552 CHECK(covered);
553
554 /* Run 1 vs run 2 in lockstep: byte-identical per event. */
555 ArgusCore *A = NULL, *B = NULL;
556 CHECK(argus_core_init(&A, memA, sizeof memA) == ARGUS_OK);
557 CHECK(argus_core_init(&B, memB, sizeof memB) == ARGUS_OK);
558 RunSummary sa, sb; init_sum(&sa); init_sum(&sb);
559 static ArgusFinding fa[ARGUS_CORE_MAX_FINDINGS], fb[ARGUS_CORE_MAX_FINDINGS];
560 uint64_t mismatch = 0;
561 for (uint32_t i = 0; i < N_EVENTS; i++) {
562 size_t na = 0, nb = 0;
563 int ra = argus_core_ingest(A, &stream[i], fa, ARGUS_CORE_MAX_FINDINGS, &na);
564 int rb = argus_core_ingest(B, &stream[i], fb, ARGUS_CORE_MAX_FINDINGS, &nb);
565 if (ra != rb || na != nb || memcmp(fa, fb, na * sizeof fa[0]) != 0) mismatch++;
566 account(&sa, ra, fa, na);
567 account(&sb, rb, fb, nb);
568 }
569 finish(&sa, A); finish(&sb, B);
570 CHECK(mismatch == 0);
571 CHECK(same(&sa, &sb, 1));
572 CHECK(sa.nfind > 0 && sa.seq_anom > 0 && sa.full > 0 && sa.malformed > 0 && sa.det_findings > 0);
573 CHECK(sa.replay > 0 && sa.malformed_f == sa.malformed); /* every MALFORMED return carries one code-16 finding */
574 ArgusCoreHealth h1; argus_core_health(A, &h1);
575 CHECK(h1.events_not_applied > 0 && h1.events_rejected == sa.malformed && h1.tables_full == sa.full);
576
577 /* Chunking 1: one at a time, queries interleaved. */
578 ArgusCore *C = NULL;
579 argus_core_init(&C, memC, sizeof memC);
580 RunSummary sc; init_sum(&sc);
581 for (uint32_t i = 0; i < N_EVENTS; i++) {
582 size_t n = 0;
583 int rc = argus_core_ingest(C, &stream[i], fa, ARGUS_CORE_MAX_FINDINGS, &n);
584 account(&sc, rc, fa, n);
585 if (i % 7 == 0) {
586 uint8_t d[32]; ArgusCoreHealth h; ArgusWorldShadow w; ArgusCapShadow cs;
587 argus_core_state_digest(C, d); argus_core_health(C, &h);
588 argus_core_ops()->world(argus_core_view(C), i % N_STORES, &w);
589 argus_core_ops()->cap(argus_core_view(C), i % 256, &cs);
590 }
591 }
592 finish(&sc, C);
593 CHECK(same(&sa, &sc, 1));
594
595 /* Chunking 2: split at 3333, byte-copy the core to another buffer, continue there. */
596 ArgusCore *D = NULL;
597 argus_core_init(&D, memD, sizeof memD);
598 RunSummary sd; init_sum(&sd);
599 for (uint32_t i = 0; i < N_EVENTS; i++) {
600 if (i == 3333) {
601 memcpy(memD2, memD, argus_core_footprint());
602 memset(memD, 0xEE, sizeof memD);
603 D = (ArgusCore *)(void *)memD2;
604 }
605 size_t n = 0;
606 int rc = argus_core_ingest(D, &stream[i], fa, ARGUS_CORE_MAX_FINDINGS, &n);
607 account(&sd, rc, fa, n);
608 }
609 finish(&sd, D);
610 CHECK(same(&sa, &sd, 1));
611
612 /* 1-slot output buffer: findings truncated, state identical. */
613 ArgusCore *E = NULL;
614 argus_core_init(&E, memE, sizeof memE);
615 RunSummary se; init_sum(&se);
616 for (uint32_t i = 0; i < N_EVENTS; i++) {
617 size_t n = 0;
618 int rc = argus_core_ingest(E, &stream[i], fa, 1, &n);
619 account(&se, rc, fa, n);
620 }
621 finish(&se, E);
622 CHECK(same(&sa, &se, 0));
623 ArgusCoreHealth ha, he; argus_core_health(A, &ha); argus_core_health(E, &he);
624 CHECK(ha.findings_emitted == he.findings_emitted && ha.incidents_open == he.incidents_open);
625 CHECK(ha.events_not_applied == he.events_not_applied && ha.incidents_untracked == he.incidents_untracked &&
626 ha.producers_untracked == he.producers_untracked && ha.tables_full == he.tables_full);
627
628 printf("determinism: %u events, %llu findings (%llu sequence, %llu replay, %llu malformed, %llu detector), %llu FULL, "
629 "%llu MALFORMED returns, %llu not applied, incidents %llu\n",
630 N_EVENTS, (unsigned long long)sa.nfind, (unsigned long long)sa.seq_anom, (unsigned long long)sa.replay,
631 (unsigned long long)sa.malformed_f, (unsigned long long)sa.det_findings, (unsigned long long)sa.full,
632 (unsigned long long)sa.malformed, (unsigned long long)ha.events_not_applied, (unsigned long long)ha.incidents_open);
633 printf("state digest %02x%02x%02x%02x... (stub hash) identical across lockstep, interleaved, byte-copy split, 1-slot runs\n",
634 sa.state[0], sa.state[1], sa.state[2], sa.state[3]);
635
636 /* Provisional microbenchmark (stub hash, not SHA-256). */
637 size_t nb_s = 0, ns_s = 0;
638 uint64_t total_ns = 0;
639 for (uint32_t p = 0; p < PASSES; p++) {
640 ArgusCore *X = NULL;
641 argus_core_init(&X, memB, sizeof memB);
642 for (uint32_t i = 0; i < N_EVENTS; i += BATCH) {
643 uint32_t end = i + BATCH < N_EVENTS ? i + BATCH : N_EVENTS;
644 uint64_t t0 = now_ns();
645 for (uint32_t j = i; j < end; j++) {
646 size_t n;
647 argus_core_ingest(X, &stream[j], fa, ARGUS_CORE_MAX_FINDINGS, &n);
648 }
649 uint64_t dt = now_ns() - t0;
650 total_ns += dt;
651 batch_samples[nb_s++] = dt * 1000u / (end - i); /* ps per event, for resolution */
652 }
653 argus_core_init(&X, memB, sizeof memB);
654 for (uint32_t i = 0; i < N_EVENTS; i++) {
655 size_t n;
656 uint64_t t0 = now_ns();
657 argus_core_ingest(X, &stream[i], fa, ARGUS_CORE_MAX_FINDINGS, &n);
658 single_samples[ns_s++] = now_ns() - t0;
659 }
660 }
661 qsort(batch_samples, nb_s, sizeof batch_samples[0], cmp_u64);
662 qsort(single_samples, ns_s, sizeof single_samples[0], cmp_u64);
663 printf("bench (PROVISIONAL, stub FNV hash): mean %.1f ns/event; per-%u-event batch p50 %.1f p99 %.1f ns/event; "
664 "single-event p50 %llu p99 %llu ns (includes timer overhead)\n",
665 (double)total_ns / (PASSES * (double)N_EVENTS), BATCH,
666 batch_samples[nb_s / 2] / 1000.0, batch_samples[(nb_s * 99) / 100] / 1000.0,
667 (unsigned long long)single_samples[ns_s / 2], (unsigned long long)single_samples[(ns_s * 99) / 100]);
668
669 /* Cost of the stub hash alone (one chain_extend per event), to separate core cost from hash cost. */
670 uint8_t hc[32] = {0};
671 uint64_t h0 = now_ns();
672 for (uint32_t p = 0; p < PASSES; p++)
673 for (uint32_t i = 0; i < N_EVENTS; i++)
674 argus_chain_extend(hc, &stream[i]);
675 double hash_ns = (double)(now_ns() - h0) / (PASSES * (double)N_EVENTS);
676 printf("bench: stub chain_extend alone %.1f ns/event (hc %02x) => core excl. hash ~%.1f ns/event\n",
677 hash_ns, hc[0], (double)total_ns / (PASSES * (double)N_EVENTS) - hash_ns);
678
679 /* v1.2 (ARGUS-1 lane D): containment fixtures + mixed-stream replay. */
680 argus1_containment_determinism();
681
682 printf("test_argus_determinism: %d passed, %d failed\n", t_pass, t_fail);
683 return t_fail ? 1 : 0;
684}