omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / osh / osh_trace.c
| 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 | /* |
| 18 | * osh_trace.c -- core-only step trace of the Omega-native shell (OSH-AIENOS-0 differential fixture). |
| 19 | * Mirrors the sequencing of src/osh/host/osh_shell.c in script-file mode (run_src / lex_parse / run_list) over |
| 20 | * osh_core_call, but executes nothing: on PIPELINE_READY it only records the request and takes the next fake status; |
| 21 | * NEED_VAR is answered from a fixed variable table and the fixture's positionals. |
| 22 | * usage: osh_trace interp|native lex.osc parse.osc expand.osc script |
| 23 | * Per fixture, optional: <script minus .sh>.status whitespace-separated pipeline statuses, used in order (then 0) |
| 24 | * <script minus .sh>.args positional parameters $1.., one per line; $0 is "osh_trace" |
| 25 | * Output, one line per unit call: step <n> unit=<lex|parse|expand> in_len=<n> ret=<u64> ws_sha256=<hex of WS_CELLS*8 bytes LE> |
| 26 | * and on each pipeline request: request <n> sha256=<hex of the request record cells> |
| 27 | * and for driver-level events: event <text>; the last line is always event exit=<status> (2 after a refusal) |
| 28 | * Not modelled (nothing runs): the exit builtin, exit with too many arguments dropping the list, ^C, -c exact end of |
| 29 | * input. INPUT_MAX models the Aienux read-only input window (4096 bytes): a list buffer that would exceed it is |
| 30 | * refused, so above 4096 bytes this is not a reference for the Linux shell (its buffer is 1 MiB). Native mode needs |
| 31 | * an AArch64 host, as every osh test does. |
| 32 | */ |
| 33 | #include <stdint.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "osh_core.h" |
| 39 | #include "osh_host.h" |
| 40 | #include "osh_layout.h" |
| 41 | #include "sha256.h" |
| 42 | |
| 43 | #define WS ((size_t)OSH_WS_CELLS) |
| 44 | #define INPUT_MAX 4096u |
| 45 | enum { ST_MORE = 100, ST_BUDGET = 101, ST_NEED_VAR = 102, ST_PIPE = 103, ST_COMPLETE = 104 }; |
| 46 | |
| 47 | static OshCore core; |
| 48 | static int native_mode; |
| 49 | static uint64_t w[OSH_WS_CELLS]; |
| 50 | static uint8_t buf[INPUT_MAX]; |
| 51 | static size_t blen; |
| 52 | static unsigned long step_no, req_no; |
| 53 | static const char *src_text; |
| 54 | static size_t src_len, src_pos; |
| 55 | |
| 56 | static void hex(const uint8_t *p, size_t n, char *o) |
| 57 | { |
| 58 | uint8_t d[SHA256_DIGEST_SIZE]; |
| 59 | sha256_hash(p, n, d); |
| 60 | for (int i = 0; i < SHA256_DIGEST_SIZE; i++) sprintf(o + 2 * i, "%02x", d[i]); |
| 61 | } |
| 62 | |
| 63 | static uint64_t call(int unit) |
| 64 | { |
| 65 | static const char *const nm[] = {"lex", "parse", "expand"}; |
| 66 | uint64_t r = osh_core_call(&core, unit, native_mode, buf, blen, w, WS); |
| 67 | char h[65]; |
| 68 | hex((const uint8_t *)w, WS * 8, h); |
| 69 | printf("step %lu unit=%s in_len=%zu ret=%llu ws_sha256=%s\n", ++step_no, nm[unit], blen, (unsigned long long)r, h); |
| 70 | return r; |
| 71 | } |
| 72 | |
| 73 | static void reset_list(void) |
| 74 | { |
| 75 | memset(w, 0, sizeof w); |
| 76 | w[OSH_S_MAGIC] = OSH_ABI_MAGIC; |
| 77 | w[OSH_S_VERSION] = OSH_ABI_VERSION; |
| 78 | w[OSH_S_WS_CELLS] = OSH_WS_CELLS; |
| 79 | blen = 0; |
| 80 | } |
| 81 | |
| 82 | static uint64_t run_entry(int unit) |
| 83 | { |
| 84 | for (int i = 0; i < 100000; i++) { |
| 85 | uint64_t st = call(unit); |
| 86 | if (st != ST_BUDGET) return st; |
| 87 | } |
| 88 | return OSH_CORE_FAULT; |
| 89 | } |
| 90 | |
| 91 | static uint64_t lex_parse(void) |
| 92 | { |
| 93 | uint64_t st = run_entry(OSH_U_LEX); |
| 94 | if (st != 0) return st; |
| 95 | return run_entry(OSH_U_PARSE); |
| 96 | } |
| 97 | |
| 98 | static int append(const uint8_t *p, size_t n) |
| 99 | { |
| 100 | if (blen + n > INPUT_MAX) { |
| 101 | printf("event input_window_exceeded have=%zu add=%zu max=%u\n", blen, n, INPUT_MAX); |
| 102 | return -1; |
| 103 | } |
| 104 | memcpy(buf + blen, p, n); |
| 105 | blen += n; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int read_line(void) |
| 110 | { |
| 111 | if (src_pos >= src_len) return 0; |
| 112 | const char *s = src_text + src_pos; |
| 113 | size_t n = 0; |
| 114 | while (src_pos + n < src_len && s[n] != '\n') n++; |
| 115 | int nl = src_pos + n < src_len; |
| 116 | if (append((const uint8_t *)s, n) != 0) return -1; |
| 117 | if (append((const uint8_t *)"\n", 1) != 0) return -1; |
| 118 | src_pos += n + (size_t)nl; |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | static size_t dec(uint64_t v, uint8_t *o) |
| 123 | { |
| 124 | uint8_t t[24]; |
| 125 | size_t n = 0; |
| 126 | do { t[n++] = (uint8_t)('0' + v % 10); v /= 10; } while (v); |
| 127 | for (size_t i = 0; i < n; i++) o[i] = t[n - 1 - i]; |
| 128 | return n; |
| 129 | } |
| 130 | |
| 131 | static const char *var_get(const char *name) |
| 132 | { |
| 133 | if (!strcmp(name, "HOME")) return "/home/t"; |
| 134 | if (!strcmp(name, "X")) return "a b"; |
| 135 | if (!strcmp(name, "EMPTY")) return ""; |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | #define MAX_FAKE 256 |
| 140 | #define MAX_POS 9 |
| 141 | static int last_status; /* the shell's $?: fake pipeline statuses, 2 after a refusal */ |
| 142 | static int fake[MAX_FAKE], nfake, fake_i; |
| 143 | static char *pos[MAX_POS]; |
| 144 | static int npos; |
| 145 | static const char ARG0[] = "osh_trace"; |
| 146 | |
| 147 | static int have_status; /* a .status file was given: running out of statuses is a fixture error */ |
| 148 | static int next_fake(void) |
| 149 | { |
| 150 | if (fake_i < nfake) return fake[fake_i++]; |
| 151 | if (have_status) { printf("event fake_status_exhausted after=%d\n", nfake); fflush(stdout); fprintf(stderr, "osh_trace: .status has too few statuses\n"); exit(2); } |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static void answer(void) |
| 156 | { |
| 157 | uint64_t kind = w[OSH_S_VR_KIND], a = w[OSH_S_VR_A], len = w[OSH_S_VR_LEN]; |
| 158 | const uint8_t *val = NULL; |
| 159 | uint8_t num[24]; |
| 160 | size_t vl = 0; |
| 161 | int found = 0; |
| 162 | switch (kind) { |
| 163 | case 1: { |
| 164 | char name[256]; |
| 165 | if (a <= blen && len < sizeof name && len <= blen - a) { |
| 166 | memcpy(name, buf + a, (size_t)len); |
| 167 | name[len] = 0; |
| 168 | const char *v = var_get(name); |
| 169 | if (v) { found = 1; val = (const uint8_t *)v; vl = strlen(v); } |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | case 2: found = 1; vl = dec((uint64_t)(unsigned)last_status, num); val = num; break; |
| 174 | case 3: /* as osh_shell.c answer() */ |
| 175 | if (a == 0) { found = 1; val = (const uint8_t *)ARG0; vl = strlen(ARG0); } |
| 176 | else if (a >= 1 && a <= (uint64_t)npos) { found = 1; val = (const uint8_t *)pos[a - 1]; vl = strlen(pos[a - 1]); } |
| 177 | break; |
| 178 | case 4: found = 1; vl = dec((uint64_t)npos, num); val = num; break; |
| 179 | default: break; |
| 180 | } |
| 181 | w[OSH_STAGING + 0] = (uint64_t)found; |
| 182 | w[OSH_STAGING + 1] = vl; |
| 183 | w[OSH_STAGING + 2] = (uint64_t)npos; |
| 184 | w[OSH_STAGING + 3] = 0; |
| 185 | for (size_t i = 0; i < vl && i < 1024; i++) w[OSH_STAGING + 4 + i] = val[i]; |
| 186 | } |
| 187 | |
| 188 | /* returns 1 when the driver must stop */ |
| 189 | static int run_list(void) |
| 190 | { |
| 191 | w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)last_status; |
| 192 | for (int guard = 0; guard < 1000000; guard++) { |
| 193 | uint64_t st = call(OSH_U_EXPAND); |
| 194 | switch (st) { |
| 195 | case ST_BUDGET: continue; |
| 196 | case ST_NEED_VAR: answer(); continue; |
| 197 | case ST_PIPE: { |
| 198 | uint64_t nc = w[OSH_REQUEST + 1]; |
| 199 | if (nc > (OSH_OUT - OSH_REQUEST - OSH_REQ_HDR_CELLS) / OSH_CMD_CELLS) { /* never read past REQUEST */ |
| 200 | printf("event request_ncmds_out_of_range ncmds=%llu\n", (unsigned long long)nc); |
| 201 | last_status = 2; |
| 202 | return 1; |
| 203 | } |
| 204 | size_t cells = (size_t)(OSH_REQ_HDR_CELLS + nc * OSH_CMD_CELLS); |
| 205 | char h[65]; |
| 206 | hex((const uint8_t *)(w + OSH_REQUEST), cells * 8, h); |
| 207 | printf("request %lu sha256=%s\n", ++req_no, h); |
| 208 | last_status = next_fake(); /* nothing ran: the fixture's next status */ |
| 209 | w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)last_status; |
| 210 | continue; |
| 211 | } |
| 212 | case ST_COMPLETE: return 0; |
| 213 | default: /* as osh_shell.c: a refusal stops a non-interactive shell with 2, a unit fault with 70 */ |
| 214 | if (st == OSH_CORE_FAULT || st < 200) { printf("event internal_error status=%llu\n", (unsigned long long)st); last_status = 70; return 1; } |
| 215 | last_status = 2; |
| 216 | return 1; |
| 217 | } |
| 218 | } |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | static int run_src(void) |
| 223 | { |
| 224 | for (;;) { |
| 225 | reset_list(); |
| 226 | uint64_t st; |
| 227 | int eof = 0; |
| 228 | for (;;) { |
| 229 | int r = read_line(); |
| 230 | if (r < 0) { last_status = 2; return 2; } |
| 231 | if (r == 0) { |
| 232 | eof = 1; |
| 233 | if (blen == 0) return 0; |
| 234 | w[OSH_S_EOI] = 1; |
| 235 | } |
| 236 | st = lex_parse(); |
| 237 | if (st == 0 && w[OSH_PX_STATE] != 4) st = eof ? ST_COMPLETE : ST_MORE; |
| 238 | if (st == ST_MORE && !eof) continue; |
| 239 | break; |
| 240 | } |
| 241 | if (st == ST_COMPLETE) { |
| 242 | } else if (st == 0) { |
| 243 | if (run_list()) return 1; |
| 244 | } else { |
| 245 | last_status = 2; /* lexer or parser refusal */ |
| 246 | return 2; |
| 247 | } |
| 248 | if (eof) return 0; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* read a whole file; a missing optional file gives NULL */ |
| 253 | static char *slurp(const char *path, size_t *n, int optional) |
| 254 | { |
| 255 | FILE *f = fopen(path, "rb"); |
| 256 | if (!f) { |
| 257 | if (optional) return NULL; |
| 258 | fprintf(stderr, "osh_trace: cannot open %s\n", path); |
| 259 | exit(2); |
| 260 | } |
| 261 | size_t cap = 1 << 16, len = 0; |
| 262 | char *b = malloc(cap + 1); |
| 263 | size_t r; |
| 264 | if (!b) { fprintf(stderr, "osh_trace: out of memory\n"); exit(2); } |
| 265 | while ((r = fread(b + len, 1, cap - len, f)) > 0) { |
| 266 | len += r; |
| 267 | if (len == cap) { |
| 268 | char *nb = realloc(b, (cap *= 2) + 1); |
| 269 | if (!nb) { fprintf(stderr, "osh_trace: out of memory\n"); exit(2); } |
| 270 | b = nb; |
| 271 | } |
| 272 | } |
| 273 | if (ferror(f)) { fprintf(stderr, "osh_trace: cannot read %s\n", path); exit(2); } |
| 274 | fclose(f); |
| 275 | b[len] = 0; |
| 276 | *n = len; |
| 277 | return b; |
| 278 | } |
| 279 | |
| 280 | /* <script minus .sh> + ext, or exit when the script name does not end in .sh */ |
| 281 | static void sidecar(const char *script, const char *ext, char *out, size_t cap) |
| 282 | { |
| 283 | size_t l = strlen(script); |
| 284 | if (l < 3 || strcmp(script + l - 3, ".sh") != 0 || l - 3 + strlen(ext) + 1 > cap) { |
| 285 | fprintf(stderr, "osh_trace: script must end in .sh: %s\n", script); |
| 286 | exit(2); |
| 287 | } |
| 288 | memcpy(out, script, l - 3); |
| 289 | strcpy(out + l - 3, ext); |
| 290 | } |
| 291 | |
| 292 | static void load_fixture(const char *script) |
| 293 | { |
| 294 | char p[4096]; |
| 295 | size_t n; |
| 296 | sidecar(script, ".status", p, sizeof p); |
| 297 | char *s = slurp(p, &n, 1); |
| 298 | if (s) { |
| 299 | char *e; |
| 300 | have_status = 1; |
| 301 | for (char *q = s;;) { |
| 302 | while (*q == ' ' || *q == '\t' || *q == '\n') q++; |
| 303 | if (!*q) break; |
| 304 | long v = strtol(q, &e, 10); |
| 305 | if (e == q || v < 0 || v > 255) { fprintf(stderr, "osh_trace: bad status in %s\n", p); exit(2); } |
| 306 | if (nfake == MAX_FAKE) { fprintf(stderr, "osh_trace: more than %d statuses in %s\n", MAX_FAKE, p); exit(2); } |
| 307 | fake[nfake++] = (int)v; |
| 308 | q = e; |
| 309 | } |
| 310 | } |
| 311 | sidecar(script, ".args", p, sizeof p); |
| 312 | s = slurp(p, &n, 1); |
| 313 | if (s) { |
| 314 | for (char *q = s; *q;) { |
| 315 | if (npos == MAX_POS) { fprintf(stderr, "osh_trace: more than %d arguments in %s\n", MAX_POS, p); exit(2); } |
| 316 | char *nl = strchr(q, '\n'); |
| 317 | if (nl) *nl = 0; |
| 318 | pos[npos++] = q; |
| 319 | if (!nl) break; |
| 320 | q = nl + 1; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | int main(int argc, char **argv) |
| 326 | { |
| 327 | if (argc != 6) { fprintf(stderr, "usage: osh_trace interp|native lex.osc parse.osc expand.osc script\n"); return 2; } |
| 328 | { |
| 329 | uint16_t one = 1; |
| 330 | if (*(uint8_t *)&one != 1) { fprintf(stderr, "osh_trace: needs a little-endian host\n"); return 2; } |
| 331 | } |
| 332 | native_mode = strcmp(argv[1], "native") == 0; |
| 333 | const char *srcs[3]; |
| 334 | size_t lens[3]; |
| 335 | for (int i = 0; i < 3; i++) srcs[i] = slurp(argv[2 + i], &lens[i], 0); |
| 336 | char err[300]; |
| 337 | if (osh_core_init(&core, srcs, lens, err, sizeof err) != 0) { fprintf(stderr, "osh_trace: %s\n", err); return 2; } |
| 338 | src_text = slurp(argv[5], &src_len, 0); |
| 339 | load_fixture(argv[5]); |
| 340 | run_src(); |
| 341 | printf("event exit=%d\n", last_status); |
| 342 | return 0; |
| 343 | } |