6939196: method handle signatures off the boot class path get linkage errors
Summary: Adjust MethodType lookup logic to search off the BCP, but not to cache those results
Reviewed-by: twisti
--- a/hotspot/src/share/vm/classfile/systemDictionary.cpp Sat May 01 02:42:18 2010 -0700
+++ b/hotspot/src/share/vm/classfile/systemDictionary.cpp Sat May 01 21:57:35 2010 -0700
@@ -2343,12 +2343,9 @@
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 @@
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 @@
// 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 @@
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());
}
--- a/hotspot/src/share/vm/classfile/systemDictionary.hpp Sat May 01 02:42:18 2010 -0700
+++ b/hotspot/src/share/vm/classfile/systemDictionary.hpp Sat May 01 21:57:35 2010 -0700
@@ -466,13 +466,12 @@
// 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,
--- a/hotspot/src/share/vm/interpreter/linkResolver.cpp Sat May 01 02:42:18 2010 -0700
+++ b/hotspot/src/share/vm/interpreter/linkResolver.cpp Sat May 01 21:57:35 2010 -0700
@@ -172,14 +172,16 @@
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 @@
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 @@
// 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());
}
--- a/hotspot/src/share/vm/interpreter/linkResolver.hpp Sat May 01 02:42:18 2010 -0700
+++ b/hotspot/src/share/vm/interpreter/linkResolver.hpp Sat May 01 21:57:35 2010 -0700
@@ -103,7 +103,8 @@
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);
--- a/hotspot/src/share/vm/prims/methodHandles.cpp Sat May 01 02:42:18 2010 -0700
+++ b/hotspot/src/share/vm/prims/methodHandles.cpp Sat May 01 21:57:35 2010 -0700
@@ -475,16 +475,25 @@
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 @@
}
if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION;
- return;
+ break; // go to second chance
}
}
methodHandle m = result.resolved_method();
@@ -591,8 +600,42 @@
sun_dyn_MemberName::set_modifiers(mname(), mods);
return;
}
+ default:
+ THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format");
}
- 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;
+ }
+ }
+ }
}
// Conversely, a member name which is only initialized from JVM internals
@@ -993,6 +1036,13 @@
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 @@
// 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);
+ 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;
+ }
}
- if (ptype_oop == NULL) {
- // null does not match any non-reference; use Object to report the error
- ptype_oop = object_java_mirror();
- }
- 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 {
--- a/hotspot/src/share/vm/runtime/signature.cpp Sat May 01 02:42:18 2010 -0700
+++ b/hotspot/src/share/vm/runtime/signature.cpp Sat May 01 21:57:35 2010 -0700
@@ -327,6 +327,26 @@
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
--- a/hotspot/src/share/vm/runtime/signature.hpp Sat May 01 02:42:18 2010 -0700
+++ b/hotspot/src/share/vm/runtime/signature.hpp Sat May 01 21:57:35 2010 -0700
@@ -402,6 +402,9 @@
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();