/* * 956_tuprecv_f64_run — runtime + byte-id regression net for #105: a * tuple-from-call receive corrupts the f64 word when the callee is * BRANCHED. Covers ALL THREE receive forms, which share the #83 * tuple_rseq cursor and all carried the same defect: * 1. SINGLE-VAR `let r = norm(); ...r.0` (cglet 16B-tuple branch) * 2. DESTRUCTURE `let (m,i) = norm()` (N_MLET / cgmlet+tupstore) * 3. REASSIGN `m,i = norm()` (N_MASSIGN / cgmassign+tupstore) * * Root (#105, a #103-FACE-Z regression): a (f64,i64)/(i64,f64) tuple * returns its f64 word in X0 (the SSE return reg) and its integer word * in an integer reg (tuple_rseq AX/DX). All three receives spilled the * f64 word via MOVQ from the integer cursor — but that reg holds GARBAGE * (the f64 is in X0). #103-FACE-Z's field read (MOVSD slot,X0) then read * that garbage. The fix makes every receive spill CLASS-AWARE: an f64/f32 * word spills `MOVSD/MOVSS X0, slot`, an integer word spills `MOVQ * , slot` (as before). cstage cgen.c (3 sites) + wwstage cgenstmt.ww * (cglet branch + the shared tupstore helper, which covers cgmlet and * cgmassign), identically. * * WHY BRANCHED CALLEES: a SINGLE-return callee masks the bug via * register coincidence — a float-literal return leaves the f64 bit * pattern in AX (literal materialise goes through AX), so MOVQ AX,slot * happens to store the right bits; and X0 stays live to the receive. A * BRANCHED / multi-statement callee whose f64 word is a non-literal * (e.g. an f64 param) has an inner CALL clobber AX, so the integer-reg * spill stores garbage. The bug rows below therefore all use branched * callees with an f64-param word — single-return rows are gate-blind to * #105 (this is the gate lesson the task pins). * * GATE-BLIND TO BYTE-ID ALONE: both stages are symmetric-WRONG on master * (both emit MOVQ AX,slot), so the cs==ww .s gate HOLDS on master for * the bug rows — they diverge only at RUNTIME. Each row carries BOTH * dimensions (modelled on 954_tuprecv_run): * (a) cstage `ww build` + run, asserting the exit code — this is what * catches #105 (master returns the wrong exit). * (b) w6c vs w6c_ww `.s` cmp — guards rule-10 (both stages fixed * identically). * * CONTROL rows pin that the fix touches nothing else: an all-integer * branched 2-tuple (correct pre- and post-fix; MOVQ path untouched), the * destructure form `let (a,b)=mk()` (cgmlet, a separate path the fix * does not touch), a single-return (f64,i64) field read (#103 FACE-Z * shape, correct pre- and post-fix), and a bare 0f64 compare (#103 * FACE-X shape, no tuple involved). */ #include #include #include #include #include #include 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; int chk_stamped; }; static const struct row rows[] = { /* BUG — minimal repro. norm is BRANCHED (inner issub() CALL clobbers * AX) and its f64 word is the param n, not a literal. Pre-fix the * receive spills MOVQ AX,slot (garbage); r.0 != 16.0 -> return 1. * Post-fix MOVSD X0,slot -> 16.0 -> return 0. */ { "f64_i64_br", "package main;\n" "fn issub(n: f64) bool = { return false; };\n" "fn norm(n: f64) (f64, i64) = {\n" "\tif (issub(n)) { return (n*2.0, -52); };\n" "\treturn (n, 0);\n" "};\n" "export fn main() i32 = {\n" "\tconst r = norm(16.0);\n" "\tconst m: f64 = r.0;\n" "\tif (m != 16.0) { return 1; };\n" "\treturn 0;\n" "};\n", 0 }, /* BUG — deferred read. An intervening f64 CALL clobbers X0 AFTER the * receive; the read of r.0 must come from the SPILLED slot, not a * stale X0. Strongest catch: proves the spill happened at receive * time and survives X0 clobber. */ { "f64_i64_deferred", "package main;\n" "fn issub(n: f64) bool = { return false; };\n" "fn norm(n: f64) (f64, i64) = {\n" "\tif (issub(n)) { return (n*2.0, -52); };\n" "\treturn (n, 0);\n" "};\n" "fn clob(x: f64) f64 = { return x + 1.0; };\n" "export fn main() i32 = {\n" "\tconst r = norm(16.0);\n" "\tconst junk: f64 = clob(3.0);\n" "\tconst m: f64 = r.0;\n" "\tif (m != 16.0) { return 1; };\n" "\tif (junk != 4.0) { return 2; };\n" "\treturn 0;\n" "};\n", 0 }, /* BUG — order-swap (i64, f64): f64 is word1, spilled from DX pre-fix * (garbage; the f64 is in X0). Confirms the fix is position-aware: * word1's f64 -> MOVSD X0, slot+8; word0's i64 -> MOVQ AX, slot+0. */ { "i64_f64_br", "package main;\n" "fn issub(n: f64) bool = { return false; };\n" "fn norm(n: f64) (i64, f64) = {\n" "\tif (issub(n)) { return (-52, n*2.0); };\n" "\treturn (0, n);\n" "};\n" "export fn main() i32 = {\n" "\tconst r = norm(16.0);\n" "\tconst i: i64 = r.0;\n" "\tconst m: f64 = r.1;\n" "\tif (m != 16.0) { return 1; };\n" "\tif (i != 0) { return 2; };\n" "\treturn 0;\n" "};\n", 0 }, /* BUG — DESTRUCTURE form `let (m,i)=norm()` (N_MLET / cgmlet+tupstore). * f64 binding m is element 0 (cursor AX); pre-fix MOVQ AX,slot stores * garbage (issub clobbered AX). Post-fix MOVSD X0,slot. m=16.0, i=0. */ { "destr_f64_i64_br", "package main;\n" "fn issub(n: f64) bool = { return false; };\n" "fn norm(n: f64) (f64, i64) = {\n" "\tif (issub(n)) { return (n*2.0, -52); };\n" "\treturn (n, 0);\n" "};\n" "export fn main() i32 = {\n" "\tlet (m, i) = norm(16.0);\n" "\tif (m != 16.0) { return 1; };\n" "\tif (i != 0) { return 2; };\n" "\treturn 0;\n" "};\n", 0, 1 }, /* BUG — DESTRUCTURE order-swap `let (i,m)=norm()`, (i64,f64). f64 * binding m is element 1 (cursor DX); pre-fix MOVQ DX,slot garbage, * post-fix MOVSD X0,slot. i=0, m=16.0. */ { "destr_i64_f64_br", "package main;\n" "fn issub(n: f64) bool = { return false; };\n" "fn norm(n: f64) (i64, f64) = {\n" "\tif (issub(n)) { return (-52, n*2.0); };\n" "\treturn (0, n);\n" "};\n" "export fn main() i32 = {\n" "\tlet (i, m) = norm(16.0);\n" "\tif (m != 16.0) { return 1; };\n" "\tif (i != 0) { return 2; };\n" "\treturn 0;\n" "};\n", 0, 1 }, /* BUG — REASSIGN form `m,i = norm()` (N_MASSIGN / cgmassign+tupstore) * into pre-declared slots. Same f64-element-from-X0 defect. m=16.0. */ { "massign_f64_i64_br", "package main;\n" "fn issub(n: f64) bool = { return false; };\n" "fn norm(n: f64) (f64, i64) = {\n" "\tif (issub(n)) { return (n*2.0, -52); };\n" "\treturn (n, 0);\n" "};\n" "export fn main() i32 = {\n" "\tlet m: f64 = 0.0;\n" "\tlet i: i64 = 0;\n" "\tm, i = norm(16.0);\n" "\tif (m != 16.0) { return 1; };\n" "\tif (i != 0) { return 2; };\n" "\treturn 0;\n" "};\n", 0 }, /* BUG — REASSIGN order-swap `i,m = norm()`, (i64,f64). f64 reassign * target is element 1 (cursor DX). i=0, m=16.0. */ { "massign_i64_f64_br", "package main;\n" "fn issub(n: f64) bool = { return false; };\n" "fn norm(n: f64) (i64, f64) = {\n" "\tif (issub(n)) { return (-52, n*2.0); };\n" "\treturn (0, n);\n" "};\n" "export fn main() i32 = {\n" "\tlet i: i64 = 0;\n" "\tlet m: f64 = 0.0;\n" "\ti, m = norm(16.0);\n" "\tif (m != 16.0) { return 1; };\n" "\tif (i != 0) { return 2; };\n" "\treturn 0;\n" "};\n", 0 }, /* CONTROL — all-integer branched 2-tuple. The integer-cursor MOVQ * path is untouched by the fix (e0/e1 not float), so this is correct * pre- and post-fix and byte-id both ways. a=5, b=7 -> 12. */ { "ctl_int_br", "package main;\n" "fn issub(n: i64) bool = { return false; };\n" "fn mk(n: i64) (i64, i64) = {\n" "\tif (issub(n)) { return (n*2, -1); };\n" "\treturn (n, 7);\n" "};\n" "export fn main() i32 = {\n" "\tconst r = mk(5);\n" "\tconst a: i64 = r.0;\n" "\tconst b: i64 = r.1;\n" "\treturn (a: i32) + (b: i32);\n" "};\n", 12 }, /* CONTROL — destructure-with-NO-f64 `let (a,b)=mk()`. The fix now * touches cgmlet/tupstore, but an all-integer element takes the * unchanged MOVQ path, so this stays byte-id and correct pre- and * post-fix — guards that the class check doesn't perturb integers. * a=5, b=7 -> 12. */ { "ctl_destr", "package main;\n" "fn issub(n: i64) bool = { return false; };\n" "fn mk(n: i64) (i64, i64) = {\n" "\tif (issub(n)) { return (n*2, -1); };\n" "\treturn (n, 7);\n" "};\n" "export fn main() i32 = {\n" "\tlet (a, b) = mk(5);\n" "\treturn (a: i32) + (b: i32);\n" "};\n", 12 }, /* CONTROL — #103 FACE-Z single-return (f64,i64) field read. The f64 * word is a literal, so AX coincidentally holds its bits on master; * correct pre- and post-fix (the fix changes MOVQ AX,slot -> MOVSD * X0,slot but the run result is unchanged). f=2.5->2, i=7 -> 9. */ { "ctl_facez_single", "package main;\n" "fn mk() (f64, i64) = { return (2.5, 7); };\n" "export fn main() i32 = {\n" "\tconst t = mk();\n" "\tconst f: f64 = t.0;\n" "\tconst i: i64 = t.1;\n" "\treturn (f: i32) + (i: i32);\n" "};\n", 9 }, /* CONTROL — #103 FACE-X bare 0f64 compare (no tuple). The fix does * not touch the compare path; pure no-op guard. -> 0. */ { "ctl_facex_0f64", "package main;\n" "export fn main() i32 = {\n" "\tconst z: f64 = 0.0;\n" "\tif (z != 0.0) { return 1; };\n" "\treturn 0;\n" "};\n", 0 }, { 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, "tuprecv_f64: w6c_ww missing — cannot run the " "cs==ww byte-id gate\n"); return 1; } int n = 0, fail = 0; for (int i = 0; rows[i].src; i++, n++) { char src[64]; snprintf(src, sizeof src, "/tmp/wwtupf_%d_%d.ww", getpid(), i); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; continue; } fputs(rows[i].src, f); fclose(f); /* (a) cstage build + run in a scratch dir. */ char tmpdir[64]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wwtupf_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); char cmd[2048]; snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s", tmpdir, bin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: cstage build failed\n", rows[i].label); fail++; unlink(src); rmdir(tmpdir); continue; } char outbin[128]; const char *base = strrchr(src, '/'); base = base ? base + 1 : src; snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); char *dot = strrchr(outbin, '.'); if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; 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++; } unlink(outbin); rmdir(tmpdir); /* (b) cs==ww byte-id gate: emit .s from both stages, cmp. */ char cs_s[64], ws_s[64]; snprintf(cs_s, sizeof cs_s, "/tmp/wwtupf_%d_%d_cs.s", getpid(), i); snprintf(ws_s, sizeof ws_s, "/tmp/wwtupf_%d_%d_ww.s", 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++; unlink(src); 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++; unlink(src); unlink(cs_s); 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++; } /* (c) #121 A-narrow stamp gate: the un-annotated float- * destructure binding must now carry a checker type stamp, so * w6c_ww emits no `asserttyped:` diagnostic. Non-vacuous — * pre-stamp (HEAD) w6c_ww fires asserttyped on the f64 binding * ident (nil n.type_). */ if (rows[i].chk_stamped) { char errf[80]; snprintf(errf, sizeof errf, "/tmp/wwtupf_%d_%d_err.txt", getpid(), i); snprintf(cmd, sizeof cmd, "%s -o /dev/null %s 2>%s", w6c_ww, src, errf); runwait(cmd); snprintf(cmd, sizeof cmd, "grep -q asserttyped %s", errf); if (runwait(cmd) == 0) { fprintf(stderr, "row[%s]: w6c_ww emitted " "asserttyped (destructure binding " "unstamped)\n", rows[i].label); fail++; } unlink(errf); } unlink(src); unlink(cs_s); unlink(ws_s); } if (fail) { fprintf(stderr, "%d/%d tuple-receive-f64 tests failed\n", fail, n); return 1; } printf("tuprecv_f64: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); return 0; }