fixed bug in handling of arrays in TypeBasedPointerAnalysis
git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@2693 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
parent
db975c4f1f
commit
6e92ca411b
|
@ -0,0 +1,33 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 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 arrayAlias;
|
||||
|
||||
public class TestArrayAlias {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Object[] o = new Integer[10];
|
||||
testMayAlias1(o, (Integer[])o);
|
||||
Object[] o2 = new Object[20];
|
||||
testMayAlias2(o2, (Object[][]) o2);
|
||||
testMayAlias3((Integer[])o, (String[])o);
|
||||
}
|
||||
|
||||
private static void testMayAlias1(Object[] o, Integer[] o2) {
|
||||
}
|
||||
|
||||
private static void testMayAlias2(Object[] o, Object[][] o2) {
|
||||
|
||||
}
|
||||
|
||||
private static void testMayAlias3(Integer[] o, String[] o2) {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 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 com.ibm.wala.core.tests.ptrs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.ibm.wala.core.tests.callGraph.CallGraphTestUtil;
|
||||
import com.ibm.wala.core.tests.util.TestConstants;
|
||||
import com.ibm.wala.core.tests.util.WalaTestCase;
|
||||
import com.ibm.wala.eclipse.util.CancelException;
|
||||
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.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.callgraph.propagation.PointerKey;
|
||||
import com.ibm.wala.ipa.cha.ClassHierarchy;
|
||||
import com.ibm.wala.ipa.cha.ClassHierarchyException;
|
||||
import com.ibm.wala.util.debug.Assertions;
|
||||
import com.ibm.wala.util.intset.OrdinalSet;
|
||||
|
||||
public class TypeBasedArrayAliasTest extends WalaTestCase {
|
||||
|
||||
public void testTypeBasedArrayAlias() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
|
||||
AnalysisScope scope = CallGraphTestUtil.makeJ2SEAnalysisScope(TestConstants.WALA_TESTDATA, CallGraphTestUtil.REGRESSION_EXCLUSIONS);
|
||||
ClassHierarchy cha = ClassHierarchy.make(scope);
|
||||
Iterable<Entrypoint> entrypoints = com.ibm.wala.ipa.callgraph.impl.Util
|
||||
.makeMainEntrypoints(scope, cha, TestConstants.ARRAY_ALIAS_MAIN);
|
||||
AnalysisOptions options = CallGraphTestUtil.makeAnalysisOptions(scope, entrypoints);
|
||||
|
||||
// RTA yields a TypeBasedPointerAnalysis
|
||||
CallGraphBuilder builder = Util.makeRTABuilder(options, new AnalysisCache(),cha, scope);
|
||||
CallGraph cg = builder.makeCallGraph(options, null);
|
||||
PointerAnalysis pa = builder.getPointerAnalysis();
|
||||
|
||||
CGNode node = findNode(cg, "testMayAlias1");
|
||||
PointerKey pk1 = pa.getHeapModel().getPointerKeyForLocal(node, 1);
|
||||
PointerKey pk2 = pa.getHeapModel().getPointerKeyForLocal(node, 2);
|
||||
assertTrue(mayAliased(pk1, pk2, pa));
|
||||
|
||||
node = findNode(cg, "testMayAlias2");
|
||||
pk1 = pa.getHeapModel().getPointerKeyForLocal(node, 1);
|
||||
pk2 = pa.getHeapModel().getPointerKeyForLocal(node, 2);
|
||||
assertTrue(mayAliased(pk1, pk2, pa));
|
||||
|
||||
node = findNode(cg, "testMayAlias3");
|
||||
pk1 = pa.getHeapModel().getPointerKeyForLocal(node, 1);
|
||||
pk2 = pa.getHeapModel().getPointerKeyForLocal(node, 2);
|
||||
assertTrue(mayAliased(pk1, pk2, pa));
|
||||
}
|
||||
|
||||
private final static CGNode findNode(CallGraph cg, String methodName) {
|
||||
for (Iterator<? extends CGNode> it = cg.iterator(); it.hasNext(); ) {
|
||||
CGNode n = it.next();
|
||||
if (n.getMethod().getName().toString().equals(methodName)) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
Assertions.UNREACHABLE("Unexpected: failed to find " + methodName + " node");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean mayAliased(PointerKey pk1, PointerKey pk2, PointerAnalysis pa) {
|
||||
OrdinalSet<InstanceKey> ptsTo1 = pa.getPointsToSet(pk1);
|
||||
OrdinalSet<InstanceKey> ptsTo2 = pa.getPointsToSet(pk2);
|
||||
boolean foundIntersection = false;
|
||||
outer: for (InstanceKey i : ptsTo1) {
|
||||
for (InstanceKey j : ptsTo2) {
|
||||
if (i.equals(j)) {
|
||||
foundIntersection = true;
|
||||
break outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
return foundIntersection;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -38,6 +38,8 @@ public interface TestConstants {
|
|||
|
||||
public final static String MULTI_DIM_MAIN = "LmultiDim/TestMultiDim";
|
||||
|
||||
public final static String ARRAY_ALIAS_MAIN = "LarrayAlias/TestArrayAlias";
|
||||
|
||||
public final static String REFLECT1_MAIN = "Lreflection/Reflect1";
|
||||
|
||||
public final static String REFLECT2_MAIN = "Lreflection/Reflect2";
|
||||
|
|
|
@ -28,6 +28,7 @@ import com.ibm.wala.ipa.callgraph.propagation.LocalPointerKeyWithFilter;
|
|||
import com.ibm.wala.ipa.callgraph.propagation.PointerKey;
|
||||
import com.ibm.wala.ipa.callgraph.propagation.StaticFieldKey;
|
||||
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
||||
import com.ibm.wala.types.TypeReference;
|
||||
import com.ibm.wala.util.collections.HashMapFactory;
|
||||
import com.ibm.wala.util.collections.HashSetFactory;
|
||||
import com.ibm.wala.util.collections.Iterator2Collection;
|
||||
|
@ -38,15 +39,14 @@ import com.ibm.wala.util.intset.OrdinalSet;
|
|||
|
||||
/**
|
||||
*
|
||||
* A trivial field-based pointer analysis solution, which only uses the
|
||||
* information of which types (classes) are live.
|
||||
* A trivial field-based pointer analysis solution, which only uses the information of which types (classes) are live.
|
||||
*
|
||||
* @author sfink
|
||||
*/
|
||||
public class TypeBasedPointerAnalysis extends AbstractPointerAnalysis {
|
||||
|
||||
private final Collection<IClass> klasses;
|
||||
|
||||
|
||||
private final TypeBasedHeapModel heapModel;
|
||||
|
||||
/**
|
||||
|
@ -55,10 +55,8 @@ public class TypeBasedPointerAnalysis extends AbstractPointerAnalysis {
|
|||
private final Map<IClass, OrdinalSet<InstanceKey>> pointsTo = HashMapFactory.make();
|
||||
|
||||
/**
|
||||
* @param klasses
|
||||
* Collection<IClass>
|
||||
* @throws AssertionError
|
||||
* if klasses is null
|
||||
* @param klasses Collection<IClass>
|
||||
* @throws AssertionError if klasses is null
|
||||
*/
|
||||
private TypeBasedPointerAnalysis(AnalysisOptions options, Collection<IClass> klasses, CallGraph cg) throws AssertionError {
|
||||
super(cg, makeInstanceKeys(klasses));
|
||||
|
@ -67,8 +65,7 @@ public class TypeBasedPointerAnalysis extends AbstractPointerAnalysis {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param c
|
||||
* Collection<IClass>
|
||||
* @param c Collection<IClass>
|
||||
*/
|
||||
private static MutableMapping<InstanceKey> makeInstanceKeys(Collection<IClass> c) {
|
||||
assert c != null;
|
||||
|
@ -105,8 +102,7 @@ public class TypeBasedPointerAnalysis extends AbstractPointerAnalysis {
|
|||
}
|
||||
|
||||
/**
|
||||
* Compute the set of {@link InstanceKey}s which may represent a particular
|
||||
* type.
|
||||
* Compute the set of {@link InstanceKey}s which may represent a particular type.
|
||||
*/
|
||||
private OrdinalSet<InstanceKey> computeOrdinalInstanceSet(IClass type) {
|
||||
Collection<IClass> klasses = null;
|
||||
|
@ -124,7 +120,14 @@ public class TypeBasedPointerAnalysis extends AbstractPointerAnalysis {
|
|||
Collection<IClass> c = HashSetFactory.make();
|
||||
for (IClass klass : klasses) {
|
||||
if (klass.isArrayClass()) {
|
||||
c.add(klass);
|
||||
TypeReference elementType = klass.getReference().getArrayElementType();
|
||||
if (elementType.isPrimitiveType()) {
|
||||
c.add(klass);
|
||||
} else {
|
||||
// just add Object[], since with array typing rules we have no idea
|
||||
// the exact type of array the reference is pointing to
|
||||
c.add(klass.getClassHierarchy().lookupClass(TypeReference.JavaLangObject.getArrayTypeForElementType()));
|
||||
}
|
||||
} else if (this.klasses.contains(klass)) {
|
||||
c.add(klass);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue