[pull] master from ruby:master#790
Merged
pull[bot] merged 4 commits intoturkdevops:masterfrom Feb 19, 2026
Merged
Conversation
Similar to the way ZJIT already folds +, -, and * operations. One
complication is that the % operator behaves differently in Ruby than in
Rust for negative values. For example in Ruby:
```
ruby -e "puts 11 % -3" # => -1
```
vs Rust:
```
println!("{}", 11 % -3); // => 2
```
Rust has the `#rem_euclid()` method; however, it also differs from Ruby.
My solution was to call into the cruby #rb_fix_mod_fix method to get the
correct behaviour.
Note that any `% 0` operations are not folded as they need to raise a
ZeroDivisionError.
One concern was an integer overflow. Fortunately that's not possible.
Given any two Fixnums `a`, and `b`, by definition the result of `a % b`
must be closer to 0 than `b`, making an overflow impossible.
One adjacent edge case is when calculating `RUBY_FIXNUM_MIN % -1`. The
result is 0 which is obviously fine; however, the related result of
`RUBY_FIXNUM_MIN / -1` would overflow. The #rb_fix_mod_fix method avoids
this problem by not calculating the `div` portion. See:
https://github.com/ruby/ruby/blob/07f5126c104507ec70c5bfb2eb5204ceb22b7ebd/internal/fixnum.h#L164
When --zjit-trace-exits is enabled, rb_zjit_record_exit_stack was called before compile_exit restored the VM state (cfp->pc, cfp->sp, stack, locals). This ccall clobbers caller-saved registers that may hold stack/local operands, causing garbage values to be written to the VM stack when compile_exit runs afterward. This led to crashes like `klass: T_FALSE` in the interpreter when those corrupted values were used as receivers. Fix by splitting compile_exit into compile_exit_save_state and compile_exit_return, and emitting the full exit inline when recording: save VM state first, then call rb_zjit_record_exit_stack (so rb_profile_frames sees valid cfp->pc), then return. Before this I was able to cause a crash with ``` ruby --zjit-trace-exits -rffi -e1 ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See Commits and Changes for more details.
Created by
pull[bot] (v2.0.0-alpha.4)
Can you help keep this open source service alive? 💖 Please sponsor : )