wcc/ww: widen of a module-global struct ident copies the full payload

Widening a global struct into a tagged slot copied word0 only; copy
the full payload from g(SB). Both-wrong pair: cstage zero-fills the
payload (filed task #43); rows pin ww-runtime-correct with the
documented cstage residual. Review item #50.
This commit is contained in:
2026-06-12 09:16:52 +09:00
parent 4b8c8fd9ed
commit 32848194d7
5 changed files with 339 additions and 0 deletions

View File

@@ -261,6 +261,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_dotbasehijack_run \ $(BIN)/test_dotbasehijack_run \
$(BIN)/test_globtagreassign_run \ $(BIN)/test_globtagreassign_run \
$(BIN)/test_globstructret_run \ $(BIN)/test_globstructret_run \
$(BIN)/test_globstructwiden_run \
$(BIN)/test_arr_ptr_global \ $(BIN)/test_arr_ptr_global \
$(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_infer_len \
$(BIN)/test_def_arr_len \ $(BIN)/test_def_arr_len \
@@ -781,6 +782,16 @@ $(BIN)/test_globstructret_run: test/wcc/989_globstructret_run.c \
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
# 989_globstructwiden_run (F8-c7, #50, #263): widening a module-global struct
# ident into a tagged union must copy the full payload. ww-runtime-correct;
# cstage zero-fills the payload (cs!=ww residual, cstage half = task #43).
$(BIN)/test_globstructwiden_run: test/wcc/989_globstructwiden_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 $@ $<
$(BIN)/test_let_global: test/wcc/630_let_global.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/test_let_global: test/wcc/630_let_global.c $(BIN)/ww $(BIN)/w6c \
$(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<

View File

@@ -21820,6 +21820,60 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
emitline("(BP)\n"); emitline("(BP)\n");
return; return;
}; }; }; };
// #50 (#263 ww-runtime-correct): a module-GLOBAL struct ident source.
// rhsstructpayload below is local/literal-only, so a global struct value
// fell to the scalar word0 arm — payload truncated. Land g(SB) in SI and
// byte-copy the struct words into slot+8; cstage zero-fills the payload
// (cstage half #43). Local/literal sources keep the existing arms (byte-id).
if (src.kind == nkind.N_IDENT) {
if (localfindnode(c, src.str) == nil) {
let gtn: *node = letvartnode(c, src.str);
if (gtn != nil) { if (gtn.kind == nkind.N_TNAME) {
let gsi: *structinfo = structlookupchain(c, gtn);
if (gsi != nil) {
emitline("\tXORQ\tAX, AX\n");
let gz: i32 = 0;
for (gz < slot_sz) {
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + gz): i64);
emitline("(BP)\n");
gz += 8;
};
let gtag: i32 = taggedvariantindext(c, dt, src);
if (gtag < 0) { gtag = 0; };
if (aggargsrcaddr(c, src, "SI")) {
let gtot: i32 = gsi.totsize;
let gk: i32 = 0;
for (gk + 8 <= gtot) {
emitline("\tMOVQ\t");
emitoff(gk: i64);
emitline("(SI), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + 8 + gk): i64);
emitline("(BP)\n");
gk += 8;
};
if (gk < gtot) {
let gtail: i32 = gtot - gk;
let glop: str = "MOVQ";
if (gtail == 4) { glop = "MOVL"; }
else { if (gtail == 1) { glop = "MOVB"; }; };
emitline("\t"); emitline(glop); emitline("\t");
emitoff(gk: i64); emitline("(SI), AX\n");
emitline("\t"); emitline(glop); emitline("\tAX, ");
emitoff((slot_off + 8 + gk): i64); emitline("(BP)\n");
};
emitline("\tMOVQ\t$");
emitint(gtag: i64);
emitline(", ");
emitoff(slot_off: i64);
emitline("(BP)\n");
return;
};
};
}; };
};
};
// Struct payload (literal or ident). // Struct payload (literal or ident).
let sname: str = rhsstructpayload(c, src); let sname: str = rhsstructpayload(c, src);
if (sname.len > 0) { if (sname.len > 0) {

View File

@@ -4400,6 +4400,60 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
emitline("(BP)\n"); emitline("(BP)\n");
return; return;
}; }; }; };
// #50 (#263 ww-runtime-correct): a module-GLOBAL struct ident source.
// rhsstructpayload below is local/literal-only, so a global struct value
// fell to the scalar word0 arm — payload truncated. Land g(SB) in SI and
// byte-copy the struct words into slot+8; cstage zero-fills the payload
// (cstage half #43). Local/literal sources keep the existing arms (byte-id).
if (src.kind == nkind.N_IDENT) {
if (localfindnode(c, src.str) == nil) {
let gtn: *node = letvartnode(c, src.str);
if (gtn != nil) { if (gtn.kind == nkind.N_TNAME) {
let gsi: *structinfo = structlookupchain(c, gtn);
if (gsi != nil) {
emitline("\tXORQ\tAX, AX\n");
let gz: i32 = 0;
for (gz < slot_sz) {
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + gz): i64);
emitline("(BP)\n");
gz += 8;
};
let gtag: i32 = taggedvariantindext(c, dt, src);
if (gtag < 0) { gtag = 0; };
if (aggargsrcaddr(c, src, "SI")) {
let gtot: i32 = gsi.totsize;
let gk: i32 = 0;
for (gk + 8 <= gtot) {
emitline("\tMOVQ\t");
emitoff(gk: i64);
emitline("(SI), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + 8 + gk): i64);
emitline("(BP)\n");
gk += 8;
};
if (gk < gtot) {
let gtail: i32 = gtot - gk;
let glop: str = "MOVQ";
if (gtail == 4) { glop = "MOVL"; }
else { if (gtail == 1) { glop = "MOVB"; }; };
emitline("\t"); emitline(glop); emitline("\t");
emitoff(gk: i64); emitline("(SI), AX\n");
emitline("\t"); emitline(glop); emitline("\tAX, ");
emitoff((slot_off + 8 + gk): i64); emitline("(BP)\n");
};
emitline("\tMOVQ\t$");
emitint(gtag: i64);
emitline(", ");
emitoff(slot_off: i64);
emitline("(BP)\n");
return;
};
};
}; };
};
};
// Struct payload (literal or ident). // Struct payload (literal or ident).
let sname: str = rhsstructpayload(c, src); let sname: str = rhsstructpayload(c, src);
if (sname.len > 0) { if (sname.len > 0) {

View File

@@ -21820,6 +21820,60 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
emitline("(BP)\n"); emitline("(BP)\n");
return; return;
}; }; }; };
// #50 (#263 ww-runtime-correct): a module-GLOBAL struct ident source.
// rhsstructpayload below is local/literal-only, so a global struct value
// fell to the scalar word0 arm — payload truncated. Land g(SB) in SI and
// byte-copy the struct words into slot+8; cstage zero-fills the payload
// (cstage half #43). Local/literal sources keep the existing arms (byte-id).
if (src.kind == nkind.N_IDENT) {
if (localfindnode(c, src.str) == nil) {
let gtn: *node = letvartnode(c, src.str);
if (gtn != nil) { if (gtn.kind == nkind.N_TNAME) {
let gsi: *structinfo = structlookupchain(c, gtn);
if (gsi != nil) {
emitline("\tXORQ\tAX, AX\n");
let gz: i32 = 0;
for (gz < slot_sz) {
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + gz): i64);
emitline("(BP)\n");
gz += 8;
};
let gtag: i32 = taggedvariantindext(c, dt, src);
if (gtag < 0) { gtag = 0; };
if (aggargsrcaddr(c, src, "SI")) {
let gtot: i32 = gsi.totsize;
let gk: i32 = 0;
for (gk + 8 <= gtot) {
emitline("\tMOVQ\t");
emitoff(gk: i64);
emitline("(SI), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + 8 + gk): i64);
emitline("(BP)\n");
gk += 8;
};
if (gk < gtot) {
let gtail: i32 = gtot - gk;
let glop: str = "MOVQ";
if (gtail == 4) { glop = "MOVL"; }
else { if (gtail == 1) { glop = "MOVB"; }; };
emitline("\t"); emitline(glop); emitline("\t");
emitoff(gk: i64); emitline("(SI), AX\n");
emitline("\t"); emitline(glop); emitline("\tAX, ");
emitoff((slot_off + 8 + gk): i64); emitline("(BP)\n");
};
emitline("\tMOVQ\t$");
emitint(gtag: i64);
emitline(", ");
emitoff(slot_off: i64);
emitline("(BP)\n");
return;
};
};
}; };
};
};
// Struct payload (literal or ident). // Struct payload (literal or ident).
let sname: str = rhsstructpayload(c, src); let sname: str = rhsstructpayload(c, src);
if (sname.len > 0) { if (sname.len > 0) {

View File

@@ -0,0 +1,166 @@
/*
* 989_globstructwiden_run — F8-c7 (report-item #50, #263): widening a module-
* GLOBAL struct ident into a tagged union (`let x: (S | str) = gs;`) must
* copy gs's full payload into the box, not just word0.
*
* THE BUG (cat-A silent miscompile, #263 BOTH-WRONG): cgwidentaggedstorebp's
* struct-payload arm keys off rhsstructpayload, which returns a struct name
* only for an N_STRUCTLIT or a LOCAL ident. A module-global struct ident
* returned empty, so the value fell to the scalar word0 widen arm — only the
* first 8 bytes reached the box, the rest stale/zero. cstage is ALSO wrong:
* it zero-fills the payload (gs is never read). Both wrong (#263); the cstage
* half is filed as task #43.
*
* THE WWSTAGE FIX (ww-runtime-correct): a dedicated global-struct-source arm
* (gated on structlookupchain so str/slice/tagged globals don't trip it)
* zero-fills the slot, lands g(SB) in SI via aggargsrcaddr, byte-copies the
* struct words into slot+8, and writes the variant tag. Local/literal
* sources keep their existing arms (byte-id). cstage stays wrong → cs≠ww.
*
* Rows assert ww runtime-correct (field-equality check → 0) AND pin cstage's
* deterministic residual: cstage zero-fills the payload → the fields read 0,
* never match → the match arm returns 2. (#43 closes cs → cs==ww.)
* row | shape | cs | ww
* -----------+---------------------------------------------+----+----
* widen_16 | gp:{a=5,b=6}; (pt|str)=gp; match pt fields | 2 | 0
* widen_24 | gp:{a=5,b=6,c=7}; (tri|str)=gp; match fields | 2 | 0
*/
#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_cs;
int want_ww;
};
static const struct row rows[] = {
{ "widen_16",
"package main;\n"
"type pt = struct { a: i64, b: i64 };\n"
"let gp: pt = pt { a = 5, b = 6 };\n"
"export fn main() int = {\n"
" let x: (pt | str) = gp;\n"
" match (x) {\n"
" case let q: pt => { if (q.a == 5 && q.b == 6) { return 0; }; return 2; };\n"
" case str => return 3;\n"
" };\n"
"};\n",
2, 0 },
{ "widen_24",
"package main;\n"
"type tri = struct { a: i64, b: i64, c: i64 };\n"
"let gp: tri = tri { a = 5, b = 6, c = 7 };\n"
"export fn main() int = {\n"
" let x: (tri | str) = gp;\n"
" match (x) {\n"
" case let q: tri => { if (q.a == 5 && q.b == 6 && q.c == 7) { return 0; }; return 2; };\n"
" case str => return 3;\n"
" };\n"
"};\n",
2, 0 },
};
/* run_build — build+run `src` via `driver`; returns the binary's exit
* code, or -1 on a build failure. */
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/gsw_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/gsw_%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);
struct { const char *name; const char *drv; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "globstructwiden_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
int is_ww = (d == 1);
for (int i = 0; i < n; i++) {
total++;
int want = is_ww ? rows[i].want_ww : rows[i].want_cs;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != want) {
fprintf(stderr, "globstructwiden_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "globstructwiden_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("globstructwiden_run: %d/%d ok\n", total, total);
return 0;
}