wcc/ww: alloc of an alias struct literal chases the alias for size and fill

alloc(alias{...}) keyed the size and field-fill off the syntactic
alias name — it under-allocated and emitted zero field stores. Chase
the alias via structlookupchain to the resolved struct (depth-2
chains verified). The scalar else-branch keeps its pre-existing
benign cs!=ww divergence, surfaced here and deferred as task #57
(site note at the arm). Review item #26.
This commit is contained in:
2026-06-13 01:49:07 +09:00
parent 36e17b58f2
commit faf1a2908b
5 changed files with 202 additions and 24 deletions

View File

@@ -256,6 +256,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_arrlit_tail_zero_run \
$(BIN)/test_defdim_slice_run \
$(BIN)/test_trystr_run \
$(BIN)/test_allocalias_run \
$(BIN)/test_gunsigned_run \
$(BIN)/test_taggedidx_run \
$(BIN)/test_fnptrcollide_run \
@@ -746,6 +747,16 @@ $(BIN)/test_trystr_run: test/wcc/989_trystr_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_allocalias_run (#26): alloc(T{...}) for an alias T sizes + field-fills
# from the alias-chased struct layout, not a name-keyed lookup of the literal
# head. Builds+runs on BOTH driver twins (rule-10).
$(BIN)/test_allocalias_run: test/wcc/989_allocalias_run.c \
$(BIN)/ww $(BIN)/ww_ww \
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_gunsigned_run (F7-c5, #25): a module-global unsigned ident on the
# divide/shift/relational path must pick the unsigned opcode. Builds+runs on
# BOTH driver twins (rule-10). CLASS-M — see the test header.

View File

@@ -28927,14 +28927,14 @@ fn cgalloc(c: *cgen, n: *node) void = {
let sz: i32 = 8;
let si: *structinfo = nil;
if (v.kind == nkind.N_STRUCTLIT) {
let trefn: *node = v.lhs;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (trefn != nil) {
if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; }
else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; };
};
si = structlookup(c, sname);
// #26: chase the alias chain on the literal's type ref so an
// alias head (`type pt = point; alloc(pt{...})`) resolves to
// the underlying struct's layout — name-keyed structlookup on
// the syntactic head missed it (under-alloc $8 + zero field
// stores). Mirrors cstage cgen.c:8223-8240 (type_default chases
// named before sizing/walking fields); same alias-chase helper
// the #22 sites use.
si = structlookupchain(c, v.lhs);
if (si != nil) { sz = si.totsize; };
};
let okl: str = mklabel(c, "alloc_ok");
@@ -29010,6 +29010,10 @@ fn cgalloc(c: *cgen, n: *node) void = {
};
};
} else {
// #57 (retained cs!=ww, deferred): scalar/ptr alloc keeps the
// default-8 size + MOVQ store; cstage sizes the scalar exactly
// ($1/MOVB for u8) and the latent alloc(str|slice) wants 24B not 8.
// Byte-id-visible, runtime-benign; not folded into the #26 arm.
cgexpr(c, v);
emitline("\tMOVQ\t(SP), BX\n");
let sop: str = "MOVQ";

View File

@@ -5707,14 +5707,14 @@ fn cgalloc(c: *cgen, n: *node) void = {
let sz: i32 = 8;
let si: *structinfo = nil;
if (v.kind == nkind.N_STRUCTLIT) {
let trefn: *node = v.lhs;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (trefn != nil) {
if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; }
else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; };
};
si = structlookup(c, sname);
// #26: chase the alias chain on the literal's type ref so an
// alias head (`type pt = point; alloc(pt{...})`) resolves to
// the underlying struct's layout — name-keyed structlookup on
// the syntactic head missed it (under-alloc $8 + zero field
// stores). Mirrors cstage cgen.c:8223-8240 (type_default chases
// named before sizing/walking fields); same alias-chase helper
// the #22 sites use.
si = structlookupchain(c, v.lhs);
if (si != nil) { sz = si.totsize; };
};
let okl: str = mklabel(c, "alloc_ok");
@@ -5790,6 +5790,10 @@ fn cgalloc(c: *cgen, n: *node) void = {
};
};
} else {
// #57 (retained cs!=ww, deferred): scalar/ptr alloc keeps the
// default-8 size + MOVQ store; cstage sizes the scalar exactly
// ($1/MOVB for u8) and the latent alloc(str|slice) wants 24B not 8.
// Byte-id-visible, runtime-benign; not folded into the #26 arm.
cgexpr(c, v);
emitline("\tMOVQ\t(SP), BX\n");
let sop: str = "MOVQ";

View File

@@ -28927,14 +28927,14 @@ fn cgalloc(c: *cgen, n: *node) void = {
let sz: i32 = 8;
let si: *structinfo = nil;
if (v.kind == nkind.N_STRUCTLIT) {
let trefn: *node = v.lhs;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (trefn != nil) {
if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; }
else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; };
};
si = structlookup(c, sname);
// #26: chase the alias chain on the literal's type ref so an
// alias head (`type pt = point; alloc(pt{...})`) resolves to
// the underlying struct's layout — name-keyed structlookup on
// the syntactic head missed it (under-alloc $8 + zero field
// stores). Mirrors cstage cgen.c:8223-8240 (type_default chases
// named before sizing/walking fields); same alias-chase helper
// the #22 sites use.
si = structlookupchain(c, v.lhs);
if (si != nil) { sz = si.totsize; };
};
let okl: str = mklabel(c, "alloc_ok");
@@ -29010,6 +29010,10 @@ fn cgalloc(c: *cgen, n: *node) void = {
};
};
} else {
// #57 (retained cs!=ww, deferred): scalar/ptr alloc keeps the
// default-8 size + MOVQ store; cstage sizes the scalar exactly
// ($1/MOVB for u8) and the latent alloc(str|slice) wants 24B not 8.
// Byte-id-visible, runtime-benign; not folded into the #26 arm.
cgexpr(c, v);
emitline("\tMOVQ\t(SP), BX\n");
let sop: str = "MOVQ";

View File

@@ -0,0 +1,155 @@
/*
* 989_allocalias_run (#26) — `alloc(T{...})` where T is an alias of a struct
* must size + field-fill from the resolved (alias-chased) struct layout.
*
* THE BUG (wwstage only): cgalloc looked up the struct layout by the literal
* head NAME via structlookup, which scans c.structs by name and never matches
* an alias. For `type pt = point; alloc(pt{x=1,y=2})` it found nil, kept the
* default size 8 (under-alloc for the 16B struct) and skipped the whole
* field-store loop — the *pt pointed at uninitialised heap (rc=0, p.x wrong).
* cstage sizes from type_default(v->type) and walks u->fields. THE FIX: chase
* the alias chain with structlookupchain on the literal's type ref.
*
* row | shape | exit (cs==ww)
* -------------+------------------------------------+--------------
* alias | type pt=point; alloc(pt{x=1,y=2}) | 0 (was ww 1)
* direct ctrl | alloc(point{x=1,y=2}) | 0
* alias returns p.x*0+ok: pre-fix ww exited 1 (p.x != 1, zero-filled heap).
*/
#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;
}
struct row { const char *label; const char *src; int want_exit; };
static const struct row rows[] = {
{ "alias",
"package main;\n"
"import rt;\n"
"type point = struct { x: i64, y: i64 };\n"
"type pt = point;\n"
"export fn main() i32 = {\n"
" let p: *pt = alloc(pt{x=1,y=2})!;\n"
" if (p.x != 1) { return 1; };\n"
" if (p.y != 2) { return 2; };\n"
" return 0;\n"
"};\n",
0 },
{ "direct",
"package main;\n"
"import rt;\n"
"type point = struct { x: i64, y: i64 };\n"
"export fn main() i32 = {\n"
" let p: *point = alloc(point{x=1,y=2})!;\n"
" if (p.x != 1) { return 1; };\n"
" if (p.y != 2) { return 2; };\n"
" return 0;\n"
"};\n",
0 },
};
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/alal_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/alal_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
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], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int have_ww = (access(wdrv, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
int gc = run_build(cdrv, &rows[i], i);
if (gc < 0) {
fprintf(stderr, "allocalias[cstage][%s]: build/run failed "
"(got %d)\n", rows[i].label, gc);
fail++;
continue;
}
if (gc != rows[i].want_exit) {
fprintf(stderr, "allocalias[cstage][%s]: exit=%d want=%d\n",
rows[i].label, gc, rows[i].want_exit);
fail++;
}
if (!have_ww) {
fprintf(stderr, "allocalias: skip wwstage (no %s)\n", wdrv);
continue;
}
int gw = run_build(wdrv, &rows[i], i);
if (gw != gc) {
fprintf(stderr, "allocalias[%s]: cs=%d != ww=%d "
"(alloc(alias{}) under-alloc / zero fields — #26)\n",
rows[i].label, gc, gw);
fail++;
}
if (gw != rows[i].want_exit) {
fprintf(stderr, "allocalias[wwstage][%s]: exit=%d want=%d\n",
rows[i].label, gw, rows[i].want_exit);
fail++;
}
}
if (fail) {
fprintf(stderr, "allocalias_run: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("allocalias_run: %d/%d ok\n", total, total);
return 0;
}