methodHandles.cpp revision 4558:746b070f5022
1270114Sse/* 2270114Sse * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. 3270114Sse * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4270114Sse * 5270114Sse * This code is free software; you can redistribute it and/or modify it 6270114Sse * under the terms of the GNU General Public License version 2 only, as 7270114Sse * published by the Free Software Foundation. 8270114Sse * 9270114Sse * This code is distributed in the hope that it will be useful, but WITHOUT 10270114Sse * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11270114Sse * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12270114Sse * version 2 for more details (a copy is included in the LICENSE file that 13270114Sse * accompanied this code). 14270114Sse * 15270114Sse * You should have received a copy of the GNU General Public License version 16270114Sse * 2 along with this work; if not, write to the Free Software Foundation, 17270114Sse * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18322225Sse * 19270114Sse * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20270114Sse * or visit www.oracle.com if you need additional information or have any 21270114Sse * questions. 22270114Sse * 23283164Semaste */ 24270114Sse 25270114Sse#include "precompiled.hpp" 26283164Semaste#include "classfile/symbolTable.hpp" 27270114Sse#include "compiler/compileBroker.hpp" 28270114Sse#include "interpreter/interpreter.hpp" 29283164Semaste#include "interpreter/oopMapCache.hpp" 30283164Semaste#include "memory/allocation.inline.hpp" 31283164Semaste#include "memory/oopFactory.hpp" 32270114Sse#include "prims/jvmtiRedefineClassesTrace.hpp" 33270114Sse#include "prims/methodHandles.hpp" 34322225Sse#include "runtime/compilationPolicy.hpp" 35270114Sse#include "runtime/javaCalls.hpp" 36270114Sse#include "runtime/reflection.hpp" 37270114Sse#include "runtime/signature.hpp" 38270114Sse#include "runtime/stubRoutines.hpp" 39270114Sse 40270114Sse 41270114Sse/* 42270114Sse * JSR 292 reference implementation: method handles 43270114Sse * The JDK 7 reference implementation represented method handle 44270114Sse * combinations as chains. Each link in the chain had a "vmentry" 45270114Sse * field which pointed at a bit of assembly code which performed 46270114Sse * one transformation before dispatching to the next link in the chain. 47270114Sse * 48270114Sse * The current reference implementation pushes almost all code generation 49270114Sse * responsibility to (trusted) Java code. A method handle contains a 50270114Sse * pointer to its "LambdaForm", which embodies all details of the method 51270114Sse * handle's behavior. The LambdaForm is a normal Java object, managed 52270114Sse * by a runtime coded in Java. 53270114Sse */ 54270114Sse 55270114Ssebool MethodHandles::_enabled = false; // set true after successful native linkage 56270114SseMethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; 57270114Sse 58270310Sse//------------------------------------------------------------------------------ 59270310Sse// MethodHandles::generate_adapters 60270114Sse// 61270310Ssevoid MethodHandles::generate_adapters() { 62270310Sse if (!EnableInvokeDynamic || SystemDictionary::MethodHandle_klass() == NULL) return; 63270310Sse 64270310Sse assert(_adapter_code == NULL, "generate only once"); 65270310Sse 66270114Sse ResourceMark rm; 67270310Sse TraceTime timer("MethodHandles adapters generation", TraceStartupTime); 68270310Sse _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size); 69270310Sse if (_adapter_code == NULL) 70270310Sse vm_exit_out_of_memory(adapter_code_size, OOM_MALLOC_ERROR, 71270310Sse "CodeCache: no room for MethodHandles adapters"); 72270114Sse { 73270310Sse CodeBuffer code(_adapter_code); 74270310Sse MethodHandlesAdapterGenerator g(&code); 75270310Sse g.generate(); 76270114Sse code.log_section_sizes("MethodHandlesAdapterBlob"); 77270114Sse } 78270114Sse} 79270114Sse 80270114Sse//------------------------------------------------------------------------------ 81270114Sse// MethodHandlesAdapterGenerator::generate 82270310Sse// 83270310Ssevoid MethodHandlesAdapterGenerator::generate() { 84270310Sse // Generate generic method handle adapters. 85270310Sse // Generate interpreter entries 86270114Sse for (Interpreter::MethodKind mk = Interpreter::method_handle_invoke_FIRST; 87270310Sse mk <= Interpreter::method_handle_invoke_LAST; 88270310Sse mk = Interpreter::MethodKind(1 + (int)mk)) { 89270114Sse vmIntrinsics::ID iid = Interpreter::method_handle_intrinsic(mk); 90270114Sse StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid)); 91270114Sse address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid); 92270114Sse if (entry != NULL) { 93270114Sse Interpreter::set_entry_for_kind(mk, entry); 94270114Sse } 95270114Sse // If the entry is not set, it will throw AbstractMethodError. 96270114Sse } 97270114Sse} 98270114Sse 99270310Ssevoid MethodHandles::set_enabled(bool z) { 100270310Sse if (_enabled != z) { 101270310Sse guarantee(z && EnableInvokeDynamic, "can only enable once, and only if -XX:+EnableInvokeDynamic"); 102270310Sse _enabled = z; 103270310Sse } 104270114Sse} 105270114Sse 106270114Sse// MemberName support 107270114Sse 108270114Sse// import java_lang_invoke_MemberName.* 109270114Sseenum { 110270114Sse IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD, 111270114Sse IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR, 112270114Sse IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD, 113270114Sse IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE, 114270114Sse CALLER_SENSITIVE = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE, 115270114Sse REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT, 116270114Sse REFERENCE_KIND_MASK = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK, 117270114Sse SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES, 118270114Sse SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES, 119270114Sse ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE 120270114Sse}; 121270114Sse 122270114SseHandle MethodHandles::new_MemberName(TRAPS) { 123270114Sse Handle empty; 124270114Sse instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass()); 125283164Semaste if (!k->is_initialized()) k->initialize(CHECK_(empty)); 126270114Sse return Handle(THREAD, k->allocate_instance(THREAD)); 127270114Sse} 128270114Sse 129270114Sseoop MethodHandles::init_MemberName(Handle mname, Handle target) { 130270114Sse Thread* thread = Thread::current(); 131270114Sse oop target_oop = target(); 132270114Sse Klass* target_klass = target_oop->klass(); 133270114Sse if (target_klass == SystemDictionary::reflect_Field_klass()) { 134270114Sse oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() 135270114Sse int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() 136270114Sse int mods = java_lang_reflect_Field::modifiers(target_oop); 137270114Sse oop type = java_lang_reflect_Field::type(target_oop); 138270114Sse oop name = java_lang_reflect_Field::name(target_oop); 139270114Sse KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); 140270114Sse intptr_t offset = InstanceKlass::cast(k())->field_offset(slot); 141270114Sse return init_field_MemberName(mname, k, accessFlags_from(mods), type, name, offset); 142270114Sse } else if (target_klass == SystemDictionary::reflect_Method_klass()) { 143270114Sse oop clazz = java_lang_reflect_Method::clazz(target_oop); 144270114Sse int slot = java_lang_reflect_Method::slot(target_oop); 145270114Sse KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); 146270114Sse if (!k.is_null() && k->oop_is_instance()) { 147270114Sse Method* m = InstanceKlass::cast(k())->method_with_idnum(slot); 148270114Sse return init_method_MemberName(mname, m, true, k); 149270114Sse } 150270114Sse } else if (target_klass == SystemDictionary::reflect_Constructor_klass()) { 151270114Sse oop clazz = java_lang_reflect_Constructor::clazz(target_oop); 152270114Sse int slot = java_lang_reflect_Constructor::slot(target_oop); 153270114Sse KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); 154270114Sse if (!k.is_null() && k->oop_is_instance()) { 155270114Sse Method* m = InstanceKlass::cast(k())->method_with_idnum(slot); 156270114Sse return init_method_MemberName(mname, m, false, k); 157270114Sse } 158270114Sse } else if (target_klass == SystemDictionary::MemberName_klass()) { 159270114Sse // Note: This only works if the MemberName has already been resolved. 160270114Sse oop clazz = java_lang_invoke_MemberName::clazz(target_oop); 161270114Sse int flags = java_lang_invoke_MemberName::flags(target_oop); 162270114Sse Metadata* vmtarget=java_lang_invoke_MemberName::vmtarget(target_oop); 163270114Sse intptr_t vmindex = java_lang_invoke_MemberName::vmindex(target_oop); 164270114Sse KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); 165270114Sse int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 166270114Sse if (vmtarget == NULL) return NULL; // not resolved 167270114Sse if ((flags & IS_FIELD) != 0) { 168270114Sse assert(vmtarget->is_klass(), "field vmtarget is Klass*"); 169270114Sse int basic_mods = (ref_kind_is_static(ref_kind) ? JVM_ACC_STATIC : 0); 170270114Sse // FIXME: how does k (receiver_limit) contribute? 171270114Sse KlassHandle k_vmtarget(thread, (Klass*)vmtarget); 172270114Sse return init_field_MemberName(mname, k_vmtarget, accessFlags_from(basic_mods), NULL, NULL, vmindex); 173270114Sse } else if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { 174270114Sse assert(vmtarget->is_method(), "method or constructor vmtarget is Method*"); 175270114Sse return init_method_MemberName(mname, (Method*)vmtarget, ref_kind_does_dispatch(ref_kind), k); 176270114Sse } else { 177270114Sse return NULL; 178270114Sse } 179270114Sse } 180270114Sse return NULL; 181270114Sse} 182270114Sse 183270114Sseoop MethodHandles::init_method_MemberName(Handle mname, Method* m, bool do_dispatch, 184270114Sse KlassHandle receiver_limit_h) { 185270114Sse Klass* receiver_limit = receiver_limit_h(); 186270114Sse AccessFlags mods = m->access_flags(); 187270114Sse int flags = (jushort)( mods.as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ); 188270114Sse int vmindex = Method::nonvirtual_vtable_index; // implies never any dispatch 189270114Sse Klass* mklass = m->method_holder(); 190270114Sse if (receiver_limit == NULL) 191270114Sse receiver_limit = mklass; 192270114Sse if (m->is_initializer()) { 193270114Sse flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); 194270114Sse } else if (mods.is_static()) { 195270114Sse flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT); 196270114Sse // Get vindex from itable if method holder is an interface. 197270114Sse if (m->method_holder()->is_interface()) { 198270114Sse vmindex = klassItable::compute_itable_index(m); 199270114Sse } 200270114Sse } else if (receiver_limit != mklass && 201270310Sse !receiver_limit->is_subtype_of(mklass)) { 202270114Sse return NULL; // bad receiver limit 203270310Sse } else if (receiver_limit->is_interface() && 204270310Sse mklass->is_interface()) { 205270310Sse flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT); 206270310Sse receiver_limit = mklass; // ignore passed-in limit; interfaces are interconvertible 207270310Sse vmindex = klassItable::compute_itable_index(m); 208270310Sse } else if (mklass != receiver_limit && mklass->is_interface()) { 209270114Sse flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); 210270114Sse // it is a miranda method, so m->vtable_index is not what we want 211270114Sse ResourceMark rm; 212270114Sse klassVtable* vt = InstanceKlass::cast(receiver_limit)->vtable(); 213270114Sse vmindex = vt->index_of_miranda(m->name(), m->signature()); 214322225Sse } else if (!do_dispatch || m->can_be_statically_bound()) { 215270114Sse flags |= IS_METHOD | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); 216270114Sse } else { 217270114Sse flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); 218270114Sse vmindex = m->vtable_index(); 219270114Sse } 220270114Sse 221270114Sse // @CallerSensitive annotation detected 222270114Sse if (m->caller_sensitive()) { 223270114Sse flags |= CALLER_SENSITIVE; 224270310Sse } 225270310Sse 226270310Sse oop mname_oop = mname(); 227270310Sse java_lang_invoke_MemberName::set_flags( mname_oop, flags); 228270310Sse java_lang_invoke_MemberName::set_vmtarget(mname_oop, m); 229270310Sse java_lang_invoke_MemberName::set_vmindex( mname_oop, vmindex); // vtable/itable index 230270310Sse java_lang_invoke_MemberName::set_clazz( mname_oop, receiver_limit->java_mirror()); 231270114Sse // Note: name and type can be lazily computed by resolve_MemberName, 232270114Sse // if Java code needs them as resolved String and MethodType objects. 233270114Sse // The clazz must be eagerly stored, because it provides a GC 234270114Sse // root to help keep alive the Method*. 235270114Sse // If relevant, the vtable or itable value is stored as vmindex. 236270114Sse // This is done eagerly, since it is readily available without 237270114Sse // constructing any new objects. 238270114Sse // TO DO: maybe intern mname_oop 239270114Sse m->method_holder()->add_member_name(mname); 240270114Sse return mname(); 241270114Sse} 242270114Sse 243270114SseHandle MethodHandles::init_method_MemberName(Handle mname, CallInfo& info, TRAPS) { 244270114Sse Handle empty; 245270114Sse if (info.resolved_appendix().not_null()) { 246270114Sse // The resolved MemberName must not be accompanied by an appendix argument, 247270114Sse // since there is no way to bind this value into the MemberName. 248270114Sse // Caller is responsible to prevent this from happening. 249270114Sse THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty); 250270114Sse } 251270114Sse methodHandle m = info.resolved_method(); 252270114Sse KlassHandle defc = info.resolved_klass(); 253270114Sse int vmindex = -1; 254270114Sse if (defc->is_interface() && m->method_holder()->is_interface()) { 255270114Sse // LinkResolver does not report itable indexes! (fix this?) 256270114Sse vmindex = klassItable::compute_itable_index(m()); 257270114Sse } else if (m->can_be_statically_bound()) { 258270114Sse // LinkResolver reports vtable index even for final methods! 259270114Sse vmindex = Method::nonvirtual_vtable_index; 260270114Sse } else { 261270114Sse vmindex = info.vtable_index(); 262270114Sse } 263270114Sse oop res = init_method_MemberName(mname, m(), (vmindex >= 0), defc()); 264270114Sse assert(res == NULL || (java_lang_invoke_MemberName::vmindex(res) == vmindex), ""); 265270114Sse return Handle(THREAD, res); 266270114Sse} 267270114Sse 268270114Sseoop MethodHandles::init_field_MemberName(Handle mname, KlassHandle field_holder, 269270114Sse AccessFlags mods, oop type, oop name, 270270114Sse intptr_t offset, bool is_setter) { 271270114Sse int flags = (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS ); 272270114Sse flags |= IS_FIELD | ((mods.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT); 273270114Sse if (is_setter) flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT); 274270114Sse Metadata* vmtarget = field_holder(); 275270114Sse int vmindex = offset; // determines the field uniquely when combined with static bit 276270114Sse oop mname_oop = mname(); 277270114Sse java_lang_invoke_MemberName::set_flags(mname_oop, flags); 278270114Sse java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); 279270114Sse java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); 280270114Sse java_lang_invoke_MemberName::set_clazz(mname_oop, field_holder->java_mirror()); 281270114Sse if (name != NULL) 282270114Sse java_lang_invoke_MemberName::set_name(mname_oop, name); 283270114Sse if (type != NULL) 284270114Sse java_lang_invoke_MemberName::set_type(mname_oop, type); 285270114Sse // Note: name and type can be lazily computed by resolve_MemberName, 286270114Sse // if Java code needs them as resolved String and Class objects. 287270114Sse // Note that the incoming type oop might be pre-resolved (non-null). 288270114Sse // The base clazz and field offset (vmindex) must be eagerly stored, 289270114Sse // because they unambiguously identify the field. 290270114Sse // Although the fieldDescriptor::_index would also identify the field, 291270114Sse // we do not use it, because it is harder to decode. 292270114Sse // TO DO: maybe intern mname_oop 293270114Sse InstanceKlass::cast(field_holder())->add_member_name(mname); 294270114Sse return mname(); 295270114Sse} 296270114Sse 297270114SseHandle MethodHandles::init_field_MemberName(Handle mname, FieldAccessInfo& info, TRAPS) { 298270114Sse return Handle(); 299270114Sse#if 0 // FIXME 300270114Sse KlassHandle field_holder = info.klass(); 301270114Sse intptr_t field_offset = info.field_offset(); 302270114Sse return init_field_MemberName(mname_oop, field_holder(), 303270114Sse info.access_flags(), 304270114Sse type, name, 305270114Sse field_offset, false /*is_setter*/); 306270114Sse#endif 307270114Sse} 308270114Sse 309270114Sse 310270114Sse// JVM 2.9 Special Methods: 311270114Sse// A method is signature polymorphic if and only if all of the following conditions hold : 312270114Sse// * It is declared in the java.lang.invoke.MethodHandle class. 313270114Sse// * It has a single formal parameter of type Object[]. 314270114Sse// * It has a return type of Object. 315270114Sse// * It has the ACC_VARARGS and ACC_NATIVE flags set. 316270114Ssebool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) { 317270114Sse if (klass == NULL) 318270114Sse return false; 319270114Sse // The following test will fail spuriously during bootstrap of MethodHandle itself: 320270114Sse // if (klass != SystemDictionary::MethodHandle_klass()) 321270114Sse // Test the name instead: 322270114Sse if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle()) 323270114Sse return false; 324270114Sse Symbol* poly_sig = vmSymbols::object_array_object_signature(); 325270114Sse Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig); 326270114Sse if (m == NULL) return false; 327270114Sse int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS; 328270114Sse int flags = m->access_flags().as_int(); 329270114Sse return (flags & required) == required; 330270114Sse} 331270114Sse 332270114Sse 333270114SseSymbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) { 334270114Sse assert(is_signature_polymorphic_intrinsic(iid), err_msg("iid=%d", iid)); 335270114Sse switch (iid) { 336270114Sse case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name(); 337270114Sse case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name(); 338270114Sse case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name(); 339270114Sse case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name(); 340270114Sse case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name(); 341270114Sse } 342270114Sse assert(false, ""); 343270114Sse return 0; 344270114Sse} 345270114Sse 346270114Sseint MethodHandles::signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid) { 347270114Sse switch (iid) { 348270114Sse case vmIntrinsics::_invokeBasic: return 0; 349270114Sse case vmIntrinsics::_linkToVirtual: return JVM_REF_invokeVirtual; 350270114Sse case vmIntrinsics::_linkToStatic: return JVM_REF_invokeStatic; 351270114Sse case vmIntrinsics::_linkToSpecial: return JVM_REF_invokeSpecial; 352270114Sse case vmIntrinsics::_linkToInterface: return JVM_REF_invokeInterface; 353270114Sse } 354270114Sse assert(false, err_msg("iid=%d", iid)); 355270114Sse return 0; 356270114Sse} 357270114Sse 358270114SsevmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Symbol* name) { 359270114Sse vmSymbols::SID name_id = vmSymbols::find_sid(name); 360270114Sse switch (name_id) { 361270114Sse // The ID _invokeGeneric stands for all non-static signature-polymorphic methods, except built-ins. 362270114Sse case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name): return vmIntrinsics::_invokeGeneric; 363270114Sse // The only built-in non-static signature-polymorphic method is MethodHandle.invokeBasic: 364270114Sse case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeBasic_name): return vmIntrinsics::_invokeBasic; 365270114Sse 366270114Sse // There is one static signature-polymorphic method for each JVM invocation mode. 367270114Sse case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual; 368270114Sse case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic; 369270114Sse case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial; 370270114Sse case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface; 371270114Sse } 372270114Sse 373270114Sse // Cover the case of invokeExact and any future variants of invokeFoo. 374270114Sse Klass* mh_klass = SystemDictionary::well_known_klass( 375270114Sse SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) ); 376270114Sse if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name)) 377270114Sse return vmIntrinsics::_invokeGeneric; 378270114Sse 379270114Sse // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against. 380270114Sse // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic. 381270114Sse return vmIntrinsics::_none; 382270114Sse} 383270114Sse 384270114SsevmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) { 385270114Sse if (klass != NULL && 386270114Sse klass->name() == vmSymbols::java_lang_invoke_MethodHandle()) { 387270114Sse vmIntrinsics::ID iid = signature_polymorphic_name_id(name); 388270114Sse if (iid != vmIntrinsics::_none) 389270114Sse return iid; 390270114Sse if (is_method_handle_invoke_name(klass, name)) 391270114Sse return vmIntrinsics::_invokeGeneric; 392270114Sse } 393270114Sse return vmIntrinsics::_none; 394270114Sse} 395270114Sse 396270114Sse 397270114Sse// convert the external string or reflective type to an internal signature 398270114SseSymbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) { 399270114Sse if (java_lang_invoke_MethodType::is_instance(type_str)) { 400270114Sse return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found, CHECK_NULL); 401270114Sse } else if (java_lang_Class::is_instance(type_str)) { 402270114Sse return java_lang_Class::as_signature(type_str, false, CHECK_NULL); 403270114Sse } else if (java_lang_String::is_instance(type_str)) { 404270114Sse if (intern_if_not_found) { 405270114Sse return java_lang_String::as_symbol(type_str, CHECK_NULL); 406270114Sse } else { 407270114Sse return java_lang_String::as_symbol_or_null(type_str); 408270114Sse } 409270114Sse } else { 410270114Sse THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", NULL); 411270114Sse } 412270114Sse} 413270114Sse 414270114Ssestatic const char OBJ_SIG[] = "Ljava/lang/Object;"; 415270114Sseenum { OBJ_SIG_LEN = 18 }; 416270114Sse 417270114Ssebool MethodHandles::is_basic_type_signature(Symbol* sig) { 418270114Sse assert(vmSymbols::object_signature()->utf8_length() == (int)OBJ_SIG_LEN, ""); 419270114Sse assert(vmSymbols::object_signature()->equals(OBJ_SIG), ""); 420270114Sse const int len = sig->utf8_length(); 421270114Sse for (int i = 0; i < len; i++) { 422270114Sse switch (sig->byte_at(i)) { 423270114Sse case 'L': 424270114Sse // only java/lang/Object is valid here 425270114Sse if (sig->index_of_at(i, OBJ_SIG, OBJ_SIG_LEN) != i) 426270114Sse return false; 427270114Sse i += OBJ_SIG_LEN-1; //-1 because of i++ in loop 428270114Sse continue; 429270114Sse case '(': case ')': case 'V': 430270114Sse case 'I': case 'J': case 'F': case 'D': 431270114Sse continue; 432270114Sse //case '[': 433270114Sse //case 'Z': case 'B': case 'C': case 'S': 434270114Sse default: 435270114Sse return false; 436270114Sse } 437270114Sse } 438270114Sse return true; 439270114Sse} 440270114Sse 441270114SseSymbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS) { 442270114Sse Symbol* bsig = NULL; 443270114Sse if (sig == NULL) { 444270114Sse return sig; 445270114Sse } else if (is_basic_type_signature(sig)) { 446270114Sse sig->increment_refcount(); 447270114Sse return sig; // that was easy 448270114Sse } else if (sig->byte_at(0) != '(') { 449270114Sse BasicType bt = char2type(sig->byte_at(0)); 450270114Sse if (is_subword_type(bt)) { 451270114Sse bsig = vmSymbols::int_signature(); 452270114Sse } else { 453270114Sse assert(bt == T_OBJECT || bt == T_ARRAY, "is_basic_type_signature was false"); 454270114Sse bsig = vmSymbols::object_signature(); 455270114Sse } 456270114Sse } else { 457270114Sse ResourceMark rm; 458270114Sse stringStream buffer(128); 459270114Sse buffer.put('('); 460270114Sse int arg_pos = 0, keep_arg_pos = -1; 461270114Sse if (keep_last_arg) 462270114Sse keep_arg_pos = ArgumentCount(sig).size() - 1; 463270114Sse for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { 464270114Sse BasicType bt = ss.type(); 465270114Sse size_t this_arg_pos = buffer.size(); 466270114Sse if (ss.at_return_type()) { 467270114Sse buffer.put(')'); 468270114Sse } 469270114Sse if (arg_pos == keep_arg_pos) { 470270114Sse buffer.write((char*) ss.raw_bytes(), 471270114Sse (int) ss.raw_length()); 472270114Sse } else if (bt == T_OBJECT || bt == T_ARRAY) { 473270114Sse buffer.write(OBJ_SIG, OBJ_SIG_LEN); 474270114Sse } else { 475270114Sse if (is_subword_type(bt)) 476270114Sse bt = T_INT; 477270114Sse buffer.put(type2char(bt)); 478270114Sse } 479270114Sse arg_pos++; 480270114Sse } 481270114Sse const char* sigstr = buffer.base(); 482270114Sse int siglen = (int) buffer.size(); 483270114Sse bsig = SymbolTable::new_symbol(sigstr, siglen, THREAD); 484270114Sse } 485270114Sse assert(is_basic_type_signature(bsig) || 486270114Sse // detune assert in case the injected argument is not a basic type: 487270114Sse keep_last_arg, ""); 488270114Sse return bsig; 489270114Sse} 490270114Sse 491270114Ssevoid MethodHandles::print_as_basic_type_signature_on(outputStream* st, 492270114Sse Symbol* sig, 493270114Sse bool keep_arrays, 494270114Sse bool keep_basic_names) { 495270114Sse st = st ? st : tty; 496270114Sse int len = sig->utf8_length(); 497270114Sse int array = 0; 498270114Sse bool prev_type = false; 499270114Sse for (int i = 0; i < len; i++) { 500270114Sse char ch = sig->byte_at(i); 501270114Sse switch (ch) { 502270114Sse case '(': case ')': 503270114Sse prev_type = false; 504270114Sse st->put(ch); 505270114Sse continue; 506270114Sse case '[': 507322225Sse if (!keep_basic_names && keep_arrays) 508322225Sse st->put(ch); 509322225Sse array++; 510322225Sse continue; 511322225Sse case 'L': 512270114Sse { 513322225Sse if (prev_type) st->put(','); 514322225Sse int start = i+1, slash = start; 515322225Sse while (++i < len && (ch = sig->byte_at(i)) != ';') { 516322225Sse if (ch == '/' || ch == '.' || ch == '$') slash = i+1; 517322225Sse } 518322225Sse if (slash < i) start = slash; 519270114Sse if (!keep_basic_names) { 520270114Sse st->put('L'); 521270114Sse } else { 522270114Sse for (int j = start; j < i; j++) 523270114Sse st->put(sig->byte_at(j)); 524270114Sse prev_type = true; 525270114Sse } 526270114Sse break; 527322225Sse } 528322225Sse default: 529322225Sse { 530270114Sse if (array && char2type(ch) != T_ILLEGAL && !keep_arrays) { 531270114Sse ch = '['; 532270114Sse array = 0; 533270114Sse } 534270114Sse if (prev_type) st->put(','); 535270114Sse const char* n = NULL; 536270114Sse if (keep_basic_names) 537270114Sse n = type2name(char2type(ch)); 538270114Sse if (n == NULL) { 539270114Sse // unknown letter, or we don't want to know its name 540270114Sse st->put(ch); 541270114Sse } else { 542270114Sse st->print(n); 543270114Sse prev_type = true; 544270114Sse } 545270114Sse break; 546270114Sse } 547270114Sse } 548270114Sse // Switch break goes here to take care of array suffix: 549270114Sse if (prev_type) { 550270114Sse while (array > 0) { 551270114Sse st->print("[]"); 552270114Sse --array; 553270114Sse } 554270114Sse } 555270114Sse array = 0; 556270114Sse } 557270114Sse} 558270114Sse 559270114Sse 560270114Sse 561270114Ssestatic oop object_java_mirror() { 562270114Sse return SystemDictionary::Object_klass()->java_mirror(); 563270114Sse} 564270114Sse 565270114Ssestatic oop field_name_or_null(Symbol* s) { 566270114Sse if (s == NULL) return NULL; 567270114Sse return StringTable::lookup(s); 568270114Sse} 569270114Sse 570270114Ssestatic oop field_signature_type_or_null(Symbol* s) { 571270114Sse if (s == NULL) return NULL; 572270114Sse BasicType bt = FieldType::basic_type(s); 573270114Sse if (is_java_primitive(bt)) { 574270114Sse assert(s->utf8_length() == 1, ""); 575270114Sse return java_lang_Class::primitive_mirror(bt); 576270114Sse } 577270114Sse // Here are some more short cuts for common types. 578270114Sse // They are optional, since reference types can be resolved lazily. 579270114Sse if (bt == T_OBJECT) { 580270114Sse if (s == vmSymbols::object_signature()) { 581270114Sse return object_java_mirror(); 582270114Sse } else if (s == vmSymbols::class_signature()) { 583270114Sse return SystemDictionary::Class_klass()->java_mirror(); 584270114Sse } else if (s == vmSymbols::string_signature()) { 585270114Sse return SystemDictionary::String_klass()->java_mirror(); 586270114Sse } 587270114Sse } 588270114Sse return NULL; 589270114Sse} 590270114Sse 591270114Sse 592270114Sse// An unresolved member name is a mere symbolic reference. 593270114Sse// Resolving it plants a vmtarget/vmindex in it, 594270114Sse// which refers directly to JVM internals. 595270114SseHandle MethodHandles::resolve_MemberName(Handle mname, TRAPS) { 596270114Sse Handle empty; 597270114Sse assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 598270114Sse 599270114Sse if (java_lang_invoke_MemberName::vmtarget(mname()) != NULL) { 600270114Sse // Already resolved. 601270114Sse DEBUG_ONLY(int vmindex = java_lang_invoke_MemberName::vmindex(mname())); 602270114Sse assert(vmindex >= Method::nonvirtual_vtable_index, ""); 603270114Sse return mname; 604270114Sse } 605270114Sse 606270114Sse Handle defc_oop(THREAD, java_lang_invoke_MemberName::clazz(mname())); 607270114Sse Handle name_str(THREAD, java_lang_invoke_MemberName::name( mname())); 608 Handle type_str(THREAD, java_lang_invoke_MemberName::type( mname())); 609 int flags = java_lang_invoke_MemberName::flags(mname()); 610 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 611 if (!ref_kind_is_valid(ref_kind)) { 612 THROW_MSG_(vmSymbols::java_lang_InternalError(), "obsolete MemberName format", empty); 613 } 614 615 DEBUG_ONLY(int old_vmindex); 616 assert((old_vmindex = java_lang_invoke_MemberName::vmindex(mname())) == 0, "clean input"); 617 618 if (defc_oop.is_null() || name_str.is_null() || type_str.is_null()) { 619 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve", empty); 620 } 621 622 instanceKlassHandle defc; 623 { 624 Klass* defc_klass = java_lang_Class::as_Klass(defc_oop()); 625 if (defc_klass == NULL) return empty; // a primitive; no resolution possible 626 if (!defc_klass->oop_is_instance()) { 627 if (!defc_klass->oop_is_array()) return empty; 628 defc_klass = SystemDictionary::Object_klass(); 629 } 630 defc = instanceKlassHandle(THREAD, defc_klass); 631 } 632 if (defc.is_null()) { 633 THROW_MSG_(vmSymbols::java_lang_InternalError(), "primitive class", empty); 634 } 635 defc->link_class(CHECK_(empty)); // possible safepoint 636 637 // convert the external string name to an internal symbol 638 TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str()); 639 if (name == NULL) return empty; // no such name 640 if (name == vmSymbols::class_initializer_name()) 641 return empty; // illegal name 642 643 vmIntrinsics::ID mh_invoke_id = vmIntrinsics::_none; 644 if ((flags & ALL_KINDS) == IS_METHOD && 645 (defc() == SystemDictionary::MethodHandle_klass()) && 646 (ref_kind == JVM_REF_invokeVirtual || 647 ref_kind == JVM_REF_invokeSpecial || 648 // static invocation mode is required for _linkToVirtual, etc.: 649 ref_kind == JVM_REF_invokeStatic)) { 650 vmIntrinsics::ID iid = signature_polymorphic_name_id(name); 651 if (iid != vmIntrinsics::_none && 652 ((ref_kind == JVM_REF_invokeStatic) == is_signature_polymorphic_static(iid))) { 653 // Virtual methods invoke and invokeExact, plus internal invokers like _invokeBasic. 654 // For a static reference it could an internal linkage routine like _linkToVirtual, etc. 655 mh_invoke_id = iid; 656 } 657 } 658 659 // convert the external string or reflective type to an internal signature 660 TempNewSymbol type = lookup_signature(type_str(), (mh_invoke_id != vmIntrinsics::_none), CHECK_(empty)); 661 if (type == NULL) return empty; // no such signature exists in the VM 662 663 // Time to do the lookup. 664 switch (flags & ALL_KINDS) { 665 case IS_METHOD: 666 { 667 CallInfo result; 668 bool do_dispatch = true; // default, neutral setting 669 { 670 assert(!HAS_PENDING_EXCEPTION, ""); 671 if (ref_kind == JVM_REF_invokeStatic) { 672 //do_dispatch = false; // no need, since statics are never dispatched 673 LinkResolver::resolve_static_call(result, 674 defc, name, type, KlassHandle(), false, false, THREAD); 675 } else if (ref_kind == JVM_REF_invokeInterface) { 676 LinkResolver::resolve_interface_call(result, Handle(), defc, 677 defc, name, type, KlassHandle(), false, false, THREAD); 678 } else if (mh_invoke_id != vmIntrinsics::_none) { 679 assert(!is_signature_polymorphic_static(mh_invoke_id), ""); 680 LinkResolver::resolve_handle_call(result, 681 defc, name, type, KlassHandle(), THREAD); 682 } else if (ref_kind == JVM_REF_invokeSpecial) { 683 do_dispatch = false; // force non-virtual linkage 684 LinkResolver::resolve_special_call(result, 685 defc, name, type, KlassHandle(), false, THREAD); 686 } else if (ref_kind == JVM_REF_invokeVirtual) { 687 LinkResolver::resolve_virtual_call(result, Handle(), defc, 688 defc, name, type, KlassHandle(), false, false, THREAD); 689 } else { 690 assert(false, err_msg("ref_kind=%d", ref_kind)); 691 } 692 if (HAS_PENDING_EXCEPTION) { 693 return empty; 694 } 695 } 696 return init_method_MemberName(mname, result, THREAD); 697 } 698 case IS_CONSTRUCTOR: 699 { 700 CallInfo result; 701 { 702 assert(!HAS_PENDING_EXCEPTION, ""); 703 if (name == vmSymbols::object_initializer_name()) { 704 LinkResolver::resolve_special_call(result, 705 defc, name, type, KlassHandle(), false, THREAD); 706 } else { 707 break; // will throw after end of switch 708 } 709 if (HAS_PENDING_EXCEPTION) { 710 return empty; 711 } 712 } 713 assert(result.is_statically_bound(), ""); 714 return init_method_MemberName(mname, result, THREAD); 715 } 716 case IS_FIELD: 717 { 718 // This is taken from LinkResolver::resolve_field, sans access checks. 719 fieldDescriptor fd; // find_field initializes fd if found 720 KlassHandle sel_klass(THREAD, InstanceKlass::cast(defc())->find_field(name, type, &fd)); 721 // check if field exists; i.e., if a klass containing the field def has been selected 722 if (sel_klass.is_null()) return empty; // should not happen 723 oop type = field_signature_type_or_null(fd.signature()); 724 oop name = field_name_or_null(fd.name()); 725 bool is_setter = (ref_kind_is_valid(ref_kind) && ref_kind_is_setter(ref_kind)); 726 mname = Handle(THREAD, 727 init_field_MemberName(mname, sel_klass, 728 fd.access_flags(), type, name, fd.offset(), is_setter)); 729 return mname; 730 } 731 default: 732 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty); 733 } 734 735 return empty; 736} 737 738// Conversely, a member name which is only initialized from JVM internals 739// may have null defc, name, and type fields. 740// Resolving it plants a vmtarget/vmindex in it, 741// which refers directly to JVM internals. 742void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) { 743 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 744 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 745 int vmindex = java_lang_invoke_MemberName::vmindex(mname()); 746 if (vmtarget == NULL) { 747 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); 748 } 749 750 bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL); 751 bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL); 752 bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL); 753 int flags = java_lang_invoke_MemberName::flags(mname()); 754 755 if (suppress != 0) { 756 if (suppress & _suppress_defc) have_defc = true; 757 if (suppress & _suppress_name) have_name = true; 758 if (suppress & _suppress_type) have_type = true; 759 } 760 761 if (have_defc && have_name && have_type) return; // nothing needed 762 763 switch (flags & ALL_KINDS) { 764 case IS_METHOD: 765 case IS_CONSTRUCTOR: 766 { 767 assert(vmtarget->is_method(), "method or constructor vmtarget is Method*"); 768 methodHandle m(THREAD, (Method*)vmtarget); 769 DEBUG_ONLY(vmtarget = NULL); // safety 770 if (m.is_null()) break; 771 if (!have_defc) { 772 InstanceKlass* defc = m->method_holder(); 773 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); 774 } 775 if (!have_name) { 776 //not java_lang_String::create_from_symbol; let's intern member names 777 Handle name = StringTable::intern(m->name(), CHECK); 778 java_lang_invoke_MemberName::set_name(mname(), name()); 779 } 780 if (!have_type) { 781 Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK); 782 java_lang_invoke_MemberName::set_type(mname(), type()); 783 } 784 return; 785 } 786 case IS_FIELD: 787 { 788 // This is taken from LinkResolver::resolve_field, sans access checks. 789 assert(vmtarget->is_klass(), "field vmtarget is Klass*"); 790 if (!((Klass*) vmtarget)->oop_is_instance()) break; 791 instanceKlassHandle defc(THREAD, (Klass*) vmtarget); 792 DEBUG_ONLY(vmtarget = NULL); // safety 793 bool is_static = ((flags & JVM_ACC_STATIC) != 0); 794 fieldDescriptor fd; // find_field initializes fd if found 795 if (!defc->find_field_from_offset(vmindex, is_static, &fd)) 796 break; // cannot expand 797 if (!have_defc) { 798 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); 799 } 800 if (!have_name) { 801 //not java_lang_String::create_from_symbol; let's intern member names 802 Handle name = StringTable::intern(fd.name(), CHECK); 803 java_lang_invoke_MemberName::set_name(mname(), name()); 804 } 805 if (!have_type) { 806 // If it is a primitive field type, don't mess with short strings like "I". 807 Handle type = field_signature_type_or_null(fd.signature()); 808 if (type.is_null()) { 809 java_lang_String::create_from_symbol(fd.signature(), CHECK); 810 } 811 java_lang_invoke_MemberName::set_type(mname(), type()); 812 } 813 return; 814 } 815 } 816 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); 817} 818 819int MethodHandles::find_MemberNames(KlassHandle k, 820 Symbol* name, Symbol* sig, 821 int mflags, KlassHandle caller, 822 int skip, objArrayHandle results) { 823 // %%% take caller into account! 824 825 Thread* thread = Thread::current(); 826 827 if (k.is_null() || !k->oop_is_instance()) return -1; 828 829 int rfill = 0, rlimit = results->length(), rskip = skip; 830 // overflow measurement: 831 int overflow = 0, overflow_limit = MAX2(1000, rlimit); 832 833 int match_flags = mflags; 834 bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0); 835 bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0); 836 bool local_only = !(search_superc | search_intfc); 837 bool classes_only = false; 838 839 if (name != NULL) { 840 if (name->utf8_length() == 0) return 0; // a match is not possible 841 } 842 if (sig != NULL) { 843 if (sig->utf8_length() == 0) return 0; // a match is not possible 844 if (sig->byte_at(0) == '(') 845 match_flags &= ~(IS_FIELD | IS_TYPE); 846 else 847 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD); 848 } 849 850 if ((match_flags & IS_TYPE) != 0) { 851 // NYI, and Core Reflection works quite well for this query 852 } 853 854 if ((match_flags & IS_FIELD) != 0) { 855 for (FieldStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) { 856 if (name != NULL && st.name() != name) 857 continue; 858 if (sig != NULL && st.signature() != sig) 859 continue; 860 // passed the filters 861 if (rskip > 0) { 862 --rskip; 863 } else if (rfill < rlimit) { 864 Handle result(thread, results->obj_at(rfill++)); 865 if (!java_lang_invoke_MemberName::is_instance(result())) 866 return -99; // caller bug! 867 oop type = field_signature_type_or_null(st.signature()); 868 oop name = field_name_or_null(st.name()); 869 oop saved = MethodHandles::init_field_MemberName(result, st.klass(), 870 st.access_flags(), type, name, 871 st.offset()); 872 if (saved != result()) 873 results->obj_at_put(rfill-1, saved); // show saved instance to user 874 } else if (++overflow >= overflow_limit) { 875 match_flags = 0; break; // got tired of looking at overflow 876 } 877 } 878 } 879 880 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { 881 // watch out for these guys: 882 Symbol* init_name = vmSymbols::object_initializer_name(); 883 Symbol* clinit_name = vmSymbols::class_initializer_name(); 884 if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit> 885 bool negate_name_test = false; 886 // fix name so that it captures the intention of IS_CONSTRUCTOR 887 if (!(match_flags & IS_METHOD)) { 888 // constructors only 889 if (name == NULL) { 890 name = init_name; 891 } else if (name != init_name) { 892 return 0; // no constructors of this method name 893 } 894 } else if (!(match_flags & IS_CONSTRUCTOR)) { 895 // methods only 896 if (name == NULL) { 897 name = init_name; 898 negate_name_test = true; // if we see the name, we *omit* the entry 899 } else if (name == init_name) { 900 return 0; // no methods of this constructor name 901 } 902 } else { 903 // caller will accept either sort; no need to adjust name 904 } 905 for (MethodStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) { 906 Method* m = st.method(); 907 Symbol* m_name = m->name(); 908 if (m_name == clinit_name) 909 continue; 910 if (name != NULL && ((m_name != name) ^ negate_name_test)) 911 continue; 912 if (sig != NULL && m->signature() != sig) 913 continue; 914 // passed the filters 915 if (rskip > 0) { 916 --rskip; 917 } else if (rfill < rlimit) { 918 Handle result(thread, results->obj_at(rfill++)); 919 if (!java_lang_invoke_MemberName::is_instance(result())) 920 return -99; // caller bug! 921 oop saved = MethodHandles::init_method_MemberName(result, m, true, NULL); 922 if (saved != result()) 923 results->obj_at_put(rfill-1, saved); // show saved instance to user 924 } else if (++overflow >= overflow_limit) { 925 match_flags = 0; break; // got tired of looking at overflow 926 } 927 } 928 } 929 930 // return number of elements we at leasted wanted to initialize 931 return rfill + overflow; 932} 933 934//------------------------------------------------------------------------------ 935// MemberNameTable 936// 937 938MemberNameTable::MemberNameTable() : GrowableArray<jweak>(10, true) { 939 assert_locked_or_safepoint(MemberNameTable_lock); 940} 941 942MemberNameTable::~MemberNameTable() { 943 assert_locked_or_safepoint(MemberNameTable_lock); 944 int len = this->length(); 945 946 for (int idx = 0; idx < len; idx++) { 947 jweak ref = this->at(idx); 948 JNIHandles::destroy_weak_global(ref); 949 } 950} 951 952// Return entry index if found, return -1 otherwise. 953int MemberNameTable::find_member_name(oop mem_name) { 954 assert_locked_or_safepoint(MemberNameTable_lock); 955 int len = this->length(); 956 957 for (int idx = 0; idx < len; idx++) { 958 jweak ref = this->at(idx); 959 oop entry = JNIHandles::resolve(ref); 960 if (entry == mem_name) { 961 return idx; 962 } 963 } 964 return -1; 965} 966 967void MemberNameTable::add_member_name(jweak mem_name_wref) { 968 assert_locked_or_safepoint(MemberNameTable_lock); 969 oop mem_name = JNIHandles::resolve(mem_name_wref); 970 971 // Each member name may appear just once: add only if not found 972 if (find_member_name(mem_name) == -1) { 973 this->append(mem_name_wref); 974 } 975} 976 977#if INCLUDE_JVMTI 978oop MemberNameTable::find_member_name_by_method(Method* old_method) { 979 assert_locked_or_safepoint(MemberNameTable_lock); 980 oop found = NULL; 981 int len = this->length(); 982 983 for (int idx = 0; idx < len; idx++) { 984 oop mem_name = JNIHandles::resolve(this->at(idx)); 985 if (mem_name == NULL) { 986 continue; 987 } 988 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mem_name); 989 if (method == old_method) { 990 found = mem_name; 991 break; 992 } 993 } 994 return found; 995} 996 997// It is called at safepoint only 998void MemberNameTable::adjust_method_entries(Method** old_methods, Method** new_methods, 999 int methods_length, bool *trace_name_printed) { 1000 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); 1001 // search the MemberNameTable for uses of either obsolete or EMCP methods 1002 for (int j = 0; j < methods_length; j++) { 1003 Method* old_method = old_methods[j]; 1004 Method* new_method = new_methods[j]; 1005 oop mem_name = find_member_name_by_method(old_method); 1006 if (mem_name != NULL) { 1007 java_lang_invoke_MemberName::adjust_vmtarget(mem_name, new_method); 1008 1009 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { 1010 if (!(*trace_name_printed)) { 1011 // RC_TRACE_MESG macro has an embedded ResourceMark 1012 RC_TRACE_MESG(("adjust: name=%s", 1013 old_method->method_holder()->external_name())); 1014 *trace_name_printed = true; 1015 } 1016 // RC_TRACE macro has an embedded ResourceMark 1017 RC_TRACE(0x00400000, ("MemberName method update: %s(%s)", 1018 new_method->name()->as_C_string(), 1019 new_method->signature()->as_C_string())); 1020 } 1021 } 1022 } 1023} 1024#endif // INCLUDE_JVMTI 1025 1026// 1027// Here are the native methods in java.lang.invoke.MethodHandleNatives 1028// They are the private interface between this JVM and the HotSpot-specific 1029// Java code that implements JSR 292 method handles. 1030// 1031// Note: We use a JVM_ENTRY macro to define each of these, for this is the way 1032// that intrinsic (non-JNI) native methods are defined in HotSpot. 1033// 1034 1035JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) { 1036 switch (which) { 1037 case MethodHandles::GC_COUNT_GWT: 1038#ifdef COMPILER2 1039 return true; 1040#else 1041 return false; 1042#endif 1043 } 1044 return 0; 1045} 1046JVM_END 1047 1048#ifndef PRODUCT 1049#define EACH_NAMED_CON(template, requirement) \ 1050 template(MethodHandles,GC_COUNT_GWT) \ 1051 template(java_lang_invoke_MemberName,MN_IS_METHOD) \ 1052 template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \ 1053 template(java_lang_invoke_MemberName,MN_IS_FIELD) \ 1054 template(java_lang_invoke_MemberName,MN_IS_TYPE) \ 1055 template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \ 1056 template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \ 1057 template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \ 1058 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \ 1059 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \ 1060 template(MethodHandles,GC_LAMBDA_SUPPORT) \ 1061 /*end*/ 1062 1063#define IGNORE_REQ(req_expr) /* req_expr */ 1064#define ONE_PLUS(scope,value) 1+ 1065static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0; 1066#define VALUE_COMMA(scope,value) scope::value, 1067static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 }; 1068#define STRING_NULL(scope,value) #value "\0" 1069static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) }; 1070 1071static bool advertise_con_value(int which) { 1072 if (which < 0) return false; 1073 bool ok = true; 1074 int count = 0; 1075#define INC_COUNT(scope,value) \ 1076 ++count; 1077#define CHECK_REQ(req_expr) \ 1078 if (which < count) return ok; \ 1079 ok = (req_expr); 1080 EACH_NAMED_CON(INC_COUNT, CHECK_REQ); 1081#undef INC_COUNT 1082#undef CHECK_REQ 1083 assert(count == con_value_count, ""); 1084 if (which < count) return ok; 1085 return false; 1086} 1087 1088#undef ONE_PLUS 1089#undef VALUE_COMMA 1090#undef STRING_NULL 1091#undef EACH_NAMED_CON 1092#endif // PRODUCT 1093 1094JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { 1095#ifndef PRODUCT 1096 if (advertise_con_value(which)) { 1097 assert(which >= 0 && which < con_value_count, ""); 1098 int con = con_values[which]; 1099 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh)); 1100 if (box.not_null() && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) { 1101 const char* str = &con_names[0]; 1102 for (int i = 0; i < which; i++) 1103 str += strlen(str) + 1; // skip name and null 1104 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint 1105 box->obj_at_put(0, name); 1106 } 1107 return con; 1108 } 1109#endif 1110 return 0; 1111} 1112JVM_END 1113 1114// void init(MemberName self, AccessibleObject ref) 1115JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) { 1116 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } 1117 if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); } 1118 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1119 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); 1120 MethodHandles::init_MemberName(mname, target); 1121} 1122JVM_END 1123 1124// void expand(MemberName self) 1125JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1126 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } 1127 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1128 MethodHandles::expand_MemberName(mname, 0, CHECK); 1129} 1130JVM_END 1131 1132// void resolve(MemberName self, Class<?> caller) 1133JVM_ENTRY(jobject, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { 1134 if (mname_jh == NULL) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); } 1135 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1136 1137 // The trusted Java code that calls this method should already have performed 1138 // access checks on behalf of the given caller. But, we can verify this. 1139 if (VerifyMethodHandles && caller_jh != NULL && 1140 java_lang_invoke_MemberName::clazz(mname()) != NULL) { 1141 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname())); 1142 if (reference_klass != NULL) { 1143 // Emulate LinkResolver::check_klass_accessability. 1144 Klass* caller = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh)); 1145 if (!Reflection::verify_class_access(caller, 1146 reference_klass, 1147 true)) { 1148 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), reference_klass->external_name()); 1149 } 1150 } 1151 } 1152 1153 Handle resolved = MethodHandles::resolve_MemberName(mname, CHECK_NULL); 1154 if (resolved.is_null()) { 1155 int flags = java_lang_invoke_MemberName::flags(mname()); 1156 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 1157 if (!MethodHandles::ref_kind_is_valid(ref_kind)) { 1158 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format"); 1159 } 1160 if ((flags & ALL_KINDS) == IS_FIELD) { 1161 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "field resolution failed"); 1162 } else if ((flags & ALL_KINDS) == IS_METHOD || 1163 (flags & ALL_KINDS) == IS_CONSTRUCTOR) { 1164 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "method resolution failed"); 1165 } else { 1166 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed"); 1167 } 1168 } 1169 1170 return JNIHandles::make_local(THREAD, resolved()); 1171} 1172JVM_END 1173 1174static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) { 1175 if (mname == NULL || 1176 java_lang_invoke_MemberName::vmtarget(mname) == NULL) { 1177 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved"); 1178 } else { 1179 int flags = java_lang_invoke_MemberName::flags(mname); 1180 if ((flags & IS_FIELD) != 0 && 1181 (must_be_static 1182 ? (flags & JVM_ACC_STATIC) != 0 1183 : (flags & JVM_ACC_STATIC) == 0)) { 1184 int vmindex = java_lang_invoke_MemberName::vmindex(mname); 1185 return (jlong) vmindex; 1186 } 1187 } 1188 const char* msg = (must_be_static ? "static field required" : "non-static field required"); 1189 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg); 1190 return 0; 1191} 1192 1193JVM_ENTRY(jlong, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1194 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD); 1195} 1196JVM_END 1197 1198JVM_ENTRY(jlong, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1199 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD); 1200} 1201JVM_END 1202 1203JVM_ENTRY(jobject, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1204 // use the other function to perform sanity checks: 1205 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL); 1206 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh)); 1207 return JNIHandles::make_local(THREAD, clazz); 1208} 1209JVM_END 1210 1211JVM_ENTRY(jobject, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1212 if (mname_jh == NULL) return NULL; 1213 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1214 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname()); 1215 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 1216 objArrayHandle result = oopFactory::new_objArray(SystemDictionary::Object_klass(), 2, CHECK_NULL); 1217 jvalue vmindex_value; vmindex_value.j = (long)vmindex; 1218 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL); 1219 result->obj_at_put(0, x); 1220 x = NULL; 1221 if (vmtarget == NULL) { 1222 x = NULL; 1223 } else if (vmtarget->is_klass()) { 1224 x = ((Klass*) vmtarget)->java_mirror(); 1225 } else if (vmtarget->is_method()) { 1226 Handle mname2 = MethodHandles::new_MemberName(CHECK_NULL); 1227 x = MethodHandles::init_method_MemberName(mname2, (Method*)vmtarget, false, NULL); 1228 } 1229 result->obj_at_put(1, x); 1230 return JNIHandles::make_local(env, result()); 1231} 1232JVM_END 1233 1234 1235 1236// static native int getMembers(Class<?> defc, String matchName, String matchSig, 1237// int matchFlags, Class<?> caller, int skip, MemberName[] results); 1238JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls, 1239 jclass clazz_jh, jstring name_jh, jstring sig_jh, 1240 int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) { 1241 if (clazz_jh == NULL || results_jh == NULL) return -1; 1242 KlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz_jh))); 1243 1244 objArrayHandle results(THREAD, (objArrayOop) JNIHandles::resolve(results_jh)); 1245 if (results.is_null() || !results->is_objArray()) return -1; 1246 1247 TempNewSymbol name = NULL; 1248 TempNewSymbol sig = NULL; 1249 if (name_jh != NULL) { 1250 name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh)); 1251 if (name == NULL) return 0; // a match is not possible 1252 } 1253 if (sig_jh != NULL) { 1254 sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh)); 1255 if (sig == NULL) return 0; // a match is not possible 1256 } 1257 1258 KlassHandle caller; 1259 if (caller_jh != NULL) { 1260 oop caller_oop = JNIHandles::resolve_non_null(caller_jh); 1261 if (!java_lang_Class::is_instance(caller_oop)) return -1; 1262 caller = KlassHandle(THREAD, java_lang_Class::as_Klass(caller_oop)); 1263 } 1264 1265 if (name != NULL && sig != NULL && results.not_null()) { 1266 // try a direct resolve 1267 // %%% TO DO 1268 } 1269 1270 int res = MethodHandles::find_MemberNames(k, name, sig, mflags, 1271 caller, skip, results); 1272 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks 1273 return res; 1274} 1275JVM_END 1276 1277JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { 1278 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); 1279 Handle target (THREAD, JNIHandles::resolve(target_jh)); 1280 { 1281 // Walk all nmethods depending on this call site. 1282 MutexLocker mu(Compile_lock, thread); 1283 Universe::flush_dependents_on(call_site, target); 1284 java_lang_invoke_CallSite::set_target(call_site(), target()); 1285 } 1286} 1287JVM_END 1288 1289JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { 1290 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); 1291 Handle target (THREAD, JNIHandles::resolve(target_jh)); 1292 { 1293 // Walk all nmethods depending on this call site. 1294 MutexLocker mu(Compile_lock, thread); 1295 Universe::flush_dependents_on(call_site, target); 1296 java_lang_invoke_CallSite::set_target_volatile(call_site(), target()); 1297 } 1298} 1299JVM_END 1300 1301/// JVM_RegisterMethodHandleMethods 1302 1303#undef CS // Solaris builds complain 1304 1305#define LANG "Ljava/lang/" 1306#define JLINV "Ljava/lang/invoke/" 1307 1308#define OBJ LANG"Object;" 1309#define CLS LANG"Class;" 1310#define STRG LANG"String;" 1311#define CS JLINV"CallSite;" 1312#define MT JLINV"MethodType;" 1313#define MH JLINV"MethodHandle;" 1314#define MEM JLINV"MemberName;" 1315 1316#define CC (char*) /*cast a literal from (const char*)*/ 1317#define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 1318 1319// These are the native methods on java.lang.invoke.MethodHandleNatives. 1320static JNINativeMethod required_methods_JDK8[] = { 1321 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, 1322 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, 1323 {CC"resolve", CC"("MEM""CLS")"MEM, FN_PTR(MHN_resolve_Mem)}, 1324 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, 1325 // static native int getNamedCon(int which, Object[] name) 1326 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, 1327 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 1328 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 1329 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)}, 1330 {CC"objectFieldOffset", CC"("MEM")J", FN_PTR(MHN_objectFieldOffset)}, 1331 {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)}, 1332 {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)}, 1333 {CC"staticFieldOffset", CC"("MEM")J", FN_PTR(MHN_staticFieldOffset)}, 1334 {CC"staticFieldBase", CC"("MEM")"OBJ, FN_PTR(MHN_staticFieldBase)}, 1335 {CC"getMemberVMInfo", CC"("MEM")"OBJ, FN_PTR(MHN_getMemberVMInfo)} 1336}; 1337 1338// This one function is exported, used by NativeLookup. 1339 1340JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { 1341 if (!EnableInvokeDynamic) { 1342 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); 1343 return; // bind nothing 1344 } 1345 1346 assert(!MethodHandles::enabled(), "must not be enabled"); 1347 bool enable_MH = true; 1348 1349 jclass MH_class = NULL; 1350 if (SystemDictionary::MethodHandle_klass() == NULL) { 1351 enable_MH = false; 1352 } else { 1353 oop mirror = SystemDictionary::MethodHandle_klass()->java_mirror(); 1354 MH_class = (jclass) JNIHandles::make_local(env, mirror); 1355 } 1356 1357 int status; 1358 1359 if (enable_MH) { 1360 ThreadToNativeFromVM ttnfv(thread); 1361 1362 status = env->RegisterNatives(MHN_class, required_methods_JDK8, sizeof(required_methods_JDK8)/sizeof(JNINativeMethod)); 1363 if (status != JNI_OK || env->ExceptionOccurred()) { 1364 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 1365 enable_MH = false; 1366 env->ExceptionClear(); 1367 } 1368 } 1369 1370 if (TraceInvokeDynamic) { 1371 tty->print_cr("MethodHandle support loaded (using LambdaForms)"); 1372 } 1373 1374 if (enable_MH) { 1375 MethodHandles::generate_adapters(); 1376 MethodHandles::set_enabled(true); 1377 } 1378} 1379JVM_END 1380