Skip to content

Commit

Permalink
jre cached disabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1t3p1g committed Feb 1, 2024
1 parent db1e1a8 commit aa022d9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 25 deletions.
6 changes: 4 additions & 2 deletions config/settings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/tabby/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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(){
Expand Down
39 changes: 30 additions & 9 deletions src/main/java/tabby/common/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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!");
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/tabby/config/GlobalConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)){
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/tabby/core/Analyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public void runSootAnalysis(Map<String, String> targets, List<String> 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();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/tabby/core/collector/FileCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Map<String, String> collectJdkDependencies() throws IOException {
Map<String, String> dependencies = new HashMap<>();

Set<String> 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()){
Expand All @@ -67,9 +67,9 @@ public Map<String, String> 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{
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/tabby/core/scanner/ClassInfoScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,10 +54,12 @@ public void run(List<String> paths){
public Map<String, CompletableFuture<ClassReference>> loadAndExtract(List<String> targets){
Map<String, CompletableFuture<ClassReference>> results = new HashMap<>();
log.info("Start to collect {} targets' class information.", targets.size());

Map<String, List<String>> moduleClasses = null;
if(JavaVersionUtils.isAtLeast(9)){
if(!GlobalConfiguration.IS_USING_SETTING_JRE){
moduleClasses = ModulePathSourceLocator.v().getClassUnderModulePath("jrt:/");
}

for (final String path : targets) {
List<String> classes = getTargetClasses(path, moduleClasses);
if(classes == null) continue;
Expand Down

0 comments on commit aa022d9

Please sign in to comment.