Files
ww/test/wcc/965_f32stamp_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

216 lines
6.7 KiB
C

/*
* 965_f32stamp_run — runtime + byte-id regression net for #104 fold-2: an
* UN-suffixed float literal in an f32 context (`let x: f32 = 1.0`, `return
* 1.0` from an f32 fn) must be stamped f32 by the checker so fold-1's cgen
* narrow (CVTSD2SS at the literal materialise site) fires. Without the stamp
* the literal stays ty_untyped_float, materialises as a 64-bit double, and
* the f32 consumer reads the LOW 4 BYTES of that double — 0x00000000 == 0.0f
* for clean values (`1.0` -> 0.0f, so `x: f64 != 1.0` trips). fold-1 (964)
* only covered SUFFIXED literals (`1.0f32`); the un-suffixed common case was
* its documented hole, closed here.
*
* Both checkers stamp the literal: cstage cmd/wcc/check.c coerce_floatlit (at
* clet + cstmt N_RETURN), wwstage selfhost/cmd/wcc/check.ww coercefloatlit (in
* resolvewalk's post-order N_LET / N_RETURN handler — placed AFTER the child
* re-walk so the post-order exprtype re-stamp doesn't undo it). Both stages
* emit byte-identical asm, so the 990-997 byte-id gates can NEVER catch a
* reintroduction — only an executed-and-checked runtime probe can.
*
* SCOPE (#104 fold-2): the stamp fires at let-init and return ONLY. binop
* (`1.0 + x_f32`), unary minus (`-1.0`), assign, call-arg, and struct-field
* are DEFERRED to #120 — the wwstage cgen's exprfloatkind (cgenutil.ww)
* hardcodes a float literal to f64 and picks f32 off the operands, not the
* node stamp, so a stamped literal in those positions does not narrow in
* wwstage (cs would emit ADDSS, ww ADDSD — a byte-id break). This probe
* therefore uses bare let-init / return literals exclusively.
*
* Each row carries BOTH dimensions (like 955 / 964):
* (a) cstage `ww build` + run, asserting the exit code.
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge (rule-10).
*/
#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[] = {
/* The hole 964 leaves: a bare (UN-suffixed) f32-context literal. On
* the bug `let x: f32 = 1.0` stores the low 4 bytes of double 1.0
* (== 0x00000000 == 0.0f), so x:f64 == 0.0 != 1.0 -> 1. */
{ "let_one",
"package main;\n"
"export fn main() i32 = {\n"
" let x: f32 = 1.0;\n"
" if (x: f64 != 1.0) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* a bare decimal literal truncated through i32: 8.0 -> 8. On the bug
* the f32 slot holds 0.0f -> 0. */
{ "let_decimal",
"package main;\n"
"export fn main() i32 = {\n"
" let y: f32 = 8.0;\n"
" return y: i32;\n"
"};\n", 8 },
/* a fractional value (exactly representable): 0.5. Bug -> 0.0f, so
* p:f64 == 0.0 != 0.5 -> 1. */
{ "let_frac",
"package main;\n"
"export fn main() i32 = {\n"
" let p: f32 = 0.5;\n"
" if (p: f64 != 0.5) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* return context: an f32 fn returning a bare literal, truncated to
* i32 at the call site. 2.0 -> 2. On the bug the X0 single is the
* low half of double 2.0 (== 0.0f) -> 0. */
{ "return_bare",
"package main;\n"
"fn g() f32 = { return 2.0; };\n"
"export fn main() i32 = {\n"
" return g(): i32;\n"
"};\n", 2 },
/* return feeding a let, both un-suffixed: the literal narrows in the
* fn return, the let-init binds the (already-f32) call value. 4.0 ->
* 4. */
{ "return_then_let",
"package main;\n"
"fn h() f32 = { return 4.0; };\n"
"export fn main() i32 = {\n"
" let r: f32 = h();\n"
" if (r: f64 != 4.0) { return 1; };\n"
" return r: i32;\n"
"};\n", 4 },
{ 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, "f32stamp: 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/wwf32s_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char rmcmd[160];
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
char src[128];
snprintf(src, sizeof src, "%s/wwf32s_%d_%d.ww",
tmpdir, getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; runwait(rmcmd); continue; }
fputs(rows[i].src, f);
fclose(f);
/* (a) cstage build + run. */
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/wwf32s_%d_%d",
tmpdir, getpid(), i);
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: emit .s from both stages, cmp. */
char cs_s[128], ws_s[128];
snprintf(cs_s, sizeof cs_s, "%s/wwf32s_%d_%d_cs.s",
tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwf32s_%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 f32 un-suffixed stamp tests failed\n",
fail, n);
return 1;
}
printf("f32stamp: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
return 0;
}