Files
ww/test/wcc/944_alias_global_decl_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

452 lines
14 KiB
C

/*
* 944_alias_global_decl_run — g-fold (#77 + #78 fused): alias-typed
* GLOBAL declarations, both stages. The decl/emit dispatch was
* alias-blind on both sides:
*
* cs (#78): the let_* helper family (let_emit_size + the five kind
* predicates, cgen.c) single-peeled — a 2-level alias chain (or ONE
* user alias over a named struct) left the type TY_NAMED, so the
* global was never registered, no DATA was emitted, and reads fell
* to the frame-local path at offset 0: silent saved-BP reads (garr2
* exit-wrong, gst1 SEGV, gstr2/gsl2 silent-wrong). The routed-to
* DATA emitters re-peeled at entry and return-0'd into the silent
* skip (condition-3 members, enrollment ruling in
* .ai/rob-gfold-spec.md).
*
* ww (#77): emitletdataw's array gates + letpreintern's array leg
* keyed on the UNCHASED N_TARRAY tnode — an alias-typed global's
* N_TNAME matched no arm and the skip-policy ate the decl: loud
* `w6l: undefined reference to main.g` on every alias-global ARRAY
* row. Fix: ONE tichase at the dispatch entry (dti), array gates +
* emitarraydata read it.
*
* THIS TABLE IS THE PERMANENT GUARD, not scaffolding: the global decl
* path is lint-invisible to the future peellint — the ww side was a
* NO-PEEL consumer (zero `.under` tokens; nothing for a peel-shape
* lint to flag), and the cs helpers now spell the one chased accessor.
* Only a runtime+byte-id row pins this behavior.
*
* Train trajectory (two commits, one gated train; per-leg states were
* probe-verified, banked /tmp/implG_probes.md):
* pre-G1: garr2 cs EXIT-WRONG / gst1 cs SEGV / gstr2+gsl2+gstlit cs
* silent-wrong / gsc2+gflt2 cs loud / ww array rows link-ERR
* post-G1 (cs half): every cs row runs 0; ww array rows STILL
* link-ERR (loud, documented)
* post-G2 (ww half): every row 0/0 byte-id — this file's assertion.
*
* Probe-OUT, documented NOT pinned (failed at a DIFFERENT site, filed):
* named-tuple global init — cs checker loud-reject, ww accepts (#86)
* plain tagged global — cs silent-wrong, ww loud-reject (#87, not
* alias-family; control row fails)
*
* row | shape | want
* -------------+----------------------------------------------+-----
* ctl_plain | control: plain [4]int global r/w | 0
* garr1_read | alias arr 1-lvl, read first+LAST | 0
* garr1_wr | alias arr 1-lvl, write+read | 0
* garr_ord | alias arr, type decl AFTER let | 0
* garr2_read | alias arr 2-LVL read (#78 saved-BP row) | 0
* garr2_ordwr | alias arr 2-lvl, order-permuted, w+r | 0
* garr_u32 | alias [4]u32 w+r LAST (narrow esz anti-luck) | 0
* gsc2 | alias int 2-lvl r/w | 0
* gsc2_ord | alias int 2-lvl read, decl-after-let | 0
* gstr2 | alias str 2-lvl, len readback | 0
* gflt2 | alias f64 2-lvl | 0
* gflt2_f32 | alias f32 2-lvl (load-width pin) | 0
* gst1 | alias of named struct, field w/r (SEGV row) | 0
* gst2 | alias-of-alias named struct | 0
* gstlit | alias struct, STRUCTLIT init | 0
* gsl2 | alias slice 2-lvl, literal init + idx read | 0
* gstrarr1 | alias [2]str 1-lvl (letpreintern label leg) | 0
* gstrarr2 | alias [2]str 2-LVL | 0
* gdef_a2 | DEF: 2-lvl alias array, index read | 0
* gdef_st | DEF: alias struct, structlit + field read | 0
* gdef_f2 | DEF: 2-lvl alias f64 | 0
* hold_slice1 | alias global SLICE 1-lvl (pre-fold green) | 0
* hold_elem | alias ELEMENT type [2]row (batch-1 win) | 0
*
* Values exceed 255 (no little-endian prefix-luck) and readbacks
* assert the LAST element. Every row runs BOTH drivers and asserts
* cstage/wwstage asm byte-identity. NNN<950, self-contained /tmp
* sources, no imports (944 precedent).
*/
#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; };
static const struct row rows[] = {
{ "ctl_plain",
"package main;\n"
"let g: [4]int = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" if (g[3] != 1004) { return 1; };\n"
" g[3] = 9999;\n"
" if (g[3] != 9999) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr1_read",
"package main;\n"
"type arr = [4]int;\n"
"let g: arr = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" if (g[0] != 1001) { return 1; };\n"
" if (g[3] != 1004) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr1_wr",
"package main;\n"
"type arr = [4]int;\n"
"let g: arr = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" g[3] = 8888;\n"
" if (g[3] != 8888) { return 1; };\n"
" if (g[2] != 1003) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr_ord",
"package main;\n"
"let g: arr = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"type arr = [4]int;\n"
"export fn main() i32 = {\n"
" if (g[3] != 1004) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* #78 headline: pre-fix cs emitted NO DATA, never referenced
* main.g, and resolved g frame-local at offset 0 — LEAQ (BP) read
* the saved-BP word, exiting silently wrong. */
{ "garr2_read",
"package main;\n"
"type arr = [4]int;\n"
"type arr2 = arr;\n"
"let g: arr2 = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" if (g[0] != 1001) { return 1; };\n"
" if (g[3] != 1004) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr2_ordwr",
"package main;\n"
"let g: arr2 = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"type arr2 = arr;\n"
"type arr = [4]int;\n"
"export fn main() i32 = {\n"
" if (g[3] != 1004) { return 1; };\n"
" g[1] = 7777;\n"
" if (g[1] != 7777) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr_u32",
"package main;\n"
"type au = [4]u32;\n"
"let g: au = [1001: u32, 1002: u32, 1003: u32, 1004: u32];\n"
"export fn main() i32 = {\n"
" if (g[3] != 1004) { return 1; };\n"
" g[3] = 8888;\n"
" if (g[3] != 8888) { return 2; };\n"
" if (g[2] != 1003) { return 3; };\n"
" return 0;\n"
"};\n", 0 },
{ "gsc2",
"package main;\n"
"type myint = int;\n"
"type myint2 = myint;\n"
"let g: myint2 = 4242;\n"
"export fn main() i32 = {\n"
" if (g != 4242) { return 1; };\n"
" g = 9999;\n"
" if (g != 9999) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "gsc2_ord",
"package main;\n"
"let g: myint2 = 4242;\n"
"type myint2 = myint;\n"
"type myint = int;\n"
"export fn main() i32 = {\n"
" if (g != 4242) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gstr2",
"package main;\n"
"type ms = str;\n"
"type ms2 = ms;\n"
"let g: ms2 = \"hello\";\n"
"export fn main() i32 = {\n"
" if (g.len != 5) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gflt2",
"package main;\n"
"type mf = f64;\n"
"type mf2 = mf;\n"
"let g: mf2 = 1.5;\n"
"export fn main() i32 = {\n"
" if (g != 1.5) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gflt2_f32",
"package main;\n"
"type mg = f32;\n"
"type mg2 = mg;\n"
"let g: mg2 = 1.5f32;\n"
"export fn main() i32 = {\n"
" if (g != 1.5f32) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* ONE user alias level sufficed to SEGV cs pre-fix: the named
* struct already costs the single peel, so `pt1 = pt` is two
* TY_NAMED layers at the decl. */
{ "gst1",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"type pt1 = pt;\n"
"let g: pt1;\n"
"export fn main() i32 = {\n"
" g.y = 2002;\n"
" if (g.y != 2002) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gst2",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"type pt1 = pt;\n"
"type pt2 = pt1;\n"
"let g: pt2;\n"
"export fn main() i32 = {\n"
" g.y = 2002;\n"
" if (g.y != 2002) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gstlit",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"type pt1 = pt;\n"
"let g: pt1 = pt { x = 1001, y = 2002 };\n"
"export fn main() i32 = {\n"
" if (g.y != 2002) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gsl2",
"package main;\n"
"type sl = []int;\n"
"type sl2 = sl;\n"
"let g: sl2 = [1001: int, 1002: int, 1003: int];\n"
"export fn main() i32 = {\n"
" if (g.len != 3) { return 1; };\n"
" if (g[2] != 1003) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
/* [N]str rows also pin letpreintern's chased array-leg gate: a
* missed pre-intern desyncs _S_ label order vs cstage (byte-id
* would catch it as a label-number diff). */
{ "gstrarr1",
"package main;\n"
"type sa = [2]str;\n"
"let g: sa = [\"hello\", \"worlds!\"];\n"
"export fn main() i32 = {\n"
" if (g[1].len != 7) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gstrarr2",
"package main;\n"
"type sa = [2]str;\n"
"type sa2 = sa;\n"
"let g: sa2 = [\"hello\", \"worlds!\"];\n"
"export fn main() i32 = {\n"
" if (g[1].len != 7) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gdef_a2",
"package main;\n"
"type arr = [4]int;\n"
"type arr2 = arr;\n"
"def G: arr2 = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" if (G[3] != 1004) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gdef_st",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"type pt1 = pt;\n"
"def G: pt1 = pt { x = 1001, y = 2002 };\n"
"export fn main() i32 = {\n"
" if (G.y != 2002) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gdef_f2",
"package main;\n"
"type mf = f64;\n"
"type mf2 = mf;\n"
"def G: mf2 = 1.5;\n"
"export fn main() i32 = {\n"
" if (G != 1.5) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* No-regression holds: alias global SLICE (green before this
* fold) and alias ELEMENT type (batch-1 win). */
{ "hold_slice1",
"package main;\n"
"type sl = []int;\n"
"let g: sl = [1001: int, 1002: int, 1003: int];\n"
"export fn main() i32 = {\n"
" if (g.len != 3) { return 1; };\n"
" if (g[2] != 1003) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "hold_elem",
"package main;\n"
"type row = [3]int;\n"
"let g: [2]row = [[1010: int, 1020: int, 1030: int], [1040: int, 1050: int, 1060: int]];\n"
"export fn main() i32 = {\n"
" if (g[0][0] != 1010) { return 1; };\n"
" if (g[1][2] != 1060) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[96], src[160], outbin[160], errf[160], rmcmd[176], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/agd_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/agd_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/agd_%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;
}
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/agd_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/agd_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/agd_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, "alias_global_decl: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("alias_global_decl: %d/%d ok\n", total, total);
return 0;
}