Project

General

Profile

Actions

Bug #22644

closed

`argument stack underflow (-1)` and `iseq_set_sequence: adjust bug` on `return` inside `begin ... ensure`

Bug #22644: `argument stack underflow (-1)` and `iseq_set_sequence: adjust bug` on `return` inside `begin ... ensure`

Added by steviejj (Steven Johnstone) 2 days ago. Updated 1 day ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-11-26T13:52:03Z HEAD 17eecca74a) +PRISM [aarch64-linux]
[ruby-core:127735]

Description

Differential fuzzer finds (prism vs parse.y)

Variant A: argument stack underflow (-1)

def f
  d begin
    return
  rescue
  ensure
  end
  d
end
ruby --parser=prism  --disable-gems -e 'RubyVM::InstructionSequence.compile("def f;d begin return;rescue;ensure end;d end")'
ruby --parser=parse.y --disable-gems -e 'RubyVM::InstructionSequence.compile("def f;d begin return;rescue;ensure end;d end")'

Variant B: iseq_set_sequence: adjust bug 1 < 2

begin
  begin
    e "" * while o
      begin
        begin
          return
        ensure
          break
        end
      end
    end
  end
end
ruby --parser=prism  --disable-gems -e 'RubyVM::InstructionSequence.compile("begin begin e\"\"*while o\nbegin begin return ensure break end end end end end")'
ruby --parser=parse.y --disable-gems -e 'RubyVM::InstructionSequence.compile("begin begin e\"\"*while o\nbegin begin return ensure break end end end end end")'

Observed Output

Variant A (identical on Prism and parse.y)

-- raw disasm--------
   0000 putself                                                          (   1)
 <L004> [sp: 1, unremovable: 1, refcnt: 1]
 <L007> [sp: 1, unremovable: 1, refcnt: 2]
 <L010> [sp: 1, unremovable: 0, refcnt: 0]
   adjust: [label: -1]
   0001 putnil                                                           (   1)
   0002 throw                1                                           (   1)
 <L008> [sp: 1, unremovable: 1, refcnt: 2]
   0004 nop                                                              (   1)
 <L009> [sp: 1, unremovable: 0, refcnt: 2]
 <L005> [sp: 1, unremovable: 0, refcnt: 1]
   0005 nop                                                              (   0)
 <L006> [sp: 1, unremovable: 0, refcnt: 1]
   0006 opt_send_without_block <calldata:d, 1>                           (   1)
*  0008 pop                                                              (   1)
---------------------
<compiled>:1: argument stack underflow (-1) (SyntaxError)

Variant B (identical on Prism and parse.y)

-- raw disasm--------
 <L009> [sp: 2, unremovable: 1, refcnt: 8]
 <L014> [sp: 2, unremovable: 1, refcnt: 1]
 <L018> [sp: 2, unremovable: 1, refcnt: 1]
   adjust: [label: -1]
   0009 putnil                                                           (   2)
 <L019> [sp: 1, unremovable: 0, refcnt: 1]
 <L021> [sp: 1, unremovable: 1, refcnt: 1]
*  adjust: [label: 9]
   0010 putnil                                                           (   2)
   0011 jump                 <L010>                                      (   2)
   adjust: [label: 21]
---------------------
<compiled>:2: iseq_set_sequence: adjust bug 1 < 2 (SyntaxError)

Updated by ydah (Yudai Takada) 1 day ago Actions #1

  • Status changed from Open to Closed

Applied in changeset git|ba5c016a98797fd300f62ac58fffdcf0505bf63d.


[Bug #22644] Preserve the stack for returns through ensure

Use the same decision for adjusting the stack and emitting a direct return in both compilers. When a return must unwind through an ensure, leave pending operands on the stack. Let the VM unwind ensures in loops so an ensure that breaks out of the loop can resume with those operands intact.

Actions

Also available in: PDF Atom