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 / core / osh_test.c

743 lines · 27334 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/* osh_test.c -- OSH-AIENOS-0 kernel self-test (TEST ONLY, qualification build with CK_OSH_TEST=1).
18 *
19 * Runs the Omega-native shell core (three compiled OSC units: lex_run, parse_run, expand_run) as admitted EL0 tasks
20 * through the launch interface, over the embedded fixture scripts, with the SAME sequencing as omega's
21 * tests/osh/osh_trace.c (which mirrors src/osh/host/osh_shell.c run_src / lex_parse / run_list), and prints the
22 * same trace lines on the serial console:
23 * step <n> unit=<lex|parse|expand> in_len=<n> ret=<u64> ws_sha256=<hex of WS_CELLS*8 bytes>
24 * request <n> sha256=<hex of the request record cells>
25 * event <text> (input_window_exceeded, internal_error, request_ncmds_out_of_range, fake_status_exhausted)
26 * event exit=<status> last line of every fixture (2 after a refusal, 70 after a unit fault status)
27 * Nothing is executed: on PIPELINE_READY the driver only records the request and takes the fixture's next fake
28 * status (its .status sidecar, then 0); NEED_VAR is answered from a fixed table (HOME=/home/t, X="a b", EMPTY=""),
29 * $? = the last fake status, $0 = "osh_trace", $1.. from the fixture's .args sidecar. No environment, no files, no exec.
30 * One caller-owned workspace of 23 pages (11456 cells) lives across every call. Input goes through the read-only
31 * input window (at most 4096 bytes). A list buffer that would exceed 4096 is refused by the driver exactly as
32 * osh_trace.c does (event line), documented in the gate.
33 *
34 * Then five negative controls (lines "osh_neg"), see qemu_ck_osh_core_test.sh.
35 * QEMU (aarch64 virt), TEST signer, not physical. */
36#include "ck_internal.h"
37#include "fmt.h"
38#include "osc_task.h"
39#include "osc_admit.h"
40#include "sha256.h"
41#include "../tests/fixtures/osh/osh_layout.h"
42#include "osh_fixtures.h"
43#ifdef CK_OSH_OP_TEST
44#include "osh_op.h"
45#endif
46
47#define LABEL "QEMU (aarch64 virt), TEST signer, not physical"
48#define CKOSH_WS_CELLS 11456u
49#define WS_PAGES 23u
50#define INPUT_MAX 4096u
51#define REQ_HDR_CELLS 8u
52#define CMD_CELLS 164u
53
54/* the constants the shell was built against (omega src/osh/osh_layout.h at the pinned commit) */
55_Static_assert(OSH_WS_CELLS == CKOSH_WS_CELLS, "workspace cells differ from the pinned omega layout");
56_Static_assert((uint64_t)CKOSH_WS_CELLS * 8 <= (uint64_t)WS_PAGES * 4096, "23 pages hold the workspace");
57_Static_assert((uint64_t)CKOSH_WS_CELLS * 8 > (uint64_t)(WS_PAGES - 1) * 4096, "22 pages do not");
58_Static_assert(OSH_LINE_CAP == INPUT_MAX, "input window equals the lexer line cap");
59_Static_assert(OSH_REQ_CMD_CELLS == CMD_CELLS, "request command record size");
60
61enum { ST_MORE = 100, ST_BUDGET = 101, ST_NEED_VAR = 102, ST_PIPE = 103, ST_COMPLETE = 104 };
62enum { U_LEX = 0, U_PARSE = 1, U_EXPAND = 2, U_COUNT = 3 };
63static const char *const u_file[U_COUNT] = { "osh_lex.unit", "osh_parse.unit", "osh_expand.unit" };
64static const char *const u_entry[U_COUNT] = { "lex_run", "parse_run", "expand_run" };
65static const char *const u_label[U_COUNT] = { "lex", "parse", "expand" };
66/* each unit's realization receipt in the Store (aienos#383, tests/fixtures/osc_receipt; minted by omega checker2) */
67static const char *const u_rcpt[U_COUNT] = { "osh_lex.rcpt", "osh_parse.rcpt", "osh_expand.rcpt" };
68
69/* ck_osc_launch with the unit's receipt from the Store (none if it did not arrive: refused RECEIPT_MISSING) */
70static void launch_rcpt(const char *rname, const uint8_t *b, size_t len, const struct osc_accept *acc,
71 const struct ck_osc_launch_req *rq, struct osc_result *r, struct ck_osc_launch_info *info)
72{
73 size_t rl = 0;
74 const uint8_t *rb = ck_osc_receipt_find(rname, &rl);
75 ck_osc_launch(b, len, acc, rb, rl, rq, r, info);
76}
77
78#define UNIT_BYTES 131072u /* the packed containers (IR + code + manifest) pass 64 KiB; the code stays under CK_OSC_CODE_MAX_BYTES */
79static struct {
80 uint8_t bytes[UNIT_BYTES];
81 size_t len;
82 int have;
83 struct osc_accept acc;
84} unit[U_COUNT];
85
86static int streq(const char *a, const char *b)
87{
88 size_t n = strlen(a);
89 return n == strlen(b) && memcmp(a, b, n) == 0;
90}
91
92/* the launch gate's hand-assembled spin() unit, used only by control 4b */
93static struct {
94 uint8_t bytes[16384];
95 size_t len;
96 int have;
97 struct osc_accept acc;
98} spin_unit;
99
100void ck_osh_save(const char *name, const uint8_t *b, size_t len, const struct osc_accept *acc)
101{
102 if (streq(name, "l01_launch_fns.unit") && len <= sizeof spin_unit.bytes) {
103 memcpy(spin_unit.bytes, b, len);
104 spin_unit.len = len;
105 spin_unit.acc = *acc;
106 spin_unit.have = 1;
107 return;
108 }
109 for (unsigned u = 0; u < U_COUNT; u++) {
110 if (!streq(name, u_file[u]))
111 continue;
112 if (len > UNIT_BYTES) {
113 ck_printf("osh_test: %s is %u bytes, over the %u byte test buffer; not saved\n", name, (unsigned)len, UNIT_BYTES);
114 return;
115 }
116 memcpy(unit[u].bytes, b, len);
117 unit[u].len = len;
118 unit[u].acc = *acc;
119 unit[u].have = 1;
120 }
121}
122
123static uint8_t ws_mem[WS_PAGES * 4096] __attribute__((aligned(4096)));
124static uint8_t in_buf[INPUT_MAX];
125static size_t blen;
126static unsigned long step_no, req_no;
127static const unsigned char *src_text;
128static unsigned long src_len, src_pos;
129static int stop; /* a launch did not RETURN: the script is abandoned and the gate fails the diff */
130
131static uint64_t *const w = (uint64_t *)ws_mem;
132
133static void hex(const uint8_t *p, size_t n, char *o)
134{
135 static const char d[] = "0123456789abcdef";
136 uint8_t dg[SHA256_DIGEST_SIZE];
137 sha256_hash(p, n, dg);
138 for (unsigned i = 0; i < SHA256_DIGEST_SIZE; i++) {
139 o[2 * i] = d[dg[i] >> 4];
140 o[2 * i + 1] = d[dg[i] & 15];
141 }
142 o[64] = 0;
143}
144
145static unsigned long launches, slots_held; /* every launch is one EL0 task; a task slot not free after its call is a leak */
146
147/* One entry call through the launch interface. 0 = RETURNED (value in *ret), else the result is printed and 1. */
148static int launch(unsigned u, size_t in_len, uint64_t ws_cells, struct ck_osc_ws *wsr, uint64_t max_ticks, struct osc_result *r,
149 struct ck_osc_launch_info *info)
150{
151 struct ck_osc_launch_req rq;
152 memset(&rq, 0, sizeof rq);
153 rq.name = u_entry[u];
154 rq.name_len = strlen(u_entry[u]);
155 rq.nargs = 4;
156 rq.args[0] = ck_osc_va_in();
157 rq.args[1] = in_len;
158 rq.args[2] = ck_osc_va_ws();
159 rq.args[3] = ws_cells;
160 rq.in = in_buf;
161 rq.in_len = in_len;
162 rq.ws = wsr;
163 rq.max_ticks = max_ticks;
164 launch_rcpt(u_rcpt[u], unit[u].bytes, unit[u].len, &unit[u].acc, &rq, r, info);
165 launches++;
166 if (!info->slot_free_after)
167 slots_held++;
168 return r->cls != OSC_RES_RETURNED;
169}
170
171static struct ck_osc_ws main_ws = { ws_mem, WS_PAGES };
172
173static uint64_t call(unsigned u)
174{
175 struct osc_result r;
176 struct ck_osc_launch_info info;
177 char h[65];
178 if (launch(u, blen, CKOSH_WS_CELLS, &main_ws, 0, &r, &info)) {
179 char rs[96];
180 ck_osc_result_str(&r, rs, sizeof rs);
181 ck_printf("osh_fault: %s step %lu -> %s; %s\n", u_label[u], step_no + 1, rs, LABEL);
182 stop = 1;
183 return ~(uint64_t)0;
184 }
185 hex(ws_mem, (size_t)CKOSH_WS_CELLS * 8, h);
186 ck_printf("step %lu unit=%s in_len=%u ret=%llu ws_sha256=%s\n", ++step_no, u_label[u], (unsigned)blen,
187 (unsigned long long)r.value, h);
188 return r.value;
189}
190
191static void reset_list(void)
192{
193 memset(ws_mem, 0, sizeof ws_mem);
194 w[OSH_S_MAGIC] = OSH_ABI_MAGIC;
195 w[OSH_S_VERSION] = OSH_ABI_VERSION;
196 w[OSH_S_WS_CELLS] = OSH_WS_CELLS;
197 blen = 0;
198}
199
200static uint64_t run_entry(unsigned u)
201{
202 for (int i = 0; i < 100000; i++) {
203 uint64_t st = call(u);
204 if (stop || st != ST_BUDGET)
205 return st;
206 }
207 return ~(uint64_t)0;
208}
209
210static uint64_t lex_parse(void)
211{
212 uint64_t st = run_entry(U_LEX);
213 if (st != 0 || stop)
214 return st;
215 return run_entry(U_PARSE);
216}
217
218static int append(const uint8_t *p, size_t n)
219{
220 if (blen + n > INPUT_MAX) {
221 ck_printf("event input_window_exceeded have=%u add=%u max=%u\n", (unsigned)blen, (unsigned)n, INPUT_MAX);
222 return -1;
223 }
224 memcpy(in_buf + blen, p, n);
225 blen += n;
226 return 0;
227}
228
229static int read_line(void)
230{
231 if (src_pos >= src_len)
232 return 0;
233 const unsigned char *s = src_text + src_pos;
234 unsigned long n = 0;
235 while (src_pos + n < src_len && s[n] != '\n')
236 n++;
237 int nl = src_pos + n < src_len;
238 if (append(s, n) != 0)
239 return -1;
240 if (append((const uint8_t *)"\n", 1) != 0)
241 return -1;
242 src_pos += n + (unsigned long)nl;
243 return 1;
244}
245
246static size_t dec(uint64_t v, uint8_t *o)
247{
248 uint8_t t[24];
249 size_t n = 0;
250 do {
251 t[n++] = (uint8_t)('0' + v % 10);
252 v /= 10;
253 } while (v);
254 for (size_t i = 0; i < n; i++)
255 o[i] = t[n - 1 - i];
256 return n;
257}
258
259static const char *var_get(const char *name)
260{
261 if (streq(name, "HOME"))
262 return "/home/t";
263 if (streq(name, "X"))
264 return "a b";
265 if (streq(name, "EMPTY"))
266 return "";
267 return NULL;
268}
269
270/* Per fixture, as omega tests/osh/osh_trace.c load_fixture(): fake pipeline statuses from the .status sidecar, the
271 * positionals $1.. from the .args sidecar (one per line), $0 = "osh_trace". */
272#define MAX_FAKE 256
273#define MAX_POS 9
274static int last_status; /* the shell's $?: fake pipeline statuses, 2 after a refusal */
275static int fake[MAX_FAKE], nfake, fake_i, have_status;
276static char pos_buf[4096];
277static const char *pos[MAX_POS];
278static int npos;
279static int exhausted; /* .status ran out: omega's osh_trace exits 2 after its event line, with no exit line */
280static const char ARG0[] = "osh_trace";
281
282static int next_fake(void)
283{
284 if (fake_i < nfake)
285 return fake[fake_i++];
286 if (have_status) {
287 ck_printf("event fake_status_exhausted after=%d\n", nfake);
288 exhausted = 1;
289 }
290 return 0;
291}
292
293static int is_ws(unsigned char c)
294{
295 return c == ' ' || c == '\t' || c == '\n';
296}
297
298/* 0, or -1 with a line printed: a malformed sidecar is a fixture error (omega's osh_trace exits 2 on it) */
299static int load_fixture(unsigned i)
300{
301 nfake = fake_i = have_status = npos = exhausted = 0;
302 last_status = 0;
303 if (osh_fx[i].status) {
304 const unsigned char *s = osh_fx[i].status;
305 unsigned long n = osh_fx[i].status_len, q = 0;
306 have_status = 1;
307 for (;;) {
308 while (q < n && is_ws(s[q]))
309 q++;
310 if (q >= n)
311 break;
312 unsigned long v = 0, d = 0;
313 while (q < n && s[q] >= '0' && s[q] <= '9' && v <= 255) {
314 v = v * 10 + (unsigned long)(s[q++] - '0');
315 d++;
316 }
317 if (d == 0 || v > 255 || (q < n && !is_ws(s[q])) || nfake == MAX_FAKE) {
318 ck_printf("osh_fixture_error: %s.status; %s\n", osh_fx[i].name, LABEL);
319 return -1;
320 }
321 fake[nfake++] = (int)v;
322 }
323 }
324 if (osh_fx[i].args) {
325 const unsigned char *s = osh_fx[i].args;
326 unsigned long n = osh_fx[i].args_len, q = 0, o = 0;
327 if (n >= sizeof pos_buf) {
328 ck_printf("osh_fixture_error: %s.args too long; %s\n", osh_fx[i].name, LABEL);
329 return -1;
330 }
331 while (q < n) {
332 if (npos == MAX_POS) {
333 ck_printf("osh_fixture_error: %s.args has more than %d arguments; %s\n", osh_fx[i].name, MAX_POS, LABEL);
334 return -1;
335 }
336 pos[npos++] = pos_buf + o;
337 while (q < n && s[q] != '\n')
338 pos_buf[o++] = (char)s[q++];
339 pos_buf[o++] = 0;
340 q++; /* the newline (or one past the end) */
341 }
342 }
343 return 0;
344}
345
346static void answer(void)
347{
348 uint64_t kind = w[OSH_S_VR_KIND], a = w[OSH_S_VR_A], len = w[OSH_S_VR_LEN];
349 const uint8_t *val = NULL;
350 uint8_t num[24];
351 size_t vl = 0;
352 int found = 0;
353 switch (kind) {
354 case 1: {
355 char name[256];
356 if (a <= blen && len < sizeof name && len <= blen - a) {
357 memcpy(name, in_buf + a, (size_t)len);
358 name[len] = 0;
359 const char *v = var_get(name);
360 if (v) {
361 found = 1;
362 val = (const uint8_t *)v;
363 vl = strlen(v);
364 }
365 }
366 break;
367 }
368 case 2:
369 found = 1;
370 vl = dec((uint64_t)(unsigned)last_status, num);
371 val = num;
372 break;
373 case 3: /* as osh_shell.c answer() */
374 if (a == 0) {
375 found = 1;
376 val = (const uint8_t *)ARG0;
377 vl = strlen(ARG0);
378 } else if (a >= 1 && a <= (uint64_t)npos) {
379 found = 1;
380 val = (const uint8_t *)pos[a - 1];
381 vl = strlen(pos[a - 1]);
382 }
383 break;
384 case 4:
385 found = 1;
386 vl = dec((uint64_t)npos, num);
387 val = num;
388 break;
389 default:
390 break;
391 }
392 w[OSH_STAGING + 0] = (uint64_t)found;
393 w[OSH_STAGING + 1] = vl;
394 w[OSH_STAGING + 2] = (uint64_t)npos;
395 w[OSH_STAGING + 3] = 0;
396 for (size_t i = 0; i < vl && i < 1024; i++)
397 w[OSH_STAGING + 4 + i] = val[i];
398}
399
400#ifdef CK_OSH_OP_TEST
401static int real_mode, real_stop;
402static int real_pipeline(void);
403#endif
404
405/* returns 1 when the driver must stop */
406static int run_list(void)
407{
408 w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)last_status;
409 for (int guard = 0; guard < 1000000; guard++) {
410 uint64_t st = call(U_EXPAND);
411 if (stop)
412 return 1;
413 switch (st) {
414 case ST_BUDGET:
415 continue;
416 case ST_NEED_VAR:
417 answer();
418 continue;
419 case ST_PIPE: {
420 uint64_t nc = w[OSH_REQUEST + 1];
421 if (nc > (OSH_OUT - OSH_REQUEST - REQ_HDR_CELLS) / CMD_CELLS) { /* never read past REQUEST */
422 ck_printf("event request_ncmds_out_of_range ncmds=%llu\n", (unsigned long long)nc);
423 last_status = 2;
424 return 1;
425 }
426 size_t cells = (size_t)(REQ_HDR_CELLS + nc * CMD_CELLS);
427#ifdef CK_OSH_OP_TEST
428 if (real_mode) {
429 last_status = real_pipeline();
430 if (real_stop)
431 return 1;
432 w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)last_status;
433 continue;
434 }
435#endif
436 char h[65];
437 hex((const uint8_t *)(w + OSH_REQUEST), cells * 8, h);
438 ck_printf("request %lu sha256=%s\n", ++req_no, h);
439 last_status = next_fake(); /* nothing ran: the fixture's next status */
440 if (exhausted)
441 return 1;
442 w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)last_status;
443 continue;
444 }
445 case ST_COMPLETE:
446 return 0;
447 default: /* as osh_shell.c: a refusal stops a non-interactive shell with 2, a unit fault status with 70 */
448 if (st == ~(uint64_t)0 || st < 200) {
449 ck_printf("event internal_error status=%llu\n", (unsigned long long)st);
450 last_status = 70;
451 return 1;
452 }
453 last_status = 2;
454 return 1;
455 }
456 }
457 return 1;
458}
459
460static int run_src(void)
461{
462 for (;;) {
463 reset_list();
464 uint64_t st;
465 int eof = 0;
466 for (;;) {
467 int r = read_line();
468 if (r < 0) {
469 last_status = 2;
470 return 2;
471 }
472 if (r == 0) {
473 eof = 1;
474 if (blen == 0)
475 return 0;
476 w[OSH_S_EOI] = 1;
477 }
478 st = lex_parse();
479 if (stop)
480 return 3;
481 if (st == 0 && w[OSH_PX_STATE] != 4)
482 st = eof ? ST_COMPLETE : ST_MORE;
483 if (st == ST_MORE && !eof)
484 continue;
485 break;
486 }
487 if (st == ST_COMPLETE) {
488 } else if (st == 0) {
489 if (run_list())
490 return 1;
491 } else {
492 last_status = 2; /* lexer or parser refusal */
493 return 2;
494 }
495 if (eof)
496 return 0;
497 }
498}
499
500static void neg_line(const char *tag, const char *what, const struct osc_result *r, const struct ck_osc_launch_info *info, const char *extra)
501{
502 char rs[96];
503 ck_osc_result_str(r, rs, sizeof rs);
504 ck_printf("osh_neg%s: %s -> %s ticks=%llu budget=%llu pages_mapped=%u page_tables_zeroed=%u slot_free=%u%s%s; %s\n", tag, what,
505 rs, (unsigned long long)r->ticks, (unsigned long long)info->budget, info->pages_mapped, (unsigned)info->tables_zeroed,
506 (unsigned)info->slot_free_after, extra[0] ? " " : "", extra, LABEL);
507}
508
509/* the longest fixture, used by control 4 */
510static unsigned long_fx;
511
512static void negatives(void)
513{
514 struct osc_result r;
515 struct ck_osc_launch_info info;
516 char h0[65], h1[65], x[160];
517
518 /* 1: 22 pages, pointer range claiming 11456 cells -> refused 41 before the first instruction */
519 reset_list();
520 memcpy(in_buf, "echo hi\n", 8);
521 struct ck_osc_ws ws22 = { ws_mem, WS_PAGES - 1 };
522 launch(U_LEX, 8, CKOSH_WS_CELLS, &ws22, 0, &r, &info);
523 neg_line("1", "lex_run(in+0,8,ws+0,11456) with a 22-page workspace", &r, &info, "");
524
525 /* 2a: workspace passed with len 512 cells (23 pages mapped): emit_tok with t=127 would store at cell 572 */
526 reset_list();
527 w[17] = 127;
528 hex(ws_mem + 512 * 8, sizeof ws_mem - 512 * 8, h0); /* every cell from 512 up */
529 struct ck_osc_launch_req rq;
530 memset(&rq, 0, sizeof rq);
531 rq.name = "emit_tok";
532 rq.name_len = 8;
533 rq.nargs = 6;
534 rq.args[0] = ck_osc_va_ws();
535 rq.args[1] = 512; /* cells: len 512, far smaller than the unit needs */
536 rq.args[2] = 0x1111;
537 rq.args[3] = 0x2222;
538 rq.args[4] = 0x3333;
539 rq.args[5] = 0x4444;
540 rq.ws = &main_ws;
541 launch_rcpt(u_rcpt[U_LEX], unit[U_LEX].bytes, unit[U_LEX].len, &unit[U_LEX].acc, &rq, &r, &info);
542 hex(ws_mem + 512 * 8, sizeof ws_mem - 512 * 8, h1);
543 ck_snprintf(x, sizeof x, "cell17=%llu cells_512_up_unchanged=%u", (unsigned long long)w[17], (unsigned)streq(h0, h1));
544 neg_line("2a", "lex emit_tok(ws+0,len 512 cells,..) with w[17]=127", &r, &info, x);
545
546 /* 2b: the entry point with len 512: the unit's own ABI check refuses (WORKSPACE_SIZE 213), no write past 512 */
547 reset_list();
548 hex(ws_mem + 512 * 8, sizeof ws_mem - 512 * 8, h0);
549 memcpy(in_buf, "echo hi\n", 8);
550 launch(U_LEX, 8, 512, &main_ws, 0, &r, &info);
551 hex(ws_mem + 512 * 8, sizeof ws_mem - 512 * 8, h1);
552 ck_snprintf(x, sizeof x, "cells_512_up_unchanged=%u", (unsigned)streq(h0, h1));
553 neg_line("2b", "lex_run(in+0,8,ws+0,512) with a 23-page workspace", &r, &info, x);
554
555 /* 3: a unit with one flipped code byte is refused at admission by the Store stage; here: nothing of it can run */
556 int ran = 0;
557 for (unsigned u = 0; u < U_COUNT; u++)
558 if (streq(u_file[u], "osh_lex_flipped.unit") && unit[u].have)
559 ran = 1;
560 ck_printf("osh_neg3: osh_lex_flipped.unit -> NOT_RUN (unit was not admitted)%s; %s\n", ran ? " BUG" : "", LABEL);
561
562 /* 4: caller max_ticks 1 on the longest fixture */
563 reset_list();
564 src_text = osh_fx[long_fx].text;
565 src_len = osh_fx[long_fx].len;
566 src_pos = 0;
567 blen = 0;
568 {
569 /* the whole fixture as one list buffer (it fits the window) */
570 unsigned long n = src_len < INPUT_MAX ? src_len : INPUT_MAX - 1;
571 memcpy(in_buf, src_text, n);
572 in_buf[n++] = '\n';
573 blen = n;
574 }
575 w[OSH_S_EOI] = 1;
576 launch(U_LEX, blen, CKOSH_WS_CELLS, &main_ws, 1, &r, &info);
577 ck_snprintf(x, sizeof x, "fixture=%s in_len=%u max_ticks=1", osh_fx[long_fx].name, (unsigned)blen);
578 neg_line("4a", "lex_run over the long fixture", &r, &info, x);
579 /* 4b: the shell units are loop-bounded (every loop has a bound, every entry call a step budget), so no single call of
580 * theirs lasts a whole 10 ms tick under QEMU TCG: 4a returns inside the tick. The same cap through the same launch
581 * path, on the launch gate's spin() unit, shows the budget bites. */
582 if (spin_unit.have) {
583 memset(&rq, 0, sizeof rq);
584 rq.name = "spin";
585 rq.name_len = 4;
586 rq.max_ticks = 1;
587 launch_rcpt("l01.rcpt", spin_unit.bytes, spin_unit.len, &spin_unit.acc, &rq, &r, &info);
588 neg_line("4b", "l01 spin() with the same max_ticks=1 cap", &r, &info, "");
589 } else {
590 ck_printf("osh_neg4b: NOT_RUN (l01_launch_fns.unit was not admitted); %s\n", LABEL);
591 }
592 /* and the next launch works */
593 reset_list();
594 memcpy(in_buf, "echo hi\n", 8);
595 w[OSH_S_EOI] = 1;
596 launch(U_LEX, 8, CKOSH_WS_CELLS, &main_ws, 0, &r, &info);
597 neg_line("4n", "after the overrun, lex_run(in+0,8) on the same workspace", &r, &info, "");
598}
599
600#ifdef CK_OSH_OP_TEST
601/* ---- OSH-AIENOS-OP: a real, capability-authorized operation through the same shell units ----
602 * The fixture driver above is unchanged and is NOT run in this build (the OSH_CORE gate runs it). Here each script goes
603 * through the same lex/parse/expand EL0 tasks; on PIPELINE_READY the request record goes to core/osh_op.c instead of
604 * a fake status. Operation: `klog <words>` writes "osh_op_out: <words>" to the console; it needs WRITE on the console-log
605 * resource in the session's kernel capability table. QEMU (aarch64 virt), TEST signer, not physical. */
606static struct osh_session sess;
607static const char *case_name;
608static unsigned sink_calls;
609static int real_crashed;
610
611static int op_sink(void *ctx, const char *line, size_t len)
612{
613 (void)ctx;
614 (void)len;
615 sink_calls++;
616 ck_printf("%s", line);
617 return 0;
618}
619
620static void op_log(void *ctx, const char *line)
621{
622 (void)ctx;
623 ck_printf("%s case=%s; %s\n", line, case_name, LABEL);
624}
625
626static const struct osh_env op_env = { 0, op_sink, op_log };
627
628static void hex32(const uint8_t d[32], char o[65])
629{
630 static const char x[] = "0123456789abcdef";
631 for (unsigned i = 0; i < 32; i++) {
632 o[2 * i] = x[d[i] >> 4];
633 o[2 * i + 1] = x[d[i] & 15];
634 }
635 o[64] = 0;
636}
637
638static int real_pipeline(void)
639{
640 struct osh_result r;
641 char h[65];
642 osh_op_exec(&sess, &op_env, w + OSH_REQUEST, w + OSH_OUT, (size_t)(CKOSH_WS_CELLS - OSH_OUT), &r);
643 hex32(r.digest, h);
644 ck_printf("osh_op_pipeline: case=%s n=%u status=%d error=%s outcome=%s stop=%d crashed=%d request_digest=%s; %s\n", case_name,
645 (unsigned)r.seq, r.status, osh_err_name(r.error), osh_outcome_name(r.outcome), r.stop, r.crashed, h, LABEL);
646 if (r.stop)
647 real_stop = 1;
648 if (r.crashed)
649 real_crashed = 1;
650 return r.status;
651}
652
653static void scenario(const char *name, unsigned rights, const char *script, struct osh_hooks hk)
654{
655 case_name = name;
656 osh_session_init(&sess, rights);
657 sess.hooks = hk;
658 sink_calls = 0;
659 real_mode = 1;
660 real_stop = 0;
661 real_crashed = 0;
662 stop = 0;
663 step_no = req_no = 0;
664 nfake = fake_i = have_status = npos = exhausted = 0;
665 last_status = 0;
666 src_text = (const unsigned char *)script;
667 src_len = strlen(script);
668 src_pos = 0;
669 unsigned long l0 = launches, s0 = slots_held;
670 ck_printf("osh_op_begin: case=%s cap_rights=%u; %s\n", name, rights, LABEL);
671 int rc = run_src();
672 struct ck_handle held = sess.console_cap;
673 int empty = osh_session_close(&sess);
674 int released = ck_cap_lookup(&sess.caps, held, 0, 0) != CK_CAP_OK;
675 ck_printf("osh_op_end: case=%s rc=%d final_status=%d sink_calls=%u launches=%lu slots_held=%lu cap_table_empty=%d cap_released=%d; %s\n",
676 name, rc, last_status, sink_calls, launches - l0, slots_held - s0, empty, released, LABEL);
677 if (real_crashed) {
678 /* the adapter was lost after the effect: a restart keeps only the intent records and holds no capability */
679 struct osh_session fresh;
680 unsigned before = sink_calls;
681 osh_session_restart(&fresh, &sess);
682 unsigned n = osh_op_recover(&fresh, &op_env);
683 unsigned again = osh_op_recover(&fresh, &op_env);
684 ck_printf("osh_op_restart: case=%s open_intents=%u second_scan=%u sink_calls_before=%u sink_calls_after=%u held_cap=%u; %s\n",
685 name, n, again, before, sink_calls, (unsigned)fresh.console_cap.generation, LABEL);
686 }
687 real_mode = 0;
688}
689
690static void op_scenarios(void)
691{
692 struct osh_hooks none = { 0, 0, 0, 0 };
693 struct osh_hooks rv = { 1, 0, 0, 0 }, ir = { 0, 2, 0, 0 }, cr = { 0, 0, 1, 0 };
694 scenario("allow", CK_R_WRITE, "klog hello from osh\nklog \"status=$?\"\n", none);
695 scenario("deny_no_cap", 0, "klog SECRET-no-cap\n", none);
696 scenario("deny_read_only", CK_R_READ, "klog SECRET-read-only\n", none);
697 scenario("revoked", CK_R_WRITE, "klog SECRET-revoked\n", rv);
698 scenario("interrupt", CK_R_WRITE, "klog one; klog two; klog three\n", ir);
699 scenario("lost", CK_R_WRITE, "klog LOST-MARKER\n", cr);
700 scenario("after_restart", CK_R_WRITE, "klog fresh decision\n", none);
701 scenario("unsupported", CK_R_WRITE, "klog a | klog SECRET-pipe\nklog SECRET-redir >f\nnosuchcmd\n", none);
702 ck_printf("osh_op_done: cases=8; %s\n", LABEL);
703}
704#endif
705
706void ck_osh_all(void)
707{
708 unsigned have = 0;
709 for (unsigned u = 0; u < U_COUNT; u++)
710 have += (unsigned)unit[u].have;
711 ck_printf("osh_core: units=%u ws_cells=%u ws_pages=%u input_max=%u; %s\n", have, CKOSH_WS_CELLS, WS_PAGES, INPUT_MAX, LABEL);
712 if (have != U_COUNT) {
713 ck_printf("osh_core: NOT_RUN (a shell unit was not admitted); %s\n", LABEL);
714 return;
715 }
716#ifdef CK_OSH_OP_TEST
717 op_scenarios();
718 return;
719#endif
720 unsigned nfx = (unsigned)(sizeof osh_fx / sizeof osh_fx[0]);
721 unsigned long best = 0;
722 for (unsigned i = 0; i < nfx; i++) {
723 if (osh_fx[i].len > best && osh_fx[i].len <= INPUT_MAX) {
724 best = osh_fx[i].len;
725 long_fx = i;
726 }
727 stop = 0;
728 step_no = 0;
729 req_no = 0;
730 src_text = osh_fx[i].text;
731 src_len = osh_fx[i].len;
732 src_pos = 0;
733 ck_printf("osh_script_begin %s\n", osh_fx[i].name);
734 int rc = load_fixture(i) != 0 ? 4 : run_src();
735 /* omega's osh_trace ends every trace with the exit line, except after fake_status_exhausted (it exits 2 there);
736 * after a launch that did not RETURN (stop) the trace is incomplete and the gate's diff fails it */
737 if (rc != 4 && !stop && !exhausted)
738 ck_printf("event exit=%d\n", last_status);
739 ck_printf("osh_script_end %s rc=%d\n", osh_fx[i].name, rc);
740 }
741 negatives();
742 ck_printf("osh_core_done: scripts=%u; %s\n", nfx, LABEL);
743}