From 2a96cf14c4598e84187ec73d67eae3573f33648e Mon Sep 17 00:00:00 2001 From: xdark Date: Sun, 17 Nov 2024 21:55:21 +0300 Subject: [PATCH] Initial work to reject bundled JavaFX --- .../java/software/coley/recaf/Bootstrap.java | 1 + .../coley/recaf/ExitDebugLoggingHook.java | 64 +++++++++++++++++++ .../coley/recaf/util/ClassDefiner.java | 2 +- .../coley/recaf/util/InternalPath.java | 2 +- 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/recaf-core/src/main/java/software/coley/recaf/Bootstrap.java b/recaf-core/src/main/java/software/coley/recaf/Bootstrap.java index af929916a..2631392d3 100644 --- a/recaf-core/src/main/java/software/coley/recaf/Bootstrap.java +++ b/recaf-core/src/main/java/software/coley/recaf/Bootstrap.java @@ -65,6 +65,7 @@ public static void setWeldConsumer(@Nullable Consumer consumer) { private static SeContainer createContainer() { logger.info("Creating Recaf CDI container..."); Weld weld = new Weld("recaf"); + weld.setClassLoader(Bootstrap.class.getClassLoader()); // Setup custom interceptors & extensions logger.info("CDI: Adding interceptors & extensions"); diff --git a/recaf-core/src/main/java/software/coley/recaf/ExitDebugLoggingHook.java b/recaf-core/src/main/java/software/coley/recaf/ExitDebugLoggingHook.java index f0eeb292a..c3a31a2bc 100644 --- a/recaf-core/src/main/java/software/coley/recaf/ExitDebugLoggingHook.java +++ b/recaf-core/src/main/java/software/coley/recaf/ExitDebugLoggingHook.java @@ -16,10 +16,16 @@ import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; +import java.lang.module.ModuleReference; +import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.net.URI; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; +import java.util.Iterator; +import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; @@ -118,9 +124,33 @@ private static void handle(int code) { System.out.println(" - Version (Raw): " + JavaVersion.get()); System.out.println(" - Vendor: " + System.getProperty("java.vm.vendor", "")); System.out.println(" - Home: " + System.getProperty("java.home", "")); + System.out.println("JavaFX"); System.out.println(" - Version (Runtime): " + System.getProperty("javafx.runtime.version", "")); System.out.println(" - Version (Raw): " + System.getProperty("javafx.version", "")); + { + ClassLoader loader = ExitDebugLoggingHook.class.getClassLoader(); + String javafxClass = "javafx/beans/Observable.class"; + try { + Iterator iterator = loader.getResources(javafxClass).asIterator(); + if (!iterator.hasNext()) { + System.out.println(" - Location: not found"); + } else { + URL url = iterator.next(); + if (!iterator.hasNext()) { + System.out.println(" - Location: " + url); + } else { + System.out.println(" - Location (likely): " + url); + do { + System.out.println(" - Location (seen): " + url); + } while (iterator.hasNext()); + } + } + } catch (Exception ex) { + System.out.println(" - Location: "); + } + } + System.out.println("Operating System"); System.out.println(" - Name: " + System.getProperty("os.name")); System.out.println(" - Version: " + System.getProperty("os.version")); @@ -154,6 +184,11 @@ private static void handle(int code) { } } + System.out.println("Boot class loader:"); + dumpBootstrapClassLoader(); + System.out.println("Platform class loader:"); + dumpBuiltinClassLoader(ClassLoader.getPlatformClassLoader()); + Path root = RecafDirectoriesConfig.createBaseDirectory().resolve("config"); if (printConfigs && Files.isDirectory(root)) { System.out.println("Configs"); @@ -196,6 +231,35 @@ private static void handle(int code) { }); } + private static void dumpBuiltinClassLoader(ClassLoader loader) { + try { + Class c = Class.forName("jdk.internal.loader.BuiltinClassLoader"); + Field nameToModuleField = c.getDeclaredField("nameToModule"); + nameToModuleField.setAccessible(true); + //noinspection unchecked + Map mdouleMap = (Map) nameToModuleField.get(loader); + for (Map.Entry e : mdouleMap.entrySet()) { + ModuleReference moduleReference = e.getValue(); + System.out.printf("%s located at %s%n", moduleReference.descriptor().toNameAndVersion(), moduleReference.location() + .map(URI::toString) + .orElse("Unknown")); + } + } catch (Exception ex) { + System.out.printf("dumpBuiltinClassLoader(%s) - %n", loader); + } + } + + private static void dumpBootstrapClassLoader() { + try { + Class c = Class.forName("jdk.internal.loader.ClassLoaders"); + Method bootLoaderMethod = c.getDeclaredMethod("bootLoader"); + bootLoaderMethod.setAccessible(true); + dumpBuiltinClassLoader((ClassLoader) bootLoaderMethod.invoke(null)); + } catch (Exception ex) { + System.out.println("dumpBootstrapClassLoader - "); + } + } + @Nonnull @SuppressWarnings("all") private static String createSha1(@Nonnull File file) throws Exception { diff --git a/recaf-core/src/main/java/software/coley/recaf/util/ClassDefiner.java b/recaf-core/src/main/java/software/coley/recaf/util/ClassDefiner.java index 27a219632..a04eb3493 100644 --- a/recaf-core/src/main/java/software/coley/recaf/util/ClassDefiner.java +++ b/recaf-core/src/main/java/software/coley/recaf/util/ClassDefiner.java @@ -28,7 +28,7 @@ public ClassDefiner(@Nonnull String name, @Nonnull byte[] bytecode) { * Map of classes. */ public ClassDefiner(@Nonnull Map classes) { - super(ClassLoader.getSystemClassLoader()); + super(ClassDefiner.class.getClassLoader()); this.classes = classes; } diff --git a/recaf-core/src/main/java/software/coley/recaf/util/InternalPath.java b/recaf-core/src/main/java/software/coley/recaf/util/InternalPath.java index e706746f8..29a18df33 100644 --- a/recaf-core/src/main/java/software/coley/recaf/util/InternalPath.java +++ b/recaf-core/src/main/java/software/coley/recaf/util/InternalPath.java @@ -85,7 +85,7 @@ public String getFileName() { */ public URL getURL() throws IOException { if (internal) { - return ClassLoader.getSystemClassLoader().getResource(getPath()); + return InternalPath.class.getClassLoader().getResource(getPath()); } else { return new File(getPath()).toURI().toURL(); }