Files
ww/test/wcc/914_arr_u16_store_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

218 lines
6.9 KiB
C

/*
* 914_arr_u16_store_run — runtime + byte-id net for #128a: array-
* literal init into [N]u16 (or any [N]T where esz==2) must store with
* MOVW, not MOVQ. Pre-fix cstage's cgen.c:7180-7222 array-init dispatch
* routed esz==2 to MOVQ fall-through (the comment at 7194-7198
* documented + deferred this until A_MOVW landed in w6a); the MOVQ
* wrote 8 bytes into a 2-byte slot, overlapping the next 6 bytes of
* stack. Adjacent fully-init writes accident-corrected via overlap
* (each MOVQ rewrote the prior MOVQ's trailing 6B), but a PARTIAL
* init left high garbage in slots that should have been default-zero.
*
* Fix is the now-unblocked dispatch: `else if (esz == 2) op = A_MOVW;`
* The wwstage emitter was already correct (uses MOVW); the cstage gap
* was the deferred TODO. Both stages now emit byte-identical MOVW for
* [N]u16 (and any aliased-narrow-element [N]T whose tinfo.size == 2).
*
* Rows cover (the bug is caught via the rule-10 cs==ww byte-id gate;
* ww doesn't accept truly-partial init literals, and adjacent MOVQ
* writes accident-correct the runtime values, so byte-id is the lever):
* - u16_full_init: fully-init [4]u16, regression guard (pre-fix
* accident-correct at runtime via MOVQ overlap; .s shifts
* MOVQ→MOVW, cs==ww BYTE-IDENTICAL now)
* - u16_small_values: small u16 values, summed (regression guard)
* - u8_control: [4]u8 baseline (already MOVB pre-fix, no shift)
* - i16_signed: signed-narrow [N]i16, same dispatch — verifies the
* fix isn't gated on unsignedness
*
* TY_NAMED alias of u16 (`type myw = u16; let a: [4]myw = …`) is a
* sibling miscompile filed separately: wwstage's array-init element-
* size dispatch reads `primsize(elemn.str)` which returns 0 for an
* alias name, leaving esz at 8 (wrong slot, wrong stride). Out of
* scope for #128a (cstage MOVW landing).
*
* Each row carries (a) cstage `ww build` + run asserting the exit
* code and (b) w6c vs w6c_ww `.s` cmp (rule-10 byte-id). Post-#128a
* the partial-init row's cstage emission gains MOVW; both stages
* unchanged everywhere else.
*/
#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[] = {
/* Fully-init [4]u16: pre-fix MOVQ overlap accident-corrected;
* post-fix proper MOVW per slot. Sum of last bytes = 0xBE = 190;
* mod 256 = 190. We assert per-element to catch any value drift. */
{ "u16_full_init",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]u16 = [0xABCDu16, 0xBEEFu16, 0xC0DEu16, 0xDEADu16];\n"
" if (a[0] != 0xABCDu16) { return 1; };\n"
" if (a[1] != 0xBEEFu16) { return 2; };\n"
" if (a[2] != 0xC0DEu16) { return 3; };\n"
" if (a[3] != 0xDEADu16) { return 4; };\n"
" return 0;\n"
"};\n", 0 },
/* Same shape with smaller values — pin that the dispatch fires
* on values that don't need the upper 16 bits. */
{ "u16_small_values",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]u16 = [10u16, 20u16, 30u16, 40u16];\n"
" let s: i32 = 0;\n"
" s += a[0]: i32; s += a[1]: i32;\n"
" s += a[2]: i32; s += a[3]: i32;\n"
" return s;\n"
"};\n", 100 },
/* [4]u8 control: already correct (MOVB pre-fix). Regression
* guard — asm should be unchanged. */
{ "u8_control",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]u8 = [10u8, 20u8, 30u8, 40u8];\n"
" let s: i32 = 0;\n"
" s += a[0]: i32; s += a[1]: i32;\n"
" s += a[2]: i32; s += a[3]: i32;\n"
" return s;\n"
"};\n", 100 },
/* [4]i16: signed-narrow, same MOVW dispatch (op chosen by esz,
* not signedness). Verifies the fix isn't gated. */
{ "i16_signed",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]i16 = [10i16, 20i16, 30i16, -5i16];\n"
" let s: i32 = 0;\n"
" s += a[0]: i32; s += a[1]: i32;\n"
" s += a[2]: i32; s += a[3]: i32;\n"
" return s;\n"
"};\n", 55 /* 10+20+30+(-5) = 55 */ },
{ 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, "arru16store: 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/wwau16_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char src[128], outbin[128], rmcmd[160];
snprintf(src, sizeof src, "%s/wwau16_%d_%d.ww",
tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwau16_%d_%d",
tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (f == NULL) { runwait(rmcmd); fail++; 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++;
}
char cs_s[128], ws_s[128];
snprintf(cs_s, sizeof cs_s, "%s/wwau16_%d_%d_cs.s",
tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwau16_%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 arr-u16-store tests failed\n", fail, n);
return 1;
}
printf("arru16store: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}