package Analysis.SSJava;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
+import java.util.Vector;
import IR.ClassDescriptor;
import IR.Descriptor;
private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
+ private Map<String, Vector<String>> mapFileNameToLineVector;
+
+ private Map<Descriptor, Integer> mapDescToDefinitionLine;
+
public static final String GLOBALLOC = "GLOBALLOC";
public static final String TOPLOC = "TOPLOC";
public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
+ public static String newline = System.getProperty("line.separator");
+
LocationInfo curMethodInfo;
boolean debug = true;
this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
+
+ this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
+ this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
}
public void setupToAnalyze() {
// 3) check properties
checkLattices();
+ // 4) generate annotated source codes
+ generateAnnoatedCode();
+
+ }
+
+ 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);
+ }
+
+ }
+ }
+ }
+ }
+
+ rtr += "\")";
+
+ if (desc instanceof MethodDescriptor) {
+ TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
+ if (returnType != null && (!returnType.isVoid())) {
+ rtr += "\n@RETURNLOC(\"RETURNLOC\")";
+ }
+ rtr += "\n@THISLOC(\"this\")\n@PCLOC(\"PCLOC\")\n@GLOBALLOC(\"GLOBALLOC\")";
+
+ }
+
+ return rtr;
+ }
+
+ private void generateAnnoatedCode() {
+
+ readOriginalSourceFiles();
+
+ setupToAnalyze();
+ while (!toAnalyzeIsEmpty()) {
+ ClassDescriptor cd = toAnalyzeNext();
+
+ setupToAnalazeMethod(cd);
+
+ LocationInfo locInfo = mapClassToLocationInfo.get(cd);
+ String sourceFileName = cd.getSourceFileName();
+
+ if (cd.isInterface()) {
+ continue;
+ }
+
+ int classDefLine = mapDescToDefinitionLine.get(cd);
+ Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
+
+ if (locInfo == null) {
+ locInfo = getLocationInfo(cd);
+ }
+
+ for (Iterator iter = cd.getFields(); iter.hasNext();) {
+ Descriptor fieldDesc = (Descriptor) iter.next();
+ String locIdentifier = locInfo.getFieldInferLocation(fieldDesc).getLocIdentifier();
+ if (!getLattice(cd).containsKey(locIdentifier)) {
+ getLattice(cd).put(locIdentifier);
+ }
+ }
+
+ String fieldLatticeDefStr = generateLatticeDefinition(cd);
+ String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
+ sourceVec.set(classDefLine, annoatedSrc);
+
+ // generate annotations for field declarations
+ LocationInfo fieldLocInfo = getLocationInfo(cd);
+ Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
+
+ for (Iterator iter = cd.getFields(); iter.hasNext();) {
+ FieldDescriptor fd = (FieldDescriptor) iter.next();
+
+ String locAnnotationStr;
+ if (inferLocMap.containsKey(fd)) {
+ CompositeLocation inferLoc = inferLocMap.get(fd);
+ locAnnotationStr = generateLocationAnnoatation(inferLoc);
+ } else {
+ // if the field is not accssed by SS part, just assigns dummy
+ // location
+ locAnnotationStr = "@LOC(\"LOC\")";
+ }
+ 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();
+ SSJavaLattice<String> methodLattice = md2lattice.get(md);
+ if (methodLattice != null) {
+
+ int methodDefLine = md.getLineNum();
+
+ MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
+
+ Map<Descriptor, CompositeLocation> methodInferLocMap =
+ methodLocInfo.getMapDescToInferLocation();
+ Set<Descriptor> localVarDescSet = methodInferLocMap.keySet();
+
+ for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
+ Descriptor localVarDesc = (Descriptor) iterator.next();
+ CompositeLocation inferLoc = methodInferLocMap.get(localVarDesc);
+
+ String locAnnotationStr = generateLocationAnnoatation(inferLoc);
+
+ if (!isParameter(md, localVarDesc)) {
+ if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
+ int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
+ String orgSourceLine = sourceVec.get(varLineNum);
+ int idx =
+ orgSourceLine.indexOf(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 = methodDefStr.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
+ assert (idx != -1);
+ String annoatedStr =
+ methodDefStr.substring(0, idx) + locAnnotationStr + " "
+ + methodDefStr.substring(idx);
+ sourceVec.set(methodDefLine, annoatedStr);
+ }
+
+ }
+
+ String methodLatticeDefStr = generateLatticeDefinition(md);
+ String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
+ sourceVec.set(methodDefLine, annoatedStr);
+
+ }
+ }
+
+ }
+
+ codeGen();
+ }
+
+ 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 = "@LOC(\"";
+
+ // 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();
+ }
+
+ rtr += "\")";
+ 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 simplifyLattices() {
while (!toAnalyzeMethodIsEmpty()) {
MethodDescriptor md = toAnalyzeMethodNext();
- if (ssjava.needTobeAnnotated(md)) {
- SSJavaLattice<String> methodLattice = md2lattice.get(md);
- if (methodLattice != null) {
- methodLattice.removeRedundantEdges();
- }
+ SSJavaLattice<String> methodLattice = md2lattice.get(md);
+ if (methodLattice != null) {
+ methodLattice.removeRedundantEdges();
}
}
}
while (!toAnalyzeMethodIsEmpty()) {
MethodDescriptor md = toAnalyzeMethodNext();
- if (ssjava.needTobeAnnotated(md)) {
- SSJavaLattice<String> methodLattice = md2lattice.get(md);
- if (methodLattice != null) {
- ssjava.writeLatticeDotFile(cd, md, methodLattice);
- debug_printDescriptorToLocNameMapping(md);
- }
+ SSJavaLattice<String> methodLattice = md2lattice.get(md);
+ if (methodLattice != null) {
+ ssjava.writeLatticeDotFile(cd, md, methodLattice);
+ debug_printDescriptorToLocNameMapping(md);
}
}
}
}
}
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ FlowNode flowNode = (FlowNode) iterator.next();
+ if (flowNode.isDeclaratonNode()) {
+ CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
+ String locIdentifier = inferLoc.get(0).getLocIdentifier();
+ if (!methodLattice.containsKey(locIdentifier)) {
+ methodLattice.put(locIdentifier);
+ }
+
+ }
+ }
+
// create mapping from param idx to inferred composite location
int offset;
private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
NTuple<Location> tuple) {
- // System.out.println("@@@@@generateInferredCompositeLocation=" + tuple);
- // System.out.println("generateInferredCompositeLocation=" + tuple + " 0="
- // + tuple.get(0).getLocDescriptor());
// first, retrieve inferred location by the local var descriptor
CompositeLocation inferLoc = new CompositeLocation();
inferLoc.addLocation(inferLocElement);
}
- // System.out.println("@@@@@inferLoc=" + inferLoc);
+
+ assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
return inferLoc;
}
Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
- Set<FlowNode> localInNodeSet = new HashSet<FlowNode>();
- Set<FlowNode> localOutNodeSet = new HashSet<FlowNode>();
-
- CompositeLocation flowNodeInferLoc =
- generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(flowNode));
-
List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
- if (inNodeTuple.size() > 1) {
- for (int i = 1; i < inNodeInferredLocTuple.size(); i++) {
- NTuple<Location> prefix = inNodeInferredLocTuple.subList(0, i);
- if (!prefixList.contains(prefix)) {
- prefixList.add(prefix);
- }
- addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inNodeInferredLocTuple);
+ for (int i = 1; i < inNodeInferredLocTuple.size(); i++) {
+ NTuple<Location> prefix = inNodeInferredLocTuple.subList(0, i);
+ if (!prefixList.contains(prefix)) {
+ prefixList.add(prefix);
}
- } else {
- localInNodeSet.add(inNode);
+ addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inNodeInferredLocTuple);
}
}
}
});
- for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
- FlowNode reachableNode = (FlowNode) iterator2.next();
- if (reachableNode.getDescTuple().size() == 1) {
- localOutNodeSet.add(reachableNode);
- }
- }
-
// find out reachable nodes that have the longest common prefix
for (int i = 0; i < prefixList.size(); i++) {
NTuple<Location> curPrefix = prefixList.get(i);
}
}
- // check if the lattice has the relation in which higher prefix is
- // actually lower than the current node
- CompositeLocation prefixInferLoc = generateInferredCompositeLocation(methodInfo, curPrefix);
- if (isGreaterThan(methodLattice, flowNodeInferLoc, prefixInferLoc)) {
- reachableCommonPrefixSet.add(curPrefix);
- }
-
if (!reachableCommonPrefixSet.isEmpty()) {
// found reachable nodes that start with the prefix curPrefix
// need to assign a composite location
// methodInfo.getInferLocation(localVarDesc);
CompositeLocation newInferLocation = new CompositeLocation();
- System.out.println("PREV INFER LOCATION=" + inferLocation + " curPrefix="
- + curPrefix);
if (inferLocation.getTuple().startsWith(curPrefix)) {
// the same infer location is already existed. no need to do
// anything
Location newLocationElement = new Location(desc, newLocSymbol);
newInferLocation.addLocation(newLocationElement);
- // if (flowNode.getDescTuple().size() == 1) {
// maps local variable to location types of the common prefix
methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation.clone());
- // }
// methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation);
addMapLocSymbolToInferredLocation(methodInfo.getMethodDesc(), localVarDesc,
methodInfo.removeMaplocalVarToLocSet(localVarDesc);
// add the field/var descriptor to the set of the location symbol
- int flowNodeTupleSize = flowNode.getDescTuple().size();
- Descriptor lastFlowNodeDesc = flowNode.getDescTuple().get(flowNodeTupleSize - 1);
- int inferLocSize = newInferLocation.getSize();
- Location lastLoc = newInferLocation.get(inferLocSize - 1);
- Descriptor enclosingDesc = lastLoc.getDescriptor();
- getLocationInfo(enclosingDesc).addMapLocSymbolToDescSet(lastLoc.getLocIdentifier(),
+ int lastIdx = flowNode.getDescTuple().size() - 1;
+ Descriptor lastFlowNodeDesc = flowNode.getDescTuple().get(lastIdx);
+ Descriptor enclosinglastLastFlowNodeDesc = flowNodelocTuple.get(lastIdx).getDescriptor();
+
+ CompositeLocation newlyInferredLocForFlowNode =
+ generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
+ Location lastInferLocElement =
+ newlyInferredLocForFlowNode.get(newlyInferredLocForFlowNode.getSize() - 1);
+ Descriptor enclosingLastInferLocElement = lastInferLocElement.getDescriptor();
+
+ // getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToDescSet(
+ // lastInferLocElement.getLocIdentifier(), lastFlowNodeDesc);
+ getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToRelatedInferLoc(
+ lastInferLocElement.getLocIdentifier(), enclosinglastLastFlowNodeDesc,
lastFlowNodeDesc);
// clean up the previous location
System.out.println("-- add in-flow");
for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
NTuple<Location> tuple = (NTuple<Location>) iterator.next();
- System.out.println("--in-flow tuple=" + tuple);
Location loc = tuple.get(idx);
- String higher = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
+ String higher = loc.getLocIdentifier();
addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
}
- System.out.println("-- add local in-flow");
- for (Iterator iterator = localInNodeSet.iterator(); iterator.hasNext();) {
- FlowNode localNode = (FlowNode) iterator.next();
-
- if (localNode.equals(flowNode)) {
- continue;
- }
-
- CompositeLocation inNodeInferLoc =
- generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(localNode));
-
- if (isCompositeLocation(inNodeInferLoc)) {
- // need to make sure that newLocSymbol is lower than the infernode
- // location in the field lattice
- System.out.println("----srcNode=" + localNode + " dstNode=" + flowNode);
- addRelationToLattice(methodInfo.getMethodDesc(), methodLattice, methodInfo, localNode,
- flowNode);
-
- }
-
- }
-
System.out.println("-- add out flow");
for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
NTuple<Location> tuple = (NTuple<Location>) iterator.next();
if (tuple.size() > idx) {
Location loc = tuple.get(idx);
- String lower = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
+ String lower = loc.getLocIdentifier();
+ // String lower =
+ // locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
}
}
- System.out.println("-- add local out flow");
- for (Iterator iterator = localOutNodeSet.iterator(); iterator.hasNext();) {
- FlowNode localOutNode = (FlowNode) iterator.next();
-
- if (localOutNode.equals(flowNode)) {
- continue;
- }
-
- CompositeLocation outNodeInferLoc =
- generateInferredCompositeLocation(methodInfo,
- flowGraph.getLocationTuple(localOutNode));
-
- if (isCompositeLocation(outNodeInferLoc)) {
- System.out.println("--- srcNode=" + flowNode + " dstNode=" + localOutNode);
- addRelationToLattice(methodInfo.getMethodDesc(), methodLattice, methodInfo, flowNode,
- localOutNode);
-
- }
- }
- System.out.println("-- end of add local out flow");
-
return true;
}
DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
VarDescriptor vd = dn.getVarDescriptor();
+ mapDescToDefinitionLine.put(vd, dn.getNumLine());
NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
tupleLHS.add(vd);
- getFlowGraph(md).createNewFlowNode(tupleLHS);
+ FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
+ fn.setDeclarationNode();
if (dn.getExpression() != null) {