[ 
https://issues.apache.org/jira/browse/GROOVY-12361?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18111998#comment-18111998
 ] 

ASF GitHub Bot commented on GROOVY-12361:
-----------------------------------------

paulk-asert commented on PR #2882:
URL: https://github.com/apache/groovy/pull/2882#issuecomment-5557104316

   Thanks @daniellansun for the careful notes — all three suggestions taken in 
the follow-up commit (1a575020a4), plus Copilot's lock point which was the same 
as your first bullet under (2).
   
   **Production**
   - The hidden-type check now sits above the `try` beside the caller-sensitive 
/ abstract declines, so it reads as a policy decline rather than a define 
failure.
   - `allTypesNameable` takes the `CachedMethod` and reads the declaring class, 
return type and native parameter types the same way 
`isPubliclyInvocableFromInvokerFactory` does, so it no longer forces 
accessibility on the underlying `Method`. (Either form yields the same classes 
as `InvokerBytecode` writes; this one just avoids the side effect.)
   - The class javadoc gains a paragraph after the four define-path steps 
stating the hidden-type precondition, so a reader of `tryStep1` no longer has 
to go via GROOVY-12361.
   
   **Tests**
   - `testHiddenProxyStaysInvocablePastThreshold` now takes 
`@ResourceLock(Resources.SYSTEM_PROPERTIES)`, uses `restoreProperty`, drives a 
fresh `CachedMethod` past the threshold and asserts `invokerAttempted == true` 
/ `invoker == null` before re-invoking, mirroring 
`testCachedMethodStickyFailsWhenAllDefineStepsFail`. For the record I also 
confirmed it is not vacuous: with the gate removed it fails with the original 
`NoClassDefFoundError` on the `_groovyProxy` hidden class, and the two 
factory-level tests fail on the null assertion.
   - The old Step 3 fall-through test is renamed 
`testTryCreateDeclinesNonPublicHiddenHost`, keeps `assertNull(tryCreate(...))`, 
adds an `allTypesNameable` check, and drops the direct `tryCreateClassData` 
definition of an uninvokable trampoline. You were right that the "remaining 
classData tests rely on it" comment was inaccurate — they all use ordinary 
subjects.
   
   One small note on the lock: JUnit parallel execution is not enabled in this 
build, so the race was theoretical, but consistency with the rest of the file 
is reason enough.
   
   Not chased, as you suggested: a shared nameable-type helper with 
`CallSiteGenerator`, and extra coverage for hidden return/parameter types 
without a hidden declaring class (the three uncovered lines Codecov flags). The 
TestLens failure is the known-flaky `ChannelSelectTest` timer test, unrelated 
to this change.
   
   `InvokerFactoryTest` (62) and `CachedMethodDirectInvokerTest` (33) are green 
locally.
   




> 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