1 19 20 23 24 29 30 package soot.dava.toolkits.base.AST.structuredAnalysis; 31 32 import soot.*; 33 import soot.dava.*; 34 import java.util.*; 35 import soot.jimple.*; 36 import soot.dava.internal.AST.*; 38 39 68 69 70 public class MustMayInitialize extends StructuredAnalysis{ 71 HashMap mapping; 72 DavaFlowSet finalResult; 73 74 public static final int MUST=0; 75 public static final int MAY=1; 76 77 int MUSTMAY; 78 79 public MustMayInitialize(Object analyze,int MUSTorMAY){ 80 super(); 81 mapping = new HashMap(); 82 MUSTMAY=MUSTorMAY; 83 84 setMergeType(); 86 finalResult = (DavaFlowSet)process(analyze,new DavaFlowSet()); 88 89 } 91 92 93 public void setMergeType(){ 94 if(MUSTMAY == MUST){ 96 MERGETYPE=INTERSECTION; 97 } 99 else if(MUSTMAY == MAY){ 100 MERGETYPE=UNION; 101 } 103 else 104 throw new DavaFlowAnalysisException("Only allowed 0 or 1 for MUST or MAY values"); 105 } 106 107 111 public Object newInitialFlow(){ 112 return new DavaFlowSet(); 113 } 114 115 public Object cloneFlowSet(Object flowSet){ 116 if(flowSet instanceof DavaFlowSet){ 117 return ((DavaFlowSet)flowSet).clone(); 118 } 119 else 120 throw new RuntimeException ("cloneFlowSet not implemented for other flowSet types"); 121 } 122 123 124 125 126 130 public Object processUnaryBinaryCondition(ASTUnaryBinaryCondition cond, Object input){ 131 if(!(input instanceof DavaFlowSet)){ 132 throw new DavaFlowAnalysisException("processCondition is not implemented for other flowSet types"); 133 } 134 return input; 135 } 136 137 138 142 public Object processSynchronizedLocal(Local local,Object input){ 143 if(!(input instanceof DavaFlowSet)){ 144 throw new RuntimeException ("processCondition is not implemented for other flowSet types"); 145 } 146 return input; 147 } 148 149 150 151 155 public Object processSwitchKey(Value key,Object input){ 156 if(!(input instanceof DavaFlowSet)){ 157 throw new RuntimeException ("processCondition is not implemented for other flowSet types"); 158 } 159 return input; 160 } 161 162 163 164 165 166 167 168 169 170 171 175 public Object processStatement(Stmt s, Object input){ 176 if(!(input instanceof DavaFlowSet)){ 177 throw new RuntimeException ("processStatement is not implemented for other flowSet types"); 178 } 179 DavaFlowSet inSet = (DavaFlowSet)input; 180 181 184 if(inSet == NOPATH){ 185 return inSet; 186 } 187 188 if(s instanceof DefinitionStmt){ 189 DavaFlowSet toReturn = (DavaFlowSet)cloneFlowSet(inSet); 190 192 Value leftOp = ((DefinitionStmt)s).getLeftOp(); 193 194 SootField field = null;; 195 if(leftOp instanceof Local){ 196 toReturn.add(leftOp); 197 198 199 202 Object temp = mapping.get(leftOp); 203 List defs; 204 205 if(temp == null){ 206 defs = new ArrayList(); 208 } 209 else{ 210 defs = (ArrayList)temp; 211 } 212 defs.add(s); 213 mapping.put(leftOp,defs); 214 215 } 216 else if(leftOp instanceof FieldRef){ 217 field = ((FieldRef)leftOp).getField(); 218 toReturn.add(field); 219 220 223 Object temp = mapping.get(field); 224 List defs; 225 226 if(temp == null){ 227 defs = new ArrayList(); 229 } 230 else{ 231 defs = (ArrayList)temp; 232 } 233 defs.add(s); 234 235 mapping.put(field,defs); 236 } 237 return toReturn; 238 } 239 return input; 240 } 241 242 243 244 public boolean isMayInitialized(SootField field){ 245 if(MUSTMAY == MAY){ 246 Object temp = mapping.get(field); 247 if(temp == null) 248 return false; 249 else{ 250 List list = (List)temp; 251 if(list.size()==0) 252 return false; 253 else 254 return true; 255 } 256 } 257 else 258 throw new RuntimeException ("Cannot invoke isMayInitialized for a MUST analysis"); 259 } 260 261 262 263 public boolean isMayInitialized(Value local){ 264 if(MUSTMAY == MAY){ 265 Object temp = mapping.get(local); 266 if(temp == null) 267 return false; 268 else{ 269 List list = (List)temp; 270 if(list.size()==0) 271 return false; 272 else 273 return true; 274 } 275 } 276 else 277 throw new RuntimeException ("Cannot invoke isMayInitialized for a MUST analysis"); 278 } 279 280 281 282 283 public boolean isMustInitialized(SootField field){ 284 if(MUSTMAY == MUST){ 285 if(finalResult.contains(field)) 286 return true; 287 return false; 288 } 289 else 290 throw new RuntimeException ("Cannot invoke isMustinitialized for a MAY analysis"); 291 } 292 293 public boolean isMustInitialized(Value local){ 294 if(MUSTMAY == MUST){ 295 if(finalResult.contains(local)) 296 return true; 297 return false; 298 } 299 else 300 throw new RuntimeException ("Cannot invoke isMustinitialized for a MAY analysis"); 301 } 302 303 307 public List getDefs(Value local){ 308 Object temp = mapping.get(local); 309 if(temp == null) 310 return null; 311 else 312 return (List)temp; 313 } 314 315 316 320 public List getDefs(SootField field){ 321 Object temp = mapping.get(field); 322 if(temp == null) 323 return null; 324 else 325 return (List)temp; 326 } 327 328 329 } | Popular Tags |