jrose@1145: /* twisti@2436: * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. jrose@1145: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jrose@1145: * jrose@1145: * This code is free software; you can redistribute it and/or modify it jrose@1145: * under the terms of the GNU General Public License version 2 only, as jrose@1145: * published by the Free Software Foundation. jrose@1145: * jrose@1145: * This code is distributed in the hope that it will be useful, but WITHOUT jrose@1145: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jrose@1145: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jrose@1145: * version 2 for more details (a copy is included in the LICENSE file that jrose@1145: * accompanied this code). jrose@1145: * jrose@1145: * You should have received a copy of the GNU General Public License version jrose@1145: * 2 along with this work; if not, write to the Free Software Foundation, jrose@1145: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jrose@1145: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. jrose@1145: * jrose@1145: */ jrose@1145: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/symbolTable.hpp" stefank@2314: #include "interpreter/interpreter.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "memory/oopFactory.hpp" stefank@2314: #include "prims/methodHandles.hpp" stefank@2314: #include "runtime/javaCalls.hpp" stefank@2314: #include "runtime/reflection.hpp" stefank@2314: #include "runtime/signature.hpp" stefank@2314: #include "runtime/stubRoutines.hpp" stefank@2314: jrose@1145: /* jrose@1145: * JSR 292 reference implementation: method handles jrose@1145: */ jrose@1145: jrose@1145: bool MethodHandles::_enabled = false; // set true after successful native linkage jrose@1145: jrose@1145: MethodHandleEntry* MethodHandles::_entries[MethodHandles::_EK_LIMIT] = {NULL}; jrose@1145: const char* MethodHandles::_entry_names[_EK_LIMIT+1] = { jrose@1474: "raise_exception", jrose@1145: "invokestatic", // how a MH emulates invokestatic jrose@1145: "invokespecial", // ditto for the other invokes... jrose@1145: "invokevirtual", jrose@1145: "invokeinterface", jrose@1145: "bound_ref", // these are for BMH... jrose@1145: "bound_int", jrose@1145: "bound_long", jrose@1145: "bound_ref_direct", // (direct versions have a direct methodOop) jrose@1145: "bound_int_direct", jrose@1145: "bound_long_direct", jrose@1145: jrose@1145: // starting at _adapter_mh_first: jrose@1145: "adapter_retype_only", // these are for AMH... jrose@1474: "adapter_retype_raw", jrose@1145: "adapter_check_cast", jrose@1145: "adapter_prim_to_prim", jrose@1145: "adapter_ref_to_prim", jrose@1145: "adapter_prim_to_ref", jrose@1145: "adapter_swap_args", jrose@1145: "adapter_rot_args", jrose@1145: "adapter_dup_args", jrose@1145: "adapter_drop_args", jrose@1145: "adapter_collect_args", jrose@1145: "adapter_spread_args", jrose@1145: "adapter_flyby", jrose@1145: "adapter_ricochet", jrose@1145: jrose@1145: // optimized adapter types: jrose@1145: "adapter_swap_args/1", jrose@1145: "adapter_swap_args/2", jrose@1145: "adapter_rot_args/1,up", jrose@1145: "adapter_rot_args/1,down", jrose@1145: "adapter_rot_args/2,up", jrose@1145: "adapter_rot_args/2,down", jrose@1145: "adapter_prim_to_prim/i2i", jrose@1145: "adapter_prim_to_prim/l2i", jrose@1145: "adapter_prim_to_prim/d2f", jrose@1145: "adapter_prim_to_prim/i2l", jrose@1145: "adapter_prim_to_prim/f2d", jrose@1145: "adapter_ref_to_prim/unboxi", jrose@1145: "adapter_ref_to_prim/unboxl", jrose@1145: "adapter_spread_args/0", jrose@1145: "adapter_spread_args/1", jrose@1145: "adapter_spread_args/more", jrose@1145: jrose@1145: NULL jrose@1145: }; jrose@1145: twisti@1734: // Adapters. twisti@1734: MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; twisti@1734: int MethodHandles::_adapter_code_size = StubRoutines::method_handles_adapters_code_size; twisti@1734: jrose@1474: jobject MethodHandles::_raise_exception_method; jrose@1474: jrose@1145: #ifdef ASSERT jrose@1145: bool MethodHandles::spot_check_entry_names() { jrose@1145: assert(!strcmp(entry_name(_invokestatic_mh), "invokestatic"), ""); jrose@1145: assert(!strcmp(entry_name(_bound_ref_mh), "bound_ref"), ""); jrose@1145: assert(!strcmp(entry_name(_adapter_retype_only), "adapter_retype_only"), ""); jrose@1145: assert(!strcmp(entry_name(_adapter_ricochet), "adapter_ricochet"), ""); jrose@1145: assert(!strcmp(entry_name(_adapter_opt_unboxi), "adapter_ref_to_prim/unboxi"), ""); jrose@1145: return true; jrose@1145: } jrose@1145: #endif jrose@1145: twisti@1734: twisti@1734: //------------------------------------------------------------------------------ twisti@1734: // MethodHandles::generate_adapters twisti@1734: // twisti@2436: void MethodHandles::generate_adapters() { twisti@2698: if (!EnableInvokeDynamic || SystemDictionary::MethodHandle_klass() == NULL) return; twisti@1734: twisti@1734: assert(_adapter_code == NULL, "generate only once"); twisti@1734: twisti@1734: ResourceMark rm; twisti@1734: TraceTime timer("MethodHandles adapters generation", TraceStartupTime); twisti@1734: _adapter_code = MethodHandlesAdapterBlob::create(_adapter_code_size); twisti@1734: if (_adapter_code == NULL) twisti@1734: vm_exit_out_of_memory(_adapter_code_size, "CodeCache: no room for MethodHandles adapters"); twisti@2103: CodeBuffer code(_adapter_code); twisti@1734: MethodHandlesAdapterGenerator g(&code); twisti@2436: g.generate(); twisti@1734: } twisti@1734: twisti@1734: twisti@1734: //------------------------------------------------------------------------------ twisti@1734: // MethodHandlesAdapterGenerator::generate twisti@1734: // twisti@2436: void MethodHandlesAdapterGenerator::generate() { twisti@1734: // Generate generic method handle adapters. twisti@1734: for (MethodHandles::EntryKind ek = MethodHandles::_EK_FIRST; twisti@1734: ek < MethodHandles::_EK_LIMIT; twisti@1734: ek = MethodHandles::EntryKind(1 + (int)ek)) { twisti@1734: StubCodeMark mark(this, "MethodHandle", MethodHandles::entry_name(ek)); twisti@2436: MethodHandles::generate_method_handle_stub(_masm, ek); twisti@1734: } twisti@1734: } twisti@1734: twisti@1734: jrose@1145: void MethodHandles::set_enabled(bool z) { jrose@1145: if (_enabled != z) { twisti@2698: guarantee(z && EnableInvokeDynamic, "can only enable once, and only if -XX:+EnableInvokeDynamic"); jrose@1145: _enabled = z; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // Note: A method which does not have a TRAPS argument cannot block in the GC jrose@1145: // or throw exceptions. Such methods are used in this file to do something quick jrose@1145: // and local, like parse a data structure. For speed, such methods work on plain jrose@1145: // oops, not handles. Trapping methods uniformly operate on handles. jrose@1145: jrose@1145: methodOop MethodHandles::decode_vmtarget(oop vmtarget, int vmindex, oop mtype, jrose@1145: klassOop& receiver_limit_result, int& decode_flags_result) { jrose@1145: if (vmtarget == NULL) return NULL; jrose@1145: assert(methodOopDesc::nonvirtual_vtable_index < 0, "encoding"); jrose@1145: if (vmindex < 0) { jrose@1145: // this DMH performs no dispatch; it is directly bound to a methodOop jrose@1145: // A MemberName may either be directly bound to a methodOop, jrose@1145: // or it may use the klass/index form; both forms mean the same thing. jrose@1145: methodOop m = decode_methodOop(methodOop(vmtarget), decode_flags_result); jrose@1145: if ((decode_flags_result & _dmf_has_receiver) != 0 jrose@2639: && java_lang_invoke_MethodType::is_instance(mtype)) { jrose@1145: // Extract receiver type restriction from mtype.ptypes[0]. jrose@2639: objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(mtype); jrose@1145: oop ptype0 = (ptypes == NULL || ptypes->length() < 1) ? oop(NULL) : ptypes->obj_at(0); jrose@1145: if (java_lang_Class::is_instance(ptype0)) jrose@1145: receiver_limit_result = java_lang_Class::as_klassOop(ptype0); jrose@1145: } jrose@1145: if (vmindex == methodOopDesc::nonvirtual_vtable_index) { jrose@1145: // this DMH can be an "invokespecial" version jrose@1145: decode_flags_result &= ~_dmf_does_dispatch; jrose@1145: } else { jrose@1145: assert(vmindex == methodOopDesc::invalid_vtable_index, "random vmindex?"); jrose@1145: } jrose@1145: return m; jrose@1145: } else { twisti@1573: assert(vmtarget->is_klass(), "must be class or interface"); jrose@1145: decode_flags_result |= MethodHandles::_dmf_does_dispatch; twisti@1573: decode_flags_result |= MethodHandles::_dmf_has_receiver; jrose@1145: receiver_limit_result = (klassOop)vmtarget; jrose@1145: Klass* tk = Klass::cast((klassOop)vmtarget); jrose@1145: if (tk->is_interface()) { jrose@1145: // an itable linkage is jrose@1145: decode_flags_result |= MethodHandles::_dmf_from_interface; jrose@1145: return klassItable::method_for_itable_index((klassOop)vmtarget, vmindex); jrose@1145: } else { jrose@1145: if (!tk->oop_is_instance()) never@1577: tk = instanceKlass::cast(SystemDictionary::Object_klass()); jrose@1145: return ((instanceKlass*)tk)->method_at_vtable(vmindex); jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // MemberName and DirectMethodHandle have the same linkage to the JVM internals. jrose@1145: // (MemberName is the non-operational name used for queries and setup.) jrose@1145: jrose@1145: methodOop MethodHandles::decode_DirectMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { jrose@2639: oop vmtarget = java_lang_invoke_DirectMethodHandle::vmtarget(mh); jrose@2639: int vmindex = java_lang_invoke_DirectMethodHandle::vmindex(mh); jrose@2639: oop mtype = java_lang_invoke_DirectMethodHandle::type(mh); jrose@1145: return decode_vmtarget(vmtarget, vmindex, mtype, receiver_limit_result, decode_flags_result); jrose@1145: } jrose@1145: jrose@1145: methodOop MethodHandles::decode_BoundMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { jrose@2639: assert(java_lang_invoke_BoundMethodHandle::is_instance(mh), ""); jrose@1474: assert(mh->klass() != SystemDictionary::AdapterMethodHandle_klass(), ""); jrose@1145: for (oop bmh = mh;;) { jrose@1145: // Bound MHs can be stacked to bind several arguments. jrose@2639: oop target = java_lang_invoke_MethodHandle::vmtarget(bmh); jrose@1145: if (target == NULL) return NULL; jrose@1145: decode_flags_result |= MethodHandles::_dmf_binds_argument; jrose@1145: klassOop tk = target->klass(); jrose@1145: if (tk == SystemDictionary::BoundMethodHandle_klass()) { jrose@1145: bmh = target; jrose@1145: continue; jrose@1145: } else { jrose@2639: if (java_lang_invoke_MethodHandle::is_subclass(tk)) { jrose@1145: //assert(tk == SystemDictionary::DirectMethodHandle_klass(), "end of BMH chain must be DMH"); jrose@1145: return decode_MethodHandle(target, receiver_limit_result, decode_flags_result); jrose@1145: } else { jrose@1145: // Optimized case: binding a receiver to a non-dispatched DMH jrose@1145: // short-circuits directly to the methodOop. jrose@1474: // (It might be another argument besides a receiver also.) jrose@1145: assert(target->is_method(), "must be a simple method"); twisti@1573: decode_flags_result |= MethodHandles::_dmf_binds_method; jrose@1145: methodOop m = (methodOop) target; twisti@1573: if (!m->is_static()) twisti@1573: decode_flags_result |= MethodHandles::_dmf_has_receiver; jrose@1145: return m; jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: methodOop MethodHandles::decode_AdapterMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { jrose@1145: assert(mh->klass() == SystemDictionary::AdapterMethodHandle_klass(), ""); jrose@1145: for (oop amh = mh;;) { jrose@1145: // Adapter MHs can be stacked to convert several arguments. jrose@2639: int conv_op = adapter_conversion_op(java_lang_invoke_AdapterMethodHandle::conversion(amh)); jrose@1145: decode_flags_result |= (_dmf_adapter_lsb << conv_op) & _DMF_ADAPTER_MASK; jrose@2639: oop target = java_lang_invoke_MethodHandle::vmtarget(amh); jrose@1145: if (target == NULL) return NULL; jrose@1145: klassOop tk = target->klass(); jrose@1145: if (tk == SystemDictionary::AdapterMethodHandle_klass()) { jrose@1145: amh = target; jrose@1145: continue; jrose@1145: } else { jrose@1145: // must be a BMH (which will bind some more arguments) or a DMH (for the final call) jrose@1145: return MethodHandles::decode_MethodHandle(target, receiver_limit_result, decode_flags_result); jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: methodOop MethodHandles::decode_MethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { jrose@1145: if (mh == NULL) return NULL; jrose@1145: klassOop mhk = mh->klass(); jrose@2639: assert(java_lang_invoke_MethodHandle::is_subclass(mhk), "must be a MethodHandle"); jrose@1145: if (mhk == SystemDictionary::DirectMethodHandle_klass()) { jrose@1145: return decode_DirectMethodHandle(mh, receiver_limit_result, decode_flags_result); jrose@1145: } else if (mhk == SystemDictionary::BoundMethodHandle_klass()) { jrose@1145: return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result); jrose@1145: } else if (mhk == SystemDictionary::AdapterMethodHandle_klass()) { jrose@1145: return decode_AdapterMethodHandle(mh, receiver_limit_result, decode_flags_result); jrose@2639: } else if (java_lang_invoke_BoundMethodHandle::is_subclass(mhk)) { jrose@1474: // could be a JavaMethodHandle (but not an adapter MH) jrose@1474: return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result); jrose@1145: } else { jrose@1145: assert(false, "cannot parse this MH"); jrose@1145: return NULL; // random MH? jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: methodOop MethodHandles::decode_methodOop(methodOop m, int& decode_flags_result) { jrose@1145: assert(m->is_method(), ""); jrose@1145: if (m->is_static()) { jrose@1145: // check that signature begins '(L' or '([' (not '(I', '()', etc.) coleenp@2497: Symbol* sig = m->signature(); jrose@1145: BasicType recv_bt = char2type(sig->byte_at(1)); jrose@1145: // Note: recv_bt might be T_ILLEGAL if byte_at(2) is ')' jrose@1145: assert(sig->byte_at(0) == '(', "must be method sig"); twisti@1573: // if (recv_bt == T_OBJECT || recv_bt == T_ARRAY) twisti@1573: // decode_flags_result |= _dmf_has_receiver; jrose@1145: } else { jrose@1145: // non-static method jrose@1145: decode_flags_result |= _dmf_has_receiver; jrose@1145: if (!m->can_be_statically_bound() && !m->is_initializer()) { jrose@1145: decode_flags_result |= _dmf_does_dispatch; jrose@1145: if (Klass::cast(m->method_holder())->is_interface()) jrose@1145: decode_flags_result |= _dmf_from_interface; jrose@1145: } jrose@1145: } jrose@1145: return m; jrose@1145: } jrose@1145: jrose@1145: jrose@1145: // A trusted party is handing us a cookie to determine a method. jrose@1145: // Let's boil it down to the method oop they really want. jrose@1145: methodOop MethodHandles::decode_method(oop x, klassOop& receiver_limit_result, int& decode_flags_result) { jrose@1145: decode_flags_result = 0; jrose@1145: receiver_limit_result = NULL; jrose@1145: klassOop xk = x->klass(); jrose@1145: if (xk == Universe::methodKlassObj()) { jrose@1145: return decode_methodOop((methodOop) x, decode_flags_result); jrose@1145: } else if (xk == SystemDictionary::MemberName_klass()) { jrose@1145: // Note: This only works if the MemberName has already been resolved. jrose@1145: return decode_MemberName(x, receiver_limit_result, decode_flags_result); jrose@2639: } else if (java_lang_invoke_MethodHandle::is_subclass(xk)) { jrose@1145: return decode_MethodHandle(x, receiver_limit_result, decode_flags_result); never@1577: } else if (xk == SystemDictionary::reflect_Method_klass()) { jrose@1145: oop clazz = java_lang_reflect_Method::clazz(x); jrose@1145: int slot = java_lang_reflect_Method::slot(x); jrose@1145: klassOop k = java_lang_Class::as_klassOop(clazz); jrose@1145: if (k != NULL && Klass::cast(k)->oop_is_instance()) jrose@1145: return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot), jrose@1145: decode_flags_result); never@1577: } else if (xk == SystemDictionary::reflect_Constructor_klass()) { jrose@1145: oop clazz = java_lang_reflect_Constructor::clazz(x); jrose@1145: int slot = java_lang_reflect_Constructor::slot(x); jrose@1145: klassOop k = java_lang_Class::as_klassOop(clazz); jrose@1145: if (k != NULL && Klass::cast(k)->oop_is_instance()) jrose@1145: return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot), jrose@1145: decode_flags_result); jrose@1145: } else { jrose@1145: // unrecognized object jrose@1145: assert(!x->is_method(), "already checked"); jrose@2639: assert(!java_lang_invoke_MemberName::is_instance(x), "already checked"); jrose@1145: } jrose@1145: return NULL; jrose@1145: } jrose@1145: jrose@1145: jrose@1145: int MethodHandles::decode_MethodHandle_stack_pushes(oop mh) { jrose@1145: if (mh->klass() == SystemDictionary::DirectMethodHandle_klass()) jrose@1145: return 0; // no push/pop jrose@2639: int this_vmslots = java_lang_invoke_MethodHandle::vmslots(mh); jrose@1145: int last_vmslots = 0; jrose@1145: oop last_mh = mh; jrose@1145: for (;;) { jrose@2639: oop target = java_lang_invoke_MethodHandle::vmtarget(last_mh); jrose@1145: if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) { jrose@2639: last_vmslots = java_lang_invoke_MethodHandle::vmslots(target); jrose@1145: break; jrose@2639: } else if (!java_lang_invoke_MethodHandle::is_instance(target)) { jrose@1145: // might be klass or method jrose@1145: assert(target->is_method(), "must get here with a direct ref to method"); jrose@1145: last_vmslots = methodOop(target)->size_of_parameters(); jrose@1145: break; jrose@1145: } jrose@1145: last_mh = target; jrose@1145: } jrose@1145: // If I am called with fewer VM slots than my ultimate callee, jrose@1145: // it must be that I push the additionally needed slots. jrose@1145: // Likewise if am called with more VM slots, I will pop them. jrose@1145: return (last_vmslots - this_vmslots); jrose@1145: } jrose@1145: jrose@1145: jrose@1145: // MemberName support jrose@1145: jrose@2639: // import java_lang_invoke_MemberName.* jrose@1145: enum { jrose@2639: IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD, jrose@2639: IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR, jrose@2639: IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD, jrose@2639: IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE, jrose@2639: SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES, jrose@2639: SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES, jrose@1145: ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE, jrose@2639: VM_INDEX_UNINITIALIZED = java_lang_invoke_MemberName::VM_INDEX_UNINITIALIZED jrose@1145: }; jrose@1145: jrose@1862: Handle MethodHandles::new_MemberName(TRAPS) { jrose@1862: Handle empty; jrose@1862: instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass()); jrose@1862: if (!k->is_initialized()) k->initialize(CHECK_(empty)); jrose@1862: return Handle(THREAD, k->allocate_instance(THREAD)); jrose@1862: } jrose@1862: jrose@1145: void MethodHandles::init_MemberName(oop mname_oop, oop target_oop) { never@1577: if (target_oop->klass() == SystemDictionary::reflect_Field_klass()) { jrose@1145: oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() jrose@1145: int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() jrose@1145: int mods = java_lang_reflect_Field::modifiers(target_oop); jrose@1145: klassOop k = java_lang_Class::as_klassOop(clazz); jrose@1145: int offset = instanceKlass::cast(k)->offset_from_fields(slot); jrose@1145: init_MemberName(mname_oop, k, accessFlags_from(mods), offset); jrose@1145: } else { jrose@1145: int decode_flags = 0; klassOop receiver_limit = NULL; jrose@1145: methodOop m = MethodHandles::decode_method(target_oop, jrose@1145: receiver_limit, decode_flags); jrose@1145: bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); jrose@1145: init_MemberName(mname_oop, m, do_dispatch); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::init_MemberName(oop mname_oop, methodOop m, bool do_dispatch) { jrose@1145: int flags = ((m->is_initializer() ? IS_CONSTRUCTOR : IS_METHOD) jrose@1145: | (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS )); jrose@1145: oop vmtarget = m; jrose@1145: int vmindex = methodOopDesc::invalid_vtable_index; // implies no info yet jrose@1145: if (!do_dispatch || (flags & IS_CONSTRUCTOR) || m->can_be_statically_bound()) jrose@1145: vmindex = methodOopDesc::nonvirtual_vtable_index; // implies never any dispatch jrose@1145: assert(vmindex != VM_INDEX_UNINITIALIZED, "Java sentinel value"); jrose@2639: java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); jrose@2639: java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); jrose@2639: java_lang_invoke_MemberName::set_flags(mname_oop, flags); jrose@2639: java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(m->method_holder())->java_mirror()); jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::init_MemberName(oop mname_oop, klassOop field_holder, AccessFlags mods, int offset) { jrose@1145: int flags = (IS_FIELD | (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS )); jrose@1145: oop vmtarget = field_holder; jrose@1862: int vmindex = offset; // determines the field uniquely when combined with static bit jrose@1145: assert(vmindex != VM_INDEX_UNINITIALIZED, "bad alias on vmindex"); jrose@2639: java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); jrose@2639: java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); jrose@2639: java_lang_invoke_MemberName::set_flags(mname_oop, flags); jrose@2639: java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(field_holder)->java_mirror()); jrose@1145: } jrose@1145: jrose@1145: jrose@1145: methodOop MethodHandles::decode_MemberName(oop mname, klassOop& receiver_limit_result, int& decode_flags_result) { jrose@2639: int flags = java_lang_invoke_MemberName::flags(mname); jrose@1145: if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) == 0) return NULL; // not invocable jrose@2639: oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname); jrose@2639: int vmindex = java_lang_invoke_MemberName::vmindex(mname); jrose@1145: if (vmindex == VM_INDEX_UNINITIALIZED) return NULL; // not resolved jrose@1474: methodOop m = decode_vmtarget(vmtarget, vmindex, NULL, receiver_limit_result, decode_flags_result); jrose@2639: oop clazz = java_lang_invoke_MemberName::clazz(mname); jrose@1474: if (clazz != NULL && java_lang_Class::is_instance(clazz)) { jrose@1474: klassOop klass = java_lang_Class::as_klassOop(clazz); jrose@1474: if (klass != NULL) receiver_limit_result = klass; jrose@1474: } jrose@1474: return m; jrose@1145: } jrose@1145: coleenp@2497: // convert the external string or reflective type to an internal signature coleenp@2497: Symbol* MethodHandles::convert_to_signature(oop type_str, coleenp@2497: bool polymorphic, coleenp@2497: TRAPS) { jrose@2639: if (java_lang_invoke_MethodType::is_instance(type_str)) { jrose@2639: return java_lang_invoke_MethodType::as_signature(type_str, polymorphic, CHECK_NULL); coleenp@2497: } else if (java_lang_Class::is_instance(type_str)) { coleenp@2497: return java_lang_Class::as_signature(type_str, false, CHECK_NULL); coleenp@2497: } else if (java_lang_String::is_instance(type_str)) { coleenp@2497: if (polymorphic) { coleenp@2497: return java_lang_String::as_symbol(type_str, CHECK_NULL); coleenp@2497: } else { coleenp@2497: return java_lang_String::as_symbol_or_null(type_str); coleenp@2497: } coleenp@2497: } else { coleenp@2497: THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", NULL); coleenp@2497: } coleenp@2497: } coleenp@2497: jrose@1145: // An unresolved member name is a mere symbolic reference. jrose@1145: // Resolving it plants a vmtarget/vmindex in it, jrose@1145: // which refers dirctly to JVM internals. jrose@1145: void MethodHandles::resolve_MemberName(Handle mname, TRAPS) { jrose@2639: assert(java_lang_invoke_MemberName::is_instance(mname()), ""); jrose@1145: #ifdef ASSERT jrose@1145: // If this assert throws, renegotiate the sentinel value used by the Java code, jrose@1145: // so that it is distinct from any valid vtable index value, and any special jrose@1145: // values defined in methodOopDesc::VtableIndexFlag. jrose@1145: // The point of the slop is to give the Java code and the JVM some room jrose@1145: // to independently specify sentinel values. jrose@1145: const int sentinel_slop = 10; jrose@1145: const int sentinel_limit = methodOopDesc::highest_unused_vtable_index_value - sentinel_slop; jrose@1145: assert(VM_INDEX_UNINITIALIZED < sentinel_limit, "Java sentinel != JVM sentinels"); jrose@1145: #endif jrose@2639: if (java_lang_invoke_MemberName::vmindex(mname()) != VM_INDEX_UNINITIALIZED) jrose@1145: return; // already resolved jrose@2639: oop defc_oop = java_lang_invoke_MemberName::clazz(mname()); jrose@2639: oop name_str = java_lang_invoke_MemberName::name(mname()); jrose@2639: oop type_str = java_lang_invoke_MemberName::type(mname()); jrose@2639: int flags = java_lang_invoke_MemberName::flags(mname()); jrose@1145: jrose@1145: if (defc_oop == NULL || name_str == NULL || type_str == NULL) { jrose@1145: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve"); jrose@1145: } jrose@1145: klassOop defc_klassOop = java_lang_Class::as_klassOop(defc_oop); jrose@1145: defc_oop = NULL; // safety jrose@1145: if (defc_klassOop == NULL) return; // a primitive; no resolution possible jrose@1145: if (!Klass::cast(defc_klassOop)->oop_is_instance()) { jrose@1145: if (!Klass::cast(defc_klassOop)->oop_is_array()) return; never@1577: defc_klassOop = SystemDictionary::Object_klass(); jrose@1145: } jrose@1145: instanceKlassHandle defc(THREAD, defc_klassOop); jrose@1145: defc_klassOop = NULL; // safety jrose@1145: if (defc.is_null()) { jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), "primitive class"); jrose@1145: } jrose@1145: defc->link_class(CHECK); jrose@1145: jrose@1145: // convert the external string name to an internal symbol coleenp@2497: TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str); coleenp@2497: if (name == NULL) return; // no such name jrose@1145: name_str = NULL; // safety jrose@1145: jrose@1863: Handle polymorphic_method_type; jrose@1863: bool polymorphic_signature = false; jrose@1863: if ((flags & ALL_KINDS) == IS_METHOD && twisti@2343: (defc() == SystemDictionary::MethodHandle_klass() && coleenp@2497: methodOopDesc::is_method_handle_invoke_name(name))) jrose@1863: polymorphic_signature = true; jrose@1863: jrose@1145: // convert the external string or reflective type to an internal signature coleenp@2497: TempNewSymbol type = convert_to_signature(type_str, polymorphic_signature, CHECK); jrose@2639: if (java_lang_invoke_MethodType::is_instance(type_str) && polymorphic_signature) { coleenp@2497: polymorphic_method_type = Handle(THREAD, type_str); //preserve exactly jrose@1145: } coleenp@2497: coleenp@2497: if (type == NULL) return; // no such signature exists in the VM jrose@1145: type_str = NULL; // safety jrose@1145: jrose@1145: // Time to do the lookup. jrose@1145: switch (flags & ALL_KINDS) { jrose@1145: case IS_METHOD: jrose@1145: { jrose@1145: CallInfo result; jrose@1145: { jrose@1145: EXCEPTION_MARK; jrose@1145: if ((flags & JVM_ACC_STATIC) != 0) { jrose@1145: LinkResolver::resolve_static_call(result, jrose@1145: defc, name, type, KlassHandle(), false, false, THREAD); jrose@1145: } else if (defc->is_interface()) { jrose@1145: LinkResolver::resolve_interface_call(result, Handle(), defc, jrose@1145: defc, name, type, KlassHandle(), false, false, THREAD); jrose@1145: } else { jrose@1145: LinkResolver::resolve_virtual_call(result, Handle(), defc, jrose@1145: defc, name, type, KlassHandle(), false, false, THREAD); jrose@1145: } jrose@1145: if (HAS_PENDING_EXCEPTION) { jrose@1145: CLEAR_PENDING_EXCEPTION; jrose@1863: break; // go to second chance jrose@1145: } jrose@1145: } jrose@1145: methodHandle m = result.resolved_method(); jrose@1145: oop vmtarget = NULL; jrose@1145: int vmindex = methodOopDesc::nonvirtual_vtable_index; jrose@1145: if (defc->is_interface()) { jrose@1145: vmindex = klassItable::compute_itable_index(m()); jrose@1145: assert(vmindex >= 0, ""); jrose@1145: } else if (result.has_vtable_index()) { jrose@1145: vmindex = result.vtable_index(); jrose@1145: assert(vmindex >= 0, ""); jrose@1145: } jrose@1145: assert(vmindex != VM_INDEX_UNINITIALIZED, ""); jrose@1145: if (vmindex < 0) { jrose@1145: assert(result.is_statically_bound(), ""); jrose@1145: vmtarget = m(); jrose@1145: } else { jrose@1145: vmtarget = result.resolved_klass()->as_klassOop(); jrose@1145: } jrose@1145: int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); jrose@2639: java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); jrose@2639: java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); jrose@2639: java_lang_invoke_MemberName::set_modifiers(mname(), mods); jrose@1145: DEBUG_ONLY(int junk; klassOop junk2); jrose@1145: assert(decode_MemberName(mname(), junk2, junk) == result.resolved_method()(), jrose@1145: "properly stored for later decoding"); jrose@1145: return; jrose@1145: } jrose@1145: case IS_CONSTRUCTOR: jrose@1145: { jrose@1145: CallInfo result; jrose@1145: { jrose@1145: EXCEPTION_MARK; coleenp@2497: if (name == vmSymbols::object_initializer_name()) { jrose@1145: LinkResolver::resolve_special_call(result, jrose@1145: defc, name, type, KlassHandle(), false, THREAD); jrose@1145: } else { jrose@1145: break; // will throw after end of switch jrose@1145: } jrose@1145: if (HAS_PENDING_EXCEPTION) { jrose@1145: CLEAR_PENDING_EXCEPTION; jrose@1145: return; jrose@1145: } jrose@1145: } jrose@1145: assert(result.is_statically_bound(), ""); jrose@1145: methodHandle m = result.resolved_method(); jrose@1145: oop vmtarget = m(); jrose@1145: int vmindex = methodOopDesc::nonvirtual_vtable_index; jrose@1145: int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); jrose@2639: java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); jrose@2639: java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); jrose@2639: java_lang_invoke_MemberName::set_modifiers(mname(), mods); jrose@1145: DEBUG_ONLY(int junk; klassOop junk2); jrose@1145: assert(decode_MemberName(mname(), junk2, junk) == result.resolved_method()(), jrose@1145: "properly stored for later decoding"); jrose@1145: return; jrose@1145: } jrose@1145: case IS_FIELD: jrose@1145: { jrose@1145: // This is taken from LinkResolver::resolve_field, sans access checks. jrose@1145: fieldDescriptor fd; // find_field initializes fd if found coleenp@2497: KlassHandle sel_klass(THREAD, instanceKlass::cast(defc())->find_field(name, type, &fd)); jrose@1145: // check if field exists; i.e., if a klass containing the field def has been selected jrose@1145: if (sel_klass.is_null()) return; jrose@1145: oop vmtarget = sel_klass->as_klassOop(); jrose@1145: int vmindex = fd.offset(); jrose@1145: int mods = (fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS); jrose@1145: if (vmindex == VM_INDEX_UNINITIALIZED) break; // should not happen jrose@2639: java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); jrose@2639: java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); jrose@2639: java_lang_invoke_MemberName::set_modifiers(mname(), mods); jrose@1145: return; jrose@1145: } jrose@1863: default: jrose@1863: THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); jrose@1145: } jrose@1863: jrose@1863: // Second chance. jrose@1863: if (polymorphic_method_type.not_null()) { jrose@1863: // Look on a non-null class loader. jrose@1863: Handle cur_class_loader; jrose@2639: const int nptypes = java_lang_invoke_MethodType::ptype_count(polymorphic_method_type()); jrose@1863: for (int i = 0; i <= nptypes; i++) { jrose@1863: oop type_mirror; jrose@2639: if (i < nptypes) type_mirror = java_lang_invoke_MethodType::ptype(polymorphic_method_type(), i); jrose@2639: else type_mirror = java_lang_invoke_MethodType::rtype(polymorphic_method_type()); jrose@1863: klassOop example_type = java_lang_Class::as_klassOop(type_mirror); jrose@1863: if (example_type == NULL) continue; jrose@1863: oop class_loader = Klass::cast(example_type)->class_loader(); jrose@1863: if (class_loader == NULL || class_loader == cur_class_loader()) continue; jrose@1863: cur_class_loader = Handle(THREAD, class_loader); jrose@1863: methodOop m = SystemDictionary::find_method_handle_invoke(name, jrose@1863: type, jrose@1863: KlassHandle(THREAD, example_type), jrose@1863: THREAD); jrose@1863: if (HAS_PENDING_EXCEPTION) { jrose@1863: CLEAR_PENDING_EXCEPTION; jrose@1863: m = NULL; jrose@1863: // try again with a different class loader... jrose@1863: } jrose@1863: if (m != NULL) { jrose@1863: int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); jrose@2639: java_lang_invoke_MemberName::set_vmtarget(mname(), m); jrose@2639: java_lang_invoke_MemberName::set_vmindex(mname(), m->vtable_index()); jrose@2639: java_lang_invoke_MemberName::set_modifiers(mname(), mods); jrose@1863: return; jrose@1863: } jrose@1863: } jrose@1863: } jrose@1145: } jrose@1145: jrose@1145: // Conversely, a member name which is only initialized from JVM internals jrose@1145: // may have null defc, name, and type fields. jrose@1145: // Resolving it plants a vmtarget/vmindex in it, jrose@1145: // which refers directly to JVM internals. jrose@1145: void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) { jrose@2639: assert(java_lang_invoke_MemberName::is_instance(mname()), ""); jrose@2639: oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); jrose@2639: int vmindex = java_lang_invoke_MemberName::vmindex(mname()); jrose@1145: if (vmtarget == NULL || vmindex == VM_INDEX_UNINITIALIZED) { jrose@1145: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); jrose@1145: } jrose@1145: jrose@2639: bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL); jrose@2639: bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL); jrose@2639: bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL); jrose@2639: int flags = java_lang_invoke_MemberName::flags(mname()); jrose@1145: jrose@1145: if (suppress != 0) { jrose@1145: if (suppress & _suppress_defc) have_defc = true; jrose@1145: if (suppress & _suppress_name) have_name = true; jrose@1145: if (suppress & _suppress_type) have_type = true; jrose@1145: } jrose@1145: jrose@1145: if (have_defc && have_name && have_type) return; // nothing needed jrose@1145: jrose@1145: switch (flags & ALL_KINDS) { jrose@1145: case IS_METHOD: jrose@1145: case IS_CONSTRUCTOR: jrose@1145: { jrose@1145: klassOop receiver_limit = NULL; jrose@1145: int decode_flags = 0; jrose@1145: methodHandle m(THREAD, decode_vmtarget(vmtarget, vmindex, NULL, jrose@1145: receiver_limit, decode_flags)); jrose@1145: if (m.is_null()) break; jrose@1145: if (!have_defc) { jrose@1145: klassOop defc = m->method_holder(); jrose@1145: if (receiver_limit != NULL && receiver_limit != defc jrose@1145: && Klass::cast(receiver_limit)->is_subtype_of(defc)) jrose@1145: defc = receiver_limit; jrose@2639: java_lang_invoke_MemberName::set_clazz(mname(), Klass::cast(defc)->java_mirror()); jrose@1145: } jrose@1145: if (!have_name) { jrose@1145: //not java_lang_String::create_from_symbol; let's intern member names jrose@1145: Handle name = StringTable::intern(m->name(), CHECK); jrose@2639: java_lang_invoke_MemberName::set_name(mname(), name()); jrose@1145: } jrose@1145: if (!have_type) { jrose@1145: Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK); jrose@2639: java_lang_invoke_MemberName::set_type(mname(), type()); jrose@1145: } jrose@1145: return; jrose@1145: } jrose@1145: case IS_FIELD: jrose@1145: { jrose@1145: // This is taken from LinkResolver::resolve_field, sans access checks. jrose@1145: if (!vmtarget->is_klass()) break; jrose@1145: if (!Klass::cast((klassOop) vmtarget)->oop_is_instance()) break; jrose@1145: instanceKlassHandle defc(THREAD, (klassOop) vmtarget); jrose@1145: bool is_static = ((flags & JVM_ACC_STATIC) != 0); jrose@1145: fieldDescriptor fd; // find_field initializes fd if found jrose@1145: if (!defc->find_field_from_offset(vmindex, is_static, &fd)) jrose@1145: break; // cannot expand jrose@1145: if (!have_defc) { jrose@2639: java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); jrose@1145: } jrose@1145: if (!have_name) { jrose@1145: //not java_lang_String::create_from_symbol; let's intern member names jrose@1145: Handle name = StringTable::intern(fd.name(), CHECK); jrose@2639: java_lang_invoke_MemberName::set_name(mname(), name()); jrose@1145: } jrose@1145: if (!have_type) { jrose@1145: Handle type = java_lang_String::create_from_symbol(fd.signature(), CHECK); jrose@2639: java_lang_invoke_MemberName::set_type(mname(), type()); jrose@1145: } jrose@1145: return; jrose@1145: } jrose@1145: } jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); jrose@1145: } jrose@1145: jrose@1145: int MethodHandles::find_MemberNames(klassOop k, coleenp@2497: Symbol* name, Symbol* sig, jrose@1145: int mflags, klassOop caller, jrose@1145: int skip, objArrayOop results) { jrose@1145: DEBUG_ONLY(No_Safepoint_Verifier nsv); jrose@1145: // this code contains no safepoints! jrose@1145: jrose@1145: // %%% take caller into account! jrose@1145: jrose@1145: if (k == NULL || !Klass::cast(k)->oop_is_instance()) return -1; jrose@1145: jrose@1145: int rfill = 0, rlimit = results->length(), rskip = skip; jrose@1145: // overflow measurement: jrose@1145: int overflow = 0, overflow_limit = MAX2(1000, rlimit); jrose@1145: jrose@1145: int match_flags = mflags; jrose@1145: bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0); jrose@1145: bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0); jrose@1145: bool local_only = !(search_superc | search_intfc); jrose@1145: bool classes_only = false; jrose@1145: jrose@1145: if (name != NULL) { jrose@1145: if (name->utf8_length() == 0) return 0; // a match is not possible jrose@1145: } jrose@1145: if (sig != NULL) { jrose@1145: if (sig->utf8_length() == 0) return 0; // a match is not possible jrose@1145: if (sig->byte_at(0) == '(') jrose@1145: match_flags &= ~(IS_FIELD | IS_TYPE); jrose@1145: else jrose@1145: match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD); jrose@1145: } jrose@1145: jrose@1145: if ((match_flags & IS_TYPE) != 0) { jrose@1145: // NYI, and Core Reflection works quite well for this query jrose@1145: } jrose@1145: jrose@1145: if ((match_flags & IS_FIELD) != 0) { jrose@1145: for (FieldStream st(k, local_only, !search_intfc); !st.eos(); st.next()) { jrose@1145: if (name != NULL && st.name() != name) jrose@1145: continue; jrose@1145: if (sig != NULL && st.signature() != sig) jrose@1145: continue; jrose@1145: // passed the filters jrose@1145: if (rskip > 0) { jrose@1145: --rskip; jrose@1145: } else if (rfill < rlimit) { jrose@1145: oop result = results->obj_at(rfill++); jrose@2639: if (!java_lang_invoke_MemberName::is_instance(result)) jrose@1145: return -99; // caller bug! jrose@1145: MethodHandles::init_MemberName(result, st.klass()->as_klassOop(), st.access_flags(), st.offset()); jrose@1145: } else if (++overflow >= overflow_limit) { jrose@1145: match_flags = 0; break; // got tired of looking at overflow jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { jrose@1145: // watch out for these guys: coleenp@2497: Symbol* init_name = vmSymbols::object_initializer_name(); coleenp@2497: Symbol* clinit_name = vmSymbols::class_initializer_name(); jrose@1145: if (name == clinit_name) clinit_name = NULL; // hack for exposing jrose@1145: bool negate_name_test = false; jrose@1145: // fix name so that it captures the intention of IS_CONSTRUCTOR jrose@1145: if (!(match_flags & IS_METHOD)) { jrose@1145: // constructors only jrose@1145: if (name == NULL) { jrose@1145: name = init_name; jrose@1145: } else if (name != init_name) { jrose@1145: return 0; // no constructors of this method name jrose@1145: } jrose@1145: } else if (!(match_flags & IS_CONSTRUCTOR)) { jrose@1145: // methods only jrose@1145: if (name == NULL) { jrose@1145: name = init_name; jrose@1145: negate_name_test = true; // if we see the name, we *omit* the entry jrose@1145: } else if (name == init_name) { jrose@1145: return 0; // no methods of this constructor name jrose@1145: } jrose@1145: } else { jrose@1145: // caller will accept either sort; no need to adjust name jrose@1145: } jrose@1145: for (MethodStream st(k, local_only, !search_intfc); !st.eos(); st.next()) { jrose@1145: methodOop m = st.method(); coleenp@2497: Symbol* m_name = m->name(); jrose@1145: if (m_name == clinit_name) jrose@1145: continue; jrose@1145: if (name != NULL && ((m_name != name) ^ negate_name_test)) jrose@1145: continue; jrose@1145: if (sig != NULL && m->signature() != sig) jrose@1145: continue; jrose@1145: // passed the filters jrose@1145: if (rskip > 0) { jrose@1145: --rskip; jrose@1145: } else if (rfill < rlimit) { jrose@1145: oop result = results->obj_at(rfill++); jrose@2639: if (!java_lang_invoke_MemberName::is_instance(result)) jrose@1145: return -99; // caller bug! jrose@1145: MethodHandles::init_MemberName(result, m, true); jrose@1145: } else if (++overflow >= overflow_limit) { jrose@1145: match_flags = 0; break; // got tired of looking at overflow jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // return number of elements we at leasted wanted to initialize jrose@1145: return rfill + overflow; jrose@1145: } jrose@1145: jrose@1145: jrose@1862: // Decode this java.lang.Class object into an instanceKlass, if possible. jrose@1862: // Throw IAE if not jrose@1862: instanceKlassHandle MethodHandles::resolve_instance_klass(oop java_mirror_oop, TRAPS) { jrose@1862: instanceKlassHandle empty; jrose@1862: klassOop caller = NULL; jrose@1862: if (java_lang_Class::is_instance(java_mirror_oop)) { jrose@1862: caller = java_lang_Class::as_klassOop(java_mirror_oop); jrose@1862: } jrose@1862: if (caller == NULL || !Klass::cast(caller)->oop_is_instance()) { jrose@1862: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not a class", empty); jrose@1862: } jrose@1862: return instanceKlassHandle(THREAD, caller); jrose@1862: } jrose@1862: jrose@1145: jrose@1145: jrose@1145: // Decode the vmtarget field of a method handle. jrose@1145: // Sanitize out methodOops, klassOops, and any other non-Java data. jrose@1145: // This is for debugging and reflection. jrose@1145: oop MethodHandles::encode_target(Handle mh, int format, TRAPS) { jrose@2639: assert(java_lang_invoke_MethodHandle::is_instance(mh()), "must be a MH"); jrose@1145: if (format == ETF_HANDLE_OR_METHOD_NAME) { jrose@2639: oop target = java_lang_invoke_MethodHandle::vmtarget(mh()); jrose@1145: if (target == NULL) { jrose@1145: return NULL; // unformed MH jrose@1145: } jrose@1145: klassOop tklass = target->klass(); never@1577: if (Klass::cast(tklass)->is_subclass_of(SystemDictionary::Object_klass())) { jrose@1145: return target; // target is another MH (or something else?) jrose@1145: } jrose@1145: } jrose@1145: if (format == ETF_DIRECT_HANDLE) { jrose@1145: oop target = mh(); jrose@1145: for (;;) { jrose@1145: if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) { jrose@1145: return target; jrose@1145: } jrose@2639: if (!java_lang_invoke_MethodHandle::is_instance(target)){ jrose@1145: return NULL; // unformed MH jrose@1145: } jrose@2639: target = java_lang_invoke_MethodHandle::vmtarget(target); jrose@1145: } jrose@1145: } jrose@1145: // cases of metadata in MH.vmtarget: jrose@1145: // - AMH can have methodOop for static invoke with bound receiver jrose@1145: // - DMH can have methodOop for static invoke (on variable receiver) jrose@1145: // - DMH can have klassOop for dispatched (non-static) invoke jrose@1145: klassOop receiver_limit = NULL; jrose@1145: int decode_flags = 0; jrose@1145: methodOop m = decode_MethodHandle(mh(), receiver_limit, decode_flags); jrose@1145: if (m == NULL) return NULL; jrose@1145: switch (format) { jrose@1145: case ETF_REFLECT_METHOD: jrose@1145: // same as jni_ToReflectedMethod: jrose@1145: if (m->is_initializer()) { jrose@1145: return Reflection::new_constructor(m, THREAD); jrose@1145: } else { jrose@1145: return Reflection::new_method(m, UseNewReflection, false, THREAD); jrose@1145: } jrose@1145: jrose@1145: case ETF_HANDLE_OR_METHOD_NAME: // method, not handle jrose@1145: case ETF_METHOD_NAME: jrose@1145: { jrose@1145: if (SystemDictionary::MemberName_klass() == NULL) break; jrose@1145: instanceKlassHandle mname_klass(THREAD, SystemDictionary::MemberName_klass()); jrose@1145: mname_klass->initialize(CHECK_NULL); jrose@1145: Handle mname = mname_klass->allocate_instance_handle(CHECK_NULL); jrose@2639: java_lang_invoke_MemberName::set_vmindex(mname(), VM_INDEX_UNINITIALIZED); jrose@1145: bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); jrose@1145: init_MemberName(mname(), m, do_dispatch); jrose@1145: expand_MemberName(mname, 0, CHECK_NULL); jrose@1145: return mname(); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // Unknown format code. jrose@1145: char msg[50]; jrose@1145: jio_snprintf(msg, sizeof(msg), "unknown getTarget format=%d", format); jrose@1145: THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), msg); jrose@1145: } jrose@1145: jrose@1474: static const char* always_null_names[] = { jrose@1474: "java/lang/Void", jrose@1474: "java/lang/Null", jrose@1474: //"java/lang/Nothing", jrose@1474: "sun/dyn/empty/Empty", jrose@2639: "sun/invoke/empty/Empty", jrose@1474: NULL jrose@1474: }; jrose@1474: jrose@1474: static bool is_always_null_type(klassOop klass) { jrose@2760: if (klass == NULL) return false; // safety jrose@1474: if (!Klass::cast(klass)->oop_is_instance()) return false; jrose@1474: instanceKlass* ik = instanceKlass::cast(klass); jrose@1474: // Must be on the boot class path: jrose@1474: if (ik->class_loader() != NULL) return false; jrose@1474: // Check the name. coleenp@2497: Symbol* name = ik->name(); jrose@1474: for (int i = 0; ; i++) { jrose@1474: const char* test_name = always_null_names[i]; jrose@1474: if (test_name == NULL) break; twisti@1573: if (name->equals(test_name)) jrose@1474: return true; jrose@1474: } jrose@1474: return false; jrose@1474: } jrose@1474: jrose@1145: bool MethodHandles::class_cast_needed(klassOop src, klassOop dst) { jrose@2760: if (dst == NULL) return true; jrose@2760: if (src == NULL) return (dst != SystemDictionary::Object_klass()); never@1577: if (src == dst || dst == SystemDictionary::Object_klass()) jrose@1145: return false; // quickest checks jrose@1145: Klass* srck = Klass::cast(src); jrose@1145: Klass* dstk = Klass::cast(dst); jrose@1145: if (dstk->is_interface()) { jrose@1145: // interface receivers can safely be viewed as untyped, jrose@1145: // because interface calls always include a dynamic check never@1577: //dstk = Klass::cast(SystemDictionary::Object_klass()); jrose@1145: return false; jrose@1145: } jrose@1145: if (srck->is_interface()) { jrose@1145: // interface arguments must be viewed as untyped never@1577: //srck = Klass::cast(SystemDictionary::Object_klass()); jrose@1145: return true; jrose@1145: } jrose@1474: if (is_always_null_type(src)) { jrose@1474: // some source types are known to be never instantiated; jrose@1474: // they represent references which are always null jrose@1474: // such null references never fail to convert safely jrose@1474: return false; jrose@1474: } jrose@1145: return !srck->is_subclass_of(dstk->as_klassOop()); jrose@1145: } jrose@1145: jrose@1145: static oop object_java_mirror() { never@1577: return Klass::cast(SystemDictionary::Object_klass())->java_mirror(); jrose@1145: } jrose@1145: jrose@1145: bool MethodHandles::same_basic_type_for_arguments(BasicType src, jrose@1145: BasicType dst, jrose@1474: bool raw, jrose@1145: bool for_return) { jrose@1474: if (for_return) { jrose@1474: // return values can always be forgotten: jrose@1474: if (dst == T_VOID) return true; jrose@1474: if (src == T_VOID) return raw && (dst == T_INT); jrose@1474: // We allow caller to receive a garbage int, which is harmless. jrose@1474: // This trick is pulled by trusted code (see VerifyType.canPassRaw). jrose@1474: } jrose@1145: assert(src != T_VOID && dst != T_VOID, "should not be here"); jrose@1145: if (src == dst) return true; jrose@1145: if (type2size[src] != type2size[dst]) return false; jrose@2267: if (src == T_OBJECT || dst == T_OBJECT) return false; jrose@2267: if (raw) return true; // bitwise reinterpretation; caller guarantees safety jrose@1145: // allow reinterpretation casts for integral widening jrose@1145: if (is_subword_type(src)) { // subwords can fit in int or other subwords jrose@1145: if (dst == T_INT) // any subword fits in an int jrose@1145: return true; jrose@1145: if (src == T_BOOLEAN) // boolean fits in any subword jrose@1145: return is_subword_type(dst); jrose@1145: if (src == T_BYTE && dst == T_SHORT) jrose@1145: return true; // remaining case: byte fits in short jrose@1145: } jrose@1145: // allow float/fixed reinterpretation casts jrose@1145: if (src == T_FLOAT) return dst == T_INT; jrose@1145: if (src == T_INT) return dst == T_FLOAT; jrose@1145: if (src == T_DOUBLE) return dst == T_LONG; jrose@1145: if (src == T_LONG) return dst == T_DOUBLE; jrose@1145: return false; jrose@1145: } jrose@1145: jrose@1145: const char* MethodHandles::check_method_receiver(methodOop m, jrose@1145: klassOop passed_recv_type) { jrose@1145: assert(!m->is_static(), "caller resp."); jrose@1145: if (passed_recv_type == NULL) jrose@1145: return "receiver type is primitive"; jrose@1145: if (class_cast_needed(passed_recv_type, m->method_holder())) { jrose@1145: Klass* formal = Klass::cast(m->method_holder()); jrose@1145: return SharedRuntime::generate_class_cast_message("receiver type", jrose@1145: formal->external_name()); jrose@1145: } jrose@1145: return NULL; // checks passed jrose@1145: } jrose@1145: jrose@1145: // Verify that m's signature can be called type-safely by a method handle jrose@1145: // of the given method type 'mtype'. jrose@1145: // It takes a TRAPS argument because it must perform symbol lookups. jrose@1145: void MethodHandles::verify_method_signature(methodHandle m, jrose@1145: Handle mtype, jrose@1145: int first_ptype_pos, jrose@1145: KlassHandle insert_ptype, jrose@1145: TRAPS) { jrose@2760: Handle mhi_type; jrose@2760: if (m->is_method_handle_invoke()) { jrose@2760: // use this more exact typing instead of the symbolic signature: jrose@2760: mhi_type = Handle(THREAD, m->method_handle_type()); jrose@2760: } jrose@2639: objArrayHandle ptypes(THREAD, java_lang_invoke_MethodType::ptypes(mtype())); jrose@1145: int pnum = first_ptype_pos; jrose@1145: int pmax = ptypes->length(); jrose@2760: int anum = 0; // method argument jrose@1145: const char* err = NULL; coleenp@2497: ResourceMark rm(THREAD); jrose@1145: for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) { jrose@1145: oop ptype_oop = NULL; jrose@1145: if (ss.at_return_type()) { jrose@1145: if (pnum != pmax) jrose@1145: { err = "too many arguments"; break; } jrose@2639: ptype_oop = java_lang_invoke_MethodType::rtype(mtype()); jrose@1145: } else { jrose@1145: if (pnum >= pmax) jrose@1145: { err = "not enough arguments"; break; } jrose@1145: if (pnum >= 0) jrose@1145: ptype_oop = ptypes->obj_at(pnum); jrose@1145: else if (insert_ptype.is_null()) jrose@1145: ptype_oop = NULL; jrose@1145: else jrose@1145: ptype_oop = insert_ptype->java_mirror(); jrose@1145: pnum += 1; jrose@2760: anum += 1; jrose@1145: } jrose@2760: KlassHandle pklass; jrose@2760: BasicType ptype = T_OBJECT; jrose@2760: bool have_ptype = false; jrose@2760: // missing ptype_oop does not match any non-reference; use Object to report the error jrose@2760: pklass = SystemDictionaryHandles::Object_klass(); jrose@2760: if (ptype_oop != NULL) { jrose@2760: have_ptype = true; jrose@2760: klassOop pklass_oop = NULL; jrose@2760: ptype = java_lang_Class::as_BasicType(ptype_oop, &pklass_oop); jrose@2760: pklass = KlassHandle(THREAD, pklass_oop); jrose@2760: } jrose@2760: ptype_oop = NULL; //done with this jrose@2760: KlassHandle aklass; jrose@2760: BasicType atype = ss.type(); jrose@2760: if (atype == T_ARRAY) atype = T_OBJECT; // fold all refs to T_OBJECT jrose@2760: if (atype == T_OBJECT) { jrose@2760: if (!have_ptype) { jrose@1145: // null matches any reference jrose@1145: continue; jrose@1145: } jrose@2760: if (mhi_type.is_null()) { jrose@2760: // If we fail to resolve types at this point, we will usually throw an error. jrose@2760: TempNewSymbol name = ss.as_symbol_or_null(); jrose@2760: if (name != NULL) { jrose@2760: instanceKlass* mk = instanceKlass::cast(m->method_holder()); jrose@2760: Handle loader(THREAD, mk->class_loader()); jrose@2760: Handle domain(THREAD, mk->protection_domain()); jrose@2760: klassOop aklass_oop = SystemDictionary::resolve_or_null(name, loader, domain, CHECK); jrose@2760: if (aklass_oop != NULL) jrose@2760: aklass = KlassHandle(THREAD, aklass_oop); jrose@2760: } jrose@2760: } else { jrose@2760: // for method handle invokers we don't look at the name in the signature jrose@2760: oop atype_oop; jrose@2760: if (ss.at_return_type()) jrose@2760: atype_oop = java_lang_invoke_MethodType::rtype(mhi_type()); jrose@2760: else jrose@2760: atype_oop = java_lang_invoke_MethodType::ptype(mhi_type(), anum-1); jrose@2760: klassOop aklass_oop = NULL; jrose@2760: atype = java_lang_Class::as_BasicType(atype_oop, &aklass_oop); jrose@2760: aklass = KlassHandle(THREAD, aklass_oop); jrose@1863: } jrose@1145: } jrose@1145: if (!ss.at_return_type()) { jrose@2760: err = check_argument_type_change(ptype, pklass(), atype, aklass(), anum); jrose@1145: } else { jrose@2760: err = check_return_type_change(atype, aklass(), ptype, pklass()); // note reversal! jrose@1145: } jrose@1145: if (err != NULL) break; jrose@1145: } jrose@1145: jrose@1145: if (err != NULL) { jrose@2760: #ifndef PRODUCT jrose@2760: if (PrintMiscellaneous && (Verbose || WizardMode)) { jrose@2760: tty->print("*** verify_method_signature failed: "); jrose@2760: java_lang_invoke_MethodType::print_signature(mtype(), tty); jrose@2760: tty->cr(); jrose@2760: tty->print_cr(" first_ptype_pos = %d, insert_ptype = "UINTX_FORMAT, first_ptype_pos, insert_ptype()); jrose@2760: tty->print(" Failing method: "); jrose@2760: m->print(); jrose@2760: } jrose@2760: #endif //PRODUCT jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), err); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // Main routine for verifying the MethodHandle.type of a proposed jrose@1145: // direct or bound-direct method handle. jrose@1145: void MethodHandles::verify_method_type(methodHandle m, jrose@1145: Handle mtype, jrose@1145: bool has_bound_recv, jrose@1145: KlassHandle bound_recv_type, jrose@1145: TRAPS) { jrose@1145: bool m_needs_receiver = !m->is_static(); jrose@1145: jrose@1145: const char* err = NULL; jrose@1145: jrose@1145: int first_ptype_pos = m_needs_receiver ? 1 : 0; jrose@1474: if (has_bound_recv) { jrose@1474: first_ptype_pos -= 1; // ptypes do not include the bound argument; start earlier in them jrose@1145: if (m_needs_receiver && bound_recv_type.is_null()) jrose@1145: { err = "bound receiver is not an object"; goto die; } jrose@1145: } jrose@1145: jrose@1145: if (m_needs_receiver && err == NULL) { jrose@2639: objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(mtype()); jrose@1145: if (ptypes->length() < first_ptype_pos) jrose@1145: { err = "receiver argument is missing"; goto die; } jrose@1474: if (has_bound_recv) jrose@1145: err = check_method_receiver(m(), bound_recv_type->as_klassOop()); jrose@1145: else jrose@1474: err = check_method_receiver(m(), java_lang_Class::as_klassOop(ptypes->obj_at(first_ptype_pos-1))); jrose@1145: if (err != NULL) goto die; jrose@1145: } jrose@1145: jrose@1145: // Check the other arguments for mistypes. jrose@1145: verify_method_signature(m, mtype, first_ptype_pos, bound_recv_type, CHECK); jrose@1145: return; jrose@1145: jrose@1145: die: jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), err); jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::verify_vmslots(Handle mh, TRAPS) { jrose@1145: // Verify vmslots. jrose@2639: int check_slots = argument_slot_count(java_lang_invoke_MethodHandle::type(mh())); jrose@2639: if (java_lang_invoke_MethodHandle::vmslots(mh()) != check_slots) { jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH"); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::verify_vmargslot(Handle mh, int argnum, int argslot, TRAPS) { jrose@1145: // Verify that argslot points at the given argnum. jrose@2639: int check_slot = argument_slot(java_lang_invoke_MethodHandle::type(mh()), argnum); jrose@1145: if (argslot != check_slot || argslot < 0) { jrose@1145: const char* fmt = "for argnum of %d, vmargslot is %d, should be %d"; jrose@1145: size_t msglen = strlen(fmt) + 3*11 + 1; jrose@1145: char* msg = NEW_RESOURCE_ARRAY(char, msglen); jrose@1145: jio_snprintf(msg, msglen, fmt, argnum, argslot, check_slot); jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), msg); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // Verify the correspondence between two method types. jrose@1145: // Apart from the advertised changes, caller method type X must jrose@1145: // be able to invoke the callee method Y type with no violations jrose@1145: // of type integrity. jrose@1145: // Return NULL if all is well, else a short error message. jrose@1145: const char* MethodHandles::check_method_type_change(oop src_mtype, int src_beg, int src_end, jrose@1145: int insert_argnum, oop insert_type, jrose@1145: int change_argnum, oop change_type, jrose@1145: int delete_argnum, jrose@1474: oop dst_mtype, int dst_beg, int dst_end, jrose@1474: bool raw) { jrose@2639: objArrayOop src_ptypes = java_lang_invoke_MethodType::ptypes(src_mtype); jrose@2639: objArrayOop dst_ptypes = java_lang_invoke_MethodType::ptypes(dst_mtype); jrose@1145: jrose@1145: int src_max = src_ptypes->length(); jrose@1145: int dst_max = dst_ptypes->length(); jrose@1145: jrose@1145: if (src_end == -1) src_end = src_max; jrose@1145: if (dst_end == -1) dst_end = dst_max; jrose@1145: jrose@1145: assert(0 <= src_beg && src_beg <= src_end && src_end <= src_max, "oob"); jrose@1145: assert(0 <= dst_beg && dst_beg <= dst_end && dst_end <= dst_max, "oob"); jrose@1145: jrose@1145: // pending actions; set to -1 when done: jrose@1145: int ins_idx = insert_argnum, chg_idx = change_argnum, del_idx = delete_argnum; jrose@1145: jrose@1145: const char* err = NULL; jrose@1145: jrose@1145: // Walk along each array of parameter types, including a virtual jrose@1145: // NULL end marker at the end of each. jrose@1145: for (int src_idx = src_beg, dst_idx = dst_beg; jrose@1145: (src_idx <= src_end && dst_idx <= dst_end); jrose@1145: src_idx++, dst_idx++) { jrose@1145: oop src_type = (src_idx == src_end) ? oop(NULL) : src_ptypes->obj_at(src_idx); jrose@1145: oop dst_type = (dst_idx == dst_end) ? oop(NULL) : dst_ptypes->obj_at(dst_idx); jrose@1145: bool fix_null_src_type = false; jrose@1145: jrose@1145: // Perform requested edits. jrose@1145: if (ins_idx == src_idx) { jrose@1145: // note that the inserted guy is never affected by a change or deletion jrose@1145: ins_idx = -1; jrose@1145: src_type = insert_type; jrose@1145: fix_null_src_type = true; jrose@1145: --src_idx; // back up to process src type on next loop jrose@1145: src_idx = src_end; jrose@1145: } else { jrose@1145: // note that the changed guy can be immediately deleted jrose@1145: if (chg_idx == src_idx) { jrose@1145: chg_idx = -1; jrose@1145: assert(src_idx < src_end, "oob"); jrose@1145: src_type = change_type; jrose@1145: fix_null_src_type = true; jrose@1145: } jrose@1145: if (del_idx == src_idx) { jrose@1145: del_idx = -1; jrose@1145: assert(src_idx < src_end, "oob"); jrose@1145: --dst_idx; jrose@1145: continue; // rerun loop after skipping this position jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if (src_type == NULL && fix_null_src_type) jrose@1145: // explicit null in this case matches any dest reference jrose@1145: src_type = (java_lang_Class::is_primitive(dst_type) ? object_java_mirror() : dst_type); jrose@1145: jrose@1145: // Compare the two argument types. jrose@1145: if (src_type != dst_type) { jrose@1145: if (src_type == NULL) return "not enough arguments"; jrose@1145: if (dst_type == NULL) return "too many arguments"; jrose@1474: err = check_argument_type_change(src_type, dst_type, dst_idx, raw); jrose@1145: if (err != NULL) return err; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // Now compare return types also. jrose@2639: oop src_rtype = java_lang_invoke_MethodType::rtype(src_mtype); jrose@2639: oop dst_rtype = java_lang_invoke_MethodType::rtype(dst_mtype); jrose@1145: if (src_rtype != dst_rtype) { jrose@1474: err = check_return_type_change(dst_rtype, src_rtype, raw); // note reversal! jrose@1145: if (err != NULL) return err; jrose@1145: } jrose@1145: jrose@1145: assert(err == NULL, ""); jrose@1145: return NULL; // all is well jrose@1145: } jrose@1145: jrose@1145: jrose@1145: const char* MethodHandles::check_argument_type_change(BasicType src_type, jrose@1474: klassOop src_klass, jrose@1474: BasicType dst_type, jrose@1474: klassOop dst_klass, jrose@1474: int argnum, jrose@1474: bool raw) { jrose@1145: const char* err = NULL; jrose@1474: bool for_return = (argnum < 0); jrose@1145: jrose@1145: // just in case: jrose@1145: if (src_type == T_ARRAY) src_type = T_OBJECT; jrose@1145: if (dst_type == T_ARRAY) dst_type = T_OBJECT; jrose@1145: jrose@1145: // Produce some nice messages if VerifyMethodHandles is turned on: jrose@1474: if (!same_basic_type_for_arguments(src_type, dst_type, raw, for_return)) { jrose@1145: if (src_type == T_OBJECT) { jrose@1474: if (raw && dst_type == T_INT && is_always_null_type(src_klass)) jrose@1474: return NULL; // OK to convert a null pointer to a garbage int jrose@1145: err = ((argnum >= 0) jrose@1145: ? "type mismatch: passing a %s for method argument #%d, which expects primitive %s" jrose@1145: : "type mismatch: returning a %s, but caller expects primitive %s"); jrose@1145: } else if (dst_type == T_OBJECT) { jrose@1474: err = ((argnum >= 0) jrose@1145: ? "type mismatch: passing a primitive %s for method argument #%d, which expects %s" jrose@1145: : "type mismatch: returning a primitive %s, but caller expects %s"); jrose@1145: } else { jrose@1474: err = ((argnum >= 0) jrose@1145: ? "type mismatch: passing a %s for method argument #%d, which expects %s" jrose@1145: : "type mismatch: returning a %s, but caller expects %s"); jrose@1145: } jrose@1474: } else if (src_type == T_OBJECT && dst_type == T_OBJECT && jrose@1474: class_cast_needed(src_klass, dst_klass)) { jrose@1145: if (!class_cast_needed(dst_klass, src_klass)) { jrose@1474: if (raw) jrose@1474: return NULL; // reverse cast is OK; the MH target is trusted to enforce it jrose@1474: err = ((argnum >= 0) jrose@1145: ? "cast required: passing a %s for method argument #%d, which expects %s" jrose@1145: : "cast required: returning a %s, but caller expects %s"); jrose@1145: } else { jrose@1474: err = ((argnum >= 0) jrose@1145: ? "reference mismatch: passing a %s for method argument #%d, which expects %s" jrose@1145: : "reference mismatch: returning a %s, but caller expects %s"); jrose@1145: } jrose@1145: } else { jrose@1145: // passed the obstacle course jrose@1145: return NULL; jrose@1145: } jrose@1145: jrose@1145: // format, format, format jrose@1145: const char* src_name = type2name(src_type); jrose@1145: const char* dst_name = type2name(dst_type); jrose@1145: if (src_name == NULL) src_name = "unknown type"; jrose@1145: if (dst_name == NULL) dst_name = "unknown type"; jrose@2760: if (src_type == T_OBJECT) jrose@2760: src_name = (src_klass != NULL) ? Klass::cast(src_klass)->external_name() : "an unresolved class"; jrose@2760: if (dst_type == T_OBJECT) jrose@2760: dst_name = (dst_klass != NULL) ? Klass::cast(dst_klass)->external_name() : "an unresolved class"; jrose@1145: jrose@1145: size_t msglen = strlen(err) + strlen(src_name) + strlen(dst_name) + (argnum < 10 ? 1 : 11); jrose@1145: char* msg = NEW_RESOURCE_ARRAY(char, msglen + 1); jrose@1145: if (argnum >= 0) { jrose@1145: assert(strstr(err, "%d") != NULL, ""); jrose@1145: jio_snprintf(msg, msglen, err, src_name, argnum, dst_name); jrose@1145: } else { jrose@1145: assert(strstr(err, "%d") == NULL, ""); jrose@1145: jio_snprintf(msg, msglen, err, src_name, dst_name); jrose@1145: } jrose@1145: return msg; jrose@1145: } jrose@1145: jrose@1145: // Compute the depth within the stack of the given argument, i.e., jrose@1145: // the combined size of arguments to the right of the given argument. jrose@1145: // For the last argument (ptypes.length-1) this will be zero. jrose@1145: // For the first argument (0) this will be the size of all jrose@1145: // arguments but that one. For the special number -1, this jrose@1145: // will be the size of all arguments, including the first. jrose@1145: // If the argument is neither -1 nor a valid argument index, jrose@1145: // then return a negative number. Otherwise, the result jrose@1145: // is in the range [0..vmslots] inclusive. jrose@1145: int MethodHandles::argument_slot(oop method_type, int arg) { jrose@2639: objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(method_type); jrose@1145: int argslot = 0; jrose@1145: int len = ptypes->length(); jrose@1145: if (arg < -1 || arg >= len) return -99; jrose@1145: for (int i = len-1; i > arg; i--) { jrose@1145: BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i)); jrose@1145: argslot += type2size[bt]; jrose@1145: } jrose@1145: assert(argument_slot_to_argnum(method_type, argslot) == arg, "inverse works"); jrose@1145: return argslot; jrose@1145: } jrose@1145: jrose@1145: // Given a slot number, return the argument number. jrose@1145: int MethodHandles::argument_slot_to_argnum(oop method_type, int query_argslot) { jrose@2639: objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(method_type); jrose@1145: int argslot = 0; jrose@1145: int len = ptypes->length(); jrose@1145: for (int i = len-1; i >= 0; i--) { jrose@1145: if (query_argslot == argslot) return i; jrose@1145: BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i)); jrose@1145: argslot += type2size[bt]; jrose@1145: } jrose@1145: // return pseudo-arg deepest in stack: jrose@1145: if (query_argslot == argslot) return -1; jrose@1145: return -99; // oob slot, or splitting a double-slot arg jrose@1145: } jrose@1145: jrose@1145: methodHandle MethodHandles::dispatch_decoded_method(methodHandle m, jrose@1145: KlassHandle receiver_limit, jrose@1145: int decode_flags, jrose@1145: KlassHandle receiver_klass, jrose@1145: TRAPS) { jrose@1145: assert((decode_flags & ~_DMF_DIRECT_MASK) == 0, "must be direct method reference"); jrose@1145: assert((decode_flags & _dmf_has_receiver) != 0, "must have a receiver or first reference argument"); jrose@1145: jrose@1145: if (!m->is_static() && jrose@1145: (receiver_klass.is_null() || !receiver_klass->is_subtype_of(m->method_holder()))) jrose@1145: // given type does not match class of method, or receiver is null! jrose@1145: // caller should have checked this, but let's be extra careful... jrose@1145: return methodHandle(); jrose@1145: jrose@1145: if (receiver_limit.not_null() && jrose@1145: (receiver_klass.not_null() && !receiver_klass->is_subtype_of(receiver_limit()))) jrose@1145: // given type is not limited to the receiver type jrose@1145: // note that a null receiver can match any reference value, for a static method jrose@1145: return methodHandle(); jrose@1145: jrose@1145: if (!(decode_flags & MethodHandles::_dmf_does_dispatch)) { jrose@1145: // pre-dispatched or static method (null receiver is OK for static) jrose@1145: return m; jrose@1145: jrose@1145: } else if (receiver_klass.is_null()) { jrose@1145: // null receiver value; cannot dispatch jrose@1145: return methodHandle(); jrose@1145: jrose@1145: } else if (!(decode_flags & MethodHandles::_dmf_from_interface)) { jrose@1145: // perform virtual dispatch jrose@1145: int vtable_index = m->vtable_index(); jrose@1145: guarantee(vtable_index >= 0, "valid vtable index"); jrose@1145: jrose@1145: // receiver_klass might be an arrayKlassOop but all vtables start at jrose@1145: // the same place. The cast is to avoid virtual call and assertion. jrose@1145: // See also LinkResolver::runtime_resolve_virtual_method. jrose@1145: instanceKlass* inst = (instanceKlass*)Klass::cast(receiver_klass()); jrose@1145: DEBUG_ONLY(inst->verify_vtable_index(vtable_index)); jrose@1145: methodOop m_oop = inst->method_at_vtable(vtable_index); jrose@1145: return methodHandle(THREAD, m_oop); jrose@1145: jrose@1145: } else { jrose@1145: // perform interface dispatch jrose@1145: int itable_index = klassItable::compute_itable_index(m()); jrose@1145: guarantee(itable_index >= 0, "valid itable index"); jrose@1145: instanceKlass* inst = instanceKlass::cast(receiver_klass()); jrose@1145: methodOop m_oop = inst->method_at_itable(m->method_holder(), itable_index, THREAD); jrose@1145: return methodHandle(THREAD, m_oop); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::verify_DirectMethodHandle(Handle mh, methodHandle m, TRAPS) { jrose@1145: // Verify type. jrose@2639: Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); jrose@1145: verify_method_type(m, mtype, false, KlassHandle(), CHECK); jrose@1145: jrose@1145: // Verify vmslots. jrose@2639: if (java_lang_invoke_MethodHandle::vmslots(mh()) != m->size_of_parameters()) { jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in DMH"); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::init_DirectMethodHandle(Handle mh, methodHandle m, bool do_dispatch, TRAPS) { jrose@1145: // Check arguments. jrose@1145: if (mh.is_null() || m.is_null() || jrose@1145: (!do_dispatch && m->is_abstract())) { jrose@1145: THROW(vmSymbols::java_lang_InternalError()); jrose@1145: } jrose@1145: jrose@2639: java_lang_invoke_MethodHandle::init_vmslots(mh()); jrose@1145: jrose@1145: if (VerifyMethodHandles) { jrose@1145: // The privileged code which invokes this routine should not make jrose@1145: // a mistake about types, but it's better to verify. jrose@1145: verify_DirectMethodHandle(mh, m, CHECK); jrose@1145: } jrose@1145: jrose@1145: // Finally, after safety checks are done, link to the target method. jrose@1145: // We will follow the same path as the latter part of jrose@1145: // InterpreterRuntime::resolve_invoke(), which first finds the method jrose@1145: // and then decides how to populate the constant pool cache entry jrose@1145: // that links the interpreter calls to the method. We need the same jrose@1145: // bits, and will use the same calling sequence code. jrose@1145: jrose@1145: int vmindex = methodOopDesc::garbage_vtable_index; jrose@1145: oop vmtarget = NULL; jrose@1145: jrose@1145: instanceKlass::cast(m->method_holder())->link_class(CHECK); jrose@1145: jrose@1145: MethodHandleEntry* me = NULL; jrose@1145: if (do_dispatch && Klass::cast(m->method_holder())->is_interface()) { jrose@1145: // We are simulating an invokeinterface instruction. jrose@1145: // (We might also be simulating an invokevirtual on a miranda method, jrose@1145: // but it is safe to treat it as an invokeinterface.) jrose@1145: assert(!m->can_be_statically_bound(), "no final methods on interfaces"); jrose@1145: vmindex = klassItable::compute_itable_index(m()); jrose@1145: assert(vmindex >= 0, "(>=0) == do_dispatch"); jrose@1145: // Set up same bits as ConstantPoolCacheEntry::set_interface_call(). jrose@1145: vmtarget = m->method_holder(); // the interface jrose@1145: me = MethodHandles::entry(MethodHandles::_invokeinterface_mh); jrose@1145: } else if (!do_dispatch || m->can_be_statically_bound()) { jrose@1145: // We are simulating an invokestatic or invokespecial instruction. jrose@1145: // Set up the method pointer, just like ConstantPoolCacheEntry::set_method(). jrose@1145: vmtarget = m(); jrose@1145: // this does not help dispatch, but it will make it possible to parse this MH: jrose@1145: vmindex = methodOopDesc::nonvirtual_vtable_index; jrose@1145: assert(vmindex < 0, "(>=0) == do_dispatch"); jrose@1145: if (!m->is_static()) { jrose@1145: me = MethodHandles::entry(MethodHandles::_invokespecial_mh); jrose@1145: } else { jrose@1145: me = MethodHandles::entry(MethodHandles::_invokestatic_mh); jrose@1145: // Part of the semantics of a static call is an initialization barrier. jrose@1145: // For a DMH, it is done now, when the handle is created. jrose@1145: Klass* k = Klass::cast(m->method_holder()); jrose@1145: if (k->should_be_initialized()) { jrose@1145: k->initialize(CHECK); jrose@1145: } jrose@1145: } jrose@1145: } else { jrose@1145: // We are simulating an invokevirtual instruction. jrose@1145: // Set up the vtable index, just like ConstantPoolCacheEntry::set_method(). jrose@1145: // The key logic is LinkResolver::runtime_resolve_virtual_method. jrose@1145: vmindex = m->vtable_index(); jrose@1145: vmtarget = m->method_holder(); jrose@1145: me = MethodHandles::entry(MethodHandles::_invokevirtual_mh); jrose@1145: } jrose@1145: jrose@1145: if (me == NULL) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: jrose@2639: java_lang_invoke_DirectMethodHandle::set_vmtarget(mh(), vmtarget); jrose@2639: java_lang_invoke_DirectMethodHandle::set_vmindex(mh(), vmindex); jrose@1145: DEBUG_ONLY(int flags; klassOop rlimit); jrose@1145: assert(MethodHandles::decode_method(mh(), rlimit, flags) == m(), jrose@1145: "properly stored for later decoding"); jrose@1145: DEBUG_ONLY(bool actual_do_dispatch = ((flags & _dmf_does_dispatch) != 0)); jrose@1145: assert(!(actual_do_dispatch && !do_dispatch), jrose@1145: "do not perform dispatch if !do_dispatch specified"); jrose@1145: assert(actual_do_dispatch == (vmindex >= 0), "proper later decoding of do_dispatch"); jrose@1145: assert(decode_MethodHandle_stack_pushes(mh()) == 0, "DMH does not move stack"); jrose@1145: jrose@1145: // Done! jrose@2639: java_lang_invoke_MethodHandle::set_vmentry(mh(), me); jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::verify_BoundMethodHandle_with_receiver(Handle mh, jrose@1145: methodHandle m, jrose@1145: TRAPS) { jrose@1145: // Verify type. jrose@2639: oop receiver = java_lang_invoke_BoundMethodHandle::argument(mh()); jrose@2639: Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); jrose@1145: KlassHandle bound_recv_type; jrose@1145: if (receiver != NULL) bound_recv_type = KlassHandle(THREAD, receiver->klass()); jrose@1145: verify_method_type(m, mtype, true, bound_recv_type, CHECK); jrose@1145: jrose@1145: int receiver_pos = m->size_of_parameters() - 1; jrose@1145: jrose@1145: // Verify MH.vmargslot, which should point at the bound receiver. jrose@2639: verify_vmargslot(mh, -1, java_lang_invoke_BoundMethodHandle::vmargslot(mh()), CHECK); jrose@1145: //verify_vmslots(mh, CHECK); jrose@1145: jrose@1145: // Verify vmslots. jrose@2639: if (java_lang_invoke_MethodHandle::vmslots(mh()) != receiver_pos) { jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH (receiver)"); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // Initialize a BMH with a receiver bound directly to a methodOop. jrose@1145: void MethodHandles::init_BoundMethodHandle_with_receiver(Handle mh, jrose@1145: methodHandle original_m, jrose@1145: KlassHandle receiver_limit, jrose@1145: int decode_flags, jrose@1145: TRAPS) { jrose@1145: // Check arguments. jrose@1145: if (mh.is_null() || original_m.is_null()) { jrose@1145: THROW(vmSymbols::java_lang_InternalError()); jrose@1145: } jrose@1145: jrose@1145: KlassHandle receiver_klass; jrose@1145: { jrose@2639: oop receiver_oop = java_lang_invoke_BoundMethodHandle::argument(mh()); jrose@1145: if (receiver_oop != NULL) jrose@1145: receiver_klass = KlassHandle(THREAD, receiver_oop->klass()); jrose@1145: } jrose@1145: methodHandle m = dispatch_decoded_method(original_m, jrose@1145: receiver_limit, decode_flags, jrose@1145: receiver_klass, jrose@1145: CHECK); jrose@1145: if (m.is_null()) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: if (m->is_abstract()) { THROW(vmSymbols::java_lang_AbstractMethodError()); } jrose@1145: jrose@2639: java_lang_invoke_MethodHandle::init_vmslots(mh()); jrose@1145: jrose@1145: if (VerifyMethodHandles) { jrose@1145: verify_BoundMethodHandle_with_receiver(mh, m, CHECK); jrose@1145: } jrose@1145: jrose@2639: java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), m()); jrose@1145: jrose@1145: DEBUG_ONLY(int junk; klassOop junk2); jrose@1145: assert(MethodHandles::decode_method(mh(), junk2, junk) == m(), "properly stored for later decoding"); jrose@1145: assert(decode_MethodHandle_stack_pushes(mh()) == 1, "BMH pushes one stack slot"); jrose@1145: jrose@1145: // Done! jrose@2639: java_lang_invoke_MethodHandle::set_vmentry(mh(), MethodHandles::entry(MethodHandles::_bound_ref_direct_mh)); jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::verify_BoundMethodHandle(Handle mh, Handle target, int argnum, jrose@1145: bool direct_to_method, TRAPS) { jrose@1145: Handle ptype_handle(THREAD, jrose@2639: java_lang_invoke_MethodType::ptype(java_lang_invoke_MethodHandle::type(target()), argnum)); jrose@1145: KlassHandle ptype_klass; jrose@1145: BasicType ptype = java_lang_Class::as_BasicType(ptype_handle(), &ptype_klass); jrose@1145: int slots_pushed = type2size[ptype]; jrose@1145: jrose@2639: oop argument = java_lang_invoke_BoundMethodHandle::argument(mh()); jrose@1145: jrose@1145: const char* err = NULL; jrose@1145: jrose@1145: switch (ptype) { jrose@1145: case T_OBJECT: jrose@1145: if (argument != NULL) jrose@1145: // we must implicitly convert from the arg type to the outgoing ptype jrose@1145: err = check_argument_type_change(T_OBJECT, argument->klass(), ptype, ptype_klass(), argnum); jrose@1145: break; jrose@1145: jrose@1145: case T_ARRAY: case T_VOID: jrose@1145: assert(false, "array, void do not appear here"); jrose@1145: default: jrose@1145: if (ptype != T_INT && !is_subword_type(ptype)) { jrose@1145: err = "unexpected parameter type"; jrose@1145: break; jrose@1145: } jrose@1145: // check subrange of Integer.value, if necessary never@1577: if (argument == NULL || argument->klass() != SystemDictionary::Integer_klass()) { jrose@1145: err = "bound integer argument must be of type java.lang.Integer"; jrose@1145: break; jrose@1145: } jrose@1145: if (ptype != T_INT) { jrose@1145: int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT); jrose@1145: jint value = argument->int_field(value_offset); twisti@2204: int vminfo = adapter_unbox_subword_vminfo(ptype); jrose@1145: jint subword = truncate_subword_from_vminfo(value, vminfo); jrose@1145: if (value != subword) { jrose@1145: err = "bound subword value does not fit into the subword type"; jrose@1145: break; jrose@1145: } jrose@1145: } jrose@1145: break; jrose@1145: case T_FLOAT: jrose@1145: case T_DOUBLE: jrose@1145: case T_LONG: jrose@1145: { jrose@1145: // we must implicitly convert from the unboxed arg type to the outgoing ptype jrose@1145: BasicType argbox = java_lang_boxing_object::basic_type(argument); jrose@1145: if (argbox != ptype) { jrose@1145: err = check_argument_type_change(T_OBJECT, (argument == NULL never@1577: ? SystemDictionary::Object_klass() jrose@1145: : argument->klass()), jrose@1145: ptype, ptype_klass(), argnum); jrose@1145: assert(err != NULL, "this must be an error"); jrose@1145: } jrose@1145: break; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if (err == NULL) { jrose@1145: DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh())); jrose@1145: if (direct_to_method) { jrose@1145: assert(this_pushes == slots_pushed, "BMH pushes one or two stack slots"); jrose@1145: assert(slots_pushed <= MethodHandlePushLimit, ""); jrose@1145: } else { jrose@1474: int target_pushes = decode_MethodHandle_stack_pushes(target()); jrose@1474: assert(this_pushes == slots_pushed + target_pushes, "BMH stack motion must be correct"); jrose@1145: // do not blow the stack; use a Java-based adapter if this limit is exceeded twisti@1573: // FIXME twisti@1573: // if (slots_pushed + target_pushes > MethodHandlePushLimit) twisti@1573: // err = "too many bound parameters"; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if (err == NULL) { jrose@1145: // Verify the rest of the method type. jrose@2639: err = check_method_type_insertion(java_lang_invoke_MethodHandle::type(mh()), jrose@1145: argnum, ptype_handle(), jrose@2639: java_lang_invoke_MethodHandle::type(target())); jrose@1145: } jrose@1145: jrose@1145: if (err != NULL) { jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), err); jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::init_BoundMethodHandle(Handle mh, Handle target, int argnum, TRAPS) { jrose@1145: // Check arguments. jrose@2639: if (mh.is_null() || target.is_null() || !java_lang_invoke_MethodHandle::is_instance(target())) { jrose@1145: THROW(vmSymbols::java_lang_InternalError()); jrose@1145: } jrose@1145: jrose@2639: java_lang_invoke_MethodHandle::init_vmslots(mh()); jrose@1145: jrose@1145: if (VerifyMethodHandles) { jrose@1145: int insert_after = argnum - 1; jrose@2639: verify_vmargslot(mh, insert_after, java_lang_invoke_BoundMethodHandle::vmargslot(mh()), CHECK); jrose@1145: verify_vmslots(mh, CHECK); jrose@1145: } jrose@1145: twisti@1573: // Get bound type and required slots. jrose@2639: oop ptype_oop = java_lang_invoke_MethodType::ptype(java_lang_invoke_MethodHandle::type(target()), argnum); twisti@1573: BasicType ptype = java_lang_Class::as_BasicType(ptype_oop); twisti@1573: int slots_pushed = type2size[ptype]; twisti@1573: jrose@1145: // If (a) the target is a direct non-dispatched method handle, jrose@1145: // or (b) the target is a dispatched direct method handle and we jrose@1145: // are binding the receiver, cut out the middle-man. jrose@1145: // Do this by decoding the DMH and using its methodOop directly as vmtarget. jrose@1145: bool direct_to_method = false; jrose@1145: if (OptimizeMethodHandles && jrose@1145: target->klass() == SystemDictionary::DirectMethodHandle_klass() && jrose@2639: (argnum == 0 || java_lang_invoke_DirectMethodHandle::vmindex(target()) < 0)) { jrose@1145: int decode_flags = 0; klassOop receiver_limit_oop = NULL; jrose@1145: methodHandle m(THREAD, decode_method(target(), receiver_limit_oop, decode_flags)); jrose@1145: if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "DMH failed to decode"); } twisti@1573: DEBUG_ONLY(int m_vmslots = m->size_of_parameters() - slots_pushed); // pos. of 1st arg. jrose@2639: assert(java_lang_invoke_BoundMethodHandle::vmslots(mh()) == m_vmslots, "type w/ m sig"); jrose@1145: if (argnum == 0 && (decode_flags & _dmf_has_receiver) != 0) { jrose@1145: KlassHandle receiver_limit(THREAD, receiver_limit_oop); jrose@1145: init_BoundMethodHandle_with_receiver(mh, m, jrose@1145: receiver_limit, decode_flags, jrose@1145: CHECK); jrose@1145: return; jrose@1145: } jrose@1145: jrose@1145: // Even if it is not a bound receiver, we still might be able jrose@1145: // to bind another argument and still invoke the methodOop directly. jrose@1145: if (!(decode_flags & _dmf_does_dispatch)) { jrose@1145: direct_to_method = true; jrose@2639: java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), m()); jrose@1145: } jrose@1145: } jrose@1145: if (!direct_to_method) jrose@2639: java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), target()); jrose@1145: jrose@1145: if (VerifyMethodHandles) { jrose@1145: verify_BoundMethodHandle(mh, target, argnum, direct_to_method, CHECK); jrose@1145: } jrose@1145: jrose@1145: // Next question: Is this a ref, int, or long bound value? jrose@1145: MethodHandleEntry* me = NULL; jrose@1145: if (ptype == T_OBJECT) { jrose@1145: if (direct_to_method) me = MethodHandles::entry(_bound_ref_direct_mh); jrose@1145: else me = MethodHandles::entry(_bound_ref_mh); jrose@1145: } else if (slots_pushed == 2) { jrose@1145: if (direct_to_method) me = MethodHandles::entry(_bound_long_direct_mh); jrose@1145: else me = MethodHandles::entry(_bound_long_mh); jrose@1145: } else if (slots_pushed == 1) { jrose@1145: if (direct_to_method) me = MethodHandles::entry(_bound_int_direct_mh); jrose@1145: else me = MethodHandles::entry(_bound_int_mh); jrose@1145: } else { jrose@1145: assert(false, ""); jrose@1145: } jrose@1145: jrose@1145: // Done! jrose@2639: java_lang_invoke_MethodHandle::set_vmentry(mh(), me); jrose@1145: } jrose@1145: jrose@1145: static void throw_InternalError_for_bad_conversion(int conversion, const char* err, TRAPS) { jrose@1145: char msg[200]; jrose@1145: jio_snprintf(msg, sizeof(msg), "bad adapter (conversion=0x%08x): %s", conversion, err); jrose@1145: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), msg); jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::verify_AdapterMethodHandle(Handle mh, int argnum, TRAPS) { jrose@2639: jint conversion = java_lang_invoke_AdapterMethodHandle::conversion(mh()); jrose@2639: int argslot = java_lang_invoke_AdapterMethodHandle::vmargslot(mh()); jrose@1145: jrose@1145: verify_vmargslot(mh, argnum, argslot, CHECK); jrose@1145: verify_vmslots(mh, CHECK); jrose@1145: jrose@1145: jint conv_op = adapter_conversion_op(conversion); jrose@1145: if (!conv_op_valid(conv_op)) { jrose@1145: throw_InternalError_for_bad_conversion(conversion, "unknown conversion op", THREAD); jrose@1145: return; jrose@1145: } jrose@1145: EntryKind ek = adapter_entry_kind(conv_op); jrose@1145: jrose@1145: int stack_move = adapter_conversion_stack_move(conversion); jrose@1145: BasicType src = adapter_conversion_src_type(conversion); jrose@1145: BasicType dest = adapter_conversion_dest_type(conversion); jrose@1145: int vminfo = adapter_conversion_vminfo(conversion); // should be zero jrose@1145: jrose@2639: Handle argument(THREAD, java_lang_invoke_AdapterMethodHandle::argument(mh())); jrose@2639: Handle target(THREAD, java_lang_invoke_AdapterMethodHandle::vmtarget(mh())); jrose@2639: Handle src_mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); jrose@2639: Handle dst_mtype(THREAD, java_lang_invoke_MethodHandle::type(target())); jrose@1145: jrose@1145: const char* err = NULL; jrose@1145: jrose@1145: if (err == NULL) { jrose@1145: // Check that the correct argument is supplied, but only if it is required. jrose@1145: switch (ek) { jrose@1145: case _adapter_check_cast: // target type of cast jrose@1145: case _adapter_ref_to_prim: // wrapper type from which to unbox jrose@1145: case _adapter_prim_to_ref: // wrapper type to box into jrose@1145: case _adapter_collect_args: // array type to collect into jrose@1145: case _adapter_spread_args: // array type to spread from jrose@1145: if (!java_lang_Class::is_instance(argument()) jrose@1145: || java_lang_Class::is_primitive(argument())) jrose@1145: { err = "adapter requires argument of type java.lang.Class"; break; } jrose@1145: if (ek == _adapter_collect_args || jrose@1145: ek == _adapter_spread_args) { jrose@1145: // Make sure it is a suitable collection type. (Array, for now.) jrose@1145: Klass* ak = Klass::cast(java_lang_Class::as_klassOop(argument())); jrose@1145: if (!ak->oop_is_objArray()) { jrose@1145: { err = "adapter requires argument of type java.lang.Class"; break; } jrose@1145: } jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_flyby: jrose@1145: case _adapter_ricochet: jrose@2639: if (!java_lang_invoke_MethodHandle::is_instance(argument())) jrose@1145: { err = "MethodHandle adapter argument required"; break; } jrose@1145: break; jrose@1145: default: jrose@1145: if (argument.not_null()) jrose@1145: { err = "adapter has spurious argument"; break; } jrose@1145: break; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if (err == NULL) { jrose@1145: // Check that the src/dest types are supplied if needed. jrose@1145: switch (ek) { jrose@1474: case _adapter_check_cast: jrose@1474: if (src != T_OBJECT || dest != T_OBJECT) { jrose@1474: err = "adapter requires object src/dest conversion subfields"; jrose@1474: } jrose@1474: break; jrose@1145: case _adapter_prim_to_prim: jrose@1145: if (!is_java_primitive(src) || !is_java_primitive(dest) || src == dest) { jrose@1145: err = "adapter requires primitive src/dest conversion subfields"; break; jrose@1145: } jrose@1145: if ( (src == T_FLOAT || src == T_DOUBLE) && !(dest == T_FLOAT || dest == T_DOUBLE) || jrose@1145: !(src == T_FLOAT || src == T_DOUBLE) && (dest == T_FLOAT || dest == T_DOUBLE)) { jrose@1145: err = "adapter cannot convert beween floating and fixed-point"; break; jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_ref_to_prim: jrose@1145: if (src != T_OBJECT || !is_java_primitive(dest) jrose@1145: || argument() != Klass::cast(SystemDictionary::box_klass(dest))->java_mirror()) { jrose@1145: err = "adapter requires primitive dest conversion subfield"; break; jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_prim_to_ref: jrose@1145: if (!is_java_primitive(src) || dest != T_OBJECT jrose@1145: || argument() != Klass::cast(SystemDictionary::box_klass(src))->java_mirror()) { jrose@1145: err = "adapter requires primitive src conversion subfield"; break; jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_swap_args: jrose@1145: case _adapter_rot_args: jrose@1145: { jrose@1145: if (!src || src != dest) { jrose@1145: err = "adapter requires src/dest conversion subfields for swap"; break; jrose@1145: } jrose@1145: int swap_size = type2size[src]; jrose@2639: oop src_mtype = java_lang_invoke_AdapterMethodHandle::type(mh()); jrose@2639: oop dest_mtype = java_lang_invoke_AdapterMethodHandle::type(target()); jrose@2639: int slot_limit = java_lang_invoke_AdapterMethodHandle::vmslots(target()); jrose@1145: int src_slot = argslot; jrose@1145: int dest_slot = vminfo; jrose@1145: bool rotate_up = (src_slot > dest_slot); // upward rotation jrose@1145: int src_arg = argnum; jrose@1145: int dest_arg = argument_slot_to_argnum(dest_mtype, dest_slot); jrose@1145: verify_vmargslot(mh, dest_arg, dest_slot, CHECK); jrose@1145: if (!(dest_slot >= src_slot + swap_size) && jrose@1145: !(src_slot >= dest_slot + swap_size)) { jrose@1145: err = "source, destination slots must be distinct"; jrose@1145: } else if (ek == _adapter_swap_args && !(src_slot > dest_slot)) { jrose@1145: err = "source of swap must be deeper in stack"; jrose@1145: } else if (ek == _adapter_swap_args) { jrose@2639: err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype, dest_arg), jrose@2639: java_lang_invoke_MethodType::ptype(dest_mtype, src_arg), jrose@1145: dest_arg); jrose@1145: } else if (ek == _adapter_rot_args) { jrose@1145: if (rotate_up) { jrose@1145: assert((src_slot > dest_slot) && (src_arg < dest_arg), ""); jrose@1145: // rotate up: [dest_slot..src_slot-ss] --> [dest_slot+ss..src_slot] jrose@1145: // that is: [src_arg+1..dest_arg] --> [src_arg..dest_arg-1] jrose@1145: for (int i = src_arg+1; i <= dest_arg && err == NULL; i++) { jrose@2639: err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype, i), jrose@2639: java_lang_invoke_MethodType::ptype(dest_mtype, i-1), jrose@1145: i); jrose@1145: } jrose@1145: } else { // rotate down jrose@1145: assert((src_slot < dest_slot) && (src_arg > dest_arg), ""); jrose@1145: // rotate down: [src_slot+ss..dest_slot] --> [src_slot..dest_slot-ss] jrose@1145: // that is: [dest_arg..src_arg-1] --> [dst_arg+1..src_arg] jrose@1145: for (int i = dest_arg; i <= src_arg-1 && err == NULL; i++) { jrose@2639: err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype, i), jrose@2639: java_lang_invoke_MethodType::ptype(dest_mtype, i+1), jrose@1145: i); jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: if (err == NULL) jrose@2639: err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype, src_arg), jrose@2639: java_lang_invoke_MethodType::ptype(dest_mtype, dest_arg), jrose@1145: src_arg); jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_collect_args: jrose@1145: case _adapter_spread_args: jrose@1145: { jrose@1145: BasicType coll_type = (ek == _adapter_collect_args) ? dest : src; jrose@1145: BasicType elem_type = (ek == _adapter_collect_args) ? src : dest; jrose@1145: if (coll_type != T_OBJECT || elem_type != T_OBJECT) { jrose@1145: err = "adapter requires src/dest subfields"; break; jrose@1145: // later: jrose@1145: // - consider making coll be a primitive array jrose@1145: // - consider making coll be a heterogeneous collection jrose@1145: } jrose@1145: } jrose@1145: break; jrose@1145: default: jrose@1145: if (src != 0 || dest != 0) { jrose@1145: err = "adapter has spurious src/dest conversion subfields"; break; jrose@1145: } jrose@1145: break; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if (err == NULL) { jrose@1145: // Check the stack_move subfield. jrose@1145: // It must always report the net change in stack size, positive or negative. jrose@1145: int slots_pushed = stack_move / stack_move_unit(); jrose@1145: switch (ek) { jrose@1145: case _adapter_prim_to_prim: jrose@1145: case _adapter_ref_to_prim: jrose@1145: case _adapter_prim_to_ref: jrose@1145: if (slots_pushed != type2size[dest] - type2size[src]) { jrose@1145: err = "wrong stack motion for primitive conversion"; jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_dup_args: jrose@1145: if (slots_pushed <= 0) { jrose@1145: err = "adapter requires conversion subfield slots_pushed > 0"; jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_drop_args: jrose@1145: if (slots_pushed >= 0) { jrose@1145: err = "adapter requires conversion subfield slots_pushed < 0"; jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_collect_args: jrose@1145: if (slots_pushed > 1) { jrose@1145: err = "adapter requires conversion subfield slots_pushed <= 1"; jrose@1145: } jrose@1145: break; jrose@1145: case _adapter_spread_args: jrose@1145: if (slots_pushed < -1) { jrose@1145: err = "adapter requires conversion subfield slots_pushed >= -1"; jrose@1145: } jrose@1145: break; jrose@1145: default: jrose@1145: if (stack_move != 0) { jrose@1145: err = "adapter has spurious stack_move conversion subfield"; jrose@1145: } jrose@1145: break; jrose@1145: } jrose@1145: if (err == NULL && stack_move != slots_pushed * stack_move_unit()) { jrose@1145: err = "stack_move conversion subfield must be multiple of stack_move_unit"; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if (err == NULL) { jrose@1145: // Make sure this adapter does not push too deeply. jrose@1145: int slots_pushed = stack_move / stack_move_unit(); jrose@2639: int this_vmslots = java_lang_invoke_MethodHandle::vmslots(mh()); jrose@2639: int target_vmslots = java_lang_invoke_MethodHandle::vmslots(target()); jrose@1474: if (slots_pushed != (target_vmslots - this_vmslots)) { jrose@1145: err = "stack_move inconsistent with previous and current MethodType vmslots"; jrose@1145: } else if (slots_pushed > 0) { jrose@1145: // verify stack_move against MethodHandlePushLimit jrose@1474: int target_pushes = decode_MethodHandle_stack_pushes(target()); jrose@1145: // do not blow the stack; use a Java-based adapter if this limit is exceeded jrose@1474: if (slots_pushed + target_pushes > MethodHandlePushLimit) { jrose@1145: err = "adapter pushes too many parameters"; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // While we're at it, check that the stack motion decoder works: jrose@1474: DEBUG_ONLY(int target_pushes = decode_MethodHandle_stack_pushes(target())); jrose@1145: DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh())); jrose@1474: assert(this_pushes == slots_pushed + target_pushes, "AMH stack motion must be correct"); jrose@1145: } jrose@1145: jrose@1145: if (err == NULL && vminfo != 0) { jrose@1145: switch (ek) { jrose@1145: case _adapter_swap_args: jrose@1145: case _adapter_rot_args: jrose@1145: break; // OK jrose@1145: default: jrose@1145: err = "vminfo subfield is reserved to the JVM"; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // Do additional ad hoc checks. jrose@1145: if (err == NULL) { jrose@1145: switch (ek) { jrose@1145: case _adapter_retype_only: jrose@1474: err = check_method_type_passthrough(src_mtype(), dst_mtype(), false); jrose@1474: break; jrose@1474: jrose@1474: case _adapter_retype_raw: jrose@1474: err = check_method_type_passthrough(src_mtype(), dst_mtype(), true); jrose@1145: break; jrose@1145: jrose@1145: case _adapter_check_cast: jrose@1145: { jrose@1145: // The actual value being checked must be a reference: jrose@2639: err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), argnum), jrose@1145: object_java_mirror(), argnum); jrose@1145: if (err != NULL) break; jrose@1145: jrose@1145: // The output of the cast must fit with the destination argument: jrose@1145: Handle cast_class = argument; jrose@1145: err = check_method_type_conversion(src_mtype(), jrose@1145: argnum, cast_class(), jrose@1145: dst_mtype()); jrose@1145: } jrose@1145: break; jrose@1145: jrose@1145: // %%% TO DO: continue in remaining cases to verify src/dst_mtype if VerifyMethodHandles jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if (err != NULL) { jrose@1145: throw_InternalError_for_bad_conversion(conversion, err, THREAD); jrose@1145: return; jrose@1145: } jrose@1145: jrose@1145: } jrose@1145: jrose@1145: void MethodHandles::init_AdapterMethodHandle(Handle mh, Handle target, int argnum, TRAPS) { jrose@2639: oop argument = java_lang_invoke_AdapterMethodHandle::argument(mh()); jrose@2639: int argslot = java_lang_invoke_AdapterMethodHandle::vmargslot(mh()); jrose@2639: jint conversion = java_lang_invoke_AdapterMethodHandle::conversion(mh()); jrose@1145: jint conv_op = adapter_conversion_op(conversion); jrose@1145: jrose@1145: // adjust the adapter code to the internal EntryKind enumeration: jrose@1145: EntryKind ek_orig = adapter_entry_kind(conv_op); jrose@1145: EntryKind ek_opt = ek_orig; // may be optimized jrose@1145: jrose@1145: // Finalize the vmtarget field (Java initialized it to null). jrose@2639: if (!java_lang_invoke_MethodHandle::is_instance(target())) { jrose@1145: throw_InternalError_for_bad_conversion(conversion, "bad target", THREAD); jrose@1145: return; jrose@1145: } jrose@2639: java_lang_invoke_AdapterMethodHandle::set_vmtarget(mh(), target()); jrose@1145: jrose@1145: if (VerifyMethodHandles) { jrose@1145: verify_AdapterMethodHandle(mh, argnum, CHECK); jrose@1145: } jrose@1145: jrose@1145: int stack_move = adapter_conversion_stack_move(conversion); jrose@1145: BasicType src = adapter_conversion_src_type(conversion); jrose@1145: BasicType dest = adapter_conversion_dest_type(conversion); jrose@1145: int vminfo = adapter_conversion_vminfo(conversion); // should be zero jrose@1145: jrose@1145: const char* err = NULL; jrose@1145: jrose@1145: // Now it's time to finish the case analysis and pick a MethodHandleEntry. jrose@1145: switch (ek_orig) { jrose@1145: case _adapter_retype_only: jrose@1474: case _adapter_retype_raw: jrose@1145: case _adapter_check_cast: jrose@1145: case _adapter_dup_args: jrose@1145: case _adapter_drop_args: jrose@1145: // these work fine via general case code jrose@1145: break; jrose@1145: jrose@1145: case _adapter_prim_to_prim: jrose@1145: { jrose@1145: // Non-subword cases are {int,float,long,double} -> {int,float,long,double}. jrose@1145: // And, the {float,double} -> {int,long} cases must be handled by Java. jrose@1145: switch (type2size[src] *4+ type2size[dest]) { jrose@1145: case 1 *4+ 1: jrose@1145: assert(src == T_INT || is_subword_type(src), "source is not float"); jrose@1145: // Subword-related cases are int -> {boolean,byte,char,short}. jrose@1145: ek_opt = _adapter_opt_i2i; twisti@2204: vminfo = adapter_prim_to_prim_subword_vminfo(dest); jrose@1145: break; jrose@1145: case 2 *4+ 1: jrose@1145: if (src == T_LONG && (dest == T_INT || is_subword_type(dest))) { jrose@1145: ek_opt = _adapter_opt_l2i; twisti@2204: vminfo = adapter_prim_to_prim_subword_vminfo(dest); jrose@1145: } else if (src == T_DOUBLE && dest == T_FLOAT) { jrose@1145: ek_opt = _adapter_opt_d2f; jrose@1145: } else { jrose@1145: assert(false, ""); jrose@1145: } jrose@1145: break; jrose@1145: case 1 *4+ 2: jrose@1145: if (src == T_INT && dest == T_LONG) { jrose@1145: ek_opt = _adapter_opt_i2l; jrose@1145: } else if (src == T_FLOAT && dest == T_DOUBLE) { jrose@1145: ek_opt = _adapter_opt_f2d; jrose@1145: } else { jrose@1145: assert(false, ""); jrose@1145: } jrose@1145: break; jrose@1145: default: jrose@1145: assert(false, ""); jrose@1145: break; jrose@1145: } jrose@1145: } jrose@1145: break; jrose@1145: jrose@1145: case _adapter_ref_to_prim: jrose@1145: { jrose@1145: switch (type2size[dest]) { jrose@1145: case 1: jrose@1145: ek_opt = _adapter_opt_unboxi; twisti@2204: vminfo = adapter_unbox_subword_vminfo(dest); jrose@1145: break; jrose@1145: case 2: jrose@1145: ek_opt = _adapter_opt_unboxl; jrose@1145: break; jrose@1145: default: jrose@1145: assert(false, ""); jrose@1145: break; jrose@1145: } jrose@1145: } jrose@1145: break; jrose@1145: jrose@1145: case _adapter_prim_to_ref: jrose@1145: goto throw_not_impl; // allocates, hence could block jrose@1145: jrose@1145: case _adapter_swap_args: jrose@1145: case _adapter_rot_args: jrose@1145: { jrose@1145: int swap_slots = type2size[src]; jrose@2639: int slot_limit = java_lang_invoke_AdapterMethodHandle::vmslots(mh()); jrose@1145: int src_slot = argslot; jrose@1145: int dest_slot = vminfo; jrose@1145: int rotate = (ek_orig == _adapter_swap_args) ? 0 : (src_slot > dest_slot) ? 1 : -1; jrose@1145: switch (swap_slots) { jrose@1145: case 1: jrose@1145: ek_opt = (!rotate ? _adapter_opt_swap_1 : jrose@1145: rotate > 0 ? _adapter_opt_rot_1_up : _adapter_opt_rot_1_down); jrose@1145: break; jrose@1145: case 2: jrose@1145: ek_opt = (!rotate ? _adapter_opt_swap_2 : jrose@1145: rotate > 0 ? _adapter_opt_rot_2_up : _adapter_opt_rot_2_down); jrose@1145: break; jrose@1145: default: jrose@1145: assert(false, ""); jrose@1145: break; jrose@1145: } jrose@1145: } jrose@1145: break; jrose@1145: jrose@1145: case _adapter_collect_args: jrose@1145: goto throw_not_impl; // allocates, hence could block jrose@1145: jrose@1145: case _adapter_spread_args: jrose@1145: { jrose@1145: // vminfo will be the required length of the array jrose@1145: int slots_pushed = stack_move / stack_move_unit(); jrose@1145: int array_size = slots_pushed + 1; jrose@1145: assert(array_size >= 0, ""); jrose@1145: vminfo = array_size; jrose@1145: switch (array_size) { jrose@1145: case 0: ek_opt = _adapter_opt_spread_0; break; jrose@1145: case 1: ek_opt = _adapter_opt_spread_1; break; jrose@1145: default: ek_opt = _adapter_opt_spread_more; break; jrose@1145: } jrose@1145: if ((vminfo & CONV_VMINFO_MASK) != vminfo) jrose@1145: goto throw_not_impl; // overflow jrose@1145: } jrose@1145: break; jrose@1145: jrose@1145: case _adapter_flyby: jrose@1145: case _adapter_ricochet: jrose@1145: goto throw_not_impl; // runs Java code, hence could block jrose@1145: jrose@1145: default: jrose@1145: // should have failed much earlier; must be a missing case here jrose@1145: assert(false, "incomplete switch"); jrose@1145: // and fall through: jrose@1145: jrose@1145: throw_not_impl: jrose@1145: // FIXME: these adapters are NYI jrose@1145: err = "adapter not yet implemented in the JVM"; jrose@1145: break; jrose@1145: } jrose@1145: jrose@1145: if (err != NULL) { jrose@1145: throw_InternalError_for_bad_conversion(conversion, err, THREAD); jrose@1145: return; jrose@1145: } jrose@1145: jrose@1145: // Rebuild the conversion value; maybe parts of it were changed. jrose@1145: jint new_conversion = adapter_conversion(conv_op, src, dest, stack_move, vminfo); jrose@1145: jrose@1145: // Finalize the conversion field. (Note that it is final to Java code.) jrose@2639: java_lang_invoke_AdapterMethodHandle::set_conversion(mh(), new_conversion); jrose@1145: jrose@1145: // Done! jrose@2639: java_lang_invoke_MethodHandle::set_vmentry(mh(), entry(ek_opt)); jrose@1145: jrose@1145: // There should be enough memory barriers on exit from native methods jrose@1145: // to ensure that the MH is fully initialized to all threads before jrose@1145: // Java code can publish it in global data structures. jrose@1145: } jrose@1145: jrose@1145: // jrose@2639: // Here are the native methods on sun.invoke.MethodHandleImpl. jrose@1145: // They are the private interface between this JVM and the HotSpot-specific jrose@1145: // Java code that implements JSR 292 method handles. jrose@1145: // jrose@1145: // Note: We use a JVM_ENTRY macro to define each of these, for this is the way jrose@1145: // that intrinsic (non-JNI) native methods are defined in HotSpot. jrose@1145: // jrose@1145: jrose@1145: // direct method handles for invokestatic or invokespecial jrose@1145: // void init(DirectMethodHandle self, MemberName ref, boolean doDispatch, Class caller); jrose@2639: JVM_ENTRY(void, MHN_init_DMH(JNIEnv *env, jobject igcls, jobject mh_jh, jrose@1145: jobject target_jh, jboolean do_dispatch, jobject caller_jh)) { jrose@1145: ResourceMark rm; // for error messages jrose@1145: jrose@1145: // This is the guy we are initializing: jrose@1145: if (mh_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); jrose@1145: jrose@1145: // Early returns out of this method leave the DMH in an unfinished state. jrose@2639: assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); jrose@1145: jrose@1145: // which method are we really talking about? jrose@1145: if (target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: oop target_oop = JNIHandles::resolve_non_null(target_jh); jrose@2639: if (java_lang_invoke_MemberName::is_instance(target_oop) && jrose@2639: java_lang_invoke_MemberName::vmindex(target_oop) == VM_INDEX_UNINITIALIZED) { jrose@1145: Handle mname(THREAD, target_oop); jrose@1145: MethodHandles::resolve_MemberName(mname, CHECK); jrose@1145: target_oop = mname(); // in case of GC jrose@1145: } jrose@1145: jrose@1145: int decode_flags = 0; klassOop receiver_limit = NULL; jrose@1145: methodHandle m(THREAD, jrose@1145: MethodHandles::decode_method(target_oop, jrose@1145: receiver_limit, decode_flags)); jrose@1145: if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "no such method"); } jrose@1145: jrose@1145: // The trusted Java code that calls this method should already have performed jrose@1145: // access checks on behalf of the given caller. But, we can verify this. jrose@1145: if (VerifyMethodHandles && caller_jh != NULL) { jrose@1145: KlassHandle caller(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh))); jrose@1145: // If this were a bytecode, the first access check would be against jrose@1145: // the "reference class" mentioned in the CONSTANT_Methodref. jrose@1862: // We don't know at this point which class that was, and if we jrose@1862: // check against m.method_holder we might get the wrong answer. jrose@1862: // So we just make sure to handle this check when the resolution jrose@1862: // happens, when we call resolve_MemberName. jrose@1862: // jrose@1862: // (A public class can inherit public members from private supers, jrose@1862: // and it would be wrong to check access against the private super jrose@1862: // if the original symbolic reference was against the public class.) jrose@1862: // jrose@1145: // If there were a bytecode, the next step would be to lookup the method jrose@1145: // in the reference class, then then check the method's access bits. jrose@1145: // Emulate LinkResolver::check_method_accessability. jrose@1145: klassOop resolved_klass = m->method_holder(); jrose@1145: if (!Reflection::verify_field_access(caller->as_klassOop(), jrose@1862: resolved_klass, resolved_klass, jrose@1145: m->access_flags(), jrose@1145: true)) { jrose@1145: // %%% following cutout belongs in Reflection::verify_field_access? jrose@1145: bool same_pm = Reflection::is_same_package_member(caller->as_klassOop(), jrose@1862: resolved_klass, THREAD); jrose@1145: if (!same_pm) { jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), m->name_and_sig_as_C_string()); jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: MethodHandles::init_DirectMethodHandle(mh, m, (do_dispatch != JNI_FALSE), CHECK); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // bound method handles jrose@2639: JVM_ENTRY(void, MHN_init_BMH(JNIEnv *env, jobject igcls, jobject mh_jh, jrose@1145: jobject target_jh, int argnum)) { jrose@1145: ResourceMark rm; // for error messages jrose@1145: jrose@1145: // This is the guy we are initializing: jrose@1145: if (mh_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); jrose@1145: jrose@1145: // Early returns out of this method leave the BMH in an unfinished state. jrose@2639: assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); jrose@1145: jrose@1145: if (target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); jrose@1145: jrose@2639: if (!java_lang_invoke_MethodHandle::is_instance(target())) { jrose@1145: // Target object is a reflective method. (%%% Do we need this alternate path?) jrose@1145: Untested("init_BMH of non-MH"); jrose@1145: if (argnum != 0) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: int decode_flags = 0; klassOop receiver_limit_oop = NULL; jrose@1145: methodHandle m(THREAD, jrose@1145: MethodHandles::decode_method(target(), jrose@1145: receiver_limit_oop, jrose@1145: decode_flags)); jrose@1145: KlassHandle receiver_limit(THREAD, receiver_limit_oop); jrose@1145: MethodHandles::init_BoundMethodHandle_with_receiver(mh, m, jrose@1145: receiver_limit, jrose@1145: decode_flags, jrose@1145: CHECK); jrose@1145: return; jrose@1145: } jrose@1145: jrose@1145: // Build a BMH on top of a DMH or another BMH: jrose@1145: MethodHandles::init_BoundMethodHandle(mh, target, argnum, CHECK); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // adapter method handles jrose@2639: JVM_ENTRY(void, MHN_init_AMH(JNIEnv *env, jobject igcls, jobject mh_jh, jrose@1145: jobject target_jh, int argnum)) { jrose@1145: // This is the guy we are initializing: jrose@1145: if (mh_jh == NULL || target_jh == NULL) { jrose@1145: THROW(vmSymbols::java_lang_InternalError()); jrose@1145: } jrose@1145: Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); jrose@1145: Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); jrose@1145: jrose@1145: // Early returns out of this method leave the AMH in an unfinished state. jrose@2639: assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); jrose@1145: jrose@1145: MethodHandles::init_AdapterMethodHandle(mh, target, argnum, CHECK); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // method type forms jrose@2639: JVM_ENTRY(void, MHN_init_MT(JNIEnv *env, jobject igcls, jobject erased_jh)) { jrose@1145: if (erased_jh == NULL) return; jrose@1145: if (TraceMethodHandles) { jrose@1145: tty->print("creating MethodType form "); jrose@1145: if (WizardMode || Verbose) { // Warning: this calls Java code on the MH! jrose@1145: // call Object.toString() coleenp@2497: Symbol* name = vmSymbols::toString_name(); coleenp@2497: Symbol* sig = vmSymbols::void_string_signature(); jrose@1145: JavaCallArguments args(Handle(THREAD, JNIHandles::resolve_non_null(erased_jh))); jrose@1145: JavaValue result(T_OBJECT); never@1577: JavaCalls::call_virtual(&result, SystemDictionary::Object_klass(), name, sig, jrose@1145: &args, CHECK); jrose@1145: Handle str(THREAD, (oop)result.get_jobject()); jrose@1145: java_lang_String::print(str, tty); jrose@1145: } jrose@1145: tty->cr(); jrose@1145: } jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // debugging and reflection jrose@2639: JVM_ENTRY(jobject, MHN_getTarget(JNIEnv *env, jobject igcls, jobject mh_jh, jint format)) { jrose@1145: Handle mh(THREAD, JNIHandles::resolve(mh_jh)); jrose@2639: if (!java_lang_invoke_MethodHandle::is_instance(mh())) { jrose@1145: THROW_NULL(vmSymbols::java_lang_IllegalArgumentException()); jrose@1145: } jrose@1145: oop target = MethodHandles::encode_target(mh, format, CHECK_NULL); jrose@1145: return JNIHandles::make_local(THREAD, target); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@2639: JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) { jrose@1145: switch (which) { jrose@1145: case MethodHandles::GC_JVM_PUSH_LIMIT: jrose@1145: guarantee(MethodHandlePushLimit >= 2 && MethodHandlePushLimit <= 0xFF, jrose@1145: "MethodHandlePushLimit parameter must be in valid range"); jrose@1145: return MethodHandlePushLimit; jrose@1474: case MethodHandles::GC_JVM_STACK_MOVE_UNIT: jrose@1145: // return number of words per slot, signed according to stack direction jrose@1145: return MethodHandles::stack_move_unit(); jrose@1862: case MethodHandles::GC_CONV_OP_IMPLEMENTED_MASK: jrose@1862: return MethodHandles::adapter_conversion_ops_supported_mask(); jrose@1145: } jrose@1145: return 0; jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: #ifndef PRODUCT jrose@1145: #define EACH_NAMED_CON(template) \ jrose@1145: template(MethodHandles,GC_JVM_PUSH_LIMIT) \ jrose@1474: template(MethodHandles,GC_JVM_STACK_MOVE_UNIT) \ jrose@1145: template(MethodHandles,ETF_HANDLE_OR_METHOD_NAME) \ jrose@1145: template(MethodHandles,ETF_DIRECT_HANDLE) \ jrose@1145: template(MethodHandles,ETF_METHOD_NAME) \ jrose@1145: template(MethodHandles,ETF_REFLECT_METHOD) \ jrose@2639: template(java_lang_invoke_MemberName,MN_IS_METHOD) \ jrose@2639: template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \ jrose@2639: template(java_lang_invoke_MemberName,MN_IS_FIELD) \ jrose@2639: template(java_lang_invoke_MemberName,MN_IS_TYPE) \ jrose@2639: template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \ jrose@2639: template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \ jrose@2639: template(java_lang_invoke_MemberName,VM_INDEX_UNINITIALIZED) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_RETYPE_ONLY) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_RETYPE_RAW) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_CHECK_CAST) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_PRIM_TO_PRIM) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_REF_TO_PRIM) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_PRIM_TO_REF) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_SWAP_ARGS) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_ROT_ARGS) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_DUP_ARGS) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_DROP_ARGS) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_COLLECT_ARGS) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_SPREAD_ARGS) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_FLYBY) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,OP_RICOCHET) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_OP_LIMIT) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_OP_MASK) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_VMINFO_MASK) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_VMINFO_SHIFT) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_OP_SHIFT) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_DEST_TYPE_SHIFT) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_SRC_TYPE_SHIFT) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_STACK_MOVE_SHIFT) \ jrose@2639: template(java_lang_invoke_AdapterMethodHandle,CONV_STACK_MOVE_MASK) \ jrose@1145: /*end*/ jrose@1145: jrose@1145: #define ONE_PLUS(scope,value) 1+ jrose@1145: static const int con_value_count = EACH_NAMED_CON(ONE_PLUS) 0; jrose@1145: #define VALUE_COMMA(scope,value) scope::value, jrose@1145: static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA) 0 }; jrose@1145: #define STRING_NULL(scope,value) #value "\0" jrose@1145: static const char con_names[] = { EACH_NAMED_CON(STRING_NULL) }; jrose@1145: jrose@1145: #undef ONE_PLUS jrose@1145: #undef VALUE_COMMA jrose@1145: #undef STRING_NULL jrose@1145: #undef EACH_NAMED_CON jrose@1145: #endif jrose@1145: jrose@2639: JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { jrose@1145: #ifndef PRODUCT jrose@1145: if (which >= 0 && which < con_value_count) { jrose@1145: int con = con_values[which]; jrose@1145: objArrayOop box = (objArrayOop) JNIHandles::resolve(box_jh); jrose@1145: if (box != NULL && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) { jrose@1145: const char* str = &con_names[0]; jrose@1145: for (int i = 0; i < which; i++) jrose@1145: str += strlen(str) + 1; // skip name and null jrose@1145: oop name = java_lang_String::create_oop_from_str(str, CHECK_0); jrose@1145: box->obj_at_put(0, name); jrose@1145: } jrose@1145: return con; jrose@1145: } jrose@1145: #endif jrose@1145: return 0; jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // void init(MemberName self, AccessibleObject ref) jrose@2639: JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) { jrose@1145: if (mname_jh == NULL || target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); jrose@1145: oop target_oop = JNIHandles::resolve_non_null(target_jh); jrose@1145: MethodHandles::init_MemberName(mname(), target_oop); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // void expand(MemberName self) jrose@2639: JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) { jrose@1145: if (mname_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); jrose@1145: MethodHandles::expand_MemberName(mname, 0, CHECK); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // void resolve(MemberName self, Class caller) jrose@2639: JVM_ENTRY(void, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { jrose@1145: if (mname_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } jrose@1145: Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); jrose@1862: jrose@1862: // The trusted Java code that calls this method should already have performed jrose@1862: // access checks on behalf of the given caller. But, we can verify this. jrose@1862: if (VerifyMethodHandles && caller_jh != NULL) { jrose@2639: klassOop reference_klass = java_lang_Class::as_klassOop(java_lang_invoke_MemberName::clazz(mname())); jrose@1862: if (reference_klass != NULL) { jrose@1862: // Emulate LinkResolver::check_klass_accessability. jrose@1862: klassOop caller = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh)); jrose@1862: if (!Reflection::verify_class_access(caller, jrose@1862: reference_klass, jrose@1862: true)) { jrose@1862: THROW_MSG(vmSymbols::java_lang_InternalError(), Klass::cast(reference_klass)->external_name()); jrose@1862: } jrose@1862: } jrose@1862: } jrose@1862: jrose@1145: MethodHandles::resolve_MemberName(mname, CHECK); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // static native int getMembers(Class defc, String matchName, String matchSig, jrose@1145: // int matchFlags, Class caller, int skip, MemberName[] results); jrose@2639: JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls, jrose@1145: jclass clazz_jh, jstring name_jh, jstring sig_jh, jrose@1145: int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) { jrose@1145: if (clazz_jh == NULL || results_jh == NULL) return -1; jrose@1145: klassOop k_oop = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz_jh)); jrose@1145: jrose@1145: objArrayOop results = (objArrayOop) JNIHandles::resolve(results_jh); jrose@1145: if (results == NULL || !results->is_objArray()) return -1; jrose@1145: coleenp@2497: TempNewSymbol name = NULL; coleenp@2497: TempNewSymbol sig = NULL; jrose@1145: if (name_jh != NULL) { jrose@1145: name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh)); jrose@1145: if (name == NULL) return 0; // a match is not possible jrose@1145: } jrose@1145: if (sig_jh != NULL) { jrose@1145: sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh)); jrose@1145: if (sig == NULL) return 0; // a match is not possible jrose@1145: } jrose@1145: jrose@1145: klassOop caller = NULL; jrose@1145: if (caller_jh != NULL) { jrose@1145: oop caller_oop = JNIHandles::resolve_non_null(caller_jh); jrose@1145: if (!java_lang_Class::is_instance(caller_oop)) return -1; jrose@1145: caller = java_lang_Class::as_klassOop(caller_oop); jrose@1145: } jrose@1145: jrose@1145: if (name != NULL && sig != NULL && results != NULL) { jrose@1145: // try a direct resolve jrose@1145: // %%% TO DO jrose@1145: } jrose@1145: jrose@1145: int res = MethodHandles::find_MemberNames(k_oop, name, sig, mflags, jrose@1145: caller, skip, results); jrose@1145: // TO DO: expand at least some of the MemberNames, to avoid massive callbacks jrose@1145: return res; jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1161: jrose@1145: /// JVM_RegisterMethodHandleMethods jrose@1145: jrose@1145: #define LANG "Ljava/lang/" jrose@2742: #define JLINV "Ljava/lang/invoke/" jrose@1145: jrose@1145: #define OBJ LANG"Object;" jrose@1145: #define CLS LANG"Class;" jrose@1145: #define STRG LANG"String;" jrose@2742: #define MT JLINV"MethodType;" jrose@2742: #define MH JLINV"MethodHandle;" jrose@2742: #define MEM JLINV"MemberName;" jrose@2742: #define AMH JLINV"AdapterMethodHandle;" jrose@2742: #define BMH JLINV"BoundMethodHandle;" jrose@2742: #define DMH JLINV"DirectMethodHandle;" jrose@1145: jrose@1145: #define CC (char*) /*cast a literal from (const char*)*/ jrose@1145: #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) jrose@1145: jrose@2639: // These are the native methods on sun.invoke.MethodHandleNatives. jrose@1145: static JNINativeMethod methods[] = { jrose@1145: // void init(MemberName self, AccessibleObject ref) jrose@2639: {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHN_init_AMH)}, jrose@2639: {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHN_init_BMH)}, jrose@2639: {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHN_init_DMH)}, jrose@2639: {CC"init", CC"("MT")V", FN_PTR(MHN_init_MT)}, jrose@2639: {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, jrose@2639: {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, jrose@2639: {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHN_resolve_Mem)}, jrose@2639: {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHN_getTarget)}, jrose@2639: {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, jrose@1145: // static native int getNamedCon(int which, Object[] name) jrose@2639: {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, jrose@1145: // static native int getMembers(Class defc, String matchName, String matchSig, jrose@1145: // int matchFlags, Class caller, int skip, MemberName[] results); jrose@2639: {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)} jrose@1145: }; jrose@1145: jrose@1145: // This one function is exported, used by NativeLookup. jrose@1145: jrose@1145: JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { jrose@1145: assert(MethodHandles::spot_check_entry_names(), "entry enum is OK"); jrose@1145: twisti@2698: if (!EnableInvokeDynamic) { twisti@2698: warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); jrose@1145: return; // bind nothing jrose@1145: } jrose@1145: jrose@1474: bool enable_MH = true; jrose@1474: jrose@2742: { jrose@1145: ThreadToNativeFromVM ttnfv(thread); jrose@1145: jrose@1145: int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod)); jrose@1145: if (env->ExceptionOccurred()) { jrose@2742: MethodHandles::set_enabled(false); jrose@2742: warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); jrose@2742: enable_MH = false; jrose@1145: env->ExceptionClear(); jrose@1145: } jrose@1145: } jrose@1161: jrose@1474: if (enable_MH) { jrose@2638: KlassHandle MHN_klass = SystemDictionaryHandles::MethodHandleNatives_klass(); jrose@2742: if (MHN_klass.not_null()) { coleenp@2497: TempNewSymbol raiseException_name = SymbolTable::new_symbol("raiseException", CHECK); coleenp@2497: TempNewSymbol raiseException_sig = SymbolTable::new_symbol("(ILjava/lang/Object;Ljava/lang/Object;)V", CHECK); jrose@2742: methodOop raiseException_method = instanceKlass::cast(MHN_klass->as_klassOop()) coleenp@2497: ->find_method(raiseException_name, raiseException_sig); jrose@1474: if (raiseException_method != NULL && raiseException_method->is_static()) { jrose@1474: MethodHandles::set_raise_exception_method(raiseException_method); jrose@2742: } else { jrose@2742: warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); jrose@2742: enable_MH = false; jrose@1474: } jrose@2742: } else { twisti@2436: enable_MH = false; jrose@1474: } jrose@1474: } jrose@1474: jrose@1474: if (enable_MH) { twisti@2436: MethodHandles::generate_adapters(); jrose@1474: MethodHandles::set_enabled(true); jrose@1474: } jrose@1145: } jrose@1145: JVM_END