/* * 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 `ww build` + run asserting the exit code (the * cstage-segfault repro now exits cleanly). Single-file rows also carry * a w6c vs w6c_ww `.s` cmp (rule-10 byte-id); the cross-package module * rows gate on runtime exit here and are byte-id-covered cross-package * by 994. The probe table is a test-local exported `[8]u16` (package * wcmodarr — see the #93 retarget note below; it replaced strconv's * private `let left_shift_table`, which separate compilation correctly * hides). */ #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; /* main.ww content */ const char *modname; /* exported-probe-table package, or NULL */ const char *modsrc; /* that package's content, or NULL */ int want_exit; }; /* #93 (915 encapsulation retarget, ken-t): the module rows used to read * strconv's PRIVATE `let left_shift_table` — which separate compilation * CORRECTLY hides (the combined path only "worked" because the private * symbol leaked into one unit). Retargeted to a test-LOCAL package that * EXPORTS its own probe table: the same cross-package `mod.arr[i]` cgen * shape (LEAQ base + element-size stride, #128b), encapsulation-faithful * and sep-linkable. (The deeper checker tighten — loud-reject a cross-pkg * private ref — is tracked as #45; NOT attempted here.) Table prefix is * the same u16 sequence strconv used, so the expected exits are unchanged * (idx0→0, idx2→0x0801&0xFF=1, idx4→0x1006&0xFF=6). */ #define PROBE_MOD \ "export let probe_table: [8]u16 = [0u16, 0x0800u16, 0x0801u16, " \ "0x0803u16, 0x1006u16, 0x1009u16, 0x100Du16, 0x1812u16];\n" static const struct row rows[] = { /* Indexed read on an imported (exported) [N]u16 at idx=0 — load- * width fix: MOVZWQ reads the full u16 = 0x0000 → exit 0. */ { "mod_u16_idx0", "package main;\n" "import wcmodarr;\n" "export fn main() i32 = {\n" " let v: u32 = (wcmodarr.probe_table[0]: u32);\n" " return v: i32;\n" "};\n", "wcmodarr", PROBE_MOD, 0 }, /* Nontrivial idx — pre-fix segfault repro. table[2] = 0x0801; * 0x0801 mod 256 = 1. */ { "mod_u16_idx2", "package main;\n" "import wcmodarr;\n" "export fn main() i32 = {\n" " let v: u32 = (wcmodarr.probe_table[2]: u32);\n" " return v: i32;\n" "};\n", "wcmodarr", PROBE_MOD, 1 }, /* Further idx — pins per-element stride. table[4] = 0x1006; * 0x1006 mod 256 = 6. */ { "mod_u16_idx4", "package main;\n" "import wcmodarr;\n" "export fn main() i32 = {\n" " let v: u32 = (wcmodarr.probe_table[4]: u32);\n" " return v: i32;\n" "};\n", "wcmodarr", PROBE_MOD, 6 }, /* Local-array control: confirms the existing IDENT-base path is * unchanged by the #128b cgindex edit. */ { "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", NULL, NULL, 30 }, /* Same-package (NOT module-qualified) access — pins the in-package * IDENT path stays unchanged; uses a 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", NULL, NULL, 1 }, { NULL, NULL, 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 tmpdir[96]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wwmoda_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); char src[160]; snprintf(src, sizeof src, "%s/main915.ww", tmpdir); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; rmdir(tmpdir); continue; } fputs(rows[i].src, f); fclose(f); char cmd[2048]; /* Exported-probe-table package lives at //.ww * so the sep dep-scan (rooted at the entry's dir) resolves the * `import ;`. */ if (rows[i].modname != NULL) { char moddir[224], modf[320]; snprintf(moddir, sizeof moddir, "%s/%s", tmpdir, rows[i].modname); mkdir(moddir, 0755); snprintf(modf, sizeof modf, "%s/%s.ww", moddir, rows[i].modname); FILE *mf = fopen(modf, "wb"); if (mf == NULL) { fail++; snprintf(cmd, sizeof cmd, "rm -rf %s", tmpdir); (void)runwait(cmd); continue; } fputs(rows[i].modsrc, mf); fclose(mf); } /* #93 sep layout: build via `--sep -o ` (the exported * probe table links across packages under separate compilation); * WW_PKGCACHE pinned under tmpdir. */ char stem[200]; snprintf(stem, sizeof stem, "%s/main915", tmpdir); snprintf(cmd, sizeof cmd, "cd %s && WW_PKGCACHE=%s/pkgc %s/ww build --sep -o %s %s " ">/dev/null 2>&1", tmpdir, tmpdir, bin, stem, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: cstage build failed\n", rows[i].label); fail++; snprintf(cmd, sizeof cmd, "rm -rf %s", tmpdir); (void)runwait(cmd); continue; } int got = runwait(stem); 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++; } /* Byte-id leg — bare w6c vs w6c_ww (the compiler binaries, NOT * the driver, so layout-invariant). Only single-file rows can be * fed standalone; the module rows' runtime exit IS their gate * (+ the 994 bootstrap corpus for the cross-stage byte-id). */ if (rows[i].modname == NULL) { char cs_s[200], ws_s[200]; snprintf(cs_s, sizeof cs_s, "%s/cs.s", tmpdir); snprintf(ws_s, sizeof ws_s, "%s/ww.s", tmpdir); 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++; } else { 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++; } else 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++; } } } snprintf(cmd, sizeof cmd, "rm -rf %s", tmpdir); (void)runwait(cmd); } 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; }