1 package Analysis.Disjoint;
3 import java.util.Hashtable;
4 import java.util.Iterator;
6 import IR.FieldDescriptor;
7 import IR.Flat.FlatCall;
8 import IR.Flat.FlatMethod;
9 import IR.Flat.TempDescriptor;
11 /////////////////////////////////////////////
13 // Effects analysis computes read/write/strong
14 // update and other sorts of effects for the
15 // scope of a method or rblock. The effects
16 // are associated with the heap roots through
17 // which a reference to the effect target was
20 // The effects analysis piggy-backs
21 // on the disjoint reachability analysis,
22 // if requested, to support OoOJava and
23 // potentially other analysis clients.
25 /////////////////////////////////////////////
27 public class EffectsAnalysis {
29 private Hashtable<FlatMethod, EffectSet> mapFlatmethodToEffectset;
31 // private Hashtable<MethodContext, MethodEffects>
32 // mapMethodContextToMethodEffects;
33 // boolean methodeffects = false;
35 public EffectsAnalysis(boolean methodeffects) {
36 mapFlatmethodToEffectset = new Hashtable<FlatMethod, EffectSet>();
39 public void analyzeFlatFieldNode(FlatMethod fmContaining, ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
41 VariableNode vn = rg.td2vn.get(rhs);
43 EffectSet effectSet = mapFlatmethodToEffectset.get(fmContaining);
44 if (effectSet == null) {
45 effectSet = new EffectSet();
48 for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
49 RefEdge edge = iterator.next();
50 TaintSet taintSet = edge.getTaints();
51 AllocSite affectedAlloc = edge.getDst().getAllocSite();
52 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
53 Taint taint = taintSetIter.next();
55 // Effect effect = new Effect(taint.getParamIndex(), taint.getAllocSite(), affectedAlloc, Effect.read, fld);
56 // effectSet.addMethodEffect(taint.getParamIndex(), effect);
61 mapFlatmethodToEffectset.put(fmContaining, effectSet);
65 public void analyzeFlatSetFieldNode(FlatMethod fmContaining, ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld) {
67 VariableNode vn = rg.td2vn.get(lhs);
69 EffectSet effectSet = mapFlatmethodToEffectset.get(fmContaining);
70 if (effectSet == null) {
71 effectSet = new EffectSet();
74 for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
75 RefEdge edge = iterator.next();
76 TaintSet taintSet = edge.getTaints();
77 AllocSite affectedAlloc = edge.getDst().getAllocSite();
78 for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
79 Taint taint = taintSetIter.next();
81 // Effect effect = new Effect(taint.getParamIndex(), taint.getAllocSite(), affectedAlloc, Effect.write, fld);
82 // effectSet.addMethodEffect(taint.getParamIndex(), effect);
87 mapFlatmethodToEffectset.put(fmContaining, effectSet);
91 public EffectSet getEffectSet(FlatMethod fm) {
92 return mapFlatmethodToEffectset.get(fm);
96 * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
97 * return mapMethodContextToMethodEffects.get(mc); }
99 * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
100 * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
101 * MethodEffects meNew = new MethodEffects();
102 * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
106 * public void analyzeFlatCall(OwnershipGraph calleeOG, MethodContext
107 * calleeMC, MethodContext callerMC, FlatCall fc) { if(!methodeffects) return;
108 * MethodEffects me = mapMethodContextToMethodEffects.get(callerMC);
109 * MethodEffects meFlatCall = mapMethodContextToMethodEffects .get(calleeMC);
110 * me.analyzeFlatCall(calleeOG, fc, callerMC, meFlatCall);
111 * mapMethodContextToMethodEffects.put(callerMC, me); }
115 * public void analyzeFlatFieldNode(MethodContext mc, OwnershipGraph og,
116 * TempDescriptor srcDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
117 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
118 * me.analyzeFlatFieldNode(og, srcDesc, fieldDesc);
119 * mapMethodContextToMethodEffects.put(mc, me); }
121 * public void analyzeFlatSetFieldNode(MethodContext mc, OwnershipGraph og,
122 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
123 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
124 * me.analyzeFlatSetFieldNode(og, dstDesc, fieldDesc);
125 * mapMethodContextToMethodEffects.put(mc, me); }
127 * public void analyzeFlatSetElementNode(MethodContext mc, OwnershipGraph og,
128 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
129 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
130 * me.analyzeFlatSetElementNode(og, dstDesc, fieldDesc);
131 * mapMethodContextToMethodEffects.put(mc, me); }
133 * public void analyzeFlatElementNode(MethodContext mc, OwnershipGraph og,
134 * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
135 * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
136 * me.analyzeFlatElementNode(og, dstDesc, fieldDesc);
137 * mapMethodContextToMethodEffects.put(mc, me); }
140 * public void writeMethodEffectsResult() throws IOException {
142 * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
143 * "MethodEffects_report.txt"));
145 * Set<MethodContext> mcSet = mapMethodContextToMethodEffects.keySet();
146 * Iterator<MethodContext> mcIter = mcSet.iterator(); while (mcIter.hasNext())
147 * { MethodContext mc = mcIter.next(); MethodDescriptor md =
148 * (MethodDescriptor) mc.getDescriptor();
150 * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
152 * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
153 * effectsSet = me.getEffects();
155 * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
156 * md.numParameters() + startIdx; i++) {
158 * String paramName = md.getParamName(i - startIdx);
160 * Set<EffectsKey> effectSet = effectsSet.getReadingSet(i); String keyStr =
161 * "{"; if (effectSet != null) { Iterator<EffectsKey> effectIter =
162 * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
163 * effectIter.next(); keyStr += " " + key; } } keyStr += " }";
164 * bw.write(" Paramter " + paramName + " ReadingSet=" + keyStr + "\n");
166 * effectSet = effectsSet.getWritingSet(new Integer(i)); keyStr = "{"; if
167 * (effectSet != null) { Iterator<EffectsKey> effectIter =
168 * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
169 * effectIter.next(); keyStr += " " + key; } }
171 * keyStr += " }"; bw.write(" Paramter " + paramName + " WritingngSet=" +
178 * bw.close(); } catch (IOException e) { System.err.println(e); }