2321 bool is_method, TRAPS) { 2322 // Nothing to do if loaders are the same. 2323 if (loader1() == loader2()) { 2324 return NULL; 2325 } 2326 2327 SignatureStream sig_strm(signature, is_method); 2328 while (!sig_strm.is_done()) { 2329 if (sig_strm.is_object()) { 2330 Symbol* sig = sig_strm.as_symbol(); 2331 if (!add_loader_constraint(sig, loader1, loader2, THREAD)) { 2332 return sig; 2333 } 2334 } 2335 sig_strm.next(); 2336 } 2337 return NULL; 2338 } 2339 2340 2341 methodHandle SystemDictionary::find_method_handle_intrinsic(vmIntrinsics::ID iid, 2342 Symbol* signature, 2343 TRAPS) { 2344 methodHandle empty; 2345 assert(MethodHandles::is_signature_polymorphic(iid) && 2346 MethodHandles::is_signature_polymorphic_intrinsic(iid) && 2347 iid != vmIntrinsics::_invokeGeneric, 2348 "must be a known MH intrinsic iid=%d: %s", iid, vmIntrinsics::name_at(iid)); 2349 2350 unsigned int hash = invoke_method_table()->compute_hash(signature, iid); 2351 int index = invoke_method_table()->hash_to_index(hash); 2352 SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, iid); 2353 methodHandle m; 2354 if (spe == NULL || spe->method() == NULL) { 2355 spe = NULL; 2356 // Must create lots of stuff here, but outside of the SystemDictionary lock. 2357 m = Method::make_method_handle_intrinsic(iid, signature, CHECK_(empty)); 2358 if (!Arguments::is_interpreter_only()) { 2359 // Generate a compiled form of the MH intrinsic. 2360 AdapterHandlerLibrary::create_native_wrapper(m); 2361 // Check if have the compiled code. 2362 if (!m->has_compiled_code()) { 2363 THROW_MSG_(vmSymbols::java_lang_VirtualMachineError(), 2364 "Out of space in CodeCache for method handle intrinsic", empty); 2365 } 2366 } 2367 // Now grab the lock. We might have to throw away the new method, 2368 // if a racing thread has managed to install one at the same time. 2369 { 2370 MutexLocker ml(SystemDictionary_lock, THREAD); 2371 spe = invoke_method_table()->find_entry(index, hash, signature, iid); 2372 if (spe == NULL) 2373 spe = invoke_method_table()->add_entry(index, hash, signature, iid); 2374 if (spe->method() == NULL) 2375 spe->set_method(m()); 2376 } 2377 } 2378 2379 assert(spe != NULL && spe->method() != NULL, ""); 2380 assert(Arguments::is_interpreter_only() || (spe->method()->has_compiled_code() && 2381 spe->method()->code()->entry_point() == spe->method()->from_compiled_entry()), 2382 "MH intrinsic invariant"); 2383 return spe->method(); 2384 } 2385 2386 // Helper for unpacking the return value from linkMethod and linkCallSite. 2387 static methodHandle unpack_method_and_appendix(Handle mname, 2388 Klass* accessing_klass, 2389 objArrayHandle appendix_box, 2390 Handle* appendix_result, 2391 TRAPS) { 2392 methodHandle empty; 2393 if (mname.not_null()) { 2394 Method* m = java_lang_invoke_MemberName::vmtarget(mname()); 2395 if (m != NULL) { 2396 oop appendix = appendix_box->obj_at(0); 2397 if (TraceMethodHandles) { 2398 #ifndef PRODUCT 2399 ttyLocker ttyl; 2400 tty->print("Linked method=" INTPTR_FORMAT ": ", p2i(m)); 2401 m->print(); 2402 if (appendix != NULL) { tty->print("appendix = "); appendix->print(); } 2403 tty->cr(); 2404 #endif //PRODUCT 2405 } 2406 (*appendix_result) = Handle(THREAD, appendix); 2407 // the target is stored in the cpCache and if a reference to this 2408 // MemberName is dropped we need a way to make sure the 2409 // class_loader containing this method is kept alive. 2410 ClassLoaderData* this_key = accessing_klass->class_loader_data(); 2411 this_key->record_dependency(m->method_holder()); 2412 return methodHandle(THREAD, m); 2413 } 2414 } 2415 THROW_MSG_(vmSymbols::java_lang_LinkageError(), "bad value from MethodHandleNatives", empty); 2416 return empty; 2417 } 2418 2419 methodHandle SystemDictionary::find_method_handle_invoker(Klass* klass, 2420 Symbol* name, 2421 Symbol* signature, 2422 Klass* accessing_klass, 2423 Handle *appendix_result, 2424 TRAPS) { 2425 methodHandle empty; 2426 assert(THREAD->can_call_java() ,""); 2427 Handle method_type = 2428 SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_(empty)); 2429 2430 int ref_kind = JVM_REF_invokeVirtual; 2431 oop name_oop = StringTable::intern(name, CHECK_(empty)); 2432 Handle name_str (THREAD, name_oop); 2433 objArrayHandle appendix_box = oopFactory::new_objArray_handle(SystemDictionary::Object_klass(), 1, CHECK_(empty)); 2434 assert(appendix_box->obj_at(0) == NULL, ""); 2435 2436 // This should not happen. JDK code should take care of that. 2437 if (accessing_klass == NULL || method_type.is_null()) { 2438 THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad invokehandle", empty); 2439 } 2440 2441 // call java.lang.invoke.MethodHandleNatives::linkMethod(... String, MethodType) -> MemberName 2442 JavaCallArguments args; 2443 args.push_oop(Handle(THREAD, accessing_klass->java_mirror())); 2444 args.push_int(ref_kind); 2445 args.push_oop(Handle(THREAD, klass->java_mirror())); 2446 args.push_oop(name_str); 2447 args.push_oop(method_type); 2448 args.push_oop(appendix_box); 2449 JavaValue result(T_OBJECT); 2450 JavaCalls::call_static(&result, 2451 SystemDictionary::MethodHandleNatives_klass(), 2452 vmSymbols::linkMethod_name(), 2453 vmSymbols::linkMethod_signature(), 2454 &args, CHECK_(empty)); 2455 Handle mname(THREAD, (oop) result.get_jobject()); 2456 return unpack_method_and_appendix(mname, accessing_klass, appendix_box, appendix_result, THREAD); 2457 } 2458 2459 // Decide if we can globally cache a lookup of this class, to be returned to any client that asks. 2460 // We must ensure that all class loaders everywhere will reach this class, for any client. 2461 // This is a safe bet for public classes in java.lang, such as Object and String. 2462 // We also include public classes in java.lang.invoke, because they appear frequently in system-level method types. 2463 // Out of an abundance of caution, we do not include any other classes, not even for packages like java.util. 2464 static bool is_always_visible_class(oop mirror) { 2465 Klass* klass = java_lang_Class::as_Klass(mirror); 2466 if (klass->is_objArray_klass()) { 2467 klass = ObjArrayKlass::cast(klass)->bottom_klass(); // check element type 2468 } 2469 if (klass->is_typeArray_klass()) { 2470 return true; // primitive array 2471 } 2472 assert(klass->is_instance_klass(), "%s", klass->external_name()); 2473 return klass->is_public() && 2474 (InstanceKlass::cast(klass)->is_same_class_package(SystemDictionary::Object_klass()) || // java.lang 2738 JavaCallArguments args; 2739 args.push_oop(Handle(THREAD, bootstrap_specifier.caller_mirror())); 2740 args.push_int(bootstrap_specifier.bss_index()); 2741 args.push_oop(bootstrap_specifier.bsm()); 2742 args.push_oop(bootstrap_specifier.name_arg()); 2743 args.push_oop(bootstrap_specifier.type_arg()); 2744 args.push_oop(bootstrap_specifier.arg_values()); 2745 if (is_indy) { 2746 args.push_oop(appendix_box); 2747 } 2748 JavaValue result(T_OBJECT); 2749 JavaCalls::call_static(&result, 2750 SystemDictionary::MethodHandleNatives_klass(), 2751 is_indy ? vmSymbols::linkCallSite_name() : vmSymbols::linkDynamicConstant_name(), 2752 is_indy ? vmSymbols::linkCallSite_signature() : vmSymbols::linkDynamicConstant_signature(), 2753 &args, CHECK); 2754 2755 Handle value(THREAD, (oop) result.get_jobject()); 2756 if (is_indy) { 2757 Handle appendix; 2758 methodHandle method = unpack_method_and_appendix(value, 2759 bootstrap_specifier.caller(), 2760 appendix_box, 2761 &appendix, CHECK); 2762 bootstrap_specifier.set_resolved_method(method, appendix); 2763 } else { 2764 bootstrap_specifier.set_resolved_value(value); 2765 } 2766 2767 // sanity check 2768 assert(bootstrap_specifier.is_resolved() || 2769 (bootstrap_specifier.is_method_call() && 2770 bootstrap_specifier.resolved_method().not_null()), "bootstrap method call failed"); 2771 } 2772 2773 // Protection domain cache table handling 2774 2775 ProtectionDomainCacheEntry* SystemDictionary::cache_get(Handle protection_domain) { 2776 return _pd_cache_table->get(protection_domain); 2777 } 2778 2779 // ---------------------------------------------------------------------------- 2780 2781 void SystemDictionary::print_on(outputStream *st) { 2782 CDS_ONLY(SystemDictionaryShared::print_on(st)); | 2321 bool is_method, TRAPS) { 2322 // Nothing to do if loaders are the same. 2323 if (loader1() == loader2()) { 2324 return NULL; 2325 } 2326 2327 SignatureStream sig_strm(signature, is_method); 2328 while (!sig_strm.is_done()) { 2329 if (sig_strm.is_object()) { 2330 Symbol* sig = sig_strm.as_symbol(); 2331 if (!add_loader_constraint(sig, loader1, loader2, THREAD)) { 2332 return sig; 2333 } 2334 } 2335 sig_strm.next(); 2336 } 2337 return NULL; 2338 } 2339 2340 2341 Method* SystemDictionary::find_method_handle_intrinsic(vmIntrinsics::ID iid, 2342 Symbol* signature, 2343 TRAPS) { 2344 methodHandle empty; 2345 assert(MethodHandles::is_signature_polymorphic(iid) && 2346 MethodHandles::is_signature_polymorphic_intrinsic(iid) && 2347 iid != vmIntrinsics::_invokeGeneric, 2348 "must be a known MH intrinsic iid=%d: %s", iid, vmIntrinsics::name_at(iid)); 2349 2350 unsigned int hash = invoke_method_table()->compute_hash(signature, iid); 2351 int index = invoke_method_table()->hash_to_index(hash); 2352 SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, iid); 2353 methodHandle m; 2354 if (spe == NULL || spe->method() == NULL) { 2355 spe = NULL; 2356 // Must create lots of stuff here, but outside of the SystemDictionary lock. 2357 m = Method::make_method_handle_intrinsic(iid, signature, CHECK_NULL); 2358 if (!Arguments::is_interpreter_only()) { 2359 // Generate a compiled form of the MH intrinsic. 2360 AdapterHandlerLibrary::create_native_wrapper(m); 2361 // Check if have the compiled code. 2362 if (!m->has_compiled_code()) { 2363 THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), 2364 "Out of space in CodeCache for method handle intrinsic"); 2365 } 2366 } 2367 // Now grab the lock. We might have to throw away the new method, 2368 // if a racing thread has managed to install one at the same time. 2369 { 2370 MutexLocker ml(SystemDictionary_lock, THREAD); 2371 spe = invoke_method_table()->find_entry(index, hash, signature, iid); 2372 if (spe == NULL) 2373 spe = invoke_method_table()->add_entry(index, hash, signature, iid); 2374 if (spe->method() == NULL) 2375 spe->set_method(m()); 2376 } 2377 } 2378 2379 assert(spe != NULL && spe->method() != NULL, ""); 2380 assert(Arguments::is_interpreter_only() || (spe->method()->has_compiled_code() && 2381 spe->method()->code()->entry_point() == spe->method()->from_compiled_entry()), 2382 "MH intrinsic invariant"); 2383 return spe->method(); 2384 } 2385 2386 // Helper for unpacking the return value from linkMethod and linkCallSite. 2387 static Method* unpack_method_and_appendix(Handle mname, 2388 Klass* accessing_klass, 2389 objArrayHandle appendix_box, 2390 Handle* appendix_result, 2391 TRAPS) { 2392 if (mname.not_null()) { 2393 Method* m = java_lang_invoke_MemberName::vmtarget(mname()); 2394 if (m != NULL) { 2395 oop appendix = appendix_box->obj_at(0); 2396 if (TraceMethodHandles) { 2397 #ifndef PRODUCT 2398 ttyLocker ttyl; 2399 tty->print("Linked method=" INTPTR_FORMAT ": ", p2i(m)); 2400 m->print(); 2401 if (appendix != NULL) { tty->print("appendix = "); appendix->print(); } 2402 tty->cr(); 2403 #endif //PRODUCT 2404 } 2405 (*appendix_result) = Handle(THREAD, appendix); 2406 // the target is stored in the cpCache and if a reference to this 2407 // MemberName is dropped we need a way to make sure the 2408 // class_loader containing this method is kept alive. 2409 methodHandle mh(THREAD, m); // record_dependency can safepoint. 2410 ClassLoaderData* this_key = accessing_klass->class_loader_data(); 2411 this_key->record_dependency(m->method_holder()); 2412 return mh(); 2413 } 2414 } 2415 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "bad value from MethodHandleNatives"); 2416 } 2417 2418 Method* SystemDictionary::find_method_handle_invoker(Klass* klass, 2419 Symbol* name, 2420 Symbol* signature, 2421 Klass* accessing_klass, 2422 Handle *appendix_result, 2423 TRAPS) { 2424 assert(THREAD->can_call_java() ,""); 2425 Handle method_type = 2426 SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_NULL); 2427 2428 int ref_kind = JVM_REF_invokeVirtual; 2429 oop name_oop = StringTable::intern(name, CHECK_NULL); 2430 Handle name_str (THREAD, name_oop); 2431 objArrayHandle appendix_box = oopFactory::new_objArray_handle(SystemDictionary::Object_klass(), 1, CHECK_NULL); 2432 assert(appendix_box->obj_at(0) == NULL, ""); 2433 2434 // This should not happen. JDK code should take care of that. 2435 if (accessing_klass == NULL || method_type.is_null()) { 2436 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "bad invokehandle"); 2437 } 2438 2439 // call java.lang.invoke.MethodHandleNatives::linkMethod(... String, MethodType) -> MemberName 2440 JavaCallArguments args; 2441 args.push_oop(Handle(THREAD, accessing_klass->java_mirror())); 2442 args.push_int(ref_kind); 2443 args.push_oop(Handle(THREAD, klass->java_mirror())); 2444 args.push_oop(name_str); 2445 args.push_oop(method_type); 2446 args.push_oop(appendix_box); 2447 JavaValue result(T_OBJECT); 2448 JavaCalls::call_static(&result, 2449 SystemDictionary::MethodHandleNatives_klass(), 2450 vmSymbols::linkMethod_name(), 2451 vmSymbols::linkMethod_signature(), 2452 &args, CHECK_NULL); 2453 Handle mname(THREAD, (oop) result.get_jobject()); 2454 return unpack_method_and_appendix(mname, accessing_klass, appendix_box, appendix_result, THREAD); 2455 } 2456 2457 // Decide if we can globally cache a lookup of this class, to be returned to any client that asks. 2458 // We must ensure that all class loaders everywhere will reach this class, for any client. 2459 // This is a safe bet for public classes in java.lang, such as Object and String. 2460 // We also include public classes in java.lang.invoke, because they appear frequently in system-level method types. 2461 // Out of an abundance of caution, we do not include any other classes, not even for packages like java.util. 2462 static bool is_always_visible_class(oop mirror) { 2463 Klass* klass = java_lang_Class::as_Klass(mirror); 2464 if (klass->is_objArray_klass()) { 2465 klass = ObjArrayKlass::cast(klass)->bottom_klass(); // check element type 2466 } 2467 if (klass->is_typeArray_klass()) { 2468 return true; // primitive array 2469 } 2470 assert(klass->is_instance_klass(), "%s", klass->external_name()); 2471 return klass->is_public() && 2472 (InstanceKlass::cast(klass)->is_same_class_package(SystemDictionary::Object_klass()) || // java.lang 2736 JavaCallArguments args; 2737 args.push_oop(Handle(THREAD, bootstrap_specifier.caller_mirror())); 2738 args.push_int(bootstrap_specifier.bss_index()); 2739 args.push_oop(bootstrap_specifier.bsm()); 2740 args.push_oop(bootstrap_specifier.name_arg()); 2741 args.push_oop(bootstrap_specifier.type_arg()); 2742 args.push_oop(bootstrap_specifier.arg_values()); 2743 if (is_indy) { 2744 args.push_oop(appendix_box); 2745 } 2746 JavaValue result(T_OBJECT); 2747 JavaCalls::call_static(&result, 2748 SystemDictionary::MethodHandleNatives_klass(), 2749 is_indy ? vmSymbols::linkCallSite_name() : vmSymbols::linkDynamicConstant_name(), 2750 is_indy ? vmSymbols::linkCallSite_signature() : vmSymbols::linkDynamicConstant_signature(), 2751 &args, CHECK); 2752 2753 Handle value(THREAD, (oop) result.get_jobject()); 2754 if (is_indy) { 2755 Handle appendix; 2756 Method* method = unpack_method_and_appendix(value, 2757 bootstrap_specifier.caller(), 2758 appendix_box, 2759 &appendix, CHECK); 2760 methodHandle mh(THREAD, method); 2761 bootstrap_specifier.set_resolved_method(mh, appendix); 2762 } else { 2763 bootstrap_specifier.set_resolved_value(value); 2764 } 2765 2766 // sanity check 2767 assert(bootstrap_specifier.is_resolved() || 2768 (bootstrap_specifier.is_method_call() && 2769 bootstrap_specifier.resolved_method().not_null()), "bootstrap method call failed"); 2770 } 2771 2772 // Protection domain cache table handling 2773 2774 ProtectionDomainCacheEntry* SystemDictionary::cache_get(Handle protection_domain) { 2775 return _pd_cache_table->get(protection_domain); 2776 } 2777 2778 // ---------------------------------------------------------------------------- 2779 2780 void SystemDictionary::print_on(outputStream *st) { 2781 CDS_ONLY(SystemDictionaryShared::print_on(st)); |