From b06ce17baa2969454e710ed4e5135554ccb220d2 Mon Sep 17 00:00:00 2001 From: yeom Date: Mon, 16 Apr 2012 19:17:09 +0000 Subject: [PATCH] changes. --- Robust/src/Analysis/SSJava/FlowGraph.java | 3 +- .../Analysis/SSJava/LocationInference.java | 133 ++++++++++++++---- Robust/src/Analysis/SSJava/NTuple.java | 8 ++ Robust/src/Analysis/SSJava/NodeTupleSet.java | 42 ++++++ 4 files changed, 154 insertions(+), 32 deletions(-) create mode 100644 Robust/src/Analysis/SSJava/NodeTupleSet.java diff --git a/Robust/src/Analysis/SSJava/FlowGraph.java b/Robust/src/Analysis/SSJava/FlowGraph.java index f9790963..a35a93cb 100644 --- a/Robust/src/Analysis/SSJava/FlowGraph.java +++ b/Robust/src/Analysis/SSJava/FlowGraph.java @@ -92,5 +92,6 @@ public class FlowGraph { System.out.println("Creating new node=" + node); } + -} +} \ No newline at end of file diff --git a/Robust/src/Analysis/SSJava/LocationInference.java b/Robust/src/Analysis/SSJava/LocationInference.java index 275b5293..c99c6bb7 100644 --- a/Robust/src/Analysis/SSJava/LocationInference.java +++ b/Robust/src/Analysis/SSJava/LocationInference.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -17,6 +18,7 @@ import IR.NameDescriptor; import IR.Operation; import IR.State; import IR.SymbolTable; +import IR.TypeDescriptor; import IR.VarDescriptor; import IR.Tree.ArrayAccessNode; import IR.Tree.AssignmentNode; @@ -147,7 +149,7 @@ public class LocationInference { break; case Kind.DeclarationNode: - analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, new NTuple()); + analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn); break; case Kind.IfStatementNode: @@ -204,37 +206,42 @@ public class LocationInference { } - private NTuple analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable, - DeclarationNode dn, NTuple base) { + private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable, + DeclarationNode dn) { VarDescriptor vd = dn.getVarDescriptor(); - base.add(vd); - getFlowGraph(md).createNewFlowNode(base); + NTuple tupleLHS = new NTuple(); + tupleLHS.add(vd); + getFlowGraph(md).createNewFlowNode(tupleLHS); if (dn.getExpression() != null) { - NTuple rhsDescTuple = - analyzeFlowExpressionNode(md, nametable, dn.getExpression(), new NTuple()); + NodeTupleSet tupleSetRHS = new NodeTupleSet(); + analyzeFlowExpressionNode(md, nametable, dn.getExpression(), tupleSetRHS, + new NTuple()); // add a new flow edge from rhs to lhs - if (rhsDescTuple != null) { // rhs is null when values come from the top - // location - getFlowGraph(md).addValueFlowEdge(rhsDescTuple, base); + for (Iterator> iter = tupleSetRHS.iterator(); iter.hasNext();) { + NTuple from = iter.next(); + addFlowGraphEdge(md, from, tupleLHS); } } - return null; - } private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable, BlockExpressionNode ben) { - analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null); + analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null); } private NTuple analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable, - ExpressionNode en, NTuple base) { + ExpressionNode en, NodeTupleSet nodeSet, NTuple base) { + + // note that expression node can create more than one flow node + // nodeSet contains of flow nodes + + NTuple flowTuple; switch (en.kind()) { @@ -243,11 +250,15 @@ public class LocationInference { break; case Kind.FieldAccessNode: - analyzeFieldAccessNode(md, nametable, (FieldAccessNode) en); - break; + flowTuple = analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base); + nodeSet.addTuple(flowTuple); + return flowTuple; case Kind.NameNode: - return analyzeFlowNameNode(md, nametable, (NameNode) en, base); + NodeTupleSet nameNodeSet = new NodeTupleSet(); + flowTuple = analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base); + nodeSet.addTuple(flowTuple); + return flowTuple; case Kind.OpNode: // return analyzeOpNode(md, nametable, (OpNode) en, new @@ -338,13 +349,15 @@ public class LocationInference { ClassDescriptor cd = md.getClassDesc(); // left operand - NTuple leftOpTuple = - analyzeFlowExpressionNode(md, nametable, on.getLeft(), new NTuple()); + // NTuple leftOpTuple = + // analyzeFlowExpressionNode(md, nametable, on.getLeft(), new + // NTuple()); if (on.getRight() != null) { // right operand - NTuple rightOpTuple = - analyzeFlowExpressionNode(md, nametable, on.getRight(), new NTuple()); + // NTuple rightOpTuple = + // analyzeFlowExpressionNode(md, nametable, on.getRight(), new + // NTuple()); } Operation op = on.getOp(); @@ -392,11 +405,15 @@ public class LocationInference { } private NTuple analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable, - NameNode nn, NTuple base) { + NameNode nn, NodeTupleSet nodeSet, NTuple base) { + + if (base == null) { + base = new NTuple(); + } NameDescriptor nd = nn.getName(); if (nd.getBase() != null) { - analyzeFlowExpressionNode(md, nametable, nn.getExpression(), base); + analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base); } else { String varname = nd.toString(); if (varname.equals("this")) { @@ -443,12 +460,10 @@ public class LocationInference { base.add(md.getThis()); } - // Location fieldLoc = (Location) fd.getType().getExtension(); - // loc.addLocation(fieldLoc); base.add(fd); } else if (d == null) { // access static field - // FieldDescriptor fd = nn.getField(); + // FieldDescriptor fd = nn.getField();addFlowGraphEdge // // MethodLattice localLattice = ssjava.getMethodLattice(md); // String globalLocId = localLattice.getGlobalLoc(); @@ -473,8 +488,46 @@ public class LocationInference { } - private void analyzeFieldAccessNode(MethodDescriptor md, SymbolTable nametable, FieldAccessNode en) { - // TODO Auto-generated method stub + private NTuple analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable, + FieldAccessNode fan, NodeTupleSet nodeSet, NTuple base) { + + ExpressionNode left = fan.getExpression(); + TypeDescriptor ltd = left.getType(); + FieldDescriptor fd = fan.getField(); + + String varName = null; + if (left.kind() == Kind.NameNode) { + NameDescriptor nd = ((NameNode) left).getName(); + varName = nd.toString(); + } + + if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) { + // using a class name directly or access using this + if (fd.isStatic() && fd.isFinal()) { + // loc.addLocation(Location.createTopLocation(md)); + // return loc; + } + } + + // if (left instanceof ArrayAccessNode) { + // ArrayAccessNode aan = (ArrayAccessNode) left; + // left = aan.getExpression(); + // } + // fanNodeSet + base = analyzeFlowExpressionNode(md, nametable, left, nodeSet, base); + + if (!left.getType().isPrimitive()) { + + if (fd.getSymbol().equals("length")) { + // TODO + // array.length access, return the location of the array + // return loc; + } + + base.add(fd); + } + + return base; } @@ -485,6 +538,9 @@ public class LocationInference { ClassDescriptor cd = md.getClassDesc(); + NodeTupleSet nodeSetRHS = new NodeTupleSet(); + NodeTupleSet nodeSetLHS = new NodeTupleSet(); + boolean postinc = true; if (an.getOperation().getBaseOp() == null || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation() @@ -494,14 +550,15 @@ public class LocationInference { // if LHS is array access node, need to capture value flows between an array // and its index value - analyzeFlowExpressionNode(md, nametable, an.getDest(), base); + analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, base); + System.out.println("ASSIGNMENT NODE nodeSetLHS=" + nodeSetLHS); // NTuple lhsDescTuple = analyzeFlowExpressionNode(md, // nametable, an.getDest(), base); if (!postinc) { // analyze value flows of rhs expression - NTuple rhsDescTuple = - analyzeFlowExpressionNode(md, nametable, an.getSrc(), new NTuple()); + analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null); + System.out.println("ASSIGNMENT NODE nodeSetRHS=" + nodeSetRHS); } else { @@ -510,10 +567,24 @@ public class LocationInference { } + // creates edges from RHS to LHS + for (Iterator> iter = nodeSetRHS.iterator(); iter.hasNext();) { + NTuple fromTuple = iter.next(); + for (Iterator> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) { + NTuple toTuple = iter2.next(); + addFlowGraphEdge(md, fromTuple, toTuple); + } + } + } public FlowGraph getFlowGraph(MethodDescriptor md) { return mapMethodDescriptorToFlowGraph.get(md); } + public void addFlowGraphEdge(MethodDescriptor md, NTuple from, NTuple to) { + FlowGraph graph = getFlowGraph(md); + graph.addValueFlowEdge(from, to); + } + } diff --git a/Robust/src/Analysis/SSJava/NTuple.java b/Robust/src/Analysis/SSJava/NTuple.java index a968908c..289b46b9 100644 --- a/Robust/src/Analysis/SSJava/NTuple.java +++ b/Robust/src/Analysis/SSJava/NTuple.java @@ -13,6 +13,14 @@ public class NTuple { this.elements.addAll(l); } + public NTuple(NTuple tuple) { + this(tuple.toList()); + } + + public List toList() { + return elements; + } + public NTuple() { this.elements = new ArrayList(); } diff --git a/Robust/src/Analysis/SSJava/NodeTupleSet.java b/Robust/src/Analysis/SSJava/NodeTupleSet.java new file mode 100644 index 00000000..d793424c --- /dev/null +++ b/Robust/src/Analysis/SSJava/NodeTupleSet.java @@ -0,0 +1,42 @@ +package Analysis.SSJava; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import IR.Descriptor; + +public class NodeTupleSet { + + Set> set; + + public NodeTupleSet() { + set = new HashSet>(); + } + + public void addTuple(NTuple tuple) { + + // need to add additional elements because we need to create edges even from + // the base + // for example, if we have input , we need to add additional element + // and to the set + + NTuple cur = new NTuple(); + for (int i = 0; i < tuple.size(); i++) { + Descriptor d = tuple.get(i); + cur.add(d); + set.add(new NTuple(cur)); + } + + set.add(tuple); + } + + public Iterator> iterator() { + return set.iterator(); + } + + public String toString() { + return set.toString(); + } + +} -- 2.34.1