Skip to content

Commit

Permalink
Add new reflection test (#1117)
Browse files Browse the repository at this point in the history
Based on discussion in #1116, in particular #1116 (comment)
  • Loading branch information
msridhar committed Aug 26, 2022
1 parent 784ae14 commit e24abb1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.ibm.wala.core.util.warnings.Warnings;
import com.ibm.wala.ipa.callgraph.AnalysisCacheImpl;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisOptions.ReflectionOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.CallGraph;
Expand All @@ -41,11 +42,13 @@
import com.ibm.wala.util.WalaException;
import com.ibm.wala.util.collections.HashSetFactory;
import com.ibm.wala.util.collections.Iterator2Iterable;
import com.ibm.wala.util.collections.Iterator2List;
import com.ibm.wala.util.collections.Pair;
import com.ibm.wala.util.intset.OrdinalSet;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.AfterClass;
Expand Down Expand Up @@ -827,4 +830,27 @@ public void testGetMethodContext()
cgn = cg.getNodes(mcbar);
Assert.assertEquals(1, cgn.size());
}

@Test
public void testForNameThrownExceptions()
throws WalaException, IllegalArgumentException, CancelException, IOException {
AnalysisScope scope = findOrCreateAnalysisScope();
IClassHierarchy cha = findOrCreateCHA(scope);
Iterable<Entrypoint> entrypoints =
com.ibm.wala.ipa.callgraph.impl.Util.makeMainEntrypoints(
cha, "Lreflection/ForNameThrownExceptions");
AnalysisOptions options = CallGraphTestUtil.makeAnalysisOptions(scope, entrypoints);
options.setReflectionOptions(ReflectionOptions.NONE);
CallGraph cg = CallGraphTestUtil.buildZeroCFA(options, new AnalysisCacheImpl(), cha, false);
IMethod mainMethod = entrypoints.iterator().next().getMethod();
List<CGNode> mainCallees =
Iterator2List.toList(cg.getSuccNodes(cg.getNode(mainMethod, Everywhere.EVERYWHERE)));
Assert.assertTrue(mainCallees.stream().anyMatch(n -> n.toString().contains("getMessage")));
options.setReflectionOptions(ReflectionOptions.STRING_ONLY);
cg = CallGraphTestUtil.buildZeroCFA(options, new AnalysisCacheImpl(), cha, false);
mainCallees =
Iterator2List.toList(cg.getSuccNodes(cg.getNode(mainMethod, Everywhere.EVERYWHERE)));
// getMessage() should _not_ be a callee with reflection handling enabled
Assert.assertFalse(mainCallees.stream().anyMatch(n -> n.toString().contains("getMessage")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package reflection;

public class ForNameThrownExceptions {

static class MyClass {
public String sayHello() {
return "Hello from MyClass!";
}
}

public static void main(String[] args) {
try {
// This call to Class.forName() can be resolved by WALA's reflection handling. When
// it is resolved, the resulting synthetic model of Class.forName() cannot throw
// a ClassNotFoundException. Hence, no Exception values flow to e, and the call
// to e.getMessage() in the catch block has no callees
Class clazz = Class.forName("reflection.ForNameThrownExceptions$MyClass");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

0 comments on commit e24abb1

Please sign in to comment.