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

367 lines · 18155 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_osc_launch.c -- host tests for the pure parts of OSC unit launch (artifact/osc_launch.c):
18 * - the argument rule (spec 9.2) against vectors/launch.txt, and against the spec's own C reference
19 * tools/osc-launch-check.c (built by the Makefile, path in OSC_LAUNCH_REF) on 3000 pseudo-random
20 * cases that mix interesting values (alignment, wrap, overlap, range edges);
21 * - range ownership (9.1.1 item 4), the ticks budget, and the result classification (9.3, 9.1.1):
22 * RETURNED only from the return stub, TRAPPED only from the trap stub with a code in 1..14, an SVC
23 * from anywhere else and a brk reached directly are OUTCOME_UNKNOWN(FAULT), never TRAPPED, and no
24 * exception class and no trap code 0..255 can produce RETURNED;
25 * - the launch fixture unit l01 admits and its entry table drives the same argument rule. */
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/wait.h>
30#include <unistd.h>
31#include "ck_test.h"
32#include "osc_launch.h"
33#include "osc_unit.h"
34#include "osc_unit_test_anchor.h"
35
36#ifndef OSC_FIXTURE_DIR
37#define OSC_FIXTURE_DIR "native/kernel/tests/fixtures/osc_unit"
38#endif
39#ifndef OSC_LAUNCH_REF
40#define OSC_LAUNCH_REF "osc-launch-check"
41#endif
42
43static int split(char *s, uint64_t *out, int max)
44{
45 int n = 0;
46 for (char *t = strtok(s, ","); t; t = strtok(NULL, ",")) {
47 if (n >= max) return -1;
48 out[n++] = strtoull(t, NULL, 0);
49 }
50 return n;
51}
52
53/* ours: 1 OK, 0 refused; -1 unparsable */
54static int ours(const char *kinds, const char *args)
55{
56 char a[512], b[512];
57 uint64_t k[64], v[64];
58 snprintf(a, sizeof a, "%s", kinds);
59 snprintf(b, sizeof b, "%s", args);
60 int nk = split(a, k, 64), na = split(b, v, 64);
61 if (nk < 0 || na < 0) return -1;
62 uint8_t kk[64];
63 for (int i = 0; i < nk; i++) kk[i] = (uint8_t)k[i];
64 return osc_launch_args_ok(kk, (unsigned)nk, v, (unsigned)na);
65}
66
67/* Runs the reference checker directly (argv, no shell) and reads its first output line. */
68static int reference(const char *kinds, const char *args)
69{
70 char out[128];
71 int fd[2];
72 if (pipe(fd) != 0) return -1;
73 pid_t pid = fork();
74 if (pid < 0) { close(fd[0]); close(fd[1]); return -1; }
75 if (pid == 0) {
76 close(fd[0]);
77 if (dup2(fd[1], STDOUT_FILENO) < 0) _exit(127);
78 close(fd[1]);
79 execl(OSC_LAUNCH_REF, OSC_LAUNCH_REF, kinds, args, (char *)NULL);
80 _exit(127);
81 }
82 close(fd[1]);
83 FILE *p = fdopen(fd[0], "r");
84 int status = 0, got = p && fgets(out, sizeof out, p) != NULL;
85 if (p) fclose(p); else close(fd[0]);
86 if (waitpid(pid, &status, 0) != pid || !got) return -1;
87 return strncmp(out, "OK", 2) == 0 ? 1 : strncmp(out, "REFUSED LAUNCH_ARG_SHAPE 41", 27) == 0 ? 0 : -1;
88}
89
90static void launch_txt(void)
91{
92 char path[512], line[512];
93 int n = 0;
94 snprintf(path, sizeof path, "%s/vectors/launch.txt", OSC_FIXTURE_DIR);
95 FILE *f = fopen(path, "r");
96 CHECK(f != NULL);
97 if (!f) return;
98 while (fgets(line, sizeof line, f)) {
99 if (line[0] == '#' || line[0] == '\n') continue;
100 char kinds[128], args[256], verdict[128];
101 if (sscanf(line, "%127s %255s %127[^\n]", kinds, args, verdict) != 3) { CHECK(0); continue; }
102 int want = strncmp(verdict, "OK", 2) == 0;
103 if (!want) CHECK(strcmp(verdict, "REFUSED LAUNCH_ARG_SHAPE 41") == 0);
104 CHECK(ours(kinds, args) == want);
105 CHECK(reference(kinds, args) == want);
106 n++;
107 }
108 fclose(f);
109 CHECK(n == 16);
110 printf(" launch.txt: %d lines\n", n);
111}
112
113static uint64_t rs = 0x9e3779b97f4a7c15ull;
114static uint64_t rnd(void)
115{
116 rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17;
117 return rs;
118}
119
120static void differential(void)
121{
122 static const char *const shapes[] = {
123 "5,5", "11,5", "12,5", "11,5,11,5", "12,5,11,5", "11,5,12,5", "12,5,12,5", "2,5", "6,5", "1,4", "7,3",
124 "8,9", "9", "5", "11,5,5", "12,5,5,5", "11,5,11,5,12,5", "12,5,12,5,12,5", "3,5,11,5", "4,6,12,5",
125 };
126 static const uint64_t vals[] = {
127 0, 1, 2, 3, 4, 7, 8, 9, 15, 16, 0x7f, 0x80, 0xff, 0x100, 0xffff, 0x10000, 0xffffffffull, 0x100000000ull,
128 0xffffffffffffff80ull, 0xffffffffffff8000ull, 0xffffffff80000000ull, 0x1000, 0x1004, 0x1008, 0x1010, 0x1018,
129 0x2000, 0x2000000000000000ull, 0x1fffffffffffffffull, 0x8000000000000000ull, 0xfffffffffffffff0ull,
130 0xfffffffffffffff8ull, 0xffffffffffffffffull, 0xfffffffffffffff7ull,
131 };
132 const unsigned nv = sizeof vals / sizeof vals[0];
133 int mismatch = 0, ok = 0, ref = 0;
134 for (int i = 0; i < 3000; i++) {
135 const char *shape = shapes[rnd() % (sizeof shapes / sizeof shapes[0])];
136 int regs = 1;
137 for (const char *c = shape; *c; c++) regs += *c == ',';
138 char args[400];
139 size_t at = 0;
140 int cnt = regs;
141 if (rnd() % 17 == 0) cnt = regs + (rnd() % 2 ? 1 : -1); /* wrong register count now and then */
142 if (cnt < 0) cnt = 0;
143 args[0] = 0;
144 for (int r = 0; r < cnt; r++) {
145 int w = snprintf(args + at, sizeof args - at, "%s0x%llx", r ? "," : "", (unsigned long long)vals[rnd() % nv]);
146 if (w < 0 || (size_t)w >= sizeof args - at) break; /* never step past the buffer */
147 at += (size_t)w;
148 }
149 if (cnt == 0) strcpy(args, "0");
150 int a = ours(shape, args), b = reference(shape, args);
151 if (a != b) { mismatch++; if (mismatch < 5) printf(" MISMATCH kinds=%s args=%s ours=%d ref=%d\n", shape, args, a, b); }
152 a == 1 ? ok++ : ref++;
153 }
154 CHECK(mismatch == 0);
155 CHECK(ok > 200 && ref > 200); /* the sample exercises both verdicts */
156 printf(" differential vs the spec's C reference: 3000 cases, %d OK, %d refused, %d mismatches\n", ok, ref, mismatch);
157}
158
159static void ownership(void)
160{
161 const uint8_t kb[2] = { 11, 5 }, kc[2] = { 12, 5 }, kbc[4] = { 11, 5, 12, 5 };
162 uint64_t in = 0x200000, ws = 0x210000;
163 uint64_t a1[2] = { in, 16 };
164 CHECK(osc_launch_ranges_owned(kb, 2, a1, in, 16, ws, 4096)); /* exactly the input window */
165 uint64_t a2[2] = { in, 17 };
166 CHECK(!osc_launch_ranges_owned(kb, 2, a2, in, 16, ws, 4096)); /* one byte past */
167 uint64_t a3[2] = { in + 15, 1 };
168 CHECK(osc_launch_ranges_owned(kb, 2, a3, in, 16, ws, 4096)); /* last byte */
169 uint64_t a4[2] = { in - 1, 1 };
170 CHECK(!osc_launch_ranges_owned(kb, 2, a4, in, 16, ws, 4096)); /* one byte before */
171 uint64_t a5[2] = { ws, 2 };
172 CHECK(!osc_launch_ranges_owned(kb, 2, a5, in, 16, ws, 4096)); /* bytes in the workspace: not RO */
173 uint64_t c1[2] = { ws, 512 };
174 CHECK(osc_launch_ranges_owned(kc, 2, c1, in, 16, ws, 4096)); /* 512 cells = 4096 bytes */
175 uint64_t c2[2] = { ws, 513 };
176 CHECK(!osc_launch_ranges_owned(kc, 2, c2, in, 16, ws, 4096));
177 uint64_t c3[2] = { in, 1 };
178 CHECK(!osc_launch_ranges_owned(kc, 2, c3, in, 16, ws, 4096)); /* cells in the RO input */
179 CHECK(!osc_launch_ranges_owned(kc, 2, c1, in, 16, ws, 0)); /* no workspace given */
180 uint64_t c4[2] = { 0, 0 };
181 CHECK(osc_launch_ranges_owned(kc, 2, c4, in, 16, ws, 0)); /* zero length, null: never dereferenced */
182 uint64_t c5[2] = { ws, 0x2000000000000000ull };
183 CHECK(!osc_launch_ranges_owned(kc, 2, c5, in, 16, ws, 4096)); /* n*8 would wrap */
184 uint64_t m[4] = { in, 8, ws + 8, 2 };
185 CHECK(osc_launch_ranges_owned(kbc, 4, m, in, 16, ws, 4096));
186 uint64_t m2[4] = { in, 8, ws + 4088, 2 };
187 CHECK(!osc_launch_ranges_owned(kbc, 4, m2, in, 16, ws, 4096));
188}
189
190static void budget(void)
191{
192 CHECK(osc_launch_budget(1000000, 0) == 1000000); /* no cap: the declared budget */
193 CHECK(osc_launch_budget(1000000, 5) == 5); /* a cap lowers it */
194 CHECK(osc_launch_budget(3, 5) == 3); /* a cap never raises it */
195 CHECK(osc_launch_budget(1, 1) == 1);
196 CHECK(!osc_launch_budget_expired(0, 5));
197 CHECK(!osc_launch_budget_expired(4, 5));
198 CHECK(osc_launch_budget_expired(5, 5));
199 CHECK(osc_launch_budget_expired(6, 5));
200}
201
202#define RT 0x7000002000ull
203static uint64_t esr_svc(unsigned imm) { return (0x15ull << 26) | (1ull << 25) | imm; }
204static uint64_t esr_ec(uint64_t ec) { return (ec << 26) | (1ull << 25); }
205
206static struct osc_result cls(unsigned kind, uint64_t esr, uint64_t elr, uint64_t x0, uint64_t x1)
207{
208 struct osc_event e = { kind, esr, elr, x0, x1 };
209 struct osc_result r;
210 memset(&r, 0xaa, sizeof r);
211 osc_launch_classify(&e, RT, &r);
212 return r;
213}
214
215static uint64_t stub(unsigned i) { return RT + (uint64_t)i * OSC_RT_STUB_BYTES; }
216
217static void classify(void)
218{
219 struct osc_result r;
220 /* RETURNED: only from the return stub, value is x0 */
221 r = cls(OSC_EV_SVC, esr_svc(13), stub(13) + 4, 0xfeedfaceull, 99);
222 CHECK(r.cls == OSC_RES_RETURNED && r.value == 0xfeedfaceull && r.unknown_reason == 0 && r.trap_code == 0);
223 r = cls(OSC_EV_SVC, esr_svc(13), stub(13) + 4, 0, 0);
224 CHECK(r.cls == OSC_RES_RETURNED && r.value == 0); /* a void function reports 0 */
225 /* TRAPPED: the trap stub with a code in 1..14 */
226 for (unsigned c = 1; c <= 14; c++) {
227 r = cls(OSC_EV_SVC, esr_svc(OSC_SVC_TRAP), stub(OSC_SVC_TRAP) + 4, 777, c);
228 CHECK(r.cls == OSC_RES_TRAPPED && r.trap_code == c && r.unknown_reason == 0);
229 }
230 /* trap code 0, 15..255 and wide values: OUTCOME_UNKNOWN(TRAP_CODE_UNKNOWN), never TRAPPED */
231 for (uint64_t c = 0; c < 256; c++) {
232 if (c >= 1 && c <= 14) continue;
233 r = cls(OSC_EV_SVC, esr_svc(OSC_SVC_TRAP), stub(OSC_SVC_TRAP) + 4, 0, c);
234 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_TRAP_CODE_UNKNOWN);
235 }
236 r = cls(OSC_EV_SVC, esr_svc(OSC_SVC_TRAP), stub(OSC_SVC_TRAP) + 4, 0, 0x100000003ull); /* low byte is 3: still not a trap */
237 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_TRAP_CODE_UNKNOWN);
238 /* runtime services this cut does not provide */
239 for (unsigned i = 0; i < OSC_RT_SERVICES; i++) {
240 if (i == OSC_SVC_TRAP) continue;
241 r = cls(OSC_EV_SVC, esr_svc(i), stub(i) + 4, 1, 1);
242 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_OTHER);
243 }
244 /* an SVC from unit context (anywhere but a stub with the matching immediate) is refused as a FAULT */
245 r = cls(OSC_EV_SVC, esr_svc(0), 0x7000010004ull, 0, 0); /* in the unit's code */
246 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT);
247 r = cls(OSC_EV_SVC, esr_svc(13), stub(13) + 8, 5, 0); /* return-stub immediate at the wrong place */
248 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT);
249 r = cls(OSC_EV_SVC, esr_svc(2), stub(13) + 4, 5, 3); /* trap immediate at the return stub */
250 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT);
251 r = cls(OSC_EV_SVC, esr_svc(13), stub(13) + 5, 5, 0); /* misaligned return address */
252 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT);
253 r = cls(OSC_EV_SVC, esr_svc(14), stub(14) + 4, 5, 0); /* one stub past the table */
254 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT);
255 r = cls(OSC_EV_SVC, esr_svc(0), RT - 4, 5, 0); /* just below stub 0 */
256 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT);
257 r = cls(OSC_EV_SVC, esr_svc(13), 2, 5, 0); /* tiny elr: no wrap into a stub */
258 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT);
259 r = cls(OSC_EV_SVC, esr_ec(0x24) | 13, stub(13) + 4, 5, 0); /* not an SVC class at all */
260 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT);
261 /* every other synchronous class: never RETURNED, never TRAPPED; known CPU faults are FAULT */
262 for (uint64_t ec = 0; ec < 64; ec++) {
263 if (ec == 0x15) continue;
264 r = cls(OSC_EV_SYNC, esr_ec(ec) | 3, stub(13) + 4, 1, 3);
265 CHECK(r.cls == OSC_RES_UNKNOWN);
266 int known = ec == 0x00 || ec == 0x07 || ec == 0x0e || ec == 0x18 || ec == 0x20 || ec == 0x22 ||
267 ec == 0x24 || ec == 0x26 || ec == 0x3c;
268 CHECK(r.unknown_reason == (known ? OSC_UNK_FAULT : OSC_UNK_OTHER));
269 }
270 /* a brk (class 0x3c) with an immediate 1..14 reached directly is a fault, never TRAPPED (spec 8.4) */
271 for (unsigned imm = 0; imm < 16; imm++) {
272 r = cls(OSC_EV_SYNC, esr_ec(0x3c) | imm, 0x7000010000ull, imm, imm);
273 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_FAULT && r.trap_code == 0);
274 }
275 r = cls(OSC_EV_BUDGET, 0, 0x7000010000ull, 0, 0);
276 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_TICK_OVERRUN);
277 r = cls(OSC_EV_LOST, 0, 0, 0, 0);
278 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_LAUNCH_LOST);
279 r = cls(99, 0, 0, 0, 0);
280 CHECK(r.cls == OSC_RES_UNKNOWN && r.unknown_reason == OSC_UNK_OTHER);
281 /* the reason is diagnostic: values are the spec's */
282 CHECK(OSC_UNK_FAULT == 1 && OSC_UNK_TICK_OVERRUN == 2 && OSC_UNK_LAUNCH_LOST == 3 &&
283 OSC_UNK_TRAP_CODE_UNKNOWN == 4 && OSC_UNK_OTHER == 255);
284}
285
286static void fixture_unit(void)
287{
288 char path[512];
289 snprintf(path, sizeof path, "%s/launch/l01_launch_fns.unit", OSC_FIXTURE_DIR);
290 FILE *f = fopen(path, "rb");
291 CHECK(f != NULL);
292 if (!f) return;
293 static uint8_t buf[8192];
294 size_t n = fread(buf, 1, sizeof buf, f);
295 fclose(f);
296 struct osc_policy p;
297 memset(&p, 0, sizeof p);
298 static const uint8_t (*const ta)[32] = &osc_unit_test1_pk;
299 p.test_anchors = ta;
300 p.n_test = 1;
301 p.unit_formats = OSC_PROFILE_UNIT_FORMATS;
302 p.abi_versions = OSC_PROFILE_ABI;
303 static struct osc_accept a;
304 CHECK(osc_unit_admit(buf, n, &p, &a) == OSC_OK);
305 CHECK(a.function_count == 14);
306 int fi = osc_unit_lookup(&a, "counter", 7);
307 CHECK(fi == 7 && a.entry[fi].nregs == 2 && a.entry[fi].reg_kind[0] == 12 && a.entry[fi].reg_kind[1] == 5);
308 uint64_t ok[2] = { 0x210000, 1 }, bad[2] = { 0x210004, 1 };
309 CHECK(osc_launch_args_ok(a.entry[fi].reg_kind, a.entry[fi].nregs, ok, 2));
310 CHECK(!osc_launch_args_ok(a.entry[fi].reg_kind, a.entry[fi].nregs, bad, 2));
311 CHECK(!osc_launch_args_ok(a.entry[fi].reg_kind, a.entry[fi].nregs, ok, 1));
312 CHECK(osc_unit_lookup(&a, "Counter", 7) == -1 && osc_unit_lookup(&a, "trap", 4) == 0);
313 /* every word of l01 passed the section 8.4 scan inside admission; the spin loop and the bare brk are in it */
314 CHECK(a.code_len == 424);
315}
316
317static void workspace_bounds(void)
318{
319 const uint8_t kc[2] = { 12, 5 };
320 const uint64_t in = 0x200000, ws = 0x210000;
321 CHECK(OSC_WS_MAX_PAGES == 32u);
322 /* declared stack vs the launcher maximum: at the max passes, one byte over is LIMIT_EXCEEDED (14) */
323 CHECK(osc_launch_stack_ok(0, 65536) && osc_launch_stack_ok(65536, 65536));
324 CHECK(!osc_launch_stack_ok(65552, 65536) && !osc_launch_stack_ok(65537, 65536) && !osc_launch_stack_ok(1048576, 65536));
325 /* a workspace must not overlap the launcher state (slot0) or the input buffer: disjoint test */
326 {
327 const uint64_t slot = 0x80000, slen = 0x40000;
328 CHECK(osc_launch_disjoint(0x100000, 4096, slot, slen)); /* clear of it */
329 CHECK(!osc_launch_disjoint(slot, 4096, slot, slen)); /* starts inside slot0 */
330 CHECK(!osc_launch_disjoint(slot - 4096, 8192, slot, slen)); /* straddles the start */
331 CHECK(!osc_launch_disjoint(slot + slen - 4096, 8192, slot, slen)); /* straddles the end */
332 CHECK(osc_launch_disjoint(slot - 4096, 4096, slot, slen)); /* ends exactly at its start */
333 CHECK(osc_launch_disjoint(slot + slen, 4096, slot, slen)); /* begins exactly at its end */
334 CHECK(!osc_launch_disjoint(slot - 8, ~0ull, slot, slen)); /* a wrapping range overlaps */
335 CHECK(osc_launch_disjoint(slot, 4096, 0, 0) && osc_launch_disjoint(slot, 0, slot, slen)); /* empty: nothing */
336 CHECK(!osc_launch_disjoint(0x100000, 4096, 0x100040, 5)); /* an input buffer inside the workspace */
337 }
338 CHECK(osc_launch_ws_pages_ok(0) && osc_launch_ws_pages_ok(1) && osc_launch_ws_pages_ok(32));
339 CHECK(!osc_launch_ws_pages_ok(33) && !osc_launch_ws_pages_ok(0xffffffffull) && !osc_launch_ws_pages_ok(~0ull));
340 for (uint64_t pages = 0; pages <= 33; pages++) {
341 uint64_t len = pages * 4096, cells = len / 8;
342 uint64_t whole[2] = { ws, cells }, over[2] = { ws, cells + 1 };
343 uint64_t last[2] = { ws + len - 8, 1 }, past[2] = { ws + len, 1 };
344 uint64_t before[2] = { ws - 8, 1 };
345 if (pages == 0) {
346 CHECK(!osc_launch_ranges_owned(kc, 2, past, in, 16, ws, 0)); /* no workspace: nothing owned */
347 continue;
348 }
349 CHECK(osc_launch_ranges_owned(kc, 2, whole, in, 16, ws, len)); /* ptr+len ends exactly at the end */
350 CHECK(!osc_launch_ranges_owned(kc, 2, over, in, 16, ws, len)); /* one cell too many */
351 CHECK(osc_launch_ranges_owned(kc, 2, last, in, 16, ws, len)); /* the last cell */
352 CHECK(!osc_launch_ranges_owned(kc, 2, past, in, 16, ws, len)); /* first cell past the end */
353 CHECK(!osc_launch_ranges_owned(kc, 2, before, in, 16, ws, len));
354 }
355}
356
357int main(void)
358{
359 launch_txt();
360 differential();
361 ownership();
362 workspace_bounds();
363 budget();
364 classify();
365 fixture_unit();
366 return ck_t_verdict("test_osc_launch");
367}