Files
ww/test/wcc/949_valstruct_subsize_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

252 lines
7.6 KiB
C

/*
* 949_valstruct_subsize_run — runtime + byte-id net for #254: a sub-8
* (non-8-multiple) nested value-struct must size its zero-init extent
* from the type table's natural ABI size (cstage lu->size), NOT the
* slot-padded register-struct width.
*
* Root: wwstage conflated SLOT-size (round-to-8, for frame layout) with
* ABI-size (true). A nested value-struct field was sized via fieldsize()
* (cgenutil.ww TY_STRUCT -> ti.slotsize = 8), poisoning structabisize +
* registerstruct si.totsize to 8 for a struct whose true ABI size is 4.
* Two emission sites then over-sized:
* D1 (local): cglet zsz = structabisize = 8 hit the `zsz == 8` zero
* arm (cgenstmt.ww #213) -> a stray `MOVQ $0, off(BP)` that cstage
* (ABI 4 is sub-8 -> left uninit per the shared no-rhs policy)
* never emits.
* D2 (global): emitletdataw struct arm wrote si.totsize = 8 zero bytes
* of DATAW; cstage cg_let_emit_size returns u->size = 4.
* Both were SILENT cs!=ww divergences (gate-blind: a standalone wwstage
* is self-consistent; only the cs==ww .s cmp catches it).
*
* Fix (rule-13 SSoT): both sites source the extent from tinfo.size
* (peeling TY_NAMED), the same value cstage reads. fieldsize /
* registerstruct / frame slot-padding stay UNTOUCHED — moving the fix
* into the size helpers would shift nested-struct field offsets and
* re-diverge other byte-id.
*
* Rows cover the whole sub-8 class (ABI size 1/2/4) in both the local
* (D1) and global (D2) emission contexts, plus a >8 NEGATIVE control
* proving the fix didn't disable legitimate multi-word zero-init. Each
* row: cstage `ww build` + run for the exit code (correctness) AND
* w6c vs w6c_ww `.s` cmp for rule-10 byte-id (the silent-divergence net).
*/
#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[] = {
/* D1 local, ABI size 4 (inner {[4]u8}). Pre-fix wwstage emitted a
* stray MOVQ $0 cstage didn't -> .s differ. Write+read a byte so
* the exit is deterministic (sub-8 is left uninit by BOTH stages,
* matching cstage's no-rhs policy). */
{ "d1_local_4",
"package main;\n"
"type inner = struct { m: [4]u8 };\n"
"type outv = struct { i: inner };\n"
"export fn main() i32 = {\n"
" let o: outv;\n"
" o.i.m[0] = 66u8;\n"
" return o.i.m[0]: i32;\n"
"};\n", 66 },
/* D1 local, ABI size 2 ([2]u8). */
{ "d1_local_2",
"package main;\n"
"type inner = struct { m: [2]u8 };\n"
"type outv = struct { i: inner };\n"
"export fn main() i32 = {\n"
" let o: outv;\n"
" o.i.m[1] = 55u8;\n"
" return o.i.m[1]: i32;\n"
"};\n", 55 },
/* D1 local, ABI size 1 ([1]u8) — the tightest sub-8 case. */
{ "d1_local_1",
"package main;\n"
"type inner = struct { m: [1]u8 };\n"
"type outv = struct { i: inner };\n"
"export fn main() i32 = {\n"
" let o: outv;\n"
" o.i.m[0] = 44u8;\n"
" return o.i.m[0]: i32;\n"
"};\n", 44 },
/* D2 global, ABI size 4. Pre-fix wwstage emitted DATAW of 8 zero
* bytes vs cstage's 4 -> .s differ. */
{ "d2_global_4",
"package main;\n"
"type inner = struct { m: [4]u8 };\n"
"type outv = struct { i: inner };\n"
"let g: outv;\n"
"export fn main() i32 = {\n"
" g.i.m[0] = 66u8;\n"
" return g.i.m[0]: i32;\n"
"};\n", 66 },
/* D2 global, ABI size 2. */
{ "d2_global_2",
"package main;\n"
"type inner = struct { m: [2]u8 };\n"
"type outv = struct { i: inner };\n"
"let g: outv;\n"
"export fn main() i32 = {\n"
" g.i.m[1] = 55u8;\n"
" return g.i.m[1]: i32;\n"
"};\n", 55 },
/* D2 global, ABI size 1. */
{ "d2_global_1",
"package main;\n"
"type inner = struct { m: [1]u8 };\n"
"type outv = struct { i: inner };\n"
"let g: outv;\n"
"export fn main() i32 = {\n"
" g.i.m[0] = 44u8;\n"
" return g.i.m[0]: i32;\n"
"};\n", 44 },
/* NEGATIVE control — a >8 (multi-word) value-struct still zero-
* inits. Read an UNWRITTEN byte: a working multi-word zero-init
* fill makes it 0. If the fix had wrongly suppressed the >8 zero
* arm, this would read stack garbage (and byte-id would diff
* against the still-zeroing cstage). Local + global both proven. */
{ "ctl_local_16",
"package main;\n"
"type inner = struct { m: [16]u8 };\n"
"type outv = struct { i: inner };\n"
"export fn main() i32 = {\n"
" let o: outv;\n"
" return o.i.m[7]: i32;\n"
"};\n", 0 },
{ "ctl_global_16",
"package main;\n"
"type inner = struct { m: [16]u8 };\n"
"type outv = struct { i: inner };\n"
"let g: outv;\n"
"export fn main() i32 = {\n"
" return g.i.m[7]: i32;\n"
"};\n", 0 },
{ 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, "valstruct_subsize: 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/wwvss_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char rmcmd[160];
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
char src[128], outbin[128], cs_s[128], ws_s[128];
snprintf(src, sizeof src, "%s/wwvss_%d_%d.ww",
tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwvss_%d_%d",
tmpdir, getpid(), i);
snprintf(cs_s, sizeof cs_s, "%s/wwvss_%d_%d_cs.s",
tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwvss_%d_%d_ww.s",
tmpdir, getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; runwait(rmcmd); 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 valstruct-subsize tests failed\n",
fail, n);
return 1;
}
printf("valstruct_subsize: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}