Files
ww/test/wcc/989_globstructwiden_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

154 lines
4.4 KiB
C

/*
* 989_globstructwiden_run — F8-c7 (report-item #50, #263): widening a module-
* GLOBAL struct ident into a tagged union (`let x: (S | str) = gs;`) must
* copy gs's full payload into the box, not just word0.
*
* THE BUG (cat-A silent miscompile, #263 BOTH-WRONG, now CLOSED both stages):
* the struct-payload widen arm keyed off a local/literal-only struct-name
* lookup, so a module-global struct ident fell to the scalar word0 arm — only
* the first 8 bytes reached the box (wwstage), or the payload zero-filled
* (cstage, gs never read). The ww half landed in F8-c7; the cstage half (a
* global-struct-source branch — LEAQ g(SB),SI via aggarg_srcaddr, byte-copy
* into slot+8) landed in ww-core TASK #43. Both stages now copy the full
* payload and the .s is byte-identical.
*
* Rows assert the field-equality match returns 0 on both stages (cs==ww).
* row | shape | exit
* -----------+---------------------------------------------+-----
* widen_16 | gp:{a=5,b=6}; (pt|str)=gp; match pt fields | 0
* widen_24 | gp:{a=5,b=6,c=7}; (tri|str)=gp; match fields | 0
*/
#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_cs;
int want_ww;
};
static const struct row rows[] = {
{ "widen_16",
"package main;\n"
"type pt = struct { a: i64, b: i64 };\n"
"let gp: pt = pt { a = 5, b = 6 };\n"
"export fn main() int = {\n"
" let x: (pt | str) = gp;\n"
" match (x) {\n"
" case let q: pt => { if (q.a == 5 && q.b == 6) { return 0; }; return 2; };\n"
" case str => return 3;\n"
" };\n"
"};\n",
0, 0 },
{ "widen_24",
"package main;\n"
"type tri = struct { a: i64, b: i64, c: i64 };\n"
"let gp: tri = tri { a = 5, b = 6, c = 7 };\n"
"export fn main() int = {\n"
" let x: (tri | str) = gp;\n"
" match (x) {\n"
" case let q: tri => { if (q.a == 5 && q.b == 6 && q.c == 7) { return 0; }; return 2; };\n"
" case str => return 3;\n"
" };\n"
"};\n",
0, 0 },
};
/* run_build — build+run `src` via `driver`; returns the binary's exit
* code, or -1 on a build failure. */
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[128], tmpdir[64], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/gsw_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/gsw_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/gsw_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -2; }
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, src);
int brc = runwait(cmd);
int got = -1;
if (brc == 0) got = runwait(outbin);
runwait(rmcmd);
return brc == 0 ? got : -1;
}
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 cdrv[1024], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "globstructwiden_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
int is_ww = (d == 1);
for (int i = 0; i < n; i++) {
total++;
int want = is_ww ? rows[i].want_ww : rows[i].want_cs;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != want) {
fprintf(stderr, "globstructwiden_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "globstructwiden_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("globstructwiden_run: %d/%d ok\n", total, total);
return 0;
}