diff --git a/hotspot/src/share/vm/classfile/systemDictionary.cpp b/hotspot/src/share/vm/classfile/systemDictionary.cpp index 6ccbb21e6343b06880de9a15f6e5690253002a27..23c4434d0febfff6a36141f54de14449a801631d 100644 --- a/hotspot/src/share/vm/classfile/systemDictionary.cpp +++ b/hotspot/src/share/vm/classfile/systemDictionary.cpp @@ -2343,12 +2343,9 @@ char* SystemDictionary::check_signature_loaders(symbolHandle signature, methodOop SystemDictionary::find_method_handle_invoke(symbolHandle name, symbolHandle signature, - Handle class_loader, - Handle protection_domain, + KlassHandle accessing_klass, TRAPS) { if (!EnableMethodHandles) return NULL; - assert(class_loader.is_null() && protection_domain.is_null(), - "cannot load specialized versions of MethodHandle.invoke"); if (invoke_method_table() == NULL) { // create this side table lazily _invoke_method_table = new SymbolPropertyTable(_invoke_method_size); @@ -2358,30 +2355,36 @@ methodOop SystemDictionary::find_method_handle_invoke(symbolHandle name, unsigned int hash = invoke_method_table()->compute_hash(signature, name_id); int index = invoke_method_table()->hash_to_index(hash); SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, name_id); + methodHandle non_cached_result; if (spe == NULL || spe->property_oop() == NULL) { + spe = NULL; // Must create lots of stuff here, but outside of the SystemDictionary lock. if (THREAD->is_Compiler_thread()) return NULL; // do not attempt from within compiler - Handle mt = find_method_handle_type(signature(), - class_loader, protection_domain, - CHECK_NULL); + bool found_on_bcp = false; + Handle mt = find_method_handle_type(signature(), accessing_klass, found_on_bcp, CHECK_NULL); KlassHandle mh_klass = SystemDictionaryHandles::MethodHandle_klass(); methodHandle m = methodOopDesc::make_invoke_method(mh_klass, name, signature, mt, CHECK_NULL); // Now grab the lock. We might have to throw away the new method, // if a racing thread has managed to install one at the same time. - { + if (found_on_bcp) { MutexLocker ml(SystemDictionary_lock, Thread::current()); spe = invoke_method_table()->find_entry(index, hash, signature, name_id); if (spe == NULL) spe = invoke_method_table()->add_entry(index, hash, signature, name_id); if (spe->property_oop() == NULL) spe->set_property_oop(m()); + } else { + non_cached_result = m; } } - methodOop m = (methodOop) spe->property_oop(); - assert(m->is_method(), ""); - return m; + if (spe != NULL && spe->property_oop() != NULL) { + assert(spe->property_oop()->is_method(), ""); + return (methodOop) spe->property_oop(); + } else { + return non_cached_result(); + } } // Ask Java code to find or construct a java.dyn.MethodType for the given @@ -2389,30 +2392,50 @@ methodOop SystemDictionary::find_method_handle_invoke(symbolHandle name, // Because of class loader constraints, all method handle usage must be // consistent with this loader. Handle SystemDictionary::find_method_handle_type(symbolHandle signature, - Handle class_loader, - Handle protection_domain, + KlassHandle accessing_klass, + bool& return_bcp_flag, TRAPS) { + Handle class_loader, protection_domain; + bool is_on_bcp = true; // keep this true as long as we can materialize from the boot classloader Handle empty; int npts = ArgumentCount(signature()).size(); objArrayHandle pts = oopFactory::new_objArray(SystemDictionary::Class_klass(), npts, CHECK_(empty)); int arg = 0; Handle rt; // the return type from the signature for (SignatureStream ss(signature()); !ss.is_done(); ss.next()) { - oop mirror; - if (!ss.is_object()) { - mirror = Universe::java_mirror(ss.type()); - } else { - symbolOop name_oop = ss.as_symbol(CHECK_(empty)); - symbolHandle name(THREAD, name_oop); - klassOop klass = resolve_or_fail(name, - class_loader, protection_domain, - true, CHECK_(empty)); - mirror = Klass::cast(klass)->java_mirror(); + oop mirror = NULL; + if (is_on_bcp) { + mirror = ss.as_java_mirror(class_loader, protection_domain, + SignatureStream::ReturnNull, CHECK_(empty)); + if (mirror == NULL) { + // fall back from BCP to accessing_klass + if (accessing_klass.not_null()) { + class_loader = Handle(THREAD, instanceKlass::cast(accessing_klass())->class_loader()); + protection_domain = Handle(THREAD, instanceKlass::cast(accessing_klass())->protection_domain()); + } + is_on_bcp = false; + } + } + if (!is_on_bcp) { + // Resolve, throwing a real error if it doesn't work. + mirror = ss.as_java_mirror(class_loader, protection_domain, + SignatureStream::NCDFError, CHECK_(empty)); } if (ss.at_return_type()) rt = Handle(THREAD, mirror); else pts->obj_at_put(arg++, mirror); + // Check accessibility. + if (ss.is_object() && accessing_klass.not_null()) { + klassOop sel_klass = java_lang_Class::as_klassOop(mirror); + // Emulate constantPoolOopDesc::verify_constant_pool_resolve. + if (Klass::cast(sel_klass)->oop_is_objArray()) + sel_klass = objArrayKlass::cast(sel_klass)->bottom_klass(); + if (Klass::cast(sel_klass)->oop_is_instance()) { + KlassHandle sel_kh(THREAD, sel_klass); + LinkResolver::check_klass_accessability(accessing_klass, sel_kh, CHECK_(empty)); + } + } } assert(arg == npts, ""); @@ -2425,6 +2448,9 @@ Handle SystemDictionary::find_method_handle_type(symbolHandle signature, vmSymbols::findMethodHandleType_name(), vmSymbols::findMethodHandleType_signature(), &args, CHECK_(empty)); + + // report back to the caller with the MethodType and the "on_bcp" flag + return_bcp_flag = is_on_bcp; return Handle(THREAD, (oop) result.get_jobject()); } diff --git a/hotspot/src/share/vm/classfile/systemDictionary.hpp b/hotspot/src/share/vm/classfile/systemDictionary.hpp index 0d88d4206fdf8710c9b2946f0b02c1523b6dc14f..bd31fbb4957853cbfb98549d565cf1309d522381 100644 --- a/hotspot/src/share/vm/classfile/systemDictionary.hpp +++ b/hotspot/src/share/vm/classfile/systemDictionary.hpp @@ -466,13 +466,12 @@ public: // find the java.dyn.MethodHandles::invoke method for a given signature static methodOop find_method_handle_invoke(symbolHandle name, symbolHandle signature, - Handle class_loader, - Handle protection_domain, + KlassHandle accessing_klass, TRAPS); // ask Java to compute a java.dyn.MethodType object for a given signature static Handle find_method_handle_type(symbolHandle signature, - Handle class_loader, - Handle protection_domain, + KlassHandle accessing_klass, + bool& return_bcp_flag, TRAPS); // ask Java to create a dynamic call site, while linking an invokedynamic op static Handle make_dynamic_call_site(Handle bootstrap_method, diff --git a/hotspot/src/share/vm/interpreter/linkResolver.cpp b/hotspot/src/share/vm/interpreter/linkResolver.cpp index d80ef9daf5218a4bf0c548e0acd6d11432c2ddb1..1dcea5db93f04f5d44135efd9befa0b3ddf82e8a 100644 --- a/hotspot/src/share/vm/interpreter/linkResolver.cpp +++ b/hotspot/src/share/vm/interpreter/linkResolver.cpp @@ -172,14 +172,16 @@ void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name(), signature())); } -void LinkResolver::lookup_implicit_method(methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS) { +void LinkResolver::lookup_implicit_method(methodHandle& result, + KlassHandle klass, symbolHandle name, symbolHandle signature, + KlassHandle current_klass, + TRAPS) { if (EnableMethodHandles && MethodHandles::enabled() && klass() == SystemDictionary::MethodHandle_klass() && methodOopDesc::is_method_handle_invoke_name(name())) { methodOop result_oop = SystemDictionary::find_method_handle_invoke(name, signature, - Handle(), - Handle(), + current_klass, CHECK); if (result_oop != NULL) { assert(result_oop->is_method_handle_invoke() && result_oop->signature() == signature(), "consistent"); @@ -290,7 +292,7 @@ void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle res if (resolved_method.is_null()) { // JSR 292: see if this is an implicitly generated method MethodHandle.invoke(*...) - lookup_implicit_method(resolved_method, resolved_klass, method_name, method_signature, CHECK); + lookup_implicit_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, CHECK); } if (resolved_method.is_null()) { @@ -1058,7 +1060,8 @@ void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle po // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) // The extra MH receiver will be inserted into the stack on every call. methodHandle resolved_method; - lookup_implicit_method(resolved_method, resolved_klass, method_name, method_signature, CHECK); + KlassHandle current_klass(THREAD, pool->pool_holder()); + lookup_implicit_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, CHECK); if (resolved_method.is_null()) { THROW(vmSymbols::java_lang_InternalError()); } diff --git a/hotspot/src/share/vm/interpreter/linkResolver.hpp b/hotspot/src/share/vm/interpreter/linkResolver.hpp index efc1b53f5f32d0933497a6c57e8d79d084896057..673a166d0e7d3a93d7b3f62896b0d84e382f0892 100644 --- a/hotspot/src/share/vm/interpreter/linkResolver.hpp +++ b/hotspot/src/share/vm/interpreter/linkResolver.hpp @@ -103,7 +103,8 @@ class LinkResolver: AllStatic { static void lookup_method_in_klasses (methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS); static void lookup_instance_method_in_klasses (methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS); static void lookup_method_in_interfaces (methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS); - static void lookup_implicit_method (methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS); + static void lookup_implicit_method (methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, + KlassHandle current_klass, TRAPS); static int vtable_index_of_miranda_method(KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS); diff --git a/hotspot/src/share/vm/prims/methodHandles.cpp b/hotspot/src/share/vm/prims/methodHandles.cpp index 9581757f0619dd6ca68b18b821452112bc52356e..9d9c214086448108a2dcdb647c8937a47d77fe2d 100644 --- a/hotspot/src/share/vm/prims/methodHandles.cpp +++ b/hotspot/src/share/vm/prims/methodHandles.cpp @@ -475,16 +475,25 @@ void MethodHandles::resolve_MemberName(Handle mname, TRAPS) { if (name.is_null()) return; // no such name name_str = NULL; // safety + Handle polymorphic_method_type; + bool polymorphic_signature = false; + if ((flags & ALL_KINDS) == IS_METHOD && + (defc() == SystemDictionary::InvokeDynamic_klass() || + (defc() == SystemDictionary::MethodHandle_klass() && + methodOopDesc::is_method_handle_invoke_name(name())))) + polymorphic_signature = true; + // convert the external string or reflective type to an internal signature - bool force_signature = methodOopDesc::is_method_handle_invoke_name(name()); symbolHandle type; { symbolOop type_sym = NULL; if (java_dyn_MethodType::is_instance(type_str)) { - type_sym = java_dyn_MethodType::as_signature(type_str, force_signature, CHECK); + type_sym = java_dyn_MethodType::as_signature(type_str, polymorphic_signature, CHECK); + if (polymorphic_signature) + polymorphic_method_type = Handle(THREAD, type_str); //preserve exactly } else if (java_lang_Class::is_instance(type_str)) { - type_sym = java_lang_Class::as_signature(type_str, force_signature, CHECK); + type_sym = java_lang_Class::as_signature(type_str, false, CHECK); } else if (java_lang_String::is_instance(type_str)) { - if (force_signature) { + if (polymorphic_signature) { type = java_lang_String::as_symbol(type_str, CHECK); } else { type_sym = java_lang_String::as_symbol_or_null(type_str); @@ -517,7 +526,7 @@ void MethodHandles::resolve_MemberName(Handle mname, TRAPS) { } if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_EXCEPTION; - return; + break; // go to second chance } } methodHandle m = result.resolved_method(); @@ -591,8 +600,42 @@ void MethodHandles::resolve_MemberName(Handle mname, TRAPS) { sun_dyn_MemberName::set_modifiers(mname(), mods); return; } + default: + THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); + } + + // Second chance. + if (polymorphic_method_type.not_null()) { + // Look on a non-null class loader. + Handle cur_class_loader; + const int nptypes = java_dyn_MethodType::ptype_count(polymorphic_method_type()); + for (int i = 0; i <= nptypes; i++) { + oop type_mirror; + if (i < nptypes) type_mirror = java_dyn_MethodType::ptype(polymorphic_method_type(), i); + else type_mirror = java_dyn_MethodType::rtype(polymorphic_method_type()); + klassOop example_type = java_lang_Class::as_klassOop(type_mirror); + if (example_type == NULL) continue; + oop class_loader = Klass::cast(example_type)->class_loader(); + if (class_loader == NULL || class_loader == cur_class_loader()) continue; + cur_class_loader = Handle(THREAD, class_loader); + methodOop m = SystemDictionary::find_method_handle_invoke(name, + type, + KlassHandle(THREAD, example_type), + THREAD); + if (HAS_PENDING_EXCEPTION) { + CLEAR_PENDING_EXCEPTION; + m = NULL; + // try again with a different class loader... + } + if (m != NULL) { + int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); + sun_dyn_MemberName::set_vmtarget(mname(), m); + sun_dyn_MemberName::set_vmindex(mname(), m->vtable_index()); + sun_dyn_MemberName::set_modifiers(mname(), mods); + return; + } + } } - THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); } // Conversely, a member name which is only initialized from JVM internals @@ -993,6 +1036,13 @@ void MethodHandles::verify_method_signature(methodHandle m, pnum += 1; mnum += 1; } + klassOop pklass = NULL; + BasicType ptype = T_OBJECT; + if (ptype_oop != NULL) + ptype = java_lang_Class::as_BasicType(ptype_oop, &pklass); + else + // null does not match any non-reference; use Object to report the error + pklass = SystemDictionary::Object_klass(); klassOop mklass = NULL; BasicType mtype = ss.type(); if (mtype == T_ARRAY) mtype = T_OBJECT; // fold all refs to T_OBJECT @@ -1001,21 +1051,22 @@ void MethodHandles::verify_method_signature(methodHandle m, // null matches any reference continue; } + KlassHandle pklass_handle(THREAD, pklass); pklass = NULL; // If we fail to resolve types at this point, we will throw an error. symbolOop name_oop = ss.as_symbol(CHECK); symbolHandle name(THREAD, name_oop); instanceKlass* mk = instanceKlass::cast(m->method_holder()); Handle loader(THREAD, mk->class_loader()); Handle domain(THREAD, mk->protection_domain()); - mklass = SystemDictionary::resolve_or_fail(name, loader, domain, - true, CHECK); - } - if (ptype_oop == NULL) { - // null does not match any non-reference; use Object to report the error - ptype_oop = object_java_mirror(); + mklass = SystemDictionary::resolve_or_null(name, loader, domain, CHECK); + pklass = pklass_handle(); + if (mklass == NULL && pklass != NULL && + Klass::cast(pklass)->name() == name() && + m->is_method_handle_invoke()) { + // Assume a match. We can't really decode the signature of MH.invoke*. + continue; + } } - klassOop pklass = NULL; - BasicType ptype = java_lang_Class::as_BasicType(ptype_oop, &pklass); if (!ss.at_return_type()) { err = check_argument_type_change(ptype, pklass, mtype, mklass, mnum); } else { diff --git a/hotspot/src/share/vm/runtime/signature.cpp b/hotspot/src/share/vm/runtime/signature.cpp index c9c3859a61295120483f935744fab8a960f9a706..ca46e2ceafd43038fe94b8d0d556cbd391669957 100644 --- a/hotspot/src/share/vm/runtime/signature.cpp +++ b/hotspot/src/share/vm/runtime/signature.cpp @@ -327,6 +327,26 @@ symbolOop SignatureStream::as_symbol(TRAPS) { return result; } +klassOop SignatureStream::as_klass(Handle class_loader, Handle protection_domain, + FailureMode failure_mode, TRAPS) { + if (!is_object()) return NULL; + symbolOop name = as_symbol(CHECK_NULL); + if (failure_mode == ReturnNull) { + return SystemDictionary::resolve_or_null(name, class_loader, protection_domain, THREAD); + } else { + bool throw_error = (failure_mode == NCDFError); + return SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, THREAD); + } +} + +oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain, + FailureMode failure_mode, TRAPS) { + if (!is_object()) + return Universe::java_mirror(type()); + klassOop klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL); + if (klass == NULL) return NULL; + return Klass::cast(klass)->java_mirror(); +} symbolOop SignatureStream::as_symbol_or_null() { // Create a symbol from for string _begin _end diff --git a/hotspot/src/share/vm/runtime/signature.hpp b/hotspot/src/share/vm/runtime/signature.hpp index 9d67b0334ec145d7fcf8a65d8f0eb37fbe2de29c..aeed8037ca9c14a99d0a7602f4512d750d9cf6ce 100644 --- a/hotspot/src/share/vm/runtime/signature.hpp +++ b/hotspot/src/share/vm/runtime/signature.hpp @@ -402,6 +402,9 @@ class SignatureStream : public StackObj { bool is_array() const; // True if this argument is an array BasicType type() const { return _type; } symbolOop as_symbol(TRAPS); + enum FailureMode { ReturnNull, CNFException, NCDFError }; + klassOop as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS); + oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS); // return same as_symbol except allocation of new symbols is avoided. symbolOop as_symbol_or_null();