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

814 lines · 36660 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 * R16 G6 operator emergency stop (spec/r16-operator-emergency-stop.md).
19 *
20 * Real authority (the rx_caproot root process), real caller credentials, a
21 * worker pool and a generation store. Every case states what must hold:
22 *
23 * E1 refusals no credential, a forged credential, another subject's
24 * capability, the wrong right, the wrong resource, a revoked
25 * capability, and a reaction's own subject holding a valid
26 * control capability: each refused with its code; the world
27 * digest, the crumb count and the halt state are unchanged.
28 * E2 stop an authorized stop takes effect at once (OPERATOR_STOP crumb
29 * naming the capability and the reason); a second stop answers
30 * RX_HALT_ALREADY and changes nothing; outside publication is
31 * refused (RX_ERR_HALTED) and changes nothing.
32 * E3 in flight a stop taken while a reaction computes: its result is not
33 * published (CANCELLED, RX_ERR_HALTED), work queued behind it is
34 * not taken; on resume both run and each commits exactly once.
35 * E4 race publishers and two operators racing stop/resume: in the crumb
36 * log no EXTERNAL or COMMIT lies between a STOP and its RESUME,
37 * each accepted stop has exactly one STOP crumb, every refused
38 * publication returned RX_ERR_HALTED, and the chain verifies.
39 * E5 promotion a stop during a promotion's live barrier and during its disk
40 * writes refuses that promotion (RX_GEN_ERR_HALTED, active
41 * generation unchanged in memory and on disk); a proposal under
42 * a stop is refused; after resume the same promoter promotes.
43 * E6 durable the stop writes a sealed mark; a new world over the same
44 * directory starts stopped (restored); a mark with one byte
45 * changed still stops it (RX_ERR_TORN); an unbound store will not
46 * promote while the mark exists; resume keeps the stop record in
47 * a sealed OPERATOR_HALT.resumed.* file and removes the mark; a
48 * resume that cannot retire the mark leaves the world stopped.
49 * E7 budget a stop while R3 (woken by another reaction's commit) computes,
50 * with an activation budget of 1: the refused activation does not
51 * spend the budget, R3 is not quarantined and runs on resume;
52 * object create and retire are refused under the stop and change
53 * nothing; a symlink mark stops a new world (TORN) and resume
54 * removes it.
55 *
56 * Host only, CPU only. Exit 0 = PASS.
57 */
58#include "runtime/rx_caproot.h"
59#include "runtime/rx_generation.h"
60#include "runtime/rx_world.h"
61
62#include <dirent.h>
63#include <fcntl.h>
64#include <pthread.h>
65#include <stdatomic.h>
66#include <stdint.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <sys/stat.h>
71#include <time.h>
72#include <unistd.h>
73
74#define U(x) ((unsigned long long)(x))
75
76static int g_fail;
77static void check(int ok, const char *what) {
78 printf("%s %s\n", ok ? "[+]" : "[-]", what);
79 if (!ok) g_fail++;
80}
81
82enum { RES_E = 0x710, RES_O = 0x711, RES_Q = 0x712 };
83enum { SUBJ_EXT = 9001, SUBJ_R = 9002, SUBJ_R2 = 9003, SUBJ_R3 = 9004, OP = 9100, OP2 = 9101, INTRUDER = 9102 };
84
85/* ---- gate: R parks in its function until released ------------------------- */
86static struct {
87 pthread_mutex_t mu;
88 pthread_cond_t cv;
89 int armed, armed3, inside, go;
90} g = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0, 0, 0 };
91static int g_with_r3; /* setup adds R3, a reaction woken by R's commit (E7) */
92
93typedef struct {
94 RxCapRoot root;
95 RxCapAdmin admin;
96 RxWorld w;
97 RxObjRef e, o, q;
98 uint32_t id_r, id_r2, id_r3;
99 RxCapRef c_ext, c_op, c_op2;
100 RxCallerCred cr_op, cr_op2, cr_intr, cr_r, cr_r2, cr_r3;
101} Env;
102
103static int fn_r(RxCtx *c) {
104 Env *e = c->user;
105 pthread_mutex_lock(&g.mu);
106 if (g.armed) {
107 g.inside = 1;
108 pthread_cond_broadcast(&g.cv);
109 while (!g.go) pthread_cond_wait(&g.cv, &g.mu);
110 g.go = 0;
111 g.inside = 0;
112 g.armed = 0;
113 }
114 pthread_mutex_unlock(&g.mu);
115 c->n_out = 1;
116 c->out[0] = (RxMutation){ e->o, 0, c->in[0].field[0] + 1 };
117 return 0;
118}
119
120static int fn_r2(RxCtx *c) {
121 Env *e = c->user;
122 c->n_out = 1;
123 c->out[0] = (RxMutation){ e->q, 0, c->in[0].field[1] * 10 };
124 return 0;
125}
126
127/* R3: woken by R's commit on O (an internal cause, so its episode budget is not
128 * reset by the wake), held in its function when armed3 is set. */
129static int fn_r3(RxCtx *c) {
130 Env *e = c->user;
131 pthread_mutex_lock(&g.mu);
132 if (g.armed3) {
133 g.inside = 1;
134 pthread_cond_broadcast(&g.cv);
135 while (!g.go) pthread_cond_wait(&g.cv, &g.mu);
136 g.go = 0;
137 g.inside = 0;
138 g.armed3 = 0;
139 }
140 pthread_mutex_unlock(&g.mu);
141 c->n_out = 1;
142 c->out[0] = (RxMutation){ e->q, 1, c->in[0].field[0] * 100 };
143 return 0;
144}
145
146static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) {
147 RxCapMint m;
148 memset(&m, 0, sizeof m);
149 m.issuer = 1;
150 m.subject = subject;
151 m.resource = resource;
152 m.rights = rights;
153 m.parent = (RxCapRef){ UINT32_MAX, 0 };
154 m.authority = rx_capadmin_office(&e->admin);
155 RxCapRef r = { UINT32_MAX, 0 };
156 if (rx_capadmin_mint(&e->admin, &m, &r) != RX_CAP_OK) fprintf(stderr, "mint failed\n");
157 return r;
158}
159
160static RxObjRef mkobj(Env *e, uint64_t resource) {
161 uint64_t init[RX_MAX_FIELDS] = { 0 };
162 RxObjRef r = { UINT32_MAX, 0 };
163 rx_world_create(&e->w, 1, RX_PERSIST_RESIDENT, resource, init, &r);
164 return r;
165}
166
167static int setup(Env *e, uint32_t workers, uint64_t crumb_cap) {
168 memset(e, 0, sizeof *e);
169 if (rx_caproot_start(&e->root, &e->admin) != RX_CAP_OK) return -1;
170 if (rx_world_init(&e->w, &e->root, workers, crumb_cap) != RX_OK) return -1;
171 e->w.external_subject = SUBJ_EXT;
172 e->e = mkobj(e, RES_E);
173 e->o = mkobj(e, RES_O);
174 e->q = mkobj(e, RES_Q);
175 e->c_ext = mint(e, SUBJ_EXT, RES_E, RX_RIGHT_WRITE);
176 e->c_op = mint(e, OP, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT);
177 e->c_op2 = mint(e, OP2, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT);
178 if (rx_world_enroll_caller(&e->w, OP, &e->cr_op) != RX_CALLER_OK ||
179 rx_world_enroll_caller(&e->w, OP2, &e->cr_op2) != RX_CALLER_OK ||
180 rx_world_enroll_caller(&e->w, INTRUDER, &e->cr_intr) != RX_CALLER_OK ||
181 rx_world_enroll_caller(&e->w, SUBJ_R, &e->cr_r) != RX_CALLER_OK ||
182 rx_world_enroll_caller(&e->w, SUBJ_R2, &e->cr_r2) != RX_CALLER_OK)
183 return -1;
184 RxReactionDesc d;
185 memset(&d, 0, sizeof d);
186 d.name = "estop.r";
187 d.faculty = RX_FACULTY_OMEGA;
188 d.subject = SUBJ_R;
189 d.caller = e->cr_r;
190 d.priority = RX_PRIO_FOREGROUND;
191 d.fn = fn_r;
192 d.user = e;
193 d.triggers[d.n_triggers++] = (RxDep){ e->e, RX_FIELD(0) };
194 d.writes[d.n_writes++] = (RxDep){ e->o, RX_FIELD(0) };
195 d.caps[d.n_caps++] = (RxCapNeed){ mint(e, SUBJ_R, RES_E, RX_RIGHT_READ), RES_E, RX_RIGHT_READ };
196 d.caps[d.n_caps++] = (RxCapNeed){ mint(e, SUBJ_R, RES_O, RX_RIGHT_WRITE), RES_O, RX_RIGHT_WRITE };
197 if (rx_world_add_reaction(&e->w, &d, &e->id_r) != RX_OK) return -1;
198 memset(&d, 0, sizeof d);
199 d.name = "estop.r2";
200 d.faculty = RX_FACULTY_AIEN;
201 d.subject = SUBJ_R2;
202 d.caller = e->cr_r2;
203 d.priority = RX_PRIO_FOREGROUND;
204 d.fn = fn_r2;
205 d.user = e;
206 d.triggers[d.n_triggers++] = (RxDep){ e->e, RX_FIELD(1) };
207 d.writes[d.n_writes++] = (RxDep){ e->q, RX_FIELD(0) };
208 d.caps[d.n_caps++] = (RxCapNeed){ mint(e, SUBJ_R2, RES_E, RX_RIGHT_READ), RES_E, RX_RIGHT_READ };
209 d.caps[d.n_caps++] = (RxCapNeed){ mint(e, SUBJ_R2, RES_Q, RX_RIGHT_WRITE), RES_Q, RX_RIGHT_WRITE };
210 if (rx_world_add_reaction(&e->w, &d, &e->id_r2) != RX_OK) return -1;
211 if (g_with_r3) {
212 if (rx_world_enroll_caller(&e->w, SUBJ_R3, &e->cr_r3) != RX_CALLER_OK) return -1;
213 memset(&d, 0, sizeof d);
214 d.name = "estop.r3";
215 d.faculty = RX_FACULTY_AIEN;
216 d.subject = SUBJ_R3;
217 d.caller = e->cr_r3;
218 d.priority = RX_PRIO_FOREGROUND;
219 d.fn = fn_r3;
220 d.user = e;
221 d.triggers[d.n_triggers++] = (RxDep){ e->o, RX_FIELD(0) };
222 d.writes[d.n_writes++] = (RxDep){ e->q, RX_FIELD(1) };
223 d.caps[d.n_caps++] = (RxCapNeed){ mint(e, SUBJ_R3, RES_O, RX_RIGHT_READ), RES_O, RX_RIGHT_READ };
224 d.caps[d.n_caps++] = (RxCapNeed){ mint(e, SUBJ_R3, RES_Q, RX_RIGHT_WRITE), RES_Q, RX_RIGHT_WRITE };
225 if (rx_world_add_reaction(&e->w, &d, &e->id_r3) != RX_OK) return -1;
226 }
227 if (rx_world_bind_callers(&e->w) != RX_OK) return -1;
228 return 0;
229}
230
231static void teardown(Env *e) {
232 rx_world_destroy(&e->w);
233 rx_caproot_stop(&e->root, &e->admin);
234}
235
236static uint64_t field(Env *e, RxObjRef r) {
237 RxObject o;
238 if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX;
239 return o.field[0];
240}
241
242static int64_t publish(Env *e, uint32_t f, uint64_t v) {
243 RxMutation m = { e->e, f, v };
244 return rx_world_publish_external(&e->w, e->c_ext, &m, 1);
245}
246
247static uint64_t crumbs(Env *e) {
248 pthread_mutex_lock(&e->w.mu);
249 uint64_t n = e->w.n_crumbs;
250 pthread_mutex_unlock(&e->w.mu);
251 return n;
252}
253
254static void digest(Env *e, uint8_t out[32]) { rx_world_digest(&e->w, out); }
255
256static uint64_t reaction_commits(Env *e, uint32_t rid) {
257 uint64_t n = 0;
258 pthread_mutex_lock(&e->w.mu);
259 for (uint64_t i = 0; i < e->w.n_crumbs; i++)
260 if (e->w.crumbs[i].kind == RX_CRUMB_COMMIT && e->w.crumbs[i].reaction == rid) n++;
261 pthread_mutex_unlock(&e->w.mu);
262 return n;
263}
264
265static int wait_inside(void) {
266 for (int i = 0; i < 5000; i++) {
267 pthread_mutex_lock(&g.mu);
268 int in = g.inside;
269 pthread_mutex_unlock(&g.mu);
270 if (in) return 0;
271 usleep(1000);
272 }
273 return -1;
274}
275
276static void gate_release(void) {
277 pthread_mutex_lock(&g.mu);
278 g.go = 1;
279 pthread_cond_broadcast(&g.cv);
280 pthread_mutex_unlock(&g.mu);
281}
282
283/* Bounded wait until reaction rid has `want` commits (5 s). */
284static int wait_commits(Env *e, uint32_t rid, uint64_t want) {
285 for (int i = 0; i < 5000; i++) {
286 if (reaction_commits(e, rid) >= want) return 0;
287 usleep(1000);
288 }
289 return -1;
290}
291
292static int stop(Env *e, uint32_t subj, const RxCallerCred *cr, RxCapRef cap, int32_t why) {
293 return rx_world_emergency_stop(&e->w, subj, cr, cap, why, NULL);
294}
295static int resume(Env *e, uint32_t subj, const RxCallerCred *cr, RxCapRef cap) {
296 return rx_world_emergency_resume(&e->w, subj, cr, cap, NULL);
297}
298static bool halted(Env *e) {
299 RxHaltStatus h;
300 rx_world_halt_status(&e->w, &h);
301 return h.halted;
302}
303
304/* ---- E1 refusals ----------------------------------------------------------- */
305static void e1_refusals(Env *e) {
306 uint8_t d0[32], d1[32];
307 digest(e, d0);
308 uint64_t n0 = crumbs(e);
309 RxCallerCred forged = e->cr_op;
310 forged.secret[0] ^= 1;
311 RxCapRef wrong_right = mint(e, OP, RX_WORLD_RES_CONTROL, RX_RIGHT_WRITE);
312 RxCapRef wrong_res = mint(e, OP, RES_O, RX_WORLD_RIGHT_HALT);
313 RxCapRef of_r = mint(e, SUBJ_R, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT);
314 RxCapRef revoked = mint(e, OP, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT);
315 check(rx_capadmin_revoke(&e->admin, rx_capadmin_office(&e->admin), revoked) == RX_CAP_OK,
316 "E1 setup: a control capability revoked by the office");
317 struct { const char *what; uint32_t subj; const RxCallerCred *cr; RxCapRef cap; int want; } t[] = {
318 { "no credential", OP, NULL, e->c_op, RX_ERR_IDENTITY },
319 { "forged credential", OP, &forged, e->c_op, RX_ERR_IDENTITY },
320 { "enrolled intruder presenting the operator's capability", INTRUDER, &e->cr_intr, e->c_op,
321 RX_ERR_AUTHORITY },
322 { "operator with the wrong right on the control resource", OP, &e->cr_op, wrong_right,
323 RX_ERR_AUTHORITY },
324 { "operator with the halt right on another resource", OP, &e->cr_op, wrong_res,
325 RX_ERR_AUTHORITY },
326 { "operator with a revoked control capability", OP, &e->cr_op, revoked, RX_ERR_AUTHORITY },
327 { "a reaction's subject with a valid control capability", SUBJ_R, &e->cr_r, of_r,
328 RX_ERR_AUTHORITY },
329 };
330 for (size_t i = 0; i < sizeof t / sizeof t[0]; i++) {
331 char what[200];
332 int rc = stop(e, t[i].subj, t[i].cr, t[i].cap, 7);
333 snprintf(what, sizeof what, "E1 stop refused: %s (rc %d, want %d)", t[i].what, rc, t[i].want);
334 check(rc == t[i].want, what);
335 }
336 check(!halted(e), "E1 the world is still running after every refused stop");
337 digest(e, d1);
338 check(memcmp(d0, d1, 32) == 0 && crumbs(e) == n0,
339 "E1 refused stops changed neither the world digest nor the crumb log");
340 check(resume(e, OP, &forged, e->c_op) == RX_ERR_IDENTITY,
341 "E1 a forged resume is refused before 'not stopped' is told");
342 check(resume(e, OP, &e->cr_op, e->c_op) == RX_HALT_NOT_STOPPED,
343 "E1 an authorized resume of a running world answers RX_HALT_NOT_STOPPED");
344 check(crumbs(e) == n0, "E1 the not-stopped resume left no crumb");
345 check(publish(e, 1, 4) > 0 && wait_commits(e, e->id_r2, 1) == 0 && field(e, e->q) == 40,
346 "E1 control: the world still publishes and reacts (R2 wrote 4*10)");
347}
348
349/* ---- E2 stop and E3 in flight ------------------------------------------------ */
350static void e2_e3(Env *e) {
351 /* E3 first half: R is computing when the stop lands; R2's wake is queued
352 * behind it on the single worker. */
353 pthread_mutex_lock(&g.mu);
354 g.armed = 1;
355 pthread_mutex_unlock(&g.mu);
356 uint64_t r_before = reaction_commits(e, e->id_r), r2_before = reaction_commits(e, e->id_r2);
357 check(publish(e, 0, 41) > 0, "E3 setup: publish E.f0 = 41 (wakes R)");
358 check(wait_inside() == 0, "E3 setup: R is computing, held in its function");
359 check(publish(e, 1, 5) > 0, "E3 setup: publish E.f1 = 5 (wakes R2, queued: one worker)");
360
361 uint64_t n0 = crumbs(e);
362 RxHaltStatus h;
363 int rc = rx_world_emergency_stop(&e->w, OP, &e->cr_op, e->c_op, 77, &h);
364 check(rc == RX_OK && h.halted && h.seq == 1 && h.subject == OP && h.reason == 77,
365 "E2 an authorized stop takes effect (seq 1, operator, reason 77)");
366 const RxCrumb *k = rx_world_crumb(&e->w, h.crumb);
367 check(k && k->kind == RX_CRUMB_OPERATOR_STOP && k->caps[0].cap_id == e->c_op.cap_id &&
368 k->caps[0].generation == e->c_op.generation && k->reason == 77 &&
369 k->reaction == UINT32_MAX,
370 "E2 OPERATOR_STOP crumb names the control capability and the reason");
371 check(h.durable == 0, "E2 no halt directory: durable = 0 (said, not hidden)");
372 uint64_t n1 = crumbs(e);
373 check(n1 == n0 + 1, "E2 the stop added exactly one crumb");
374 check(stop(e, OP2, &e->cr_op2, e->c_op2, 78) == RX_HALT_ALREADY && crumbs(e) == n1,
375 "E2 a second stop (another operator) answers RX_HALT_ALREADY, no crumb");
376 rx_world_halt_status(&e->w, &h);
377 check(h.seq == 1 && h.subject == OP && h.reason == 77, "E2 the second stop changed nothing");
378 uint8_t d0[32], d1[32];
379 digest(e, d0);
380 check(publish(e, 0, 999) == RX_ERR_HALTED, "E2 outside publication refused: RX_ERR_HALTED");
381 digest(e, d1);
382 check(memcmp(d0, d1, 32) == 0 && crumbs(e) == n1, "E2 the refused publication changed nothing");
383
384 /* R finishes computing under the stop. */
385 uint64_t o_before = field(e, e->o);
386 gate_release();
387 int cancelled = 0;
388 for (int i = 0; i < 5000 && !cancelled; i++) {
389 pthread_mutex_lock(&e->w.mu);
390 for (uint64_t j = n1; j < e->w.n_crumbs; j++)
391 if (e->w.crumbs[j].kind == RX_CRUMB_CANCELLED && e->w.crumbs[j].reaction == e->id_r &&
392 e->w.crumbs[j].reason == RX_ERR_HALTED)
393 cancelled = 1;
394 pthread_mutex_unlock(&e->w.mu);
395 if (!cancelled) usleep(1000);
396 }
397 check(cancelled, "E3 the activation computed under the stop ended CANCELLED, RX_ERR_HALTED");
398 usleep(50000);
399 check(field(e, e->o) == o_before && reaction_commits(e, e->id_r) == r_before,
400 "E3 nothing it computed was published (O unchanged, no COMMIT)");
401 check(reaction_commits(e, e->id_r2) == r2_before,
402 "E3 R2's queued wake was not taken while stopped (50 ms)");
403 rx_world_halt_status(&e->w, &h);
404 check(h.refused == 2, "E3 refused count = 2 (one publication, one activation)");
405
406 check(resume(e, OP2, &e->cr_op2, e->c_op2) == RX_OK,
407 "E2 another authorized operator resumes");
408 rx_world_halt_status(&e->w, &h);
409 const RxCrumb *rk = rx_world_crumb(&e->w, e->w.n_crumbs);
410 int found = 0;
411 pthread_mutex_lock(&e->w.mu);
412 for (uint64_t j = n1; j < e->w.n_crumbs; j++) {
413 const RxCrumb *c = &e->w.crumbs[j];
414 if (c->kind != RX_CRUMB_OPERATOR_RESUME) continue;
415 for (uint32_t p = 0; p < c->n_parents; p++) found |= c->parents[p] == n1;
416 found &= c->caps[0].cap_id == e->c_op2.cap_id;
417 }
418 pthread_mutex_unlock(&e->w.mu);
419 (void)rk;
420 check(!h.halted && found, "E2 OPERATOR_RESUME crumb: parent = the stop, cap = the resumer's");
421 check(wait_commits(e, e->id_r, r_before + 1) == 0 && wait_commits(e, e->id_r2, r2_before + 1) == 0,
422 "E3 on resume the cancelled activation and the queued wake both ran");
423 usleep(50000);
424 check(field(e, e->o) == 42 && field(e, e->q) == 50, "E3 their results: O = 41+1, Q = 5*10");
425 check(reaction_commits(e, e->id_r) == r_before + 1 && reaction_commits(e, e->id_r2) == r2_before + 1,
426 "E3 each committed exactly once");
427}
428
429/* ---- E4 race ----------------------------------------------------------------- */
430typedef struct {
431 Env *e;
432 atomic_int run;
433 atomic_long ok, halted_rc, other_rc;
434 atomic_long stops_ok, resumes_ok, stop_other, resume_other;
435} Race;
436
437static void *publisher(void *a) {
438 Race *r = a;
439 uint64_t v = 100;
440 while (atomic_load(&r->run) && atomic_load(&r->ok) < 60000) {
441 int64_t rc = publish(r->e, (uint32_t)(v & 1), v);
442 if (rc > 0) atomic_fetch_add(&r->ok, 1);
443 else if (rc == RX_ERR_HALTED) atomic_fetch_add(&r->halted_rc, 1);
444 else atomic_fetch_add(&r->other_rc, 1);
445 v++;
446 }
447 return NULL;
448}
449
450typedef struct { Race *r; uint32_t subj; RxCallerCred *cr; RxCapRef cap; int n; } Op;
451static void *operator_thread(void *a) {
452 Op *o = a;
453 for (int i = 0; i < o->n; i++) {
454 int rc = stop(o->r->e, o->subj, o->cr, o->cap, 100 + i);
455 if (rc == RX_OK) atomic_fetch_add(&o->r->stops_ok, 1);
456 else if (rc != RX_HALT_ALREADY) atomic_fetch_add(&o->r->stop_other, 1);
457 usleep(200);
458 rc = resume(o->r->e, o->subj, o->cr, o->cap);
459 if (rc == RX_OK) atomic_fetch_add(&o->r->resumes_ok, 1);
460 else if (rc != RX_HALT_NOT_STOPPED) atomic_fetch_add(&o->r->resume_other, 1);
461 usleep(100);
462 }
463 return NULL;
464}
465
466static void e4_race(Env *e) {
467 uint64_t n0 = crumbs(e);
468 Race r;
469 memset(&r, 0, sizeof r);
470 r.e = e;
471 atomic_store(&r.run, 1);
472 pthread_t p[2], o[2];
473 Op o1 = { &r, OP, &e->cr_op, e->c_op, 300 }, o2 = { &r, OP2, &e->cr_op2, e->c_op2, 300 };
474 for (int i = 0; i < 2; i++) pthread_create(&p[i], NULL, publisher, &r);
475 pthread_create(&o[0], NULL, operator_thread, &o1);
476 pthread_create(&o[1], NULL, operator_thread, &o2);
477 pthread_join(o[0], NULL);
478 pthread_join(o[1], NULL);
479 atomic_store(&r.run, 0);
480 for (int i = 0; i < 2; i++) pthread_join(p[i], NULL);
481 if (halted(e)) resume(e, OP, &e->cr_op, e->c_op);
482 rx_world_wait_quiescent(&e->w, 5000);
483
484 long stops = 0, resumes = 0, inside_bad = 0, stop_in_stop = 0, resume_outside = 0;
485 int in = 0;
486 pthread_mutex_lock(&e->w.mu);
487 for (uint64_t j = n0; j < e->w.n_crumbs; j++) {
488 const RxCrumb *c = &e->w.crumbs[j];
489 if (c->kind == RX_CRUMB_OPERATOR_STOP) { stops++; stop_in_stop += in; in = 1; }
490 else if (c->kind == RX_CRUMB_OPERATOR_RESUME) { resumes++; resume_outside += !in; in = 0; }
491 else if (in && (c->kind == RX_CRUMB_EXTERNAL || c->kind == RX_CRUMB_COMMIT)) inside_bad++;
492 }
493 uint64_t overflow = e->w.stats.crumb_overflow;
494 pthread_mutex_unlock(&e->w.mu);
495 char what[240];
496 snprintf(what, sizeof what,
497 "E4 race: %ld publications ok, %ld refused HALTED, %ld other; %ld stops, %ld resumes",
498 (long)atomic_load(&r.ok), (long)atomic_load(&r.halted_rc), (long)atomic_load(&r.other_rc),
499 stops, resumes);
500 check(atomic_load(&r.ok) > 0 && atomic_load(&r.halted_rc) > 0 && atomic_load(&r.other_rc) == 0,
501 what);
502 check(overflow == 0, "E4 the crumb log did not overflow (every event is in it)");
503 check(inside_bad == 0, "E4 no EXTERNAL or COMMIT crumb between a STOP and its RESUME");
504 check(stop_in_stop == 0 && resume_outside == 0, "E4 STOP and RESUME strictly alternate");
505 check(stops == atomic_load(&r.stops_ok) && resumes == atomic_load(&r.resumes_ok) + (stops > atomic_load(&r.resumes_ok) ? 1 : 0),
506 "E4 one STOP crumb per accepted stop, one RESUME per accepted resume");
507 check(atomic_load(&r.stop_other) == 0 && atomic_load(&r.resume_other) == 0,
508 "E4 every stop/resume answer was OK, ALREADY or NOT_STOPPED");
509 uint64_t checked = 0;
510 check(rx_world_verify_crumbs(&e->w, &checked) == 0, "E4 the crumb chain verifies");
511}
512
513/* ---- E5 promotion -------------------------------------------------------------- */
514enum { PROP = 9200, PRO = 9201 };
515static int auth_any(void *c, uint32_t id, uint64_t gen, uint32_t s, uint64_t res, uint32_t rights) {
516 (void)c; (void)id; (void)gen; (void)s; (void)res; (void)rights;
517 return 0;
518}
519
520typedef struct { Env *e; int rc, fired; } StopNow;
521static void stop_live(void *ctx) {
522 StopNow *s = ctx;
523 if (s->fired++) return;
524 s->rc = stop(s->e, OP, &s->e->cr_op, s->e->c_op, 501);
525}
526static void stop_disk(const char *dir, void *ctx) { (void)dir; stop_live(ctx); }
527
528static void e5_promotion(void) {
529 Env e;
530 check(setup(&e, 1, 1u << 16) == 0, "E5 setup: second world");
531 /* A fresh world: the store's proposer and promoter are its callers. The
532 * world above is bound already; this one enrolls them before binding. */
533 teardown(&e);
534 memset(&e, 0, sizeof e);
535 RxCallerCred prop, pro;
536 char dir[64];
537 RxGenStore *gs = NULL;
538 int ok = rx_caproot_start(&e.root, &e.admin) == RX_CAP_OK &&
539 rx_world_init(&e.w, &e.root, 1, 1u << 12) == RX_OK;
540 if (ok) {
541 e.c_op = mint(&e, OP, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT);
542 ok = rx_world_enroll_caller(&e.w, OP, &e.cr_op) == RX_CALLER_OK &&
543 rx_world_enroll_caller(&e.w, PROP, &prop) == RX_CALLER_OK &&
544 rx_world_enroll_caller(&e.w, PRO, &pro) == RX_CALLER_OK &&
545 rx_world_bind_callers(&e.w) == RX_OK &&
546 mkdtemp(strcpy(dir, "/tmp/rx-estop-gen-XXXXXX")) &&
547 rx_gen_open(dir, &gs) == RX_GEN_OK &&
548 rx_gen_bind_authority(gs, rx_world_caller_check_fn, &e.w, auth_any, NULL) == RX_GEN_OK;
549 }
550 check(ok, "E5 setup: world with operator, proposer, promoter; bound generation store");
551 if (!ok) return;
552 static const uint8_t ev[] = "estop";
553 RxGenObject obj = { 1, 1, { 0 } };
554 RxGenDraft gd;
555 memset(&gd, 0, sizeof gd);
556 gd.proofs_ok = 1;
557 gd.n_objects = 1;
558 gd.objects = &obj;
559 gd.evidence = ev;
560 gd.evidence_len = sizeof ev - 1;
561 for (int v = 0; v < 2; v++) {
562 const char *where = v == 0 ? "live barrier" : "disk writes";
563 uint64_t cand = 0, a0 = 0, l0 = 0, a1 = 0, l1 = 0;
564 int prc = rx_gen_propose_as(gs, PROP, &prop, &gd, &cand);
565 rx_gen_active(gs, &a0, &l0);
566 StopNow s = { &e, -99, 0 };
567 RxPromotionRequest req = { cand, PRO, 1, 1, RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE, pro };
568 if (v == 1) rx_gen_set_disk_hook(gs, stop_disk, &s);
569 int rc = prc != RX_GEN_OK ? prc
570 : rx_gen_promote(gs, &req, NULL, NULL, NULL, NULL, v == 0 ? stop_live : NULL,
571 v == 0 ? &s : NULL);
572 if (v == 1) rx_gen_set_disk_hook(gs, NULL, NULL);
573 rx_gen_active(gs, &a1, &l1);
574 RxRecoveryRecord d;
575 memset(&d, 0, sizeof d);
576 rx_gen_recover(dir, &d);
577 char what[200];
578 snprintf(what, sizeof what, "E5 stop during the %s: stop rc %d, promote rc %d (want %d)",
579 where, s.rc, rc, RX_GEN_ERR_HALTED);
580 check(prc == RX_GEN_OK && s.rc == RX_OK && rc == RX_GEN_ERR_HALTED, what);
581 check(a1 == a0 && d.active_id == a0, "E5 active generation unchanged, in memory and on disk");
582 uint64_t c2 = 0;
583 check(rx_gen_propose_as(gs, PROP, &prop, &gd, &c2) == RX_GEN_ERR_HALTED,
584 "E5 a proposal under the stop is refused: RX_GEN_ERR_HALTED");
585 check(resume(&e, OP, &e.cr_op, e.c_op) == RX_OK, "E5 operator resumes");
586 }
587 uint64_t cand = 0, a1 = 0, l1 = 0;
588 int prc = rx_gen_propose_as(gs, PROP, &prop, &gd, &cand);
589 RxPromotionRequest req = { cand, PRO, 1, 1, RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE, pro };
590 int rc = prc == RX_GEN_OK ? rx_gen_promote(gs, &req, NULL, NULL, NULL, NULL, NULL, NULL) : prc;
591 rx_gen_active(gs, &a1, &l1);
592 check(rc == RX_GEN_OK && a1 == cand, "E5 control: after resume the same promoter promotes");
593 rx_gen_close(gs);
594 teardown(&e);
595 char cmd[128];
596 snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir);
597 if (system(cmd) != 0) fprintf(stderr, "cleanup of %s failed\n", dir);
598}
599
600/* ---- E6 durable ------------------------------------------------------------------ */
601static int read_file(const char *path, char *buf, size_t cap) {
602 int fd = open(path, O_RDONLY);
603 if (fd < 0) return -1;
604 ssize_t n = read(fd, buf, cap - 1);
605 close(fd);
606 if (n < 0) return -1;
607 buf[n] = 0;
608 return (int)n;
609}
610
611static int count_resumed(const char *dir, char *name, size_t cap) {
612 DIR *d = opendir(dir);
613 if (!d) return -1;
614 int n = 0;
615 struct dirent *de;
616 while ((de = readdir(d)))
617 if (strncmp(de->d_name, RX_GEN_HALT_MARK ".resumed.", strlen(RX_GEN_HALT_MARK) + 9) == 0) {
618 n++;
619 snprintf(name, cap, "%s", de->d_name);
620 }
621 closedir(d);
622 return n;
623}
624
625static void e6_durable(void) {
626 char dir[64], mark[160], raw[600];
627 check(mkdtemp(strcpy(dir, "/tmp/rx-estop-mark-XXXXXX")) != NULL, "E6 setup: halt directory");
628 snprintf(mark, sizeof mark, "%s/%s", dir, RX_GEN_HALT_MARK);
629 Env e;
630 check(setup(&e, 1, 1u << 16) == 0, "E6 setup: world");
631 check(rx_world_set_halt_dir(&e.w, dir) == 0, "E6 no mark: set_halt_dir answers 0, world runs");
632 RxHaltStatus h;
633 check(rx_world_emergency_stop(&e.w, OP, &e.cr_op, e.c_op, 606, &h) == RX_OK && h.durable == 1,
634 "E6 the stop wrote its mark durably (durable = 1)");
635 int n = read_file(mark, raw, sizeof raw);
636 char want_head[200];
637 snprintf(want_head, sizeof want_head, "aien-operator-halt v1\nseq 1\nsubject %u\ncap %u %llu\nreason 606\n",
638 OP, e.c_op.cap_id, U(e.c_op.generation));
639 check(n > 0 && strncmp(raw, want_head, strlen(want_head)) == 0 && strstr(raw, "\nsha256 "),
640 "E6 the mark names the operator, the capability and the reason, and is sealed");
641 teardown(&e); /* the process "restarts": a new world over the same directory */
642
643 check(setup(&e, 1, 1u << 16) == 0, "E6 restart: new world");
644 int rr = rx_world_set_halt_dir(&e.w, dir);
645 rx_world_halt_status(&e.w, &h);
646 check(rr == 1 && h.halted && h.restored && h.seq == 1 && h.subject == OP && h.reason == 606,
647 "E6 the new world starts stopped, restored from the mark (seq, operator, reason)");
648 check(publish(&e, 0, 1) == RX_ERR_HALTED, "E6 the restored stop refuses publication");
649 teardown(&e);
650
651 /* A changed byte: still stopped, reported torn. */
652 char torn[600];
653 memcpy(torn, raw, (size_t)n + 1);
654 torn[30] = torn[30] == '1' ? '2' : '1';
655 char tpath[200];
656 snprintf(tpath, sizeof tpath, "%s/torn", dir);
657 check(mkdir(tpath, 0700) == 0, "E6 setup: directory with a damaged mark");
658 char tmark[240];
659 snprintf(tmark, sizeof tmark, "%s/%s", tpath, RX_GEN_HALT_MARK);
660 FILE *f = fopen(tmark, "w");
661 if (f) { fwrite(torn, 1, (size_t)n, f); fclose(f); }
662 check(setup(&e, 1, 1u << 16) == 0, "E6 torn: new world");
663 rr = rx_world_set_halt_dir(&e.w, tpath);
664 rx_world_halt_status(&e.w, &h);
665 check(rr == RX_ERR_TORN && h.halted && h.reason == RX_ERR_TORN,
666 "E6 a mark with one byte changed still stops the world (RX_ERR_TORN)");
667 check(resume(&e, OP, &e.cr_op, e.c_op) == RX_OK && access(tmark, F_OK) != 0,
668 "E6 an authorized resume lifts the torn stop and retires its mark");
669 char rname[260], rpath[520], rbuf[1400];
670 check(count_resumed(tpath, rname, sizeof rname) == 1, "E6 the torn record is kept, not deleted");
671 snprintf(rpath, sizeof rpath, "%s/%s", tpath, rname);
672 check(read_file(rpath, rbuf, sizeof rbuf) > n && memcmp(rbuf, torn, (size_t)n) == 0,
673 "E6 the kept record starts with the damaged bytes, unchanged");
674 teardown(&e);
675
676 /* An unbound store over the directory will not promote while the mark exists. */
677 RxGenStore *gs = NULL;
678 check(rx_gen_open(dir, &gs) == RX_GEN_OK, "E6 unbound store opened over the halt directory");
679 static const uint8_t ev[] = "estop";
680 RxGenObject obj = { 1, 1, { 0 } };
681 RxGenDraft gd;
682 memset(&gd, 0, sizeof gd);
683 gd.proofs_ok = 1;
684 gd.n_objects = 1;
685 gd.objects = &obj;
686 gd.evidence = ev;
687 gd.evidence_len = sizeof ev - 1;
688 uint64_t cand = 0, a0 = 0, l0 = 0, a1 = 0, l1 = 0;
689 int prc = rx_gen_propose(gs, PROP, &gd, &cand);
690 rx_gen_active(gs, &a0, &l0);
691 RxPromotionRequest req = { cand, PRO, 1, 1, RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE, { 0, { 0 } } };
692 int rc = prc == RX_GEN_OK ? rx_gen_promote(gs, &req, auth_any, NULL, NULL, NULL, NULL, NULL) : prc;
693 rx_gen_active(gs, &a1, &l1);
694 check(rc == RX_GEN_ERR_HALTED && a1 == a0, "E6 unbound store: promotion refused by the mark");
695
696 /* Resume through a world: record kept, mark gone, the store promotes. */
697 check(setup(&e, 1, 1u << 16) == 0 && rx_world_set_halt_dir(&e.w, dir) == 1, "E6 world restored stopped");
698 if (geteuid() == 0) {
699 printf("[~] E6 read-only directory case skipped: running as root ignores the permission\n");
700 } else {
701 check(chmod(dir, 0500) == 0, "E6 setup: halt directory made read-only");
702 check(resume(&e, OP, &e.cr_op, e.c_op) == RX_ERR_IO && halted(&e) && access(mark, F_OK) == 0,
703 "E6 a resume that cannot retire the mark is refused: world stays stopped, mark stays");
704 check(chmod(dir, 0700) == 0, "E6 setup: halt directory writable again");
705 }
706 check(resume(&e, OP, &e.cr_op, e.c_op) == RX_OK && !halted(&e) && access(mark, F_OK) != 0,
707 "E6 resume: world runs, mark removed");
708 check(count_resumed(dir, rname, sizeof rname) == 1, "E6 exactly one kept stop record");
709 snprintf(rpath, sizeof rpath, "%s/%s", dir, rname);
710 int rn = read_file(rpath, rbuf, sizeof rbuf);
711 char who[120];
712 snprintf(who, sizeof who, "resumed_by %u\nresume_cap %u %llu\n", OP, e.c_op.cap_id,
713 U(e.c_op.generation));
714 check(rn > n && memcmp(rbuf, raw, (size_t)n) == 0 && strstr(rbuf + n, who) &&
715 strstr(rbuf + n, "\nsha256 "),
716 "E6 kept record = the stop's bytes + who resumed, sealed");
717 rc = rx_gen_promote(gs, &req, auth_any, NULL, NULL, NULL, NULL, NULL);
718 rx_gen_active(gs, &a1, &l1);
719 check(rc == RX_GEN_OK && a1 == cand, "E6 control: with the mark gone the store promotes");
720 rx_gen_close(gs);
721 teardown(&e);
722 char cmd[128];
723 snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir);
724 if (system(cmd) != 0) fprintf(stderr, "cleanup of %s failed\n", dir);
725}
726
727/* ---- E7 budget, create/retire, a mark that is not a file -------------------- */
728static void e7_budget_objects(void) {
729 Env e;
730 g_with_r3 = 1;
731 int su = setup(&e, 1, 1u << 16);
732 g_with_r3 = 0;
733 check(su == 0, "E7 setup: world with R3 (woken by R's commit)");
734 if (su != 0) return;
735 RxStabilityBudget sb;
736 memset(&sb, 0, sizeof sb);
737 sb.activation_budget = 1; /* one activation per reaction per episode */
738 rx_world_set_stability(&e.w, &sb);
739 pthread_mutex_lock(&g.mu);
740 g.armed3 = 1;
741 pthread_mutex_unlock(&g.mu);
742 check(publish(&e, 0, 6) > 0, "E7 setup: publish E.f0 = 6 (R runs, its commit wakes R3)");
743 check(wait_inside() == 0, "E7 setup: R3 is computing, held in its function");
744 check(stop(&e, OP, &e.cr_op, e.c_op, 707) == RX_OK, "E7 operator stop while R3 computes");
745 gate_release();
746 usleep(50 * 1000);
747 pthread_mutex_lock(&e.w.mu);
748 bool quarantined = e.w.reactions[e.id_r3].quarantined;
749 pthread_mutex_unlock(&e.w.mu);
750 check(!quarantined, "E7 the refused activation did not spend R3's episode budget (not quarantined)");
751
752 /* Create and retire are commits too: refused under the stop, nothing changes. */
753 uint8_t d0[32], d1[32];
754 digest(&e, d0);
755 uint64_t n0 = crumbs(&e);
756 uint64_t init[RX_MAX_FIELDS] = { 0 };
757 RxObjRef nr = { UINT32_MAX, 0 };
758 check(rx_world_create(&e.w, 1, RX_PERSIST_RESIDENT, 0x7ff, init, &nr) == RX_ERR_HALTED &&
759 nr.id == UINT32_MAX,
760 "E7 object create under the stop: RX_ERR_HALTED, no object");
761 check(rx_world_retire(&e.w, e.q) == RX_ERR_HALTED, "E7 object retire under the stop: RX_ERR_HALTED");
762 digest(&e, d1);
763 check(memcmp(d0, d1, 32) == 0 && crumbs(&e) == n0 && field(&e, e.q) != UINT64_MAX,
764 "E7 refused create/retire changed nothing (digest, crumbs, Q still live)");
765
766 check(resume(&e, OP, &e.cr_op, e.c_op) == RX_OK, "E7 operator resume");
767 check(wait_commits(&e, e.id_r3, 1) == 0, "E7 R3 ran again after resume and committed");
768 RxObject q;
769 check(rx_world_read(&e.w, e.q, &q) == RX_OK && q.field[1] == 700, "E7 its result: Q.f1 = (6+1)*100");
770 teardown(&e);
771
772 /* A mark that is a symlink (here dangling) is not a mark the world can read:
773 * the world starts stopped (TORN, never "absent, go"), and resume keeps a
774 * record and removes the link, so the store is not blocked for good. */
775 char dir[64], mark[160], rname[260];
776 check(mkdtemp(strcpy(dir, "/tmp/rx-estop-link-XXXXXX")) != NULL, "E7 setup: halt directory");
777 snprintf(mark, sizeof mark, "%s/%s", dir, RX_GEN_HALT_MARK);
778 check(symlink("/nonexistent/rx-estop-target", mark) == 0, "E7 setup: OPERATOR_HALT is a dangling symlink");
779 check(setup(&e, 1, 1u << 16) == 0, "E7 link: new world");
780 int rr = rx_world_set_halt_dir(&e.w, dir);
781 check(rr == RX_ERR_TORN && halted(&e), "E7 a symlink mark stops the world (RX_ERR_TORN)");
782 check(resume(&e, OP, &e.cr_op, e.c_op) == RX_OK && !halted(&e), "E7 resume lifts it");
783 struct stat st;
784 check(lstat(mark, &st) != 0 && count_resumed(dir, rname, sizeof rname) == 1,
785 "E7 the link is removed and a resumed record kept");
786 teardown(&e);
787 char cmd[128];
788 snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir);
789 if (system(cmd) != 0) fprintf(stderr, "cleanup of %s failed\n", dir);
790}
791
792int main(void) {
793 setvbuf(stdout, NULL, _IOLBF, 0);
794 Env e;
795 if (setup(&e, 1, 1u << 16) != 0) {
796 printf("[-] setup failed\nRX_EMERGENCY_STOP: FAIL\n");
797 return 1;
798 }
799 e1_refusals(&e);
800 e2_e3(&e);
801 teardown(&e);
802 if (setup(&e, 4, 1u << 21) != 0) {
803 printf("[-] setup (4 workers) failed\nRX_EMERGENCY_STOP: FAIL\n");
804 return 1;
805 }
806 e4_race(&e);
807 teardown(&e);
808 e5_promotion();
809 e6_durable();
810 e7_budget_objects();
811 printf("RX_EMERGENCY_STOP: %s (%d failures; host CPU, real caproot authority)\n",
812 g_fail ? "FAIL" : "PASS", g_fail);
813 return g_fail ? 1 : 0;
814}