Files
ww/test/wcc/796_xmod_valglobal_dot_run.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the
compiler's <stem>.sepwork scratch landed beside the source and was never
cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs
and fabricates phantom test failures + silent harness aborts, and for
in-repo fixture builds leaked .sepwork into the tracked tree.

Each leaking build now writes its source + output inside a per-invocation
tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and
rm -rf's the tmpdir on every exit path -- including fopen-fail and the
expected-fail reject builds (scratch is mkdir'd before the build can fail).
`ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993
byte-id comparison logic is byte-for-byte unchanged.

Two items filed separately (this commit holds the no-Makefile / no-main.c
rail):
- #13: a stale <src>.s byte-id readback (749) silently no-ops since
  separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline.
- #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no
  -o and leak main.sepwork in-tree (bounded, gitignored; own commit).

One concern -- sepwork leak hygiene -- across 228 drivers; uniform
transform applied per-file and two-round reviewed. make test: all 402
passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
2026-06-22 23:29:39 +09:00

173 lines
5.3 KiB
C

/*
* 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 <stdio.h>
#include <stdlib.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[] = {
/* 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 tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwxmvd_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
char src[128], outbin[128], cs_s[128], ws_s[128], rmcmd[160];
snprintf(src, sizeof src, "%s/wwxmvd_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwxmvd_%d_%d", tmpdir, getpid(), i);
snprintf(cs_s, sizeof cs_s, "%s/wwxmvd_%d_%d_cs.s", tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwxmvd_%d_%d_ww.s", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; runwait(rmcmd); continue; }
fputs(rows[i].src, f);
fclose(f);
/* (a) cstage build + run. */
char cmd[2048];
snprintf(cmd, sizeof cmd, "%s/ww build -o %s %s", bin, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: cstage build failed\n",
rows[i].label);
fail++;
runwait(rmcmd);
continue;
}
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++;
}
/* (b) cs==ww byte-id gate. */
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++; runwait(rmcmd); 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++; runwait(rmcmd); 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++;
}
runwait(rmcmd);
}
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;
}