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 private Hashtable<FlatMethod, EffectSet> fm2effectSet;
31 private Hashtable<FlatSESEEnterNode, EffectSet> sese2effectSet;
33 public EffectsAnalysis() {
34 fm2effectSet = new Hashtable<FlatMethod, EffectSet>();
35 sese2effectSet = new Hashtable<FlatSESEEnterNode, EffectSet>();
38 public void analyzeFlatFieldNode(FlatMethod fmContaining,
39 FlatSESEEnterNode seseContaining,
40 ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
42 VariableNode vn = rg.td2vn.get(rhs);
44 for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
45 RefEdge edge = iterator.next();
46 TaintSet taintSet = edge.getTaints();
47 AllocSite affectedAlloc = edge.getDst().getAllocSite();
48 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
49 Taint taint = taintSetIter.next();
51 EffectSet effectSet = fm2effectSet.get(fmContaining);
52 if (effectSet == null) {
53 effectSet = new EffectSet();
56 Effect effect = new Effect(affectedAlloc, Effect.read, fld);
58 add( fmContaining, taint, effect );
59 add( seseContaining, taint, effect );
64 public void analyzeFlatSetFieldNode(FlatMethod fmContaining,
65 FlatSESEEnterNode seseContaining,
66 ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld, boolean strongUpdate) {
68 VariableNode vn = rg.td2vn.get(lhs);
70 for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
71 RefEdge edge = iterator.next();
72 TaintSet taintSet = edge.getTaints();
73 AllocSite affectedAlloc = edge.getDst().getAllocSite();
74 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
75 Taint taint = taintSetIter.next();
77 Effect effect = new Effect(affectedAlloc, Effect.write, fld);
78 add( fmContaining, taint, effect );
79 add( seseContaining, taint, effect );
82 Effect effectSU = new Effect(affectedAlloc, Effect.strongupdate, fld);
83 add( fmContaining, taint, effect );
84 add( seseContaining, taint, effect );
90 public void analyzeFlatCall(FlatMethod fmContaining, FlatSESEEnterNode seseContaining,
91 FlatMethod fmCallee, Hashtable<Taint, TaintSet> tCallee2tsCaller) {
93 EffectSet esCaller = getEffectSet(fmContaining);
94 if( esCaller == null ) {
95 esCaller = new EffectSet();
98 EffectSet esCallee = getEffectSet(fmCallee);
99 if( esCallee == null ) {
100 esCallee = new EffectSet();
103 Iterator meItr = esCallee.getAllEffectPairs();
104 while( meItr.hasNext() ) {
105 Map.Entry me = (Map.Entry) meItr.next();
106 Taint tCallee = (Taint) me.getKey();
107 HashSet<Effect> effects = (HashSet<Effect>) me.getValue();
109 if( tCallee2tsCaller.containsKey( tCallee ) ) {
111 Iterator<Taint> tItr = tCallee2tsCaller.get( tCallee ).iterator();
112 while( tItr.hasNext() ) {
113 Taint tCaller = tItr.next();
115 Iterator<Effect> eItr = effects.iterator();
116 while( eItr.hasNext() ) {
117 Effect e = eItr.next();
119 esCaller.addEffect( tCaller, e );
122 add( fmContaining, tCaller, esCaller );
123 add( seseContaining, tCaller, esCaller );
129 public EffectSet getEffectSet(FlatMethod fm) {
130 return fm2effectSet.get(fm);
133 public void writeEffectsPerMethodAndRBlock( String outfile ) {
135 BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
137 bw.write( "Effects Per Method\n\n" );
139 Iterator meItr1 = fm2effectSet.entrySet().iterator();
140 while( meItr1.hasNext() ) {
141 Map.Entry me1 = (Map.Entry) meItr1.next();
142 FlatMethod fm = (FlatMethod) me1.getKey();
143 EffectSet es = (EffectSet) me1.getValue();
145 bw.write( "\n"+fm+"\n--------------\n" );
147 Iterator meItr2 = es.getAllEffectPairs();
148 while( meItr2.hasNext() ) {
149 Map.Entry me2 = (Map.Entry) meItr2.next();
150 Taint taint = (Taint) me2.getKey();
151 HashSet<Effect> effects = (HashSet<Effect>) me2.getValue();
153 Iterator<Effect> eItr = effects.iterator();
154 while( eItr.hasNext() ) {
155 Effect e = eItr.next();
157 bw.write( " "+taint+"-->"+e+"\n" );
163 bw.write( "\n\nEffects Per RBlock\n\n" );
165 meItr1 = sese2effectSet.entrySet().iterator();
166 while( meItr1.hasNext() ) {
167 Map.Entry me1 = (Map.Entry) meItr1.next();
168 FlatSESEEnterNode sese = (FlatSESEEnterNode) me1.getKey();
169 EffectSet es = (EffectSet) me1.getValue();
171 bw.write( "\n"+sese.toPrettyString()+"\n--------------\n" );
173 Iterator meItr2 = es.getAllEffectPairs();
174 while( meItr2.hasNext() ) {
175 Map.Entry me2 = (Map.Entry) meItr2.next();
176 Taint taint = (Taint) me2.getKey();
177 HashSet<Effect> effects = (HashSet<Effect>) me2.getValue();
179 Iterator<Effect> eItr = effects.iterator();
180 while( eItr.hasNext() ) {
181 Effect e = eItr.next();
183 bw.write( " "+taint+"-->"+e+"\n" );
189 } catch( IOException e ) {}
192 protected void add( FlatMethod fm, Taint t, Effect e ) {
193 EffectSet es = fm2effectSet.get( fm );
195 es = new EffectSet();
197 es.addEffect( t, e );
199 fm2effectSet.put( fm, es );
202 protected void add( FlatSESEEnterNode sese, Taint t, Effect e ) {
204 if( sese.getIsCallerSESEplaceholder() ) {
208 EffectSet es = sese2effectSet.get( sese );
210 es = new EffectSet();
212 es.addEffect( t, e );
214 sese2effectSet.put( sese, es );
217 protected void add( FlatMethod fm, Taint t, EffectSet es ) {
218 EffectSet esExisting = fm2effectSet.get( fm );
219 if( esExisting == null ) {
220 esExisting = new EffectSet();
223 Iterator meItr = es.getAllEffectPairs();
224 while( meItr.hasNext() ) {
225 Map.Entry me = (Map.Entry) meItr.next();
226 Taint taint = (Taint) me.getKey();
227 HashSet<Effect> effects = (HashSet<Effect>) me.getValue();
229 Iterator<Effect> eItr = effects.iterator();
230 while( eItr.hasNext() ) {
231 Effect e = eItr.next();
233 esExisting.addEffect( taint, e );
237 fm2effectSet.put( fm, esExisting );
240 protected void add( FlatSESEEnterNode sese, Taint t, EffectSet es ) {
242 if( sese.getIsCallerSESEplaceholder() ) {
246 EffectSet esExisting = sese2effectSet.get( sese );
248 if( esExisting == null ) {
249 esExisting = new EffectSet();
252 Iterator meItr = es.getAllEffectPairs();
253 while( meItr.hasNext() ) {
254 Map.Entry me = (Map.Entry) meItr.next();
255 Taint taint = (Taint) me.getKey();
256 HashSet<Effect> effects = (HashSet<Effect>) me.getValue();
258 Iterator<Effect> eItr = effects.iterator();
259 while( eItr.hasNext() ) {
260 Effect e = eItr.next();
262 esExisting.addEffect( taint, e );
266 sese2effectSet.put( sese, esExisting );
271 * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
272 * return mapMethodContextToMethodEffects.get(mc); }
274 * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
275 * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
276 * MethodEffects meNew = new MethodEffects();
277 * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
281 * public void analyzeFlatCall(OwnershipGraph calleeOG, MethodContext
282 * calleeMC, MethodContext callerMC, FlatCall fc) { if(!methodeffects) return;
283 * MethodEffects me = mapMethodContextToMethodEffects.get(callerMC);
284 * MethodEffects meFlatCall = mapMethodContextToMethodEffects .get(calleeMC);
285 * me.analyzeFlatCall(calleeOG, fc, callerMC, meFlatCall);
286 * mapMethodContextToMethodEffects.put(callerMC, me); }
290 * public void analyzeFlatFieldNode(MethodContext mc, OwnershipGraph og,
291 * TempDescriptor srcDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
292 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
293 * me.analyzeFlatFieldNode(og, srcDesc, fieldDesc);
294 * mapMethodContextToMethodEffects.put(mc, me); }
296 * public void analyzeFlatSetFieldNode(MethodContext mc, OwnershipGraph og,
297 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
298 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
299 * me.analyzeFlatSetFieldNode(og, dstDesc, fieldDesc);
300 * mapMethodContextToMethodEffects.put(mc, me); }
302 * public void analyzeFlatSetElementNode(MethodContext mc, OwnershipGraph og,
303 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
304 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
305 * me.analyzeFlatSetElementNode(og, dstDesc, fieldDesc);
306 * mapMethodContextToMethodEffects.put(mc, me); }
308 * public void analyzeFlatElementNode(MethodContext mc, OwnershipGraph og,
309 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
310 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
311 * me.analyzeFlatElementNode(og, dstDesc, fieldDesc);
312 * mapMethodContextToMethodEffects.put(mc, me); }
315 * public void writeMethodEffectsResult() throws IOException {
317 * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
318 * "MethodEffects_report.txt"));
320 * Set<MethodContext> mcSet = mapMethodContextToMethodEffects.keySet();
321 * Iterator<MethodContext> mcIter = mcSet.iterator(); while (mcIter.hasNext())
322 * { MethodContext mc = mcIter.next(); MethodDescriptor md =
323 * (MethodDescriptor) mc.getDescriptor();
325 * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
327 * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
328 * effectsSet = me.getEffects();
330 * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
331 * md.numParameters() + startIdx; i++) {
333 * String paramName = md.getParamName(i - startIdx);
335 * Set<EffectsKey> effectSet = effectsSet.getReadingSet(i); String keyStr =
336 * "{"; if (effectSet != null) { Iterator<EffectsKey> effectIter =
337 * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
338 * effectIter.next(); keyStr += " " + key; } } keyStr += " }";
339 * bw.write(" Paramter " + paramName + " ReadingSet=" + keyStr + "\n");
341 * effectSet = effectsSet.getWritingSet(new Integer(i)); keyStr = "{"; if
342 * (effectSet != null) { Iterator<EffectsKey> effectIter =
343 * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
344 * effectIter.next(); keyStr += " " + key; } }
346 * keyStr += " }"; bw.write(" Paramter " + paramName + " WritingngSet=" +
353 * bw.close(); } catch (IOException e) { System.err.println(e); }