Skip to content

Commit 1f920bc

Browse files
authored
Merge pull request #67 from debricked/add-path-to-caller
Add path to caller
2 parents 63aefbe + bde0f25 commit 1f920bc

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@
55
This repo analyses your code to determine what parts of your dependencies you use, and stores this in a file which can be picked up debricked.
66

77
This, combined with our information about what parts of dependencies are affected by CVEs, allows us to determine whether you use the parts of a dependency affected by a vulnerability, or if its safe to continue using the dependency in spite of the vulnerability.
8+
9+
10+
## Setup
11+
12+
Go to common java directory: `cd java/common/`
13+
14+
Build SootWrapper: `mvn clean package -X -DskipTests`
15+
16+
You will now have jar-file in the target directory: `java/common/target`.

java/common/src/main/java/SootWrapper/SootWrapper.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static AnalysisResult writeAnalysis(JSONWriter jwriter, Iterable<? extend
6868
}
6969
analysedMethods.add(methodToAnalyse);
7070

71-
jwriter.value(getSignatureJSONArray(methodToAnalyse, cg));
71+
jwriter.value(getSignatureJSONArray(methodToAnalyse, cg, pathToClassFiles));
7272

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

109-
private static JSONArray getSignatureJSONArray(SootMethod methodToAnalyse, CallGraph cg) {
109+
private static JSONArray getSignatureJSONArray(SootMethod methodToAnalyse, CallGraph cg, Iterable<? extends Path> pathToClassFiles) {
110110
TargetSignature targetSignature = getFormattedTargetSignature(methodToAnalyse);
111111
JSONArray callee = new JSONArray();
112112
callee.put(targetSignature.getMethod());
@@ -123,21 +123,30 @@ private static JSONArray getSignatureJSONArray(SootMethod methodToAnalyse, CallG
123123
Edge e = edgesInto.next();
124124
MethodOrMethodContext source = e.getSrc();
125125
SootMethod sourceMethod = source instanceof MethodContext ? source.method() : (SootMethod) source;
126-
SourceSignature sourceSignature = getFormattedSourceSignature(sourceMethod, e.srcStmt() == null ? -1 : e.srcStmt().getJavaSourceStartLineNumber());
126+
SourceSignature sourceSignature = getFormattedSourceSignature(
127+
sourceMethod,
128+
e.srcStmt() == null ? -1 : e.srcStmt().getJavaSourceStartLineNumber(),
129+
pathToClassFiles
130+
);
127131
JSONArray caller = new JSONArray();
128132
caller.put(sourceSignature.getMethod());
129133
caller.put(sourceSignature.getLineNumber());
134+
caller.put(sourceSignature.getFileName());
130135
callers.put(caller);
131136
}
132137
callee.put(callers);
133138

134139
return callee;
135140
}
136141

137-
private static SourceSignature getFormattedSourceSignature(SootMethod method, int lineNumber) {
142+
private static SourceSignature getFormattedSourceSignature(SootMethod method, int lineNumber, Iterable<? extends Path> pathToClassFiles) {
138143
return method == null
139-
? new SourceSignature("-", -1)
140-
: new SourceSignature(getSignatureString(method), lineNumber);
144+
? new SourceSignature("-", -1, "-")
145+
: new SourceSignature(
146+
getSignatureString(method),
147+
lineNumber,
148+
getProbableSourceName(method, pathToClassFiles)
149+
);
141150
}
142151

143152
private static TargetSignature getFormattedTargetSignature(SootMethod method) {
@@ -169,6 +178,26 @@ private static String getSignatureString(SootMethod method) {
169178
return sb.toString();
170179
}
171180

181+
private static String getModuleString(SootMethod method) {
182+
StringBuilder sb = new StringBuilder();
183+
String classString = method.getDeclaringClass().toString();
184+
boolean foundDot = false;
185+
for (int i = 0; i < classString.length(); i++) {
186+
char c = classString.charAt(i);
187+
if (c != '.') {
188+
sb.append(c);
189+
} else {
190+
foundDot = true;
191+
break;
192+
}
193+
}
194+
if (!foundDot) {
195+
return "";
196+
}
197+
return sb.toString();
198+
}
199+
200+
172201
private static String getProbableName(SootClass c) {
173202
if (c.isJavaLibraryClass()) {
174203
return "-";
@@ -183,6 +212,20 @@ private static String getProbableName(SootClass c) {
183212
return className;
184213
}
185214

215+
private static String getProbableSourceName(SootMethod method, Iterable<? extends Path> pathToClassFiles) {
216+
String moduleName = getModuleString(method);
217+
String onlyDeclaringClassName = method.getDeclaringClass().getName().replaceFirst(moduleName + ".", "/");
218+
if (moduleName.length() == 0) {
219+
return "<unknown>";
220+
}
221+
for (Path path : pathToClassFiles) {
222+
if (path.toString().endsWith(moduleName)) {
223+
return path.toString() + onlyDeclaringClassName + ".java";
224+
}
225+
}
226+
return "-";
227+
}
228+
186229
private static String getParameterClass(Type parameter) {
187230
String[] paramType = parameter.toString().split("\\.");
188231
return paramType[paramType.length-1];

java/common/src/main/java/SootWrapper/SourceSignature.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ public class SourceSignature {
55

66
private final int lineNumber;
77

8-
public SourceSignature(String method, int lineNumber) {
8+
private final String fileName;
9+
10+
public SourceSignature(String method, int lineNumber, String fileName) {
911
this.method = method;
1012
this.lineNumber = lineNumber;
13+
this.fileName = fileName;
1114
}
1215

1316
public String getMethod() {
@@ -17,4 +20,8 @@ public String getMethod() {
1720
public int getLineNumber() {
1821
return lineNumber;
1922
}
23+
24+
public String getFileName() {
25+
return fileName;
26+
}
2027
}

java/common/src/test/java/SootWrapper/SootWrapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private static Map<TargetSignature, Set<SourceSignature>> getCallGraphMap(
316316
JSONArray sourcesJSON = theEntry.getJSONArray(7);
317317
for (int j = 0; j < sourcesJSON.length(); j++) {
318318
JSONArray theSource = sourcesJSON.getJSONArray(j);
319-
sources.add(new SourceSignature(theSource.getString(0), theSource.getInt(1)));
319+
sources.add(new SourceSignature(theSource.getString(0), theSource.getInt(1), "-"));
320320
}
321321
calls.put(tar, sources);
322322
}

0 commit comments

Comments
 (0)