/* * 932_str_elem_cap_run — runtime coverage for the F2 fold: a str-element * VALUE read via N_INDEX must load the full 24B {ptr,len,cap} header, not * just {ptr,len}. str is 24B since Phase 2 (#1); pre-F2 the N_INDEX * str-element arms dropped the cap word. * * The existing byte-id gates (990-997) can't catch a no-op fold here: a * shared 2-word miscompile passes byte-id silently. So this pins the * *runtime* contract — build each fixture through both the cstage `ww` * and the wwstage `ww_ww` driver and confirm the program's assertions * hold (exit 0). * * Each row POISONS the element so cap != len (a `.cap =` pseudo-field * write, no malloc / no import — keeps the fixture self-contained so * ww_ww writes intermediates only next to the /tmp source, not lib/). * A 2-word read leaves cap = len (Site A) or a stale slice-cap (Site B), * so the read-back .cap mismatches the poisoned value and the row fails. * * Both N_INDEX base forms are covered: * - Site A: N_IDENT base — `xs[0]` on a local `[N]str`. * - Site B: fallback base — `b.items[0]` where the base `b.items` is * an N_DOT (slice field), not a plain ident. * * The cap word is observed through `let e: str = ` (a 3-word * copy into the slot) then `e.cap` (an N_IDENT pseudo-field read off the * slot). A direct `.cap` would NOT observe it — the non-ident * .cap N_DOT path only handles .ptr/.len today (a separate gap, out of * F2's scope). */ #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[] = { /* Site A — N_IDENT base. Poison cap=8 (len=2), read back via a * local [2]str. Broken 2-word read leaves cap = len = 2. */ { "sitea_ident_base", "export fn main() i32 = {\n" " let p: str = \"hi\";\n" " p.cap = 8i32;\n" " let xs: [2]str;\n" " xs[0] = p;\n" " let e: str = xs[0];\n" " if (e.cap: i32 != 8) { return 1; };\n" " if (e.len: i32 != 2) { return 2; };\n" " return 0;\n" "};\n", 0 }, /* Site B — fallback base. Element behind an N_DOT slice field, so * the N_INDEX base isn't a plain ident. Poison cap=9 (len=5). * Broken read leaves cap = the slice header's own cap, not the * element's. */ { "siteb_fallback_base", "type box = struct { items: []str };\n" "export fn main() i32 = {\n" " let q: str = \"world\";\n" " q.cap = 9i32;\n" " let arr: [2]str;\n" " arr[0] = q;\n" " let b: box;\n" " b.items = arr[0:2];\n" " let e: str = b.items[0];\n" " if (e.cap: i32 != 9) { return 1; };\n" " if (e.len: i32 != 5) { 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/strelemcap_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/strelemcap_%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_elem_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_elem_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_elem_cap_run: %d/%d fixtures failed\n", fail, total); return 1; } printf("str_elem_cap_run: %d/%d ok\n", total, total); return 0; }