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() != null &&
54 t.getSESE().getIsCallerSESEplaceholder() ) {
58 Taint tNoPreds = Canonical.changePredsTo( t,
62 Set<Effect> effectSet = taint2effects.get(tNoPreds);
63 if (effectSet == null) {
64 effectSet = new HashSet<Effect>();
67 taint2effects.put(tNoPreds, effectSet);
71 public void analyzeFlatFieldNode(ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
73 VariableNode vn = rg.td2vn.get(rhs);
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);
84 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
85 Taint taint = taintSetIter.next();
91 public void analyzeFlatSetFieldNode(ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld, boolean strongUpdate) {
93 VariableNode vn = rg.td2vn.get(lhs);
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;
106 effectSU = new Effect(affectedAlloc, Effect.strongupdate, fld);
109 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
110 Taint taint = taintSetIter.next();
111 add( taint, effect );
114 add( taint, effectSU );
121 public String toString() {
122 return taint2effects.toString();
125 public void writeEffects( String outfile ) {
127 BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
129 bw.write( "Effects\n---------------\n\n" );
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();
137 Iterator<Effect> eItr = effects.iterator();
138 while( eItr.hasNext() ) {
139 Effect e = eItr.next();
141 bw.write( taint+"-->"+e+"\n" );
146 } catch( IOException e ) {}
150 * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
151 * return mapMethodContextToMethodEffects.get(mc); }
153 * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
154 * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
155 * MethodEffects meNew = new MethodEffects();
156 * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
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); }
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); }
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); }
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); }
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); }
194 * public void writeMethodEffectsResult() throws IOException {
196 * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
197 * "MethodEffects_report.txt"));
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();
204 * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
206 * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
207 * effectsSet = me.getEffects();
209 * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
210 * md.numParameters() + startIdx; i++) {
212 * String paramName = md.getParamName(i - startIdx);
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");
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; } }
225 * keyStr += " }"; bw.write(" Paramter " + paramName + " WritingngSet=" +
232 * bw.close(); } catch (IOException e) { System.err.println(e); }