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

Accessing constants on Java proxy object may lead to NullPointerException #5783

Open
jonasoberschweiber opened this issue Jul 6, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@jonasoberschweiber
Copy link

commented Jul 6, 2019

Environment

JRuby version is jruby 9.2.7.0 (2.5.3) 2019-04-09 8a269e3 Java HotSpot(TM) 64-Bit Server VM 25.121-b13 on 1.8.0_121-b13 +jit [darwin-x86_64]
Operating system is Darwin jobembp.local 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64

Expected Behavior

All objects reachable via ObjectSpace can be safely inspected using constants.

Actual Behavior

Some Java objects reachable via ObjectSpace that extend RubyObject return a list of constants on calling constants which includes NEVER. Calling any method on NEVER leads to a java.lang.NullPointerException. I've hit this behaviour while trying to make Sorbet work on JRuby. Specifically, I've had this problem with CParseParam from racc. I've created a repro case with some more detailed explanation of the behaviour I'm seeing here.

@kares

This comment has been minimized.

Copy link
Member

commented Jul 7, 2019

this is a known issue as UNDEF and NEVER are part of the Java API.
we're trying hard not to reflect on JRuby's Java classes but certain features still do so.

in your case the problem might be how 'ext' is loaded:https://github.com/jonasoberschweiber/jruby-object-space-repro/blob/master/repro.rb#L2
... JRuby know has a more blessed way:JRuby.load_ext('java_class_name') - so such classes need NOT to be used from within .rb, including https://github.com/jonasoberschweiber/jruby-object-space-repro/blob/master/repro.rb#L11 (constants should be setup from the native ext part instead)

@kares

This comment has been minimized.

Copy link
Member

commented Jul 7, 2019

this should do (haven't tested but if CI is happy its good to go): ruby/racc#115

@jonasoberschweiber

This comment has been minimized.

Copy link
Author

commented Jul 7, 2019

Thanks for your reply!

this is a known issue as UNDEF and NEVER are part of the Java API.
we're trying hard not to reflect on JRuby's Java classes but certain features still do so.

Is there any way to safely work around this issue without skipping everything that lives under the Java module? Some way to reliably tell when enumerating all objects in ObjectSpace which object's constants can/cannot be safely inspected? Would checking for RubyBasicObject in ancestors work? Or is there some another, better way?

@kares

This comment has been minimized.

Copy link
Member

commented Jul 8, 2019

likely allops will fail, but its pbly only two problematic constants: UNDEF and NEVER
so you would need to check against their (qulified) names directly ... sorry for the inconvenience.

most ext loading should be safe but we might found a library that isn't updated such as you did.
still, racc next release should work nicely (or try gem 'racc', git: ...) ruby/racc#115

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.