From afd0a54dd04a8bc190f32ec28199b6edefafdc7c Mon Sep 17 00:00:00 2001 From: "Daniel D. Daugherty" Date: Mon, 24 Mar 2008 17:20:54 -0700 Subject: [PATCH] 6667089: 3/3 multiple redefinitions of a class break reflection Add regression test for multiple redefinitions of a class break reflection. Reviewed-by: sspitsyn --- .../instrument/RedefineMethodAddInvoke.sh | 82 +++++++++++++++++++ .../RedefineMethodAddInvokeAgent.java | 43 ++++++++++ .../RedefineMethodAddInvokeApp.java | 70 ++++++++++++++++ .../RedefineMethodAddInvokeTarget.java | 37 +++++++++ .../RedefineMethodAddInvokeTarget_1.java | 43 ++++++++++ .../RedefineMethodAddInvokeTarget_2.java | 49 +++++++++++ 6 files changed, 324 insertions(+) create mode 100644 jdk/test/java/lang/instrument/RedefineMethodAddInvoke.sh create mode 100644 jdk/test/java/lang/instrument/RedefineMethodAddInvokeAgent.java create mode 100644 jdk/test/java/lang/instrument/RedefineMethodAddInvokeApp.java create mode 100644 jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget.java create mode 100644 jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget_1.java create mode 100644 jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget_2.java diff --git a/jdk/test/java/lang/instrument/RedefineMethodAddInvoke.sh b/jdk/test/java/lang/instrument/RedefineMethodAddInvoke.sh new file mode 100644 index 00000000000..641d56246cc --- /dev/null +++ b/jdk/test/java/lang/instrument/RedefineMethodAddInvoke.sh @@ -0,0 +1,82 @@ +# +# Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, +# CA 95054 USA or visit www.sun.com if you need additional information or +# have any questions. +# + +# @test +# @bug 6667089 +# @summary Reflexive invocation of newly added methods broken. +# @author Daniel D. Daugherty +# +# @run shell MakeJAR3.sh RedefineMethodAddInvokeAgent 'Can-Redefine-Classes: true' +# @run build RedefineMethodAddInvokeApp +# @run shell RedefineMethodAddInvoke.sh +# + +if [ "${TESTJAVA}" = "" ] +then + echo "TESTJAVA not set. Test cannot execute. Failed." + exit 1 +fi + +if [ "${TESTSRC}" = "" ] +then + echo "TESTSRC not set. Test cannot execute. Failed." + exit 1 +fi + +if [ "${TESTCLASSES}" = "" ] +then + echo "TESTCLASSES not set. Test cannot execute. Failed." + exit 1 +fi + +JAVAC="${TESTJAVA}"/bin/javac +JAVA="${TESTJAVA}"/bin/java + +cp "${TESTSRC}"/RedefineMethodAddInvokeTarget_1.java \ + RedefineMethodAddInvokeTarget.java +"${JAVAC}" -d . RedefineMethodAddInvokeTarget.java +mv RedefineMethodAddInvokeTarget.java RedefineMethodAddInvokeTarget_1.java +mv RedefineMethodAddInvokeTarget.class RedefineMethodAddInvokeTarget_1.class + +cp "${TESTSRC}"/RedefineMethodAddInvokeTarget_2.java \ + RedefineMethodAddInvokeTarget.java +"${JAVAC}" -d . RedefineMethodAddInvokeTarget.java +mv RedefineMethodAddInvokeTarget.java RedefineMethodAddInvokeTarget_2.java +mv RedefineMethodAddInvokeTarget.class RedefineMethodAddInvokeTarget_2.class + +"${JAVA}" ${TESTVMOPTS} -javaagent:RedefineMethodAddInvokeAgent.jar \ + -classpath "${TESTCLASSES}" RedefineMethodAddInvokeApp > output.log 2>&1 +cat output.log + +MESG="Exception" +grep "$MESG" output.log +result=$? +if [ "$result" = 0 ]; then + echo "FAIL: found '$MESG' in the test output" + result=1 +else + echo "PASS: did NOT find '$MESG' in the test output" + result=0 +fi + +exit $result diff --git a/jdk/test/java/lang/instrument/RedefineMethodAddInvokeAgent.java b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeAgent.java new file mode 100644 index 00000000000..dd0ab924ca3 --- /dev/null +++ b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeAgent.java @@ -0,0 +1,43 @@ +/* + * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +import java.lang.instrument.Instrumentation; + +public class RedefineMethodAddInvokeAgent { + private static Instrumentation instrumentation; + + private RedefineMethodAddInvokeAgent() { + } + + public static void premain(String agentArgs, Instrumentation inst) { + System.out.println("Hello from RedefineMethodAddInvokeAgent!"); + System.out.println("isRedefineClassesSupported()=" + + inst.isRedefineClassesSupported()); + + instrumentation = inst; + } + + public static Instrumentation getInstrumentation() { + return instrumentation; + } +} diff --git a/jdk/test/java/lang/instrument/RedefineMethodAddInvokeApp.java b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeApp.java new file mode 100644 index 00000000000..72b769388ba --- /dev/null +++ b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeApp.java @@ -0,0 +1,70 @@ +/* + * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +import java.io.*; +import java.lang.instrument.*; + +public class RedefineMethodAddInvokeApp { + public static void main(String args[]) throws Exception { + System.out.println("Hello from RedefineMethodAddInvokeApp!"); + + new RedefineMethodAddInvokeApp().doTest(); + + System.exit(0); + } + + private void doTest() throws Exception { + RedefineMethodAddInvokeTarget target = + new RedefineMethodAddInvokeTarget(); + + System.out.println("RedefineMethodAddInvokeApp: invoking myMethod()"); + target.test(0); // invoke the original myMethod() + + // add myMethod1() + do_redefine(1); + + System.out.println("RedefineMethodAddInvokeApp: invoking myMethod1()"); + target.test(1); // invoke myMethod1() + + // add myMethod2() + do_redefine(2); + + System.out.println("RedefineMethodAddInvokeApp: invoking myMethod2()"); + target.test(2); // invoke myMethod2() + } + + private static void do_redefine(int counter) throws Exception { + File f = new File("RedefineMethodAddInvokeTarget_" + counter + + ".class"); + System.out.println("Reading test class from " + f); + InputStream redefineStream = new FileInputStream(f); + + byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream); + + ClassDefinition redefineParamBlock = new ClassDefinition( + RedefineMethodAddInvokeTarget.class, redefineBuffer); + + RedefineMethodAddInvokeAgent.getInstrumentation().redefineClasses( + new ClassDefinition[] {redefineParamBlock}); + } +} diff --git a/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget.java b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget.java new file mode 100644 index 00000000000..a073ecb5233 --- /dev/null +++ b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget.java @@ -0,0 +1,37 @@ +/* + * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +import java.lang.reflect.Method; + +public class RedefineMethodAddInvokeTarget { + public void test(int counter) throws Exception { + Method method = getClass().getDeclaredMethod("myMethod" + + (counter == 0 ? "" : counter)); + method.setAccessible(true); + method.invoke(this); + } + + public void myMethod() { + System.out.println("Hello from the original myMethod()!"); + } +} diff --git a/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget_1.java b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget_1.java new file mode 100644 index 00000000000..8844cacfa8e --- /dev/null +++ b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget_1.java @@ -0,0 +1,43 @@ +/* + * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +import java.lang.reflect.Method; + +public class RedefineMethodAddInvokeTarget { + public void test(int counter) throws Exception { + Method method = getClass().getDeclaredMethod("myMethod" + + (counter == 0 ? "" : counter)); + method.setAccessible(true); + method.invoke(this); + } + + public void myMethod() { + System.out.println("Hello from the non-EMCP myMethod()!"); + } + + private final void myMethod1() { + System.out.println("Hello from myMethod1()!"); + System.out.println("Calling myMethod() from myMethod1():"); + myMethod(); + } +} diff --git a/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget_2.java b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget_2.java new file mode 100644 index 00000000000..b50700c960b --- /dev/null +++ b/jdk/test/java/lang/instrument/RedefineMethodAddInvokeTarget_2.java @@ -0,0 +1,49 @@ +/* + * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +import java.lang.reflect.Method; + +public class RedefineMethodAddInvokeTarget { + public void test(int counter) throws Exception { + Method method = getClass().getDeclaredMethod("myMethod" + + (counter == 0 ? "" : counter)); + method.setAccessible(true); + method.invoke(this); + } + + public void myMethod() { + System.out.println("Hello from the non-EMCP again myMethod()!"); + } + + private final void myMethod1() { + System.out.println("Hello from myMethod1()!"); + System.out.println("Calling myMethod() from myMethod1():"); + myMethod(); + } + + private final void myMethod2() { + System.out.println("Hello from myMethod2()!"); + System.out.println("Calling myMethod1() from myMethod2():"); + myMethod1(); + } +}