Skip to content

Commit

Permalink
Merge pull request #19 from IBM/analysis-json-out-reformatted
Browse files Browse the repository at this point in the history
Analysis json out reformatted
  • Loading branch information
rahlk authored May 17, 2024
2 parents 3ec824a + 6d296b4 commit ae8f815
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 50 deletions.
Empty file added output.json
Empty file.
14 changes: 3 additions & 11 deletions src/main/java/com/ibm/northstar/CodeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* The type Code analyzer.
*/
@Command(name = "codeanalyzer", mixinStandardHelpOptions = true, sortOptions = false, version = "codeanalyzer v1.1", description = "Convert java binary (*.jar, *.ear, *.war) into a comprehensive system dependency graph.")
@Command(name = "codeanalyzer", mixinStandardHelpOptions = true, sortOptions = false, version = "codeanalyzer v1.1", description = "Convert java binary into a comprehensive system dependency graph.")
public class CodeAnalyzer implements Runnable {

@Option(names = {"-i", "--input"}, description = "Path to the project root directory.")
Expand All @@ -58,7 +58,7 @@ public class CodeAnalyzer implements Runnable {
@Option(names = {"--no-build"}, description = "Do not build your application. Use this option if you have already built your application.")
private static boolean noBuild = false;

@Option(names = {"-a", "--analysis-level"}, description = "Level of analysis to perform. Options: 1 (for just symbol table) or 2 (for full analysis including the system depenedency graph). Default: 1")
@Option(names = {"-a", "--analysis-level"}, description = "Level of analysis to perform. Options: 1 (for just symbol table) or 2 (for call graph). Default: 1")
private static int analysisLevel = 1;

@Option(names = {"-d", "--dependencies"}, description = "Path to the application 3rd party dependencies that may be helpful in analyzing the application.")
Expand Down Expand Up @@ -158,15 +158,7 @@ private static void analyze() throws IOException, ClassHierarchyException, CallG

private static void emit(String consolidatedJSONString) throws IOException {
if (output == null) {
byte[] bytes = consolidatedJSONString.getBytes(StandardCharsets.UTF_8);
// Create the GZIPOutputStream, using System.out
GZIPOutputStream gzipOS = new GZIPOutputStream(System.out);
// Write the byte array to the GZIPOutputStream
gzipOS.write(bytes);
// Flush the GZIPOutputStream
gzipOS.flush();
// Close the GZIPOutputStream
gzipOS.close();
System.out.println(consolidatedJSONString);
} else {
// If output is not null, export to a file
File file = new File(output, "analysis.json");
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/com/ibm/northstar/SystemDependencyGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import com.ibm.wala.cast.ir.ssa.AstIRFactory;
import com.ibm.wala.cast.java.translator.jdt.ecj.ECJClassLoaderFactory;
import com.ibm.wala.classLoader.CallSiteReference;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.ipa.callgraph.*;
import com.ibm.wala.ipa.callgraph.AnalysisOptions.ReflectionOptions;
import com.ibm.wala.ipa.callgraph.impl.Util;
Expand All @@ -37,7 +35,6 @@
import com.ibm.wala.ipa.slicer.SDG;
import com.ibm.wala.ipa.slicer.Slicer;
import com.ibm.wala.ipa.slicer.Statement;
import com.ibm.wala.ssa.IR;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.util.collections.HashMapFactory;
import com.ibm.wala.util.graph.Graph;
Expand Down Expand Up @@ -79,7 +76,6 @@ private static JSONExporter<Pair<String, Callable>, AbstractGraphEdge> getGraphE
return gson.toJson(vertex);
}
);
// exporter.setVertexAttributeProvider(v -> v.getRight().getAttributes());
exporter.setEdgeAttributeProvider(AbstractGraphEdge::getAttributes);
return exporter;
}
Expand Down Expand Up @@ -155,41 +151,6 @@ private static org.jgrapht.Graph<Pair<String, Callable>, AbstractGraphEdge> buil
}
}
}));

callGraph.getEntrypointNodes()
.forEach(p -> {
// Get call statements that may execute in a given method
Iterator<CallSiteReference> outGoingCalls = p.iterateCallSites();
outGoingCalls.forEachRemaining(n -> {
callGraph.getPossibleTargets(p, n).stream()
.filter(o -> AnalysisUtils.isApplicationClass(o.getMethod().getDeclaringClass()))
.forEach(o -> {

// Add the source nodes to the graph as vertices
Pair<String, Callable> source = Optional.ofNullable(getCallableFromSymbolTable(p.getMethod())).orElseGet(() -> createAndPutNewCallableInSymbolTable(p.getMethod()));
graph.addVertex(source);

// Add the target nodes to the graph as vertices
Pair<String, Callable> target = Optional.ofNullable(getCallableFromSymbolTable(o.getMethod())).orElseGet(() -> createAndPutNewCallableInSymbolTable(o.getMethod()));
graph.addVertex(target);

if (!source.equals(target) && source.getRight() != null && target.getRight() != null) {

// Get the edge between the source and the target
AbstractGraphEdge cgEdge = graph.getEdge(source, target);

if (cgEdge == null) {
graph.addEdge(source, target, new CallEdge());
}
// If edge exists, then increment the weight
else {
cgEdge.incrementWeight();
}
}
});
});
});

return graph;
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/ibm/northstar/utils/ScopeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public static AnalysisScope createScope(String projectPath, String applicationDe
addDefaultExclusions(scope);

Log.info("Loading Java SE standard libs.");

if (System.getenv("JAVA_HOME") == null) {
Log.error("JAVA_HOME is not set.");
throw new RuntimeException("JAVA_HOME is not set.");
}

String[] stdlibs = Files.walk(Paths.get(System.getenv("JAVA_HOME"), "jmods"))
.filter(path -> path.toString().endsWith(".jmod"))
.map(path -> path.toAbsolutePath().toString())
Expand Down

0 comments on commit ae8f815

Please sign in to comment.