wcc/ww: return of a module-global struct ident copies the global's bytes

Returning a global struct by name emitted nothing into the return
scratch (ww) — copy from the g(SB) base. Both-wrong pair: cstage
zeroes the retscr instead (filed task #42); rows pin ww-runtime-correct
with the documented cstage residual. Review item #41.
This commit is contained in:
2026-06-12 09:13:26 +09:00
parent b3a355744f
commit 4b8c8fd9ed
5 changed files with 200 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
/*
* 989_globstructret_run — F8-c6 (report-item #41, #263): returning a module-
* GLOBAL struct ident by value (`return g;`) must copy g's bytes into the
* return buffer.
*
* THE BUG (cat-A silent miscompile, #263 BOTH-WRONG): cgenstmt.ww cgreturn's
* ≤24B register-struct return path resolves an N_IDENT source via
* localfindnode and word-copies from its BP slot only `if (rl != nil)`. A
* module-global struct ident (rl==nil) hit no branch — after the @retscr
* zero-fill it copied NOTHING, so the callee returned a zeroed struct and g
* was never referenced. cstage is ALSO wrong: it copies frame garbage
* ((BP)/8(BP)), never g(SB). Both wrong (#263); the cstage half is filed as
* task #42.
*
* THE WWSTAGE FIX (ww-runtime-correct): a global N_IDENT source is marked
* addrsrc, routing it through the existing memcpy path — aggargsrcaddr emits
* LEAQ g(SB),SI and the loop copies rsz bytes into @retscr. Locals keep the
* BP word-copy (byte-id preserved). cstage stays wrong → cs≠ww residual.
*
* Rows assert ww runtime-correct (the field-equality check returns 0) AND
* pin cstage's deterministic residual: garbage fields never satisfy the
* equality, so the cstage binary takes the else and returns 1. (#42 closes
* the cs side → these become cs==ww.)
* row | shape (return g; then check) | cs | ww
* -----------+-------------------------------------------+----+----
* ret_16 | point{x=3,y=4} (rsz 16); x==3 && y==4 | 1 | 0
* ret_24 | triple{a=5,b=6,c=7} (rsz 24); all three | 1 | 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[] = {
{ "ret_16",
"package main;\n"
"type point = struct { x: i64, y: i64 };\n"
"let g: point = point { x = 3, y = 4 };\n"
"fn get() point = { return g; };\n"
"export fn main() int = {\n"
" let p: point = get();\n"
" if (p.x == 3 && p.y == 4) { return 0; };\n"
" return 1;\n"
"};\n",
1, 0 },
{ "ret_24",
"package main;\n"
"type triple = struct { a: i64, b: i64, c: i64 };\n"
"let g: triple = triple { a = 5, b = 6, c = 7 };\n"
"fn get() triple = { return g; };\n"
"export fn main() int = {\n"
" let p: triple = get();\n"
" if (p.a == 5 && p.b == 6 && p.c == 7) { return 0; };\n"
" return 1;\n"
"};\n",
1, 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/gsr_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/gsr_%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, "globstructret_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, "globstructret_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "globstructret_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("globstructret_run: %d/%d ok\n", total, total);
return 0;
}