[ 
https://issues.apache.org/jira/browse/GROOVY-12361?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-12361:
-------------------------------
    Description: 
The generated {{DirectInvoker}} trampoline from GROOVY-12325 names the target 
method's declaring class in its bytecode ({{CHECKCAST}} on the receiver, 
{{INVOKEVIRTUAL}}/{{INVOKEINTERFACE}} on the declaring type, or the 
{{invokeExact}} descriptor on the classData path). When that declaring class is 
a *hidden class* (JEP 371), the name is not resolvable by any class loader: the 
trampoline defines and initialises fine, but the first call through it fails 
with {{NoClassDefFoundError}}. The error escapes {{CachedMethod.invoke}} as-is 
because generation is sticky-successful and the failure only happens at 
invocation time.

Groovy itself produces hidden declaring classes: {{ProxyGeneratorAdapter}} 
defines its proxies as hidden nestmates (GROOVY-12223) whenever the superclass 
is on the same loader as Groovy, so any map-as-abstract-class coercion of an 
application-classpath type is affected once the method has been invoked 
{{groovy.cachedmethod.invoker.threshold}} times (default 1000).

h3. Reproducer

Precompile so that {{Shape}} sits next to the Groovy jar on the application 
class path (a script compiled by {{GroovyClassLoader}} gets an ordinary, 
non-hidden proxy and does not reproduce):

{code:groovy}
abstract class Shape { abstract String name() }
class ProxyRepro {
    static void main(String[] args) {
        def s = [name: { 'circle' }] as Shape
        println "hidden proxy: " + s.getClass().isHidden()
        // two cold call sites share the CachedMethod; its hit count reaches the
        // trampoline threshold while each indy site is still below its own
        600.times { assert s.name() == 'circle' }
        600.times { assert s.name() == 'circle' }
        println "ok"
    }
}
{code}

{noformat}
$ java -cp groovy-6.0.0-SNAPSHOT.jar 
org.codehaus.groovy.tools.FileSystemCompiler -d out ProxyRepro.groovy
$ java -cp groovy-6.0.0-SNAPSHOT.jar:out ProxyRepro
hidden proxy: true
Exception in thread "main" java.lang.NoClassDefFoundError: 
Shape1_groovyProxy/0x0000007001120000
        at 
org.codehaus.groovy.reflection.CachedMethod.invokeGenerated(CachedMethod.java:491)
        at 
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:452)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:298)
        at 
org.codehaus.groovy.vmplugin.v8.IndyInterface.invokeColdReflective(IndyInterface.java:650)
        ...
{noformat}

A single call site masks the bug because the indy site promotes to the full 
method-handle chain at the same hit count (1000) and stops going through 
{{CachedMethod.invoke}}; two sites sharing the method, any MOP-path invocation 
({{invokeMethod}}, categories, per-instance metaclass, {{@Delegate}} etc.), or 
{{-Dgroovy.cachedmethod.invoker.threshold=0}} exposes it. Setting either 
{{-Dgroovy.indy.cold.reflection=false}} or 
{{-Dgroovy.cachedmethod.invoker.disable=true}} avoids it. JDK 21, Groovy master 
(6.0.0-SNAPSHOT).

h3. Fix

{{InvokerFactory.tryCreate}} should decline (sticky null, so 
{{CachedMethod.invoke}} stays reflective) when the declaring class, the return 
type or any parameter type (array components included) {{isHidden()}}. All four 
define steps are affected, including Step 3 (classData), whose {{invokeExact}} 
descriptor also names the type. 
{{InvokerFactoryTest.testDefineStepsFallsThroughToClassDataForHiddenNonPublicHost}}
 currently asserts the opposite and even notes that the resulting trampoline 
must not be invoked; it needs to assert {{null}} instead. A fix with tests is 
available on branch {{groovy12354spike}} (the {{allTypesNameable}} gate).


> CachedMethod DirectInvoker: trampoline for a hidden declaring class throws 
> NoClassDefFoundError on first invoke
> ---------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12361
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12361
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>
> The generated {{DirectInvoker}} trampoline from GROOVY-12325 names the target 
> method's declaring class in its bytecode ({{CHECKCAST}} on the receiver, 
> {{INVOKEVIRTUAL}}/{{INVOKEINTERFACE}} on the declaring type, or the 
> {{invokeExact}} descriptor on the classData path). When that declaring class 
> is a *hidden class* (JEP 371), the name is not resolvable by any class 
> loader: the trampoline defines and initialises fine, but the first call 
> through it fails with {{NoClassDefFoundError}}. The error escapes 
> {{CachedMethod.invoke}} as-is because generation is sticky-successful and the 
> failure only happens at invocation time.
> Groovy itself produces hidden declaring classes: {{ProxyGeneratorAdapter}} 
> defines its proxies as hidden nestmates (GROOVY-12223) whenever the 
> superclass is on the same loader as Groovy, so any map-as-abstract-class 
> coercion of an application-classpath type is affected once the method has 
> been invoked {{groovy.cachedmethod.invoker.threshold}} times (default 1000).
> h3. Reproducer
> Precompile so that {{Shape}} sits next to the Groovy jar on the application 
> class path (a script compiled by {{GroovyClassLoader}} gets an ordinary, 
> non-hidden proxy and does not reproduce):
> {code:groovy}
> abstract class Shape { abstract String name() }
> class ProxyRepro {
>     static void main(String[] args) {
>         def s = [name: { 'circle' }] as Shape
>         println "hidden proxy: " + s.getClass().isHidden()
>         // two cold call sites share the CachedMethod; its hit count reaches 
> the
>         // trampoline threshold while each indy site is still below its own
>         600.times { assert s.name() == 'circle' }
>         600.times { assert s.name() == 'circle' }
>         println "ok"
>     }
> }
> {code}
> {noformat}
> $ java -cp groovy-6.0.0-SNAPSHOT.jar 
> org.codehaus.groovy.tools.FileSystemCompiler -d out ProxyRepro.groovy
> $ java -cp groovy-6.0.0-SNAPSHOT.jar:out ProxyRepro
> hidden proxy: true
> Exception in thread "main" java.lang.NoClassDefFoundError: 
> Shape1_groovyProxy/0x0000007001120000
>       at 
> org.codehaus.groovy.reflection.CachedMethod.invokeGenerated(CachedMethod.java:491)
>       at 
> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:452)
>       at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:298)
>       at 
> org.codehaus.groovy.vmplugin.v8.IndyInterface.invokeColdReflective(IndyInterface.java:650)
>       ...
> {noformat}
> A single call site masks the bug because the indy site promotes to the full 
> method-handle chain at the same hit count (1000) and stops going through 
> {{CachedMethod.invoke}}; two sites sharing the method, any MOP-path 
> invocation ({{invokeMethod}}, categories, per-instance metaclass, 
> {{@Delegate}} etc.), or {{-Dgroovy.cachedmethod.invoker.threshold=0}} exposes 
> it. Setting either {{-Dgroovy.indy.cold.reflection=false}} or 
> {{-Dgroovy.cachedmethod.invoker.disable=true}} avoids it. JDK 21, Groovy 
> master (6.0.0-SNAPSHOT).
> h3. Fix
> {{InvokerFactory.tryCreate}} should decline (sticky null, so 
> {{CachedMethod.invoke}} stays reflective) when the declaring class, the 
> return type or any parameter type (array components included) {{isHidden()}}. 
> All four define steps are affected, including Step 3 (classData), whose 
> {{invokeExact}} descriptor also names the type. 
> {{InvokerFactoryTest.testDefineStepsFallsThroughToClassDataForHiddenNonPublicHost}}
>  currently asserts the opposite and even notes that the resulting trampoline 
> must not be invoked; it needs to assert {{null}} instead. A fix with tests is 
> available on branch {{groovy12354spike}} (the {{allTypesNameable}} gate).



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to