cleaning up OoOJava and related systems, touching lots of files, these systems are...
[IRC.git] / Robust / src / Analysis / Disjoint / EffectsAnalysis.java
1 package Analysis.Disjoint;
2
3 import java.util.*;
4 import java.util.Map.Entry;
5 import java.io.*;
6
7 import IR.FieldDescriptor;
8 import IR.Flat.FlatCall;
9 import IR.Flat.FlatMethod;
10 import IR.Flat.FlatNode;
11 import IR.Flat.TempDescriptor;
12 import IR.Flat.FlatSESEEnterNode;
13
14 /////////////////////////////////////////////
15 // 
16 //  Effects analysis computes read/write/strong
17 //  update and other sorts of effects for the
18 //  scope of a method or rblock.  The effects
19 //  are associated with the heap roots through
20 //  which a reference to the effect target was
21 //  obtained.
22 //
23 //  The effects analysis piggy-backs
24 //  on the disjoint reachability analysis,
25 //  if requested, to support OoOJava and
26 //  potentially other analysis clients.
27 //
28 /////////////////////////////////////////////
29
30 public class EffectsAnalysis {
31
32   // the effects analysis should combine taints
33   // that match except for predicates--preds just
34   // support interprocedural analysis
35   private Hashtable<Taint, Set<Effect>> taint2effects;
36
37   // redundant views of the effect set for
38   // efficient retrieval
39   private Hashtable<FlatSESEEnterNode, Hashtable<Taint, Set<Effect>> > sese2te;
40   private Hashtable<FlatNode,          Hashtable<Taint, Set<Effect>> > stallSite2te;
41
42
43   public EffectsAnalysis() {
44     taint2effects = new Hashtable<Taint, Set<Effect>>();
45
46     sese2te      = new Hashtable<FlatSESEEnterNode, Hashtable<Taint, Set<Effect>> >();
47     stallSite2te = new Hashtable<FlatNode,          Hashtable<Taint, Set<Effect>> >();
48   }
49
50
51   public Set<Effect> getEffects(Taint t) {
52     Taint tNoPreds = Canonical.changePredsTo( t,
53                                               ReachGraph.predsEmpty
54                                               );
55     return taint2effects.get(tNoPreds);
56   }
57
58   public Iterator iteratorTaintEffectPairs() {
59     return taint2effects.entrySet().iterator();
60   }
61
62   protected void add(Taint t, Effect e) {
63
64     Taint tNoPreds = Canonical.changePredsTo( t,
65                                               ReachGraph.predsEmpty
66                                               );
67
68     // add to the global bag
69     Set<Effect> effectSet = taint2effects.get(tNoPreds);
70     if (effectSet == null) {
71       effectSet = new HashSet<Effect>();
72     }
73     effectSet.add(e);
74     taint2effects.put(tNoPreds, effectSet);
75
76     // add to the alternate retrieval bags
77     if( t.getSESE() != null ) {
78       FlatSESEEnterNode sese = t.getSESE();
79
80       Hashtable<Taint, Set<Effect>> te = sese2te.get( sese );
81       if( te == null ) {
82         te = new Hashtable<Taint, Set<Effect>>();
83       }
84
85       Set<Effect> effects = te.get(tNoPreds);
86       if (effects == null) {
87         effects = new HashSet<Effect>();
88       }
89       effects.add(e);
90       te.put(tNoPreds, effects);
91
92       sese2te.put(sese, te);
93
94     } else {
95       assert t.getStallSite() != null;
96       FlatNode stallSite = t.getStallSite();
97
98       Hashtable<Taint, Set<Effect>> te = stallSite2te.get( stallSite );
99       if( te == null ) {
100         te = new Hashtable<Taint, Set<Effect>>();
101       }
102
103       Set<Effect> effects = te.get(tNoPreds);
104       if (effects == null) {
105         effects = new HashSet<Effect>();
106       }
107       effects.add(e);
108       te.put(tNoPreds, effects);
109       stallSite2te.put(stallSite, te);
110     }    
111   }
112
113
114   public Hashtable<Taint, Set<Effect>> get( FlatSESEEnterNode sese ) {
115     return sese2te.get(sese);
116   }
117
118   public Hashtable<Taint, Set<Effect>> get( FlatNode stallSite ) {
119     return stallSite2te.get(stallSite);
120   }
121
122
123
124   public void analyzeFlatFieldNode(ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
125
126     VariableNode vn = rg.td2vn.get(rhs);
127     if( vn == null ) {
128       return;
129     }
130
131     for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
132       RefEdge   edge          = iterator.next();
133       TaintSet  taintSet      = edge.getTaints();
134       AllocSite affectedAlloc = edge.getDst().getAllocSite();
135       Effect    effect        = new Effect(affectedAlloc, Effect.read, fld);
136
137       for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
138         Taint taint = taintSetIter.next();        
139         add(taint, effect);
140       }
141     }
142   }
143
144   public void analyzeFlatSetFieldNode(ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld, boolean strongUpdate) {
145
146     VariableNode vn = rg.td2vn.get(lhs);
147     if( vn == null ) {
148       return;
149     }
150
151     for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
152       RefEdge   edge          = iterator.next();
153       TaintSet  taintSet      = edge.getTaints();
154       AllocSite affectedAlloc = edge.getDst().getAllocSite();
155       Effect    effect        = new Effect(affectedAlloc, Effect.write, fld);       
156       Effect    effectSU      = null;
157
158       if (strongUpdate) {
159         effectSU = new Effect(affectedAlloc, Effect.strongupdate, fld);
160       }
161
162       for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
163         Taint taint = taintSetIter.next();
164         add( taint, effect );
165
166         if (strongUpdate) {
167           add( taint, effectSU );
168         }
169       }
170     }
171   }
172
173
174   public String toString() {
175     return taint2effects.toString();    
176   }
177
178   public void writeEffects( String outfile ) {
179     try {
180       BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
181       
182       bw.write( "Effects\n---------------\n\n" );
183
184       Iterator meItr = taint2effects.entrySet().iterator();
185       while( meItr.hasNext() ) {
186         Map.Entry   me      = (Map.Entry)   meItr.next();
187         Taint       taint   = (Taint)       me.getKey();
188         Set<Effect> effects = (Set<Effect>) me.getValue();
189
190         Iterator<Effect> eItr = effects.iterator();
191         while( eItr.hasNext() ) {
192           Effect e = eItr.next();
193             
194           bw.write( taint+"-->"+e+"\n" );          
195         }
196       }
197
198       bw.close();
199     } catch( IOException e ) {}
200   }
201
202   /*
203    * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
204    * return mapMethodContextToMethodEffects.get(mc); }
205    * 
206    * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
207    * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
208    * MethodEffects meNew = new MethodEffects();
209    * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
210    */
211
212   /*
213    * public void analyzeFlatCall(OwnershipGraph calleeOG, MethodContext
214    * calleeMC, MethodContext callerMC, FlatCall fc) { if(!methodeffects) return;
215    * MethodEffects me = mapMethodContextToMethodEffects.get(callerMC);
216    * MethodEffects meFlatCall = mapMethodContextToMethodEffects .get(calleeMC);
217    * me.analyzeFlatCall(calleeOG, fc, callerMC, meFlatCall);
218    * mapMethodContextToMethodEffects.put(callerMC, me); }
219    */
220
221   /*
222    * public void analyzeFlatFieldNode(MethodContext mc, OwnershipGraph og,
223    * TempDescriptor srcDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
224    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
225    * me.analyzeFlatFieldNode(og, srcDesc, fieldDesc);
226    * mapMethodContextToMethodEffects.put(mc, me); }
227    * 
228    * public void analyzeFlatSetFieldNode(MethodContext mc, OwnershipGraph og,
229    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
230    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
231    * me.analyzeFlatSetFieldNode(og, dstDesc, fieldDesc);
232    * mapMethodContextToMethodEffects.put(mc, me); }
233    * 
234    * public void analyzeFlatSetElementNode(MethodContext mc, OwnershipGraph og,
235    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
236    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
237    * me.analyzeFlatSetElementNode(og, dstDesc, fieldDesc);
238    * mapMethodContextToMethodEffects.put(mc, me); }
239    * 
240    * public void analyzeFlatElementNode(MethodContext mc, OwnershipGraph og,
241    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
242    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
243    * me.analyzeFlatElementNode(og, dstDesc, fieldDesc);
244    * mapMethodContextToMethodEffects.put(mc, me); }
245    * 
246    * 
247    * public void writeMethodEffectsResult() throws IOException {
248    * 
249    * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
250    * "MethodEffects_report.txt"));
251    * 
252    * Set<MethodContext> mcSet = mapMethodContextToMethodEffects.keySet();
253    * Iterator<MethodContext> mcIter = mcSet.iterator(); while (mcIter.hasNext())
254    * { MethodContext mc = mcIter.next(); MethodDescriptor md =
255    * (MethodDescriptor) mc.getDescriptor();
256    * 
257    * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
258    * 
259    * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
260    * effectsSet = me.getEffects();
261    * 
262    * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
263    * md.numParameters() + startIdx; i++) {
264    * 
265    * String paramName = md.getParamName(i - startIdx);
266    * 
267    * Set<EffectsKey> effectSet = effectsSet.getReadingSet(i); String keyStr =
268    * "{"; if (effectSet != null) { Iterator<EffectsKey> effectIter =
269    * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
270    * effectIter.next(); keyStr += " " + key; } } keyStr += " }";
271    * bw.write("  Paramter " + paramName + " ReadingSet=" + keyStr + "\n");
272    * 
273    * effectSet = effectsSet.getWritingSet(new Integer(i)); keyStr = "{"; if
274    * (effectSet != null) { Iterator<EffectsKey> effectIter =
275    * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
276    * effectIter.next(); keyStr += " " + key; } }
277    * 
278    * keyStr += " }"; bw.write("  Paramter " + paramName + " WritingngSet=" +
279    * keyStr + "\n");
280    * 
281    * } bw.write("\n");
282    * 
283    * }
284    * 
285    * bw.close(); } catch (IOException e) { System.err.println(e); }
286    * 
287    * }
288    */
289   
290   public Hashtable<Taint, Set<Effect>> getAllEffects() {
291     return taint2effects;
292   }
293 }