/* * 939_str_massign_store_cap_run — runtime coverage for the G3 fold: a * tuple-destructure REASSIGNMENT `a, s = call()` (N_MASSIGN) whose str element * must write the full 24B {ptr,len,cap} header into the (already 24B) str slot, * not just {ptr}. str is 24B since Phase 2 (#1); the N_MASSIGN arm previously * stored the str element with the bare scalar path (PUSHQ DX; ...; MOVQ DX, * slot) — one word, dropping len/cap. This is the third and final STORE-cluster * fold (after arrfield 937 and chainfield 938), closing the store cluster on * the tuple-destructure REASSIGN shape. * * N_MASSIGN vs N_MLET: `let a, s = call()` is N_MLET (fresh bindings, slots * sized from each binding's type) and ALREADY destructures 3-word. `a, s = * call()` with a, s PRE-DECLARED is N_MASSIGN (reassignment, slots already * exist). The fix mirrors N_MLET's (DX,CX,R8)->(.ptr,.len,.cap) routing at the * N_MASSIGN arm but localfinds the existing slot instead of allocating it. * The fixture MUST pre-declare a and s then reassign WITHOUT `let`, or the * parser emits N_MLET and this arm is never reached. * * Sites: cgen.c's N_MASSIGN case (str-XOR branch, l0/l1 N_IDENT, str-ness from * the checker-stamped l->type) and the cgenstmt.ww cgmassign twin (wwstage has * no checker, so str-ness comes from the called fn's return-type tuple element, * exactly as cgmlet derives it). ONE-STR ONLY, matching N_MLET's coverage; the * two-str destructure is a shared N_MLET/N_MASSIGN gap (task #22). * * Rows exercise BOTH XOR positions (the routing is position-agnostic, as in * N_MLET): * A `a, s = mk()` mk() returns (i64, str) — str is the 2nd element (l1). * B `s, a = mk2()` mk2() returns (str, i64) — str is the 1st element (l0). * * RHS cap!=len: the returned str is a literal whose .cap is MUTATED to a value * DISTINCT from its len (NOT a bare literal — literals carry cap==len, which * would hide a dropped cap; a `buf[lo:hi]` sub-slice was rejected too: ww * yields cap==len for it, task #20, equally hiding the drop). cap=8 and len=2 * are both nonzero and unequal so a 1-word store that drops cap is detectable. * * PRE-POISON (cap=5 != 8, len=4 != 2, both nonzero): before the G3 store under * test, all three slot words of `s` are seeded with a DIFFERENT str (ptr='q', * len=4, cap=5) via `let s: str = q` — a PROVEN already-3-word let-init copy * (the same path 935's spoil() relies on), NOT the G3 store itself (if G3 is * broken its own poison write would also drop cap, leaving uninit words rather * than a controlled poison). The let-init both DECLARES s (so the subsequent * comma-assign is a reassignment = N_MASSIGN) and poisons its slot. A broken * 1-word G3 store writes only s.ptr and never touches +8/+16, so the 3-word * read-back observes the poison len 4 / cap 5, never 2 / 8 — deterministic * discrimination with no reliance on a stale register. * * FULL-TRIPLE ASSERT: a register-reallocation slip in the 3-word store could * clobber ptr while wiring len/cap, so each row reads s back (landed 3-word * tuple/let reads) and checks ptr (first byte through it — 'h'=104), len (2), * cap (8), plus the scalar side a (5) — confirming the XOR branch wires the * scalar slot too. The broken 1-word store writes ptr correctly (it IS the one * word it keeps), so len AND cap are the fail-before discriminators; ptr and a * guard the fix. * * Verified fail-before (reverted the store edit on BOTH stages → all rows exit * 1, cap reads the poison 5) / pass-after (exit 0), both the cstage `ww` and * wwstage `ww_ww` drivers. Confirmed via emitted asm the fixture hits the * N_MASSIGN arm (CALL ...; MOVQ {AX,DX,CX,R8} -> slots), not N_MLET. */ #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; }; static const struct row rows[] = { /* A — `a, s = mk()`, str the 2nd tuple element (l1). a and s are * pre-declared (so the comma-assign is N_MASSIGN, not N_MLET); s is * poisoned (cap=5,len=4,'q') by its let-init copy. The G3 store then * lands the test str (cap=8,len=2,'h') via (DX,CX,R8)->s, AX->a. */ { "massign_store_str_pos1", "fn mk() (i64, str) = {\n" " let p: str = \"hi\"; p.cap = 8i32;\n" " return (5i64, p);\n" "};\n" "export fn main() i32 = {\n" " let q: str = \"qqqq\"; q.cap = 5i32;\n" " let a: i64 = 7i64;\n" " let s: str = q;\n" " a, s = mk();\n" " if (s.cap: i32 != 8) { return 1; };\n" " if (s.len: i32 != 2) { return 2; };\n" " if (s[0] != 104u8) { return 3; };\n" " if (a != 5) { return 4; };\n" " return 0;\n" "};\n", 0 }, /* B — `s, a = mk2()`, str the 1st tuple element (l0). Same poison and * test values; exercises the s0_is_str routing (DX,CX,R8)->s, AX->a * from the other XOR side. */ { "massign_store_str_pos0", "fn mk2() (str, i64) = {\n" " let p: str = \"hi\"; p.cap = 8i32;\n" " return (p, 5i64);\n" "};\n" "export fn main() i32 = {\n" " let q: str = \"qqqq\"; q.cap = 5i32;\n" " let s: str = q;\n" " let a: i64 = 7i64;\n" " s, a = mk2();\n" " if (s.cap: i32 != 8) { return 1; };\n" " if (s.len: i32 != 2) { return 2; };\n" " if (s[0] != 104u8) { return 3; };\n" " if (a != 5) { return 4; };\n" " return 0;\n" "};\n", 0 }, }; static int run_driver(const char *driver, const struct row *r, int i) { char src[96], tmpdir[96], cmd[1024]; snprintf(src, sizeof src, "/tmp/strmassignstore_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/strmassignstore_%d_d_%d", getpid(), i); FILE *f = fopen(src, "wb"); if (!f) return -1; fputs(r->src, f); fclose(f); mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s build %s", tmpdir, driver, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); unlink(src); rmdir(tmpdir); return -1; } const char *base = strrchr(src, '/'); base = base ? base + 1 : src; char outbin[160]; snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); char *dot = strrchr(outbin, '.'); if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; int got = runwait(outbin); unlink(src); unlink(outbin); rmdir(tmpdir); return got; } int main(void) { const char *bin = getenv("BIN"); if (!bin) bin = "out/bin"; char absbin[512]; if (bin[0] != '/') { char cwd[256]; if (getcwd(cwd, sizeof cwd) == NULL) return 1; snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); bin = absbin; } char cdrv[640]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); char wdrv[640]; snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); struct { const char *name; const char *path; int gated_on_existence; } drivers[] = { { "cstage", cdrv, 0 }, { "wwstage", wdrv, 1 }, { NULL, NULL, 0 }, }; int n = (int)(sizeof rows / sizeof rows[0]); int total = 0, fail = 0; for (int d = 0; drivers[d].name; d++) { if (drivers[d].gated_on_existence && access(drivers[d].path, X_OK) != 0) { fprintf(stderr, "str_massign_store_cap_run: skip %s (no %s)\n", drivers[d].name, drivers[d].path); continue; } for (int i = 0; i < n; i++) { int got = run_driver(drivers[d].path, &rows[i], i); total++; if (got != rows[i].want) { fprintf(stderr, "str_massign_store_cap_run[%s][%s]: exit=%d want=%d\n", drivers[d].name, rows[i].label, got, rows[i].want); fail++; } } } if (fail) { fprintf(stderr, "str_massign_store_cap_run: %d/%d fixtures failed\n", fail, total); return 1; } printf("str_massign_store_cap_run: %d/%d ok\n", total, total); return 0; }