00001 package edu.ksu.cis.bandera.pdgslicer;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 import ca.mcgill.sable.util.*;
00037 import ca.mcgill.sable.soot.*;
00038 import ca.mcgill.sable.soot.jimple.*;
00039 import edu.ksu.cis.bandera.pdgslicer.datastructure.*;
00040 import edu.ksu.cis.bandera.pdgslicer.exceptions.*;
00041 import java.io.*;
00042 import java.util.BitSet;
00043 import java.util.Vector;
00044 import java.util.Enumeration;
00045
00046
00047
00048
00049 public class PreProcess {
00050
00051
00052
00053 private List classInfoList;
00054
00055
00056
00057
00058 private Map methodCritMap = new HashMap();
00059 private boolean emptySliceCriterion = false;
00060
00061
00062
00063 private Set sliceLocalSet = new ArraySet();
00064
00065
00066
00067 public PreProcess(List clsList) {
00068 classInfoList = clsList;
00069 }
00070
00071
00072
00073
00074
00075 boolean emptySliceCriterion() {
00076 return emptySliceCriterion;
00077 }
00078
00079
00080
00081
00082
00083
00084 public void extracting(Vector sliceInterests) {
00085 for (Enumeration e = sliceInterests.elements(); e.hasMoreElements();) {
00086 SliceInterest sliceInterest = (SliceInterest) e.nextElement();
00087 if (sliceInterest instanceof SliceLocal) {
00088 SliceLocal sliceLocal = (SliceLocal) sliceInterest;
00089 sliceLocalSet.add(sliceLocal.getLocal());
00090 }
00091 }
00092 for (Enumeration e = sliceInterests.elements(); e.hasMoreElements();) {
00093 SliceInterest sliceInterest = (SliceInterest) e.nextElement();
00094 if (sliceInterest instanceof SliceLocal) {
00095 SliceLocal sliceLocal = (SliceLocal) sliceInterest;
00096 putLocalToCritMap(sliceLocal.getSootMethod(), sliceLocal.getLocal());
00097 } else
00098 if (sliceInterest instanceof SliceStatement) {
00099 SliceStatement sliceStatement = (SliceStatement) sliceInterest;
00100 putStatementToCritMap(sliceStatement.getSootMethod(), sliceStatement.getStmt());
00101 } else
00102 if (sliceInterest instanceof SlicePoint) {
00103 SlicePoint slicePoint = (SlicePoint) sliceInterest;
00104 putPointToCritMap(slicePoint.getSootMethod(), slicePoint.getStmt());
00105 } else
00106 if (sliceInterest instanceof SliceField) {
00107 SliceField sliceField = (SliceField) sliceInterest;
00108 putFieldToCritMap(sliceField.getSootClass(), sliceField.getSootField());
00109 }
00110 }
00111 putMdCritMapToMdInfo();
00112 }
00113
00114
00115
00116
00117
00118
00119
00120 public void extractingForDL(SootClass[] classes) {
00121 DeadlockRelatedCriterion dlCrit = new DeadlockRelatedCriterion(classes);
00122 Vector sliceInterestForDL = dlCrit.getSliceInterestForDL();
00123 if (sliceInterestForDL.isEmpty())
00124 emptySliceCriterion = true;
00125 else
00126 extracting(sliceInterestForDL);
00127 }
00128
00129
00130
00131
00132
00133
00134
00135 static Map extractRelVarMapFromCriterion(SliceCriterion sc) {
00136 Map relVarMap = new HashMap();
00137 Set slicePoints = sc.getSlicePoints();
00138 Set sliceVars = sc.getSliceVars();
00139 Set variables = new ArraySet();
00140 variables.addAll(sliceVars);
00141 Set sliceStatements = sc.getSliceStatements();
00142 for (Iterator stmtIt = sliceStatements.iterator(); stmtIt.hasNext();) {
00143 Stmt stmt = (Stmt) stmtIt.next();
00144 for (Iterator useBoxIt = stmt.getUseBoxes().iterator(); useBoxIt.hasNext();) {
00145 ValueBox valueBox = (ValueBox) useBoxIt.next();
00146 Value value = valueBox.getValue();
00147 if ((value instanceof Local) || (value instanceof FieldRef))
00148 variables.add(value);
00149 }
00150 }
00151 for (Iterator pointIt = slicePoints.iterator(); pointIt.hasNext();)
00152 relVarMap.put((Stmt) pointIt.next(), variables);
00153 for (Iterator stmtIt = sliceStatements.iterator(); stmtIt.hasNext();) {
00154 Stmt stmt = (Stmt) stmtIt.next();
00155 if (relVarMap.containsKey(stmt))
00156 continue;
00157 relVarMap.put(stmt, variables);
00158 }
00159 return relVarMap;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 private Set getInstanceFieldRef(Map localAssMap, SootField sootField) {
00172 Set returnSet = new ArraySet();
00173 for (Iterator localIt = localAssMap.keySet().iterator(); localIt.hasNext();) {
00174 Value defVar = (Value) localIt.next();
00175 if (!(defVar instanceof InstanceFieldRef))
00176 continue;
00177 InstanceFieldRef defInsRef = (InstanceFieldRef) defVar;
00178 SootField insFd = defInsRef.getField();
00179 Value baseValue = defInsRef.getBase();
00180 if (baseValue instanceof Local) {
00181 Local baseLocal = (Local) baseValue;
00182 if (insFd.equals(sootField))
00183
00184 returnSet.add(defInsRef);
00185 }
00186 }
00187 return returnSet;
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 private Set getStaticFieldRef(Map localAssMap, SootField staticField) {
00200 Set returnSet = new ArraySet();
00201 for (Iterator localIt = localAssMap.keySet().iterator(); localIt.hasNext();) {
00202 Object definedVar = localIt.next();
00203 if (definedVar instanceof StaticFieldRef) {
00204 StaticFieldRef sfr = (StaticFieldRef) definedVar;
00205 SootField definedFd = sfr.getField();
00206 if (staticField.equals(definedFd)) {
00207 returnSet.add(sfr);
00208 }
00209 }
00210 }
00211 return returnSet;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 private void putFieldToCritMap(SootClass sootClass, SootField field) {
00226 int modifiers = field.getModifiers();
00227 if (Modifier.isStatic(modifiers))
00228 putStaticFieldToCritMap(sootClass, field);
00229 else
00230 if (Modifier.isPrivate(modifiers))
00231 putPrivateInsFdToCritMap(sootClass, field);
00232 else {
00233 System.out.println("now, we treat fields rather than static and private as public simply, need further programming on protected fields");
00234 putPubInstanceFdToCritMap(sootClass, field);
00235 }
00236
00237
00238
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 private void putFieldToCritMap(SootMethod sootMethod, FieldRef field) {
00251 if (methodCritMap.containsKey(sootMethod)) {
00252 SliceCriterion criterion = (SliceCriterion) methodCritMap.get(sootMethod);
00253 criterion.getSliceVars().add(field);
00254 } else {
00255 Set varSet = new ArraySet();
00256 varSet.add(field);
00257 methodCritMap.put(sootMethod, new SliceCriterion(new ArraySet(), varSet, new ArraySet()));
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 private void putLocalToCritMap(SootMethod sootMethod, Local local) {
00271 if (methodCritMap.containsKey(sootMethod)) {
00272 SliceCriterion criterion = (SliceCriterion) methodCritMap.get(sootMethod);
00273 criterion.getSliceVars().add(local);
00274 } else {
00275 Set varSet = new ArraySet();
00276 varSet.add(local);
00277 methodCritMap.put(sootMethod, new SliceCriterion(new ArraySet(), varSet, new ArraySet()));
00278 }
00279 }
00280
00281
00282
00283
00284
00285 private void putMdCritMapToMdInfo() {
00286 for (Iterator keyIt = methodCritMap.keySet().iterator(); keyIt.hasNext();) {
00287 SootMethod sootMethod = (SootMethod) keyIt.next();
00288
00289
00290
00291
00292
00293
00294 MethodInfo mdInfo = (MethodInfo) Slicer.sootMethodInfoMap.get(sootMethod);
00295 if (mdInfo == null) continue;
00296 mdInfo.sCriterion = (SliceCriterion) methodCritMap.get(sootMethod);
00297 Map relVarMap = extractRelVarMapFromCriterion(mdInfo.sCriterion);
00298 mdInfo.sCriterion.setRelVarMap(relVarMap);
00299 }
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 private void putPointToCritMap(SootMethod sootMethod, Stmt point) {
00312 if (methodCritMap.containsKey(sootMethod)) {
00313 SliceCriterion criterion = (SliceCriterion) methodCritMap.get(sootMethod);
00314 criterion.getSlicePoints().add(point);
00315 } else {
00316 Set pointSet = new ArraySet();
00317 pointSet.add(point);
00318 methodCritMap.put(sootMethod, new SliceCriterion(pointSet, new ArraySet(), new ArraySet()));
00319 }
00320 }
00321
00322
00323
00324
00325 private void putPrivateInsFdToCritMap(SootClass sootClass, SootField field) {
00326 for (Iterator classIt = classInfoList.iterator(); classIt.hasNext();) {
00327 ClassInfo classInfo = (ClassInfo) classIt.next();
00328 if (!classInfo.sootClass.equals(sootClass))
00329 continue;
00330 for (Iterator mdIt = classInfo.methodsInfoList.iterator(); mdIt.hasNext();) {
00331 MethodInfo mdInfo = (MethodInfo) mdIt.next();
00332 Set fieldRefs = getInstanceFieldRef(mdInfo.indexMaps.localAssMap(), field);
00333 for (Iterator refIt = fieldRefs.iterator(); refIt.hasNext();){
00334 FieldRef fieldRef = (FieldRef) refIt.next();
00335 putFieldToCritMap(mdInfo.sootMethod, fieldRef);
00336 }
00337 }
00338 }
00339 }
00340
00341
00342
00343
00344 private void putPubInstanceFdToCritMap(SootClass sootClass, SootField field) {
00345 for (Iterator classIt = classInfoList.iterator(); classIt.hasNext();) {
00346 ClassInfo classInfo = (ClassInfo) classIt.next();
00347 for (Iterator mdIt = classInfo.methodsInfoList.iterator(); mdIt.hasNext();) {
00348 MethodInfo mdInfo = (MethodInfo) mdIt.next();
00349 Set fieldRefs = getInstanceFieldRef(mdInfo.indexMaps.localAssMap(), field);
00350 for (Iterator refIt = fieldRefs.iterator(); refIt.hasNext();)
00351 {
00352 FieldRef fieldRef = (FieldRef) refIt.next();
00353 putFieldToCritMap(mdInfo.sootMethod, fieldRef);
00354 }
00355 }
00356 }
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 private void putStatementToCritMap(SootMethod sootMethod, Stmt stmt) {
00369 if (methodCritMap.containsKey(sootMethod)) {
00370 SliceCriterion criterion = (SliceCriterion) methodCritMap.get(sootMethod);
00371 criterion.getSliceStatements().add(stmt);
00372 } else {
00373 Set stmtSet = new ArraySet();
00374 stmtSet.add(stmt);
00375 methodCritMap.put(sootMethod, new SliceCriterion(new ArraySet(), new ArraySet(), stmtSet));
00376 }
00377 }
00378
00379
00380
00381
00382 private void putStaticFieldToCritMap(SootClass sootClass, SootField staticField) {
00383 for (Iterator classIt = classInfoList.iterator(); classIt.hasNext();) {
00384 ClassInfo classInfo = (ClassInfo) classIt.next();
00385 for (Iterator mdIt = classInfo.methodsInfoList.iterator(); mdIt.hasNext();) {
00386 MethodInfo mdInfo = (MethodInfo) mdIt.next();
00387 Set staticFdRefs = getStaticFieldRef(mdInfo.indexMaps.localAssMap(), staticField);
00388 for (Iterator refIt = staticFdRefs.iterator(); refIt.hasNext();){
00389 FieldRef staticFdRef = (FieldRef) refIt.next();
00390 putFieldToCritMap(mdInfo.sootMethod, staticFdRef);
00391 }
00392 }
00393 }
00394 }
00395 }