Skip to content

Commit

Permalink
modify version matching strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
hanada committed Dec 14, 2023
1 parent 5ec1f26 commit 0ec95cd
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,4 @@ public String getCandidateSig() {
return candidateSig;
}

public void setCandidateSig(String candidateSig) {
this.candidateSig = candidateSig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import soot.toolkits.scalar.UnitValueBoxPair;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -34,6 +35,7 @@ public class CrashAnalysis extends Analyzer {
Map<String, Set<String>> androidCGMap;
String relatedVarType="";
String[] versions = {"2.3", "4.4", "5.0", "6.0", "7.0", "8.0", "9.0", "10.0", "11.0", "12.0"};
int[] APILevels = {10, 19, 21, 23, 24, 26, 27, 28, 29, 30};

public CrashAnalysis() {
crashInfoList = Global.v().getAppModel().getCrashInfoList();
Expand Down Expand Up @@ -103,12 +105,15 @@ private void addCrashTraces(int initscore, CrashInfo crashInfo) {
reason.put("Explanation", "Not influence the keyVar but in crash trace");
JSONArray trace = new JSONArray();
reason.put("Trace", trace);
trace.add(candi);
Set<SootMethod> methods = SootUtils.getSootMethodBySimpleName(candi);
for(SootMethod sm: methods) {
trace.add(sm.getSignature());
crashInfo.addBuggyCandidates(candi, sm.getSignature(), initscore--, reason);
break;
}
// if(methods.size()==0){
// crashInfo.addBuggyCandidates(candi, "", initscore--, reason);
// }
}
}
private void getPartOfExtendedCallTrace(CrashInfo crashInfo) {
Expand Down Expand Up @@ -142,13 +147,14 @@ private void getExtendedCallTrace(CrashInfo crashInfo) {
String candi = crashInfo.getCrashMethodList().get(index);
JSONArray trace = new JSONArray();
reason.put("Trace", trace);
trace.add(candi);
crashInfo.addExtendedCallDepth(candi, 1, reason.clone());
Set<SootMethod> methods = SootUtils.getSootMethodBySimpleName(candi);
for (SootMethod sm : methods) {
trace.add(sm.getSignature());
addEntryMethods2ExtendedCG(sm, crashInfo, reason.clone());
String last = (index == 0) ? crashInfo.getCrashAPI() : crashInfo.getCrashMethodList().get(index - 1);
addPredCallersOfMethodsInStack(last, sm, crashInfo, reason.clone());
break;
}
}
String firstAppMethod = crashInfo.getCrashMethodList().get(crashInfo.getCrashMethodList().size() - 1);
Expand Down Expand Up @@ -376,7 +382,6 @@ private String extendRelatedValues(SootMethod crashMethod, List<Unit> allPreds,
*/
private void withParameterHandler(int score, CrashInfo crashInfo, boolean hasFaultInducingParas) {
int n = 0;
//TODO!!! rerun
if(crashInfo.getExceptionInfo()!=null && crashInfo.getExceptionInfo().getRelatedVarType()!=null) {
if(crashInfo.getExceptionInfo().getCallerOfSingnlar2SourceVar()!=null){
if(hasFaultInducingParas) {
Expand Down Expand Up @@ -459,7 +464,7 @@ private void getCrashAPIInvocationRelatedMethod(CrashInfo crashInfo, int score)
JSONArray trace = new JSONArray();
reason.put("Trace", trace);
trace.add(crashInfo.getCrashAPI());
trace.add(callee);
trace.add(method.getSignature());
crashInfo.addBuggyCandidates(callee, method.getSignature(), score, reason);
}
}
Expand All @@ -486,6 +491,9 @@ private void addExtendedCallGraph(CrashInfo crashInfo) {
crashInfo.addBuggyCandidates(candi, sm.getSignature(), score, reason);
break;
}
// if(methods.size()==0){
// crashInfo.addBuggyCandidates(candi, "", score, reason);
// }
}
}
//according to the parameter send into framework API
Expand Down Expand Up @@ -531,13 +539,12 @@ private void findMethodsWhoModifyParamWapper(int score, CrashInfo crashInfo, Soo
}
}

private void findMethodsWhoModifyParam(int score, CrashInfo crashInfo, SootMethod crashMethod, Unit unit, Value faultInducingValue) {
private void findMethodsWhoModifyParam(int score, CrashInfo crashInfo, SootMethod crashMethod, Unit unit, Value faultInducingValue){
List<Unit> allPreds = new ArrayList<>();
SootUtils.getAllPredsofUnit(crashMethod, unit,allPreds);
HashSet<SootField> fields = new HashSet<>();
extendRelatedValues(crashMethod, allPreds, unit, faultInducingValue, new ArrayList<>(), fields);

List<SootField> keyFields = getKeySootFields(crashMethod, crashInfo, faultInducingValue);
List<SootField> keyFields = getKeySootFields(crashMethod, crashInfo, faultInducingValue);
for (SootField field : fields) {
if(keyFields!=null && !keyFields.contains(field))
continue;
Expand Down Expand Up @@ -630,7 +637,6 @@ private int getParameterTerminateMethod(int score, CrashInfo crashInfo, List<Str
reason.put("Influenced method", crashInfo.getSignaler());
JSONArray trace = new JSONArray();
reason.put("Trace", trace);

if(signature != null){
trace.add(signature);
}else{
Expand Down Expand Up @@ -694,12 +700,15 @@ private void noParameterPassingMethodScore(int initScore, CrashInfo crashInfo) {
reason.put("Influenced method", crashInfo.getSignaler());
JSONArray trace = new JSONArray();
reason.put("Trace", trace);
trace.add(candi);
Set<SootMethod> methods = SootUtils.getSootMethodBySimpleName(candi);
for(SootMethod sm: methods) {
trace.add(sm.getSignature());
crashInfo.addBuggyCandidates(candi, sm.getSignature(), initScore--, reason);
break;
}
// if(methods.size()==0){
// crashInfo.addBuggyCandidates(candi, "", initScore--, reason);
// }

for(SootMethod sm: methods) {
if (sm == null) continue;
Expand Down Expand Up @@ -981,7 +990,7 @@ private void overrideMissingHandler(int score, CrashInfo crashInfo) {
sootMethod = sm;
}
}
if(sootMethod!=null) {
if(sootMethod==null) {
String candi = sub.getName() + "." + crashInfo.getSubMethodName();
int updateScore = score - getOrderInTrace(crashInfo, candi);

Expand All @@ -991,7 +1000,7 @@ private void overrideMissingHandler(int score, CrashInfo crashInfo) {
JSONArray trace = new JSONArray();
reason.put("Trace", trace);
trace.add(crashInfo.getMethodName());
crashInfo.addBuggyCandidates(candi, sootMethod.getSignature(),updateScore, reason);
crashInfo.addBuggyCandidates(candi, "",updateScore, reason);
}
}
}
Expand Down Expand Up @@ -1019,6 +1028,60 @@ private int getOrderInTrace(CrashInfo crashInfo, String candi) {
}

public void getExceptionOfCrashInfo() {
log.info("getExceptionOfCrashInfo...");
for(CrashInfo crashInfo: this.crashInfoList) {
if(crashInfo.getTrace().size()==0 ) continue;
String targetVer;
String targetMethodName;
if(MyConfig.getInstance().getAndroidOSVersion()==null) {
String str = crashInfo.getId() + "\t" + crashInfo.getSignaler() + "\t";
String[] versionTypes = new String[versions.length];
String[] versionTypeCandis = new String[versions.length];
String[] targetMethodNames = new String[versions.length];
int i = -1;
int targetVerId = 0;
int minDistance2TargetAPI = 100;
for (String version : versions) {
i++;
Pair<String, String> pair = getExceptionWithGivenVersion(crashInfo, version, true);
versionTypes[i] = pair.getO1();
targetMethodNames[i] = pair.getO2();
if (versionTypes[i].equals("notFound")) {
Pair<String, String> pair2 = getExceptionWithGivenVersion(crashInfo, version, false);
versionTypeCandis[i] = pair2.getO1();
targetMethodNames[i] = pair2.getO2();
}else if (versionTypes[i].equals("noFile")){
log.info("version "+ versions[i] +" not exist.");
}else{
if(APILevels[i]>=appModel.getMinSdkVersion()){
if(Math.abs(appModel.getTargetSdkVersion()-APILevels[i])<minDistance2TargetAPI){
minDistance2TargetAPI = Math.abs(appModel.getTargetSdkVersion()-APILevels[i]);
targetVerId = i;
}
}
}
}
targetVer = versions[targetVerId];
targetMethodName = targetMethodNames[targetVerId];
log.info("versionTypes: "+PrintUtils.printArray(versionTypes));
log.info("versionTypeCandis: "+PrintUtils.printArray(versionTypeCandis));
log.info("version "+ versions[targetVerId] +" is matched.");
}else{
targetVer = MyConfig.getInstance().getAndroidOSVersion();
targetMethodName = crashInfo.getSignaler();
}
String androidFolder = MyConfig.getInstance().getExceptionFolderPath()+File.separator+"android"+targetVer+File.separator;
MyConfig.getInstance().setExceptionFilePath(androidFolder+"exceptionInfo"+File.separator);
MyConfig.getInstance().setPermissionFilePath(androidFolder+"Permission"+File.separator+"permission.txt");
MyConfig.getInstance().setAndroidCGFilePath(androidFolder+"CallGraphInfo"+File.separator+"android"+targetVer+"_cg.txt");
log.info("target is "+ targetVer);
MyConfig.getInstance().setTargetVersion(targetVer);
readExceptionSummary(crashInfo, targetMethodName);
}
}


public void getExceptionOfCrashInfo_Back() {
log.info("getExceptionOfCrashInfo...");
for(CrashInfo crashInfo: this.crashInfoList) {
if(crashInfo.getTrace().size()==0 ) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.iscas.crashtracker.utils.SootUtils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

Expand Down Expand Up @@ -52,7 +53,7 @@ public void writeToJson(String path, List<CrashInfo> crashInfoList) {
SerializerFeature.SortField);
printWriter.write(jsonString);
printWriter.close();
} catch (Exception e) {
} catch (IOException e) {
e.printStackTrace();
}
}
Expand All @@ -67,7 +68,8 @@ private void addBasicInDataset(JSONObject jsonObject, CrashInfo crashInfo) {
}
jsonObject.put("stack trace" , traceArray);
jsonObject.put("Labeled Buggy Method", crashInfo.getReal());

jsonObject.put("Manifest targetSdkVersion",Global.v().getAppModel().getTargetSdkVersion());
jsonObject.put("Manifest minSdkVersion",Global.v().getAppModel().getMinSdkVersion());
// jsonObject.put("Labeled Buggy API", crashInfo.getBuggyApi());
// jsonObject.put("labeledCategory", crashInfo.getCategory());
// jsonObject.put("labeledReason", crashInfo.getReason());
Expand Down Expand Up @@ -153,10 +155,10 @@ private void addResultsByTool(JSONObject jsonObject, CrashInfo crashInfo) {
workList.add(bc.getCandidateSig());
}

for(int i =0; i<5; i++) {
for(int i =0; i<3; i++) {
List<String> addedInThisStep = new ArrayList<>();
for (String ref : workList) {
updateRefList(ref, refMethodList, refFieldList, addedInThisStep,refToInvokeStack);
updateRefList(crashInfo,ref, refMethodList, refFieldList, addedInThisStep,refToInvokeStack);
}
workList = addedInThisStep;
}
Expand All @@ -165,15 +167,16 @@ private void addResultsByTool(JSONObject jsonObject, CrashInfo crashInfo) {
jsonObject.put("Reference Field List" , refFieldList);


JSONArray noneCodeLabelArray = new JSONArray();
for (String label: crashInfo.getNoneCodeLabel()) {
noneCodeLabelArray.add(label);
}
jsonObject.put("None-Code Labels" , noneCodeLabelArray);
// JSONArray noneCodeLabelArray = new JSONArray();
// for (String label: crashInfo.getNoneCodeLabel( )) {
// noneCodeLabelArray.add(label);
// }
// Object put = jsonObject.put("None-Code Labels", noneCodeLabelArray);
}

private void updateRefList(String candidateSig, JSONArray refMethodList, JSONArray refFieldList, List<String> addedInThisStep, Map<String, String> refToInvokeStack) {
for(String usedMethod: SootUtils.getUsedMethodList(candidateSig)){
private void updateRefList(CrashInfo crashInfo, String candidateSig, JSONArray refMethodList, JSONArray refFieldList, List<String> addedInThisStep, Map<String, String> refToInvokeStack) {

for(String usedMethod: SootUtils.getUsedMethodList(crashInfo, candidateSig)){
String historyStack = "";
if(refMethodList.contains(usedMethod))continue;
if(refToInvokeStack.containsKey(candidateSig)){
Expand All @@ -183,7 +186,7 @@ private void updateRefList(String candidateSig, JSONArray refMethodList, JSONArr
refToInvokeStack.put(usedMethod, historyStack +candidateSig);
addedInThisStep.add(usedMethod);
}
for(String usedField: SootUtils.getUsedFieldList(candidateSig)){
for(String usedField: SootUtils.getUsedFieldList(crashInfo, candidateSig)){
String historyStack = "";
if(refToInvokeStack.containsKey(candidateSig)){
historyStack = refToInvokeStack.get(candidateSig) +" --> ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public void analyze() {
appModel.getExtendedPakgs().add(pkg);
appModel.setVersionCode(manifestManager.getVersionCode());
appModel.setUsesPermissionSet(manifestManager.getPermissions());

appModel.setUsesPermissionSet(manifestManager.getPermissions());
appModel.setUsesPermissionSet(manifestManager.getPermissions());
appModel.setTargetSdkVersion(manifestManager.getTargetSdkVersion());
appModel.setMinSdkVersion(manifestManager.getMinSdkVersion());
// -------------------------------------------------------------------------------------------------------------
// BUGFIX: 09/06/2022 LightningRS
// -------------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class AppModel implements Serializable {
private String permission;
private Set<String> usesPermissionSet;
private Set<String> applicationClassNames;
private int targetSdkVersion;

private int minSdkVersion;

// call graph related
private final Set<SootMethod> allMethods;
Expand Down Expand Up @@ -387,5 +389,20 @@ public Set<SootMethod> getEntryMethods() {
}


public int getTargetSdkVersion() {
return targetSdkVersion;
}

public void setTargetSdkVersion(int targetSdkVersion) {
this.targetSdkVersion = targetSdkVersion;
}

public int getMinSdkVersion() {
return minSdkVersion;
}

public void setMinSdkVersion(int minSdkVersion) {
this.minSdkVersion = minSdkVersion;
}

}
Loading

0 comments on commit 0ec95cd

Please sign in to comment.