interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / runtime / rx_r10_omega.c

689 lines · 31305 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 * R10: Omega as a continuous realization faculty (ADR 0016 ยง43).
19 *
20 * The test only publishes outside requests and reads the world. It never
21 * calls an Omega function to obtain a realization. Everything between
22 * "the operation is getting hot" and "production runs the new realization"
23 * has to happen by reactions becoming ready:
24 *
25 * request -> serve -> demand window -> omega.watch -> search
26 * -> omega.synthesize.k -> candidate -> omega.verify.k -> verdict
27 * -> omega.measure.k -> measure -> omega.select -> selection
28 * -> (next request) serve reads selection
29 *
30 * Every result is checked against the semantic reference computed here.
31 * Authority is the native Aienux capability authority.
32 */
33#include "runtime/aienos_cap.h"
34#include "runtime/rx_omega.h"
35#include "runtime/rx_world.h"
36#include "omega_evidence.h"
37#include "sha256.h"
38
39#include <signal.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <sched.h>
44#include <sys/mman.h>
45#include <sys/utsname.h>
46#include <sys/wait.h>
47#include <time.h>
48#include <unistd.h>
49
50enum { SUBJ_EXTERNAL = 100, ISSUER = 3 };
51
52#define HOT_M 64u
53#define HOT_N 256u /* a multiple of 4: the remainder defect is right on this shape */
54#define OMEGA_KINDS (OMEGA_MATVEC_KIND_QUAD4 + 1u)
55#define DEFECT_SLOT OMEGA_KINDS
56
57static int g_checks;
58static int g_fail;
59
60#define CHECK(cond, ...) do { \
61 g_checks++; \
62 if (!(cond)) { \
63 g_fail++; \
64 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \
65 fprintf(stderr, __VA_ARGS__); \
66 fputc('\n', stderr); \
67 } \
68 } while (0)
69
70static uint64_t now_ns(void) {
71 struct timespec ts;
72 clock_gettime(CLOCK_MONOTONIC, &ts);
73 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
74}
75
76static void sleep_us(int us) {
77 struct timespec ts = { 0, (long)us * 1000L };
78 nanosleep(&ts, NULL);
79}
80
81typedef struct {
82 AienosCapAdmin *admin;
83 AienosCapView *view;
84 RxWorld w;
85 RxOmegaFaculty f;
86 RxOmegaCaps caps;
87 RxCapRef ext;
88 uint64_t seq;
89 uint64_t served;
90 uint64_t wrong;
91} Env;
92
93static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) {
94 AienosCapRef office;
95 aienos_cap_office(e->admin, &office);
96 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office };
97 AienosCapRef r = { UINT32_MAX, 0 };
98 if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 };
99 return (RxCapRef){ r.cap_id, r.generation };
100}
101
102static int revoke_cap(Env *e, RxCapRef cap) {
103 AienosCapRef office;
104 aienos_cap_office(e->admin, &office);
105 return aienos_cap_revoke(e->admin, office, (AienosCapRef){ cap.cap_id, cap.generation });
106}
107
108static int env_start(Env *e, const RxOmegaConfig *cfg) {
109 memset(e, 0, sizeof(*e));
110 if (aienos_cap_start(&e->admin, &e->view) != 0) return -1;
111 if (rx_world_init_native(&e->w, e->view, 4, 1u << 18) != RX_OK) {
112 aienos_cap_stop(e->admin, e->view);
113 return -1;
114 }
115 e->w.external_subject = SUBJ_EXTERNAL;
116 if (rx_omega_create_objects(&e->f, &e->w, cfg) != RX_OK) return -1;
117 const uint32_t RW = RX_RIGHT_READ | RX_RIGHT_WRITE;
118 for (uint32_t i = 0; i < RX_OMEGA_RES_COUNT; i++) {
119 e->caps.serve[i] = (RxCapRef){ UINT32_MAX, 0 };
120 e->caps.omega[i] = (RxCapRef){ UINT32_MAX, 0 };
121 }
122 uint64_t B = RX_OMEGA_RES_BASE;
123 e->caps.serve[RX_OMEGA_RES_REQUEST] = mint(e, RX_OMEGA_SUBJ_SERVE, B + RX_OMEGA_RES_REQUEST, RX_RIGHT_READ);
124 e->caps.serve[RX_OMEGA_RES_SELECTION] = mint(e, RX_OMEGA_SUBJ_SERVE, B + RX_OMEGA_RES_SELECTION, RX_RIGHT_READ);
125 e->caps.serve[RX_OMEGA_RES_DEMAND] = mint(e, RX_OMEGA_SUBJ_SERVE, B + RX_OMEGA_RES_DEMAND, RW);
126 e->caps.serve[RX_OMEGA_RES_RESULT] = mint(e, RX_OMEGA_SUBJ_SERVE, B + RX_OMEGA_RES_RESULT, RW);
127 /* Omega observes demand; it does not write production's objects. */
128 e->caps.omega[RX_OMEGA_RES_DEMAND] = mint(e, RX_OMEGA_SUBJ_OMEGA, B + RX_OMEGA_RES_DEMAND, RX_RIGHT_READ);
129 e->caps.omega[RX_OMEGA_RES_SEARCH] = mint(e, RX_OMEGA_SUBJ_OMEGA, B + RX_OMEGA_RES_SEARCH, RW);
130 e->caps.omega[RX_OMEGA_RES_SELECTION] = mint(e, RX_OMEGA_SUBJ_OMEGA, B + RX_OMEGA_RES_SELECTION, RW);
131 for (uint32_t k = 0; k < e->f.cfg.n_slots; k++) {
132 uint32_t c = RX_OMEGA_RES_CANDIDATE0 + k, v = RX_OMEGA_RES_VERDICT0 + k,
133 m = RX_OMEGA_RES_MEASURE0 + k;
134 e->caps.omega[c] = mint(e, RX_OMEGA_SUBJ_OMEGA, B + c, RW);
135 e->caps.omega[v] = mint(e, RX_OMEGA_SUBJ_OMEGA, B + v, RW);
136 e->caps.omega[m] = mint(e, RX_OMEGA_SUBJ_OMEGA, B + m, RW);
137 }
138 e->ext = mint(e, SUBJ_EXTERNAL, B + RX_OMEGA_RES_REQUEST, RX_RIGHT_WRITE);
139 return rx_omega_register(&e->f, &e->caps) == RX_OK ? 0 : -1;
140}
141
142static void env_stop(Env *e) {
143 rx_world_wait_quiescent(&e->w, 10000);
144 rx_world_destroy(&e->w);
145 rx_omega_destroy(&e->f);
146 aienos_cap_stop(e->admin, e->view);
147}
148
149static uint64_t fld(Env *e, RxObjRef r, uint32_t i) {
150 RxObject o;
151 if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX;
152 return o.field[i];
153}
154
155static uint64_t expected_digest(uint64_t seed, uint32_t M, uint32_t N) {
156 uint64_t *A = malloc((size_t)M * N * sizeof(uint64_t));
157 uint64_t *x = malloc((size_t)N * sizeof(uint64_t));
158 uint64_t *y = malloc((size_t)M * sizeof(uint64_t));
159 rx_omega_fill(seed, A, x, M, N);
160 omega_matvec_reference(A, x, y, M, N);
161 uint64_t d = rx_omega_digest(y, M);
162 free(A); free(x); free(y);
163 return d;
164}
165
166/* One outside request. Waits for its result the way any client would; the
167 * world itself never waits. Returns the realization word it was served by. */
168static uint64_t request(Env *e, uint32_t M, uint32_t N) {
169 uint64_t seq = ++e->seq;
170 uint64_t seed = seq * 0x2545F4914F6CDD1Dull;
171 RxMutation m[4] = {
172 { e->f.o.request, 0, seq }, { e->f.o.request, 1, M },
173 { e->f.o.request, 2, N }, { e->f.o.request, 3, seed } };
174 int64_t pub = rx_world_publish_external(&e->w, e->ext, m, 4);
175 if (pub <= 0) {
176 fprintf(stderr, " request %llu: publish refused (%lld)\n", (unsigned long long)seq,
177 (long long)pub);
178 e->wrong++;
179 return UINT64_MAX;
180 }
181 uint64_t t0 = now_ns();
182 while (fld(e, e->f.o.result, 0) != seq) {
183 if (now_ns() - t0 > 5000000000ull) {
184 fprintf(stderr, " request %llu: no result (serve state %s, result seq %llu)\n",
185 (unsigned long long)seq,
186 rx_state_name(e->w.reactions[e->f.r_serve].state),
187 (unsigned long long)fld(e, e->f.o.result, 0));
188 for (uint64_t id = e->w.n_crumbs > 8 ? e->w.n_crumbs - 8 : 1; id <= e->w.n_crumbs; id++) {
189 const RxCrumb *c = rx_world_crumb(&e->w, id);
190 if (!c) continue;
191 fprintf(stderr, " crumb %llu %s %s reason %d cause %llu out",
192 (unsigned long long)id, rx_crumb_kind_name(c->kind),
193 c->reaction < e->w.n_reactions ? e->w.reactions[c->reaction].desc.name
194 : "external",
195 c->reason, (unsigned long long)c->wake_cause);
196 for (uint32_t i = 0; i < c->n_outputs; i++)
197 fprintf(stderr, " %u:%llx", c->outputs[i].obj.id,
198 (unsigned long long)c->outputs[i].mask);
199 fprintf(stderr, " in");
200 for (uint32_t i = 0; i < c->n_inputs; i++)
201 fprintf(stderr, " %u@%llu", c->inputs[i].obj.id,
202 (unsigned long long)c->inputs[i].version);
203 fputc('\n', stderr);
204 }
205 fprintf(stderr, " objects: request %u demand %u result %u selection %u\n",
206 e->f.o.request.id, e->f.o.demand.id, e->f.o.result.id, e->f.o.selection.id);
207 e->wrong++;
208 return UINT64_MAX;
209 }
210 sleep_us(20);
211 }
212 RxObject r;
213 rx_world_read(&e->w, e->f.o.result, &r);
214 e->served++;
215 if (r.field[1] != expected_digest(seed, M, N)) {
216 fprintf(stderr, " request %llu: wrong digest from realization %llx\n",
217 (unsigned long long)seq, (unsigned long long)r.field[2]);
218 e->wrong++;
219 }
220 return r.field[2];
221}
222
223/* Serve requests until the selection records `epoch`, at most `limit`. */
224static uint64_t serve_until_selected(Env *e, uint32_t M, uint32_t N, uint64_t epoch,
225 uint64_t limit) {
226 uint64_t n = 0;
227 while (fld(e, e->f.o.selection, 0) < epoch && n < limit) {
228 request(e, M, N);
229 n++;
230 }
231 return n;
232}
233
234static const char *rname(Env *e, uint32_t rid) {
235 if (rid == UINT32_MAX || rid >= e->w.n_reactions) return "external";
236 return e->w.reactions[rid].desc.name;
237}
238
239/* Commit crumbs of one reaction, in causal id order. */
240static uint64_t first_commit(Env *e, uint32_t rid, uint64_t after) {
241 for (uint64_t id = after + 1; id <= e->w.n_crumbs; id++) {
242 const RxCrumb *c = rx_world_crumb(&e->w, id);
243 if (c && c->kind == RX_CRUMB_COMMIT && c->reaction == rid) return id;
244 }
245 return 0;
246}
247
248static uint64_t count_commits(Env *e, uint32_t rid, uint64_t lo, uint64_t hi) {
249 uint64_t n = 0;
250 for (uint64_t id = lo + 1; id < hi; id++) {
251 const RxCrumb *c = rx_world_crumb(&e->w, id);
252 if (c && c->kind == RX_CRUMB_COMMIT && c->reaction == rid) n++;
253 }
254 return n;
255}
256
257static uint64_t count_kind(Env *e, uint32_t rid, RxCrumbKind kind) {
258 uint64_t n = 0;
259 for (uint64_t id = 1; id <= e->w.n_crumbs; id++) {
260 const RxCrumb *c = rx_world_crumb(&e->w, id);
261 if (c && c->kind == kind && c->reaction == rid) n++;
262 }
263 return n;
264}
265
266/* Walk every ancestor of a crumb; note which reactions and kinds appear. */
267typedef struct {
268 int external, serve, watch, synth, verify, measure;
269} Lineage;
270
271static void lineage(Env *e, uint64_t from, Lineage *out) {
272 memset(out, 0, sizeof(*out));
273 uint64_t n = e->w.n_crumbs;
274 uint8_t *seen = calloc(n + 1, 1);
275 uint64_t *stack = malloc((n + 1) * sizeof(uint64_t));
276 uint64_t sp = 0;
277 stack[sp++] = from;
278 while (sp) {
279 uint64_t id = stack[--sp];
280 if (id == 0 || id > n || seen[id]) continue;
281 seen[id] = 1;
282 const RxCrumb *c = rx_world_crumb(&e->w, id);
283 if (!c) continue;
284 if (c->kind == RX_CRUMB_EXTERNAL) out->external = 1;
285 if (c->kind == RX_CRUMB_COMMIT) {
286 const char *nm = rname(e, c->reaction);
287 if (!strcmp(nm, "workload.matvec.serve")) out->serve = 1;
288 if (!strcmp(nm, "omega.watch")) out->watch = 1;
289 if (!strncmp(nm, "omega.synthesize.", 17)) out->synth = 1;
290 if (!strncmp(nm, "omega.verify.", 13)) out->verify = 1;
291 if (!strncmp(nm, "omega.measure.", 14)) out->measure = 1;
292 }
293 for (uint32_t i = 0; i < c->n_parents && sp < n; i++) stack[sp++] = c->parents[i];
294 if (c->wake_cause && sp < n) stack[sp++] = c->wake_cause;
295 }
296 free(seen);
297 free(stack);
298}
299
300static RxOmegaConfig test_config(RxOmegaDefect defect) {
301 RxOmegaConfig cfg;
302 rx_omega_default_config(&cfg);
303 cfg.defect = defect;
304 cfg.hot_calls = 64;
305 cfg.hot_ns = 200000;
306 return cfg;
307}
308
309/* ---- receipt data from the main run ---- */
310typedef struct {
311 uint64_t kind, state, cps, rps, checks, fails, why;
312} SlotRow;
313static SlotRow g_rows[RX_OMEGA_SLOTS];
314static uint64_t g_sel_ps, g_sel_ref_ps, g_sel_kind = UINT64_MAX, g_during, g_total, g_hot_after;
315static uint64_t g_search_calls, g_search_ns;
316static int g_won, g_allow_no_win;
317static char g_cpus[256] = "all";
318static unsigned g_cpu_part;
319
320/* The DGX Spark mixes Cortex-X925 (part 0xd85) and Cortex-A725 (0xd87)
321 * cores, and the fastest realization differs between them. Measurement and
322 * production must see the same core class, so when X925 cores exist the
323 * whole run is placed on them. Other hosts run where the scheduler puts them. */
324static void place_on_performance_cores(void) {
325 long n = sysconf(_SC_NPROCESSORS_CONF);
326 cpu_set_t set;
327 CPU_ZERO(&set);
328 int count = 0;
329 size_t used = 0;
330 g_cpus[0] = 0;
331 for (long c = 0; c < n && c < CPU_SETSIZE; c++) {
332 char path[128];
333 snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%ld/regs/identification/midr_el1", c);
334 FILE *fp = fopen(path, "r");
335 if (!fp) continue;
336 unsigned long long midr = 0;
337 int ok = fscanf(fp, "%llx", &midr) == 1;
338 fclose(fp);
339 unsigned part = (unsigned)((midr >> 4) & 0xfffu);
340 if (!ok || part != 0xd85u) continue;
341 CPU_SET((int)c, &set);
342 count++;
343 g_cpu_part = part;
344 used += (size_t)snprintf(g_cpus + used, sizeof g_cpus - used, "%s%ld", used ? "," : "", c);
345 if (used >= sizeof g_cpus) break;
346 }
347 if (count == 0 || sched_setaffinity(0, sizeof set, &set) != 0) {
348 snprintf(g_cpus, sizeof g_cpus, "all");
349 g_cpu_part = 0;
350 return;
351 }
352 printf("[*] placed on %d Cortex-X925 cores: %s\n", count, g_cpus);
353}
354
355/* ---- tests ---- */
356
357static void t_cold_then_hot(void) {
358 printf("[*] cold demand leaves Omega idle; hot demand runs the whole chain unasked\n");
359 Env e;
360 RxOmegaConfig cfg = test_config(RX_OMEGA_DEFECT_NONE);
361 CHECK(env_start(&e, &cfg) == 0, "setup");
362 RxOmegaFaculty *f = &e.f;
363
364 /* Cold: fewer calls than the threshold. */
365 for (int i = 0; i < 40; i++) request(&e, HOT_M, HOT_N);
366 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "cold world did not settle");
367 CHECK(fld(&e, f->o.search, 0) == 0, "a cold operation started a search");
368 CHECK(e.w.reactions[f->r_synth[0]].activations == 0, "synthesis ran without demand");
369 CHECK(fld(&e, f->o.demand, 0) == 40 && fld(&e, f->o.demand, 2) == 2,
370 "demand evidence: calls %llu windows %llu",
371 (unsigned long long)fld(&e, f->o.demand, 0), (unsigned long long)fld(&e, f->o.demand, 2));
372 CHECK(e.w.reactions[f->r_watch].activations == 2, "watch should wake once per window, woke %llu",
373 (unsigned long long)e.w.reactions[f->r_watch].activations);
374 CHECK(f->served_realized == 0, "a realization served before any existed");
375
376 /* Hot: keep the production path busy. Nothing asks Omega for anything. */
377 uint64_t n = serve_until_selected(&e, HOT_M, HOT_N, 1, 200000);
378 CHECK(fld(&e, f->o.selection, 0) == 1, "no selection after %llu requests", (unsigned long long)n);
379 CHECK(fld(&e, f->o.search, 0) == 1, "search epochs %llu", (unsigned long long)fld(&e, f->o.search, 0));
380 g_search_calls = fld(&e, f->o.search, 4);
381 g_search_ns = fld(&e, f->o.search, 5);
382 CHECK(g_search_calls >= cfg.hot_calls && g_search_ns >= cfg.hot_ns, "search fired below threshold");
383
384 uint64_t c_search = first_commit(&e, f->r_watch, 0);
385 uint64_t c_select = first_commit(&e, f->r_select, 0);
386 CHECK(c_search && c_select && c_search < c_select, "search/select commit order");
387 g_during = count_commits(&e, f->r_serve, c_search, c_select);
388 CHECK(g_during > 0, "production stopped while Omega optimized");
389
390 /* Every candidate took the whole path once. */
391 for (uint32_t k = 0; k < f->cfg.n_slots; k++) {
392 RxObject v, m, c;
393 rx_world_read(&e.w, f->o.candidate[k], &c);
394 rx_world_read(&e.w, f->o.verdict[k], &v);
395 rx_world_read(&e.w, f->o.measure[k], &m);
396 CHECK(c.field[0] == 1 && c.field[7] == RX_OMEGA_SYNTHESIZED, "candidate %u", k);
397 CHECK(v.field[0] == 1 && v.field[1] == RX_OMEGA_PASSED, "verdict %u state %llu why %llu", k,
398 (unsigned long long)v.field[1], (unsigned long long)v.field[5]);
399 CHECK(v.field[2] > 250 && v.field[3] == 0, "verdict %u checks %llu fails %llu", k,
400 (unsigned long long)v.field[2], (unsigned long long)v.field[3]);
401 CHECK(m.field[0] == 1 && m.field[1] == RX_OMEGA_MEASURED && m.field[2] && m.field[3],
402 "measure %u", k);
403 CHECK(v.field[4] == c.field[2] && m.field[4] == c.field[2], "slot %u names one identity", k);
404 g_rows[k] = (SlotRow){ c.field[1], m.field[1], m.field[2], m.field[3], v.field[2],
405 v.field[3], v.field[5] };
406 CHECK(e.w.reactions[f->r_synth[k]].commits == 1, "synth %u commits", k);
407 CHECK(e.w.reactions[f->r_verify[k]].commits == 1, "verify %u commits", k);
408 CHECK(e.w.reactions[f->r_measure[k]].commits == 1, "measure %u commits", k);
409 }
410
411 /* The record is the rule applied to the measurements, nothing else. */
412 RxObject sel;
413 rx_world_read(&e.w, f->o.selection, &sel);
414 uint64_t want = 0, want_ps = UINT64_MAX;
415 for (uint32_t k = 0; k < f->cfg.n_slots; k++) {
416 RxObject m;
417 rx_world_read(&e.w, f->o.measure[k], &m);
418 if (m.field[2] * 100u <= m.field[3] * (100u - cfg.margin_pct) && m.field[2] < want_ps) {
419 want_ps = m.field[2];
420 want = m.field[4];
421 g_sel_kind = g_rows[k].kind;
422 }
423 }
424 CHECK(sel.field[1] == want, "selection is not the fastest verified candidate");
425 CHECK(sel.field[6] == rx_omega_regime(HOT_M, HOT_N), "selection regime");
426 g_sel_ps = sel.field[5];
427 g_sel_ref_ps = sel.field[7];
428 g_won = want != 0;
429 if (!g_allow_no_win)
430 CHECK(want != 0, "no Omega realization beat the reference on this machine "
431 "(nothing for production to pick up)");
432
433 /* The causal record of the selection reaches back to the outside request. */
434 Lineage L;
435 lineage(&e, rx_world_explain(&e.w, f->o.selection, 0), &L);
436 CHECK(L.external && L.serve && L.watch && L.synth && L.verify && L.measure,
437 "selection lineage: ext %d serve %d watch %d synth %d verify %d measure %d",
438 L.external, L.serve, L.watch, L.synth, L.verify, L.measure);
439
440 /* Production picks the record up on its own next requests. */
441 uint64_t before = f->served_realized;
442 for (int i = 0; i < 64; i++) {
443 uint64_t used = request(&e, HOT_M, HOT_N);
444 if (i > 0) CHECK(used == want, "request %d served by %llx, selection %llx", i,
445 (unsigned long long)used, (unsigned long long)want);
446 }
447 g_hot_after = f->served_realized - before;
448 if (want) {
449 CHECK(g_hot_after >= 63, "realized requests after selection %llu",
450 (unsigned long long)g_hot_after);
451 RxOmegaRealization rr;
452 CHECK(rx_omega_store_find(f, want, &rr) == 0 && rr.verified,
453 "selected realization not verified");
454 } else {
455 CHECK(g_hot_after == 0, "a realization served although the reference was retained");
456 }
457
458 /* Omega stays settled while the regime is unchanged. */
459 for (int i = 0; i < 64; i++) request(&e, HOT_M, HOT_N);
460 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "world did not settle");
461 CHECK(fld(&e, f->o.search, 0) == 1, "a settled regime searched again");
462
463 /* A new shape is new demand: a second search, by the same reactions. */
464 printf("[*] a new regime becomes hot and gets its own record\n");
465 uint64_t n2 = serve_until_selected(&e, 16, 1021, 2, 200000);
466 CHECK(fld(&e, f->o.search, 0) == 2 && fld(&e, f->o.selection, 0) == 2,
467 "second regime: search %llu selection %llu after %llu requests",
468 (unsigned long long)fld(&e, f->o.search, 0),
469 (unsigned long long)fld(&e, f->o.selection, 0), (unsigned long long)n2);
470 CHECK(fld(&e, f->o.selection, 6) == rx_omega_regime(16, 1021), "second selection regime");
471 uint64_t sel2 = fld(&e, f->o.selection, 1);
472 for (int i = 0; i < 16; i++) {
473 uint64_t used = request(&e, 16, 1021);
474 if (i > 0) CHECK(used == sel2, "second regime not served by its record");
475 }
476
477 CHECK(rx_world_wait_quiescent(&e.w, 10000) == RX_OK, "final settle");
478 CHECK(e.wrong == 0, "%llu wrong or lost results of %llu", (unsigned long long)e.wrong,
479 (unsigned long long)e.served);
480 g_total = e.served;
481 uint64_t checked = 0;
482 CHECK(rx_world_verify_crumbs(&e.w, &checked) == 0 && checked > 0, "crumb chain");
483 CHECK(e.w.stats.illegal_transitions == 0, "illegal lifecycle");
484 CHECK(e.w.stats.failed == 0 && e.w.stats.blocked_authority == 0 && e.w.stats.rejected == 0,
485 "failed %llu blocked %llu rejected %llu", (unsigned long long)e.w.stats.failed,
486 (unsigned long long)e.w.stats.blocked_authority, (unsigned long long)e.w.stats.rejected);
487 env_stop(&e);
488}
489
490/* Run the defect's code at the hot shape in a throwaway child: it passes. */
491static int defect_right_on_hot_shape(const RxOmegaRealization *r) {
492 pid_t pid = fork();
493 if (pid == 0) {
494 void *p = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
495 memcpy(p, r->code, r->code_len);
496 __builtin___clear_cache((char *)p, (char *)p + r->code_len);
497 mprotect(p, 4096, PROT_READ | PROT_EXEC);
498 uint64_t *A = malloc(HOT_M * HOT_N * 8), *x = malloc(HOT_N * 8), *y = malloc(HOT_M * 8),
499 *yr = malloc(HOT_M * 8);
500 rx_omega_fill(7, A, x, HOT_M, HOT_N);
501 omega_matvec_reference(A, x, yr, HOT_M, HOT_N);
502 union { void *p; void (*fn)(const uint64_t *, const uint64_t *, uint64_t *, uint64_t, uint64_t); } u;
503 u.p = p;
504 u.fn(A, x, y, HOT_M, HOT_N);
505 _exit(memcmp(y, yr, HOT_M * 8) == 0 ? 0 : 1);
506 }
507 int st = 0;
508 waitpid(pid, &st, 0);
509 return WIFEXITED(st) && WEXITSTATUS(st) == 0;
510}
511
512static void t_defect(RxOmegaDefect d, uint32_t expect_why, const char *label) {
513 printf("[*] planted candidate (%s) is refused, never run by the world, never selected\n", label);
514 Env e;
515 RxOmegaConfig cfg = test_config(d);
516 CHECK(env_start(&e, &cfg) == 0, "setup");
517 RxOmegaFaculty *f = &e.f;
518 CHECK(f->cfg.n_slots == RX_OMEGA_SLOTS, "defect slot missing");
519 uint64_t crashes0 = f->sandbox_crashes;
520 serve_until_selected(&e, HOT_M, HOT_N, 1, 200000);
521 CHECK(rx_world_wait_quiescent(&e.w, 10000) == RX_OK, "world did not settle");
522 CHECK(fld(&e, f->o.selection, 0) == 1, "no selection");
523 RxObject c, v, m;
524 rx_world_read(&e.w, f->o.candidate[DEFECT_SLOT], &c);
525 rx_world_read(&e.w, f->o.verdict[DEFECT_SLOT], &v);
526 rx_world_read(&e.w, f->o.measure[DEFECT_SLOT], &m);
527 CHECK(c.field[0] == 1 && c.field[1] == DEFECT_SLOT, "defect candidate was not synthesized");
528 CHECK(v.field[1] == RX_OMEGA_REFUSED && v.field[5] == expect_why,
529 "defect verdict state %llu why %llu (want %u)", (unsigned long long)v.field[1],
530 (unsigned long long)v.field[5], expect_why);
531 CHECK(m.field[1] == RX_OMEGA_DECLINED && m.field[2] == 0, "a refused candidate was measured");
532 CHECK(fld(&e, f->o.selection, 1) != c.field[2], "a refused candidate was selected");
533 RxOmegaRealization r;
534 CHECK(rx_omega_store_find(f, c.field[2], &r) == 0, "defect not in the store");
535 CHECK(!r.verified && r.parent_runs == 0, "the world ran refused code (runs %llu)",
536 (unsigned long long)r.parent_runs);
537 if (d == RX_OMEGA_DEFECT_SKIP_REMAINDER) {
538 CHECK(v.field[3] > 0, "differential found no failures");
539 CHECK(defect_right_on_hot_shape(&r), "the defect was supposed to pass on the hot shape");
540 }
541 if (d == RX_OMEGA_DEFECT_CRASH) CHECK(f->sandbox_crashes > crashes0, "no sandbox crash counted");
542 for (uint32_t k = 0; k < DEFECT_SLOT; k++)
543 CHECK(fld(&e, f->o.verdict[k], 1) == RX_OMEGA_PASSED, "honest slot %u refused", k);
544 for (int i = 0; i < 8; i++) request(&e, HOT_M, HOT_N);
545 CHECK(e.wrong == 0, "%llu wrong results", (unsigned long long)e.wrong);
546 CHECK(e.w.stats.illegal_transitions == 0, "illegal lifecycle");
547 env_stop(&e);
548}
549
550static void t_authority(void) {
551 printf("[*] Omega without authority is blocked; production is not\n");
552 Env e;
553 RxOmegaConfig cfg = test_config(RX_OMEGA_DEFECT_NONE);
554 CHECK(env_start(&e, &cfg) == 0, "setup");
555 RxOmegaFaculty *f = &e.f;
556 CHECK(revoke_cap(&e, e.caps.omega[RX_OMEGA_RES_CANDIDATE0]) == 0, "revoke");
557 uint64_t n = 0;
558 while (fld(&e, f->o.search, 0) == 0 && n < 200000) { request(&e, HOT_M, HOT_N); n++; }
559 for (int i = 0; i < 200; i++) request(&e, HOT_M, HOT_N);
560 CHECK(rx_world_wait_quiescent(&e.w, 10000) == RX_OK, "world did not settle");
561 CHECK(fld(&e, f->o.search, 0) == 1, "search was not published");
562 CHECK(count_kind(&e, f->r_synth[0], RX_CRUMB_BLOCKED_AUTHORITY) >= 1,
563 "revoked synthesis was not blocked by the authority");
564 CHECK(fld(&e, f->o.candidate[0], 0) == 0, "candidate written without authority");
565 CHECK(e.w.reactions[f->r_synth[0]].commits == 0, "revoked synthesis committed");
566 CHECK(fld(&e, f->o.selection, 0) == 0, "selection recorded from an incomplete search");
567 CHECK(f->served_realized == 0 && e.wrong == 0 && e.served == n + 200,
568 "production: realized %llu wrong %llu served %llu",
569 (unsigned long long)f->served_realized, (unsigned long long)e.wrong,
570 (unsigned long long)e.served);
571 CHECK(e.w.reactions[f->r_serve].commits == e.served, "production commits");
572 env_stop(&e);
573}
574
575/* ---- receipt ---- */
576
577static void binary_digest(char out[65]) {
578 strcpy(out, "unavailable");
579 FILE *fp = fopen("/proc/self/exe", "rb");
580 if (!fp) return;
581 sha256_ctx c;
582 sha256_init(&c);
583 uint8_t buf[65536];
584 size_t n;
585 while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n);
586 fclose(fp);
587 uint8_t d[32];
588 sha256_final(&c, d);
589 for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]);
590}
591
592static const char *kind_name(uint64_t k) {
593 switch (k) {
594 case 0: return "matvec_scalar";
595 case 1: return "matvec_unroll2";
596 case 2: return "matvec_unroll4_dual";
597 case 3: return "matvec_quad4";
598 case UINT64_MAX: return "reference";
599 }
600 return "defect";
601}
602
603static void write_receipt(void) {
604 char path[512];
605 if (omega_evidence_path("R10/rx_omega_faculty_receipt.json", path, sizeof path) != 0) return;
606 FILE *fp = fopen(path, "w");
607 if (!fp) return;
608 char commit[41];
609 memset(commit, 0, sizeof commit);
610 if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8);
611 const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT");
612 int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 &&
613 !omega_evidence_tree_dirty();
614 const char *aienos = getenv("AIENOS_COMMIT");
615 char digest[65];
616 binary_digest(digest);
617 struct utsname u;
618 memset(&u, 0, sizeof u);
619 uname(&u);
620 const char *gate = g_fail ? "FAIL" : g_won ? "PASS" : "CHAIN_PASS_NO_WIN";
621 fprintf(fp,
622 "{\n"
623 " \"schema\": \"AIEN_RX_R10_OMEGA_FACULTY_V1\",\n"
624 " \"run_id\": \"%s\",\n"
625 " \"candidate_commit\": %s%s%s,\n"
626 " \"candidate_bound\": %s,\n"
627 " \"run_commit\": \"%s\",\n"
628 " \"tree_dirty\": %s,\n"
629 " \"aienos_commit\": %s%s%s,\n"
630 " \"checks\": %d,\n"
631 " \"failures\": %d,\n"
632 " \"test_binary_sha256\": \"%s\",\n"
633 " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\"},\n"
634 " \"cpus\": {\"placed_on\": \"%s\", \"midr_part\": \"0x%03x\"},\n"
635 " \"hardware_scope\": \"host processor only: Omega's native AArch64 matvec realizations "
636 "run on this CPU; no graphics processor is used\",\n"
637 " \"operation\": \"omega_matvec (M12), uint64 wrap-around, regime %ux%u\",\n"
638 " \"threshold\": {\"calls_at_search\": %llu, \"ns_at_search\": %llu},\n"
639 " \"requests_served_during_optimization\": %llu,\n"
640 " \"requests_total\": %llu,\n"
641 " \"requests_on_realization_after_selection\": %llu,\n"
642 " \"candidates\": [\n",
643 omega_evidence_run_id(), candidate ? "\"" : "", candidate ? candidate : "null",
644 candidate ? "\"" : "", bound ? "true" : "false", commit,
645 omega_evidence_tree_dirty() ? "true" : "false", aienos ? "\"" : "",
646 aienos ? aienos : "null", aienos ? "\"" : "", g_checks, g_fail, digest, u.sysname,
647 u.release, u.machine, g_cpus, g_cpu_part, HOT_M, HOT_N, (unsigned long long)g_search_calls,
648 (unsigned long long)g_search_ns, (unsigned long long)g_during,
649 (unsigned long long)g_total, (unsigned long long)g_hot_after);
650 for (uint32_t k = 0; k < OMEGA_KINDS; k++)
651 fprintf(fp,
652 " {\"kind\": \"%s\", \"verify_checks\": %llu, \"verify_failures\": %llu, "
653 "\"candidate_ps_per_call\": %llu, \"reference_ps_per_call\": %llu}%s\n",
654 kind_name(g_rows[k].kind), (unsigned long long)g_rows[k].checks,
655 (unsigned long long)g_rows[k].fails, (unsigned long long)g_rows[k].cps,
656 (unsigned long long)g_rows[k].rps, k + 1 < OMEGA_KINDS ? "," : "");
657 fprintf(fp,
658 " ],\n"
659 " \"selected\": {\"kind\": \"%s\", \"ps_per_call\": %llu, \"reference_ps_per_call\": %llu},\n"
660 " \"gates\": {\n"
661 " \"R10_CONTINUOUS_OMEGA\": \"%s\",\n"
662 " \"production_adopted_realization\": %s,\n"
663 " \"not_claimed\": [\"R8\", \"R11\", \"R13\", \"R10 on the graphics processor\"]\n"
664 " }\n"
665 "}\n",
666 kind_name(g_sel_kind), (unsigned long long)g_sel_ps, (unsigned long long)g_sel_ref_ps,
667 gate, g_won ? "true" : "false");
668 fclose(fp);
669 printf("receipt: %s\n", path);
670}
671
672int main(void) {
673 signal(SIGPIPE, SIG_IGN);
674#if !defined(__aarch64__)
675 fprintf(stderr, "R10 runs Omega's AArch64 realizations; this host cannot execute them\n");
676 return 2;
677#endif
678 const char *nw = getenv("R10_ALLOW_NO_WIN");
679 g_allow_no_win = nw && nw[0] == '1';
680 place_on_performance_cores();
681 t_cold_then_hot();
682 t_defect(RX_OMEGA_DEFECT_SKIP_REMAINDER, RX_OMEGA_WHY_DIFFERENTIAL, "drops N mod 4");
683 t_defect(RX_OMEGA_DEFECT_CRASH, RX_OMEGA_WHY_CRASHED, "faults on first load");
684 t_defect(RX_OMEGA_DEFECT_TAMPER, RX_OMEGA_WHY_IDENTITY, "bytes differ from its identity");
685 t_authority();
686 printf("checks %d failures %d\n", g_checks, g_fail);
687 write_receipt();
688 return g_fail ? 1 : 0;
689}