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

901 lines · 37182 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 * Qualification for the generation barrier. The important cases are a stop
19 * after every persistence step, and the refusals around promotion authority.
20 * The Linux checker and the native authority must agree on the promotion right.
21 */
22#include "runtime/aienos_cap.h"
23#include "runtime/rx_caproot.h"
24#include "runtime/rx_generation.h"
25#include "runtime/rx_world.h"
26#include "omega_evidence.h"
27#include "sha256.h"
28
29#include <errno.h>
30#include <pthread.h>
31#include <signal.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/stat.h>
36#include <sys/utsname.h>
37#include <sys/wait.h>
38#include <unistd.h>
39
40#define SUBJ_PROPOSER 8u
41#define SUBJ_AUTHORITY 4u
42#define RES_LIVE 0x10ull
43
44static int g_fail;
45static int g_checks;
46
47#define CHECK(cond, ...) \
48 do { \
49 g_checks++; \
50 if (!(cond)) { \
51 g_fail++; \
52 fprintf(stderr, "FAIL %s:%d ", __FILE__, __LINE__); \
53 fprintf(stderr, __VA_ARGS__); \
54 fputc('\n', stderr); \
55 } \
56 } while (0)
57
58typedef struct {
59 const char *name;
60 int linux_rc;
61 int native_rc;
62} DiffRow;
63
64static DiffRow g_diff[16];
65static int g_ndiff;
66
67static void expect_eq(const char *name, int linux_rc, int native_rc) {
68 CHECK(linux_rc == native_rc, "%s linux %d native %d", name, linux_rc, native_rc);
69 if (linux_rc == native_rc && g_ndiff < (int)(sizeof g_diff / sizeof g_diff[0])) {
70 g_diff[g_ndiff].name = name;
71 g_diff[g_ndiff].linux_rc = linux_rc;
72 g_diff[g_ndiff].native_rc = native_rc;
73 g_ndiff++;
74 }
75}
76
77typedef struct {
78 int step;
79 int coherent;
80 uint64_t lineage;
81 int receipt_after;
82 char evidence[32];
83} CrashRow;
84
85static CrashRow g_crash[8];
86static int g_ncrash;
87
88typedef struct {
89 RxCapRoot root;
90 RxCapAdmin admin;
91 AienosCapAdmin *native_admin;
92 AienosCapView *native_view;
93} Auth;
94
95static int auth_start(Auth *a) {
96 memset(a, 0, sizeof(*a));
97 if (rx_caproot_start(&a->root, &a->admin) != RX_CAP_OK) return -1;
98 if (aienos_cap_start(&a->native_admin, &a->native_view) != 0) {
99 rx_caproot_stop(&a->root, &a->admin);
100 return -1;
101 }
102 return 0;
103}
104
105static void auth_stop(Auth *a) {
106 aienos_cap_stop(a->native_admin, a->native_view);
107 rx_caproot_stop(&a->root, &a->admin);
108}
109
110static int mint_both(Auth *a, uint32_t subject, uint32_t rights, RxCapRef *linux_out,
111 AienosCapRef *native_out) {
112 RxCapMint m;
113 memset(&m, 0, sizeof m);
114 m.issuer = 3;
115 m.subject = subject;
116 m.resource = RX_GEN_RES_PROMOTION;
117 m.rights = rights;
118 m.parent = (RxCapRef){UINT32_MAX, 0};
119 m.authority = rx_capadmin_office(&a->admin);
120 int lrc = rx_capadmin_mint(&a->admin, &m, linux_out);
121 AienosCapMint n;
122 memset(&n, 0, sizeof n);
123 n.issuer = 3;
124 n.subject = subject;
125 n.resource = RX_GEN_RES_PROMOTION;
126 n.rights = rights;
127 n.parent.cap_id = UINT32_MAX;
128 if (aienos_cap_office(a->native_admin, &n.authority) != 0) return -1;
129 int nrc = aienos_cap_mint(a->native_admin, &n, native_out);
130 if (lrc != nrc) return -1;
131 return lrc;
132}
133
134static int native_auth(void *ctx, uint32_t cap_id, uint64_t cap_generation, uint32_t subject,
135 uint64_t resource, uint32_t rights) {
136 Auth *a = ctx;
137 AienosCapRef ref = {cap_id, cap_generation};
138 AienosCapEntry entry;
139 return aienos_cap_validate(a->native_view, ref, subject, resource, rights, &entry);
140}
141
142static void draft_init(RxGenDraft *d, const uint8_t *evidence, size_t n) {
143 memset(d, 0, sizeof(*d));
144 d->authority_epoch = 1;
145 d->authority_generation = 1;
146 d->proofs_ok = 1;
147 d->evidence = evidence;
148 d->evidence_len = n;
149 static const uint8_t model[] = "model-unused";
150 static const uint8_t realization[] = "realization";
151 static const uint8_t config[] = "config";
152 static const uint8_t provenance[] = "host-proof";
153 d->model = model;
154 d->model_len = sizeof model - 1;
155 d->realization = realization;
156 d->realization_len = sizeof realization - 1;
157 d->config = config;
158 d->config_len = sizeof config - 1;
159 d->provenance = provenance;
160 d->provenance_len = sizeof provenance - 1;
161}
162
163static int read_text(const char *path, char *buf, size_t n) {
164 FILE *f = fopen(path, "rb");
165 if (!f) return -1;
166 size_t got = fread(buf, 1, n - 1, f);
167 fclose(f);
168 buf[got] = 0;
169 return (int)got;
170}
171
172static int evidence_of(const char *dir, uint64_t id, char *buf, size_t n) {
173 char path[512];
174 snprintf(path, sizeof path, "%s/g/%llu/evidence", dir, (unsigned long long)id);
175 return read_text(path, buf, n);
176}
177
178static void rm_tree(const char *dir) {
179 char cmd[640];
180 snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir);
181 int rc = system(cmd);
182 (void)rc;
183}
184
185static int fresh_dir(char *out, size_t n) {
186 char tmpl[] = "/tmp/r9-barrier-XXXXXX";
187 char *dir = mkdtemp(tmpl);
188 if (!dir) return -1;
189 if (strlen(dir) + 1 > n) return -1;
190 memcpy(out, dir, strlen(dir) + 1);
191 return 0;
192}
193
194static RxPromotionRequest request_for(uint64_t id, uint32_t subject, AienosCapRef cap) {
195 RxPromotionRequest r;
196 memset(&r, 0, sizeof r);
197 r.candidate_id = id;
198 r.subject = subject;
199 r.cap_id = cap.cap_id;
200 r.cap_generation = cap.generation;
201 r.resource = RX_GEN_RES_PROMOTION;
202 r.rights = RX_GEN_RIGHT_PROMOTE;
203 return r;
204}
205
206static void authority_differential(void) {
207 Auth a;
208 CHECK(auth_start(&a) == 0, "authority did not start");
209 RxCapRef linux_cap = {0, 0};
210 AienosCapRef native_cap = {0, 0};
211 int mrc = mint_both(&a, SUBJ_AUTHORITY, RX_RIGHT_PROMOTE, &linux_cap, &native_cap);
212 CHECK(mrc == RX_CAP_OK, "promote mint failed %d", mrc);
213 RxCapEntry lentry;
214 AienosCapEntry nentry;
215 int lrc = rx_caproot_validate(&a.root, linux_cap, SUBJ_AUTHORITY, RX_GEN_RES_PROMOTION,
216 RX_RIGHT_PROMOTE, &lentry);
217 int nrc = aienos_cap_validate(a.native_view, native_cap, SUBJ_AUTHORITY, RX_GEN_RES_PROMOTION,
218 RX_RIGHT_PROMOTE, &nentry);
219 expect_eq("promote right validates", lrc, nrc);
220
221 RxCapRef bad_l = {0, 0};
222 AienosCapRef bad_n = {0, 0};
223 RxCapMint badm;
224 memset(&badm, 0, sizeof badm);
225 badm.issuer = 3;
226 badm.subject = SUBJ_AUTHORITY;
227 badm.resource = RX_GEN_RES_PROMOTION;
228 badm.rights = RX_RIGHT_PROMOTE | RX_RIGHT_DELEGATE;
229 badm.parent = (RxCapRef){UINT32_MAX, 0};
230 badm.authority = rx_capadmin_office(&a.admin);
231 lrc = rx_capadmin_mint(&a.admin, &badm, &bad_l);
232 AienosCapMint nbad;
233 memset(&nbad, 0, sizeof nbad);
234 nbad.issuer = 3;
235 nbad.subject = SUBJ_AUTHORITY;
236 nbad.resource = RX_GEN_RES_PROMOTION;
237 nbad.rights = RX_RIGHT_PROMOTE | RX_RIGHT_DELEGATE;
238 nbad.parent.cap_id = UINT32_MAX;
239 aienos_cap_office(a.native_admin, &nbad.authority);
240 nrc = aienos_cap_mint(a.native_admin, &nbad, &bad_n);
241 expect_eq("promote cannot be combined with hand-off", lrc, nrc);
242
243 RxCapMint child;
244 memset(&child, 0, sizeof child);
245 child.issuer = SUBJ_AUTHORITY;
246 child.subject = 5;
247 child.resource = RX_GEN_RES_PROMOTION;
248 child.rights = RX_RIGHT_PROMOTE;
249 child.parent = linux_cap;
250 child.authority = linux_cap;
251 RxCapRef cout = {0, 0};
252 lrc = rx_capadmin_mint(&a.admin, &child, &cout);
253 AienosCapMint nchild;
254 memset(&nchild, 0, sizeof nchild);
255 nchild.issuer = SUBJ_AUTHORITY;
256 nchild.subject = 5;
257 nchild.resource = RX_GEN_RES_PROMOTION;
258 nchild.rights = RX_RIGHT_PROMOTE;
259 nchild.parent = native_cap;
260 nchild.authority = native_cap;
261 AienosCapRef nout = {0, 0};
262 nrc = aienos_cap_mint(a.native_admin, &nchild, &nout);
263 expect_eq("promote right cannot be handed on", lrc, nrc);
264
265 lrc = rx_capadmin_revoke(&a.admin, rx_capadmin_office(&a.admin), linux_cap);
266 AienosCapRef office;
267 aienos_cap_office(a.native_admin, &office);
268 nrc = aienos_cap_revoke(a.native_admin, office, native_cap);
269 expect_eq("revoke promotion right", lrc, nrc);
270 lrc = rx_caproot_validate(&a.root, linux_cap, SUBJ_AUTHORITY, RX_GEN_RES_PROMOTION,
271 RX_RIGHT_PROMOTE, &lentry);
272 nrc = aienos_cap_validate(a.native_view, native_cap, SUBJ_AUTHORITY, RX_GEN_RES_PROMOTION,
273 RX_RIGHT_PROMOTE, &nentry);
274 expect_eq("revoked promotion right", lrc, nrc);
275
276 AienosCapMint forged;
277 memset(&forged, 0, sizeof forged);
278 forged.subject = SUBJ_PROPOSER;
279 forged.resource = RX_GEN_RES_PROMOTION;
280 forged.rights = RX_RIGHT_PROMOTE;
281 forged.parent.cap_id = UINT32_MAX;
282 uint8_t token[32];
283 memset(token, 0, sizeof token);
284 nrc = aienos_cap_cognition_mint(a.native_view, &forged, token);
285 expect_eq("reaction cannot mint a promotion", RX_CAP_ERR_UNAUTHORIZED, nrc);
286 auth_stop(&a);
287}
288
289static int child_crash(const char *dir, int step) {
290 Auth a;
291 if (auth_start(&a) != 0) return 2;
292 RxCapRef ignore = {0, 0};
293 AienosCapRef cap = {0, 0};
294 if (mint_both(&a, SUBJ_AUTHORITY, RX_RIGHT_PROMOTE, &ignore, &cap) != RX_CAP_OK) return 3;
295 RxGenStore *store = NULL;
296 if (rx_gen_open(dir, &store) != RX_GEN_OK) return 4;
297 const uint8_t evidence[] = "CANDIDATE";
298 RxGenObject obj;
299 memset(&obj, 0, sizeof obj);
300 obj.id = 1;
301 obj.generation = 1;
302 obj.digest[0] = 9;
303 RxGenDraft draft;
304 draft_init(&draft, evidence, sizeof evidence - 1);
305 draft.objects = &obj;
306 draft.n_objects = 1;
307 uint64_t id = 0;
308 if (rx_gen_propose(store, SUBJ_PROPOSER, &draft, &id) != RX_GEN_OK) return 5;
309 RxGenWork effect = {.id = 42, .class = RX_WORK_EXTERNAL,
310 .state = RX_WORK_ISSUED, .required = 0};
311 if (rx_gen_add_work(store, id, &effect) != RX_GEN_OK) return 6;
312 rx_gen_set_crash(store, step);
313 RxPromotionRequest req = request_for(id, SUBJ_AUTHORITY, cap);
314 int rc = rx_gen_promote(store, &req, native_auth, &a, NULL, NULL, NULL, NULL);
315 auth_stop(&a);
316 rx_gen_close(store);
317 return rc == RX_GEN_OK ? 0 : 7;
318}
319
320static int child_busy(const char *dir) {
321 Auth a;
322 if (auth_start(&a) != 0) return 21;
323 RxCapRef ignore = {0, 0};
324 AienosCapRef cap = {0, 0};
325 if (mint_both(&a, SUBJ_AUTHORITY, RX_RIGHT_PROMOTE, &ignore, &cap) != RX_CAP_OK) return 21;
326 RxGenStore *store = NULL;
327 if (rx_gen_open(dir, &store) != RX_GEN_OK) return 21;
328 const uint8_t evidence[] = "OTHER";
329 RxGenDraft draft;
330 draft_init(&draft, evidence, sizeof evidence - 1);
331 uint64_t id = 0;
332 if (rx_gen_propose(store, SUBJ_PROPOSER, &draft, &id) != RX_GEN_OK) return 21;
333 RxPromotionRequest req = request_for(id, SUBJ_AUTHORITY, cap);
334 int rc = rx_gen_promote(store, &req, native_auth, &a, NULL, NULL, NULL, NULL);
335 auth_stop(&a);
336 rx_gen_close(store);
337 return rc == RX_GEN_ERR_BUSY ? 20 : 21;
338}
339
340static int run_child(const char *argv0, const char *dir, const char *mode, int step) {
341 pid_t pid = fork();
342 if (pid < 0) return -1;
343 if (pid == 0) {
344 char step_text[16];
345 snprintf(step_text, sizeof step_text, "%d", step);
346 execl(argv0, argv0, mode, dir, step_text, (char *)NULL);
347 _exit(127);
348 }
349 int status = 0;
350 if (waitpid(pid, &status, 0) < 0) return -1;
351 if (!WIFEXITED(status)) return -1;
352 return WEXITSTATUS(status);
353}
354
355static void check_crash(const char *argv0, int step, uint64_t want_lineage, const char *want_evidence,
356 int receipt_before_recover) {
357 char dir[64];
358 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
359 int code = run_child(argv0, dir, "--crash", step);
360 CHECK(code == 86, "crash step %d exited %d", step, code);
361 uint64_t id_guess = want_lineage == 1 ? 1 : 2;
362 char path[512];
363 snprintf(path, sizeof path, "%s/g/%llu/receipt", dir, (unsigned long long)id_guess);
364 int receipt_before = access(path, R_OK) == 0;
365 if (want_lineage == 1) {
366 CHECK(!receipt_before, "step %d wrote a receipt for a generation that is not active", step);
367 } else if (!receipt_before_recover) {
368 CHECK(!receipt_before, "step %d wrote the receipt before the receipt step", step);
369 } else {
370 CHECK(receipt_before, "step %d was missing the receipt", step);
371 }
372 RxRecoveryRecord rec;
373 int rc = rx_gen_recover(dir, &rec);
374 CHECK(rc == RX_GEN_OK, "step %d recovery %d", step, rc);
375 CHECK(rec.coherent, "step %d incoherent", step);
376 CHECK(rec.lineage == want_lineage, "step %d lineage %llu", step,
377 (unsigned long long)rec.lineage);
378 char evidence[32];
379 CHECK(evidence_of(dir, rec.active_id, evidence, sizeof evidence) > 0, "step %d evidence missing",
380 step);
381 CHECK(strcmp(evidence, want_evidence) == 0, "step %d evidence '%s'", step, evidence);
382 CHECK(strcmp(evidence, "GENESIS") == 0 || strcmp(evidence, "CANDIDATE") == 0,
383 "step %d mixed evidence", step);
384 if (want_lineage == 2) {
385 CHECK(rec.receipt_present, "step %d receipt not completed", step);
386 CHECK(rec.event_present, "step %d change was not published", step);
387 CHECK(rx_gen_reject_replay(dir, 42) == RX_GEN_ERR_REPLAY, "step %d replayed an effect", step);
388 } else {
389 CHECK(rx_gen_reject_replay(dir, 42) == RX_GEN_OK, "step %d recorded an effect that was not committed",
390 step);
391 }
392 if (g_ncrash < 8) {
393 g_crash[g_ncrash].step = step;
394 g_crash[g_ncrash].coherent = rec.coherent;
395 g_crash[g_ncrash].lineage = rec.lineage;
396 g_crash[g_ncrash].receipt_after = rec.receipt_present;
397 snprintf(g_crash[g_ncrash].evidence, sizeof g_crash[0].evidence, "%s", evidence);
398 g_ncrash++;
399 }
400 rm_tree(dir);
401}
402
403static RxWorld g_world;
404static RxCapRoot g_root;
405static RxCapAdmin g_admin;
406static RxCapRef g_live_cap;
407static RxObjRef g_live_obj;
408static volatile int g_stop;
409static volatile int g_live_ok;
410static volatile int g_live_bad;
411static RxGenStore *g_store;
412static uint64_t g_cand;
413static int g_closed_mutation;
414
415static void *live_thread(void *arg) {
416 (void)arg;
417 uint64_t value = 1;
418 while (!g_stop) {
419 RxMutation m = {g_live_obj, 0, value++};
420 int64_t id = rx_world_publish_external(&g_world, g_live_cap, &m, 1);
421 if (id >= 0) g_live_ok++;
422 else g_live_bad++;
423 }
424 return NULL;
425}
426
427static void on_live(void *ctx) {
428 (void)ctx;
429 const uint8_t nope[] = "nope";
430 g_closed_mutation = rx_gen_set_evidence(g_store, g_cand, nope, sizeof nope - 1);
431 RxMutation m = {g_live_obj, 0, 99};
432 int64_t id = rx_world_publish_external(&g_world, g_live_cap, &m, 1);
433 if (id >= 0) g_live_ok++;
434 else g_live_bad++;
435}
436
437typedef struct {
438 RxGenStore *store;
439 uint64_t candidate;
440} DrainCtx;
441
442static int on_drain(uint64_t work_id, void *ctx) {
443 DrainCtx *d = ctx;
444 const uint8_t text[] = "DRAINED";
445 if (rx_gen_set_evidence(d->store, d->candidate, text, sizeof text - 1) != RX_GEN_OK) return -1;
446 return rx_gen_finish_work(d->store, d->candidate, work_id);
447}
448
449static void flip_evidence(const char *generation_dir, void *ctx) {
450 (void)ctx;
451 char path[512];
452 snprintf(path, sizeof path, "%s/evidence", generation_dir);
453 FILE *f = fopen(path, "r+b");
454 if (!f) return;
455 int byte = fgetc(f);
456 if (byte >= 0) {
457 fseek(f, 0, SEEK_SET);
458 fputc(byte ^ 0xff, f);
459 }
460 fclose(f);
461}
462
463static void drop_objects(const char *generation_dir, void *ctx) {
464 (void)ctx;
465 char path[512];
466 snprintf(path, sizeof path, "%s/objects", generation_dir);
467 unlink(path);
468}
469
470static int promote_ok(Auth *a, RxGenStore *store, uint64_t id, AienosCapRef cap, RxGenDrainFn drain,
471 void *drain_ctx, RxGenLiveFn live) {
472 RxPromotionRequest req = request_for(id, SUBJ_AUTHORITY, cap);
473 return rx_gen_promote(store, &req, native_auth, a, drain, drain_ctx, live, NULL);
474}
475
476static void negative_and_live(void) {
477 Auth a;
478 CHECK(auth_start(&a) == 0, "authority did not start");
479 CHECK(rx_caproot_start(&g_root, &g_admin) == RX_CAP_OK, "live world authority");
480 CHECK(rx_world_init(&g_world, &g_root, 1, 1u << 16) == RX_OK, "live world");
481 g_world.external_subject = 100;
482 uint64_t init[RX_MAX_FIELDS] = {0};
483 CHECK(rx_world_create(&g_world, 1, RX_PERSIST_RESIDENT, RES_LIVE, init, &g_live_obj) == RX_OK,
484 "live object");
485 RxCapMint wm;
486 memset(&wm, 0, sizeof wm);
487 wm.issuer = 3;
488 wm.subject = 100;
489 wm.resource = RES_LIVE;
490 wm.rights = RX_RIGHT_WRITE;
491 wm.parent = (RxCapRef){UINT32_MAX, 0};
492 wm.authority = rx_capadmin_office(&g_admin);
493 CHECK(rx_capadmin_mint(&g_admin, &wm, &g_live_cap) == RX_CAP_OK, "live write right");
494
495 RxCapRef linux_cap = {0, 0};
496 AienosCapRef cap = {0, 0};
497 CHECK(mint_both(&a, SUBJ_AUTHORITY, RX_RIGHT_PROMOTE, &linux_cap, &cap) == RX_CAP_OK,
498 "authority promote right");
499 RxCapRef self_l = {0, 0};
500 AienosCapRef self_n = {0, 0};
501 CHECK(mint_both(&a, SUBJ_PROPOSER, RX_RIGHT_PROMOTE, &self_l, &self_n) == RX_CAP_OK,
502 "proposer was given a promotion right for the refusal test");
503
504 char dir[64];
505 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
506 RxGenStore *store = NULL;
507 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open");
508 g_store = store;
509 const uint8_t evidence[] = "CANDIDATE";
510 RxGenObject obj;
511 memset(&obj, 0, sizeof obj);
512 obj.id = 7;
513 obj.generation = 1;
514 obj.digest[0] = 3;
515 RxGenDraft draft;
516 draft_init(&draft, evidence, sizeof evidence - 1);
517 draft.objects = &obj;
518 draft.n_objects = 1;
519 uint64_t first = 0, second = 0;
520 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &first) == RX_GEN_OK, "first candidate");
521 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &second) == RX_GEN_OK, "second candidate");
522 g_cand = first;
523 RxGenWork ephemeral = {.id = 1, .class = RX_WORK_EPHEMERAL,
524 .state = RX_WORK_PENDING, .required = 0};
525 RxGenWork optional = {.id = 7, .class = RX_WORK_EVIDENCE,
526 .state = RX_WORK_PENDING, .required = 0};
527 RxGenWork required = {.id = 3, .class = RX_WORK_EVIDENCE,
528 .state = RX_WORK_PENDING, .required = 1};
529 RxGenWork effect = {.id = 11, .class = RX_WORK_EXTERNAL,
530 .state = RX_WORK_ISSUED, .required = 0};
531 CHECK(rx_gen_add_work(store, first, &ephemeral) == RX_GEN_OK, "ephemeral");
532 CHECK(rx_gen_add_work(store, first, &optional) == RX_GEN_OK, "optional evidence");
533 CHECK(rx_gen_add_work(store, first, &required) == RX_GEN_OK, "required evidence");
534 CHECK(rx_gen_add_work(store, first, &effect) == RX_GEN_OK, "external effect");
535
536 RxPromotionRequest self = request_for(first, SUBJ_PROPOSER, self_n);
537 CHECK(rx_gen_promote(store, &self, native_auth, &a, NULL, NULL, NULL, NULL) ==
538 RX_GEN_ERR_AUTHORITY,
539 "candidate authorized its own promotion");
540 uint64_t active = 0, lineage = 0;
541 rx_gen_active(store, &active, &lineage);
542 CHECK(lineage == 1, "self-promotion changed the active generation");
543
544 rx_gen_close(store);
545 rm_tree(dir);
546 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
547 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "reopen");
548 g_store = store;
549 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &first) == RX_GEN_OK, "candidate");
550 g_cand = first;
551 AienosCapRef office;
552 aienos_cap_office(a.native_admin, &office);
553 CHECK(aienos_cap_revoke(a.native_admin, office, cap) == 0, "revoke");
554 CHECK(promote_ok(&a, store, first, cap, NULL, NULL, NULL) == RX_GEN_ERR_AUTHORITY,
555 "revoked authority promoted");
556 rx_gen_active(store, &active, &lineage);
557 CHECK(lineage == 1, "revoked promotion changed the generation");
558
559 /* A fresh authority, because the previous one was revoked. */
560 auth_stop(&a);
561 CHECK(auth_start(&a) == 0, "restart authority");
562 CHECK(mint_both(&a, SUBJ_AUTHORITY, RX_RIGHT_PROMOTE, &linux_cap, &cap) == RX_CAP_OK, "remint");
563 CHECK(mint_both(&a, SUBJ_PROPOSER, RX_RIGHT_PROMOTE, &self_l, &self_n) == RX_CAP_OK, "remint self");
564 rx_gen_close(store);
565 rm_tree(dir);
566 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
567 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open for the live promotion");
568 g_store = store;
569 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &first) == RX_GEN_OK, "live candidate");
570 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &second) == RX_GEN_OK, "sibling candidate");
571 g_cand = first;
572 CHECK(rx_gen_add_work(store, first, &ephemeral) == RX_GEN_OK, "ephemeral");
573 CHECK(rx_gen_add_work(store, first, &optional) == RX_GEN_OK, "optional");
574 CHECK(rx_gen_add_work(store, first, &required) == RX_GEN_OK, "required");
575 CHECK(rx_gen_add_work(store, first, &effect) == RX_GEN_OK, "effect");
576 self = request_for(first, SUBJ_PROPOSER, self_n);
577 CHECK(rx_gen_promote(store, &self, native_auth, &a, NULL, NULL, NULL, NULL) == RX_GEN_ERR_AUTHORITY,
578 "candidate authorized itself");
579
580 g_stop = 0;
581 g_live_ok = 0;
582 g_live_bad = 0;
583 g_closed_mutation = 0;
584 pthread_t thread;
585 CHECK(pthread_create(&thread, NULL, live_thread, NULL) == 0, "live thread");
586 DrainCtx drain = {store, first};
587 int prc = promote_ok(&a, store, first, cap, on_drain, &drain, on_live);
588 RxGenPhases phases;
589 CHECK(rx_gen_last_phases(store, &phases) == RX_GEN_OK,
590 "promotion timing was recorded");
591 CHECK(phases.candidate_id == first && phases.result == prc &&
592 phases.enter_ns > 0 && phases.enter_ns <= phases.barrier_ns &&
593 phases.barrier_ns <= phases.flip_ns && phases.flip_ns <= phases.receipt_ns,
594 "promotion phases follow the durable order");
595 g_stop = 1;
596 pthread_join(thread, NULL);
597 CHECK(prc == RX_GEN_OK, "promotion failed %d", prc);
598 CHECK(g_closed_mutation == RX_GEN_ERR_CLOSING, "candidate accepted a change while closing");
599 CHECK(g_live_ok > 0 && g_live_bad == 0, "live reactions stopped or failed (%d ok, %d bad)",
600 g_live_ok, g_live_bad);
601 rx_gen_active(store, &active, &lineage);
602 CHECK(lineage == 2 && active == first, "active lineage %llu", (unsigned long long)lineage);
603 char ev[32];
604 CHECK(evidence_of(dir, active, ev, sizeof ev) > 0, "evidence missing");
605 CHECK(strcmp(ev, "DRAINED") == 0, "evidence was '%s'", ev);
606 RxRecoveryRecord rec;
607 CHECK(rx_gen_recover(dir, &rec) == RX_GEN_OK, "recover after promotion");
608 CHECK(rec.n_excluded == 1 && rec.excluded_ids[0] == 7, "optional evidence was not excluded");
609 CHECK(rec.n_external == 1 && rec.external_ids[0] == 11, "external effect was not frozen");
610 CHECK(rx_gen_reject_replay(dir, 11) == RX_GEN_ERR_REPLAY, "effect was replayed");
611 int events = 0;
612 char epath[512];
613 snprintf(epath, sizeof epath, "%s/events", dir);
614 FILE *ef = fopen(epath, "r");
615 unsigned long long eid = 0, elin = 0;
616 if (ef) {
617 while (fscanf(ef, "%llu %llu", &eid, &elin) == 2) events++;
618 fclose(ef);
619 }
620 CHECK(events == 1, "generation change was published %d times", events);
621 CHECK(promote_ok(&a, store, second, cap, NULL, NULL, NULL) == RX_GEN_ERR_STALE,
622 "second candidate from the same parent was promoted");
623
624 /* Stale object generation, before any commit. */
625 rx_gen_close(store);
626 rm_tree(dir);
627 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
628 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open");
629 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &first) == RX_GEN_OK, "stale candidate");
630 CHECK(rx_gen_observe_object(store, first, 0, 1) == RX_GEN_OK, "observe");
631 uint8_t changed[32];
632 memset(changed, 4, sizeof changed);
633 CHECK(rx_gen_mutate_object(store, first, 0, 2, changed) == RX_GEN_OK, "mutate");
634 CHECK(promote_ok(&a, store, first, cap, NULL, NULL, NULL) == RX_GEN_ERR_STALE,
635 "stale object generation was promoted");
636 rx_gen_active(store, &active, &lineage);
637 CHECK(lineage == 1, "stale object changed the generation");
638
639 /* Missing proof. */
640 rx_gen_close(store);
641 rm_tree(dir);
642 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
643 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open");
644 draft.proofs_ok = 0;
645 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &first) == RX_GEN_OK, "unproven");
646 CHECK(promote_ok(&a, store, first, cap, NULL, NULL, NULL) == RX_GEN_ERR_VERIFY,
647 "missing proof was promoted");
648 draft.proofs_ok = 1;
649
650 /* Evidence changed after the freeze, and a missing object. */
651 rx_gen_close(store);
652 rm_tree(dir);
653 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
654 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open");
655 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &first) == RX_GEN_OK, "tamper candidate");
656 rx_gen_set_disk_hook(store, flip_evidence, NULL);
657 CHECK(promote_ok(&a, store, first, cap, NULL, NULL, NULL) == RX_GEN_ERR_TORN,
658 "changed evidence was promoted");
659 rx_gen_active(store, &active, &lineage);
660 CHECK(lineage == 1, "tampered evidence changed the generation");
661 char genesis[32];
662 CHECK(evidence_of(dir, 1, genesis, sizeof genesis) > 0, "genesis evidence");
663 CHECK(strcmp(genesis, "GENESIS") == 0, "genesis evidence changed");
664
665 rx_gen_close(store);
666 rm_tree(dir);
667 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
668 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open");
669 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &first) == RX_GEN_OK, "missing object");
670 rx_gen_set_disk_hook(store, drop_objects, NULL);
671 CHECK(promote_ok(&a, store, first, cap, NULL, NULL, NULL) == RX_GEN_ERR_MISSING,
672 "missing object was promoted");
673
674 /* Corrupt the committed root. Recovery must refuse rather than mix. */
675 rx_gen_close(store);
676 rm_tree(dir);
677 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
678 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open");
679 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &first) == RX_GEN_OK, "corrupt candidate");
680 rx_gen_set_disk_hook(store, NULL, NULL);
681 CHECK(promote_ok(&a, store, first, cap, NULL, NULL, NULL) == RX_GEN_OK, "commit before corruption");
682 rx_gen_close(store);
683 char root_path[512];
684 snprintf(root_path, sizeof root_path, "%s/g/%llu/root", dir, (unsigned long long)first);
685 FILE *rf = fopen(root_path, "r+b");
686 CHECK(rf != NULL, "root file");
687 if (rf) {
688 fseek(rf, 100, SEEK_SET);
689 int byte = fgetc(rf);
690 fseek(rf, 100, SEEK_SET);
691 fputc(byte ^ 0xff, rf);
692 fclose(rf);
693 }
694 CHECK(rx_gen_recover(dir, &rec) == RX_GEN_ERR_TORN, "corrupt checkpoint was accepted");
695 CHECK(evidence_of(dir, 1, genesis, sizeof genesis) > 0, "parent evidence");
696 CHECK(strcmp(genesis, "GENESIS") == 0, "parent was rewritten after corruption");
697
698 /* Two processes. One holds the barrier; the other must wait outside it. */
699 rm_tree(dir);
700 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
701 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open");
702 CHECK(rx_gen_hold_barrier(store) == RX_GEN_OK, "hold");
703 char exe[512];
704 ssize_t nread = readlink("/proc/self/exe", exe, sizeof exe - 1);
705 CHECK(nread > 0, "exe");
706 if (nread > 0) exe[nread] = 0;
707 int busy = run_child(exe, dir, "--busy", 0);
708 CHECK(busy == 20, "concurrent promotion returned %d", busy);
709 CHECK(rx_gen_release_barrier(store) == RX_GEN_OK, "release");
710 rx_gen_close(store);
711 rm_tree(dir);
712
713 rx_world_destroy(&g_world);
714 rx_caproot_stop(&g_root, &g_admin);
715 auth_stop(&a);
716}
717
718static void binary_digest(char out[65]) {
719 strcpy(out, "unavailable");
720 FILE *f = fopen("/proc/self/exe", "rb");
721 if (!f) return;
722 sha256_ctx c;
723 sha256_init(&c);
724 uint8_t buf[65536];
725 size_t n;
726 while ((n = fread(buf, 1, sizeof buf, f)) > 0) sha256_update(&c, buf, n);
727 fclose(f);
728 uint8_t d[32];
729 sha256_final(&c, d);
730 for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]);
731}
732
733static void write_receipt(void) {
734 char path[512];
735 if (omega_evidence_path("R9/rx_generation_barrier_receipt.json", path, sizeof path) != 0) return;
736 FILE *f = fopen(path, "w");
737 if (!f) return;
738 char commit[41];
739 memset(commit, 0, sizeof commit);
740 if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8);
741 const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT");
742 const char *aienos = getenv("AIENOS_COMMIT");
743 int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 &&
744 !omega_evidence_tree_dirty();
745 char digest[65];
746 binary_digest(digest);
747 struct utsname u;
748 memset(&u, 0, sizeof u);
749 uname(&u);
750 fprintf(f,
751 "{\n"
752 " \"schema\": \"AIEN_RX_R9_GENERATION_BARRIER_V1\",\n"
753 " \"run_id\": \"%s\",\n"
754 " \"candidate_commit\": %s%s%s,\n"
755 " \"candidate_bound\": %s,\n"
756 " \"run_commit\": \"%s\",\n"
757 " \"tree_dirty\": %s,\n"
758 " \"aienos_commit\": %s%s%s,\n"
759 " \"checks\": %d,\n"
760 " \"failures\": %d,\n"
761 " \"test_binary_sha256\": \"%s\",\n"
762 " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\"},\n"
763 " \"hardware_scope\": \"host CPU only; no graphics processor; Linux authority checker retained\",\n"
764 " \"gates\": {\n"
765 " \"R9_GENERATION_BARRIER\": \"PASS (host; one coherent generation after every injected stop)\",\n"
766 " \"not_claimed\": [\"R8\", \"R10\", \"R11\", \"R12 silicon\", \"R13\"]\n"
767 " },\n"
768 " \"differential\": [\n",
769 omega_evidence_run_id(), candidate ? "\"" : "", candidate ? candidate : "null",
770 candidate ? "\"" : "", bound ? "true" : "false", commit,
771 omega_evidence_tree_dirty() ? "true" : "false", aienos ? "\"" : "",
772 aienos ? aienos : "null", aienos ? "\"" : "", g_checks, g_fail, digest, u.sysname,
773 u.release, u.machine);
774 for (int i = 0; i < g_ndiff; i++) {
775 fprintf(f, " {\"name\": \"%s\", \"linux\": %d, \"native\": %d}%s\n", g_diff[i].name,
776 g_diff[i].linux_rc, g_diff[i].native_rc, i + 1 < g_ndiff ? "," : "");
777 }
778 fprintf(f, " ],\n \"crashes\": [\n");
779 for (int i = 0; i < g_ncrash; i++) {
780 fprintf(f,
781 " {\"step\": %d, \"coherent\": %s, \"lineage\": %llu, \"evidence\": \"%s\", "
782 "\"receipt\": %s}%s\n",
783 g_crash[i].step, g_crash[i].coherent ? "true" : "false",
784 (unsigned long long)g_crash[i].lineage, g_crash[i].evidence,
785 g_crash[i].receipt_after ? "true" : "false", i + 1 < g_ncrash ? "," : "");
786 }
787 fprintf(f, " ]\n}\n");
788 fclose(f);
789 printf("receipt: %s\n", path);
790 printf("candidate bound: %s\n", bound ? "yes" : "no");
791}
792
793/* Regression (R14). A living body promotes again and again. The store has
794 * four draft slots; before the fix they were never freed, so the fifth
795 * proposal in one process was refused (RX_GEN_ERR_BUSY) forever. A finished
796 * draft (promoted, or stale because its parent is gone) frees its slot and
797 * can never be promoted again. */
798static void drafts_are_released(void) {
799 Auth a;
800 CHECK(auth_start(&a) == 0, "authority did not start");
801 RxCapRef linux_cap = {0, 0};
802 AienosCapRef cap = {0, 0};
803 CHECK(mint_both(&a, SUBJ_AUTHORITY, RX_RIGHT_PROMOTE, &linux_cap, &cap) == RX_CAP_OK,
804 "promote right");
805 char dir[64];
806 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
807 RxGenStore *store = NULL;
808 CHECK(rx_gen_open(dir, &store) == RX_GEN_OK, "open");
809 if (!store) { auth_stop(&a); return; }
810 const uint8_t evidence[] = "AGAIN";
811 RxGenDraft draft;
812 draft_init(&draft, evidence, sizeof evidence - 1);
813 uint64_t id = 0, active = 0, lineage = 0;
814 for (int i = 0; i < 6; i++) {
815 int prc = rx_gen_propose(store, SUBJ_PROPOSER, &draft, &id);
816 CHECK(prc == RX_GEN_OK, "proposal %d refused (%d)", i + 1, prc);
817 if (prc != RX_GEN_OK) break;
818 CHECK(promote_ok(&a, store, id, cap, NULL, NULL, NULL) == RX_GEN_OK,
819 "promotion %d refused", i + 1);
820 }
821 rx_gen_active(store, &active, &lineage);
822 CHECK(lineage == 7 && active == id, "six promotions reached lineage %llu",
823 (unsigned long long)lineage);
824 CHECK(promote_ok(&a, store, id, cap, NULL, NULL, NULL) == RX_GEN_ERR_ARG,
825 "a promoted draft was offered for promotion again");
826 uint64_t x = 0, y = 0;
827 CHECK(rx_gen_propose(store, SUBJ_PROPOSER, &draft, &x) == RX_GEN_OK &&
828 rx_gen_propose(store, SUBJ_PROPOSER, &draft, &y) == RX_GEN_OK, "two drafts");
829 CHECK(promote_ok(&a, store, x, cap, NULL, NULL, NULL) == RX_GEN_OK, "first of two");
830 CHECK(promote_ok(&a, store, y, cap, NULL, NULL, NULL) == RX_GEN_ERR_STALE, "stale second");
831 CHECK(promote_ok(&a, store, y, cap, NULL, NULL, NULL) == RX_GEN_ERR_ARG,
832 "a stale draft stayed promotable");
833 RxRecoveryRecord rec;
834 CHECK(rx_gen_recover(dir, &rec) == RX_GEN_OK && rec.coherent && rec.active_id == x &&
835 rec.lineage == 8, "recovery after the drafts");
836 rx_gen_close(store);
837 rm_tree(dir);
838 auth_stop(&a);
839}
840
841/* A committed generation's blob is read through its root: the right bytes,
842 * and a refusal when the file no longer matches (R14 restart path). */
843static void read_blob_checked(void) {
844 char dir[256];
845 CHECK(fresh_dir(dir, sizeof dir) == 0, "temp directory");
846 RxGenStore *s = NULL;
847 CHECK(rx_gen_open(dir, &s) == RX_GEN_OK && s, "open");
848 if (!s) return;
849 uint8_t buf[64];
850 size_t n = 0;
851 CHECK(rx_gen_read_blob(s, 1, "evidence", buf, sizeof buf, &n) == RX_GEN_OK && n == 7 &&
852 memcmp(buf, "GENESIS", 7) == 0, "genesis evidence was not read back");
853 CHECK(rx_gen_read_blob(s, 1, "nonsense", buf, sizeof buf, &n) == RX_GEN_ERR_ARG,
854 "an unknown blob name was accepted");
855 CHECK(rx_gen_read_blob(s, 1, "evidence", buf, 3, &n) == RX_GEN_ERR_ARG,
856 "a blob larger than the buffer was accepted");
857 CHECK(rx_gen_read_blob(s, 9, "evidence", buf, sizeof buf, &n) == RX_GEN_ERR_MISSING,
858 "a generation that does not exist was read");
859 char path[512];
860 snprintf(path, sizeof path, "%s/g/1/evidence", dir);
861 FILE *f = fopen(path, "wb");
862 if (f) { fwrite("GENESIX", 1, 7, f); fclose(f); }
863 CHECK(rx_gen_read_blob(s, 1, "evidence", buf, sizeof buf, &n) == RX_GEN_ERR_TORN,
864 "a changed blob was read as the committed one");
865 rx_gen_close(s);
866 rm_tree(dir);
867}
868
869int main(int argc, char **argv) {
870 signal(SIGPIPE, SIG_IGN);
871 if (argc >= 4 && strcmp(argv[1], "--crash") == 0) return child_crash(argv[2], atoi(argv[3]));
872 if (argc >= 3 && strcmp(argv[1], "--busy") == 0) return child_busy(argv[2]);
873
874 char exe[512];
875 ssize_t nread = readlink("/proc/self/exe", exe, sizeof exe - 1);
876 if (nread < 0) return 1;
877 exe[nread] = 0;
878
879 authority_differential();
880 const int steps[] = {
881 RX_CRASH_BEFORE_CANDIDATE_WRITE, RX_CRASH_DURING_CANDIDATE_WRITE,
882 RX_CRASH_AFTER_CANDIDATE_WRITE, RX_CRASH_BEFORE_ROOT_FLIP,
883 RX_CRASH_DURING_ROOT_FLIP, RX_CRASH_AFTER_ROOT_FLIP,
884 RX_CRASH_BEFORE_RECEIPT, RX_CRASH_AFTER_RECEIPT};
885 const uint64_t want[] = {1, 1, 1, 1, 1, 2, 2, 2};
886 const char *text[] = {"GENESIS", "GENESIS", "GENESIS", "GENESIS",
887 "GENESIS", "CANDIDATE", "CANDIDATE", "CANDIDATE"};
888 const int receipt_already[] = {0, 0, 0, 0, 0, 0, 0, 1};
889 for (int i = 0; i < 8; i++)
890 check_crash(exe, steps[i], want[i], text[i], receipt_already[i]);
891 negative_and_live();
892 read_blob_checked();
893 drafts_are_released();
894 if (g_fail) {
895 fprintf(stderr, "%d generation-barrier failures\n", g_fail);
896 return 1;
897 }
898 write_receipt();
899 printf("generation barrier kept a single coherent generation through every injected stop\n");
900 return 0;
901}