2014-10-02 01:32:36 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2014 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
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-09-04 22:56:05 +00:00
|
|
|
/**
|
|
|
|
* 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 com.ibm.wala.demandpa.driver;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Collection;
|
|
|
|
import com.ibm.wala.analysis.reflection.InstanceKeyWithNode;
|
|
|
|
import com.ibm.wala.analysis.typeInference.TypeAbstraction;
|
|
|
|
import com.ibm.wala.analysis.typeInference.TypeInference;
|
2018-02-05 23:18:37 +00:00
|
|
|
import com.ibm.wala.classLoader.Language;
|
2012-09-04 22:56:05 +00:00
|
|
|
import com.ibm.wala.core.tests.callGraph.CallGraphTestUtil;
|
|
|
|
import com.ibm.wala.core.tests.demandpa.TestInfo;
|
|
|
|
import com.ibm.wala.demandpa.alg.DemandRefinementPointsTo;
|
|
|
|
import com.ibm.wala.demandpa.alg.IDemandPointerAnalysis;
|
|
|
|
import com.ibm.wala.demandpa.alg.statemachine.DummyStateMachine;
|
|
|
|
import com.ibm.wala.demandpa.flowgraph.IFlowLabel;
|
|
|
|
import com.ibm.wala.demandpa.util.CallGraphMapUtil;
|
|
|
|
import com.ibm.wala.demandpa.util.MemoryAccessMap;
|
|
|
|
import com.ibm.wala.demandpa.util.SimpleMemoryAccessMap;
|
2017-02-03 01:33:27 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.AnalysisCacheImpl;
|
2012-09-04 22:56:05 +00:00
|
|
|
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.HeapModel;
|
|
|
|
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
|
|
|
|
import com.ibm.wala.ipa.callgraph.propagation.LocalPointerKey;
|
|
|
|
import com.ibm.wala.ipa.callgraph.propagation.PointerAnalysis;
|
|
|
|
import com.ibm.wala.ipa.callgraph.propagation.SSAPropagationCallGraphBuilder;
|
|
|
|
import com.ibm.wala.ipa.cha.ClassHierarchy;
|
|
|
|
import com.ibm.wala.ipa.cha.ClassHierarchyException;
|
2017-01-12 21:34:54 +00:00
|
|
|
import com.ibm.wala.ipa.cha.ClassHierarchyFactory;
|
2012-09-04 22:56:05 +00:00
|
|
|
import com.ibm.wala.ssa.IR;
|
|
|
|
import com.ibm.wala.types.MethodReference;
|
|
|
|
import com.ibm.wala.util.CancelException;
|
|
|
|
import com.ibm.wala.util.config.AnalysisScopeReader;
|
|
|
|
import com.ibm.wala.util.debug.Assertions;
|
|
|
|
import com.ibm.wala.util.intset.OrdinalSet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Driver that tests analysis results against ZeroOneCFA analysis.
|
|
|
|
*
|
|
|
|
* @author Manu Sridharan
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class CompareToZeroOneCFADriver {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param args
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// for (String testCase : TestInfo.ALL_TEST_CASES) {
|
|
|
|
// runUnitTestCase(testCase);
|
|
|
|
// }
|
|
|
|
// runUnitTestCase(TestInfo.TEST_PRIMITIVES);
|
|
|
|
// runApplication("/home/manu/research/DOMO/tests/JLex.jar");
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private static void runUnitTestCase(String mainClass) throws IllegalArgumentException, CancelException, IOException {
|
|
|
|
System.err.println("=======---------------=============");
|
|
|
|
System.err.println(("ANALYZING " + mainClass + "\n\n"));
|
|
|
|
// describe the "scope", what is the program we're analyzing
|
|
|
|
AnalysisScope scope = CallGraphTestUtil.makeJ2SEAnalysisScope(TestInfo.SCOPE_FILE, CallGraphTestUtil.REGRESSION_EXCLUSIONS);
|
|
|
|
Object warnings = new Object();
|
|
|
|
|
|
|
|
// build a type hierarchy
|
|
|
|
ClassHierarchy cha = null;
|
|
|
|
try {
|
2017-01-12 21:34:54 +00:00
|
|
|
cha = ClassHierarchyFactory.make(scope);
|
2012-09-04 22:56:05 +00:00
|
|
|
} catch (ClassHierarchyException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up call graph construction options; mainly what should be considered
|
|
|
|
// entrypoints?
|
|
|
|
Iterable<Entrypoint> entrypoints = com.ibm.wala.ipa.callgraph.impl.Util.makeMainEntrypoints(scope, cha, mainClass);
|
|
|
|
AnalysisOptions options = CallGraphTestUtil.makeAnalysisOptions(scope, entrypoints);
|
|
|
|
|
|
|
|
// run existing pointer analysis
|
|
|
|
doTests(scope, cha, options);
|
|
|
|
System.err.println("ALL FINE");
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private static void runApplication(String appJar) throws IllegalArgumentException, CancelException, IOException {
|
|
|
|
System.err.println("=======---------------=============");
|
|
|
|
System.err.println(("ANALYZING " + appJar + "\n\n"));
|
|
|
|
|
|
|
|
AnalysisScope scope = AnalysisScopeReader.makeJavaBinaryAnalysisScope(appJar, new File(CallGraphTestUtil.REGRESSION_EXCLUSIONS));
|
|
|
|
|
|
|
|
// TODO: return the warning set (need a CAPA type)
|
|
|
|
// invoke DOMO to build a DOMO class hierarchy object
|
|
|
|
Object warnings = new Object();
|
|
|
|
ClassHierarchy cha = null;
|
|
|
|
try {
|
2017-01-12 21:34:54 +00:00
|
|
|
cha = ClassHierarchyFactory.make(scope);
|
2012-09-04 22:56:05 +00:00
|
|
|
} catch (ClassHierarchyException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterable<Entrypoint> entrypoints = com.ibm.wala.ipa.callgraph.impl.Util.makeMainEntrypoints(scope, cha);
|
|
|
|
AnalysisOptions options = new AnalysisOptions(scope, entrypoints);
|
|
|
|
doTests(scope, cha, options);
|
|
|
|
System.err.println("ALL FINE");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void doTests(AnalysisScope scope, final ClassHierarchy cha, AnalysisOptions options) throws IllegalArgumentException, CancelException {
|
2018-02-05 23:18:37 +00:00
|
|
|
final SSAPropagationCallGraphBuilder builder = Util.makeVanillaZeroOneCFABuilder(Language.JAVA, options, new AnalysisCacheImpl(), cha, scope);
|
2012-09-04 22:56:05 +00:00
|
|
|
final CallGraph oldCG = builder.makeCallGraph(options,null);
|
2014-05-20 20:00:06 +00:00
|
|
|
final PointerAnalysis<InstanceKey> pa = builder.getPointerAnalysis();
|
2012-09-04 22:56:05 +00:00
|
|
|
|
|
|
|
// now, run our analysis
|
|
|
|
// build an RTA call graph
|
Fix nearly all Eclipse warnings about using raw types
Along the way, I also converted many "for (;;)" loops into modern
"for (:)" loops. I didn't systematically look for all opportunities
to do this, though. I merely made this change where I was already
converting raw Iterator uses into modern Iterator<...> uses.
Better use of generics also allowed many casts to become statically
redundant. I have removed all such redundant casts.
Only three raw-types warnings remain after this batch of fixes. All
three involve raw uses of CallGraphBuilder. I've tried to fix these
too, but it quickly snowballs into a cascade of changes that may or
may not eventually reach a statically-type-save fixed point. I may
give these last few problem areas another go in the future. For now,
though, the hundreds of other fixes seem worth keeping even if there
are a few stragglers.
This commit may change some public APIs, but only by making weaker
type signatures stronger by replacing raw types with generic types.
For example, we may change something like "Set" into "Set<String>",
but we're not adding new arguments, changing any
underlying (post-generics-erasure) types, etc.
2017-07-09 18:38:35 +00:00
|
|
|
CallGraphBuilder<InstanceKey> rtaBuilder = Util.makeRTABuilder(options, new AnalysisCacheImpl(), cha, scope);
|
2012-09-04 22:56:05 +00:00
|
|
|
final CallGraph cg = rtaBuilder.makeCallGraph(options, null);
|
|
|
|
// System.err.println(cg.toString());
|
|
|
|
|
|
|
|
MemoryAccessMap fam = new SimpleMemoryAccessMap(cg, rtaBuilder.getPointerAnalysis().getHeapModel(), false);
|
|
|
|
|
|
|
|
final IDemandPointerAnalysis dmp = makeDemandPointerAnalysis(options, cha, scope, cg, fam);
|
|
|
|
|
|
|
|
final class Helper {
|
|
|
|
void checkPointersInMethod(CGNode node) {
|
|
|
|
// TODO remove this hack
|
|
|
|
if (node.getMethod().getReference().toString().indexOf("clone()Ljava/lang/Object;") != -1) {
|
|
|
|
System.err.println(("SKIPPING " + node));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CGNode oldNode = CallGraphMapUtil.mapCGNode(node, cg, oldCG);
|
|
|
|
if (oldNode == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
System.err.println(("METHOD " + node));
|
|
|
|
IR ir = node.getIR();
|
|
|
|
TypeInference ti = TypeInference.make(ir, false);
|
|
|
|
for (int i = 1; i <= ir.getSymbolTable().getMaxValueNumber(); i++) {
|
|
|
|
TypeAbstraction t = ti.getType(i);
|
|
|
|
if (t != null) {
|
|
|
|
final HeapModel heapModel = dmp.getHeapModel();
|
|
|
|
LocalPointerKey pk = (LocalPointerKey) heapModel.getPointerKeyForLocal(node, i);
|
|
|
|
LocalPointerKey oldPk = (LocalPointerKey) CallGraphMapUtil.mapPointerKey(pk, cg, oldCG, heapModel);
|
|
|
|
Collection<InstanceKey> p2set = dmp.getPointsTo(pk);
|
2014-05-20 16:59:26 +00:00
|
|
|
OrdinalSet<InstanceKey> otherP2Set = pa.getPointsToSet(oldPk);
|
2012-09-04 22:56:05 +00:00
|
|
|
System.err.println(("OLD POINTS-TO " + otherP2Set));
|
|
|
|
for (InstanceKey key : otherP2Set) {
|
|
|
|
if (knownBug(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
InstanceKey newKey = CallGraphMapUtil.mapInstKey(key, oldCG, cg, heapModel);
|
|
|
|
if (!p2set.contains(newKey)) {
|
|
|
|
System.err.println("BADNESS");
|
|
|
|
System.err.println(("pointer key " + pk));
|
|
|
|
System.err.println(("missing " + newKey));
|
|
|
|
Assertions.UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
Helper h = new Helper();
|
2017-11-28 20:26:09 +00:00
|
|
|
for (CGNode node : cg) {
|
2012-09-04 22:56:05 +00:00
|
|
|
h.checkPointersInMethod(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean knownBug(InstanceKey key) {
|
|
|
|
// if (key instanceof MultiNewArrayAllocationSiteKey) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
if (key instanceof InstanceKeyWithNode) {
|
|
|
|
CGNode node = ((InstanceKeyWithNode) key).getNode();
|
|
|
|
MethodReference methodRef = node.getMethod().getReference();
|
|
|
|
if (methodRef.toString().equals("< Primordial, Ljava/lang/Object, clone()Ljava/lang/Object; >")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static IDemandPointerAnalysis makeDemandPointerAnalysis(AnalysisOptions options, ClassHierarchy cha, AnalysisScope scope,
|
|
|
|
CallGraph cg, MemoryAccessMap fam) {
|
2018-02-05 23:18:37 +00:00
|
|
|
SSAPropagationCallGraphBuilder builder = Util.makeVanillaZeroOneCFABuilder(Language.JAVA, options, new AnalysisCacheImpl(), cha, scope);
|
2012-09-04 22:56:05 +00:00
|
|
|
// return new TestNewGraphPointsTo(cg, builder, fam, cha, warnings);
|
|
|
|
DemandRefinementPointsTo fullDemandPointsTo = DemandRefinementPointsTo.makeWithDefaultFlowGraph(cg, builder, fam, cha, options, new DummyStateMachine.Factory<IFlowLabel>());
|
|
|
|
// fullDemandPointsTo.setOnTheFly(true);
|
|
|
|
// fullDemandPointsTo.setRefineFields(true);
|
|
|
|
return fullDemandPointsTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|