Skip to content

Commit

Permalink
Merge pull request #1053 from soot-oss/featureImproveJavaHomeIntegartion
Browse files Browse the repository at this point in the history
provide a Decorator to add the default Java Runtime as AnalysisInputLocation depending on the executing Java version
  • Loading branch information
swissiety authored Oct 1, 2024
2 parents 4954992 + 034b9c7 commit 9369a01
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 35 deletions.
2 changes: 1 addition & 1 deletion docs/callgraphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Below, we show how to create a type hierarchy:
String cpString = "src/test/resources/Callgraph/binary";
List<AnalysisInputLocation> inputLocations = new ArrayList();
inputLocations.add(new JavaClassPathAnalysisInputLocation(cpStr));
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());

JavaView view = new JavaView(inputLocations);
```
Expand Down
2 changes: 1 addition & 1 deletion docs/typehierarchy.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The TypeHierarchy models the relationship of Classes or Interfaces of a OOP prog
String cpString = "src/test/resources/Callgraph/binary";
List<AnalysisInputLocation> inputLocations = new ArrayList();
inputLocations.add(new JavaClassPathAnalysisInputLocation(cpStr));
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());

JavaView view = new JavaView(inputLocations);
TypeHierarchy typehierarchy = view.getTypeHierarchy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sootup.core.model.SootClass;
import sootup.core.model.SootMethod;
import sootup.core.signatures.MethodSignature;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.types.JavaClassType;
Expand All @@ -39,7 +39,7 @@ public CallGraph loadCallGraph(JavaView view) {
@Test
public void ICFGDotExportTest() {
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
inputLocations.add(new JavaClassPathAnalysisInputLocation("src/test/resources/icfg/binary"));

view = new JavaView(inputLocations);
Expand Down Expand Up @@ -75,7 +75,7 @@ public void ICFGDotExportTest() {
@Test
public void ICFGDotExportTest2() {
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
inputLocations.add(new JavaClassPathAnalysisInputLocation("src/test/resources/icfg/binary"));

view = new JavaView(inputLocations);
Expand Down Expand Up @@ -111,7 +111,7 @@ public void ICFGDotExportTest2() {
@Test
public void ICFGArrayListDotExport() {
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
inputLocations.add(new JavaClassPathAnalysisInputLocation("src/test/resources/icfg/binary"));

view = new JavaView(inputLocations);
Expand Down Expand Up @@ -139,7 +139,7 @@ public void ICFGArrayListDotExport() {
@Test
public void ICFGInterfaceDotExport() {
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
inputLocations.add(new JavaClassPathAnalysisInputLocation("src/test/resources/icfg/binary"));

view = new JavaView(inputLocations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import sootup.core.model.SootMethod;
import sootup.core.model.SourceType;
import sootup.core.signatures.MethodSignature;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.types.JavaClassType;
Expand Down Expand Up @@ -79,7 +79,7 @@ private void runAnalysis() {
*/
private void setupSoot(String targetTestClassName) {
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
inputLocations.add(
new JavaClassPathAnalysisInputLocation(
"src/test/resources/taint/binary", SourceType.Application, Collections.emptyList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import sootup.core.model.SourceType;
import sootup.core.signatures.MethodSignature;
import sootup.core.types.ClassType;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.types.JavaClassType;
Expand All @@ -45,7 +45,7 @@ private JavaView createViewForClassPath(String classPath) {

private JavaView createViewForClassPath(String classPath, boolean useSourceCodeFrontend) {
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
if (useSourceCodeFrontend) {
inputLocations.add(new JavaSourcePathAnalysisInputLocation(classPath));
} else {
Expand Down Expand Up @@ -1053,7 +1053,7 @@ public void testStopAtLibraryClass() {
String classPath = "src/test/resources/callgraph/Library/binary/";

List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
inputLocations.add(
new JavaClassPathAnalysisInputLocation(classPath + "application/", SourceType.Application));
inputLocations.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.signatures.MethodSignature;
import sootup.core.types.ClassType;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.views.JavaView;

Expand All @@ -32,7 +32,7 @@ public static void setUp() {
inputLocations.add(
new JavaClassPathAnalysisInputLocation(
"src/test/resources/callgraph/ConcreteDispatch/binary"));
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
view = new JavaView(inputLocations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@
import sootup.core.types.ClassType;
import sootup.core.types.PrimitiveType;
import sootup.core.views.View;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.core.language.JavaJimple;
import sootup.java.core.views.JavaView;

@Tag("Java8")
public class InstantiateClassValueVisitorTest {
@Test
public void testVisitor() {
View view = new JavaView(new DefaultRTJarAnalysisInputLocation());
View view = new JavaView(new DefaultRuntimeAnalysisInputLocation());
IdentifierFactory identifierFactory = view.getIdentifierFactory();

InstantiateClassValueVisitor instantiateVisitor = new InstantiateClassValueVisitor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.typehierarchy.ViewTypeHierarchy;
import sootup.core.types.ClassType;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.types.JavaClassType;
Expand All @@ -39,7 +39,7 @@ public void test() {
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(
new JavaClassPathAnalysisInputLocation("src/test/resources/ClassHierarchy/binary"));
inputLocations.add(new DefaultRTJarAnalysisInputLocation()); // add rt.jar
inputLocations.add(new DefaultRuntimeAnalysisInputLocation()); // add rt.jar

JavaView view = new JavaView(inputLocations);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* <p>Info: This only works if you are running java 8 or older. Otherwise use {@link
* JrtFileSystemAnalysisInputLocation}.
*/
public class DefaultRTJarAnalysisInputLocation extends ArchiveBasedAnalysisInputLocation {
class DefaultRTJarAnalysisInputLocation extends ArchiveBasedAnalysisInputLocation {

public DefaultRTJarAnalysisInputLocation() {
this(SourceType.Library);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package sootup.java.bytecode.frontend.inputlocation;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull;
import sootup.core.frontend.SootClassSource;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SourceType;
import sootup.core.transform.BodyInterceptor;
import sootup.core.types.ClassType;
import sootup.core.views.View;
import sootup.interceptors.BytecodeBodyInterceptors;

/** AnalysisInputLocation that points to the shipped Java Runtime of the current JVM execution */
public class DefaultRuntimeAnalysisInputLocation implements AnalysisInputLocation {

@Nonnull private final AnalysisInputLocation backingInputLocation;

public DefaultRuntimeAnalysisInputLocation() {
this(SourceType.Library);
}

public DefaultRuntimeAnalysisInputLocation(@Nonnull SourceType srcType) {
this(srcType, BytecodeBodyInterceptors.Default.getBodyInterceptors());
}

public DefaultRuntimeAnalysisInputLocation(
@Nonnull SourceType srcType, @Nonnull List<BodyInterceptor> bodyInterceptors) {

String version = System.getProperty("java.version");
// are we using Java 8 or lower in the current JVM execution?
if (version.startsWith("1")) {
backingInputLocation = new DefaultRTJarAnalysisInputLocation(srcType, bodyInterceptors);
} else {
backingInputLocation = new JrtFileSystemAnalysisInputLocation(srcType, bodyInterceptors);
}
}

@Nonnull
@Override
public Optional<? extends SootClassSource> getClassSource(
@Nonnull ClassType type, @Nonnull View view) {
return backingInputLocation.getClassSource(type, view);
}

@Nonnull
@Override
public Collection<? extends SootClassSource> getClassSources(@Nonnull View view) {
return backingInputLocation.getClassSources(view);
}

@Nonnull
@Override
public SourceType getSourceType() {
return backingInputLocation.getSourceType();
}

@Nonnull
@Override
public List<BodyInterceptor> getBodyInterceptors() {
return backingInputLocation.getBodyInterceptors();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import sootup.interceptors.CopyPropagator;
import sootup.interceptors.DeadAssignmentEliminator;
import sootup.interceptors.TypeAssigner;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.views.JavaView;

Expand All @@ -34,7 +34,8 @@ public class RuntimeJarConversionTests {

@Test
public void testJarWithDefaultInterceptors() {
AnalysisInputLocation inputLocation = new DefaultRTJarAnalysisInputLocation(SourceType.Library);
AnalysisInputLocation inputLocation =
new DefaultRuntimeAnalysisInputLocation(SourceType.Library);
convertInputLocation(inputLocation);
}

Expand Down Expand Up @@ -81,7 +82,7 @@ private static void convertInputLocation(AnalysisInputLocation inputLocation) {
// @Test
public void testJar() {
AnalysisInputLocation inputLocation =
new DefaultRTJarAnalysisInputLocation(SourceType.Library, Collections.emptyList());
new DefaultRuntimeAnalysisInputLocation(SourceType.Library, Collections.emptyList());
convertInputLocation(inputLocation);
}

Expand All @@ -108,7 +109,7 @@ public void testJar() {

private static Body convertMethod(String methodSignature) {
AnalysisInputLocation inputLocation =
new DefaultRTJarAnalysisInputLocation(SourceType.Library, bodyInterceptors);
new DefaultRuntimeAnalysisInputLocation(SourceType.Library, bodyInterceptors);
return convertMethod(methodSignature, inputLocation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sootup.core.model.SootMethod;
import sootup.core.model.SourceType;
import sootup.core.signatures.MethodSignature;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.JavaSootMethod;
Expand All @@ -31,7 +31,7 @@ public void testFix_StackUnderrun_convertPutFieldInsn_init() {
fail("The rt.jar is not available after Java 8. You are using version " + version);
}

JavaView view = new JavaView(new DefaultRTJarAnalysisInputLocation());
JavaView view = new JavaView(new DefaultRuntimeAnalysisInputLocation());

final JavaIdentifierFactory idf = JavaIdentifierFactory.getInstance();
JavaClassType mainClassSignature =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void runtimeContains(View view, String classname, String packageName) {

@Test
public void testRuntimeJar() {
PathBasedAnalysisInputLocation pathBasedNamespace = new DefaultRTJarAnalysisInputLocation();
AnalysisInputLocation pathBasedNamespace = new DefaultRuntimeAnalysisInputLocation();

JavaView v = new JavaView(pathBasedNamespace);

Expand All @@ -202,7 +202,7 @@ public void testRuntimeJar() {
*/
@Test
public void testInputLocationLibraryMode() {
JavaView view = new JavaView(new DefaultRTJarAnalysisInputLocation());
JavaView view = new JavaView(new DefaultRuntimeAnalysisInputLocation());

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sootup.interceptors.typeresolving.BytecodeHierarchy;
import sootup.interceptors.typeresolving.types.BottomType;
import sootup.interceptors.typeresolving.types.TopType;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.views.JavaView;

Expand Down Expand Up @@ -57,7 +57,8 @@ public void setUp() {
JavaClassPathAnalysisInputLocation analysisInputLocation =
new JavaClassPathAnalysisInputLocation(jarFile);
view =
new JavaView(Arrays.asList(new DefaultRTJarAnalysisInputLocation(), analysisInputLocation));
new JavaView(
Arrays.asList(new DefaultRuntimeAnalysisInputLocation(), analysisInputLocation));

// create types
IdentifierFactory factory = view.getIdentifierFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sootup.core.types.ClassType;
import sootup.core.types.Type;
import sootup.interceptors.typeresolving.Typing;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaSootClass;
import sootup.java.core.JavaSootMethod;
Expand All @@ -31,7 +31,7 @@ public void buildView(String baseDir, String className) {
new JavaClassPathAnalysisInputLocation(
baseDir, SourceType.Application, Collections.emptyList());
AnalysisInputLocation rtJar =
new DefaultRTJarAnalysisInputLocation(SourceType.Application, Collections.emptyList());
new DefaultRuntimeAnalysisInputLocation(SourceType.Application, Collections.emptyList());

view = new JavaView(Arrays.asList(analysisInputLocation, rtJar));

Expand Down
4 changes: 2 additions & 2 deletions sootup.tests/src/test/java/sootup/tests/CallGraphTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sootup.core.model.SootClass;
import sootup.core.model.SootMethod;
import sootup.core.signatures.MethodSignature;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.types.JavaClassType;
import sootup.java.core.views.JavaView;
Expand All @@ -42,7 +42,7 @@ protected AbstractCallGraphAlgorithm createAlgorithm(JavaView view) {

private JavaView createViewForClassPath(String classPath) {
List<AnalysisInputLocation> inputLocations = new ArrayList<>();
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());
inputLocations.add(new JavaSourcePathAnalysisInputLocation(classPath));

return new JavaView(inputLocations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sootup.core.typehierarchy.HierarchyComparator;
import sootup.core.types.ClassType;
import sootup.core.views.View;
import sootup.java.bytecode.frontend.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation;
import sootup.java.core.views.JavaView;
import sootup.java.frontend.inputlocation.JavaSourcePathAnalysisInputLocation;

Expand All @@ -28,7 +28,7 @@ public static void setUp() {
inputLocations.add(
new JavaSourcePathAnalysisInputLocation(
Collections.singleton("src/test/resources/javatypehierarchy/Comparator")));
inputLocations.add(new DefaultRTJarAnalysisInputLocation());
inputLocations.add(new DefaultRuntimeAnalysisInputLocation());

view = new JavaView(inputLocations);
}
Expand Down

0 comments on commit 9369a01

Please sign in to comment.