8139069: JVM should throw ClassFormatError for <init> methods in interfaces

If method being parsed is in an interface, throw ClassFormatError if its name is "<init>"

Reviewed-by: acorn, lfoltan
This commit is contained in:
Harold Seigel 2015-10-14 13:30:47 -04:00
parent 5659603602
commit df9b5759f7
5 changed files with 125 additions and 4 deletions

View File

@ -1997,6 +1997,10 @@ methodHandle ClassFileParser::parse_method(bool is_interface,
verify_legal_method_modifiers(flags, is_interface, name, CHECK_(nullHandle));
}
if (name == vmSymbols::object_initializer_name() && is_interface) {
classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_(nullHandle));
}
int args_size = -1; // only used when _need_verify is true
if (_need_verify) {
args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +

View File

@ -1579,11 +1579,9 @@ void ClassVerifier::verify_method(methodHandle m, TRAPS) {
return;
}
// Make sure "this" has been initialized if current method is an
// <init>. Note that "<init>" methods in interfaces are just
// normal methods. Interfaces cannot have ctors.
// <init>.
if (_method->name() == vmSymbols::object_initializer_name() &&
current_frame.flag_this_uninit() &&
!current_class()->is_interface()) {
current_frame.flag_this_uninit()) {
verify_error(ErrorContext::bad_code(bci),
"Constructor must call super() or this() "
"before return");

View File

@ -0,0 +1,57 @@
/*
* 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 8139069
* @summary Check that any method named <init> in an interface causes ClassFormatError
* @compile nonvoidinit.jasm voidinit.jasm
* @run main InitInInterface
*/
// Test that an <init> method is not allowed in interfaces.
public class InitInInterface {
public static void main(String args[]) throws Throwable {
System.out.println("Regression test for bug 8130183");
try {
Class newClass = Class.forName("nonvoidinit");
throw new RuntimeException(
"ClassFormatError not thrown for non-void <init> in an interface");
} catch (java.lang.ClassFormatError e) {
if (!e.getMessage().contains("Interface cannot have a method named <init>")) {
throw new RuntimeException("Unexpected exception nonvoidint: " + e.getMessage());
}
}
try {
Class newClass = Class.forName("voidinit");
throw new RuntimeException(
"ClassFormatError not thrown for void <init> in an interface");
} catch (java.lang.ClassFormatError e) {
if (!e.getMessage().contains("Interface cannot have a method named <init>")) {
throw new RuntimeException("Unexpected exception voidint: " + e.getMessage());
}
}
}
}

View File

@ -0,0 +1,31 @@
/*
* 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.
*
*/
// Interface containing non-void <init> method.
public interface nonvoidinit version 50:0
{
public abstract Method "<init>":"()I";
} // end Class nonvoidinit

View File

@ -0,0 +1,31 @@
/*
* 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.
*
*/
// Interface containing void <init> method.
public interface voidinit version 50:0
{
public abstract Method "<init>":"()V";
} // end Class voidinit