--- old/src/share/vm/classfile/classFileParser.cpp 2014-05-27 16:45:25.830090407 +0400 +++ new/src/share/vm/classfile/classFileParser.cpp 2014-05-27 16:45:25.658086293 +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_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-27 16:45:26.404104136 +0400 +++ new/src/share/vm/oops/instanceKlass.cpp 2014-05-27 16:45:26.241100237 +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_super() const { + Klass* klass = const_cast(this); + while (klass != NULL) { + if (InstanceKlass::cast(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-27 16:45:26.939116933 +0400 +++ new/src/share/vm/oops/instanceKlass.hpp 2014-05-27 16:45:26.780113128 +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_super() const; +#endif + // Access to the implementor of an interface. Klass* implementor() const { --- /dev/null 2014-05-27 19:34:52.971356053 +0400 +++ new/test/runtime/RedefineFinalizer/Agent.java 2014-05-27 16:45:27.283125160 +0400 @@ -0,0 +1,75 @@ +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 Instrumentation instrumentation; + + 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 l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(1, l0); + 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 l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(6, l0); + 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 l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(8, l0); + 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 { + instrumentation = inst; + agentmain(args,inst); + } + + public static void agentmain(String args, Instrumentation inst) throws Exception { + instrumentation = inst; + ClassDefinition martyrDef = + new ClassDefinition(Class.forName("Martyr"), makeNewMartyr()); + inst.redefineClasses(martyrDef); + } +} --- /dev/null 2014-05-27 19:34:52.971356053 +0400 +++ new/test/runtime/RedefineFinalizer/Main.java 2014-05-27 16:45:27.747136258 +0400 @@ -0,0 +1,12 @@ +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-27 19:34:52.971356053 +0400 +++ new/test/runtime/RedefineFinalizer/Martyr.java 2014-05-27 16:45:28.210147332 +0400 @@ -0,0 +1,9 @@ +public class Martyr { + public String getName() { + return "Redefinition NOT done"; + } + + protected void finalize() { + } + +} --- /dev/null 2014-05-27 19:34:52.971356053 +0400 +++ new/test/runtime/RedefineFinalizer/MartyrSon.java 2014-05-27 16:45:28.671158357 +0400 @@ -0,0 +1,5 @@ +class MartyrSon extends Martyr { + public String getName() { + return super.getName(); + } +} --- /dev/null 2014-05-27 19:34:52.971356053 +0400 +++ new/test/runtime/RedefineFinalizer/manifest.mf 2014-05-27 16:45:29.136169480 +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-27 19:34:52.971356053 +0400 +++ new/test/runtime/RedefineFinalizer/testme.sh 2014-05-27 16:45:29.597180505 +0400 @@ -0,0 +1,27 @@ +#!/bin/sh + +# @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