Added Testcases for ExceptionAnalysis and ExceptionAnalysis2EdgeFilter.

This commit is contained in:
Stephan Gocht 2016-01-13 02:07:58 +01:00
parent 2869af24df
commit 672876c595
4 changed files with 543 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package exceptionpruning;
public class OwnException extends RuntimeException {
}

View File

@ -0,0 +1,148 @@
package exceptionpruning;
public class TestPruning {
public void testTryCatchOwnException(int i) {
try {
switch (i) {
case 1:
invokeSingleThrowOwnException();
break;
case 2:
throw new OwnException();
case 3:
invokeSinglePassThrough();
break;
case 5:
invokeSingleRecursive(i);
break;
case 4:
invokeSingleRecursive2(i);
break;
}
} catch (OwnException e) {
}
}
public int testTryCatchImplicitException(int i) {
int res = 0;
try {
int[] a = new int[]{1,3,4};
switch (i) {
case 3:
invokeSingleImplicitException(a);
break;
case 4:
res = a[5];
}
} catch (ArrayIndexOutOfBoundsException e) {
}
return res;
}
public void testTryCatchSuper(int i) {
try {
switch (i) {
case 5:
invokeSingleThrowOwnException();
break;
case 6:
throw new OwnException();
case 7:
invokeAllPassThrough();
break;
}
} catch (RuntimeException e) {
}
}
public int testTryCatchMultipleExceptions(int i) {
int res = 0;
try {
int[] a = new int[]{1,3,4};
switch (i) {
case 7:
invokeAll();
break;
case 8:
throw new OwnException();
case 10:
res = a[5];
break;
case 11:
invokeSingleRecursive(i);
break;
case 12:
invokeSingleRecursive2(i);
break;
case 13:
invokeAllPassThrough();
break;
}
} catch (ArrayIndexOutOfBoundsException e) {
} catch (OwnException e) {
}
return res;
}
public void invokeAll() {
invokeSingleThrowOwnException();
invokeSingleImplicitException(null);
}
public void invokeAllPassThrough() {
invokeAll();
}
public void invokeSinglePassThrough() {
invokeSingleThrowOwnException();
}
public void invokeSingleThrowOwnException() {
throw new OwnException();
}
public int invokeSingleImplicitException(int[] a) {
// may throw NullPointerException implicit
return a[5];
}
public void invokeSingleRecursive(int i) {
if (i == 0) {
throw new OwnException();
} else {
invokeSingleRecursive(i - 1);
}
}
public void invokeSingleRecursive2(int i) {
if (i == 0) {
throw new OwnException();
} else {
invokeSingleRecursive2Helper(i - 1);
}
}
public void invokeSingleRecursive2Helper(int i) {
invokeSingleRecursive2(i);
}
public static void main(String[] args) {
TestPruning test = new TestPruning();
for (int i = 0; i < 50; i++) {
test.testTryCatchOwnException(i);
test.testTryCatchMultipleExceptions(i);
test.testTryCatchImplicitException(i);
test.testTryCatchSuper(i);
}
}
}

View File

@ -0,0 +1,217 @@
package com.ibm.wala.core.tests.exceptionpruning;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import com.ibm.wala.analysis.exceptionanalysis.ExceptionAnalysis;
import com.ibm.wala.analysis.exceptionanalysis.ExceptionAnalysis2EdgeFilter;
import com.ibm.wala.cfg.ControlFlowGraph;
import com.ibm.wala.core.tests.util.TestConstants;
import com.ibm.wala.ipa.callgraph.AnalysisCache;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.CallGraphBuilder;
import com.ibm.wala.ipa.callgraph.CallGraphBuilderCancelException;
import com.ibm.wala.ipa.callgraph.Entrypoint;
import com.ibm.wala.ipa.callgraph.impl.Util;
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
import com.ibm.wala.ipa.callgraph.propagation.PointerAnalysis;
import com.ibm.wala.ipa.cfg.EdgeFilter;
import com.ibm.wala.ipa.cfg.PrunedCFG;
import com.ibm.wala.ipa.cfg.exceptionpruning.ExceptionFilter2EdgeFilter;
import com.ibm.wala.ipa.cfg.exceptionpruning.filter.IgnoreExceptionsFilter;
import com.ibm.wala.ipa.cfg.exceptionpruning.interprocedural.CombinedInterproceduralExceptionFilter;
import com.ibm.wala.ipa.cfg.exceptionpruning.interprocedural.IgnoreExceptionsInterFilter;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ssa.AllIntegerDueToBranchePiPolicy;
import com.ibm.wala.ssa.ISSABasicBlock;
import com.ibm.wala.ssa.SSACFG;
import com.ibm.wala.ssa.SSAInstruction;
import com.ibm.wala.ssa.SSAInvokeInstruction;
import com.ibm.wala.ssa.SSAThrowInstruction;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.config.AnalysisScopeReader;
import com.ibm.wala.util.ref.ReferenceCleanser;
/**
* This Test checks, if the number of deleted edges is correct for TestPruning,
* it is also doing a plausibility check for deleted edges (only edges after
* exceptional instructions should be deleted) and that no new edges are
* inserted.
*
* @author Stephan Gocht <stephan@gobro.de>
*
*/
public class ExceptionAnalysis2EdgeFilterTest {
private static ClassLoader CLASS_LOADER = ExceptionAnalysis2EdgeFilterTest.class.getClassLoader();
public static String REGRESSION_EXCLUSIONS = "Java60RegressionExclusions.txt";
private static ClassHierarchy cha;
private static CallGraph cg;
private static PointerAnalysis<InstanceKey> pointerAnalysis;
private static CombinedInterproceduralExceptionFilter<SSAInstruction> filter;
@Rule
public ErrorCollector collector = new ErrorCollector();
@BeforeClass
public static void init() throws IOException, ClassHierarchyException, IllegalArgumentException, CallGraphBuilderCancelException {
AnalysisOptions options;
AnalysisScope scope;
scope = AnalysisScopeReader.readJavaScope(TestConstants.WALA_TESTDATA, new File(REGRESSION_EXCLUSIONS), CLASS_LOADER);
cha = ClassHierarchy.make(scope);
Iterable<Entrypoint> entrypoints = Util.makeMainEntrypoints(scope, cha, "Lexceptionpruning/TestPruning");
options = new AnalysisOptions(scope, entrypoints);
options.getSSAOptions().setPiNodePolicy(new AllIntegerDueToBranchePiPolicy());
ReferenceCleanser.registerClassHierarchy(cha);
AnalysisCache cache = new AnalysisCache();
ReferenceCleanser.registerCache(cache);
CallGraphBuilder builder = Util.makeZeroCFABuilder(options, cache, cha, scope);
cg = builder.makeCallGraph(options, null);
pointerAnalysis = builder.getPointerAnalysis();
/*
* We will ignore some exceptions to focus on the exceptions we want to
* raise (OwnException, ArrayIndexOutOfBoundException)
*/
filter = new CombinedInterproceduralExceptionFilter<SSAInstruction>();
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(TypeReference.JavaLangOutOfMemoryError)));
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(
TypeReference.JavaLangNullPointerException)));
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(
TypeReference.JavaLangExceptionInInitializerError)));
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(
TypeReference.JavaLangNegativeArraySizeException)));
}
@Test
public void test() {
HashMap<String, Integer> deletedExceptional = new HashMap<String, Integer>();
int deletedNormal = 0;
ExceptionAnalysis analysis = new ExceptionAnalysis(cg, pointerAnalysis, cha, filter);
analysis.solve();
for (CGNode node : cg) {
if (node.getIR() != null && !node.getIR().isEmptyIR()) {
EdgeFilter<ISSABasicBlock> exceptionAnalysedEdgeFilter = new ExceptionAnalysis2EdgeFilter(analysis, node);
SSACFG cfg_orig = node.getIR().getControlFlowGraph();
ExceptionFilter2EdgeFilter<ISSABasicBlock> filterOnlyEdgeFilter = new ExceptionFilter2EdgeFilter<ISSABasicBlock>(
filter.getFilter(node), cha, cfg_orig);
ControlFlowGraph<SSAInstruction, ISSABasicBlock> cfg = PrunedCFG.make(cfg_orig, filterOnlyEdgeFilter);
ControlFlowGraph<SSAInstruction, ISSABasicBlock> exceptionPruned = PrunedCFG.make(cfg_orig, exceptionAnalysedEdgeFilter);
for (ISSABasicBlock block : cfg) {
if (exceptionPruned.containsNode(block)) {
for (ISSABasicBlock normalSucc : cfg.getNormalSuccessors(block)) {
if (!exceptionPruned.getNormalSuccessors(block).contains(normalSucc)) {
checkRemovingNormalOk(node, cfg, block, normalSucc);
if (node.getMethod().getDeclaringClass().getName().getClassName().toString().equals("TestPruning")) {
deletedNormal += 1;
}
}
}
for (ISSABasicBlock exceptionalSucc : cfg.getExceptionalSuccessors(block)) {
if (!exceptionPruned.getExceptionalSuccessors(block).contains(exceptionalSucc)) {
if (node.getMethod().getDeclaringClass().getName().getClassName().toString().equals("TestPruning")) {
boolean count = true;
SSAInstruction instruction = block.getLastInstruction();
if (instruction instanceof SSAInvokeInstruction && ((SSAInvokeInstruction) instruction).isSpecial()) {
count = false;
}
if (count) {
Integer value = 0;
String key = node.getMethod().getName().toString();
if (deletedExceptional.containsKey(key)) {
value = deletedExceptional.get(key);
}
deletedExceptional.put(key, value + 1);
}
}
}
}
}
}
checkNoNewEdges(cfg, exceptionPruned);
}
}
assertEquals("Number of normal edges deleted wrong:", 0, deletedNormal);
for (String key : deletedExceptional.keySet()) {
int value = deletedExceptional.get(key);
String text = "Number of exceptional edges deleted wrong for " + key + ":";
if (key.equals("testTryCatchMultipleExceptions")) {
assertEquals(text, 12, value);
} else if (key.equals("testTryCatchOwnException")) {
assertEquals(text, 5, value);
} else if (key.equals("testTryCatchSuper")) {
assertEquals(text, 3, value);
} else if (key.equals("testTryCatchImplicitException")) {
assertEquals(text, 5, value);
} else if (key.equals("main")) {
assertEquals(text, 4, value);
} else {
assertEquals(text, 0, value);
}
}
}
private void checkRemovingNormalOk(CGNode node, ControlFlowGraph<SSAInstruction, ISSABasicBlock> cfg, ISSABasicBlock block,
ISSABasicBlock normalSucc) {
if (!block.getLastInstruction().isPEI() || !filter.getFilter(node).alwaysThrowsException(block.getLastInstruction())) {
specialCaseThrowFiltered(cfg, normalSucc);
} else {
assertTrue(block.getLastInstruction().isPEI());
assertTrue(filter.getFilter(node).alwaysThrowsException(block.getLastInstruction()));
}
}
/**
* If a filtered exception is thrown explicit with a throw command, all
* previous nodes, which only have normal edges to the throw statement will be
* deleted. They don't have a connection to the exit node anymore.
*
* So, if there is a throw statement as normal successor, evrything is fine.
*
* @param cfg
* @param normalSucc
*/
private void specialCaseThrowFiltered(ControlFlowGraph<SSAInstruction, ISSABasicBlock> cfg, ISSABasicBlock normalSucc) {
ISSABasicBlock next = normalSucc;
while (!(next.getLastInstruction() instanceof SSAThrowInstruction)) {
assertTrue(cfg.getNormalSuccessors(next).iterator().hasNext());
next = cfg.getNormalSuccessors(next).iterator().next();
}
}
private void checkNoNewEdges(ControlFlowGraph<SSAInstruction, ISSABasicBlock> original,
ControlFlowGraph<SSAInstruction, ISSABasicBlock> filtered) {
for (ISSABasicBlock block : filtered) {
for (ISSABasicBlock normalSucc : filtered.getNormalSuccessors(block)) {
assertTrue(original.getNormalSuccessors(block).contains(normalSucc));
}
for (ISSABasicBlock exceptionalSucc : filtered.getExceptionalSuccessors(block)) {
assertTrue(original.getExceptionalSuccessors(block).contains(exceptionalSucc));
}
}
}
}

View File

@ -0,0 +1,173 @@
package com.ibm.wala.core.tests.exceptionpruning;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.not;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import com.ibm.wala.analysis.exceptionanalysis.ExceptionAnalysis;
import com.ibm.wala.analysis.exceptionanalysis.IntraproceduralExceptionAnalysis;
import com.ibm.wala.classLoader.CallSiteReference;
import com.ibm.wala.core.tests.util.TestConstants;
import com.ibm.wala.ipa.callgraph.AnalysisCache;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.CallGraphBuilder;
import com.ibm.wala.ipa.callgraph.CallGraphBuilderCancelException;
import com.ibm.wala.ipa.callgraph.Entrypoint;
import com.ibm.wala.ipa.callgraph.impl.Util;
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
import com.ibm.wala.ipa.callgraph.propagation.PointerAnalysis;
import com.ibm.wala.ipa.cfg.exceptionpruning.filter.IgnoreExceptionsFilter;
import com.ibm.wala.ipa.cfg.exceptionpruning.interprocedural.CombinedInterproceduralExceptionFilter;
import com.ibm.wala.ipa.cfg.exceptionpruning.interprocedural.IgnoreExceptionsInterFilter;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ssa.AllIntegerDueToBranchePiPolicy;
import com.ibm.wala.ssa.SSAInstruction;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.config.AnalysisScopeReader;
import com.ibm.wala.util.ref.ReferenceCleanser;
/**
* This class checks, if the number of exceptions which might occur intra and
* interprocedural is right. As well as the number of caught exceptions for each
* call site.
*
* @author Stephan Gocht <stephan@gobro.de>
*
*/
public class ExceptionAnalysisTest {
private static ClassLoader CLASS_LOADER = ExceptionAnalysisTest.class.getClassLoader();
public static String REGRESSION_EXCLUSIONS = "Java60RegressionExclusions.txt";
private static ClassHierarchy cha;
private static CallGraph cg;
private static PointerAnalysis<InstanceKey> pointerAnalysis;
private static CombinedInterproceduralExceptionFilter<SSAInstruction> filter;
@Rule
public ErrorCollector collector = new ErrorCollector();
@BeforeClass
public static void init() throws IOException, ClassHierarchyException, IllegalArgumentException, CallGraphBuilderCancelException {
AnalysisOptions options;
AnalysisScope scope;
scope = AnalysisScopeReader.readJavaScope(TestConstants.WALA_TESTDATA, new File(REGRESSION_EXCLUSIONS), CLASS_LOADER);
cha = ClassHierarchy.make(scope);
Iterable<Entrypoint> entrypoints = Util.makeMainEntrypoints(scope, cha, "Lexceptionpruning/TestPruning");
options = new AnalysisOptions(scope, entrypoints);
options.getSSAOptions().setPiNodePolicy(new AllIntegerDueToBranchePiPolicy());
ReferenceCleanser.registerClassHierarchy(cha);
AnalysisCache cache = new AnalysisCache();
ReferenceCleanser.registerCache(cache);
CallGraphBuilder builder = Util.makeZeroCFABuilder(options, cache, cha, scope);
cg = builder.makeCallGraph(options, null);
pointerAnalysis = builder.getPointerAnalysis();
/*
* We will ignore some exceptions to focus on the exceptions we want to
* raise (OwnException, ArrayIndexOutOfBoundException)
*/
filter = new CombinedInterproceduralExceptionFilter<SSAInstruction>();
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(TypeReference.JavaLangOutOfMemoryError)));
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(
TypeReference.JavaLangNullPointerException)));
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(
TypeReference.JavaLangExceptionInInitializerError)));
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(
TypeReference.JavaLangExceptionInInitializerError)));
filter.add(new IgnoreExceptionsInterFilter<SSAInstruction>(new IgnoreExceptionsFilter(
TypeReference.JavaLangNegativeArraySizeException)));
}
@Test
public void testIntra() {
for (CGNode node : cg) {
IntraproceduralExceptionAnalysis analysis = new IntraproceduralExceptionAnalysis(node, filter.getFilter(node), cha,
pointerAnalysis);
if (node.getMethod().getDeclaringClass().getName().getClassName().toString().equals("TestPruning")) {
checkThrownExceptions(node, analysis);
checkCaughtExceptions(node, analysis);
}
}
}
private void checkCaughtExceptions(CGNode node, IntraproceduralExceptionAnalysis analysis) {
String text = "Number of caught exceptions did not match in " + node.getMethod().getName().toString()
+ ". The follwoing exceptions were caught: ";
Iterator<CallSiteReference> it = node.iterateCallSites();
while (it.hasNext()) {
Set<TypeReference> caught = analysis.getCaughtExceptions(it.next());
if (node.getMethod().getName().toString().matches("testTryCatch.*")) {
if (node.getMethod().getName().toString().equals("testTryCatchMultipleExceptions")) {
collector.checkThat(text + caught.toString(), caught.size(), equalTo(2));
} else if (node.getMethod().getName().toString().equals("testTryCatchSuper")) {
collector.checkThat(text + caught.toString(), caught.size(), not(anyOf(equalTo(0), equalTo(1), equalTo(2), equalTo(3))));
} else {
collector.checkThat(text + caught.toString(), caught.size(), equalTo(1));
}
} else {
collector.checkThat(text + caught.toString(), caught.size(), equalTo(0));
}
}
}
private void checkThrownExceptions(CGNode node, IntraproceduralExceptionAnalysis analysis) {
Set<TypeReference> exceptions = analysis.getExceptions();
String text = "Number of thrown exceptions did not match in " + node.getMethod().getName().toString()
+ ". The follwoing exceptions were thrown: " + exceptions.toString();
if (node.getMethod().getName().toString().matches("invokeSingle.*")
&& (!node.getMethod().getName().toString().equals("invokeSingleRecursive2Helper"))
&& (!node.getMethod().getName().toString().equals("invokeSinglePassThrough"))) {
collector.checkThat(text, exceptions.size(), equalTo(1));
} else {
collector.checkThat(text, exceptions.size(), equalTo(0));
}
}
@Test
public void testInterprocedural() {
ExceptionAnalysis analysis = new ExceptionAnalysis(cg, pointerAnalysis, cha, filter);
analysis.solve();
for (CGNode node : cg) {
if (node.getMethod().getDeclaringClass().getName().getClassName().toString().equals("TestPruning")) {
Set<TypeReference> exceptions = analysis.getCGNodeExceptions(node);
String text = "Number of thrown exceptions did not match in " + node.getMethod().getName().toString()
+ ". The follwoing exceptions were thrown: " + exceptions.toString();
if (node.getMethod().getName().toString().matches("invokeSingle.*")) {
collector.checkThat(text, exceptions.size(), equalTo(1));
} else if (node.getMethod().getName().toString().matches("testTryCatch.*")) {
collector.checkThat(text, exceptions.size(), equalTo(0));
} else if (node.getMethod().getName().toString().matches("invokeAll.*")) {
collector.checkThat(text, exceptions.size(), equalTo(2));
} else if (node.getMethod().getName().toString().equals("main")) {
collector.checkThat(text, exceptions.size(), equalTo(0));
} else {
String text2 = "Found method, i didn't know the expected number of exceptions for: "
+ node.getMethod().getName().toString();
collector.checkThat(text2, node.getMethod().getName().toString(), anyOf(equalTo("main"), equalTo("<init>")));
}
analysis.getCGNodeExceptions(node);
}
}
}
}