Mon, 02 Jun 2014 19:08:18 +0200
8044364: runtime/RedefineFinalizer test fails on windows
Summary: Rewrote the test in pure Java, added RedefineClassHelper utility class
Reviewed-by: coleenp, allwin, gtriantafill, dsamersoff
test/runtime/RedefineFinalizer/Agent.java | file | annotate | diff | comparison | revisions | |
test/runtime/RedefineFinalizer/Main.java | file | annotate | diff | comparison | revisions | |
test/runtime/RedefineFinalizer/Martyr.java | file | annotate | diff | comparison | revisions | |
test/runtime/RedefineFinalizer/MartyrSon.java | file | annotate | diff | comparison | revisions | |
test/runtime/RedefineFinalizer/RedefineFinalizer.java | file | annotate | diff | comparison | revisions | |
test/runtime/RedefineFinalizer/manifest.mf | file | annotate | diff | comparison | revisions | |
test/runtime/RedefineFinalizer/testme.sh | file | annotate | diff | comparison | revisions | |
test/testlibrary/RedefineClassHelper.java | file | annotate | diff | comparison | revisions | |
test/testlibrary_tests/RedefineClassTest.java | file | annotate | diff | comparison | revisions |
1.1 --- a/test/runtime/RedefineFinalizer/Agent.java Wed May 28 07:36:32 2014 -0700 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,94 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. 1.11 - * 1.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 - * version 2 for more details (a copy is included in the LICENSE file that 1.16 - * accompanied this code). 1.17 - * 1.18 - * You should have received a copy of the GNU General Public License version 1.19 - * 2 along with this work; if not, write to the Free Software Foundation, 1.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 - * 1.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 - * or visit www.oracle.com if you need additional information or have any 1.24 - * questions. 1.25 - */ 1.26 - 1.27 -import java.lang.instrument.Instrumentation; 1.28 -import java.lang.instrument.ClassDefinition; 1.29 - 1.30 -import jdk.internal.org.objectweb.asm.ClassWriter; 1.31 -import jdk.internal.org.objectweb.asm.Label; 1.32 -import jdk.internal.org.objectweb.asm.MethodVisitor; 1.33 -import jdk.internal.org.objectweb.asm.Opcodes; 1.34 - 1.35 -public class Agent implements Opcodes { 1.36 - 1.37 - private static byte[] makeNewMartyr() { 1.38 - ClassWriter cw = new ClassWriter(0); 1.39 - MethodVisitor mv; 1.40 - 1.41 - cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "Martyr", null, "java/lang/Object", null); 1.42 - cw.visitSource(null, null); 1.43 - 1.44 - { 1.45 - mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 1.46 - mv.visitCode(); 1.47 - Label lab0 = new Label(); 1.48 - mv.visitLabel(lab0); 1.49 - mv.visitLineNumber(1, lab0); 1.50 - mv.visitVarInsn(ALOAD, 0); 1.51 - mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); 1.52 - mv.visitInsn(RETURN); 1.53 - mv.visitMaxs(1, 1); 1.54 - mv.visitEnd(); 1.55 - } 1.56 - 1.57 - { 1.58 - mv = cw.visitMethod(ACC_PUBLIC, "getName", "()Ljava/lang/String;", null, null); 1.59 - mv.visitCode(); 1.60 - Label lab0 = new Label(); 1.61 - mv.visitLabel(lab0); 1.62 - mv.visitLineNumber(6, lab0); 1.63 - mv.visitLdcInsn("Redefinition done"); 1.64 - mv.visitInsn(ARETURN); 1.65 - mv.visitMaxs(1, 1); 1.66 - mv.visitEnd(); 1.67 - } 1.68 - 1.69 - { 1.70 - mv = cw.visitMethod(ACC_PROTECTED, "finalize", "()V", null, null); 1.71 - mv.visitCode(); 1.72 - Label lab0 = new Label(); 1.73 - mv.visitLabel(lab0); 1.74 - mv.visitLineNumber(8, lab0); 1.75 - mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); 1.76 - mv.visitLdcInsn("Finalizer called"); 1.77 - mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); 1.78 - mv.visitInsn(RETURN); 1.79 - mv.visitMaxs(2, 1); 1.80 - mv.visitEnd(); 1.81 - } 1.82 - 1.83 - cw.visitEnd(); 1.84 - return cw.toByteArray(); 1.85 - } 1.86 - 1.87 - 1.88 - public static void premain(String args, Instrumentation inst) throws Exception { 1.89 - agentmain(args, inst); 1.90 - } 1.91 - 1.92 - public static void agentmain(String args, Instrumentation inst) throws Exception { 1.93 - ClassDefinition martyrDef = 1.94 - new ClassDefinition(Class.forName("Martyr"), makeNewMartyr()); 1.95 - inst.redefineClasses(martyrDef); 1.96 - } 1.97 -}
2.1 --- a/test/runtime/RedefineFinalizer/Main.java Wed May 28 07:36:32 2014 -0700 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,34 +0,0 @@ 2.4 -/* 2.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 - * 2.8 - * This code is free software; you can redistribute it and/or modify it 2.9 - * under the terms of the GNU General Public License version 2 only, as 2.10 - * published by the Free Software Foundation. 2.11 - * 2.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 - * version 2 for more details (a copy is included in the LICENSE file that 2.16 - * accompanied this code). 2.17 - * 2.18 - * You should have received a copy of the GNU General Public License version 2.19 - * 2 along with this work; if not, write to the Free Software Foundation, 2.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 - * 2.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 - * or visit www.oracle.com if you need additional information or have any 2.24 - * questions. 2.25 - */ 2.26 - 2.27 -public class Main { 2.28 - public static void main(String[] args) { 2.29 - try { 2.30 - MartyrSon m = new MartyrSon(); 2.31 - System.out.println(m.getName()); 2.32 - System.runFinalization(); 2.33 - } catch (Throwable e) { 2.34 - e.printStackTrace(); 2.35 - } 2.36 - } 2.37 -}
3.1 --- a/test/runtime/RedefineFinalizer/Martyr.java Wed May 28 07:36:32 2014 -0700 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,33 +0,0 @@ 3.4 -/* 3.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 - * 3.8 - * This code is free software; you can redistribute it and/or modify it 3.9 - * under the terms of the GNU General Public License version 2 only, as 3.10 - * published by the Free Software Foundation. 3.11 - * 3.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 - * version 2 for more details (a copy is included in the LICENSE file that 3.16 - * accompanied this code). 3.17 - * 3.18 - * You should have received a copy of the GNU General Public License version 3.19 - * 2 along with this work; if not, write to the Free Software Foundation, 3.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 - * 3.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 - * or visit www.oracle.com if you need additional information or have any 3.24 - * questions. 3.25 - */ 3.26 - 3.27 -public class Martyr { 3.28 - public String getName() { 3.29 - return "Redefinition NOT done"; 3.30 - } 3.31 - 3.32 - protected void finalize() { 3.33 - // should be empty 3.34 - } 3.35 - 3.36 -}
4.1 --- a/test/runtime/RedefineFinalizer/MartyrSon.java Wed May 28 07:36:32 2014 -0700 4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 @@ -1,28 +0,0 @@ 4.4 -/* 4.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 4.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 - * 4.8 - * This code is free software; you can redistribute it and/or modify it 4.9 - * under the terms of the GNU General Public License version 2 only, as 4.10 - * published by the Free Software Foundation. 4.11 - * 4.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 - * version 2 for more details (a copy is included in the LICENSE file that 4.16 - * accompanied this code). 4.17 - * 4.18 - * You should have received a copy of the GNU General Public License version 4.19 - * 2 along with this work; if not, write to the Free Software Foundation, 4.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 - * 4.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 - * or visit www.oracle.com if you need additional information or have any 4.24 - * questions. 4.25 - */ 4.26 - 4.27 -class MartyrSon extends Martyr { 4.28 - public String getName() { 4.29 - return super.getName(); 4.30 - } 4.31 -}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/runtime/RedefineFinalizer/RedefineFinalizer.java Mon Jun 02 19:08:18 2014 +0200 5.3 @@ -0,0 +1,64 @@ 5.4 +/* 5.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +/* 5.28 + * @test 5.29 + * @bug 6904403 5.30 + * @summary Don't assert if we redefine finalize method 5.31 + * @library /testlibrary 5.32 + * @build RedefineClassHelper 5.33 + * @run main RedefineClassHelper 5.34 + * @run main/othervm -javaagent:redefineagent.jar RedefineFinalizer 5.35 + */ 5.36 + 5.37 +/* 5.38 + * Regression test for hitting: 5.39 + * 5.40 + * assert(f == k->has_finalizer()) failed: inconsistent has_finalizer 5.41 + * 5.42 + * when redefining finalizer method 5.43 + */ 5.44 +public class RedefineFinalizer { 5.45 + 5.46 + public static String newB = 5.47 + "class RedefineFinalizer$B {" + 5.48 + " protected void finalize() { " + 5.49 + " System.out.println(\"Finalizer called\");" + 5.50 + " }" + 5.51 + "}"; 5.52 + 5.53 + public static void main(String[] args) throws Exception { 5.54 + RedefineClassHelper.redefineClass(B.class, newB); 5.55 + 5.56 + A a = new A(); 5.57 + } 5.58 + 5.59 + static class A extends B { 5.60 + } 5.61 + 5.62 + static class B { 5.63 + protected void finalize() { 5.64 + // should be empty 5.65 + } 5.66 + } 5.67 +}
6.1 --- a/test/runtime/RedefineFinalizer/manifest.mf Wed May 28 07:36:32 2014 -0700 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,5 +0,0 @@ 6.4 -Main-Class: Main 6.5 -Agent-Class: Agent 6.6 -Can-Redefine-Classes: true 6.7 -Can-Retransform-Classes: true 6.8 -Premain-Class: Agent
7.1 --- a/test/runtime/RedefineFinalizer/testme.sh Wed May 28 07:36:32 2014 -0700 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,49 +0,0 @@ 7.4 -#!/bin/sh 7.5 - 7.6 -# Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. 7.7 -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.8 -# 7.9 -# This code is free software; you can redistribute it and/or modify it 7.10 -# under the terms of the GNU General Public License version 2 only, as 7.11 -# published by the Free Software Foundation. 7.12 -# 7.13 -# This code is distributed in the hope that it will be useful, but WITHOUT 7.14 -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.15 -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.16 -# version 2 for more details (a copy is included in the LICENSE file that 7.17 -# accompanied this code). 7.18 -# 7.19 -# You should have received a copy of the GNU General Public License version 7.20 -# 2 along with this work; if not, write to the Free Software Foundation, 7.21 -# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.22 -# 7.23 -# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.24 -# or visit www.oracle.com if you need additional information or have any 7.25 -# questions. 7.26 - 7.27 - 7.28 -# @test 7.29 -# @bug 6904403 7.30 -# @summary Don't assert if we redefine finalize method 7.31 -# @run shell testme.sh 7.32 - 7.33 -# This test shouldn't provoke and assert(f == k->has_finalizer()) failed: inconsistent has_finalizer 7.34 - 7.35 -. ${TESTSRC}/../../test_env.sh 7.36 - 7.37 -JAVAC=${COMPILEJAVA}${FS}bin${FS}javac 7.38 -JAR=${COMPILEJAVA}${FS}bin${FS}jar 7.39 -JAVA=${TESTJAVA}${FS}bin${FS}java 7.40 - 7.41 -TOOLS_JAR=${TESTJAVA}${FS}lib${FS}tools.jar 7.42 - 7.43 -cp ${TESTSRC}${FS}*.java . 7.44 -${JAVAC} -XDignore.symbol.file -classpath ${TOOLS_JAR} -sourcepath ${TESTSRC} *.java 7.45 -if [ $? -eq 1 ] 7.46 - then 7.47 - echo "Compilation failed" 7.48 - exit 7.49 -fi 7.50 - 7.51 -${JAR} cvfm testcase.jar ${TESTSRC}/manifest.mf . 7.52 -${JAVA} -Xbootclasspath/a:${TOOLS_JAR} -javaagent:${PWD}/testcase.jar Main
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/testlibrary/RedefineClassHelper.java Mon Jun 02 19:08:18 2014 +0200 8.3 @@ -0,0 +1,79 @@ 8.4 +/* 8.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. 8.11 + * 8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.15 + * version 2 for more details (a copy is included in the LICENSE file that 8.16 + * accompanied this code). 8.17 + * 8.18 + * You should have received a copy of the GNU General Public License version 8.19 + * 2 along with this work; if not, write to the Free Software Foundation, 8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.21 + * 8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.23 + * or visit www.oracle.com if you need additional information or have any 8.24 + * questions. 8.25 + */ 8.26 + 8.27 +import java.io.PrintWriter; 8.28 +import java.lang.instrument.*; 8.29 +import com.oracle.java.testlibrary.*; 8.30 + 8.31 +/* 8.32 + * Helper class to write tests that redefine classes. 8.33 + * When main method is run, it will create a redefineagent.jar that can be used 8.34 + * with the -javaagent option to support redefining classes in jtreg tests. 8.35 + * 8.36 + * See sample test in test/testlibrary_tests/RedefineClassTest.java 8.37 + */ 8.38 +public class RedefineClassHelper { 8.39 + 8.40 + public static Instrumentation instrumentation; 8.41 + public static void premain(String agentArgs, Instrumentation inst) { 8.42 + instrumentation = inst; 8.43 + } 8.44 + 8.45 + /** 8.46 + * Redefine a class 8.47 + * 8.48 + * @param clazz Class to redefine 8.49 + * @param javacode String with the new java code for the class to be redefined 8.50 + */ 8.51 + public static void redefineClass(Class clazz, String javacode) throws Exception { 8.52 + byte[] bytecode = InMemoryJavaCompiler.compile(clazz.getName(), javacode); 8.53 + redefineClass(clazz, bytecode); 8.54 + } 8.55 + 8.56 + /** 8.57 + * Redefine a class 8.58 + * 8.59 + * @param clazz Class to redefine 8.60 + * @param bytecode byte[] with the new class 8.61 + */ 8.62 + public static void redefineClass(Class clazz, byte[] bytecode) throws Exception { 8.63 + instrumentation.redefineClasses(new ClassDefinition(clazz, bytecode)); 8.64 + } 8.65 + 8.66 + /** 8.67 + * Main method to be invoked before test to create the redefineagent.jar 8.68 + */ 8.69 + public static void main(String[] args) throws Exception { 8.70 + ClassFileInstaller.main("RedefineClassHelper"); 8.71 + 8.72 + PrintWriter pw = new PrintWriter("MANIFEST.MF"); 8.73 + pw.println("Premain-Class: RedefineClassHelper"); 8.74 + pw.println("Can-Redefine-Classes: true"); 8.75 + pw.close(); 8.76 + 8.77 + sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); 8.78 + if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineClassHelper.class" })) { 8.79 + throw new Exception("jar operation failed"); 8.80 + } 8.81 + } 8.82 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/test/testlibrary_tests/RedefineClassTest.java Mon Jun 02 19:08:18 2014 +0200 9.3 @@ -0,0 +1,54 @@ 9.4 +/* 9.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.7 + * 9.8 + * This code is free software; you can redistribute it and/or modify it 9.9 + * under the terms of the GNU General Public License version 2 only, as 9.10 + * published by the Free Software Foundation. 9.11 + * 9.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 9.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 9.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 9.15 + * version 2 for more details (a copy is included in the LICENSE file that 9.16 + * accompanied this code). 9.17 + * 9.18 + * You should have received a copy of the GNU General Public License version 9.19 + * 2 along with this work; if not, write to the Free Software Foundation, 9.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 9.21 + * 9.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 9.23 + * or visit www.oracle.com if you need additional information or have any 9.24 + * questions. 9.25 + */ 9.26 + 9.27 +/* 9.28 + * @test 9.29 + * @library /testlibrary 9.30 + * @summary Proof of concept test for RedefineClassHelper 9.31 + * @build RedefineClassHelper 9.32 + * @run main RedefineClassHelper 9.33 + * @run main/othervm -javaagent:redefineagent.jar RedefineClassTest 9.34 + */ 9.35 + 9.36 +import static com.oracle.java.testlibrary.Asserts.*; 9.37 +import com.oracle.java.testlibrary.*; 9.38 + 9.39 +/* 9.40 + * Proof of concept test for the test utility class RedefineClassHelper 9.41 + */ 9.42 +public class RedefineClassTest { 9.43 + 9.44 + public static String newClass = "class RedefineClassTest$A { public int Method() { return 2; } }"; 9.45 + public static void main(String[] args) throws Exception { 9.46 + A a = new A(); 9.47 + assertTrue(a.Method() == 1); 9.48 + RedefineClassHelper.redefineClass(A.class, newClass); 9.49 + assertTrue(a.Method() == 2); 9.50 + } 9.51 + 9.52 + static class A { 9.53 + public int Method() { 9.54 + return 1; 9.55 + } 9.56 + } 9.57 +}