Skip to content

Commit

Permalink
warn if TR interceptor without rt.jar, and can't go any further than …
Browse files Browse the repository at this point in the history
…exception type
  • Loading branch information
sahilagichani14 committed Jul 28, 2024
1 parent dc598ea commit 0cd9b17
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import categories.TestCategories;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -25,10 +23,7 @@
import sootup.core.util.Utils;
import sootup.java.bytecode.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaPackageName;
import sootup.java.core.JavaSootClass;
import sootup.java.core.interceptors.LocalNameStandardizer;
import sootup.java.core.interceptors.TypeAssigner;
import sootup.java.core.interceptors.UnusedLocalEliminator;
import sootup.java.core.interceptors.typeresolving.TypeResolver;
import sootup.java.core.interceptors.typeresolving.types.TopType;
import sootup.java.core.types.JavaClassType;
Expand Down Expand Up @@ -297,32 +292,34 @@ public void testStringDefaultMethodsTest() {

@Test
public void testTrWithoutLs() {
String directoryPath = baseDir;
String directoryPath = baseDir + "Misc/";
try {
List<String> fileNames = listFiles(directoryPath);
fileNames.forEach(System.out::println);
fileNames.forEach(this::applyTrToJar);
List<String> jarFileNames = listJarFiles(directoryPath);
jarFileNames.forEach(
jarFile -> {
System.out.println("Applying Type Assigner to jar " + jarFile);
applyTypeAssignerToJar(jarFile);
});
} catch (IOException e) {
e.printStackTrace();
}
}

public static List<String> listFiles(String directoryPath) throws IOException {
public static List<String> listJarFiles(String directoryPath) throws IOException {
try (Stream<Path> paths = Files.walk(Paths.get(directoryPath))) {
return paths
.filter(Files::isExecutable)
.filter(path -> path.toString().toLowerCase().endsWith(".jar"))
//.map(Path::toString)
.sorted((path1, path2) -> {
.filter(Files::isRegularFile)
.filter(path -> path.toString().toLowerCase().endsWith(".jar"))
.sorted(
(path1, path2) -> {
try {
return Long.compare(Files.size(path1), Files.size(path2));
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.map(path -> path.getFileName().toString())
.filter(filename -> !filename.equals("MiniHierarchy.jar"))
.collect(Collectors.toList());
.map(path -> path.getFileName().toString())
.collect(Collectors.toList());
}
}

Expand All @@ -343,26 +340,20 @@ private Body getMiscBody(String name) {
}

// Apply Type Assigner then Unused Local Eliminator without using Local Splitter first.
private void applyTrToJar(String jarName) {
JavaClassPathAnalysisInputLocation inputJarLocation = new JavaClassPathAnalysisInputLocation(
private void applyTypeAssignerToJar(String jarName) {
JavaClassPathAnalysisInputLocation inputJarLocation =
new JavaClassPathAnalysisInputLocation(
baseDir + "Misc/" + jarName,
SourceType.Library,
Arrays.asList(new TypeAssigner()));
final JavaView view = new JavaView(Arrays.asList(inputJarLocation));

// MethodSignature methodSignature = view
// .getIdentifierFactory()
// .getMethodSignature(
// classType, "main", "void", Collections.singletonList("java.lang.String[]"));
Collections.singletonList(new TypeAssigner()));
final JavaView view = new JavaView(Collections.singletonList(inputJarLocation));

for (SootClass sootClass : view.getClasses()) {
for (SootMethod sootMethod : sootClass.getMethods()) {
if(sootMethod.isConcrete()){
if (sootMethod.isConcrete()) {
Body body = view.getMethod(sootMethod.getSignature()).get().getBody();
}
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.*;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sootup.core.IdentifierFactory;
import sootup.core.graph.StmtGraph;
import sootup.core.jimple.basic.Immediate;
Expand All @@ -47,6 +49,8 @@
/** @author Zun Wang */
public class AugEvalFunction {

private static final Logger logger = LoggerFactory.getLogger(AugEvalFunction.class);

private final ClassType stringClassType;
private final ClassType classClassType;
private final ClassType methodHandleClassType;
Expand Down Expand Up @@ -248,9 +252,12 @@ private Deque<ClassType> getExceptionPath(@Nonnull ClassType exceptionType) {
final Optional<? extends ClassType> superclassOpt =
view.getClass(exceptionType).flatMap(SootClass::getSuperclass);
if (!superclassOpt.isPresent()) {
// TODO: ms: don't fail completely.. work as far as information exists and warn.
throw new IllegalStateException(
"The path from '" + exceptionType + "' to java.lang.Throwable cannot be found!");
// Note: We have progressed as far as the available information allows.
logger.warn(
"The path from '"
+ exceptionType
+ "' to java.lang.Throwable cannot be found! Are you certain you don't want to include rt.jar? Including rt.jar could provide additional classes and resources that might be necessary for full functionality.");
break;
}

ClassType superType = superclassOpt.get();
Expand Down

0 comments on commit 0cd9b17

Please sign in to comment.