Files
ww/test/wcc/795_xmod_valglobal_run.c
Hojun-Cho 8481a05c3a 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.
2026-06-01 09:01:56 +09:00

217 lines
7.5 KiB
C

/*
* 795_xmod_valglobal_run — project #1 close, the cgen residual of #55.
* Runtime + cs==ww byte-id net for the cross-module bare value-GLOBAL
* load miscompile. Sibling of the CHECKER test 794_xmod_ident_prefer,
* which deliberately omitted a byte-id assertion because THIS cgen bug
* would diverge the asm independently (see 794's NOTE). With #1 fixed,
* the byte-id now holds and is asserted here.
*
* THE BUG (cgen-only, both stages emit IDENTICAL wrong asm → byte-id
* was BLIND to it): a bare cross-module value-global load mangled its
* symbol via a NON-preferring leaf lookup (cstage masym->mod_mangle->
* mod_lookup first-match; wwstage emitsymname->modlookup). With
* package aa; export let v: i32 = 7; fn getv() = { return v; }
* package main; import aa; let v: i32 = 99; main = { return aa.getv(); }
* the bare `v` inside aa.getv resolved to main.v, and aa.v's DATA slot
* was ALSO labeled main.v (collision) — so aa.getv() returned 99, not 7.
* Functions were already correct (they thread a cur_mod hint via mafn/
* emitfnname); value globals did not.
*
* THE FIX (#1): reference-site mangle uses the resolved module (curmod-
* prefer for bare idents); definition-site mangle uses the decl's OWN
* module — threaded per-site the way fns already do (cstage mahint with
* c->cur_mod / d->module; wwstage emitsymnamehint with c.curmod /
* d.nmod). The value lookup mangles ONLY on an exact (name, module)
* match and otherwise leaves the name BARE — no first-leaf fallback —
* because exported value globals are export-skipped from the module map
* (they keep their bare-name data ABI), and a first-match fallback would
* mis-mangle an exported `v` onto another module's private `v`. So the
* exported aa.v stays `v`, the private main.v stays `main.v`, no
* collision.
*
* EACH ROW CARRIES BOTH DIMENSIONS (953_f64crossmod_run model):
* (a) cstage `ww build` (in a /tmp scratch dir) + run, asserting the
* exit code — pins that 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 (both stages were wrong the
* same way); a green byte-id alone never caught it, so the runtime
* row (a) is the real net and (b) guards rule-10 going forward.
*
* Single-file multi-package form (like 953): `package aa; ... package
* main; import aa; ...` in one source, so w6c/w6c_ww see the cross-
* module reference without -I path plumbing, and the build is self-
* contained (no selfhost traversal → no .combined.ww fixture race, so
* this lives in the parallel phase, not the 990-997 skip-set).
*
* GATE POLARITY: must stay GREEN. A wrong exit means the value-global
* mangle regressed (collision returned); a byte-id FAIL means the stages
* diverged on the mangle (rule-10 violation).
*
* SCOPE NOTE: str/slice/array value-globals are intentionally NOT rowed
* here — `len(str-global)` and indexed-global reads trip a SEPARATE,
* still-open cs!=ww divergence in the load body (the N_DOT-base / index
* sibling tracked as #229), orthogonal to this symbol-mangle fix.
*/
#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_exit; };
static const struct row rows[] = {
/* canonical #1: exported aa.v (7) vs private main.v (99); aa.getv()
* must read aa's v, not collide onto main.v. Pre-fix: 99. */
{ "i32_let_export_vs_private",
"package aa;\n"
"export let v: i32 = 7;\n"
"export fn getv() i32 = { return v; };\n"
"package main;\n"
"import aa;\n"
"let v: i32 = 99;\n"
"export fn main() i32 = { return aa.getv(); };\n", 7 },
/* def-constant flavour: exported def vs private def, same leaf. */
{ "def_export_vs_private",
"package aa;\n"
"export def K: i32 = 5;\n"
"export fn getk() i32 = { return K; };\n"
"package main;\n"
"import aa;\n"
"def K: i32 = 88;\n"
"export fn main() i32 = { return aa.getk(); };\n", 5 },
/* float flavour: exercises the LEAQ+MOVSD load arm and the
* emit_floatlit_data DATA label. aa.getf() == 2.5 -> i32 2. */
{ "f64_let_export_vs_private",
"package aa;\n"
"export let f: f64 = 2.5;\n"
"export fn getf() f64 = { return f; };\n"
"package main;\n"
"import aa;\n"
"let f: f64 = 9.9;\n"
"export fn main() i32 = { return aa.getf(): i32; };\n", 2 },
{ 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: 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/wwxmv_%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 so intermediates and
* the output binary land there. */
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwxmv_%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/wwxmv_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwxmv_%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 value-global tests failed\n", fail, n);
return 1;
}
printf("xmod_valglobal: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
return 0;
}