Files
ww/test/wcc/820_arr_zero_vs_infer.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

341 lines
11 KiB
C

/*
* 820_arr_zero_vs_infer — an EXPLICIT zero/short fixed-size array over-filled
* by its initializer (`[0]int = [1,2]`, `[2]int = [1,2,3]`) is a LOUD length-
* mismatch on BOTH stages; an INFER `[_]` still infers its length from the
* initializer (task #9; ken oracle .ai/ken-9-oracle.md, rob spec
* .ai/rob-9-spec.md).
*
* The bug: post-resolve_type, both `[0]` and `[_]` collapse to alen==0 — the
* Type loses the distinction. The #71 over-fill diagnostic was suppressed for
* alen==0, so `[0]int = [1,2]` slipped past and each stage misbehaved
* DIFFERENTLY (byte-id-blind):
* - cstage silently RESIZED [0]→[2] (exit 2), or for the `def`/`let` cases
* resized too.
* - wwstage kept [0] and OOB-read / SEGFAULTed (exit 8 / 139), or resized
* the local (exit 2) — inconsistent across local vs module.
*
* The fix (one both-stage CHECKER commit): the AST RETAINS the distinction the
* Type loses — an infer `[_]` leaves the N_TARRAY length-child NULL, an
* explicit `[N]` (incl `[0]`) carries an N_INTLIT. cstage gates the four
* infer-resize / no-init sites on is_infer_arr(<type-AST>) and drops the
* `alen > 0` exemption at the over-fill check; wwstage's shared count-gate
* checkarrlitfits fires whenever `arrtn.rhs != nil`. So an explicit `[N]=[init]`
* with count>N louds in EVERY context, INCLUDING N==0, before codegen.
*
* Mutation-sanity: every neg `[0]` row BUILT+RAN pre-fix (silent resize / OOB);
* it must now FAIL to build (the rows assert build-FAIL, which only holds
* post-fix). def_two_overfill (`[2]=[1,2,3]`) is the #71 N>0 regression guard.
*
* neg row | shape | gate
* -------------------+------------------------------------+----------
* def_zero_overfill | def X:[0]int=[1,2] | build FAIL
* let_zero_overfill | let X:[0]int=[1,2] (module) | build FAIL
* local_zero_overfill| local [0]int=[1,2] | build FAIL
* def_two_overfill | def X:[2]int=[1,2,3] (#71 guard) | build FAIL
* str_zero_overfill | def X:[0]str=["a"] (elem-agnostic) | build FAIL
*
* pos row | shape | want
* ------------+--------------------------------+------
* infer_ctl | def X:[_]int=[10,20]; X[1] | 20 (#11 infer works)
* empty_zero | let X:[0]int=[]; return 7 | 7 (legit empty array)
* infer_len | let X:[_]int=[1,2,3]; X.len | 3 (infer unaffected)
*/
#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;
}
/* beid — include this row in the cstage-vs-wwstage byte-id sweep. Both the
* empty_zero global (`let X:[0]int=[]`) and the empty_zero_local row now
* converge byte-identically (beid=1): #15 closed the empty-`[0]T` cgen
* divergence — wwstage's spurious zero-width `DATAW main.X(SB),""` (cstage
* omits a zero-byte global) and its over-allocated `$16` local frame (cstage
* `$0` — a zero-length array reserves no slot) are both gated on the array
* being non-empty. See .ai/rob-15-spec.md: cgen.ww emitletdataw gates the
* array DATAW on `sz > 0`; cgenutil.ww slotsize returns 0 for a TY_ARRAY of
* alen==0. Both forms still run 7. */
struct row { const char *label; const char *src; int want; int beid; };
static const struct row rows[] = {
/* infer_ctl — `[_]` still infers length from the initializer (#11). */
{ "infer_ctl",
"package main;\n"
"def X: [_]int = [10, 20];\n"
"export fn main() i32 = {\n"
"\treturn X[1]: i32;\n"
"};\n",
20, 1 },
/* empty_zero — a real zero-length array (`[0]int = []`) stays VALID.
* beid=1: #15 closed the empty-array-global DATAW divergence. */
{ "empty_zero",
"package main;\n"
"let X: [0]int = [];\n"
"export fn main() i32 = {\n"
"\treturn 7;\n"
"};\n",
7, 1 },
/* empty_zero_local — a LOCAL zero-length array (`[0]int = []`) reserves
* no frame slot (#15: cstage `$0`, wwstage was `$16`). beid=1. */
{ "empty_zero_local",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [0]int = [];\n"
"\treturn 7;\n"
"};\n",
7, 1 },
/* void_local — a zero-SIZE (not zero-length) local: `done = void` sizes
* 0 via slotsize, but cstage cglet defaults a non-composite local to an
* 8B slot. #15 dropped localreserve's sub-8 floor, which had masked this
* — letslotsize now floors a void local to 8 (cstage parity). beid=1: a
* regression here (void slot 0) collides with the spilled param. */
{ "void_local",
"package main;\n"
"type done = void;\n"
"export fn main() i32 = {\n"
"\tlet d: done;\n"
"\tlet a: i32 = 7;\n"
"\treturn a;\n"
"};\n",
7, 1 },
/* infer_len — `[_]` infer is unaffected, `.len` reads the real count. */
{ "infer_len",
"package main;\n"
"let X: [_]int = [1, 2, 3];\n"
"export fn main() i32 = {\n"
"\treturn X.len: i32;\n"
"};\n",
3, 1 },
};
/* An EXPLICIT `[N]int = [init]` with init-count > N — both stages must FAIL
* the build (loud over-fill diagnostic, not silent resize / OOB). */
static const char *neg[] = {
/* def_zero_overfill */
"package main;\n"
"def X: [0]int = [1, 2];\n"
"export fn main() i32 = {\n"
"\treturn X[1]: i32;\n"
"};\n",
/* let_zero_overfill */
"package main;\n"
"let X: [0]int = [1, 2];\n"
"export fn main() i32 = {\n"
"\treturn X[1]: i32;\n"
"};\n",
/* local_zero_overfill */
"package main;\n"
"export fn main() i32 = {\n"
"\tlet X: [0]int = [1, 2];\n"
"\treturn X[1]: i32;\n"
"};\n",
/* def_two_overfill — the #71 N>0 regression guard, must stay loud */
"package main;\n"
"def X: [2]int = [1, 2, 3];\n"
"export fn main() i32 = {\n"
"\treturn X[1]: i32;\n"
"};\n",
/* str_zero_overfill — element-type-agnostic */
"package main;\n"
"def X: [0]str = [\"a\"];\n"
"export fn main() i32 = {\n"
"\treturn 0;\n"
"};\n",
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], 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(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, "%s build -o %s %s 2>/dev/null",
driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
/* build_should_fail — the over-fill must error on `driver`; returns 0 when the
* build correctly FAILS, non-zero when it wrongly succeeded. */
static int
build_should_fail(const char *driver, const char *src, int i)
{
char tmpdir[64], s[128], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/azin_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(s, sizeof s, "%s/azin_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/out", tmpdir);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(s, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, s);
int rc = runwait(cmd);
runwait(rmcmd);
return rc == 0 ? -1 : 0; /* build must NOT succeed */
}
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */
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/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;
}
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 nn = (int)(sizeof neg / sizeof neg[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, "arr_zero_vs_infer: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != rows[i].want) {
fprintf(stderr,
"arr_zero_vs_infer[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
for (int i = 0; i < nn; i++) {
total++;
if (build_should_fail(drivers[d].path, neg[i],
100 + i) != 0) {
fprintf(stderr,
"arr_zero_vs_infer[%s][neg%d]: built ok, "
"expected a loud error\n",
drivers[d].name, i);
fail++;
}
}
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
if (!rows[i].beid)
continue; /* see `beid` — out-of-#9 divergence */
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"arr_zero_vs_infer: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("arr_zero_vs_infer: %d/%d ok\n", total, total);
return 0;
}