summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Almaz Mingaleev <mingaleev@google.com> 2024-12-02 11:16:20 +0000
committer Almaz Mingaleev <mingaleev@google.com> 2024-12-02 16:31:24 +0000
commitb69aa62e67c88eaa35d0807333d06c418380c265 (patch)
tree45cce94b1e308f57afa4d45baec64b8f1b24c8e2
parent5b9716f84db8bc3612f1344daccfb412a9c1422d (diff)
Make sure that MH/VH do not operate on uninitialized classes.
Bug: 379259800 Test: ./art/test/testrunner/testrunner.py -b --jvm Test: ./art/test/testrunner/testrunner.py -b --host --64 Change-Id: Ie0c4a60ed1ff3b25c35dc324ae11d24b8fdcec92
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/expected-stderr.txt0
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/expected-stdout.txt0
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/info.txt2
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticGetter.java20
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticMethod.java24
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticSetter.java20
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/src/Main.java89
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/src/VHStaticGet.java20
-rw-r--r--test/2285-var-and-methodhandles-staticfield-initialization/src/VHStaticPut.java20
9 files changed, 195 insertions, 0 deletions
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/expected-stderr.txt b/test/2285-var-and-methodhandles-staticfield-initialization/expected-stderr.txt
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/expected-stderr.txt
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/expected-stdout.txt b/test/2285-var-and-methodhandles-staticfield-initialization/expected-stdout.txt
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/expected-stdout.txt
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/info.txt b/test/2285-var-and-methodhandles-staticfield-initialization/info.txt
new file mode 100644
index 0000000000..df9ecac8da
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/info.txt
@@ -0,0 +1,2 @@
+Invocation of a VarHandle or a MethodHandle targeting a static field should trigger initialization
+of its declaring class.
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticGetter.java b/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticGetter.java
new file mode 100644
index 0000000000..8aec79b4e6
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticGetter.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class MHStaticGetter {
+ public static int a = 10;
+ public static int b = a + 2;
+}
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticMethod.java b/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticMethod.java
new file mode 100644
index 0000000000..c4a2054e06
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticMethod.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class MHStaticMethod {
+ public static int a = 10;
+ public static int b = a + 2;
+
+ public static int getB() {
+ return b;
+ }
+}
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticSetter.java b/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticSetter.java
new file mode 100644
index 0000000000..d5594f4ae1
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/src/MHStaticSetter.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class MHStaticSetter {
+ public static int a = 10;
+ public static int b = a + 2;
+}
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/src/Main.java b/test/2285-var-and-methodhandles-staticfield-initialization/src/Main.java
new file mode 100644
index 0000000000..43257658cf
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/src/Main.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import static java.lang.invoke.MethodType.methodType;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
+
+public class Main {
+
+ public static void main(String[] args) throws Throwable {
+ testMethodHandleStaticFieldGet();
+ testMethodHandleStaticFieldPut();
+ testMethodHandleStaticMethod();
+ testVarHandleStaticFieldGet();
+ testVarHandleStaticFieldPut();
+ }
+
+ private static void testMethodHandleStaticFieldGet() throws Throwable {
+ MethodHandle mh = MethodHandles.lookup()
+ .findStaticGetter(MHStaticGetter.class, "a", int.class);
+
+ int actual = (int) mh.invokeExact();
+
+ assertEquals(actual, 10);
+ assertEquals(MHStaticGetter.b, 12);
+ }
+
+
+ private static void testMethodHandleStaticFieldPut() throws Throwable {
+ MethodHandle mh = MethodHandles.lookup()
+ .findStaticSetter(MHStaticSetter.class, "a", int.class);
+
+ mh.invokeExact(100);
+
+ assertEquals(MHStaticSetter.a, 100);
+ assertEquals(MHStaticGetter.b, 12);
+ }
+
+ private static void testMethodHandleStaticMethod() throws Throwable {
+ MethodHandle mh = MethodHandles.lookup()
+ .findStatic(MHStaticMethod.class, "getB", methodType(int.class));
+
+ int actualB = (int) mh.invokeExact();
+
+ assertEquals(actualB, 12);
+ assertEquals(MHStaticMethod.a, 10);
+ }
+
+ private static void testVarHandleStaticFieldGet() throws Throwable {
+ VarHandle vh = MethodHandles.lookup()
+ .findStaticVarHandle(VHStaticGet.class, "a", int.class);
+
+ int actual = (int) vh.get();
+
+ assertEquals(actual, 10);
+ assertEquals(VHStaticGet.b, 12);
+ }
+
+ private static void testVarHandleStaticFieldPut() throws Throwable {
+ VarHandle vh = MethodHandles.lookup()
+ .findStaticVarHandle(VHStaticGet.class, "a", int.class);
+
+ vh.set(100);
+
+ assertEquals(VHStaticGet.a, 100);
+ assertEquals(VHStaticGet.b, 12);
+ }
+
+ private static void assertEquals(int actual, int expected) {
+ if (actual != expected) {
+ throw new AssertionError("Expected: " + expected + ", but got: " + actual);
+ }
+ }
+}
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/src/VHStaticGet.java b/test/2285-var-and-methodhandles-staticfield-initialization/src/VHStaticGet.java
new file mode 100644
index 0000000000..62ed256296
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/src/VHStaticGet.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class VHStaticGet {
+ public static int a = 10;
+ public static int b = a + 2;
+}
diff --git a/test/2285-var-and-methodhandles-staticfield-initialization/src/VHStaticPut.java b/test/2285-var-and-methodhandles-staticfield-initialization/src/VHStaticPut.java
new file mode 100644
index 0000000000..4ba9e5ca75
--- /dev/null
+++ b/test/2285-var-and-methodhandles-staticfield-initialization/src/VHStaticPut.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class VHStaticPut {
+ public static int a = 10;
+ public static int b = a + 2;
+}