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 // 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;
43 public EffectsAnalysis() {
44 taint2effects = new Hashtable<Taint, Set<Effect>>();
46 sese2te = new Hashtable<FlatSESEEnterNode, Hashtable<Taint, Set<Effect>> >();
47 stallSite2te = new Hashtable<FlatNode, Hashtable<Taint, Set<Effect>> >();
51 public Set<Effect> getEffects(Taint t) {
52 Taint tNoPreds = Canonical.changePredsTo( t,
55 return taint2effects.get(tNoPreds);
58 public Iterator iteratorTaintEffectPairs() {
59 return taint2effects.entrySet().iterator();
62 protected void add(Taint t, Effect e) {
64 Taint tNoPreds = Canonical.changePredsTo( t,
68 // add to the global bag
69 Set<Effect> effectSet = taint2effects.get(tNoPreds);
70 if (effectSet == null) {
71 effectSet = new HashSet<Effect>();
74 taint2effects.put(tNoPreds, effectSet);
76 // add to the alternate retrieval bags
77 if( t.getSESE() != null ) {
78 FlatSESEEnterNode sese = t.getSESE();
80 Hashtable<Taint, Set<Effect>> te = sese2te.get( sese );
82 te = new Hashtable<Taint, Set<Effect>>();
85 Set<Effect> effects = te.get(tNoPreds);
86 if (effects == null) {
87 effects = new HashSet<Effect>();
90 te.put(tNoPreds, effects);
92 sese2te.put(sese, te);
95 assert t.getStallSite() != null;
96 FlatNode stallSite = t.getStallSite();
98 Hashtable<Taint, Set<Effect>> te = stallSite2te.get( stallSite );
100 te = new Hashtable<Taint, Set<Effect>>();
103 Set<Effect> effects = te.get(tNoPreds);
104 if (effects == null) {
105 effects = new HashSet<Effect>();
108 te.put(tNoPreds, effects);
109 stallSite2te.put(stallSite, te);
114 public Hashtable<Taint, Set<Effect>> get( FlatSESEEnterNode sese ) {
115 return sese2te.get(sese);
118 public Hashtable<Taint, Set<Effect>> get( FlatNode stallSite ) {
119 return stallSite2te.get(stallSite);
124 public void analyzeFlatFieldNode(ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
126 VariableNode vn = rg.td2vn.get(rhs);
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);
137 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
138 Taint taint = taintSetIter.next();
144 public void analyzeFlatSetFieldNode(ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld, boolean strongUpdate) {
146 VariableNode vn = rg.td2vn.get(lhs);
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;
159 effectSU = new Effect(affectedAlloc, Effect.strongupdate, fld);
162 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
163 Taint taint = taintSetIter.next();
164 add( taint, effect );
167 add( taint, effectSU );
174 public String toString() {
175 return taint2effects.toString();
178 public void writeEffects( String outfile ) {
180 BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
182 bw.write( "Effects\n---------------\n\n" );
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();
190 Iterator<Effect> eItr = effects.iterator();
191 while( eItr.hasNext() ) {
192 Effect e = eItr.next();
194 bw.write( taint+"-->"+e+"\n" );
199 } catch( IOException e ) {}
203 * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
204 * return mapMethodContextToMethodEffects.get(mc); }
206 * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
207 * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
208 * MethodEffects meNew = new MethodEffects();
209 * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
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); }
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); }
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); }
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); }
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); }
247 * public void writeMethodEffectsResult() throws IOException {
249 * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
250 * "MethodEffects_report.txt"));
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();
257 * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
259 * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
260 * effectsSet = me.getEffects();
262 * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
263 * md.numParameters() + startIdx; i++) {
265 * String paramName = md.getParamName(i - startIdx);
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");
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; } }
278 * keyStr += " }"; bw.write(" Paramter " + paramName + " WritingngSet=" +
285 * bw.close(); } catch (IOException e) { System.err.println(e); }
290 public Hashtable<Taint, Set<Effect>> getAllEffects() {
291 return taint2effects;