Files
ww/test/wcc/919_strarray_static_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

205 lines
7.0 KiB
C

/*
* 919_strarray_static_run — BUG #18. Runtime + cs==ww byte-id net for a
* module-level `let xs: [N]str = ["a","b",…];` static table.
*
* THE BUG (BOTH stages dropped it — shared-logic gap, not rule-10):
* A module-level [N]str static init emitted NO .data at all. The
* per-element str header carries a ptr→rodata relocation (not just
* bytes), so it can't ride emit_array_lit_bytes (byte-only): the str-
* element case fell through to fold_int_literal, returned 0, and
* emit_lets zero-init'd / skipped — leaving `main.<tab>` undefined.
* `w6l: undefined reference` at link. Function-LOCAL [N]str worked
* (runtime element stores); only the module-level STATIC DATA form was
* broken.
*
* THE FIX (#18): emit_strarray_data / emitstrarraydata apply the scalar-
* str-global pattern (DATAW header with a zero ptr placeholder + inline
* LE len, then a per-element `DATAR sym+idx*esz(SB),_S_n(SB)`) at each
* element offset. let_pre_intern / letpreintern pre-intern each element
* strlit so the _S_ rodata rows precede the DATAR references. Scoped to
* the DATAW (`let`) directive — A_DATAR requires a DATAW holder.
*
* EACH ROW CARRIES BOTH DIMENSIONS (801 model):
* (a) cstage `ww build` + run, asserting the exit — proves the table's
* .len bytes are correct AND each element's .ptr relocation
* resolves to the right rodata label (the rows deref a NON-zero
* element's first byte, so a dropped/wrong reloc gives a wrong
* char — built-in negative control).
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge.
*
* GATE POLARITY: must stay GREEN. A wrong exit means the static table
* relocations regressed; a byte-id FAIL means the stages diverged.
*
* NB: the rows read elements via the `.ptr`/`.len` pseudo-fields, NOT
* the `len()` builtin — `len(xs[i])` on an indexed str element is a
* separate pre-existing read-side miscompile (returns the ptr; filed
* alongside #18), orthogonal to the emission fix under test here.
*/
#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[] = {
/* .len bytes: 2+3+1 == 6. A dropped table would fail to link. */
{ "strtab_len",
"package main;\n"
"let t: [3]str = [\"ab\",\"cde\",\"f\"];\n"
"export fn main() i32 = { return (t[0].len + t[1].len + t[2].len): i32; };\n",
6 },
/* element-2 .ptr reloc: *(t[2].ptr) == 'C' (67), NOT 'A' — proves
* the +2*esz relocation resolves to "C", not element 0. */
{ "strtab_ptr_elem2",
"package main;\n"
"let t: [3]str = [\"A\",\"B\",\"C\"];\n"
"export fn main() i32 = { let p: *u8 = t[2].ptr; return (*p): i32; };\n",
67 },
/* runtime-indexed .ptr: i=1, *(t[i].ptr) == 'y' (121) — the
* LEAQ tab(SB)+i*esz addressing reaches the right element's reloc. */
{ "strtab_ptr_varindex",
"package main;\n"
"let t: [3]str = [\"xx\",\"yyy\",\"z\"];\n"
"export fn main() i32 = { let i: i32 = 1; let p: *u8 = t[i].ptr; "
"return (*p): i32; };\n",
121 },
/* empty-element (no reloc, len 0) followed by a real element: t[0]
* len==0, *(t[1].ptr) == 'Z' (90) — the empty slot must not shift
* the following element's reloc offset. */
{ "strtab_empty_then_ptr",
"package main;\n"
"let t: [2]str = [\"\",\"Z\"];\n"
"export fn main() i32 = { if (t[0].len != 0) { return 88; }; "
"let p: *u8 = t[1].ptr; return (*p): i32; };\n",
90 },
/* repeat suffix `[X, Y...]` — the trailing `...` fills the rest of
* [3]str with the last explicit element ("Y"). t[2] is a repeat-
* filled slot: *(t[2].ptr) == 'Y' (89), NOT 'X' — proves the +48
* fill row's DATAR resolves to last_ev ("Y"), exercising the
* repeat branch (its own len-fill + DATAR loop) the other rows skip. */
{ "strtab_repeat_suffix",
"package main;\n"
"let t: [3]str = [\"X\",\"Y\"...];\n"
"export fn main() i32 = { let p: *u8 = t[2].ptr; return (*p): i32; };\n",
89 },
{ 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, "strarray_static: w6c_ww missing — cannot run "
"the cs==ww byte-id gate\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/wwsas_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
char rmcmd[160];
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
char src[128], outbin[128];
snprintf(src, sizeof src, "%s/wwsas_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwsas_%d_%d", tmpdir, getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { runwait(rmcmd); fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
/* (a) cstage build + run. */
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. */
char cs_s[128], ws_s[128];
snprintf(cs_s, sizeof cs_s, "%s/wwsas_%d_%d_cs.s", tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwsas_%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++;
}
runwait(rmcmd);
}
if (fail) {
fprintf(stderr, "%d/%d strarray static-init tests failed\n",
fail, n);
return 1;
}
printf("strarray_static: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}