Skip to content

Commit

Permalink
Adds *real* path to caller by finding the probable path via matching …
Browse files Browse the repository at this point in the history
…module names
  • Loading branch information
fastlag committed Feb 1, 2024
1 parent 193f6fc commit bde0f25
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions java/common/src/main/java/SootWrapper/SootWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static AnalysisResult writeAnalysis(JSONWriter jwriter, Iterable<? extend
}
analysedMethods.add(methodToAnalyse);

jwriter.value(getSignatureJSONArray(methodToAnalyse, cg));
jwriter.value(getSignatureJSONArray(methodToAnalyse, cg, pathToClassFiles));

Iterator<Edge> edgesOut = cg.edgesOutOf(methodToAnalyse);
while (edgesOut.hasNext()) {
Expand Down Expand Up @@ -106,7 +106,7 @@ public static AnalysisResult writeAnalysis(JSONWriter jwriter, Iterable<? extend
return new AnalysisResult(phantoms, badPhantoms);
}

private static JSONArray getSignatureJSONArray(SootMethod methodToAnalyse, CallGraph cg) {
private static JSONArray getSignatureJSONArray(SootMethod methodToAnalyse, CallGraph cg, Iterable<? extends Path> pathToClassFiles) {
TargetSignature targetSignature = getFormattedTargetSignature(methodToAnalyse);
JSONArray callee = new JSONArray();
callee.put(targetSignature.getMethod());
Expand All @@ -123,7 +123,11 @@ private static JSONArray getSignatureJSONArray(SootMethod methodToAnalyse, CallG
Edge e = edgesInto.next();
MethodOrMethodContext source = e.getSrc();
SootMethod sourceMethod = source instanceof MethodContext ? source.method() : (SootMethod) source;
SourceSignature sourceSignature = getFormattedSourceSignature(sourceMethod, e.srcStmt() == null ? -1 : e.srcStmt().getJavaSourceStartLineNumber());
SourceSignature sourceSignature = getFormattedSourceSignature(
sourceMethod,
e.srcStmt() == null ? -1 : e.srcStmt().getJavaSourceStartLineNumber(),
pathToClassFiles
);
JSONArray caller = new JSONArray();
caller.put(sourceSignature.getMethod());
caller.put(sourceSignature.getLineNumber());
Expand All @@ -135,13 +139,13 @@ private static JSONArray getSignatureJSONArray(SootMethod methodToAnalyse, CallG
return callee;
}

private static SourceSignature getFormattedSourceSignature(SootMethod method, int lineNumber) {
private static SourceSignature getFormattedSourceSignature(SootMethod method, int lineNumber, Iterable<? extends Path> pathToClassFiles) {
return method == null
? new SourceSignature("-", -1, "-")
: new SourceSignature(
getSignatureString(method),
lineNumber,
getProbableName(method.getDeclaringClass())
getProbableSourceName(method, pathToClassFiles)
);
}

Expand Down Expand Up @@ -174,6 +178,26 @@ private static String getSignatureString(SootMethod method) {
return sb.toString();
}

private static String getModuleString(SootMethod method) {
StringBuilder sb = new StringBuilder();
String classString = method.getDeclaringClass().toString();
boolean foundDot = false;
for (int i = 0; i < classString.length(); i++) {
char c = classString.charAt(i);
if (c != '.') {
sb.append(c);
} else {
foundDot = true;
break;
}
}
if (!foundDot) {
return "";
}
return sb.toString();
}


private static String getProbableName(SootClass c) {
if (c.isJavaLibraryClass()) {
return "-";
Expand All @@ -188,6 +212,20 @@ private static String getProbableName(SootClass c) {
return className;
}

private static String getProbableSourceName(SootMethod method, Iterable<? extends Path> pathToClassFiles) {
String moduleName = getModuleString(method);
String onlyDeclaringClassName = method.getDeclaringClass().getName().replaceFirst(moduleName + ".", "/");
if (moduleName.length() == 0) {
return "<unknown>";
}
for (Path path : pathToClassFiles) {
if (path.toString().endsWith(moduleName)) {
return path.toString() + onlyDeclaringClassName + ".java";
}
}
return "-";
}

private static String getParameterClass(Type parameter) {
String[] paramType = parameter.toString().split("\\.");
return paramType[paramType.length-1];
Expand Down

0 comments on commit bde0f25

Please sign in to comment.