+ private void addAddtionalOrderingConstraints(MethodDescriptor mdCaller) {
+
+ // First, assign a composite location to a node in the flow graph
+ GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
+
+ FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
+ Map<Location, CompositeLocation> callerMapLocToCompLoc =
+ callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
+ Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
+
+ Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
+
+ Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
+ for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
+ MethodInvokeNode min = (MethodInvokeNode) iterator.next();
+ MethodDescriptor mdCallee = min.getMethod();
+ calleeSet.add(mdCallee);
+
+ //
+ // add an additional ordering constraint
+ // if the first element of a parameter composite location matches 'this' reference,
+ // the corresponding argument in the caller is required to be higher than the translated
+ // parameter location in the caller lattice
+ // TODO
+ // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
+
+ //
+ // update return flow nodes in the caller
+ CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc();
+ System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc);
+ if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis())
+ && returnLoc.getSize() > 1) {
+ System.out.println("###RETURN COMP LOC=" + returnLoc);
+ NTuple<Location> returnLocTuple = returnLoc.getTuple();
+ NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
+ System.out.println("###basetuple=" + baseTuple);
+ NTuple<Descriptor> newReturnTuple = baseTuple.clone();
+ for (int i = 1; i < returnLocTuple.size(); i++) {
+ newReturnTuple.add(returnLocTuple.get(i).getLocDescriptor());
+ }
+ System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple);
+
+ FlowReturnNode holderNode = callerFlowGraph.getFlowReturnNode(min);
+ NodeTupleSet holderTupleSet =
+ getNodeTupleSetFromReturnNode(getFlowGraph(mdCaller), holderNode);
+
+ callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple);
+
+ // then need to remove old constraints
+ // TODO SAT
+ System.out.println("###REMOVE OLD CONSTRAINTS=" + holderNode);
+ for (Iterator<NTuple<Descriptor>> iter = holderTupleSet.iterator(); iter.hasNext();) {
+ NTuple<Descriptor> tupleFromHolder = iter.next();
+ Set<FlowEdge> holderOutEdge = callerFlowGraph.getOutEdgeSet(holderNode);
+ for (Iterator iterator2 = holderOutEdge.iterator(); iterator2.hasNext();) {
+ FlowEdge outEdge = (FlowEdge) iterator2.next();
+ NTuple<Descriptor> toberemovedTuple = outEdge.getEndTuple();
+ // System.out.println("---remove " + tupleFromHolder + " -> " + toberemovedTuple);
+ callerFlowGraph.removeEdge(tupleFromHolder, toberemovedTuple);
+ }
+ }
+
+ } else {
+ // if the return loc set was empty and later pcloc was connected to the return loc
+ // need to make sure that return loc reflects to this changes.
+ FlowReturnNode flowReturnNode = callerFlowGraph.getFlowReturnNode(min);
+ if (flowReturnNode != null && flowReturnNode.getReturnTupleSet().isEmpty()) {
+
+ if (needToUpdateReturnLocHolder(min.getMethod(), flowReturnNode)) {
+ NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
+ NTuple<Descriptor> newReturnTuple = baseTuple.clone();
+ flowReturnNode.addTuple(newReturnTuple);
+ }
+
+ }
+
+ }
+
+ }
+
+ for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
+ MethodDescriptor callee = (MethodDescriptor) iterator.next();
+ addAddtionalOrderingConstraints(callee);
+ }
+
+ }
+
+ private boolean needToUpdateReturnLocHolder(MethodDescriptor mdCallee,
+ FlowReturnNode flowReturnNode) {
+ FlowGraph fg = getFlowGraph(mdCallee);
+ MethodSummary summary = getMethodSummary(mdCallee);
+ CompositeLocation returnCompLoc = summary.getRETURNLoc();
+ NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnCompLoc.getTuple());
+ Set<FlowNode> incomingNodeToReturnNode =
+ fg.getIncomingFlowNodeSet(fg.getFlowNode(returnDescTuple));
+ for (Iterator iterator = incomingNodeToReturnNode.iterator(); iterator.hasNext();) {
+ FlowNode inNode = (FlowNode) iterator.next();
+ if (inNode.getDescTuple().get(0).equals(mdCallee.getThis())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void addMapMethodDescToMethodInvokeNodeSet(MethodInvokeNode min) {
+ MethodDescriptor md = min.getMethod();
+ if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
+ mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
+ }
+ mapMethodDescToMethodInvokeNodeSet.get(md).add(min);
+ }
+
+ private Set<MethodInvokeNode> getMethodInvokeNodeSetByMethodDesc(MethodDescriptor md) {
+ if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
+ mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
+ }
+ return mapMethodDescToMethodInvokeNodeSet.get(md);
+ }
+
+ private void addOrderingConstraintFromCompLocParamToArg(MethodDescriptor mdCaller,
+ MethodInvokeNode min) {
+ System.out.println("-addOrderingConstraintFromCompLocParamToArg=" + min.printNode(0));
+
+ GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(ssjava.getMethodContainingSSJavaLoop());
+
+ Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
+
+ MethodDescriptor mdCallee = min.getMethod();
+
+ FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
+ for (int idx = 0; idx < calleeFlowGraph.getNumParameters(); idx++) {
+ FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
+ NTuple<Location> globalParamLocTuple =
+ translateToLocTuple(mdCallee, paramNode.getDescTuple());
+ translateToLocTuple(mdCallee, paramNode.getDescTuple());
+ CompositeLocation compLoc = paramNode.getCompositeLocation();
+ System.out.println("---paramNode=" + paramNode + " compLoc=" + compLoc);
+ if (compLoc != null) {
+ NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
+ NTuple<Location> globalArgLocTuple = translateToLocTuple(mdCaller, argTuple);
+
+ if (!isLiteralValueLocTuple(globalArgLocTuple)
+ && !isLiteralValueLocTuple(globalParamLocTuple)) {
+ if (!globalGraph.hasValueFlowEdge(globalArgLocTuple, globalParamLocTuple)) {
+ System.out.println("----- add global flow globalArgLocTuple=" + globalArgLocTuple
+ + "-> globalParamLocTuple=" + globalParamLocTuple);
+ hasChanges = true;
+ globalGraph.addValueFlowEdge(globalArgLocTuple, globalParamLocTuple);
+ }
+ }
+
+ for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
+ NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
+
+ if (!isLiteralValueLocTuple(pcLocTuple) && !isLiteralValueLocTuple(globalParamLocTuple)) {
+ if (!globalGraph.hasValueFlowEdge(pcLocTuple, globalParamLocTuple)) {
+ System.out
+ .println("----- add global flow PCLOC="
+ + pcLocTuple
+ + "-> globalParamLocTu!globalArgLocTuple.get(0).getLocDescriptor().equals(LITERALDESC)ple="
+ + globalParamLocTuple);
+ hasChanges = true;
+
+ globalGraph.addValueFlowEdge(pcLocTuple, globalParamLocTuple);
+ }
+ }
+
+ }
+ }
+ }
+ }
+
+ private boolean isLiteralValueLocTuple(NTuple<Location> locTuple) {
+ return locTuple.get(0).getLocDescriptor().equals(LITERALDESC);
+ }
+
+ public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
+ CompositeLocation inferCompLoc) {
+ Descriptor localDesc = loc.getLocDescriptor();
+
+ Set<FlowNode> nodeSet = flowGraph.getNodeSet();
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ FlowNode node = (FlowNode) iterator.next();
+ if (node.getDescTuple().startsWith(localDesc)
+ && !node.getDescTuple().get(0).equals(LITERALDESC)) {
+ // need to assign the inferred composite location to this node
+ CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
+ node.setCompositeLocation(newCompLoc);
+ System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
+ }
+ }
+ }
+
+ private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
+ CompositeLocation inferCompLoc) {
+
+ System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
+ + inferCompLoc);
+
+ MethodDescriptor md = (MethodDescriptor) inferCompLoc.get(0).getDescriptor();
+
+ CompositeLocation newCompLoc = new CompositeLocation();
+ for (int i = 0; i < inferCompLoc.getSize(); i++) {
+ newCompLoc.addLocation(inferCompLoc.get(i));
+ }
+
+ Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
+ Descriptor enclosingDescriptor;
+ if (lastDescOfPrefix instanceof InterDescriptor) {
+ enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(lastDescOfPrefix);
+ } else {
+ enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
+ }
+
+ for (int i = 1; i < nodeDescTuple.size(); i++) {
+ Descriptor desc = nodeDescTuple.get(i);
+ Location locElement = new Location(enclosingDescriptor, desc);
+ newCompLoc.addLocation(locElement);
+
+ enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
+ }
+
+ return newCompLoc;
+ }
+
+ private void translateMapLocationToInferCompositeLocationToCalleeGraph(
+ GlobalFlowGraph callerGraph, MethodInvokeNode min) {
+
+ MethodDescriptor mdCallee = min.getMethod();
+ MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
+ Map<Location, CompositeLocation> callerMapLocToCompLoc =
+ callerGraph.getMapLocationToInferCompositeLocation();
+
+ Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
+
+ FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
+ GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
+
+ NTuple<Location> baseLocTuple = null;
+ if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
+ baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
+ }
+
+ // System.out.println("\n-#translate caller=" + mdCaller + " infer composite loc to callee="
+ // + mdCallee + " baseLocTuple=" + baseLocTuple);
+
+ Set<Location> keySet = callerMapLocToCompLoc.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Location key = (Location) iterator.next();
+ CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
+
+ if (!key.getDescriptor().equals(mdCaller)) {
+
+ CompositeLocation newCalleeCompLoc;
+ if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
+ // System.out.println("-----need to translate callerCompLoc=" + callerCompLoc
+ // + " with baseTuple=" + baseLocTuple);
+ newCalleeCompLoc =
+ translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
+
+ calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
+ // System.out.println("1---key=" + key + " callerCompLoc=" + callerCompLoc
+ // + " newCalleeCompLoc=" + newCalleeCompLoc);
+ // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
+ if (!newCalleeCompLoc.get(0).getDescriptor().equals(mdCallee)) {
+ System.exit(0);
+ }
+
+ // System.out.println("-----baseLoctuple=" + baseLocTuple);
+ } else {
+ // check if it is the global access
+ Location compLocFirstElement = callerCompLoc.getTuple().get(0);
+ if (compLocFirstElement.getDescriptor().equals(mdCallee)
+ && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
+
+ newCalleeCompLoc = new CompositeLocation();
+ Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
+
+ newCalleeCompLoc.addLocation(newMethodLoc);
+ for (int i = 1; i < callerCompLoc.getSize(); i++) {
+ newCalleeCompLoc.addLocation(callerCompLoc.get(i));
+ }
+ calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
+ // System.out.println("2---key=" + key + " callerCompLoc=" + callerCompLoc
+ // + " newCalleeCompLoc=" + newCalleeCompLoc);
+ // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
+
+ } else {
+ int paramIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
+ if (paramIdx == -1) {
+ // here, the first element of the current composite location comes from the current
+ // callee
+ // so transfer the same composite location to the callee
+ if (!calleeGlobalGraph.contrainsInferCompositeLocationMapKey(key)) {
+ if (callerCompLoc.get(0).getDescriptor().equals(mdCallee)) {
+ // System.out.println("3---key=" + key + " callerCompLoc=" + callerCompLoc
+ // + " newCalleeCompLoc=" + callerCompLoc);
+ // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
+ calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, callerCompLoc);
+ } else {
+ // System.out.println("3---SKIP key=" + key + " callerCompLoc=" + callerCompLoc);
+ }
+ }
+ continue;
+ }
+
+ // It is the case where two parameters have relative orderings between them by having
+ // composite locations
+ // if we found the param idx, it means that the first part of the caller composite
+ // location corresponds to the one of arguments.
+ // for example, if the caller argument is <<caller.this>,<Decoder.br>>
+ // and the current caller composite location mapping
+ // <<caller.this>,<Decoder.br>,<Br.value>>
+ // and the parameter which matches with the caller argument is 'Br brParam'
+ // then, the translated callee composite location will be <<callee.brParam>,<Br.value>>
+ NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(paramIdx);
+
+ FlowNode paramFlowNode = calleeFlowGraph.getParamFlowNode(paramIdx);
+ NTuple<Location> paramLocTuple =
+ translateToLocTuple(mdCallee, paramFlowNode.getDescTuple());
+ newCalleeCompLoc = new CompositeLocation();
+ for (int i = 0; i < paramLocTuple.size(); i++) {
+ newCalleeCompLoc.addLocation(paramLocTuple.get(i));
+ }
+ for (int i = argTuple.size(); i < callerCompLoc.getSize(); i++) {
+ newCalleeCompLoc.addLocation(callerCompLoc.get(i));
+ }
+ calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
+ // System.out.println("4---key=" + key + " callerCompLoc=" + callerCompLoc
+ // + " newCalleeCompLoc=" + newCalleeCompLoc);
+ // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
+
+ }
+
+ }
+
+ }
+ }
+
+ System.out.println("#ASSIGN COMP LOC TO CALLEE PARAMS: callee=" + mdCallee + " caller="
+ + mdCaller);
+ // If the location of an argument has a composite location
+ // need to assign a proper composite location to the corresponding callee parameter
+ Set<Integer> idxSet = mapIdxToArgTuple.keySet();
+ for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
+ Integer idx = (Integer) iterator.next();
+
+ if (idx == 0 && !min.getMethod().isStatic()) {
+ continue;
+ }
+
+ NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
+ System.out.println("-argTuple=" + argTuple + " idx=" + idx);
+ if (argTuple.size() > 0) {
+ // check if an arg tuple has been already assigned to a composite location
+ NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
+ Location argLocalLoc = argLocTuple.get(0);
+
+ // if (!isPrimitiveType(argTuple)) {
+ if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
+
+ CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
+ for (int i = 1; i < argLocTuple.size(); i++) {
+ callerCompLoc.addLocation(argLocTuple.get(i));
+ }
+
+ System.out.println("---callerCompLoc=" + callerCompLoc);
+
+ // if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
+
+ FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
+
+ NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
+ NTuple<Location> calleeParamLocTuple =
+ translateToLocTuple(mdCallee, calleeParamDescTuple);
+
+ int refParamIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
+ System.out.println("-----paramIdx=" + refParamIdx);
+ if (refParamIdx == 0 && !mdCallee.isStatic()) {
+
+ System.out.println("-------need to translate callerCompLoc=" + callerCompLoc
+ + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
+ + calleeParamLocTuple);
+
+ CompositeLocation newCalleeCompLoc =
+ translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
+
+ calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
+ newCalleeCompLoc);
+
+ System.out.println("---------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
+ + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
+
+ } else if (refParamIdx != -1) {
+ // the first element of an argument composite location matches with one of paramtere
+ // composite locations
+
+ System.out.println("-------param match case=");
+
+ NTuple<Descriptor> argTupleRef = mapIdxToArgTuple.get(refParamIdx);
+ FlowNode refParamFlowNode = calleeFlowGraph.getParamFlowNode(refParamIdx);
+ NTuple<Location> refParamLocTuple =
+ translateToLocTuple(mdCallee, refParamFlowNode.getDescTuple());
+
+ System.out.println("---------refParamLocTuple=" + refParamLocTuple
+ + " from argTupleRef=" + argTupleRef);
+
+ CompositeLocation newCalleeCompLoc = new CompositeLocation();
+ for (int i = 0; i < refParamLocTuple.size(); i++) {
+ newCalleeCompLoc.addLocation(refParamLocTuple.get(i));
+ }
+ for (int i = argTupleRef.size(); i < callerCompLoc.getSize(); i++) {
+ newCalleeCompLoc.addLocation(callerCompLoc.get(i));
+ }
+
+ calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
+ newCalleeCompLoc);
+
+ calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
+ System.out.println("-----------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
+ + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
+
+ } else {
+ CompositeLocation newCalleeCompLoc =
+ calculateCompositeLocationFromSubGlobalGraph(mdCallee, calleeParamFlowNode);
+ if (newCalleeCompLoc != null) {
+ calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
+ newCalleeCompLoc);
+ calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
+ }
+ }
+
+ System.out.println("-----------------calleeParamFlowNode="
+ + calleeParamFlowNode.getCompositeLocation());
+
+ // }
+
+ }
+ }
+
+ }
+
+ }
+
+ private CompositeLocation calculateCompositeLocationFromSubGlobalGraph(MethodDescriptor md,
+ FlowNode paramNode) {
+
+ System.out.println("#############################################################");
+ System.out.println("calculateCompositeLocationFromSubGlobalGraph=" + paramNode);
+
+ GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
+ NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
+ GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
+
+ List<NTuple<Location>> prefixList = calculatePrefixList(subGlobalFlowGraph, paramGlobalNode);
+
+ Location prefixLoc = paramLocTuple.get(0);
+
+ Set<GlobalFlowNode> reachableNodeSet =
+ subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
+ // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
+
+ // System.out.println("node=" + node + " prefixList=" + prefixList);
+
+ for (int i = 0; i < prefixList.size(); i++) {
+ NTuple<Location> curPrefix = prefixList.get(i);
+ Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
+
+ for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
+ GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
+ if (reachNode.getLocTuple().startsWith(curPrefix)) {
+ reachableCommonPrefixSet.add(reachNode.getLocTuple());
+ }
+ }
+ // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
+
+ if (!reachableCommonPrefixSet.isEmpty()) {
+
+ MethodDescriptor curPrefixFirstElementMethodDesc =
+ (MethodDescriptor) curPrefix.get(0).getDescriptor();
+
+ MethodDescriptor nodePrefixLocFirstElementMethodDesc =
+ (MethodDescriptor) prefixLoc.getDescriptor();
+
+ // System.out.println("curPrefixFirstElementMethodDesc=" +
+ // curPrefixFirstElementMethodDesc);
+ // System.out.println("nodePrefixLocFirstElementMethodDesc="
+ // + nodePrefixLocFirstElementMethodDesc);
+
+ if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
+ || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
+ curPrefixFirstElementMethodDesc)) {
+
+ // TODO
+ // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
+
+ Location curPrefixLocalLoc = curPrefix.get(0);
+ if (subGlobalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
+ // in this case, the local variable of the current prefix has already got a composite
+ // location
+ // so we just ignore the current composite location.
+
+ // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
+ // + " DUE TO " + curPrefix);
+ return null;
+ }
+
+ if (!needToGenerateCompositeLocation(paramGlobalNode, curPrefix)) {
+ System.out.println("NO NEED TO GENERATE COMP LOC to " + paramGlobalNode
+ + " with prefix=" + curPrefix);
+ return null;
+ }
+
+ Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
+ CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
+ System.out.println("NEED TO ASSIGN COMP LOC TO " + paramGlobalNode + " with prefix="
+ + curPrefix);
+ System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
+
+ // makes sure that a newly generated location appears in the hierarchy graph
+ for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
+ Location curLoc = newCompLoc.get(compIdx);
+ getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
+ }
+
+ subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
+
+ return newCompLoc;
+
+ }
+
+ }
+
+ }
+ return null;
+ }
+
+ private int getParamIdx(CompositeLocation compLoc,
+ Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple) {
+
+ // if the composite location is started with the argument descriptor
+ // return the argument's index. o.t. return -1
+
+ Set<Integer> keySet = mapIdxToArgTuple.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Integer key = (Integer) iterator.next();
+ NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(key);
+ if (argTuple.size() > 0 && translateToDescTuple(compLoc.getTuple()).startsWith(argTuple)) {
+ System.out.println("compLoc.getTuple=" + compLoc + " is started with " + argTuple);
+ return key.intValue();
+ }
+ }
+
+ return -1;
+ }
+
+ private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
+
+ Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
+
+ if (lastDesc instanceof FieldDescriptor) {
+ return ((FieldDescriptor) lastDesc).getType().isPrimitive();
+ } else if (lastDesc instanceof VarDescriptor) {
+ return ((VarDescriptor) lastDesc).getType().isPrimitive();
+ } else if (lastDesc instanceof InterDescriptor) {
+ return true;
+ }
+
+ return false;
+ }
+
+ private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
+ NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
+
+ CompositeLocation newCalleeCompLoc = new CompositeLocation();
+
+ Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
+ newCalleeCompLoc.addLocation(calleeThisLoc);
+
+ // remove the base tuple from the caller
+ // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
+ // ,which is relative to the 'this' variable, <THIS,...>
+ for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
+ newCalleeCompLoc.addLocation(callerCompLoc.get(i));
+ }
+
+ return newCalleeCompLoc;
+
+ }
+
+ private void calculateGlobalValueFlowCompositeLocation() {
+
+ System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
+ MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
+ GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
+
+ Set<Location> calculatedPrefixSet = new HashSet<Location>();
+
+ Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
+
+ next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ GlobalFlowNode node = (GlobalFlowNode) iterator.next();
+
+ Location prefixLoc = node.getLocTuple().get(0);
+
+ if (calculatedPrefixSet.contains(prefixLoc)) {
+ // the prefix loc has been already assigned to a composite location
+ continue;
+ }
+
+ calculatedPrefixSet.add(prefixLoc);
+
+ // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
+ List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
+
+ Set<GlobalFlowNode> reachableNodeSet =
+ globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
+ // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
+
+ // System.out.println("node=" + node + " prefixList=" + prefixList);
+ System.out.println("---prefixList=" + prefixList);
+
+ nextprefix: for (int i = 0; i < prefixList.size(); i++) {
+ NTuple<Location> curPrefix = prefixList.get(i);
+ System.out.println("---curPrefix=" + curPrefix);
+ Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
+
+ for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
+ GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
+ if (reachNode.getLocTuple().startsWith(curPrefix)) {
+ reachableCommonPrefixSet.add(reachNode.getLocTuple());
+ }
+ }
+ // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
+
+ if (!reachableCommonPrefixSet.isEmpty()) {
+
+ MethodDescriptor curPrefixFirstElementMethodDesc =
+ (MethodDescriptor) curPrefix.get(0).getDescriptor();
+
+ MethodDescriptor nodePrefixLocFirstElementMethodDesc =
+ (MethodDescriptor) prefixLoc.getDescriptor();
+
+ // System.out.println("curPrefixFirstElementMethodDesc=" +
+ // curPrefixFirstElementMethodDesc);
+ // System.out.println("nodePrefixLocFirstElementMethodDesc="
+ // + nodePrefixLocFirstElementMethodDesc);
+
+ if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
+ || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
+ curPrefixFirstElementMethodDesc)) {
+
+ // TODO
+ // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
+
+ Location curPrefixLocalLoc = curPrefix.get(0);
+ if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
+ // in this case, the local variable of the current prefix has already got a composite
+ // location
+ // so we just ignore the current composite location.
+
+ // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
+ // + " DUE TO " + curPrefix);
+
+ continue next;
+ }
+
+ if (!needToGenerateCompositeLocation(node, curPrefix)) {
+ System.out.println("NO NEED TO GENERATE COMP LOC to " + node + " with prefix="
+ + curPrefix);
+ // System.out.println("prefixList=" + prefixList);
+ // System.out.println("reachableNodeSet=" + reachableNodeSet);
+ continue nextprefix;
+ }
+
+ Location targetLocalLoc = node.getLocTuple().get(0);
+ CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
+ System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
+ System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc="
+ + newCompLoc);
+ globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
+ // }
+
+ continue next;
+ // }
+
+ }
+
+ }
+
+ }
+
+ }
+ }
+
+ private boolean checkFlowNodeReturnThisField(MethodDescriptor md) {
+
+ MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
+ GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
+
+ FlowGraph flowGraph = getFlowGraph(md);
+
+ ClassDescriptor enclosingDesc = getClassTypeDescriptor(md.getThis());
+ if (enclosingDesc == null) {
+ return false;
+ }
+
+ int count = 0;
+ Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
+ Set<GlobalFlowNode> globalReturnNodeSet = new HashSet<GlobalFlowNode>();
+ for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
+ FlowNode flowNode = (FlowNode) iterator.next();
+ NTuple<Location> locTuple = translateToLocTuple(md, flowNode.getDescTuple());
+ GlobalFlowNode globalReturnNode = globalFlowGraph.getFlowNode(locTuple);
+ globalReturnNodeSet.add(globalReturnNode);
+
+ List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, globalReturnNode);
+ for (int i = 0; i < prefixList.size(); i++) {
+ NTuple<Location> curPrefix = prefixList.get(i);
+ ClassDescriptor cd =
+ getClassTypeDescriptor(curPrefix.get(curPrefix.size() - 1).getLocDescriptor());
+ if (cd != null && cd.equals(enclosingDesc)) {
+ count++;
+ break;
+ }
+ }
+
+ }
+
+ if (count == returnNodeSet.size()) {
+ // in this case, all return nodes in the method returns values coming from a location that
+ // starts with "this"
+
+ System.out.println("$$$SET RETURN LOC TRUE=" + md);
+ mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.TRUE);
+
+ // NameDescriptor returnLocDesc = new NameDescriptor("RLOC" + (locSeed++));
+ // NTuple<Descriptor> rDescTuple = new NTuple<Descriptor>();
+ // rDescTuple.add(md.getThis());
+ // rDescTuple.add(returnLocDesc);
+ //
+ // for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
+ // FlowNode rnode = (FlowNode) iterator.next();
+ // flowGraph.addValueFlowEdge(rnode.getDescTuple(), rDescTuple);
+ // }
+ //
+ // getMethodSummary(md).setRETURNLoc(new CompositeLocation(translateToLocTuple(md,
+ // rDescTuple)));
+
+ } else {
+ mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.FALSE);
+ }
+
+ return mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
+
+ }
+
+ private boolean needToGenerateCompositeLocation(GlobalFlowNode node, NTuple<Location> curPrefix) {
+ // return true if there is a path between a node to which we want to give a composite location
+ // and nodes which start with curPrefix
+
+ System.out.println("---needToGenerateCompositeLocation curPrefix=" + curPrefix);
+
+ Location targetLocalLoc = node.getLocTuple().get(0);
+
+ MethodDescriptor md = (MethodDescriptor) targetLocalLoc.getDescriptor();
+ FlowGraph flowGraph = getFlowGraph(md);
+
+ FlowNode flowNode = flowGraph.getFlowNode(node.getDescTuple());
+ Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
+
+ Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
+ for (Iterator iterator = paramNodeSet.iterator(); iterator.hasNext();) {
+ FlowNode paramFlowNode = (FlowNode) iterator.next();
+ if (curPrefix.startsWith(translateToLocTuple(md, paramFlowNode.getDescTuple()))) {
+ return true;
+ }
+ }
+
+ if (targetLocalLoc.getLocDescriptor() instanceof InterDescriptor) {
+ Pair<MethodInvokeNode, Integer> pair =
+ ((InterDescriptor) targetLocalLoc.getLocDescriptor()).getMethodArgIdxPair();
+
+ if (pair != null) {
+ System.out.println("$$$TARGETLOCALLOC HOLDER=" + targetLocalLoc);
+
+ MethodInvokeNode min = pair.getFirst();
+ Integer paramIdx = pair.getSecond();
+ MethodDescriptor mdCallee = min.getMethod();
+
+ FlowNode paramNode = getFlowGraph(mdCallee).getParamFlowNode(paramIdx);
+ if (checkNodeReachToReturnNode(mdCallee, paramNode)) {
+ return true;
+ }
+
+ }
+
+ }
+
+ GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
+ Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
+
+ if (!md.isStatic()) {
+ ClassDescriptor currentMethodThisType = getClassTypeDescriptor(md.getThis());
+ for (int i = 0; i < curPrefix.size(); i++) {
+ ClassDescriptor prefixType = getClassTypeDescriptor(curPrefix.get(i).getLocDescriptor());
+ if (prefixType != null && prefixType.equals(currentMethodThisType)) {
+ System.out.println("PREFIX TYPE MATCHES WITH=" + currentMethodThisType);
+
+ if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
+ boolean hasCompReturnLocWithThis =
+ mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
+ if (hasCompReturnLocWithThis) {
+ if (checkNodeReachToReturnNode(md, flowNode)) {
+ return true;
+ }
+ }
+ }
+
+ for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
+ GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
+ if (subGlobalReachalbeNode.getLocTuple().get(0).getLocDescriptor().equals(md.getThis())) {
+ System.out.println("PREFIX FOUND=" + subGlobalReachalbeNode);
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ Location lastLocationOfPrefix = curPrefix.get(curPrefix.size() - 1);
+ // check whether prefix appears in the list of parameters
+ Set<MethodInvokeNode> minSet = mapMethodDescToMethodInvokeNodeSet.get(md);
+ found: for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
+ MethodInvokeNode min = (MethodInvokeNode) iterator.next();
+ Map<Integer, NTuple<Descriptor>> map = mapMethodInvokeNodeToArgIdxMap.get(min);
+ Set<Integer> keySet = map.keySet();
+ // System.out.println("min=" + min.printNode(0));
+
+ for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
+ Integer argIdx = (Integer) iterator2.next();
+ NTuple<Descriptor> argTuple = map.get(argIdx);
+
+ if (!(!md.isStatic() && argIdx == 0)) {
+ // if the argTuple is empty, we don't need to do with anything(LITERAL CASE).
+ if (argTuple.size() > 0
+ && argTuple.get(argTuple.size() - 1).equals(lastLocationOfPrefix.getLocDescriptor())) {
+ NTuple<Location> locTuple =
+ translateToLocTuple(md, flowGraph.getParamFlowNode(argIdx).getDescTuple());
+ lastLocationOfPrefix = locTuple.get(0);
+ System.out.println("ARG CASE=" + locTuple);
+ for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
+ GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
+ // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
+ NTuple<Location> globalReachlocTuple = subGlobalReachalbeNode.getLocTuple();
+ for (int i = 0; i < globalReachlocTuple.size(); i++) {
+ if (globalReachlocTuple.get(i).equals(lastLocationOfPrefix)) {
+ System.out.println("ARG " + argTuple + " IS MATCHED WITH="
+ + lastLocationOfPrefix);
+ return true;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private boolean checkNodeReachToReturnNode(MethodDescriptor md, FlowNode node) {
+
+ FlowGraph flowGraph = getFlowGraph(md);
+ Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(node);
+ if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
+ boolean hasCompReturnLocWithThis =
+ mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
+
+ if (hasCompReturnLocWithThis) {
+ for (Iterator iterator = flowGraph.getReturnNodeSet().iterator(); iterator.hasNext();) {
+ FlowNode returnFlowNode = (FlowNode) iterator.next();
+ if (reachableSet.contains(returnFlowNode)) {
+ return true;
+ }
+ }
+ }
+ }
+ return false;
+ }
+
+ private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
+ CompositeLocation newCompLoc = compLocPrefix.clone();
+ NTuple<Location> locTuple = node.getLocTuple();
+ for (int i = 1; i < locTuple.size(); i++) {
+ newCompLoc.addLocation(locTuple.get(i));
+ }
+ node.setInferCompositeLocation(newCompLoc);
+ }
+
+ private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
+
+ System.out.println("\n##### calculatePrefixList node=" + node);
+
+ Set<GlobalFlowNode> incomingNodeSetPrefix =
+ graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
+ // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
+
+ Set<GlobalFlowNode> reachableNodeSetPrefix =
+ graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
+ // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
+
+ List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
+
+ for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
+ GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
+ NTuple<Location> inNodeTuple = inNode.getLocTuple();
+
+ if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
+ || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
+ continue;
+ }
+
+ for (int i = 1; i < inNodeTuple.size(); i++) {
+ NTuple<Location> prefix = inNodeTuple.subList(0, i);
+ if (!prefixList.contains(prefix)) {
+ prefixList.add(prefix);
+ }
+ }
+ }
+
+ Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
+ public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
+ int s0 = arg0.size();
+ int s1 = arg1.size();
+ if (s0 > s1) {
+ return -1;
+ } else if (s0 == s1) {
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ });
+
+ return prefixList;
+
+ }
+
+ private CompositeLocation calculateCompositeLocationFromFlowGraph(MethodDescriptor md,
+ FlowNode node) {
+
+ System.out.println("#############################################################");
+ System.out.println("calculateCompositeLocationFromFlowGraph=" + node);
+
+ FlowGraph flowGraph = getFlowGraph(md);
+ // NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
+ // GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
+
+ List<NTuple<Location>> prefixList = calculatePrefixListFlowGraph(flowGraph, node);
+
+ // Set<GlobalFlowNode> reachableNodeSet =
+ // subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
+ //
+ Set<FlowNode> reachableNodeSet =
+ flowGraph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
+
+ // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
+
+ // System.out.println("node=" + node + " prefixList=" + prefixList);
+
+ for (int i = 0; i < prefixList.size(); i++) {
+ NTuple<Location> curPrefix = prefixList.get(i);
+ Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
+
+ for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
+ FlowNode reachNode = (FlowNode) iterator2.next();
+ NTuple<Location> reachLocTuple = translateToLocTuple(md, reachNode.getCurrentDescTuple());
+ if (reachLocTuple.startsWith(curPrefix)) {
+ reachableCommonPrefixSet.add(reachLocTuple);
+ }
+ }
+ // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
+
+ if (!reachableCommonPrefixSet.isEmpty()) {
+
+ MethodDescriptor curPrefixFirstElementMethodDesc =
+ (MethodDescriptor) curPrefix.get(0).getDescriptor();
+
+ Location curPrefixLocalLoc = curPrefix.get(0);
+
+ Location targetLocalLoc = new Location(md, node.getDescTuple().get(0));
+ // Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
+
+ CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
+ System.out.println("NEED2ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
+ System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
+
+ node.setCompositeLocation(newCompLoc);
+
+ return newCompLoc;
+
+ }
+
+ }
+ return null;
+ }
+
+ private List<NTuple<Location>> calculatePrefixListFlowGraph(FlowGraph graph, FlowNode node) {
+
+ System.out.println("\n##### calculatePrefixList node=" + node);
+
+ MethodDescriptor md = graph.getMethodDescriptor();
+ Set<FlowNode> incomingNodeSetPrefix =
+ graph.getIncomingNodeSetByPrefix(node.getDescTuple().get(0));
+ // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
+
+ Set<FlowNode> reachableNodeSetPrefix =
+ graph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
+ // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
+
+ List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
+
+ for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
+ FlowNode inNode = (FlowNode) iterator.next();
+ NTuple<Location> inNodeTuple = translateToLocTuple(md, inNode.getCurrentDescTuple());
+
+ // if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
+ // || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
+ // continue;
+ // }
+
+ for (int i = 1; i < inNodeTuple.size(); i++) {
+ NTuple<Location> prefix = inNodeTuple.subList(0, i);
+ if (!prefixList.contains(prefix)) {
+ prefixList.add(prefix);
+ }
+ }
+ }
+
+ Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
+ public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
+ int s0 = arg0.size();
+ int s1 = arg1.size();
+ if (s0 > s1) {
+ return -1;
+ } else if (s0 == s1) {
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ });
+
+ return prefixList;
+
+ }
+
+ private boolean containsClassDesc(ClassDescriptor cd, NTuple<Location> prefixLocTuple) {
+ for (int i = 0; i < prefixLocTuple.size(); i++) {
+ Location loc = prefixLocTuple.get(i);
+ Descriptor locDesc = loc.getLocDescriptor();
+ if (locDesc != null) {
+ ClassDescriptor type = getClassTypeDescriptor(locDesc);
+ if (type != null && type.equals(cd)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
+
+ MethodDescriptor md = flowGraph.getMethodDescriptor();
+
+ GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(md);
+
+ // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
+ Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
+
+ for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
+
+ FlowEdge edge = (FlowEdge) iterator.next();
+ NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
+ NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
+
+ if (flowGraph.getFlowNode(srcDescTuple) instanceof FlowReturnNode
+ || flowGraph.getFlowNode(dstDescTuple) instanceof FlowReturnNode) {
+ continue;
+ }
+
+ // here only keep the first element(method location) of the descriptor
+ // tuple
+ NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
+ NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
+
+ globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
+
+ }
+
+ return globalGraph;
+ }
+
+ private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
+
+ NTuple<Location> locTuple = new NTuple<Location>();
+
+ Descriptor enclosingDesc = md;
+ for (int i = 0; i < descTuple.size(); i++) {
+ Descriptor desc = descTuple.get(i);
+
+ Location loc = new Location(enclosingDesc, desc);
+ locTuple.add(loc);
+
+ if (desc instanceof VarDescriptor) {
+ enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
+ } else if (desc instanceof FieldDescriptor) {
+ enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
+ } else {
+ enclosingDesc = desc;
+ }
+
+ }
+
+ return locTuple;
+
+ }
+
+ private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller) {
+
+ // the transformation for a call site propagates flows through parameters
+ // if the method is virtual, it also grab all relations from any possible
+ // callees
+
+ Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
+
+ for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
+ MethodInvokeNode min = (MethodInvokeNode) iterator.next();
+ MethodDescriptor mdCallee = min.getMethod();
+ Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
+ if (mdCallee.isStatic()) {
+ setPossibleCallees.add(mdCallee);
+ } else {
+ Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
+ // removes method descriptors that are not invoked by the caller
+ calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
+ setPossibleCallees.addAll(calleeSet);
+ }
+
+ for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
+ MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
+ propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
+ }
+
+ }
+
+ }
+
+ private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
+ MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
+
+ System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
+ FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
+ Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
+
+ System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
+ + mapMethodInvokeNodeToArgIdxMap.get(min));
+
+ Set<Integer> keySet = mapIdxToArg.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Integer idx = (Integer) iterator.next();
+ NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
+ if (argDescTuple.size() > 0) {
+ NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
+ NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
+ NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
+ System.out.println("-------paramDescTuple=" + paramDescTuple + "->argDescTuple="
+ + argDescTuple);
+ addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
+ }
+ }
+
+ // addValueFlowBetweenParametersToCaller(min, mdCaller, possibleMdCallee);
+
+ NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
+ GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
+ Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
+ for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
+ GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
+ addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
+ }
+
+ System.out.println("$$$GLOBAL PC LOC ADD=" + mdCaller);
+ Set<NTuple<Location>> pcLocTupleSet = mapMethodInvokeNodeToPCLocTupleSet.get(min);
+ System.out.println("---pcLocTupleSet=" + pcLocTupleSet);
+ GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
+ for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
+ GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
+ if (calleeNode.isParamNodeWithIncomingFlows()) {
+ System.out.println("calleeNode.getLocTuple()" + calleeNode.getLocTuple());
+ NTuple<Location> callerSrcNodeLocTuple =
+ translateToCallerLocTuple(min, possibleMdCallee, mdCaller, calleeNode.getLocTuple());
+ System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
+ if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
+ for (Iterator iterator2 = pcLocTupleSet.iterator(); iterator2.hasNext();) {
+ NTuple<Location> pcLocTuple = (NTuple<Location>) iterator2.next();
+
+ callerSubGlobalGraph.addValueFlowEdge(pcLocTuple, callerSrcNodeLocTuple);
+ }
+ }
+ }
+
+ }
+
+ }
+
+ private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
+ MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
+
+ GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
+ GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
+
+ System.out.println("$addValueFlowFromCalleeNode calleeSrcNode=" + calleeSrcNode);
+
+ NTuple<Location> callerSrcNodeLocTuple =
+ translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
+ System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
+
+ if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
+
+ Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
+
+ for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
+ GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
+ NTuple<Location> callerDstNodeLocTuple =
+ translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
+ // System.out.println("outNode=" + outNode + " callerDstNodeLocTuple="
+ // + callerDstNodeLocTuple);
+ if (callerSrcNodeLocTuple != null && callerDstNodeLocTuple != null
+ && callerSrcNodeLocTuple.size() > 0 && callerDstNodeLocTuple.size() > 0) {
+ callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
+ }
+ }
+ }
+
+ }
+
+ private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
+ MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
+ // this method will return the same nodeLocTuple if the corresponding argument is literal
+ // value.
+
+ // System.out.println("translateToCallerLocTuple=" + nodeLocTuple);
+
+ FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
+ NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
+ if (calleeFlowGraph.isParameter(nodeDescTuple)) {
+ int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
+ NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
+
+ // if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
+ // // the type of argument is primitive.
+ // return nodeLocTuple.clone();
+ // }
+ // System.out.println("paramIdx=" + paramIdx + " argDescTuple=" + argDescTuple + " from min="
+ // + min.printNode(0));
+ NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
+
+ NTuple<Location> callerLocTuple = new NTuple<Location>();
+
+ callerLocTuple.addAll(argLocTuple);
+ for (int i = 1; i < nodeLocTuple.size(); i++) {
+ callerLocTuple.add(nodeLocTuple.get(i));
+ }
+ return callerLocTuple;
+ } else {
+ return nodeLocTuple.clone();
+ }
+
+ }
+
+ public static boolean isPrimitive(Descriptor desc) {
+
+ if (desc instanceof FieldDescriptor) {
+ return ((FieldDescriptor) desc).getType().isPrimitive();
+ } else if (desc instanceof VarDescriptor) {
+ return ((VarDescriptor) desc).getType().isPrimitive();
+ } else if (desc instanceof InterDescriptor) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public static boolean isReference(Descriptor desc) {
+
+ if (desc instanceof FieldDescriptor) {
+
+ TypeDescriptor type = ((FieldDescriptor) desc).getType();
+ if (type.isArray()) {
+ return !type.isPrimitive();
+ } else {
+ return type.isPtr();
+ }
+
+ } else if (desc instanceof VarDescriptor) {
+ TypeDescriptor type = ((VarDescriptor) desc).getType();
+ if (type.isArray()) {
+ return !type.isPrimitive();
+ } else {
+ return type.isPtr();
+ }
+ }
+
+ return false;
+ }
+
+ private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
+
+ NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
+ for (int i = 0; i < locTuple.size(); i++) {
+ descTuple.add(locTuple.get(i).getLocDescriptor());
+ }
+ return descTuple;
+
+ }
+
+ public LocationSummary getLocationSummary(Descriptor d) {
+ if (!mapDescToLocationSummary.containsKey(d)) {
+ if (d instanceof MethodDescriptor) {
+ mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
+ } else if (d instanceof ClassDescriptor) {
+ mapDescToLocationSummary.put(d, new FieldSummary());
+ }
+ }
+ return mapDescToLocationSummary.get(d);
+ }
+
+ private void generateMethodSummary() {
+
+ Set<MethodDescriptor> keySet = md2lattice.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ MethodDescriptor md = (MethodDescriptor) iterator.next();
+
+ System.out.println("\nSSJAVA: generate method summary: " + md);
+
+ FlowGraph flowGraph = getFlowGraph(md);
+ MethodSummary methodSummary = getMethodSummary(md);
+
+ HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
+
+ // set the 'this' reference location
+ if (!md.isStatic()) {
+ System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
+ methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
+ }
+
+ // set the 'global' reference location if needed
+ if (methodSummary.hasGlobalAccess()) {
+ methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
+ }
+
+ // construct a parameter mapping that maps a parameter descriptor to an
+ // inferred composite location
+ for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
+ FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
+ CompositeLocation inferredCompLoc =
+ updateCompositeLocation(flowNode.getCompositeLocation());
+ System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc + " original="
+ + flowNode.getCompositeLocation());
+
+ Descriptor localVarDesc = flowNode.getDescTuple().get(0);
+ methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
+ methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
+ }
+
+ }
+
+ }
+
+ private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
+
+ int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
+
+ for (int idx = 0; idx < size; idx++) {
+ Location loc1 = locTuple1.get(idx);
+ Location loc2 = locTuple2.get(idx);
+
+ Descriptor desc1 = loc1.getDescriptor();
+ Descriptor desc2 = loc2.getDescriptor();
+
+ if (!desc1.equals(desc2)) {
+ throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
+ }
+
+ Descriptor locDesc1 = loc1.getLocDescriptor();
+ Descriptor locDesc2 = loc2.getLocDescriptor();
+
+ HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
+
+ HNode node1 = hierarchyGraph.getHNode(locDesc1);
+ HNode node2 = hierarchyGraph.getHNode(locDesc2);
+
+ System.out.println("---node1=" + node1 + " node2=" + node2);
+ System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
+ + hierarchyGraph.getIncomingNodeSet(node2));
+
+ if (locDesc1.equals(locDesc2)) {
+ continue;
+ } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
+ && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
+ return false;
+ } else {
+ return true;
+ }
+
+ }
+
+ return false;
+
+ }
+
+ private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
+
+ int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
+
+ for (int idx = 0; idx < size; idx++) {
+ Location loc1 = locTuple1.get(idx);
+ Location loc2 = locTuple2.get(idx);
+
+ Descriptor desc1 = loc1.getDescriptor();
+ Descriptor desc2 = loc2.getDescriptor();
+
+ if (!desc1.equals(desc2)) {
+ throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
+ }
+
+ Descriptor locDesc1 = loc1.getLocDescriptor();
+ Descriptor locDesc2 = loc2.getLocDescriptor();
+
+ HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
+
+ HNode node1 = hierarchyGraph.getHNode(locDesc1);
+ HNode node2 = hierarchyGraph.getHNode(locDesc2);
+
+ System.out.println("---node1=" + node1 + " node2=" + node2);
+ System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
+ + hierarchyGraph.getIncomingNodeSet(node2));
+
+ if (locDesc1.equals(locDesc2)) {
+ continue;
+ } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
+ return true;
+ } else {
+ return false;
+ }
+
+ }
+
+ return false;
+ }
+
+ private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
+ CompositeLocation newCompLoc = new CompositeLocation();
+
+ // System.out.println("compLoc=" + compLoc);
+ for (int i = 0; i < compLoc.getSize(); i++) {
+ Location loc = compLoc.get(i);
+ Descriptor enclosingDescriptor = loc.getDescriptor();
+ Descriptor locDescriptor = loc.getLocDescriptor();
+
+ HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
+ // System.out.println("-hnode=" + hnode + " from=" + locDescriptor +
+ // " enclosingDescriptor="
+ // + enclosingDescriptor);
+ // System.out.println("-getLocationSummary(enclosingDescriptor)="
+ // + getLocationSummary(enclosingDescriptor));
+ String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
+ // System.out.println("-locName=" + locName);
+ Location newLoc = new Location(enclosingDescriptor, locName);
+ newLoc.setLocDescriptor(locDescriptor);
+ newCompLoc.addLocation(newLoc);
+ }
+
+ return newCompLoc;
+ }
+
+ private void debug_writeLattices() {
+
+ Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor key = (Descriptor) iterator.next();
+ SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
+ // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
+ HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
+ if (key instanceof ClassDescriptor) {
+ writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
+ "_SIMPLE");
+ } else if (key instanceof MethodDescriptor) {
+ MethodDescriptor md = (MethodDescriptor) key;
+ writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
+ "_SIMPLE");
+ }
+
+ LocationSummary ls = getLocationSummary(key);
+ System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
+ }
+
+ Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
+ for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
+ ClassDescriptor cd = (ClassDescriptor) iterator.next();
+ writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
+ cd2lattice.get(cd), "");
+ }
+
+ Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
+ for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
+ MethodDescriptor md = (MethodDescriptor) iterator.next();
+ writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
+ md2lattice.get(md), "");
+ }
+
+ }
+
+ private void buildLattice() {
+
+ BuildLattice buildLattice = new BuildLattice(this);
+
+ Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+
+ SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
+
+ addMapDescToSimpleLattice(desc, simpleLattice);
+
+ HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
+ System.out.println("\n## insertIntermediateNodesToStraightLine:"
+ + simpleHierarchyGraph.getName());
+ SSJavaLattice<String> lattice =
+ buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
+ lattice.removeRedundantEdges();
+
+ if (desc instanceof ClassDescriptor) {
+ // field lattice
+ cd2lattice.put((ClassDescriptor) desc, lattice);
+ // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
+ } else if (desc instanceof MethodDescriptor) {
+ // method lattice
+ md2lattice.put((MethodDescriptor) desc, lattice);
+ MethodDescriptor md = (MethodDescriptor) desc;
+ ClassDescriptor cd = md.getClassDesc();
+ // ssjava.writeLatticeDotFile(cd, md, lattice);
+ }
+
+ // System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
+ // HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
+ // HierarchyGraph skeletonGraphWithCombinationNode =
+ // skeletonGraph.clone();
+ // skeletonGraphWithCombinationNode.setName(desc + "_SC");
+ //
+ // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
+ // System.out.println("Identifying Combination Nodes:");
+ // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
+ // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
+ // mapDescriptorToCombineSkeletonHierarchyGraph.put(desc,
+ // skeletonGraphWithCombinationNode);
+ }
+
+ }
+
+ public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
+ mapDescriptorToSimpleLattice.put(desc, lattice);
+ }
+
+ public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
+ return mapDescriptorToSimpleLattice.get(desc);
+ }
+
+ private void simplifyHierarchyGraph() {
+ Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ // System.out.println("SSJAVA: remove redundant edges: " + desc);
+ HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
+ simpleHierarchyGraph.setName(desc + "_SIMPLE");
+ simpleHierarchyGraph.removeRedundantEdges();
+ mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
+ }
+ }
+
+ private void insertCombinationNodes() {
+ Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
+ HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
+ HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
+ skeletonGraphWithCombinationNode.setName(desc + "_SC");
+
+ HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
+ System.out.println("Identifying Combination Nodes:");
+ skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
+ skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
+ mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
+ }
+ }
+
+ private void constructSkeletonHierarchyGraph() {
+ Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ System.out.println("SSJAVA: Constructing Skeleton Hierarchy Graph: " + desc);
+ HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
+ HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
+ skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
+ skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
+ skeletonGraph.simplifyHierarchyGraph();
+ // skeletonGraph.combineRedundantNodes(false);
+ // skeletonGraph.removeRedundantEdges();
+ mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
+ }
+ }
+
+ private void debug_writeHierarchyDotFiles() {
+
+ Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ getHierarchyGraph(desc).writeGraph();
+ }
+
+ }
+
+ private void debug_writeSimpleHierarchyDotFiles() {
+
+ Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ getHierarchyGraph(desc).writeGraph();
+ getSimpleHierarchyGraph(desc).writeGraph();
+ getSimpleHierarchyGraph(desc).writeGraph(true);
+ }
+
+ }
+
+ private void debug_writeSkeletonHierarchyDotFiles() {
+
+ Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ getSkeletonHierarchyGraph(desc).writeGraph();
+ }
+
+ }
+
+ private void debug_writeSkeletonCombinationHierarchyDotFiles() {
+
+ Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ getSkeletonCombinationHierarchyGraph(desc).writeGraph();
+ }
+
+ }
+
+ public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
+ return mapDescriptorToSimpleHierarchyGraph.get(d);
+ }
+
+ private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
+ if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
+ mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
+ }
+ return mapDescriptorToSkeletonHierarchyGraph.get(d);
+ }
+
+ public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
+ if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
+ mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
+ }
+ return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
+ }
+
+ private void constructHierarchyGraph() {
+
+ LinkedList<MethodDescriptor> methodDescList =
+ (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
+
+ while (!methodDescList.isEmpty()) {
+ MethodDescriptor md = methodDescList.removeLast();
+ if (state.SSJAVADEBUG) {
+ HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
+ System.out.println();
+ System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
+ constructHierarchyGraph(md, hierarchyGraph);
+ mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
+
+ }
+ }
+
+ setupToAnalyze();
+ while (!toAnalyzeIsEmpty()) {
+ ClassDescriptor cd = toAnalyzeNext();
+ HierarchyGraph graph = getHierarchyGraph(cd);
+ for (Iterator iter = cd.getFields(); iter.hasNext();) {
+ FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
+ if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
+ graph.getHNode(fieldDesc);
+ }
+ }
+ }
+
+ Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor key = (Descriptor) iterator.next();
+ HierarchyGraph graph = getHierarchyGraph(key);
+
+ Set<HNode> nodeToBeConnected = new HashSet<HNode>();
+ for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
+ HNode node = (HNode) iterator2.next();
+ if (!node.isSkeleton() && !node.isCombinationNode()) {
+ if (graph.getIncomingNodeSet(node).size() == 0) {
+ nodeToBeConnected.add(node);
+ }
+ }
+ }
+
+ for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
+ HNode node = (HNode) iterator2.next();
+ System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
+ graph.addEdge(graph.getHNode(TOPDESC), node);
+ }
+
+ }
+
+ }
+
+ private void constructHierarchyGraph2() {
+
+ // do fixed-point analysis
+
+ LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
+
+ // Collections.sort(descriptorListToAnalyze, new
+ // Comparator<MethodDescriptor>() {
+ // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
+ // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
+ // }
+ // });
+
+ // 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()) {
+ // start to analyze leaf node
+ MethodDescriptor md = methodDescriptorsToVisitStack.pop();
+
+ HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
+ // MethodSummary methodSummary = new MethodSummary(md);
+
+ // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
+ // curMethodInfo = methodInfo;
+
+ System.out.println();
+ System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
+
+ constructHierarchyGraph(md, hierarchyGraph);
+
+ HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
+ // MethodSummary prevMethodSummary = getMethodSummary(md);
+
+ if (!hierarchyGraph.equals(prevHierarchyGraph)) {
+
+ mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
+ // mapDescToLocationSummary.put(md, methodSummary);
+
+ // results for callee changed, so enqueue dependents caller for
+ // further analysis
+ Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
+ while (depsItr.hasNext()) {
+ MethodDescriptor methodNext = depsItr.next();
+ if (!methodDescriptorsToVisitStack.contains(methodNext)
+ && methodDescriptorToVistSet.contains(methodNext)) {
+ methodDescriptorsToVisitStack.add(methodNext);
+ }
+ }
+
+ }
+
+ }
+
+ setupToAnalyze();
+ while (!toAnalyzeIsEmpty()) {
+ ClassDescriptor cd = toAnalyzeNext();
+ HierarchyGraph graph = getHierarchyGraph(cd);
+ for (Iterator iter = cd.getFields(); iter.hasNext();) {
+ FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
+ if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
+ graph.getHNode(fieldDesc);
+ }
+ }
+ }
+
+ Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor key = (Descriptor) iterator.next();
+ HierarchyGraph graph = getHierarchyGraph(key);
+
+ Set<HNode> nodeToBeConnected = new HashSet<HNode>();
+ for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
+ HNode node = (HNode) iterator2.next();
+ if (!node.isSkeleton() && !node.isCombinationNode()) {
+ if (graph.getIncomingNodeSet(node).size() == 0) {
+ nodeToBeConnected.add(node);
+ }
+ }
+ }
+
+ for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
+ HNode node = (HNode) iterator2.next();
+ System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
+ graph.addEdge(graph.getHNode(TOPDESC), node);
+ }
+
+ }
+
+ }
+
+ private HierarchyGraph getHierarchyGraph(Descriptor d) {
+ if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
+ mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
+ }
+ return mapDescriptorToHierarchyGraph.get(d);
+ }
+
+ private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
+
+ // visit each node of method flow graph
+ FlowGraph fg = getFlowGraph(md);
+ // Set<FlowNode> nodeSet = fg.getNodeSet();
+
+ Set<FlowEdge> edgeSet = fg.getEdgeSet();
+
+ Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
+ for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ methodGraph.getHNode(desc).setSkeleton(true);
+ }
+
+ // for the method lattice, we need to look at the first element of
+ // NTuple<Descriptor>
+ boolean hasGlobalAccess = false;
+ // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ // FlowNode originalSrcNode = (FlowNode) iterator.next();
+ for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
+ FlowEdge edge = (FlowEdge) iterator.next();
+
+ FlowNode originalSrcNode = fg.getFlowNode(edge.getInitTuple());
+ Set<FlowNode> sourceNodeSet = new HashSet<FlowNode>();
+ if (originalSrcNode instanceof FlowReturnNode) {
+ FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
+ System.out.println("rnode=" + rnode);
+ Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
+ for (Iterator iterator2 = tupleSet.iterator(); iterator2.hasNext();) {
+ NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator2.next();
+ sourceNodeSet.add(fg.getFlowNode(nTuple));
+ System.out.println("&&&SOURCE fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
+ }
+ } else {
+ sourceNodeSet.add(originalSrcNode);
+ }
+
+ // System.out.println("---sourceNodeSet=" + sourceNodeSet + " from originalSrcNode="
+ // + originalSrcNode);
+
+ for (Iterator iterator3 = sourceNodeSet.iterator(); iterator3.hasNext();) {
+ FlowNode srcNode = (FlowNode) iterator3.next();
+
+ NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
+ Descriptor srcLocalDesc = srcNodeTuple.get(0);
+
+ if (srcLocalDesc instanceof InterDescriptor
+ && ((InterDescriptor) srcLocalDesc).getMethodArgIdxPair() != null) {
+
+ if (srcNode.getCompositeLocation() == null) {
+ continue;
+ }
+ }
+
+ // if the srcNode is started with the global descriptor
+ // need to set as a skeleton node
+ if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
+ hasGlobalAccess = true;
+ }
+
+ // Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(originalSrcNode);
+ // for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
+ // FlowEdge outEdge = (FlowEdge) iterator2.next();
+ // FlowNode originalDstNode = outEdge.getDst();
+ FlowNode originalDstNode = fg.getFlowNode(edge.getEndTuple());
+
+ Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
+ if (originalDstNode instanceof FlowReturnNode) {
+ FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
+ // System.out.println("\n-returnNode=" + rnode);
+ Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
+ for (Iterator iterator4 = tupleSet.iterator(); iterator4.hasNext();) {
+ NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator4.next();
+ dstNodeSet.add(fg.getFlowNode(nTuple));
+ System.out.println("&&&DST fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
+ }
+ } else {
+ dstNodeSet.add(originalDstNode);
+ }
+ // System.out.println("---dstNodeSet=" + dstNodeSet);
+ for (Iterator iterator4 = dstNodeSet.iterator(); iterator4.hasNext();) {
+ FlowNode dstNode = (FlowNode) iterator4.next();
+
+ NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
+ Descriptor dstLocalDesc = dstNodeTuple.get(0);
+
+ if (dstLocalDesc instanceof InterDescriptor
+ && ((InterDescriptor) dstLocalDesc).getMethodArgIdxPair() != null) {
+ if (dstNode.getCompositeLocation() == null) {
+ System.out.println("%%%%%%%%%%%%%SKIP=" + dstNode);
+ continue;
+ }
+ }
+
+ // if (outEdge.getInitTuple().equals(srcNodeTuple)
+ // && outEdge.getEndTuple().equals(dstNodeTuple)) {
+
+ NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
+ NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
+
+ System.out.println("-srcCurTuple=" + srcCurTuple + " dstCurTuple=" + dstCurTuple
+ + " srcNode=" + srcNode + " dstNode=" + dstNode);
+
+ // srcCurTuple = translateBaseTuple(srcNode, srcCurTuple);
+ // dstCurTuple = translateBaseTuple(dstNode, dstCurTuple);
+
+ if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
+ && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
+
+ // value flows between fields
+ Descriptor desc = srcCurTuple.get(0);
+ ClassDescriptor classDesc;
+
+ if (desc.equals(GLOBALDESC)) {
+ classDesc = md.getClassDesc();
+ } else {
+ VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
+ classDesc = varDesc.getType().getClassDesc();
+ }
+ extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
+
+ } else if ((srcCurTuple.size() == 1 && dstCurTuple.size() == 1)
+ || ((srcCurTuple.size() > 1 || dstCurTuple.size() > 1) && !srcCurTuple.get(0).equals(
+ dstCurTuple.get(0)))) {
+
+ // value flow between a primitive local var - a primitive local var or local var -
+ // field
+
+ Descriptor srcDesc = srcCurTuple.get(0);
+ Descriptor dstDesc = dstCurTuple.get(0);
+
+ methodGraph.addEdge(srcDesc, dstDesc);
+
+ if (fg.isParamDesc(srcDesc)) {
+ methodGraph.setParamHNode(srcDesc);
+ }
+ if (fg.isParamDesc(dstDesc)) {
+ methodGraph.setParamHNode(dstDesc);
+ }
+
+ }
+
+ // }
+ // }
+
+ }
+
+ }
+
+ }
+
+ // If the method accesses static fields
+ // set hasGloabalAccess true in the method summary.
+ if (hasGlobalAccess) {
+ getMethodSummary(md).setHasGlobalAccess();
+ }
+ methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
+
+ if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
+ // if the current method contains the event loop
+ // we need to set all nodes of the hierarchy graph as a skeleton node
+ Set<HNode> hnodeSet = methodGraph.getNodeSet();
+ for (Iterator iterator = hnodeSet.iterator(); iterator.hasNext();) {
+ HNode hnode = (HNode) iterator.next();
+ hnode.setSkeleton(true);
+ }
+ }
+
+ }
+
+ private NTuple<Descriptor> translateBaseTuple(FlowNode flowNode, NTuple<Descriptor> inTuple) {
+
+ if (flowNode.getBaseTuple() != null) {
+
+ NTuple<Descriptor> translatedTuple = new NTuple<Descriptor>();
+
+ NTuple<Descriptor> baseTuple = flowNode.getBaseTuple();
+
+ for (int i = 0; i < baseTuple.size(); i++) {
+ translatedTuple.add(baseTuple.get(i));
+ }
+
+ for (int i = 1; i < inTuple.size(); i++) {
+ translatedTuple.add(inTuple.get(i));
+ }
+
+ System.out.println("------TRANSLATED " + inTuple + " -> " + translatedTuple);
+ return translatedTuple;
+
+ } else {
+ return inTuple;
+ }
+
+ }
+
+ private MethodSummary getMethodSummary(MethodDescriptor md) {
+ if (!mapDescToLocationSummary.containsKey(md)) {
+ mapDescToLocationSummary.put(md, new MethodSummary(md));
+ }
+ return (MethodSummary) mapDescToLocationSummary.get(md);
+ }
+
+ private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
+
+ String classSymbol = cd.getSymbol();
+ int idx = classSymbol.lastIndexOf("$");
+ if (idx != -1) {
+ classSymbol = classSymbol.substring(idx + 1);
+ }
+
+ String pattern = "class " + classSymbol + " ";
+ if (strLine.indexOf(pattern) != -1) {
+ mapDescToDefinitionLine.put(cd, lineNum);
+ }
+ }
+
+ private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
+ int lineNum) {
+ for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
+ MethodDescriptor md = (MethodDescriptor) iterator.next();
+ String pattern = md.getMethodDeclaration();
+ if (strLine.indexOf(pattern) != -1) {
+ mapDescToDefinitionLine.put(md, lineNum);
+ methodSet.remove(md);
+ return;
+ }
+ }
+
+ }
+
+ private void readOriginalSourceFiles() {
+
+ SymbolTable classtable = state.getClassSymbolTable();
+
+ Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
+ classDescSet.addAll(classtable.getValueSet());
+
+ try {
+ // inefficient implement. it may re-visit the same file if the file
+ // contains more than one class definitions.
+ for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
+ ClassDescriptor cd = (ClassDescriptor) iterator.next();
+
+ Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
+ methodSet.addAll(cd.getMethodTable().getValueSet());
+
+ String sourceFileName = cd.getSourceFileName();
+ Vector<String> lineVec = new Vector<String>();
+
+ mapFileNameToLineVector.put(sourceFileName, lineVec);
+
+ BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
+ String strLine;
+ int lineNum = 1;
+ lineVec.add(""); // the index is started from 1.
+ while ((strLine = in.readLine()) != null) {
+ lineVec.add(lineNum, strLine);
+ addMapClassDefinitionToLineNum(cd, strLine, lineNum);
+ addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
+ lineNum++;
+ }
+
+ }
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ private String generateLatticeDefinition(Descriptor desc) {
+
+ Set<String> sharedLocSet = new HashSet<String>();
+
+ SSJavaLattice<String> lattice = getLattice(desc);
+ String rtr = "@LATTICE(\"";
+
+ Map<String, Set<String>> map = lattice.getTable();
+ Set<String> keySet = map.keySet();
+ boolean first = true;
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ String key = (String) iterator.next();
+ if (!key.equals(lattice.getTopItem())) {
+ Set<String> connectedSet = map.get(key);
+
+ if (connectedSet.size() == 1) {
+ if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
+ if (!first) {
+ rtr += ",";
+ } else {
+ rtr += "LOC,";
+ first = false;
+ }
+ rtr += key;
+ if (lattice.isSharedLoc(key)) {
+ rtr += "," + key + "*";
+ }
+ }
+ }
+
+ for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
+ String loc = (String) iterator2.next();
+ if (!loc.equals(lattice.getBottomItem())) {
+ if (!first) {
+ rtr += ",";
+ } else {
+ rtr += "LOC,";
+ first = false;
+ }
+ rtr += loc + "<" + key;
+ if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
+ rtr += "," + key + "*";
+ sharedLocSet.add(key);
+ }
+ if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
+ rtr += "," + loc + "*";
+ sharedLocSet.add(loc);
+ }
+
+ }
+ }
+ }
+ }
+
+ if (desc instanceof MethodDescriptor) {
+ System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
+
+ MethodDescriptor md = (MethodDescriptor) desc;
+ MethodSummary methodSummary = getMethodSummary(md);
+
+ TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
+ if (!ssjava.getMethodContainingSSJavaLoop().equals(desc) && returnType != null
+ && (!returnType.isVoid())) {
+ CompositeLocation returnLoc = methodSummary.getRETURNLoc();
+ if (returnLoc.getSize() == 1) {
+ String returnLocStr = generateLocationAnnoatation(methodSummary.getRETURNLoc());
+ if (rtr.indexOf(returnLocStr) == -1) {
+ rtr += "," + returnLocStr;
+ }
+ }
+ }
+ rtr += "\")";
+
+ if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
+ if (returnType != null && (!returnType.isVoid())) {
+ rtr +=
+ "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
+ }
+
+ CompositeLocation pcLoc = methodSummary.getPCLoc();
+ if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
+ rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
+ }
+ }
+
+ if (!md.isStatic()) {
+ rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
+ }
+ rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
+
+ } else {
+ rtr += "\")";
+ }
+
+ return rtr;
+ }
+
+ private void generateAnnoatedCode() {
+
+ readOriginalSourceFiles();
+
+ setupToAnalyze();
+ while (!toAnalyzeIsEmpty()) {
+ ClassDescriptor cd = toAnalyzeNext();
+
+ setupToAnalazeMethod(cd);
+
+ String sourceFileName = cd.getSourceFileName();
+
+ if (cd.isInterface()) {
+ continue;
+ }
+
+ int classDefLine = mapDescToDefinitionLine.get(cd);
+ Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
+
+ LocationSummary fieldLocSummary = getLocationSummary(cd);
+
+ String fieldLatticeDefStr = generateLatticeDefinition(cd);
+ String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
+ sourceVec.set(classDefLine, annoatedSrc);
+
+ // generate annotations for field declarations
+ // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
+ Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
+
+ for (Iterator iter = cd.getFields(); iter.hasNext();) {
+ FieldDescriptor fd = (FieldDescriptor) iter.next();
+
+ String locAnnotationStr;
+ // CompositeLocation inferLoc = inferLocMap.get(fd);
+ String locName = mapFieldNameToLocName.get(fd.getSymbol());
+
+ if (locName != null) {
+ // infer loc is null if the corresponding field is static and final
+ // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
+ locAnnotationStr = "@LOC(\"" + locName + "\")";
+ int fdLineNum = fd.getLineNum();
+ String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
+ String fieldDeclaration = fd.toString();
+ fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
+ String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
+ sourceVec.set(fdLineNum, annoatedStr);
+ }
+
+ }
+
+ while (!toAnalyzeMethodIsEmpty()) {
+ MethodDescriptor md = toAnalyzeMethodNext();
+
+ if (!ssjava.needTobeAnnotated(md)) {
+ continue;
+ }
+
+ SSJavaLattice<String> methodLattice = md2lattice.get(md);
+ if (methodLattice != null) {
+
+ int methodDefLine = md.getLineNum();
+
+ // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
+ // Map<Descriptor, CompositeLocation> methodInferLocMap =
+ // methodLocInfo.getMapDescToInferLocation();
+
+ MethodSummary methodSummary = getMethodSummary(md);
+
+ Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
+ methodSummary.getMapVarDescToInferCompositeLocation();
+ System.out.println("-----md=" + md);
+ System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
+
+ Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
+
+ Set<String> localLocElementSet = methodLattice.getElementSet();
+
+ for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
+ Descriptor localVarDesc = (Descriptor) iterator.next();
+ System.out.println("-------localVarDesc=" + localVarDesc);
+ CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
+
+ String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
+ if (!localLocElementSet.contains(localLocIdentifier)) {
+ methodLattice.put(localLocIdentifier);
+ }
+
+ String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
+
+ if (!isParameter(md, localVarDesc)) {
+ if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
+ int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
+ String orgSourceLine = sourceVec.get(varLineNum);
+ System.out.println("varLineNum=" + varLineNum + " org src=" + orgSourceLine);
+ int idx =
+ orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
+ System.out.println("idx=" + idx
+ + " generateVarDeclaration((VarDescriptor) localVarDesc)="
+ + generateVarDeclaration((VarDescriptor) localVarDesc));
+ assert (idx != -1);
+ String annoatedStr =
+ orgSourceLine.substring(0, idx) + locAnnotationStr + " "
+ + orgSourceLine.substring(idx);
+ sourceVec.set(varLineNum, annoatedStr);
+ }
+ } else {
+ String methodDefStr = sourceVec.get(methodDefLine);
+
+ int idx =
+ getParamLocation(methodDefStr,
+ generateVarDeclaration((VarDescriptor) localVarDesc));
+ System.out.println("methodDefStr=" + methodDefStr + " localVarDesc=" + localVarDesc
+ + " idx=" + idx);
+ assert (idx != -1);
+
+ String annoatedStr =
+ methodDefStr.substring(0, idx) + locAnnotationStr + " "
+ + methodDefStr.substring(idx);
+ sourceVec.set(methodDefLine, annoatedStr);
+ }
+
+ }
+
+ // check if the lattice has to have the location type for the this
+ // reference...
+
+ // boolean needToAddthisRef = hasThisReference(md);
+ // if (localLocElementSet.contains("this")) {
+ // methodLattice.put("this");
+ // }
+
+ String methodLatticeDefStr = generateLatticeDefinition(md);
+ String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
+ sourceVec.set(methodDefLine, annoatedStr);
+
+ }
+ }
+
+ }
+
+ codeGen();
+ }
+
+ private boolean hasThisReference(MethodDescriptor md) {
+
+ FlowGraph fg = getFlowGraph(md);
+ Set<FlowNode> nodeSet = fg.getNodeSet();
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ FlowNode flowNode = (FlowNode) iterator.next();
+ if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private int getParamLocation(String methodStr, String paramStr) {
+
+ String pattern = paramStr + ",";
+
+ int idx = methodStr.indexOf(pattern);
+ if (idx != -1) {
+ return idx;
+ } else {
+ pattern = paramStr + ")";
+ return methodStr.indexOf(pattern);
+ }
+
+ }
+
+ private String generateVarDeclaration(VarDescriptor varDesc) {
+
+ TypeDescriptor td = varDesc.getType();
+ String rtr = td.toString();
+ if (td.isArray()) {
+ for (int i = 0; i < td.getArrayCount(); i++) {
+ rtr += "[]";
+ }
+ }
+ rtr += " " + varDesc.getName();
+ return rtr;
+
+ }
+
+ private String generateLocationAnnoatation(CompositeLocation loc) {
+ String rtr = "";
+ // method location
+ Location methodLoc = loc.get(0);
+ rtr += methodLoc.getLocIdentifier();
+
+ for (int i = 1; i < loc.getSize(); i++) {
+ Location element = loc.get(i);
+ rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
+ }
+
+ return rtr;
+ }
+
+ private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
+ return getFlowGraph(md).isParamDesc(localVarDesc);
+ }
+
+ private String extractFileName(String fileName) {
+ int idx = fileName.lastIndexOf("/");
+ if (idx == -1) {
+ return fileName;
+ } else {
+ return fileName.substring(idx + 1);
+ }
+
+ }
+
+ private void codeGen() {
+
+ Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
+ for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
+ String orgFileName = (String) iterator.next();
+ String outputFileName = extractFileName(orgFileName);
+
+ Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
+
+ try {
+
+ FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
+ BufferedWriter out = new BufferedWriter(fileWriter);
+
+ for (int i = 0; i < sourceVec.size(); i++) {
+ out.write(sourceVec.get(i));
+ out.newLine();
+ }
+ out.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ }
+
+ private void checkLattices() {
+
+ LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
+
+ // current descriptors to visit in fixed-point interprocedural analysis,
+ // prioritized by
+ // dependency in the call graph
+ methodDescriptorsToVisitStack.clear();
+
+ // descriptorListToAnalyze.removeFirst();
+
+ Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
+ methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
+
+ while (!descriptorListToAnalyze.isEmpty()) {
+ MethodDescriptor md = descriptorListToAnalyze.removeFirst();
+ checkLatticesOfVirtualMethods(md);
+ }
+
+ }
+
+ private void debug_writeLatticeDotFile() {
+ // generate lattice dot file
+
+ setupToAnalyze();
+
+ while (!toAnalyzeIsEmpty()) {
+ ClassDescriptor cd = toAnalyzeNext();
+
+ setupToAnalazeMethod(cd);
+
+ SSJavaLattice<String> classLattice = cd2lattice.get(cd);
+ if (classLattice != null) {
+ ssjava.writeLatticeDotFile(cd, null, classLattice);
+ debug_printDescriptorToLocNameMapping(cd);
+ }
+
+ while (!toAnalyzeMethodIsEmpty()) {
+ MethodDescriptor md = toAnalyzeMethodNext();
+ SSJavaLattice<String> methodLattice = md2lattice.get(md);
+ if (methodLattice != null) {
+ ssjava.writeLatticeDotFile(cd, md, methodLattice);
+ debug_printDescriptorToLocNameMapping(md);
+ }
+ }
+ }
+
+ }
+
+ private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
+
+ LocationInfo info = getLocationInfo(desc);
+ System.out.println("## " + desc + " ##");
+ System.out.println(info.getMapDescToInferLocation());
+ LocationInfo locInfo = getLocationInfo(desc);
+ System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
+ System.out.println("###################");
+
+ }
+
+ private void calculateExtraLocations() {
+
+ LinkedList<MethodDescriptor> methodDescList = ssjava.getSortedDescriptors();
+ for (Iterator iterator = methodDescList.iterator(); iterator.hasNext();) {
+ MethodDescriptor md = (MethodDescriptor) iterator.next();
+ if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
+ calculateExtraLocations(md);
+ }
+ }
+
+ }
+
+ private void checkLatticesOfVirtualMethods(MethodDescriptor md) {