/* * 802_lenidx_run — BUG #19. Runtime + cs==ww byte-id net for the * `len()` builtin applied to an INDEXED str/slice element (`len(xs[i])`). * * THE BUG (cstage == wwstage, BOTH wrong — shared gap, NOT rule-10): * `len(t[i])` over a `[N]str` / `[]str` returned the element's .ptr, * not its length. The len() builtin (cgen.c / cgenexpr.ww) had arms for * an N_IDENT slice/str operand (.len at BP+off+8), a tuple-element N_DOT * (#235), and a whole TY_ARRAY (→ alen) — but NOT an N_INDEX element. * So `len(xs[i])` fell to the bare `cgexpr(arg)` fallback, which loads * the str/slice element header via cgslicehdr leaving AX=.ptr, BX=.len, * CX=.cap — and len() then returned AX (the ptr) AS the length. Same * family as #14 (literal `.len` returned ptr) and #18 (shared static-init * gap). byte-id was BLIND: no bootstrap source uses len(indexed-element) * — the corpus indexes-then-reads .len explicitly or hardcodes lengths. * CONTRAST (controls, all already correct): `t[i].len` pseudo-field, * `t[i].ptr`, and scalar `len(s)` over a bare str variable. * * THE FIX (#19): add an N_INDEX arm to the len() builtin in BOTH stages — * `cgexpr(arg)` to materialise the element, then `MOVQ BX, AX` to shuffle * the len word (already in BX after cgslicehdr) into the result reg. The * same MOVQ BX,AX shape as #14's `.len` pseudo-field fix. Covers str AND * slice elements (both are 24B headers loaded by cgslicehdr). * * EACH ROW CARRIES BOTH DIMENSIONS (801 model): * (a) cstage `ww build` + run, asserting the exit — pins that the * converged asm is runtime-correct (real lengths, not ptr bytes). * (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge (rule-10). * * GATE POLARITY: must stay GREEN. A wrong exit means len(indexed-element) * regressed back to returning the ptr; a byte-id FAIL means the stages * diverged. * * SCOPE NOTE: the []T (slice-of-slice) ELEMENT len shares the identical * codegen path (cgslicehdr → BX=len → MOVQ BX,AX) and is byte-id-clean in * isolation, but a clean runtime row for it is blocked by a SEPARATE * pre-existing bug — the `[2][]u8` array-literal → slice coercion emits a * divergent frame offset (cstage -24(BP) vs wwstage -40(BP)). That gap is * unrelated to len() (it reproduces with the `.len` pseudo-field too) and * is filed separately; these rows stay on str elements to avoid it. * * C5 SWEEP (#10 F2 + #41 FA2/FB1): the arm enumeration leaked FOUR * siblings over time (#235 → #19 → F2 → FA2/FB1) — every unhandled * operand shape fell to a bare cgexpr fallback returning the slice DATA * POINTER as the length, silent and byte-id-blind. The fix replaces the * fallback with ONE uniform header-place route (cgplaceaddr → .len at * place+8) in BOTH stages; non-place operands (string literal, slicing * expr, call result — all previously the same silent ptr-garbage) now * die LOUD (rule 7). Rows below pin: len(*p) param + local (FB1), * len(*p) on an EMPTY slice (a bare exit-code row can't pin this — the * garbage ptr's low byte masks to 0 — hence the branchy discriminator), * len(**pp) (chained deref, same resolver spine), len(xs[i].field) * (F2), len((*p)[i].field) incl. computed index, len(s.field) + * len(p.field) (struct-field cousins), the [N]T-const and ident-str * neutrality controls (global and tuple fast-paths have dedicated runs: * 797, 903), and reject rows with the exact rule-7 text. All * garbage/reject rows verified FAILING at the parent e481cb8 (the * per-row garbage exits below cite the d642017 probes; garbage is * frame-layout-dependent and drifts between commits — the FAILING * status is the pin, not the value). */ #include #include #include #include #include #include static int runwait(const char *cmd) { int rc = system(cmd); if (rc == -1) return -1; if (WIFEXITED(rc)) return WEXITSTATUS(rc); return -1; } /* reject != NULL marks a build-reject row: both stages must refuse the * source LOUD and the diagnostic must contain that exact text. */ struct row { const char *label; const char *src; int want_exit; const char *reject; }; static const struct row rows[] = { /* the exact #19 repro: a [3]str table, len(t[1]) == 3. Pre-fix this * returned a ptr low-byte (58), not 3. */ { "arrtab_mid", "package main;\n" "let t: [3]str = [\"a\", \"bcd\", \"\"];\n" "export fn main() i32 = { return len(t[1]): i32; };\n", 3 }, /* empty-string element edge: len(t[2]) == 0 (a ptr would be nonzero). */ { "arrtab_empty", "package main;\n" "let t: [3]str = [\"a\", \"bcd\", \"\"];\n" "export fn main() i32 = { return len(t[2]): i32; };\n", 0 }, /* first element: len(t[0]) == 1 — guards an off-by-one in the index * scale (a ptr or the wrong element would not be 1). */ { "arrtab_first", "package main;\n" "let t: [3]str = [\"a\", \"bcd\", \"\"];\n" "export fn main() i32 = { return len(t[0]): i32; };\n", 1 }, /* []str slice parameter — the callee-side shape: len(xs[i]) over a * slice arg, summing all three lengths (1 + 3 + 0 == 4). */ { "slice_param_sum", "package main;\n" "fn slen(xs: []str, i: i64) i32 = { return len(xs[i]): i32; };\n" "export fn main() i32 = {\n" " let arr: [3]str = [\"a\", \"bcd\", \"\"];\n" " let xs: []str = arr;\n" " return slen(xs, 0) + slen(xs, 1) + slen(xs, 2);\n" "};\n", 4 }, /* NEGATIVE CONTROL: the `.len` pseudo-field on the same indexed * element must agree with len() (both == 3), AND `.ptr` must still * deref to the first byte ('b' == 98) — proving the fix left the * already-correct .len/.ptr field paths untouched. Returns 3 only if * len()==.len AND *.ptr=='b'. */ { "neg_control_field_agree", "package main;\n" "let t: [3]str = [\"a\", \"bcd\", \"\"];\n" "export fn main() i32 = {\n" " if (len(t[1]) != t[1].len) { return 91; };\n" " let p: *u8 = t[1].ptr;\n" " if (*p != 'b') { return 92; };\n" " return len(t[1]): i32;\n" "};\n", 3 }, /* ---- C5 sweep rows (#10 F2 + #41 FA2/FB1) ---- */ /* FB1 exact repro: len(*p) through a *[]T PARAM loaded the slice * header's word 0 (the data pointer) as the length — silent * ptr-garbage (exit 48 at d642017), byte-id both stages. */ { "deref_param", "package main;\n" "fn n(p: *[]i64) i32 = { return len(*p): i32; };\n" "export fn main() i32 = {\n" " let xs: []i64 = [10, 20, 30];\n" " return n(&xs);\n" "};\n", 3, NULL }, /* FB1, local-ptr variant (exit 64 at d642017). */ { "deref_local", "package main;\n" "export fn main() i32 = {\n" " let xs: []i64 = [10, 20, 30];\n" " let p: *[]i64 = &xs;\n" " return len(*p): i32;\n" "};\n", 3, NULL }, /* EMPTY-slice deref: the off-by-header bug returns .ptr (nonzero), * len() must say 0. A bare exit row can't pin it — the garbage * ptr's low byte happens to mask to 0 (verified at e481cb8) — so * branch on len()!=0 and return distinct codes. */ { "deref_empty", "package main;\n" "fn n(p: *[]i64) i32 = {\n" " if (len(*p) != 0) { return 9; };\n" " return 42;\n" "};\n" "export fn main() i32 = {\n" " let xs: []i64 = [1];\n" " let ys: []i64 = xs[0:0];\n" " return n(&ys);\n" "};\n", 42, NULL }, /* chained deref len(**pp) — the resolver spine must recurse through * BOTH hops (exit 208 garbage at e481cb8). */ { "deref_chain", "package main;\n" "export fn main() i32 = {\n" " let xs: []i64 = [10, 20, 30];\n" " let p: *[]i64 = &xs;\n" " let pp: **[]i64 = &p;\n" " return len(**pp): i32;\n" "};\n", 3, NULL }, /* F2 exact repro: len(xs[i].field) — N_DOT over an N_INDEX base * matched no arm (the #235 arm requires an IDENT base) and fell to * the ptr-garbage fallback (exit 147 at d642017). */ { "idx_field", "package main;\n" "type S = struct { pad: i64, name: str };\n" "export fn main() i32 = {\n" " let xs: [2]S = [S{ pad = 1, name = \"a\" }, S{ pad = 2, name = \"bcde\" }];\n" " return len(xs[1].name): i32;\n" "};\n", 4, NULL }, /* full deref spine: len((*p)[i].field) (exit 10 at d642017). */ { "deref_idx_field", "package main;\n" "type S = struct { pad: i64, name: str };\n" "fn n(p: *[]S, i: i64) i32 = { return len((*p)[i].name): i32; };\n" "export fn main() i32 = {\n" " let arr: [2]S = [S{ pad = 1, name = \"a\" }, S{ pad = 2, name = \"bcde\" }];\n" " let xs: []S = arr;\n" " return n(&xs, 1);\n" "};\n", 4, NULL }, /* computed index through the spine — cgplaceaddr cgexpr's the index * operand, so i+1 must resolve identically to a constant. */ { "deref_idx_field_computed", "package main;\n" "type S = struct { pad: i64, name: str };\n" "fn n(p: *[]S, i: i64) i32 = { return len((*p)[i + 1].name): i32; };\n" "export fn main() i32 = {\n" " let arr: [2]S = [S{ pad = 1, name = \"a\" }, S{ pad = 2, name = \"bcde\" }];\n" " let xs: []S = arr;\n" " return n(&xs, 0);\n" "};\n", 4, NULL }, /* struct-field cousins: len(s.field) (exit 75 at d642017) and * len(p.field) through a *struct (exit 87 at d642017) — the #235 * arm's inner non-tuple fallback was the same ptr-garbage. */ { "dot_field", "package main;\n" "type S = struct { pad: i64, name: str };\n" "export fn main() i32 = {\n" " let s: S = S{ pad = 7, name = \"abc\" };\n" " return len(s.name): i32;\n" "};\n", 3, NULL }, { "ptr_field", "package main;\n" "type S = struct { pad: i64, name: str };\n" "export fn main() i32 = {\n" " let s: S = S{ pad = 7, name = \"abc\" };\n" " let p: *S = &s;\n" " return len(p.name): i32;\n" "};\n", 3, NULL }, /* neutrality controls — already correct pre-sweep, pin that the * enumerated fast-paths ([N]T const fold, ident str) still hold. */ { "neutral_array_const", "package main;\n" "export fn main() i32 = {\n" " let a: [5]u8 = [1, 2, 3, 4, 5];\n" " return len(a): i32;\n" "};\n", 5, NULL }, { "neutral_ident_str", "package main;\n" "export fn main() i32 = {\n" " let s: str = \"abcd\";\n" " return len(s): i32;\n" "};\n", 4, NULL }, /* non-place operands: previously the same SILENT ptr-garbage * (strlit exit 40, slice-expr exit 48, call-result exit 0 at * master); now LOUD per rule 7 in both stages. */ { "reject_strlit", "package main;\n" "export fn main() i32 = { return len(\"abc\"): i32; };\n", 0, "#10/#41: len() operand shape not place-resolvable (rule-7)" }, { "reject_sliceexpr", "package main;\n" "export fn main() i32 = {\n" " let xs: []i64 = [10, 20, 30, 40];\n" " return len(xs[1:3]): i32;\n" "};\n", 0, "#10/#41: len() operand shape not place-resolvable (rule-7)" }, { "reject_callres", "package main;\n" "fn mk() []i64 = {\n" " let xs: []i64 = [10, 20, 30];\n" " return xs;\n" "};\n" "export fn main() i32 = { return len(mk()): i32; };\n", 0, "#10/#41: len() operand shape not place-resolvable (rule-7)" }, { NULL, NULL, 0, NULL } }; static int file_contains(const char *path, const char *needle) { FILE *f = fopen(path, "rb"); if (!f) return 0; char buf[8192]; size_t got = fread(buf, 1, sizeof buf - 1, f); buf[got] = '\0'; fclose(f); return strstr(buf, needle) != NULL; } static int slurp_eq(const char *a, const char *b) { FILE *fa = fopen(a, "rb"); FILE *fb = fopen(b, "rb"); if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } int rc = 0; for (;;) { int ca = fgetc(fa); int cb = fgetc(fb); if (ca != cb) { rc = -1; break; } if (ca == EOF) break; } fclose(fa); fclose(fb); return rc; } int main(void) { const char *bin = getenv("BIN"); if (!bin) bin = "out/bin"; char absbin[1024]; if (bin[0] != '/') { char cwd[1024]; if (getcwd(cwd, sizeof cwd) == NULL) return 1; snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); bin = absbin; } char w6c[1100], w6c_ww[1100]; snprintf(w6c, sizeof w6c, "%s/w6c", bin); snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); if (access(w6c_ww, X_OK) != 0) { fprintf(stderr, "lenidx: w6c_ww missing — cannot run the " "cs==ww byte-id gate\n"); return 1; } int n = 0, fail = 0; for (int i = 0; rows[i].src; i++, n++) { char src[64]; snprintf(src, sizeof src, "/tmp/wwli_%d_%d.ww", getpid(), i); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; continue; } fputs(rows[i].src, f); fclose(f); /* reject rows: BOTH stages must refuse with the exact rule-7 * text — a silent accept means the ptr-garbage fallback is * back. */ if (rows[i].reject) { char errf[64], cmd2[2048]; int bad = 0; snprintf(errf, sizeof errf, "/tmp/wwli_%d_%d.err", getpid(), i); snprintf(cmd2, sizeof cmd2, "%s -o /dev/null %s 2>%s", w6c, src, errf); if (runwait(cmd2) == 0) { fprintf(stderr, "row[%s]: w6c ACCEPTED a " "reject row\n", rows[i].label); bad = 1; } else if (!file_contains(errf, rows[i].reject)) { fprintf(stderr, "row[%s]: w6c rejected but " "without the rule-7 text\n", rows[i].label); bad = 1; } snprintf(cmd2, sizeof cmd2, "%s -o /dev/null %s 2>%s", w6c_ww, src, errf); if (runwait(cmd2) == 0) { fprintf(stderr, "row[%s]: w6c_ww ACCEPTED a " "reject row\n", rows[i].label); bad = 1; } else if (!file_contains(errf, rows[i].reject)) { fprintf(stderr, "row[%s]: w6c_ww rejected but " "without the rule-7 text\n", rows[i].label); bad = 1; } if (bad) fail++; unlink(errf); unlink(src); continue; } /* (a) cstage build + run in a scratch dir. */ char tmpdir[64]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wwli_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); char cmd[2048]; snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s", tmpdir, bin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: cstage build failed\n", rows[i].label); fail++; unlink(src); rmdir(tmpdir); continue; } char outbin[128]; const char *base = strrchr(src, '/'); base = base ? base + 1 : src; snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); char *dot = strrchr(outbin, '.'); if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; int got = runwait(outbin); if (got != rows[i].want_exit) { fprintf(stderr, "row[%s]: cstage exit %d, want %d\n", rows[i].label, got, rows[i].want_exit); fail++; } unlink(outbin); rmdir(tmpdir); /* (b) cs==ww byte-id gate. */ char cs_s[64], ws_s[64]; snprintf(cs_s, sizeof cs_s, "/tmp/wwli_%d_%d_cs.s", getpid(), i); snprintf(ws_s, sizeof ws_s, "/tmp/wwli_%d_%d_ww.s", getpid(), i); snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label); fail++; unlink(src); continue; } snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, ws_s, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c_ww failed\n", rows[i].label); fail++; unlink(src); unlink(cs_s); continue; } if (slurp_eq(cs_s, ws_s) != 0) { fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER " "(rule-10 byte-id violation)\n", rows[i].label); fail++; } unlink(src); unlink(cs_s); unlink(ws_s); } if (fail) { fprintf(stderr, "%d/%d lenidx tests failed\n", fail, n); return 1; } printf("lenidx: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); return 0; }