View Javadoc

1   /*
2    * Created on Dec 8, 2003
3    */
4   package joeq.Compiler.Analysis.IPSSA.Apps;
5   
6   import java.util.Collection;
7   import java.util.Iterator;
8   import joeq.Class.jq_Method;
9   import joeq.Compiler.Analysis.IPA.PAResultSelector;
10  import joeq.Compiler.Analysis.IPA.PAResults;
11  import joeq.Compiler.Analysis.IPSSA.IPSSABuilder;
12  import joeq.Compiler.Analysis.IPSSA.SSADefinition;
13  import joeq.Compiler.Analysis.IPSSA.Utils.AnalysisObjectSpec;
14  import joeq.Compiler.Analysis.IPSSA.Utils.ReachabilityTrace;
15  import joeq.Compiler.Analysis.IPSSA.Utils.AnalysisObjectSpec.UnknownAnalysisObjectExeption;
16  import jwutil.util.Assert;
17  import net.sf.javabdd.TypedBDDFactory.TypedBDD;
18  
19  /***
20   * @author V.Benjamin Livshits
21   * This is a sample application that prints all paths between two definitions.
22   * Use one of the subclasses that rely on different sources for def-use data.
23   * 
24   * @see IPSSABuilder.Application
25   * @version $Id: ReachabilityTracerApp.java 2002 2004-10-16 04:13:11Z joewhaley $
26   */
27  public abstract class ReachabilityTracerApp extends IPSSABuilder.Application {
28      protected String _def1_str;
29      protected String _def2_str;
30      private static boolean _verbose = false;
31  
32      ReachabilityTracerApp(IPSSABuilder builder, String name, String[] args) {
33          super(builder, name, args);
34      }
35      
36      private static void usage(String[] argv) {
37          System.err.print("Invalid parameters: ");
38          for(int i = 0; i < argv.length; i++) {
39              System.err.print(argv[i] + " ");
40          }
41          System.err.println("");     
42          
43          System.exit(1);   
44      }
45  
46      /***
47       * This will be added by implementations.
48       * */
49      protected abstract void printPath(String def1_str, String def2_str);
50  
51      protected void parseParams(String[] argv) {
52          if(argv == null) return;
53          if(argv.length < 2) usage(argv);
54          StringBuffer buf = new StringBuffer(); 
55          for(int i = 0; i < argv.length; i++) {
56              String arg = argv[i];
57              if(arg.equals("->")) {
58                  _def1_str = buf.toString();
59                  buf.setLength(0);                    
60              }else{
61                  buf.append(arg);
62                  buf.append(" ");
63              }                
64          }
65          _def2_str = buf.toString();        
66      }
67          
68      public void run() {
69          printPath(_def1_str, _def2_str);        
70      }
71      
72      /***
73       * This one uses PA results directly.
74       * */
75      public static class PAReachabilityTracerApp extends ReachabilityTracerApp {
76          private PAResults _pa;
77  
78          public PAReachabilityTracerApp(){
79               this(null, null, null);
80          }
81       
82          PAReachabilityTracerApp(IPSSABuilder builder, String name, String[] args) {
83              super(builder, name, args);
84          }
85          
86          public void initialize() {
87              //test();
88          }        
89  
90          protected void printPath(String def1_str, String def2_str) {
91              System.err.println("In printPath("+ def1_str+ ", " + def2_str + ")");
92              Assert._assert(_builder != null);
93              _pa = _builder.getPAResults();
94              PAResultSelector sel = new PAResultSelector(_pa);
95              
96              TypedBDD src, dst;
97  
98              try {
99                  // return value node
100                 src = AnalysisObjectSpec.PAObjectSpec.create(sel, def1_str).getBDD();
101                 // parameter
102                 dst = AnalysisObjectSpec.PAObjectSpec.create(sel, def2_str).getBDD();
103             } catch (AnalysisObjectSpec.UnknownAnalysisObjectExeption e) {
104                 System.err.println(e);
105                 return;
106             }
107             
108             sel.collectReachabilityTraces(src, dst);            
109         }
110         
111         /***
112          * Get a BDD for the return node of method.
113          * */                
114         public void test() {
115             Assert._assert(_builder != null);
116             _pa = _builder.getPAResults();
117             Assert._assert(_pa != null);
118             PAResultSelector sel = new PAResultSelector(_pa);
119 
120             TypedBDD ret, param;
121 
122             try {
123                 // return value node
124                 ret = AnalysisObjectSpec.PAObjectSpec.create(sel, "return Main/TT getF").getBDD();
125                 // parameter
126                 param = AnalysisObjectSpec.PAObjectSpec.create(sel, "param Main/TT setF 1").getBDD();
127             }catch(AnalysisObjectSpec.UnknownAnalysisObjectExeption e) {
128                 System.err.println(e);
129                 return;                
130             }
131            
132             sel.collectReachabilityTraces(ret, param);
133         }        
134     }
135     
136     /***
137      * This is one that works on IPSSA.
138      * */
139     public static class IPSSAReachabilityTracerApp extends ReachabilityTracerApp {
140         public IPSSAReachabilityTracerApp(){
141             this(null, null, null);
142         }
143         IPSSAReachabilityTracerApp(IPSSABuilder builder, String name, String[] args) {
144             super(builder, name, args);
145         }
146     
147         protected void printPath(String def1_str, String def2_str) {
148             SSADefinition def1, def2;             
149             System.err.println("In printPath("+ def1_str+ ", " + def2_str + ")");
150             try {
151                 def1 = AnalysisObjectSpec.IPSSAObjectSpec.create(_builder, def1_str).getDefinition();                               
152                 def2 = AnalysisObjectSpec.IPSSAObjectSpec.create(_builder, def2_str).getDefinition();
153             } catch (UnknownAnalysisObjectExeption e) {
154                 System.err.println(e);
155                 return;
156             }
157 
158             Assert._assert(def1 != null); Assert._assert(def2 != null);
159 
160             jq_Method method1 = def1.getMethod();
161             jq_Method method2 = def2.getMethod();
162             if(_verbose) {
163                 System.err.println("Computing all paths between " + def1_str + " in " + method1 + " and " + def2_str + " in " + method2);
164             }
165         
166             printPath(def1, def2);          
167         }
168 
169         protected void printPath(SSADefinition def1, SSADefinition def2) {
170             System.err.println("Calculating paths between " + def1 + " and " + def2);
171             Collection/*ReachabilityTrace*/ traces = ReachabilityTrace.Algorithms.collectReachabilityTraces(def1, def2);
172             for(Iterator iter = traces.iterator(); iter.hasNext(); ) {
173                 ReachabilityTrace trace = (ReachabilityTrace)iter.next();
174                 
175                 System.out.println("\t" + trace.toString());
176             }                       
177         }
178     }
179 }