Commit 437e9fc7
437e9fc7ac3aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix: four hang patterns from #548
Adam Doppelt's "Puzzles, third edition" -- four shapes that compiled cleanly but hung at runtime.
1. a.concat(a, a) -- self-concat infinite loop + dropped
tail args.
compile_call_stmt's array#concat arm queried the
source-array length inside its push loop. When the arg
aliased the receiver (a.concat(a)), every push grew the
src len and the loop never terminated. The arm also only
processed the first arg (compile_arg0), silently dropping
the second.
Fix: snapshot each arg's length before its inner loop, and iterate every arg in turn.
2. /\#{re}/ -- regex compiler infinite loop on literal
{ after an atom.
The regex \#{re} lowers \# to literal #, then arrives
at compile_atom with { as the leading char. { was in
compile_atom's "not an atom" filter (alongside * + ?),
so compile_atom returned without consuming. compile_seq's
outer while peek() >= 0 loop then spun -- compile_quantified
returns no-atom, control falls back to compile_seq, peek is
still {, repeat.
CRuby treats /{re}/ as matching the literal text {re}
(a {...} without a valid quantifier shape is literal).
Fix: remove { from compile_atom's filter so a leading
{ falls through to the literal-char emit. Quantifier {n}
after an atom is still handled by the existing
compile_quantified else if (ch == '{') arm.
3. while () ; 123 ; end -- empty parens condition lowers
to always-true.
Empty () infers as "void" and lowers compile_expr to
literal 0. compile_cond_expr's catch-all
((expr), TRUE) flipped the empty-parens condition to
always-true and the while loop hung. CRuby treats () as
nil (while nil never enters).
Fix: treat "void" like "nil" in compile_cond_expr -- both lower to FALSE.
4. i += 1; raise; rescue; retry unless i == 7 -- retry-
counter not preserved across longjmp.
lv_i modified between setjmp and longjmp must be
volatile per the C standard -- the compiler is allowed
to keep non-volatile locals in registers across the pair,
so longjmp's stack-unwind doesn't see the latest value.
With -O3, lv_i was register-cached and the rescue
handler saw a stale 0, so i == 7 was never satisfied and
retry spun forever.
Fix: pre-scan method bodies for setjmp-triggering shapes
(BeginNode + rescue / RetryNode / CallNode raise) and
mark non-pointer locals as volatile. Pointer locals stay
non-volatile -- they're already GC-rooted via SP_GC_ROOT
which incidentally keeps them stable across longjmp; the
volatile-qualifier cascade through helper signatures
(sp_str_length, sp_IntArray_length, etc.) would otherwise
trip -Werror in the test harness.
The same pointer-local exclusion is applied at top-level declarations too (emit_main / compile_body_decl_locals) -- the previous always-volatile-when-setjmp path tagged pointer locals too but the discarded-qualifier warning was silently absorbed when CFLAGS didn't include -Werror. With the test harness's per-test -Werror, the warning surfaces. The exclusion is the safer default.
Closes #548
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 437e9fc7ac3a
- Parent
- 4529387ebba2
lib/regexp/re_compile.cmodifiedView file
break; default: if (ch < 0 || ch == ')' || ch == '|' || ch == '*' || ch == '+' || ch == '?' || ch == '{') { if (ch < 0 || ch == ')' || ch == '|' || ch == '*' || ch == '+' || ch == '?') { return; /* not an atom */ } /* `{` with no preceding atom (or after one whose quantifier parse failed) is a literal `{`. Without this, compile_seq's outer loop spins -- compile_quantified returns no-atom and the loop never advances. CRuby treats `/{re}/` as matching the literal text `{re}`; we mirror that here. Issue #548. */ next_char(c); if ((c->flags & RE_FLAG_IGNORECASE) && ch < 128) { if (ch >= 'A' && ch <= 'Z') {spinel_codegen.rbmodifiedView file
Loading diff…
test/hangs_548.rbaddedView file
Loading diff…
test/hangs_548.rb.expectedaddedView file
Loading diff…