interplane.Get Omega
Read in your language

aienux

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

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / svc / continuity_commit.c

406 lines · 14194 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/* continuity_commit.c -- see continuity_commit.h. */
18#include <string.h>
19
20#include "continuity_commit.h"
21#include "../../store/store_v1.h"
22
23static int map_cc(int rc, const char *why_in, const char **why)
24{
25 if (rc == CC_OK) return 0;
26 if (why) *why = why_in ? why_in : "continuity object";
27 if (rc == CC_E_CORRUPT) return CR_CORRUPT;
28 if (rc == CC_E_LIMIT) return CR_LIMIT;
29 return CR_E_ARG;
30}
31
32static int is_zero32(const uint8_t *p)
33{
34 uint8_t a = 0;
35 for (int i = 0; i < 32; i++) a |= p[i];
36 return a == 0;
37}
38
39/* INV-8: ONE transaction. (MC-8 mutant: the manifest, always last, goes alone.) */
40static int do_transact(const struct cr_sink *snk, const struct cr_wobj *o, size_t n, int *store_rc)
41{
42 int rc;
43#ifdef CM_MUTANT_SPLIT_TXN
44 if (n > 1) {
45 rc = snk->transact(snk->ctx, o, n - 1);
46 if (rc == 0) rc = snk->transact(snk->ctx, o + n - 1, 1);
47 } else
48#endif
49 rc = snk->transact(snk->ctx, o, n);
50 if (rc != 0) {
51 if (store_rc) *store_rc = rc;
52 return CR_STORE;
53 }
54 return 0;
55}
56
57static int writable(const struct cr_source *src)
58{
59#ifdef CM_MUTANT_NO_WRITABLE_CHECK
60 (void)src;
61 return CR_RESOLVED;
62#else
63 return cr_writable(src);
64#endif
65}
66
67static int args_ok(const struct cr_source *src, const struct cr_sink *snk, const struct cr_work *w,
68 const struct cr_txwork *tw, const struct cr_view *out)
69{
70 return src && snk && snk->transact && w && tw && out;
71}
72
73int cr_provision(const struct cr_source *src, const struct cr_sink *snk, struct cr_work *w,
74 struct cr_txwork *tw, struct ck_rng *rng, const uint8_t store_uuid[16],
75 uint8_t source, struct cr_view *out, const char **why, int *store_rc)
76{
77 if (why) *why = NULL;
78 if (store_rc) *store_rc = 0;
79 if (!args_ok(src, snk, w, tw, out) || !rng || !store_uuid || !src->generation ||
80 (source != CC_SOURCE_OPERATOR && source != CC_SOURCE_QUALIFICATION))
81 return CR_E_ARG;
82 memset(out, 0, sizeof *out);
83
84 int o = writable(src); /* INV-11: before anything else */
85 if (o != CR_RESOLVED) return CR_READ_ONLY;
86
87 /* Provision only from Unprovisioned: a root (also two) is AlreadyProvisioned;
88 * orphans, a broken graph or a store error are refused with that outcome. */
89 o = cr_resolve(src, w, &tw->tmp, why, store_rc);
90#ifdef CM_MUTANT_PROVISION_WITH_ROOT
91 if (o == CR_RESOLVED || o == CR_CONFLICT) o = CR_UNPROVISIONED; /* MC-7 */
92#endif
93 if (o == CR_RESOLVED || o == CR_CONFLICT) return CR_ALREADY_PROVISIONED;
94 if (o != CR_UNPROVISIONED) return o;
95
96 uint8_t agent[32];
97#ifdef CM_MUTANT_FIXED_AGENT
98 memset(agent, 0x42, sizeof agent); /* MC-10 */
99 (void)rng;
100#else
101 if (ck_rng_fill(rng, agent, sizeof agent) != 0) return CR_NO_ENTROPY; /* no fallback */
102#endif
103 if (is_zero32(agent)) {
104 if (why) *why = "zero agent id";
105 return CR_CORRUPT;
106 }
107 uint64_t gen = src->generation(src->ctx);
108 if (gen == UINT64_MAX) {
109 if (why) *why = "store generation";
110 return CR_LIMIT;
111 }
112
113 struct cc_root root;
114 memset(&root, 0, sizeof root);
115 memcpy(root.agent_id, agent, 32);
116 memcpy(root.store_uuid, store_uuid, 16);
117 cc_root_branch_id(agent, root.root_branch);
118 root.provisioned_generation = gen + 1;
119 root.source = source;
120 uint8_t root_id[32], state_id[32];
121 size_t n_root = 0, n_state = 0, n_man = 0;
122 const char *y = NULL;
123 int rc = cc_root_encode(&root, tw->b[0], sizeof tw->b[0], &n_root, &y);
124 if ((o = map_cc(rc, y, why))) return o;
125 if (cc_object_id(CC_KIND_AGENT_ROOT, tw->b[0], n_root, root_id) != CC_OK) return CR_E_ARG;
126 cc_state_genesis(&tw->state, agent, 1);
127 rc = cc_state_encode(&tw->state, tw->b[1], sizeof tw->b[1], &n_state, &y);
128 if ((o = map_cc(rc, y, why))) return o;
129 if (cc_object_id(CC_KIND_AGENT_STATE, tw->b[1], n_state, state_id) != CC_OK) return CR_E_ARG;
130
131 memset(&tw->man, 0, sizeof tw->man);
132 memcpy(tw->man.root, root_id, 32);
133 tw->man.sequence = 1;
134 tw->man.incarnation = 1;
135 memcpy(tw->man.agent_state, state_id, 32);
136 rc = cc_manifest_encode(&tw->man, tw->b[2], sizeof tw->b[2], &n_man, &y);
137 if ((o = map_cc(rc, y, why))) return o;
138
139 struct cr_wobj objs[3] = {
140 {CC_KIND_AGENT_ROOT, CC_STORE_OBJECT_VERSION, tw->b[0], n_root},
141 {CC_KIND_AGENT_STATE, CC_STORE_OBJECT_VERSION, tw->b[1], n_state},
142 {CC_KIND_MANIFEST, CC_STORE_OBJECT_VERSION, tw->b[2], n_man},
143 };
144 if ((o = do_transact(snk, objs, 3, store_rc))) return o;
145 return cr_resolve(src, w, out, why, store_rc);
146}
147
148int cr_commit(const struct cr_source *src, const struct cr_sink *snk, struct cr_work *w,
149 struct cr_txwork *tw, const struct cr_view *cur, const struct cr_update *up,
150 int new_incarnation, struct cr_view *out, const char **why, int *store_rc)
151{
152 if (why) *why = NULL;
153 if (store_rc) *store_rc = 0;
154 if (!args_ok(src, snk, w, tw, out) || !cur || !up || cur == out) return CR_E_ARG;
155 if (up->n_rec && !up->rec) return CR_E_ARG;
156 memset(out, 0, sizeof *out);
157
158 int o = writable(src);
159 if (o != CR_RESOLVED) return CR_READ_ONLY;
160
161 if (cur->manifest.sequence == UINT64_MAX) {
162 if (why) *why = "manifest sequence";
163 return CR_LIMIT;
164 }
165 uint64_t seq = cur->manifest.sequence + 1;
166 struct cr_wobj objs[3];
167 size_t n = 0, lens[3] = {0, 0, 0};
168 const char *y = NULL;
169 int rc;
170
171 memset(&tw->man, 0, sizeof tw->man);
172 memcpy(tw->man.agent_state, cur->manifest.agent_state, 32);
173 tw->man.n_wal = cur->manifest.n_wal;
174 memcpy(tw->man.wal, cur->manifest.wal, sizeof tw->man.wal);
175
176 if (up->state) {
177 if (memcmp(up->state->agent_id, cur->root.agent_id, 32) != 0) {
178 if (why) *why = "agent state belongs to another agent";
179 return CR_CORRUPT;
180 }
181 memcpy(&tw->state, up->state, sizeof tw->state);
182 tw->state.written_at = seq; /* INV-9 */
183 rc = cc_state_encode(&tw->state, tw->b[n], sizeof tw->b[n], &lens[n], &y);
184 if ((o = map_cc(rc, y, why))) return o;
185 if (cc_object_id(CC_KIND_AGENT_STATE, tw->b[n], lens[n], tw->man.agent_state) != CC_OK)
186 return CR_E_ARG;
187 objs[n] = (struct cr_wobj){CC_KIND_AGENT_STATE, CC_STORE_OBJECT_VERSION, tw->b[n], lens[n]};
188 n++;
189 }
190
191 if (up->n_rec) {
192 if (cur->manifest.n_wal >= CC_MAX_WAL_SEGMENTS) {
193 if (why) *why = "cortex WAL needs compaction";
194 return CR_LIMIT;
195 }
196 if (up->n_rec > CC_MAX_WAL_RECORDS) {
197 if (why) *why = "WAL segment record count";
198 return CR_LIMIT;
199 }
200 memset(&tw->wal, 0, sizeof tw->wal);
201 tw->wal.sequence = seq;
202 tw->wal.n = up->n_rec;
203 for (uint32_t i = 0; i < up->n_rec; i++) tw->wal.rec[i] = up->rec[i];
204 rc = cc_wal_encode(&tw->wal, tw->b[n], sizeof tw->b[n], &lens[n], &y);
205 if ((o = map_cc(rc, y, why))) return o;
206 if (cc_object_id(CC_KIND_CORTEX_WAL, tw->b[n], lens[n], tw->man.wal[tw->man.n_wal]) != CC_OK)
207 return CR_E_ARG;
208 tw->man.n_wal++;
209 objs[n] = (struct cr_wobj){CC_KIND_CORTEX_WAL, CC_STORE_OBJECT_VERSION, tw->b[n], lens[n]};
210 n++;
211 }
212
213 uint64_t inc = cur->manifest.incarnation;
214 if (new_incarnation) {
215 if (inc == UINT64_MAX) {
216 if (why) *why = "incarnation";
217 return CR_LIMIT;
218 }
219 inc++;
220 }
221 memcpy(tw->man.root, cur->root_id, 32);
222 memcpy(tw->man.previous, cur->manifest_id, 32);
223 tw->man.sequence = seq;
224 tw->man.incarnation = inc;
225 rc = cc_manifest_encode(&tw->man, tw->b[n], sizeof tw->b[n], &lens[n], &y);
226 if ((o = map_cc(rc, y, why))) return o;
227 objs[n] = (struct cr_wobj){CC_KIND_MANIFEST, CC_STORE_OBJECT_VERSION, tw->b[n], lens[n]};
228 n++;
229
230 if ((o = do_transact(snk, objs, n, store_rc))) return o;
231 return cr_resolve(src, w, out, why, store_rc);
232}
233
234int cr_resume(const struct cr_source *src, const struct cr_sink *snk, struct cr_work *w,
235 struct cr_txwork *tw, struct cr_view *out, int *committed, const char **why,
236 int *store_rc)
237{
238 if (committed) *committed = 0;
239 if (why) *why = NULL;
240 if (store_rc) *store_rc = 0;
241 if (!args_ok(src, snk, w, tw, out)) return CR_E_ARG;
242 struct cr_view *cur = &tw->tmp;
243 int o = cr_resolve(src, w, cur, why, store_rc);
244#ifdef CM_MUTANT_RESUME_PROVISIONS
245 if (o == CR_UNPROVISIONED) { /* MC-1: a resume that mints an identity */
246 static const uint8_t fake_uuid[16] = {0};
247 uint8_t agent[32];
248 memset(agent, 0x99, sizeof agent);
249 struct cc_root root;
250 memset(&root, 0, sizeof root);
251 memcpy(root.agent_id, agent, 32);
252 memcpy(root.store_uuid, fake_uuid, 16);
253 cc_root_branch_id(agent, root.root_branch);
254 root.provisioned_generation = src->generation ? src->generation(src->ctx) + 1 : 1;
255 root.source = CC_SOURCE_OPERATOR;
256 uint8_t rid[32], sid[32];
257 size_t nr = 0, ns = 0, nm = 0;
258 cc_root_encode(&root, tw->b[0], sizeof tw->b[0], &nr, NULL);
259 cc_object_id(CC_KIND_AGENT_ROOT, tw->b[0], nr, rid);
260 cc_state_genesis(&tw->state, agent, 1);
261 cc_state_encode(&tw->state, tw->b[1], sizeof tw->b[1], &ns, NULL);
262 cc_object_id(CC_KIND_AGENT_STATE, tw->b[1], ns, sid);
263 memset(&tw->man, 0, sizeof tw->man);
264 memcpy(tw->man.root, rid, 32);
265 tw->man.sequence = 1;
266 tw->man.incarnation = 1;
267 memcpy(tw->man.agent_state, sid, 32);
268 cc_manifest_encode(&tw->man, tw->b[2], sizeof tw->b[2], &nm, NULL);
269 struct cr_wobj objs[3] = {
270 {CC_KIND_AGENT_ROOT, 1, tw->b[0], nr},
271 {CC_KIND_AGENT_STATE, 1, tw->b[1], ns},
272 {CC_KIND_MANIFEST, 1, tw->b[2], nm},
273 };
274 if (do_transact(snk, objs, 3, store_rc) == 0) o = cr_resolve(src, w, cur, why, store_rc);
275 }
276#endif
277 if (o != CR_RESOLVED) {
278 memset(out, 0, sizeof *out);
279 return o; /* INV-4: Unprovisioned stays Unprovisioned, nothing written */
280 }
281 if (cr_writable(src) != CR_RESOLVED) { /* INV-11: verified view, no commit */
282 memcpy(out, cur, sizeof *out);
283 return CR_RESOLVED;
284 }
285#ifdef CM_MUTANT_RESUME_NO_COMMIT
286 memcpy(out, cur, sizeof *out); /* MC-3: no incarnation + 1 */
287 if (committed) *committed = 1;
288 return CR_RESOLVED;
289#else
290 static const struct cr_update none = {NULL, NULL, 0};
291 o = cr_commit(src, snk, w, tw, cur, &none, 1, out, why, store_rc);
292 if (o == CR_RESOLVED && committed) *committed = 1;
293 return o;
294#endif
295}
296
297/* ---- markers (TEST-ONLY serial lines of continuity_boot.c; a CK_SERVICE image
298 * prints its own lowercase "continuity:" lines and must not carry the
299 * "CONTINUITY: " prefix: Makefile cont_check) ---- */
300#if !defined(CK_SERVICE)
301struct mb {
302 char *p;
303 size_t cap, n;
304 int over;
305};
306static void mb_s(struct mb *b, const char *s)
307{
308 for (; *s; s++) {
309 if (b->n + 1 < b->cap) b->p[b->n] = *s;
310 else b->over = 1;
311 b->n++;
312 }
313}
314static void mb_u(struct mb *b, uint64_t v)
315{
316 char t[24];
317 int i = 0;
318 do t[i++] = (char)('0' + v % 10);
319 while ((v /= 10) != 0);
320 char o[24];
321 for (int k = 0; k < i; k++) o[k] = t[i - 1 - k];
322 o[i] = 0;
323 mb_s(b, o);
324}
325static void mb_i(struct mb *b, int v)
326{
327 if (v < 0) {
328 mb_s(b, "-");
329 mb_u(b, (uint64_t)0 - (uint64_t)(int64_t)v);
330 } else
331 mb_u(b, (uint64_t)v);
332}
333static void mb_hex(struct mb *b, const uint8_t *p, size_t n)
334{
335 static const char H[] = "0123456789abcdef";
336 char t[3] = {0, 0, 0};
337 for (size_t i = 0; i < n; i++) {
338 t[0] = H[p[i] >> 4];
339 t[1] = H[p[i] & 15];
340 mb_s(b, t);
341 }
342}
343static size_t mb_done(struct mb *b)
344{
345 if (!b->cap) return 0;
346 if (b->over) {
347 b->p[0] = 0;
348 return 0;
349 }
350 b->p[b->n] = 0;
351 return b->n;
352}
353
354size_t cr_marker_view(char *out, size_t cap, const char *word, const struct cr_view *v)
355{
356 struct mb b = {out, cap, 0, 0};
357 if (!out || !word || !v) return 0;
358 mb_s(&b, "CONTINUITY: ");
359 mb_s(&b, word);
360 mb_s(&b, " agent=");
361 mb_hex(&b, v->root.agent_id, 32);
362 mb_s(&b, " incarnation=");
363 mb_u(&b, v->manifest.incarnation);
364 mb_s(&b, " sequence=");
365 mb_u(&b, v->manifest.sequence);
366 mb_s(&b, " cortex=");
367 mb_u(&b, v->cortex_count);
368 mb_s(&b, " branches=");
369 mb_u(&b, v->state.n);
370 mb_s(&b, " memory=");
371 mb_hex(&b, v->memory, 8);
372 return mb_done(&b);
373}
374
375size_t cr_marker_outcome(char *out, size_t cap, int outcome, const char *why, int store_rc)
376{
377 struct mb b = {out, cap, 0, 0};
378 if (!out) return 0;
379 switch (outcome) {
380 case CR_UNPROVISIONED: mb_s(&b, "CONTINUITY: UNPROVISIONED"); break;
381 case CR_CONFLICT: mb_s(&b, "CONTINUITY: CONFLICT"); break;
382 case CR_NO_ENTROPY: mb_s(&b, "CONTINUITY: NO_ENTROPY"); break;
383 case CR_CORRUPT:
384 mb_s(&b, "CONTINUITY: CORRUPT (");
385 mb_s(&b, why ? why : "continuity unreadable");
386 mb_s(&b, ")");
387 break;
388 case CR_LIMIT:
389 mb_s(&b, "CONTINUITY: STOP (Limit(\"");
390 mb_s(&b, why ? why : "");
391 mb_s(&b, "\"))");
392 break;
393 case CR_STORE:
394 mb_s(&b, "CONTINUITY: STOP (Store(");
395 mb_i(&b, store_rc);
396 mb_s(&b, "))");
397 break;
398 default:
399 mb_s(&b, "CONTINUITY: STOP (");
400 mb_s(&b, cr_outcome_name(outcome));
401 mb_s(&b, ")");
402 break;
403 }
404 return mb_done(&b);
405}
406#endif /* !CK_SERVICE */