From 62b3e170dce9ff35ed4fb287961db209c41acaab Mon Sep 17 00:00:00 2001 From: onebitbool Date: Fri, 11 Aug 2023 16:55:09 +0800 Subject: [PATCH] Add: conditionLimit command line argument --- .../java/com/iscas/crashtracker/Main.java | 58 +++++++++++++------ .../com/iscas/crashtracker/base/MyConfig.java | 10 ++++ .../client/exception/ExceptionAnalyzer.java | 20 ++++++- README.md | 8 ++- scripts/runCrashTracker-framework.py | 6 +- 5 files changed, 79 insertions(+), 23 deletions(-) diff --git a/CrashTracker/src/main/java/com/iscas/crashtracker/Main.java b/CrashTracker/src/main/java/com/iscas/crashtracker/Main.java index 0584c12..db5855b 100644 --- a/CrashTracker/src/main/java/com/iscas/crashtracker/Main.java +++ b/CrashTracker/src/main/java/com/iscas/crashtracker/Main.java @@ -5,6 +5,7 @@ import com.iscas.crashtracker.client.cg.cgApk.CallGraphofApkClient; import com.iscas.crashtracker.client.crash.ApkCrashAnalysisClient; import com.iscas.crashtracker.client.crash.JarCrashAnalysisClient; +import com.iscas.crashtracker.client.exception.ConditionTracker; import com.iscas.crashtracker.client.exception.ExceptionInfoClient; import com.iscas.crashtracker.client.manifest.ManifestClient; import com.iscas.crashtracker.client.soot.IROutputClient; @@ -117,23 +118,23 @@ private static BaseClient getClient() { private static Options getOptions() { Options options = new Options(); - options.addOption("h", false, "-h: Show the help information."); + options.addOption("h", false, "Show the help information."); /** input **/ - options.addOption("name", true, "-name: Set the name of the apk under analysis."); - options.addOption("path", true, "-path: Set the path to the apk under analysis."); - options.addOption("androidJar", true, "-androidJar: Set the path of android.jar."); - options.addOption("crashPath", true, "-crashPath: crash information file."); - options.addOption("frameworkVersion", true, "-frameworkVersion: The version of framework under analysis"); - options.addOption("strategy", true, "-strategy: effectiveness of strategy m"); + options.addOption("name", true, "Set the name of the apk under analysis."); + options.addOption("path", true, "Set the path to the apk under analysis."); + options.addOption("androidJar", true, "Set the path of android.jar."); + options.addOption("crashPath", true, "crash information file."); + options.addOption("frameworkVersion", true, "The version of framework under analysis"); + options.addOption("strategy", true, "effectiveness of strategy m"); - options.addOption("exceptionPath", true, "-exceptionPath: exception file folder [optional]."); - options.addOption("androidCGPath", true, "-androidCGPath: Android CallGraph file [optional."); - options.addOption("permissionPath", true, "-permissionPath: Android permissionPath file [optional."); + options.addOption("exceptionPath", true, "exception file folder [optional]."); + options.addOption("androidCGPath", true, "Android CallGraph file [optional."); + options.addOption("permissionPath", true, "Android permissionPath file [optional."); /** analysis config **/ - options.addOption("client", true, "-client " + options.addOption("client", true, "client used.\n" + "ExceptionInfoClient: Extract exception information from Android framework.\n" + "CrashAnalysisClient: Analysis the crash information for an apk.\n" + "JarCrashAnalysisClient: Analysis the crash information for an third party SDK.\n" @@ -142,14 +143,17 @@ private static Options getOptions() { + "IROutputClient: Output soot IR files.\n" ); /** analysis config **/ - options.addOption("time", true, "-time [default:90]: Set the max running time (min)."); - options.addOption("callgraphAlgorithm", true, "-callgraphAlgorithm [default:SPARK]: Set algorithm for CG, CHA or SPARK."); + options.addOption("time", true, "[default:90]: Set the max running time (min)."); + options.addOption("callgraphAlgorithm", true, "[default:SPARK]: Set algorithm for CG, CHA or SPARK."); /** output **/ - options.addOption("outputDir", true, "-outputDir: Set the output folder of the apk."); - options.addOption("sootOutput", false, "-sootOutput: Output the sootOutput"); - options.addOption("crashInput", true, "-crashInput: crashInfo.json file path"); - options.addOption("exceptionInput", true, "-exceptionInput: exception file folder"); -// options.addOption("callgraphAlgorithm", false, "-callgraphAlgorithm: callgraphAlgorithm"); + options.addOption("outputDir", true, "Set the output folder of the apk."); + options.addOption("sootOutput", false, "Output the sootOutput"); + options.addOption("crashInput", true, "crashInfo.json file path"); + options.addOption("exceptionInput", true, "exception file folder"); + options.addOption("conditionLimit", true, "the number of conditions to be retained.\n" + + "all: keep all\n" + + "one: keep one condition\n" + + "three: keep three condition"); return options; } @@ -200,6 +204,24 @@ private static void analyzeArgs(CommandLine mCmd) { } } + String limit = mCmd.getOptionValue("conditionLimit"); + if (limit != null) { + switch(limit) { + case("all"): + MyConfig.getInstance().setConditionLimit(ConditionTracker.All); + break; + case("one"): + MyConfig.getInstance().setConditionLimit(ConditionTracker.One); + break; + case("three"): + MyConfig.getInstance().setConditionLimit(ConditionTracker.Three); + break; + default: + log.error("Invalid condition limit!"); + System.exit(0); + } + } + MyConfig.getInstance().setStrategy(mCmd.getOptionValue("strategy", "")); log.info("###The strategy is #" + MyConfig.getInstance().getStrategy()+"#"); diff --git a/CrashTracker/src/main/java/com/iscas/crashtracker/base/MyConfig.java b/CrashTracker/src/main/java/com/iscas/crashtracker/base/MyConfig.java index 4b1a0f1..70ce8d5 100644 --- a/CrashTracker/src/main/java/com/iscas/crashtracker/base/MyConfig.java +++ b/CrashTracker/src/main/java/com/iscas/crashtracker/base/MyConfig.java @@ -1,5 +1,6 @@ package com.iscas.crashtracker.base; +import com.iscas.crashtracker.client.exception.ConditionTracker; import com.iscas.crashtracker.utils.FileUtils; import soot.options.Options; @@ -34,6 +35,7 @@ public class MyConfig { private String PermissionFilePath; private String AndroidCGFilePath ; private String AndroidOSVersion = null; + private ConditionTracker conditionLimit = ConditionTracker.All; private String Strategy=""; private boolean outputIR = false; @@ -281,4 +283,12 @@ public void setOutputIR(boolean outputIR) { public boolean getOutputIR() { return outputIR; } + + public void setConditionLimit(ConditionTracker limit) { + conditionLimit = limit; + } + + public ConditionTracker getConditionLimit() { + return conditionLimit; + } } \ No newline at end of file diff --git a/CrashTracker/src/main/java/com/iscas/crashtracker/client/exception/ExceptionAnalyzer.java b/CrashTracker/src/main/java/com/iscas/crashtracker/client/exception/ExceptionAnalyzer.java index df1cbf0..2441043 100644 --- a/CrashTracker/src/main/java/com/iscas/crashtracker/client/exception/ExceptionAnalyzer.java +++ b/CrashTracker/src/main/java/com/iscas/crashtracker/client/exception/ExceptionAnalyzer.java @@ -528,7 +528,7 @@ private boolean conditionCheck(ExceptionInfo exceptionInfo, SootMethod sootMetho */ private void getExceptionCondition(SootMethod sootMethod, Unit unit, ExceptionInfo exceptionInfo, Set getCondHistory, boolean fromThrow, Unit lastGoto) { - ConditionTracker conditionTracker = ConditionTracker.All; + ConditionTracker conditionTracker = MyConfig.getInstance().getConditionLimit(); if(getCondHistory.contains(unit) || getCondHistory.size()> ConstantUtils.CONDITIONHISTORYSIZE) return;// if defUnit is not a pred of unit getCondHistory.add(unit); Body body = sootMethod.getActiveBody(); @@ -540,6 +540,16 @@ private void getExceptionCondition(SootMethod sootMethod, Unit unit, ExceptionIn boolean ifMeetTryCatch = false; for (Unit predUnit : predsOf) { if (predUnit instanceof IfStmt) { + //direct condition or multiple condition + if(conditionTracker == ConditionTracker.One){ + if(exceptionInfo.getConditionUnits().size()>0) continue; + } else if(conditionTracker == ConditionTracker.Three){ + if(exceptionInfo.getConditionUnits().size()>=3) continue; + }else if(conditionTracker == ConditionTracker.SmallBlock) { + // && ((IfStmt) predUnit).getTarget() != lastGoto + if (exceptionInfo.getConditionUnits().size() > 0 && lastGoto != null) + continue; + } exceptionInfo.getTracedUnits().add(predUnit); IfStmt ifStmt = (IfStmt) predUnit; lastGoto = ifStmt.getTarget(); @@ -579,6 +589,14 @@ private void getExceptionCondition(SootMethod sootMethod, Unit unit, ExceptionIn } } if(ifMeetTryCatch) continue; + if(conditionTracker == ConditionTracker.One){ + if(fromThrow && exceptionInfo.getConditions().size()>0 ) continue; + } else if(conditionTracker == ConditionTracker.Three){ + if(fromThrow && exceptionInfo.getConditionUnits().size()>=3) continue; + }else if(conditionTracker == ConditionTracker.SmallBlock) { + if(fromThrow && exceptionInfo.getConditions().size()>0 && gotoTargets.contains(predUnit)) + continue; + } getExceptionCondition(sootMethod, predUnit, exceptionInfo,getCondHistory, fromThrow, lastGoto); } } diff --git a/README.md b/README.md index 90d3910..805b381 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ Extracting exception-thrown summary (ETS) for that framework is required before ``` # Use the following commands to analyze your framework files. -python scripts/runCrashTracker-framework.py [framework code path] [framework code folder name] [version] [outputDir] +python scripts/runCrashTracker-framework.py [framework code path] [framework code folder name] [version] [conditionLimit] [outputDir] For example, if the sturcture of your files is as follows: +-- CrashTrackerTool @@ -137,7 +137,7 @@ For example, if the sturcture of your files is as follows: version. Also, you can download from https://github.com/hanada31/AndroidFrameworkImpl and unzip files) run: - python scripts/runCrashTracker-framework.py M_framework android2.3 2.3 ETSResults + python scripts/runCrashTracker-framework.py M_framework android2.3 2.3 all ETSResults ``` ## CrashTracker.jar -h Arguments @@ -154,6 +154,10 @@ usage: java -jar CrashTracker.jar [options] [-path] [-name] [-androidJar] [-outp CallGraphClient: Output call graph files. ManifestClient: Output manifest.xml file. IROutputClient: Output soot IR files. + -conditionLimit -conditionLimit: the number of conditions to be retained. + all: keep all + one: keep one condition + three: keep three condition -name -name: Set the name of the apk under analysis. -path -path: Set the path to the apk under analysis. -crashPath -crashInput: crash information file. diff --git a/scripts/runCrashTracker-framework.py b/scripts/runCrashTracker-framework.py index 9a4c8e4..1dc136c 100644 --- a/scripts/runCrashTracker-framework.py +++ b/scripts/runCrashTracker-framework.py @@ -22,10 +22,12 @@ def execute_cmd(cmd): path = sys.argv[1] name = sys.argv[2] version = sys.argv[3] - output = sys.argv[4] + limit = sys.argv[4] + output = sys.argv[5] command = "java -jar " + jarFile + " -path " + path + " -name " + name + " -androidJar " + sdk + \ - " -client ExceptionInfoClient" + " -outputDir " + output + " -frameworkVersion " + version + " -client ExceptionInfoClient" + " -outputDir " + output + " -frameworkVersion " + version + \ + " -conditionLimit " + limit print("Start command") execute_cmd(command) print("Execute over!")