Skip to content

Build literal ranges that can raise with an instruction - #9851

Open
sferik wants to merge 1 commit into
jruby:masterfrom
sferik:ir-range-literal-raise
Open

sferik wants to merge 1 commit into
jruby:masterfrom
sferik:ir-range-literal-raise

Conversation

@sferik

@sferik sferik commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

This fixes the test:mri:core:jit failures on master since #9838. All the tests pass, but then test-unit's at_exit block crashes with ArrayIndexOutOfBoundsException: Index -1 in ThreadContext#getNextFrame.

A range whose endpoints are both literals becomes a Range operand, which gets built wherever its value is used rather than where it appears. That's fine for 1..10 or "a".."z", but a range of incomparable literals like 1.."3" raises ArgumentError when it's built, so it raises in the wrong place:

begin
  (1.."3")
rescue ArgumentError
  :rescued
end
# CRuby: :rescued
# JRuby: ArgumentError: bad value for range

The range is the value of the begin body, so it's built after the rescue region and the rescue never runs. That happens in every mode and goes back at least to 10.1.2.0.

The same thing in a compiled block is worse with -Xdebug.fullTrace, which the MRI test tasks use. The block's IR ends with pop_block_frame followed by return(rng<Fixnum:1..frozen:"3">), so the range raises after the frame is popped, and the global ensure block pops it again. test_range.rb has assert_raise(ArgumentError) { (1.."3") } in test_new, which leaks one frame each time it runs. Those blocks couldn't be compiled before #9838, which is why this only started then. It doesn't show up without full tracing, which is why I couldn't reproduce it locally at first.

This only makes a Range operand when building it can't raise: when an endpoint is nil, or both are real numbers, strings or symbols. Anything else, like 1.."3", :a..1 or true..false, is built by a BuildRangeInstr where it appears.

The new spec in general_spec.rb fails in all four modes before this change and passes after it. With CI's exact options, rake test:mri:core:jit passes locally with no crash, and the compiler specs pass with and without invokedynamic.

A range whose endpoints are both literals became a Range operand,
which is built wherever its value is used rather than where it appears.
But a range of incomparable literals like 1.."3" raises ArgumentError
when it's built, so it raised in the wrong place. As the value of a
begin body it raised after the rescue region, so the rescue never ran.
As the value of a compiled block under --debug, it raised after the
block had popped its frame, and the block's exception handler popped
the frame again.

That frame leak is what has been crashing test:mri:core:jit since
jruby#9838 let blocks like
{ (1.."3") } in test_range.rb compile: test-unit's at_exit block runs
out of frames and fails with ArrayIndexOutOfBoundsException.

Only make a Range operand when building it can't raise: when an
endpoint is nil, or both are real numbers, strings or symbols.
Anything else is built by a BuildRangeInstr where it appears.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant