You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/08/28 14:25:03 UTC
[groovy] branch danielsun/fix-clone-array-based-on-groovy10730pr updated (9abed60959 -> c9d29861a3)
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a change to branch danielsun/fix-clone-array-based-on-groovy10730pr
in repository https://gitbox.apache.org/repos/asf/groovy.git
discard 9abed60959 Fix illegal access issues
discard 7e3812d1c3 Fix illegal access issues in `StreamingTemplateEngineTest`
discard 2ac7e17614 Remove the jdk16+ add-opens jvm args
discard 45146486e1 Fix accessing `clone` illegally
new 1739a97f27 Remove the jdk16+ add-opens jvm args
new c9d29861a3 Fix accessing `clone` illegally
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (9abed60959)
\
N -- N -- N refs/heads/danielsun/fix-clone-array-based-on-groovy10730pr (c9d29861a3)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../org/codehaus/groovy/transform/ImmutableTransformTest.groovy | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
[groovy] 02/02: Fix accessing `clone` illegally
Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch danielsun/fix-clone-array-based-on-groovy10730pr
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit c9d29861a3580592fbaaa878800f36df481f86c7
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Aug 28 22:24:38 2022 +0800
Fix accessing `clone` illegally
---
src/main/java/groovy/lang/MetaClassImpl.java | 22 +-----
.../org/codehaus/groovy/runtime/ArrayUtil.java | 30 ++++++++
.../org/codehaus/groovy/runtime/InvokerHelper.java | 4 ++
.../org/codehaus/groovy/vmplugin/v8/Selector.java | 11 +++
src/test/groovy/bugs/Groovy9103.groovy | 9 +++
.../groovy/transform/ImmutableTransformTest.groovy | 8 +--
.../groovy/text/StreamingTemplateEngineTest.groovy | 80 ++++++++++++----------
7 files changed, 100 insertions(+), 64 deletions(-)
diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index c32102989d..14ca8081fb 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -1131,7 +1131,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
if (receiverClass.isArray()) {
if (methodName.equals("clone") && arguments.length == 0) {
try {
- return (Object[]) MethodHandleHolder.CLONE_ARRAY_METHOD_HANDLE.invokeExact((Object[]) object);
+ return (Object[]) ArrayUtil.getCloneArrayMethodHandle().invokeExact((Object[]) object);
} catch (Throwable t) {
throw new GroovyRuntimeException(t);
}
@@ -1164,26 +1164,6 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
}
- private static class MethodHandleHolder {
- private static final MethodHandle CLONE_ARRAY_METHOD_HANDLE;
- static {
- final Class<ArrayUtil> arrayUtilClass = ArrayUtil.class;
- Method cloneArrayMethod;
- try {
- cloneArrayMethod = arrayUtilClass.getDeclaredMethod("cloneArray", Object[].class);
- } catch (NoSuchMethodException e) {
- throw new GroovyBugError("Failed to find `cloneArray` method in class `" + arrayUtilClass.getName() + "`", e);
- }
-
- try {
- CLONE_ARRAY_METHOD_HANDLE = MethodHandles.lookup().in(arrayUtilClass).unreflect(cloneArrayMethod);
- } catch (IllegalAccessException e) {
- throw new GroovyBugError("Failed to create method handle for " + cloneArrayMethod);
- }
- }
- private MethodHandleHolder() {}
- }
-
private static final ClassValue<Map<String, Set<Method>>> SPECIAL_METHODS_MAP = new ClassValue<Map<String, Set<Method>>>() {
@Override
protected Map<String, Set<Method>> computeValue(final Class<?> type) {
diff --git a/src/main/java/org/codehaus/groovy/runtime/ArrayUtil.java b/src/main/java/org/codehaus/groovy/runtime/ArrayUtil.java
index 72bcd83c51..d35a4d80ea 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ArrayUtil.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ArrayUtil.java
@@ -18,6 +18,12 @@
*/
package org.codehaus.groovy.runtime;
+import org.codehaus.groovy.GroovyBugError;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.Method;
+
/**
* This is a generated class used internally during the writing of bytecode within the CallSiteWriter logic.
* This is not a class exposed to users, as is the case with almost all classes in the org.codehaus.groovy packages.
@@ -55,6 +61,30 @@ package org.codehaus.groovy.runtime;
public class ArrayUtil {
private static final Object[] EMPTY = new Object[0];
+ public static MethodHandle getCloneArrayMethodHandle() {
+ return MethodHandleHolder.CLONE_ARRAY_METHOD_HANDLE;
+ }
+
+ private static class MethodHandleHolder {
+ private static final MethodHandle CLONE_ARRAY_METHOD_HANDLE;
+ static {
+ final Class<ArrayUtil> arrayUtilClass = ArrayUtil.class;
+ Method cloneArrayMethod;
+ try {
+ cloneArrayMethod = arrayUtilClass.getDeclaredMethod("cloneArray", Object[].class);
+ } catch (NoSuchMethodException e) {
+ throw new GroovyBugError("Failed to find `cloneArray` method in class `" + arrayUtilClass.getName() + "`", e);
+ }
+
+ try {
+ CLONE_ARRAY_METHOD_HANDLE = MethodHandles.lookup().in(arrayUtilClass).unreflect(cloneArrayMethod);
+ } catch (IllegalAccessException e) {
+ throw new GroovyBugError("Failed to create method handle for " + cloneArrayMethod);
+ }
+ }
+ private MethodHandleHolder() {}
+ }
+
public static <T> T[] cloneArray(T[] array) {
return array.clone();
}
diff --git a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
index 66926668b3..e39de8d48c 100644
--- a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -586,6 +586,10 @@ public class InvokerHelper {
// it's an instance; check if it's a Java one
if (!(object instanceof GroovyObject)) {
+ if (null != object && object.getClass().isArray() && "clone".equals(methodName) && (null == arguments || arguments.getClass().isArray() && 0 == ((Object[]) arguments).length)) {
+ return ArrayUtil.cloneArray((Object[]) object);
+ }
+
return invokePojoMethod(object, methodName, arguments);
}
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java b/src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java
index 2352a1c21b..c5d75a4968 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java
@@ -38,6 +38,7 @@ import org.codehaus.groovy.reflection.CachedMethod;
import org.codehaus.groovy.reflection.ClassInfo;
import org.codehaus.groovy.reflection.GeneratedMetaMethod;
import org.codehaus.groovy.reflection.stdclasses.CachedSAMClass;
+import org.codehaus.groovy.runtime.ArrayUtil;
import org.codehaus.groovy.runtime.GeneratedClosure;
import org.codehaus.groovy.runtime.GroovyCategorySupport;
import org.codehaus.groovy.runtime.GroovyCategorySupport.CategoryMethod;
@@ -673,9 +674,19 @@ public abstract class Selector {
}
}
+ private static final Method OBJECT_CLONE_METHOD;
+ static {
+ try {
+ OBJECT_CLONE_METHOD = Object.class.getDeclaredMethod("clone");
+ } catch (NoSuchMethodException e) {
+ throw new GroovyBugError(e); // should never happen
+ }
+ }
private MethodHandle correctClassForNameAndUnReflectOtherwise(Method m) throws IllegalAccessException {
if (m.getDeclaringClass() == Class.class && m.getName().equals("forName") && m.getParameterTypes().length == 1) {
return MethodHandles.insertArguments(CLASS_FOR_NAME, 1, true, sender.getClassLoader());
+ } else if (null != args && args.getClass().isArray() && OBJECT_CLONE_METHOD.equals(m)) {
+ return ArrayUtil.getCloneArrayMethodHandle();
} else {
return LOOKUP.unreflect(m);
}
diff --git a/src/test/groovy/bugs/Groovy9103.groovy b/src/test/groovy/bugs/Groovy9103.groovy
index ab1be1a0c5..721344f0c6 100644
--- a/src/test/groovy/bugs/Groovy9103.groovy
+++ b/src/test/groovy/bugs/Groovy9103.groovy
@@ -69,4 +69,13 @@ final class Groovy9103 {
Object obj = new Tuple1('abc')
assert obj.clone()
}
+
+ @Test
+ void testClone4() {
+ assertScript '''
+ int[] nums = [1, 2, 3]
+ int[] cloned = nums.clone()
+ assert nums == cloned
+ '''
+ }
}
diff --git a/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy b/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy
index f108715511..58f602223e 100644
--- a/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy
@@ -139,8 +139,7 @@ class ImmutableTransformTest extends GroovyShellTestCase {
@Test
void testCloneableFieldNotCloneableObject() {
- def cls = shouldFail(CloneNotSupportedException) {
- def objects = evaluate("""
+ shouldFail(isAtLeastJdk('16') ? IllegalAccessException : CloneNotSupportedException, '''
import groovy.transform.Immutable
class Dolly {
@@ -154,10 +153,7 @@ class ImmutableTransformTest extends GroovyShellTestCase {
def dolly = new Dolly(name: "The Sheep")
[dolly, new Lab(name: "Area 51", clone: dolly)]
- """)
- }
-
- assert cls == 'Dolly'
+ ''')
}
@Test
diff --git a/subprojects/groovy-templates/src/test/groovy/groovy/text/StreamingTemplateEngineTest.groovy b/subprojects/groovy-templates/src/test/groovy/groovy/text/StreamingTemplateEngineTest.groovy
index 78b0ed0b47..94e1273ac4 100644
--- a/subprojects/groovy-templates/src/test/groovy/groovy/text/StreamingTemplateEngineTest.groovy
+++ b/subprojects/groovy-templates/src/test/groovy/groovy/text/StreamingTemplateEngineTest.groovy
@@ -23,6 +23,8 @@ import org.junit.Test
import java.util.concurrent.ConcurrentHashMap
+import static groovy.test.GroovyAssert.assertScript
+
class StreamingTemplateEngineTest {
TemplateEngine engine
Map binding
@@ -463,48 +465,52 @@ class StreamingTemplateEngineTest {
@Test
void reuseClassLoader1() {
- final reuseOption = 'groovy.StreamingTemplateEngine.reuseClassLoader'
- System.setProperty(reuseOption, 'true')
+ assertScript '''
+ final reuseOption = 'groovy.StreamingTemplateEngine.reuseClassLoader'
+ System.setProperty(reuseOption, 'true')
- try {
- // reload class to initialize static field from the beginning
- def steClass = reloadClass('groovy.text.StreamingTemplateEngine')
-
- GroovyClassLoader gcl = new GroovyClassLoader()
- def engine = steClass.newInstance(gcl)
- assert 'Hello, Daniel' == engine.createTemplate('Hello, ${name}').make([name: 'Daniel']).toString()
- assert gcl.loadedClasses.length > 0
- def cloned = gcl.loadedClasses.clone()
- assert 'Hello, Paul' == engine.createTemplate('Hello, ${name}').make([name: 'Paul']).toString()
- assert cloned == gcl.loadedClasses
- } finally {
- System.clearProperty(reuseOption)
- }
+ try {
+ // reload class to initialize static field from the beginning
+ def steClass = groovy.text.StreamingTemplateEngineTest.reloadClass('groovy.text.StreamingTemplateEngine')
+
+ GroovyClassLoader gcl = new GroovyClassLoader()
+ def engine = steClass.newInstance(gcl)
+ assert 'Hello, Daniel' == engine.createTemplate('Hello, ${name}').make([name: 'Daniel']).toString()
+ assert gcl.loadedClasses.length > 0
+ def cloned = gcl.loadedClasses.clone()
+ assert 'Hello, Paul' == engine.createTemplate('Hello, ${name}').make([name: 'Paul']).toString()
+ assert cloned == gcl.loadedClasses
+ } finally {
+ System.clearProperty(reuseOption)
+ }
+ '''
}
@Test
void reuseClassLoader2() {
- final reuseOption = 'groovy.StreamingTemplateEngine.reuseClassLoader'
- System.setProperty(reuseOption, 'true')
-
- try {
- // reload class to initialize static field from the beginning
- def steClass = reloadClass('groovy.text.StreamingTemplateEngine')
-
- GroovyClassLoader gcl = new GroovyClassLoader()
- def engine = steClass.newInstance(gcl)
- assert 'Hello, Daniel' == engine.createTemplate('Hello, ${name}').make([name: 'Daniel']).toString()
- assert gcl.loadedClasses.length > 0
- def cloned = gcl.loadedClasses.clone()
- engine = steClass.newInstance(gcl)
- assert 'Hello, Paul' == engine.createTemplate('Hello, ${name}').make([name: 'Paul']).toString()
- assert cloned == gcl.loadedClasses
- } finally {
- System.clearProperty(reuseOption)
- }
- }
-
- private static Class reloadClass(String className) {
+ assertScript '''
+ final reuseOption = 'groovy.StreamingTemplateEngine.reuseClassLoader'
+ System.setProperty(reuseOption, 'true')
+
+ try {
+ // reload class to initialize static field from the beginning
+ def steClass = groovy.text.StreamingTemplateEngineTest.reloadClass('groovy.text.StreamingTemplateEngine')
+
+ GroovyClassLoader gcl = new GroovyClassLoader()
+ def engine = steClass.newInstance(gcl)
+ assert 'Hello, Daniel' == engine.createTemplate('Hello, ${name}').make([name: 'Daniel']).toString()
+ assert gcl.loadedClasses.length > 0
+ def cloned = gcl.loadedClasses.clone()
+ engine = steClass.newInstance(gcl)
+ assert 'Hello, Paul' == engine.createTemplate('Hello, ${name}').make([name: 'Paul']).toString()
+ assert cloned == gcl.loadedClasses
+ } finally {
+ System.clearProperty(reuseOption)
+ }
+ '''
+ }
+
+ static Class reloadClass(String className) {
def clazz =
new GroovyClassLoader() {
private final Map<String, Class> loadedClasses = new ConcurrentHashMap<String, Class>()
[groovy] 01/02: Remove the jdk16+ add-opens jvm args
Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch danielsun/fix-clone-array-based-on-groovy10730pr
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 1739a97f2765b884cee4f86d984bd7064210a089
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Aug 28 22:24:10 2022 +0800
Remove the jdk16+ add-opens jvm args
---
buildSrc/src/main/groovy/org.apache.groovy-tested.gradle | 6 ------
1 file changed, 6 deletions(-)
diff --git a/buildSrc/src/main/groovy/org.apache.groovy-tested.gradle b/buildSrc/src/main/groovy/org.apache.groovy-tested.gradle
index dfa7479e60..19ac5e7492 100644
--- a/buildSrc/src/main/groovy/org.apache.groovy-tested.gradle
+++ b/buildSrc/src/main/groovy/org.apache.groovy-tested.gradle
@@ -32,18 +32,12 @@ sourceSets {
}
}
-// TODO remove the jdk16+ add-opens jvm args once offending code/tests are fixed
-// https://docs.gradle.org/7.5/userguide/upgrading_version_7.html#removes_implicit_add_opens_for_test_workers
tasks.withType(Test).configureEach {
def fs = objects.newInstance(TestServices).fileSystemOperations
def grapeDirectory = new File(temporaryDir, "grape")
def jdk8 = ['-XX:+UseConcMarkSweepGC']
def jdk9 = ['-Djava.locale.providers=COMPAT,SPI']
// def jdk9 = ['-Djava.locale.providers=COMPAT,SPI', '--illegal-access=debug']
- if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16)) {
- jdk9 += ["--add-opens=java.base/java.lang=ALL-UNNAMED",
- "--add-opens=java.base/java.util=ALL-UNNAMED"]
- }
def common = ['-ea', "-Xms${groovyJUnit_ms}", "-Xmx${groovyJUnit_mx}", "-Duser.language=en"]
if (JavaVersion.current().isJava9Compatible()) {
jvmArgs(*common, *jdk9)