- private void checkSharedLocationResult() {
-
- // mapping of method containing ssjava loop has the final result of
- // shared location analysis
-
- ClearingSummary result =
- mapMethodDescriptorToCompleteClearingSummary.get(methodContainingSSJavaLoop);
-
- String str = generateNotClearedResult(result);
- if (str.length() > 0) {
- throw new Error(
- "Following concrete locations of the shared abstract location are not cleared at the same time:\n"
- + str);
- }
-
- }
-
- private String generateNotClearedResult(ClearingSummary result) {
- Set<NTuple<Descriptor>> keySet = result.keySet();
-
- StringBuffer str = new StringBuffer();
- for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
- NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
- SharedStatus status = result.get(hpKey);
- Hashtable<Location, Pair<Set<Descriptor>, Boolean>> map = status.getMap();
- Set<Location> locKeySet = map.keySet();
- for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
- Location locKey = (Location) iterator2.next();
- if (status.haveWriteEffect(locKey)) {
- Pair<Set<Descriptor>, Boolean> pair = map.get(locKey);
- if (!pair.getSecond().booleanValue()) {
- // not cleared!
- str.append("- Concrete locations of the shared location '" + locKey
- + "' are not cleared out, which are reachable through the heap path '" + hpKey
- + ".\n");
- }
- }
- }
- }
-
- return str.toString();
-
- }
-
- private void writeReadMapFile() {
-
- String fileName = "SharedLocationReadMap";
-
- try {
- BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".txt"));
-
- Set<MethodDescriptor> keySet = mapMethodDescriptorToReadSummary.keySet();
- for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
- MethodDescriptor mdKey = (MethodDescriptor) iterator.next();
- ReadSummary summary = mapMethodDescriptorToReadSummary.get(mdKey);
- bw.write("Method " + mdKey + "::\n");
- bw.write(summary + "\n\n");
- }
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
- private void sharedLocationAnalysis() {
- // verify that all concrete locations of shared location are cleared out at
- // the same time once per the out-most loop
-
- computeSharedCoverSet();
-
- if (state.SSJAVADEBUG) {
- writeReadMapFile();
- }
-
- // methodDescriptorsToVisitStack.clear();
- // methodDescriptorsToVisitStack.add(sortedDescriptors.peekFirst());
-
- LinkedList<MethodDescriptor> descriptorListToAnalyze =
- (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
-
- // current descriptors to visit in fixed-point interprocedural analysis,
- // prioritized by
- // dependency in the call graph
- methodDescriptorsToVisitStack.clear();
-
- Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
- methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
-
- while (!descriptorListToAnalyze.isEmpty()) {
- MethodDescriptor md = descriptorListToAnalyze.removeFirst();
- methodDescriptorsToVisitStack.add(md);
- }
-
- // analyze scheduled methods until there are no more to visit
- while (!methodDescriptorsToVisitStack.isEmpty()) {
- MethodDescriptor md = methodDescriptorsToVisitStack.pop();
-
- ClearingSummary completeSummary =
- sharedLocation_analyzeMethod(md, (md.equals(methodContainingSSJavaLoop)));
-
- ClearingSummary prevCompleteSummary = mapMethodDescriptorToCompleteClearingSummary.get(md);
-
- if (!completeSummary.equals(prevCompleteSummary)) {
-
- mapMethodDescriptorToCompleteClearingSummary.put(md, completeSummary);
-
- // results for callee changed, so enqueue dependents caller for
- // further analysis
- Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
- while (depsItr.hasNext()) {
- MethodDescriptor methodNext = depsItr.next();
- if (!methodDescriptorsToVisitStack.contains(methodNext)) {
- methodDescriptorsToVisitStack.add(methodNext);
- }
- }
-
- // if there is set of callee to be analyzed,
- // add this set into the top of stack
- Iterator<MethodDescriptor> calleeIter = calleesToEnqueue.iterator();
- while (calleeIter.hasNext()) {
- MethodDescriptor mdNext = calleeIter.next();
- if (!methodDescriptorsToVisitStack.contains(mdNext)) {
- methodDescriptorsToVisitStack.add(mdNext);
- }
- }
- calleesToEnqueue.clear();
-
- }
-
- }
-
- }
-
- private ClearingSummary sharedLocation_analyzeMethod(MethodDescriptor md,
- boolean onlyVisitSSJavaLoop) {
-
- if (state.SSJAVADEBUG) {
- System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + md);
- }
-
- FlatMethod fm = state.getMethodFlat(md);
-
- // intraprocedural analysis
- Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
-
- // start a new mapping of partial results for each flat node
- mapFlatNodeToClearingSummary = new Hashtable<FlatNode, ClearingSummary>();
-
- if (onlyVisitSSJavaLoop) {
- flatNodesToVisit.add(ssjavaLoopEntrance);
- } else {
- flatNodesToVisit.add(fm);
- }
-
- Set<FlatNode> returnNodeSet = new HashSet<FlatNode>();
-
- while (!flatNodesToVisit.isEmpty()) {
- FlatNode fn = flatNodesToVisit.iterator().next();
- flatNodesToVisit.remove(fn);
-
- ClearingSummary curr = new ClearingSummary();
-
- Set<ClearingSummary> prevSet = new HashSet<ClearingSummary>();
- for (int i = 0; i < fn.numPrev(); i++) {
- FlatNode prevFn = fn.getPrev(i);
- ClearingSummary in = mapFlatNodeToClearingSummary.get(prevFn);
- if (in != null) {
- prevSet.add(in);
- }
- }
- mergeSharedLocationAnaylsis(curr, prevSet);
-
- sharedLocation_nodeActions(md, fn, curr, returnNodeSet, onlyVisitSSJavaLoop);
- ClearingSummary clearingPrev = mapFlatNodeToClearingSummary.get(fn);
-
- if (!curr.equals(clearingPrev)) {
- mapFlatNodeToClearingSummary.put(fn, curr);
-
- for (int i = 0; i < fn.numNext(); i++) {
- FlatNode nn = fn.getNext(i);
-
- if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
- flatNodesToVisit.add(nn);
- }
-
- }
- }
-
- }
-
- ClearingSummary completeSummary = new ClearingSummary();
- Set<ClearingSummary> summarySet = new HashSet<ClearingSummary>();
-
- if (onlyVisitSSJavaLoop) {
- // when analyzing ssjava loop,
- // complete summary is merging of all previous nodes of ssjava loop
- // entrance
- for (int i = 0; i < ssjavaLoopEntrance.numPrev(); i++) {
- ClearingSummary frnSummary =
- mapFlatNodeToClearingSummary.get(ssjavaLoopEntrance.getPrev(i));
- if (frnSummary != null) {
- summarySet.add(frnSummary);
- }
- }
- } else {
- // merging all exit node summary into the complete summary
- if (!returnNodeSet.isEmpty()) {
- for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
- FlatNode frn = (FlatNode) iterator.next();
- ClearingSummary frnSummary = mapFlatNodeToClearingSummary.get(frn);
- summarySet.add(frnSummary);
- }
- }
- }
- mergeSharedLocationAnaylsis(completeSummary, summarySet);
-
- return completeSummary;
- }
-
- private void sharedLocation_nodeActions(MethodDescriptor md, FlatNode fn, ClearingSummary curr,
- Set<FlatNode> returnNodeSet, boolean isSSJavaLoop) {
-
- TempDescriptor lhs;
- TempDescriptor rhs;
- FieldDescriptor fld;
- switch (fn.kind()) {
-
- case FKind.FlatMethod: {
- FlatMethod fm = (FlatMethod) fn;
-
- ClearingSummary summaryFromCaller =
- mapMethodDescriptorToInitialClearingSummary.get(fm.getMethod());
-
- Set<ClearingSummary> inSet = new HashSet<ClearingSummary>();
- if (summaryFromCaller != null) {
- inSet.add(summaryFromCaller);
- mergeSharedLocationAnaylsis(curr, inSet);
- }
-
- }
- break;
-
- case FKind.FlatOpNode: {
- FlatOpNode fon = (FlatOpNode) fn;
- lhs = fon.getDest();
- rhs = fon.getLeft();
-
- if (fon.getOp().getOp() == Operation.ASSIGN) {
- if (rhs.getType().isImmutable() && isSSJavaLoop) {
- // in ssjavaloop, we need to take care about reading local variables!
- NTuple<Descriptor> rhsHeapPath = new NTuple<Descriptor>();
- NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
- rhsHeapPath.add(LOCAL);
- lhsHeapPath.add(LOCAL);
- if (!lhs.getSymbol().startsWith("neverused")) {
- readLocation(md, curr, rhsHeapPath, getLocation(rhs), rhs);
- writeLocation(md, curr, lhsHeapPath, getLocation(lhs), lhs);
- }
- }
- }
-
- }
- break;
-
- case FKind.FlatSetFieldNode:
- case FKind.FlatSetElementNode: {
-
- // x.f=y
-
- if (fn.kind() == FKind.FlatSetFieldNode) {
- FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
- lhs = fsfn.getDst();
- fld = fsfn.getField();
- rhs = fsfn.getSrc();
- } else {
- FlatSetElementNode fsen = (FlatSetElementNode) fn;
- lhs = fsen.getDst();
- rhs = fsen.getSrc();
- TypeDescriptor td = lhs.getType().dereference();
- fld = getArrayField(td);
- }
-
- // write(field)
- NTuple<Descriptor> lhsHeapPath = computePath(lhs);
- NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
- if (fld.getType().isImmutable()) {
-
- writeLocation(md, curr, fldHeapPath, getLocation(fld), fld);
-
- Descriptor desc = fldHeapPath.get(fldHeapPath.size() - 1);
- if (desc instanceof FieldDescriptor) {
- NTuple<Descriptor> arrayPath = new NTuple<Descriptor>();
- for (int i = 0; i < fldHeapPath.size() - 1; i++) {
- arrayPath.add(fldHeapPath.get(i));
- }
- SharedStatus state = getState(curr, arrayPath);
- state.setWriteEffect(getLocation(desc));
- }
-
- } else {
- // updates reference field case:
- fldHeapPath.add(fld);
- updateWriteEffectOnReferenceField(curr, fldHeapPath);
- }
-
- }
- break;
-
- case FKind.FlatCall: {
-
- FlatCall fc = (FlatCall) fn;
-
- if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
- // ssjava util case!
- // have write effects on the first argument
-
- if (fc.getArg(0).getType().isArray()) {
- // updates reference field case:
- // 2. if there exists a tuple t in sharing summary that starts with
- // hp(x) then, set flag of tuple t to 'true'
- NTuple<Descriptor> argHeapPath = computePath(fc.getArg(0));
-
- Location loc = getLocation(fc.getArg(0));
- NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>();
- for (int i = 0; i < argHeapPath.size() - 1; i++) {
- newHeapPath.add(argHeapPath.get(i));
- }
- fld = (FieldDescriptor) argHeapPath.get(argHeapPath.size() - 1);
- argHeapPath = newHeapPath;
-
- writeLocation(md, curr, argHeapPath, loc, fld);
- }
-
- } else {
- // find out the set of callees
- MethodDescriptor mdCallee = fc.getMethod();
- FlatMethod fmCallee = state.getMethodFlat(mdCallee);
- Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
- setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
-
- possibleCalleeCompleteSummarySetToCaller.clear();
-
- for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
- MethodDescriptor mdPossibleCallee = (MethodDescriptor) iterator.next();
- FlatMethod calleeFlatMethod = state.getMethodFlat(mdPossibleCallee);
-
- addDependent(mdPossibleCallee, // callee
- md); // caller
-
- calleesToEnqueue.add(mdPossibleCallee);
-
- // updates possible callee's initial summary using caller's current
- // writing status
- ClearingSummary prevCalleeInitSummary =
- mapMethodDescriptorToInitialClearingSummary.get(mdPossibleCallee);
-
- ClearingSummary calleeInitSummary =
- bindHeapPathOfCalleeCallerEffects(fc, calleeFlatMethod, curr);
-
- Set<ClearingSummary> inSet = new HashSet<ClearingSummary>();
- if (prevCalleeInitSummary != null) {
- inSet.add(prevCalleeInitSummary);
- mergeSharedLocationAnaylsis(calleeInitSummary, inSet);
- }
-
- // if changes, update the init summary
- // and reschedule the callee for analysis
- if (!calleeInitSummary.equals(prevCalleeInitSummary)) {
-
- if (!methodDescriptorsToVisitStack.contains(mdPossibleCallee)) {
- methodDescriptorsToVisitStack.add(mdPossibleCallee);
- }
-
- mapMethodDescriptorToInitialClearingSummary.put(mdPossibleCallee, calleeInitSummary);
- }
-
- }
-
- // contribute callee's writing effects to the caller
- mergeSharedLocationAnaylsis(curr, possibleCalleeCompleteSummarySetToCaller);
-
- }
-
- }
- break;
-
- case FKind.FlatReturnNode: {
- returnNodeSet.add(fn);
- }
- break;
-
- }
-
- }
-
- private void updateWriteEffectOnReferenceField(ClearingSummary curr, NTuple<Descriptor> heapPath) {
-
- // 2. if there exists a tuple t in sharing summary that starts with
- // hp(x) then, set flag of tuple t to 'true'
- Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
- for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
- NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
- if (hpKey.startsWith(heapPath)) {
- curr.get(hpKey).updateFlag(true);
- }
- }
-
- }
-
- private ClearingSummary bindHeapPathOfCalleeCallerEffects(FlatCall fc,
- FlatMethod calleeFlatMethod, ClearingSummary curr) {
-
- ClearingSummary boundSet = new ClearingSummary();
-
- // create mapping from arg idx to its heap paths
- Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
- new Hashtable<Integer, NTuple<Descriptor>>();
-
- if (fc.getThis() != null) {
- // arg idx is starting from 'this' arg
- NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
- if (thisHeapPath == null) {
- // method is called without creating new flat node representing 'this'
- thisHeapPath = new NTuple<Descriptor>();
- thisHeapPath.add(fc.getThis());
- }
-
- mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
- }
-
- for (int i = 0; i < fc.numArgs(); i++) {
- TempDescriptor arg = fc.getArg(i);
- NTuple<Descriptor> argHeapPath = computePath(arg);
- mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
- }
-
- Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
- new Hashtable<Integer, TempDescriptor>();
- int offset = 0;
- if (calleeFlatMethod.getMethod().isStatic()) {
- // static method does not have implicit 'this' arg
- offset = 1;
- }
- for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
- TempDescriptor param = calleeFlatMethod.getParameter(i);
- mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
- }
-
- // binding caller's writing effects to callee's params
- for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
- NTuple<Descriptor> argHeapPath = mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i));
-
- if (argHeapPath != null) {
- // if method is static, the first argument is nulll because static
- // method does not have implicit "THIS" arg
- TempDescriptor calleeParamHeapPath = mapParamIdx2ParamTempDesc.get(Integer.valueOf(i));
-
- // iterate over caller's writing effect set
- Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
- for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
- NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
- // current element is reachable caller's arg
- // so need to bind it to the caller's side and add it to the
- // callee's
- // init summary
- if (hpKey.startsWith(argHeapPath)) {
- NTuple<Descriptor> boundHeapPath = replace(hpKey, argHeapPath, calleeParamHeapPath);
- boundSet.put(boundHeapPath, curr.get(hpKey).clone());
- }
-
- }
- }
-
- }
-
- // contribute callee's complete summary into the caller's current summary
- ClearingSummary calleeCompleteSummary =
- mapMethodDescriptorToCompleteClearingSummary.get(calleeFlatMethod.getMethod());
- if (calleeCompleteSummary != null) {
- ClearingSummary boundCalleeEfffects = new ClearingSummary();
- for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
- NTuple<Descriptor> argHeapPath = mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i));
-
- if (argHeapPath != null) {
- // if method is static, the first argument is nulll because static
- // method does not have implicit "THIS" arg
- TempDescriptor calleeParamHeapPath = mapParamIdx2ParamTempDesc.get(Integer.valueOf(i));
-
- // iterate over callee's writing effect set
- Set<NTuple<Descriptor>> hpKeySet = calleeCompleteSummary.keySet();
- for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
- NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
- // current element is reachable caller's arg
- // so need to bind it to the caller's side and add it to the
- // callee's
- // init summary
- if (hpKey.startsWith(calleeParamHeapPath)) {
-
- NTuple<Descriptor> boundHeapPathForCaller = replace(hpKey, argHeapPath);
-
- boundCalleeEfffects.put(boundHeapPathForCaller, calleeCompleteSummary.get(hpKey)
- .clone());
-
- }
- }
-
- }
-
- }
- possibleCalleeCompleteSummarySetToCaller.add(boundCalleeEfffects);
- }
-
- return boundSet;
- }
-
- private NTuple<Descriptor> replace(NTuple<Descriptor> hpKey, NTuple<Descriptor> argHeapPath) {
-
- // replace the head of heap path with caller's arg path
- // for example, heap path 'param.a.b' in callee's side will be replaced with
- // (corresponding arg heap path).a.b for caller's side
-
- NTuple<Descriptor> bound = new NTuple<Descriptor>();
-
- for (int i = 0; i < argHeapPath.size(); i++) {
- bound.add(argHeapPath.get(i));
- }
-
- for (int i = 1; i < hpKey.size(); i++) {
- bound.add(hpKey.get(i));
- }
-
- return bound;
- }
-
- private NTuple<Descriptor> replace(NTuple<Descriptor> effectHeapPath,
- NTuple<Descriptor> argHeapPath, TempDescriptor calleeParamHeapPath) {
- // replace the head of caller's heap path with callee's param heap path
-
- NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
- boundHeapPath.add(calleeParamHeapPath);
-
- for (int i = argHeapPath.size(); i < effectHeapPath.size(); i++) {
- boundHeapPath.add(effectHeapPath.get(i));
- }
-
- return boundHeapPath;
- }
-