1 package Analysis.Disjoint;
4 import java.util.Map.Entry;
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;
14 /////////////////////////////////////////////
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
23 // The effects analysis piggy-backs
24 // on the disjoint reachability analysis,
25 // if requested, to support OoOJava and
26 // potentially other analysis clients.
28 /////////////////////////////////////////////
30 public class EffectsAnalysis {
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;
37 public EffectsAnalysis() {
38 taint2effects = new Hashtable<Taint, Set<Effect>>();
42 public Set<Effect> getEffects(Taint t) {
43 Taint tNoPreds = Canonical.changePredsTo( t,
46 return taint2effects.get(tNoPreds);
49 public Iterator iteratorTaintEffectPairs() {
50 return taint2effects.entrySet().iterator();
53 public Hashtable<Taint, Set<Effect>> getSESEEffects(FlatSESEEnterNode sese){
55 Hashtable<Taint, Set<Effect>> taint2Effects = new Hashtable<Taint, Set<Effect>>();
56 Iterator iter=iteratorTaintEffectPairs();
57 while (iter.hasNext()) {
58 Entry entry = (Entry) iter.next();
59 Taint taint = (Taint) entry.getKey();
60 Set<Effect> effects = (Set<Effect>) entry.getValue();
61 if (taint.getSESE().equals(sese)) {
62 Iterator<Effect> eIter = effects.iterator();
63 while (eIter.hasNext()) {
64 Effect effect = eIter.next();
65 if (taint.getSESE().equals(sese)) {
66 Set<Effect> effectSet = taint2Effects.get(taint);
67 if (effectSet == null) {
68 effectSet = new HashSet<Effect>();
70 effectSet.add(effect);
71 taint2Effects.put(taint, effectSet);
81 public Hashtable<Taint, Set<Effect>> getStallSiteEffects(FlatNode fn, TempDescriptor td){
83 Hashtable<Taint, Set<Effect>> taint2Effects = new Hashtable<Taint, Set<Effect>>();
84 Iterator iter=iteratorTaintEffectPairs();
85 while(iter.hasNext()){
86 Entry entry=(Entry)iter.next();
87 Taint taint=(Taint)entry.getKey();
88 Set<Effect> effects=(Set<Effect>)entry.getValue();
89 if(taint.getStallSite().equals(fn)){
90 Iterator<Effect> eIter=effects.iterator();
91 while (eIter.hasNext()) {
92 Effect effect = eIter.next();
93 if( taint.getStallSite().equals(fn) && taint.getVar().equals(td) ){
94 Set<Effect> effectSet=taint2Effects.get(taint);
96 effectSet=new HashSet<Effect>();
98 effectSet.add(effect);
99 taint2Effects.put(taint, effectSet);
104 return taint2Effects;
108 protected void add(Taint t, Effect e) {
109 if( t.getSESE() != null &&
110 t.getSESE().getIsCallerSESEplaceholder() ) {
114 Taint tNoPreds = Canonical.changePredsTo( t,
115 ReachGraph.predsEmpty
118 Set<Effect> effectSet = taint2effects.get(tNoPreds);
119 if (effectSet == null) {
120 effectSet = new HashSet<Effect>();
123 taint2effects.put(tNoPreds, effectSet);
127 public void analyzeFlatFieldNode(ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
129 VariableNode vn = rg.td2vn.get(rhs);
134 for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
135 RefEdge edge = iterator.next();
136 TaintSet taintSet = edge.getTaints();
137 AllocSite affectedAlloc = edge.getDst().getAllocSite();
138 Effect effect = new Effect(affectedAlloc, Effect.read, fld);
140 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
141 Taint taint = taintSetIter.next();
147 public void analyzeFlatSetFieldNode(ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld, boolean strongUpdate) {
149 VariableNode vn = rg.td2vn.get(lhs);
154 for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
155 RefEdge edge = iterator.next();
156 TaintSet taintSet = edge.getTaints();
157 AllocSite affectedAlloc = edge.getDst().getAllocSite();
158 Effect effect = new Effect(affectedAlloc, Effect.write, fld);
159 Effect effectSU = null;
162 effectSU = new Effect(affectedAlloc, Effect.strongupdate, fld);
165 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
166 Taint taint = taintSetIter.next();
167 add( taint, effect );
170 add( taint, effectSU );
177 public String toString() {
178 return taint2effects.toString();
181 public void writeEffects( String outfile ) {
183 BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
185 bw.write( "Effects\n---------------\n\n" );
187 Iterator meItr = taint2effects.entrySet().iterator();
188 while( meItr.hasNext() ) {
189 Map.Entry me = (Map.Entry) meItr.next();
190 Taint taint = (Taint) me.getKey();
191 Set<Effect> effects = (Set<Effect>) me.getValue();
193 Iterator<Effect> eItr = effects.iterator();
194 while( eItr.hasNext() ) {
195 Effect e = eItr.next();
197 bw.write( taint+"-->"+e+"\n" );
202 } catch( IOException e ) {}
206 * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
207 * return mapMethodContextToMethodEffects.get(mc); }
209 * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
210 * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
211 * MethodEffects meNew = new MethodEffects();
212 * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
216 * public void analyzeFlatCall(OwnershipGraph calleeOG, MethodContext
217 * calleeMC, MethodContext callerMC, FlatCall fc) { if(!methodeffects) return;
218 * MethodEffects me = mapMethodContextToMethodEffects.get(callerMC);
219 * MethodEffects meFlatCall = mapMethodContextToMethodEffects .get(calleeMC);
220 * me.analyzeFlatCall(calleeOG, fc, callerMC, meFlatCall);
221 * mapMethodContextToMethodEffects.put(callerMC, me); }
225 * public void analyzeFlatFieldNode(MethodContext mc, OwnershipGraph og,
226 * TempDescriptor srcDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
227 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
228 * me.analyzeFlatFieldNode(og, srcDesc, fieldDesc);
229 * mapMethodContextToMethodEffects.put(mc, me); }
231 * public void analyzeFlatSetFieldNode(MethodContext mc, OwnershipGraph og,
232 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
233 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
234 * me.analyzeFlatSetFieldNode(og, dstDesc, fieldDesc);
235 * mapMethodContextToMethodEffects.put(mc, me); }
237 * public void analyzeFlatSetElementNode(MethodContext mc, OwnershipGraph og,
238 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
239 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
240 * me.analyzeFlatSetElementNode(og, dstDesc, fieldDesc);
241 * mapMethodContextToMethodEffects.put(mc, me); }
243 * public void analyzeFlatElementNode(MethodContext mc, OwnershipGraph og,
244 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
245 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
246 * me.analyzeFlatElementNode(og, dstDesc, fieldDesc);
247 * mapMethodContextToMethodEffects.put(mc, me); }
250 * public void writeMethodEffectsResult() throws IOException {
252 * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
253 * "MethodEffects_report.txt"));
255 * Set<MethodContext> mcSet = mapMethodContextToMethodEffects.keySet();
256 * Iterator<MethodContext> mcIter = mcSet.iterator(); while (mcIter.hasNext())
257 * { MethodContext mc = mcIter.next(); MethodDescriptor md =
258 * (MethodDescriptor) mc.getDescriptor();
260 * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
262 * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
263 * effectsSet = me.getEffects();
265 * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
266 * md.numParameters() + startIdx; i++) {
268 * String paramName = md.getParamName(i - startIdx);
270 * Set<EffectsKey> effectSet = effectsSet.getReadingSet(i); String keyStr =
271 * "{"; if (effectSet != null) { Iterator<EffectsKey> effectIter =
272 * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
273 * effectIter.next(); keyStr += " " + key; } } keyStr += " }";
274 * bw.write(" Paramter " + paramName + " ReadingSet=" + keyStr + "\n");
276 * effectSet = effectsSet.getWritingSet(new Integer(i)); keyStr = "{"; if
277 * (effectSet != null) { Iterator<EffectsKey> effectIter =
278 * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
279 * effectIter.next(); keyStr += " " + key; } }
281 * keyStr += " }"; bw.write(" Paramter " + paramName + " WritingngSet=" +
288 * bw.close(); } catch (IOException e) { System.err.println(e); }