KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > spark > builder > ContextInsensitiveBuilder


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.jimple.spark.builder;
21 import soot.jimple.spark.*;
22 import soot.jimple.spark.pag.*;
23 import soot.jimple.toolkits.callgraph.*;
24 import soot.jimple.toolkits.pointer.util.NativeMethodDriver;
25 import soot.jimple.toolkits.pointer.util.NativeHelper;
26 import soot.jimple.toolkits.pointer.DumbPointerAnalysis;
27 import soot.*;
28 import java.util.*;
29 import soot.jimple.*;
30 import soot.jimple.spark.internal.*;
31 import soot.jimple.spark.sets.PointsToSetInternal;
32 import soot.jimple.spark.solver.OnFlyCallGraph;
33 import soot.util.queue.*;
34 import soot.options.*;
35
36 /** A context insensitive pointer assignment graph builder.
37  * @author Ondrej Lhotak
38  */

39 public class ContextInsensitiveBuilder {
40     public void preJimplify() {
41         boolean change = true;
42         while( change ) {
43             change = false;
44             for( Iterator cIt = new ArrayList(Scene.v().getClasses()).iterator(); cIt.hasNext(); ) {
45                 final SootClass c = (SootClass) cIt.next();
46                 for( Iterator mIt = c.methodIterator(); mIt.hasNext(); ) {
47                     final SootMethod m = (SootMethod) mIt.next();
48                     if( !m.isConcrete() ) continue;
49                     if( m.isNative() ) continue;
50                     if( m.isPhantom() ) continue;
51                     if( !m.hasActiveBody() ) {
52                         change = true;
53                         m.retrieveActiveBody();
54                     }
55                 }
56             }
57         }
58     }
59     /** Creates an empty pointer assignment graph. */
60     public PAG setup( SparkOptions opts ) {
61         pag = new PAG( opts );
62         if( opts.simulate_natives() ) {
63             pag.nativeMethodDriver = new NativeMethodDriver(new SparkNativeHelper(pag));
64         }
65         if( opts.on_fly_cg() && !opts.vta() ) {
66             ofcg = new OnFlyCallGraph( (PAG) pag );
67             pag.setOnFlyCallGraph( ofcg );
68         } else {
69             cgb = new CallGraphBuilder( DumbPointerAnalysis.v() );
70         }
71         return pag;
72     }
73     /** Fills in the pointer assignment graph returned by setup. */
74     public void build() {
75         QueueReader callEdges;
76         if( ofcg != null ) {
77             callEdges = ofcg.callGraph().listener();
78             ofcg.build();
79             reachables = ofcg.reachableMethods();
80             reachables.update();
81         } else {
82             callEdges = cgb.getCallGraph().listener();
83             cgb.build();
84             reachables = cgb.reachables();
85         }
86         for( Iterator cIt = Scene.v().getClasses().iterator(); cIt.hasNext(); ) {
87             final SootClass c = (SootClass) cIt.next();
88         handleClass( c );
89     }
90         Stmt s = null;
91         while(callEdges.hasNext()) {
92             Edge e = (Edge) callEdges.next();
93             MethodPAG.v( pag, e.tgt() ).addToPAG(null);
94             pag.addCallTarget( e );
95         }
96
97         if( pag.getOpts().verbose() ) {
98             G.v().out.println( "Total methods: "+totalMethods );
99             G.v().out.println( "Initially reachable methods: "+analyzedMethods );
100             G.v().out.println( "Classes with at least one reachable method: "+classes );
101         }
102     }
103
104     /* End of public methods. */
105     /* End of package methods. */
106     protected void handleClass( SootClass c ) {
107         boolean incedClasses = false;
108     Iterator methodsIt = c.methodIterator();
109     while( methodsIt.hasNext() )
110     {
111         SootMethod m = (SootMethod) methodsIt.next();
112         if( !m.isConcrete() && !m.isNative() ) continue;
113             totalMethods++;
114             if( reachables.contains( m ) ) {
115                 MethodPAG mpag = MethodPAG.v( pag, m );
116                 mpag.build();
117                 mpag.addToPAG(null);
118                 analyzedMethods++;
119                 if( !incedClasses ) {
120                     incedClasses = true;
121                     classes++;
122                 }
123             }
124     }
125     }
126
127
128     private PAG pag;
129     private CallGraphBuilder cgb;
130     private OnFlyCallGraph ofcg;
131     private ReachableMethods reachables;
132     int classes = 0;
133     int totalMethods = 0;
134     int analyzedMethods = 0;
135     int stmts = 0;
136 }
137
138
Popular Tags