Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
8266814: Improve library loading with SymbolLookup abstraction
Reviewed-by: jvernee, sundar
- Loading branch information
Showing
with
350 additions
and 269 deletions.
- +59 −0 make/modules/jdk.incubator.foreign/Lib.gmk
- +10 −18 src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/CLinker.java
- +77 −0 src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/SymbolLookup.java
- +5 −6 src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/package-info.java
- +64 −0 src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/SystemLookup.java
- +16 −2 src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/SharedUtils.java
- +0 −81 src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/VMFunctions.java
- +28 −13 ...share/native/libjava/VMFunctions.c → jdk.incubator.foreign/share/native/libsyslookup/syslookup.c}
- +5 −2 test/jdk/java/foreign/SafeFunctionAccessTest.java
- +9 −12 test/jdk/java/foreign/StdLibTest.java
- +5 −2 test/jdk/java/foreign/TestDowncall.java
- +7 −4 test/jdk/java/foreign/TestIntrinsics.java
- +3 −0 test/jdk/java/foreign/TestNulls.java
- +9 −9 test/jdk/java/foreign/{TestLibraryLookup.java → TestSymbolLookup.java}
- +5 −3 test/jdk/java/foreign/TestUpcall.java
- +3 −1 test/jdk/java/foreign/TestUpcallHighArity.java
- +3 −1 test/jdk/java/foreign/TestUpcallStructScope.java
- +5 −1 test/jdk/java/foreign/TestVarArgs.java
- +0 −77 test/jdk/java/foreign/libStdLibTest.c
- +3 −2 test/jdk/java/foreign/stackwalk/TestStackWalk.java
- +3 −1 test/jdk/java/foreign/valist/VaListTest.java
- +5 −3 test/jdk/java/foreign/virtual/TestVirtualCalls.java
- +12 −10 test/micro/org/openjdk/bench/jdk/incubator/foreign/CallOverheadHelper.java
- +4 −4 test/micro/org/openjdk/bench/jdk/incubator/foreign/StrLenTest.java
- +2 −1 test/micro/org/openjdk/bench/jdk/incubator/foreign/Upcalls.java
- +4 −2 test/micro/org/openjdk/bench/jdk/incubator/foreign/VaList.java
- +0 −12 test/micro/org/openjdk/bench/jdk/incubator/foreign/libStrLen.c
- +4 −2 test/micro/org/openjdk/bench/jdk/incubator/foreign/points/support/PanamaPoint.java
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,59 @@ | ||
# | ||
# 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. Oracle designates this | ||
# particular file as subject to the "Classpath" exception as provided | ||
# by Oracle in the LICENSE file that accompanied this code. | ||
# | ||
# 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 LibCommon.gmk | ||
|
||
ifeq ($(call isTargetOs, linux), true) | ||
|
||
$(eval $(call SetupJdkLibrary, BUILD_LIBCSTDLIB, \ | ||
NAME := syslookup, \ | ||
OPTIMIZATION := HIGH, \ | ||
DISABLED_WARNINGS_gcc := sign-compare pointer-arith, \ | ||
DISABLED_WARNINGS_clang := sign-compare pointer-arith format-nonliteral, \ | ||
CFLAGS := $(CFLAGS_JDKLIB), \ | ||
CXXFLAGS := $(CXXFLAGS_JDKLIB), \ | ||
LDFLAGS := -Wl$(COMMA)--no-as-needed -lc -lm $(LDFLAGS_JDKLIB) $(call SET_SHARED_LIBRARY_ORIGIN), \ | ||
LIBS := $(LIBCXX), \ | ||
)) | ||
|
||
else ifeq ($(call isTargetOs, windows), false) | ||
|
||
$(eval $(call SetupJdkLibrary, BUILD_LIBCSTDLIB, \ | ||
NAME := syslookup, \ | ||
OPTIMIZATION := HIGH, \ | ||
DISABLED_WARNINGS_gcc := sign-compare pointer-arith, \ | ||
DISABLED_WARNINGS_clang := sign-compare pointer-arith format-nonliteral, \ | ||
CFLAGS := $(CFLAGS_JDKLIB), \ | ||
CXXFLAGS := $(CXXFLAGS_JDKLIB), \ | ||
LDFLAGS := $(LDFLAGS_JDKLIB) $(call SET_SHARED_LIBRARY_ORIGIN), \ | ||
LIBS := $(LIBCXX), \ | ||
)) | ||
|
||
|
||
endif | ||
|
||
TARGETS += $(BUILD_LIBCSTDLIB) | ||
|
||
################################################################################ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* 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. | ||
*/ | ||
package jdk.incubator.foreign; | ||
|
||
import jdk.internal.access.JavaLangAccess; | ||
import jdk.internal.access.SharedSecrets; | ||
import jdk.internal.reflect.CallerSensitive; | ||
import jdk.internal.reflect.Reflection; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* A symbol lookup. Exposes a lookup operation for searching symbols, see {@link SymbolLookup#lookup(String)}. | ||
* <p> Unless otherwise specified, passing a {@code null} argument, or an array argument containing one or more {@code null} | ||
* elements to a method in this class causes a {@link NullPointerException NullPointerException} to be thrown. </p> | ||
*/ | ||
@FunctionalInterface | ||
public interface SymbolLookup { | ||
|
||
/** | ||
* Looks up a symbol with given name in this lookup. | ||
* | ||
* @param name the symbol name. | ||
* @return the memory address associated with the symbol (if any). | ||
*/ | ||
Optional<MemoryAddress> lookup(String name); | ||
|
||
/** | ||
* Obtains a symbol lookup suitable to find symbols in native libraries associated with the caller's classloader | ||
* (that is, libraries loaded using {@link System#loadLibrary} or {@link System#load}). | ||
* <p> | ||
* This method is <a href="package-summary.html#restricted"><em>restricted</em></a>. | ||
* Restricted method are unsafe, and, if used incorrectly, their use might crash | ||
* the JVM or, worse, silently result in memory corruption. Thus, clients should refrain from depending on | ||
* restricted methods, and use safe and supported functionalities, where possible. | ||
* | ||
* @return a symbol lookup suitable to find symbols in libraries loaded by the caller's classloader. | ||
* @throws IllegalCallerException if access to this method occurs from a module {@code M} and the command line option | ||
* {@code --enable-native-access} is either absent, or does not mention the module name {@code M}, or | ||
* {@code ALL-UNNAMED} in case {@code M} is an unnamed module. | ||
*/ | ||
@CallerSensitive | ||
static SymbolLookup loaderLookup() { | ||
Class<?> caller = Reflection.getCallerClass(); | ||
Reflection.ensureNativeAccess(caller); | ||
ClassLoader loader = Objects.requireNonNull(caller.getClassLoader()); | ||
return name -> { | ||
Objects.requireNonNull(name); | ||
JavaLangAccess javaLangAccess = SharedSecrets.getJavaLangAccess(); | ||
MemoryAddress addr = MemoryAddress.ofLong(javaLangAccess.findNative(loader, name)); | ||
return addr == MemoryAddress.NULL? Optional.empty() : Optional.of(addr); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package jdk.internal.foreign; | ||
|
||
import jdk.incubator.foreign.SymbolLookup; | ||
import jdk.incubator.foreign.MemoryAddress; | ||
import jdk.internal.loader.NativeLibraries; | ||
import jdk.internal.loader.NativeLibrary; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class SystemLookup implements SymbolLookup { | ||
|
||
private SystemLookup() { } | ||
|
||
final static SystemLookup INSTANCE = new SystemLookup(); | ||
|
||
/* | ||
* On POSIX systems, dlsym will allow us to lookup symbol in library dependencies; the same trick doesn't work | ||
* on Windows. For this reason, on Windows we do not generate any side-library, and load msvcrt.dll directly instead. | ||
*/ | ||
final NativeLibrary syslookup = switch (CABI.current()) { | ||
case SysV, AArch64 -> NativeLibraries.rawNativeLibraries(SystemLookup.class, false).loadLibrary("syslookup"); | ||
case Win64 -> NativeLibraries.rawNativeLibraries(SystemLookup.class, false) | ||
.loadLibrary(null, Path.of(System.getenv("SystemRoot"), "System32", "msvcrt.dll").toFile()); | ||
}; | ||
|
||
@Override | ||
public Optional<MemoryAddress> lookup(String name) { | ||
Objects.requireNonNull(name); | ||
long addr = syslookup.find(name); | ||
return addr == 0 ? | ||
Optional.empty() : Optional.of(MemoryAddress.ofLong(addr)); | ||
} | ||
|
||
public static SystemLookup getInstance() { | ||
return INSTANCE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.