From 16c6c232a2450db9af7bcb5b57c505c178e48304 Mon Sep 17 00:00:00 2001 From: Rahul Krishna Date: Wed, 15 May 2024 13:10:00 -0400 Subject: [PATCH 1/2] Enable analysis over a single file provided as a string to codeanalyzer. Signed-off-by: Rahul Krishna --- output.json | 0 src/main/java/com/ibm/northstar/CodeAnalyzer.java | 12 ++---------- 2 files changed, 2 insertions(+), 10 deletions(-) create mode 100644 output.json diff --git a/output.json b/output.json new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/com/ibm/northstar/CodeAnalyzer.java b/src/main/java/com/ibm/northstar/CodeAnalyzer.java index 1049997e..bda5a0c4 100644 --- a/src/main/java/com/ibm/northstar/CodeAnalyzer.java +++ b/src/main/java/com/ibm/northstar/CodeAnalyzer.java @@ -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.") @@ -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"); From 83f042c984a33b26d7d8e493a1774b9b93bbcd89 Mon Sep 17 00:00:00 2001 From: Rahul Krishna Date: Fri, 17 May 2024 15:49:12 -0400 Subject: [PATCH 2/2] Remove a separate field for CALL_DEP Signed-off-by: Rahul Krishna --- .../java/com/ibm/northstar/CodeAnalyzer.java | 2 +- .../ibm/northstar/SystemDependencyGraph.java | 39 ------------------- .../com/ibm/northstar/utils/ScopeUtils.java | 6 +++ 3 files changed, 7 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/ibm/northstar/CodeAnalyzer.java b/src/main/java/com/ibm/northstar/CodeAnalyzer.java index bda5a0c4..ee691069 100644 --- a/src/main/java/com/ibm/northstar/CodeAnalyzer.java +++ b/src/main/java/com/ibm/northstar/CodeAnalyzer.java @@ -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.") diff --git a/src/main/java/com/ibm/northstar/SystemDependencyGraph.java b/src/main/java/com/ibm/northstar/SystemDependencyGraph.java index 276c63b7..48c99adb 100644 --- a/src/main/java/com/ibm/northstar/SystemDependencyGraph.java +++ b/src/main/java/com/ibm/northstar/SystemDependencyGraph.java @@ -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; @@ -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; @@ -79,7 +76,6 @@ private static JSONExporter, AbstractGraphEdge> getGraphE return gson.toJson(vertex); } ); -// exporter.setVertexAttributeProvider(v -> v.getRight().getAttributes()); exporter.setEdgeAttributeProvider(AbstractGraphEdge::getAttributes); return exporter; } @@ -155,41 +151,6 @@ private static org.jgrapht.Graph, AbstractGraphEdge> buil } } })); - - callGraph.getEntrypointNodes() - .forEach(p -> { - // Get call statements that may execute in a given method - Iterator 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 source = Optional.ofNullable(getCallableFromSymbolTable(p.getMethod())).orElseGet(() -> createAndPutNewCallableInSymbolTable(p.getMethod())); - graph.addVertex(source); - - // Add the target nodes to the graph as vertices - Pair 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; } diff --git a/src/main/java/com/ibm/northstar/utils/ScopeUtils.java b/src/main/java/com/ibm/northstar/utils/ScopeUtils.java index 242e55e0..5005630a 100644 --- a/src/main/java/com/ibm/northstar/utils/ScopeUtils.java +++ b/src/main/java/com/ibm/northstar/utils/ScopeUtils.java @@ -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())