Thread: [Fb-contrib-commit] SF.net SVN: fb-contrib:[1367] trunk/fb-contrib/src/com/mebigfatguy/ fbcontrib/
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-10-15 03:33:59
|
Revision: 1367
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1367&view=rev
Author: dbrosius
Date: 2009-10-15 03:33:46 +0000 (Thu, 15 Oct 2009)
Log Message:
-----------
add new detector CVAA - by Bhaskar Maddala
Added Paths:
-----------
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2009-10-15 03:33:46 UTC (rev 1367)
@@ -0,0 +1,169 @@
+/*
+ * fb-contrib - Auxiliary detectors for Java programs
+ * Copyright (C) 2009 Bhaskar Maddala
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package com.mebigfatguy.fbcontrib.detect;
+
+import org.apache.bcel.classfile.Code;
+import org.apache.bcel.classfile.LocalVariable;
+import org.apache.bcel.classfile.LocalVariableTable;
+import org.apache.bcel.generic.ArrayType;
+import org.apache.bcel.generic.ObjectType;
+import org.apache.bcel.generic.Type;
+
+import com.mebigfatguy.fbcontrib.utils.RegisterUtils;
+
+import edu.umd.cs.findbugs.BugInstance;
+import edu.umd.cs.findbugs.BugReporter;
+import edu.umd.cs.findbugs.BytecodeScanningDetector;
+import edu.umd.cs.findbugs.OpcodeStack;
+
+/**
+ * Finds contravariant array assignments. Since arrays are mutable data structures, their use
+ * must be restricted to covariant or invariant usage
+ *
+ * <pre>
+ * class A {}
+ * class B extends A {}
+ *
+ * B[] b = new B[2];
+ * A[] a = b;
+ * a[0] = new A(); // results in ArrayStoreException (Runtime)
+ * </pre>
+ *
+ */
+public class ContraVariantArrayAssignment extends BytecodeScanningDetector {
+ private final BugReporter bugReporter;
+ private final OpcodeStack stack;
+
+ /**
+ * constructs a CVAA detector given the reporter to report bugs on.
+
+ * @param bugReporter the sync of bug reports
+ */
+ public ContraVariantArrayAssignment(final BugReporter bugReporter) {
+ this.bugReporter = bugReporter;
+ this.stack = new OpcodeStack();
+ }
+
+ /**
+ * implements the visitor to pass through constructors and static initializers to the
+ * byte code scanning code. These methods are not reported, but are used to build
+ * SourceLineAnnotations for fields, if accessed.
+ *
+ * @param obj the context object of the currently parsed code attribute
+ */
+ @Override
+ public void visitCode(Code obj) {
+ stack.resetForMethodEntry(this);
+ LocalVariableTable lvt = getMethod().getLocalVariableTable();
+ if(lvt != null) {
+ super.visitCode(obj);
+ }
+ }
+
+ @Override
+ public void sawOpcode(int seen) {
+ try{
+ switch(seen){
+ case ASTORE:
+ case ASTORE_0:
+ case ASTORE_1:
+ case ASTORE_2:
+ case ASTORE_3:
+ if(stack.getStackDepth() > 0){
+ OpcodeStack.Item item = stack.getStackItem(0);
+ String sourceSignature = item.getSignature();
+ LocalVariable lv = getMethod().getLocalVariableTable()
+ .getLocalVariable(RegisterUtils.getAStoreReg(this, seen), getNextPC());
+ if(lv != null){
+ String targetSignature = lv.getSignature();
+ checkSignatures(sourceSignature, targetSignature);
+ }
+ }
+ break;
+ case PUTFIELD:
+ case PUTSTATIC:
+ if(stack.getStackDepth() > 0){
+ OpcodeStack.Item item = stack.getStackItem(0);
+ String sourceSignature = item.getSignature();
+ String targetSignature = getSigConstantOperand();
+ checkSignatures(sourceSignature, targetSignature);
+ }
+ break;
+ case INVOKESTATIC:
+ case INVOKEVIRTUAL:
+ case INVOKEINTERFACE:
+ case INVOKESPECIAL:
+ if(stack.getStackDepth() > 0){
+ String signature = getSigConstantOperand();
+ checkMethodInvocation(signature);
+ }
+ break;
+ }
+ super.sawOpcode(seen);
+ }
+ finally{
+ stack.sawOpcode(this, seen);
+ }
+ }
+
+ private boolean isArrayType(String signature){
+ return Type.getType(signature) instanceof ArrayType;
+ }
+
+ private boolean isObjectType(String signature){
+ return ((ArrayType)Type.getType(signature)).getBasicType() instanceof ObjectType;
+ }
+
+ private void checkSignatures(String sourceSignature, String targetSignature) {
+ try{
+ if(isArrayType(sourceSignature)) {
+ if(!isArrayType(targetSignature)){
+ bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", HIGH_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ } else {
+ if(isObjectType(sourceSignature) && isObjectType(targetSignature)){
+ ObjectType sourceType = (ObjectType) ((ArrayType) Type.getType(sourceSignature)).getBasicType();
+ ObjectType targetType = (ObjectType) ((ArrayType) Type.getType(targetSignature)).getBasicType();
+ if(sourceType.subclassOf(targetType) && !targetType.subclassOf(sourceType)) {
+ bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", HIGH_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ }
+ }
+ }
+ }
+ }
+ catch(ClassNotFoundException cnfe) {
+ bugReporter.reportMissingClass(cnfe);
+ }
+ }
+
+ private void checkMethodInvocation(String signature) {
+ Type[] types = Type.getArgumentTypes(signature);
+ for(int i = 0; i < types.length; i++){
+ String targetSignature = types[i].getSignature();
+ OpcodeStack.Item item = stack.getStackItem(types.length - i - 1);
+ String sourceSignature = item.getSignature();
+ checkSignatures(sourceSignature, targetSignature);
+ }
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <dbr...@us...> - 2009-10-15 14:05:38
|
Revision: 1376
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1376&view=rev
Author: dbrosius
Date: 2009-10-15 14:05:30 +0000 (Thu, 15 Oct 2009)
Log Message:
-----------
more patches from bhaskar maddala
Modified Paths:
--------------
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2009-10-15 04:16:44 UTC (rev 1375)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2009-10-15 14:05:30 UTC (rev 1376)
@@ -49,6 +49,7 @@
public class ContraVariantArrayAssignment extends BytecodeScanningDetector {
private final BugReporter bugReporter;
private final OpcodeStack stack;
+ private final boolean checkMethods;
/**
* constructs a CVAA detector given the reporter to report bugs on.
@@ -57,7 +58,8 @@
*/
public ContraVariantArrayAssignment(final BugReporter bugReporter) {
this.bugReporter = bugReporter;
- this.stack = new OpcodeStack();
+ stack = new OpcodeStack();
+ checkMethods = System.getProperty("CCVA_CHECKMETHODS") != null;
}
/**
@@ -109,7 +111,7 @@
case INVOKEVIRTUAL:
case INVOKEINTERFACE:
case INVOKESPECIAL:
- if(stack.getStackDepth() > 0){
+ if(checkMethods && stack.getStackDepth() > 0){
String signature = getSigConstantOperand();
checkMethodInvocation(signature);
}
@@ -134,7 +136,7 @@
try{
if(isArrayType(sourceSignature)) {
if(!isArrayType(targetSignature)){
- bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", HIGH_PRIORITY)
+ bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", LOW_PRIORITY)
.addClass(this)
.addMethod(this)
.addSourceLine(this));
@@ -143,7 +145,7 @@
ObjectType sourceType = (ObjectType) ((ArrayType) Type.getType(sourceSignature)).getBasicType();
ObjectType targetType = (ObjectType) ((ArrayType) Type.getType(targetSignature)).getBasicType();
if(sourceType.subclassOf(targetType) && !targetType.subclassOf(sourceType)) {
- bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", HIGH_PRIORITY)
+ bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", NORMAL_PRIORITY)
.addClass(this)
.addMethod(this)
.addSourceLine(this));
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <dbr...@us...> - 2009-12-31 22:58:41
|
Revision: 1420
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1420&view=rev
Author: dbrosius
Date: 2009-12-31 22:58:30 +0000 (Thu, 31 Dec 2009)
Log Message:
-----------
CVAA shouldn't fire if the array basic types are the same in checkSignatures
Modified Paths:
--------------
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2009-12-28 06:24:35 UTC (rev 1419)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2009-12-31 22:58:30 UTC (rev 1420)
@@ -119,7 +119,7 @@
if(Type.getType(sourceSignature) instanceof ObjectType ) {
ObjectType sourceType = (ObjectType) Type.getType(sourceSignature);
ObjectType targetType = (ObjectType) ((ArrayType) Type.getType(targetSignature)).getBasicType();
- if(!sourceType.subclassOf(targetType)){
+ if(!sourceType.equals(targetType) && !sourceType.subclassOf(targetType)){
bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", HIGH_PRIORITY)
.addClass(this)
.addMethod(this)
@@ -159,7 +159,7 @@
if(isObjectType(sourceSignature) && isObjectType(targetSignature)){
ObjectType sourceType = (ObjectType) ((ArrayType) Type.getType(sourceSignature)).getBasicType();
ObjectType targetType = (ObjectType) ((ArrayType) Type.getType(targetSignature)).getBasicType();
- if(!targetType.subclassOf(sourceType)) {
+ if(!targetType.equals(sourceType) && !targetType.subclassOf(sourceType)) {
bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", NORMAL_PRIORITY)
.addClass(this)
.addMethod(this)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <dbr...@us...> - 2010-01-10 01:19:12
|
Revision: 1454
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1454&view=rev
Author: dbrosius
Date: 2010-01-10 01:19:04 +0000 (Sun, 10 Jan 2010)
Log Message:
-----------
if the store target is an java.lang.Object, just ignore
Modified Paths:
--------------
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2010-01-09 17:27:53 UTC (rev 1453)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2010-01-10 01:19:04 UTC (rev 1454)
@@ -149,6 +149,10 @@
private void checkSignatures(String sourceSignature, String targetSignature) {
try{
+ if ("Ljava/lang/Object;".equals(targetSignature)) {
+ return;
+ }
+
if(isArrayType(sourceSignature)) {
if(!isArrayType(targetSignature)){
bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", LOW_PRIORITY)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <dbr...@us...> - 2010-01-10 02:14:16
|
Revision: 1456
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1456&view=rev
Author: dbrosius
Date: 2010-01-10 02:14:08 +0000 (Sun, 10 Jan 2010)
Log Message:
-----------
if the target type is java.lang.Object, then ignore
Modified Paths:
--------------
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2010-01-10 02:05:50 UTC (rev 1455)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ContraVariantArrayAssignment.java 2010-01-10 02:14:08 UTC (rev 1456)
@@ -115,19 +115,21 @@
if(!value.isNull()) {
String sourceSignature = value.getSignature();
String targetSignature = arrayref.getSignature();
- try{
- if(Type.getType(sourceSignature) instanceof ObjectType ) {
- ObjectType sourceType = (ObjectType) Type.getType(sourceSignature);
- ObjectType targetType = (ObjectType) ((ArrayType) Type.getType(targetSignature)).getBasicType();
- if(!sourceType.equals(targetType) && !sourceType.subclassOf(targetType)){
- bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", HIGH_PRIORITY)
- .addClass(this)
- .addMethod(this)
- .addSourceLine(this));
+ if (!"Ljava/lang/Object;".equals(targetSignature)) {
+ try{
+ if(Type.getType(sourceSignature) instanceof ObjectType ) {
+ ObjectType sourceType = (ObjectType) Type.getType(sourceSignature);
+ ObjectType targetType = (ObjectType) ((ArrayType) Type.getType(targetSignature)).getBasicType();
+ if(!sourceType.equals(targetType) && !sourceType.subclassOf(targetType)){
+ bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", HIGH_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ }
}
+ } catch (ClassNotFoundException cnfe) {
+ bugReporter.reportMissingClass(cnfe);
}
- } catch (ClassNotFoundException cnfe) {
- bugReporter.reportMissingClass(cnfe);
}
}
break;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|