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

Duped modules' class-level methods produce NPE when inspected #5245

Closed
headius opened this Issue Jul 12, 2018 · 2 comments

Comments

Projects
None yet
1 participant
@headius
Copy link
Member

headius commented Jul 12, 2018

This affects JRuby 9.2 and probably most versions before that as well.

module Foo
  module_function def blah
  end
end
p Foo.dup.method(:blah)

Produces the following NPE:

Unhandled Java exception: java.lang.NullPointerException
java.lang.NullPointerException: null
               typeOk at org/jruby/runtime/callsite/CacheEntry.java:18
                 call at org/jruby/runtime/callsite/CachingCallSite.java:136
            rbInspect at org/jruby/RubyBasicObject.java:1192
              inspect at org/jruby/RubyObject.java:431
              inspect at org/jruby/RubyMethod.java:283
                 call at org/jruby/RubyMethod$INVOKER$i$0$0$inspect.gen:-1
         cacheAndCall at org/jruby/runtime/callsite/CachingCallSite.java:323
                 call at org/jruby/runtime/callsite/CachingCallSite.java:139
            rbInspect at org/jruby/RubyBasicObject.java:1192
                    p at org/jruby/RubyKernel.java:498
                 call at org/jruby/RubyKernel$INVOKER$s$0$0$p.gen:-1
                 call at org/jruby/internal/runtime/methods/JavaMethod.java:797
                 call at org/jruby/internal/runtime/methods/DynamicMethod.java:202
         cacheAndCall at org/jruby/runtime/callsite/CachingCallSite.java:344
                 call at org/jruby/runtime/callsite/CachingCallSite.java:170
       invokeOther7:p at -e:1
               <main> at -e:1
  invokeWithArguments at java/lang/invoke/MethodHandle.java:627
                 load at org/jruby/ir/Compiler.java:94
            runScript at org/jruby/Ruby.java:852
          runNormally at org/jruby/Ruby.java:771
          runNormally at org/jruby/Ruby.java:789
          runFromMain at org/jruby/Ruby.java:601
        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

The NPE is due to the copied method pointing at the singleton class which has an attached object that has no metaclass. I'm not sure why this happens, nor why non-dup'ed modules are unaffected.

Ran into it because all our Java interface proxies are duped copies of JavaInterfaceTemplate, producing methods that can't be inspected.

@headius headius added this to the JRuby 9.2.1.0 milestone Jul 12, 2018

@headius

This comment has been minimized.

Copy link
Member Author

headius commented Jul 12, 2018

Note that using clone works properly because it is designed to duplicate the singleton class as well.

I believe the source of the problem lies somewhere in this call from RubyModule's initialize_copy method:

        RubyModule originalModule = (RubyModule)original;

>>>     if (!getMetaClass().isSingleton()) setMetaClass(originalModule.getSingletonClassClone());
        setSuperClass(originalModule.getSuperClass());

The implementation of getSingletonClassClone uses UNDEF, which has no metaclass:

    protected RubyClass getSingletonClassClone() {
        return getSingletonClassCloneAndAttach(UNDEF);
    }
@headius

This comment has been minimized.

Copy link
Member Author

headius commented Jul 12, 2018

I believe this was fixed in MRI in https://bugs.ruby-lang.org/issues/3461.

I will incorporate the same fix and add some specs.

@headius headius closed this in f6447e9 Jul 12, 2018

headius added a commit that referenced this issue Jul 12, 2018

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.