8281003: MethodHandles::lookup throws NPE if caller is null
Reviewed-by: ihse, mchung, jrose, alanb
This commit is contained in:
parent
847a99b53d
commit
67763df4dc
@ -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)
|
||||
|
@ -109,14 +109,25 @@ public class MethodHandles {
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<String, String> env = pb.environment();
|
||||
|
||||
String libDir = Platform.libDir().toString();
|
||||
String vmDir = Platform.jvmLibDir().toString();
|
||||
|
||||
// set up shared library path
|
||||
String sharedLibraryPathEnvName = Platform.sharedLibraryPathVariableName();
|
||||
env.compute(sharedLibraryPathEnvName,
|
||||
(k, v) -> (v == null) ? libDir : v + File.pathSeparator + libDir);
|
||||
env.compute(sharedLibraryPathEnvName,
|
||||
(k, v) -> (v == null) ? vmDir : v + File.pathSeparator + vmDir);
|
||||
|
||||
System.out.println("Launching: " + launcher + " shared library path: " +
|
||||
env.get(sharedLibraryPathEnvName));
|
||||
new OutputAnalyzer(pb.start())
|
||||
.outputTo(System.out)
|
||||
.errorTo(System.err)
|
||||
.shouldHaveExitValue(0);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "jni.h"
|
||||
#include "assert.h"
|
||||
|
||||
static jclass class_IllegalCallerException;
|
||||
|
||||
int checkAndClearIllegalCallerExceptionThrown(JNIEnv *env) {
|
||||
jthrowable t = (*env)->ExceptionOccurred(env);
|
||||
if ((*env)->IsInstanceOf(env, t, class_IllegalCallerException) == JNI_TRUE) {
|
||||
(*env)->ExceptionClear(env);
|
||||
return JNI_TRUE;
|
||||
}
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
int main(int argc, char** args) {
|
||||
JavaVM *jvm;
|
||||
JNIEnv *env;
|
||||
JavaVMInitArgs vm_args;
|
||||
JavaVMOption options[1];
|
||||
jint rc;
|
||||
|
||||
|
||||
vm_args.version = JNI_VERSION_1_2;
|
||||
vm_args.nOptions = 0;
|
||||
vm_args.options = options;
|
||||
|
||||
if ((rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args)) != JNI_OK) {
|
||||
printf("ERROR: cannot create VM.\n");
|
||||
exit(-1);
|
||||
}
|
||||
class_IllegalCallerException = (*env)->FindClass(env, "java/lang/IllegalCallerException");
|
||||
assert (class_IllegalCallerException != NULL);
|
||||
|
||||
// call MethodHandles.lookup()
|
||||
jclass methodHandlesClass = (*env)->FindClass(env, "java/lang/invoke/MethodHandles");
|
||||
assert(methodHandlesClass != NULL);
|
||||
jmethodID mid_MethodHandles_lookup = (*env)->GetStaticMethodID(env, methodHandlesClass, "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;" );
|
||||
assert(mid_MethodHandles_lookup != NULL);
|
||||
jobject l = (*env)->CallStaticObjectMethod(env, methodHandlesClass, mid_MethodHandles_lookup );
|
||||
if ((rc = checkAndClearIllegalCallerExceptionThrown(env)) != JNI_TRUE) {
|
||||
printf("ERROR: Didn't get the expected IllegalCallerException.\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
printf("Expected IllegalCallerException was thrown\n");
|
||||
|
||||
(*jvm)->DestroyJavaVM(jvm);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user