Skip to content

Commit

Permalink
Merge branch 'develop' into feat/parameter_count
Browse files Browse the repository at this point in the history
  • Loading branch information
stschott authored Jan 6, 2025
2 parents 638b871 + d86d5bb commit 13be056
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/docguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
this enables that tutorial code can be tested and will fail if its not up to date anymore :)

```
{{ include('basicSetup/BasicSetup.java')}}
{{ include('basicSetup/BasicSetupTest.java')}}
```
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,12 @@
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<version>9.6</version>
<version>9.7.1</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>9.7</version>
<version>9.7.1</version>
</dependency>
<dependency>
<groupId>org.smali</groupId>
Expand Down Expand Up @@ -441,16 +441,22 @@
</dependency>

<!-- testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
23 changes: 10 additions & 13 deletions sootup.core/src/main/java/sootup/core/model/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair;
import sootup.core.graph.MutableBlockStmtGraph;
import sootup.core.graph.MutableStmtGraph;
import sootup.core.graph.StmtGraph;
Expand Down Expand Up @@ -497,21 +499,16 @@ public static Map<LValue, Collection<Stmt>> collectDefs(Collection<Stmt> stmts)
}

/**
* Collects all using statements of a Local from a list of statements
* Collects all using statements of a Values from a list of statements
*
* @param stmts The searched list of statements
* @return A map of Locals and their using statements
* @return A map of Values and their using statements
*/
public static Map<Value, Collection<Stmt>> collectUses(Collection<Stmt> stmts) {
Map<Value, Collection<Stmt>> allUses = new HashMap<>();
for (Stmt stmt : stmts) {
for (Iterator<Value> iterator = stmt.getUses().iterator(); iterator.hasNext(); ) {
Value value = iterator.next();
Collection<Stmt> localUses = allUses.computeIfAbsent(value, key -> new ArrayList<>());
localUses.add(stmt);
allUses.put(value, localUses);
}
}
return allUses;
public static Map<Value, List<Stmt>> collectUses(Collection<Stmt> stmts) {
return stmts.stream()
.flatMap(stmt -> stmt.getUses().map(value -> (Pair.of(value, stmt))))
.collect(
Collectors.groupingBy(
Pair::getLeft, Collectors.mapping(Pair::getRight, Collectors.toList())));
}
}
11 changes: 10 additions & 1 deletion sootup.examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -55,6 +59,11 @@
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/** This example illustrates how to create and use a new Soot Project. */
@Tag("Java8")
public class BasicSetup {
public class BasicSetupTest {

@Test
public void createByteCodeProject() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/** This example illustrates how to invoke body interceptors. */
@Tag("Java8")
public class BodyInterceptor {
public class BodyInterceptorTest {

@Test
public void test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import sootup.callgraph.CallGraph;
Expand All @@ -18,7 +19,7 @@
import sootup.java.core.views.JavaView;

@Tag("Java8")
public class CallgraphExample {
public class CallgraphExampleTest {

@Test
public void test() {
Expand Down Expand Up @@ -46,12 +47,16 @@ public void test() {

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

CallGraphAlgorithm cha = new ClassHierarchyAnalysisAlgorithm(view);

// Create CG by initializing CHA with entry method(s)
CallGraph cg = cha.initialize(Collections.singletonList(entryMethodSignature));

cg.callsFrom(entryMethodSignature).forEach(System.out::println);
System.out.println("Call Graph from B:");
final Set<CallGraph.Call> calls = cg.callsFrom(entryMethodSignature);
calls.forEach(System.out::println);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* This code example will show you how to build and examine a class hierarchy using sootup.
*/
@Tag("Java8")
public class ClassHierarchy {
public class ClassHierarchyTest {

@Test
public void test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @author Bastian Haverkamp
*/
@Tag("Java8")
public class MutatingSootClass {
public class MutatingSootClassTest {

@Disabled
public void test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Aggregator(boolean dontAggregateFieldLocals) {
public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) {
MutableStmtGraph graph = builder.getStmtGraph();
List<Stmt> stmts = builder.getStmts();
Map<Value, Collection<Stmt>> usesMap = Body.collectUses(stmts);
Map<Value, List<Stmt>> usesMap = Body.collectUses(stmts);

for (Stmt stmt : stmts) {
if (!(stmt instanceof JAssignStmt)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view)
return;
}

Map<Value, Collection<Stmt>> essentialUses = Body.collectUses(essentialStmts);
Map<Value, List<Stmt>> essentialUses = Body.collectUses(essentialStmts);
// Eliminate dead assignments from invokes such as x = f(), where x is no longer used
List<JAssignStmt> postProcess = new ArrayList<>();
for (Stmt stmt : stmts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public synchronized Optional<JavaSootClass> getClass(

if (!foundClassSources.isEmpty()) {

return buildClassFrom(foundClassSources.get(0));
return Optional.of(buildClassFrom(foundClassSources.get(0)));
} else {
PackageName packageName = type.getPackageName();
if (packageName instanceof ModulePackageName
Expand All @@ -189,7 +189,7 @@ public synchronized Optional<JavaSootClass> getClass(
.collect(Collectors.toList());

if (!foundClassSources.isEmpty()) {
return buildClassFrom(foundClassSources.get(0));
return Optional.of(buildClassFrom(foundClassSources.get(0)));
} else {
// automatic module can access the unnamed module -> try to find in classpath (as if
// modules do not exist)
Expand Down Expand Up @@ -231,7 +231,7 @@ public synchronized Optional<JavaSootClass> getClass(
})
.findAny();

return foundClassSources.flatMap(this::buildClassFrom);
return foundClassSources.map(this::buildClassFrom);
}
}

Expand Down Expand Up @@ -333,11 +333,7 @@ public synchronized Collection<JavaSootClass> getModuleClasses(
.map(src -> (JavaSootClassSource) src));
}
}
return stream
.map(this::buildClassFrom)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
return stream.map(this::buildClassFrom).collect(Collectors.toList());
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ public synchronized Stream<JavaSootClass> getClasses() {
Stream<JavaSootClass> resolvedClasses =
inputLocations.stream()
.flatMap(location -> location.getClassSources(this).stream())
.map(this::buildClassFrom)
.filter(Optional::isPresent)
.map(Optional::get);
.map(this::buildClassFrom);

isFullyResolved = true;

Expand All @@ -107,7 +105,7 @@ public synchronized Optional<JavaSootClass> getClass(@Nonnull ClassType type) {
}

Optional<JavaSootClassSource> abstractClass = getClassSource(type);
return abstractClass.flatMap(this::buildClassFrom);
return abstractClass.map(this::buildClassFrom);
}

@Nonnull
Expand Down Expand Up @@ -155,7 +153,7 @@ protected Optional<JavaSootClassSource> getClassSource(@Nonnull ClassType type)
}

@Nonnull
protected synchronized Optional<JavaSootClass> buildClassFrom(AbstractClassSource classSource) {
protected synchronized JavaSootClass buildClassFrom(AbstractClassSource classSource) {

ClassType classType = classSource.getClassType();
JavaSootClass theClass;
Expand All @@ -167,6 +165,6 @@ protected synchronized Optional<JavaSootClass> buildClassFrom(AbstractClassSourc
classSource.buildClass(classSource.getAnalysisInputLocation().getSourceType());
cache.putClass(classType, theClass);
}
return Optional.of(theClass);
return theClass;
}
}

0 comments on commit 13be056

Please sign in to comment.