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

249 lines
8.0 KiB
C

/*
* 944_alias_structlit_init_run — #63: let-init from an alias-NAMED
* struct LITERAL. wwstage's cglet N_STRUCTLIT arm resolved the struct
* by a BARE structlookup(c, sname): for an alias `type rep2 = rep`
* only the base `rep` is registered, so the lookup returned nil and
* the field-fill never fired. The nil then split by slot size —
* • <=8B: the small-let scalar default zeroed the slot and DROPPED
* the literal (SILENT wrong: `let r = rep2{id=6}` read 0), and
* • >8B: no fill arm matched -> the cglet "unhandled rhs shape"
* LOUD (task #7/rule-7).
* Same root, two symptoms. The fix routes the arm through
* structlookupchain (the #92/W2 SSoT already used at cgenstmt:1974 /
* :2687), which chases the alias chain to the base struct. cstage
* operates on the resolved Type* via type_chase_named and was always
* correct, so this is ww-only align-UP; cs UNTOUCHED. Oracle: ken
* .ai/ken-63-oracle.md.
*
* The N_IDENT-copy (cgenstmt:2625) and N_CALL-recv (:2511) sibling
* arms are also bare-structlookup but are NOT in scope: the copy
* falls through to a generic path byte-identical with cstage (both
* stages run correct), and the recv is blocked upstream by the
* aggregate-return shape (#272/#277), and the ≤24B return (:1363)
* likewise louds via the scalar-default catch. The >24B sret RETURN
* (:1250) had NO such catch — it was silently wrong (returned an
* uninitialised sret buffer); reviewer-63 routed it through
* structlookupchain in the same commit (see sret_return_alias32).
* The N_IDENT struct-copy (:2625) falls through to a generic path
* both stages run correct (asm divergence is the #81/#65 alias
* field-READ class). See the #63 ledger.
*
* row | shape | want
* -------------------+---------------------------------------+-----
* letinit_typed | type rep2=rep(8B); let r: rep2 = |
* | rep2{id=6}; read r.id (was silent 0) | 6
* letinit_untyped | inferred: let r = rep2{id=6}; r.id | 6
* letlit_alias16 | type row=st(16B); let a: row = |
* | row{a=3,b=4}; read a.a+a.b (was LOUD) | 7
* control_direct | non-alias pt2{...} 16B let-init — the |
* | no-regress control (alias hop is the |
* | trigger), byte-id both stages | 7
* sret_return_alias32| type biga=big(32B); mk() returns |
* | biga{...}; the >24B sret RETURN twin |
* | (:1250, was silent 0) | 10
*
* Every row asserts cstage/wwstage asm byte-id (rule 10). NNN<950,
* self-contained (/tmp, no imports) — rule-14's selfhost-sibling race
* does not apply (941/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[] = {
{ "letinit_typed",
"package main;\n"
"type rep = struct { id: size };\n"
"type rep2 = rep;\n"
"export fn main() i32 = {\n"
" let r: rep2 = rep2{id=6};\n"
" return r.id: i32;\n"
"};\n", 6 },
{ "letinit_untyped",
"package main;\n"
"type rep = struct { id: size };\n"
"type rep2 = rep;\n"
"export fn main() i32 = {\n"
" let r = rep2{id=6};\n"
" return r.id: i32;\n"
"};\n", 6 },
{ "letlit_alias16",
"package main;\n"
"type st = struct { a: size, b: size };\n"
"type row = st;\n"
"export fn main() i32 = {\n"
" let a: row = row{a=3, b=4};\n"
" return (a.a + a.b): i32;\n"
"};\n", 7 },
{ "control_direct",
"package main;\n"
"type pt2 = struct { a: size, b: size };\n"
"export fn main() i32 = {\n"
" let x: pt2 = pt2{a=3, b=4};\n"
" return (x.a + x.b): i32;\n"
"};\n", 7 },
/* reviewer-63 survivor leg: the >24B sret-RETURN twin of the
* let-init site (cgenstmt:1250). sretretsize chases the alias for
* the size gate so the sret arm fires, but the field-fill used a
* bare structlookup(sname) that missed the alias name -> the fill
* was SKIPPED and the callee returned an uninitialised sret buffer
* (ww silent 0; cs correct 10). The impl ledger wrongly declined
* :1250 as "already chases"; routed through structlookupchain. */
{ "sret_return_alias32",
"package main;\n"
"type big = struct { a: size, b: size, c: size, d: size };\n"
"type biga = big;\n"
"fn mk() biga = { return biga{a=1, b=2, c=3, d=4}; };\n"
"export fn main() i32 = {\n"
" let r: biga = mk();\n"
" return (r.a + r.b + r.c + r.d): i32;\n"
"};\n", 10 },
};
/* 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[128], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/asli_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/asli_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/asli_%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/asli_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/asli_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/asli_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_structlit_init: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("alias_structlit_init: %d/%d ok\n", total, total);
return 0;
}