1 package Analysis.Disjoint;
6 import IR.FieldDescriptor;
7 import IR.Flat.FlatCall;
8 import IR.Flat.FlatMethod;
9 import IR.Flat.TempDescriptor;
10 import IR.Flat.FlatSESEEnterNode;
12 /////////////////////////////////////////////
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
21 // The effects analysis piggy-backs
22 // on the disjoint reachability analysis,
23 // if requested, to support OoOJava and
24 // potentially other analysis clients.
26 /////////////////////////////////////////////
28 public class EffectsAnalysis {
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;
35 public EffectsAnalysis() {
36 taint2effects = new Hashtable<Taint, Set<Effect>>();
40 public Set<Effect> getEffects(Taint t) {
41 Taint tNoPreds = Canonical.changePredsTo( t,
44 return taint2effects.get(tNoPreds);
47 public Iterator iteratorTaintEffectPairs() {
48 return taint2effects.entrySet().iterator();
52 protected void add(Taint t, Effect e) {
53 if( t.getSESE().getIsCallerSESEplaceholder() ) {
57 Taint tNoPreds = Canonical.changePredsTo( t,
61 Set<Effect> effectSet = taint2effects.get(tNoPreds);
62 if (effectSet == null) {
63 effectSet = new HashSet<Effect>();
66 taint2effects.put(tNoPreds, effectSet);
70 public void analyzeFlatFieldNode(ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
72 VariableNode vn = rg.td2vn.get(rhs);
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);
83 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
84 Taint taint = taintSetIter.next();
90 public void analyzeFlatSetFieldNode(ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld, boolean strongUpdate) {
92 VariableNode vn = rg.td2vn.get(lhs);
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;
105 effectSU = new Effect(affectedAlloc, Effect.strongupdate, fld);
108 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
109 Taint taint = taintSetIter.next();
110 add( taint, effect );
113 add( taint, effectSU );
120 public String toString() {
121 return taint2effects.toString();
124 public void writeEffects( String outfile ) {
126 BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
128 bw.write( "Effects\n---------------\n\n" );
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();
136 Iterator<Effect> eItr = effects.iterator();
137 while( eItr.hasNext() ) {
138 Effect e = eItr.next();
140 bw.write( taint+"-->"+e+"\n" );
145 } catch( IOException e ) {}
149 * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
150 * return mapMethodContextToMethodEffects.get(mc); }
152 * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
153 * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
154 * MethodEffects meNew = new MethodEffects();
155 * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
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); }
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); }
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); }
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); }
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); }
193 * public void writeMethodEffectsResult() throws IOException {
195 * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
196 * "MethodEffects_report.txt"));
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();
203 * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
205 * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
206 * effectsSet = me.getEffects();
208 * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
209 * md.numParameters() + startIdx; i++) {
211 * String paramName = md.getParamName(i - startIdx);
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");
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; } }
224 * keyStr += " }"; bw.write(" Paramter " + paramName + " WritingngSet=" +
231 * bw.close(); } catch (IOException e) { System.err.println(e); }