mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-19 10:34:38 +02:00
8275703: System.loadLibrary fails on Big Sur for libraries hidden from filesystem
Reviewed-by: dholmes, alanb, mcimadamore
This commit is contained in:
parent
abe52aea23
commit
309acbf0e8
10 changed files with 199 additions and 32 deletions
|
@ -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.
|
||||
*
|
||||
* 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.util.ArrayList;
|
||||
import sun.security.action.GetPropertyAction;
|
||||
|
||||
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() {}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* such that if the original pathname did not exist, then the
|
||||
|
|
|
@ -36,7 +36,6 @@ import java.util.ArrayDeque;
|
|||
import java.util.Deque;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
@ -56,7 +55,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||
* will fail.
|
||||
*/
|
||||
public final class NativeLibraries {
|
||||
|
||||
private static final boolean loadLibraryOnlyIfPresent = ClassLoaderHelper.loadLibraryOnlyIfPresent();
|
||||
private final Map<String, NativeLibraryImpl> libraries = new ConcurrentHashMap<>();
|
||||
private final ClassLoader loader;
|
||||
// caller, if non-null, is the fromClass parameter for NativeLibraries::loadLibrary
|
||||
|
@ -145,7 +144,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 file the path of the native library
|
||||
|
@ -158,14 +158,17 @@ public final class NativeLibraries {
|
|||
boolean isBuiltin = (name != null);
|
||||
if (!isBuiltin) {
|
||||
name = AccessController.doPrivileged(new PrivilegedAction<>() {
|
||||
public String run() {
|
||||
try {
|
||||
return file.exists() ? file.getCanonicalPath() : null;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
public String run() {
|
||||
try {
|
||||
if (loadLibraryOnlyIfPresent && !file.exists()) {
|
||||
return null;
|
||||
}
|
||||
return file.getCanonicalPath();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
if (name == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -389,7 +392,7 @@ public final class NativeLibraries {
|
|||
throw new InternalError("Native library " + name + " has been loaded");
|
||||
}
|
||||
|
||||
return load(this, name, isBuiltin, isJNI);
|
||||
return load(this, name, isBuiltin, isJNI, loadLibraryOnlyIfPresent);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -575,7 +578,9 @@ public final class NativeLibraries {
|
|||
|
||||
// JNI FindClass expects the caller class if invoked from JNI_OnLoad
|
||||
// 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 String findBuiltinLib(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.
|
||||
*
|
||||
* 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
|
||||
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;
|
||||
jint jniVersion;
|
||||
|
@ -127,7 +128,7 @@ Java_jdk_internal_loader_NativeLibraries_load
|
|||
cname = JNU_GetStringPlatformChars(env, name, 0);
|
||||
if (cname == 0)
|
||||
return JNI_FALSE;
|
||||
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname);
|
||||
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname, throwExceptionIfFail);
|
||||
if (isJNI) {
|
||||
if (handle) {
|
||||
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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -32,6 +32,14 @@ class 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
|
||||
* 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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -31,6 +31,14 @@ class 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
|
||||
* such that if the original pathname did not exist, then the
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue