8130669: VM prohibits <clinit> methods with return values
authorhseigel
Thu, 09 Jul 2015 15:39:05 -0400
changeset 31777 0bfed49b6beb
parent 31776 70dd826a47b5
child 31778 7c57068badaa
8130669: VM prohibits <clinit> methods with return values Summary: Ignore <clinit> methods with return values instead of throwing ClassFormatError exceptions Reviewed-by: acorn, iklam
hotspot/src/share/vm/classfile/classFileParser.cpp
hotspot/src/share/vm/classfile/verifier.cpp
hotspot/test/runtime/classFileParserBug/BadInitMethod.java
hotspot/test/runtime/classFileParserBug/badInit.jasm
hotspot/test/runtime/classFileParserBug/ignoredClinit.jasm
--- a/hotspot/src/share/vm/classfile/classFileParser.cpp	Thu Jul 09 08:36:37 2015 -0400
+++ b/hotspot/src/share/vm/classfile/classFileParser.cpp	Thu Jul 09 15:39:05 2015 -0400
@@ -5087,8 +5087,8 @@
     // The first non-signature thing better be a ')'
     if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
       length--;
-      if (name->utf8_length() > 0 && name->byte_at(0) == '<') {
-        // All internal methods must return void
+      if (name == vmSymbols::object_initializer_name()) {
+        // All "<init>" methods must return void
         if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) {
           return args_size;
         }
--- a/hotspot/src/share/vm/classfile/verifier.cpp	Thu Jul 09 08:36:37 2015 -0400
+++ b/hotspot/src/share/vm/classfile/verifier.cpp	Thu Jul 09 15:39:05 2015 -0400
@@ -2846,7 +2846,7 @@
   if (sig_stream.type() != T_VOID) {
     if (method_name == vmSymbols::object_initializer_name()) {
       // <init> method must have a void return type
-      /* Unreachable?  Class file parser verifies that methods with '<' have
+      /* Unreachable?  Class file parser verifies that <init> methods have
        * void return */
       verify_error(ErrorContext::bad_code(bci),
           "Return type must be void in <init> method");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/classFileParserBug/BadInitMethod.java	Thu Jul 09 15:39:05 2015 -0400
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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 General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+/*
+ * @test
+ * @bug 8130669
+ * @summary VM prohibits <clinit> methods with return values
+ * @compile ignoredClinit.jasm
+ * @compile badInit.jasm
+ * @run main/othervm -Xverify:all BadInitMethod
+ */
+
+// Test that a non-void <clinit> method does not cause an exception to be
+// thrown.  But that a non-void <init> method causes a ClassFormatError
+// exception.
+public class BadInitMethod {
+    public static void main(String args[]) throws Throwable {
+
+        System.out.println("Regression test for bug 8130669");
+        try {
+            Class newClass = Class.forName("ignoredClinit");
+        } catch (java.lang.Throwable e) {
+            throw new RuntimeException("Unexpected exception: " + e.getMessage());
+        }
+
+        try {
+            Class newClass = Class.forName("badInit");
+            throw new RuntimeException("Expected ClassFormatError exception not thrown");
+        } catch (java.lang.ClassFormatError e) {
+            System.out.println("Test BadInitMethod passed");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/classFileParserBug/badInit.jasm	Thu Jul 09 15:39:05 2015 -0400
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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 General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+super public class badInit
+    version 52:0
+{
+
+
+// This <init> method has a non-void signature.  It should cause a
+// ClassFormatError exception.
+Method "<init>":"(I)I"
+    stack 1 locals 2
+{
+    aload_0;
+    invokespecial    Method java/lang/Object."<init>":"()V";
+    iconst_4;
+    ireturn;
+}
+
+public static Method main:"([Ljava/lang/String;)V"
+    stack 0 locals 1
+{
+    return;
+}
+
+} // end Class badInit
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/classFileParserBug/ignoredClinit.jasm	Thu Jul 09 15:39:05 2015 -0400
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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 General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+// This class contains a <clinit> method with signature: ()I.  The JVM should
+// not complain about this because methods named <clinit> that have arguments
+// and/or are not void should be ignored by the JVM.
+
+public class ignoredClinit version 51:0
+{
+
+    public static Method "<clinit>":"()I"
+    stack 1 locals 1
+    {
+        iconst_0;
+        ireturn;
+    }
+
+} // end Class ignoredClinit