Skip to content

Commit

Permalink
Add slicer test
Browse files Browse the repository at this point in the history
This test relates to a mailing list question from 
Gebrehiwet Biyane Welearegai:

https://groups.google.com/forum/#!topic/wala-sourceforge-net/lS7lyCHfAaw
  • Loading branch information
msridhar committed Dec 14, 2015
1 parent 2f47ffa commit 0eabfa2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
37 changes: 37 additions & 0 deletions com.ibm.wala.core.testdata/src/slice/TestPrimGetterSetter2.java
@@ -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());
}
}
Expand Up @@ -32,9 +32,14 @@
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;
Expand Down Expand Up @@ -626,6 +631,46 @@ public void testPrimGetterSetter() throws ClassHierarchyException, IllegalArgume
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();
Expand Down
Expand Up @@ -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";
Expand Down

0 comments on commit 0eabfa2

Please sign in to comment.