From: yeom Date: Fri, 29 Apr 2011 23:42:00 +0000 (+0000) Subject: changes: now, the annotated SSJava class library passes the flow-down rule checking. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=28ba27af212c85e959199cd7bf50a77e3634f178;p=IRC.git changes: now, the annotated SSJava class library passes the flow-down rule checking. --- diff --git a/Robust/src/Analysis/SSJava/CompositeLocation.java b/Robust/src/Analysis/SSJava/CompositeLocation.java index 15af1f82..1adb4d4c 100644 --- a/Robust/src/Analysis/SSJava/CompositeLocation.java +++ b/Robust/src/Analysis/SSJava/CompositeLocation.java @@ -30,14 +30,13 @@ public class CompositeLocation extends Location { if (loc instanceof DeltaLocation) { type = Location.DELTA; } - locTuple.addElement(loc); } public void addLocationSet(Set set) { - for (Iterator iterator = set.iterator(); iterator.hasNext(); ) { + for (Iterator iterator = set.iterator(); iterator.hasNext();) { Location location = (Location) iterator.next(); locTuple.addElement(location); } @@ -48,7 +47,7 @@ public class CompositeLocation extends Location { // need to get more optimization version later Set locSet = getBaseLocationSet(); - for (Iterator iterator = locSet.iterator(); iterator.hasNext(); ) { + for (Iterator iterator = locSet.iterator(); iterator.hasNext();) { Location location = (Location) iterator.next(); if (location.getClassDescriptor().equals(cd)) { return location; @@ -64,7 +63,7 @@ public class CompositeLocation extends Location { Map cd2loc = new Hashtable(); Set baseLocSet = getBaseLocationSet(); - for (Iterator iterator = baseLocSet.iterator(); iterator.hasNext(); ) { + for (Iterator iterator = baseLocSet.iterator(); iterator.hasNext();) { Location location = (Location) iterator.next(); cd2loc.put(location.getClassDescriptor(), location); } @@ -138,6 +137,15 @@ public class CompositeLocation extends Location { return result; } + public void removieLocation(ClassDescriptor cd) { + for (int i = 0; i < locTuple.size(); i++) { + if (locTuple.at(i).getClassDescriptor().equals(cd)) { + locTuple.removeAt(i); + return; + } + } + } + public String toString() { // for better representation diff --git a/Robust/src/Analysis/SSJava/FlowDownCheck.java b/Robust/src/Analysis/SSJava/FlowDownCheck.java index e4472229..3828e885 100644 --- a/Robust/src/Analysis/SSJava/FlowDownCheck.java +++ b/Robust/src/Analysis/SSJava/FlowDownCheck.java @@ -24,6 +24,7 @@ import IR.Tree.AssignmentNode; import IR.Tree.BlockExpressionNode; import IR.Tree.BlockNode; import IR.Tree.BlockStatementNode; +import IR.Tree.CastNode; import IR.Tree.CreateObjectNode; import IR.Tree.DeclarationNode; import IR.Tree.ExpressionNode; @@ -35,6 +36,7 @@ import IR.Tree.LoopNode; import IR.Tree.MethodInvokeNode; import IR.Tree.NameNode; import IR.Tree.OpNode; +import IR.Tree.ReturnNode; import IR.Tree.SubBlockNode; import IR.Tree.TertiaryNode; import IR.Tree.TreeNode; @@ -86,7 +88,7 @@ public class FlowDownCheck { ClassDescriptor cd = (ClassDescriptor) obj; toanalyze.remove(cd); - if(!cd.isInterface()){ + if (!cd.isInterface()) { checkDeclarationInClass(cd); for (Iterator method_it = cd.getMethods(); method_it.hasNext();) { MethodDescriptor md = (MethodDescriptor) method_it.next(); @@ -141,7 +143,7 @@ public class FlowDownCheck { Object obj = toanalyze.iterator().next(); ClassDescriptor cd = (ClassDescriptor) obj; toanalyze.remove(cd); - + checkClass(cd); for (Iterator method_it = cd.getMethods(); method_it.hasNext();) { MethodDescriptor md = (MethodDescriptor) method_it.next(); @@ -219,6 +221,8 @@ public class FlowDownCheck { private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) { + + bn.getVarTable().setParent(nametable); // it will return the lowest location in the block node CompositeLocation lowestLoc = null; for (int i = 0; i < bn.size(); i++) { @@ -258,8 +262,8 @@ public class FlowDownCheck { break; case Kind.ReturnNode: - // checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn); - return null; + compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn); + break; case Kind.SubBlockNode: compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn); @@ -273,6 +277,23 @@ public class FlowDownCheck { return compLoc; } + private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable, + ReturnNode rn) { + ClassDescriptor cd = md.getClassDesc(); + CompositeLocation loc = new CompositeLocation(cd); + // TODO: by default, return node has "bottom" location no matter what it is + // going to return + // but definitely I need to consider it more!!! + loc.addLocation(Location.createBottomLocation(cd)); + /* + * ExpressionNode returnExp = rn.getReturnExpression(); if + * (rn.getReturnExpression() != null) { loc = + * checkLocationFromExpressionNode(md, nametable, returnExp, loc); } + */ + // System.out.println(rn.printNode(0) + " has loc=" + loc); + return loc; + } + private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) { @@ -298,6 +319,7 @@ public class FlowDownCheck { } else { // check for loop case BlockNode bn = ln.getInitializer(); + bn.getVarTable().setParent(nametable); // calculate glb location of condition and update statements CompositeLocation condLoc = @@ -313,6 +335,8 @@ public class FlowDownCheck { glbInputSet.add(updateLoc); CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(cd, glbInputSet, cd); + + // check location of 'forloop' body CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody()); if (blockLoc == null) { @@ -398,20 +422,29 @@ public class FlowDownCheck { // checking location order if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc, localCD)) { throw new Error("The value flow from " + expressionLoc + " to " + destLoc - + " does not respect location hierarchy on the assignment " + dn.printNode(0)); + + " does not respect location hierarchy on the assignment " + dn.printNode(0) + + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine()); } } return expressionLoc; } else { - return null; + + if (destLoc instanceof Location) { + CompositeLocation comp = new CompositeLocation(localCD); + comp.addLocation(destLoc); + return comp; + } else { + return (CompositeLocation) destLoc; + } + } } private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable, SubBlockNode sbn) { - checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode()); + checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode()); } private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md, @@ -464,6 +497,10 @@ public class FlowDownCheck { compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en); break; + case Kind.CastNode: + compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en); + break; + // case Kind.InstanceOfNode: // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td); // return null; @@ -491,6 +528,15 @@ public class FlowDownCheck { } + private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable, + CastNode cn) { + + ExpressionNode en = cn.getExpression(); + return checkLocationFromExpressionNode(md, nametable, en, + new CompositeLocation(md.getClassDesc())); + + } + private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn) { ClassDescriptor cd = md.getClassDesc(); @@ -614,7 +660,6 @@ public class FlowDownCheck { cd)); addTypeLocation(aan.getExpression().getType(), arrayLoc); glbInputSet.add(arrayLoc); - CompositeLocation indexLoc = checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(cd)); glbInputSet.add(indexLoc); @@ -669,9 +714,11 @@ public class FlowDownCheck { addTypeLocation(on.getRight().getType(), rightLoc); } - System.out.println("checking op node=" + on.printNode(0)); - System.out.println("left loc=" + leftLoc + " from " + on.getLeft().getClass()); - System.out.println("right loc=" + rightLoc); + // System.out.println("checking op node=" + on.printNode(0)); + // System.out.println("left loc=" + leftLoc + " from " + + // on.getLeft().getClass()); + // System.out.println("right loc=" + rightLoc + " from " + + // on.getLeft().getClass()); Operation op = on.getOp(); @@ -681,8 +728,7 @@ public class FlowDownCheck { case Operation.UNARYMINUS: case Operation.LOGIC_NOT: // single operand - on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN)); - break; + return leftLoc; case Operation.LOGIC_OR: case Operation.LOGIC_AND: @@ -716,8 +762,6 @@ public class FlowDownCheck { throw new Error(op.toString()); } - return null; - } private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md, @@ -741,6 +785,11 @@ public class FlowDownCheck { } else { String varname = nd.toString(); + if (varname.equals("this")) { + // 'this' itself is top location in the local hierarchy + loc.addLocation(Location.createTopLocation(md.getClassDesc())); + return loc; + } Descriptor d = (Descriptor) nametable.get(varname); Location localLoc = null; @@ -766,14 +815,23 @@ public class FlowDownCheck { private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md, SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) { - FieldDescriptor fd = fan.getField(); - Location fieldLoc = td2loc.get(fd); - loc.addLocation(fieldLoc); - ExpressionNode left = fan.getExpression(); - CompositeLocation compLoc = checkLocationFromExpressionNode(md, nametable, left, loc); - addTypeLocation(left.getType(), compLoc); - return compLoc; + loc = checkLocationFromExpressionNode(md, nametable, left, loc); + addTypeLocation(left.getType(), loc); + + if (!left.getType().isPrimitive()) { + FieldDescriptor fd = fan.getField(); + Location fieldLoc = td2loc.get(fd); + + // in the case of "this.field", need to get rid of 'this' location from + // the composite location + if (loc.getCd2Loc().containsKey(md.getClassDesc())) { + loc.removieLocation(md.getClassDesc()); + } + loc.addLocation(fieldLoc); + } + + return loc; } private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md, @@ -793,10 +851,13 @@ public class FlowDownCheck { if (!postinc) { srcLocation = new CompositeLocation(cd); srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation); - + // System.out.println("an=" + an.printNode(0) + " an.getSrc()=" + + // an.getSrc().getClass() + // + " at " + cd.getSourceFileName() + "::" + an.getNumLine()); if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, cd)) { throw new Error("The value flow from " + srcLocation + " to " + destLocation - + " does not respect location hierarchy on the assignment " + an.printNode(0)); + + " does not respect location hierarchy on the assignment " + an.printNode(0) + "at " + + cd.getSourceFileName() + "::" + an.getNumLine()); } } else { destLocation = @@ -849,7 +910,7 @@ public class FlowDownCheck { Location loc = new Location(cd, locationID); td2loc.put(vd, loc); addTypeLocation(vd.getType(), loc); - + } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) { CompositeLocation compLoc = new CompositeLocation(cd); @@ -937,7 +998,7 @@ public class FlowDownCheck { Vector annotationVec = fd.getType().getAnnotationMarkers(); - // currently enforce every variable to have corresponding location + // currently enforce every field to have corresponding location if (annotationVec.size() == 0) { throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class " + cd.getSymbol()); @@ -949,23 +1010,23 @@ public class FlowDownCheck { + " has more than one location."); } - // check if location is defined AnnotationDescriptor ad = annotationVec.elementAt(0); - if (ad.getType() == AnnotationDescriptor.MARKER_ANNOTATION) { - String locationID = annotationVec.elementAt(0).getMarker(); - Lattice lattice = (Lattice) state.getCd2LocationOrder().get(cd); - if (lattice == null || (!lattice.containsKey(locationID))) { - throw new Error("Location " + locationID - + " is not defined in the location hierarchy of class " + cd.getSymbol() + "."); - } + if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) { - Location localLoc = new Location(cd, locationID); - td2loc.put(fd, localLoc); - addTypeLocation(fd.getType(), localLoc); + if (ad.getMarker().equals(SSJavaAnalysis.LOC)) { + String locationID = ad.getValue(); + // check if location is defined + Lattice lattice = (Lattice) state.getCd2LocationOrder().get(cd); + if (lattice == null || (!lattice.containsKey(locationID))) { + throw new Error("Location " + locationID + + " is not defined in the location hierarchy of class " + cd.getSymbol() + "."); + } + Location loc = new Location(cd, locationID); + td2loc.put(fd, loc); + addTypeLocation(fd.getType(), loc); - } else if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) { - if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) { + } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) { if (ad.getValue().length() == 0) { throw new Error("Delta function of " + fd.getSymbol() + " does not have any locations: " @@ -995,6 +1056,7 @@ public class FlowDownCheck { } } + } private void addTypeLocation(TypeDescriptor type, Location loc) { @@ -1007,7 +1069,7 @@ public class FlowDownCheck { public static boolean isGreaterThan(Location loc1, Location loc2, ClassDescriptor priorityCD) { - // System.out.println("isGreaterThan=" + loc1 + " ? " + loc2); + // System.out.println("\nisGreaterThan=" + loc1 + " ? " + loc2); CompositeLocation compLoc1; CompositeLocation compLoc2; diff --git a/Robust/src/Analysis/SSJava/NTuple.java b/Robust/src/Analysis/SSJava/NTuple.java index c6a1168d..d001e6fb 100644 --- a/Robust/src/Analysis/SSJava/NTuple.java +++ b/Robust/src/Analysis/SSJava/NTuple.java @@ -57,5 +57,9 @@ public class NTuple { public int hashCode() { return elements.hashCode(); } + + public void removeAt(int i){ + elements.remove(i); + } } diff --git a/Robust/src/ClassLibrary/SSJava/Object.java b/Robust/src/ClassLibrary/SSJava/Object.java index df78349d..7df28891 100644 --- a/Robust/src/ClassLibrary/SSJava/Object.java +++ b/Robust/src/ClassLibrary/SSJava/Object.java @@ -1,7 +1,7 @@ public class Object { locdef{ - data + in } public native int hashCode(); @@ -14,7 +14,7 @@ public class Object { return "Object"+hashCode(); } - public boolean equals(@LOC("data") Object o) { + public boolean equals(@LOC("in") Object o) { if (o==this) return true; return false; diff --git a/Robust/src/ClassLibrary/SSJava/String.java b/Robust/src/ClassLibrary/SSJava/String.java index 24a87cf0..43a71a2f 100644 --- a/Robust/src/ClassLibrary/SSJava/String.java +++ b/Robust/src/ClassLibrary/SSJava/String.java @@ -1,76 +1,97 @@ public class String { - char value[]; - int count; - int offset; - private int cachedHashcode; + + locdef{ + data(str.length-offset)) length=str.length-offset; - char charstr[]=new char[length]; - for(int i=0; i(str.length)) length=str.length; - char charstr[]=new char[length]; - for(int i=0; i(str.length-offset)) length=str.length-offset; - char charstr[]=new char[length]; - for(int i=0; ithis.count||endIndex>this.count||beginIndex>endIndex) { // FIXME @@ -104,53 +127,38 @@ public class String { return str; } - public String subString(int beginIndex) { + public String subString(@LOC("in") int beginIndex) { return this.subString(beginIndex, this.count); } - public int lastindexOf(int ch) { + public int lastindexOf(@LOC("in") int ch) { return this.lastindexOf(ch, count - 1); } - public int lastIndexOf(char ch) { + public int lastIndexOf(@LOC("in") char ch) { return this.lastindexOf((int)ch, count - 1); } - - public static String concat2(String s1, String s2) { + + public static String concat2(@LOC("in") String s1, @LOC("in") String s2) { if (s1==null) return "null".concat(s2); else return s1.concat(s2); } - public String concat(String str) { - String newstr=new String(); - newstr.count=this.count+str.count; - char charstr[]=new char[newstr.count]; - newstr.value=charstr; - newstr.offset=0; - for(int i=0; i0; i--) if (this.charAt(i)==ch) - return i; + return i; return -1; } - public String replace(char oldch, char newch) { + public String replace(@LOC("in") char oldch, @LOC("in") char newch) { char[] buffer=new char[count]; for(int i=0; i='a'&&x<='z') { - x=(char) ((x-'a')+'A'); + x=(char) ((x-'a')+'A'); } buffer[i]=x; } @@ -173,78 +181,78 @@ public class String { for(int i=0; i='A'&&x<='Z') { - x=(char) ((x-'A')+'a'); + x=(char) ((x-'A')+'a'); } buffer[i]=x; } return new String(buffer); } - public int indexOf(int ch) { + public int indexOf(@LOC("in") int ch) { return this.indexOf(ch, 0); } - public int indexOf(int ch, int fromIndex) { + public int indexOf(@LOC("in") int ch, @LOC("in") int fromIndex) { for(int i=fromIndex; ifromIndex) k=fromIndex; for(; k>=0; k--) { if (regionMatches(k, str, 0, str.count)) - return k; + return k; } return -1; } - public int lastIndexOf(String str) { + public int lastIndexOf(@LOC("in") String str) { return lastIndexOf(str, count-str.count); } - public boolean startsWith(String str) { + public boolean startsWith(@LOC("in") String str) { return regionMatches(0, str, 0, str.count); } - public boolean startsWith(String str, int toffset) { + public boolean startsWith(@LOC("in") String str, @LOC("in") int toffset) { return regionMatches(toffset, str, 0, str.count); } - public boolean regionMatches(int toffset, String other, int ooffset, int len) { + public boolean regionMatches(@LOC("in") int toffset, @LOC("in") String other, @LOC("in") int ooffset, @LOC("in") int len) { if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count) return false; for(int i=0; i count) || (srcBegin > srcEnd)) { // FIXME System.printString("Index error: "+srcBegin+" "+srcEnd+" "+count+"\n"+this); @@ -272,42 +280,29 @@ public class String { dst[j++]=value[i+offset]; return; } - +*/ public int length() { return count; } - - public char charAt(int i) { +/* + public char charAt(@LOC("in") int i) { return value[i+offset]; } - +*/ public String toString() { return this; } - public static String valueOf(Object o) { + public static String valueOf(@LOC("in") Object o) { if (o==null) return "null"; else return o.toString(); } - - public static String valueOf(boolean b) { - if (b) - return new String("true"); - else - return new String("false"); - } - - public static String valueOf(char c) { - char ar[]=new char[1]; - ar[0]=c; - return new String(ar); - } - - public static String valueOf(int x) { - int length=0; - int tmp; + + public static String valueOf(@LOC("in") int x) { + @LOC("in") int length=0; + @LOC("in") int tmp; if (x<0) tmp=-x; else @@ -317,12 +312,12 @@ public class String { length=length+1; } while(tmp!=0); - char chararray[]; + @LOC("in") char chararray[]; if (x<0) chararray=new char[length+1]; else chararray=new char[length]; - int voffset; + @LOC("in") int voffset; if (x<0) { chararray[0]='-'; voffset=1; @@ -336,8 +331,24 @@ public class String { } while (length!=0); return new String(chararray); } + +/* + public static String valueOf(@LOC("in") boolean b) { + if (b) + return new String("true"); + else + return new String("false"); + } + + public static String valueOf(@LOC("in") char c) { + @LOC("data") char ar[]=new char[1]; + ar[0]=c; + return new String(ar); + } - public static String valueOf(double val) { + + + public static String valueOf(@LOC("in") double val) { char[] chararray=new char[20]; String s=new String(); s.offset=0; @@ -345,10 +356,10 @@ public class String { s.value=chararray; return s; } - + public static native int convertdoubletochar(double val, char [] chararray); - public static String valueOf(long x) { + public static String valueOf(@LOC("in") long x) { int length=0; long tmp; if (x<0) @@ -380,57 +391,57 @@ public class String { return new String(chararray); } - public int compareTo(String s) { + public int compareTo(@LOC("in") String s) { int smallerlength=count='a'&&l<='z') - l=(char)((l-'a')+'A'); + l=(char)((l-'a')+'A'); if (r>='a'&&r<='z') - r=(char)((r-'a')+'A'); + r=(char)((r-'a')+'A'); if (l!=r) - return false; + return false; } return true; } -/* + public Vector split() { Vector splitted = new Vector(); int i; @@ -469,11 +480,11 @@ public class String { t.count=i-oldi; splitted.addElement(t); } - + return splitted; } -*/ - public boolean contains(String str) + + public boolean contains(@LOC("in") String str) { int i,j; char[] strChar = str.toCharArray(); @@ -494,12 +505,12 @@ public class String { return false; } - + public String trim() { int len = count; int st = 0; - int off = offset; /* avoid getfield opcode */ - char[] val = value; /* avoid getfield opcode */ + int off = offset; //avoid getfield opcode + char[] val = value; // avoid getfield opcode while ((st < len) && (val[off + st] <= ' ')) { st++; @@ -509,9 +520,10 @@ public class String { } return ((st > 0) || (len < count)) ? substring(st, len) : this; } - - public boolean matches(String regex) { + + public boolean matches(@LOC("in") String regex) { System.println("String.matches() is not fully supported"); return this.equals(regex); } + */ }