diff --git a/make/test/JtregNativeJdk.gmk b/make/test/JtregNativeJdk.gmk index 62c0fc809b3..974ab77cb7b 100644 --- a/make/test/JtregNativeJdk.gmk +++ b/make/test/JtregNativeJdk.gmk @@ -1,5 +1,5 @@ # -# Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -62,6 +62,7 @@ ifeq ($(call isTargetOs, windows), true) WIN_LIB_JLI := $(SUPPORT_OUTPUTDIR)/native/java.base/libjli/jli.lib BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJliLaunchTest := $(WIN_LIB_JLI) BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeCallerAccessTest := jvm.lib + BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerLookupTest := jvm.lib BUILD_JDK_JTREG_EXECUTABLES_LIBS_exerevokeall := advapi32.lib BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncStackWalk := /EHsc BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncInvokers := /EHsc @@ -80,6 +81,7 @@ else endif BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJliLaunchTest := -ljli BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeCallerAccessTest := -ljvm + BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerLookupTest := -ljvm endif ifeq ($(call isTargetOs, macosx), true) diff --git a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java index b7d5bde0aeb..bdc26750992 100644 --- a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java +++ b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java @@ -109,14 +109,25 @@ public class MethodHandles { *
* This method is caller sensitive, which means that it may return different
* values to different callers.
+ * In cases where {@code MethodHandles.lookup} is called from a context where
+ * there is no caller frame on the stack (e.g. when called directly
+ * from a JNI attached thread), {@code IllegalCallerException} is thrown.
+ * To obtain a {@link Lookup lookup object} in such a context, use an auxiliary class that will
+ * implicitly be identified as the caller, or use {@link MethodHandles#publicLookup()}
+ * to obtain a low-privileged lookup instead.
* @return a lookup object for the caller of this method, with
* {@linkplain Lookup#ORIGINAL original} and
* {@linkplain Lookup#hasFullPrivilegeAccess() full privilege access}.
+ * @throws IllegalCallerException if there is no caller frame on the stack.
*/
@CallerSensitive
@ForceInline // to ensure Reflection.getCallerClass optimization
public static Lookup lookup() {
- return new Lookup(Reflection.getCallerClass());
+ final Class> c = Reflection.getCallerClass();
+ if (c == null) {
+ throw new IllegalCallerException("no caller frame");
+ }
+ return new Lookup(c);
}
/**
diff --git a/test/jdk/java/lang/invoke/MethodHandles/exeNullCallerLookup/NullCallerLookupTest.java b/test/jdk/java/lang/invoke/MethodHandles/exeNullCallerLookup/NullCallerLookupTest.java
new file mode 100644
index 00000000000..cda0b2bc7af
--- /dev/null
+++ b/test/jdk/java/lang/invoke/MethodHandles/exeNullCallerLookup/NullCallerLookupTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+/**
+ * @test
+ * @bug 8281003
+ * @summary Test uses custom launcher that starts VM using JNI that verifies
+ * MethodHandles::lookup with null caller class throws an IllegalCallerException.
+ * @library /test/lib
+ * @requires os.family != "aix"
+ * @run main/native NullCallerLookupTest
+ */
+
+// Test disabled on AIX since we cannot invoke the JVM on the primordial thread.
+
+import java.io.File;
+import java.util.Map;
+import jdk.test.lib.Platform;
+import jdk.test.lib.process.OutputAnalyzer;
+
+import java.io.IOException;
+import java.nio.file.Path;
+
+public class NullCallerLookupTest {
+ public static void main(String[] args) throws IOException {
+ Path launcher = Path.of(System.getProperty("test.nativepath"), "NullCallerLookupTest");
+ ProcessBuilder pb = new ProcessBuilder(launcher.toString());
+ Map