--- old/src/share/vm/classfile/classFileParser.cpp 2014-05-28 16:50:04.251520265 +0400 +++ new/src/share/vm/classfile/classFileParser.cpp 2014-05-28 16:50:04.074516185 +0400 @@ -4309,9 +4309,15 @@ Method* m = k->lookup_method(vmSymbols::finalize_method_name(), vmSymbols::void_method_signature()); if (m != NULL && !m->is_empty_method()) { - f = true; + f = true; + } + + // Spec doesn't prevent agent from redefinition of empty finalizer. + // Despite the fact that it's generally bad idea and redefined finalizer + // will not work as expected we shouldn't abort vm in this case + if (!k->has_redefined_this_or_super()) { + assert(f == k->has_finalizer(), "inconsistent has_finalizer"); } - assert(f == k->has_finalizer(), "inconsistent has_finalizer"); #endif // Check if this klass supports the java.lang.Cloneable interface --- old/src/share/vm/oops/instanceKlass.cpp 2014-05-28 16:50:04.825533497 +0400 +++ new/src/share/vm/oops/instanceKlass.cpp 2014-05-28 16:50:04.658529646 +0400 @@ -1479,6 +1479,21 @@ return NULL; } +#ifdef ASSERT +// search through class hierarchy and return true if this class or +// one of the superclasses was redefined +bool InstanceKlass::has_redefined_this_or_super() const { + const InstanceKlass* klass = this; + while (klass != NULL) { + if (klass->has_been_redefined()) { + return true; + } + klass = InstanceKlass::cast(klass->super()); + } + return false; +} +#endif + // lookup a method in the default methods list then in all transitive interfaces // Do NOT return private or static methods Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name, --- old/src/share/vm/oops/instanceKlass.hpp 2014-05-28 16:50:05.373546129 +0400 +++ new/src/share/vm/oops/instanceKlass.hpp 2014-05-28 16:50:05.208542324 +0400 @@ -754,6 +754,11 @@ bool implements_interface(Klass* k) const; bool is_same_or_direct_interface(Klass* k) const; +#ifdef ASSERT + // check whether this class or one of its superclasses was redefined + bool has_redefined_this_or_super() const; +#endif + // Access to the implementor of an interface. Klass* implementor() const { @@ -811,8 +816,8 @@ // Casting from Klass* static InstanceKlass* cast(Klass* k) { - assert(k->is_klass(), "must be"); - assert(k->oop_is_instance(), "cast to InstanceKlass"); + assert(k == NULL || k->is_klass(), "must be"); + assert(k == NULL || k->oop_is_instance(), "cast to InstanceKlass"); return (InstanceKlass*) k; } --- /dev/null 2014-05-28 17:40:08.932424258 +0400 +++ new/test/runtime/RedefineFinalizer/Agent.java 2014-05-28 16:50:05.730554357 +0400 @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2014, 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. + */ + +import java.lang.instrument.Instrumentation; +import java.lang.instrument.ClassDefinition; + +import jdk.internal.org.objectweb.asm.ClassWriter; +import jdk.internal.org.objectweb.asm.Label; +import jdk.internal.org.objectweb.asm.MethodVisitor; +import jdk.internal.org.objectweb.asm.Opcodes; + +public class Agent implements Opcodes { + + private static byte[] makeNewMartyr() { + ClassWriter cw = new ClassWriter(0); + MethodVisitor mv; + + cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "Martyr", null, "java/lang/Object", null); + cw.visitSource(null, null); + + { + mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); + mv.visitCode(); + Label lab0 = new Label(); + mv.visitLabel(lab0); + mv.visitLineNumber(1, lab0); + mv.visitVarInsn(ALOAD, 0); + mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V"); + mv.visitInsn(RETURN); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + + { + mv = cw.visitMethod(ACC_PUBLIC, "getName", "()Ljava/lang/String;", null, null); + mv.visitCode(); + Label lab0 = new Label(); + mv.visitLabel(lab0); + mv.visitLineNumber(6, lab0); + mv.visitLdcInsn("Redefinition done"); + mv.visitInsn(ARETURN); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + + { + mv = cw.visitMethod(ACC_PROTECTED, "finalize", "()V", null, null); + mv.visitCode(); + Label lab0 = new Label(); + mv.visitLabel(lab0); + mv.visitLineNumber(8, lab0); + mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); + mv.visitLdcInsn("Finalizer called"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); + mv.visitInsn(RETURN); + mv.visitMaxs(2, 1); + mv.visitEnd(); + } + + cw.visitEnd(); + return cw.toByteArray(); + } + + + public static void premain(String args, Instrumentation inst) throws Exception { + agentmain(args, inst); + } + + public static void agentmain(String args, Instrumentation inst) throws Exception { + ClassDefinition martyrDef = + new ClassDefinition(Class.forName("Martyr"), makeNewMartyr()); + inst.redefineClasses(martyrDef); + } +} --- /dev/null 2014-05-28 17:40:08.932424258 +0400 +++ new/test/runtime/RedefineFinalizer/Main.java 2014-05-28 16:50:06.207565352 +0400 @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2014, 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. + */ + +public class Main { + public static void main(String[] args) { + try { + MartyrSon m = new MartyrSon(); + System.out.println(m.getName()); + System.runFinalization(); + } catch (Throwable e) { + e.printStackTrace(); + } + } +} --- /dev/null 2014-05-28 17:40:08.932424258 +0400 +++ new/test/runtime/RedefineFinalizer/Martyr.java 2014-05-28 16:50:06.685576370 +0400 @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2014, 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. + */ + +public class Martyr { + public String getName() { + return "Redefinition NOT done"; + } + + protected void finalize() { + // should be empty + } + +} --- /dev/null 2014-05-28 17:40:08.932424258 +0400 +++ new/test/runtime/RedefineFinalizer/MartyrSon.java 2014-05-28 16:50:07.165587435 +0400 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2014, 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. + */ + +class MartyrSon extends Martyr { + public String getName() { + return super.getName(); + } +} --- /dev/null 2014-05-28 17:40:08.932424258 +0400 +++ new/test/runtime/RedefineFinalizer/manifest.mf 2014-05-28 16:50:07.638598338 +0400 @@ -0,0 +1,5 @@ +Main-Class: Main +Agent-Class: Agent +Can-Redefine-Classes: true +Can-Retransform-Classes: true +Premain-Class: Agent --- /dev/null 2014-05-28 17:40:08.932424258 +0400 +++ new/test/runtime/RedefineFinalizer/testme.sh 2014-05-28 16:50:08.112609264 +0400 @@ -0,0 +1,49 @@ +#!/bin/sh + +# Copyright (c) 2014 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 6904403 +# @summary Don't assert if we redefine finalize method +# @run shell testme.sh + +# This test shouldn't provoke and assert(f == k->has_finalizer()) failed: inconsistent has_finalizer + +. ${TESTSRC}/../../test_env.sh + +JAVAC=${COMPILEJAVA}${FS}bin${FS}javac +JAR=${COMPILEJAVA}${FS}bin${FS}jar +JAVA=${TESTJAVA}${FS}bin${FS}java + +TOOLS_JAR=${TESTJAVA}${FS}lib${FS}tools.jar + +cp ${TESTSRC}${FS}*.java . +${JAVAC} -XDignore.symbol.file -classpath ${TOOLS_JAR} -sourcepath ${TESTSRC} *.java +if [ $? -eq 1 ] + then + echo "Compilation failed" + exit +fi + +${JAR} cvfm testcase.jar ${TESTSRC}/manifest.mf . +${JAVA} -Xbootclasspath/a:${TOOLS_JAR} -javaagent:${PWD}/testcase.jar Main