Files
ww/test/wcc/954_tuprecv_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

278 lines
9.4 KiB
C

/*
* 954_tuprecv_run — runtime + byte-id regression net for #102: the 16B
* whole-tuple-from-call receive (`let t = mk()`, then field reads t.0 /
* t.1).
*
* Root: wwstage's cglet had NO 16B tuple-from-call receive branch, so a
* `let t = call()` whose callee returns a 2-eightbyte (16B) tuple fell
* through to the generic single-word store (`MOVQ AX, off(BP)`) and
* never spilled word1 (the DX eightbyte) — silent loss of t.1. cstage
* has the branch (cmd/w6c/cgen.c:6652: spill AX -> off+0 AND DX ->
* off+8). The fix mirrors it in cglet.
*
* NOTE on the diagnosis: this is NOT a tupstore cursor off-by-one, and
* it is NOT f64-specific. The destructure form `let (a,b) = mk()`
* (cgmlet + tupstore cursor) was already byte-id; only the whole-tuple
* N_LET receive dropped word1. An f64 element only made it visible
* first, but the all-integer (i64,i64) receive dropped DX too — so the
* "all-integer control" row below is ALSO a fix row (it diverges on
* master 60c3e51, byte-id after the fix), not a pure no-op control.
*
* THIS TEST MUST CATCH A WWSTAGE-ONLY DIVERGENCE. cstage is correct
* before and after, so a cstage-only probe is gate-blind. Each row
* carries BOTH dimensions (modelled on 953_f64crossmod_run):
* (a) cstage `ww build` + run, asserting the exit code — pins the asm
* both stages converge on as runtime-correct.
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge. On master
* 60c3e51 (pre-fix) the three scalar-pair single-var rows each
* diverge by exactly one dropped `MOVQ DX, -8(BP)`; post-fix all
* are byte-identical.
*
* Three CONTROL rows pin that the fix touches nothing else: the
* destructure form `let (a,b) = mk()` (cgmlet + tupstore cursor), a
* 32B wide/str-element single-var tuple, and a 16B struct{i64,i64}
* single-var receive — all stay byte-id both pre- and post-fix. The fix
* is in cglet/N_LET, gated on sz==16 AND rettupleof != nil; the struct
* row is the over-catch guard: sz IS 16 but rettupleof returns nil for a
* non-tuple return, so the new branch must NOT hijack the struct's
* generic store. Without that guard a struct receive would route through
* the AX/DX tuple spill and miscompile any struct whose layout differs
* from {word0,word1}.
*/
#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; int chk_stamped; };
static const struct row rows[] = {
/* mixed (f64, i64): f=2.5 -> i32 2, i=7 -> i32 7, 2+7 = 9. The
* f64 element rides its eightbyte in AX at receive and is re-read
* from X0 at field-read; the i64 (t.1) rides DX — the dropped
* word. */
{ "f64_i64",
"package main;\n"
"fn mk() (f64, i64) = { return (2.5, 7); };\n"
"export fn main() i32 = {\n"
"\tlet t = mk();\n"
"\tlet f: f64 = t.0;\n"
"\tlet i: i64 = t.1;\n"
"\treturn (f: i32) + (i: i32);\n"
"};\n", 9 },
/* order-swap (i64, f64): f64 in the 2nd slot. i=7, f=2.5 -> 2,
* 7+2 = 9. Confirms the spill is position-agnostic (both words go
* over the integer cursor regardless of which holds the f64). */
{ "i64_f64",
"package main;\n"
"fn mk() (i64, f64) = { return (7, 2.5); };\n"
"export fn main() i32 = {\n"
"\tlet t = mk();\n"
"\tlet i: i64 = t.0;\n"
"\tlet f: f64 = t.1;\n"
"\treturn (i: i32) + (f: i32);\n"
"};\n", 9 },
/* all-integer (i64, i64): a=5, b=7, 5+7 = 12. NOT a no-op control
* — pre-fix wwstage dropped DX here too (proves the fix is not
* f64-gated). */
{ "i64_i64",
"package main;\n"
"fn mk() (i64, i64) = { return (5, 7); };\n"
"export fn main() i32 = {\n"
"\tlet t = mk();\n"
"\tlet a: i64 = t.0;\n"
"\tlet b: i64 = t.1;\n"
"\treturn (a: i32) + (b: i32);\n"
"};\n", 12 },
/* CONTROL — destructure form `let (f,i) = mk()` (cgmlet + tupstore
* cursor, a different and already-correct path; bootstrap relies on
* it, which is why 990-997 pass). The fix is in cglet/N_LET only, so
* this stays byte-id pre- and post-fix. f=2.5->2, i=7, 2+7 = 9. */
{ "ctl_destr",
"package main;\n"
"fn mk() (f64, i64) = { return (2.5, 7); };\n"
"export fn main() i32 = {\n"
"\tlet (f, i) = mk();\n"
"\treturn (f: i32) + (i: i32);\n"
"};\n", 9, 1 },
/* CONTROL — wide/str element single-var (i64, str) is a 32B tuple
* handled by the separate 32B receive branch; the fix's sz==16 gate
* excludes it, so it stays byte-id pre- and post-fix. t.0 = 7. */
{ "ctl_str",
"package main;\n"
"fn mk() (i64, str) = { return (7, \"hi\"); };\n"
"export fn main() i32 = {\n"
"\tlet t: (i64, str) = mk();\n"
"\treturn t.0: i32;\n"
"};\n", 7 },
/* CONTROL — over-catch guard. A 16B struct{i64,i64} from-call receive
* is sz==16 like the bug rows, but rettupleof returns nil for a
* non-tuple return, so the fix's new branch must NOT hijack it; it
* stays on the generic struct store and byte-id pre- and post-fix.
* Pins that the fix's tuple/struct discrimination (#102) holds. a=5,
* b=7, 5+7 = 12. */
{ "ctl_struct",
"package main;\n"
"type pair = struct { a: i64, b: i64 };\n"
"fn mk() pair = { return pair { a = 5, b = 7 }; };\n"
"export fn main() i32 = {\n"
"\tlet t = mk();\n"
"\treturn (t.a: i32) + (t.b: i32);\n"
"};\n", 12 },
{ 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, "tuprecv: 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++) {
/* src reused across the build-phase (a) AND the w6c/w6c_ww
* byte-id phase (b)/(c); keep src, outbin and both .s under one
* tmpdir and defer a single rm -rf to the end so the compiler's
* .sepwork scratch (derived from the source path) lands in
* tmpdir, not bare /tmp. */
char tmpdir[64], rmcmd[160];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwtup_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
char src[128];
snprintf(src, sizeof src, "%s/wwtup_%d_%d.ww",
tmpdir, getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; runwait(rmcmd); continue; }
fputs(rows[i].src, f);
fclose(f);
/* (a) cstage build + run; -o pins the binary into tmpdir. */
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/wwtup_%d_%d",
tmpdir, getpid(), i);
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: emit .s from both stages, cmp.
* FAILS on the pre-fix wwstage divergence (dropped MOVQ DX). */
char cs_s[128], ws_s[128];
snprintf(cs_s, sizeof cs_s, "%s/wwtup_%d_%d_cs.s",
tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwtup_%d_%d_ww.s",
tmpdir, 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++; 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++;
}
/* (c) #121 A-narrow stamp gate: the un-annotated float-
* destructure binding must now carry a checker type stamp, so
* w6c_ww emits no `asserttyped:` diagnostic. Non-vacuous —
* pre-stamp (HEAD) w6c_ww fires asserttyped on the f64 binding
* ident (nil n.type_). */
if (rows[i].chk_stamped) {
char errf[128];
snprintf(errf, sizeof errf,
"%s/wwtup_%d_%d_err.txt", tmpdir, getpid(), i);
snprintf(cmd, sizeof cmd,
"%s -o /dev/null %s 2>%s", w6c_ww, src, errf);
runwait(cmd);
snprintf(cmd, sizeof cmd,
"grep -q asserttyped %s", errf);
if (runwait(cmd) == 0) {
fprintf(stderr, "row[%s]: w6c_ww emitted "
"asserttyped (destructure binding "
"unstamped)\n", rows[i].label);
fail++;
}
}
runwait(rmcmd);
}
if (fail) {
fprintf(stderr, "%d/%d tuple-receive tests failed\n",
fail, n);
return 1;
}
printf("tuprecv: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
return 0;
}