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

243 lines
7.4 KiB
C

/*
* 949_f9_float_run — F9 argument widen/drain ABI (float) cluster.
*
* The cgcall push/drain handling of a CONCRETE arg widened into a
* tagged-union parameter slot, where a float is involved:
*
* #30 + #48 (align-UP, wwstage-only): the cgcall DRAIN loop had no
* widen-first pop branch. A concrete arg widened into a tagged param
* was pushed as slotsize/8 GP words (tag + payload), but the drain
* classified per the ARG's source type:
* #30 — a float arg AFTER a widened (i64|void) arg: the widened
* box under-drained by one GP word, so `MOVSD (SP),X0` for
* the following f64 read the box's leftover payload word.
* #48 — the widened arg IS an f64 source: source-type=f64 hit the
* float arm, so `MOVSD (SP),X0` ate the TAG word into X0 and
* the payload landed in DI as the tag → callee fell through.
* Both are one missing branch — a widen-first GP pop placed BEFORE
* the float check, draining slotsize/8 words into the integer arg
* cursor (DI=tag, SI=payload). cstage already does this (precomputed
* widen[i]); this aligns wwstage UP and the rows pin cs==ww byte-id.
*
* REGISTER-CURSOR INVARIANT (rob): a lucky-but-wrong float shuffle can pass
* exit codes, so the run rows use values that only survive if the tag/
* payload land in the right GP regs AND the float in the right XMM:
* #30 returns 0 iff f's b (the f64 arg following the widen) == 1.5;
* #48 returns 2 iff the (i64|f64) box's tag selects the f64 arm (DI=tag).
* Both also assert cstage/wwstage .s byte-identical.
*
* NNN<950, self-contained (/tmp, no imports; #30 returns i32 rather than
* importing os, so the asm byte-id row can run w6c/w6c_ww directly).
*/
#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;
}
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), cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
#define K_RUN 0
#define K_BUILDERR 1
struct row { const char *label; const char *src; int want;
int kind; const char *experr; };
static const struct row rows[] = {
/* #30: a widened (i64|void) arg FOLLOWED by an f64 arg. Pre-fix the
* f64 drain (MOVSD (SP),X0) read the widened box's leftover payload
* word (int 7) instead of 1.5, so r != 1.5 → 1. */
{ "widen_then_float",
"package main;\n"
"fn f(a: (i64 | void), b: f64) f64 = { return b; };\n"
"export fn main() i32 = {\n"
" let r: f64 = f(7, 1.5);\n"
" if (r == 1.5) { return 0; };\n"
" return 1;\n"
"};\n", 0, K_RUN, NULL },
/* #48: the widened arg IS an f64 source (i64|f64). Pre-fix the float
* arm ate the tag word into X0 and the payload landed in DI as the
* tag, so the match fell through both arms → 9. Correct = 2 (f64). */
{ "float_source_widen",
"package main;\n"
"fn g(v: (i64 | f64)) i32 = {\n"
" match (v) {\n"
" case let n: i64 =>\n"
" return 1;\n"
" case let d: f64 =>\n"
" return 2;\n"
" };\n"
" return 9;\n"
"};\n"
"export fn main() i32 = {\n"
" let d: f64 = 3.5;\n"
" return g(d);\n"
"};\n", 2, K_RUN, NULL },
/* #49 (#263 both-stages): a RUNTIME f64 producer (mk) feeds a value
* widened into (f64|void), and the f64 arm READS the payload
* (d == 2.5). Pre-fix the widen-PUSH did `PUSHQ AX` for the payload
* while the f64 sat in X0, so d read stale bits → 1 (cs) / 3 (ww,
* the #48 pop divergence compounding). #48's arm dodged this by not
* reading the payload; this one catches the stale-AX push. Both
* stages now spill X0 → 0. (After the c4 drain fix the two stages
* already agree on the pop; this push fix closes the shared bug.) */
{ "runtime_float_widen_payload",
"package main;\n"
"type fv = (f64 | void);\n"
"fn mk(x: f64) f64 = { return x + 1.5; };\n"
"fn take(v: fv) i32 = {\n"
" match (v) {\n"
" case let d: f64 => {\n"
" if (d == 2.5) { return 0; };\n"
" return 1;\n"
" };\n"
" case void => { return 2; };\n"
" };\n"
" return 3;\n"
"};\n"
"export fn main() i32 = {\n"
" let d: f64 = mk(1.0);\n"
" return take(d);\n"
"};\n", 0, K_RUN, NULL },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[96], src[128], outbin[128], errf[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/f9f_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/f9f_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/f9f_%d_%d", tmpdir, getpid(), i);
snprintf(errf, sizeof errf, "%s/err", tmpdir);
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 >/dev/null 2>%s",
driver, outbin, src, errf);
int brc = runwait(cmd);
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
if (got != r->want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, r->want);
return 1;
}
return 0;
}
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[96], cs[96], ws[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/f9f_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/f9f_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/f9f_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
int rc = slurp_eq(cs, ws);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[2080];
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 cdrv[2120], wdrv[2120];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
}
for (int i = 0; i < n; i++) {
if (rows[i].kind == K_BUILDERR)
continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "f9_float: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("f9_float: %d/%d ok\n", total, total);
return 0;
}