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 / artifact_store.c

417 lines · 15556 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/* artifact_store.c -- see artifact_store.h. */
18#include "artifact_store.h"
19#include <stddef.h>
20#include "ck.h"
21#include "sha256.h"
22
23static void az(void *p, size_t n)
24{
25 uint8_t *b = p;
26 while (n--) *b++ = 0;
27}
28static void acp(void *d, const void *s, size_t n)
29{
30 uint8_t *o = d;
31 const uint8_t *i = s;
32 while (n--) *o++ = *i++;
33}
34static int aeq(const void *a, const void *b, size_t n)
35{
36 const uint8_t *x = a, *y = b;
37 for (size_t i = 0; i < n; i++)
38 if (x[i] != y[i]) return 0;
39 return 1;
40}
41static uint32_t g32(const uint8_t *p)
42{
43 return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
44}
45static size_t slen(const char *s)
46{
47 size_t n = 0;
48 while (s[n]) n++;
49 return n;
50}
51static uint16_t g16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); }
52#ifdef CK_ART_STORE_WRITER
53static void p32(uint8_t *p, uint32_t v)
54{
55 for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i));
56}
57static void p16(uint8_t *p, uint16_t v)
58{
59 p[0] = (uint8_t)v;
60 p[1] = (uint8_t)(v >> 8);
61}
62#endif
63
64static const uint8_t IX_MAGIC[8] = {'A', 'I', 'E', 'N', 'A', 'I', 'X', '1'};
65static const uint8_t CH_MAGIC[8] = {'A', 'I', 'E', 'N', 'A', 'C', 'H', '1'};
66
67static uint32_t chunks_for(uint32_t len) { return (len + CK_ART_CHUNK_DATA - 1u) / CK_ART_CHUNK_DATA; }
68static uint32_t chunk_size(uint32_t len, uint32_t i)
69{
70 uint32_t off = i * CK_ART_CHUNK_DATA, rest = len - off;
71 return rest < CK_ART_CHUNK_DATA ? rest : CK_ART_CHUNK_DATA;
72}
73
74/* name: 1..32 bytes of printable ASCII without space, zero padded to 32. */
75static int name_ok(const uint8_t *nlen, const uint8_t *name)
76{
77 if (*nlen == 0 || *nlen > CK_ART_NAME_MAX) return 0;
78 for (uint32_t i = 0; i < CK_ART_NAME_MAX; i++) {
79 if (i < *nlen) {
80 if (name[i] < 0x21 || name[i] > 0x7e) return 0;
81 } else if (name[i]) {
82 return 0;
83 }
84 }
85 return 1;
86}
87
88void ck_art_set_free(ck_art_set *set)
89{
90 if (!set) return;
91 for (uint32_t i = 0; i < CK_ART_MAX_ENTRIES; i++)
92 if (set->e[i].bytes) {
93 ck_free(set->e[i].bytes);
94 set->e[i].bytes = 0;
95 }
96}
97
98static int bad(ck_art_set *set, const char **why, const char *w, uint8_t *buf, uint64_t *gens)
99{
100 ck_art_set_free(set);
101 az(set, sizeof *set);
102 if (why) *why = w;
103 if (buf) ck_free(buf);
104 if (gens) ck_free(gens);
105 return -1;
106}
107
108int ck_art_collect(ss_store *s, ck_art_set *set, const char **why)
109{
110 if (why) *why = "ok";
111 if (!set) return -1;
112 az(set, sizeof *set);
113 if (!s || !s->ws) {
114 if (why) *why = "no Store";
115 return -1;
116 }
117 ss_workspace *ws = s->ws;
118
119 /* Newest index = highest Store generation among index-kind claims. */
120 int best = -1;
121 for (uint32_t k = 0; k < s->nclaims; k++) {
122 const ss_claim *c = &ws->claims[k];
123 if (c->c.obj.object_kind != CK_ART_INDEX_KIND) continue;
124 if (best < 0 || c->c.store_generation > ws->claims[best].c.store_generation) best = (int)k;
125 }
126 if (best < 0) return 0;
127
128 uint8_t *buf = ck_alloc(SS_MAX_PLAINTEXT);
129 uint64_t *gens = ck_alloc(sizeof(uint64_t) * CK_ART_MAX_ENTRIES * CK_ART_MAX_CHUNKS);
130 if (!buf || !gens) return bad(set, why, "out of memory", buf, gens);
131 az(gens, sizeof(uint64_t) * CK_ART_MAX_ENTRIES * CK_ART_MAX_CHUNKS);
132
133 uint8_t sid[32];
134 size_t len = 0;
135 uint16_t kind = 0;
136 acp(sid, ws->claims[best].sid, 32);
137 uint64_t igen = ws->claims[best].c.store_generation;
138 if (ss_read(s, sid, buf, SS_MAX_PLAINTEXT, &len, &kind) != 0 || kind != CK_ART_INDEX_KIND)
139 return bad(set, why, "index object read failed", buf, gens);
140 if (len < CK_ART_INDEX_HDR || !aeq(buf, IX_MAGIC, 8) || g32(buf + 8) != CK_ART_VERSION)
141 return bad(set, why, "index malformed (header)", buf, gens);
142 uint32_t n = g32(buf + 12);
143 if (n > CK_ART_MAX_ENTRIES || len != CK_ART_INDEX_HDR + (size_t)n * CK_ART_INDEX_ENTRY)
144 return bad(set, why, "index malformed (count/length)", buf, gens);
145 set->found = 1;
146 set->index_generation = igen;
147 sha256_hash(buf, len, set->index_sha256);
148 set->count = n;
149 for (uint32_t i = 0; i < n; i++) {
150 const uint8_t *e = buf + CK_ART_INDEX_HDR + (size_t)i * CK_ART_INDEX_ENTRY;
151 ck_art_entry *a = &set->e[i];
152 if (!name_ok(e, e + 4) || e[1] || e[2] || e[3] || g16(e + 42) || g32(e + 44))
153 return bad(set, why, "index malformed (entry)", buf, gens);
154 uint32_t blen = g32(e + 36);
155 uint16_t ch = g16(e + 40);
156 if (blen == 0) return bad(set, why, "index malformed (empty artifact)", buf, gens);
157 acp(a->name, e + 4, e[0]);
158 a->name[e[0]] = 0;
159 for (uint32_t j = 0; j < i; j++)
160 if (aeq(set->e[j].name, a->name, CK_ART_NAME_MAX + 1))
161 return bad(set, why, "index malformed (duplicate name)", buf, gens);
162 a->len = blen;
163 a->chunks = ch;
164 acp(a->want, e + 48, 32);
165 if (blen > CK_ART_MAX_BYTES) {
166 a->state = CK_ART_TOO_LARGE; /* never read; the loader refuses it */
167 continue;
168 }
169 if (ch != chunks_for(blen)) return bad(set, why, "index malformed (chunk count)", buf, gens);
170 a->state = CK_ART_MISSING; /* until every chunk is found */
171 a->bytes = ck_alloc(blen);
172 if (!a->bytes) return bad(set, why, "out of memory", buf, gens);
173 az(a->bytes, blen);
174 }
175
176 /* Chunks: committed no later than the index, matching an entry exactly.
177 * For one (entry, chunk) the newest generation wins. */
178 for (uint32_t k = 0; k < s->nclaims; k++) {
179 const ss_claim *c = &ws->claims[k];
180 if (c->c.obj.object_kind != CK_ART_CHUNK_KIND) continue;
181 uint64_t g = c->c.store_generation;
182 if (g > igen) {
183 set->chunks_ignored++;
184 continue;
185 }
186 acp(sid, c->sid, 32);
187 if (ss_read(s, sid, buf, SS_MAX_PLAINTEXT, &len, &kind) != 0 || kind != CK_ART_CHUNK_KIND)
188 return bad(set, why, "chunk object read failed", buf, gens);
189 int used = 0;
190 if (len >= CK_ART_CHUNK_HDR && aeq(buf, CH_MAGIC, 8) && g32(buf + 8) == CK_ART_VERSION &&
191 name_ok(buf + 20, buf + 24) && !buf[21] && !buf[22] && !buf[23]) {
192 uint32_t tl = g32(buf + 12);
193 uint16_t ci = g16(buf + 16), cc = g16(buf + 18);
194 for (uint32_t i = 0; i < n; i++) {
195 ck_art_entry *a = &set->e[i];
196 if (a->state == CK_ART_TOO_LARGE || !a->bytes) continue;
197 if (buf[20] != (uint8_t)slen(a->name) || !aeq(buf + 24, a->name, buf[20])) continue;
198 if (!aeq(buf + 56, a->want, 32)) break; /* another write's chunk */
199 if (tl != a->len || cc != a->chunks || ci >= cc) break;
200 uint32_t sz = chunk_size(tl, ci);
201 if (len != CK_ART_CHUNK_HDR + sz) break;
202 uint64_t *slot = &gens[i * CK_ART_MAX_CHUNKS + ci];
203 if (*slot && *slot >= g) break; /* an equal or newer copy already won */
204 if (!*slot) a->present++;
205 *slot = g;
206 acp(a->bytes + (size_t)ci * CK_ART_CHUNK_DATA, buf + CK_ART_CHUNK_HDR, sz);
207 used = 1;
208 break;
209 }
210 }
211 if (used) set->chunks_used++;
212 else set->chunks_ignored++;
213 }
214 for (uint32_t i = 0; i < n; i++) {
215 ck_art_entry *a = &set->e[i];
216 if (a->state == CK_ART_TOO_LARGE) continue;
217 if (a->present == a->chunks) sha256_hash(a->bytes, a->len, a->sha256);
218 if (a->present == a->chunks && aeq(a->sha256, a->want, 32)) {
219 a->state = CK_ART_OK;
220 } else {
221 if (a->present == a->chunks) a->state = CK_ART_MISMATCH;
222 ck_free(a->bytes);
223 a->bytes = 0;
224 }
225 }
226 az(buf, SS_MAX_PLAINTEXT);
227 ck_free(buf);
228 ck_free(gens);
229 return 0;
230}
231
232/* ---- kernel stage glue ---------------------------------------------------- */
233static ck_art_set *g_set;
234static struct ck_disk_artifact g_out[CK_ART_MAX_ENTRIES];
235static struct ck_disk_artifacts g_res;
236static int g_loaded;
237
238static void hex(char *o, const uint8_t *b, size_t n)
239{
240 static const char d[] = "0123456789abcdef";
241 for (size_t i = 0; i < n; i++) {
242 o[2 * i] = d[b[i] >> 4];
243 o[2 * i + 1] = d[b[i] & 15];
244 }
245 o[2 * n] = 0;
246}
247
248void ck_art_stage_load(ss_store *s, const char *refusal)
249{
250 char h[65];
251 g_loaded = 1;
252 az(&g_res, sizeof g_res);
253 g_res.a = g_out;
254 if (!s) {
255 g_res.why = "boot disk Store refused";
256 ck_printf("artifact_disk: none (Store refused: %s); no artifact bytes read\n", refusal ? refusal : "?");
257 return;
258 }
259 if (!g_set) g_set = ck_alloc(sizeof *g_set);
260 if (!g_set) {
261 g_res.why = "out of memory";
262 ck_printf("artifact_disk: none (out of memory)\n");
263 return;
264 }
265 ck_art_set_free(g_set); /* bytes of an earlier load (host tests) */
266 az(g_out, sizeof g_out);
267 const char *why = 0;
268 int rc = ck_art_collect(s, g_set, &why);
269 if (rc < 0) {
270 g_res.why = "artifact index refused";
271 ck_printf("artifact_disk: REFUSED (%s); no artifact used\n", why);
272 return;
273 }
274 if (!g_set->found) {
275 g_res.why = "no artifact index in the boot disk Store";
276 ck_printf("artifact_disk: none (no artifact index in the boot disk Store)\n");
277 return;
278 }
279 hex(h, g_set->index_sha256, 32);
280 ck_printf("artifact_disk: index generation=%llu entries=%u chunks_used=%u chunks_ignored=%u sha256=%s\n",
281 (unsigned long long)g_set->index_generation, g_set->count, g_set->chunks_used,
282 g_set->chunks_ignored, h);
283 for (uint32_t i = 0; i < g_set->count; i++) {
284 const ck_art_entry *a = &g_set->e[i];
285 g_out[i].name = a->name;
286 g_out[i].len = a->len;
287 g_out[i].bytes = a->state == CK_ART_OK ? a->bytes : 0;
288 g_out[i].state = a->state == CK_ART_OK ? CK_DISK_ART_OK :
289 a->state == CK_ART_TOO_LARGE ? CK_DISK_ART_TOO_LARGE : CK_DISK_ART_MISSING;
290 if (a->state == CK_ART_OK) {
291 hex(h, a->sha256, 32);
292 ck_printf("artifact_disk: %s bytes=%u sha256=%s\n", a->name, a->len, h);
293 } else if (a->state == CK_ART_TOO_LARGE) {
294 ck_printf("artifact_disk: %s bytes=%u too-large (limit %u); not read\n", a->name, a->len,
295 CK_ART_MAX_BYTES);
296 } else if (a->state == CK_ART_MISMATCH) {
297 ck_printf("artifact_disk: %s bytes=%u assembled sha256 differs from the index; not used\n",
298 a->name, a->len);
299 } else {
300 ck_printf("artifact_disk: %s bytes=%u missing chunks=%u/%u; not used\n", a->name, a->len,
301 a->present, a->chunks);
302 }
303 }
304 g_res.available = 1;
305 g_res.why = "ok";
306 g_res.generation = g_set->index_generation;
307 g_res.count = g_set->count;
308}
309
310int ck_stage_disk_artifacts(struct ck_disk_artifacts *out)
311{
312 if (!out || !g_loaded) return -1;
313 *out = g_res;
314 return 0;
315}
316
317void ck_stage_disk_artifacts_free(void)
318{
319 if (g_set) {
320 uint64_t nb = 0;
321 uint32_t na = 0;
322 for (uint32_t i = 0; i < CK_ART_MAX_ENTRIES; i++)
323 if (g_set->e[i].bytes) {
324 nb += g_set->e[i].len;
325 na++;
326 }
327 ck_printf("artifact_disk: released heap copies artifacts=%u bytes=%llu\n", na, (unsigned long long)nb);
328 ck_art_set_free(g_set);
329 ck_free(g_set);
330 g_set = 0;
331 }
332 az(g_out, sizeof g_out);
333 az(&g_res, sizeof g_res);
334}
335
336/* ---- host writer ------------------------------------------------------------ */
337#ifdef CK_ART_STORE_WRITER
338static int put_name(uint8_t *nlen, uint8_t *name, const char *s)
339{
340 size_t l = s ? slen(s) : 0;
341 if (l == 0 || l > CK_ART_NAME_MAX) return -1;
342 *nlen = (uint8_t)l;
343 az(name, CK_ART_NAME_MAX);
344 acp(name, s, l);
345 return name_ok(nlen, name) ? 0 : -1;
346}
347
348int ck_art_write(ss_store *s, const ck_art_input *in, size_t n)
349{
350 if (!s || (n && !in) || n > CK_ART_MAX_ENTRIES) return -1;
351 static uint8_t chunk[SS_MAX_OBJECTS][SS_MAX_PLAINTEXT];
352 static uint8_t index[CK_ART_INDEX_HDR + CK_ART_MAX_ENTRIES * CK_ART_INDEX_ENTRY];
353 ss_object objs[SS_MAX_OBJECTS];
354 size_t no = 0;
355 az(index, sizeof index);
356 acp(index, IX_MAGIC, 8);
357 p32(index + 8, CK_ART_VERSION);
358 p32(index + 12, (uint32_t)n);
359 /* validate every entry before the first transaction */
360 for (size_t i = 0; i < n; i++) {
361 uint8_t nl, nm[CK_ART_NAME_MAX];
362 if (put_name(&nl, nm, in[i].name)) return -2;
363 size_t li = slen(in[i].name);
364 for (size_t j = 0; j < i; j++) {
365 size_t lj = slen(in[j].name);
366 if (li == lj && aeq(in[j].name, in[i].name, li)) return -2;
367 }
368 if (in[i].len == 0 || in[i].len > CK_ART_MAX_BYTES || (!in[i].missing && !in[i].bytes)) return -3;
369 }
370 for (size_t i = 0; i < n; i++) {
371 uint8_t *e = index + CK_ART_INDEX_HDR + i * CK_ART_INDEX_ENTRY;
372 if (put_name(e, e + 4, in[i].name)) return -2;
373 size_t li = slen(in[i].name);
374 for (size_t j = 0; j < i; j++) {
375 size_t lj = slen(in[j].name);
376 if (li == lj && aeq(in[j].name, in[i].name, li)) return -2;
377 }
378 if (in[i].len == 0 || in[i].len > CK_ART_MAX_BYTES || (!in[i].missing && !in[i].bytes)) return -3;
379 uint32_t len = (uint32_t)in[i].len, cc = chunks_for(len);
380 p32(e + 36, len);
381 p16(e + 40, (uint16_t)cc);
382 uint8_t dg[32];
383 az(dg, sizeof dg);
384 if (!in[i].missing) sha256_hash(in[i].bytes, len, dg);
385 acp(e + 48, dg, 32);
386 if (in[i].missing) continue;
387 for (uint32_t c = 0; c < cc; c++) {
388 uint8_t *b = chunk[no];
389 uint32_t sz = chunk_size(len, c);
390 az(b, CK_ART_CHUNK_HDR);
391 acp(b, CH_MAGIC, 8);
392 p32(b + 8, CK_ART_VERSION);
393 p32(b + 12, len);
394 p16(b + 16, (uint16_t)c);
395 p16(b + 18, (uint16_t)cc);
396 put_name(b + 20, b + 24, in[i].name);
397 acp(b + 56, dg, 32);
398 acp(b + CK_ART_CHUNK_HDR, in[i].bytes + (size_t)c * CK_ART_CHUNK_DATA, sz);
399 objs[no].kind = CK_ART_CHUNK_KIND;
400 objs[no].version = CK_ART_VERSION;
401 objs[no].bytes = b;
402 objs[no].len = CK_ART_CHUNK_HDR + sz;
403 if (++no == SS_MAX_OBJECTS) {
404 int rc = ss_transact(s, objs, no, 0, 0, 0);
405 if (rc) return rc;
406 no = 0;
407 }
408 }
409 }
410 if (no) {
411 int rc = ss_transact(s, objs, no, 0, 0, 0);
412 if (rc) return rc;
413 }
414 ss_object ix = {CK_ART_INDEX_KIND, CK_ART_VERSION, index, CK_ART_INDEX_HDR + n * CK_ART_INDEX_ENTRY};
415 return ss_transact(s, &ix, 1, 0, 0, 0);
416}
417#endif