From aa022d9ec40d2a6e72a1e132807123d6d05fe77e Mon Sep 17 00:00:00 2001 From: wh1t3P1g Date: Thu, 1 Feb 2024 22:29:41 +0800 Subject: [PATCH] jre cached disabled by default --- config/settings.properties | 6 ++- src/main/java/tabby/App.java | 4 +- .../java/tabby/common/utils/FileUtils.java | 39 ++++++++++++++----- .../tabby/config/GlobalConfiguration.java | 12 +++--- src/main/java/tabby/core/Analyser.java | 4 +- .../tabby/core/collector/FileCollector.java | 8 ++-- .../tabby/core/scanner/ClassInfoScanner.java | 5 ++- 7 files changed, 53 insertions(+), 25 deletions(-) diff --git a/config/settings.properties b/config/settings.properties index c7b9d92..f14ce30 100644 --- a/config/settings.properties +++ b/config/settings.properties @@ -8,8 +8,10 @@ tabby.output.directory = ./output/dev tabby.debug.details = false # jdk settings -tabby.build.isJRE9Module = false -tabby.build.javaHome = /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home +tabby.build.useSettingJRE = false +tabby.build.isJRE9Module = true +#tabby.build.javaHome = /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home +tabby.build.javaHome = /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home tabby.build.isJDKProcess = false tabby.build.withAllJDK = false tabby.build.isJDKOnly = false diff --git a/src/main/java/tabby/App.java b/src/main/java/tabby/App.java index d746c97..0164b40 100644 --- a/src/main/java/tabby/App.java +++ b/src/main/java/tabby/App.java @@ -34,7 +34,7 @@ public static void main(String[] args) { public void setJavaHome(){ // set java home - if(GlobalConfiguration.TARGET_JAVA_HOME == null){ + if(GlobalConfiguration.TARGET_JAVA_HOME == null || !GlobalConfiguration.IS_USING_SETTING_JRE){ String javaHome = System.getProperty("java.home"); if(javaHome == null){ javaHome = System.getenv("JAVA_HOME"); @@ -44,7 +44,7 @@ public void setJavaHome(){ } } - log.info("Analysis target java.home: " + GlobalConfiguration.TARGET_JAVA_HOME); + log.info("Target java.home: " + GlobalConfiguration.TARGET_JAVA_HOME); } public void setLogDebugLevel(){ diff --git a/src/main/java/tabby/common/utils/FileUtils.java b/src/main/java/tabby/common/utils/FileUtils.java index 8df471e..944afce 100644 --- a/src/main/java/tabby/common/utils/FileUtils.java +++ b/src/main/java/tabby/common/utils/FileUtils.java @@ -307,8 +307,22 @@ public static void copy(String source, String target) throws IOException { Files.copy(Paths.get(source), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); } + private static String getJavaHome(){ + String javaHome = System.getProperty("java.home"); + if(javaHome == null){ + javaHome = System.getenv("JAVA_HOME"); + } + return javaHome; + } + public static Set findAllJdkDependencies(JModTransferPlugin plugin){ - String javaHome = GlobalConfiguration.TARGET_JAVA_HOME; + String javaHome; + if(GlobalConfiguration.IS_USING_SETTING_JRE){ + javaHome = GlobalConfiguration.TARGET_JAVA_HOME; + }else{ + javaHome = getJavaHome(); + GlobalConfiguration.IS_JRE9_MODULE = true; // 仅允许 17 运行,所以为 true + } if(javaHome == null){ throw new RuntimeException("JAVA_HOME not set!"); @@ -337,14 +351,21 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IO return FileVisitResult.CONTINUE; } - if(source.endsWith(".jar")){ - String dest = String.join(File.separator, Arrays.asList(GlobalConfiguration.JRE_LIBS_PATH, filename)); - futures.add(plugin.transfer(source, dest)); - libraries.add(dest); - }else if(source.endsWith(".jmod")){ - String dest = String.join(File.separator, Arrays.asList(GlobalConfiguration.JRE_LIBS_PATH, filename+".jar")); - futures.add(plugin.transfer(source, dest)); - libraries.add(dest); + String dest; + if(GlobalConfiguration.IS_USING_SETTING_JRE){ + if(source.endsWith(".jar")){ + dest = String.join(File.separator, Arrays.asList(GlobalConfiguration.JRE_LIBS_PATH, filename)); + futures.add(plugin.transfer(source, dest)); + libraries.add(dest); + }else if(source.endsWith(".jmod")){ + dest = String.join(File.separator, Arrays.asList(GlobalConfiguration.JRE_LIBS_PATH, filename+".jar")); + futures.add(plugin.transfer(source, dest)); + libraries.add(dest); + } + }else{ + if(source.endsWith(".jar") || source.endsWith(".jmod")){ + libraries.add(source); + } } return FileVisitResult.CONTINUE; diff --git a/src/main/java/tabby/config/GlobalConfiguration.java b/src/main/java/tabby/config/GlobalConfiguration.java index c9850e9..fd432f4 100644 --- a/src/main/java/tabby/config/GlobalConfiguration.java +++ b/src/main/java/tabby/config/GlobalConfiguration.java @@ -61,16 +61,11 @@ public class GlobalConfiguration { public static boolean isInitialed = false; public static boolean isNeedStop = false; public static boolean IS_JRE9_MODULE = false; + public static boolean IS_USING_SETTING_JRE = false; public static String TARGET_JAVA_HOME = null; public static String THREAD_POOL_SIZE = "max"; public static ThreadPoolTaskExecutor tabbyCollectorExecutor; - static { - if(!FileUtils.fileExists(JRE_LIBS_PATH)){ - FileUtils.createDirectory(JRE_LIBS_PATH); - } - } - public static void init(){ if(props == null){ props = new Properties(); @@ -91,6 +86,7 @@ public static void init(){ COMMON_JARS_PATH = String.join(File.separator, RULES_PATH, "commonJars.json"); THREAD_POOL_SIZE = getProperty("tabby.build.thread.size", "max", props); IS_JRE9_MODULE = getBooleanProperty("tabby.build.isJRE9Module", "false", props); + IS_USING_SETTING_JRE = getBooleanProperty("tabby.build.useSettingJRE", "false", props); TARGET_JAVA_HOME = getProperty("tabby.build.javaHome", null, props); int maxThreadPoolSize = Runtime.getRuntime().availableProcessors(); if("max".equals(THREAD_POOL_SIZE)){ @@ -123,6 +119,10 @@ public static void initConfig(){ clean(OUTPUT_DIRECTORY); } + if(IS_USING_SETTING_JRE && !FileUtils.fileExists(JRE_LIBS_PATH)){ + FileUtils.createDirectory(JRE_LIBS_PATH); + } + // resolve cache directory OUTPUT_DIRECTORY = FileUtils.getRealPath(OUTPUT_DIRECTORY); diff --git a/src/main/java/tabby/core/Analyser.java b/src/main/java/tabby/core/Analyser.java index 72d7da4..ecb8a42 100644 --- a/src/main/java/tabby/core/Analyser.java +++ b/src/main/java/tabby/core/Analyser.java @@ -85,7 +85,9 @@ public void runSootAnalysis(Map targets, List classpaths SootConfiguration.initSootOption(); addBasicClasses(); log.info("Load basic classes"); - Scene.v().setSootClassPath(String.join(File.pathSeparator, classpaths)); + if(GlobalConfiguration.IS_USING_SETTING_JRE){ + Scene.v().setSootClassPath(String.join(File.pathSeparator, classpaths)); + } Scene.v().loadBasicClasses(); log.info("Load dynamic classes"); Scene.v().loadDynamicClasses(); diff --git a/src/main/java/tabby/core/collector/FileCollector.java b/src/main/java/tabby/core/collector/FileCollector.java index ce32246..eb55ef0 100644 --- a/src/main/java/tabby/core/collector/FileCollector.java +++ b/src/main/java/tabby/core/collector/FileCollector.java @@ -56,7 +56,7 @@ public Map collectJdkDependencies() throws IOException { Map dependencies = new HashMap<>(); Set jdkLibs = null; - if(FileUtils.fileExists(GlobalConfiguration.JRE_LIBS_PATH)){ + if(GlobalConfiguration.IS_USING_SETTING_JRE && FileUtils.fileExists(GlobalConfiguration.JRE_LIBS_PATH)){ jdkLibs = FileUtils.findAllJarFiles(GlobalConfiguration.JRE_LIBS_PATH, false); } if(jdkLibs == null || jdkLibs.isEmpty()){ @@ -67,9 +67,9 @@ public Map collectJdkDependencies() throws IOException { if(GlobalConfiguration.IS_WITH_ALL_JDK){ dependencies.put(FileUtils.getFileMD5(filepath), filepath); }else if(GlobalConfiguration.IS_JRE9_MODULE){ - if(filepath.endsWith("java.base.jmod.jar") - || filepath.endsWith("java.desktop.jmod.jar") - || filepath.endsWith("java.logging.jmod.jar")){ + if(filepath.contains("java.base.jmod") + || filepath.contains("java.desktop.jmod") + || filepath.contains("java.logging.jmod")){ dependencies.put(FileUtils.getFileMD5(filepath), filepath); } }else{ diff --git a/src/main/java/tabby/core/scanner/ClassInfoScanner.java b/src/main/java/tabby/core/scanner/ClassInfoScanner.java index ccf4a1a..252cc0e 100644 --- a/src/main/java/tabby/core/scanner/ClassInfoScanner.java +++ b/src/main/java/tabby/core/scanner/ClassInfoScanner.java @@ -13,6 +13,7 @@ import tabby.common.bean.ref.MethodReference; import tabby.common.utils.JavaVersionUtils; import tabby.common.utils.SemanticUtils; +import tabby.config.GlobalConfiguration; import tabby.core.collector.ClassInfoCollector; import tabby.core.container.DataContainer; @@ -53,10 +54,12 @@ public void run(List paths){ public Map> loadAndExtract(List targets){ Map> results = new HashMap<>(); log.info("Start to collect {} targets' class information.", targets.size()); + Map> moduleClasses = null; - if(JavaVersionUtils.isAtLeast(9)){ + if(!GlobalConfiguration.IS_USING_SETTING_JRE){ moduleClasses = ModulePathSourceLocator.v().getClassUnderModulePath("jrt:/"); } + for (final String path : targets) { List classes = getTargetClasses(path, moduleClasses); if(classes == null) continue;