mirror of
https://github.com/openjdk/jdk15u.git
synced 2025-12-10 17:33:04 -06:00
8275703: System.loadLibrary fails on Big Sur for libraries hidden from filesystem
Reviewed-by: vkempik Backport-of: 309acbf0e86a0d248294503fccc7a936fa0a846e
This commit is contained in:
parent
528e52f4d7
commit
916b139f72
@ -84,8 +84,9 @@ ifeq ($(call isTargetOs, macosx), true)
|
|||||||
-framework Cocoa -framework SystemConfiguration
|
-framework Cocoa -framework SystemConfiguration
|
||||||
else
|
else
|
||||||
BUILD_JDK_JTREG_EXCLUDE += libTestMainKeyWindow.m
|
BUILD_JDK_JTREG_EXCLUDE += libTestMainKeyWindow.m
|
||||||
BUILD_JDK_JTREG_EXCLUDE += exeJniInvocationTest.c
|
|
||||||
BUILD_JDK_JTREG_EXCLUDE += libTestDynamicStore.m
|
BUILD_JDK_JTREG_EXCLUDE += libTestDynamicStore.m
|
||||||
|
BUILD_JDK_JTREG_EXCLUDE += exeJniInvocationTest.c
|
||||||
|
BUILD_JDK_JTREG_EXCLUDE += exeLibraryCache.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(call isTargetOs, linux), true)
|
ifeq ($(call isTargetOs, linux), true)
|
||||||
|
|||||||
@ -162,7 +162,7 @@ JNIEXPORT jboolean JNICALL
|
|||||||
JVM_IsUseContainerSupport(void);
|
JVM_IsUseContainerSupport(void);
|
||||||
|
|
||||||
JNIEXPORT void * JNICALL
|
JNIEXPORT void * JNICALL
|
||||||
JVM_LoadLibrary(const char *name);
|
JVM_LoadLibrary(const char *name, jboolean throwException);
|
||||||
|
|
||||||
JNIEXPORT void JNICALL
|
JNIEXPORT void JNICALL
|
||||||
JVM_UnloadLibrary(void * handle);
|
JVM_UnloadLibrary(void * handle);
|
||||||
|
|||||||
@ -3560,7 +3560,7 @@ JVM_END
|
|||||||
|
|
||||||
// Library support ///////////////////////////////////////////////////////////////////////////
|
// Library support ///////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name))
|
JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name, jboolean throwException))
|
||||||
//%note jvm_ct
|
//%note jvm_ct
|
||||||
JVMWrapper("JVM_LoadLibrary");
|
JVMWrapper("JVM_LoadLibrary");
|
||||||
char ebuf[1024];
|
char ebuf[1024];
|
||||||
@ -3570,18 +3570,23 @@ JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name))
|
|||||||
load_result = os::dll_load(name, ebuf, sizeof ebuf);
|
load_result = os::dll_load(name, ebuf, sizeof ebuf);
|
||||||
}
|
}
|
||||||
if (load_result == NULL) {
|
if (load_result == NULL) {
|
||||||
char msg[1024];
|
if (throwException) {
|
||||||
jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
|
char msg[1024];
|
||||||
// Since 'ebuf' may contain a string encoded using
|
jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
|
||||||
// platform encoding scheme, we need to pass
|
// Since 'ebuf' may contain a string encoded using
|
||||||
// Exceptions::unsafe_to_utf8 to the new_exception method
|
// platform encoding scheme, we need to pass
|
||||||
// as the last argument. See bug 6367357.
|
// Exceptions::unsafe_to_utf8 to the new_exception method
|
||||||
Handle h_exception =
|
// as the last argument. See bug 6367357.
|
||||||
Exceptions::new_exception(thread,
|
Handle h_exception =
|
||||||
vmSymbols::java_lang_UnsatisfiedLinkError(),
|
Exceptions::new_exception(thread,
|
||||||
msg, Exceptions::unsafe_to_utf8);
|
vmSymbols::java_lang_UnsatisfiedLinkError(),
|
||||||
|
msg, Exceptions::unsafe_to_utf8);
|
||||||
|
|
||||||
THROW_HANDLE_0(h_exception);
|
THROW_HANDLE_0(h_exception);
|
||||||
|
} else {
|
||||||
|
log_info(library)("Failed to load library %s", name);
|
||||||
|
return load_result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, name, p2i(load_result));
|
log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, name, p2i(load_result));
|
||||||
return load_result;
|
return load_result;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -27,11 +27,37 @@ package jdk.internal.loader;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import sun.security.action.GetPropertyAction;
|
||||||
|
|
||||||
class ClassLoaderHelper {
|
class ClassLoaderHelper {
|
||||||
|
private static final boolean hasDynamicLoaderCache;
|
||||||
|
static {
|
||||||
|
String osVersion = GetPropertyAction.privilegedGetProperty("os.version");
|
||||||
|
// dynamic linker cache support on os.version >= 11.x
|
||||||
|
int major = 11;
|
||||||
|
int i = osVersion.indexOf('.');
|
||||||
|
try {
|
||||||
|
major = Integer.parseInt(i < 0 ? osVersion : osVersion.substring(0, i));
|
||||||
|
} catch (NumberFormatException e) {}
|
||||||
|
hasDynamicLoaderCache = major >= 11;
|
||||||
|
}
|
||||||
|
|
||||||
private ClassLoaderHelper() {}
|
private ClassLoaderHelper() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if loading a native library only if
|
||||||
|
* it's present on the file system.
|
||||||
|
*
|
||||||
|
* @implNote
|
||||||
|
* On macOS 11.x or later which supports dynamic linker cache,
|
||||||
|
* the dynamic library is not present on the filesystem. The
|
||||||
|
* library cannot determine if a dynamic library exists on a
|
||||||
|
* given path or not and so this method returns false.
|
||||||
|
*/
|
||||||
|
static boolean loadLibraryOnlyIfPresent() {
|
||||||
|
return !hasDynamicLoaderCache;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an alternate path name for the given file
|
* Returns an alternate path name for the given file
|
||||||
* such that if the original pathname did not exist, then the
|
* such that if the original pathname did not exist, then the
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -53,7 +53,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
* will fail.
|
* will fail.
|
||||||
*/
|
*/
|
||||||
public final class NativeLibraries {
|
public final class NativeLibraries {
|
||||||
|
private static final boolean loadLibraryOnlyIfPresent = ClassLoaderHelper.loadLibraryOnlyIfPresent();
|
||||||
private final Map<String, NativeLibraryImpl> libraries = new ConcurrentHashMap<>();
|
private final Map<String, NativeLibraryImpl> libraries = new ConcurrentHashMap<>();
|
||||||
private final ClassLoader loader;
|
private final ClassLoader loader;
|
||||||
// caller, if non-null, is the fromClass parameter for NativeLibraries::loadLibrary
|
// caller, if non-null, is the fromClass parameter for NativeLibraries::loadLibrary
|
||||||
@ -142,7 +142,8 @@ public final class NativeLibraries {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Load a native library from the given file. Returns null if file does not exist.
|
* Load a native library from the given file. Returns null if the given
|
||||||
|
* library is determined to be non-loadable, which is system-dependent.
|
||||||
*
|
*
|
||||||
* @param fromClass the caller class calling System::loadLibrary
|
* @param fromClass the caller class calling System::loadLibrary
|
||||||
* @param file the path of the native library
|
* @param file the path of the native library
|
||||||
@ -154,14 +155,17 @@ public final class NativeLibraries {
|
|||||||
boolean isBuiltin = (name != null);
|
boolean isBuiltin = (name != null);
|
||||||
if (!isBuiltin) {
|
if (!isBuiltin) {
|
||||||
name = AccessController.doPrivileged(new PrivilegedAction<>() {
|
name = AccessController.doPrivileged(new PrivilegedAction<>() {
|
||||||
public String run() {
|
public String run() {
|
||||||
try {
|
try {
|
||||||
return file.exists() ? file.getCanonicalPath() : null;
|
if (loadLibraryOnlyIfPresent && !file.exists()) {
|
||||||
} catch (IOException e) {
|
return null;
|
||||||
return null;
|
}
|
||||||
|
return file.getCanonicalPath();
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -380,7 +384,7 @@ public final class NativeLibraries {
|
|||||||
throw new InternalError("Native library " + name + " has been loaded");
|
throw new InternalError("Native library " + name + " has been loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
return load(this, name, isBuiltin, isJNI);
|
return load(this, name, isBuiltin, isJNI, loadLibraryOnlyIfPresent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,7 +464,9 @@ public final class NativeLibraries {
|
|||||||
|
|
||||||
// JNI FindClass expects the caller class if invoked from JNI_OnLoad
|
// JNI FindClass expects the caller class if invoked from JNI_OnLoad
|
||||||
// and JNI_OnUnload is NativeLibrary class
|
// and JNI_OnUnload is NativeLibrary class
|
||||||
private static native boolean load(NativeLibraryImpl impl, String name, boolean isBuiltin, boolean isJNI);
|
private static native boolean load(NativeLibraryImpl impl, String name,
|
||||||
|
boolean isBuiltin, boolean isJNI,
|
||||||
|
boolean throwExceptionIfFail);
|
||||||
private static native void unload(String name, boolean isBuiltin, boolean isJNI, long handle);
|
private static native void unload(String name, boolean isBuiltin, boolean isJNI, long handle);
|
||||||
private static native String findBuiltinLib(String name);
|
private static native String findBuiltinLib(String name);
|
||||||
private static native long findEntry0(NativeLibraryImpl lib, String name);
|
private static native long findEntry0(NativeLibraryImpl lib, String name);
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -113,7 +113,8 @@ static void *findJniFunction(JNIEnv *env, void *handle,
|
|||||||
*/
|
*/
|
||||||
JNIEXPORT jboolean JNICALL
|
JNIEXPORT jboolean JNICALL
|
||||||
Java_jdk_internal_loader_NativeLibraries_load
|
Java_jdk_internal_loader_NativeLibraries_load
|
||||||
(JNIEnv *env, jobject this, jobject lib, jstring name, jboolean isBuiltin, jboolean isJNI)
|
(JNIEnv *env, jobject this, jobject lib, jstring name,
|
||||||
|
jboolean isBuiltin, jboolean isJNI, jboolean throwExceptionIfFail)
|
||||||
{
|
{
|
||||||
const char *cname;
|
const char *cname;
|
||||||
jint jniVersion;
|
jint jniVersion;
|
||||||
@ -127,7 +128,7 @@ Java_jdk_internal_loader_NativeLibraries_load
|
|||||||
cname = JNU_GetStringPlatformChars(env, name, 0);
|
cname = JNU_GetStringPlatformChars(env, name, 0);
|
||||||
if (cname == 0)
|
if (cname == 0)
|
||||||
return JNI_FALSE;
|
return JNI_FALSE;
|
||||||
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname);
|
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname, throwExceptionIfFail);
|
||||||
if (isJNI) {
|
if (isJNI) {
|
||||||
if (handle) {
|
if (handle) {
|
||||||
JNI_OnLoad_t JNI_OnLoad;
|
JNI_OnLoad_t JNI_OnLoad;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -32,6 +32,14 @@ class ClassLoaderHelper {
|
|||||||
|
|
||||||
private ClassLoaderHelper() {}
|
private ClassLoaderHelper() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if loading a native library only if
|
||||||
|
* it's present on the file system.
|
||||||
|
*/
|
||||||
|
static boolean loadLibraryOnlyIfPresent() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an alternate path name for the given file
|
* Returns an alternate path name for the given file
|
||||||
* such that if the original pathname did not exist, then the
|
* such that if the original pathname did not exist, then the
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -31,6 +31,14 @@ class ClassLoaderHelper {
|
|||||||
|
|
||||||
private ClassLoaderHelper() {}
|
private ClassLoaderHelper() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if loading a native library only if
|
||||||
|
* it's present on the file system.
|
||||||
|
*/
|
||||||
|
static boolean loadLibraryOnlyIfPresent() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an alternate path name for the given file
|
* Returns an alternate path name for the given file
|
||||||
* such that if the original pathname did not exist, then the
|
* such that if the original pathname did not exist, then the
|
||||||
|
|||||||
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021, 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 8275703
|
||||||
|
* @library /test/lib
|
||||||
|
* @requires os.family == "mac"
|
||||||
|
* @run main/native/othervm -Djava.library.path=/usr/lib LibraryFromCache blas
|
||||||
|
* @run main/native/othervm -Djava.library.path=/usr/lib LibraryFromCache BLAS
|
||||||
|
* @summary Test System::loadLibrary to be able to load a library even
|
||||||
|
* if it's not present on the filesystem on macOS which supports
|
||||||
|
* dynamic library cache
|
||||||
|
*/
|
||||||
|
|
||||||
|
import jdk.test.lib.process.OutputAnalyzer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
public class LibraryFromCache {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
String libname = args[0];
|
||||||
|
if (!systemHasLibrary(libname)) {
|
||||||
|
System.out.println("Test skipped. Library " + libname + " not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.loadLibrary(libname);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns true if dlopen successfully loads the specified library
|
||||||
|
*/
|
||||||
|
private static boolean systemHasLibrary(String libname) throws IOException {
|
||||||
|
Path launcher = Paths.get(System.getProperty("test.nativepath"), "LibraryCache");
|
||||||
|
ProcessBuilder pb = new ProcessBuilder(launcher.toString(), "lib" + libname + ".dylib");
|
||||||
|
OutputAnalyzer outputAnalyzer = new OutputAnalyzer(pb.start());
|
||||||
|
System.out.println(outputAnalyzer.getOutput());
|
||||||
|
return outputAnalyzer.getExitValue() == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021, 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 <dlfcn.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
void *handle;
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
fprintf(stderr, "Usage: %s <lib_filename_or_full_path>\n", argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Attempting to load library '%s'...\n", argv[1]);
|
||||||
|
|
||||||
|
handle = dlopen(argv[1], RTLD_LAZY);
|
||||||
|
|
||||||
|
if (handle == NULL) {
|
||||||
|
fprintf(stderr, "Unable to load library!\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Library successfully loaded!\n");
|
||||||
|
|
||||||
|
return dlclose(handle);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user