From d960971c6e32c1406ce47766b477da55340b5f20 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 27 May 2026 00:12:39 +0900 Subject: [PATCH] wcc: continue runs post-step in 3-clause for and range form (#138) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `for (init; cond; post) { ... continue; ... }` and `for (let i .. xs) { ... continue; ... }` now emit a `post` (3-clause) or `rpost` (range) label between the body and the JMP back to the cond-test. `continue` jumps to that label, runs the post-step, then re-tests the loop condition — mirrors C/Go/Hare semantics. Pre-fix both stages emitted `JMP loop_top` for continue, SKIPPING the post-step → the value that triggered continue never advanced → silent infinite loop on the first matching iteration. Found by impl-strconv-fold2 during the fold-3 decimal.ha port: `leftshift_newdigits`'s `for (... i+=1) { ... else if (d.digits[i]==p5[i]) continue; ... }` would infinite-loop at the first equal digit. BOTH stages were identically buggy → 990-997 cs==ww byte-id held → gate-blind. Bootstrap audit (`grep -rE 'for \(let .*\.\.' lib/ selfhost/`) confirmed zero existing callers with continue in either the 3-clause or range form; bootstrap-NEUTRAL. Sites: cmd/w6c/cgen.c N_FOR + N_FORRANGE; selfhost/cmd/wcc/ cgenstmt.ww cgfor + cgforrange. 1-clause `for (cond)` byte-id preserved (cont_target stays = loop_top when n.rhs == nil). Rule-11 carve-out: 3-clause and range share the lowered structure; fixing one without the other would leave the same silent miscompile in N_FORRANGE — one-class closure on the continue-skips-post bug, same precedent as #133-expanded. 911_continue_run: 4 rows. for3_skip_one (lead's repro, was infinite loop, now 4), for3_skip_two (nested continues, 30), range_skip (Hare-range continue, was infinite loop, now 120), for1_continue_ byteid (1-clause regression assertion — bootstrap shape unchanged). Pre-existing parser-side divergences (cstage silently drops post in the never-used 2-clause `for (cond; post)`; wwstage doesn't support infinite `for {}`) deferred to #139 — not in decimal.ha, no shared class with the cgen continue-skips-post. --- Makefile | 6 + cmd/w6c/cgen.c | 25 ++- selfhost/cmd/w6c/main.combined.ww | 23 ++- selfhost/cmd/wcc/cgenstmt.ww | 23 ++- selfhost/cmd/wwdump/main.combined.ww | 23 ++- test/wcc/911_continue_run.c | 228 +++++++++++++++++++++++++++ 6 files changed, 316 insertions(+), 12 deletions(-) create mode 100644 test/wcc/911_continue_run.c diff --git a/Makefile b/Makefile index 6f3ff5fe..0fa4aaa3 100644 --- a/Makefile +++ b/Makefile @@ -332,6 +332,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_deref_narrow_run \ $(BIN)/test_idx_compound_run \ $(BIN)/test_dotbase_arr_run \ + $(BIN)/test_continue_run \ $(BIN)/test_f64cgen_run \ $(BIN)/test_f64crossmod_run \ $(BIN)/test_tuprecv_run \ @@ -1106,6 +1107,11 @@ $(BIN)/test_dotbase_arr_run: test/wcc/949_dotbase_arr_run.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_continue_run: test/wcc/911_continue_run.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_f64cgen_run: test/wcc/951_f64cgen_run.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index ecb80bf2..865d2eaf 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -7812,8 +7812,15 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) char *end = mklabel(c, "rend"); char *natural_exit = end; if (n->els) natural_exit = mklabel(c, "relseloop"); + /* #138 (range form): `continue` must run the implicit `i+=1` + * post-step before re-testing the loop bound. Pre-fix the + * cont-target was `loop` (top), skipping the ADDQ $1, ioff + * below the body — infinite loop on the value that triggered + * continue. Dedicated `rpost` label; bootstrap-NEUTRAL (no + * range-form continue callers in lib/ or selfhost/). */ + char *rpost = mklabel(c, "rpost"); if (nloops < LOOP_MAX) { - loop_cont[nloops] = loop; + loop_cont[nloops] = rpost; loop_brk[nloops] = end; nloops++; } @@ -7842,6 +7849,7 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, binds[b].off)); } cgstmt(c, n->body, locals, frame); + label(c, rpost); ins2(c, A_ADDQ, aimm(1), amem(D_BP, ioff)); ins1(c, A_JMP, abranch(loop)); if (n->els) { @@ -7860,6 +7868,14 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) * the else block sits between them. */ char *natural_exit = end; if (n->els) natural_exit = mklabel(c, "elseloop"); + /* #138: `continue` in a 3-clause `for (init; cond; post)` must + * run the post-step before re-testing cond. Pre-fix the + * continue-target was `loop` (top), which SKIPPED post → state + * never advanced → infinite loop. Allocate a dedicated `post` + * label only when there IS a post-step (`n->rhs`); else keep + * continue → loop-top, byte-id with 1-clause for. */ + char *cont_target = loop; + if (n->rhs) cont_target = mklabel(c, "post"); if (n->lhs) cgstmt(c, n->lhs, locals, frame); label(c, loop); if (n->cond) { @@ -7868,13 +7884,16 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) ins1(c, A_JE, abranch(natural_exit)); } if (nloops < LOOP_MAX) { - loop_cont[nloops] = loop; + loop_cont[nloops] = cont_target; loop_brk[nloops] = end; nloops++; } cgstmt(c, n->body, locals, frame); if (nloops > 0) nloops--; - if (n->rhs) cgexpr(c, n->rhs, *locals); + if (n->rhs) { + label(c, cont_target); + cgexpr(c, n->rhs, *locals); + } ins1(c, A_JMP, abranch(loop)); if (n->els) { label(c, natural_exit); diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 27115091..19c7f938 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -22365,6 +22365,14 @@ fn cgfor(c: *cgen, n: *node) void = { // label so the else body sits between it and the break target. let naturall: str = endl; if (n.els != nil) { naturall = mklabel(c, "elseloop"); }; + // #138: `continue` in a 3-clause `for (init; cond; post)` must + // run the post-step before re-testing cond. Pre-fix the continue- + // target was `topl`, which SKIPPED the post-step → state never + // advanced → infinite loop. Allocate a dedicated `post` label + // only when there IS a post-step (`n.rhs != nil`); else keep + // continue → loop-top, byte-id with 1-clause for. + let conttgt: str = topl; + if (n.rhs != nil) { conttgt = mklabel(c, "post"); }; if (n.lhs != nil) { cgstmt(c, n.lhs); }; @@ -22376,14 +22384,17 @@ fn cgfor(c: *cgen, n: *node) void = { }; c.loopendbuf[c.looptop] = endl; - c.loopcontbuf[c.looptop] = topl; + c.loopcontbuf[c.looptop] = conttgt; c.looptop += 1; if (n.body != nil) { cgstmt(c, n.body); }; c.looptop -= 1; - if (n.rhs != nil) { cgexpr(c, n.rhs); }; + if (n.rhs != nil) { + emitlabel(conttgt); + cgexpr(c, n.rhs); + }; emitline("\tJMP\t"); emitline(topl); emitline("\n"); if (n.els != nil) { emitlabel(naturall); @@ -22738,8 +22749,13 @@ fn cgforrange(c: *cgen, n: *node) void = { let endl: str = mklabel(c, "rend"); let naturall: str = endl; if (n.els != nil) { naturall = mklabel(c, "relseloop"); }; + // #138 (range form): `continue` must run the implicit `i+=1` + // post-step before re-testing the bound. Pre-fix cont = loopl + // (top), skipping the ADDQ $1, ioff below — infinite loop on + // the value that triggered continue. Dedicated `rpost` label. + let rpost: str = mklabel(c, "rpost"); - c.loopcontbuf[c.looptop] = loopl; + c.loopcontbuf[c.looptop] = rpost; c.loopendbuf[c.looptop] = endl; c.looptop += 1; @@ -22796,6 +22812,7 @@ fn cgforrange(c: *cgen, n: *node) void = { c.looptop -= 1; + emitlabel(rpost); emitline("\tADDQ\t$1, "); emitoff(ioff: i64); emitline("(BP)\n"); diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 2c1c31b4..3a4a8961 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -1362,6 +1362,14 @@ fn cgfor(c: *cgen, n: *node) void = { // label so the else body sits between it and the break target. let naturall: str = endl; if (n.els != nil) { naturall = mklabel(c, "elseloop"); }; + // #138: `continue` in a 3-clause `for (init; cond; post)` must + // run the post-step before re-testing cond. Pre-fix the continue- + // target was `topl`, which SKIPPED the post-step → state never + // advanced → infinite loop. Allocate a dedicated `post` label + // only when there IS a post-step (`n.rhs != nil`); else keep + // continue → loop-top, byte-id with 1-clause for. + let conttgt: str = topl; + if (n.rhs != nil) { conttgt = mklabel(c, "post"); }; if (n.lhs != nil) { cgstmt(c, n.lhs); }; @@ -1373,14 +1381,17 @@ fn cgfor(c: *cgen, n: *node) void = { }; c.loopendbuf[c.looptop] = endl; - c.loopcontbuf[c.looptop] = topl; + c.loopcontbuf[c.looptop] = conttgt; c.looptop += 1; if (n.body != nil) { cgstmt(c, n.body); }; c.looptop -= 1; - if (n.rhs != nil) { cgexpr(c, n.rhs); }; + if (n.rhs != nil) { + emitlabel(conttgt); + cgexpr(c, n.rhs); + }; emitline("\tJMP\t"); emitline(topl); emitline("\n"); if (n.els != nil) { emitlabel(naturall); @@ -1735,8 +1746,13 @@ fn cgforrange(c: *cgen, n: *node) void = { let endl: str = mklabel(c, "rend"); let naturall: str = endl; if (n.els != nil) { naturall = mklabel(c, "relseloop"); }; + // #138 (range form): `continue` must run the implicit `i+=1` + // post-step before re-testing the bound. Pre-fix cont = loopl + // (top), skipping the ADDQ $1, ioff below — infinite loop on + // the value that triggered continue. Dedicated `rpost` label. + let rpost: str = mklabel(c, "rpost"); - c.loopcontbuf[c.looptop] = loopl; + c.loopcontbuf[c.looptop] = rpost; c.loopendbuf[c.looptop] = endl; c.looptop += 1; @@ -1793,6 +1809,7 @@ fn cgforrange(c: *cgen, n: *node) void = { c.looptop -= 1; + emitlabel(rpost); emitline("\tADDQ\t$1, "); emitoff(ioff: i64); emitline("(BP)\n"); diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 3f770659..eb723459 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -22365,6 +22365,14 @@ fn cgfor(c: *cgen, n: *node) void = { // label so the else body sits between it and the break target. let naturall: str = endl; if (n.els != nil) { naturall = mklabel(c, "elseloop"); }; + // #138: `continue` in a 3-clause `for (init; cond; post)` must + // run the post-step before re-testing cond. Pre-fix the continue- + // target was `topl`, which SKIPPED the post-step → state never + // advanced → infinite loop. Allocate a dedicated `post` label + // only when there IS a post-step (`n.rhs != nil`); else keep + // continue → loop-top, byte-id with 1-clause for. + let conttgt: str = topl; + if (n.rhs != nil) { conttgt = mklabel(c, "post"); }; if (n.lhs != nil) { cgstmt(c, n.lhs); }; @@ -22376,14 +22384,17 @@ fn cgfor(c: *cgen, n: *node) void = { }; c.loopendbuf[c.looptop] = endl; - c.loopcontbuf[c.looptop] = topl; + c.loopcontbuf[c.looptop] = conttgt; c.looptop += 1; if (n.body != nil) { cgstmt(c, n.body); }; c.looptop -= 1; - if (n.rhs != nil) { cgexpr(c, n.rhs); }; + if (n.rhs != nil) { + emitlabel(conttgt); + cgexpr(c, n.rhs); + }; emitline("\tJMP\t"); emitline(topl); emitline("\n"); if (n.els != nil) { emitlabel(naturall); @@ -22738,8 +22749,13 @@ fn cgforrange(c: *cgen, n: *node) void = { let endl: str = mklabel(c, "rend"); let naturall: str = endl; if (n.els != nil) { naturall = mklabel(c, "relseloop"); }; + // #138 (range form): `continue` must run the implicit `i+=1` + // post-step before re-testing the bound. Pre-fix cont = loopl + // (top), skipping the ADDQ $1, ioff below — infinite loop on + // the value that triggered continue. Dedicated `rpost` label. + let rpost: str = mklabel(c, "rpost"); - c.loopcontbuf[c.looptop] = loopl; + c.loopcontbuf[c.looptop] = rpost; c.loopendbuf[c.looptop] = endl; c.looptop += 1; @@ -22796,6 +22812,7 @@ fn cgforrange(c: *cgen, n: *node) void = { c.looptop -= 1; + emitlabel(rpost); emitline("\tADDQ\t$1, "); emitoff(ioff: i64); emitline("(BP)\n"); diff --git a/test/wcc/911_continue_run.c b/test/wcc/911_continue_run.c new file mode 100644 index 00000000..b26d6217 --- /dev/null +++ b/test/wcc/911_continue_run.c @@ -0,0 +1,228 @@ +/* + * 911_continue_run — runtime + byte-id net for #138: `continue` in a + * loop with a post-step (3-clause C-style `for (init; cond; post)` OR + * Hare-range `for (let x .. xs)` with its implicit `i+=1`) must run + * the post-step BEFORE re-testing the cond / bound. Pre-fix the + * continue-target was the loop top, which SKIPPED the post-step → the + * value that triggered continue never advanced → infinite loop. + * + * Pre-fix BOTH STAGES emitted identical buggy asm (`JMP loop_top` for + * continue, post-step JMP'd over) — cs==ww byte-id held → 990-997 gate + * was BLIND to the miscompile. Found by impl-strconv-fold2 during the + * fold-3 decimal.ha port: leftshift_newdigits uses `for (let i: u32 = + * 0u32; i < n; i += 1u32) { ... else if (d.digits[i]==p5[i]) continue; + * ... }`; pre-fix that path infinite-looped at the first equal digit. + * + * Fix (both stages): allocate a dedicated `post` (3-clause) / `rpost` + * (range) label; continue jumps to that label; the label emits before + * the post-step; fall-through naturally hits it too. For 1-clause `for + * (cond)` with no post-step, the cont-target stays = loop-top + * (unchanged from pre-#138, byte-id preserved on the corpus's + * 1-clause shape). + * + * Rows cover the 3 shapes the fix targets PLUS a 1-clause regression + * row asserting the byte-id surface for the 1-clause path is + * unchanged. cstage `ww build` + run for runtime; w6c vs w6c_ww `.s` + * cmp for rule-10 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; const char *src; int want_exit; }; + +static const struct row rows[] = { + /* The lead's repro: 3-clause for with continue. Pre-fix infinite + * loop on i=2; post-fix count=4 (i=0,1,3,4 all increment count). */ + { "for3_skip_one", + "package main;\n" + "export fn main() i32 = {\n" + " let count: i32 = 0;\n" + " for (let i: i32 = 0; i < 5; i += 1) {\n" + " if (i == 2) { continue; };\n" + " count += 1;\n" + " };\n" + " return count;\n" + "};\n", 4 }, + /* Two skips: continue must advance i correctly each time. count + * over i=0,2,4 only (skip 1 and 3); 3 hits × 10 = 30. */ + { "for3_skip_two", + "package main;\n" + "export fn main() i32 = {\n" + " let c: i32 = 0;\n" + " for (let i: i32 = 0; i < 5; i += 1) {\n" + " if (i == 1) { continue; };\n" + " if (i == 3) { continue; };\n" + " c += 10;\n" + " };\n" + " return c;\n" + "};\n", 30 }, + /* Hare-range form `for (let x .. xs)`. Continue must run the + * implicit `i+=1` increment. Pre-fix infinite-loop on first + * matching element; post-fix sum over non-skipped values. xs = + * [10, 20, 30, 40, 50]; skip 30; sum = 10+20+40+50 = 120. */ + { "range_skip", + "package main;\n" + "export fn main() i32 = {\n" + " let xs: [5]i32 = [10, 20, 30, 40, 50];\n" + " let sum: i32 = 0;\n" + " for (let v .. xs) {\n" + " if (v == 30) { continue; };\n" + " sum += v;\n" + " };\n" + " return sum;\n" + "};\n", 120 }, + /* 1-clause `for (cond)` with continue. No post-step, so cont- + * target stays = loop-top. Pre-#138 emission must be byte-id + * preserved — this is the bootstrap shape. Manual post-step + * inside the body. count over i=0,1,3,4 (skip i=2) → 4. */ + { "for1_continue_byteid", + "package main;\n" + "export fn main() i32 = {\n" + " let count: i32 = 0;\n" + " let i: i32 = 0;\n" + " for (i < 5) {\n" + " let cur: i32 = i;\n" + " i += 1;\n" + " if (cur == 2) { continue; };\n" + " count += 1;\n" + " };\n" + " return count;\n" + "};\n", 4 }, + { 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, "continue: w6c_ww missing — cannot run the " + "cs==ww byte-id gate (the whole point of this test)\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/wwcont_%d_%d.ww", getpid(), i); + FILE *f = fopen(src, "wb"); + if (f == NULL) { fail++; continue; } + fputs(rows[i].src, f); + fclose(f); + + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwcont_%d_d_%d", + getpid(), i); + mkdir(tmpdir, 0755); + + /* Build with 5s timeout — pre-fix the buggy rows infinite- + * looped; post-fix all rows must exit cleanly under it. */ + 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'; + + /* timeout wrapper: pre-fix bug = infinite loop; want clean + * exit. exit 124 = timeout/hang. */ + char rcmd[256]; + snprintf(rcmd, sizeof rcmd, "timeout 5 %s", outbin); + int got = runwait(rcmd); + if (got != rows[i].want_exit) { + fprintf(stderr, "row[%s]: cstage exit %d, want %d " + "(124 = timeout / pre-fix infinite loop)\n", + rows[i].label, got, rows[i].want_exit); + fail++; + } + unlink(outbin); rmdir(tmpdir); + + char cs_s[64], ws_s[64]; + snprintf(cs_s, sizeof cs_s, "/tmp/wwcont_%d_%d_cs.s", + getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/wwcont_%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++; + } + unlink(src); unlink(cs_s); unlink(ws_s); + } + + if (fail) { + fprintf(stderr, "%d/%d continue tests failed\n", fail, n); + return 1; + } + printf("continue: %d/%d ok (cstage run + cs==ww byte-id)\n", + n, n); + return 0; +}