/* * 944_array_zeroinit_run — #84: uninit `[N]T` ARRAY locals were NEVER * zero-filled. The bare-let no-rhs zero-fill (cmd/w6c/cgen.c N_LET else * + selfhost/cmd/wcc/cgenstmt.ww cglet) was gated `sz>8 && !TY_ARRAY`, * so `let a: [3]int;` read whatever the stack held — BOTH stages, * both-wrong-IDENTICAL, byte-id-blind (#263-class): the cs==ww gate * cannot see a shared-garbage read. The fix drops the `!TY_ARRAY` * exclusion symmetrically (arrays zero-fill like every other * composite, Go-zero per user ruling); the zero-fill extent is the * array's chased ABI size (lu->size / chased tinfo.size, NOT the * slot-padded size), so a non-8-multiple array zeroes its exact byte * count. * * GATE-BLIND → the load-bearing net is a RUNTIME zero-read, NOT asm * presence: a clean (fresh) stack frame is already 0, masking the bug. * So every row DIRTIES the stack first (a filler fn writes 165 across * a 512B frame), then declares the uninit array in a sibling frame * that reuses that region, and asserts the read is 0. byte-id won't * catch a regression here — only run-and-check-value will. * * row | shape (after a 165-dirtied stack) | want * -----------------+------------------------------------------+----- * array_elem_0 | let a: [3]int; sum a[0..2] | 0 * narrow_array_0 | let a: [4]u32; sum a[0..3] (esz 4) | 0 * nonmult8_array_0 | let b: [20]u8; sum b[0..19] (ABI 20 != | * | slot 24 — extent-source guard) | 0 * array_2d_0 | let m: [2][2]int; sum all 4 | 0 * control_initd | let c: [3]int = [7,8,9]; sum (no-regress)| 24 * * Each row also asserts cstage/wwstage .s byte-id (rule 10 — the fix * adds the SAME insns to both). NNN<950, self-contained (/tmp, no * imports) — rule-14's selfhost-sibling race does not apply (941/944 * precedent). #84 array-only; the tagged/plain-*T reject-set is #113. */ #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; } 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), cb = fgetc(fb); if (ca != cb) { rc = -1; break; } if (ca == EOF) break; } fclose(fa); fclose(fb); return rc; } struct row { const char *label; const char *src; int want; }; /* Every src dirties the stack via dirty() (a 512B [64]int filled with * 165) before the reader's uninit array lands on that region. */ #define DIRTY \ "fn dirty() int = {\n" \ " let j: [64]int;\n" \ " for (let i: int = 0; i < 64; i += 1) { j[i] = 165; };\n" \ " return j[0];\n" \ "};\n" static const struct row rows[] = { { "array_elem_0", "package main;\n" DIRTY "fn rd() i32 = { let a: [3]int; return (a[0]+a[1]+a[2]): i32; };\n" "export fn main() i32 = { dirty(); return rd(); };\n", 0 }, { "narrow_array_0", "package main;\n" DIRTY "fn rd() i32 = { let a: [4]u32;\n" " return (a[0]+a[1]+a[2]+a[3]): i32; };\n" "export fn main() i32 = { dirty(); return rd(); };\n", 0 }, { "nonmult8_array_0", "package main;\n" DIRTY "fn rd() i32 = { let b: [20]u8; let s: i32 = 0;\n" " for (let i: int = 0; i < 20; i += 1) { s += b[i]: i32; };\n" " return s; };\n" "export fn main() i32 = { dirty(); return rd(); };\n", 0 }, { "array_2d_0", "package main;\n" DIRTY "fn rd() i32 = { let m: [2][2]int;\n" " return (m[0][0]+m[0][1]+m[1][0]+m[1][1]): i32; };\n" "export fn main() i32 = { dirty(); return rd(); };\n", 0 }, /* no-regress control: an INITIALIZED array keeps its values. */ { "control_initd", "package main;\n" DIRTY "fn rd() i32 = { let c: [3]int = [7, 8, 9];\n" " return (c[0]+c[1]+c[2]): i32; };\n" "export fn main() i32 = { dirty(); return rd(); };\n", 24 }, }; /* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */ static int run_driver(const char *driver, const struct row *r, int i) { char src[96], tmpdir[96], errf[96], cmd[1024]; snprintf(src, sizeof src, "/tmp/azi_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/azi_%d_d_%d", getpid(), i); snprintf(errf, sizeof errf, "/tmp/azi_%d_e_%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 && timeout 20 %s build %s >/dev/null 2>%s", tmpdir, driver, src, errf); int brc = runwait(cmd); if (brc != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); unlink(src); unlink(errf); rmdir(tmpdir); return -1; } const char *base = strrchr(src, '/'); base = base ? base + 1 : src; char outbin[256]; 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); unlink(errf); rmdir(tmpdir); if (got != r->want) { fprintf(stderr, "row[%s]: %s exit %d, want %d\n", r->label, driver, got, r->want); return 1; } return 0; } /* cs==ww .s byte-id (rule 10). */ static int asm_byte_identical(const char *bin, const struct row *r, int i) { char src[96], cs[96], ws[96], cmd[1024]; snprintf(src, sizeof src, "/tmp/azi_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/azi_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/azi_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; } int rc = slurp_eq(cs, ws); 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[2080]; 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[2120], wdrv[2120]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); int n = (int)(sizeof rows / sizeof rows[0]); int total = 0, fail = 0; for (int i = 0; i < n; i++) { total++; if (run_driver(cdrv, &rows[i], i) != 0) fail++; } if (access(wdrv, X_OK) == 0) { for (int i = 0; i < n; i++) { total++; if (run_driver(wdrv, &rows[i], i) != 0) fail++; } for (int i = 0; i < n; i++) { total++; if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; } } if (fail) { fprintf(stderr, "array_zeroinit: %d/%d checks failed\n", fail, total); return 1; } printf("array_zeroinit: %d/%d ok\n", total, total); return 0; }