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.
208 lines
7.1 KiB
C
208 lines
7.1 KiB
C
/*
|
|
* 913_def_mangle_run — byte-id regression net for #127: top-level
|
|
* `def X: T = LITERAL;` must emit cs==ww identical DATA symbols. Pre-
|
|
* #127 the wwstage `emitdefconstants` (selfhost/cmd/wcc/cgen.ww:1483)
|
|
* used a separate `d.exported`/`d.nmod` gate at the DATA-emit site
|
|
* instead of the same `emitsymname` mangler used at LOAD/CALL sites.
|
|
* When a non-exported `def X` in package main collided with the
|
|
* imported os.PATH_MAX (whose exported def landed too via the
|
|
* combined.ww embedding), the wwstage's DATA emitted the os one
|
|
* unqualified (`PATH_MAX(SB)`) while cstage emitted it module-
|
|
* qualified (`main.PATH_MAX(SB)`) — a 1-line cs/ww divergence in
|
|
* selfhost/cmd/ww/main.combined.ww that the 990-997 corpus didn't
|
|
* include.
|
|
*
|
|
* Fix (rule-12 sea-of-stars consolidation): replace the 8 lines of
|
|
* duplicated d.exported/d.nmod logic in emitdefconstants with a
|
|
* single `emitsymname(c, d.str)` call — the SAME mangler used at
|
|
* every LOAD/CALL site. Removes the asymmetry by construction:
|
|
* DATA-emit and LOAD-emit now route through one path. Cstage twin
|
|
* cmd/w6c/cgen.c:8494 already uses `mod_mangle(c, d->str)` for
|
|
* exactly this purpose.
|
|
*
|
|
* The original PATH_MAX consumer was cleaned up in 90d31c5 (drew's
|
|
* task #32 commit-1 dropped the duplicate main-local def in favor
|
|
* of the imported os.PATH_MAX), so the divergence is bootstrap-
|
|
* NEUTRAL post-#127 — confirmed by all 5 tool combined.ww emitting
|
|
* cs==ww byte-identical asm. This probe pins the consolidation
|
|
* forward: a new caller introducing a collision would now produce
|
|
* the same symbol shape from both stages, by construction.
|
|
*
|
|
* Rows exercise:
|
|
* - exported_def: `export def X: i32 = 4096;` — verifies the
|
|
* unqualified emit path (modlookup returns empty for exported).
|
|
* - main_local_def: `def X: i32 = 4096;` in package main — verifies
|
|
* the qualified emit path (modlookup returns "main"; both stages
|
|
* emit `main.X(SB)`).
|
|
* - load_site_match: a function reading the def — asserts the
|
|
* LOAD-site symbol shape matches the DATA-emit symbol shape (the
|
|
* core consolidation invariant — both routes through emitsymname).
|
|
*
|
|
* Each row carries (a) cstage `ww build` + run asserting the exit
|
|
* code and (b) w6c vs w6c_ww `.s` cmp (rule-10 byte-id).
|
|
*/
|
|
#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[] = {
|
|
/* Exported i64 def: emitsymname → modlookup returns empty
|
|
* (exported defs aren't tracked in mod_map per collectmods's
|
|
* `if (d.exported == 0)` gate) → unqualified `X(SB)` in DATA.
|
|
* i64 type chosen to dodge a separate pre-existing wwstage
|
|
* cgident divergence on narrow signed defs (extra MOVSXD on
|
|
* LOAD); the DEF-emit invariant this row pins is the DATA-side
|
|
* symbol shape, byte-id with cstage. */
|
|
{ "exported_def_i64",
|
|
"package main;\n"
|
|
"export def X: i64 = 100i64;\n"
|
|
"export fn main() i32 = { return X: i32; };\n", 100 },
|
|
/* Non-exported (package-private) i64 def: modlookup returns
|
|
* "main" → `main.X(SB)` qualified in DATA. LOAD site (cgident
|
|
* via emitsymname) also produces `main.X(SB)` — consolidation
|
|
* invariant: DATA and LOAD agree by construction. */
|
|
{ "main_local_def_i64",
|
|
"package main;\n"
|
|
"def X: i64 = 77i64;\n"
|
|
"export fn main() i32 = { return X: i32; };\n", 77 },
|
|
/* u64 width — pin that emitsymname doesn't differ on width. */
|
|
{ "u64_def",
|
|
"package main;\n"
|
|
"def X: u64 = 200u64;\n"
|
|
"export fn main() i32 = { return X: i32; };\n", 200 },
|
|
/* Multiple defs at package level — pin emitsymname is consistent
|
|
* across multiple DATA-emit calls (the surrounding loop walks
|
|
* file.list; per-def state should not leak). */
|
|
{ "multi_def",
|
|
"package main;\n"
|
|
"def A: u64 = 10u64;\n"
|
|
"def B: u64 = 30u64;\n"
|
|
"export def C: u64 = 60u64;\n"
|
|
"export fn main() i32 = { return (A + B + C): i32; };\n", 100 },
|
|
{ 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, "defmangle: w6c_ww missing — cannot run the "
|
|
"cs==ww byte-id gate (the whole point of this test)\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/wwdef_%d_d_%d",
|
|
getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
|
|
/* #8: source, binary, .sepwork scratch and the byte-id .s
|
|
* dumps all live under tmpdir; rm -rf on every exit path.
|
|
* Symbol mangling is package/import-derived, not entry-path
|
|
* derived, so the in-tmpdir source keeps the cs==ww .s identical. */
|
|
char src[128], outbin[128], cs_s[128], ws_s[128], rmcmd[160];
|
|
snprintf(src, sizeof src, "%s/wwdef_%d_%d.ww", tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/wwdef_%d_%d", tmpdir, getpid(), i);
|
|
snprintf(cs_s, sizeof cs_s, "%s/cs.s", tmpdir);
|
|
snprintf(ws_s, sizeof ws_s, "%s/ww.s", tmpdir);
|
|
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { runwait(rmcmd); fail++; continue; }
|
|
fputs(rows[i].src, f);
|
|
fclose(f);
|
|
|
|
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++;
|
|
}
|
|
|
|
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 def-mangle tests failed\n", fail, n);
|
|
return 1;
|
|
}
|
|
printf("defmangle: %d/%d ok (cstage run + cs==ww byte-id)\n",
|
|
n, n);
|
|
return 0;
|
|
}
|