KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > spark > solver > Checker


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.solver;
21 import soot.jimple.spark.*;
22 import soot.jimple.spark.pag.*;
23 import soot.jimple.spark.sets.*;
24 import soot.*;
25 import java.util.*;
26
27 /** Checks points-to sets with pointer assignment graph to make sure everything
28  * has been correctly propagated.
29  * @author Ondrej Lhotak
30  */

31
32 public class Checker {
33     public Checker( PAG pag ) { this.pag = pag; }
34     /** Actually does the propagation. */
35     public void check() {
36     for( Iterator it = pag.allocSources().iterator(); it.hasNext(); ) {
37         handleAllocNode( (AllocNode) it.next() );
38     }
39         for( Iterator it = pag.simpleSources().iterator(); it.hasNext(); ) {
40             handleSimples( (VarNode) it.next() );
41         }
42         for( Iterator it = pag.loadSources().iterator(); it.hasNext(); ) {
43             handleLoads( (FieldRefNode) it.next() );
44         }
45         for( Iterator it = pag.storeSources().iterator(); it.hasNext(); ) {
46             handleStores( (VarNode) it.next() );
47         }
48     }
49
50     /* End of public methods. */
51     /* End of package methods. */
52
53     protected void checkAll( final Node container, PointsToSetInternal nodes,
54             final Node upstream ) {
55         nodes.forall( new P2SetVisitor() {
56         public final void visit( Node n ) {
57                 checkNode( container, n, upstream );
58             }
59         } );
60     }
61     protected void checkNode( Node container, Node n, Node upstream ) {
62         if( container.getReplacement() != container )
63             throw new RuntimeException JavaDoc( "container "+container+" is illegal" );
64         if( upstream.getReplacement() != upstream )
65             throw new RuntimeException JavaDoc( "upstream "+upstream+" is illegal" );
66         PointsToSetInternal p2set = container.getP2Set();
67         FastHierarchy fh = pag.getTypeManager().getFastHierarchy();
68         if( !p2set.contains( n )
69                 && ( fh == null || container.getType() == null ||
70                 fh.canStoreType( n.getType(), container.getType() ) ) ) {
71             G.v().out.println( "Check failure: "+container+" does not have "+n
72                     +"; upstream is "+upstream );
73         }
74     }
75     protected void handleAllocNode( AllocNode src ) {
76     Node[] targets = pag.allocLookup( src );
77     for( int i = 0; i < targets.length; i++ ) {
78             checkNode( targets[i], src, src );
79     }
80     }
81
82     protected void handleSimples( VarNode src ) {
83     PointsToSetInternal srcSet = src.getP2Set();
84     if( srcSet.isEmpty() ) return;
85     final Node[] simpleTargets = pag.simpleLookup( src );
86     for( int i = 0; i < simpleTargets.length; i++ ) {
87             checkAll( simpleTargets[i], srcSet, src );
88     }
89     }
90
91     protected void handleStores( final VarNode src ) {
92     final PointsToSetInternal srcSet = src.getP2Set();
93     if( srcSet.isEmpty() ) return;
94     Node[] storeTargets = pag.storeLookup( src );
95     for( int i = 0; i < storeTargets.length; i++ ) {
96             final FieldRefNode fr = (FieldRefNode) storeTargets[i];
97             final SparkField f = fr.getField();
98             fr.getBase().getP2Set().forall( new P2SetVisitor() {
99             public final void visit( Node n ) {
100                     AllocDotField nDotF = pag.makeAllocDotField(
101                         (AllocNode) n, f );
102                     checkAll( nDotF, srcSet, src );
103                 }
104             } );
105     }
106     }
107
108     protected void handleLoads( final FieldRefNode src ) {
109     final Node[] loadTargets = pag.loadLookup( src );
110         final SparkField f = src.getField();
111         src.getBase().getP2Set().forall( new P2SetVisitor() {
112         public final void visit( Node n ) {
113                 AllocDotField nDotF = ((AllocNode)n).dot( f );
114                 if( nDotF == null ) return;
115                 PointsToSetInternal set = nDotF.getP2Set();
116                 if( set.isEmpty() ) return;
117                 for( int i = 0; i < loadTargets.length; i++ ) {
118                     VarNode target = (VarNode) loadTargets[i];
119                     checkAll( target, set, src );
120                 }
121             }
122         } );
123     }
124
125     protected PAG pag;
126 }
127
128
129
130
Popular Tags