Do effects as a global space, don't even need to consider call site transform, taints...
[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().getIsCallerSESEplaceholder() ) {
54       return;
55     }
56
57     Taint tNoPreds = Canonical.changePredsTo( t,
58                                               ReachGraph.predsEmpty
59                                               );
60
61     Set<Effect> effectSet = taint2effects.get(tNoPreds);
62     if (effectSet == null) {
63       effectSet = new HashSet<Effect>();
64     }
65     effectSet.add(e);
66     taint2effects.put(tNoPreds, effectSet);
67   }
68
69
70   public void analyzeFlatFieldNode(ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
71
72     VariableNode vn = rg.td2vn.get(rhs);
73     if( vn == null ) {
74       return;
75     }
76
77     for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
78       RefEdge   edge          = iterator.next();
79       TaintSet  taintSet      = edge.getTaints();
80       AllocSite affectedAlloc = edge.getDst().getAllocSite();
81       Effect    effect        = new Effect(affectedAlloc, Effect.read, fld);
82
83       for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
84         Taint taint = taintSetIter.next();        
85         add(taint, effect);
86       }
87     }
88   }
89
90   public void analyzeFlatSetFieldNode(ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld, boolean strongUpdate) {
91
92     VariableNode vn = rg.td2vn.get(lhs);
93     if( vn == null ) {
94       return;
95     }
96
97     for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
98       RefEdge   edge          = iterator.next();
99       TaintSet  taintSet      = edge.getTaints();
100       AllocSite affectedAlloc = edge.getDst().getAllocSite();
101       Effect    effect        = new Effect(affectedAlloc, Effect.write, fld);       
102       Effect    effectSU      = null;
103
104       if (strongUpdate) {
105         effectSU = new Effect(affectedAlloc, Effect.strongupdate, fld);
106       }
107
108       for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
109         Taint taint = taintSetIter.next();
110         add( taint, effect );
111
112         if (strongUpdate) {
113           add( taint, effectSU );
114         }
115       }
116     }
117   }
118
119
120   public String toString() {
121     return taint2effects.toString();    
122   }
123
124   public void writeEffects( String outfile ) {
125     try {
126       BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
127       
128       bw.write( "Effects\n---------------\n\n" );
129
130       Iterator meItr = taint2effects.entrySet().iterator();
131       while( meItr.hasNext() ) {
132         Map.Entry   me      = (Map.Entry)   meItr.next();
133         Taint       taint   = (Taint)       me.getKey();
134         Set<Effect> effects = (Set<Effect>) me.getValue();
135
136         Iterator<Effect> eItr = effects.iterator();
137         while( eItr.hasNext() ) {
138           Effect e = eItr.next();
139             
140           bw.write( taint+"-->"+e+"\n" );          
141         }
142       }
143
144       bw.close();
145     } catch( IOException e ) {}
146   }
147
148   /*
149    * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
150    * return mapMethodContextToMethodEffects.get(mc); }
151    * 
152    * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
153    * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
154    * MethodEffects meNew = new MethodEffects();
155    * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
156    */
157
158   /*
159    * public void analyzeFlatCall(OwnershipGraph calleeOG, MethodContext
160    * calleeMC, MethodContext callerMC, FlatCall fc) { if(!methodeffects) return;
161    * MethodEffects me = mapMethodContextToMethodEffects.get(callerMC);
162    * MethodEffects meFlatCall = mapMethodContextToMethodEffects .get(calleeMC);
163    * me.analyzeFlatCall(calleeOG, fc, callerMC, meFlatCall);
164    * mapMethodContextToMethodEffects.put(callerMC, me); }
165    */
166
167   /*
168    * public void analyzeFlatFieldNode(MethodContext mc, OwnershipGraph og,
169    * TempDescriptor srcDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
170    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
171    * me.analyzeFlatFieldNode(og, srcDesc, fieldDesc);
172    * mapMethodContextToMethodEffects.put(mc, me); }
173    * 
174    * public void analyzeFlatSetFieldNode(MethodContext mc, OwnershipGraph og,
175    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
176    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
177    * me.analyzeFlatSetFieldNode(og, dstDesc, fieldDesc);
178    * mapMethodContextToMethodEffects.put(mc, me); }
179    * 
180    * public void analyzeFlatSetElementNode(MethodContext mc, OwnershipGraph og,
181    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
182    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
183    * me.analyzeFlatSetElementNode(og, dstDesc, fieldDesc);
184    * mapMethodContextToMethodEffects.put(mc, me); }
185    * 
186    * public void analyzeFlatElementNode(MethodContext mc, OwnershipGraph og,
187    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
188    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
189    * me.analyzeFlatElementNode(og, dstDesc, fieldDesc);
190    * mapMethodContextToMethodEffects.put(mc, me); }
191    * 
192    * 
193    * public void writeMethodEffectsResult() throws IOException {
194    * 
195    * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
196    * "MethodEffects_report.txt"));
197    * 
198    * Set<MethodContext> mcSet = mapMethodContextToMethodEffects.keySet();
199    * Iterator<MethodContext> mcIter = mcSet.iterator(); while (mcIter.hasNext())
200    * { MethodContext mc = mcIter.next(); MethodDescriptor md =
201    * (MethodDescriptor) mc.getDescriptor();
202    * 
203    * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
204    * 
205    * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
206    * effectsSet = me.getEffects();
207    * 
208    * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
209    * md.numParameters() + startIdx; i++) {
210    * 
211    * String paramName = md.getParamName(i - startIdx);
212    * 
213    * Set<EffectsKey> effectSet = effectsSet.getReadingSet(i); String keyStr =
214    * "{"; if (effectSet != null) { Iterator<EffectsKey> effectIter =
215    * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
216    * effectIter.next(); keyStr += " " + key; } } keyStr += " }";
217    * bw.write("  Paramter " + paramName + " ReadingSet=" + keyStr + "\n");
218    * 
219    * effectSet = effectsSet.getWritingSet(new Integer(i)); keyStr = "{"; if
220    * (effectSet != null) { Iterator<EffectsKey> effectIter =
221    * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
222    * effectIter.next(); keyStr += " " + key; } }
223    * 
224    * keyStr += " }"; bw.write("  Paramter " + paramName + " WritingngSet=" +
225    * keyStr + "\n");
226    * 
227    * } bw.write("\n");
228    * 
229    * }
230    * 
231    * bw.close(); } catch (IOException e) { System.err.println(e); }
232    * 
233    * }
234    */
235 }