wcc/ww: tagged GLOBAL reassign stores tag and payload

Reassigning a module-global tagged union stored the payload into the
tag word; emit the full tag+payload store to g(SB). Both-wrong pair:
cstage silently DROPS the store entirely (filed task #41) — rows pin
ww-runtime-correct and the documented cstage residual. Review item #32.
This commit is contained in:
2026-06-12 09:10:16 +09:00
parent 94bfca761c
commit b3a355744f
5 changed files with 247 additions and 0 deletions

View File

@@ -259,6 +259,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_globstrslice_run \
$(BIN)/test_globtagisas_run \
$(BIN)/test_dotbasehijack_run \
$(BIN)/test_globtagreassign_run \
$(BIN)/test_arr_ptr_global \
$(BIN)/test_def_arr_infer_len \
$(BIN)/test_def_arr_len \
@@ -758,6 +759,17 @@ $(BIN)/test_dotbasehijack_run: test/wcc/989_dotbasehijack_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_globtagreassign_run (F8-c5, #32, #263): reassigning a module-global
# tagged ident (g = expr) must store tag+payload. ww-runtime-correct; cstage
# drops the store (cs!=ww residual, cstage half = task #41). Per-driver
# expected rows. See the test header.
$(BIN)/test_globtagreassign_run: test/wcc/989_globtagreassign_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)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<

View File

@@ -30976,6 +30976,25 @@ fn cgassign(c: *cgen, n: *node) void = {
};
};
};
// #32 (#263 ww-runtime-correct): tagged-union GLOBAL reassign
// `g = expr`. No BP slot — LEAQ g(SB),BX then the shared widener
// stores tag+payload off BX (mirror the local arm above + the
// global-struct-field tagged arm at :10220). Pre-fix the generic
// scalar store below clobbered the tag word. cstage drops the
// store entirely — cs!=ww residual until the cstage half (#41).
if (lc == nil) {
let gtn: *node = letvartnode(c, lhs.str);
if (gtn != nil) {
if (istaggedtype(c, gtn)) {
let gsz: i32 = slotsize(c, gtn);
emitline("\tLEAQ\t");
emitsymname(c, lhs.str);
emitline("(SB), BX\n");
cgwidentaggedstore(c, gtn.type_: *tinfo, n.rhs, "BX", 0, gsz);
return;
};
};
};
};
};
};

View File

@@ -8205,6 +8205,25 @@ fn cgassign(c: *cgen, n: *node) void = {
};
};
};
// #32 (#263 ww-runtime-correct): tagged-union GLOBAL reassign
// `g = expr`. No BP slot — LEAQ g(SB),BX then the shared widener
// stores tag+payload off BX (mirror the local arm above + the
// global-struct-field tagged arm at :10220). Pre-fix the generic
// scalar store below clobbered the tag word. cstage drops the
// store entirely — cs!=ww residual until the cstage half (#41).
if (lc == nil) {
let gtn: *node = letvartnode(c, lhs.str);
if (gtn != nil) {
if (istaggedtype(c, gtn)) {
let gsz: i32 = slotsize(c, gtn);
emitline("\tLEAQ\t");
emitsymname(c, lhs.str);
emitline("(SB), BX\n");
cgwidentaggedstore(c, gtn.type_: *tinfo, n.rhs, "BX", 0, gsz);
return;
};
};
};
};
};
};

View File

@@ -30976,6 +30976,25 @@ fn cgassign(c: *cgen, n: *node) void = {
};
};
};
// #32 (#263 ww-runtime-correct): tagged-union GLOBAL reassign
// `g = expr`. No BP slot — LEAQ g(SB),BX then the shared widener
// stores tag+payload off BX (mirror the local arm above + the
// global-struct-field tagged arm at :10220). Pre-fix the generic
// scalar store below clobbered the tag word. cstage drops the
// store entirely — cs!=ww residual until the cstage half (#41).
if (lc == nil) {
let gtn: *node = letvartnode(c, lhs.str);
if (gtn != nil) {
if (istaggedtype(c, gtn)) {
let gsz: i32 = slotsize(c, gtn);
emitline("\tLEAQ\t");
emitsymname(c, lhs.str);
emitline("(SB), BX\n");
cgwidentaggedstore(c, gtn.type_: *tinfo, n.rhs, "BX", 0, gsz);
return;
};
};
};
};
};
};

View File

@@ -0,0 +1,178 @@
/*
* 989_globtagreassign_run — F8-c5 (report-item #32, #263): reassigning a
* module-GLOBAL tagged-union ident (`g = expr`) must store the new tag and
* payload, not clobber only one word.
*
* THE BUG (cat-A silent miscompile, #263 BOTH-WRONG): cgenexpr.ww cgassign's
* tagged-union reassignment arm delegated to cgwidentaggedstore ONLY for a
* LOCAL ident (localfindnode != nil). A module-global tagged ident (lc==nil)
* had no arm and fell through to the generic scalar store, which wrote one
* word into the tag slot and left the payload stale. cstage is ALSO wrong:
* it drops the store entirely (the new value never reaches g) — so the repro
* is cs≠ww residual until the cstage half lands (ww-core TASK #41).
*
* THE WWSTAGE FIX (ww-runtime-correct): add the lc==nil arm — LEAQ g(SB),BX
* then cgwidentaggedstore stores tag+payload off BX (mirrors the local arm
* and the global-struct-field tagged arm). ww now stores the full box.
* cstage stays wrong → the rows assert ww-correct AND pin cstage's
* documented-wrong value (store dropped → g keeps its init), so the residual
* is never misread as a regression.
*
* Rows (per-driver expected — want_cs / want_ww):
* row | shape | cs | ww
* ----------------+----------------------------------------+----+----
* reassign_i64 | g:(i64|void)=0; g=7; match i64 | 0 | 7
* reassign_str | g:(str|i64)=0; g="hello"; match str→len | 88 | 5
* reassign_union | g:(i64|bool)=0; g=5; match i64 | 0 | 5
* (cstage drops the store → g keeps its init → the i64/void/i64-tag arm;
* #41 closes the cs side, at which point these become cs==ww.)
*/
#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[] = {
{ "reassign_i64",
"package main;\n"
"let g: (i64 | void) = 0;\n"
"export fn main() int = {\n"
" g = 7;\n"
" match (g) {\n"
" case let n: i64 => return n: int;\n"
" case void => return 99;\n"
" };\n"
"};\n",
0, 7 },
{ "reassign_str",
"package main;\n"
"let g: (str | i64) = 0;\n"
"export fn main() int = {\n"
" g = \"hello\";\n"
" match (g) {\n"
" case let s: str => return len(s): int;\n"
" case i64 => return 88;\n"
" };\n"
"};\n",
88, 5 },
{ "reassign_union",
"package main;\n"
"let g: (i64 | bool) = 0;\n"
"export fn main() int = {\n"
" g = 5;\n"
" match (g) {\n"
" case let n: i64 => return n: int;\n"
" case bool => return 77;\n"
" };\n"
"};\n",
0, 5 },
};
/* 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/gtr_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/gtr_%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, "globtagreassign_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, "globtagreassign_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "globtagreassign_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("globtagreassign_run: %d/%d ok\n", total, total);
return 0;
}