/* * 688_global_arr_elem_field — a module-GLOBAL indexed struct-element * `.field` READ (`g[i].field`, g at module scope) lowers the field * offset, not just the element stride. The READ twin of #11 (which * fixed the global `g[i] = v` WRITE) and the #15 sibling. * * Pre-fix (#21): the `arr[i].field` N_DOT branch in both stages was * gated on a LOCAL lookup (cstage localfind != 0, wwstage * localfindnode != nil). A module-global base missed it and fell * through — cstage to a generic index-load that drops f->offset * (reads element[i] at offset 0, so g[0].b returned a's value), and * wwstage to the module-qualified fallback (garbage — no g load). * Byte-id-divergent silent miscompile. * * The fix (BOTH stages, converged byte-identical): resolve the global * the same way the N_INDEX arm does (cstage let_islet || def_isarraydef; * wwstage letvartnode || defvartnode) and dispatch the base load by * shape — array -> LEAQ name(SB) (the symbol IS the storage), * slice/ptr -> MOVQ name(SB) (the symbol's first word IS the .ptr) — * then load the field at f->offset, exactly as the local arm does. * esz = element stride and f->offset both come from the type table. * * Coverage: * arr_a0/a1 | [2]pt struct array, g[0].a / g[1].a — control, * | offset 0. RUNTIME + byte-id. * arr_b0/b1 | [2]pt, g[0].b / g[1].b — THE bug (offset 8, the * | reads that returned a's value pre-fix). RUNTIME. * rec_* | [2]rec { tag:u8, x:i32, y:i64 } — non-8-aligned * | fields (x@4, y@8) + a u8 sub-word leaf, to stress * | f->offset and the load-width path. RUNTIME. * slice_x1 | [2]rec backing + `let g: []rec = arr;`, g[1].x * | read. Exercises the slice-base MOVQ-deref arm * | (MOVQ g(SB),BX loads .ptr) — the asm is byte-id and * | correct, but a slice-of-struct module global does * | not data-emit a symbol yet (a separate, pre-existing * | data-emission gap, sibling of #10/#20), so it cannot * | LINK/run. Asserted byte-id ONLY; filed separately. * * Why this was never caught: 680/681 cover only LOCAL `[N]struct` * bases (BP-relative). The global base routes through name(SB) and was * a distinct, untested arm. * * Cstage and wwstage each on every runtime fixture; wwstage gated on * access(X_OK). All fixtures additionally asm-byte-id checked. */ #include #include #include #include #include #include /* want == BYTEID_ONLY: the row cannot LINK/run (a slice-of-struct * module global does not data-emit yet — separate gap), so only the * cstage/wwstage asm-byte-id is asserted, never an exit value. */ #define BYTEID_ONLY (-2147483647 - 1) 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; }; static const struct row rows[] = { /* [2]pt, .a field at offset 0 — control. */ { "arr_a0", "type pt = struct { a: i64, b: i64 };\n" "let g: [2]pt = [pt { a = 1, b = 2 }, pt { a = 3, b = 4 }];\n" "export fn main() i32 = { return g[0].a: i32; };\n", 1 }, { "arr_a1", "type pt = struct { a: i64, b: i64 };\n" "let g: [2]pt = [pt { a = 1, b = 2 }, pt { a = 3, b = 4 }];\n" "export fn main() i32 = { return g[1].a: i32; };\n", 3 }, /* [2]pt, .b at offset 8 — THE bug: pre-fix g[i].b returned a's * value (cstage) or garbage (wwstage). */ { "arr_b0", "type pt = struct { a: i64, b: i64 };\n" "let g: [2]pt = [pt { a = 1, b = 2 }, pt { a = 3, b = 4 }];\n" "export fn main() i32 = { return g[0].b: i32; };\n", 2 }, { "arr_b1", "type pt = struct { a: i64, b: i64 };\n" "let g: [2]pt = [pt { a = 1, b = 2 }, pt { a = 3, b = 4 }];\n" "export fn main() i32 = { return g[1].b: i32; };\n", 4 }, /* [2]rec, non-8-aligned i32 field (x@4) on the second element — * stresses f->offset combined with the element stride. */ { "rec_x1", "type rec = struct { tag: u8, x: i32, y: i64 };\n" "let g: [2]rec = [rec { tag = 1u8, x = 100, y = 11 }," " rec { tag = 2u8, x = 200, y = 22 }];\n" "export fn main() i32 = { return g[1].x: i32; };\n", 200 }, /* [2]rec, i64 field (y@8) on the second element. */ { "rec_y1", "type rec = struct { tag: u8, x: i32, y: i64 };\n" "let g: [2]rec = [rec { tag = 1u8, x = 100, y = 11 }," " rec { tag = 2u8, x = 200, y = 22 }];\n" "export fn main() i32 = { return g[1].y: i32; };\n", 22 }, /* [2]rec, u8 leaf (tag@0) — sub-word zero-extend load on a * global element base. */ { "rec_tag1", "type rec = struct { tag: u8, x: i32, y: i64 };\n" "let g: [2]rec = [rec { tag = 1u8, x = 100, y = 11 }," " rec { tag = 2u8, x = 200, y = 22 }];\n" "export fn main() i32 = { return g[1].tag: i32; };\n", 2 }, /* slice-base MOVQ-deref arm: `let g: []rec = arr;`, g[1].b read. * Byte-id only — a slice-of-struct module global does not * data-emit yet (separate gap), so it cannot link/run. The .s * holds MOVQ g(SB),BX (the .ptr load) + the correct f->offset on * both stages. */ { "slice_x1", "type rec = struct { tag: u8, x: i32, y: i64 };\n" "let arr: [2]rec = [rec { tag = 1u8, x = 100, y = 11 }," " rec { tag = 2u8, x = 200, y = 22 }];\n" "let g: []rec = arr;\n" "export fn main() i32 = { return g[1].x: i32; };\n", BYTEID_ONLY }, }; static int run_driver(const char *driver, const struct row *r, int i) { char src[64], tmpdir[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/gae_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/gae_%d_d_%d", getpid(), i); FILE *f = fopen(src, "wb"); if (!f) return -1; fputs(r->src, f); fclose(f); mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", tmpdir, driver, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); unlink(src); rmdir(tmpdir); return -1; } const char *base = strrchr(src, '/'); base = base ? base + 1 : src; char outbin[128]; snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); char *dot = strrchr(outbin, '.'); if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; int got = runwait(outbin); unlink(src); unlink(outbin); rmdir(tmpdir); return got; } /* asm_byte_identical — generate .s via cstage's w6c and wwstage's * w6c_ww and diff. Pins rule-10 convergence: pre-fix the global-base * read diverged (cstage dropped f->offset, wwstage emitted no g load). */ static int asm_byte_identical(const char *bin, const struct row *r, int i) { char src[64], cs[64], ws[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/gae_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/gae_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/gae_asm_%d_%d_w.s", getpid(), i); FILE *f = fopen(src, "wb"); if (!f) return -1; fputs(r->src, f); fclose(f); snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c errored\n", r->label); unlink(src); return -1; } snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null", bin, ws, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label); unlink(src); unlink(cs); return -1; } FILE *fc = fopen(cs, "rb"); FILE *fw = fopen(ws, "rb"); int rc = 0; if (!fc || !fw) { rc = -1; } else { for (;;) { int a = fgetc(fc); int b = fgetc(fw); if (a != b) { rc = -1; break; } if (a == EOF) break; } } if (fc) fclose(fc); if (fw) fclose(fw); if (rc != 0) fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n", r->label); unlink(src); unlink(cs); unlink(ws); 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 cdrv[1024]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); char wdrv[1024]; snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); struct { const char *name; const char *path; int gated_on_existence; } drivers[] = { { "cstage", cdrv, 0 }, { "wwstage", wdrv, 1 }, { NULL, NULL, 0 }, }; int n = (int)(sizeof rows / sizeof rows[0]); int total = 0, fail = 0; for (int d = 0; drivers[d].name; d++) { if (drivers[d].gated_on_existence && access(drivers[d].path, X_OK) != 0) { fprintf(stderr, "global_arr_elem_field: skip %s (no %s)\n", drivers[d].name, drivers[d].path); continue; } for (int i = 0; i < n; i++) { if (rows[i].want == BYTEID_ONLY) continue; /* byte-id only below */ int got = run_driver(drivers[d].path, &rows[i], i); total++; if (got != rows[i].want) { fprintf(stderr, "global_arr_elem_field[%s][%s]: exit=%d want=%d\n", drivers[d].name, rows[i].label, got, rows[i].want); fail++; } } } if (access(wdrv, X_OK) == 0) { for (int i = 0; i < n; i++) { total++; if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; } } if (fail) { fprintf(stderr, "global_arr_elem_field: %d/%d fixtures failed\n", fail, total); return 1; } printf("global_arr_elem_field: %d/%d ok\n", total, total); return 0; }