Skip to content

Don't load the context twice for __ENCODING__ - #9831

Merged
headius merged 1 commit into
jruby:masterfrom
sferik:jit-encoding-stack
Oct 20, 2026
Merged

headius merged 1 commit into
jruby:masterfrom
sferik:jit-encoding-stack

Conversation

@sferik

@sferik sferik commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9825.

GetEncodingInstr loaded the ThreadContext and then called pushRubyEncoding, which loads it again itself (its doc says "Stack required: none"). The extra context sat on the operand stack unused. In straight-line code nothing noticed, but anywhere branches merge, like v ? __ENCODING__ : 1 or v && __ENCODING__, the two paths reached the merge with different stack depths, and ASM failed to compute frames. The fix is to drop the extra loadContext().

The new spec covers both of those forms. It fails in JIT and AOT mode before this change and passes in all four modes after it. With it, test/mri/ruby/test_pattern_matching.rb compiles completely, and the JIT compiles the methods from the issue and returns the right encodings.

While writing the spec, I noticed that array patterns like [1] in [1] fail in the IR persistence mode with an unrelated NullPointerException, so the spec sticks to the non-pattern forms. That turned out to be a bug in reading persisted IR, which also breaks jrubyc-compiled files with array patterns, and is now #9834.

GetEncodingInstr loaded the ThreadContext before calling
pushRubyEncoding, which loads it again itself. The extra context was
left on the operand stack. That went unnoticed in straight-line code,
but where branches merge the stack depths differed, so ASM failed to
compute frames and the method couldn't be compiled.

Fixes jruby#9825
@sferik
sferik force-pushed the jit-encoding-stack branch from 2a64917 to e31e3fa Compare October 19, 2026 23:49

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of word salad for a simple double push, but obviously this is an easy and obvious fix.

@headius headius added this to the JRuby 10.1.3.0 milestone Oct 20, 2026
@headius
headius merged commit 5343487 into jruby:master Oct 20, 2026
135 checks passed
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.

JIT can't compile __ENCODING__ on one side of a branch (ArrayIndexOutOfBoundsException in ASM frame computation)

2 participants