Files
ww/test/wcc/944_array_zeroinit_run.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the
compiler's <stem>.sepwork scratch landed beside the source and was never
cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs
and fabricates phantom test failures + silent harness aborts, and for
in-repo fixture builds leaked .sepwork into the tracked tree.

Each leaking build now writes its source + output inside a per-invocation
tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and
rm -rf's the tmpdir on every exit path -- including fopen-fail and the
expected-fail reject builds (scratch is mkdir'd before the build can fail).
`ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993
byte-id comparison logic is byte-for-byte unchanged.

Two items filed separately (this commit holds the no-Makefile / no-main.c
rail):
- #13: a stale <src>.s byte-id readback (749) silently no-ops since
  separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline.
- #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no
  -o and leak main.sepwork in-tree (bounded, gitignored; own commit).

One concern -- sepwork leak hygiene -- across 228 drivers; uniform
transform applied per-file and two-round reviewed. make test: all 402
passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
2026-06-22 23:29:39 +09:00

223 lines
7.0 KiB
C

/*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
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 tmpdir[96], src[160], outbin[160], errf[160], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/azi_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/azi_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/azi_%d_%d", tmpdir, getpid(), i);
snprintf(errf, sizeof errf, "%s/err", tmpdir);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd,
"timeout 20 %s build -o %s %s >/dev/null 2>%s",
driver, outbin, src, errf);
int brc = runwait(cmd);
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
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;
}