Files
ww/test/wcc/989_ffivariadic_run.c
Hojun-Cho 8f0ce09f2a w6c: promote f32 arg to double in C-variadic tail (#14)
C99 6.5.2.2p6 requires float->double promotion for floats in the
variadic region of a call; w6c emitted MOVSS (4B), so a C variadic's
va_arg(double) read 8B of garbage. Promote at push (CVTSS2SD) so the
8B slot holds a real double, covering the XMM-reg and >8-float spill
cases uniformly; fi/AL XMM-count is unchanged.

cstage only; the wwstage twin rides C2 (#10), which adds wwstage's
C-variadic-call codegen.
2026-06-21 11:14:14 +09:00

204 lines
6.6 KiB
C

/*
* 989_ffivariadic_run — C1 (catB-54): a ww caller of a C variadic function
* (`@symbol("f") fn f(a: i64, ...) f64;`) must set the SysV AL register to
* the number of XMM regs used to pass the variadic FLOAT args. The C callee
* gates its xmm-save-area stores on `test %al,%al`, so a wrong AL makes
* va_arg(double) read garbage.
*
* THE BUG (cat-A silent miscompile, byte-id-blind): cgen.c hardcoded AL=0
* (`XORQ AX,AX`) at the variadic-call site — correct only for a zero-float
* variadic call. THE FIX: emit AL = the XMM cursor `fi` (the count of float
* args placed in XMM regs). Ref SysV §3.5.7, ref/qbe/amd64/sysv.c:384.
*
* RUNTIME gate (byte-id can never see AL correctness): each row builds a ww
* caller that calls the C fixture `double fixture(long n, ...)` (a
* va_arg(double) summer, test/wcc/data/ffivariadic/fixture.c, linked from
* libffifix.a) and asserts the returned sum. cstage `ww` ONLY — wwstage AL
* is C2.
*
* NON-VACUITY DEVIATION (reported to lead): the spec's `fixture(2,1.0,2.0)`
* is VACUOUS on this box — with AL=0 the two skipped xmm slots happen to
* alias stale stack that already holds 1.0/2.0, so the 2-float call returns
* the correct 3.0 even unfixed. At 3+ floats the coincidence breaks: AL=0
* deterministically returns the wrong sum. Every row below uses >=3 floats,
* so reverting the fix to `XORQ AX,AX` FAILS this test (proven). A 2-float
* row would pass both ways and prove nothing.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.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; /* 0 == sum matched */
};
static const struct row rows[] = {
/* 3 floats: 1+2+3 == 6. The smallest non-vacuous count (see header). */
{ "three",
"package main;\n"
"@symbol(\"fixture\") fn fixture(n: i64, ...) f64;\n"
"export fn main() int = {\n"
" let r: f64 = fixture(3, 1.0, 2.0, 3.0);\n"
" if (r == 6.0) { return 0; };\n"
" return 1;\n"
"};\n",
0 },
/* 5 floats, fewer than the 8 XMM arg regs: 1+2+3+4+5 == 15. */
{ "five",
"package main;\n"
"@symbol(\"fixture\") fn fixture(n: i64, ...) f64;\n"
"export fn main() int = {\n"
" let r: f64 = fixture(5, 1.0, 2.0, 3.0, 4.0, 5.0);\n"
" if (r == 15.0) { return 0; };\n"
" return 1;\n"
"};\n",
0 },
/* 8 floats == all XMM arg regs (AL caps at 8): 1+..+8 == 36. */
{ "eight",
"package main;\n"
"@symbol(\"fixture\") fn fixture(n: i64, ...) f64;\n"
"export fn main() int = {\n"
" let r: f64 = fixture(8, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
" if (r == 36.0) { return 0; };\n"
" return 1;\n"
"};\n",
0 },
/* 3 f32 args (#14): C default arg promotion widens each to f64, so
* the callee's va_arg(double) reads 1.5+2.5+3.0 == 7.0. Values are
* exact in f32, so the f64 compare is exact. Unpromoted (MOVSS, 4B)
* the high 4B of each 8B slot is stale stack, so va_arg(double)
* pulls garbage and the sum misses 7.0 — this row FAILS pre-fix. */
{ "f32three",
"package main;\n"
"@symbol(\"fixture\") fn fixture(n: i64, ...) f64;\n"
"export fn main() int = {\n"
" let r: f64 = fixture(3, 1.5: f32, 2.5: f32, 3.0: f32);\n"
" if (r == 7.0) { return 0; };\n"
" return 1;\n"
"};\n",
0 },
/* Sharper than f32three: 0.1 is INEXACT in f32, so the f32-rounded
* value differs from the f64 literal 0.1. The expected side `(x: f64)`
* is the SAME f32 var widened at runtime (CVTSS2SD), so equality holds
* iff the variadic arg carried the f32-rounded value promoted to f64 —
* NOT the original f64 literal and NOT stale-high-bit garbage. Both a
* no-promote (MOVSS) and a hypothetical direct-f64 pass would miss it.
* Single f32 arg also exercises the fi==1 boundary. */
{ "f32inexact",
"package main;\n"
"@symbol(\"fixture\") fn fixture(n: i64, ...) f64;\n"
"export fn main() int = {\n"
" let x: f32 = 0.1: f32;\n"
" let r: f64 = fixture(1, x);\n"
" if (r == (x: f64)) { return 0; };\n"
" return 1;\n"
"};\n",
0 },
/* Mixed f32 + f64 in one variadic call: only the f32 args (1.5, 3.5)
* promote; the f64 arg (2.0) passes at its native width and must NOT be
* double-promoted or skewed. 1.5+2.0+3.5 == 7.0, exact in both widths. */
{ "f32mixed",
"package main;\n"
"@symbol(\"fixture\") fn fixture(n: i64, ...) f64;\n"
"export fn main() int = {\n"
" let r: f64 = fixture(3, 1.5: f32, 2.0, 3.5: f32);\n"
" if (r == 7.0) { return 0; };\n"
" return 1;\n"
"};\n",
0 },
};
/* run_build — build+run `src` via cstage `driver`, linking libffifix.a from
* `libdir`. Returns the binary's exit code, or -1 on a build failure. */
static int
run_build(const char *driver, const char *libdir, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/ffivar_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/ffivar_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd,
"cd %s && %s build -L%s -lffifix %s 2>/dev/null",
tmpdir, driver, libdir, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
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 cdrv[1024], libdir[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
/* libffifix.a lives beside $(BIN) under $(OUT)/ffivariadic — the
* Makefile builds it there as a prereq of this test binary. */
snprintf(libdir, sizeof libdir, "%s/../ffivariadic", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
int got = run_build(cdrv, libdir, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "ffivariadic[%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
}
if (fail) {
fprintf(stderr, "ffivariadic: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("ffivariadic: %d/%d ok\n", total, total);
return 0;
}