Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.ClassCastException: Cannot cast org.jruby.RubyFloat to org.jruby.RubyInteger #5655

Open
ntysdd opened this Issue Mar 18, 2019 · 6 comments

Comments

Projects
None yet
3 participants
@ntysdd
Copy link

ntysdd commented Mar 18, 2019

Environment

Provide at least:

  • JRuby version (jruby -v) and command line (flags, JRUBY_OPTS, etc)
    jruby 9.2.6.0 (2.5.3) 2019-02-11 15ba00b Java HotSpot(TM) 64-Bit Server VM 11.0.2+9-LTS on 11.0.2+9-LTS +jit [linux-x86_64]
    JRUBY_OPTS='-J-Djruby.compile.invokedynamic=true -J-XX:+UnlockExperimentalVMOptions -J-XX:+EnableJVMCI -J-XX:+UseJVMCICompiler'

  • Operating system and platform (e.g. uname -a)
    Linux aaa-laptop 4.18.0-16-generic #17-Ubuntu SMP Fri Feb 8 00:06:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Other relevant info you may wish to add:

  • Installed or activated gems
  • Application/framework version (e.g. Rails, Sinatra)
  • Environment variables

Expected Behavior

  • Describe your expectation of how JRuby should behave, perhaps by showing how CRuby/MRI behaves.
  • Provide an executable Ruby script or a link to an example repository.

no exception

Actual Behavior

  • Describe or show the actual behavior.
  • Provide text or screen capture showing the behavior.

exception


Unhandled Java exception: java.lang.ClassCastException: Cannot cast org.jruby.RubyFloat to org.jruby.RubyInteger
java.lang.ClassCastException: Cannot cast org.jruby.RubyFloat to org.jruby.RubyInteger
                         cast at java/lang/Class.java:3606
                            f at a.rb:5
  RUBY$method$f$0$__VARARGS__ at a.rb:-1
                         call at org/jruby/internal/runtime/methods/CompiledIRMethod.java:91
                       invoke at org/jruby/ir/targets/InvokeSite.java:183
                       <main> at a.rb:14
          invokeWithArguments at java/lang/invoke/MethodHandle.java:710
                         load at org/jruby/ir/Compiler.java:94
                    runScript at org/jruby/Ruby.java:854
                  runNormally at org/jruby/Ruby.java:777
                  runNormally at org/jruby/Ruby.java:795
                  runFromMain at org/jruby/Ruby.java:607
                doRunFromMain at org/jruby/Main.java:415
                  internalRun at org/jruby/Main.java:307
                          run at org/jruby/Main.java:234
                         main at org/jruby/Main.java:206


$ java -version
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

$ cat a.rb
def f(to)
	s = 0
	i = to.to_i
	while i >= 1
		s += 1.0 / (i * i)
		i -= 1
	end
	puts s
end

f(1E4)
f(1E5)
f(1E6)
f(1E7)
@kares

This comment has been minimized.

Copy link
Member

kares commented Mar 19, 2019

this seems to be a JIT issue ... only reproduces with -Xcompile.invokedynamic

@headius

This comment has been minimized.

Copy link
Member

headius commented Apr 9, 2019

Shockingly this does not fail on the standard OpenJDK JIT, C2. It seems to be another case that only fails when running on GraalVM.

@headius

This comment has been minimized.

Copy link
Member

headius commented Apr 9, 2019

A complete trace seems to show this failing inside the JDK implementation of method handles:

[] ~/projects/jruby $ jruby -Xjit.logging -Xcompile.invokedynamic -Xbacktrace.style=full blah.rb
2019-04-08T23:13:29.581-05:00 [main] INFO Ruby : done compiling target script: blah.rb
1.6448340718480596
1.6449240668982263
1.6449330668487263
Unhandled Java exception: java.lang.ClassCastException: Cannot cast org.jruby.RubyFloat to org.jruby.RubyInteger
java.lang.ClassCastException: Cannot cast org.jruby.RubyFloat to org.jruby.RubyInteger
        newClassCastException at java/lang/invoke/MethodHandleImpl.java:361
                castReference at java/lang/invoke/MethodHandleImpl.java:356
                            f at blah.rb:5
  RUBY$method$f$0$__VARARGS__ at blah.rb:-1
                         call at org/jruby/internal/runtime/methods/CompiledIRMethod.java:91
                       invoke at org/jruby/ir/targets/InvokeSite.java:183
                       <main> at blah.rb:14
          invokeWithArguments at java/lang/invoke/MethodHandle.java:627
                         load at org/jruby/ir/Compiler.java:94
                    runScript at org/jruby/Ruby.java:856
                  runNormally at org/jruby/Ruby.java:779
                  runNormally at org/jruby/Ruby.java:797
                  runFromMain at org/jruby/Ruby.java:609
                doRunFromMain at org/jruby/Main.java:415
                  internalRun at org/jruby/Main.java:307
                          run at org/jruby/Main.java:234
                         main at org/jruby/Main.java:206
@headius

This comment has been minimized.

Copy link
Member

headius commented Apr 9, 2019

Appears to be coming from this code:

if (self instanceof RubySymbol ||
self instanceof RubyFixnum ||
self instanceof RubyFloat ||
self instanceof RubyNil ||
self instanceof RubyBoolean.True ||
self instanceof RubyBoolean.False) {
test = SmartBinder
.from(signature.asFold(boolean.class))
.permute("self")
.insert(1, "selfJavaType", self.getClass())
.cast(boolean.class, Object.class, Class.class)
.invoke(TEST_CLASS);

However that logic should be confirming that the target type is actually a RubyFloat or RubyInteger before proceeding with any casting or calling. Strange.

@headius

This comment has been minimized.

Copy link
Member

headius commented Apr 9, 2019

I printed out the signatures of this TEST_CLASS chain of handles, and it appears to be consistently Object. I don't see any reason why it would be attempting to cast anything, unless the typecheck in the TEST_CLASS handle is getting inlined and specialized for the given type incorrectly.

@headius

This comment has been minimized.

Copy link
Member

headius commented Apr 9, 2019

I cannot see how this is our error, so I have filed oracle/graal#1075.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.