Files
ww/test/wcc/703_nested_structlit.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

285 lines
8.7 KiB
C

/*
* 703_nested_structlit — silent zero of nested STRUCTLIT fields
* (task #17).
*
* Pre-existing landmine surfaced by worker-cgnassign during #5.
* For a struct literal whose field value is itself an N_STRUCTLIT
* of a struct-typed field, the outer field-walk did
* `cgexpr(field_value); store-AX-sized` — cgexpr has no whole-
* struct-in-register convention, so for a nested literal it lands
* AX = first qword and the trailing bytes silently stay zero (or
* stack garbage in the hostile case). The same shape applies at
* three sites in each stage:
* - N_LET N_STRUCTLIT — `let o: outer = outer { i = inner{...} }`
* - N_ASSIGN N_IDENT-lhs N_STRUCTLIT — `o = outer { i = inner{...} }`
* - N_RETURN N_STRUCTLIT — `return outer { i = inner{...} }`
*
* Workaround used in 701_cgassign_struct rows that need nested
* shape: `let o: outer; o.f = ...` (write-by-field). Avoids the
* bug; doesn't fix it.
*
* Fix: a shared helper `cg_structlit_fill_bp` (cstage) /
* `cgstructlitfillbp` (wwstage) factors the field-walk, recursing
* when a field's value is itself an N_STRUCTLIT for a struct-typed
* field. Bp-relative addressing only; the N_ASSIGN N_DOT-lhs
* structlit walks (via_ptr / global) keep their inline field-walk
* and still drop nested-STRUCTLIT silently — separate follow-up
* task ("cgen: nested STRUCTLIT in N_ASSIGN N_DOT structlit").
*
* Each row pins:
* - cstage value correctness (process exit code).
* - wwstage value correctness (when ww_ww exists).
* - cstage vs wwstage byte-identical .s output (catches drift).
*/
#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; };
static const struct row rows[] = {
/* N_LET nested structlit, all i64 fields. Pre-fix: only o.i.a
* (first qword) was stored; o.i.b silently stayed 0. Want:
* 7 + 8 + 12 = 27. */
{ "let_nested_i64",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"fn main() i32 = {\n"
" let o: outer = outer { i = inner { a = 7i64, b = 8i64 }, t = 12i64 };\n"
" return (o.i.a + o.i.b + o.t): i32;\n"
"};\n",
27 },
/* N_LET nested structlit, multi-level (3-deep). Pre-fix:
* only o.m.in.a was stored; b and t and outer.x silently 0.
* Want: 1+2+3+4 = 10. */
{ "let_nested_3deep",
"type leaf = struct { a: i64, b: i64 };\n"
"type middle = struct { in: leaf, t: i64 };\n"
"type outer = struct { m: middle, x: i64 };\n"
"fn main() i32 = {\n"
" let o: outer = outer {\n"
" m = middle {\n"
" in = leaf { a = 1i64, b = 2i64 },\n"
" t = 3i64\n"
" },\n"
" x = 4i64\n"
" };\n"
" return (o.m.in.a + o.m.in.b + o.m.t + o.x): i32;\n"
"};\n",
10 },
/* N_LET nested structlit, narrow i32 fields. Pins sized-store
* dispatch in the helper — MOVL not MOVQ. Want: 4+5+6+7 = 22. */
{ "let_nested_i32",
"type inner = struct { a: i32, b: i32 };\n"
"type outer = struct { i: inner, x: i32, y: i32 };\n"
"fn main() i32 = {\n"
" let o: outer = outer { i = inner { a = 4, b = 5 }, x = 6, y = 7 };\n"
" return o.i.a + o.i.b + o.x + o.y;\n"
"};\n",
22 },
/* N_ASSIGN N_IDENT-lhs nested structlit. Pre-fix: o.i.b
* stayed 0 after the reassign (only first qword stored).
* Want: 9+11+30 = 50. */
{ "assign_ident_nested",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"fn main() i32 = {\n"
" let o: outer;\n"
" o = outer { i = inner { a = 9i64, b = 11i64 }, t = 30i64 };\n"
" return (o.i.a + o.i.b + o.t): i32;\n"
"};\n",
50 },
/* N_RETURN nested structlit. Pre-fix: only b's first qword
* (its .a) landed in the receive slot; b's .b silently 0.
* Want: 13+17+25 = 55. */
{ "return_nested",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"fn mko() outer = {\n"
" return outer { i = inner { a = 13i64, b = 17i64 }, t = 25i64 };\n"
"};\n"
"fn main() i32 = {\n"
" let r: outer = mko();\n"
" return (r.i.a + r.i.b + r.t): i32;\n"
"};\n",
55 },
/* Mixed: inner struct surrounded by other scalar fields, with
* the inner appearing in the MIDDLE of the outer (non-zero
* outer foff for the recursion target). Pins that the helper
* threads `bpoff + outer_foff` correctly. All fields are i64 so
* the wwstage/cstage struct-layout alignment divergence (task
* #15, mixed i32/struct fields) doesn't bite — switch to i32
* once #15 lands. Sum: 100+1+2+50 = 153. */
{ "let_nested_middle",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { pre: i64, i: inner, post: i64 };\n"
"fn main() i32 = {\n"
" let o: outer = outer {\n"
" pre = 100i64,\n"
" i = inner { a = 1i64, b = 2i64 },\n"
" post = 50i64\n"
" };\n"
" return (o.pre + o.i.a + o.i.b + o.post): i32;\n"
"};\n",
153 },
};
/* run_driver — compile r->src via the given driver and exec; return
* the process exit code. Mirror of 701/702. */
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcns_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/wcns_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wcns_%d_%d", tmpdir, getpid(), i);
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", driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
/* asm_byte_identical — generate .s via cstage's w6c and wwstage's
* w6c_ww and diff. Mirror of 701/702. */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[64], cs[64], ws[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/wcns_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/wcns_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/wcns_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
if (!fc || !fw) {
rc = -1;
} else {
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
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 cdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[1024];
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; int gated_on_existence; }
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_on_existence
&& access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "nested_structlit: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != rows[i].want) {
fprintf(stderr,
"nested_structlit[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"nested_structlit: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("nested_structlit: %d/%d ok\n", total, total);
return 0;
}