make demand pointer analysis tests less dependent on standard library implementation

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@4234 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
msridhar1 2011-07-25 18:10:31 +00:00
parent e9199401c9
commit 253444a6ad
15 changed files with 179 additions and 155 deletions

View File

@ -0,0 +1,57 @@
/*******************************************************************************
* 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 demandpa;
/**
* doesn't actually work; just for testing pointer analysis
*/
public class DummyHashMap {
Object[] keys = new Object[10];
Object[] values = new Object[10];
public void put(Object object, Object object2) {
int ind = object.hashCode() % keys.length;
keys[ind] = object;
values[ind] = object2;
}
public Iter keyIter() {
return new Iter() {
public Object next() {
return keys[0];
}
};
}
public Iter valuesIter() {
return new Iter() {
public Object next() {
return values[0];
}
};
}
public Object get(Object key1) {
return values[0];
}
private Iter keyOrValueIter(int type) {
return type == 0 ? keyIter() : valuesIter();
}
public Iter elements() {
return keyOrValueIter(-1);
}
}

View File

@ -0,0 +1,25 @@
/*******************************************************************************
* 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 demandpa;
public class DummyHashSet {
private final DummyHashMap hashMap = new DummyHashMap();
public void add(Object object) {
hashMap.put(object, object);
}
public Iter iterator() {
return hashMap.keyIter();
}
}

View File

@ -0,0 +1,58 @@
/*******************************************************************************
* 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 demandpa;
public class DummyLinkedList {
static class Element {
Element next;
Object data;
}
Element head = null;
public void add(Object o) {
if (head == null) {
head = new Element();
head.data = o;
} else {
Element tmp = head;
while (tmp.next != null) {
tmp = tmp.next;
}
Element newElement = new Element();
newElement.data = o;
tmp.next = newElement;
}
}
public Object get(int ind) {
Element tmp = head;
for (int i = 0; i < ind; i++) {
tmp = tmp.next;
}
return tmp.data;
}
public Iter iterator() {
return new Iter() {
public Object next() {
// just return some arbitrary element, from the point of view of flow-insensitive points-to analysis
Element tmp = head;
while (tmp.data == tmp.next) { // shouldn't be able to interpret this condition
tmp = tmp.next;
}
return tmp.data;
}
};
}
}

View File

@ -10,7 +10,6 @@
*******************************************************************************/
package demandpa;
import java.util.HashSet;
/**
* @author manu
@ -18,10 +17,9 @@ import java.util.HashSet;
*/
public class FlowsToTestHashSet {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashSet s1 = new HashSet();
HashSet s2 = new HashSet();
DummyHashSet s1 = new DummyHashSet();
DummyHashSet s2 = new DummyHashSet();
s1.add(new FlowsToType());
s2.add(new Object());
Object o1 = s1.iterator().next();

View File

@ -37,14 +37,12 @@
*/
package demandpa;
import java.util.HashMap;
public class TestHashMapGet {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashMap s1 = new HashMap();
HashMap s2 = new HashMap();
DummyHashMap s1 = new DummyHashMap();
DummyHashMap s2 = new DummyHashMap();
Object key1 = new Object();
Object key2 = new Object();
s1.put(key1, new Object());

View File

@ -37,14 +37,12 @@
*/
package demandpa;
import java.util.HashSet;
public class TestHashSet {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashSet s1 = new HashSet();
HashSet s2 = new HashSet();
DummyHashSet s1 = new DummyHashSet();
DummyHashSet s2 = new DummyHashSet();
s1.add(new Object());
s2.add(new Object());
Object o1 = s1.iterator().next();

View File

@ -37,23 +37,21 @@
*/
package demandpa;
import java.util.Hashtable;
public class TestHashtableEnum {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Hashtable h1 = new Hashtable();
Hashtable h2 = new Hashtable();
DummyHashMap h1 = new DummyHashMap();
DummyHashMap h2 = new DummyHashMap();
Object key1 = new Object();
Object key2 = new Object();
h1.put(key1, new Object());
h2.put(key2, new Object());
Object o1 = h1.elements().nextElement();
Object o2 = h2.elements().nextElement();
Object o1 = h1.elements().next();
Object o2 = h2.elements().next();
TestUtil.makeVarUsed(o1);
TestUtil.testThisVar(o2);

View File

@ -1,61 +0,0 @@
/*******************************************************************************
* 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.
*
* This file is a derivative of code released by the University of
* California under the terms listed below.
*
* Refinement Analysis Tools is Copyright (c) 2007 The Regents of the
* University of California (Regents). Provided that this notice and
* the following two paragraphs are included in any distribution of
* Refinement Analysis Tools or its derivative work, Regents agrees
* not to assert any of Regents' copyright rights in Refinement
* Analysis Tools against recipient for recipient's reproduction,
* preparation of derivative works, public display, public
* performance, distribution or sublicensing of Refinement Analysis
* Tools and derivative works, in source code and object code form.
* This agreement not to assert does not confer, by implication,
* estoppel, or otherwise any license or rights in any intellectual
* property of Regents, including, but not limited to, any patents
* of Regents or Regents' employees.
*
* IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
* INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE
* AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE AND FURTHER DISCLAIMS ANY STATUTORY
* WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE AND ACCOMPANYING
* DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
* IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
* UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
package demandpa;
import java.util.Hashtable;
public class TestHashtableGet {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Hashtable h1 = new Hashtable();
Hashtable h2 = new Hashtable();
Object key1 = new Object();
Object key2 = new Object();
h1.put(key1, new Object());
h2.put(key2, new Object());
Object o1 = h1.get(key1);
Object o2 = h2.get(key2);
TestUtil.makeVarUsed(o1);
TestUtil.testThisVar(o2);
}
}

View File

@ -37,17 +37,15 @@
*/
package demandpa;
import java.util.LinkedList;
public class TestLinkedList {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
LinkedList l1 = new LinkedList();
LinkedList l2 = new LinkedList();
DummyLinkedList l1 = new DummyLinkedList();
DummyLinkedList l2 = new DummyLinkedList();
l1.add(new Object());
l2.add(new Object());
Object o1 = l1.get(0);

View File

@ -37,17 +37,15 @@
*/
package demandpa;
import java.util.LinkedList;
public class TestLinkedListIter {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
LinkedList l1 = new LinkedList();
LinkedList l2 = new LinkedList();
DummyLinkedList l1 = new DummyLinkedList();
DummyLinkedList l2 = new DummyLinkedList();
l1.add(new Object());
l2.add(new Object());
Object o1 = l1.iterator().next();

View File

@ -90,11 +90,6 @@ public abstract class AbstractPtrTest {
*/
protected final String scopeFile;
/**
* analysis scope from latest test
*/
private AnalysisScope scope = null;
protected AbstractPtrTest(String scopeFile) {
this.scopeFile = scopeFile;
}
@ -162,64 +157,22 @@ public abstract class AbstractPtrTest {
return null;
}
/**
* Test if the computed size of some points-to test matches what is expected.
*
* @param mainClass the main class of the test; we choose a variable from its main() method that is passed to the method
* TestUtil.testThisVar()
* @param expected14Size expected p2set size when using 1.4 libraries
* @param expected15Size expected p2set size when using 1.4 libraries
* @param expected16Size expected p2set size when using 1.4 libraries
* @throws ClassHierarchyException
* @throws IllegalArgumentException
* @throws CancelException
* @throws IOException
*/
protected void doPointsToSizeTest(String mainClass, int expected14Size, int expected15Size, int expected16Size)
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
assert scope == null;
Collection<InstanceKey> pointsTo = getPointsToSetToTest(mainClass);
if (debug) {
System.err.println("points-to for " + mainClass + ": " + pointsTo);
}
assert scope != null;
try {
if (scope.isJava16Libraries()) {
Assert.assertEquals(expected16Size, pointsTo.size());
} else if (scope.isJava15Libraries()) {
Assert.assertEquals(expected15Size, pointsTo.size());
} else if (scope.isJava14Libraries()) {
Assert.assertEquals(expected14Size, pointsTo.size());
} else {
Assertions.UNREACHABLE("unexpected library version");
}
} finally {
// don't hold on to a scope pointer
scope = null;
}
}
protected void doFlowsToSizeTest(String mainClass, int size) throws ClassHierarchyException, IllegalArgumentException,
CancelException, IOException {
assert scope == null;
Collection<PointerKey> flowsTo = getFlowsToSetToTest(mainClass);
if (debug) {
System.err.println("flows-to for " + mainClass + ": " + flowsTo);
}
assert scope != null;
try {
Assert.assertEquals(size, flowsTo.size());
} finally {
// don't hold on to a scope pointer
scope = null;
}
Assert.assertEquals(size, flowsTo.size());
}
private Collection<PointerKey> getFlowsToSetToTest(String mainClass) throws ClassHierarchyException, IllegalArgumentException,
CancelException, IOException {
final DemandRefinementPointsTo dmp = makeDemandPointerAnalysis(mainClass);
// find the single allocation site of FlowsToType, make an InstanceKey, and query it
// find the single allocation site of FlowsToType, make an InstanceKey, and
// query it
CGNode mainMethod = AbstractPtrTest.findMainMethod(dmp.getBaseCallGraph());
InstanceKey keyToQuery = getFlowsToInstanceKey(mainMethod, dmp.getHeapModel());
Collection<PointerKey> flowsTo = dmp.getFlowsTo(keyToQuery).snd;
@ -227,12 +180,13 @@ public abstract class AbstractPtrTest {
}
/**
* returns the instance key corresponding to the single allocation site of type FlowsToType
* returns the instance key corresponding to the single allocation site of
* type FlowsToType
*/
private InstanceKey getFlowsToInstanceKey(CGNode mainMethod, HeapModel heapModel) {
// TODO Auto-generated method stub
TypeReference flowsToTypeRef = TypeReference.findOrCreate(ClassLoaderReference.Application, StringStuff
.deployment2CanonicalTypeString("demandpa.FlowsToType"));
TypeReference flowsToTypeRef = TypeReference.findOrCreate(ClassLoaderReference.Application,
StringStuff.deployment2CanonicalTypeString("demandpa.FlowsToType"));
final IR mainIR = mainMethod.getIR();
if (debug) {
System.err.println(mainIR);
@ -248,7 +202,11 @@ public abstract class AbstractPtrTest {
protected void doPointsToSizeTest(String mainClass, int expectedSize) throws ClassHierarchyException, IllegalArgumentException,
CancelException, IOException {
doPointsToSizeTest(mainClass, expectedSize, expectedSize, expectedSize);
Collection<InstanceKey> pointsTo = getPointsToSetToTest(mainClass);
if (debug) {
System.err.println("points-to for " + mainClass + ": " + pointsTo);
}
Assert.assertEquals(expectedSize, pointsTo.size());
}
private Collection<InstanceKey> getPointsToSetToTest(String mainClass) throws ClassHierarchyException, IllegalArgumentException,
@ -265,7 +223,6 @@ public abstract class AbstractPtrTest {
protected DemandRefinementPointsTo makeDemandPointerAnalysis(String mainClass) throws ClassHierarchyException,
IllegalArgumentException, CancelException, IOException {
AnalysisScope scope = findOrCreateAnalysisScope();
this.scope = scope;
// build a type hierarchy
IClassHierarchy cha = findOrCreateCHA(scope);
@ -279,10 +236,12 @@ public abstract class AbstractPtrTest {
final CallGraph cg = cgBuilder.makeCallGraph(options, null);
// System.err.println(cg.toString());
// MemoryAccessMap mam = new SimpleMemoryAccessMap(cg, cgBuilder.getPointerAnalysis().getHeapModel(), false);
// MemoryAccessMap mam = new SimpleMemoryAccessMap(cg,
// cgBuilder.getPointerAnalysis().getHeapModel(), false);
MemoryAccessMap mam = new PABasedMemoryAccessMap(cg, cgBuilder.getPointerAnalysis());
SSAPropagationCallGraphBuilder builder = Util.makeVanillaZeroOneCFABuilder(options, analysisCache, cha, scope);
DemandRefinementPointsTo fullDemandPointsTo = DemandRefinementPointsTo.makeWithDefaultFlowGraph(cg, builder, mam, cha, options, getStateMachineFactory());
DemandRefinementPointsTo fullDemandPointsTo = DemandRefinementPointsTo.makeWithDefaultFlowGraph(cg, builder, mam, cha, options,
getStateMachineFactory());
return fullDemandPointsTo;
}

View File

@ -86,8 +86,8 @@ public class ContextSensitiveTest extends AbstractPtrTest {
@Test
public void testHashtableEnum() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
// 3 because
// can't tell between key, value, and entry enumerators in Hashtable
doPointsToSizeTest(TestInfo.TEST_HASHTABLE_ENUM, 3);
// can't tell between key and value enumerators in Hashtable
doPointsToSizeTest(TestInfo.TEST_HASHTABLE_ENUM, 2);
}
@Ignore("support for this combination of context sensitivity and on-the-fly call graph refinement is not yet implemented")
@ -135,12 +135,12 @@ public class ContextSensitiveTest extends AbstractPtrTest {
@Test
public void testHashSet() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
doPointsToSizeTest(TestInfo.TEST_HASH_SET, 2, 2, 1);
doPointsToSizeTest(TestInfo.TEST_HASH_SET, 1);
}
@Test
public void testHashMapGet() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
doPointsToSizeTest(TestInfo.TEST_HASHMAP_GET, 2, 1, 1);
doPointsToSizeTest(TestInfo.TEST_HASHMAP_GET, 1);
}
@Test

View File

@ -67,7 +67,7 @@ public class NoRefinePtrTest extends AbstractPtrTest {
@Test
public void testHashSet() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
doPointsToSizeTest(TestInfo.TEST_HASH_SET, 3, 3, 2);
doPointsToSizeTest(TestInfo.TEST_HASH_SET, 2);
}
@Test

View File

@ -85,8 +85,6 @@ public class TestInfo {
public static final String TEST_ARRAY_LIST = "Ldemandpa/TestArrayList";
public static final String TEST_HASHTABLE_GET = "Ldemandpa/TestHashtableGet";
public static final String TEST_HASHTABLE_ENUM = "Ldemandpa/TestHashtableEnum";
public static final String TEST_NASTY_PTRS = "Ldemandpa/TestNastyPtrs";

View File

@ -55,8 +55,8 @@ public class TunedRefinementTest extends AbstractPtrTest {
@Test
public void testHashtableEnum() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
// 3 because
// can't tell between key, value, and entry enumerators in Hashtable
doPointsToSizeTest(TestInfo.TEST_HASHTABLE_ENUM, 3);
// can't tell between key and value enumerators in Hashtable
doPointsToSizeTest(TestInfo.TEST_HASHTABLE_ENUM, 2);
}
// we know this one fails...
@ -107,12 +107,12 @@ public class TunedRefinementTest extends AbstractPtrTest {
@Test
public void testHashSet() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
doPointsToSizeTest(TestInfo.TEST_HASH_SET, 2, 2, 1);
doPointsToSizeTest(TestInfo.TEST_HASH_SET, 1);
}
@Test
public void testHashMapGet() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
doPointsToSizeTest(TestInfo.TEST_HASHMAP_GET, 2, 1, 1);
doPointsToSizeTest(TestInfo.TEST_HASHMAP_GET, 1);
}
@Test