8188145: MethodHandle resolution should follow JVMS sequence of lookup by name & type before type descriptor resolution
authorvlivanov
Fri, 12 Jan 2018 01:52:06 +0300
changeset 48514 9608f7f41c4e
parent 48513 5b834ec96236
child 48515 0da9fb7d7d04
8188145: MethodHandle resolution should follow JVMS sequence of lookup by name & type before type descriptor resolution Reviewed-by: kvn, psandoz
src/hotspot/share/classfile/systemDictionary.cpp
src/hotspot/share/classfile/systemDictionary.hpp
src/hotspot/share/prims/methodHandles.cpp
src/hotspot/share/prims/methodHandles.hpp
src/java.base/share/classes/java/lang/invoke/MemberName.java
test/hotspot/jtreg/runtime/invokedynamic/MethodHandleConstantHelper.jasm
test/hotspot/jtreg/runtime/invokedynamic/MethodHandleConstantTest.java
--- a/src/hotspot/share/classfile/systemDictionary.cpp	Wed Jan 10 22:52:08 2018 -0500
+++ b/src/hotspot/share/classfile/systemDictionary.cpp	Fri Jan 12 01:52:06 2018 +0300
@@ -2734,43 +2734,58 @@
   return method_type;
 }
 
+Handle SystemDictionary::find_field_handle_type(Symbol* signature,
+                                                Klass* accessing_klass,
+                                                TRAPS) {
+  Handle empty;
+  ResourceMark rm(THREAD);
+  SignatureStream ss(signature, /*is_method=*/ false);
+  if (!ss.is_done()) {
+    Handle class_loader, protection_domain;
+    if (accessing_klass != NULL) {
+      class_loader      = Handle(THREAD, accessing_klass->class_loader());
+      protection_domain = Handle(THREAD, accessing_klass->protection_domain());
+    }
+    oop mirror = ss.as_java_mirror(class_loader, protection_domain, SignatureStream::NCDFError, CHECK_(empty));
+    ss.next();
+    if (ss.is_done()) {
+      return Handle(THREAD, mirror);
+    }
+  }
+  return empty;
+}
+
 // Ask Java code to find or construct a method handle constant.
 Handle SystemDictionary::link_method_handle_constant(Klass* caller,
                                                      int ref_kind, //e.g., JVM_REF_invokeVirtual
                                                      Klass* callee,
-                                                     Symbol* name_sym,
+                                                     Symbol* name,
                                                      Symbol* signature,
                                                      TRAPS) {
   Handle empty;
-  Handle name = java_lang_String::create_from_symbol(name_sym, CHECK_(empty));
-  Handle type;
-  if (signature->utf8_length() > 0 && signature->byte_at(0) == '(') {
-    type = find_method_handle_type(signature, caller, CHECK_(empty));
-  } else if (caller == NULL) {
-    // This should not happen.  JDK code should take care of that.
+  if (caller == NULL) {
     THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad MH constant", empty);
-  } else {
-    ResourceMark rm(THREAD);
-    SignatureStream ss(signature, false);
-    if (!ss.is_done()) {
-      oop mirror = ss.as_java_mirror(Handle(THREAD, caller->class_loader()),
-                                     Handle(THREAD, caller->protection_domain()),
-                                     SignatureStream::NCDFError, CHECK_(empty));
-      type = Handle(THREAD, mirror);
-      ss.next();
-      if (!ss.is_done())  type = Handle();  // error!
-    }
   }
-  if (type.is_null()) {
-    THROW_MSG_(vmSymbols::java_lang_LinkageError(), "bad signature", empty);
-  }
+  Handle name_str      = java_lang_String::create_from_symbol(name,      CHECK_(empty));
+  Handle signature_str = java_lang_String::create_from_symbol(signature, CHECK_(empty));
+
+  // Put symbolic info from the MH constant into freshly created MemberName and resolve it.
+  Handle mname = MemberName_klass()->allocate_instance_handle(CHECK_(empty));
+  java_lang_invoke_MemberName::set_clazz(mname(), callee->java_mirror());
+  java_lang_invoke_MemberName::set_name (mname(), name_str());
+  java_lang_invoke_MemberName::set_type (mname(), signature_str());
+  java_lang_invoke_MemberName::set_flags(mname(), MethodHandles::ref_kind_to_flags(ref_kind));
+  MethodHandles::resolve_MemberName(mname, caller, CHECK_(empty));
+
+  // After method/field resolution succeeded, it's safe to resolve MH signature as well.
+  Handle type = MethodHandles::resolve_MemberName_type(mname, caller, CHECK_(empty));
 
   // call java.lang.invoke.MethodHandleNatives::linkMethodHandleConstant(Class caller, int refKind, Class callee, String name, Object type) -> MethodHandle
   JavaCallArguments args;
   args.push_oop(Handle(THREAD, caller->java_mirror()));  // the referring class
   args.push_int(ref_kind);
   args.push_oop(Handle(THREAD, callee->java_mirror()));  // the target class
-  args.push_oop(name);
+  args.push_oop(name_str);
   args.push_oop(type);
   JavaValue result(T_OBJECT);
   JavaCalls::call_static(&result,
--- a/src/hotspot/share/classfile/systemDictionary.hpp	Wed Jan 10 22:52:08 2018 -0500
+++ b/src/hotspot/share/classfile/systemDictionary.hpp	Fri Jan 12 01:52:06 2018 +0300
@@ -532,6 +532,11 @@
                                            Klass* accessing_klass,
                                            TRAPS);
 
+  // find a java.lang.Class object for a given signature
+  static Handle    find_field_handle_type(Symbol* signature,
+                                          Klass* accessing_klass,
+                                          TRAPS);
+
   // ask Java to compute a java.lang.invoke.MethodHandle object for a given CP entry
   static Handle    link_method_handle_constant(Klass* caller,
                                                int ref_kind, //e.g., JVM_REF_invokeVirtual
--- a/src/hotspot/share/prims/methodHandles.cpp	Wed Jan 10 22:52:08 2018 -0500
+++ b/src/hotspot/share/prims/methodHandles.cpp	Fri Jan 12 01:52:06 2018 +0300
@@ -122,6 +122,48 @@
   ALL_KINDS      = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE
 };
 
+int MethodHandles::ref_kind_to_flags(int ref_kind) {
+  assert(ref_kind_is_valid(ref_kind), "%d", ref_kind);
+  int flags = (ref_kind << REFERENCE_KIND_SHIFT);
+  if (ref_kind_is_field(ref_kind)) {
+    flags |= IS_FIELD;
+  } else if (ref_kind_is_method(ref_kind)) {
+    flags |= IS_METHOD;
+  } else if (ref_kind == JVM_REF_newInvokeSpecial) {
+    flags |= IS_CONSTRUCTOR;
+  }
+  return flags;
+}
+
+Handle MethodHandles::resolve_MemberName_type(Handle mname, Klass* caller, TRAPS) {
+  Handle empty;
+  Handle type(THREAD, java_lang_invoke_MemberName::type(mname()));
+  if (!java_lang_String::is_instance_inlined(type())) {
+    return type; // already resolved
+  }
+  Symbol* signature = java_lang_String::as_symbol_or_null(type());
+  if (signature == NULL) {
+    return empty;  // no such signature exists in the VM
+  }
+  Handle resolved;
+  int flags = java_lang_invoke_MemberName::flags(mname());
+  switch (flags & ALL_KINDS) {
+    case IS_METHOD:
+    case IS_CONSTRUCTOR:
+      resolved = SystemDictionary::find_method_handle_type(signature, caller, CHECK_(empty));
+      break;
+    case IS_FIELD:
+      resolved = SystemDictionary::find_field_handle_type(signature, caller, CHECK_(empty));
+      break;
+    default:
+      THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty);
+  }
+  if (resolved.is_null()) {
+    THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad MemberName type", empty);
+  }
+  return resolved;
+}
+
 oop MethodHandles::init_MemberName(Handle mname, Handle target, TRAPS) {
   // This method is used from java.lang.invoke.MemberName constructors.
   // It fills in the new MemberName from a java.lang.reflect.Member.
--- a/src/hotspot/share/prims/methodHandles.hpp	Wed Jan 10 22:52:08 2018 -0500
+++ b/src/hotspot/share/prims/methodHandles.hpp	Fri Jan 12 01:52:06 2018 +0300
@@ -70,6 +70,8 @@
   static int find_MemberNames(Klass* k, Symbol* name, Symbol* sig,
                               int mflags, Klass* caller,
                               int skip, objArrayHandle results, TRAPS);
+  static Handle resolve_MemberName_type(Handle mname, Klass* caller, TRAPS);
+
   // bit values for suppress argument to expand_MemberName:
   enum { _suppress_defc = 1, _suppress_name = 2, _suppress_type = 4 };
 
@@ -191,6 +193,8 @@
             ref_kind == JVM_REF_invokeInterface);
   }
 
+  static int ref_kind_to_flags(int ref_kind);
+
 #include CPU_HEADER(methodHandles)
 
   // Tracing
--- a/src/java.base/share/classes/java/lang/invoke/MemberName.java	Wed Jan 10 22:52:08 2018 -0500
+++ b/src/java.base/share/classes/java/lang/invoke/MemberName.java	Fri Jan 12 01:52:06 2018 +0300
@@ -900,9 +900,9 @@
             buf.append(getName(clazz));
             buf.append('.');
         }
-        String name = getName();
+        String name = this.name; // avoid expanding from VM
         buf.append(name == null ? "*" : name);
-        Object type = getType();
+        Object type = this.type; // avoid expanding from VM
         if (!isInvocable()) {
             buf.append('/');
             buf.append(type == null ? "*" : getName(type));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/invokedynamic/MethodHandleConstantHelper.jasm	Fri Jan 12 01:52:06 2018 +0300
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2017, 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.
+ *
+ */
+
+package runtime/invokedynamic;
+
+super public class MethodHandleConstantHelper
+    version 51:0
+{
+
+static Field "testField":"I";
+
+static Method "testMethod":"()V"
+{
+    return;
+}
+
+public static Method "testMethodSignatureResolutionFailure":"()V"
+    stack 1 locals 1
+{
+          ldc MethodHandle REF_invokeStatic:
+                  MethodHandleConstantHelper.testMethod:
+                  "(LNotExist;)V";
+
+          return;
+}
+
+public static Method "testFieldSignatureResolutionFailure":"()V"
+    stack 1 locals 1
+{
+          ldc MethodHandle REF_getField:
+                  MethodHandleConstantHelper.testField:
+                  "LNotExist;";
+
+          return;
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/invokedynamic/MethodHandleConstantTest.java	Fri Jan 12 01:52:06 2018 +0300
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2017, 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 8188145
+ * @compile MethodHandleConstantHelper.jasm
+ * @run main runtime.invokedynamic.MethodHandleConstantTest
+ */
+package runtime.invokedynamic;
+
+import java.lang.invoke.*;
+
+public class MethodHandleConstantTest {
+    static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
+    static final MethodType TEST_MT = MethodType.methodType(void.class);
+    static final Class<?> TEST_CLASS;
+
+    static {
+       try {
+          TEST_CLASS = Class.forName("runtime.invokedynamic.MethodHandleConstantHelper");
+       } catch (ClassNotFoundException e) {
+           throw new Error(e);
+       }
+    }
+
+    static void test(String testName, Class<? extends Throwable> expectedError) {
+        System.out.print(testName);
+        try {
+            LOOKUP.findStatic(TEST_CLASS, testName, TEST_MT).invokeExact();
+        } catch (Throwable e) {
+            if (expectedError.isInstance(e)) {
+                // expected
+            } else {
+                e.printStackTrace();
+                String msg = String.format("%s: wrong exception: %s, but %s expected",
+                                           testName, e.getClass().getName(), expectedError.getName());
+                throw new AssertionError(msg);
+            }
+        }
+        System.out.println(": PASSED");
+    }
+
+    public static void main(String[] args) throws Throwable {
+        test("testMethodSignatureResolutionFailure", NoSuchMethodError.class);
+        test("testFieldSignatureResolutionFailure",  NoSuchFieldError.class);
+    }
+}