Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new reflection test #1117

Merged
merged 6 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}