Commit 71e29303

71e2930379b7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 13 files · +12 added · ~1 modified

Description

Merge pull request #1441 from companygardener/fix/loop-control-flow-codegen

codegen: fix two loop control-flow miscompilations (hoisted condition arg; break-in-case-in-loop)

Details

Commit
71e2930379b7
Parent
e96c64cc3b67
Merge parent
9355bd19e14c

src/codegen_stmt.cmodifiedView file

@@ -1177,6 +1177,34 @@
11771177 }
11781178
11791179 /* case/when -> an if / else-if chain. Statement form. */
1180+/* True if the subtree contains a `break` that targets the ENCLOSING loop
1181+ (i.e. not captured by a nested loop or block-bearing iterator). Used to
1182+ decide whether a `case` may lower to a C `switch`: a Ruby `break` inside a
1183+ `when` must break the loop, but a C `break` inside a `switch` only exits the
1184+ switch -- so a case whose when-bodies break the loop must use the if-else
1185+ form instead, where C `break` correctly targets the loop. */
1186+static int subtree_has_loop_break(Compiler *c, int root) {
1187+ if (root < 0) return 0;
1188+ const NodeTable *nt = c->nt;
1189+ const char *ty = nt_type(nt, root);
1190+ if (ty) {
1191+ if (!strcmp(ty, "BreakNode")) return 1;
1192+ /* nested loops / block-bearing iterators capture their own break/next */
1193+ if (!strcmp(ty, "WhileNode") || !strcmp(ty, "UntilNode") || !strcmp(ty, "ForNode"))
1194+ return 0;
1195+ if (!strcmp(ty, "CallNode") && nt_ref(nt, root, "block") >= 0)
1196+ return 0;
1197+ }
1198+ int nr = nt_num_refs(nt, root);
1199+ for (int i = 0; i < nr; i++) if (subtree_has_loop_break(c, nt_ref_at(nt, root, i))) return 1;
1200+ int na = nt_num_arrs(nt, root);
1201+ for (int i = 0; i < na; i++) {
1202+ int n = 0; const int *el = nt_arr_at(nt, root, i, &n);
1203+ for (int j = 0; j < n; j++) if (subtree_has_loop_break(c, el[j])) return 1;
1204+ }
1205+ return 0;
1206+}
1207+
11801208 void emit_case(Compiler *c, int id, Buf *b, int indent) {
11811209 const NodeTable *nt = c->nt;
11821210 int pred = nt_ref(nt, id, "predicate");
@@ -1210,7 +1238,13 @@
12101238 if (!cty || strcmp(cty, "IntegerNode")) { all_int = 0; break; }
12111239 }
12121240 }
1213- if (all_int) {
1241+ /* A C switch can't host a Ruby `break` that targets the enclosing loop
1242+ (C break exits the switch, not the loop). If any arm breaks the loop,
1243+ fall through to the if-else form below where break works correctly. */
1244+ int has_lbreak = (else_clause >= 0) && subtree_has_loop_break(c, nt_ref(nt, else_clause, "statements"));
1245+ for (int w = 0; w < nw && !has_lbreak; w++)
1246+ if (subtree_has_loop_break(c, nt_ref(nt, whens[w], "statements"))) has_lbreak = 1;
1247+ if (all_int && !has_lbreak) {
12141248 emit_indent(b, indent);
12151249 if (pt == TY_POLY) buf_printf(b, "switch (sp_poly_to_i(_t%d)) {\n", t);
12161250 else buf_printf(b, "switch (_t%d) {\n", t);
@@ -1579,15 +1613,45 @@
15791613 g_hoist_len_var = hbuf; g_hoist_len_recv = hn;
15801614 }
15811615 }
1582- emit_indent(b, indent);
1583- buf_puts(b, "while (");
1584- if (is_until) buf_puts(b, "!(");
1585- emit_cond(c, pred, b);
1586- if (is_until) buf_puts(b, ")");
1587- buf_puts(b, ") {\n");
1588- emit_loop_body(c, body, b, indent + 1);
1589- emit_indent(b, indent);
1590- buf_puts(b, "}\n");
1616+ /* Capture the predicate and any expression preludes it needs (method-call
1617+ temps etc.) into local buffers. A loop condition like
1618+ `advance while ident_continue_byte?(byte)` evaluates a method call
1619+ (`byte`, which reads mutable state) every iteration -- but emit_cond
1620+ routes that call's setup through g_pre, which is normally flushed ONCE
1621+ before the statement. For a loop that hoists the call out of the loop, so
1622+ the condition never re-evaluates (infinite loop / stuck position). Re-emit
1623+ the preludes INSIDE the loop and break on the (negated) condition so the
1624+ condition is recomputed each iteration. Conditions with no prelude keep
1625+ the plain `while (cond)` form. */
1626+ Buf cpre; memset(&cpre, 0, sizeof cpre);
1627+ Buf ccond; memset(&ccond, 0, sizeof ccond);
1628+ Buf *sv_pre = g_pre; int sv_ind = g_indent;
1629+ g_pre = &cpre; g_indent = indent + 1;
1630+ emit_cond(c, pred, &ccond);
1631+ g_pre = sv_pre; g_indent = sv_ind;
1632+ if (cpre.p && cpre.p[0]) {
1633+ emit_indent(b, indent); buf_puts(b, "while (1) {\n");
1634+ buf_puts(b, cpre.p);
1635+ emit_indent(b, indent + 1);
1636+ buf_puts(b, "if (");
1637+ if (!is_until) buf_puts(b, "!(");
1638+ buf_puts(b, ccond.p ? ccond.p : "0");
1639+ if (!is_until) buf_puts(b, ")");
1640+ buf_puts(b, ") break;\n");
1641+ emit_loop_body(c, body, b, indent + 1);
1642+ emit_indent(b, indent); buf_puts(b, "}\n");
1643+ } else {
1644+ emit_indent(b, indent);
1645+ buf_puts(b, "while (");
1646+ if (is_until) buf_puts(b, "!(");
1647+ buf_puts(b, ccond.p ? ccond.p : "");
1648+ if (is_until) buf_puts(b, ")");
1649+ buf_puts(b, ") {\n");
1650+ emit_loop_body(c, body, b, indent + 1);
1651+ emit_indent(b, indent);
1652+ buf_puts(b, "}\n");
1653+ }
1654+ free(cpre.p); free(ccond.p);
15911655 g_hoist_len_var = sv_hvar; g_hoist_len_recv = sv_hrecv;
15921656 }
15931657