Clean-up library loading a bit

This commit is contained in:
Michael Pfaff 2022-08-16 20:34:35 -04:00
parent b53c59b989
commit c84c67d672
Signed by: michael
GPG Key ID: CF402C4A012AA9D4
1 changed files with 21 additions and 14 deletions

View File

@ -382,14 +382,14 @@ public class LWJGLUtil {
String classloader_path = getPathFromClassLoader(libname, classloader);
if (classloader_path != null) {
log("getPathFromClassLoader: Path found: " + classloader_path);
logger().log(() -> "getPathFromClassLoader: Path found: " + classloader_path);
possible_paths.add(classloader_path);
}
for ( String platform_lib_name : platform_lib_names ) {
String lwjgl_classloader_path = getPathFromClassLoader("lwjgl", classloader);
if ( lwjgl_classloader_path != null ) {
log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
logger().log(() -> "getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator))
+ File.separator + platform_lib_name);
}
@ -409,6 +409,7 @@ public class LWJGLUtil {
possible_paths.add(path + File.separator + platform_lib_name);
}
// TODO: this can be very dangerous (see recent (2022-08) use of completely safe notepad.exe to load a malicious dll)
//add current path
String current_dir = getPrivilegedProperty("user.dir");
possible_paths.add(current_dir + File.separator + platform_lib_name);
@ -480,7 +481,7 @@ public class LWJGLUtil {
/**
* Tries to locate named library from the current ClassLoader
* This method exists because native libraries are loaded from native code, and as such
* is exempt from ClassLoader library loading rutines. It therefore always fails.
* is exempt from ClassLoader library loading routines. It therefore always fails.
* We therefore invoke the protected method of the ClassLoader to see if it can
* locate it.
*
@ -489,28 +490,34 @@ public class LWJGLUtil {
* @return Absolute path to library if found, otherwise null
*/
private static String getPathFromClassLoader(final String libname, final ClassLoader classloader) {
Class<?> c = null;
Class<?> clazz = null;
try {
if (LWJGLUtil.DEBUG) {
logger().log(() -> "getPathFromClassLoader: searching for: " + libname);
}
c = classloader.getClass();
while (c != null) {
final Class<?> clazz = c;
clazz = classloader.getClass();
Method findLibrary = null;
while (clazz != null) {
Class<?> clazz1 = clazz;
try {
Method findLibrary = clazz.getDeclaredMethod("findLibrary", String.class);
findLibrary = clazz.getDeclaredMethod("findLibrary", String.class);
findLibrary.setAccessible(true);
String path = (String)findLibrary.invoke(classloader, libname);
return path;
logger().log(() -> "Found findLibrary method on " + clazz1.getName());
break;
} catch (Exception e) {
logger().log(() -> "Failed to locate findLibrary method on " + clazz, e);
c = c.getSuperclass();
if (DEBUG) {
logger().log(() -> "Failed to locate findLibrary method on " + clazz1.getName(), e);
}
clazz = clazz.getSuperclass();
}
}
if (findLibrary != null) {
String path = (String)findLibrary.invoke(classloader, libname);
return path;
}
} catch (Exception e) {
Class<?> c1 = c;
logger().log(() -> "Failure locating using classloader " + c1, e);
logger().log(() -> "Failure locating using classloader " + classloader, e);
}
return null;
}