8192936: RI does not follow the JVMTI RedefineClasses spec that is too strict in the definition

Introduce new flag fo compatibility: -XX:AllowRedefinitionToAddOrDeleteMethods

Reviewed-by: jcbeyler, sspitsyn
This commit is contained in:
Coleen Phillimore 2019-04-19 21:49:54 -07:00
parent a8a29bbae6
commit 6ebf2ce655
14 changed files with 184 additions and 24 deletions

View File

@ -787,6 +787,12 @@ static jvmtiError check_nest_attributes(InstanceKlass* the_class,
return JVMTI_ERROR_NONE;
}
static bool can_add_or_delete(Method* m) {
// Compatibility mode
return (AllowRedefinitionToAddDeleteMethods &&
(m->is_private() && (m->is_static() || m->is_final())));
}
jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
InstanceKlass* the_class,
InstanceKlass* scratch_class) {
@ -992,12 +998,7 @@ jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
break;
case added:
// method added, see if it is OK
new_flags = (jushort) k_new_method->access_flags().get_flags();
if ((new_flags & JVM_ACC_PRIVATE) == 0
// hack: private should be treated as final, but alas
|| (new_flags & (JVM_ACC_FINAL|JVM_ACC_STATIC)) == 0
) {
// new methods must be private
if (!can_add_or_delete(k_new_method)) {
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
}
{
@ -1026,12 +1027,7 @@ jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
break;
case deleted:
// method deleted, see if it is OK
old_flags = (jushort) k_old_method->access_flags().get_flags();
if ((old_flags & JVM_ACC_PRIVATE) == 0
// hack: private should be treated as final, but alas
|| (old_flags & (JVM_ACC_FINAL|JVM_ACC_STATIC)) == 0
) {
// deleted methods must be private
if (!can_add_or_delete(k_old_method)) {
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED;
}
log_trace(redefine, class, normalize)

View File

@ -540,6 +540,7 @@ static SpecialFlag const special_jvm_flags[] = {
{ "FailOverToOldVerifier", JDK_Version::jdk(13), JDK_Version::jdk(14), JDK_Version::undefined() },
{ "AllowJNIEnvProxy", JDK_Version::jdk(13), JDK_Version::jdk(14), JDK_Version::jdk(15) },
{ "ThreadLocalHandshakes", JDK_Version::jdk(13), JDK_Version::jdk(14), JDK_Version::jdk(15) },
{ "AllowRedefinitionToAddDeleteMethods", JDK_Version::jdk(13), JDK_Version::undefined(), JDK_Version::undefined() },
// --- Deprecated alias flags (see also aliased_jvm_flags) - sorted by obsolete_in then expired_in:
{ "DefaultMaxRAMFraction", JDK_Version::jdk(8), JDK_Version::undefined(), JDK_Version::undefined() },

View File

@ -978,6 +978,10 @@ define_pd_global(uint64_t,MaxRAM, 1ULL*G);
product(bool, VerifyMergedCPBytecodes, true, \
"Verify bytecodes after RedefineClasses constant pool merging") \
\
product(bool, AllowRedefinitionToAddDeleteMethods, false, \
"Allow redefinition to add and delete private static or " \
"final methods for compatibility with old releases") \
\
develop(bool, TraceBytecodes, false, \
"Trace bytecode execution") \
\

View File

@ -31,7 +31,7 @@
* java.instrument
* jdk.jartool/sun.tools.jar
* @run main RedefineClassHelper
* @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class*=trace RedefineAddLambdaExpression
* @run main/othervm -javaagent:redefineagent.jar -XX:+AllowRedefinitionToAddDeleteMethods -Xlog:redefine+class*=trace RedefineAddLambdaExpression
*/
interface MathOperation {

View File

@ -31,7 +31,7 @@
* java.instrument
* jdk.jartool/sun.tools.jar
* @run main RedefineClassHelper
* @run main/native/othervm -javaagent:redefineagent.jar -Xlog:redefine+class*=trace RedefineDeleteJmethod
* @run main/native/othervm -javaagent:redefineagent.jar -XX:+AllowRedefinitionToAddDeleteMethods -Xlog:redefine+class*=trace RedefineDeleteJmethod
*/
class B {

View File

@ -31,7 +31,7 @@
* java.instrument
* jdk.jartool/sun.tools.jar
* @run main RedefineClassHelper
* @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class*=trace RedefineSubtractLambdaExpression
* @run main/othervm -javaagent:redefineagent.jar -XX:+AllowRedefinitionToAddDeleteMethods -Xlog:redefine+class*=trace RedefineSubtractLambdaExpression
*/
interface MathOperation {

View File

@ -0,0 +1,138 @@
/*
* Copyright (c) 2019, 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 8192936
* @summary RI does not follow the JVMTI RedefineClasses spec; need to disallow adding and deleting methods
* @library /test/lib
* @modules java.base/jdk.internal.misc
* @modules java.compiler
* java.instrument
* jdk.jartool/sun.tools.jar
* @run main RedefineClassHelper
* @run main/othervm -javaagent:redefineagent.jar TestAddDeleteMethods
*/
import static jdk.test.lib.Asserts.assertEquals;
// package access top-level class to avoid problem with RedefineClassHelper
// and nested types.
class A {
private static void foo() { System.out.println("OLD foo called"); }
private final void finalFoo() { System.out.println("OLD finalFoo called"); }
public void publicFoo() { foo(); finalFoo(); }
}
public class TestAddDeleteMethods {
static A a;
public static String newA =
"class A {" +
"private static void foo() { System.out.println(\"NEW foo called\"); }" +
"private final void finalFoo() { System.out.println(\"NEW finalFoo called\"); }" +
"public void publicFoo() { foo(); finalFoo(); }" +
"}";
public static String newAddBar =
"class A {" +
"private void bar() { System.out.println(\"NEW bar called\"); }" +
"private static void foo() { System.out.println(\"NEW foo called\"); }" +
"private final void finalFoo() { System.out.println(\"NEW finalFoo called\"); }" +
"public void publicFoo() { foo(); bar(); finalFoo(); }" +
"}";
public static String newAddFinalBar =
"class A {" +
"private final void bar() { System.out.println(\"NEW bar called\"); }" +
"private static void foo() { System.out.println(\"NEW foo called\"); }" +
"private final void finalFoo() { System.out.println(\"NEW finalFoo called\"); }" +
"public void publicFoo() { foo(); bar(); finalFoo(); }" +
"}";
public static String newAddPublicBar =
"class A {" +
"public void bar() { System.out.println(\"NEW public bar called\"); }" +
"private static void foo() { System.out.println(\"NEW foo called\"); }" +
"private final void finalFoo() { System.out.println(\"NEW finalFoo called\"); }" +
"public void publicFoo() { foo(); bar(); finalFoo(); }" +
"}";
public static String newDeleteFoo =
"class A {" +
"private final void finalFoo() { System.out.println(\"NEW finalFoo called\"); }" +
"public void publicFoo() { finalFoo(); }" +
"}";
public static String newDeleteFinalFoo =
"class A {" +
"private static void foo() { System.out.println(\"NEW foo called\"); }" +
"public void publicFoo() { foo(); }" +
"}";
public static String newDeletePublicFoo =
"class A {" +
"private static void foo() { System.out.println(\"NEW foo called\"); }" +
"private final void finalFoo() { System.out.println(\"NEW finalFoo called\"); }" +
"}";
static private final String ExpMsgPrefix = "attempted to ";
static private final String ExpMsgPostfix = " a method";
public static void test(String newBytes, String expSuffix) throws Exception {
String expectedMessage = ExpMsgPrefix + expSuffix + ExpMsgPostfix;
try {
RedefineClassHelper.redefineClass(A.class, newBytes);
a.publicFoo();
throw new RuntimeException("Failed, expected UOE");
} catch (UnsupportedOperationException uoe) {
String message = uoe.getMessage();
System.out.println("Got expected UOE " + message);
if (!message.endsWith(expectedMessage)) {
throw new RuntimeException("Expected UOE error message to end with: " + expectedMessage);
}
}
}
static {
a = new A();
}
public static void main(String[] args) throws Exception {
a.publicFoo();
// Should pass because this only changes bytes of methods.
RedefineClassHelper.redefineClass(A.class, newA);
a.publicFoo();
// Add private static bar
test(newAddBar, "add");
test(newAddFinalBar, "add");
test(newAddPublicBar, "add");
test(newDeleteFoo, "delete");
test(newDeleteFinalFoo, "delete");
test(newDeletePublicFoo, "delete");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2019, 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
@ -46,8 +46,12 @@ class RedefineAddPrivateMethodTarg {
}
public class RedefineAddPrivateMethod extends JdbTest {
static private final String ALLOW_ADD_DELETE_OPTION = "-XX:+AllowRedefinitionToAddDeleteMethods";
public static void main(String argv[]) {
new RedefineAddPrivateMethod().run();
RedefineAddPrivateMethod test = new RedefineAddPrivateMethod();
test.launchOptions.addVMOptions(ALLOW_ADD_DELETE_OPTION);
test.run();
}
private RedefineAddPrivateMethod() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, 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
@ -65,6 +65,7 @@ public class Debuggee implements Closeable {
public static class Launcher {
private final String mainClass;
private final List<String> options = new LinkedList<>();
private String vmOptions = null;
private String transport = "dt_socket";
private String address = null;
private boolean suspended = true;
@ -81,6 +82,10 @@ public class Debuggee implements Closeable {
this.options.addAll(options);
return this;
}
public Launcher addVMOptions(String vmOptions) {
this.vmOptions = vmOptions;
return this;
}
// default is "dt_socket"
public Launcher setTransport(String value) {
transport = value;
@ -104,6 +109,9 @@ public class Debuggee implements Closeable {
public ProcessBuilder prepare() {
List<String> debuggeeArgs = new LinkedList<>();
if (vmOptions != null) {
debuggeeArgs.add(vmOptions);
}
debuggeeArgs.add("-agentlib:jdwp=transport=" + transport
+ (address == null ? "" : ",address=" + address)
+ ",server=y,suspend=" + (suspended ? "y" : "n"));

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, 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
@ -40,6 +40,7 @@ public abstract class JdbTest {
public final String debuggeeClass;
public final List<String> debuggeeOptions = new LinkedList<>();
public String sourceFilename;
public String vmOptions = null;
public LaunchOptions(String debuggeeClass) {
this.debuggeeClass = debuggeeClass;
@ -56,6 +57,10 @@ public abstract class JdbTest {
sourceFilename = name;
return this;
}
public LaunchOptions addVMOptions(String vmOptions) {
this.vmOptions = vmOptions;
return this;
}
}
public JdbTest(LaunchOptions launchOptions) {
@ -72,7 +77,7 @@ public abstract class JdbTest {
protected Jdb jdb;
protected Debuggee debuggee;
private final LaunchOptions launchOptions;
protected LaunchOptions launchOptions;
// returns the whole jdb output as a string
public String getJdbOutput() {
@ -108,6 +113,7 @@ public abstract class JdbTest {
// launch debuggee
debuggee = Debuggee.launcher(launchOptions.debuggeeClass)
.addOptions(launchOptions.debuggeeOptions)
.addVMOptions(launchOptions.vmOptions)
.launch();
// launch jdb

View File

@ -33,7 +33,7 @@
* @compile ../../NamedBuffer.java
* @compile redef/Xost.java
* @run main RedefineClassHelper
* @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class+update*=debug,membername+table=debug MethodHandleDeletedMethod
* @run main/othervm -XX:+AllowRedefinitionToAddDeleteMethods -javaagent:redefineagent.jar -Xlog:redefine+class+update*=debug,membername+table=debug MethodHandleDeletedMethod
*/
import java.io.File;

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2008, 2019, 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
@ -71,6 +71,7 @@ mv RedefineMethodAddInvokeTarget.java RedefineMethodAddInvokeTarget_2.java
mv RedefineMethodAddInvokeTarget.class RedefineMethodAddInvokeTarget_2.class
"${JAVA}" ${TESTVMOPTS} -javaagent:RedefineMethodAddInvokeAgent.jar \
-XX:+AllowRedefinitionToAddDeleteMethods \
-classpath "${TESTCLASSES}" RedefineMethodAddInvokeApp > output.log 2>&1
cat output.log

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2014, 2019, 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
@ -72,6 +72,7 @@ mv RedefineMethodDelInvokeTarget.java RedefineMethodDelInvokeTarget_2.java
mv RedefineMethodDelInvokeTarget.class RedefineMethodDelInvokeTarget_2.class
"${JAVA}" ${TESTVMOPTS} -javaagent:RedefineMethodDelInvokeAgent.jar \
-XX:+AllowRedefinitionToAddDeleteMethods \
-classpath "${TESTCLASSES}" RedefineMethodDelInvokeApp > output.log 2>&1
result=$?

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2013, 2019, 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
@ -69,6 +69,7 @@ cp "${TESTSRC}"/RedefineMethodInBacktraceTargetB_2.java \
"${JAVAC}" ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d . RedefineMethodInBacktraceTargetB.java
"${JAVA}" ${TESTVMOPTS} -javaagent:RedefineMethodInBacktraceAgent.jar \
-XX:+AllowRedefinitionToAddDeleteMethods \
-classpath "${TESTCLASSES}" RedefineMethodInBacktraceApp > output.log 2>&1
RUN_RESULT=$?