Files
ww/test/wcc/940_str_forrange_loopvar_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

164 lines
5.0 KiB
C

/*
* 940_str_forrange_loopvar_run — corpus coverage for the step-3 Fold 1
* fold: ranging a `str` value-form (`for (let b .. s)`) and reading the
* loop var `b` back must narrow to MOVZBQ (u8 zero-extend), matching
* cstage. Pre-fold the wwstage cgen registered the loop var with a nil
* tnode (str had no element node the way []u8 does), so the read-back
* fell through localloadop to a wide MOVQ; cstage's checker stamps the
* binding u8, so it narrowed. The fold synthesises the u8 element off
* str.sub (Phase 2 F1) so the loop var carries a u8 tnode and the
* existing narrow-load logic fires on its own.
*
* REAL GATE IS SHAPE BYTE-ID, NOT RUNTIME. The MOVQ-vs-MOVZBQ
* divergence is runtime-benign: the slot is always written by a
* MOVZBQ-into-AX then MOVQ-AX-into-slot, so the upper bytes are already
* zero — a MOVQ read-back yields the same value a MOVZBQ would. A
* runtime probe therefore passes on BOTH the buggy and the fixed code
* and CANNOT distinguish them. The load-bearing gate is diffing the
* cstage (w6c) and wwstage (w6c_ww) .s at the loop-var read:
* pre-fold: cstage MOVZBQ vs wwstage MOVQ (1-line diff)
* post-fold: byte-identical
* This fixture exists for corpus presence — it confirms the path lowers
* and runs correctly through both drivers; it does not by itself prove
* the narrow.
*
* Loop-var read-back is exercised two ways the fold touches:
* - Site A: pass `b` to a fn (`use1(b)` → cgident read into an arg).
* - Site B: use `b` in an arithmetic expression (`sum += b: i32`).
*/
#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[] = {
/* Site A — read-back into a fn arg. "AB" is 0x41,0x42; the callee
* checks each byte zero-extends to its exact value, so a botched
* wide read (had the slot held garbage) would mismatch. */
{ "sitea_arg_readback",
"fn check(x: u8) i32 = {\n"
" if (x: i32 < 65) { return 1; };\n"
" if (x: i32 > 66) { return 1; };\n"
" return 0;\n"
"};\n"
"export fn main() i32 = {\n"
" let s: str = \"AB\";\n"
" for (let b .. s) {\n"
" if (check(b) != 0) { return 1; };\n"
" };\n"
" return 0;\n"
"};\n",
0 },
/* Site B — read-back in an arithmetic expression. Sum the bytes of
* "AB" (0x41 + 0x42 = 131) and confirm. */
{ "siteb_expr_readback",
"export fn main() i32 = {\n"
" let s: str = \"AB\";\n"
" let sum: i32 = 0;\n"
" for (let b .. s) {\n"
" sum += b: i32;\n"
" };\n"
" if (sum != 131) { return 1; };\n"
" return 0;\n"
"};\n",
0 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[96], src[160], outbin[160], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/strforrange_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/strforrange_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/strforrange_%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;
}
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];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[640];
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,
"str_forrange_loopvar_run: 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,
"str_forrange_loopvar_run[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "str_forrange_loopvar_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("str_forrange_loopvar_run: %d/%d ok\n", total, total);
return 0;
}