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