w6c+wwstage: qualify cross-module value-global by defining module (#1)

A bare cross-module value-global load mis-qualified its symbol: cgen
mangled it with curmod via a non-preferring leaf lookup, so an exported
`let v` in module aa emitted both its DATA storage AND its bare-load as
main.v, colliding with main's private v. aa.getv() returned 99, not 7.
Functions were already correct (they thread a cur_mod hint via mafn /
emitfnname); value-globals did not. Both stages emitted IDENTICAL wrong
asm, so the byte-id gate was blind to it; combined.ww (frontend) is clean
-- the bug is purely in cgen. This is the cgen residual of #55 (#1 cgen
value-global module-qualifier).

Fix, symmetric in cmd/w6c/cgen.c + selfhost/cmd/wcc/{cgen,cgenexpr}.ww:
reference-site mangle uses the resolved module (curmod-prefer for bare
idents); definition/DATA-site mangle uses the decl's own module
(d->module / d.nmod) -- threaded per-site the way fns already do, via
mahint / emitsymnamehint. The fn-mangle path is left byte-for-byte
untouched.

Deviation from the signed-off spec (ratified by rob-pike after this
finding): the spec prescribed reusing the fn lookup (mod_mangle_fn /
modlookupforfn), but its first-match fallback mis-fires for value-
globals -- mod_collect export-skips exported non-fn decls (cgen.c:1059)
to keep their bare-name data ABI, so an exported leaf is absent from the
module map and the fallback grabs another module's same-leaf private
global. The value path therefore uses a distinct exact-(name,module)-or-
bare lookup (mod_lookup_value / modlookupvalue): mangle only on an exact
match, else stay bare. Byte-id-neutral on all existing single-owner code;
exported globals stay bare (ABI preserved), private stay module-qualified.

Honest boundary (rule 7): if two modules BOTH export the same value leaf,
both stay bare and the linker sees a duplicate symbol -- a correct, loud,
link-time ABI clash (like C), NOT a silent miscompile; left to the
linker, not papered over with a cgen heuristic.

Test: test/wcc/795_xmod_valglobal_run.c -- runtime (the exported global
read returns its own value, not the colliding private one) + cs==ww
byte-id, across i32-let / def-const / f64-let. Sibling to the checker
test 794_xmod_ident_prefer, which deliberately omitted byte-id because
this cgen bug diverged the asm independently.
This commit is contained in:
2026-06-01 08:43:02 +09:00
parent 954badd28f
commit 8481a05c3a
7 changed files with 575 additions and 101 deletions

View File

@@ -1107,6 +1107,36 @@ mod_lookup_for_fn(const char *name, const char *hint)
return first;
}
/* Value-global variant: mangle ONLY on an exact (name, hint) match;
* otherwise return NULL so the name stays bare. Unlike the fn variant
* there is NO first-leaf-match fallback — exported value globals are
* export-skipped from mod_map (mod_collect keeps their bare-name data
* ABI, see the skip at `!isfn && d->export`), so a first-match fallback
* would mis-mangle an exported `v` onto another module's private `v`
* (#1 cgen value-global module-qualifier, the cgen residual of #55).
* Bare-on-miss is correct: a missing entry means the leaf is either an
* exported global (its own bare symbol) or not module-private at all.
*
* HONEST BOUNDARY (rule 7) — do NOT "fix" the following into a
* workaround: if two modules BOTH export the same value leaf, both stay
* bare and the linker sees a duplicate symbol. That is a CORRECT, loud,
* link-time ABI clash (identical to C's two-extern-same-name rule), NOT
* a silent miscompile. A bare reference can never legitimately resolve
* to another module's PRIVATE global, so first-match is never wanted on
* the value path; the only ambiguity left is genuine duplicate exports,
* which belong to the linker, not to a cgen disambiguation heuristic. */
static const char *
mod_lookup_value(const char *name, const char *hint)
{
if (hint == NULL) return NULL;
for (Mod *m = mod_map; m; m = m->next) {
if (strcmp(m->name, name) != 0) continue;
if (m->module != NULL && strcmp(m->module, hint) == 0)
return m->module;
}
return NULL;
}
/* Collect every top-level `let` whose declared type we can store
* in a single .data slot. Names not in this map fall through to
* the old "drop assignment" path; with a clear link-time
@@ -1306,6 +1336,20 @@ mod_mangle_fn(Cg *c, const char *ident, const char *hint)
return mod_join(c, mod, ident);
}
/* Value-global flavoured mangle: same shape as mod_mangle_fn but over
* mod_lookup_value (exact-(ident,hint)-or-bare, no first-match
* fallback). See mod_lookup_value for why value globals can't share the
* fn fallback. */
static const char *
mod_mangle_value(Cg *c, const char *ident, const char *hint)
{
const char *resolved = ffi_resolve(ident);
if (resolved != ident) return resolved;
const char *mod = mod_lookup_value(ident, hint);
if (mod == NULL) return ident;
return mod_join(c, mod, ident);
}
/* Forward decl — masym below depends on asym defined further down. */
static Adr asym(const char *s);
@@ -1324,6 +1368,23 @@ mafn(Cg *c, const char *ident, const char *hint)
return asym(mod_mangle_fn(c, ident, hint));
}
/* Value-global address builder. masym's non-hinted mod_lookup picks
* the first leaf-name match, so two modules with a same-leaf value
* global (`let v` in both) collapse onto one DATA label and a bare
* cross-module read resolves to the wrong module (#1 cgen value-global
* module-qualifier, the cgen residual of #55). Thread a per-site hint
* the way mafn does — curmod at a bare reference, the decl's own module
* at the definition label — over the same module-generic decl map.
* Kept distinct from mafn (vs renamed) to leave the fn-mangle path
* byte-for-byte untouched. Routes through mod_mangle_value (exact-or-
* bare) so an exported global stays bare instead of mis-mangling onto
* another module's same-leaf private global. */
static Adr
mahint(Cg *c, const char *ident, const char *hint)
{
return asym(mod_mangle_value(c, ident, hint));
}
void
cg_init(Cg *c, Arena *a)
{
@@ -2529,7 +2590,8 @@ cgexpr(Cg *c, Node *n, Local *locals)
* third 8B (cap); the address holder CX gets
* overwritten by the cap as the last step, after
* we no longer need it (#1/Phase 3). */
ins2(c, A_LEAQ, masym(c, n->str), areg(D_CX));
ins2(c, A_LEAQ, mahint(c, n->str, c->cur_mod),
areg(D_CX));
ins2(c, A_MOVQ, amem(D_CX, 0), areg(D_AX));
ins2(c, A_MOVQ, amem(D_CX, 8), areg(D_BX));
ins2(c, A_MOVQ, amem(D_CX, 16), areg(D_CX));
@@ -2546,7 +2608,8 @@ cgexpr(Cg *c, Node *n, Local *locals)
* (#129 Phase A.1 LOAD-side twin of the
* emit_floatlit_data DATA-side SSoT). */
int op = type_isf32(n->type) ? A_MOVSS : A_MOVSD;
ins2(c, A_LEAQ, masym(c, n->str), areg(D_CX));
ins2(c, A_LEAQ, mahint(c, n->str, c->cur_mod),
areg(D_CX));
ins2(c, op, amem(D_CX, 0), areg(D_X0));
goto ident_done;
}
@@ -2560,13 +2623,15 @@ cgexpr(Cg *c, Node *n, Local *locals)
int gop = let_islet(n->str)
? localloadop(n->type) : A_MOVQ;
if (gop == A_MOVQ) {
ins2(c, A_MOVQ, masym(c, n->str), areg(D_AX));
ins2(c, A_MOVQ, mahint(c, n->str, c->cur_mod),
areg(D_AX));
} else {
/* w6a has no MOVSXD/MOVSWQ/MOVSBQ D_EXTERN
* source form, so route through a LEAQ scratch
* the same way top-level str/slice/float lets
* do. */
ins2(c, A_LEAQ, masym(c, n->str), areg(D_CX));
ins2(c, A_LEAQ, mahint(c, n->str, c->cur_mod),
areg(D_CX));
ins2(c, gop, amem(D_CX, 0), areg(D_AX));
}
}
@@ -9354,7 +9419,7 @@ emit_data_row_zero(FILE *out, const char *dir, const char *name, int sz)
* shape doesn't reduce to a foldable float literal. */
static int
emit_floatlit_data(FILE *out, Cg *c, const char *directive,
const char *name, Type *t, Node *rhs)
const char *name, const char *module, Type *t, Node *rhs)
{
int isf32 = type_isf32(t);
int sz = isf32 ? 4 : 8;
@@ -9380,7 +9445,8 @@ emit_floatlit_data(FILE *out, Cg *c, const char *directive,
v = x.u;
}
}
fprintf(out, "%s %s(SB),\"", directive, mod_mangle(c, name));
fprintf(out, "%s %s(SB),\"", directive,
mod_mangle_value(c, name, module));
/* IEEE-754 sign-bit XOR for negation happens INSIDE the emit
* loop on the top byte only — semantically identical to a whole-
* u64 XOR with 2^63 (or 2^31 for f32) but never materialises
@@ -9541,11 +9607,12 @@ emit_struct_lit_bytes(FILE *out, Cg *c, Type *t, Node *rhs, u64 base)
* the type isn't a struct. */
static int
emit_struct_data(FILE *out, Cg *c, const char *directive,
const char *name, Type *t, Node *rhs)
const char *name, const char *module, Type *t, Node *rhs)
{
Type *u = (t && t->kind == TY_NAMED) ? t->under : t;
if (u == NULL || u->kind != TY_STRUCT) return 0;
fprintf(out, "%s %s(SB),\"", directive, mod_mangle(c, name));
fprintf(out, "%s %s(SB),\"", directive,
mod_mangle_value(c, name, module));
emit_struct_lit_bytes(out, c, t, rhs, 0);
fputs("\"\n", out);
return 1;
@@ -9784,12 +9851,13 @@ emit_array_lit_bytes(FILE *out, Cg *c, Type *t, Node *rhs, int emit_phase)
* bytes (would corrupt the asm if rhs reduces partway through). */
static int
emit_array_data(FILE *out, Cg *c, const char *directive,
const char *name, Type *t, Node *rhs)
const char *name, const char *module, Type *t, Node *rhs)
{
Type *u = type_unwrap(t);
if (u == NULL || u->kind != TY_ARRAY) return 0;
if (!emit_array_lit_bytes(out, c, t, rhs, 0)) return 0;
fprintf(out, "%s %s(SB),\"", directive, mod_mangle(c, name));
fprintf(out, "%s %s(SB),\"", directive,
mod_mangle_value(c, name, module));
emit_array_lit_bytes(out, c, t, rhs, 1);
fputs("\"\n", out);
return 1;
@@ -9805,7 +9873,7 @@ emit_lets(Cg *c, FILE *out, Node *file)
if (sz == 0) continue;
if (let_isfloat(d->type)) {
(void)emit_floatlit_data(out, c, "DATAW",
d->str, d->type, d->rhs);
d->str, d->module, d->type, d->rhs);
continue;
}
/* #129 A.2: gate `!let_isstruct` so an 8B struct lit
@@ -9830,7 +9898,8 @@ emit_lets(Cg *c, FILE *out, Node *file)
* round-trip via the sign-extended u64. */
if (!fold_int_literal(r, &v)) continue;
}
emit_data_row(out, "DATAW", mod_mangle(c, d->str), v);
emit_data_row(out, "DATAW",
mod_mangle_value(c, d->str, d->module), v);
continue;
}
/* Strip leading casts on the rhs so a `nil: str` etc.
@@ -9848,7 +9917,7 @@ emit_lets(Cg *c, FILE *out, Node *file)
if (sz == (int)ty_str->size && r != NULL && r->kind == N_STRLIT
&& r->strlen > 0) {
const char *lab = intern_strlit(c, r->str, r->strlen);
const char *sym = mod_mangle(c, d->str);
const char *sym = mod_mangle_value(c, d->str, d->module);
u64 v = r->strlen;
/* 16-byte payload: 8 zero placeholder + LE len. */
fprintf(out, "DATAW %s(SB),\"", sym);
@@ -9868,7 +9937,7 @@ emit_lets(Cg *c, FILE *out, Node *file)
* through to zero-init (existing path below). */
if (r != NULL && r->kind == N_ARRLIT && let_isarray(d->type)) {
if (emit_array_data(out, c, "DATAW", d->str,
d->type, r))
d->module, d->type, r))
continue;
/* fall through to zero-init */
}
@@ -9885,14 +9954,15 @@ emit_lets(Cg *c, FILE *out, Node *file)
* so the link surfaced an undefined ref. */
if (is_struct && r->kind == N_STRUCTLIT) {
if (emit_struct_data(out, c, "DATAW", d->str,
d->type, r))
d->module, d->type, r))
continue;
}
if (is_struct) continue;
if (is_array) continue;
if (r->kind != N_NIL && !empty_str) continue;
}
emit_data_row_zero(out, "DATAW", mod_mangle(c, d->str), sz);
emit_data_row_zero(out, "DATAW",
mod_mangle_value(c, d->str, d->module), sz);
}
}
@@ -9913,7 +9983,8 @@ emit_defs(Cg *c, FILE *out, Node *file)
if (d->kind != N_DEF || d->rhs == NULL) continue;
u64 v;
if (fold_int_literal(d->rhs, &v)) {
fprintf(out, "DATA %s(SB),\"", mod_mangle(c, d->str));
fprintf(out, "DATA %s(SB),\"",
mod_mangle_value(c, d->str, d->module));
for (int i = 0; i < 8; i++) {
unsigned b = (unsigned)((v >> (i * 8)) & 0xff);
if (b == '"' || b == '\\')
@@ -9932,7 +10003,7 @@ emit_defs(Cg *c, FILE *out, Node *file)
* at link. */
if (let_isfloat(d->type)) {
(void)emit_floatlit_data(out, c, "DATA",
d->str, d->type, d->rhs);
d->str, d->module, d->type, d->rhs);
continue;
}
/* #129 A.2: struct-typed def with N_STRUCTLIT rhs. Parallel
@@ -9942,7 +10013,7 @@ emit_defs(Cg *c, FILE *out, Node *file)
* for the LOAD path to find something. */
if (let_isstruct(d->type) && d->rhs->kind == N_STRUCTLIT) {
(void)emit_struct_data(out, c, "DATA",
d->str, d->type, d->rhs);
d->str, d->module, d->type, d->rhs);
continue;
}
/* #129 A.3: array-typed def with N_ARRLIT rhs. Parallel to
@@ -9951,7 +10022,7 @@ emit_defs(Cg *c, FILE *out, Node *file)
* LEAQ name(SB). */
if (let_isarray(d->type) && d->rhs->kind == N_ARRLIT) {
(void)emit_array_data(out, c, "DATA",
d->str, d->type, d->rhs);
d->str, d->module, d->type, d->rhs);
continue;
}
}