/* * 915_arr_module_index_run — runtime + byte-id net for #128b: a * cross-package indexed read on an imported global `let arr: [N]T` * (the `mod.arr[i]` shape) must compute the array's ADDRESS via LEAQ * and stride by the element type's size. Pre-fix both stages * mis-compiled this shape, but differently: * - cstage: emitted `MOVQ mod.arr(SB), AX` (loaded the first 8 bytes * of the array as if it were a pointer-var value), then ADDed the * index, then MOVZBQ at that junk address — SEGFAULT-class memory * corruption on any non-trivial index. cstage also defaulted esz=1 * (the `bt = n->lhs->type` was NULL for SK_USE module idents). * - wwstage: had the same MOVQ-not-LEAQ bug; in earlier session * state also defaulted esz=8 + MOVQ-load (wrong stride + wrong * load-width). By the time #128b ratified, wwstage was emitting * IMUL$2 + MOVZWQ correctly (via stamped n.type_ path) but the * base load remained MOVQ-not-LEAQ. * * Fix (per the design's option B — use-site at cgindex/cgassign, NOT * a provider-shape change at cgdot): * - cstage: extend `cg_dotbase_addr` (the #135 helper) to recognise * module-imported `let X: [N]T` and emit `LEAQ X(SB)` for the * base. Add a `let_var_type(name)` lookup (LetVar gains a `type` * field) so cgindex's N_INDEX case can fall back to the imported * let's type when `n->lhs->type` is NULL (SK_USE-bound ident). * - wwstage: extend `dotbaseaddr` parallel — when the inner ident * isn't a local (localfindnode → nil) but `letvartnode(base.str)` * resolves to N_TARRAY, emit `LEAQ symname(SB)`. * * Rule-10 alignment: both stages share the same cgindex N_INDEX * fallback shape; the helper is the use-site fix per #135's pattern, * preserving cgdot's current MOVQ semantics so whole-array-assign * shapes (defensive case; not exercised in committed code per * design-pass audit) aren't disturbed. * * Each row carries (a) cstage `ww build` + run asserting the exit * code (the cstage-segfault repro now exits cleanly) and (b) a w6c vs * w6c_ww `.s` cmp (rule-10 byte-id). Imports `strconv` because its * `let left_shift_table: [65]u16` is the canonical reviewer-stofdata * probe surface for this bug. */ #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; } struct row { const char *label; const char *src; int want_exit; }; static const struct row rows[] = { /* The reviewer-stofdata probe: indexed read on an imported [N]u16 * at idx=0. Pre-fix cstage segfaulted on nontrivial indices and * MOVZBQ-loaded the wrong byte; here at idx=0 the wrong-stride * doesn't matter, so the test exit reflects the load-width fix: * MOVZWQ now reads the full u16 = 0x0000 → exit 0. */ { "mod_u16_idx0", "package main;\n" "import strconv;\n" "export fn main() i32 = {\n" " let v: u32 = (strconv.left_shift_table[0]: u32);\n" " return v: i32;\n" "};\n", 0 }, /* Nontrivial idx: pre-fix segfault repro. table[2] = 0x0801; * 0x0801 mod 256 = 1. */ { "mod_u16_idx2", "package main;\n" "import strconv;\n" "export fn main() i32 = {\n" " let v: u32 = (strconv.left_shift_table[2]: u32);\n" " return v: i32;\n" "};\n", 1 }, /* Further idx — pins per-element stride. table[4] = 0x1006; * 0x1006 mod 256 = 6. */ { "mod_u16_idx4", "package main;\n" "import strconv;\n" "export fn main() i32 = {\n" " let v: u32 = (strconv.left_shift_table[4]: u32);\n" " return v: i32;\n" "};\n", 6 }, /* Local-array control: confirms the existing IDENT-base path is * unchanged by the #128b cgindex edit. Pre-fix already worked; the * fix is gated by `bt == NULL` so this row's byte-id is * preserved. */ { "local_arr_control", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u16 = [10u16, 20u16, 30u16, 40u16];\n" " let v: u32 = (a[2]: u32);\n" " return v: i32;\n" "};\n", 30 }, /* Same-package access (NOT module-qualified) — pins that the * existing in-package IDENT path stays unchanged. left_shift_table * exists in strconv; access from within `package strconv` would be * IDENT-base. Wrap in a tiny helper here to keep test self- * contained; uses local table. */ { "local_u16_idx_nonzero", "package main;\n" "export fn main() i32 = {\n" " let t: [8]u16 = [0u16, 0x0800u16, 0x0801u16, 0x0803u16, " "0x1006u16, 0x1009u16, 0x100Du16, 0x1812u16];\n" " let v: u32 = (t[2]: u32);\n" " return v: i32;\n" "};\n", 1 }, { NULL, NULL, 0 } }; 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, "modarridx: w6c_ww missing — cannot run " "the cs==ww byte-id gate (the whole point of this test)\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/wwmoda_%d_%d.ww", getpid(), i); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; continue; } fputs(rows[i].src, f); fclose(f); char tmpdir[64]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wwmoda_%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); /* Byte-id only meaningful when the source compiles standalone * to a .s. Module-imported rows need the combined.ww shape * (which `ww build` produces inside tmpdir); we already ran * that above. For the byte-id leg, point at the combined.ww. */ char combined[160]; snprintf(combined, sizeof combined, "%s/%s.combined.ww", tmpdir, base); (void)combined; /* Both stages emit on the original .ww directly; the * combined.ww has the same surface for cgen purposes. We use * the original src for byte-id since w6c/w6c_ww handle the * import resolution when invoked on the file with -I and the * lib search. The probe row asserts module-qualified shape * via direct w6c{,_ww} on the .ww; if the stage can't find * the import, the build above would have failed first. */ char cs_s[64], ws_s[64]; snprintf(cs_s, sizeof cs_s, "/tmp/wwmoda_%d_%d_cs.s", getpid(), i); snprintf(ws_s, sizeof ws_s, "/tmp/wwmoda_%d_%d_ww.s", getpid(), i); /* Skip byte-id leg for rows that need imports — w6c/w6c_ww * standalone won't resolve `import strconv;`. The runtime * leg above (via `ww build`) IS the byte-id+correctness * gate for these rows; the full bootstrap byte-id (994) is * the corpus check. */ int needs_import = (strstr(rows[i].src, "import ") != NULL); if (!needs_import) { 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(cs_s); unlink(ws_s); } unlink(src); } if (fail) { fprintf(stderr, "%d/%d mod-arr-idx tests failed\n", fail, n); return 1; } printf("modarridx: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); return 0; }