diff --git a/Makefile b/Makefile index dca8b897..2f3f8c85 100644 --- a/Makefile +++ b/Makefile @@ -260,6 +260,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_globtagisas_run \ $(BIN)/test_dotbasehijack_run \ $(BIN)/test_globtagreassign_run \ + $(BIN)/test_globstructret_run \ $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ @@ -770,6 +771,16 @@ $(BIN)/test_globtagreassign_run: test/wcc/989_globtagreassign_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_globstructret_run (F8-c6, #41, #263): returning a module-global struct +# ident by value must copy g's bytes. ww-runtime-correct; cstage copies frame +# garbage (cs!=ww residual, cstage half = task #42). Per-driver expected rows. +$(BIN)/test_globstructret_run: test/wcc/989_globstructret_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 $@ $< diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index be1f8693..5a451a25 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -36329,6 +36329,14 @@ fn cgreturn(c: *cgen, n: *node) void = { let addrsrc: bool = false; if (rhs.kind == nkind.N_IDENT) { okrhs = true; + // #41 (#263 ww-runtime-correct): a module-global struct + // source has no BP slot — route it through the addrsrc + // memcpy (LEAQ g(SB),SI via aggargsrcaddr). Pre-fix the + // rl==nil N_IDENT arm below emitted nothing → zeroed + // @retscr. cstage copies frame garbage (cstage half #42). + if (localfindnode(c, rhs.str) == nil) { + addrsrc = true; + }; }; if (rhs.kind == nkind.N_STRUCTLIT) { okrhs = true; diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 5e85878e..3bb50c28 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -1421,6 +1421,14 @@ fn cgreturn(c: *cgen, n: *node) void = { let addrsrc: bool = false; if (rhs.kind == nkind.N_IDENT) { okrhs = true; + // #41 (#263 ww-runtime-correct): a module-global struct + // source has no BP slot — route it through the addrsrc + // memcpy (LEAQ g(SB),SI via aggargsrcaddr). Pre-fix the + // rl==nil N_IDENT arm below emitted nothing → zeroed + // @retscr. cstage copies frame garbage (cstage half #42). + if (localfindnode(c, rhs.str) == nil) { + addrsrc = true; + }; }; if (rhs.kind == nkind.N_STRUCTLIT) { okrhs = true; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 280677a7..c589181d 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -36329,6 +36329,14 @@ fn cgreturn(c: *cgen, n: *node) void = { let addrsrc: bool = false; if (rhs.kind == nkind.N_IDENT) { okrhs = true; + // #41 (#263 ww-runtime-correct): a module-global struct + // source has no BP slot — route it through the addrsrc + // memcpy (LEAQ g(SB),SI via aggargsrcaddr). Pre-fix the + // rl==nil N_IDENT arm below emitted nothing → zeroed + // @retscr. cstage copies frame garbage (cstage half #42). + if (localfindnode(c, rhs.str) == nil) { + addrsrc = true; + }; }; if (rhs.kind == nkind.N_STRUCTLIT) { okrhs = true; diff --git a/test/wcc/989_globstructret_run.c b/test/wcc/989_globstructret_run.c new file mode 100644 index 00000000..0bdcc538 --- /dev/null +++ b/test/wcc/989_globstructret_run.c @@ -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 +#include +#include +#include +#include +#include + +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; +}