+
+ }
+
+ private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
+ CompositeLocation updatedCompLoc = new CompositeLocation();
+ for (int i = 0; i < compLoc.getSize(); i++) {
+ Location loc = compLoc.get(i);
+ String nodeIdentifier = loc.getLocIdentifier();
+ Descriptor enclosingDesc = loc.getDescriptor();
+ String locName;
+ if (!enclosingDesc.equals(GLOBALDESC)) {
+ LocationSummary locSummary = getLocationSummary(enclosingDesc);
+ HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(enclosingDesc);
+ if (scGraph != null) {
+ HNode curNode = scGraph.getCurrentHNode(nodeIdentifier);
+ if (curNode != null) {
+ nodeIdentifier = curNode.getName();
+ }
+ }
+ locName = locSummary.getLocationName(nodeIdentifier);
+ } else {
+ locName = nodeIdentifier;
+ }
+ Location updatedLoc = new Location(enclosingDesc, locName);
+ updatedCompLoc.addLocation(updatedLoc);
+ }
+
+ return updatedCompLoc;
+ }
+
+ private void translateCompositeLocationAssignmentToFlowGraph(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();
+ for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
+ Location methodLoc = (Location) iterator.next();
+ if (methodLoc.getDescriptor().equals(mdCaller)) {
+ CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
+ assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
+ }
+ }
+
+ Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
+
+ Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
+ for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
+ MethodInvokeNode min = (MethodInvokeNode) iterator.next();
+ // need to translate a composite location that is started with the base
+ // tuple of 'min'.
+ translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
+ calleeSet.add(min.getMethod());
+ }
+
+ for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
+ MethodDescriptor callee = (MethodDescriptor) iterator.next();
+ translateCompositeLocationAssignmentToFlowGraph(callee);
+ }
+
+ }
+
+ 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);
+
+ 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 = null;
+ } 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();
+
+ 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 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("---callee loc=" + key + " newCalleeCompLoc=" + newCalleeCompLoc);
+ } 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("-----*AFTER TRANSLATING COMP LOC MAPPING, CALLEE MAPPING="
+ // + calleeGlobalGraph.getMapLocationToInferCompositeLocation());
+
+ // If the location of an argument has a composite location
+ // need to assign a proper composite location to the corresponding callee parameter
+ // System.out.println("---translate arg composite location to callee param. min="
+ // + min.printNode(0));
+ Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
+ 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);
+ 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));
+ }
+
+ if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
+
+ FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
+ NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
+ NTuple<Location> calleeParamLocTuple =
+ translateToLocTuple(mdCallee, calleeParamDescTuple);
+
+ 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("---callee loc=" + calleeParamLocTuple.get(0)
+ + " newCalleeCompLoc=" + newCalleeCompLoc);
+
+ // System.out.println("###need to assign composite location to=" + calleeParamDescTuple
+ // + " with baseTuple=" + baseLocTuple);
+ }
+
+ }
+ }
+
+ }
+
+ }
+
+ 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();
+ }
+
+ return true;
+ }
+
+ 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 + " reachableNodeSet="
+ // + reachableNodeSet);
+
+ 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());
+ }
+ }
+
+ if (!reachableCommonPrefixSet.isEmpty()) {
+
+ MethodDescriptor curPrefixFirstElementMethodDesc =
+ (MethodDescriptor) curPrefix.get(0).getDescriptor();
+
+ MethodDescriptor nodePrefixLocFirstElementMethodDesc =
+ (MethodDescriptor) prefixLoc.getDescriptor();
+
+ 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;
+ }
+
+ Location targetLocalLoc = node.getLocTuple().get(0);
+ // CompositeLocation curCompLoc = globalFlowGraph.getCompositeLocation(targetLocalLoc);
+ // if ((curPrefix.size() + 1) > curCompLoc.getSize()) {
+
+ CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
+ System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
+ System.out.println("- newCompLoc=" + newCompLoc);
+ globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
+ // }
+
+ continue next;
+ // }
+
+ }
+
+ }
+
+ }
+
+ }
+ // Set<GlobalFlowNode> inNodeSet =
+ // graph.getIncomingNodeSetWithPrefix(prefix);
+ // System.out.println("inNodeSet=" + inNodeSet + " from=" + node);
+ }
+
+ 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();
+
+ 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;
+ }
+ }
+ });
+
+ // remove a prefix which is not suitable for generating composite location
+ Location localVarLoc = node.getLocTuple().get(0);
+ MethodDescriptor md = (MethodDescriptor) localVarLoc.getDescriptor();
+ ClassDescriptor cd = md.getClassDesc();
+
+ int idx = 0;
+
+ Set<NTuple<Location>> toberemoved = new HashSet<NTuple<Location>>();
+ for (int i = 0; i < prefixList.size(); i++) {
+ NTuple<Location> prefixLocTuple = prefixList.get(i);
+ if (!containsClassDesc(cd, prefixLocTuple)) {
+ toberemoved.add(prefixLocTuple);
+ }
+ }
+
+ prefixList.removeAll(toberemoved);
+
+ return prefixList;
+
+ // List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
+ //
+ // for (Iterator iterator = incomingNodeSet.iterator(); iterator.hasNext();) {
+ // GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
+ // NTuple<Location> inNodeTuple = inNode.getLocTuple();
+ //
+ // 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 = new GlobalFlowGraph(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();
+
+ // here only keep the first element(method location) of the descriptor
+ // tuple
+ NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
+ // Location srcMethodLoc = srcLocTuple.get(0);
+ // Descriptor srcVarDesc = srcMethodLoc.getLocDescriptor();
+ // // if (flowGraph.isParamDesc(srcVarDesc) &&
+ // (!srcVarDesc.equals(md.getThis()))) {
+ // if (!srcVarDesc.equals(md.getThis())) {
+ // srcLocTuple = new NTuple<Location>();
+ // Location loc = new Location(md, srcVarDesc);
+ // srcLocTuple.add(loc);
+ // }
+ //
+ NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
+ // Location dstMethodLoc = dstLocTuple.get(0);
+ // Descriptor dstVarDesc = dstMethodLoc.getLocDescriptor();
+ // if (!dstVarDesc.equals(md.getThis())) {
+ // dstLocTuple = new NTuple<Location>();
+ // Location loc = new Location(md, dstVarDesc);
+ // dstLocTuple.add(loc);
+ // }
+
+ globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
+
+ }
+
+ return globalGraph;
+ }
+
+ private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
+
+ NTuple<Location> locTuple = new NTuple<Location>();
+
+ Descriptor enclosingDesc = md;
+ // System.out.println("md=" + md + " descTuple=" + descTuple);
+ 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 {
+ // TODO: inter descriptor case
+ enclosingDesc = desc;
+ }
+
+ }
+
+ return locTuple;