Files
ww/test/wcc/951_f64cgen_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

247 lines
8.4 KiB
C

/*
* 951_f64cgen_run — runtime regression net for the two GATE-BLIND f64
* codegen bugs #96 and #97. Both stages (cstage cgen.c, wwstage
* cgenexpr.ww) emit byte-identical asm before and after the fix, so the
* 990-997 byte-id gates can NEVER catch a reintroduction — only an
* executed-and-checked runtime probe can. This file is that probe.
*
* Table-driven like 700_e2e: each row is a self-contained ww program;
* the C-side cstage `ww build` compiles it, we run the binary and assert
* the exit code. Rows that self-check return 0 on pass / a locator code
* on the first failing assertion; rows that assert value propagation
* return a computed result the harness compares to `want_exit`.
*
* cstage-only by design (mirrors 700_e2e + 969_checked_run): `ww_ww run`
* is broken (#95) and per-program wwstage byte-id is the 990-997 gates'
* job, not this file's. The fix's cs==ww symmetry is verified there.
*
* #96 — f64/f32 deref-load must MOVSD/MOVSS into X0, not MOVQ into AX.
* #97 — f64/f32 compare must consult PF (parity) for IEEE-754 NaN: with
* a NaN operand `!=` is true, the other five relops false.
*/
#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 *src; int want_exit; };
static const struct row rows[] = {
/* #96 deref-load: dirty X0 with a non-x value just before the
* call so an accidental X0-retention can't mask a broken return
* load; f64 return through a bare deref must ride X0, and `*px`
* must reach X0 for the MULSD. (ken's f64gate p96_deref.) */
{ "package main;\n"
"fn deref(p: *f64) f64 = { return *p; };\n"
"export fn main() i32 = {\n"
" let x: f64 = 7.5;\n"
" let y: f64 = 1.25;\n"
" let px: *f64 = &x;\n"
" let junk: f64 = y * 2.0;\n"
" if (junk != 2.5) { return 3; };\n"
" let r: f64 = deref(px);\n"
" if (r != 7.5) { return 1; };\n"
" let q: f64 = *px * 2.0;\n"
" if (q != 15.0) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
/* #96 value propagation: f64 returned through an f64-returning fn
* then truncated to i32. On the bug the value strands in AX and
* the i32 cast reads stale X0 -> wrong exit. */
{ "package main;\n"
"fn deref(p: *f64) f64 = { return *p; };\n"
"export fn main() i32 = {\n"
" let x: f64 = 42.0;\n"
" let r: f64 = deref(&x);\n"
" return r: i32;\n"
"};\n", 42 },
/* #96 arith-through-deref: `*p * 2.0` -> i32. */
{ "package main;\n"
"export fn main() i32 = {\n"
" let x: f64 = 7.5;\n"
" let p: *f64 = &x;\n"
" let q: f64 = *p * 2.0;\n"
" return q: i32;\n"
"};\n", 15 },
/* #96 f64frombits round-trip: reinterpret a u64 bit pattern as
* f64 via `*((&bits): *f64)`. The deref node is f64-typed so it
* must MOVSD into X0; on the bug it MOVQs into AX and the i32 cast
* reads stale X0. 0x4045000000000000 == 42.0. */
{ "package main;\n"
"export fn main() i32 = {\n"
" let bits: u64 = 0x4045000000000000u64;\n"
" let f: f64 = *((&bits): *f64);\n"
" return f: i32;\n"
"};\n", 42 },
/* #96 copysign-style sign transfer built from primitives: tobits/
* frombits are f64<->u64 reinterpret derefs (both float and int
* deref-loads exercised). copysign(5.0, -1.0) == -5.0. */
{ "package main;\n"
"fn tobits(f: f64) u64 = { return *((&f): *u64); };\n"
"fn frombits(b: u64) f64 = { return *((&b): *f64); };\n"
"export fn main() i32 = {\n"
" let x: f64 = 5.0;\n"
" let y: f64 = -1.0;\n"
" let mag: u64 = tobits(x) & 0x7fffffffffffffffu64;\n"
" let sgn: u64 = tobits(y) & 0x8000000000000000u64;\n"
" let r: f64 = frombits(mag | sgn);\n"
" if (r > 0.0) { return 1; };\n"
" if (r < -4.5) { if (r > -5.5) { return 0; }; };\n"
" return 2;\n"
"};\n", 0 },
/* #97 full NaN relop sweep (f64, UCOMISD). Runtime NaN via
* 0.0/0.0 through opaque fns so the checker can't const-fold it.
* isnan via self-inequality; NaN against each of the 6 relops;
* isnan(1.0)==false; ordered non-NaN rows guard for regression.
* (ken's f64gate p97_nan.) */
{ "package main;\n"
"fn zero() f64 = { return 0.0; };\n"
"fn one() f64 = { return 1.0; };\n"
"export fn main() i32 = {\n"
" let z: f64 = zero();\n"
" let nan: f64 = z / z;\n"
" let x: f64 = one();\n"
" if (!(nan != nan)) { return 1; };\n"
" if (nan == nan) { return 2; };\n"
" if (nan == x) { return 3; };\n"
" if (nan < x) { return 4; };\n"
" if (nan <= x) { return 5; };\n"
" if (nan > x) { return 6; };\n"
" if (nan >= x) { return 7; };\n"
" if (!(nan != x)) { return 8; };\n"
" if (x != x) { return 9; };\n"
" if (!(x == x)) { return 10; };\n"
" if (!(x < 2.0)) { return 11; };\n"
" if (!(x <= 1.0)) { return 12; };\n"
" if (!(2.0 > x)) { return 13; };\n"
" if (!(1.0 >= x)) { return 14; };\n"
" if (x > 2.0) { return 15; };\n"
" return 0;\n"
"};\n", 0 },
/* #97 value propagation: of the 6 relops against NaN, exactly one
* (`!=`) is true. Returns the true-count; the bug returns 3 (==,
* <, <= all wrongly fire on the unordered ZF/CF). */
{ "package main;\n"
"fn z() f64 = { return 0.0; };\n"
"export fn main() i32 = {\n"
" let nan: f64 = z() / z();\n"
" let x: f64 = 1.0;\n"
" let n: i32 = 0;\n"
" if (nan == x) { n += 1; };\n"
" if (nan != x) { n += 1; };\n"
" if (nan < x) { n += 1; };\n"
" if (nan <= x) { n += 1; };\n"
" if (nan > x) { n += 1; };\n"
" if (nan >= x) { n += 1; };\n"
" return n;\n"
"};\n", 1 },
/* #97 f32 path (UCOMISS): NaN must follow the same unordered
* rules, and ordered f64 relops with no NaN must stay correct.
* (ken's f64gate p97_f32_ordered.) */
{ "package main;\n"
"fn z32() f32 = { return 0.0; };\n"
"fn one32() f32 = { return 1.0; };\n"
"export fn main() i32 = {\n"
" let z: f32 = z32();\n"
" let nan: f32 = z / z;\n"
" let x: f32 = one32();\n"
" if (!(nan != nan)) { return 1; };\n"
" if (nan == nan) { return 2; };\n"
" if (nan < x) { return 3; };\n"
" if (nan >= x) { return 4; };\n"
" let a: f64 = 2.0;\n"
" let b: f64 = 3.0;\n"
" if (!(a < b)) { return 5; };\n"
" if (a > b) { return 6; };\n"
" if (!(a <= a)) { return 7; };\n"
" if (!(b >= a)) { return 8; };\n"
" if (!(a == 2.0)){ return 9; };\n"
" if (a != 2.0) { return 10; };\n"
" if (b < a) { return 11; };\n"
" if (!(b > a)) { return 12; };\n"
" return 0;\n"
"};\n", 0 },
/* #97 `>`/`>=` LEFT-BARE arm with runtime-built operands: the JA/
* JAE template stays unchanged by the fix, so this guards that the
* untouched arm still computes ordered greater-than correctly.
* (ken's f64gate p97_gtonly.) */
{ "package main;\n"
"fn z() f64 = { return 0.0; };\n"
"export fn main() i32 = {\n"
" let a: f64 = z() + 2.0;\n"
" let b: f64 = z() + 3.0;\n"
" if (!(b > a)) { return 1; };\n"
" if (a > b) { return 2; };\n"
" if (!(b >= a)) { return 3; };\n"
" if (!(a >= a)) { return 4; };\n"
" return 0;\n"
"};\n", 0 },
{ NULL, 0 }
};
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;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwf64_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
char src[128], outbin[128], rmcmd[160];
snprintf(src, sizeof src, "%s/wwf64_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwf64_%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[1024];
snprintf(cmd, sizeof cmd, "%s/ww build -o %s %s",
bin, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row %d: build failed\n src: %s\n",
i, rows[i].src);
fail++;
runwait(rmcmd);
continue;
}
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "row %d: exit %d, want %d\n src: %s\n",
i, got, rows[i].want_exit, rows[i].src);
fail++;
}
runwait(rmcmd);
}
if (fail) {
fprintf(stderr, "%d/%d f64cgen tests failed\n", fail, n);
return 1;
}
printf("f64cgen: %d/%d ok\n", n, n);
return 0;
}