Files
ww/test/wcc/937_forrange_fieldbase_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

342 lines
10 KiB
C

/*
* 937_forrange_fieldbase_run — #70: by-value for-range over a NON-IDENT
* slice/str base (field chain, indexed element, call result). Pre-#70
* the range header stored cgexpr's AX — the DATA POINTER (slice cgexpr
* leaves AX=ptr, BX=len, CX=cap) — into the single bound temp, and the
* per-iteration code had no non-ident base arm, so the bound reload
* doubled as the base: i was compared against the POINTER and the loop
* walked off the end (regex.finish, SEGV on the first non-empty
* charsets; empty slices coincidentally exited on ptr==0 — latent since
* fold 1, BYTE-ID both stages, so the 989 M_ID gate held on
* both-wrong-identical). Now: bound = len, base ptr spilled to its own
* .rgb slot. Deref bases (*p — the #11 family, cgexpr does NOT deliver
* the header) and non-ident ARRAY bases stay LOUD.
*
* row | shape | want
* -------------------+----------------------------------------+-----
* field_base | h.xs ([]i64) value + ptr root | 0
* field_base_24B | p.names ([]str) — the finish() shape | 0
* indexed_base | m[0] ([][]i64 element) | 0
* call_base | mk() ([]i64 call result) | 0
* empty_field | zero header field exits cleanly | 0
* eval_once | base reassigned INSIDE the loop — |
* | header captured once (a per-iteration |
* | re-read of the field would mis-sum) | 0
* reject_deref | *q base stays loud (#11) | err
* reject_arr_field | h.arr ([3]i64 field) stays loud | err
*
* Every K_RUN row also asserts cstage/wwstage asm byte-id. NNN<950,
* self-contained (/tmp, no imports).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
}
#define K_RUN 0 /* build+run both drivers, exit==want, + cs==ww byte-id */
#define K_BUILDERR 1 /* build must FAIL with experr on BOTH drivers (rule 7) */
struct row { const char *label; const char *src; int want;
int kind; const char *experr; };
/* errlog_has — a BUILDERR row must fail WITH its diagnostic; any other
* failure (parse error, crash) is a vacuous reject (940 precedent). */
static int
errlog_has(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return 0;
char buf[8192];
size_t got = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[got] = '\0';
return strstr(buf, needle) != NULL;
}
static const struct row rows[] = {
{ "field_base",
"package main;\n"
"type holder = struct { xs: []i64, name: str };\n"
"fn mk() []i64 = {\n"
" let xs: []i64;\n"
" append(xs, 7);\n"
" append(xs, 8);\n"
" return xs;\n"
"};\n"
"export fn main() i32 = {\n"
" let h: holder = holder { xs = mk(), name = \"h\" };\n"
" let s1: i64 = 0;\n"
" for (let v .. h.xs) { s1 += v; };\n"
" if (s1 != 15) { return 1; };\n"
" let p: *holder = &h;\n"
" let s2: i64 = 0;\n"
" for (let v .. p.xs) { s2 += v; };\n"
" if (s2 != 15) { return 2; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* 24B str-header elements through a ptr field — regex.finish's
* exact shape (the live SEGV repro). */
{ "field_base_24B",
"package main;\n"
"type bag = struct { names: []str, n: i64 };\n"
"export fn main() i32 = {\n"
" let names: []str;\n"
" append(names, \"ab\");\n"
" append(names, \"cde\");\n"
" let b: bag = bag { names = names, n = 2 };\n"
" let p: *bag = &b;\n"
" let total: i64 = 0;\n"
" for (let nm .. p.names) {\n"
" total += (len(nm): i64);\n"
" };\n"
" if (total != 5) { return 1; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* indexed element base — the task-#57 shape, graduated by #70 */
{ "indexed_base",
"package main;\n"
"fn mk() []i64 = {\n"
" let xs: []i64;\n"
" append(xs, 7);\n"
" append(xs, 8);\n"
" return xs;\n"
"};\n"
"export fn main() i32 = {\n"
" let m: [][]i64;\n"
" append(m, mk());\n"
" let s: i64 = 0;\n"
" for (let v .. m[0]) { s += v; };\n"
" if (s != 15) { return 1; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
{ "call_base",
"package main;\n"
"fn mk() []i64 = {\n"
" let xs: []i64;\n"
" append(xs, 7);\n"
" append(xs, 8);\n"
" return xs;\n"
"};\n"
"export fn main() i32 = {\n"
" let s: i64 = 0;\n"
" for (let v .. mk()) { s += v; };\n"
" if (s != 15) { return 1; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* zero header: the pre-#70 code exited only because ptr==0; the
* fixed code must exit because len==0. */
{ "empty_field",
"package main;\n"
"type holder = struct { xs: []i64, name: str };\n"
"export fn main() i32 = {\n"
" let h: holder = holder { name = \"e\", ... };\n"
" for (let v .. h.xs) { return 1; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* the range header (base ptr + len) is captured ONCE at loop
* entry — Hare evaluates the range operand once. A fix that
* re-read the field per iteration would walk b (sum 7+100) and
* never see the reassign-then-original discipline. */
{ "eval_once",
"package main;\n"
"type holder = struct { xs: []i64, n: i64 };\n"
"export fn main() i32 = {\n"
" let a: []i64;\n"
" append(a, 7);\n"
" append(a, 8);\n"
" let b: []i64;\n"
" append(b, 100);\n"
" let h: holder = holder { xs = a, n = 0 };\n"
" let s: i64 = 0;\n"
" for (let v .. h.xs) {\n"
" s += v;\n"
" h.xs = b;\n"
" };\n"
" if (s != 15) { return 1; };\n"
" if (len(h.xs) != 1) { return 2; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* deref base: cgexpr on *p does not deliver the AX/BX/CX header
* (the #11 deref-spine family) — pre-#70 it crashed or mis-summed
* SILENTLY; now loud both stages. */
{ "reject_deref",
"package main;\n"
"export fn main() i32 = {\n"
" let xs: []i64;\n"
" append(xs, 1);\n"
" let q: *[]i64 = &xs;\n"
" let s: i64 = 0;\n"
" for (let v .. *q) { s += v; };\n"
" return (s: i32);\n"
"};\n", 0,
K_BUILDERR, "for-range over a deref base unwired (#11)" },
/* non-ident ARRAY base: no base spill and a different cgexpr
* register shape — loud (rule 7) until a consumer wires it. */
{ "reject_arr_field",
"package main;\n"
"type holder = struct { arr: [3]i64, n: i64 };\n"
"export fn main() i32 = {\n"
" let h: holder = holder { arr = [1, 2, 3], n = 0 };\n"
" let s: i64 = 0;\n"
" for (let v .. h.arr) { s += v; };\n"
" return (s: i32);\n"
"};\n", 0,
K_BUILDERR, "for-range over a non-ident array base unwired (#70)" },
};
/* 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[128], outbin[128], errf[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/tfr_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/tfr_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/tfr_%d_%d", tmpdir, getpid(), i);
snprintf(errf, sizeof errf, "%s/tfr_%d_e_%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 >/dev/null 2>%s",
driver, outbin, src, errf);
int brc = runwait(cmd);
if (r->kind == K_BUILDERR) {
int ok = (brc != 0)
&& (r->experr == NULL || errlog_has(errf, r->experr));
if (!ok)
fprintf(stderr, "row[%s]: %s expected loud builderr "
"\"%s\" (brc=%d)\n", r->label, driver,
r->experr ? r->experr : "", brc);
runwait(rmcmd);
return ok ? 0 : 1;
}
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/tfr_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/tfr_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/tfr_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++) {
if (rows[i].kind == K_BUILDERR)
continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "forrange_fieldbase: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("forrange_fieldbase: %d/%d ok\n", total, total);
return 0;
}