/* * 989_defercap_run (#40, F13 c1) — the defer stack must fail loud at its cap * in BOTH stages, not silently drop the overflowing deferred call. * * THE BUG (cat-A, both stages diverged AND both wrong at their cap): wwstage * cgstmt N_DEFER pushed only while defertop < DEFER_MAX where DEFER_MAX was 16 * (cgen.ww), so a function with 17 defers silently dropped the 17th — the * counter reached 16, exit 1, while cstage (DEFER_MAX 32) emitted all 17. * cstage in turn silently dropped its own 33rd defer. The runtime-correct * target (CAP-SEMANTICS rule: cstage silent at its own cap => loud both): * the cap is the shared 32, and a function exceeding it is a hard compile * error in BOTH stages (cgenstmt.ww N_DEFER + cgen.c N_DEFER fatal). * * row | ndefers | shape | result (cs==ww) * ------------+---------+-------------------------------+---------------- * seventeen | 17 | original repro (>old ww 16) | exit 0 * at_cap_32 | 32 | exactly the shared cap | exit 0 * over_cap_33 | 33 | one past the cap | build FAILS loud * * seventeen was RED pre-c1 on wwstage (counter 16, exit 1 vs cstage exit 0). * over_cap_33 was RED pre-c1 on BOTH stages (silent rc=0 accept dropping the * overflowing defer). at_cap_32 pins the boundary stays accepted byte-id. */ #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; int ndefers; int want_build_fail; int want_exit; }; static const struct row rows[] = { { "seventeen", 17, 0, 0 }, { "at_cap_32", 32, 0, 0 }, { "over_cap_33", 33, 1, 0 }, }; /* Emit `package main` with a doit() carrying ndefers `defer bump();` and a * main() that checks the global counter equals ndefers. */ static int write_src(const char *path, int ndefers) { FILE *f = fopen(path, "wb"); if (!f) return -1; fputs("package main;\n" "let cnt: i32 = 0;\n" "fn bump() void = { cnt = cnt + 1; };\n" "fn doit() void = {\n", f); for (int i = 0; i < ndefers; i++) fputs("\tdefer bump();\n", f); fprintf(f, "};\n" "export fn main() i32 = {\n" "\tdoit();\n" "\tif (cnt == %d) { return 0; };\n" "\treturn 1;\n" "};\n", ndefers); fclose(f); return 0; } /* Returns the build rc in *brc; the run exit code (or -1) as the value. */ static int build_run(const char *driver, const struct row *r, int i, int *brc) { char src[64], tmpdir[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/dcap_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/dcap_%d_d_%d", getpid(), i); if (write_src(src, r->ndefers) != 0) { *brc = -1; return -1; } mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", tmpdir, driver, src); *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 got; } 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], wdrv[1024]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); int have_ww = (access(wdrv, X_OK) == 0); int n = (int)(sizeof rows / sizeof rows[0]); int total = 0, fail = 0; for (int i = 0; i < n; i++) { total++; int cbrc, gc = build_run(cdrv, &rows[i], i, &cbrc); if (rows[i].want_build_fail) { if (cbrc == 0) { fprintf(stderr, "defercap[cstage][%s]: built rc=0, " "expected loud build failure (#40)\n", rows[i].label); fail++; } } else { if (cbrc != 0 || gc != rows[i].want_exit) { fprintf(stderr, "defercap[cstage][%s]: brc=%d exit=%d " "want_exit=%d\n", rows[i].label, cbrc, gc, rows[i].want_exit); fail++; } } if (!have_ww) { fprintf(stderr, "defercap: skip wwstage (no %s)\n", wdrv); continue; } int wbrc, gw = build_run(wdrv, &rows[i], i, &wbrc); if (rows[i].want_build_fail) { if (wbrc == 0) { fprintf(stderr, "defercap[wwstage][%s]: built rc=0, " "expected loud build failure (#40)\n", rows[i].label); fail++; } /* both-fail is the rule-10 agreement here */ if ((cbrc == 0) != (wbrc == 0)) { fprintf(stderr, "defercap[%s]: build-fail divergence " "cs_rc=%d ww_rc=%d (#40)\n", rows[i].label, cbrc, wbrc); fail++; } } else { if (wbrc != 0 || gw != rows[i].want_exit) { fprintf(stderr, "defercap[wwstage][%s]: brc=%d exit=%d " "want_exit=%d\n", rows[i].label, wbrc, gw, rows[i].want_exit); fail++; } if (gw != gc) { fprintf(stderr, "defercap[%s]: cs=%d != ww=%d " "(defer-cap divergence — #40)\n", rows[i].label, gc, gw); fail++; } } } if (fail) { fprintf(stderr, "defercap_run: %d/%d checks failed\n", fail, total); return 1; } printf("defercap_run: %d/%d ok\n", total, total); return 0; }