Bug #22655
open`eval("+1"*2**20)` dies with SIGILL instead of SystemStackError on Ruby 3.4/4.0 (Prism)
Description
Reproduction¶
It also happens without eval:
$ ruby -e 'File.write("plus.rb", "+1"*2**20)'
$ ruby plus.rb
zsh: illegal hardware instruction ruby plus.rb
The process is killed by SIGILL (exit status 132). Ruby prints nothing, not even a [BUG] report.
Results¶
macOS 27.3, arm64:
| Ruby | Result |
|---|---|
| 3.3.12 | stack level too deep (SystemStackError) |
| 3.4.10 +PRISM | crash (SIGILL) |
3.4.10 --parser=parse.y |
stack level too deep (SystemStackError) |
| 4.0.7 +PRISM | crash (SIGILL) |
4.0.7 --parser=parse.y |
stack level too deep (SystemStackError) |
| 4.1.0dev (2026-10-13 master c31e15064b) +PRISM | stack level too deep (SystemStackError) |
With 4.0.7, the result depends on the size:
| Input | Result |
|---|---|
"+1"*2**15 |
OK |
"+1"*2**16, "+1"*2**17 |
stack level too deep (SystemStackError) |
"+1"*2**18 and larger |
crash (SIGILL) |
I tested only on macOS.
Analysis¶
Parsing itself succeeds.
Prism.parse("+1"*2**20) returns no errors, because the left-associative + chain does not count toward the nesting depth limit.
The crash happens in two steps.
pm_compile_nodeandpm_compile_call_noderecurse once for each+, and the machine stack overflows (about 58,000 frames). Ruby turns this overflow intoSystemStackError.- While the exception propagates,
pm_parse_result_freefrees the AST withpm_node_destroy, which also recurses once for each+. With 2**18 or more levels, this overflows the machine stack again, and the second overflow kills the process.
This is why 216 and 217 raise SystemStackError normally and 2**18 crashes.
The stack at the SIGILL (lldb, 4.0.7, eval case):
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGILL
pm_node_destroy x174188
pm_node_list_destroy x1
pm_node_destroy x2
pm_parse_result_free x1
eval_make_iseq x1
eval_string_with_cref x1
rb_f_eval x1
vm_call_cfunc_with_frame_ x1
vm_exec_core x1
rb_vm_exec x1
rb_ec_exec_node x1
ruby_run_node x1
main x1
3.4.10 shows the same stack.
In the file case, pm_parse_result_free is called from ruby_process_options.
master does not crash, probably because 45250a4169 ("[PRISM] Use arena allocator for nodes") frees nodes without recursion.
ruby_4_0 (f388de2647) still frees nodes with pm_node_destroy.
Related¶
- [Bug #21114] (Prism hang on deeply nested
def) - https://github.com/ruby/prism/issues/4305 (
PRISM_DEPTH_MAXIMUMand the C stack size)
No data to display