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 / test_osh_expand.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 | * test_osh_expand.c -- host driver for the osh expander unit (src/osh/osh_expand.osc), aien-architecture#158 cut 4c. |
| 19 | * |
| 20 | * Compiles the lexer, parser and expander units and runs the host sequence (lex_run until done, parse_run, then |
| 21 | * expand_run answering NEED_VAR from an environment table and feeding back pipeline statuses) in the reference |
| 22 | * interpreter AND as native AArch64, with exact-size heap buffers (under ASan every out-of-range byte is a report), |
| 23 | * against: |
| 24 | * - the independent C reference expander (osh_expand_ref.c), request record cell for cell, expanded bytes, the |
| 25 | * sequence of variable requests, refusal code and offset; |
| 26 | * - the host request decoder (osh_req.c): every record the unit returns must decode; |
| 27 | * - itself, with the input fed in random chunk splits, with repeated calls at 101 and 102, interp vs native workspace |
| 28 | * byte for byte; |
| 29 | * - the conformance vectors (tests/osh/vectors, expected.txt, lexer + parser + expander level); |
| 30 | * - a table of hostile cases with hand-written expected records (many cross-checked against GNU bash), caps, ABI |
| 31 | * header refusals, corrupted tables (no memory error, engines agree); |
| 32 | * - a fixed-seed fuzz of random command lines over a fixed environment. |
| 33 | * Usage: test_osh_expand LEX.osc PARSE.osc EXPAND.osc VECTORS_DIR [FUZZ_COUNT] |
| 34 | * Final line: OSH_EXPAND_PASS or OSH_EXPAND_FAIL. |
| 35 | */ |
| 36 | #include <stdarg.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | |
| 41 | #include "osh_core.h" |
| 42 | #include "osh_expand_ref.h" |
| 43 | #include "osh_host.h" |
| 44 | #include "osh_layout.h" |
| 45 | #include "osh_lex_ref.h" |
| 46 | #include "osh_parse_ref.h" |
| 47 | |
| 48 | #define WS ((size_t)OSH_WS_CELLS) |
| 49 | #define REC_MAX (8 + 8 * 164) |
| 50 | |
| 51 | static unsigned long n_checks, n_fail; |
| 52 | static unsigned long n_cases, n_ref_cmp, n_eng_cmp, n_chunk, n_idem, n_decode, n_req, n_pipes, n_resume102, n_hostile, n_vec, n_fuzz, n_corrupt, n_corrupt_trap; |
| 53 | static unsigned long n_code[256]; |
| 54 | static unsigned long n_final_ok, n_final_err; |
| 55 | |
| 56 | static void check(int cond, const char *fmt, ...) |
| 57 | { |
| 58 | n_checks++; |
| 59 | if (cond) return; |
| 60 | n_fail++; |
| 61 | if (n_fail <= 40) { |
| 62 | va_list ap; |
| 63 | va_start(ap, fmt); |
| 64 | fprintf(stderr, "FAIL: "); |
| 65 | vfprintf(stderr, fmt, ap); |
| 66 | fprintf(stderr, "\n"); |
| 67 | va_end(ap); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static OshCore CORE; |
| 72 | typedef enum { INTERP = 0, NATIVE = 1 } Engine; |
| 73 | |
| 74 | static char *slurp(const char *path, size_t *len) |
| 75 | { |
| 76 | FILE *f = fopen(path, "rb"); |
| 77 | if (!f) return NULL; |
| 78 | size_t cap = 4096, n = 0; |
| 79 | char *b = malloc(cap); |
| 80 | for (;;) { |
| 81 | size_t r = fread(b + n, 1, cap - n, f); |
| 82 | n += r; |
| 83 | if (n < cap) break; |
| 84 | cap *= 2; |
| 85 | b = realloc(b, cap); |
| 86 | } |
| 87 | fclose(f); |
| 88 | *len = n; |
| 89 | return b; |
| 90 | } |
| 91 | |
| 92 | static uint64_t rng_next(uint64_t *s) |
| 93 | { |
| 94 | uint64_t x = *s; |
| 95 | x ^= x >> 12; |
| 96 | x ^= x << 25; |
| 97 | x ^= x >> 27; |
| 98 | *s = x; |
| 99 | return x * 0x2545F4914F6CDD1DULL; |
| 100 | } |
| 101 | |
| 102 | /* one entry call on an exact-size copy of d[0..n): the status, or ~0 on a fault */ |
| 103 | static uint64_t ucall(int unit, Engine e, uint64_t *w, size_t wn, const uint8_t *d, size_t n) |
| 104 | { |
| 105 | uint8_t *b = NULL; |
| 106 | if (n) { |
| 107 | b = malloc(n); |
| 108 | if (!b) { fprintf(stderr, "oom\n"); exit(2); } |
| 109 | memcpy(b, d, n); |
| 110 | } |
| 111 | uint64_t r = osh_core_call(&CORE, unit, e == NATIVE, b, n, w, wn); |
| 112 | free(b); |
| 113 | return r; |
| 114 | } |
| 115 | |
| 116 | static uint64_t *sess_new(void) |
| 117 | { |
| 118 | uint64_t *w = calloc(WS, sizeof(uint64_t)); |
| 119 | if (!w) { fprintf(stderr, "oom\n"); exit(2); } |
| 120 | w[OSH_S_MAGIC] = OSH_ABI_MAGIC; |
| 121 | w[OSH_S_VERSION] = OSH_ABI_VERSION; |
| 122 | w[OSH_S_WS_CELLS] = OSH_WS_CELLS; |
| 123 | return w; |
| 124 | } |
| 125 | |
| 126 | /* ---- environment ---- */ |
| 127 | static void env_set_var(OshXrEnv *env, const char *name, const uint8_t *v, size_t n) |
| 128 | { |
| 129 | int i; |
| 130 | for (i = 0; i < env->nvars; i++) |
| 131 | if (strcmp(env->var[i].name, name) == 0) break; |
| 132 | if (i == env->nvars) { |
| 133 | if (i >= OSH_XR_MAXVARS) return; |
| 134 | env->nvars++; |
| 135 | snprintf(env->var[i].name, sizeof env->var[i].name, "%s", name); |
| 136 | } |
| 137 | env->var[i].v.set = 1; |
| 138 | env->var[i].v.len = n; |
| 139 | memcpy(env->var[i].v.b, v, n < OSH_XR_VALCAP ? n : OSH_XR_VALCAP); |
| 140 | } |
| 141 | |
| 142 | static void env_set_pos(OshXrEnv *env, int k, const uint8_t *v, size_t n) |
| 143 | { |
| 144 | OshXrStr *s = k == 0 ? &env->arg0 : &env->pos[k - 1]; |
| 145 | s->set = 1; |
| 146 | s->len = n; |
| 147 | memcpy(s->b, v, n < OSH_XR_VALCAP ? n : OSH_XR_VALCAP); |
| 148 | if (k > env->npos) env->npos = k; |
| 149 | } |
| 150 | |
| 151 | /* unescape \n \t \0 \\ for hostile-table environment text */ |
| 152 | static size_t unesc(const char *s, size_t n, uint8_t *out) |
| 153 | { |
| 154 | size_t o = 0; |
| 155 | for (size_t i = 0; i < n; i++) { |
| 156 | if (s[i] == '\\' && i + 1 < n) { |
| 157 | i++; |
| 158 | out[o++] = s[i] == 'n' ? '\n' : s[i] == 't' ? '\t' : s[i] == '0' ? 0 : (uint8_t)s[i]; |
| 159 | } else out[o++] = (uint8_t)s[i]; |
| 160 | } |
| 161 | return o; |
| 162 | } |
| 163 | |
| 164 | /* env text: lines NAME=VALUE, @status=N, @argK=VALUE (README of the vectors). esc: process \n \t \0 \\ in values. */ |
| 165 | static void env_parse(OshXrEnv *env, const char *text, size_t len, int esc) |
| 166 | { |
| 167 | size_t i = 0; |
| 168 | while (i < len) { |
| 169 | size_t e = i; |
| 170 | while (e < len && text[e] != '\n') e++; |
| 171 | const char *l = text + i; |
| 172 | size_t ll = e - i; |
| 173 | i = e + 1; |
| 174 | if (ll == 0) continue; |
| 175 | const char *eq = memchr(l, '=', ll); |
| 176 | if (!eq) continue; |
| 177 | size_t nl = (size_t)(eq - l), vl = ll - nl - 1; |
| 178 | uint8_t v[OSH_XR_VALCAP]; |
| 179 | size_t n2; |
| 180 | if (esc) n2 = unesc(eq + 1, vl, v); |
| 181 | else { memcpy(v, eq + 1, vl); n2 = vl; } |
| 182 | if (l[0] == '@') { |
| 183 | if (nl == 7 && memcmp(l, "@status", 7) == 0) env->status = (uint64_t)strtoull((const char *)v, NULL, 10); |
| 184 | else if (nl == 5 && memcmp(l, "@arg", 4) == 0) env_set_pos(env, l[4] - '0', v, n2); |
| 185 | } else { |
| 186 | char name[64]; |
| 187 | if (nl >= sizeof name) continue; |
| 188 | memcpy(name, l, nl); |
| 189 | name[nl] = 0; |
| 190 | env_set_var(env, name, v, n2); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* ---- pipeline statuses the "host" reports ---- */ |
| 196 | static uint64_t pstat(uint64_t seed, unsigned idx) |
| 197 | { |
| 198 | uint64_t s = seed + 0x9E3779B97F4A7C15ULL * (idx + 1); |
| 199 | s ^= s >> 30; s *= 0xBF58476D1CE4E5B9ULL; s ^= s >> 27; s *= 0x94D049BB133111EBULL; s ^= s >> 31; |
| 200 | return s % 3 == 0 ? 0 : s % 5 + 1; |
| 201 | } |
| 202 | |
| 203 | /* ---- the host run ---- */ |
| 204 | typedef struct { |
| 205 | unsigned idx; /* pipeline index in the list */ |
| 206 | uint64_t rec[REC_MAX]; |
| 207 | size_t nrec; |
| 208 | uint8_t out[OSH_XR_OUTCAP]; |
| 209 | size_t out_used; |
| 210 | unsigned nreq; |
| 211 | OshXrReq req[OSH_XR_MAXREQ]; |
| 212 | } HPipe; |
| 213 | |
| 214 | typedef struct { |
| 215 | uint64_t final; /* 104 complete, 100 more input, else refusal code */ |
| 216 | uint64_t err_off; |
| 217 | int from_lexer; |
| 218 | unsigned np; |
| 219 | HPipe p[16]; |
| 220 | unsigned lex_resumes; |
| 221 | unsigned budget_resumes; |
| 222 | } HRes; |
| 223 | |
| 224 | typedef struct { |
| 225 | int eoi, noskip; |
| 226 | uint64_t seed; /* pipeline status stream; 0 with fixed_status */ |
| 227 | int fixed_status; /* vectors: $? is env.status for every request, and no skipping */ |
| 228 | unsigned chunk_mode; /* 0 single shot, 1 one byte, 2 random 1..16, 3 random up to the rest */ |
| 229 | uint64_t *rng; |
| 230 | int probe; /* repeat calls at 101 / 102 and require identical outcome */ |
| 231 | } HOpt; |
| 232 | |
| 233 | static uint64_t run_entry(int unit, Engine e, uint64_t *w, const uint8_t *d, size_t n, unsigned *resumes) |
| 234 | { |
| 235 | for (int r = 0; r < 40; r++) { |
| 236 | uint64_t st = ucall(unit, e, w, WS, d, n); |
| 237 | if (st != 101) return st; |
| 238 | if (resumes) (*resumes)++; |
| 239 | } |
| 240 | check(0, "more than 40 budget resumes"); |
| 241 | return ~0ULL; |
| 242 | } |
| 243 | |
| 244 | static uint64_t lex_parse(Engine e, uint64_t *w, const uint8_t *d, size_t n, int *from_lexer, unsigned *lres) |
| 245 | { |
| 246 | uint64_t st = run_entry(OSH_U_LEX, e, w, d, n, lres); |
| 247 | *from_lexer = 1; |
| 248 | if (st != 0) return st; |
| 249 | *from_lexer = 0; |
| 250 | return run_entry(OSH_U_PARSE, e, w, d, n, NULL); |
| 251 | } |
| 252 | |
| 253 | static uint64_t lex_parse_chunked(Engine e, uint64_t *w, const uint8_t *d, size_t n, int eoi, unsigned mode, uint64_t *rng, int *from_lexer, unsigned *lres) |
| 254 | { |
| 255 | size_t cur = 0; |
| 256 | uint64_t st = 0; |
| 257 | *from_lexer = 1; |
| 258 | if (n == 0 || mode == 0) { |
| 259 | w[OSH_S_EOI] = (uint64_t)eoi; |
| 260 | return lex_parse(e, w, d, n, from_lexer, lres); |
| 261 | } |
| 262 | while (cur < n) { |
| 263 | size_t ch = 1; |
| 264 | if (mode == 2) ch = 1 + rng_next(rng) % 16; |
| 265 | else if (mode == 3) ch = 1 + rng_next(rng) % (n - cur); |
| 266 | if (ch > n - cur) ch = n - cur; |
| 267 | cur += ch; |
| 268 | st = lex_parse(e, w, d, cur, from_lexer, lres); |
| 269 | if (st >= 200) return st; |
| 270 | if (st == 0 && !*from_lexer) return st; |
| 271 | } |
| 272 | if (eoi) { |
| 273 | w[OSH_S_EOI] = 1; |
| 274 | st = lex_parse(e, w, d, n, from_lexer, lres); |
| 275 | } |
| 276 | return st; |
| 277 | } |
| 278 | |
| 279 | static void answer_var(uint64_t *w, const uint8_t *d, size_t n, const OshXrEnv *env, OshXrReq *rq) |
| 280 | { |
| 281 | uint64_t kind = w[OSH_S_VR_KIND], a = w[OSH_S_VR_A], len = w[OSH_S_VR_LEN]; |
| 282 | rq->kind = (int)kind; |
| 283 | rq->a = a; |
| 284 | rq->len = len; |
| 285 | int found; |
| 286 | size_t vl; |
| 287 | uint64_t np; |
| 288 | uint8_t val[OSH_XR_VALCAP]; |
| 289 | const uint8_t *name = NULL; |
| 290 | size_t nl = 0; |
| 291 | if (kind == 1) { |
| 292 | if (a > n || len > n - a) { check(0, "NAME request outside the input"); return; } |
| 293 | name = d + a; |
| 294 | nl = len; |
| 295 | } |
| 296 | osh_xr_env_get(env, (int)kind, name, nl, a, &found, &vl, &np, val); |
| 297 | w[OSH_STAGING + 0] = (uint64_t)found; |
| 298 | w[OSH_STAGING + 1] = vl; |
| 299 | w[OSH_STAGING + 2] = np; |
| 300 | w[OSH_STAGING + 3] = 0; |
| 301 | for (size_t i = 0; i < vl && i < 1024; i++) w[OSH_STAGING + 4 + i] = val[i]; |
| 302 | } |
| 303 | |
| 304 | static void host_run(Engine e, const uint8_t *d, size_t n, const OshXrEnv *env0, const HOpt *o, HRes *r, uint64_t **wout) |
| 305 | { |
| 306 | uint64_t *w = sess_new(); |
| 307 | OshXrEnv *env = malloc(sizeof *env); |
| 308 | memcpy(env, env0, sizeof *env); |
| 309 | memset(r, 0, sizeof *r); |
| 310 | uint64_t st = lex_parse_chunked(e, w, d, n, o->eoi, o->chunk_mode, o->rng, &r->from_lexer, &r->lex_resumes); |
| 311 | if (st != 0 || r->from_lexer) { |
| 312 | r->final = st; |
| 313 | r->err_off = st >= 200 ? w[OSH_S_ERR_OFF] : 0; |
| 314 | goto done; |
| 315 | } |
| 316 | if (w[OSH_PX_STATE] != 4) { /* parser consumed every token of a list that has no end yet: blank so far; the host reads more or, at end of input, has an empty list */ |
| 317 | r->final = 104; |
| 318 | goto done; |
| 319 | } |
| 320 | w[OSH_S_NOSKIP] = (uint64_t)o->noskip; |
| 321 | w[OSH_S_LAST_STATUS] = env->status; |
| 322 | unsigned cur_pipe = 0; |
| 323 | for (int guard = 0; guard < 4000; guard++) { |
| 324 | st = ucall(OSH_U_EXPAND, e, w, WS, d, n); |
| 325 | if (st == 101) { |
| 326 | r->budget_resumes++; |
| 327 | continue; |
| 328 | } |
| 329 | if (st == 102) { |
| 330 | n_resume102++; |
| 331 | if (r->np >= 16) { check(0, "more than 16 pipelines"); break; } |
| 332 | OshXrReq rq; |
| 333 | if (o->probe) { |
| 334 | uint64_t *snap = malloc(WS * 8); |
| 335 | memcpy(snap, w, WS * 8); |
| 336 | uint64_t again = ucall(OSH_U_EXPAND, e, w, WS, d, n); |
| 337 | check(again == 102 && memcmp(snap, w, WS * 8) == 0, "repeated call at NEED_VAR before the answer changed the outcome (%llu)", (unsigned long long)again); |
| 338 | n_idem++; |
| 339 | free(snap); |
| 340 | } |
| 341 | answer_var(w, d, n, env, &rq); |
| 342 | HPipe *hp = &r->p[r->np]; |
| 343 | if (hp->nreq < OSH_XR_MAXREQ) hp->req[hp->nreq++] = rq; |
| 344 | n_req++; |
| 345 | continue; |
| 346 | } |
| 347 | if (st == 103) { |
| 348 | HPipe *hp = &r->p[r->np]; |
| 349 | uint64_t nc = w[OSH_REQUEST + 1]; |
| 350 | hp->nrec = (size_t)(8 + nc * 164); |
| 351 | if (nc < 1 || nc > 8) { check(0, "ncmds %llu", (unsigned long long)nc); break; } |
| 352 | memcpy(hp->rec, w + OSH_REQUEST, hp->nrec * 8); |
| 353 | hp->out_used = (size_t)w[OSH_REQUEST + 4]; |
| 354 | if (hp->out_used > OSH_XR_OUTCAP) { check(0, "out_used %zu", hp->out_used); break; } |
| 355 | for (size_t i = 0; i < hp->out_used; i++) hp->out[i] = (uint8_t)w[OSH_OUT + i]; |
| 356 | hp->idx = (unsigned)w[OSH_EX_PIPE]; |
| 357 | /* the host decoder must accept the record */ |
| 358 | OshRequest *rq = malloc(sizeof *rq); |
| 359 | int rc = osh_req_decode(w + OSH_REQUEST, hp->nrec, w + OSH_OUT, hp->out_used, rq); |
| 360 | n_decode++; |
| 361 | check(rc == 0, "record refused by the host decoder (%d)", rc); |
| 362 | free(rq); |
| 363 | uint64_t s = o->fixed_status ? env->status : pstat(o->seed, cur_pipe); |
| 364 | cur_pipe++; |
| 365 | env->status = s; |
| 366 | w[OSH_S_LAST_STATUS] = s; |
| 367 | r->np++; |
| 368 | n_pipes++; |
| 369 | continue; |
| 370 | } |
| 371 | r->final = st; |
| 372 | r->err_off = st >= 200 ? w[OSH_S_ERR_OFF] : 0; |
| 373 | if (st >= 200 && (st < 213 || st > 217)) check(w[OSH_S_PHASE] == 255 && w[OSH_S_ERR_CODE] == st, "refusal not recorded terminal"); |
| 374 | goto done; |
| 375 | } |
| 376 | check(0, "host loop did not settle"); |
| 377 | done: |
| 378 | free(env); |
| 379 | if (wout) *wout = w; |
| 380 | else free(w); |
| 381 | } |
| 382 | |
| 383 | /* ---- the reference run ---- */ |
| 384 | typedef struct { |
| 385 | uint64_t final; |
| 386 | uint64_t err_off; |
| 387 | unsigned np; |
| 388 | OshXrOut *o[16]; |
| 389 | } RRes; |
| 390 | |
| 391 | static void ref_run(const uint8_t *d, size_t n, const OshXrEnv *env0, const HOpt *o, RRes *rr) |
| 392 | { |
| 393 | memset(rr, 0, sizeof *rr); |
| 394 | OshRefLex *lx = malloc(sizeof *lx); |
| 395 | OshRefParse *pr = calloc(1, sizeof *pr); |
| 396 | osh_lex_ref(d, n, o->eoi, lx); |
| 397 | if (lx->status != 0) { rr->final = lx->status; rr->err_off = lx->status >= 200 ? lx->err_off : 0; goto out; } |
| 398 | osh_parse_ref(d, n, lx, o->eoi, pr); |
| 399 | if (pr->status != 0) { rr->final = pr->status; rr->err_off = pr->status >= 200 ? pr->err_off : 0; goto out; } |
| 400 | OshXrEnv *env = malloc(sizeof *env); |
| 401 | memcpy(env, env0, sizeof *env); |
| 402 | unsigned cand = 0, k = 0; |
| 403 | uint64_t last = env->status; |
| 404 | for (;;) { |
| 405 | unsigned p = osh_xr_next(pr, cand, last, o->noskip || o->fixed_status); |
| 406 | if (p >= pr->npipe) { rr->final = 104; break; } |
| 407 | OshXrOut *x = malloc(sizeof *x); |
| 408 | osh_xr_pipe(d, n, lx, pr, env, p, x); |
| 409 | rr->o[rr->np] = x; |
| 410 | rr->np++; |
| 411 | if (x->status != 103) { rr->final = x->status; rr->err_off = x->err_off; break; } |
| 412 | last = o->fixed_status ? env->status : pstat(o->seed, k); |
| 413 | k++; |
| 414 | env->status = last; |
| 415 | cand = p + 1; |
| 416 | } |
| 417 | free(env); |
| 418 | out: |
| 419 | free(lx); |
| 420 | free(pr); |
| 421 | } |
| 422 | |
| 423 | static void rr_free(RRes *rr) |
| 424 | { |
| 425 | for (unsigned i = 0; i < rr->np; i++) free(rr->o[i]); |
| 426 | } |
| 427 | |
| 428 | /* ---- comparisons ---- */ |
| 429 | static void dump_input(const uint8_t *d, size_t n) |
| 430 | { |
| 431 | fprintf(stderr, " input (%zu): ", n); |
| 432 | for (size_t q = 0; q < n && q < 160; q++) { |
| 433 | if (d[q] >= 32 && d[q] < 127) fputc(d[q], stderr); |
| 434 | else fprintf(stderr, "\\x%02x", d[q]); |
| 435 | } |
| 436 | fprintf(stderr, "\n"); |
| 437 | } |
| 438 | |
| 439 | static int cmp_ref(const char *tag, const uint8_t *d, size_t n, const HRes *h, const RRes *rr) |
| 440 | { |
| 441 | n_ref_cmp++; |
| 442 | int ok = 1; |
| 443 | if (h->final != rr->final) ok = 0; |
| 444 | if (rr->final >= 200 && h->err_off != rr->err_off) ok = 0; |
| 445 | /* pipelines the reference expanded before a refusal are the ones the host saw (the refusing one is last in ref) */ |
| 446 | unsigned want = (rr->final >= 200 && rr->np) ? rr->np - 1 : rr->np; |
| 447 | if (rr->final < 200 || h->final >= 200) { |
| 448 | if (h->np != want && h->final == rr->final) ok = 0; |
| 449 | } |
| 450 | for (unsigned i = 0; i < h->np && i < rr->np && ok; i++) { |
| 451 | const OshXrOut *x = rr->o[i]; |
| 452 | const HPipe *p = &h->p[i]; |
| 453 | if (x->status != 103) break; |
| 454 | if (p->nrec != x->nrec || memcmp(p->rec, x->rec, x->nrec * 8) != 0) ok = 0; |
| 455 | if (p->out_used != x->out_used || memcmp(p->out, x->out, x->out_used) != 0) ok = 0; |
| 456 | if (p->nreq != x->nreq) ok = 0; |
| 457 | else |
| 458 | for (unsigned q = 0; q < x->nreq; q++) |
| 459 | if (p->req[q].kind != x->req[q].kind || p->req[q].a != x->req[q].a || p->req[q].len != x->req[q].len) ok = 0; |
| 460 | } |
| 461 | if (!ok) { |
| 462 | fprintf(stderr, " ref mismatch [%s]: osc final %llu@%llu np %u | ref final %llu@%llu np %u\n", tag, (unsigned long long)h->final, |
| 463 | (unsigned long long)h->err_off, h->np, (unsigned long long)rr->final, (unsigned long long)rr->err_off, rr->np); |
| 464 | dump_input(d, n); |
| 465 | for (unsigned i = 0; i < h->np && i < rr->np; i++) { |
| 466 | const OshXrOut *x = rr->o[i]; |
| 467 | const HPipe *p = &h->p[i]; |
| 468 | if (x->status != 103) break; |
| 469 | for (size_t c = 0; c < x->nrec; c++) |
| 470 | if (p->rec[c] != x->rec[c]) { |
| 471 | fprintf(stderr, " pipe %u rec cell %zu: osc %llu ref %llu\n", i, c, (unsigned long long)p->rec[c], (unsigned long long)x->rec[c]); |
| 472 | break; |
| 473 | } |
| 474 | if (p->out_used != x->out_used) fprintf(stderr, " pipe %u out_used osc %zu ref %zu\n", i, p->out_used, x->out_used); |
| 475 | else |
| 476 | for (size_t c = 0; c < x->out_used; c++) |
| 477 | if (p->out[c] != x->out[c]) { fprintf(stderr, " pipe %u out byte %zu: osc %u ref %u\n", i, c, p->out[c], x->out[c]); break; } |
| 478 | if (p->nreq != x->nreq) fprintf(stderr, " pipe %u nreq osc %u ref %u\n", i, p->nreq, x->nreq); |
| 479 | } |
| 480 | } |
| 481 | check(ok, "reference mismatch [%s]", tag); |
| 482 | return ok; |
| 483 | } |
| 484 | |
| 485 | static int same_hres(const HRes *a, const HRes *b) |
| 486 | { |
| 487 | if (a->final != b->final || a->err_off != b->err_off || a->np != b->np || a->from_lexer != b->from_lexer) return 0; |
| 488 | for (unsigned i = 0; i < a->np; i++) { |
| 489 | if (a->p[i].nrec != b->p[i].nrec || memcmp(a->p[i].rec, b->p[i].rec, a->p[i].nrec * 8)) return 0; |
| 490 | if (a->p[i].out_used != b->p[i].out_used || memcmp(a->p[i].out, b->p[i].out, a->p[i].out_used)) return 0; |
| 491 | if (a->p[i].nreq != b->p[i].nreq) return 0; |
| 492 | for (unsigned q = 0; q < a->p[i].nreq; q++) |
| 493 | if (a->p[i].req[q].kind != b->p[i].req[q].kind || a->p[i].req[q].a != b->p[i].req[q].a || a->p[i].req[q].len != b->p[i].req[q].len) return 0; |
| 494 | } |
| 495 | return 1; |
| 496 | } |
| 497 | |
| 498 | /* One input through everything: both engines (single shot, probes), the reference, and a chunked run. */ |
| 499 | static void run_case(const char *tag, const uint8_t *d, size_t n, const OshXrEnv *env, int eoi, int noskip, uint64_t seed, uint64_t *rng, unsigned long iter, HRes *keep) |
| 500 | { |
| 501 | HOpt o; |
| 502 | memset(&o, 0, sizeof o); |
| 503 | o.eoi = eoi; |
| 504 | o.noskip = noskip; |
| 505 | o.seed = seed; |
| 506 | o.rng = rng; |
| 507 | o.probe = 1; |
| 508 | HRes *a = malloc(sizeof *a), *b = malloc(sizeof *b), *c = malloc(sizeof *c); |
| 509 | uint64_t *wa, *wb; |
| 510 | host_run(INTERP, d, n, env, &o, a, &wa); |
| 511 | host_run(NATIVE, d, n, env, &o, b, &wb); |
| 512 | n_cases++; |
| 513 | n_eng_cmp++; |
| 514 | int same = memcmp(wa, wb, WS * 8) == 0 && same_hres(a, b); |
| 515 | check(same, "[%s] eoi=%d interp and native disagree (final %llu vs %llu)", tag, eoi, (unsigned long long)a->final, (unsigned long long)b->final); |
| 516 | RRes rr; |
| 517 | ref_run(d, n, env, &o, &rr); |
| 518 | cmp_ref(tag, d, n, a, &rr); |
| 519 | n_code[a->final & 255]++; |
| 520 | if (a->final == 104) n_final_ok++; |
| 521 | else if (a->final >= 200) n_final_err++; |
| 522 | /* chunked lexing and parsing, either engine, same outcome */ |
| 523 | OshRefLex *lxr = malloc(sizeof *lxr); |
| 524 | osh_lex_ref(d, n, eoi, lxr); |
| 525 | int clean = lxr->status == 0; |
| 526 | free(lxr); |
| 527 | if (clean) { |
| 528 | HOpt oc = o; |
| 529 | oc.chunk_mode = 1 + (unsigned)(rng_next(rng) % 3); |
| 530 | if (oc.chunk_mode == 1 && n > 48) oc.chunk_mode = 2; |
| 531 | oc.probe = 0; |
| 532 | uint64_t *wc; |
| 533 | host_run((iter & 1) ? INTERP : NATIVE, d, n, env, &oc, c, &wc); |
| 534 | n_chunk++; |
| 535 | check(same_hres(a, c), "[%s] eoi=%d chunked (mode %u) differs from single shot (final %llu vs %llu)", tag, eoi, oc.chunk_mode, (unsigned long long)c->final, |
| 536 | (unsigned long long)a->final); |
| 537 | free(wc); |
| 538 | } |
| 539 | if (keep) memcpy(keep, a, sizeof *keep); |
| 540 | rr_free(&rr); |
| 541 | free(wa); |
| 542 | free(wb); |
| 543 | free(a); |
| 544 | free(b); |
| 545 | free(c); |
| 546 | } |
| 547 | |
| 548 | /* ---- the vector grammar ---- */ |
| 549 | static void fq(char *out, size_t *o, const uint8_t *s, size_t n) |
| 550 | { |
| 551 | int rep = n >= 64; |
| 552 | for (size_t i = 1; i < n && rep; i++) |
| 553 | if (s[i] != s[0]) rep = 0; |
| 554 | if (rep) { |
| 555 | *o += (size_t)sprintf(out + *o, "\"%c\"*%zu", s[0], n); |
| 556 | return; |
| 557 | } |
| 558 | out[(*o)++] = '"'; |
| 559 | for (size_t i = 0; i < n; i++) { |
| 560 | uint8_t c = s[i]; |
| 561 | if (c == '"' || c == '\\') { out[(*o)++] = '\\'; out[(*o)++] = (char)c; } |
| 562 | else if (c < 0x20 || c > 0x7e) *o += (size_t)sprintf(out + *o, "\\x%02x", c); |
| 563 | else out[(*o)++] = (char)c; |
| 564 | } |
| 565 | out[(*o)++] = '"'; |
| 566 | } |
| 567 | |
| 568 | static void fmt_result(const HRes *h, int eoi, char *out) |
| 569 | { |
| 570 | size_t o = 0; |
| 571 | (void)eoi; |
| 572 | if (h->final == 100) { strcpy(out, "more"); return; } |
| 573 | if (h->final >= 200) { |
| 574 | const char *nm = osh_code_name((unsigned)h->final); |
| 575 | sprintf(out, "refuse %llu %s off=%llu", (unsigned long long)h->final, nm ? nm : "?", (unsigned long long)h->err_off); |
| 576 | return; |
| 577 | } |
| 578 | o += (size_t)sprintf(out, "ok"); |
| 579 | if (h->lex_resumes) o += (size_t)sprintf(out + o, " lex_resumes=%u", h->lex_resumes); |
| 580 | for (unsigned i = 0; i < h->np; i++) { |
| 581 | const HPipe *p = &h->p[i]; |
| 582 | o += (size_t)sprintf(out + o, " | P%u conn=%llu", p->idx, (unsigned long long)p->rec[2]); |
| 583 | uint64_t nc = p->rec[1]; |
| 584 | for (uint64_t c = 0; c < nc; c++) { |
| 585 | const uint64_t *b = p->rec + 8 + c * 164; |
| 586 | o += (size_t)sprintf(out + o, " | C%llu b=%llu argv=", (unsigned long long)c, (unsigned long long)b[3]); |
| 587 | for (uint64_t k = 0; k < b[0]; k++) { |
| 588 | if (k) out[o++] = ','; |
| 589 | fq(out, &o, p->out + b[4 + 2 * k], (size_t)b[5 + 2 * k]); |
| 590 | } |
| 591 | o += (size_t)sprintf(out + o, " as="); |
| 592 | for (uint64_t k = 0; k < b[1]; k++) { |
| 593 | const uint64_t *e = b + 68 + 4 * k; |
| 594 | if (k) out[o++] = ','; |
| 595 | memcpy(out + o, p->out + e[0], (size_t)e[1]); |
| 596 | o += (size_t)e[1]; |
| 597 | out[o++] = '='; |
| 598 | fq(out, &o, p->out + e[2], (size_t)e[3]); |
| 599 | } |
| 600 | o += (size_t)sprintf(out + o, " rd="); |
| 601 | for (uint64_t k = 0; k < b[2]; k++) { |
| 602 | const uint64_t *e = b + 132 + 4 * k; |
| 603 | static const char *const kn[] = {"?", "IN", "OUT", "APPEND", "DUP"}; |
| 604 | if (k) out[o++] = ','; |
| 605 | o += (size_t)sprintf(out + o, "%s%llu:", kn[e[0] < 5 ? e[0] : 0], (unsigned long long)e[1]); |
| 606 | if (e[0] == 4) o += (size_t)sprintf(out + o, "%llu", (unsigned long long)e[2]); |
| 607 | else fq(out, &o, p->out + e[2], (size_t)e[3]); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | out[o] = 0; |
| 612 | } |
| 613 | |
| 614 | /* ---- conformance vectors ---- */ |
| 615 | static int run_vectors(const char *dir) |
| 616 | { |
| 617 | char path[512]; |
| 618 | snprintf(path, sizeof path, "%s/expected.txt", dir); |
| 619 | size_t el; |
| 620 | char *exp = slurp(path, &el); |
| 621 | if (!exp) { fprintf(stderr, "cannot read %s\n", path); return 2; } |
| 622 | size_t i = 0; |
| 623 | uint64_t rng = 0x1234567ULL; |
| 624 | while (i < el) { |
| 625 | size_t e = i; |
| 626 | while (e < el && exp[e] != '\n') e++; |
| 627 | char line[8192]; |
| 628 | size_t ll = e - i < sizeof line - 1 ? e - i : sizeof line - 1; |
| 629 | memcpy(line, exp + i, ll); |
| 630 | line[ll] = 0; |
| 631 | i = e + 1; |
| 632 | if (ll == 0 || line[0] == '#') continue; |
| 633 | char vn[16]; |
| 634 | const char *sp = strchr(line, ' '); |
| 635 | if (!sp || (size_t)(sp - line) >= sizeof vn) continue; |
| 636 | memcpy(vn, line, (size_t)(sp - line)); |
| 637 | vn[sp - line] = 0; |
| 638 | char fn[600]; |
| 639 | snprintf(fn, sizeof fn, "%s/%s.in", dir, vn); |
| 640 | size_t dl; |
| 641 | char *in = slurp(fn, &dl); |
| 642 | if (!in) { check(0, "missing %s", fn); continue; } |
| 643 | OshXrEnv *env = calloc(1, sizeof *env); |
| 644 | snprintf(fn, sizeof fn, "%s/%s.env", dir, vn); |
| 645 | size_t vl; |
| 646 | char *ev = slurp(fn, &vl); |
| 647 | if (ev) { env_parse(env, ev, vl, 0); free(ev); } |
| 648 | HOpt o; |
| 649 | memset(&o, 0, sizeof o); |
| 650 | o.fixed_status = 1; |
| 651 | o.noskip = 1; |
| 652 | o.rng = &rng; |
| 653 | o.probe = 1; |
| 654 | for (Engine en = INTERP; en <= NATIVE; en++) { |
| 655 | HRes *h = malloc(sizeof *h); |
| 656 | host_run(en, (const uint8_t *)in, dl, env, &o, h, NULL); |
| 657 | char got[16384]; |
| 658 | fmt_result(h, 0, got); |
| 659 | n_vec++; |
| 660 | int ok = strcmp(got, line + (sp - line) + 1) == 0; |
| 661 | if (!ok) { |
| 662 | fprintf(stderr, " vector %s %s:\n want: %s\n got: %s\n", vn, en == INTERP ? "interp" : "native", sp + 1, got); |
| 663 | } |
| 664 | check(ok, "vector %s %s differs from expected.txt", vn, en == INTERP ? "interp" : "native"); |
| 665 | free(h); |
| 666 | } |
| 667 | /* and the full differential on the vector input */ |
| 668 | run_case(vn, (const uint8_t *)in, dl, env, 0, 1, 0, &rng, n_vec, NULL); |
| 669 | free(env); |
| 670 | free(in); |
| 671 | } |
| 672 | free(exp); |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /* ---- hostile table: input, environment, expected line in the vector grammar (pipelines run with status 0, no skipping) ---- */ |
| 677 | typedef struct { |
| 678 | const char *name, *in, *env, *want; |
| 679 | } Case; |
| 680 | |
| 681 | #define Q "\"" |
| 682 | static const Case CASES[] = { |
| 683 | {"plain", "echo a b\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a\",\"b\" as= rd="}, |
| 684 | {"single quotes keep everything", "echo 'a $X \\ b'\n", "X=v", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a $X \\\\ b\" as= rd="}, |
| 685 | {"double quotes expand", "echo \"a $X b\"\n", "X=v", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a v b\" as= rd="}, |
| 686 | {"unquoted split", "echo $S\n", "S=a b c", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a\",\"b\",\"c\" as= rd="}, |
| 687 | {"quoted no split", "echo \"$S\"\n", "S=a b c", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a b c\" as= rd="}, |
| 688 | {"empty unquoted dropped", "echo $E x\n", "E=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"x\" as= rd="}, |
| 689 | {"unset unquoted dropped", "echo $U x\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"x\" as= rd="}, |
| 690 | {"empty quoted kept", "echo \"$E\" '' \"\" x\n", "E=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"\",\"\",\"\",\"x\" as= rd="}, |
| 691 | {"quoted unset kept", "echo \"$U\"\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"\" as= rd="}, |
| 692 | {"empty beside literal", "echo a${E}b\n", "E=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"ab\" as= rd="}, |
| 693 | {"IFS value beside literal", "echo a${S}b\n", "S= x ", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a\",\"x\",\"b\" as= rd="}, |
| 694 | {"leading IFS joins nothing", "echo a$L\n", "L= b", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a\",\"b\" as= rd="}, |
| 695 | {"trailing IFS then literal", "echo ${T}z\n", "T=b ", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"b\",\"z\" as= rd="}, |
| 696 | {"all IFS value drops", "echo x $S y\n", "S= ", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"x\",\"y\" as= rd="}, |
| 697 | {"all IFS beside literal", "echo x$S\n", "S= ", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"x\" as= rd="}, |
| 698 | {"tab and newline split", "echo $W\n", "W=w\\tx\\ny", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"w\",\"x\",\"y\" as= rd="}, |
| 699 | {"two expansions join", "echo $A$B\n", "A=1\nB=2", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"12\" as= rd="}, |
| 700 | {"expansion ends at non-name", "echo $A-$B.x\n", "A=1\nB=2", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"1-2.x\" as= rd="}, |
| 701 | {"braces", "echo ${A}b ${A}_ $A_\n", "A=1\nA_=u", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"1b\",\"1_\",\"u\" as= rd="}, |
| 702 | {"positional", "echo $0 $1 $2 $3 ${1}x $10\n", "@arg0=osh\n@arg1=a\n@arg2=b c", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"osh\",\"a\",\"b\",\"c\",\"ax\",\"a0\" as= rd="}, |
| 703 | {"count and status", "echo $# $?\n", "@status=7\n@arg1=a\n@arg2=b", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"2\",\"7\" as= rd="}, |
| 704 | {"quoted at", "echo \"$@\"\n", "@arg1=a b\n@arg2=\n@arg3=c", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a b\",\"\",\"c\" as= rd="}, |
| 705 | {"quoted at, none", "echo x \"$@\" y\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"x\",\"y\" as= rd="}, |
| 706 | {"quoted at with text", "echo \"a$@b\"\n", "@arg1=1\n@arg2=2", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a1\",\"2b\" as= rd="}, |
| 707 | {"quoted at with text, none", "echo \"a$@b\"\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"ab\" as= rd="}, |
| 708 | {"quoted at then empty", "echo \"$@\"\"\"\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"\" as= rd="}, |
| 709 | {"quoted at then var, none (bash: no field)", "echo \"$@$E\"\n", "E=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\" as= rd="}, |
| 710 | {"quoted at single", "echo \"$@\"\n", "@arg1=only", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"only\" as= rd="}, |
| 711 | {"quoted at empties", "echo \"$@\"\n", "@arg1=\n@arg2=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"\",\"\" as= rd="}, |
| 712 | {"quoted var then at, none (bash: no field)", "echo \"$E$@\"\n", "E=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\" as= rd="}, |
| 713 | {"quoted positional then at, none", "echo \"$1$@\" x\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"x\" as= rd="}, |
| 714 | {"quoted var alone, empty, is one field", "echo \"$E\"\n", "E=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"\" as= rd="}, |
| 715 | {"quoted star then var, none, is one field", "echo \"$*$E\"\n", "E=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"\" as= rd="}, |
| 716 | {"quoted var with value then at, none", "echo \"$X$@\"\n", "X=v", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"v\" as= rd="}, |
| 717 | {"unquoted at splits", "echo $@\n", "@arg1=a b\n@arg2=\n@arg3=c", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a\",\"b\",\"c\" as= rd="}, |
| 718 | {"unquoted star splits", "echo $*\n", "@arg1=a b\n@arg2=c", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a\",\"b\",\"c\" as= rd="}, |
| 719 | {"quoted star joins", "echo \"$*\"\n", "@arg1=a b\n@arg2=\n@arg3=c", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a b c\" as= rd="}, |
| 720 | {"quoted star none", "echo \"$*\" x\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"\",\"x\" as= rd="}, |
| 721 | {"unquoted at none", "echo $@ x\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"x\" as= rd="}, |
| 722 | {"unquoted at joins text", "echo x$@y\n", "@arg1=a\n@arg2=b", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"xa\",\"by\" as= rd="}, |
| 723 | {"unquoted at, empty positionals", "echo x$@y\n", "@arg1=\n@arg2=", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"x\",\"y\" as= rd="}, |
| 724 | {"backslash unquoted", "echo a\\ b \\$X \\\\ \\\"\n", "X=v", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a b\",\"$X\",\"\\\\\",\"\\\"\" as= rd="}, |
| 725 | {"backslash in double quotes", "echo \"\\$X \\\" \\\\ \\a \\`\"\n", "X=v", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"$X \\\" \\\\ \\\\a `\" as= rd="}, |
| 726 | {"continuation", "ec\\\nho a\\\nb\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"ab\" as= rd="}, |
| 727 | {"continuation in double quotes", "echo \"a\\\nb\"\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"ab\" as= rd="}, |
| 728 | {"adjacent quotes join", "echo 'a'\"b\"c$X\n", "X=v", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"abcv\" as= rd="}, |
| 729 | {"literal dollar", "echo $ \"$\" a$ $/ \"$.\"\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"$\",\"$\",\"a$\",\"$/\",\"$.\" as= rd="}, |
| 730 | {"quoted glob chars are literal", "echo '*' \"?\" \"[x\"\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"*\",\"?\",\"[x\" as= rd="}, |
| 731 | {"glob in quoted value fine", "echo \"$G\"\n", "G=a*b", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a*b\" as= rd="}, |
| 732 | {"glob in unquoted value refused", "echo $G\n", "G=a*b", "refuse 245 VALUE_GLOB off=5"}, |
| 733 | {"question in unquoted value refused", "echo x $G\n", "G=a?b", "refuse 245 VALUE_GLOB off=7"}, |
| 734 | {"bracket in unquoted value refused", "echo $G\n", "G=a[b", "refuse 245 VALUE_GLOB off=5"}, |
| 735 | {"glob in positional unquoted at refused", "echo $@\n", "@arg1=*", "refuse 245 VALUE_GLOB off=5"}, |
| 736 | {"glob refused in redirect target", "echo >$G\n", "G=a*", "refuse 245 VALUE_GLOB off=6"}, |
| 737 | {"glob fine in assignment value", "A=$G\n", "G=a*", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"a*\" rd="}, |
| 738 | {"NUL in value refused", "echo \"$N\"\n", "N=a\\0b", "refuse 244 VALUE_NUL off=5"}, |
| 739 | {"NUL in unquoted value refused", "echo $N\n", "N=a\\0b", "refuse 244 VALUE_NUL off=5"}, |
| 740 | {"assignment only", "A=1 B=\"x y\" C=$S\n", "S=p q", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"1\",B=\"x y\",C=\"p q\" rd="}, |
| 741 | {"assignment empty value", "A= B=''\n", "", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"\",B=\"\" rd="}, |
| 742 | {"assignment not split", "A=$S\n", "S=a b", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"a b\" rd="}, |
| 743 | {"assignment at joins", "A=$@\n", "@arg1=a b\n@arg2=c", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"a b c\" rd="}, |
| 744 | {"assignment quoted at joins", "A=\"$@\"\n", "@arg1=a\n@arg2=c", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"a c\" rd="}, |
| 745 | {"assignment star none", "A=$*\n", "", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"\" rd="}, |
| 746 | {"prefix assignment sees old value", "A=1 B=$A cmd\n", "A=old", "ok | P0 conn=1 | C0 b=0 argv=\"cmd\" as=A=\"1\",B=\"old\" rd="}, |
| 747 | {"standalone assignments are sequential", "A=1 B=$A\n", "A=old", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"1\",B=\"1\" rd="}, |
| 748 | {"standalone sequential, latest wins", "A=1 A=$A$A B=${A}x\n", "", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"1\",A=\"11\",B=\"11x\" rd="}, |
| 749 | {"standalone sequential, other name from host", "A=1 B=$C\n", "C=h", "ok | P0 conn=1 | C0 b=0 argv= as=A=\"1\",B=\"h\" rd="}, |
| 750 | {"assignment after a word is a word", "echo A=$S\n", "S=a b", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"A=a\",\"b\" as= rd="}, |
| 751 | {"assignment after an empty expansion is a word", "$E A=1\n", "E=", "ok | P0 conn=1 | C0 b=0 argv=\"A=1\" as= rd="}, |
| 752 | {"assignment words, redirection between", "A=1 >f B=2 cmd\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"cmd\" as=A=\"1\",B=\"2\" rd=OUT1:\"f\""}, |
| 753 | {"redirections", "cat <in >out >>app 2>err\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"cat\" as= rd=IN0:\"in\",OUT1:\"out\",APPEND1:\"app\",OUT2:\"err\""}, |
| 754 | {"dup order is kept", "ls 2>&1 >o\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"ls\" as= rd=DUP2:1,OUT1:\"o\""}, |
| 755 | {"dup order is kept 2", "ls >o 2>&1\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"ls\" as= rd=OUT1:\"o\",DUP2:1"}, |
| 756 | {"dup in", "cat <&0 >&2 1>&2\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"cat\" as= rd=DUP0:0,DUP1:2,DUP1:2"}, |
| 757 | {"redirect target expands", "cat <$X >\"$X\"\n", "X=f.txt", "ok | P0 conn=1 | C0 b=0 argv=\"cat\" as= rd=IN0:\"f.txt\",OUT1:\"f.txt\""}, |
| 758 | {"redirect target empty quoted", "cat > ''\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"cat\" as= rd=OUT1:\"\""}, |
| 759 | {"redirect target splits to two", "cat > $S\n", "S=a b", "refuse 243 AMBIGUOUS_REDIRECT off=6"}, |
| 760 | {"redirect target empty", "cat > $E\n", "E=", "refuse 243 AMBIGUOUS_REDIRECT off=6"}, |
| 761 | {"redirect target unset", "cat > $U\n", "", "refuse 243 AMBIGUOUS_REDIRECT off=6"}, |
| 762 | {"redirect target quoted at none", "cat > \"$@\"\n", "", "refuse 243 AMBIGUOUS_REDIRECT off=6"}, |
| 763 | {"redirect target quoted at one", "cat > \"$@\"\n", "@arg1=f", "ok | P0 conn=1 | C0 b=0 argv=\"cat\" as= rd=OUT1:\"f\""}, |
| 764 | {"redirect target one field from split", "cat > $S\n", "S= f ", "ok | P0 conn=1 | C0 b=0 argv=\"cat\" as= rd=OUT1:\"f\""}, |
| 765 | {"pipeline and connectors", "a | b | c; d && e || f\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"a\" as= rd= | C1 b=0 argv=\"b\" as= rd= | C2 b=0 argv=\"c\" as= rd= | P1 conn=2 | C0 b=0 argv=\"d\" as= rd= | P2 conn=3 | C0 b=0 argv=\"e\" as= rd= | P3 conn=1 | C0 b=0 argv=\"f\" as= rd="}, |
| 766 | {"builtins", "cd /tmp; pwd; printf x; export A=1; unset A; exit 3\n", "", "ok | P0 conn=1 | C0 b=1 argv=\"cd\",\"/tmp\" as= rd= | P1 conn=1 | C0 b=2 argv=\"pwd\" as= rd= | P2 conn=1 | C0 b=3 argv=\"printf\",\"x\" as= rd= | P3 conn=1 | C0 b=4 argv=\"export\",\"A=1\" as= rd= | P4 conn=1 | C0 b=5 argv=\"unset\",\"A\" as= rd= | P5 conn=1 | C0 b=6 argv=\"exit\",\"3\" as= rd="}, |
| 767 | {"builtin by variable", "$B x\n", "B=cd", "ok | P0 conn=1 | C0 b=1 argv=\"cd\",\"x\" as= rd="}, |
| 768 | {"builtin quoted", "\"cd\" 'pwd'\n", "", "ok | P0 conn=1 | C0 b=1 argv=\"cd\",\"pwd\" as= rd="}, |
| 769 | {"builtin name as argument is not a builtin", "echo cd exit\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"cd\",\"exit\" as= rd="}, |
| 770 | {"builtin after assignment", "A=1 cd x\n", "", "ok | P0 conn=1 | C0 b=1 argv=\"cd\",\"x\" as=A=\"1\" rd="}, |
| 771 | {"builtin in a pipeline", "cd x | cat\n", "", "ok | P0 conn=1 | C0 b=1 argv=\"cd\",\"x\" as= rd= | C1 b=0 argv=\"cat\" as= rd="}, |
| 772 | {"unsupported set", "set -e\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 773 | {"unsupported via variable", "echo ok; $B -e\n", "B=eval", "refuse 239 UNSUPPORTED_BUILTIN off=9"}, |
| 774 | {"unsupported quoted", "'shift'\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 775 | {"unsupported dot", ". ./x\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 776 | {"unsupported after empty expansion", "$E source x\n", "E=", "refuse 239 UNSUPPORTED_BUILTIN off=3"}, |
| 777 | {"unsupported in second command", "a | exec b\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=4"}, |
| 778 | {"each unsupported name", "trap\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 779 | {"read is unsupported", "read x\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 780 | {"readarray is unsupported (9-byte name, #344)", "readarray x\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 781 | {"readarray via variable", "$R x\n", "R=readarray", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 782 | {"continue return break local readonly alias wait", "wait\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 783 | {"export NAME=value is not split", "export A=$Y B\n", "Y=a b", "ok | P0 conn=1 | C0 b=4 argv=\"export\",\"A=a b\",\"B\" as= rd="}, |
| 784 | {"export value keeps glob bytes", "export A=$Y\n", "Y=*", "ok | P0 conn=1 | C0 b=4 argv=\"export\",\"A=*\" as= rd="}, |
| 785 | {"export value joins $@", "export A=$@ C=\"$@\"\n", "@arg1=p\n@arg2=q", "ok | P0 conn=1 | C0 b=4 argv=\"export\",\"A=p q\",\"C=p q\" as= rd="}, |
| 786 | {"quoted export word is not an assignment: $@ splits (bash)", "export \"B=$@\"\n", "@arg1=p\n@arg2=q", "ok | P0 conn=1 | C0 b=4 argv=\"export\",\"B=p\",\"q\" as= rd="}, |
| 787 | {"export through a variable splits (bash)", "$E A=$Y\n", "E=export\nY=a b", "ok | P0 conn=1 | C0 b=4 argv=\"export\",\"A=a\",\"b\" as= rd="}, |
| 788 | {"quoted export splits (bash)", "\"export\" A=$Y\n", "Y=a b", "ok | P0 conn=1 | C0 b=4 argv=\"export\",\"A=a\",\"b\" as= rd="}, |
| 789 | {"non NAME= argument of export still splits", "export $Y\n", "Y=a b", "ok | P0 conn=1 | C0 b=4 argv=\"export\",\"a\",\"b\" as= rd="}, |
| 790 | {"assignment-like argument of another command splits", "echo A=$Y\n", "Y=a b", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"A=a\",\"b\" as= rd="}, |
| 791 | {"colon is unsupported", ": x\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 792 | {"declare is unsupported", "declare x=1\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 793 | {"complete (8 bytes) is unsupported", "complete\n", "", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 794 | {"shopt via variable is unsupported", "$S -s x\n", "S=shopt", "refuse 239 UNSUPPORTED_BUILTIN off=0"}, |
| 795 | {"external lookalike", "settings readx evalx\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"settings\",\"readx\",\"evalx\" as= rd="}, |
| 796 | {"long name is not a builtin", "continuex\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"continuex\" as= rd="}, |
| 797 | {"blank list", "\n", "", "ok"}, |
| 798 | {"comment", "echo a # not b $X\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a\" as= rd="}, |
| 799 | {"hash inside word", "echo a#b\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"a#b\" as= rd="}, |
| 800 | {"high bytes pass through", "echo \xc3\xa9 \xff\n", "", "ok | P0 conn=1 | C0 b=0 argv=\"echo\",\"\\xc3\\xa9\",\"\\xff\" as= rd="}, |
| 801 | {"lexer refusal keeps its code", "echo $(x)\n", "", "refuse 226 CMDSUB off=6"}, |
| 802 | {"parser refusal keeps its code", "if x\n", "", "refuse 233 IF_COMPOUND off=0"}, |
| 803 | {"more input", "echo \"abc", "", "more"}, |
| 804 | }; |
| 805 | |
| 806 | static void run_hostile(uint64_t *rng) |
| 807 | { |
| 808 | for (size_t i = 0; i < sizeof CASES / sizeof CASES[0]; i++) { |
| 809 | const Case *c = &CASES[i]; |
| 810 | OshXrEnv *env = calloc(1, sizeof *env); |
| 811 | env_parse(env, c->env, strlen(c->env), 1); |
| 812 | HOpt o; |
| 813 | memset(&o, 0, sizeof o); |
| 814 | o.fixed_status = 1; |
| 815 | o.noskip = 1; |
| 816 | o.rng = rng; |
| 817 | o.probe = 1; |
| 818 | for (Engine en = INTERP; en <= NATIVE; en++) { |
| 819 | HRes *h = malloc(sizeof *h); |
| 820 | host_run(en, (const uint8_t *)c->in, strlen(c->in), env, &o, h, NULL); |
| 821 | char got[16384]; |
| 822 | fmt_result(h, 0, got); |
| 823 | int ok = strcmp(got, c->want) == 0; |
| 824 | if (!ok) fprintf(stderr, " case [%s] %s:\n want: %s\n got: %s\n", c->name, en == INTERP ? "interp" : "native", c->want, got); |
| 825 | check(ok, "hostile [%s] %s", c->name, en == INTERP ? "interp" : "native"); |
| 826 | free(h); |
| 827 | } |
| 828 | n_hostile++; |
| 829 | run_case(c->name, (const uint8_t *)c->in, strlen(c->in), env, 0, 1, 0, rng, i, NULL); |
| 830 | free(env); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | /* ---- capacities and larger shapes (built at run time) ---- */ |
| 835 | static size_t setb(uint8_t *b, const char *s) |
| 836 | { |
| 837 | size_t l = strlen(s); |
| 838 | memcpy(b, s, l); |
| 839 | return l; |
| 840 | } |
| 841 | |
| 842 | static void run_caps(uint64_t *rng) |
| 843 | { |
| 844 | OshXrEnv *env = calloc(1, sizeof *env); |
| 845 | static uint8_t buf[8192], val[1200]; |
| 846 | size_t n; |
| 847 | HRes *h = malloc(sizeof *h); |
| 848 | HOpt o; |
| 849 | memset(&o, 0, sizeof o); |
| 850 | o.fixed_status = 1; |
| 851 | o.noskip = 1; |
| 852 | o.rng = rng; |
| 853 | o.probe = 1; |
| 854 | #define RUN_EXPECT(label, wantfinal, wantoff) \ |
| 855 | do { \ |
| 856 | for (Engine en = INTERP; en <= NATIVE; en++) { \ |
| 857 | host_run(en, buf, n, env, &o, h, NULL); \ |
| 858 | check(h->final == (wantfinal) && (((wantfinal) < 200) || (long long)(wantoff) < 0 || h->err_off == (wantoff)), "cap [%s] %s: final %llu@%llu want %d@%d", label, \ |
| 859 | en == INTERP ? "interp" : "native", (unsigned long long)h->final, (unsigned long long)h->err_off, (int)(wantfinal), (int)(wantoff)); \ |
| 860 | } \ |
| 861 | n_hostile++; \ |
| 862 | run_case(label, buf, n, env, 0, 1, 0, rng, 0, NULL); \ |
| 863 | } while (0) |
| 864 | |
| 865 | /* 32 fields: ok; 33: CAP_FIELDS */ |
| 866 | n = 0; n = setb(buf, "echo"); |
| 867 | for (int i = 0; i < 31; i++) { buf[n++] = ' '; buf[n++] = 'x'; } |
| 868 | buf[n++] = '\n'; |
| 869 | RUN_EXPECT("32 argv words", 104, 0); |
| 870 | n = 0; n = setb(buf, "echo"); |
| 871 | for (int i = 0; i < 31; i++) { buf[n++] = ' '; buf[n++] = 'x'; } |
| 872 | n += setb(buf + n, " y\n"); |
| 873 | RUN_EXPECT("33 argv words (CAP_WORDS)", 206, n - 2); |
| 874 | /* split into 33 fields: the 33rd closes at the word end */ |
| 875 | { |
| 876 | size_t k = 0; |
| 877 | for (int i = 0; i < 33; i++) { val[k++] = 'f'; val[k++] = ' '; } |
| 878 | env_set_var(env, "F", val, k); |
| 879 | } |
| 880 | n = setb(buf, "echo $F\n"); |
| 881 | RUN_EXPECT("split into 33 fields", 209, 5); |
| 882 | n = setb(buf, "$F\n"); |
| 883 | RUN_EXPECT("split into 33 fields (first word)", 209, 0); |
| 884 | { |
| 885 | size_t k = 0; |
| 886 | for (int i = 0; i < 32; i++) { val[k++] = 'f'; val[k++] = ' '; } |
| 887 | env_set_var(env, "F", val, k); |
| 888 | } |
| 889 | n = setb(buf, "$F\n"); |
| 890 | RUN_EXPECT("split into 32 fields", 104, 0); |
| 891 | /* values: 1024 ok, 1025 CAP_VALUE */ |
| 892 | memset(val, 'v', 1024); |
| 893 | env_set_var(env, "V1024", val, 1024); |
| 894 | memset(val, 'v', 1025); |
| 895 | env_set_var(env, "V1025", val, 1025); |
| 896 | n = setb(buf, "echo \"$V1024\"\n"); |
| 897 | RUN_EXPECT("value of 1024 bytes", 104, 0); |
| 898 | n = setb(buf, "echo \"$V1025\"\n"); |
| 899 | RUN_EXPECT("value of 1025 bytes", 211, 5); |
| 900 | n = setb(buf, "echo $V1025\n"); |
| 901 | RUN_EXPECT("value of 1025 bytes unquoted", 211, 5); |
| 902 | /* CAP_OUT is 8192 bytes of argv strings in one pipeline, the command name included */ |
| 903 | memset(val, 'x', 1023); |
| 904 | val[1023] = 0; |
| 905 | n = setb(buf, "e"); |
| 906 | for (int i = 0; i < 7; i++) n += setb(buf + n, " \"$V1024\""); |
| 907 | n += setb(buf + n, " "); |
| 908 | n += setb(buf + n, (const char *)val); |
| 909 | n += setb(buf + n, "\n"); |
| 910 | RUN_EXPECT("8192 expanded bytes", 104, -1); |
| 911 | n = setb(buf, "e"); |
| 912 | for (int i = 0; i < 7; i++) n += setb(buf + n, " \"$V1024\""); |
| 913 | n += setb(buf + n, " "); |
| 914 | n += setb(buf + n, (const char *)val); |
| 915 | n += setb(buf + n, "x\n"); |
| 916 | RUN_EXPECT("8193 expanded bytes", 210, -1); |
| 917 | n = 0; |
| 918 | for (int i = 0; i < 8; i++) n += setb(buf + n, "A=\"$V1024\" "); |
| 919 | buf[n - 1] = '\n'; |
| 920 | RUN_EXPECT("assignment names and values count toward CAP_OUT", 210, -1); |
| 921 | /* CAP_VARREQ: 64 requests ok, 65 refused (words are capped at 32 per command, so spread over commands) */ |
| 922 | val[0] = 'x'; |
| 923 | env_set_var(env, "X", val, 1); |
| 924 | { |
| 925 | static const unsigned c64[] = {22, 21, 21}, c65[] = {22, 22, 21}; |
| 926 | for (int v = 0; v < 2; v++) { |
| 927 | const unsigned *cn = v ? c65 : c64; |
| 928 | n = 0; |
| 929 | for (int c = 0; c < 3; c++) { |
| 930 | n += setb(buf + n, "echo"); |
| 931 | for (unsigned i = 0; i < cn[c]; i++) n += setb(buf + n, " $X"); |
| 932 | n += setb(buf + n, c < 2 ? " | " : "\n"); |
| 933 | } |
| 934 | if (v) RUN_EXPECT("65 variable requests over three commands", 212, -1); |
| 935 | else RUN_EXPECT("64 variable requests over three commands", 104, 0); |
| 936 | } |
| 937 | } |
| 938 | /* "$@" costs 1 + npos requests */ |
| 939 | for (int k = 1; k <= 10; k++) { |
| 940 | uint8_t v = (uint8_t)('a' + k % 26); |
| 941 | env_set_pos(env, k, &v, 1); |
| 942 | } |
| 943 | n = setb(buf, "echo \"$@\"\n"); |
| 944 | RUN_EXPECT("quoted at, 10 positionals", 104, 0); |
| 945 | n = setb(buf, "echo \"$@\" \"$@\" | echo \"$@\" \"$@\"\n"); |
| 946 | RUN_EXPECT("quoted at, 44 requests", 104, 0); |
| 947 | n = setb(buf, "echo \"$@\" \"$@\" | echo \"$@\" \"$@\" | echo \"$@\" \"$@\"\n"); |
| 948 | RUN_EXPECT("quoted at, 66 requests", 212, -1); |
| 949 | n = setb(buf, "echo \"$@\" \"$@\" \"$@\" \"$@\"\n"); |
| 950 | RUN_EXPECT("quoted at four times (41 fields)", 209, -1); |
| 951 | /* budget: a 1024 byte value copied many times needs several calls */ |
| 952 | n = setb(buf, "echo \"$V1024\" \"$V1024\" \"$V1024\"\n"); |
| 953 | RUN_EXPECT("3072 bytes, several budget resumes", 104, 0); |
| 954 | host_run(INTERP, buf, n, env, &o, h, NULL); |
| 955 | check(h->budget_resumes >= 2, "budget resumes %u", h->budget_resumes); |
| 956 | free(h); |
| 957 | free(env); |
| 958 | #undef RUN_EXPECT |
| 959 | } |
| 960 | |
| 961 | /* ---- header refusals and corrupted state ---- */ |
| 962 | static void run_abi(uint64_t *rng) |
| 963 | { |
| 964 | const char *line = "echo $X\n"; |
| 965 | size_t n = strlen(line); |
| 966 | OshXrEnv *env = calloc(1, sizeof *env); |
| 967 | for (Engine en = INTERP; en <= NATIVE; en++) { |
| 968 | uint64_t *w = sess_new(); |
| 969 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 217, "expand_run before the lexer and parser: ABI_RESERVED"); |
| 970 | free(w); |
| 971 | w = sess_new(); |
| 972 | w[OSH_S_MAGIC] = 1; |
| 973 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 214, "bad magic"); |
| 974 | free(w); |
| 975 | w = sess_new(); |
| 976 | w[OSH_S_VERSION] = 2; |
| 977 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 215, "bad version"); |
| 978 | free(w); |
| 979 | w = sess_new(); |
| 980 | check(ucall(OSH_U_EXPAND, en, w, 1, (const uint8_t *)line, n) == 216, "short header"); |
| 981 | free(w); |
| 982 | w = sess_new(); |
| 983 | check(ucall(OSH_U_EXPAND, en, w, WS - 1, (const uint8_t *)line, n) == 213, "workspace one cell short"); |
| 984 | free(w); |
| 985 | w = sess_new(); |
| 986 | w[50] = 1; |
| 987 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 217, "reserved cell"); |
| 988 | free(w); |
| 989 | /* a refusal is terminal and repeated */ |
| 990 | w = sess_new(); |
| 991 | int fl; |
| 992 | unsigned lr = 0; |
| 993 | check(lex_parse(en, w, (const uint8_t *)line, n, &fl, &lr) == 0, "lex+parse"); |
| 994 | uint64_t st = ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n); |
| 995 | check(st == 102, "NEED_VAR expected, got %llu", (unsigned long long)st); |
| 996 | w[OSH_STAGING] = 1; |
| 997 | w[OSH_STAGING + 1] = 2; |
| 998 | w[OSH_STAGING + 4] = 'a'; |
| 999 | w[OSH_STAGING + 5] = '*'; |
| 1000 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 245, "VALUE_GLOB"); |
| 1001 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 245, "refusal repeats"); |
| 1002 | check(w[OSH_S_PHASE] == 255 && w[OSH_S_ERR_OFF] == 5, "terminal with offset"); |
| 1003 | free(w); |
| 1004 | /* a staging value of 1025 with found=1 */ |
| 1005 | w = sess_new(); |
| 1006 | lex_parse(en, w, (const uint8_t *)line, n, &fl, &lr); |
| 1007 | ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n); |
| 1008 | w[OSH_STAGING] = 1; |
| 1009 | w[OSH_STAGING + 1] = 1025; |
| 1010 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 211, "CAP_VALUE from staging length"); |
| 1011 | free(w); |
| 1012 | /* an unanswered request stays 102 and changes nothing */ |
| 1013 | w = sess_new(); |
| 1014 | lex_parse(en, w, (const uint8_t *)line, n, &fl, &lr); |
| 1015 | ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n); |
| 1016 | uint64_t *snap = malloc(WS * 8); |
| 1017 | memcpy(snap, w, WS * 8); |
| 1018 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 102 && memcmp(snap, w, WS * 8) == 0, "unanswered repeat"); |
| 1019 | free(snap); |
| 1020 | free(w); |
| 1021 | /* corrupt expander cells */ |
| 1022 | w = sess_new(); |
| 1023 | lex_parse(en, w, (const uint8_t *)line, n, &fl, &lr); |
| 1024 | w[OSH_EX_INIT] = 1; |
| 1025 | w[OSH_EX_ST] = 9; |
| 1026 | check(ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, n) == 217, "corrupt state cell"); |
| 1027 | free(w); |
| 1028 | /* an input slice shorter than the tokens say: bounds trap or refusal, never a read outside */ |
| 1029 | w = sess_new(); |
| 1030 | lex_parse(en, w, (const uint8_t *)line, n, &fl, &lr); |
| 1031 | uint64_t r = ucall(OSH_U_EXPAND, en, w, WS, (const uint8_t *)line, 3); |
| 1032 | check(r == OSH_CORE_FAULT || r >= 100, "short input slice returned %llu", (unsigned long long)r); |
| 1033 | free(w); |
| 1034 | /* NULL/empty input slice with a workspace that expects bytes */ |
| 1035 | w = sess_new(); |
| 1036 | lex_parse(en, w, (const uint8_t *)line, n, &fl, &lr); |
| 1037 | r = ucall(OSH_U_EXPAND, en, w, WS, NULL, 0); |
| 1038 | check(r == OSH_CORE_FAULT || r >= 100, "empty input slice returned %llu", (unsigned long long)r); |
| 1039 | free(w); |
| 1040 | } |
| 1041 | (void)rng; |
| 1042 | free(env); |
| 1043 | } |
| 1044 | |
| 1045 | /* flip random cells of a valid post-parse workspace and call the expander on both engines: no memory error (ASan), |
| 1046 | * same outcome and same workspace on both engines (a trap is an outcome) */ |
| 1047 | static void run_corrupt(uint64_t *rng, unsigned count) |
| 1048 | { |
| 1049 | static const char *const seeds[] = { |
| 1050 | "echo a$X \"b $S\" 'c' >out 2>&1 <in\n", |
| 1051 | "A=1 B=$A c=$@ cmd \"$@\" $* ${1}x && x | y || z; q\n", |
| 1052 | "cd /tmp; pwd | cat > $E\n", |
| 1053 | "echo \"$#\" $? ${0}\n", |
| 1054 | }; |
| 1055 | OshXrEnv *env = calloc(1, sizeof *env); |
| 1056 | { const char *et = "X=v\nS=a b\nE=\n@arg1=p\n@arg2=q r\n"; env_parse(env, et, strlen(et), 0); } |
| 1057 | HRes *dummy = malloc(sizeof *dummy); |
| 1058 | (void)dummy; |
| 1059 | for (unsigned it = 0; it < count; it++) { |
| 1060 | const char *line = seeds[it % 4]; |
| 1061 | size_t n = strlen(line); |
| 1062 | uint64_t *wa = sess_new(), *wb; |
| 1063 | int fl; |
| 1064 | unsigned lr = 0; |
| 1065 | lex_parse(INTERP, wa, (const uint8_t *)line, n, &fl, &lr); |
| 1066 | wa[OSH_S_NOSKIP] = rng_next(rng) & 1; |
| 1067 | /* corrupt */ |
| 1068 | unsigned nf = 1 + (unsigned)(rng_next(rng) % 4); |
| 1069 | for (unsigned f = 0; f < nf; f++) { |
| 1070 | uint64_t r = rng_next(rng); |
| 1071 | size_t cell; |
| 1072 | switch (r % 5) { |
| 1073 | case 0: cell = OSH_TOKENS + (r >> 8) % 64; break; |
| 1074 | case 1: cell = OSH_CMDS + (r >> 8) % 48; break; |
| 1075 | case 2: cell = OSH_PIPES + (r >> 8) % 24; break; |
| 1076 | case 3: cell = 12 + (r >> 8) % 36; break; |
| 1077 | default: cell = OSH_STAGING + (r >> 8) % 8; break; |
| 1078 | } |
| 1079 | uint64_t v = rng_next(rng); |
| 1080 | switch ((v >> 60) % 4) { |
| 1081 | case 0: v &= 0xFF; break; |
| 1082 | case 1: v &= 0xFFFF; break; |
| 1083 | case 2: v = ~0ULL - (v & 3); break; |
| 1084 | default: v &= 0xFFFFFFFFULL; break; |
| 1085 | } |
| 1086 | wa[cell] = v; |
| 1087 | } |
| 1088 | wb = malloc(WS * 8); |
| 1089 | memcpy(wb, wa, WS * 8); |
| 1090 | uint64_t ra = ~0ULL, rb = ~0ULL; |
| 1091 | for (int step = 0; step < 6; step++) { |
| 1092 | ra = ucall(OSH_U_EXPAND, INTERP, wa, WS, (const uint8_t *)line, n); |
| 1093 | rb = ucall(OSH_U_EXPAND, NATIVE, wb, WS, (const uint8_t *)line, n); |
| 1094 | if (ra != rb) break; |
| 1095 | if (ra == OSH_CORE_FAULT) break; |
| 1096 | if (ra == 102) { |
| 1097 | for (uint64_t *w = wa, k = 0; k < 2; k++, w = wb) { |
| 1098 | w[OSH_STAGING] = 1; |
| 1099 | w[OSH_STAGING + 1] = 1; |
| 1100 | w[OSH_STAGING + 2] = 2; |
| 1101 | w[OSH_STAGING + 4] = 'q'; |
| 1102 | } |
| 1103 | continue; |
| 1104 | } |
| 1105 | if (ra == 103) continue; |
| 1106 | break; |
| 1107 | } |
| 1108 | n_corrupt++; |
| 1109 | if (ra == OSH_CORE_FAULT) n_corrupt_trap++; |
| 1110 | check(ra == rb && memcmp(wa, wb, WS * 8) == 0, "corrupt %u: engines disagree (%llx vs %llx, workspace %s)", it, (unsigned long long)ra, (unsigned long long)rb, |
| 1111 | memcmp(wa, wb, WS * 8) == 0 ? "same" : "differs"); |
| 1112 | free(wa); |
| 1113 | free(wb); |
| 1114 | } |
| 1115 | free(dummy); |
| 1116 | free(env); |
| 1117 | } |
| 1118 | |
| 1119 | /* ---- fuzz ---- */ |
| 1120 | static uint64_t g_rng_state; |
| 1121 | static unsigned rnd(unsigned m) { return (unsigned)(rng_next(&g_rng_state) % m); } |
| 1122 | |
| 1123 | static const char *const FWORD[] = {"a", "bc", "x-y", "/tmp/f", "=", "1", "a=b", "-n", "it's", "A", "foo.txt", "#h", "{", "}", "x\\ y", "\\*", "\\$X", "\\\"", "\\'"}; |
| 1124 | static const char *const FVAR[] = {"$X", "${X}", "$S", "${S}", "$E", "$U", "$L", "$T", "$G", "$Q", "$W", "$B", "$M", "$1", "$2", "$3", "$0", "$?", "$#", "$@", "$*", |
| 1125 | "${1}", "$9", "$", "${E}x", "$X$S", "$X-$S", "${X}_", "$X_", "$H", "$Z", "$N", "$V"}; |
| 1126 | static const char *const FDQ[] = {"a", "b c", " ", "", "\\$", "\\\"", "\\\\", "\\n", "'", "`x", "$X", "${S}", "$S", "$E", "$U", "$@", "$*", "$#", "$?", "$1", "$G", "$", "*", "?", "[", "\\\n"}; |
| 1127 | static const char *const FSQ[] = {"a", "b c", "", "$X", "\\", "\"", "*"}; |
| 1128 | static const char *const FOP[] = {" ;", " &&", " ||", " |", " ;", " |"}; |
| 1129 | static const char *const FREDIR[] = {">", ">>", "<", "2>", "2>>", "0<", "1>", "2>&1", ">&2", "<&0", "1>&2", ">&1", "2<"}; |
| 1130 | static const char *const FJUNK[] = {"*", "?", "[", "~", "`", "(", ")", "&", "$(", "$((", "${", "${X", "${10}", "${X:-y}", "$$", "$!", "<<", "<<<", ">|", "{a,b}", "\\", "\"", "'", "!", "if", "set", "exec", "cd", "pwd", "exit", "printf", "export", "unset", "IFS=x", "wait", "time"}; |
| 1131 | |
| 1132 | static size_t gen_word(char *b, size_t n) |
| 1133 | { |
| 1134 | size_t o = 0; |
| 1135 | unsigned parts = 1 + rnd(4); |
| 1136 | for (unsigned i = 0; i < parts; i++) { |
| 1137 | const char *s; |
| 1138 | switch (rnd(8)) { |
| 1139 | case 0: case 1: s = FWORD[rnd(sizeof FWORD / sizeof *FWORD)]; break; |
| 1140 | case 2: case 3: s = FVAR[rnd(sizeof FVAR / sizeof *FVAR)]; break; |
| 1141 | case 4: { |
| 1142 | o += (size_t)snprintf(b + o, n - o, "\""); |
| 1143 | unsigned k = rnd(4); |
| 1144 | for (unsigned j = 0; j < k; j++) o += (size_t)snprintf(b + o, n - o, "%s", FDQ[rnd(sizeof FDQ / sizeof *FDQ)]); |
| 1145 | o += (size_t)snprintf(b + o, n - o, "\""); |
| 1146 | continue; |
| 1147 | } |
| 1148 | case 5: o += (size_t)snprintf(b + o, n - o, "'%s'", FSQ[rnd(sizeof FSQ / sizeof *FSQ)]); continue; |
| 1149 | case 6: s = rnd(3) == 0 ? FJUNK[rnd(sizeof FJUNK / sizeof *FJUNK)] : FVAR[rnd(sizeof FVAR / sizeof *FVAR)]; break; |
| 1150 | default: s = ""; break; |
| 1151 | } |
| 1152 | o += (size_t)snprintf(b + o, n - o, "%s", s); |
| 1153 | } |
| 1154 | return o; |
| 1155 | } |
| 1156 | |
| 1157 | static size_t gen_line(char *b, size_t n) |
| 1158 | { |
| 1159 | size_t o = 0; |
| 1160 | unsigned npipe = 1 + rnd(4); |
| 1161 | for (unsigned p = 0; p < npipe && o + 400 < n; p++) { |
| 1162 | unsigned ncmd = 1 + (rnd(4) == 0 ? rnd(3) : 0); |
| 1163 | for (unsigned c = 0; c < ncmd; c++) { |
| 1164 | if (c) o += (size_t)snprintf(b + o, n - o, " |"); |
| 1165 | unsigned nt = rnd(6); |
| 1166 | for (unsigned t = 0; t < nt; t++) { |
| 1167 | o += (size_t)snprintf(b + o, n - o, " "); |
| 1168 | unsigned k = rnd(10); |
| 1169 | if (k == 0) { |
| 1170 | o += (size_t)snprintf(b + o, n - o, "%s ", FREDIR[rnd(sizeof FREDIR / sizeof *FREDIR)]); |
| 1171 | o += gen_word(b + o, n - o); |
| 1172 | } else if (k == 1 && t == 0) { |
| 1173 | o += (size_t)snprintf(b + o, n - o, "%c=", 'A' + (int)rnd(3)); |
| 1174 | o += gen_word(b + o, n - o); |
| 1175 | } else if (k == 2) { |
| 1176 | o += (size_t)snprintf(b + o, n - o, "%s", FJUNK[rnd(sizeof FJUNK / sizeof *FJUNK)]); |
| 1177 | } else if (k == 3 && t == 0) { |
| 1178 | static const char *const cmds[] = {"cd", "pwd", "printf", "export", "unset", "exit", "echo", "set", "eval", "wait"}; |
| 1179 | o += (size_t)snprintf(b + o, n - o, "%s", cmds[rnd(sizeof cmds / sizeof *cmds)]); |
| 1180 | } else { |
| 1181 | o += gen_word(b + o, n - o); |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | if (p + 1 < npipe) o += (size_t)snprintf(b + o, n - o, "%s", FOP[rnd(sizeof FOP / sizeof *FOP)]); |
| 1186 | } |
| 1187 | o += (size_t)snprintf(b + o, n - o, "\n"); |
| 1188 | return o; |
| 1189 | } |
| 1190 | |
| 1191 | static void fuzz_env(OshXrEnv *env) |
| 1192 | { |
| 1193 | memset(env, 0, sizeof *env); |
| 1194 | static uint8_t big[1100]; |
| 1195 | const char *txt = "X=v\nS=a b c\nE=\nL= lead\nT=trail \nG=x*y\nQ=q?\nW=w\\tx\\ny\nB=b\nM=mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm\nZ=z\nV=-\nN=n\\0z\n"; |
| 1196 | env_parse(env, txt, strlen(txt), 1); |
| 1197 | memset(big, 'h', 1030); |
| 1198 | env_set_var(env, "H", big, 1030); |
| 1199 | } |
| 1200 | |
| 1201 | static void run_fuzz(uint64_t seed, unsigned long count) |
| 1202 | { |
| 1203 | g_rng_state = seed; |
| 1204 | uint64_t rng = seed ^ 0xABCDEF; |
| 1205 | OshXrEnv *env = malloc(sizeof *env); |
| 1206 | char *line = malloc(4096); |
| 1207 | unsigned long tick = 0; |
| 1208 | for (unsigned long it = 0; it < count; it++) { |
| 1209 | fuzz_env(env); |
| 1210 | env->npos = 0; |
| 1211 | unsigned np = rnd(5); |
| 1212 | static const char *const pv[] = {"p1", "", "a b", " x ", "*", "q", "-", "p?"}; |
| 1213 | for (unsigned k = 1; k <= np; k++) { |
| 1214 | const char *s = pv[rnd(sizeof pv / sizeof *pv)]; |
| 1215 | env_set_pos(env, (int)k, (const uint8_t *)s, strlen(s)); |
| 1216 | } |
| 1217 | if (rnd(4) != 0) env_set_pos(env, 0, (const uint8_t *)"osh", 3); |
| 1218 | env->status = rnd(4); |
| 1219 | size_t n = gen_line(line, 4000); |
| 1220 | int eoi = rnd(8) == 0; |
| 1221 | int noskip = rnd(3) == 0; |
| 1222 | uint64_t sseed = rng_next(&rng); |
| 1223 | HRes *keep = NULL; |
| 1224 | run_case("fuzz", (const uint8_t *)line, n, env, eoi, noskip, sseed, &rng, it, keep); |
| 1225 | n_fuzz++; |
| 1226 | if (++tick == 20000) { tick = 0; fprintf(stderr, " fuzz %lu / %lu\n", it + 1, count); } |
| 1227 | } |
| 1228 | free(line); |
| 1229 | free(env); |
| 1230 | } |
| 1231 | |
| 1232 | static void unit_stats(void) |
| 1233 | { |
| 1234 | static const char *const nm[] = {"lex", "parse", "expand"}; |
| 1235 | for (int u = 0; u < OSH_U_COUNT; u++) { |
| 1236 | const OscUnit *U = CORE.u[u].U; |
| 1237 | unsigned maxv = 0, maxi = 0, maxp = 0; |
| 1238 | const char *vn = "", *in = ""; |
| 1239 | for (int f = 0; f < U->nfuncs; f++) { |
| 1240 | const OscFunc *fn = &U->funcs[f]; |
| 1241 | if (u == OSH_U_EXPAND) printf("expand fn %-12s params=%u vregs=%u/%u insns=%u/%u\n", fn->name, fn->nparams, fn->nvregs, OSC_MAX_VREGS, fn->ninsns, OSC_MAX_INSNS); |
| 1242 | if (fn->nvregs > maxv) { maxv = fn->nvregs; vn = fn->name; } |
| 1243 | if (fn->ninsns > maxi) { maxi = fn->ninsns; in = fn->name; } |
| 1244 | if (fn->nparams > maxp) maxp = fn->nparams; |
| 1245 | check(fn->nparams <= OSC_MAX_PARAMS, "%s has %u param registers", fn->name, fn->nparams); |
| 1246 | } |
| 1247 | printf("%s unit: %d functions (max %u), max params %u (max %u); biggest vregs %s %u (headroom %u of %u); biggest insns %s %u (headroom %u of %u)\n", nm[u], |
| 1248 | U->nfuncs, OSC_MAX_FUNCS, maxp, OSC_MAX_PARAMS, vn, maxv, OSC_MAX_VREGS - maxv, OSC_MAX_VREGS, in, maxi, OSC_MAX_INSNS - maxi, OSC_MAX_INSNS); |
| 1249 | check(U->nfuncs <= (int)OSC_MAX_FUNCS, "too many functions in %s", nm[u]); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | int main(int argc, char **argv) |
| 1254 | { |
| 1255 | if (argc < 5) { |
| 1256 | fprintf(stderr, "usage: %s LEX.osc PARSE.osc EXPAND.osc VECTORS_DIR [FUZZ_COUNT]\n", argv[0]); |
| 1257 | return 2; |
| 1258 | } |
| 1259 | unsigned long fuzz = argc > 5 ? strtoul(argv[5], NULL, 10) : 100000; |
| 1260 | char *src[3]; |
| 1261 | size_t len[3]; |
| 1262 | for (int i = 0; i < 3; i++) { |
| 1263 | src[i] = slurp(argv[1 + i], &len[i]); |
| 1264 | if (!src[i]) { fprintf(stderr, "cannot read %s\n", argv[1 + i]); return 2; } |
| 1265 | } |
| 1266 | char err[300]; |
| 1267 | if (osh_core_init(&CORE, (const char *const *)src, len, err, sizeof err) != 0) { |
| 1268 | fprintf(stderr, "%s\n", err); |
| 1269 | printf("OSH_EXPAND_FAIL\n"); |
| 1270 | return 1; |
| 1271 | } |
| 1272 | unit_stats(); |
| 1273 | uint64_t rng = 0xD1CEC0FFEEULL; |
| 1274 | if (run_vectors(argv[4]) != 0) return 2; |
| 1275 | printf("vectors: %lu comparisons\n", n_vec); |
| 1276 | run_hostile(&rng); |
| 1277 | run_caps(&rng); |
| 1278 | run_abi(&rng); |
| 1279 | unsigned long hostile = n_hostile; |
| 1280 | run_corrupt(&rng, 20000); |
| 1281 | run_fuzz(0x5EED5EEDULL, fuzz); |
| 1282 | printf("cases %lu (hostile and cap cases %lu, fuzz %lu), interp-vs-native workspace comparisons %lu, reference comparisons %lu, chunked reruns %lu\n", n_cases, |
| 1283 | hostile, n_fuzz, n_eng_cmp, n_ref_cmp, n_chunk); |
| 1284 | printf("pipelines emitted %lu (each decoded by the host decoder: %lu), variable requests answered %lu, repeated-call probes %lu\n", n_pipes, n_decode, n_req, n_idem); |
| 1285 | printf("outcomes: %lu lists complete, %lu refused; corrupted-workspace runs %lu (%lu trapped, engines agreed on all)\n", n_final_ok, n_final_err, n_corrupt, n_corrupt_trap); |
| 1286 | printf("refusal codes seen:"); |
| 1287 | for (int c = 0; c < 256; c++) |
| 1288 | if (n_code[c] && c >= 200) printf(" %d=%lu", c, n_code[c]); |
| 1289 | printf("\ncalls: interp %lu native %lu faults %lu\n", CORE.calls_interp, CORE.calls_native, CORE.faults); |
| 1290 | printf("checks %lu, failures %lu\n", n_checks, n_fail); |
| 1291 | int bad = n_fail != 0; |
| 1292 | for (int c = 220; c <= 248; c++) (void)c; |
| 1293 | osh_core_free(&CORE); |
| 1294 | for (int i = 0; i < 3; i++) free(src[i]); |
| 1295 | printf(bad ? "OSH_EXPAND_FAIL\n" : "OSH_EXPAND_PASS\n"); |
| 1296 | return bad; |
| 1297 | } |