You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2015/03/07 01:23:07 UTC

svn commit: r1664767 - in /commons/proper/bcel/trunk: ./ src/changes/ src/main/java/org/apache/bcel/verifier/structurals/ src/test/java/org/apache/bcel/verifier/ src/test/resources/org/ src/test/resources/org/apache/ src/test/resources/org/apache/bcel/...

Author: ebourg
Date: Sat Mar  7 00:23:06 2015
New Revision: 1664767

URL: http://svn.apache.org/r1664767
Log:
The verifier now checks if methods with a void return type attempt to return an object. Thanks to Jérôme Leroux (BCEL-184)

Added:
    commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java
    commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java
    commons/proper/bcel/trunk/src/test/resources/org/
    commons/proper/bcel/trunk/src/test/resources/org/apache/
    commons/proper/bcel/trunk/src/test/resources/org/apache/bcel/
    commons/proper/bcel/trunk/src/test/resources/org/apache/bcel/verifier/
    commons/proper/bcel/trunk/src/test/resources/org/apache/bcel/verifier/tests/
    commons/proper/bcel/trunk/src/test/resources/org/apache/bcel/verifier/tests/TestReturn01.class   (with props)
Modified:
    commons/proper/bcel/trunk/RELEASE-NOTES.txt
    commons/proper/bcel/trunk/src/changes/changes.xml
    commons/proper/bcel/trunk/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java

Modified: commons/proper/bcel/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/RELEASE-NOTES.txt?rev=1664767&r1=1664766&r2=1664767&view=diff
==============================================================================
--- commons/proper/bcel/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/bcel/trunk/RELEASE-NOTES.txt Sat Mar  7 00:23:06 2015
@@ -99,11 +99,11 @@ Bug fixes from 5.2
 [BCEL-174] Verification of interfaces with default methods fails with Java 8
 [BCEL-177] MethodParameters should read 1 byte not two for parameter count
 [BCEL-181] ClassLoaderRepository.loadClass(String) leaks input streams
+[BCEL-184] JustIce verifier does not check correctly the returned value of a method
 [BCEL-194] LocalVariableGen hashCode() function is incorrrect
 [BCEL-197] Add support for TypeVariables to Utility.signatureToString()
 [BCEL-207] MethodGen.removeLocalVariable(s) doesn't remove the associated Targetters
 
-
 Feedback
 --------
 

Modified: commons/proper/bcel/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/changes/changes.xml?rev=1664767&r1=1664766&r2=1664767&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/changes/changes.xml (original)
+++ commons/proper/bcel/trunk/src/changes/changes.xml Sat Mar  7 00:23:06 2015
@@ -63,6 +63,9 @@ The <action> type attribute can be add,u
 
   <body>
     <release version="6.0" date="TBA" description="Major release with Java 7 and 8 support">
+      <action issue="BCEL-184" type="fix" due-to="Jérôme Leroux">
+        The verifier now checks if methods with a void return type attempt to return an object.
+      </action>
       <action issue="BCEL-207" type="fix" due-to="Mark Roberts">
         MethodGen.removeLocalVariable now properly unreference the removed variable
         from the targetters of the instruction handlers delimiting the scope of the variable.

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java?rev=1664767&r1=1664766&r2=1664767&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java (original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java Sat Mar  7 00:23:06 2015
@@ -413,6 +413,9 @@ public class InstConstraintVisitor exten
             }
         }
         if (o instanceof ARETURN){
+            if (method_type == Type.VOID){
+                constraintViolated(o, "ARETURN instruction in void method.");
+            }
             if (stack().peek() == Type.NULL){
                 return;
             }

Added: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java?rev=1664767&view=auto
==============================================================================
--- commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java (added)
+++ commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java Sat Mar  7 00:23:06 2015
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+package org.apache.bcel.verifier;
+
+import junit.framework.TestCase;
+
+import org.apache.bcel.Repository;
+import org.apache.bcel.classfile.JavaClass;
+
+public abstract class AbstractVerifierTestCase extends TestCase {
+
+    public static final String TEST_PACKAGE = "org.apache.bcel.verifier.tests.";
+
+    /**
+     * Asserts that the verification of the given class is OK. If it isn't it throws an AssertionFailedError with the given message.
+     *
+     * @param classname simple classname of the class to verify
+     * @param message   message displayed if assertion fails
+     */
+    public void assertVerifyOK(String classname, String message) {
+        final String testClassname = TEST_PACKAGE + classname;
+        assertTrue(message, doAllPasses(testClassname));
+    }
+
+    /**
+     * Asserts that the verification of the given class is rejected. If it isn't it throws an AssertionFailedError with the given message.
+     *
+     * @param classname simple classname of the class to verify
+     * @param message   message displayed if assertion fails
+     */
+    public void assertVerifyRejected(String classname, String message) {
+        final String testClassname = TEST_PACKAGE + classname;
+        assertFalse(message, doAllPasses(testClassname));
+    }
+
+    /**
+     * Executes all the verification on the given class.
+     *
+     * @param classname name of the class to verify
+     * @return false if the verification fails, true otherwise
+     */
+    public boolean doAllPasses(String classname) {
+        int nbMethods = 0;
+
+        try {
+            JavaClass jc = Repository.lookupClass(classname);
+            nbMethods = jc.getMethods().length;
+        } catch (ClassNotFoundException e) {
+            fail(e.getMessage());
+            return false;
+        }
+
+        Verifier verifier = VerifierFactory.getVerifier(classname);
+        VerificationResult result = verifier.doPass1();
+        if (result.getStatus() != VerificationResult.VERIFIED_OK) {
+            return false;
+        }
+
+        result = verifier.doPass2();
+        if (result.getStatus() != VerificationResult.VERIFIED_OK) {
+            return false;
+        }
+
+        for (int i = nbMethods; --i >= 0; ) {
+            result = verifier.doPass3a(i);
+            if (result.getStatus() != VerificationResult.VERIFIED_OK) {
+                return false;
+            }
+            result = verifier.doPass3b(i);
+            if (result.getStatus() != VerificationResult.VERIFIED_OK) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+}

Added: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java?rev=1664767&view=auto
==============================================================================
--- commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java (added)
+++ commons/proper/bcel/trunk/src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java Sat Mar  7 00:23:06 2015
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.bcel.verifier;
+
+public class VerifierReturnTestCase extends AbstractVerifierTestCase {
+
+    public void testInvalidReturn() {
+        assertVerifyRejected("TestReturn01", "Verification of a void method that returns an object must fail.");
+    }
+
+}

Added: commons/proper/bcel/trunk/src/test/resources/org/apache/bcel/verifier/tests/TestReturn01.class
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/resources/org/apache/bcel/verifier/tests/TestReturn01.class?rev=1664767&view=auto
==============================================================================
Binary file - no diff available.

Propchange: commons/proper/bcel/trunk/src/test/resources/org/apache/bcel/verifier/tests/TestReturn01.class
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream