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

207 lines
7.0 KiB
C

/*
* 949_structlit_arrfield_run — runtime + byte-id net for #249: struct
* array-field init/read silent miscompiles. Two distinct roots, both
* gate-blind (cs==ww broken identically pre-fix):
*
* BUG B — reading an array field of a module-GLOBAL struct value
* (`G.arr[i]`). The N_INDEX fallback's cg_dotbase_addr / dotbaseaddr
* helper (the #135 sibling) had no module-global-struct base arm:
* cstage emitted `LEAQ (BP)` (read the stack → 0), wwstage fell to
* cgexpr(N_DOT) which loaded the field VALUE as a pointer → SEGFAULT.
* The .data was already correct (emit_struct_lit_bytes #129 A.3); only
* the READ base address was wrong. Fix: a global value-struct base
* emits `LEAQ name(SB) (+ ADDQ field_off)`, mirroring the scalar
* global-field read (cgen.c:7532). const globals are def_isstructdef.
*
* BUG A — initializing an array field from a struct literal
* (`e{ arr = [..] }`) as a local. cg_structlit_fill / cgstructlitfill
* had no TY_ARRAY field arm; the array field fell to the generic
* scalar tail (cgexpr the N_ARRLIT → AX, store one sized word) which
* silently DROPPED every element. Fix: a TY_ARRAY field arm element-
* wise stores the N_ARRLIT at base+field_off+i*esz, reusing the proven
* N_LET array-init shape. The GLOBAL literal-init path is unaffected
* (it goes through emit_struct_lit_bytes, already correct).
*
* cstage `ww build` + run for exit code; w6c vs w6c_ww `.s` cmp for the
* rule-10 byte-id gate. u8 element rows only (a `[N]u8` read cast to i32
* is byte-id; wider widths hit the i32-return MOVL/MOVSXD cs/ww
* divergence — see 949_dotbase_arr_run's deferral note).
*/
#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[] = {
/* BUG B: read [4]u8 field of a module-global `let` struct at idx 0
* (field_off 0). Pre-fix cstage→0, wwstage→SEGFAULT. encmap[0]='A'
* (65). */
{ "global_let_read",
"package main;\n"
"type e = struct { encmap: [4]u8 };\n"
"let g: e = e { encmap = [65u8, 66u8, 67u8, 68u8] };\n"
"export fn main() i32 = { return g.encmap[0]: i32; };\n", 65 },
/* BUG B: read [4]u8 field of a module-global `def` (const) struct
* at idx 2, with a non-zero field offset (a [3]u8 pad ahead of it) —
* exercises def_isstructdef + the ADDQ $field_off arm. The base64
* `const std_encoding` shape. pad ahead, encmap[2]='C' (67). */
{ "global_def_read_off",
"package main;\n"
"type e = struct { pad: [3]u8, encmap: [4]u8 };\n"
"def G: e = e { pad = [9u8, 9u8, 9u8],"
" encmap = [65u8, 66u8, 67u8, 68u8] };\n"
"export fn main() i32 = { return G.encmap[2]: i32; };\n", 67 },
/* BUG A: local struct-literal init of a [4]u8 field, read idx 0.
* Pre-fix the array field fell to the generic scalar tail (cgexpr
* the N_ARRLIT → AX, store one word) and silently dropped every
* element → 0. encmap[0]='A' (65). */
{ "local_lit_read0",
"package main;\n"
"type e = struct { encmap: [4]u8 };\n"
"export fn main() i32 = {\n"
" let g: e = e { encmap = [65u8, 66u8, 67u8, 68u8] };\n"
" return g.encmap[0]: i32;\n"
"};\n", 65 },
/* BUG A: same init, read idx 3 — asserts the LAST element landed
* (the pre-fix single-word store would never reach it). 'D' (68). */
{ "local_lit_read3",
"package main;\n"
"type e = struct { encmap: [4]u8 };\n"
"export fn main() i32 = {\n"
" let g: e = e { encmap = [65u8, 66u8, 67u8, 68u8] };\n"
" return g.encmap[3]: i32;\n"
"};\n", 68 },
/* NB: a trailing `...` repeat in a struct-LITERAL array field
* (`encmap = [7u8...]`) is rejected by the CHECKER ("[1]u8 not
* assignable to [4]u8") — the field type-check doesn't apply the
* repeat-length inference that bare `let a: [N]T = [v...]` gets.
* The cg_structlit_fill array arm mirrors the N_LET `...` handling
* for symmetry, but that path is checker-unreachable today (separate
* checker gap, not #249). No row exercises it. */
{ 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, "structlit_arrfield: 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/wwsaf_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char src[128], outbin[128], cs_s[160], ws_s[160], rmcmd[160];
snprintf(src, sizeof src, "%s/wwsaf_%d_%d.ww",
tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwsaf_%d_%d",
tmpdir, getpid(), i);
snprintf(cs_s, sizeof cs_s, "%s/wwsaf_%d_%d_cs.s",
tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwsaf_%d_%d_ww.s",
tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
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 structlit-arrfield tests failed\n",
fail, n);
return 1;
}
printf("structlit_arrfield: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}