2010-04-28 16:53:07 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* 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.examples.analysis.dataflow;
|
|
|
|
|
2013-05-26 17:07:46 +00:00
|
|
|
import java.io.ByteArrayInputStream;
|
2011-07-22 20:24:35 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2010-04-28 16:53:07 +00:00
|
|
|
import org.junit.AfterClass;
|
2017-03-18 02:04:17 +00:00
|
|
|
import org.junit.Assert;
|
2010-04-28 16:53:07 +00:00
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import com.ibm.wala.classLoader.IMethod;
|
2018-02-05 23:18:37 +00:00
|
|
|
import com.ibm.wala.classLoader.Language;
|
2010-04-28 16:53:07 +00:00
|
|
|
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.dataflow.IFDS.ISupergraph;
|
|
|
|
import com.ibm.wala.dataflow.IFDS.TabulationResult;
|
|
|
|
import com.ibm.wala.dataflow.graph.BitVectorSolver;
|
2017-02-03 01:33:27 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.AnalysisCacheImpl;
|
2010-04-28 16:53:07 +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.CallGraphBuilderCancelException;
|
|
|
|
import com.ibm.wala.ipa.callgraph.Entrypoint;
|
2017-02-03 01:33:27 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.IAnalysisCacheView;
|
2010-04-28 19:36:22 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.impl.Everywhere;
|
2010-04-28 16:53:07 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.impl.Util;
|
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
|
|
|
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
|
2010-04-28 16:53:07 +00:00
|
|
|
import com.ibm.wala.ipa.cfg.BasicBlockInContext;
|
|
|
|
import com.ibm.wala.ipa.cfg.ExplodedInterproceduralCFG;
|
|
|
|
import com.ibm.wala.ipa.cha.ClassHierarchyException;
|
2017-01-12 21:34:54 +00:00
|
|
|
import com.ibm.wala.ipa.cha.ClassHierarchyFactory;
|
2010-04-28 16:53:07 +00:00
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
|
|
|
import com.ibm.wala.ssa.IR;
|
2010-04-28 19:36:22 +00:00
|
|
|
import com.ibm.wala.ssa.SSAOptions;
|
2010-04-28 16:53:07 +00:00
|
|
|
import com.ibm.wala.ssa.analysis.ExplodedControlFlowGraph;
|
|
|
|
import com.ibm.wala.ssa.analysis.IExplodedBasicBlock;
|
|
|
|
import com.ibm.wala.types.ClassLoaderReference;
|
|
|
|
import com.ibm.wala.types.MethodReference;
|
|
|
|
import com.ibm.wala.util.CancelException;
|
|
|
|
import com.ibm.wala.util.collections.Pair;
|
|
|
|
import com.ibm.wala.util.config.AnalysisScopeReader;
|
2013-05-26 17:07:46 +00:00
|
|
|
import com.ibm.wala.util.config.FileOfClasses;
|
2010-04-28 16:53:07 +00:00
|
|
|
import com.ibm.wala.util.intset.IntIterator;
|
|
|
|
import com.ibm.wala.util.intset.IntSet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests of various flow analysis engines.
|
|
|
|
*/
|
|
|
|
public class DataflowTest extends WalaTestCase {
|
|
|
|
|
|
|
|
private static AnalysisScope scope;
|
|
|
|
|
|
|
|
private static IClassHierarchy cha;
|
|
|
|
|
2013-05-26 17:07:46 +00:00
|
|
|
// more aggressive exclusions to avoid library blowup
|
|
|
|
// in interprocedural tests
|
|
|
|
private static final String EXCLUSIONS = "java\\/awt\\/.*\n" +
|
|
|
|
"javax\\/swing\\/.*\n" +
|
|
|
|
"sun\\/awt\\/.*\n" +
|
|
|
|
"sun\\/swing\\/.*\n" +
|
|
|
|
"com\\/sun\\/.*\n" +
|
|
|
|
"sun\\/.*\n" +
|
|
|
|
"org\\/netbeans\\/.*\n" +
|
|
|
|
"org\\/openide\\/.*\n" +
|
|
|
|
"com\\/ibm\\/crypto\\/.*\n" +
|
|
|
|
"com\\/ibm\\/security\\/.*\n" +
|
|
|
|
"org\\/apache\\/xerces\\/.*\n" +
|
|
|
|
"java\\/security\\/.*\n" +
|
|
|
|
"";
|
|
|
|
|
|
|
|
|
2010-04-28 16:53:07 +00:00
|
|
|
@BeforeClass
|
|
|
|
public static void beforeClass() throws Exception {
|
|
|
|
|
2013-05-26 17:07:46 +00:00
|
|
|
scope = AnalysisScopeReader.readJavaScope(TestConstants.WALA_TESTDATA, null, DataflowTest.class.getClassLoader());
|
2010-04-28 16:53:07 +00:00
|
|
|
|
2013-05-26 17:07:46 +00:00
|
|
|
scope.setExclusions(new FileOfClasses(new ByteArrayInputStream(EXCLUSIONS.getBytes("UTF-8"))));
|
2010-04-28 16:53:07 +00:00
|
|
|
try {
|
2017-01-12 21:34:54 +00:00
|
|
|
cha = ClassHierarchyFactory.make(scope);
|
2010-04-28 16:53:07 +00:00
|
|
|
} catch (ClassHierarchyException e) {
|
2017-03-18 04:38:59 +00:00
|
|
|
throw new Exception(e);
|
2010-04-28 16:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see junit.framework.TestCase#tearDown()
|
|
|
|
*/
|
|
|
|
@AfterClass
|
|
|
|
public static void afterClass() throws Exception {
|
|
|
|
scope = null;
|
|
|
|
cha = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIntraproc1() {
|
2017-02-03 01:33:27 +00:00
|
|
|
IAnalysisCacheView cache = new AnalysisCacheImpl();
|
2010-04-28 16:53:07 +00:00
|
|
|
final MethodReference ref = MethodReference.findOrCreate(ClassLoaderReference.Application, "Ldataflow/StaticDataflow", "test1",
|
|
|
|
"()V");
|
|
|
|
IMethod method = cha.resolveMethod(ref);
|
2010-04-28 19:36:22 +00:00
|
|
|
IR ir = cache.getIRFactory().makeIR(method, Everywhere.EVERYWHERE, SSAOptions.defaultOptions());
|
2010-04-28 16:53:07 +00:00
|
|
|
ExplodedControlFlowGraph ecfg = ExplodedControlFlowGraph.make(ir);
|
|
|
|
IntraprocReachingDefs reachingDefs = new IntraprocReachingDefs(ecfg, cha);
|
|
|
|
BitVectorSolver<IExplodedBasicBlock> solver = reachingDefs.analyze();
|
|
|
|
for (IExplodedBasicBlock ebb : ecfg) {
|
|
|
|
if (ebb.getNumber() == 4) {
|
|
|
|
IntSet out = solver.getOut(ebb).getValue();
|
|
|
|
Assert.assertEquals(1, out.size());
|
|
|
|
Assert.assertTrue(out.contains(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIntraproc2() {
|
2017-02-03 01:33:27 +00:00
|
|
|
IAnalysisCacheView cache = new AnalysisCacheImpl();
|
2010-04-28 16:53:07 +00:00
|
|
|
final MethodReference ref = MethodReference.findOrCreate(ClassLoaderReference.Application, "Ldataflow/StaticDataflow", "test2",
|
|
|
|
"()V");
|
|
|
|
IMethod method = cha.resolveMethod(ref);
|
2010-04-28 19:36:22 +00:00
|
|
|
IR ir = cache.getIRFactory().makeIR(method, Everywhere.EVERYWHERE, SSAOptions.defaultOptions());
|
2010-04-28 16:53:07 +00:00
|
|
|
ExplodedControlFlowGraph ecfg = ExplodedControlFlowGraph.make(ir);
|
|
|
|
IntraprocReachingDefs reachingDefs = new IntraprocReachingDefs(ecfg, cha);
|
|
|
|
BitVectorSolver<IExplodedBasicBlock> solver = reachingDefs.analyze();
|
|
|
|
for (IExplodedBasicBlock ebb : ecfg) {
|
|
|
|
if (ebb.getNumber() == 10) {
|
|
|
|
IntSet out = solver.getOut(ebb).getValue();
|
|
|
|
Assert.assertEquals(2, out.size());
|
|
|
|
Assert.assertTrue(out.contains(0));
|
|
|
|
Assert.assertTrue(out.contains(2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testContextInsensitive() throws IllegalArgumentException, CallGraphBuilderCancelException {
|
|
|
|
Iterable<Entrypoint> entrypoints = com.ibm.wala.ipa.callgraph.impl.Util.makeMainEntrypoints(scope, cha,
|
|
|
|
"Ldataflow/StaticDataflow");
|
|
|
|
AnalysisOptions options = CallGraphTestUtil.makeAnalysisOptions(scope, entrypoints);
|
|
|
|
|
2018-02-05 23:18:37 +00:00
|
|
|
CallGraphBuilder<InstanceKey> builder = Util.makeZeroOneCFABuilder(Language.JAVA, options, new AnalysisCacheImpl(), cha, scope);
|
2010-04-28 16:53:07 +00:00
|
|
|
CallGraph cg = builder.makeCallGraph(options, null);
|
|
|
|
ExplodedInterproceduralCFG icfg = ExplodedInterproceduralCFG.make(cg);
|
|
|
|
ContextInsensitiveReachingDefs reachingDefs = new ContextInsensitiveReachingDefs(icfg, cha);
|
|
|
|
BitVectorSolver<BasicBlockInContext<IExplodedBasicBlock>> solver = reachingDefs.analyze();
|
|
|
|
for (BasicBlockInContext<IExplodedBasicBlock> bb : icfg) {
|
|
|
|
if (bb.getNode().toString().contains("testInterproc")) {
|
|
|
|
IExplodedBasicBlock delegate = bb.getDelegate();
|
|
|
|
if (delegate.getNumber() == 4) {
|
|
|
|
IntSet solution = solver.getOut(bb).getValue();
|
|
|
|
IntIterator intIterator = solution.intIterator();
|
2017-03-12 03:20:51 +00:00
|
|
|
List<Pair<CGNode, Integer>> applicationDefs = new ArrayList<>();
|
2010-04-28 16:53:07 +00:00
|
|
|
while (intIterator.hasNext()) {
|
|
|
|
int next = intIterator.next();
|
2011-07-22 20:24:35 +00:00
|
|
|
final Pair<CGNode, Integer> def = reachingDefs.getNodeAndInstrForNumber(next);
|
|
|
|
if (def.fst.getMethod().getDeclaringClass().getClassLoader().getReference().equals(ClassLoaderReference.Application)) {
|
|
|
|
System.out.println(def);
|
|
|
|
applicationDefs.add(def);
|
|
|
|
}
|
2010-04-28 16:53:07 +00:00
|
|
|
}
|
2011-07-22 20:24:35 +00:00
|
|
|
Assert.assertEquals(2, applicationDefs.size());
|
2010-04-28 16:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testContextSensitive() throws IllegalArgumentException, CancelException {
|
|
|
|
Iterable<Entrypoint> entrypoints = com.ibm.wala.ipa.callgraph.impl.Util.makeMainEntrypoints(scope, cha,
|
|
|
|
"Ldataflow/StaticDataflow");
|
|
|
|
AnalysisOptions options = CallGraphTestUtil.makeAnalysisOptions(scope, entrypoints);
|
|
|
|
|
2018-02-05 23:18:37 +00:00
|
|
|
CallGraphBuilder<InstanceKey> builder = Util.makeZeroOneCFABuilder(Language.JAVA, options, new AnalysisCacheImpl(), cha, scope);
|
2010-04-28 16:53:07 +00:00
|
|
|
CallGraph cg = builder.makeCallGraph(options, null);
|
2017-03-25 22:18:27 +00:00
|
|
|
ContextSensitiveReachingDefs reachingDefs = new ContextSensitiveReachingDefs(cg);
|
2010-04-28 16:53:07 +00:00
|
|
|
TabulationResult<BasicBlockInContext<IExplodedBasicBlock>, CGNode, Pair<CGNode, Integer>> result = reachingDefs.analyze();
|
|
|
|
ISupergraph<BasicBlockInContext<IExplodedBasicBlock>, CGNode> supergraph = reachingDefs.getSupergraph();
|
|
|
|
for (BasicBlockInContext<IExplodedBasicBlock> bb : supergraph) {
|
|
|
|
if (bb.getNode().toString().contains("testInterproc")) {
|
|
|
|
IExplodedBasicBlock delegate = bb.getDelegate();
|
|
|
|
if (delegate.getNumber() == 4) {
|
|
|
|
IntSet solution = result.getResult(bb);
|
|
|
|
IntIterator intIterator = solution.intIterator();
|
2017-03-12 03:20:51 +00:00
|
|
|
List<Pair<CGNode, Integer>> applicationDefs = new ArrayList<>();
|
2010-04-28 16:53:07 +00:00
|
|
|
while (intIterator.hasNext()) {
|
|
|
|
int next = intIterator.next();
|
2011-07-22 20:24:35 +00:00
|
|
|
final Pair<CGNode, Integer> def = reachingDefs.getDomain().getMappedObject(next);
|
|
|
|
if (def.fst.getMethod().getDeclaringClass().getClassLoader().getReference().equals(ClassLoaderReference.Application)) {
|
|
|
|
System.out.println(def);
|
|
|
|
applicationDefs.add(def);
|
|
|
|
}
|
2010-04-28 16:53:07 +00:00
|
|
|
}
|
2011-07-22 20:24:35 +00:00
|
|
|
Assert.assertEquals(1, applicationDefs.size());
|
2010-04-28 16:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|