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.
258 lines
8.0 KiB
C
258 lines
8.0 KiB
C
/*
|
|
* 940_mixed_scalar_tuple_sret_run — project #240: an over-cap tuple whose
|
|
* elements mix a scalar with slices/str (e.g. `(int, []u8, str)`) must lay
|
|
* out IDENTICALLY in both stages and round-trip through an sret return.
|
|
*
|
|
* Two SILENT, gate-blind (no bootstrap mixed-scalar tuple) cs!=ww
|
|
* divergences met here:
|
|
*
|
|
* (a) callee SEND (cstage cgen.c N_RETURN over-cap-tuple arm): the packed
|
|
* element offset advanced by the LITERAL expression's type size, not
|
|
* the declared return-type element size. A bare int literal element is
|
|
* stamped TY_UNTYPED_INT (size 0), so `foff += e->type->size` added 0
|
|
* for a leading scalar — the next element clobbered it at offset 0 and
|
|
* every trailing element packed 8 bytes low. wwstage already walked the
|
|
* return-type tuple (c.fnret.list) for the size, so the stages diverged
|
|
* on the callee frame's store offsets. Fix: size foff from cg_ret_type's
|
|
* tuple params (rule-13 type table), mirroring wwstage.
|
|
*
|
|
* (b) caller RECEIVE (wwstage cgenstmt.ww cglet N_TTUPLE arm): the register
|
|
* tuple-receive branch (designed for in-cap 16/32B tuples) had NO
|
|
* capacity gate, so a 56B over-cap tuple was received via AX/DX/CX/R8
|
|
* (+ R8 fill) instead of from the sret dest the callee actually wrote.
|
|
* cstage gates that branch on `sz == 16 || sz == 32` and falls an
|
|
* over-cap tuple through to the sret receive. Fix: add the same size
|
|
* gate to wwstage.
|
|
*
|
|
* Each program assigns DISTINCT, checkable values to every element (a scalar
|
|
* read directly, a slice/str via len()), and returns 0 only when all read
|
|
* back correctly — so a wrong offset (either bug) yields a nonzero exit.
|
|
* Both an annotated and an inferred `let t` are covered (same receive path).
|
|
*
|
|
* All K_RUN: build+run exit 0 on BOTH drivers AND cs==ww byte-identical.
|
|
* NNN<950, self-contained (/tmp, no imports), so rule-14's selfhost-sibling
|
|
* race does not apply (903/940/945 precedent).
|
|
*/
|
|
#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;
|
|
}
|
|
|
|
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), cb = fgetc(fb);
|
|
if (ca != cb) { rc = -1; break; }
|
|
if (ca == EOF) break;
|
|
}
|
|
fclose(fa); fclose(fb);
|
|
return rc;
|
|
}
|
|
|
|
struct row { const char *label; const char *src; int want; };
|
|
|
|
static const struct row rows[] = {
|
|
/* leading scalar (the canonical #240 case), annotated let. */
|
|
{ "int_slice_str",
|
|
"package main;\n"
|
|
"fn mk() (int, []u8, str) = {\n"
|
|
" let b: []u8; b.len = 5; b.cap = 9;\n"
|
|
" let s: str = \"abcd\";\n"
|
|
" return (42, b, s);\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
" let t: (int, []u8, str) = mk();\n"
|
|
" if (t.0 != 42) { return 1; };\n"
|
|
" if (len(t.1) != 5) { return 2; };\n"
|
|
" if (len(t.2) != 4) { return 3; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
/* same shape, inferred `let t = mk();` — exercises the receive
|
|
* path that picks up the type from the callee return. */
|
|
{ "int_slice_str_inferred",
|
|
"package main;\n"
|
|
"fn mk() (int, []u8, str) = {\n"
|
|
" let b: []u8; b.len = 6; b.cap = 8;\n"
|
|
" let s: str = \"xyz\";\n"
|
|
" return (17, b, s);\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
" let t = mk();\n"
|
|
" if (t.0 != 17) { return 1; };\n"
|
|
" if (len(t.1) != 6) { return 2; };\n"
|
|
" if (len(t.2) != 3) { return 3; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
/* trailing scalar: foff must reach the last element's offset. */
|
|
{ "slice_str_int",
|
|
"package main;\n"
|
|
"fn mk() ([]u8, str, int) = {\n"
|
|
" let b: []u8; b.len = 7; b.cap = 9;\n"
|
|
" let s: str = \"hello\";\n"
|
|
" return (b, s, 99);\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
" let t: ([]u8, str, int) = mk();\n"
|
|
" if (len(t.0) != 7) { return 1; };\n"
|
|
" if (len(t.1) != 5) { return 2; };\n"
|
|
" if (t.2 != 99) { return 3; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
/* scalar in the middle. */
|
|
{ "str_int_slice",
|
|
"package main;\n"
|
|
"fn mk() (str, int, []u8) = {\n"
|
|
" let s: str = \"abcde\";\n"
|
|
" let b: []u8; b.len = 3; b.cap = 4;\n"
|
|
" return (s, 77, b);\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
" let t: (str, int, []u8) = mk();\n"
|
|
" if (len(t.0) != 5) { return 1; };\n"
|
|
" if (t.1 != 77) { return 2; };\n"
|
|
" if (len(t.2) != 3) { return 3; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
};
|
|
|
|
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */
|
|
static int
|
|
run_driver(const char *driver, const struct row *r, int i)
|
|
{
|
|
char tmpdir[96], src[128], outbin[128], errf[128], rmcmd[160], cmd[1024];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/mxst_%d_d_%d", getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
snprintf(src, sizeof src, "%s/mxst_%d_%d.ww", tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/mxst_%d_%d", tmpdir, getpid(), i);
|
|
snprintf(errf, sizeof errf, "%s/err", tmpdir);
|
|
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (!f) { runwait(rmcmd); return -1; }
|
|
fputs(r->src, f);
|
|
fclose(f);
|
|
|
|
snprintf(cmd, sizeof cmd, "%s build -o %s %s >/dev/null 2>%s",
|
|
driver, outbin, src, errf);
|
|
int brc = runwait(cmd);
|
|
if (brc != 0) {
|
|
fprintf(stderr, "row[%s]: build via %s failed\n",
|
|
r->label, driver);
|
|
runwait(rmcmd);
|
|
return -1;
|
|
}
|
|
|
|
int got = runwait(outbin);
|
|
|
|
runwait(rmcmd);
|
|
if (got != r->want) {
|
|
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
|
|
r->label, driver, got, r->want);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* cs==ww .s byte-id (rule 10). */
|
|
static int
|
|
byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i)
|
|
{
|
|
char src[96], cs_s[96], ws_s[96], cmd[1024];
|
|
snprintf(src, sizeof src, "/tmp/mxst_bi_%d_%d.ww", getpid(), i);
|
|
snprintf(cs_s, sizeof cs_s, "/tmp/mxst_bi_%d_%d_cs.s", getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "/tmp/mxst_bi_%d_%d_ww.s", getpid(), i);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (!f) return -1;
|
|
fputs(r->src, f);
|
|
fclose(f);
|
|
|
|
int rc = 0;
|
|
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", r->label); rc = 1; }
|
|
else {
|
|
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", r->label); rc = 1; }
|
|
else if (slurp_eq(cs_s, ws_s) != 0) {
|
|
fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER "
|
|
"(#240 mixed-scalar tuple sret-layout regression)\n",
|
|
r->label);
|
|
rc = 1;
|
|
}
|
|
}
|
|
unlink(src); unlink(cs_s); unlink(ws_s);
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
const char *bin = getenv("BIN");
|
|
if (!bin) bin = "out/bin";
|
|
char absbin[512];
|
|
if (bin[0] != '/') {
|
|
char cwd[256];
|
|
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
|
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
|
bin = absbin;
|
|
}
|
|
|
|
char cdrv[640], wdrv[640], w6c[640], w6c_ww[640];
|
|
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
|
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
|
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
|
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
|
|
|
struct { const char *name; const char *path; 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].path, X_OK) != 0) {
|
|
fprintf(stderr, "mixed_scalar_tuple_sret: skip %s (no %s)\n",
|
|
drivers[d].name, drivers[d].path);
|
|
continue;
|
|
}
|
|
for (int i = 0; i < n; i++) {
|
|
total++;
|
|
if (run_driver(drivers[d].path, &rows[i], i) != 0) fail++;
|
|
}
|
|
}
|
|
|
|
if (access(w6c_ww, X_OK) == 0) {
|
|
for (int i = 0; i < n; i++) {
|
|
total++;
|
|
if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++;
|
|
}
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "mixed_scalar_tuple_sret: %d/%d checks failed\n",
|
|
fail, total);
|
|
return 1;
|
|
}
|
|
printf("mixed_scalar_tuple_sret: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|