Skip to content

Commit

Permalink
Merge branch 'develop' into 1021-bug-copypropagator-not-propagating-c…
Browse files Browse the repository at this point in the history
…onstant-to-sout-binary-expressions-if-condition
  • Loading branch information
sahilagichani14 committed Aug 18, 2024
2 parents e81aa4e + 753427b commit f373fd5
Show file tree
Hide file tree
Showing 26 changed files with 61 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,30 +446,16 @@ public CallGraph addClass(@Nonnull CallGraph oldCallGraph, @Nonnull JavaClassTyp
* @return - MethodSignature of main method.
*/
public MethodSignature findMainMethod() {
Set<SootClass> classes = new HashSet<>(); /* Set to track the classes to check */
for (SootClass aClass : view.getClasses()) {
if (!aClass.isLibraryClass()) {
classes.add(aClass);
}
}

Collection<SootMethod> mainMethods = new HashSet<>(); /* Set to store the methods */
for (SootClass aClass : classes) {
for (SootMethod method : aClass.getMethods()) {
if (method.isStatic()
&& method
.getSignature()
.equals(
JavaIdentifierFactory.getInstance()
.getMethodSignature(
aClass.getType(),
"main",
"void",
Collections.singletonList("java.lang.String[]")))) {
mainMethods.add(method);
}
}
}
Collection<SootMethod> mainMethods =
view.getClasses()
.filter(aClass -> !aClass.isLibraryClass())
.flatMap(aClass -> aClass.getMethods().stream())
.filter(
method ->
method.isStatic()
&& JavaIdentifierFactory.getInstance()
.isMainSubSignature(method.getSignature().getSubSignature()))
.collect(Collectors.toSet());

if (mainMethods.size() > 1) {
throw new RuntimeException(
Expand Down
4 changes: 2 additions & 2 deletions sootup.core/src/main/java/sootup/core/views/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* #L%
*/

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import sootup.core.IdentifierFactory;
import sootup.core.model.SootClass;
Expand All @@ -44,7 +44,7 @@ public interface View {

/** Return all classes in the view. */
@Nonnull
Collection<? extends SootClass> getClasses();
Stream<? extends SootClass> getClasses();

/**
* Return a class with given signature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public void testJarWithDefaultInterceptors() {

private static void convertInputLocation(AnalysisInputLocation inputLocation) {
JavaView view = new JavaView(Collections.singletonList(inputLocation));
long classesCount = view.getClasses().size();
long classesCount = view.getClasses().count();
if (debug) {
System.out.println("classes: " + classesCount);
}
int[] failedConversions = {0};
long[] progress = {0};

long count =
view.getClasses().stream()
view.getClasses()
.peek(
c -> {
if (!debug) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void test() {

JavaView view = new JavaView(inputLocation);

assertEquals(1, view.getClasses().size());
assertEquals(1, view.getClasses().count());

view.getClasses().stream().findFirst().get().getMethods().forEach(SootMethod::getBody);
view.getClasses().findFirst().get().getMethods().forEach(SootMethod::getBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void test() {

JavaView view = new JavaView(Collections.singletonList(inputLocation));

assertEquals(91, view.getClasses().size());
assertEquals(91, view.getClasses().count());

ClassType clazzType =
JavaIdentifierFactory.getInstance().getClassType("cn.jpush.android.data.f");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public void multiReleaseJar() {

// getClasses
List<String> collectedClassesWPrintBody9 =
view_9.getClasses().stream()
view_9
.getClasses()
.map(c -> c.getMethod(printBodyMethodSubSig))
.filter(Optional::isPresent)
.map(m -> m.get().getBody().toString())
Expand All @@ -157,7 +158,8 @@ public void multiReleaseJar() {
assertTrue(collectedClassesWPrintBody9.get(0).contains("java 9"));

List<String> collectedClassesWPrintBody10 =
view_10.getClasses().stream()
view_10
.getClasses()
.map(c -> c.getMethod(printBodyMethodSubSig))
.filter(Optional::isPresent)
.map(m -> m.get().getBody().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void testClassInWar() {
// Get the view
JavaView view = new JavaView(new JavaClassPathAnalysisInputLocation(warFile));

assertEquals(19, view.getClasses().size());
assertEquals(19, view.getClasses().count());

// Create java class signature
ClassType utilsClassSignature = view.getIdentifierFactory().getClassType("Employee", "ds");
Expand Down Expand Up @@ -215,11 +215,7 @@ public void testInputLocationLibraryMode() {

Collection<SootClass> classes = new HashSet<>(); // Set to track the classes to check

for (SootClass aClass : view.getClasses()) {
if (!aClass.isLibraryClass()) {
classes.add(aClass);
}
}
view.getClasses().filter(aClass -> !aClass.isLibraryClass()).forEach(classes::add);

assertEquals(0, classes.size(), "User Defined class found, expected none");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import sootup.core.inputlocation.AnalysisInputLocation;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void writeTestMetrics(TestMetrics testMetrics) {

private Collection<JavaSootClass> getClasses(JavaView view) {
try {
return view.getClasses();
return view.getClasses().collect(Collectors.toList());
} catch (Exception e) {
throw new RuntimeException("Error while getting class list", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ public void testIssue739() {
final ClassType classType = view.getIdentifierFactory().getClassType("Issue739_Aggregator");
assertTrue(view.getClass(classType).isPresent());

for (JavaSootMethod javaSootMethod :
view.getClasses().stream().findFirst().get().getMethods()) {
for (JavaSootMethod javaSootMethod : view.getClasses().findFirst().get().getMethods()) {
final Body body = javaSootMethod.getBody();
}
}
Expand All @@ -215,8 +214,7 @@ public void testIssue911() {
final ClassType classType = view.getIdentifierFactory().getClassType("Issue911_Aggregator");
assertTrue(view.getClass(classType).isPresent());

for (JavaSootMethod javaSootMethod :
view.getClasses().stream().findFirst().get().getMethods()) {
for (JavaSootMethod javaSootMethod : view.getClasses().findFirst().get().getMethods()) {
final Body body = javaSootMethod.getBody();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void JRT() {

// ~200_000 methods
view.getClasses()
.parallelStream()
.parallel()
.flatMap(clazz -> clazz.getMethods().stream())
.filter(method -> !method.isAbstract() && !method.isNative())
.forEach(SootMethod::getBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
* #L%
*/

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import sootup.core.cache.ClassCache;
import sootup.core.cache.FullCache;
Expand Down Expand Up @@ -82,20 +81,17 @@ protected JavaView(
/** Resolves all classes that are part of the view and stores them in the cache. */
@Override
@Nonnull
public synchronized Collection<JavaSootClass> getClasses() {
public synchronized Stream<JavaSootClass> getClasses() {
if (isFullyResolved && cache instanceof FullCache) {
return cache.getClasses().stream()
.map(clazz -> (JavaSootClass) clazz)
.collect(Collectors.toList());
return cache.getClasses().stream().map(clazz -> (JavaSootClass) clazz);
}

Collection<JavaSootClass> resolvedClasses =
Stream<JavaSootClass> resolvedClasses =
inputLocations.stream()
.flatMap(location -> location.getClassSources(this).stream())
.map(this::buildClassFrom)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
.map(Optional::get);

isFullyResolved = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import static org.junit.jupiter.api.Assertions.*;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
Expand Down Expand Up @@ -71,12 +71,12 @@ public void testResolveUndefinedClassAfterAllResolved() {

@Disabled
public void testResolveAll() {
Collection<JavaSootClass> classes = this.view.getClasses();
Stream<JavaSootClass> classes = this.view.getClasses();

assertEquals(classes.size(), this.signatures.size());
assertEquals(classes.count(), this.signatures.size());

assertEquals(
classes.stream()
classes
.map(AbstractClass::getType)
.sorted(Comparator.comparing(Type::toString))
.collect(Collectors.toList()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testGetClassSources() {
final JavaView view = new JavaView(inputLocation);

Collection<? extends AbstractClassSource> classSources =
view.getClasses().stream().map(jsc -> jsc.getClassSource()).collect(Collectors.toList());
view.getClasses().map(jsc -> jsc.getClassSource()).collect(Collectors.toList());

ClassType type = new JavaClassType("Array1", PackageName.DEFAULT_PACKAGE);
Optional<ClassType> optionalFoundType =
Expand Down Expand Up @@ -91,11 +91,7 @@ public void testInputSourcePathLibraryMode() {
JavaView view = new JavaView(inputLocation);

Set<SootClass> classes = new HashSet<>(); // Set to track the classes to check
for (SootClass aClass : view.getClasses()) {
if (!aClass.isLibraryClass()) {
classes.add(aClass);
}
}
view.getClasses().filter(aClass -> !aClass.isLibraryClass()).forEach(classes::add);

assertEquals(0, classes.size(), "User Defined class found, expected none");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import sootup.core.IdentifierFactory;
import sootup.core.cache.ClassCache;
Expand Down Expand Up @@ -77,8 +78,8 @@ public JimpleView(

@Override
@Nonnull
public synchronized Collection<SootClass> getClasses() {
return getAbstractClassSources();
public synchronized Stream<SootClass> getClasses() {
return getAbstractClassSources().stream();
}

@Nonnull
Expand Down
8 changes: 3 additions & 5 deletions sootup.qilin/src/main/java/qilin/core/PTAScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ public SootMethod getMethod(String methodSignature) {
}

public Collection<SootClass> getApplicationClasses() {
Collection<? extends SootClass> classes = view.getClasses();
return classes.stream().filter(SootClass::isApplicationClass).collect(Collectors.toSet());
return view.getClasses().filter(SootClass::isApplicationClass).collect(Collectors.toSet());
}

public Collection<SootClass> getLibraryClasses() {
Collection<? extends SootClass> classes = view.getClasses();
return classes.stream().filter(SootClass::isLibraryClass).collect(Collectors.toSet());
return view.getClasses().filter(SootClass::isLibraryClass).collect(Collectors.toSet());
}

public boolean containsMethod(String methodSignature) {
Expand All @@ -117,7 +115,7 @@ public boolean containsField(String fieldSignature) {
}

public Collection<? extends SootClass> getClasses() {
return view.getClasses();
return view.getClasses().collect(Collectors.toList());
}

public Collection<SootClass> getPhantomClasses() {
Expand Down
2 changes: 1 addition & 1 deletion sootup.qilin/src/main/java/qilin/core/VirtualCalls.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class VirtualCalls {

public VirtualCalls(View view) {
this.view = view;
this.typeToVtbl = DataFactory.createMap(view.getClasses().size());
this.typeToVtbl = DataFactory.createMap((int) view.getClasses().count());
}

public SootMethod resolveSpecial(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public CallGraphBuilder(PTA pta) {
this.ptaScene = pta.getScene();
ptaScene.setCallGraph(new OnFlyCallGraph());
this.virtualCalls = new VirtualCalls(ptaScene.getView());
receiverToSites = DataFactory.createMap(ptaScene.getView().getClasses().size());
receiverToSites = DataFactory.createMap((int) ptaScene.getView().getClasses().count());
methodToInvokeStmt = DataFactory.createMap();
reachMethods = DataFactory.createSet();
calledges = DataFactory.createSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ExceptionHandler {
public ExceptionHandler(PTA pta) {
this.pta = pta;
this.pag = pta.getPag();
this.throwNodeToSites = DataFactory.createMap(pta.getView().getClasses().size());
this.throwNodeToSites = DataFactory.createMap((int) pta.getView().getClasses().count());
}

public Collection<ExceptionThrowSite> throwSitesLookUp(VarNode throwNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
Expand Down Expand Up @@ -348,10 +347,7 @@ public List<SootMethod> all() {
/** Returns a list of all static initializers. */
public List<SootMethod> clinits() {
List<SootMethod> ret = new ArrayList<>();
Collection<? extends SootClass> classes = view.getClasses();
for (SootClass cl : classes) {
addMethod(ret, cl, sigClinit);
}
view.getClasses().forEach(cl -> addMethod(ret, cl, sigClinit));
return ret;
}

Expand Down
8 changes: 2 additions & 6 deletions sootup.qilin/src/main/java/qilin/stat/CallGraphStat.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package qilin.stat;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
Expand All @@ -31,7 +30,6 @@
import qilin.core.pag.ContextMethod;
import qilin.core.pag.ContextVarNode;
import qilin.core.pag.LocalVarNode;
import sootup.core.model.SootClass;
import sootup.core.model.SootMethod;

public class CallGraphStat implements AbstractStat {
Expand Down Expand Up @@ -70,10 +68,8 @@ public CallGraphStat(PTA pta) {

private void init() {
// stat method numbers
Collection<? extends SootClass> clazzs = pta.getView().getClasses();
for (SootClass clazz : clazzs) {
allMethods += clazz.getMethods().size();
}
allMethods +=
pta.getView().getClasses().map(clazz -> clazz.getMethods().size()).reduce(0, Integer::sum);
//
OnFlyCallGraph csCallGraph = pta.getCgb().getCallGraph();
CSCallEdges = csCallGraph.size();
Expand Down
Loading

0 comments on commit f373fd5

Please sign in to comment.