Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8266814: Improve library loading with SymbolLookup abstraction #531

Closed
@@ -58,35 +58,15 @@
* 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.

This comment has been minimized.

@JornVernee

JornVernee May 10, 2021
Member

Should this include an @throws for the ICE that can be thrown by the native access check?

* @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 loaderLookup0(loader);
}

/**
* Obtains a symbol lookup suitable to find symbols in native libraries associated with the given 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.
*
* @param loader the classloader whose symbol lookup is to be retrieved.
* @return a symbol lookup suitable to find symbols in libraries loaded by given classloader.
*/
@CallerSensitive
static SymbolLookup loaderLookup(ClassLoader loader) {
Reflection.ensureNativeAccess(Reflection.getCallerClass());
Objects.requireNonNull(loader);
return loaderLookup0(loader);
}

private static SymbolLookup loaderLookup0(ClassLoader loader) {
return name -> {
Objects.requireNonNull(name);
JavaLangAccess javaLangAccess = SharedSecrets.getJavaLangAccess();
@@ -40,10 +40,14 @@ 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)

This comment has been minimized.

@JornVernee

JornVernee May 10, 2021
Member

e.g. a short comment here that explains why Windows is different.

This comment has been minimized.

@mcimadamore

mcimadamore May 10, 2021
Author Collaborator

I'll add a comment here

.loadLibrary(Path.of(System.getenv("SystemRoot"), "System32", "msvcrt.dll").toString());
.loadLibrary(null, Path.of(System.getenv("SystemRoot"), "System32", "msvcrt.dll").toFile());
};

@Override