More pieces for new version of analysis
[IRC.git] / Robust / src / Analysis / OwnershipAnalysis / ReferenceEdge.java
1 package Analysis.OwnershipAnalysis;
2
3 import IR.*;
4 import IR.Flat.*;
5
6
7 public class ReferenceEdge {
8
9   // null descriptors mean "any field"
10   protected TypeDescriptor type;
11   protected String field;
12
13   protected boolean isInitialParam;
14
15   protected ReachabilitySet beta;
16   protected ReachabilitySet betaNew;
17
18   protected OwnershipNode src;
19   protected HeapRegionNode dst;
20   private int taintIdentifier;
21   private int SESEtaintIdentifier;
22
23
24   public ReferenceEdge(OwnershipNode src,
25                        HeapRegionNode dst,
26                        TypeDescriptor type,
27                        String field,
28                        boolean isInitialParam,
29                        ReachabilitySet beta) {
30
31     this.src                     = src;
32     this.dst                     = dst;
33     this.type                    = type;
34     this.field                   = field;
35     this.isInitialParam = isInitialParam;
36     this.taintIdentifier = 0;
37     this.SESEtaintIdentifier = 0;
38
39     if( beta != null ) {
40       this.beta = beta;
41     } else {
42       this.beta = new ReachabilitySet().makeCanonical();
43     }
44
45     // when edges are not undergoing a transitional operation
46     // that is changing beta info, betaNew is always empty
47     betaNew = new ReachabilitySet().makeCanonical();
48   }
49
50
51   public ReferenceEdge copy() {
52           ReferenceEdge copy= new ReferenceEdge(src,
53                              dst,
54                              type,
55                              field,
56                              isInitialParam,
57                              beta);
58           copy.setTaintIdentifier(this.taintIdentifier);
59           return copy;
60   }
61
62
63   public boolean equals(Object o) {
64     if( o == null ) {
65       return false;
66     }
67
68     if( !(o instanceof ReferenceEdge) ) {
69       return false;
70     }
71
72     ReferenceEdge edge = (ReferenceEdge) o;
73
74     if( !typeEquals( edge.type ) ) {
75       return false;
76     }
77
78     if( !fieldEquals( edge.field ) ) {
79       return false;
80     }
81
82     // Equality of edges is only valid within a graph, so
83     // compare src and dst by reference
84     if( !(src == edge.src) ||
85         !(dst == edge.dst)   ) {
86       return false;
87     }
88
89     return true;
90   }
91
92
93   public boolean equalsIncludingBeta(ReferenceEdge edge) {
94     return equals(edge) && beta.equals(edge.beta);
95   }
96
97
98   public int hashCode() {
99     int hash = 0;
100
101     if( type != null ) {
102       hash += type.hashCode()*17;
103     }
104
105     if( field != null ) {
106       hash += field.hashCode()*7;
107     }
108
109     hash += src.hashCode()*11;
110     hash += dst.hashCode();
111
112     return hash;
113   }
114
115
116   public OwnershipNode getSrc() {
117     return src;
118   }
119
120   public void setSrc(OwnershipNode on) {
121     assert on != null;
122     src = on;
123   }
124
125   public HeapRegionNode getDst() {
126     return dst;
127   }
128
129   public void setDst(HeapRegionNode hrn) {
130     assert hrn != null;
131     dst = hrn;
132   }
133
134
135   public TypeDescriptor getType() {
136     return type;
137   }
138
139   public void setType( TypeDescriptor td ) {
140     type = td;
141   }
142
143   public String getField() {
144     return field;
145   }
146
147   public void setField( String s ) {
148     field = s;
149   }
150
151
152   public boolean typeEquals( TypeDescriptor td ) {
153     if( type == null && td == null ) {
154       return true;
155     }
156     if( type == null ) {
157       return false;
158     }
159     return type.equals( td );
160   }
161
162   public boolean fieldEquals( String s ) {
163     if( field == null && s == null ) {
164       return true;
165     }
166     if( field == null ) {
167       return false;
168     }
169     return field.equals( s );
170   }
171
172   public boolean typeAndFieldEquals( ReferenceEdge e ) {
173     return typeEquals ( e.getType()  ) &&
174            fieldEquals( e.getField() );
175   }
176
177
178   public boolean isInitialParam() {
179     return isInitialParam;
180   }
181
182   public void setIsInitialParam(boolean isInitialParam) {
183     this.isInitialParam = isInitialParam;
184   }
185
186
187   public ReachabilitySet getBeta() {
188     return beta;
189   }
190
191   public void setBeta(ReachabilitySet beta) {
192     assert beta != null;
193     this.beta = beta;
194   }
195
196   public ReachabilitySet getBetaNew() {
197     return betaNew;
198   }
199
200   public void setBetaNew(ReachabilitySet beta) {
201     assert beta != null;
202     this.betaNew = beta;
203   }
204
205   public void applyBetaNew() {
206     assert betaNew != null;
207
208     beta    = betaNew;
209     betaNew = new ReachabilitySet().makeCanonical();
210   }
211
212
213   public String toGraphEdgeString(boolean hideSubsetReachability,
214                                   boolean hideEdgeTaints) {
215     String edgeLabel = "";
216
217     if (type != null) {
218       edgeLabel += type.toPrettyString() + "\\n";
219     }
220
221     if (field != null) {
222       edgeLabel += field + "\\n";
223     }
224
225     if (isInitialParam) {
226       edgeLabel += "*init*\\n";
227     }
228
229     if( !hideEdgeTaints ) {
230       edgeLabel += "*taint*=" + Integer.toBinaryString(taintIdentifier)
231         + "\\n*SESE*=" + Integer.toBinaryString(SESEtaintIdentifier)
232         + "\\n";
233     }
234
235     edgeLabel += beta.toStringEscapeNewline(hideSubsetReachability);
236       
237     return edgeLabel;
238   }
239
240   public String toString() {
241     if( type != null ) {
242       return new String("("+src+"->"+type.toPrettyString()+" "+field+"->"+dst+")");
243     }
244
245     return new String("("+src+"->"+type+" "+field+"->"+dst+")");
246   }
247   
248   public void tainedBy(Integer paramIdx){
249           int newTaint=(int) Math.pow(2, paramIdx.intValue());
250           taintIdentifier=taintIdentifier | newTaint;
251   }
252   
253   public void setTaintIdentifier(int newTaint){
254           taintIdentifier=newTaint;
255   }
256   
257   public void unionTaintIdentifier(int newTaint){
258           taintIdentifier=taintIdentifier | newTaint;
259   }
260   
261   public void minusTaintIdentifier(int removedTaint){
262           taintIdentifier = taintIdentifier & (~removedTaint);
263   }
264   
265   public int getTaintIdentifier(){
266           return taintIdentifier;
267   }
268   
269   public int getSESETaintIdentifier(){
270           return SESEtaintIdentifier;
271   }
272   
273   public void setSESETaintIdentifier(int newTaint){
274           SESEtaintIdentifier=newTaint;
275   }
276   
277   public void unionSESETaintIdentifier(int newTaint){
278           SESEtaintIdentifier=SESEtaintIdentifier | newTaint;
279   }
280   
281   
282 }