8211065: Private method check in linkResolver is incorrect
authordholmes
Tue, 09 Oct 2018 20:19:22 -0400
changeset 52066 49a21be61dcd
parent 52065 dea8a62cdfc3
child 52067 2e72562697bf
8211065: Private method check in linkResolver is incorrect Reviewed-by: acorn, lfoltan
src/hotspot/share/interpreter/linkResolver.cpp
test/hotspot/jtreg/runtime/Nestmates/privateMethods/TestInvokeErrors.java
test/hotspot/jtreg/runtime/linkResolver/TestDeletedMethod.java
test/hotspot/jtreg/runtime/linkResolver/TestDeletedMethod_Sub.jcod
test/hotspot/jtreg/runtime/linkResolver/TestDeletedMethod_Super.jcod
--- a/src/hotspot/share/interpreter/linkResolver.cpp	Tue Oct 09 14:57:23 2018 -0700
+++ b/src/hotspot/share/interpreter/linkResolver.cpp	Tue Oct 09 20:19:22 2018 -0400
@@ -793,24 +793,6 @@
     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
   }
 
-  // For private method invocation we should only find the method in the resolved class.
-  // If that is not the case then we have a found a supertype method that we have nestmate
-  // access to.
-  if (resolved_method->is_private() && resolved_method->method_holder() != resolved_klass) {
-    ResourceMark rm(THREAD);
-    DEBUG_ONLY(bool is_nestmate = InstanceKlass::cast(link_info.current_klass())->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD);)
-    assert(is_nestmate, "was only expecting nestmates to get here!");
-    Exceptions::fthrow(
-      THREAD_AND_LOCATION,
-      vmSymbols::java_lang_NoSuchMethodError(),
-      "%s: method %s%s not found",
-      resolved_klass->external_name(),
-      resolved_method->name()->as_C_string(),
-      resolved_method->signature()->as_C_string()
-    );
-    return NULL;
-  }
-
   return resolved_method;
 }
 
--- a/test/hotspot/jtreg/runtime/Nestmates/privateMethods/TestInvokeErrors.java	Tue Oct 09 14:57:23 2018 -0700
+++ b/test/hotspot/jtreg/runtime/Nestmates/privateMethods/TestInvokeErrors.java	Tue Oct 09 20:19:22 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8046171
+ * @bug 8046171 8211065
  * @summary Setup nestmate calls to private methods then use
  *          modified jcod classes to introduce errors. Test with
  *          and without verification enabled
@@ -96,14 +96,10 @@
             System.out.println("Got expected exception:" + nsme);
         }
 
-        try {
-            MissingMethodWithSuper m = new MissingMethodWithSuper();
-            m.priv_invoke();
-            throw new Error("Unexpected success invoking MissingMethodWithSuper.priv_invoke");
-        }
-        catch (NoSuchMethodError nsme) {
-            System.out.println("Got expected exception:" + nsme);
-        }
+        // This test was revised to expect successful invocation of the
+        // super class method - see JDK-8211065
+        MissingMethodWithSuper m = new MissingMethodWithSuper();
+        m.priv_invoke();
 
         // Verification of Helper will trigger the nestmate access check failure
         try {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/linkResolver/TestDeletedMethod.java	Tue Oct 09 20:19:22 2018 -0400
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2018, 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 8211065
+ * @summary Test that deleting a subclass method implementation results in
+ * execution of a superclass implementation - if it is accessible.
+ * @compile TestDeletedMethod.java
+ * @compile TestDeletedMethod_Sub.jcod TestDeletedMethod_Super.jcod
+ * @run main/othervm -XX:+RelaxAccessControlCheck TestDeletedMethod
+ */
+
+// The access control relaxation was originally done to ensure an assertion
+// in the nestmate logic would not trigger unintentionally because it
+// assumed only nestmates could be involved. The assertion no longer exists
+// but we keep the test as a non-nestmate version of the situation.
+
+/* package */ class TestDeletedMethod_Super {
+    public static final int ID = 2;
+    private static int m() {
+        System.out.println("Super.m");
+        return ID;
+    }
+}
+
+/* package */ class TestDeletedMethod_Sub extends TestDeletedMethod_Super {
+    public static final int ID = 1;
+    // At runtime this implementation is not present
+    private static int m() {
+        System.out.println("Sub.m");
+        return ID;
+    }
+    public static int test() {
+        return TestDeletedMethod_Sub.m();
+    }
+}
+
+public class TestDeletedMethod {
+    public static void main(String[] args) {
+        int x = TestDeletedMethod_Sub.test();
+        if (x != TestDeletedMethod_Super.ID)
+            throw new RuntimeException("Wrong method invoked: " + x);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/linkResolver/TestDeletedMethod_Sub.jcod	Tue Oct 09 20:19:22 2018 -0400
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2018, 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.
+ */
+
+// We have deleted the local method m() by renaming it
+// We have set the class file version to 49 to allow relaxed access checks
+
+class TestDeletedMethod_Sub {
+  0xCAFEBABE;
+  0; // minor version
+  49; // version
+  [] { // Constant Pool
+    ; // first element is empty
+    Method #7 #21; // #1
+    Field #22 #23; // #2
+    String #24; // #3
+    Method #25 #26; // #4
+    class #27; // #5
+    Method #5 #28; // #6
+    class #29; // #7
+    Utf8 "ID"; // #8
+    Utf8 "I"; // #9
+    Utf8 "ConstantValue"; // #10
+    int 0x00000001; // #11
+    Utf8 "<init>"; // #12
+    Utf8 "()V"; // #13
+    Utf8 "Code"; // #14
+    Utf8 "LineNumberTable"; // #15
+    Utf8 "m"; // #16
+    Utf8 "()I"; // #17
+    Utf8 "test"; // #18
+    Utf8 "SourceFile"; // #19
+    Utf8 "TestDeletedMethod.java"; // #20
+    NameAndType #12 #13; // #21
+    class #30; // #22
+    NameAndType #31 #32; // #23
+    Utf8 "Sub.m"; // #24
+    class #33; // #25
+    NameAndType #34 #35; // #26
+    Utf8 "TestDeletedMethod_Sub"; // #27
+    NameAndType #16 #17; // #28
+    Utf8 "TestDeletedMethod_Super"; // #29
+    Utf8 "java/lang/System"; // #30
+    Utf8 "out"; // #31
+    Utf8 "Ljava/io/PrintStream;"; // #32
+    Utf8 "java/io/PrintStream"; // #33
+    Utf8 "println"; // #34
+    Utf8 "(Ljava/lang/String;)V"; // #35
+    Utf8 "m_renamed"; // #36 added
+  } // Constant Pool
+
+  0x0020; // access
+  #5;// this_cpx
+  #7;// super_cpx
+
+  [] { // Interfaces
+  } // Interfaces
+
+  [] { // fields
+    { // Member
+      0x0019; // access
+      #8; // name_cpx
+      #9; // sig_cpx
+      [] { // Attributes
+        Attr(#10) { // ConstantValue
+          #11;
+        } // end ConstantValue
+      } // Attributes
+    } // Member
+  } // fields
+
+  [] { // methods
+    { // Member
+      0x0000; // access
+      #12; // name_cpx
+      #13; // sig_cpx
+      [] { // Attributes
+        Attr(#14) { // Code
+          1; // max_stack
+          1; // max_locals
+          Bytes[]{
+            0x2AB70001B1;
+          };
+          [] { // Traps
+          } // end Traps
+          [] { // Attributes
+            Attr(#15) { // LineNumberTable
+              [] { // LineNumberTable
+                0  43;
+              }
+            } // end LineNumberTable
+          } // Attributes
+        } // end Code
+      } // Attributes
+    } // Member
+    ;
+    { // Member
+      0x000A; // access
+      #36; // name_cpx UPDATED to rename method
+      #17; // sig_cpx
+      [] { // Attributes
+        Attr(#14) { // Code
+          2; // max_stack
+          0; // max_locals
+          Bytes[]{
+            0xB200021203B60004;
+            0x04AC;
+          };
+          [] { // Traps
+          } // end Traps
+          [] { // Attributes
+            Attr(#15) { // LineNumberTable
+              [] { // LineNumberTable
+                0  47;
+                8  48;
+              }
+            } // end LineNumberTable
+          } // Attributes
+        } // end Code
+      } // Attributes
+    } // Member
+    ;
+    { // Member
+      0x0009; // access
+      #18; // name_cpx
+      #17; // sig_cpx
+      [] { // Attributes
+        Attr(#14) { // Code
+          1; // max_stack
+          0; // max_locals
+          Bytes[]{
+            0xB80006AC;
+          };
+          [] { // Traps
+          } // end Traps
+          [] { // Attributes
+            Attr(#15) { // LineNumberTable
+              [] { // LineNumberTable
+                0  51;
+              }
+            } // end LineNumberTable
+          } // Attributes
+        } // end Code
+      } // Attributes
+    } // Member
+  } // methods
+
+  [] { // Attributes
+    Attr(#19) { // SourceFile
+      #20;
+    } // end SourceFile
+  } // Attributes
+} // end class TestDeletedMethod_Sub
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/linkResolver/TestDeletedMethod_Super.jcod	Tue Oct 09 20:19:22 2018 -0400
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2018, 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.
+ */
+
+// We have set the class file version to 49 to allow relaxed access checks
+
+class TestDeletedMethod_Super {
+  0xCAFEBABE;
+  0; // minor version
+  49; // version
+  [] { // Constant Pool
+    ; // first element is empty
+    Method #6 #19; // #1
+    Field #20 #21; // #2
+    String #22; // #3
+    Method #23 #24; // #4
+    class #25; // #5
+    class #26; // #6
+    Utf8 "ID"; // #7
+    Utf8 "I"; // #8
+    Utf8 "ConstantValue"; // #9
+    int 0x00000002; // #10
+    Utf8 "<init>"; // #11
+    Utf8 "()V"; // #12
+    Utf8 "Code"; // #13
+    Utf8 "LineNumberTable"; // #14
+    Utf8 "m"; // #15
+    Utf8 "()I"; // #16
+    Utf8 "SourceFile"; // #17
+    Utf8 "TestDeletedMethod.java"; // #18
+    NameAndType #11 #12; // #19
+    class #27; // #20
+    NameAndType #28 #29; // #21
+    Utf8 "Super.m"; // #22
+    class #30; // #23
+    NameAndType #31 #32; // #24
+    Utf8 "TestDeletedMethod_Super"; // #25
+    Utf8 "java/lang/Object"; // #26
+    Utf8 "java/lang/System"; // #27
+    Utf8 "out"; // #28
+    Utf8 "Ljava/io/PrintStream;"; // #29
+    Utf8 "java/io/PrintStream"; // #30
+    Utf8 "println"; // #31
+    Utf8 "(Ljava/lang/String;)V"; // #32
+  } // Constant Pool
+
+  0x0020; // access
+  #5;// this_cpx
+  #6;// super_cpx
+
+  [] { // Interfaces
+  } // Interfaces
+
+  [] { // fields
+    { // Member
+      0x0019; // access
+      #7; // name_cpx
+      #8; // sig_cpx
+      [] { // Attributes
+        Attr(#9) { // ConstantValue
+          #10;
+        } // end ConstantValue
+      } // Attributes
+    } // Member
+  } // fields
+
+  [] { // methods
+    { // Member
+      0x0000; // access
+      #11; // name_cpx
+      #12; // sig_cpx
+      [] { // Attributes
+        Attr(#13) { // Code
+          1; // max_stack
+          1; // max_locals
+          Bytes[]{
+            0x2AB70001B1;
+          };
+          [] { // Traps
+          } // end Traps
+          [] { // Attributes
+            Attr(#14) { // LineNumberTable
+              [] { // LineNumberTable
+                0  35;
+              }
+            } // end LineNumberTable
+          } // Attributes
+        } // end Code
+      } // Attributes
+    } // Member
+    ;
+    { // Member
+      0x000A; // access
+      #15; // name_cpx
+      #16; // sig_cpx
+      [] { // Attributes
+        Attr(#13) { // Code
+          2; // max_stack
+          0; // max_locals
+          Bytes[]{
+            0xB200021203B60004;
+            0x05AC;
+          };
+          [] { // Traps
+          } // end Traps
+          [] { // Attributes
+            Attr(#14) { // LineNumberTable
+              [] { // LineNumberTable
+                0  38;
+                8  39;
+              }
+            } // end LineNumberTable
+          } // Attributes
+        } // end Code
+      } // Attributes
+    } // Member
+  } // methods
+
+  [] { // Attributes
+    Attr(#17) { // SourceFile
+      #18;
+    } // end SourceFile
+  } // Attributes
+} // end class TestDeletedMethod_Super