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 / tests / test_imac_report.c

398 lines · 18968 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/* test_imac_report.c -- host test for the iMac kernel's lab-only network
18 * report (x86/report_x86.c, built with -DCK_X86_REPORT_HOST): the allowlist
19 * filter and the frames.
20 *
21 * test_imac_report unit checks on the filter and frames
22 * test_imac_report send PORT LOGFILE also writes a sample report to
23 * LOGFILE and sends every part's UDP payload to 127.0.0.1:PORT (last
24 * part first, then all again), so scripts/ck_imac_report_test.sh can
25 * check that ck_imac_report_listen rebuilds LOGFILE byte for byte.
26 * test_imac_report sendfull PORT LOGFILE the same for a full 64-part,
27 * sealed report (run id 9), the most the console copy can hold.
28 * test_imac_report legacy PORT sends one datagram in the retired
29 * v=1 raw-console format, which the listener must refuse. */
30#define _POSIX_C_SOURCE 200809L
31#define _GNU_SOURCE /* memmem */
32#include <arpa/inet.h>
33#include <fcntl.h>
34#include <netinet/in.h>
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/socket.h>
40#include <time.h>
41#include <unistd.h>
42
43#include "aienos_net.h"
44#include "ck_test.h"
45#include "console_log.h"
46#include "report_x86.h"
47
48static const uint8_t mac[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x01};
49/* 56 lines of 61 bytes, each an allowlisted "pci: " line: 4 parts. */
50static char log_text[56 * 61];
51
52/* A console log mixing allowlisted lines with lines that must never leave
53 * the machine: keys off the allowlist (chat, keys, tokens, the memory
54 * layout), near-miss keys, control and non-ASCII bytes inside an allowed
55 * key, an over-long line, and an unterminated tail. */
56static const char allowed_part[] =
57 "AIENOS-KERNEL-0\n"
58 "aienos_commit: 0123abcd\n"
59 "dmar: inventory-only register_access=none; DMA confinement NOT QUALIFIED\n"
60 "pci: ahci 00:1f.2 id=8086:2922 class=0106\n"
61 "net_tg3: link up 1000 Mb/s full duplex\n"
62 "summary: cores=4/4 timer=ok sata_disks=1 nvme_disks=1 gpt_valid=1 writes=none net=up\n"
63 "screen: Allen answered (11 characters in, 15 out)\n"
64 "allen_memory: saved generation 2 to copy B (1 fact) in 9 ms\n"
65 "ahci_part: disk=0 entry=1 type=38daac89-5ead-4b40-8b1e-3687a7418061 first_lba=2048 last_lba=2099199 mib=1024 name=\"AIENOS Allen memory\" (AIENOS)\n"
66 "kernel: alive\n"
67 "panic: exception: vector 14 #PF error=0x2\n"
68 "AIENOS-KERNEL-0 DONE\n";
69
70static size_t build_mixed(char *out, size_t cap)
71{
72 static const char *const lines[] = {
73 "AIENOS-KERNEL-0\n",
74 "allen: secret-chat-SENTINEL\n",
75 "aienos_commit: 0123abcd\n",
76 "dmar: inventory-only register_access=none; DMA confinement NOT QUALIFIED\n",
77 "key: SENTINEL-KEY\n",
78 "memory: frame_ranges=3 free_bytes=1 heap=0x1000+64 SENTINEL-MEM\n",
79 "pci: ahci 00:1f.2 id=8086:2922 class=0106\n",
80 "token: SENTINEL-TOKEN\n",
81 "credential: SENTINEL-CRED\n",
82 "chat: user said SENTINEL-CHAT\n",
83 "nett: SENTINEL-NEAR\n",
84 "net SENTINEL-NOCOLON\n",
85 " pci: SENTINEL-LEADING-SPACE\n",
86 "PCI: SENTINEL-CASE\n",
87 "net: ok \x1b[2J SENTINEL-ESC\n",
88 "summary: \tSENTINEL-TAB\n",
89 "report: caf\xc3\xa9 SENTINEL-UTF8\n",
90 "power: \r SENTINEL-CR\n",
91 "net_tg3: link up 1000 Mb/s full duplex\n",
92 "summary: cores=4/4 timer=ok sata_disks=1 nvme_disks=1 gpt_valid=1 writes=none net=up\n",
93 "keyboard_line: Hello SENTINEL-TYPED\n",
94 "screen: Allen answered (11 characters in, 15 out)\n",
95 "allen: Your favorite color is SENTINEL-FACT.\n",
96 "allen_memory: saved generation 2 to copy B (1 fact) in 9 ms\n",
97 "allen_memoryx: SENTINEL-NEAR2\n",
98 "ahci_part: disk=0 entry=1 type=38daac89-5ead-4b40-8b1e-3687a7418061 first_lba=2048 last_lba=2099199 mib=1024 name=\"AIENOS Allen memory\" (AIENOS)\n",
99 "\n",
100 "kernel: alive\n",
101 "panic: exception: vector 14 #PF error=0x2\n",
102 "AIENOS-KERNEL-0 DONE\n",
103 };
104 size_t w = 0;
105 for (size_t i = 0; i < sizeof lines / sizeof lines[0]; i++) {
106 size_t n = strlen(lines[i]);
107 if (w + n > cap)
108 return 0;
109 memcpy(out + w, lines[i], n);
110 w += n;
111 if (i == 3) { /* an over-long but otherwise allowed line */
112 const char *p = "stack: ";
113 memcpy(out + w, p, 7);
114 w += 7;
115 for (unsigned k = 0; k < CK_X86_REPORT_LINE_MAX; k++)
116 out[w++] = 'x';
117 memcpy(out + w, "SENTINEL-LONG\n", 14);
118 w += 14;
119 }
120 }
121 static const char tail[] = "screen: SENTINEL-TAIL"; /* no newline */
122 memcpy(out + w, tail, sizeof tail - 1);
123 return w + sizeof tail - 1;
124}
125
126static void filter_checks(void)
127{
128 static char raw[4096], out[4096];
129 size_t rl = build_mixed(raw, sizeof raw);
130 CHECK(rl > 0);
131 CHECK(memmem(raw, rl, "SENTINEL", 8) != 0);
132 size_t n = ck_x86_report_filter(raw, rl, out, sizeof out);
133 /* The allowed lines survive byte for byte, in order; nothing else does. */
134 CHECK(n == sizeof allowed_part - 1 && memcmp(out, allowed_part, n) == 0);
135 CHECK(memmem(out, n, "SENTINEL", 8) == 0);
136 for (size_t i = 0; i < n; i++)
137 CHECK(out[i] == '\n' || (out[i] >= 0x20 && out[i] <= 0x7e));
138 /* Nor does any sentinel reach a frame built from the filtered copy. */
139 uint8_t f[1600];
140 for (unsigned p = 0; p < ck_x86_report_parts(n); p++) {
141 size_t fl = ck_x86_report_frame(mac, 1, out, n, p, f, sizeof f);
142 CHECK(fl > 0 && memmem(f, fl, "SENTINEL", 8) == 0);
143 }
144 /* A line at the length cap is kept, one byte more is dropped. */
145 char edge[2 * CK_X86_REPORT_LINE_MAX + 8], eo[sizeof edge];
146 size_t e = 0;
147 memcpy(edge, "cpu: ", 5);
148 for (e = 5; e < CK_X86_REPORT_LINE_MAX; e++)
149 edge[e] = 'a';
150 edge[e++] = '\n';
151 size_t keep = e;
152 memcpy(edge + e, "cpu: ", 5);
153 for (size_t k = 5; k <= CK_X86_REPORT_LINE_MAX; k++)
154 edge[e + k] = 'b';
155 e += CK_X86_REPORT_LINE_MAX + 1;
156 edge[e++] = '\n';
157 CHECK(ck_x86_report_filter(edge, e, eo, sizeof eo) == keep && memcmp(eo, edge, keep) == 0);
158 /* Only whole lines are written: a line that does not fit is dropped. */
159 size_t first = strlen("AIENOS-KERNEL-0\n");
160 CHECK(ck_x86_report_filter(raw, rl, out, first + 5) == first && memcmp(out, raw, first) == 0);
161 CHECK(ck_x86_report_filter(raw, rl, out, first - 1) == 14 && memcmp(out, "kernel: alive\n", 14) == 0);
162 CHECK(ck_x86_report_filter(0, rl, out, sizeof out) == 0);
163 CHECK(ck_x86_report_filter(raw, rl, 0, sizeof out) == 0);
164 CHECK(ck_x86_report_filter(raw, 0, out, sizeof out) == 0);
165}
166/* Unwraps one frame; returns the UDP payload view or 0. */
167/* The late-diagnostics boundary, through the console's own append
168 * (x86/console_log.h): the boot lines stay, an allowlisted public line
169 * written after the old 16 KiB cap still reaches the report, conversation
170 * lines written before and after it never do, and a copy that fills says so. */
171static const uint8_t *unwrap(const uint8_t *f, size_t n, size_t *plen);
172static char full_report[CK_X86_REPORT_MAX]; /* the sealed worst case, for the listener round trip */
173static size_t full_len;
174static struct ck_x86_log klog;
175static size_t written;
176
177static void say(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
178static void say(const char *fmt, ...)
179{
180 char line[300];
181 va_list ap;
182 va_start(ap, fmt);
183 int n = vsnprintf(line, sizeof line, fmt, ap);
184 va_end(ap);
185 ck_x86_log_append(&klog, line, (size_t)n);
186 written += (size_t)n;
187}
188
189static void late_checks(void)
190{
191 static char out[CK_X86_REPORT_MAX], raw_cut[CK_X86_LOG_BYTES], rebuilt[CK_X86_REPORT_MAX];
192 uint8_t f[1600];
193 const size_t old_cap = 16384;
194
195 CHECK(CK_X86_LOG_BYTES == CK_X86_REPORT_MAX);
196 CHECK(CK_X86_REPORT_MAX / CK_X86_REPORT_CHUNK == 64); /* the listener's MAX_PARTS */
197
198 /* The boot, then conversation until the log is past the old cap. */
199 say("AIENOS-KERNEL-0\naienos_commit: 0123abcd\n");
200 for (unsigned i = 0; klog.len < old_cap + 2048 && klog.dropped == 0; i++) {
201 say("pci: fn 00:%02x.0 id=8086:%04x class=0600\n", i & 31, i);
202 say("keyboard_line: SENTINEL-CHAT-%u private words typed to Allen\n", i);
203 say("allen: SENTINEL-REPLY-%u private words Allen said back\n", i);
204 }
205 size_t before = klog.len;
206 say("local_model: starting public BOS experiment tokens=32 cpu_limit_s=120\n");
207 say("keyboard_line: SENTINEL-AFTER-STARTING\n");
208 say("local_model: LATE-COMPLETION public fixed prompt, 32 tokens in 41 s\n");
209 say("allen: SENTINEL-AFTER-COMPLETION\n");
210 say("screen: unterminated SENTINEL-TAIL");
211 CHECK(before > old_cap && klog.dropped == 0 && klog.len == written);
212
213 size_t n = ck_x86_report_filter(klog.buf, klog.len, out, sizeof out);
214 CHECK(n > 0 && n <= CK_X86_REPORT_MAX);
215 CHECK(memcmp(out, "AIENOS-KERNEL-0\naienos_commit: 0123abcd\n", 40) == 0);
216 const char *late = memmem(out, n, "LATE-COMPLETION", 15);
217 CHECK(late != 0 && memmem(out, n, "local_model: starting public BOS", 32) != 0);
218 CHECK(memmem(out, n, "SENTINEL", 8) == 0);
219 /* Negative control: the old 16 KiB copy would have lost the completion. */
220 memcpy(raw_cut, klog.buf, old_cap);
221 size_t cn = ck_x86_report_filter(raw_cut, old_cap, out + n, sizeof out - n);
222 CHECK(cn > 0 && memmem(out + n, cn, "LATE-COMPLETION", 15) == 0);
223 CHECK(ck_x86_report_seal(out, n, sizeof out, klog.dropped) == n); /* nothing dropped, no note */
224
225 /* Every part of it frames, carries no conversation, and rebuilds byte for byte. */
226 unsigned parts = ck_x86_report_parts(n);
227 CHECK(parts > 1 && parts <= 64); /* the raw log is past 16 KiB; the filtered report is what the allowlist keeps */
228 size_t got = 0;
229 for (unsigned p = 0; p < parts; p++) {
230 size_t fl = ck_x86_report_frame(mac, 7, out, n, p, f, sizeof f), pl = 0;
231 CHECK(fl >= 60 && fl <= 1514 && memmem(f, fl, "SENTINEL", 8) == 0);
232 const uint8_t *d = fl ? unwrap(f, fl, &pl) : 0;
233 CHECK(d != 0);
234 const uint8_t *nl = d ? memchr(d, '\n', pl) : 0;
235 CHECK(nl != 0);
236 if (nl && got + pl - (size_t)(nl + 1 - d) <= sizeof rebuilt) {
237 memcpy(rebuilt + got, nl + 1, pl - (size_t)(nl + 1 - d));
238 got += pl - (size_t)(nl + 1 - d);
239 }
240 }
241 CHECK(got == n && memcmp(rebuilt, out, n) == 0);
242
243 /* A copy that fills: the head stays, the rest is counted, the report says so. */
244 char head[64];
245 memcpy(head, klog.buf, sizeof head);
246 for (unsigned i = 0; klog.len < CK_X86_LOG_BYTES || i < 100; i++)
247 say("pci: fn 00:%02x.1 id=8086:%04x class=0604 padding padding padding padding\n", i & 31, i);
248 CHECK(klog.len == CK_X86_LOG_BYTES && klog.dropped > 0 && klog.len + klog.dropped == written);
249 CHECK(memcmp(klog.buf, head, sizeof head) == 0);
250 size_t m = ck_x86_report_filter(klog.buf, klog.len, out, sizeof out - CK_X86_REPORT_NOTE_MAX);
251 size_t sealed = ck_x86_report_seal(out, m, sizeof out, klog.dropped);
252 char note[80];
253 int nn = snprintf(note, sizeof note, "report: console_copy_full dropped_bytes=%zu\n", klog.dropped);
254 CHECK(nn > 0 && sealed == m + (size_t)nn && memcmp(out + m, note, (size_t)nn) == 0);
255 CHECK(sealed <= CK_X86_REPORT_MAX && ck_x86_report_parts(sealed) > 1);
256 CHECK(memcmp(out, "AIENOS-KERNEL-0\n", 16) == 0 && memmem(out, m, "LATE-COMPLETION", 15) != 0);
257 CHECK(ck_x86_report_frame(mac, 8, out, sealed, ck_x86_report_parts(sealed) - 1, f, sizeof f) >= 60);
258 CHECK(ck_x86_report_seal(out, m, m + 10, klog.dropped) == m); /* no room: unchanged, never overruns */
259
260 /* Worst case: every line allowed, the copy overfull. The sealed report
261 * still fits the 64 parts the listener rebuilds, and its last frame builds. */
262 memset(&klog, 0, sizeof klog);
263 written = 0;
264 for (unsigned i = 0; klog.dropped == 0; i++)
265 say("pci: fn 00:%02x.1 id=8086:%04x class=0604 padding padding padding padding\n", i & 31, i);
266 m = ck_x86_report_filter(klog.buf, klog.len, out, sizeof out - CK_X86_REPORT_NOTE_MAX);
267 sealed = ck_x86_report_seal(out, m, sizeof out, klog.dropped);
268 CHECK(sealed > m && sealed <= CK_X86_REPORT_MAX && ck_x86_report_parts(sealed) == 64);
269 CHECK(ck_x86_report_frame(mac, 9, out, sealed, 63, f, sizeof f) >= 60);
270 CHECK(ck_x86_report_frame(mac, 9, out, sealed, 64, f, sizeof f) == 0);
271 memcpy(full_report, out, sealed);
272 full_len = sealed;
273}
274
275static const uint8_t *unwrap(const uint8_t *f, size_t n, size_t *plen)
276{
277 net_eth_frame e;
278 net_ipv4_header ih;
279 net_udp_header uh;
280 const uint8_t *ip, *pl;
281 size_t il;
282 static const uint8_t bc[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
283 const net_ipv4 any = {{0, 0, 0, 0}}, bip = {{255, 255, 255, 255}};
284 if (net_eth_parse(f, n, &e) != NET_OK || memcmp(e.destination.b, bc, 6) || memcmp(e.source.b, mac, 6) ||
285 e.ethertype != NET_ETHERTYPE_IPV4)
286 return 0;
287 if (net_ipv4_parse(e.payload, e.payload_len, &ih, &ip, &il) != NET_OK || ih.protocol != NET_IPPROTO_UDP ||
288 ih.ttl != 1 || memcmp(ih.source.b, any.b, 4) || memcmp(ih.destination.b, bip.b, 4))
289 return 0;
290 if (net_udp_parse(ip, il, ih.source, ih.destination, &uh, &pl, plen) != NET_OK ||
291 uh.source_port != CK_X86_REPORT_PORT || uh.destination_port != CK_X86_REPORT_PORT)
292 return 0;
293 if (ip[6] == 0 && ip[7] == 0) /* the checksum must be present, not skipped */
294 return 0;
295 return pl;
296}
297
298int main(int argc, char **argv)
299{
300 for (size_t i = 0; i < sizeof log_text; i++)
301 log_text[i] = (i % 61 == 60) ? '\n' : i % 61 < 5 ? "pci: "[i % 61] : (char)('A' + i % 26);
302 size_t len = sizeof log_text;
303 uint64_t run = 0x0123456789abcdefull;
304 uint8_t f[1600];
305 unsigned parts = ck_x86_report_parts(len);
306
307 CHECK(ck_x86_report_parts(0) == 0);
308 CHECK(ck_x86_report_parts(1) == 1);
309 CHECK(ck_x86_report_parts(CK_X86_REPORT_CHUNK) == 1);
310 CHECK(ck_x86_report_parts(CK_X86_REPORT_CHUNK + 1) == 2);
311 CHECK(ck_x86_report_parts(CK_X86_REPORT_MAX) == CK_X86_REPORT_MAX / CK_X86_REPORT_CHUNK);
312 CHECK(ck_x86_report_parts(CK_X86_REPORT_MAX + 1) == 0);
313 CHECK(parts == 4);
314 filter_checks();
315 late_checks();
316 /* The sample report is what the filter makes of itself: every line is kept. */
317 static char filtered[sizeof log_text];
318 CHECK(ck_x86_report_filter(log_text, len, filtered, sizeof filtered) == len &&
319 memcmp(filtered, log_text, len) == 0);
320 CHECK(ck_x86_report_frame(mac, run, log_text, len, parts, f, sizeof f) == 0);
321 CHECK(ck_x86_report_frame(mac, run, log_text, len, 0, f, 100) == 0);
322 CHECK(ck_x86_report_frame(0, run, log_text, len, 0, f, sizeof f) == 0);
323
324 char rebuilt[sizeof log_text];
325 size_t got = 0;
326 for (unsigned p = 0; p < parts; p++) {
327 size_t n = ck_x86_report_frame(mac, run, log_text, len, p, f, sizeof f);
328 CHECK(n >= 60 && n <= 1514);
329 size_t pl = 0;
330 const uint8_t *d = unwrap(f, n, &pl);
331 CHECK(d != 0);
332 if (!d)
333 continue;
334 char want[128];
335 int h = snprintf(want, sizeof want, "AIENOS-REPORT v=2 auth=none run=0123456789abcdef part=%u/%u bytes=%zu\n",
336 p,
337 parts, len);
338 CHECK(pl > (size_t)h && memcmp(d, want, (size_t)h) == 0);
339 if (pl > (size_t)h && got + pl - h <= sizeof rebuilt) {
340 memcpy(rebuilt + got, d + h, pl - h);
341 got += pl - h;
342 }
343 }
344 CHECK(got == len && memcmp(rebuilt, log_text, len) == 0);
345
346 if (argc == 4 && !strcmp(argv[1], "send")) {
347 int lfd = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
348 FILE *lf = lfd < 0 ? 0 : fdopen(lfd, "w");
349 CHECK(lf && fwrite(log_text, 1, len, lf) == len && fclose(lf) == 0);
350 int s = socket(AF_INET, SOCK_DGRAM, 0);
351 struct sockaddr_in a;
352 memset(&a, 0, sizeof a);
353 a.sin_family = AF_INET;
354 a.sin_port = htons((unsigned short)atoi(argv[2]));
355 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
356 CHECK(s >= 0);
357 for (unsigned round = 0; round < 2; round++)
358 for (unsigned k = 0; k < parts; k++) {
359 unsigned p = round ? k : parts - 1 - k;
360 size_t n = ck_x86_report_frame(mac, run, log_text, len, p, f, sizeof f), pl = 0;
361 const uint8_t *d = unwrap(f, n, &pl);
362 CHECK(d && sendto(s, d, pl, 0, (struct sockaddr *)&a, sizeof a) == (ssize_t)pl);
363 nanosleep(&(struct timespec){0, 20000000}, 0);
364 }
365 close(s);
366 }
367 if (argc == 4 && !strcmp(argv[1], "sendfull")) { /* all 64 parts of the sealed worst case, run id 9 */
368 int lfd = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
369 FILE *lf = lfd < 0 ? 0 : fdopen(lfd, "w");
370 CHECK(lf && fwrite(full_report, 1, full_len, lf) == full_len && fclose(lf) == 0);
371 int s = socket(AF_INET, SOCK_DGRAM, 0);
372 struct sockaddr_in a;
373 memset(&a, 0, sizeof a);
374 a.sin_family = AF_INET;
375 a.sin_port = htons((unsigned short)atoi(argv[2]));
376 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
377 CHECK(s >= 0);
378 for (unsigned p = 0; p < ck_x86_report_parts(full_len); p++) {
379 size_t n = ck_x86_report_frame(mac, 9, full_report, full_len, p, f, sizeof f), pl = 0;
380 const uint8_t *d = unwrap(f, n, &pl);
381 CHECK(d && sendto(s, d, pl, 0, (struct sockaddr *)&a, sizeof a) == (ssize_t)pl);
382 nanosleep(&(struct timespec){0, 2000000}, 0);
383 }
384 close(s);
385 }
386 if (argc == 3 && !strcmp(argv[1], "legacy")) { /* a forged or old v=1 raw-console part */
387 static const char old[] = "AIENOS-IMAC-REPORT v=1 run=00000000000000ff part=0/1 bytes=5\nhello";
388 int s = socket(AF_INET, SOCK_DGRAM, 0);
389 struct sockaddr_in a;
390 memset(&a, 0, sizeof a);
391 a.sin_family = AF_INET;
392 a.sin_port = htons((unsigned short)atoi(argv[2]));
393 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
394 CHECK(s >= 0 && sendto(s, old, sizeof old - 1, 0, (struct sockaddr *)&a, sizeof a) == (ssize_t)(sizeof old - 1));
395 close(s);
396 }
397 return ck_t_verdict("test_imac_report");
398}