Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CallgraphExample/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions CallgraphExample/.idea/runConfigurations/Run_CallgraphExample.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions CallgraphExample/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# SootUp-Examples
Example code to help getting start with SootUp

## How to install SootUp 2.0.0
```
git clone https://github.com/soot-oss/SootUp
mvn install
```
This installs SootUp 2.0.0-SNAPSHOT version to your local Maven Repository
70 changes: 48 additions & 22 deletions CallgraphExample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,68 @@
<parent>
<groupId>org.soot-oss</groupId>
<artifactId>sootup</artifactId>
<version>1.1.2-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.callgraph</artifactId>
<version>1.1.2-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.core</artifactId>
<version>1.1.2-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.bytecode</artifactId>
<version>1.1.2-SNAPSHOT</version>
<artifactId>sootup.java.bytecode.frontend</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.core</artifactId>
<version>1.1.2-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.sourcecode</artifactId>
<version>1.1.2-SNAPSHOT</version>
<artifactId>sootup.java.sourcecode.frontend</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.jimple.parser</artifactId>
<version>1.1.2-SNAPSHOT</version>
<artifactId>sootup.jimple.frontend</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.qilin</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.interceptors</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.analysis.interprocedural</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.analysis.intraprocedural</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.sourcecode</artifactId>
<version>1.1.2-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--Logging Framework Provider-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.14</version>
</dependency>
</dependencies>

<build>
<plugins>
Expand All @@ -66,6 +81,17 @@
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/test/resources/Callgraph/source</compileSourceRoot>
</compileSourceRoots>
<outputDirectory>${project.basedir}/src/test/resources/Callgraph/binary</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,109 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;

import qilin.CoreConfig;
import qilin.core.PTA;
import qilin.driver.PTAFactory;
import qilin.driver.PTAPattern;
import sootup.callgraph.CallGraph;
import sootup.callgraph.CallGraphAlgorithm;
import sootup.callgraph.ClassHierarchyAnalysisAlgorithm;
import sootup.callgraph.RapidTypeAnalysisAlgorithm;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.signatures.MethodSignature;
import sootup.core.typehierarchy.ViewTypeHierarchy;
import sootup.core.types.ClassType;
import sootup.core.types.VoidType;
import sootup.java.bytecode.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.core.types.PrimitiveType;

import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
//import sootup.java.core.JavaProject;
import sootup.java.core.JavaSootClass;
import sootup.java.core.language.JavaLanguage;
import sootup.java.core.views.JavaView;

import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.Level;

public class CallgraphExample {
static {
// Set logger level programmatically
Logger rootLogger = (Logger) LoggerFactory.getLogger("sootup");
rootLogger.setLevel(Level.ERROR);

Logger callgraphLogger = (Logger) LoggerFactory.getLogger("sootup.callgraph");
callgraphLogger.setLevel(Level.ERROR);

Logger typeHierarchyLogger = (Logger) LoggerFactory.getLogger("sootup.core.typehierarchy");
typeHierarchyLogger.setLevel(Level.ERROR);
}

public static void main(String[] args) {
// CoreConfig config = new CoreConfig();
// System.out.println(config.getPtaConfig());
// Create a AnalysisInputLocation, which points to a directory. All class files will be loaded
// from the directory
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(
new JavaClassPathAnalysisInputLocation("src/test/resources/Callgraph/binary"));
inputLocations.add(
new JavaClassPathAnalysisInputLocation(
System.getProperty("java.home") + "/lib/rt.jar")); // add rt.jar

JavaView view = new JavaView(inputLocations);

// Get a MethodSignature
ClassType classTypeA = view.getIdentifierFactory().getClassType("A");
ClassType classTypeB = view.getIdentifierFactory().getClassType("B");
ClassType classTypeC = view.getIdentifierFactory().getClassType("C");
MethodSignature entryMethodSignature =
JavaIdentifierFactory.getInstance()
.getMethodSignature(
classTypeB,
classTypeC,
JavaIdentifierFactory.getInstance()
.getMethodSubSignature(
"calc", VoidType.getInstance(), Collections.singletonList(classTypeA)));
"calc",
PrimitiveType.IntType.getInstance(),
Collections.singletonList(PrimitiveType.IntType.getInstance())
)
);

// // Create type hierarchy and CHA
// final ViewTypeHierarchy typeHierarchy = new ViewTypeHierarchy(view);
// typeHierarchy.subclassesOf(classTypeA).forEach(System.out::println);

// Create type hierarchy and CHA
final ViewTypeHierarchy typeHierarchy = new ViewTypeHierarchy(view);
System.out.println(typeHierarchy.subclassesOf(classTypeA));
CallGraphAlgorithm cha = new ClassHierarchyAnalysisAlgorithm(view);

// Create CG by initializing CHA with entry method(s)
CallGraph cg = cha.initialize(Collections.singletonList(entryMethodSignature));
System.out.println("-------------CHA---------------");
CallGraphAlgorithm cha = new ClassHierarchyAnalysisAlgorithm(view);
CallGraph cg1 = cha.initialize(Collections.singletonList(entryMethodSignature));
// cg1.callsFrom(entryMethodSignature).forEach(System.out::println);
recursivePrint(entryMethodSignature, cg1, new HashSet<>(), "");

cg.callsFrom(entryMethodSignature).forEach(System.out::println);
// Create CG by using RTA with entry method(s)
System.out.println("-------------RTA---------------");
CallGraphAlgorithm rta = new RapidTypeAnalysisAlgorithm(view);
CallGraph cg2 = rta.initialize(Collections.singletonList(entryMethodSignature));
// cg2.callsFrom(entryMethodSignature).forEach(System.out::println);
recursivePrint(entryMethodSignature, cg2, new HashSet<>(), "");

// // Create CG by using QPA with entry method(s)
// System.out.println("-------------PTA---------------");
// PTAPattern pattern = new PTAPattern("insens");
// PTA pta = PTAFactory.createPTA(pattern, view, "C");
// pta.run();
// CallGraph cg3 = pta.getCallGraph();
// cg3.callsFrom(entryMethodSignature).forEach(System.out::println);
}

public static void recursivePrint(MethodSignature m, CallGraph cg, HashSet<Integer> visited, String indent) {
// Base case: if the method has already been visited, return to avoid infinite recursion
if (visited.contains(m.hashCode())) {
return;
}
// Pretty print the node
System.out.println(indent + " -> " + m);

// Mark the method as visited
visited.add(m.hashCode());

// Pretty print the children with indentation
cg.callTargetsFrom(m).forEach(child -> recursivePrint(child, cg, visited, indent + " "));
}
}
Binary file modified CallgraphExample/src/test/resources/Callgraph/binary/A.class
Binary file not shown.
Binary file modified CallgraphExample/src/test/resources/Callgraph/binary/B.class
Binary file not shown.
Binary file modified CallgraphExample/src/test/resources/Callgraph/binary/C.class
Binary file not shown.
Binary file modified CallgraphExample/src/test/resources/Callgraph/binary/D.class
Binary file not shown.
3 changes: 3 additions & 0 deletions CallgraphExample/src/test/resources/Callgraph/source/A.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ public class A {
public int calc(int a) {
return a + 3;
}
public void bar() {
System.out.println("Bar");
}
}
6 changes: 6 additions & 0 deletions CallgraphExample/src/test/resources/Callgraph/source/B.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ public class B {
public void calc(A a) {
a.calc(1); // can be A.calc, B.calc, C.calc
}
public void foo(int x) {
// conditional instantiation check
A a = new A();
a.bar();
System.out.println("Foo");
}
}
7 changes: 7 additions & 0 deletions CallgraphExample/src/test/resources/Callgraph/source/C.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
public class C extends A {
public int calc(int a) {
B b = new B();
b.foo(a);
return a + 2;
}

public static void main(String[] args) {
C c = new C();
c.calc(1);
}
}