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_r12_resident.c

829 lines · 37288 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 * Host rules for one resident seat. The graphics processor is not started.
19 * A passing run is not the R12 gate. silicon_observed stays false.
20 *
21 * The stand-in sits in the same image as the processor: it observes a claim
22 * naming input A and output B, performs the seat's one fixed operation
23 * (B.field0 = A.field0 + A.field1), and publishes back. The dependent
24 * reaction wakes from that publication and writes C. Authority is the native
25 * Aienux authority. Nothing here calls the graphics seat as a function.
26 */
27#include "runtime/aienos_cap.h"
28#include "runtime/rx_world.h"
29#include "omega_evidence.h"
30#include "sha256.h"
31
32#include <signal.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/utsname.h>
37#include <time.h>
38#include <unistd.h>
39
40#define RES_A 0x10u
41#define RES_B 0x20u
42#define RES_C 0x30u
43#define X 40u
44#define Y 2u
45
46enum { SUBJ_SEAT = 3, SUBJ_DEPEND = 4, SUBJ_OPERATOR = 7, SUBJ_EXTERNAL = 100, ISSUER = 3 };
47
48static int g_checks;
49static int g_fail;
50
51#define CHECK(cond, ...) do { \
52 g_checks++; \
53 if (!(cond)) { \
54 g_fail++; \
55 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \
56 fprintf(stderr, __VA_ARGS__); \
57 fputc('\n', stderr); \
58 } \
59 } while (0)
60
61static void sleep_ms(int ms) {
62 struct timespec ts = { ms / 1000, (long)(ms % 1000) * 1000000L };
63 nanosleep(&ts, NULL);
64}
65
66static uint64_t load_u64(const uint8_t *p) {
67 uint64_t v = 0;
68 for (int i = 0; i < 8; i++) v |= (uint64_t)p[i] << (8 * i);
69 return v;
70}
71
72static uint64_t window_field(RxWorld *w, uint32_t id, uint32_t field) {
73 uint64_t off = w->objects[id].region_offset;
74 return load_u64(w->coherent + off + (uint64_t)field * 8u);
75}
76
77/* The world checks capabilities through the native Aienux authority. */
78typedef struct {
79 AienosCapAdmin *admin;
80 AienosCapView *view;
81 RxWorld w;
82} Env;
83
84typedef struct {
85 RxObjRef a;
86 RxObjRef b;
87 RxObjRef c;
88} Chain;
89
90static int g_poison;
91
92static int fn_poison(RxCtx *c) {
93 (void)c;
94 g_poison++;
95 return -1;
96}
97
98/* C = B + 1 on the processor. */
99static int fn_depend(RxCtx *c) {
100 Chain *ch = c->user;
101 const RxSnapshotDep *s = NULL;
102 for (uint32_t i = 0; i < c->n_in; i++)
103 if (c->in[i].obj.id == ch->b.id) s = &c->in[i];
104 if (!s) return -1;
105 c->out[c->n_out++] = (RxMutation){ ch->c, 0, s->field[0] + 1 };
106 return 0;
107}
108
109static int env_start(Env *e) {
110 memset(e, 0, sizeof(*e));
111 if (aienos_cap_start(&e->admin, &e->view) != 0) return -1;
112 if (rx_world_init_native(&e->w, e->view, 2, 1u << 16) != RX_OK) {
113 aienos_cap_stop(e->admin, e->view);
114 return -1;
115 }
116 e->w.external_subject = SUBJ_EXTERNAL;
117 return 0;
118}
119
120static void env_stop(Env *e) {
121 rx_world_destroy(&e->w);
122 aienos_cap_stop(e->admin, e->view);
123}
124
125static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) {
126 AienosCapRef office;
127 aienos_cap_office(e->admin, &office);
128 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office };
129 AienosCapRef r = { UINT32_MAX, 0 };
130 if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 };
131 return (RxCapRef){ r.cap_id, r.generation };
132}
133
134static int revoke_cap(Env *e, RxCapRef cap) {
135 AienosCapRef office;
136 aienos_cap_office(e->admin, &office);
137 return aienos_cap_revoke(e->admin, office, (AienosCapRef){ cap.cap_id, cap.generation });
138}
139
140static RxObjRef mkobj(Env *e, uint64_t resource) {
141 uint64_t init[RX_MAX_FIELDS] = { 0 };
142 RxObjRef r = { UINT32_MAX, 0 };
143 rx_world_create(&e->w, 1, RX_PERSIST_RESIDENT, resource, init, &r);
144 return r;
145}
146
147static void offer(RxWorld *w, uint32_t accel) {
148 RxResourceBudget b;
149 memset(&b, 0, sizeof(b));
150 b.slots = 8;
151 b.memory_bytes = UINT64_MAX;
152 b.energy_budget = UINT64_MAX;
153 b.offered_locality = UINT32_MAX;
154 b.offered_accel = accel;
155 b.compute_mask = UINT32_MAX;
156 rx_world_set_resources(w, &b);
157}
158
159static void desc_init(RxReactionDesc *d, const char *name, uint32_t faculty, uint32_t subject,
160 RxFn fn, void *user) {
161 memset(d, 0, sizeof(*d));
162 d->name = name;
163 d->faculty = faculty;
164 d->subject = subject;
165 d->priority = RX_PRIO_FOREGROUND;
166 d->fn = fn;
167 d->user = user;
168}
169
170static void seat_desc(RxReactionDesc *d, RxObjRef a, RxObjRef b, RxCapRef in, RxCapRef out) {
171 desc_init(d, "resident.seat.add", RX_FACULTY_AEGIS, SUBJ_SEAT, fn_poison, NULL);
172 d->need.accelerator_features = RX_ACCEL_BLACKWELL;
173 d->n_triggers = 1;
174 d->triggers[0] = (RxDep){ a, RX_FIELD(0) | RX_FIELD(1) };
175 d->n_writes = 1;
176 d->writes[0] = (RxDep){ b, RX_FIELD(0) };
177 d->n_caps = 2;
178 d->caps[0] = (RxCapNeed){ in, RES_A, RX_RIGHT_READ };
179 d->caps[1] = (RxCapNeed){ out, RES_B, RX_RIGHT_WRITE };
180}
181
182static int wait_seat(RxWorld *w, uint32_t id) {
183 for (int i = 0; i < 2000; i++) {
184 if (w->reactions[id].state == RX_RUNNING && w->reactions[id].resident_seat) return 0;
185 RxState s = w->reactions[id].state;
186 if (s == RX_BLOCKED_RESOURCE || s == RX_BLOCKED_AUTHORITY || s == RX_FAILED ||
187 s == RX_REJECTED || s == RX_DORMANT)
188 return 1;
189 sleep_ms(1);
190 }
191 return -1;
192}
193
194static uint64_t field_of(RxWorld *w, RxObjRef r, uint32_t f) {
195 RxObject o;
196 if (rx_world_read(w, r, &o) != RX_OK) return UINT64_MAX;
197 return o.field[f];
198}
199
200static void t_shape(void) {
201 printf("[*] registration shape\n");
202 Env e;
203 CHECK(env_start(&e) == 0, "setup");
204 RxObjRef a = mkobj(&e, RES_A);
205 RxObjRef b = mkobj(&e, RES_B);
206 RxCapRef in = mint(&e, SUBJ_SEAT, RES_A, RX_RIGHT_READ);
207 RxCapRef out = mint(&e, SUBJ_SEAT, RES_B, RX_RIGHT_READ | RX_RIGHT_WRITE);
208 RxReactionDesc d;
209 seat_desc(&d, a, b, in, out);
210 d.need.accelerator_features = 0;
211 d.fn = NULL;
212 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_ERR_ARG, "a processor reaction needs a body");
213
214 seat_desc(&d, a, a, in, out);
215 d.writes[0] = (RxDep){ a, RX_FIELD(0) };
216 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_ERR_ARG,
217 "the seat must not write the object it reads");
218
219 seat_desc(&d, a, b, in, out);
220 d.writes[0] = (RxDep){ b, RX_FIELD(1) };
221 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_ERR_ARG, "the seat writes field 0 only");
222
223 seat_desc(&d, a, b, in, out);
224 d.triggers[0] = (RxDep){ a, RX_FIELD(2) };
225 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_ERR_ARG, "the seat reads fields 0 and 1");
226
227 seat_desc(&d, a, b, in, out);
228 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK, "seat registration");
229
230 /* Regression: an R8 capability slot is an authority wake, not a second
231 * GPU operand. The data operand must remain trigger zero. */
232 RxObjRef slot = mkobj(&e, RES_C);
233 RxCapRef slot_read = mint(&e, SUBJ_SEAT, RES_C, RX_RIGHT_READ);
234 seat_desc(&d, a, b, in, out);
235 d.n_triggers = 2;
236 d.triggers[1] = (RxDep){slot, RX_FIELD(0) | RX_FIELD(1) | RX_FIELD(2)};
237 d.n_caps = 3;
238 d.caps[2] = (RxCapNeed){slot_read, RES_C, RX_RIGHT_READ};
239 d.cap_slotted[1] = true;
240 d.cap_slot[1] = slot;
241 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_OK,
242 "seat with an R8 capability-slot wake was refused");
243 d.cap_slotted[1] = false;
244 CHECK(rx_world_add_reaction(&e.w, &d, NULL) == RX_ERR_ARG,
245 "unrelated second seat trigger was accepted");
246 env_stop(&e);
247}
248
249typedef struct {
250 Env e;
251 Chain ch;
252 uint32_t seat;
253 uint32_t dep;
254 RxCapRef ext;
255 RxCapRef seat_in;
256 RxCapRef seat_out;
257} Rig;
258
259static int rig_start(Rig *r, int place) {
260 memset(r, 0, sizeof(*r));
261 g_poison = 0;
262 if (env_start(&r->e) != 0) return -1;
263 r->ch.a = mkobj(&r->e, RES_A);
264 r->ch.b = mkobj(&r->e, RES_B);
265 r->ch.c = mkobj(&r->e, RES_C);
266 if (place && (rx_world_attach_physical(&r->e.w, r->ch.a) != RX_OK ||
267 rx_world_attach_physical(&r->e.w, r->ch.b) != RX_OK))
268 return -1;
269 r->seat_in = mint(&r->e, SUBJ_SEAT, RES_A, RX_RIGHT_READ);
270 r->seat_out = mint(&r->e, SUBJ_SEAT, RES_B, RX_RIGHT_READ | RX_RIGHT_WRITE);
271 if (rx_world_bind_capability(&r->e.w, r->ch.a, r->seat_in) != RX_OK) return -1;
272 if (rx_world_bind_capability(&r->e.w, r->ch.b, r->seat_out) != RX_OK) return -1;
273 r->ext = mint(&r->e, SUBJ_EXTERNAL, RES_A, RX_RIGHT_WRITE);
274 RxCapRef dep_r = mint(&r->e, SUBJ_DEPEND, RES_B, RX_RIGHT_READ);
275 RxCapRef dep_w = mint(&r->e, SUBJ_DEPEND, RES_C, RX_RIGHT_WRITE);
276 if (rx_world_enable_resident(&r->e.w) != RX_OK) return -1;
277
278 RxReactionDesc d;
279 seat_desc(&d, r->ch.a, r->ch.b, r->seat_in, r->seat_out);
280 if (rx_world_add_reaction(&r->e.w, &d, &r->seat) != RX_OK) return -1;
281
282 desc_init(&d, "cpu.dependent", RX_FACULTY_AEGIS, SUBJ_DEPEND, fn_depend, &r->ch);
283 d.n_triggers = 1;
284 d.triggers[0] = (RxDep){ r->ch.b, RX_FIELD(0) };
285 d.n_writes = 1;
286 d.writes[0] = (RxDep){ r->ch.c, RX_FIELD(0) };
287 d.n_caps = 2;
288 d.caps[0] = (RxCapNeed){ dep_r, RES_B, RX_RIGHT_READ };
289 d.caps[1] = (RxCapNeed){ dep_w, RES_C, RX_RIGHT_WRITE };
290 if (rx_world_add_reaction(&r->e.w, &d, &r->dep) != RX_OK) return -1;
291 return 0;
292}
293
294static int64_t poke(Rig *r, uint64_t x, uint64_t y) {
295 RxMutation m[2] = { { r->ch.a, 0, x }, { r->ch.a, 1, y } };
296 return rx_world_publish_external(&r->e.w, r->ext, m, 2);
297}
298
299static void t_admission_and_wake(void) {
300 printf("[*] admission, then A -> seat add -> B -> processor -> C\n");
301 Rig r;
302 CHECK(rig_start(&r, 1) == 0, "setup");
303 if (g_fail && r.e.w.n_workers == 0) return;
304 offer(&r.e.w, 0);
305 int64_t cause = poke(&r, X, Y);
306 CHECK(cause > 0, "stimulus");
307 CHECK(rx_world_wait_quiescent(&r.e.w, 1000) == RX_OK, "blocked seat did not settle");
308 CHECK(r.e.w.reactions[r.seat].state == RX_BLOCKED_RESOURCE, "seat ran without the graphics budget");
309 CHECK(r.e.w.reactions[r.seat].activations == 0, "blocked seat counted an activation");
310 CHECK(r.e.w.reactions[r.seat].resident_seat == false, "a claim was posted without admission");
311 CHECK(field_of(&r.e.w, r.ch.b, 0) == 0, "B changed before the seat ran");
312 CHECK(g_poison == 0, "the seat body was called as a function");
313
314 offer(&r.e.w, RX_ACCEL_BLACKWELL);
315 CHECK(wait_seat(&r.e.w, r.seat) == 0, "admitted seat did not reach the claim");
316 int step = rx_resident_seat_step(&r.e.w);
317 CHECK(step == 1, "stand-in step %d", step);
318 CHECK(g_poison == 0, "stand-in called the seat body");
319 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == X + Y, "B's window does not hold the sum");
320 CHECK(field_of(&r.e.w, r.ch.b, 0) == 0, "canonical B changed before accept");
321 CHECK(rx_resident_accept(&r.e.w) == RX_OK, "accept");
322 CHECK(rx_world_wait_quiescent(&r.e.w, 3000) == RX_OK, "dependent did not finish");
323
324 CHECK(field_of(&r.e.w, r.ch.a, 0) == X && field_of(&r.e.w, r.ch.a, 1) == Y, "A was rewritten");
325 CHECK(field_of(&r.e.w, r.ch.b, 0) == X + Y, "canonical B");
326 CHECK(field_of(&r.e.w, r.ch.c, 0) == X + Y + 1, "dependent did not observe B");
327 CHECK(r.e.w.objects[r.ch.b.id].coherency == RX_COHERENCY_SEAT, "coherency");
328 CHECK(g_poison == 0, "seat body ran during accept");
329 CHECK(r.e.w.reactions[r.seat].commits == 1, "seat commits %llu",
330 (unsigned long long)r.e.w.reactions[r.seat].commits);
331 CHECK(r.e.w.reactions[r.dep].commits == 1, "dependent commits");
332
333 uint64_t seat_id = 0, dep_id = 0;
334 for (uint64_t id = 1; id <= r.e.w.n_crumbs; id++) {
335 const RxCrumb *k = rx_world_crumb(&r.e.w, id);
336 if (!k || k->kind != RX_CRUMB_COMMIT) continue;
337 if (k->worker == RX_SEAT_BLACKWELL) seat_id = id;
338 if (k->reaction == r.dep) dep_id = id;
339 }
340 CHECK(seat_id != 0 && dep_id != 0, "missing commit crumbs");
341 if (seat_id && dep_id) {
342 const RxCrumb *seat = rx_world_crumb(&r.e.w, seat_id);
343 const RxCrumb *dep = rx_world_crumb(&r.e.w, dep_id);
344 CHECK(seat->wake_cause == (uint64_t)cause, "seat cause is not the stimulus");
345 CHECK(dep->wake_cause == seat_id, "dependent was not woken by the seat");
346 CHECK(rx_world_explain(&r.e.w, r.ch.b, 0) == seat_id, "B writer");
347 CHECK(rx_world_explain(&r.e.w, r.ch.c, 0) == dep_id, "C writer");
348 }
349 uint64_t checked = 0;
350 CHECK(rx_world_verify_crumbs(&r.e.w, &checked) == 0, "crumb chain");
351 CHECK(checked > 0, "no crumbs verified");
352 CHECK(r.e.w.stats.illegal_transitions == 0, "illegal lifecycle");
353
354 CHECK(rx_resident_shutdown(&r.e.w) == RX_OK, "shutdown");
355 CHECK(rx_resident_seat_step(&r.e.w) == 2, "shutdown was not observed");
356 CHECK(r.e.w.resident_stopped, "seat did not stop");
357 uint64_t tail = rx_world_publication_tail(&r.e.w);
358 CHECK(rx_resident_reset(&r.e.w) == RX_OK, "reset");
359 CHECK(!r.e.w.resident_stopped, "reset left the seat stopped");
360 CHECK(rx_world_publication_tail(&r.e.w) == tail, "reset moved the ring");
361 CHECK(rx_resident_seat_step(&r.e.w) == 0, "reset replayed a finished claim");
362 CHECK(r.e.w.reactions[r.seat].commits == 1, "replay committed again");
363 env_stop(&r.e);
364}
365
366static void t_revoked_input(void) {
367 printf("[*] input capability revoked before the seat reads\n");
368 Rig r;
369 CHECK(rig_start(&r, 1) == 0, "setup");
370 offer(&r.e.w, RX_ACCEL_BLACKWELL);
371 CHECK(poke(&r, X, Y) > 0, "stimulus");
372 CHECK(wait_seat(&r.e.w, r.seat) == 0, "claim was not posted");
373 CHECK(revoke_cap(&r.e, r.seat_in) == 0, "revoke");
374 int step = rx_resident_seat_step(&r.e.w);
375 CHECK(step == RX_ERR_AUTHORITY, "revoked claim step %d", step);
376 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == 0, "revoked claim wrote B's window");
377 CHECK(rx_resident_accept(&r.e.w) == RX_ERR_STALE_GEN, "fault was published as success");
378 CHECK(rx_world_wait_quiescent(&r.e.w, 1000) == RX_OK, "revoked seat did not settle");
379 CHECK(field_of(&r.e.w, r.ch.b, 0) == 0, "canonical B changed");
380 CHECK(field_of(&r.e.w, r.ch.c, 0) == 0, "dependent ran after a refused seat");
381 CHECK(g_poison == 0, "poison body");
382 env_stop(&r.e);
383}
384
385static void t_revoked_output(void) {
386 printf("[*] output capability revoked after the seat wrote, before publication\n");
387 Rig r;
388 CHECK(rig_start(&r, 1) == 0, "setup");
389 offer(&r.e.w, RX_ACCEL_BLACKWELL);
390 CHECK(poke(&r, X, Y) > 0, "stimulus");
391 CHECK(wait_seat(&r.e.w, r.seat) == 0, "claim was not posted");
392 CHECK(rx_resident_seat_step(&r.e.w) == 1, "stand-in step");
393 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == X + Y, "seat did not write");
394 CHECK(revoke_cap(&r.e, r.seat_out) == 0, "revoke");
395 CHECK(rx_resident_accept(&r.e.w) == RX_ERR_AUTHORITY, "revoked output published");
396 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == 0, "B's window was not restored");
397 CHECK(rx_world_wait_quiescent(&r.e.w, 1000) == RX_OK, "did not settle");
398 CHECK(field_of(&r.e.w, r.ch.b, 0) == 0, "canonical B changed");
399 CHECK(field_of(&r.e.w, r.ch.c, 0) == 0, "dependent ran");
400 env_stop(&r.e);
401}
402
403static void t_stale(void) {
404 printf("[*] stale generation\n");
405 Rig r;
406 CHECK(rig_start(&r, 1) == 0, "setup");
407 offer(&r.e.w, RX_ACCEL_BLACKWELL);
408 CHECK(poke(&r, X, Y) > 0, "stimulus");
409 CHECK(wait_seat(&r.e.w, r.seat) == 0, "claim was not posted");
410 CHECK(rx_world_retire(&r.e.w, r.ch.a) == RX_OK, "retire");
411 int step = rx_resident_seat_step(&r.e.w);
412 CHECK(step == RX_ERR_STALE_GEN, "stale step %d", step);
413 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == 0, "stale claim wrote B");
414 CHECK(rx_resident_accept(&r.e.w) == RX_ERR_STALE_GEN, "stale accept");
415 CHECK(rx_world_wait_quiescent(&r.e.w, 1000) == RX_OK, "stale seat did not settle");
416 CHECK(field_of(&r.e.w, r.ch.c, 0) == 0, "dependent ran on a stale seat");
417 CHECK(g_poison == 0, "poison body");
418 env_stop(&r.e);
419}
420
421static void t_output_moved(void) {
422 printf("[*] output generation moved after the claim\n");
423 Rig r;
424 CHECK(rig_start(&r, 1) == 0, "setup");
425 offer(&r.e.w, RX_ACCEL_BLACKWELL);
426 CHECK(poke(&r, X, Y) > 0, "stimulus");
427 CHECK(wait_seat(&r.e.w, r.seat) == 0, "claim was not posted");
428 CHECK(rx_world_retire(&r.e.w, r.ch.b) == RX_OK, "retire B");
429 int step = rx_resident_seat_step(&r.e.w);
430 CHECK(step == RX_ERR_STALE_GEN, "moved output step %d", step);
431 CHECK(rx_resident_accept(&r.e.w) == RX_ERR_STALE_GEN, "moved output accept");
432 CHECK(rx_world_wait_quiescent(&r.e.w, 1000) == RX_OK, "did not settle");
433 CHECK(field_of(&r.e.w, r.ch.c, 0) == 0, "dependent ran");
434 env_stop(&r.e);
435}
436
437static void t_hostile_window(void) {
438 printf("[*] hostile input window\n");
439 Rig r;
440 CHECK(rig_start(&r, 1) == 0, "setup");
441 offer(&r.e.w, RX_ACCEL_BLACKWELL);
442 CHECK(poke(&r, X, Y) > 0, "stimulus");
443 CHECK(wait_seat(&r.e.w, r.seat) == 0, "claim was not posted");
444 uint8_t *win = r.e.w.coherent + r.e.w.objects[r.ch.a.id].region_offset;
445 win[0] ^= 0xff;
446 int step = rx_resident_seat_step(&r.e.w);
447 CHECK(step == RX_ERR_TORN, "torn step %d", step);
448 CHECK(rx_resident_accept(&r.e.w) == RX_ERR_STALE_GEN, "torn accept");
449 CHECK(rx_world_wait_quiescent(&r.e.w, 1000) == RX_OK, "torn seat did not settle");
450 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == 0, "torn claim wrote B");
451 CHECK(field_of(&r.e.w, r.ch.c, 0) == 0, "dependent ran on a torn window");
452 env_stop(&r.e);
453}
454
455static void t_forged_result(void) {
456 printf("[*] result window that is not the sum\n");
457 Rig r;
458 CHECK(rig_start(&r, 1) == 0, "setup");
459 offer(&r.e.w, RX_ACCEL_BLACKWELL);
460 CHECK(poke(&r, X, Y) > 0, "stimulus");
461 CHECK(wait_seat(&r.e.w, r.seat) == 0, "claim was not posted");
462 CHECK(rx_resident_seat_step(&r.e.w) == 1, "stand-in step");
463 uint8_t *win = r.e.w.coherent + r.e.w.objects[r.ch.b.id].region_offset;
464 win[0] ^= 0x01;
465 CHECK(rx_resident_accept(&r.e.w) == RX_ERR_TORN, "a wrong sum was published");
466 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == 0, "B's window was not restored");
467 CHECK(rx_world_wait_quiescent(&r.e.w, 1000) == RX_OK, "did not settle");
468 CHECK(field_of(&r.e.w, r.ch.b, 0) == 0 && field_of(&r.e.w, r.ch.c, 0) == 0, "B or C moved");
469 env_stop(&r.e);
470}
471
472static void t_unplaced(void) {
473 printf("[*] unplaced objects\n");
474 Rig r;
475 CHECK(rig_start(&r, 0) == 0, "setup");
476 offer(&r.e.w, RX_ACCEL_BLACKWELL);
477 CHECK(poke(&r, X, Y) > 0, "stimulus");
478 CHECK(rx_world_wait_quiescent(&r.e.w, 2000) == RX_OK, "unplaced seat did not settle");
479 CHECK(r.e.w.reactions[r.seat].commits == 0, "unplaced seat committed");
480 CHECK(r.e.w.reactions[r.seat].resident_seat == false, "unplaced seat left a claim");
481 CHECK(field_of(&r.e.w, r.ch.b, 0) == 0, "B");
482 CHECK(field_of(&r.e.w, r.ch.c, 0) == 0, "dependent");
483 CHECK(r.e.w.stats.invalidations > 0, "no invalidation");
484 CHECK(g_poison == 0, "poison body");
485 env_stop(&r.e);
486}
487
488static void t_forged_publish(void) {
489 printf("[*] forged capability against the native authority\n");
490 Env e;
491 CHECK(env_start(&e) == 0, "setup");
492 RxObjRef a = mkobj(&e, RES_A);
493 RxCapRef forged = { 200, 7 };
494 RxMutation bad = { a, 0, 5 };
495 CHECK(rx_world_publish_external(&e.w, forged, &bad, 1) == RX_ERR_AUTHORITY, "forged publish");
496 CHECK(field_of(&e.w, a, 0) == 0, "forged publish wrote the object");
497 env_stop(&e);
498}
499
500static uint64_t crumb_count(RxWorld *w, uint32_t reaction, RxCrumbKind kind, int reason,
501 uint64_t *last) {
502 uint64_t n = 0;
503 for (uint64_t id = 1; id <= w->n_crumbs; id++) {
504 const RxCrumb *k = rx_world_crumb(w, id);
505 if (!k || k->reaction != reaction || k->kind != kind) continue;
506 if (reason && k->reason != reason) continue;
507 n++;
508 if (last) *last = id;
509 }
510 return n;
511}
512
513static OmegaSharedWorldRing *ring(RxWorld *w, uint64_t off) {
514 return (OmegaSharedWorldRing *)(w->coherent + off);
515}
516
517static void put_window(RxWorld *w, uint32_t id, uint64_t v) {
518 uint8_t *p = w->coherent + w->objects[id].region_offset;
519 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i));
520}
521
522/* The seat dies holding a claim, after writing B's window and before its
523 * result. The claim fails once with a recorded cause, B's window returns to
524 * the canonical object, the charge is released once, and the retried claim
525 * runs under the next seat generation. Nothing from the old seat publishes. */
526static void t_seat_lost_retry(void) {
527 printf("[*] seat lost holding a claim: recorded, restored, released once, retried\n");
528 Rig r;
529 CHECK(rig_start(&r, 1) == 0, "setup");
530 RxWorld *w = &r.e.w;
531 offer(w, RX_ACCEL_BLACKWELL);
532 uint32_t gen0 = w->seat_generation;
533 uint32_t ga = w->objects[r.ch.a.id].generation, gb = w->objects[r.ch.b.id].generation;
534 int64_t cause = poke(&r, X, Y);
535 CHECK(cause > 0, "stimulus");
536 CHECK(wait_seat(w, r.seat) == 0, "no claim");
537 OmegaSharedWorldRing *c2g = ring(w, rx_world_off_c2g());
538 OmegaSharedWorldDesc old = c2g->slots[w->reactions[r.seat].resident_seq & c2g->mask];
539 CHECK(old.producer_generation == gen0, "the claim does not carry the seat generation");
540 put_window(w, r.ch.b.id, X + Y); /* the dead seat got this far */
541 CHECK(w->used_slots == 1, "the claim is not charged");
542 uint64_t lost_before = w->stats.transitions[RX_RUNNING][RX_FAILED];
543
544 CHECK(rx_resident_seat_lost(w, 1) == 1, "the held claim was not declared lost");
545 CHECK(w->seat_generation == gen0 + 1, "the seat generation did not move");
546 CHECK(w->stats.transitions[RX_RUNNING][RX_FAILED] == lost_before + 1, "lost more than once");
547 CHECK(window_field(w, r.ch.b.id, 0) == 0, "B's window was not restored");
548 CHECK(field_of(w, r.ch.b, 0) == 0, "canonical B changed");
549 uint64_t fail_id = 0;
550 CHECK(crumb_count(w, r.seat, RX_CRUMB_FAILED, RX_ERR_SEAT_LOST, &fail_id) == 1,
551 "no single seat-lost crumb");
552 if (fail_id)
553 CHECK(rx_world_crumb(w, fail_id)->wake_cause == (uint64_t)cause,
554 "the loss does not name the stimulus");
555
556 CHECK(wait_seat(w, r.seat) == 0, "the lost work was not retried");
557 uint64_t seq = w->reactions[r.seat].resident_seq;
558 OmegaSharedWorldDesc fresh = c2g->slots[seq & c2g->mask];
559 CHECK(fresh.producer_generation == gen0 + 1, "the retry carries the old generation");
560 CHECK(w->reactions[r.seat].resident_parent == fail_id, "the retry is not caused by the loss");
561 CHECK(w->used_slots == 1, "the retry is charged %u times", w->used_slots);
562
563 /* A result under the old generation that names the retried claim, with
564 * the right sum in B's window. It must not publish. */
565 OmegaSharedWorldDesc forged = fresh;
566 forged.msg_type = RX_RING_PUBLISH;
567 forged.producer_generation = gen0;
568 rx_world_seal_descriptor(&forged);
569 OmegaSharedWorldRing *g2c = ring(w, rx_world_off_g2c());
570 uint64_t t = g2c->tail;
571 g2c->slots[t & g2c->mask] = forged;
572 __atomic_store_n(&g2c->tail, t + 1, __ATOMIC_RELEASE);
573 put_window(w, r.ch.b.id, X + Y);
574 CHECK(rx_resident_accept(w) == RX_ERR_STALE_GEN, "an old-generation result was accepted");
575 CHECK(field_of(w, r.ch.b, 0) == 0 && w->reactions[r.seat].commits == 0,
576 "an old-generation result published");
577 put_window(w, r.ch.b.id, 0);
578
579 /* The old claim, posted again: the seat refuses it. */
580 CHECK(rx_world_inject_descriptor(w, &old) == RX_OK, "replay not posted");
581 CHECK(rx_resident_seat_step(w) == 1, "the retried claim did not run");
582 CHECK(rx_resident_accept(w) == RX_OK, "the retried result was refused");
583 CHECK(rx_resident_seat_step(w) < 0, "the replayed claim was taken");
584 CHECK(rx_resident_accept(w) == RX_ERR_STALE_GEN, "the replay's fault notice was accepted");
585 CHECK(rx_world_wait_quiescent(w, 3000) == RX_OK, "world did not settle");
586
587 CHECK(field_of(w, r.ch.b, 0) == X + Y && field_of(w, r.ch.c, 0) == X + Y + 1,
588 "the retried work did not reach B and C");
589 CHECK(w->reactions[r.seat].commits == 1, "seat commits %llu",
590 (unsigned long long)w->reactions[r.seat].commits);
591 uint64_t commit_id = 0;
592 CHECK(crumb_count(w, r.seat, RX_CRUMB_COMMIT, 0, &commit_id) == 1, "not exactly one commit");
593 if (commit_id)
594 CHECK(rx_world_crumb(w, commit_id)->wake_cause == fail_id,
595 "the commit is not caused by the loss");
596 CHECK(w->used_slots == 0 && w->in_flight == 0, "a charge was not released");
597 CHECK(w->stats.resident_claims == w->stats.resident_closed, "claims %llu closed %llu",
598 (unsigned long long)w->stats.resident_claims,
599 (unsigned long long)w->stats.resident_closed);
600 CHECK(w->objects[r.ch.a.id].generation == ga && w->objects[r.ch.b.id].generation == gb,
601 "an object generation moved");
602 CHECK(rx_resident_seat_lost(w, 1) == 0, "an idle seat lost a claim");
603 CHECK(w->used_slots == 0 && w->reactions[r.seat].commits == 1, "a second loss changed state");
604 uint64_t checked = 0;
605 CHECK(rx_world_verify_crumbs(w, &checked) == 0, "crumb chain");
606 CHECK(w->stats.illegal_transitions == 0, "illegal lifecycle");
607 env_stop(&r.e);
608}
609
610/* Without retry the loss is final for that activation. The next change of A
611 * runs normally under the new generation. */
612static void t_seat_lost_final(void) {
613 printf("[*] seat lost without retry: the activation fails, the next one runs\n");
614 Rig r;
615 CHECK(rig_start(&r, 1) == 0, "setup");
616 RxWorld *w = &r.e.w;
617 offer(w, RX_ACCEL_BLACKWELL);
618 CHECK(poke(&r, X, Y) > 0, "stimulus");
619 CHECK(wait_seat(w, r.seat) == 0, "no claim");
620 CHECK(rx_resident_seat_lost(w, 0) == 1, "the held claim was not declared lost");
621 CHECK(rx_world_wait_quiescent(w, 3000) == RX_OK, "world did not settle");
622 CHECK(w->reactions[r.seat].state == RX_DORMANT, "the lost activation ran again");
623 CHECK(rx_resident_seat_step(w) == 0, "a discarded claim was still on the ring");
624 CHECK(w->used_slots == 0, "the charge was not released");
625 CHECK(field_of(w, r.ch.b, 0) == 0 && field_of(w, r.ch.c, 0) == 0, "B or C moved");
626 CHECK(poke(&r, 5, 6) > 0, "stimulus");
627 CHECK(wait_seat(w, r.seat) == 0, "no claim after the loss");
628 CHECK(rx_resident_seat_step(w) == 1, "stand-in step");
629 CHECK(rx_resident_accept(w) == RX_OK, "accept");
630 CHECK(rx_world_wait_quiescent(w, 3000) == RX_OK, "world did not settle");
631 CHECK(field_of(w, r.ch.b, 0) == 11 && field_of(w, r.ch.c, 0) == 12, "the next run");
632 CHECK(w->stats.resident_claims == w->stats.resident_closed, "a claim did not end once");
633 CHECK(w->stats.illegal_transitions == 0, "illegal lifecycle");
634 env_stop(&r.e);
635}
636
637static void binary_digest(char out[65]) {
638 strcpy(out, "unavailable");
639 FILE *f = fopen("/proc/self/exe", "rb");
640 if (!f) return;
641 sha256_ctx c;
642 sha256_init(&c);
643 uint8_t buf[65536];
644 size_t n;
645 while ((n = fread(buf, 1, sizeof buf, f)) > 0) sha256_update(&c, buf, n);
646 fclose(f);
647 uint8_t d[32];
648 sha256_final(&c, d);
649 for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]);
650}
651
652static void write_receipt(void) {
653 char path[512];
654 if (omega_evidence_path("R12/rx_resident_host_receipt.json", path, sizeof path) != 0) return;
655 FILE *f = fopen(path, "w");
656 if (!f) return;
657 char commit[41];
658 memset(commit, 0, sizeof commit);
659 if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8);
660 const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT");
661 int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 &&
662 !omega_evidence_tree_dirty();
663 char digest[65];
664 binary_digest(digest);
665 struct utsname u;
666 memset(&u, 0, sizeof u);
667 uname(&u);
668 fprintf(f,
669 "{\n"
670 " \"schema\": \"AIEN_RX_R12_RESIDENT_HOST_V1\",\n"
671 " \"run_id\": \"%s\",\n"
672 " \"candidate_commit\": %s%s%s,\n"
673 " \"candidate_bound\": %s,\n"
674 " \"run_commit\": \"%s\",\n"
675 " \"tree_dirty\": %s,\n"
676 " \"checks\": %d,\n"
677 " \"failures\": %d,\n"
678 " \"silicon_observed\": false,\n"
679 " \"claimed\": false,\n"
680 " \"test_binary_sha256\": \"%s\",\n"
681 " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\"},\n"
682 " \"hardware_scope\": \"host CPU only; graphics seat is an in-process stand-in; R12 is not claimed\",\n"
683 " \"gates\": {\n"
684 " \"R12_RESIDENT_SEAT\": \"HOST_RULES_PASS\",\n"
685 " \"not_claimed\": [\"R8\", \"R10\", \"R11\", \"R12\", \"R13\"]\n"
686 " }\n"
687 "}\n",
688 omega_evidence_run_id(), candidate ? "\"" : "", candidate ? candidate : "null",
689 candidate ? "\"" : "", bound ? "true" : "false", commit,
690 omega_evidence_tree_dirty() ? "true" : "false", g_checks, g_fail, digest, u.sysname,
691 u.release, u.machine);
692 fclose(f);
693 printf("receipt: %s\n", path);
694}
695
696/* Regression (R14). The seat reads its output WRITE from an R8 slot. An R8
697 * renewal writes a new reference into the slot and revokes the old one. The
698 * next claim must present the new reference; before the fix it presented the
699 * reference bound at start, and every later publication was refused. */
700#define RES_SLOT 0x40u
701static void t_slot_renewal(void) {
702 printf("[*] R8 slot renewed: the next claim presents the new grant\n");
703 Env e;
704 CHECK(env_start(&e) == 0, "setup");
705 RxObjRef a = mkobj(&e, RES_A), b = mkobj(&e, RES_B), slot = mkobj(&e, RES_SLOT);
706 CHECK(rx_world_attach_physical(&e.w, a) == RX_OK &&
707 rx_world_attach_physical(&e.w, b) == RX_OK, "placement");
708 RxCapRef in = mint(&e, SUBJ_SEAT, RES_A, RX_RIGHT_READ);
709 RxCapRef old = mint(&e, SUBJ_SEAT, RES_B, RX_RIGHT_READ | RX_RIGHT_WRITE);
710 RxCapRef slot_read = mint(&e, SUBJ_SEAT, RES_SLOT, RX_RIGHT_READ);
711 RxCapRef ext_a = mint(&e, SUBJ_EXTERNAL, RES_A, RX_RIGHT_WRITE);
712 RxCapRef installer = mint(&e, SUBJ_EXTERNAL, RES_SLOT, RX_RIGHT_WRITE);
713 RxMutation s[2] = { { slot, 0, old.cap_id }, { slot, 1, old.generation } };
714 CHECK(rx_world_publish_external(&e.w, installer, s, 2) > 0, "slot install");
715 CHECK(rx_world_bind_capability(&e.w, a, in) == RX_OK &&
716 rx_world_bind_capability(&e.w, b, old) == RX_OK, "bind");
717 CHECK(rx_world_enable_resident(&e.w) == RX_OK, "resident");
718 offer(&e.w, RX_ACCEL_BLACKWELL);
719 RxReactionDesc d;
720 uint32_t seat;
721 seat_desc(&d, a, b, in, (RxCapRef){ UINT32_MAX, 0 });
722 d.n_triggers = 2;
723 d.triggers[1] = (RxDep){ slot, RX_FIELD(0) | RX_FIELD(1) | RX_FIELD(2) };
724 d.n_caps = 3;
725 d.caps[2] = (RxCapNeed){ slot_read, RES_SLOT, RX_RIGHT_READ };
726 d.cap_slotted[1] = true;
727 d.cap_slot[1] = slot;
728 CHECK(rx_world_add_reaction(&e.w, &d, &seat) == RX_OK, "seat");
729 for (uint64_t round = 1; round <= 2; round++) {
730 if (round == 2) {
731 RxCapRef renewed = mint(&e, SUBJ_SEAT, RES_B, RX_RIGHT_READ | RX_RIGHT_WRITE);
732 RxMutation n[2] = { { slot, 0, renewed.cap_id }, { slot, 1, renewed.generation } };
733 CHECK(rx_world_publish_external(&e.w, installer, n, 2) > 0, "renewal");
734 CHECK(revoke_cap(&e, old) == 0, "revoke the replaced grant");
735 /* The slot is the seat's authority wake: it claims again at once,
736 * and that claim must publish under the renewed grant. */
737 CHECK(wait_seat(&e.w, seat) == 0, "renewal: no claim");
738 CHECK(rx_resident_seat_step(&e.w) == 1, "renewal: stand-in");
739 int racc = rx_resident_accept(&e.w);
740 CHECK(racc == RX_OK, "renewal: publication refused (%d)", racc);
741 CHECK(rx_world_wait_quiescent(&e.w, 1000) == RX_OK, "renewal settle");
742 }
743 RxMutation m[2] = { { a, 0, 100 * round }, { a, 1, round } };
744 CHECK(rx_world_publish_external(&e.w, ext_a, m, 2) > 0, "stimulus %llu",
745 (unsigned long long)round);
746 CHECK(wait_seat(&e.w, seat) == 0, "round %llu: no claim", (unsigned long long)round);
747 CHECK(rx_resident_seat_step(&e.w) == 1, "round %llu: stand-in", (unsigned long long)round);
748 int acc = rx_resident_accept(&e.w);
749 CHECK(acc == RX_OK, "round %llu: publication refused (%d)", (unsigned long long)round, acc);
750 CHECK(rx_world_wait_quiescent(&e.w, 1000) == RX_OK, "settle");
751 CHECK(field_of(&e.w, b, 0) == 100 * round + round, "round %llu: B=%llu",
752 (unsigned long long)round, (unsigned long long)field_of(&e.w, b, 0));
753 }
754 CHECK(e.w.reactions[seat].commits == 3, "commits %llu",
755 (unsigned long long)e.w.reactions[seat].commits);
756 /* The replaced grant itself is dead: it authorizes nothing. */
757 CHECK(rx_world_validate_cap(&e.w, old, SUBJ_SEAT, RES_B, RX_RIGHT_WRITE, NULL) != RX_CAP_OK,
758 "the replaced grant still validates");
759 uint64_t checked = 0;
760 CHECK(rx_world_verify_crumbs(&e.w, &checked) == 0, "crumb chain");
761 env_stop(&e);
762}
763
764/* R16 G6 operator emergency stop (spec/r16-operator-emergency-stop.md) on the
765 * resident seat, with the native Aienux authority: the seat's result lands
766 * after the stop. It is not published (canonical B and B's window keep the
767 * world's value, the dependent does not run, the claim closes); after resume
768 * the seat runs again and the chain completes with the right values. */
769static void t_emergency_stop(void) {
770 printf("[*] operator emergency stop: a seat result under a stop is refused, resume reruns it\n");
771 Rig r;
772 CHECK(rig_start(&r, 1) == 0, "setup");
773 if (g_fail && r.e.w.n_workers == 0) return;
774 RxCallerCred op;
775 CHECK(rx_world_enroll_caller(&r.e.w, SUBJ_OPERATOR, &op) == RX_OK, "enroll the operator");
776 RxCapRef halt = mint(&r.e, SUBJ_OPERATOR, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT);
777 CHECK(halt.cap_id != UINT32_MAX, "the native authority refused the operator halt capability");
778 offer(&r.e.w, RX_ACCEL_BLACKWELL);
779 CHECK(poke(&r, X, Y) > 0, "stimulus");
780 CHECK(wait_seat(&r.e.w, r.seat) == 0, "admitted seat did not reach the claim");
781 CHECK(rx_resident_seat_step(&r.e.w) == 1, "stand-in step");
782 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == X + Y, "B's window does not hold the sum");
783
784 RxHaltStatus h;
785 CHECK(rx_world_emergency_stop(&r.e.w, SUBJ_OPERATOR, &op, halt, 12, &h) == RX_OK && h.halted,
786 "operator stop");
787 CHECK(rx_resident_accept(&r.e.w) == RX_ERR_HALTED, "a seat result was published under the stop");
788 CHECK(field_of(&r.e.w, r.ch.b, 0) == 0, "canonical B changed under the stop");
789 CHECK(window_field(&r.e.w, r.ch.b.id, 0) == 0, "B's window was not re-projected from the world");
790 CHECK(r.e.w.reactions[r.seat].resident_seat == false, "the seat claim stayed open");
791 CHECK(poke(&r, X + 1, Y) == RX_ERR_HALTED, "outside publication under the stop");
792 sleep_ms(50);
793 CHECK(field_of(&r.e.w, r.ch.c, 0) == 0, "the dependent ran under the stop");
794 CHECK(rx_resident_seat_step(&r.e.w) == 0, "a claim was posted under the stop");
795 rx_world_halt_status(&r.e.w, &h);
796 CHECK(h.halted && h.refused >= 2, "stop status (halted %d, refused %llu)", (int)h.halted,
797 (unsigned long long)h.refused);
798
799 CHECK(rx_world_emergency_resume(&r.e.w, SUBJ_OPERATOR, &op, halt, &h) == RX_OK && !h.halted,
800 "operator resume");
801 CHECK(wait_seat(&r.e.w, r.seat) == 0, "the seat did not run again after resume");
802 CHECK(rx_resident_seat_step(&r.e.w) == 1, "stand-in step after resume");
803 CHECK(rx_resident_accept(&r.e.w) == RX_OK, "accept after resume");
804 CHECK(rx_world_wait_quiescent(&r.e.w, 3000) == RX_OK, "dependent did not finish");
805 CHECK(field_of(&r.e.w, r.ch.b, 0) == X + Y, "canonical B after resume");
806 CHECK(field_of(&r.e.w, r.ch.c, 0) == X + Y + 1, "dependent after resume");
807 env_stop(&r.e);
808}
809
810int main(void) {
811 signal(SIGPIPE, SIG_IGN);
812 t_shape();
813 t_admission_and_wake();
814 t_revoked_input();
815 t_revoked_output();
816 t_stale();
817 t_output_moved();
818 t_hostile_window();
819 t_forged_result();
820 t_unplaced();
821 t_forged_publish();
822 t_seat_lost_retry();
823 t_seat_lost_final();
824 t_slot_renewal();
825 t_emergency_stop();
826 printf("checks %d failures %d\n", g_checks, g_fail);
827 write_receipt();
828 return g_fail ? 1 : 0;
829}