/* * 933_str_field_cap_run — runtime coverage for the C4.6 fold: reading a * str-typed FIELD of a struct (N_DOT value read) must load the full 24B * {ptr,len,cap} header, not just {ptr,len}. str is 24B since Phase 2 (#1); * pre-C4.6 the N_DOT str-field arms loaded 2 words and dropped cap. * * The byte-id gates (990-997) can't catch a symmetric 2-word miscompile: * if both stages drop cap identically, byte-id passes silently. So this * pins the *runtime* contract — build each fixture through both the cstage * `ww` and the wwstage `ww_ww` driver and confirm the assertions hold * (exit 0). The global row is the load-bearing case: in ww the global * struct-field load is a separate arm with no slice sibling (cstage folds * local+global in one base_reg arm; ww splits them), so a cap-drop there * would hide from the local-field rows. * * Each row POISONS the source str so cap != len (a `.cap =` pseudo-field * write, no malloc / no import — self-contained, so ww_ww writes * intermediates only next to the /tmp source). A 2-word field read leaves * cap = stale, so the read-back .cap mismatches the poison and the row * fails. The cap word is observed through `let s: str = ` (a 3-word * copy into the slot) then `s.cap` (an N_IDENT pseudo-field read off the * slot) — mirrors the 932 observation shape. * * Sites (C4.6 S1 + S2, case A): * - s1local : `st.f` — direct struct local field (base BP). * - s1global : `g.f` — direct struct global field (base CX). * - s2ptr : `pst.f` — field via a *struct local (base BX, deref). */ #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[] = { /* S1 local — `st.f` direct struct local field. Poison cap=8 * (len=2). spoil() interposes a call between the field store and * the read: a call clobbers caller-saved CX, so a broken 2-word * read leaves cap = spoil's leftover (44), not the lingering store * value — without it CX would coincidentally still hold the poison * the `st.f = p` store left behind and the row wouldn't discriminate. */ { "s1local_field", "type rec = struct { f: str };\n" "fn spoil() i32 = {\n" " let z: str = \"zzzz\";\n" " z.cap = 44i32;\n" " let w: str = z;\n" " return w.cap: i32;\n" "};\n" "export fn main() i32 = {\n" " let p: str = \"hi\";\n" " p.cap = 8i32;\n" " let st: rec;\n" " st.f = p;\n" " let junk: i32 = spoil();\n" " let s: str = st.f;\n" " if (s.cap: i32 != 8) { return 1; };\n" " if (s.len: i32 != 2) { return 2; };\n" " if (junk != 44) { return 3; };\n" " return 0;\n" "};\n", 0 }, /* S1 global — `g.f` direct struct global field (base CX path). * The no-local-sibling arm in ww; poison cap=9 (len=5). */ { "s1global_field", "type rec = struct { f: str };\n" "let g: rec;\n" "export fn main() i32 = {\n" " let p: str = \"world\";\n" " p.cap = 9i32;\n" " g.f = p;\n" " let s: str = g.f;\n" " if (s.cap: i32 != 9) { return 1; };\n" " if (s.len: i32 != 5) { return 2; };\n" " return 0;\n" "};\n", 0 }, /* S2 case A — `pst.f` field via a *struct local (deref, base BX). * Poison cap=7 (len=3). */ { "s2ptr_field", "type rec = struct { f: str };\n" "export fn main() i32 = {\n" " let p: str = \"abc\";\n" " p.cap = 7i32;\n" " let st: rec;\n" " st.f = p;\n" " let pst: *rec = &st;\n" " let s: str = pst.f;\n" " if (s.cap: i32 != 7) { return 1; };\n" " if (s.len: i32 != 3) { return 2; };\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/strfieldcap_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/strfieldcap_%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_field_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_field_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_field_cap_run: %d/%d fixtures failed\n", fail, total); return 1; } printf("str_field_cap_run: %d/%d ok\n", total, total); return 0; }