Skip to content

Commit

Permalink
Merge master jdk-11.0.15+6 into openj9-staging
Browse files Browse the repository at this point in the history
  • Loading branch information
j9build committed Mar 17, 2022
2 parents 8310bdf + 131a4a4 commit 3634098
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 9 deletions.
1 change: 1 addition & 0 deletions make/test/JtregNativeJdk.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ ifeq ($(OPENJDK_TARGET_OS), macosx)
else
BUILD_JDK_JTREG_EXCLUDE += libTestMainKeyWindow.m
BUILD_JDK_JTREG_EXCLUDE += libTestDynamicStore.m
BUILD_JDK_JTREG_EXCLUDE += exeLibraryCache.c
endif

$(eval $(call SetupTestFilesCompilation, BUILD_JDK_JTREG_LIBRARIES, \
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/include/jvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ JNIEXPORT jboolean JNICALL
JVM_IsUseContainerSupport(void);

JNIEXPORT void * JNICALL
JVM_LoadLibrary(const char *name);
JVM_LoadLibrary(const char *name, jboolean throwException);

JNIEXPORT void JNICALL
JVM_UnloadLibrary(void * handle);
Expand Down
28 changes: 27 additions & 1 deletion src/java.base/macosx/classes/java/lang/ClassLoaderHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2015, 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
Expand All @@ -26,11 +26,37 @@
package java.lang;

import java.io.File;
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;
}

/**
* Indicates, whether PATH env variable is allowed to contain quoted entries.
*/
Expand Down
12 changes: 9 additions & 3 deletions src/java.base/share/classes/java/lang/ClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,8 @@ protected String findLibrary(String libname) {
* @since 1.2
*/
static class NativeLibrary {
private static final boolean loadLibraryOnlyIfPresent = ClassLoaderHelper.loadLibraryOnlyIfPresent();

// the class from which the library is loaded, also indicates
// the loader this native library belongs.
final Class<?> fromClass;
Expand All @@ -2420,7 +2422,8 @@ static class NativeLibrary {
// the version of JNI environment the native library requires.
int jniVersion;

native boolean load0(String name, boolean isBuiltin);
native boolean load0(String name, boolean isBuiltin,
boolean throwExceptionIfFail);

native long findEntry(String name);

Expand All @@ -2439,7 +2442,7 @@ boolean load() {
throw new InternalError("Native library " + name + " has been loaded");
}

if (!load0(name, isBuiltin)) return false;
if (!load0(name, isBuiltin, loadLibraryOnlyIfPresent)) return false;

// register the class loader for cleanup when unloaded
// built class loaders are never unloaded
Expand Down Expand Up @@ -2681,7 +2684,10 @@ private static boolean loadLibrary0(Class<?> fromClass, final File file) {
new PrivilegedAction<>() {
public String run() {
try {
return file.exists() ? file.getCanonicalPath() : null;
if (NativeLibrary.loadLibraryOnlyIfPresent && !file.exists()) {
return null;
}
return file.getCanonicalPath();
} catch (IOException e) {
return null;
}
Expand Down
7 changes: 4 additions & 3 deletions src/java.base/share/native/libjava/ClassLoader.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 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
Expand Down Expand Up @@ -336,7 +336,8 @@ static void *findJniFunction(JNIEnv *env, void *handle,
*/
JNIEXPORT jboolean JNICALL
Java_java_lang_ClassLoader_00024NativeLibrary_load0
(JNIEnv *env, jobject this, jstring name, jboolean isBuiltin)
(JNIEnv *env, jobject this, jstring name,
jboolean isBuiltin, jboolean throwExceptionIfFail)
{
const char *cname;
jint jniVersion;
Expand All @@ -350,7 +351,7 @@ Java_java_lang_ClassLoader_00024NativeLibrary_load0
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 (handle) {
JNI_OnLoad_t JNI_OnLoad;
JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
Expand Down
10 changes: 9 additions & 1 deletion src/java.base/unix/classes/java/lang/ClassLoaderHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2015, 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
Expand Down Expand Up @@ -36,6 +36,14 @@ private ClassLoaderHelper() {}
*/
static final boolean allowsQuotedPathElements = false;

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ private ClassLoaderHelper() {}
*/
static final boolean allowsQuotedPathElements = true;

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 3634098

Please sign in to comment.