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

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

Copilot commented on code in PR #2882:
URL: https://github.com/apache/groovy/pull/2882#discussion_r3942850502


##########
src/test/groovy/org/apache/groovy/internal/runtime/invoke/InvokerFactoryTest.groovy:
##########
@@ -474,9 +474,54 @@ final class InvokerFactoryTest {
     }
 
     // 
-------------------------------------------------------------------------
-    // Forced Step 3 through defineSteps (hidden non-public nestmate)
+    // Hidden hosts (GROOVY-12361): no step may bind a hidden declaring class
     // 
-------------------------------------------------------------------------
 
+    @Test
+    void testTryCreateDeclinesPublicHiddenHost() {
+        // The ProxyGeneratorAdapter shape: a public hidden nestmate whose 
public
+        // method is a Step 1 candidate. Its trampoline would CHECKCAST the
+        // receiver to the hidden class name, which no loader can resolve, so 
it
+        // defines fine but throws NoClassDefFoundError on first invoke.
+        byte[] bytes = emitStringPingClass(
+                'org/apache/groovy/internal/runtime/invoke/PublicHiddenHost',
+                'ping', 'hidden-pong', true)
+        Class<?> hiddenHost = 
HiddenClassDefiner.tryDefineNestmate(InvokerFactory.LOOKUP, bytes, true)
+        assertNotNull(hiddenHost)
+        assertTrue((Boolean) Class.getMethod('isHidden').invoke(hiddenHost))
+        Method ping = javaGetMethod(hiddenHost, 'ping')
+        
assertTrue(InvokerFactory.isPubliclyInvocableFromInvokerFactory(cm(ping)))
+        assertFalse(InvokerFactory.allTypesNameable(ping))
+
+        assertNull(InvokerFactory.tryCreate(cm(ping)), 'hidden declaring class 
must not get a trampoline')
+        // the reflective path keeps working
+        Object host = ((Class) hiddenHost).getConstructor().newInstance()
+        assertEquals('hidden-pong', cm(ping).invoke(host, new Object[0]))
+    }
+
+    @Test
+    void testHiddenProxyStaysInvocablePastThreshold() {

Review Comment:
   This test changes a JVM-global system property without taking the 
system-properties resource lock. Other tests in this class that mutate 
`PROPERTY_THRESHOLD` or related switches use 
`@ResourceLock(Resources.SYSTEM_PROPERTIES)` (for example, lines 608–615); 
without it, parallel JUnit execution can observe the temporary zero threshold 
or overwrite the value being restored, making the suite flaky.





> 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