Add slicer test

This test relates to a mailing list question from 
Gebrehiwet Biyane Welearegai:

https://groups.google.com/forum/#!topic/wala-sourceforge-net/lS7lyCHfAaw
This commit is contained in:
Manu Sridharan 2015-12-14 10:34:10 -08:00
parent 2f47ffa02b
commit 0eabfa2d05
3 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package slice;
public class TestPrimGetterSetter2 {
static class IntWrapper {
int i;
int getI() { return i; }
void setI(int i) {
this.i = i;
}
}
public static void doNothing(int i) {}
/**
* @param args
*/
public static void main(String[] args) {
IntWrapper w1 = new IntWrapper();
w1.setI(4);
IntWrapper w2 = new IntWrapper();
w2.setI(5);
w2.getI();
doNothing(w1.getI());
}
}

View File

@ -32,9 +32,14 @@ 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.ContextSelector;
import com.ibm.wala.ipa.callgraph.Entrypoint;
import com.ibm.wala.ipa.callgraph.impl.PartialCallGraph;
import com.ibm.wala.ipa.callgraph.impl.Util;
import com.ibm.wala.ipa.callgraph.propagation.SSAContextInterpreter;
import com.ibm.wala.ipa.callgraph.propagation.SSAPropagationCallGraphBuilder;
import com.ibm.wala.ipa.callgraph.propagation.cfa.ZeroXInstanceKeys;
import com.ibm.wala.ipa.callgraph.propagation.cfa.nCFABuilder;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.IClassHierarchy;
@ -626,6 +631,46 @@ public class SlicerTest {
Assert.assertEquals(slice.toString(), 1, countPutfields(slice));
}
/**
* Test of using N-CFA builder to distinguish receiver objects for two calls
* to a getter method. Also tests disabling SMUSH_PRIMITIVE_HOLDERS to ensure
* we get distinct abstract objects for two different primitive holders.
*/
@Test
public void testPrimGetterSetter2() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
AnalysisScope scope = findOrCreateAnalysisScope();
IClassHierarchy cha = findOrCreateCHA(scope);
Iterable<Entrypoint> entrypoints = com.ibm.wala.ipa.callgraph.impl.Util.makeMainEntrypoints(scope, cha,
TestConstants.SLICE_TEST_PRIM_GETTER_SETTER2);
AnalysisOptions options = CallGraphTestUtil.makeAnalysisOptions(scope, entrypoints);
Util.addDefaultSelectors(options, cha);
Util.addDefaultBypassLogic(options, scope, Util.class.getClassLoader(), cha);
ContextSelector appSelector = null;
SSAContextInterpreter appInterpreter = null;
SSAPropagationCallGraphBuilder builder = new nCFABuilder(1, cha, options, new AnalysisCache(), appSelector, appInterpreter);
// nCFABuilder uses type-based heap abstraction by default, but we want allocation sites
// NOTE: we disable ZeroXInstanceKeys.SMUSH_PRIMITIVE_HOLDERS for this test, since IntWrapper
// is a primitive holder
builder.setInstanceKeys(new ZeroXInstanceKeys(options, cha, builder.getContextInterpreter(), ZeroXInstanceKeys.ALLOCATIONS
| ZeroXInstanceKeys.SMUSH_MANY /* | ZeroXInstanceKeys.SMUSH_PRIMITIVE_HOLDERS */ | ZeroXInstanceKeys.SMUSH_STRINGS
| ZeroXInstanceKeys.SMUSH_THROWABLES));
CallGraph cg = builder.makeCallGraph(options, null);
CGNode test = findMainMethod(cg);
PartialCallGraph pcg = PartialCallGraph.make(cg, Collections.singleton(test));
Statement s = findCallToDoNothing(test);
System.err.println("Statement: " + s);
Collection<Statement> slice = Slicer.computeBackwardSlice(s, pcg, builder.getPointerAnalysis(), DataDependenceOptions.FULL,
ControlDependenceOptions.NONE);
dumpSlice(slice);
Assert.assertEquals(slice.toString(), 1, countAllocations(slice));
Assert.assertEquals(slice.toString(), 1, countPutfields(slice));
}
@Test
public void testTestThrowCatch() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
AnalysisScope scope = findOrCreateAnalysisScope();

View File

@ -131,6 +131,8 @@ public interface TestConstants {
public final static String SLICE_TEST_PRIM_GETTER_SETTER = "Lslice/TestPrimGetterSetter";
public final static String SLICE_TEST_PRIM_GETTER_SETTER2 = "Lslice/TestPrimGetterSetter2";
public final static String SLICE_TESTTHIN1 = "Lslice/TestThin1";
public final static String SLICE_TESTTHROWCATCH = "Lslice/TestThrowCatch";