You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OpenStruct aliases critical builtin methods to "!" versions #30
JRuby warns whenever a core method that accesses the caller's frame gets aliased, since we use the name of the method to detect that a frame should be pushed.
However current OpenStruct versions are aliasing several critical builtin methods that it should not, resulting in warnings on JRuby:
$ jruby -we 'require "ostruct"'
/Users/headius/projects/jruby/lib/ruby/stdlib/ostruct.rb:446: warning: OpenStruct#define_singleton_method accesses caller method's state and should not be aliased
/Users/headius/projects/jruby/lib/ruby/stdlib/ostruct.rb:446: warning: OpenStruct#!~ accesses caller method's state and should not be aliased
/Users/headius/projects/jruby/lib/ruby/stdlib/ostruct.rb:446: warning: OpenStruct#=~ accesses caller method's state and should not be aliased
/Users/headius/projects/jruby/lib/ruby/stdlib/ostruct.rb:446: warning: OpenStruct#instance_exec accesses caller method's state and should not be aliased
/Users/headius/projects/jruby/lib/ruby/stdlib/ostruct.rb:446: warning: OpenStruct#instance_eval accesses caller method's state and should not be aliased
We warn because aliasing very frequently involves wrapping the original method with a new pure-Ruby method, breaking the framing requirements of the aliased original.
define_singleton_method accesses the caller frame for visibility.
!~ and =~ access the caller frame for the backref frame-local $~.
instance_exec and instance_eval capture the caller frame for use as a parent frame for the eval.
I believe OpenStruct should not be aliasing these methods.
@marcandre Perhaps we could modify this to also say "don't use these methods in OpenStruct" and leave these frame-aware methods unaliased? I can't think of a good justification for allowing them, given their importance and access to the caller frame.
We warn when aliasing such methods for two reasons:
They will not be detected as needing a frame, because we do that detection by name.
Aliasing of methods is usually followed by wrapping the original method. Warning about the aliasing tells people that such wrapping will not work, due to the injection of a separate call frame.
In this case, the methods are only being aliased, so only the first point is a problem.
We use the method names for this detection because we know them all at boot time, due to their being core methods defined before any user code gets loaded. Aliasing adds new method names that need a caller frame at runtime which may happen after we have already parsed and compiled other code.
This is a limitation in JRuby, to be sure, but I think it is a valid argument that certain native methods with "special" access to the caller's frame (nearly impossible to do from pure Ruby code) should be considered untouchable. This is such a case.
FWIW if you really want to treat OpenStruct as a blank slate, it should extend the actual blank slate class we already have: BasicObject. Were that the case, this change would not have broken anything, and very few aliases would have to be defined.
define_singleton_method must be called directly. I presume that this can't be done using send : define_singleton_method either, right?
Hmm, I may have made an assumption about define_singleton_method which does not hold... from my testing it does not appear to honor the caller frame's visibility:
And so the send form works the same, since it seems to always be public rather than honoring the caller's frame's visiblity.
If we can get confirmation that define_singleton_method always sets the resulting method public, then JRuby can remove this frame requirement and the warning would go away (existing versions will still warn, but future versions would not).
Not alias !~ and =~ (I have no problem with that :-) )
Yes, let do this!
instance_exec and instance_eval, not alias them, or would silencing the warnings work?
These two are the worst of the lot, because they capture the surrounding state like any block or eval. The only workaround in JRuby would be to add special markers for instance_exec! and instance_eval! and modify our alias checking to allow aliasing from a method name that needs caller frame to another method name that also needs caller frame. It's a lot of hackery for a very specific, stdlib-borne case and we'd rather not do it.
It would be best if we just did not alias these methods at all.
modify our alias checking to allow aliasing from a method name that needs caller frame to another method name that also needs caller frame
This change is a reasonable one to make, for e.g. aliasing from instance_eval to eval (though I don't know why you'd do that) since there's no damage done if the target name has the same requirements as the source name. I have pushed jruby/jruby#6842 to make that change:
$ jruby -we 'class Foo; alias :bar :instance_eval; end; p :done'
-e:2: warning: Foo#instance_eval accesses caller method's state and should not be aliased
:done
$ jruby -we 'class Foo; alias :eval :instance_eval; end; p :done'
:done
As noted in the commit, the enhancement does not fix anything related to this issue, but if we went the direction of pre-marking the instance_exec! and instance_eval! methods, it would be a necessary prerequisite.
FWIW, the new logic also adds the frame requirements for the target name to our global table, but any already-compiled methods would not be updated (only new methods that call the target name would get a frame).
Activity
headius commentedon Sep 15, 2021
Other notes:
!~and=~methods don't make sense as!aliases because they can't be called without send.headius commentedon Sep 15, 2021
This appears to have come in with #17.
@marcandre Perhaps we could modify this to also say "don't use these methods in OpenStruct" and leave these frame-aware methods unaliased? I can't think of a good justification for allowing them, given their importance and access to the caller frame.
Revert to ostruct 0.1.0 to avoid alias warnings
marcandre commentedon Sep 15, 2021
What happens on JRuby when we call these aliases, do they still work correctly?
headius commentedon Sep 15, 2021
@marcandre No, they will not work correctly. We detect the need for a frame based on the method name, and these
!names are not included in that.headius commentedon Sep 15, 2021
To clarify a point I made above...
We warn when aliasing such methods for two reasons:
In this case, the methods are only being aliased, so only the first point is a problem.
We use the method names for this detection because we know them all at boot time, due to their being core methods defined before any user code gets loaded. Aliasing adds new method names that need a caller frame at runtime which may happen after we have already parsed and compiled other code.
This is a limitation in JRuby, to be sure, but I think it is a valid argument that certain native methods with "special" access to the caller's frame (nearly impossible to do from pure Ruby code) should be considered untouchable. This is such a case.
Tag newer specs for ostruct
headius commentedon Sep 15, 2021
FWIW if you really want to treat OpenStruct as a blank slate, it should extend the actual blank slate class we already have: BasicObject. Were that the case, this change would not have broken anything, and very few aliases would have to be defined.
headius commentedon Sep 15, 2021
There's another issue with ostruct in Ruby 2.6.8: the gemspec claims it is 0.1.0 but the code does not match that gem!
#31
marcandre commentedon Sep 16, 2021
To summarize, the following would bring back compatibility with JRuby:
define_singleton_methodmust be called directly. I presume that this can't be done usingsend : define_singleton_methodeither, right?Not alias
!~and=~(I have no problem with that :-) )instance_execandinstance_eval, not alias them, or would silencing the warnings work?headius commentedon Sep 16, 2021
Hmm, I may have made an assumption about
define_singleton_methodwhich does not hold... from my testing it does not appear to honor the caller frame's visibility:And so the
sendform works the same, since it seems to always bepublicrather than honoring the caller's frame's visiblity.If we can get confirmation that
define_singleton_methodalways sets the resulting method public, then JRuby can remove this frame requirement and the warning would go away (existing versions will still warn, but future versions would not).Yes, let do this!
These two are the worst of the lot, because they capture the surrounding state like any block or eval. The only workaround in JRuby would be to add special markers for
instance_exec!andinstance_eval!and modify our alias checking to allow aliasing from a method name that needs caller frame to another method name that also needs caller frame. It's a lot of hackery for a very specific, stdlib-borne case and we'd rather not do it.It would be best if we just did not alias these methods at all.
Avoid alias warning if target has same reqs
headius commentedon Sep 16, 2021
This change is a reasonable one to make, for e.g. aliasing from
instance_evaltoeval(though I don't know why you'd do that) since there's no damage done if the target name has the same requirements as the source name. I have pushed jruby/jruby#6842 to make that change:As noted in the commit, the enhancement does not fix anything related to this issue, but if we went the direction of pre-marking the
instance_exec!andinstance_eval!methods, it would be a necessary prerequisite.FWIW, the new logic also adds the frame requirements for the target name to our global table, but any already-compiled methods would not be updated (only new methods that call the target name would get a frame).
headius commentedon Oct 6, 2021
Anything else I can do to help move this forward?
headius commentedon Dec 2, 2021
Ping again... still have some JRuby issues outstanding because of these warnings.
marcandre commentedon Dec 2, 2021
Thanks for the ping.
Is this correct?
define_singleton_method=> not a problem on newer JRuby, no change toostructostructshould not alias!~and=~ostructshould not aliasinstance_execandinstance_eval(andeval?) if running on Rubyheadius commentedon Dec 2, 2021
Yes that would be the best (read: cleanest for us to support) change to make (and yes including
eval?).Thank you for your compassion!
Alias less methods [Fix #30]
marcandre commentedon Dec 7, 2021
@headius Version 0.5.1 should fix this. Thank you for your patience.