From 80e7ab7cf3c255237b250c5857d35a137522b773 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Mon, 1 Jun 2026 09:57:45 +0900 Subject: [PATCH] w6c+wwstage: qualify N_DOT-base + addr-of value-global by dotted module (#229) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cross-module dotted value-global read (`aa.v`) and addr-of (`&aa.v`) still mangled their symbol via the non-preferring leaf lookup (cstage masym / wwstage emitsymname), so they emitted `LEAQ main.v(SB)` — the WRONG module's same-leaf global — returning 99 instead of 7. #1 fixed the DATA def-site and the bare-ident load; these four dotted LOAD/addr sites were the residual. Thread the dotted module name (the `m` in `m.x`) — n->lhs->str / opnd->lhs->str / lhs.str / basenm — into the existing value mangle (cstage mahint, wwstage emitsymnamehint), the same polarity the TY_FN branch beside each site already uses via mafn/emitfnname. The addr-of spine-walk for a bare-root `&global.field` is a different shape and is left untouched. Byte-id-blind (the bootstrap has no colliding leaves), so a committed runtime + cs==ww test (796) is the net. --- Makefile | 9 ++ cmd/w6c/cgen.c | 18 ++- selfhost/cmd/w6c/main.combined.ww | 14 +- selfhost/cmd/wcc/cgenexpr.ww | 14 +- selfhost/cmd/wwdump/main.combined.ww | 14 +- test/wcc/796_xmod_valglobal_dot_run.c | 181 ++++++++++++++++++++++++++ 6 files changed, 238 insertions(+), 12 deletions(-) create mode 100644 test/wcc/796_xmod_valglobal_dot_run.c diff --git a/Makefile b/Makefile index 228d9893..9cdabd81 100644 --- a/Makefile +++ b/Makefile @@ -342,6 +342,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_spread_variant_match \ $(BIN)/test_xmod_ident_prefer \ $(BIN)/test_xmod_valglobal_run \ + $(BIN)/test_xmod_valglobal_dot_run \ $(BIN)/test_widen_pad_zero_run \ $(BIN)/test_named_ptr_alias_variant_widen \ $(BIN)/test_single_field_struct_zeroinit \ @@ -822,6 +823,14 @@ $(BIN)/test_xmod_valglobal_run: test/wcc/795_xmod_valglobal_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# #229 (N_DOT-base + addr-of sibling of #1): cross-module DOTTED value- +# global read AND &-address-of must mangle by the dotted module name, not +# cur_mod — runtime + cs==ww byte-id. Self-contained multi-package probe. +$(BIN)/test_xmod_valglobal_dot_run: test/wcc/796_xmod_valglobal_dot_run.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + # #15: widening a bare *vtable into a NAMED-alias variant (`stream` = # *vtable) of `(file | stream)` must compute the right tag, not default # to tag 0. Both-stage byte-id + runtime, plus a degenerate-ambiguity diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 84439340..68e0e30f 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -2713,8 +2713,13 @@ cgexpr(Cg *c, Node *n, Local *locals) opnd->lhs->str), areg(D_AX)); else + /* #229: dotted-module value + * mangle (twin of the read), so + * &aa.v takes aa's global, not a + * same-leaf collision. */ ins2(c, A_LEAQ, - masym(c, opnd->str), + mahint(c, opnd->str, + opnd->lhs->str), areg(D_AX)); break; } @@ -6774,10 +6779,17 @@ cgexpr(Cg *c, Node *n, Local *locals) * registry-without-tnode shape agrees byte-for-byte. */ int mqop = let_islet(n->str) ? localloadop(n->type) : A_MOVQ; + /* #229: thread the DOTTED module (the `m` in `m.x`) into + * the value mangle, not cur_mod — masym's non-preferring + * leaf lookup mis-mangled `aa.v` onto another module's + * same-leaf global (read the WRONG global). The TY_FN + * branch above already uses n->lhs->str via mafn. */ if (mqop == A_MOVQ) { - ins2(c, A_MOVQ, masym(c, n->str), areg(D_AX)); + ins2(c, A_MOVQ, + mahint(c, n->str, n->lhs->str), areg(D_AX)); } else { - ins2(c, A_LEAQ, masym(c, n->str), areg(D_CX)); + ins2(c, A_LEAQ, + mahint(c, n->str, n->lhs->str), areg(D_CX)); ins2(c, mqop, amem(D_CX, 0), areg(D_AX)); } goto dot_done; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index a4c078a0..a2fa93c4 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -21014,13 +21014,17 @@ fn cgdot(c: *cgen, n: *node) void = { }; }; let mqop: str = localloadop(c, letvartnode(c, fld)); + // #229: thread the dotted module (lhs.str), not c.curmod + // — the non-preferring emitsymname mis-mangled `aa.v` onto + // a same-leaf global. The TY_FN branch above already + // threads lhs.str via emitfnname. if (streq(mqop, "MOVQ")) { emitline("\tMOVQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, lhs.str); emitline("(SB), AX\n"); } else { emitline("\tLEAQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, lhs.str); emitline("(SB), CX\n"); emitline("\t"); emitline(mqop); @@ -21733,8 +21737,12 @@ fn cgun(c: *cgen, n: *node) void = { emitline("(SB), AX\n"); return; }; + // #229: dotted-module value mangle + // (basenm), twin of the read — so + // &aa.v takes aa's global, not a + // same-leaf collision. emitline("\tLEAQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, basenm); emitline("(SB), AX\n"); return; }; diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 857ebf7b..fcff3b4e 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -2354,13 +2354,17 @@ fn cgdot(c: *cgen, n: *node) void = { }; }; let mqop: str = localloadop(c, letvartnode(c, fld)); + // #229: thread the dotted module (lhs.str), not c.curmod + // — the non-preferring emitsymname mis-mangled `aa.v` onto + // a same-leaf global. The TY_FN branch above already + // threads lhs.str via emitfnname. if (streq(mqop, "MOVQ")) { emitline("\tMOVQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, lhs.str); emitline("(SB), AX\n"); } else { emitline("\tLEAQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, lhs.str); emitline("(SB), CX\n"); emitline("\t"); emitline(mqop); @@ -3073,8 +3077,12 @@ fn cgun(c: *cgen, n: *node) void = { emitline("(SB), AX\n"); return; }; + // #229: dotted-module value mangle + // (basenm), twin of the read — so + // &aa.v takes aa's global, not a + // same-leaf collision. emitline("\tLEAQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, basenm); emitline("(SB), AX\n"); return; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 7d51e352..733a104e 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -21014,13 +21014,17 @@ fn cgdot(c: *cgen, n: *node) void = { }; }; let mqop: str = localloadop(c, letvartnode(c, fld)); + // #229: thread the dotted module (lhs.str), not c.curmod + // — the non-preferring emitsymname mis-mangled `aa.v` onto + // a same-leaf global. The TY_FN branch above already + // threads lhs.str via emitfnname. if (streq(mqop, "MOVQ")) { emitline("\tMOVQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, lhs.str); emitline("(SB), AX\n"); } else { emitline("\tLEAQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, lhs.str); emitline("(SB), CX\n"); emitline("\t"); emitline(mqop); @@ -21733,8 +21737,12 @@ fn cgun(c: *cgen, n: *node) void = { emitline("(SB), AX\n"); return; }; + // #229: dotted-module value mangle + // (basenm), twin of the read — so + // &aa.v takes aa's global, not a + // same-leaf collision. emitline("\tLEAQ\t"); - emitsymname(c, fld); + emitsymnamehint(c, fld, basenm); emitline("(SB), AX\n"); return; }; diff --git a/test/wcc/796_xmod_valglobal_dot_run.c b/test/wcc/796_xmod_valglobal_dot_run.c new file mode 100644 index 00000000..5a1a535a --- /dev/null +++ b/test/wcc/796_xmod_valglobal_dot_run.c @@ -0,0 +1,181 @@ +/* + * 796_xmod_valglobal_dot_run — project #229, the N_DOT-base read + addr-of + * sibling of #1 (test 795). Runtime + cs==ww byte-id net for the cross- + * module DOTTED value-global miscompile. + * + * THE BUG (cgen-only, both stages emit IDENTICAL wrong asm → byte-id was + * BLIND to it): #1 fixed the DATA def-site and the bare-ident load mangle, + * but the DOTTED LOAD/addr sites still used the non-preferring leaf lookup + * (cstage masym, wwstage emitsymname). With + * package aa; export let v: i32 = 7; + * package main; import aa; let v: i32 = 99; main = { return aa.v; } + * the `aa.v` read AND `&aa.v` emitted `LEAQ main.v(SB)` — the WRONG + * module's global — returning 99, not 7. + * + * THE FIX (#229): thread the DOTTED module name (the `m` in `m.x`) — NOT + * cur_mod — into the existing value mangle at the four lockstep sites: + * cstage cgen.c N_DOT read + N_UN addr-of (mahint with n->lhs->str / + * opnd->lhs->str), wwstage cgenexpr.ww twins (emitsymnamehint with + * lhs.str / basenm). The TY_FN branches beside each site already use this + * dotted polarity via mafn/emitfnname; value globals now match. + * + * EACH ROW CARRIES BOTH DIMENSIONS (795 model): + * (a) cstage `ww build` + run, asserting the exit — pins the converged + * asm is runtime-correct. + * (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge. Necessary + * because the bug was byte-id-blind. + * + * GATE POLARITY: must stay GREEN. A wrong exit means the dotted value- + * global mangle regressed; a byte-id FAIL means the stages diverged + * (rule-10 violation). + */ +#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_exit; }; + +static const struct row rows[] = { + /* N_DOT read: aa.v (7) vs private main.v (99). Pre-fix: 99. */ + { "i32_dot_read_export_vs_private", + "package aa;\n" + "export let v: i32 = 7;\n" + "package main;\n" + "import aa;\n" + "let v: i32 = 99;\n" + "export fn main() i32 = { return aa.v; };\n", 7 }, + /* addr-of: &aa.v then deref. Pre-fix: 99. */ + { "i32_addrof_export_vs_private", + "package aa;\n" + "export let v: i32 = 7;\n" + "package main;\n" + "import aa;\n" + "let v: i32 = 99;\n" + "export fn main() i32 = { let p: *i32 = &aa.v; return *p; };\n", 7 }, + { NULL, NULL, 0 } +}; + +static int +slurp_eq(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"); + FILE *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } + int rc = 0; + for (;;) { + int ca = fgetc(fa); + int cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +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 w6c[1100], w6c_ww[1100]; + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + if (access(w6c_ww, X_OK) != 0) { + fprintf(stderr, "xmod_valglobal_dot: w6c_ww missing — cannot run " + "the cs==ww byte-id gate\n"); + return 1; + } + + int n = 0, fail = 0; + for (int i = 0; rows[i].src; i++, n++) { + char src[64]; + snprintf(src, sizeof src, "/tmp/wwxmvd_%d_%d.ww", getpid(), i); + FILE *f = fopen(src, "wb"); + if (f == NULL) { fail++; continue; } + fputs(rows[i].src, f); + fclose(f); + + /* (a) cstage build + run in a scratch dir. */ + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwxmvd_%d_d_%d", getpid(), i); + mkdir(tmpdir, 0755); + + char cmd[2048]; + snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s", + tmpdir, bin, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: cstage build failed\n", + rows[i].label); + fail++; + unlink(src); rmdir(tmpdir); + continue; + } + + char outbin[128]; + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + + int got = runwait(outbin); + if (got != rows[i].want_exit) { + fprintf(stderr, "row[%s]: cstage exit %d, want %d\n", + rows[i].label, got, rows[i].want_exit); + fail++; + } + unlink(outbin); rmdir(tmpdir); + + /* (b) cs==ww byte-id gate. */ + char cs_s[64], ws_s[64]; + snprintf(cs_s, sizeof cs_s, "/tmp/wwxmvd_%d_%d_cs.s", getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/wwxmvd_%d_%d_ww.s", getpid(), i); + + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label); + fail++; unlink(src); continue; + } + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c_ww, ws_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c_ww failed\n", rows[i].label); + fail++; unlink(src); unlink(cs_s); continue; + } + if (slurp_eq(cs_s, ws_s) != 0) { + fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER " + "(rule-10 byte-id violation)\n", rows[i].label); + fail++; + } + unlink(src); unlink(cs_s); unlink(ws_s); + } + + if (fail) { + fprintf(stderr, "%d/%d xmod dotted value-global tests failed\n", + fail, n); + return 1; + } + printf("xmod_valglobal_dot: %d/%d ok (cstage run + cs==ww byte-id)\n", + n, n); + return 0; +}