8280902: ResourceBundle::getBundle may throw NPE when invoked by JNI code with no caller frame

Reviewed-by: naoto, mchung, ihse
This commit is contained in:
Tim Prinzing 2022-03-09 04:02:17 +00:00 committed by Mandy Chung
parent 12693a6cf3
commit 31ad80a229
4 changed files with 215 additions and 11 deletions
make/test
src/java.base/share/classes/java/util
test/jdk/java/util/ResourceBundle/exeNullCallerResourceBundle

@ -64,6 +64,7 @@ ifeq ($(call isTargetOs, windows), true)
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeCallerAccessTest := jvm.lib
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerClassLoaderTest := jvm.lib
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerLookupTest := jvm.lib
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerResourceBundle := jvm.lib
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exerevokeall := advapi32.lib
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncStackWalk := /EHsc
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncInvokers := /EHsc
@ -84,6 +85,7 @@ else
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeCallerAccessTest := -ljvm
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerClassLoaderTest := -ljvm
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerLookupTest := -ljvm
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerResourceBundle := -ljvm
endif
ifeq ($(call isTargetOs, macosx), true)

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 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
@ -256,6 +256,12 @@ import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
* resource bundle provider</a>, it does not fall back to the
* class loader search.
*
* <p>
* In cases where the {@code getBundle} factory method is called from a context
* where there is no caller frame on the stack (e.g. when called directly from
* a JNI attached thread), the caller module is default to the unnamed module for the
* {@linkplain ClassLoader#getSystemClassLoader system class loader}.
*
* <h3>Resource bundles in automatic modules</h3>
*
* A common format of resource bundles is in {@linkplain PropertyResourceBundle
@ -1505,7 +1511,8 @@ public abstract class ResourceBundle {
}
private static Control getDefaultControl(Class<?> caller, String baseName) {
return getDefaultControl(caller.getModule(), baseName);
Module callerModule = getCallerModule(caller);
return getDefaultControl(callerModule, baseName);
}
private static Control getDefaultControl(Module targetModule, String baseName) {
@ -1536,7 +1543,8 @@ public abstract class ResourceBundle {
}
private static void checkNamedModule(Class<?> caller) {
if (caller.getModule().isNamed()) {
Module callerModule = getCallerModule(caller);
if (callerModule.isNamed()) {
throw new UnsupportedOperationException(
"ResourceBundle.Control not supported in named modules");
}
@ -1546,7 +1554,19 @@ public abstract class ResourceBundle {
Locale locale,
Class<?> caller,
Control control) {
return getBundleImpl(baseName, locale, caller, caller.getClassLoader(), control);
ClassLoader loader = getLoader(getCallerModule(caller));
return getBundleImpl(baseName, locale, caller, loader, control);
}
/*
* Determine the module to be used for the caller. If
* Reflection::getCallerClass is called from JNI with an empty
* stack frame the caller will be null, so the system class loader unnamed
* module will be used.
*/
private static Module getCallerModule(Class<?> caller) {
return (caller != null) ? caller.getModule()
: ClassLoader.getSystemClassLoader().getUnnamedModule();
}
/**
@ -1565,10 +1585,7 @@ public abstract class ResourceBundle {
Class<?> caller,
ClassLoader loader,
Control control) {
if (caller == null) {
throw new InternalError("null caller");
}
Module callerModule = caller.getModule();
Module callerModule = getCallerModule(caller);
// get resource bundles for a named module only if loader is the module's class loader
if (callerModule.isNamed() && loader == getLoader(callerModule)) {
@ -1592,7 +1609,7 @@ public abstract class ResourceBundle {
Locale locale,
Control control) {
Objects.requireNonNull(module);
Module callerModule = caller.getModule();
Module callerModule = getCallerModule(caller);
if (callerModule != module) {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
@ -2228,9 +2245,9 @@ public abstract class ResourceBundle {
*/
@CallerSensitive
public static final void clearCache() {
Class<?> caller = Reflection.getCallerClass();
Module callerModule = getCallerModule(Reflection.getCallerClass());
cacheList.keySet().removeIf(
key -> key.getCallerModule() == caller.getModule()
key -> key.getCallerModule() == callerModule
);
}

@ -0,0 +1,83 @@
/*
* 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 8280902
* @summary Test uses custom launcher that starts VM using JNI that verifies
* ResourceBundle::getBundle with null caller class functions properly
* using the system class loader unnamed module. The custom launcher
* creates a properties file and passes the VM option to the JNI
* functionality for the resource lookup.
* @library /test/lib
* @requires os.family != "aix"
* @run main/native NullCallerResourceBundle
*/
// Test disabled on AIX since we cannot invoke the JVM on the primordial thread.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import jdk.test.lib.Platform;
import jdk.test.lib.process.OutputAnalyzer;
public class NullCallerResourceBundle {
public static void main(String[] args) throws IOException {
// build a properties file for the native test
var propPath = Path.of(System.getProperty("test.classes"), "NullCallerResource.properties");
try (var stream = Files.newOutputStream(propPath)) {
var props = new Properties();
props.put("message", "Hello!");
props.save(stream, "Test property list");
}
var launcher = Path.of(System.getProperty("test.nativepath"), "NullCallerResourceBundle");
var classpathAppend = "-Djava.class.path=" + System.getProperty("test.classes");
var pb = new ProcessBuilder(launcher.toString(), classpathAppend);
var env = pb.environment();
var libDir = Platform.libDir().toString();
var vmDir = Platform.jvmLibDir().toString();
// set up shared library path
var 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,102 @@
/*
* 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"
#undef NDEBUG
#include "assert.h"
#include "string.h"
/*
* The java test running this native test passes in an argument to provide as
* an option for the configuration of the JVM. The system classpath has the
* classpath of the java test appended so it can pick up the resource that
* was created by the java part of the test.
*/
int main(int argc, char** args) {
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
jint rc;
assert(argc == 2);
options[0].optionString = args[1];
vm_args.version = JNI_VERSION_1_2;
vm_args.nOptions = 1;
vm_args.options = options;
if ((rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args)) != JNI_OK) {
printf("ERROR: cannot create VM.\n");
exit(-1);
}
// b = ResourceBundle.getBundle("NullCallerResource");
jclass class_ResourceBundle = (*env)->FindClass(env, "java/util/ResourceBundle");
assert(class_ResourceBundle != NULL);
jmethodID mid_ResourceBundle_getBundle = (*env)->GetStaticMethodID(env, class_ResourceBundle, "getBundle", "(Ljava/lang/String;)Ljava/util/ResourceBundle;" );
assert(mid_ResourceBundle_getBundle != NULL);
jobject resourceName = (*env)->NewStringUTF(env, "NullCallerResource");
assert(resourceName != NULL);
jobject b = (*env)->CallStaticObjectMethod(env, class_ResourceBundle, mid_ResourceBundle_getBundle, resourceName);
if ((*env)->ExceptionOccurred(env) != NULL) {
printf("ERROR: Exception was thrown calling ResourceBundle::getBundle.\n");
(*env)->ExceptionDescribe(env);
exit(-1);
}
// msg = b.getString("message");
jmethodID mid_ResourceBundle_getString = (*env)->GetMethodID(env, class_ResourceBundle, "getString", "(Ljava/lang/String;)Ljava/lang/String;" );
assert(mid_ResourceBundle_getString != NULL);
jobject key = (*env)->NewStringUTF(env, "message");
jobject msg =(*env)->CallObjectMethod(env, b, mid_ResourceBundle_getString, key);
if ((*env)->ExceptionOccurred(env) != NULL) {
printf("ERROR: Exception was thrown calling ResourceBundle::getString.\n");
(*env)->ExceptionDescribe(env);
exit(-1);
}
assert(msg != NULL);
// check the message
const char* cstr = (*env)->GetStringUTFChars(env, msg, NULL);
assert(cstr != NULL);
assert(strcmp(cstr,"Hello!") == 0);
// ResourceBundle.clearCache()
jmethodID mid_ResourceBundle_clearCache = (*env)->GetStaticMethodID(env, class_ResourceBundle, "clearCache", "()V" );
assert(mid_ResourceBundle_clearCache != NULL);
(*env)->CallStaticVoidMethod(env, class_ResourceBundle, mid_ResourceBundle_clearCache);
if ((*env)->ExceptionOccurred(env) != NULL) {
printf("ERROR: Exception was thrown calling ResourceBundle::clearCache.\n");
(*env)->ExceptionDescribe(env);
exit(-1);
}
(*jvm)->DestroyJavaVM(jvm);
return 0;
}