1 /* 2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "memory/allocation.inline.hpp" 29 #include "memory/oopFactory.hpp" 30 #include "prims/methodHandles.hpp" 31 #include "runtime/javaCalls.hpp" 32 #include "runtime/reflection.hpp" 33 #include "runtime/signature.hpp" 34 #include "runtime/stubRoutines.hpp" 35 36 /* 37 * JSR 292 reference implementation: method handles 38 */ 39 40 bool MethodHandles::_enabled = false; // set true after successful native linkage 41 42 MethodHandleEntry* MethodHandles::_entries[MethodHandles::_EK_LIMIT] = {NULL}; 43 const char* MethodHandles::_entry_names[_EK_LIMIT+1] = { 44 "raise_exception", 45 "invokestatic", // how a MH emulates invokestatic 46 "invokespecial", // ditto for the other invokes... 47 "invokevirtual", 48 "invokeinterface", 49 "bound_ref", // these are for BMH... 50 "bound_int", 51 "bound_long", 52 "bound_ref_direct", // (direct versions have a direct methodOop) 53 "bound_int_direct", 54 "bound_long_direct", 55 56 // starting at _adapter_mh_first: 57 "adapter_retype_only", // these are for AMH... 58 "adapter_retype_raw", 59 "adapter_check_cast", 60 "adapter_prim_to_prim", 61 "adapter_ref_to_prim", 62 "adapter_prim_to_ref", 63 "adapter_swap_args", 64 "adapter_rot_args", 65 "adapter_dup_args", 66 "adapter_drop_args", 67 "adapter_collect_args", 68 "adapter_spread_args", 69 "adapter_flyby", 70 "adapter_ricochet", 71 72 // optimized adapter types: 73 "adapter_swap_args/1", 74 "adapter_swap_args/2", 75 "adapter_rot_args/1,up", 76 "adapter_rot_args/1,down", 77 "adapter_rot_args/2,up", 78 "adapter_rot_args/2,down", 79 "adapter_prim_to_prim/i2i", 80 "adapter_prim_to_prim/l2i", 81 "adapter_prim_to_prim/d2f", 82 "adapter_prim_to_prim/i2l", 83 "adapter_prim_to_prim/f2d", 84 "adapter_ref_to_prim/unboxi", 85 "adapter_ref_to_prim/unboxl", 86 "adapter_spread_args/0", 87 "adapter_spread_args/1", 88 "adapter_spread_args/more", 89 90 NULL 91 }; 92 93 // Adapters. 94 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; 95 int MethodHandles::_adapter_code_size = StubRoutines::method_handles_adapters_code_size; 96 97 jobject MethodHandles::_raise_exception_method; 98 99 #ifdef ASSERT 100 bool MethodHandles::spot_check_entry_names() { 101 assert(!strcmp(entry_name(_invokestatic_mh), "invokestatic"), ""); 102 assert(!strcmp(entry_name(_bound_ref_mh), "bound_ref"), ""); 103 assert(!strcmp(entry_name(_adapter_retype_only), "adapter_retype_only"), ""); 104 assert(!strcmp(entry_name(_adapter_ricochet), "adapter_ricochet"), ""); 105 assert(!strcmp(entry_name(_adapter_opt_unboxi), "adapter_ref_to_prim/unboxi"), ""); 106 return true; 107 } 108 #endif 109 110 111 //------------------------------------------------------------------------------ 112 // MethodHandles::generate_adapters 113 // 114 void MethodHandles::generate_adapters() { 115 if (!EnableInvokeDynamic || SystemDictionary::MethodHandle_klass() == NULL) return; 116 117 assert(_adapter_code == NULL, "generate only once"); 118 119 ResourceMark rm; 120 TraceTime timer("MethodHandles adapters generation", TraceStartupTime); 121 _adapter_code = MethodHandlesAdapterBlob::create(_adapter_code_size); 122 if (_adapter_code == NULL) 123 vm_exit_out_of_memory(_adapter_code_size, "CodeCache: no room for MethodHandles adapters"); 124 CodeBuffer code(_adapter_code); 125 MethodHandlesAdapterGenerator g(&code); 126 g.generate(); 127 } 128 129 130 //------------------------------------------------------------------------------ 131 // MethodHandlesAdapterGenerator::generate 132 // 133 void MethodHandlesAdapterGenerator::generate() { 134 // Generate generic method handle adapters. 135 for (MethodHandles::EntryKind ek = MethodHandles::_EK_FIRST; 136 ek < MethodHandles::_EK_LIMIT; 137 ek = MethodHandles::EntryKind(1 + (int)ek)) { 138 StubCodeMark mark(this, "MethodHandle", MethodHandles::entry_name(ek)); 139 MethodHandles::generate_method_handle_stub(_masm, ek); 140 } 141 } 142 143 144 void MethodHandles::set_enabled(bool z) { 145 if (_enabled != z) { 146 guarantee(z && EnableInvokeDynamic, "can only enable once, and only if -XX:+EnableInvokeDynamic"); 147 _enabled = z; 148 } 149 } 150 151 // Note: A method which does not have a TRAPS argument cannot block in the GC 152 // or throw exceptions. Such methods are used in this file to do something quick 153 // and local, like parse a data structure. For speed, such methods work on plain 154 // oops, not handles. Trapping methods uniformly operate on handles. 155 156 methodOop MethodHandles::decode_vmtarget(oop vmtarget, int vmindex, oop mtype, 157 klassOop& receiver_limit_result, int& decode_flags_result) { 158 if (vmtarget == NULL) return NULL; 159 assert(methodOopDesc::nonvirtual_vtable_index < 0, "encoding"); 160 if (vmindex < 0) { 161 // this DMH performs no dispatch; it is directly bound to a methodOop 162 // A MemberName may either be directly bound to a methodOop, 163 // or it may use the klass/index form; both forms mean the same thing. 164 methodOop m = decode_methodOop(methodOop(vmtarget), decode_flags_result); 165 if ((decode_flags_result & _dmf_has_receiver) != 0 166 && java_lang_invoke_MethodType::is_instance(mtype)) { 167 // Extract receiver type restriction from mtype.ptypes[0]. 168 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(mtype); 169 oop ptype0 = (ptypes == NULL || ptypes->length() < 1) ? oop(NULL) : ptypes->obj_at(0); 170 if (java_lang_Class::is_instance(ptype0)) 171 receiver_limit_result = java_lang_Class::as_klassOop(ptype0); 172 } 173 if (vmindex == methodOopDesc::nonvirtual_vtable_index) { 174 // this DMH can be an "invokespecial" version 175 decode_flags_result &= ~_dmf_does_dispatch; 176 } else { 177 assert(vmindex == methodOopDesc::invalid_vtable_index, "random vmindex?"); 178 } 179 return m; 180 } else { 181 assert(vmtarget->is_klass(), "must be class or interface"); 182 decode_flags_result |= MethodHandles::_dmf_does_dispatch; 183 decode_flags_result |= MethodHandles::_dmf_has_receiver; 184 receiver_limit_result = (klassOop)vmtarget; 185 Klass* tk = Klass::cast((klassOop)vmtarget); 186 if (tk->is_interface()) { 187 // an itable linkage is <interface, itable index> 188 decode_flags_result |= MethodHandles::_dmf_from_interface; 189 return klassItable::method_for_itable_index((klassOop)vmtarget, vmindex); 190 } else { 191 if (!tk->oop_is_instance()) 192 tk = instanceKlass::cast(SystemDictionary::Object_klass()); 193 return ((instanceKlass*)tk)->method_at_vtable(vmindex); 194 } 195 } 196 } 197 198 // MemberName and DirectMethodHandle have the same linkage to the JVM internals. 199 // (MemberName is the non-operational name used for queries and setup.) 200 201 methodOop MethodHandles::decode_DirectMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { 202 oop vmtarget = java_lang_invoke_DirectMethodHandle::vmtarget(mh); 203 int vmindex = java_lang_invoke_DirectMethodHandle::vmindex(mh); 204 oop mtype = java_lang_invoke_DirectMethodHandle::type(mh); 205 return decode_vmtarget(vmtarget, vmindex, mtype, receiver_limit_result, decode_flags_result); 206 } 207 208 methodOop MethodHandles::decode_BoundMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { 209 assert(java_lang_invoke_BoundMethodHandle::is_instance(mh), ""); 210 assert(mh->klass() != SystemDictionary::AdapterMethodHandle_klass(), ""); 211 for (oop bmh = mh;;) { 212 // Bound MHs can be stacked to bind several arguments. 213 oop target = java_lang_invoke_MethodHandle::vmtarget(bmh); 214 if (target == NULL) return NULL; 215 decode_flags_result |= MethodHandles::_dmf_binds_argument; 216 klassOop tk = target->klass(); 217 if (tk == SystemDictionary::BoundMethodHandle_klass()) { 218 bmh = target; 219 continue; 220 } else { 221 if (java_lang_invoke_MethodHandle::is_subclass(tk)) { 222 //assert(tk == SystemDictionary::DirectMethodHandle_klass(), "end of BMH chain must be DMH"); 223 return decode_MethodHandle(target, receiver_limit_result, decode_flags_result); 224 } else { 225 // Optimized case: binding a receiver to a non-dispatched DMH 226 // short-circuits directly to the methodOop. 227 // (It might be another argument besides a receiver also.) 228 assert(target->is_method(), "must be a simple method"); 229 decode_flags_result |= MethodHandles::_dmf_binds_method; 230 methodOop m = (methodOop) target; 231 if (!m->is_static()) 232 decode_flags_result |= MethodHandles::_dmf_has_receiver; 233 return m; 234 } 235 } 236 } 237 } 238 239 methodOop MethodHandles::decode_AdapterMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { 240 assert(mh->klass() == SystemDictionary::AdapterMethodHandle_klass(), ""); 241 for (oop amh = mh;;) { 242 // Adapter MHs can be stacked to convert several arguments. 243 int conv_op = adapter_conversion_op(java_lang_invoke_AdapterMethodHandle::conversion(amh)); 244 decode_flags_result |= (_dmf_adapter_lsb << conv_op) & _DMF_ADAPTER_MASK; 245 oop target = java_lang_invoke_MethodHandle::vmtarget(amh); 246 if (target == NULL) return NULL; 247 klassOop tk = target->klass(); 248 if (tk == SystemDictionary::AdapterMethodHandle_klass()) { 249 amh = target; 250 continue; 251 } else { 252 // must be a BMH (which will bind some more arguments) or a DMH (for the final call) 253 return MethodHandles::decode_MethodHandle(target, receiver_limit_result, decode_flags_result); 254 } 255 } 256 } 257 258 methodOop MethodHandles::decode_MethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { 259 if (mh == NULL) return NULL; 260 klassOop mhk = mh->klass(); 261 assert(java_lang_invoke_MethodHandle::is_subclass(mhk), "must be a MethodHandle"); 262 if (mhk == SystemDictionary::DirectMethodHandle_klass()) { 263 return decode_DirectMethodHandle(mh, receiver_limit_result, decode_flags_result); 264 } else if (mhk == SystemDictionary::BoundMethodHandle_klass()) { 265 return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result); 266 } else if (mhk == SystemDictionary::AdapterMethodHandle_klass()) { 267 return decode_AdapterMethodHandle(mh, receiver_limit_result, decode_flags_result); 268 } else if (java_lang_invoke_BoundMethodHandle::is_subclass(mhk)) { 269 // could be a JavaMethodHandle (but not an adapter MH) 270 return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result); 271 } else { 272 assert(false, "cannot parse this MH"); 273 return NULL; // random MH? 274 } 275 } 276 277 methodOop MethodHandles::decode_methodOop(methodOop m, int& decode_flags_result) { 278 assert(m->is_method(), ""); 279 if (m->is_static()) { 280 // check that signature begins '(L' or '([' (not '(I', '()', etc.) 281 Symbol* sig = m->signature(); 282 BasicType recv_bt = char2type(sig->byte_at(1)); 283 // Note: recv_bt might be T_ILLEGAL if byte_at(2) is ')' 284 assert(sig->byte_at(0) == '(', "must be method sig"); 285 // if (recv_bt == T_OBJECT || recv_bt == T_ARRAY) 286 // decode_flags_result |= _dmf_has_receiver; 287 } else { 288 // non-static method 289 decode_flags_result |= _dmf_has_receiver; 290 if (!m->can_be_statically_bound() && !m->is_initializer()) { 291 decode_flags_result |= _dmf_does_dispatch; 292 if (Klass::cast(m->method_holder())->is_interface()) 293 decode_flags_result |= _dmf_from_interface; 294 } 295 } 296 return m; 297 } 298 299 300 // A trusted party is handing us a cookie to determine a method. 301 // Let's boil it down to the method oop they really want. 302 methodOop MethodHandles::decode_method(oop x, klassOop& receiver_limit_result, int& decode_flags_result) { 303 decode_flags_result = 0; 304 receiver_limit_result = NULL; 305 klassOop xk = x->klass(); 306 if (xk == Universe::methodKlassObj()) { 307 return decode_methodOop((methodOop) x, decode_flags_result); 308 } else if (xk == SystemDictionary::MemberName_klass()) { 309 // Note: This only works if the MemberName has already been resolved. 310 return decode_MemberName(x, receiver_limit_result, decode_flags_result); 311 } else if (java_lang_invoke_MethodHandle::is_subclass(xk)) { 312 return decode_MethodHandle(x, receiver_limit_result, decode_flags_result); 313 } else if (xk == SystemDictionary::reflect_Method_klass()) { 314 oop clazz = java_lang_reflect_Method::clazz(x); 315 int slot = java_lang_reflect_Method::slot(x); 316 klassOop k = java_lang_Class::as_klassOop(clazz); 317 if (k != NULL && Klass::cast(k)->oop_is_instance()) 318 return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot), 319 decode_flags_result); 320 } else if (xk == SystemDictionary::reflect_Constructor_klass()) { 321 oop clazz = java_lang_reflect_Constructor::clazz(x); 322 int slot = java_lang_reflect_Constructor::slot(x); 323 klassOop k = java_lang_Class::as_klassOop(clazz); 324 if (k != NULL && Klass::cast(k)->oop_is_instance()) 325 return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot), 326 decode_flags_result); 327 } else { 328 // unrecognized object 329 assert(!x->is_method(), "already checked"); 330 assert(!java_lang_invoke_MemberName::is_instance(x), "already checked"); 331 } 332 return NULL; 333 } 334 335 336 int MethodHandles::decode_MethodHandle_stack_pushes(oop mh) { 337 if (mh->klass() == SystemDictionary::DirectMethodHandle_klass()) 338 return 0; // no push/pop 339 int this_vmslots = java_lang_invoke_MethodHandle::vmslots(mh); 340 int last_vmslots = 0; 341 oop last_mh = mh; 342 for (;;) { 343 oop target = java_lang_invoke_MethodHandle::vmtarget(last_mh); 344 if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) { 345 last_vmslots = java_lang_invoke_MethodHandle::vmslots(target); 346 break; 347 } else if (!java_lang_invoke_MethodHandle::is_instance(target)) { 348 // might be klass or method 349 assert(target->is_method(), "must get here with a direct ref to method"); 350 last_vmslots = methodOop(target)->size_of_parameters(); 351 break; 352 } 353 last_mh = target; 354 } 355 // If I am called with fewer VM slots than my ultimate callee, 356 // it must be that I push the additionally needed slots. 357 // Likewise if am called with more VM slots, I will pop them. 358 return (last_vmslots - this_vmslots); 359 } 360 361 362 // MemberName support 363 364 // import java_lang_invoke_MemberName.* 365 enum { 366 IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD, 367 IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR, 368 IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD, 369 IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE, 370 SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES, 371 SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES, 372 ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE, 373 VM_INDEX_UNINITIALIZED = java_lang_invoke_MemberName::VM_INDEX_UNINITIALIZED 374 }; 375 376 Handle MethodHandles::new_MemberName(TRAPS) { 377 Handle empty; 378 instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass()); 379 if (!k->is_initialized()) k->initialize(CHECK_(empty)); 380 return Handle(THREAD, k->allocate_instance(THREAD)); 381 } 382 383 void MethodHandles::init_MemberName(oop mname_oop, oop target_oop) { 384 if (target_oop->klass() == SystemDictionary::reflect_Field_klass()) { 385 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() 386 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() 387 int mods = java_lang_reflect_Field::modifiers(target_oop); 388 klassOop k = java_lang_Class::as_klassOop(clazz); 389 int offset = instanceKlass::cast(k)->offset_from_fields(slot); 390 init_MemberName(mname_oop, k, accessFlags_from(mods), offset); 391 } else { 392 int decode_flags = 0; klassOop receiver_limit = NULL; 393 methodOop m = MethodHandles::decode_method(target_oop, 394 receiver_limit, decode_flags); 395 bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); 396 init_MemberName(mname_oop, m, do_dispatch); 397 } 398 } 399 400 void MethodHandles::init_MemberName(oop mname_oop, methodOop m, bool do_dispatch) { 401 int flags = ((m->is_initializer() ? IS_CONSTRUCTOR : IS_METHOD) 402 | (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS )); 403 oop vmtarget = m; 404 int vmindex = methodOopDesc::invalid_vtable_index; // implies no info yet 405 if (!do_dispatch || (flags & IS_CONSTRUCTOR) || m->can_be_statically_bound()) 406 vmindex = methodOopDesc::nonvirtual_vtable_index; // implies never any dispatch 407 assert(vmindex != VM_INDEX_UNINITIALIZED, "Java sentinel value"); 408 java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); 409 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); 410 java_lang_invoke_MemberName::set_flags(mname_oop, flags); 411 java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(m->method_holder())->java_mirror()); 412 } 413 414 void MethodHandles::init_MemberName(oop mname_oop, klassOop field_holder, AccessFlags mods, int offset) { 415 int flags = (IS_FIELD | (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS )); 416 oop vmtarget = field_holder; 417 int vmindex = offset; // determines the field uniquely when combined with static bit 418 assert(vmindex != VM_INDEX_UNINITIALIZED, "bad alias on vmindex"); 419 java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); 420 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); 421 java_lang_invoke_MemberName::set_flags(mname_oop, flags); 422 java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(field_holder)->java_mirror()); 423 } 424 425 426 methodOop MethodHandles::decode_MemberName(oop mname, klassOop& receiver_limit_result, int& decode_flags_result) { 427 int flags = java_lang_invoke_MemberName::flags(mname); 428 if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) == 0) return NULL; // not invocable 429 oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname); 430 int vmindex = java_lang_invoke_MemberName::vmindex(mname); 431 if (vmindex == VM_INDEX_UNINITIALIZED) return NULL; // not resolved 432 methodOop m = decode_vmtarget(vmtarget, vmindex, NULL, receiver_limit_result, decode_flags_result); 433 oop clazz = java_lang_invoke_MemberName::clazz(mname); 434 if (clazz != NULL && java_lang_Class::is_instance(clazz)) { 435 klassOop klass = java_lang_Class::as_klassOop(clazz); 436 if (klass != NULL) receiver_limit_result = klass; 437 } 438 return m; 439 } 440 441 // convert the external string or reflective type to an internal signature 442 Symbol* MethodHandles::convert_to_signature(oop type_str, 443 bool polymorphic, 444 TRAPS) { 445 if (java_lang_invoke_MethodType::is_instance(type_str)) { 446 return java_lang_invoke_MethodType::as_signature(type_str, polymorphic, CHECK_NULL); 447 } else if (java_lang_Class::is_instance(type_str)) { 448 return java_lang_Class::as_signature(type_str, false, CHECK_NULL); 449 } else if (java_lang_String::is_instance(type_str)) { 450 if (polymorphic) { 451 return java_lang_String::as_symbol(type_str, CHECK_NULL); 452 } else { 453 return java_lang_String::as_symbol_or_null(type_str); 454 } 455 } else { 456 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", NULL); 457 } 458 } 459 460 // An unresolved member name is a mere symbolic reference. 461 // Resolving it plants a vmtarget/vmindex in it, 462 // which refers dirctly to JVM internals. 463 void MethodHandles::resolve_MemberName(Handle mname, TRAPS) { 464 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 465 #ifdef ASSERT 466 // If this assert throws, renegotiate the sentinel value used by the Java code, 467 // so that it is distinct from any valid vtable index value, and any special 468 // values defined in methodOopDesc::VtableIndexFlag. 469 // The point of the slop is to give the Java code and the JVM some room 470 // to independently specify sentinel values. 471 const int sentinel_slop = 10; 472 const int sentinel_limit = methodOopDesc::highest_unused_vtable_index_value - sentinel_slop; 473 assert(VM_INDEX_UNINITIALIZED < sentinel_limit, "Java sentinel != JVM sentinels"); 474 #endif 475 if (java_lang_invoke_MemberName::vmindex(mname()) != VM_INDEX_UNINITIALIZED) 476 return; // already resolved 477 oop defc_oop = java_lang_invoke_MemberName::clazz(mname()); 478 oop name_str = java_lang_invoke_MemberName::name(mname()); 479 oop type_str = java_lang_invoke_MemberName::type(mname()); 480 int flags = java_lang_invoke_MemberName::flags(mname()); 481 482 if (defc_oop == NULL || name_str == NULL || type_str == NULL) { 483 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve"); 484 } 485 klassOop defc_klassOop = java_lang_Class::as_klassOop(defc_oop); 486 defc_oop = NULL; // safety 487 if (defc_klassOop == NULL) return; // a primitive; no resolution possible 488 if (!Klass::cast(defc_klassOop)->oop_is_instance()) { 489 if (!Klass::cast(defc_klassOop)->oop_is_array()) return; 490 defc_klassOop = SystemDictionary::Object_klass(); 491 } 492 instanceKlassHandle defc(THREAD, defc_klassOop); 493 defc_klassOop = NULL; // safety 494 if (defc.is_null()) { 495 THROW_MSG(vmSymbols::java_lang_InternalError(), "primitive class"); 496 } 497 defc->link_class(CHECK); 498 499 // convert the external string name to an internal symbol 500 TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str); 501 if (name == NULL) return; // no such name 502 name_str = NULL; // safety 503 504 Handle polymorphic_method_type; 505 bool polymorphic_signature = false; 506 if ((flags & ALL_KINDS) == IS_METHOD && 507 (defc() == SystemDictionary::MethodHandle_klass() && 508 methodOopDesc::is_method_handle_invoke_name(name))) 509 polymorphic_signature = true; 510 511 // convert the external string or reflective type to an internal signature 512 TempNewSymbol type = convert_to_signature(type_str, polymorphic_signature, CHECK); 513 if (java_lang_invoke_MethodType::is_instance(type_str) && polymorphic_signature) { 514 polymorphic_method_type = Handle(THREAD, type_str); //preserve exactly 515 } 516 517 if (type == NULL) return; // no such signature exists in the VM 518 type_str = NULL; // safety 519 520 // Time to do the lookup. 521 switch (flags & ALL_KINDS) { 522 case IS_METHOD: 523 { 524 CallInfo result; 525 { 526 EXCEPTION_MARK; 527 if ((flags & JVM_ACC_STATIC) != 0) { 528 LinkResolver::resolve_static_call(result, 529 defc, name, type, KlassHandle(), false, false, THREAD); 530 } else if (defc->is_interface()) { 531 LinkResolver::resolve_interface_call(result, Handle(), defc, 532 defc, name, type, KlassHandle(), false, false, THREAD); 533 } else { 534 LinkResolver::resolve_virtual_call(result, Handle(), defc, 535 defc, name, type, KlassHandle(), false, false, THREAD); 536 } 537 if (HAS_PENDING_EXCEPTION) { 538 CLEAR_PENDING_EXCEPTION; 539 break; // go to second chance 540 } 541 } 542 methodHandle m = result.resolved_method(); 543 oop vmtarget = NULL; 544 int vmindex = methodOopDesc::nonvirtual_vtable_index; 545 if (defc->is_interface()) { 546 vmindex = klassItable::compute_itable_index(m()); 547 assert(vmindex >= 0, ""); 548 } else if (result.has_vtable_index()) { 549 vmindex = result.vtable_index(); 550 assert(vmindex >= 0, ""); 551 } 552 assert(vmindex != VM_INDEX_UNINITIALIZED, ""); 553 if (vmindex < 0) { 554 assert(result.is_statically_bound(), ""); 555 vmtarget = m(); 556 } else { 557 vmtarget = result.resolved_klass()->as_klassOop(); 558 } 559 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); 560 java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); 561 java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); 562 java_lang_invoke_MemberName::set_modifiers(mname(), mods); 563 DEBUG_ONLY(int junk; klassOop junk2); 564 assert(decode_MemberName(mname(), junk2, junk) == result.resolved_method()(), 565 "properly stored for later decoding"); 566 return; 567 } 568 case IS_CONSTRUCTOR: 569 { 570 CallInfo result; 571 { 572 EXCEPTION_MARK; 573 if (name == vmSymbols::object_initializer_name()) { 574 LinkResolver::resolve_special_call(result, 575 defc, name, type, KlassHandle(), false, THREAD); 576 } else { 577 break; // will throw after end of switch 578 } 579 if (HAS_PENDING_EXCEPTION) { 580 CLEAR_PENDING_EXCEPTION; 581 return; 582 } 583 } 584 assert(result.is_statically_bound(), ""); 585 methodHandle m = result.resolved_method(); 586 oop vmtarget = m(); 587 int vmindex = methodOopDesc::nonvirtual_vtable_index; 588 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); 589 java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); 590 java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); 591 java_lang_invoke_MemberName::set_modifiers(mname(), mods); 592 DEBUG_ONLY(int junk; klassOop junk2); 593 assert(decode_MemberName(mname(), junk2, junk) == result.resolved_method()(), 594 "properly stored for later decoding"); 595 return; 596 } 597 case IS_FIELD: 598 { 599 // This is taken from LinkResolver::resolve_field, sans access checks. 600 fieldDescriptor fd; // find_field initializes fd if found 601 KlassHandle sel_klass(THREAD, instanceKlass::cast(defc())->find_field(name, type, &fd)); 602 // check if field exists; i.e., if a klass containing the field def has been selected 603 if (sel_klass.is_null()) return; 604 oop vmtarget = sel_klass->as_klassOop(); 605 int vmindex = fd.offset(); 606 int mods = (fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS); 607 if (vmindex == VM_INDEX_UNINITIALIZED) break; // should not happen 608 java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); 609 java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); 610 java_lang_invoke_MemberName::set_modifiers(mname(), mods); 611 return; 612 } 613 default: 614 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); 615 } 616 617 // Second chance. 618 if (polymorphic_method_type.not_null()) { 619 // Look on a non-null class loader. 620 Handle cur_class_loader; 621 const int nptypes = java_lang_invoke_MethodType::ptype_count(polymorphic_method_type()); 622 for (int i = 0; i <= nptypes; i++) { 623 oop type_mirror; 624 if (i < nptypes) type_mirror = java_lang_invoke_MethodType::ptype(polymorphic_method_type(), i); 625 else type_mirror = java_lang_invoke_MethodType::rtype(polymorphic_method_type()); 626 klassOop example_type = java_lang_Class::as_klassOop(type_mirror); 627 if (example_type == NULL) continue; 628 oop class_loader = Klass::cast(example_type)->class_loader(); 629 if (class_loader == NULL || class_loader == cur_class_loader()) continue; 630 cur_class_loader = Handle(THREAD, class_loader); 631 methodOop m = SystemDictionary::find_method_handle_invoke(name, 632 type, 633 KlassHandle(THREAD, example_type), 634 THREAD); 635 if (HAS_PENDING_EXCEPTION) { 636 CLEAR_PENDING_EXCEPTION; 637 m = NULL; 638 // try again with a different class loader... 639 } 640 if (m != NULL) { 641 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); 642 java_lang_invoke_MemberName::set_vmtarget(mname(), m); 643 java_lang_invoke_MemberName::set_vmindex(mname(), m->vtable_index()); 644 java_lang_invoke_MemberName::set_modifiers(mname(), mods); 645 return; 646 } 647 } 648 } 649 } 650 651 // Conversely, a member name which is only initialized from JVM internals 652 // may have null defc, name, and type fields. 653 // Resolving it plants a vmtarget/vmindex in it, 654 // which refers directly to JVM internals. 655 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) { 656 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 657 oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 658 int vmindex = java_lang_invoke_MemberName::vmindex(mname()); 659 if (vmtarget == NULL || vmindex == VM_INDEX_UNINITIALIZED) { 660 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); 661 } 662 663 bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL); 664 bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL); 665 bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL); 666 int flags = java_lang_invoke_MemberName::flags(mname()); 667 668 if (suppress != 0) { 669 if (suppress & _suppress_defc) have_defc = true; 670 if (suppress & _suppress_name) have_name = true; 671 if (suppress & _suppress_type) have_type = true; 672 } 673 674 if (have_defc && have_name && have_type) return; // nothing needed 675 676 switch (flags & ALL_KINDS) { 677 case IS_METHOD: 678 case IS_CONSTRUCTOR: 679 { 680 klassOop receiver_limit = NULL; 681 int decode_flags = 0; 682 methodHandle m(THREAD, decode_vmtarget(vmtarget, vmindex, NULL, 683 receiver_limit, decode_flags)); 684 if (m.is_null()) break; 685 if (!have_defc) { 686 klassOop defc = m->method_holder(); 687 if (receiver_limit != NULL && receiver_limit != defc 688 && Klass::cast(receiver_limit)->is_subtype_of(defc)) 689 defc = receiver_limit; 690 java_lang_invoke_MemberName::set_clazz(mname(), Klass::cast(defc)->java_mirror()); 691 } 692 if (!have_name) { 693 //not java_lang_String::create_from_symbol; let's intern member names 694 Handle name = StringTable::intern(m->name(), CHECK); 695 java_lang_invoke_MemberName::set_name(mname(), name()); 696 } 697 if (!have_type) { 698 Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK); 699 java_lang_invoke_MemberName::set_type(mname(), type()); 700 } 701 return; 702 } 703 case IS_FIELD: 704 { 705 // This is taken from LinkResolver::resolve_field, sans access checks. 706 if (!vmtarget->is_klass()) break; 707 if (!Klass::cast((klassOop) vmtarget)->oop_is_instance()) break; 708 instanceKlassHandle defc(THREAD, (klassOop) vmtarget); 709 bool is_static = ((flags & JVM_ACC_STATIC) != 0); 710 fieldDescriptor fd; // find_field initializes fd if found 711 if (!defc->find_field_from_offset(vmindex, is_static, &fd)) 712 break; // cannot expand 713 if (!have_defc) { 714 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); 715 } 716 if (!have_name) { 717 //not java_lang_String::create_from_symbol; let's intern member names 718 Handle name = StringTable::intern(fd.name(), CHECK); 719 java_lang_invoke_MemberName::set_name(mname(), name()); 720 } 721 if (!have_type) { 722 Handle type = java_lang_String::create_from_symbol(fd.signature(), CHECK); 723 java_lang_invoke_MemberName::set_type(mname(), type()); 724 } 725 return; 726 } 727 } 728 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); 729 } 730 731 int MethodHandles::find_MemberNames(klassOop k, 732 Symbol* name, Symbol* sig, 733 int mflags, klassOop caller, 734 int skip, objArrayOop results) { 735 DEBUG_ONLY(No_Safepoint_Verifier nsv); 736 // this code contains no safepoints! 737 738 // %%% take caller into account! 739 740 if (k == NULL || !Klass::cast(k)->oop_is_instance()) return -1; 741 742 int rfill = 0, rlimit = results->length(), rskip = skip; 743 // overflow measurement: 744 int overflow = 0, overflow_limit = MAX2(1000, rlimit); 745 746 int match_flags = mflags; 747 bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0); 748 bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0); 749 bool local_only = !(search_superc | search_intfc); 750 bool classes_only = false; 751 752 if (name != NULL) { 753 if (name->utf8_length() == 0) return 0; // a match is not possible 754 } 755 if (sig != NULL) { 756 if (sig->utf8_length() == 0) return 0; // a match is not possible 757 if (sig->byte_at(0) == '(') 758 match_flags &= ~(IS_FIELD | IS_TYPE); 759 else 760 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD); 761 } 762 763 if ((match_flags & IS_TYPE) != 0) { 764 // NYI, and Core Reflection works quite well for this query 765 } 766 767 if ((match_flags & IS_FIELD) != 0) { 768 for (FieldStream st(k, local_only, !search_intfc); !st.eos(); st.next()) { 769 if (name != NULL && st.name() != name) 770 continue; 771 if (sig != NULL && st.signature() != sig) 772 continue; 773 // passed the filters 774 if (rskip > 0) { 775 --rskip; 776 } else if (rfill < rlimit) { 777 oop result = results->obj_at(rfill++); 778 if (!java_lang_invoke_MemberName::is_instance(result)) 779 return -99; // caller bug! 780 MethodHandles::init_MemberName(result, st.klass()->as_klassOop(), st.access_flags(), st.offset()); 781 } else if (++overflow >= overflow_limit) { 782 match_flags = 0; break; // got tired of looking at overflow 783 } 784 } 785 } 786 787 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { 788 // watch out for these guys: 789 Symbol* init_name = vmSymbols::object_initializer_name(); 790 Symbol* clinit_name = vmSymbols::class_initializer_name(); 791 if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit> 792 bool negate_name_test = false; 793 // fix name so that it captures the intention of IS_CONSTRUCTOR 794 if (!(match_flags & IS_METHOD)) { 795 // constructors only 796 if (name == NULL) { 797 name = init_name; 798 } else if (name != init_name) { 799 return 0; // no constructors of this method name 800 } 801 } else if (!(match_flags & IS_CONSTRUCTOR)) { 802 // methods only 803 if (name == NULL) { 804 name = init_name; 805 negate_name_test = true; // if we see the name, we *omit* the entry 806 } else if (name == init_name) { 807 return 0; // no methods of this constructor name 808 } 809 } else { 810 // caller will accept either sort; no need to adjust name 811 } 812 for (MethodStream st(k, local_only, !search_intfc); !st.eos(); st.next()) { 813 methodOop m = st.method(); 814 Symbol* m_name = m->name(); 815 if (m_name == clinit_name) 816 continue; 817 if (name != NULL && ((m_name != name) ^ negate_name_test)) 818 continue; 819 if (sig != NULL && m->signature() != sig) 820 continue; 821 // passed the filters 822 if (rskip > 0) { 823 --rskip; 824 } else if (rfill < rlimit) { 825 oop result = results->obj_at(rfill++); 826 if (!java_lang_invoke_MemberName::is_instance(result)) 827 return -99; // caller bug! 828 MethodHandles::init_MemberName(result, m, true); 829 } else if (++overflow >= overflow_limit) { 830 match_flags = 0; break; // got tired of looking at overflow 831 } 832 } 833 } 834 835 // return number of elements we at leasted wanted to initialize 836 return rfill + overflow; 837 } 838 839 840 // Decode this java.lang.Class object into an instanceKlass, if possible. 841 // Throw IAE if not 842 instanceKlassHandle MethodHandles::resolve_instance_klass(oop java_mirror_oop, TRAPS) { 843 instanceKlassHandle empty; 844 klassOop caller = NULL; 845 if (java_lang_Class::is_instance(java_mirror_oop)) { 846 caller = java_lang_Class::as_klassOop(java_mirror_oop); 847 } 848 if (caller == NULL || !Klass::cast(caller)->oop_is_instance()) { 849 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not a class", empty); 850 } 851 return instanceKlassHandle(THREAD, caller); 852 } 853 854 855 856 // Decode the vmtarget field of a method handle. 857 // Sanitize out methodOops, klassOops, and any other non-Java data. 858 // This is for debugging and reflection. 859 oop MethodHandles::encode_target(Handle mh, int format, TRAPS) { 860 assert(java_lang_invoke_MethodHandle::is_instance(mh()), "must be a MH"); 861 if (format == ETF_HANDLE_OR_METHOD_NAME) { 862 oop target = java_lang_invoke_MethodHandle::vmtarget(mh()); 863 if (target == NULL) { 864 return NULL; // unformed MH 865 } 866 klassOop tklass = target->klass(); 867 if (Klass::cast(tklass)->is_subclass_of(SystemDictionary::Object_klass())) { 868 return target; // target is another MH (or something else?) 869 } 870 } 871 if (format == ETF_DIRECT_HANDLE) { 872 oop target = mh(); 873 for (;;) { 874 if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) { 875 return target; 876 } 877 if (!java_lang_invoke_MethodHandle::is_instance(target)){ 878 return NULL; // unformed MH 879 } 880 target = java_lang_invoke_MethodHandle::vmtarget(target); 881 } 882 } 883 // cases of metadata in MH.vmtarget: 884 // - AMH can have methodOop for static invoke with bound receiver 885 // - DMH can have methodOop for static invoke (on variable receiver) 886 // - DMH can have klassOop for dispatched (non-static) invoke 887 klassOop receiver_limit = NULL; 888 int decode_flags = 0; 889 methodOop m = decode_MethodHandle(mh(), receiver_limit, decode_flags); 890 if (m == NULL) return NULL; 891 switch (format) { 892 case ETF_REFLECT_METHOD: 893 // same as jni_ToReflectedMethod: 894 if (m->is_initializer()) { 895 return Reflection::new_constructor(m, THREAD); 896 } else { 897 return Reflection::new_method(m, UseNewReflection, false, THREAD); 898 } 899 900 case ETF_HANDLE_OR_METHOD_NAME: // method, not handle 901 case ETF_METHOD_NAME: 902 { 903 if (SystemDictionary::MemberName_klass() == NULL) break; 904 instanceKlassHandle mname_klass(THREAD, SystemDictionary::MemberName_klass()); 905 mname_klass->initialize(CHECK_NULL); 906 Handle mname = mname_klass->allocate_instance_handle(CHECK_NULL); 907 java_lang_invoke_MemberName::set_vmindex(mname(), VM_INDEX_UNINITIALIZED); 908 bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); 909 init_MemberName(mname(), m, do_dispatch); 910 expand_MemberName(mname, 0, CHECK_NULL); 911 return mname(); 912 } 913 } 914 915 // Unknown format code. 916 char msg[50]; 917 jio_snprintf(msg, sizeof(msg), "unknown getTarget format=%d", format); 918 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), msg); 919 } 920 921 static const char* always_null_names[] = { 922 "java/lang/Void", 923 "java/lang/Null", 924 //"java/lang/Nothing", 925 "sun/dyn/empty/Empty", 926 "sun/invoke/empty/Empty", 927 NULL 928 }; 929 930 static bool is_always_null_type(klassOop klass) { 931 if (!Klass::cast(klass)->oop_is_instance()) return false; 932 instanceKlass* ik = instanceKlass::cast(klass); 933 // Must be on the boot class path: 934 if (ik->class_loader() != NULL) return false; 935 // Check the name. 936 Symbol* name = ik->name(); 937 for (int i = 0; ; i++) { 938 const char* test_name = always_null_names[i]; 939 if (test_name == NULL) break; 940 if (name->equals(test_name)) 941 return true; 942 } 943 return false; 944 } 945 946 bool MethodHandles::class_cast_needed(klassOop src, klassOop dst) { 947 if (src == dst || dst == SystemDictionary::Object_klass()) 948 return false; // quickest checks 949 Klass* srck = Klass::cast(src); 950 Klass* dstk = Klass::cast(dst); 951 if (dstk->is_interface()) { 952 // interface receivers can safely be viewed as untyped, 953 // because interface calls always include a dynamic check 954 //dstk = Klass::cast(SystemDictionary::Object_klass()); 955 return false; 956 } 957 if (srck->is_interface()) { 958 // interface arguments must be viewed as untyped 959 //srck = Klass::cast(SystemDictionary::Object_klass()); 960 return true; 961 } 962 if (is_always_null_type(src)) { 963 // some source types are known to be never instantiated; 964 // they represent references which are always null 965 // such null references never fail to convert safely 966 return false; 967 } 968 return !srck->is_subclass_of(dstk->as_klassOop()); 969 } 970 971 static oop object_java_mirror() { 972 return Klass::cast(SystemDictionary::Object_klass())->java_mirror(); 973 } 974 975 bool MethodHandles::same_basic_type_for_arguments(BasicType src, 976 BasicType dst, 977 bool raw, 978 bool for_return) { 979 if (for_return) { 980 // return values can always be forgotten: 981 if (dst == T_VOID) return true; 982 if (src == T_VOID) return raw && (dst == T_INT); 983 // We allow caller to receive a garbage int, which is harmless. 984 // This trick is pulled by trusted code (see VerifyType.canPassRaw). 985 } 986 assert(src != T_VOID && dst != T_VOID, "should not be here"); 987 if (src == dst) return true; 988 if (type2size[src] != type2size[dst]) return false; 989 if (src == T_OBJECT || dst == T_OBJECT) return false; 990 if (raw) return true; // bitwise reinterpretation; caller guarantees safety 991 // allow reinterpretation casts for integral widening 992 if (is_subword_type(src)) { // subwords can fit in int or other subwords 993 if (dst == T_INT) // any subword fits in an int 994 return true; 995 if (src == T_BOOLEAN) // boolean fits in any subword 996 return is_subword_type(dst); 997 if (src == T_BYTE && dst == T_SHORT) 998 return true; // remaining case: byte fits in short 999 } 1000 // allow float/fixed reinterpretation casts 1001 if (src == T_FLOAT) return dst == T_INT; 1002 if (src == T_INT) return dst == T_FLOAT; 1003 if (src == T_DOUBLE) return dst == T_LONG; 1004 if (src == T_LONG) return dst == T_DOUBLE; 1005 return false; 1006 } 1007 1008 const char* MethodHandles::check_method_receiver(methodOop m, 1009 klassOop passed_recv_type) { 1010 assert(!m->is_static(), "caller resp."); 1011 if (passed_recv_type == NULL) 1012 return "receiver type is primitive"; 1013 if (class_cast_needed(passed_recv_type, m->method_holder())) { 1014 Klass* formal = Klass::cast(m->method_holder()); 1015 return SharedRuntime::generate_class_cast_message("receiver type", 1016 formal->external_name()); 1017 } 1018 return NULL; // checks passed 1019 } 1020 1021 // Verify that m's signature can be called type-safely by a method handle 1022 // of the given method type 'mtype'. 1023 // It takes a TRAPS argument because it must perform symbol lookups. 1024 void MethodHandles::verify_method_signature(methodHandle m, 1025 Handle mtype, 1026 int first_ptype_pos, 1027 KlassHandle insert_ptype, 1028 TRAPS) { 1029 objArrayHandle ptypes(THREAD, java_lang_invoke_MethodType::ptypes(mtype())); 1030 int pnum = first_ptype_pos; 1031 int pmax = ptypes->length(); 1032 int mnum = 0; // method argument 1033 const char* err = NULL; 1034 ResourceMark rm(THREAD); 1035 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) { 1036 oop ptype_oop = NULL; 1037 if (ss.at_return_type()) { 1038 if (pnum != pmax) 1039 { err = "too many arguments"; break; } 1040 ptype_oop = java_lang_invoke_MethodType::rtype(mtype()); 1041 } else { 1042 if (pnum >= pmax) 1043 { err = "not enough arguments"; break; } 1044 if (pnum >= 0) 1045 ptype_oop = ptypes->obj_at(pnum); 1046 else if (insert_ptype.is_null()) 1047 ptype_oop = NULL; 1048 else 1049 ptype_oop = insert_ptype->java_mirror(); 1050 pnum += 1; 1051 mnum += 1; 1052 } 1053 klassOop pklass = NULL; 1054 BasicType ptype = T_OBJECT; 1055 if (ptype_oop != NULL) 1056 ptype = java_lang_Class::as_BasicType(ptype_oop, &pklass); 1057 else 1058 // null does not match any non-reference; use Object to report the error 1059 pklass = SystemDictionary::Object_klass(); 1060 klassOop mklass = NULL; 1061 BasicType mtype = ss.type(); 1062 if (mtype == T_ARRAY) mtype = T_OBJECT; // fold all refs to T_OBJECT 1063 if (mtype == T_OBJECT) { 1064 if (ptype_oop == NULL) { 1065 // null matches any reference 1066 continue; 1067 } 1068 KlassHandle pklass_handle(THREAD, pklass); pklass = NULL; 1069 // If we fail to resolve types at this point, we will throw an error. 1070 Symbol* name = ss.as_symbol(CHECK); 1071 instanceKlass* mk = instanceKlass::cast(m->method_holder()); 1072 Handle loader(THREAD, mk->class_loader()); 1073 Handle domain(THREAD, mk->protection_domain()); 1074 mklass = SystemDictionary::resolve_or_null(name, loader, domain, CHECK); 1075 pklass = pklass_handle(); 1076 if (mklass == NULL && pklass != NULL && 1077 Klass::cast(pklass)->name() == name && 1078 m->is_method_handle_invoke()) { 1079 // Assume a match. We can't really decode the signature of MH.invoke*. 1080 continue; 1081 } 1082 } 1083 if (!ss.at_return_type()) { 1084 err = check_argument_type_change(ptype, pklass, mtype, mklass, mnum); 1085 } else { 1086 err = check_return_type_change(mtype, mklass, ptype, pklass); // note reversal! 1087 } 1088 if (err != NULL) break; 1089 } 1090 1091 if (err != NULL) { 1092 THROW_MSG(vmSymbols::java_lang_InternalError(), err); 1093 } 1094 } 1095 1096 // Main routine for verifying the MethodHandle.type of a proposed 1097 // direct or bound-direct method handle. 1098 void MethodHandles::verify_method_type(methodHandle m, 1099 Handle mtype, 1100 bool has_bound_recv, 1101 KlassHandle bound_recv_type, 1102 TRAPS) { 1103 bool m_needs_receiver = !m->is_static(); 1104 1105 const char* err = NULL; 1106 1107 int first_ptype_pos = m_needs_receiver ? 1 : 0; 1108 if (has_bound_recv) { 1109 first_ptype_pos -= 1; // ptypes do not include the bound argument; start earlier in them 1110 if (m_needs_receiver && bound_recv_type.is_null()) 1111 { err = "bound receiver is not an object"; goto die; } 1112 } 1113 1114 if (m_needs_receiver && err == NULL) { 1115 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(mtype()); 1116 if (ptypes->length() < first_ptype_pos) 1117 { err = "receiver argument is missing"; goto die; } 1118 if (has_bound_recv) 1119 err = check_method_receiver(m(), bound_recv_type->as_klassOop()); 1120 else 1121 err = check_method_receiver(m(), java_lang_Class::as_klassOop(ptypes->obj_at(first_ptype_pos-1))); 1122 if (err != NULL) goto die; 1123 } 1124 1125 // Check the other arguments for mistypes. 1126 verify_method_signature(m, mtype, first_ptype_pos, bound_recv_type, CHECK); 1127 return; 1128 1129 die: 1130 THROW_MSG(vmSymbols::java_lang_InternalError(), err); 1131 } 1132 1133 void MethodHandles::verify_vmslots(Handle mh, TRAPS) { 1134 // Verify vmslots. 1135 int check_slots = argument_slot_count(java_lang_invoke_MethodHandle::type(mh())); 1136 if (java_lang_invoke_MethodHandle::vmslots(mh()) != check_slots) { 1137 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH"); 1138 } 1139 } 1140 1141 void MethodHandles::verify_vmargslot(Handle mh, int argnum, int argslot, TRAPS) { 1142 // Verify that argslot points at the given argnum. 1143 int check_slot = argument_slot(java_lang_invoke_MethodHandle::type(mh()), argnum); 1144 if (argslot != check_slot || argslot < 0) { 1145 const char* fmt = "for argnum of %d, vmargslot is %d, should be %d"; 1146 size_t msglen = strlen(fmt) + 3*11 + 1; 1147 char* msg = NEW_RESOURCE_ARRAY(char, msglen); 1148 jio_snprintf(msg, msglen, fmt, argnum, argslot, check_slot); 1149 THROW_MSG(vmSymbols::java_lang_InternalError(), msg); 1150 } 1151 } 1152 1153 // Verify the correspondence between two method types. 1154 // Apart from the advertised changes, caller method type X must 1155 // be able to invoke the callee method Y type with no violations 1156 // of type integrity. 1157 // Return NULL if all is well, else a short error message. 1158 const char* MethodHandles::check_method_type_change(oop src_mtype, int src_beg, int src_end, 1159 int insert_argnum, oop insert_type, 1160 int change_argnum, oop change_type, 1161 int delete_argnum, 1162 oop dst_mtype, int dst_beg, int dst_end, 1163 bool raw) { 1164 objArrayOop src_ptypes = java_lang_invoke_MethodType::ptypes(src_mtype); 1165 objArrayOop dst_ptypes = java_lang_invoke_MethodType::ptypes(dst_mtype); 1166 1167 int src_max = src_ptypes->length(); 1168 int dst_max = dst_ptypes->length(); 1169 1170 if (src_end == -1) src_end = src_max; 1171 if (dst_end == -1) dst_end = dst_max; 1172 1173 assert(0 <= src_beg && src_beg <= src_end && src_end <= src_max, "oob"); 1174 assert(0 <= dst_beg && dst_beg <= dst_end && dst_end <= dst_max, "oob"); 1175 1176 // pending actions; set to -1 when done: 1177 int ins_idx = insert_argnum, chg_idx = change_argnum, del_idx = delete_argnum; 1178 1179 const char* err = NULL; 1180 1181 // Walk along each array of parameter types, including a virtual 1182 // NULL end marker at the end of each. 1183 for (int src_idx = src_beg, dst_idx = dst_beg; 1184 (src_idx <= src_end && dst_idx <= dst_end); 1185 src_idx++, dst_idx++) { 1186 oop src_type = (src_idx == src_end) ? oop(NULL) : src_ptypes->obj_at(src_idx); 1187 oop dst_type = (dst_idx == dst_end) ? oop(NULL) : dst_ptypes->obj_at(dst_idx); 1188 bool fix_null_src_type = false; 1189 1190 // Perform requested edits. 1191 if (ins_idx == src_idx) { 1192 // note that the inserted guy is never affected by a change or deletion 1193 ins_idx = -1; 1194 src_type = insert_type; 1195 fix_null_src_type = true; 1196 --src_idx; // back up to process src type on next loop 1197 src_idx = src_end; 1198 } else { 1199 // note that the changed guy can be immediately deleted 1200 if (chg_idx == src_idx) { 1201 chg_idx = -1; 1202 assert(src_idx < src_end, "oob"); 1203 src_type = change_type; 1204 fix_null_src_type = true; 1205 } 1206 if (del_idx == src_idx) { 1207 del_idx = -1; 1208 assert(src_idx < src_end, "oob"); 1209 --dst_idx; 1210 continue; // rerun loop after skipping this position 1211 } 1212 } 1213 1214 if (src_type == NULL && fix_null_src_type) 1215 // explicit null in this case matches any dest reference 1216 src_type = (java_lang_Class::is_primitive(dst_type) ? object_java_mirror() : dst_type); 1217 1218 // Compare the two argument types. 1219 if (src_type != dst_type) { 1220 if (src_type == NULL) return "not enough arguments"; 1221 if (dst_type == NULL) return "too many arguments"; 1222 err = check_argument_type_change(src_type, dst_type, dst_idx, raw); 1223 if (err != NULL) return err; 1224 } 1225 } 1226 1227 // Now compare return types also. 1228 oop src_rtype = java_lang_invoke_MethodType::rtype(src_mtype); 1229 oop dst_rtype = java_lang_invoke_MethodType::rtype(dst_mtype); 1230 if (src_rtype != dst_rtype) { 1231 err = check_return_type_change(dst_rtype, src_rtype, raw); // note reversal! 1232 if (err != NULL) return err; 1233 } 1234 1235 assert(err == NULL, ""); 1236 return NULL; // all is well 1237 } 1238 1239 1240 const char* MethodHandles::check_argument_type_change(BasicType src_type, 1241 klassOop src_klass, 1242 BasicType dst_type, 1243 klassOop dst_klass, 1244 int argnum, 1245 bool raw) { 1246 const char* err = NULL; 1247 bool for_return = (argnum < 0); 1248 1249 // just in case: 1250 if (src_type == T_ARRAY) src_type = T_OBJECT; 1251 if (dst_type == T_ARRAY) dst_type = T_OBJECT; 1252 1253 // Produce some nice messages if VerifyMethodHandles is turned on: 1254 if (!same_basic_type_for_arguments(src_type, dst_type, raw, for_return)) { 1255 if (src_type == T_OBJECT) { 1256 if (raw && dst_type == T_INT && is_always_null_type(src_klass)) 1257 return NULL; // OK to convert a null pointer to a garbage int 1258 err = ((argnum >= 0) 1259 ? "type mismatch: passing a %s for method argument #%d, which expects primitive %s" 1260 : "type mismatch: returning a %s, but caller expects primitive %s"); 1261 } else if (dst_type == T_OBJECT) { 1262 err = ((argnum >= 0) 1263 ? "type mismatch: passing a primitive %s for method argument #%d, which expects %s" 1264 : "type mismatch: returning a primitive %s, but caller expects %s"); 1265 } else { 1266 err = ((argnum >= 0) 1267 ? "type mismatch: passing a %s for method argument #%d, which expects %s" 1268 : "type mismatch: returning a %s, but caller expects %s"); 1269 } 1270 } else if (src_type == T_OBJECT && dst_type == T_OBJECT && 1271 class_cast_needed(src_klass, dst_klass)) { 1272 if (!class_cast_needed(dst_klass, src_klass)) { 1273 if (raw) 1274 return NULL; // reverse cast is OK; the MH target is trusted to enforce it 1275 err = ((argnum >= 0) 1276 ? "cast required: passing a %s for method argument #%d, which expects %s" 1277 : "cast required: returning a %s, but caller expects %s"); 1278 } else { 1279 err = ((argnum >= 0) 1280 ? "reference mismatch: passing a %s for method argument #%d, which expects %s" 1281 : "reference mismatch: returning a %s, but caller expects %s"); 1282 } 1283 } else { 1284 // passed the obstacle course 1285 return NULL; 1286 } 1287 1288 // format, format, format 1289 const char* src_name = type2name(src_type); 1290 const char* dst_name = type2name(dst_type); 1291 if (src_type == T_OBJECT) src_name = Klass::cast(src_klass)->external_name(); 1292 if (dst_type == T_OBJECT) dst_name = Klass::cast(dst_klass)->external_name(); 1293 if (src_name == NULL) src_name = "unknown type"; 1294 if (dst_name == NULL) dst_name = "unknown type"; 1295 1296 size_t msglen = strlen(err) + strlen(src_name) + strlen(dst_name) + (argnum < 10 ? 1 : 11); 1297 char* msg = NEW_RESOURCE_ARRAY(char, msglen + 1); 1298 if (argnum >= 0) { 1299 assert(strstr(err, "%d") != NULL, ""); 1300 jio_snprintf(msg, msglen, err, src_name, argnum, dst_name); 1301 } else { 1302 assert(strstr(err, "%d") == NULL, ""); 1303 jio_snprintf(msg, msglen, err, src_name, dst_name); 1304 } 1305 return msg; 1306 } 1307 1308 // Compute the depth within the stack of the given argument, i.e., 1309 // the combined size of arguments to the right of the given argument. 1310 // For the last argument (ptypes.length-1) this will be zero. 1311 // For the first argument (0) this will be the size of all 1312 // arguments but that one. For the special number -1, this 1313 // will be the size of all arguments, including the first. 1314 // If the argument is neither -1 nor a valid argument index, 1315 // then return a negative number. Otherwise, the result 1316 // is in the range [0..vmslots] inclusive. 1317 int MethodHandles::argument_slot(oop method_type, int arg) { 1318 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(method_type); 1319 int argslot = 0; 1320 int len = ptypes->length(); 1321 if (arg < -1 || arg >= len) return -99; 1322 for (int i = len-1; i > arg; i--) { 1323 BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i)); 1324 argslot += type2size[bt]; 1325 } 1326 assert(argument_slot_to_argnum(method_type, argslot) == arg, "inverse works"); 1327 return argslot; 1328 } 1329 1330 // Given a slot number, return the argument number. 1331 int MethodHandles::argument_slot_to_argnum(oop method_type, int query_argslot) { 1332 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(method_type); 1333 int argslot = 0; 1334 int len = ptypes->length(); 1335 for (int i = len-1; i >= 0; i--) { 1336 if (query_argslot == argslot) return i; 1337 BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i)); 1338 argslot += type2size[bt]; 1339 } 1340 // return pseudo-arg deepest in stack: 1341 if (query_argslot == argslot) return -1; 1342 return -99; // oob slot, or splitting a double-slot arg 1343 } 1344 1345 methodHandle MethodHandles::dispatch_decoded_method(methodHandle m, 1346 KlassHandle receiver_limit, 1347 int decode_flags, 1348 KlassHandle receiver_klass, 1349 TRAPS) { 1350 assert((decode_flags & ~_DMF_DIRECT_MASK) == 0, "must be direct method reference"); 1351 assert((decode_flags & _dmf_has_receiver) != 0, "must have a receiver or first reference argument"); 1352 1353 if (!m->is_static() && 1354 (receiver_klass.is_null() || !receiver_klass->is_subtype_of(m->method_holder()))) 1355 // given type does not match class of method, or receiver is null! 1356 // caller should have checked this, but let's be extra careful... 1357 return methodHandle(); 1358 1359 if (receiver_limit.not_null() && 1360 (receiver_klass.not_null() && !receiver_klass->is_subtype_of(receiver_limit()))) 1361 // given type is not limited to the receiver type 1362 // note that a null receiver can match any reference value, for a static method 1363 return methodHandle(); 1364 1365 if (!(decode_flags & MethodHandles::_dmf_does_dispatch)) { 1366 // pre-dispatched or static method (null receiver is OK for static) 1367 return m; 1368 1369 } else if (receiver_klass.is_null()) { 1370 // null receiver value; cannot dispatch 1371 return methodHandle(); 1372 1373 } else if (!(decode_flags & MethodHandles::_dmf_from_interface)) { 1374 // perform virtual dispatch 1375 int vtable_index = m->vtable_index(); 1376 guarantee(vtable_index >= 0, "valid vtable index"); 1377 1378 // receiver_klass might be an arrayKlassOop but all vtables start at 1379 // the same place. The cast is to avoid virtual call and assertion. 1380 // See also LinkResolver::runtime_resolve_virtual_method. 1381 instanceKlass* inst = (instanceKlass*)Klass::cast(receiver_klass()); 1382 DEBUG_ONLY(inst->verify_vtable_index(vtable_index)); 1383 methodOop m_oop = inst->method_at_vtable(vtable_index); 1384 return methodHandle(THREAD, m_oop); 1385 1386 } else { 1387 // perform interface dispatch 1388 int itable_index = klassItable::compute_itable_index(m()); 1389 guarantee(itable_index >= 0, "valid itable index"); 1390 instanceKlass* inst = instanceKlass::cast(receiver_klass()); 1391 methodOop m_oop = inst->method_at_itable(m->method_holder(), itable_index, THREAD); 1392 return methodHandle(THREAD, m_oop); 1393 } 1394 } 1395 1396 void MethodHandles::verify_DirectMethodHandle(Handle mh, methodHandle m, TRAPS) { 1397 // Verify type. 1398 Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); 1399 verify_method_type(m, mtype, false, KlassHandle(), CHECK); 1400 1401 // Verify vmslots. 1402 if (java_lang_invoke_MethodHandle::vmslots(mh()) != m->size_of_parameters()) { 1403 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in DMH"); 1404 } 1405 } 1406 1407 void MethodHandles::init_DirectMethodHandle(Handle mh, methodHandle m, bool do_dispatch, TRAPS) { 1408 // Check arguments. 1409 if (mh.is_null() || m.is_null() || 1410 (!do_dispatch && m->is_abstract())) { 1411 THROW(vmSymbols::java_lang_InternalError()); 1412 } 1413 1414 java_lang_invoke_MethodHandle::init_vmslots(mh()); 1415 1416 if (VerifyMethodHandles) { 1417 // The privileged code which invokes this routine should not make 1418 // a mistake about types, but it's better to verify. 1419 verify_DirectMethodHandle(mh, m, CHECK); 1420 } 1421 1422 // Finally, after safety checks are done, link to the target method. 1423 // We will follow the same path as the latter part of 1424 // InterpreterRuntime::resolve_invoke(), which first finds the method 1425 // and then decides how to populate the constant pool cache entry 1426 // that links the interpreter calls to the method. We need the same 1427 // bits, and will use the same calling sequence code. 1428 1429 int vmindex = methodOopDesc::garbage_vtable_index; 1430 oop vmtarget = NULL; 1431 1432 instanceKlass::cast(m->method_holder())->link_class(CHECK); 1433 1434 MethodHandleEntry* me = NULL; 1435 if (do_dispatch && Klass::cast(m->method_holder())->is_interface()) { 1436 // We are simulating an invokeinterface instruction. 1437 // (We might also be simulating an invokevirtual on a miranda method, 1438 // but it is safe to treat it as an invokeinterface.) 1439 assert(!m->can_be_statically_bound(), "no final methods on interfaces"); 1440 vmindex = klassItable::compute_itable_index(m()); 1441 assert(vmindex >= 0, "(>=0) == do_dispatch"); 1442 // Set up same bits as ConstantPoolCacheEntry::set_interface_call(). 1443 vmtarget = m->method_holder(); // the interface 1444 me = MethodHandles::entry(MethodHandles::_invokeinterface_mh); 1445 } else if (!do_dispatch || m->can_be_statically_bound()) { 1446 // We are simulating an invokestatic or invokespecial instruction. 1447 // Set up the method pointer, just like ConstantPoolCacheEntry::set_method(). 1448 vmtarget = m(); 1449 // this does not help dispatch, but it will make it possible to parse this MH: 1450 vmindex = methodOopDesc::nonvirtual_vtable_index; 1451 assert(vmindex < 0, "(>=0) == do_dispatch"); 1452 if (!m->is_static()) { 1453 me = MethodHandles::entry(MethodHandles::_invokespecial_mh); 1454 } else { 1455 me = MethodHandles::entry(MethodHandles::_invokestatic_mh); 1456 // Part of the semantics of a static call is an initialization barrier. 1457 // For a DMH, it is done now, when the handle is created. 1458 Klass* k = Klass::cast(m->method_holder()); 1459 if (k->should_be_initialized()) { 1460 k->initialize(CHECK); 1461 } 1462 } 1463 } else { 1464 // We are simulating an invokevirtual instruction. 1465 // Set up the vtable index, just like ConstantPoolCacheEntry::set_method(). 1466 // The key logic is LinkResolver::runtime_resolve_virtual_method. 1467 vmindex = m->vtable_index(); 1468 vmtarget = m->method_holder(); 1469 me = MethodHandles::entry(MethodHandles::_invokevirtual_mh); 1470 } 1471 1472 if (me == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 1473 1474 java_lang_invoke_DirectMethodHandle::set_vmtarget(mh(), vmtarget); 1475 java_lang_invoke_DirectMethodHandle::set_vmindex(mh(), vmindex); 1476 DEBUG_ONLY(int flags; klassOop rlimit); 1477 assert(MethodHandles::decode_method(mh(), rlimit, flags) == m(), 1478 "properly stored for later decoding"); 1479 DEBUG_ONLY(bool actual_do_dispatch = ((flags & _dmf_does_dispatch) != 0)); 1480 assert(!(actual_do_dispatch && !do_dispatch), 1481 "do not perform dispatch if !do_dispatch specified"); 1482 assert(actual_do_dispatch == (vmindex >= 0), "proper later decoding of do_dispatch"); 1483 assert(decode_MethodHandle_stack_pushes(mh()) == 0, "DMH does not move stack"); 1484 1485 // Done! 1486 java_lang_invoke_MethodHandle::set_vmentry(mh(), me); 1487 } 1488 1489 void MethodHandles::verify_BoundMethodHandle_with_receiver(Handle mh, 1490 methodHandle m, 1491 TRAPS) { 1492 // Verify type. 1493 oop receiver = java_lang_invoke_BoundMethodHandle::argument(mh()); 1494 Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); 1495 KlassHandle bound_recv_type; 1496 if (receiver != NULL) bound_recv_type = KlassHandle(THREAD, receiver->klass()); 1497 verify_method_type(m, mtype, true, bound_recv_type, CHECK); 1498 1499 int receiver_pos = m->size_of_parameters() - 1; 1500 1501 // Verify MH.vmargslot, which should point at the bound receiver. 1502 verify_vmargslot(mh, -1, java_lang_invoke_BoundMethodHandle::vmargslot(mh()), CHECK); 1503 //verify_vmslots(mh, CHECK); 1504 1505 // Verify vmslots. 1506 if (java_lang_invoke_MethodHandle::vmslots(mh()) != receiver_pos) { 1507 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH (receiver)"); 1508 } 1509 } 1510 1511 // Initialize a BMH with a receiver bound directly to a methodOop. 1512 void MethodHandles::init_BoundMethodHandle_with_receiver(Handle mh, 1513 methodHandle original_m, 1514 KlassHandle receiver_limit, 1515 int decode_flags, 1516 TRAPS) { 1517 // Check arguments. 1518 if (mh.is_null() || original_m.is_null()) { 1519 THROW(vmSymbols::java_lang_InternalError()); 1520 } 1521 1522 KlassHandle receiver_klass; 1523 { 1524 oop receiver_oop = java_lang_invoke_BoundMethodHandle::argument(mh()); 1525 if (receiver_oop != NULL) 1526 receiver_klass = KlassHandle(THREAD, receiver_oop->klass()); 1527 } 1528 methodHandle m = dispatch_decoded_method(original_m, 1529 receiver_limit, decode_flags, 1530 receiver_klass, 1531 CHECK); 1532 if (m.is_null()) { THROW(vmSymbols::java_lang_InternalError()); } 1533 if (m->is_abstract()) { THROW(vmSymbols::java_lang_AbstractMethodError()); } 1534 1535 java_lang_invoke_MethodHandle::init_vmslots(mh()); 1536 1537 if (VerifyMethodHandles) { 1538 verify_BoundMethodHandle_with_receiver(mh, m, CHECK); 1539 } 1540 1541 java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), m()); 1542 1543 DEBUG_ONLY(int junk; klassOop junk2); 1544 assert(MethodHandles::decode_method(mh(), junk2, junk) == m(), "properly stored for later decoding"); 1545 assert(decode_MethodHandle_stack_pushes(mh()) == 1, "BMH pushes one stack slot"); 1546 1547 // Done! 1548 java_lang_invoke_MethodHandle::set_vmentry(mh(), MethodHandles::entry(MethodHandles::_bound_ref_direct_mh)); 1549 } 1550 1551 void MethodHandles::verify_BoundMethodHandle(Handle mh, Handle target, int argnum, 1552 bool direct_to_method, TRAPS) { 1553 Handle ptype_handle(THREAD, 1554 java_lang_invoke_MethodType::ptype(java_lang_invoke_MethodHandle::type(target()), argnum)); 1555 KlassHandle ptype_klass; 1556 BasicType ptype = java_lang_Class::as_BasicType(ptype_handle(), &ptype_klass); 1557 int slots_pushed = type2size[ptype]; 1558 1559 oop argument = java_lang_invoke_BoundMethodHandle::argument(mh()); 1560 1561 const char* err = NULL; 1562 1563 switch (ptype) { 1564 case T_OBJECT: 1565 if (argument != NULL) 1566 // we must implicitly convert from the arg type to the outgoing ptype 1567 err = check_argument_type_change(T_OBJECT, argument->klass(), ptype, ptype_klass(), argnum); 1568 break; 1569 1570 case T_ARRAY: case T_VOID: 1571 assert(false, "array, void do not appear here"); 1572 default: 1573 if (ptype != T_INT && !is_subword_type(ptype)) { 1574 err = "unexpected parameter type"; 1575 break; 1576 } 1577 // check subrange of Integer.value, if necessary 1578 if (argument == NULL || argument->klass() != SystemDictionary::Integer_klass()) { 1579 err = "bound integer argument must be of type java.lang.Integer"; 1580 break; 1581 } 1582 if (ptype != T_INT) { 1583 int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT); 1584 jint value = argument->int_field(value_offset); 1585 int vminfo = adapter_unbox_subword_vminfo(ptype); 1586 jint subword = truncate_subword_from_vminfo(value, vminfo); 1587 if (value != subword) { 1588 err = "bound subword value does not fit into the subword type"; 1589 break; 1590 } 1591 } 1592 break; 1593 case T_FLOAT: 1594 case T_DOUBLE: 1595 case T_LONG: 1596 { 1597 // we must implicitly convert from the unboxed arg type to the outgoing ptype 1598 BasicType argbox = java_lang_boxing_object::basic_type(argument); 1599 if (argbox != ptype) { 1600 err = check_argument_type_change(T_OBJECT, (argument == NULL 1601 ? SystemDictionary::Object_klass() 1602 : argument->klass()), 1603 ptype, ptype_klass(), argnum); 1604 assert(err != NULL, "this must be an error"); 1605 } 1606 break; 1607 } 1608 } 1609 1610 if (err == NULL) { 1611 DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh())); 1612 if (direct_to_method) { 1613 assert(this_pushes == slots_pushed, "BMH pushes one or two stack slots"); 1614 assert(slots_pushed <= MethodHandlePushLimit, ""); 1615 } else { 1616 int target_pushes = decode_MethodHandle_stack_pushes(target()); 1617 assert(this_pushes == slots_pushed + target_pushes, "BMH stack motion must be correct"); 1618 // do not blow the stack; use a Java-based adapter if this limit is exceeded 1619 // FIXME 1620 // if (slots_pushed + target_pushes > MethodHandlePushLimit) 1621 // err = "too many bound parameters"; 1622 } 1623 } 1624 1625 if (err == NULL) { 1626 // Verify the rest of the method type. 1627 err = check_method_type_insertion(java_lang_invoke_MethodHandle::type(mh()), 1628 argnum, ptype_handle(), 1629 java_lang_invoke_MethodHandle::type(target())); 1630 } 1631 1632 if (err != NULL) { 1633 THROW_MSG(vmSymbols::java_lang_InternalError(), err); 1634 } 1635 } 1636 1637 void MethodHandles::init_BoundMethodHandle(Handle mh, Handle target, int argnum, TRAPS) { 1638 // Check arguments. 1639 if (mh.is_null() || target.is_null() || !java_lang_invoke_MethodHandle::is_instance(target())) { 1640 THROW(vmSymbols::java_lang_InternalError()); 1641 } 1642 1643 java_lang_invoke_MethodHandle::init_vmslots(mh()); 1644 1645 if (VerifyMethodHandles) { 1646 int insert_after = argnum - 1; 1647 verify_vmargslot(mh, insert_after, java_lang_invoke_BoundMethodHandle::vmargslot(mh()), CHECK); 1648 verify_vmslots(mh, CHECK); 1649 } 1650 1651 // Get bound type and required slots. 1652 oop ptype_oop = java_lang_invoke_MethodType::ptype(java_lang_invoke_MethodHandle::type(target()), argnum); 1653 BasicType ptype = java_lang_Class::as_BasicType(ptype_oop); 1654 int slots_pushed = type2size[ptype]; 1655 1656 // If (a) the target is a direct non-dispatched method handle, 1657 // or (b) the target is a dispatched direct method handle and we 1658 // are binding the receiver, cut out the middle-man. 1659 // Do this by decoding the DMH and using its methodOop directly as vmtarget. 1660 bool direct_to_method = false; 1661 if (OptimizeMethodHandles && 1662 target->klass() == SystemDictionary::DirectMethodHandle_klass() && 1663 (argnum == 0 || java_lang_invoke_DirectMethodHandle::vmindex(target()) < 0)) { 1664 int decode_flags = 0; klassOop receiver_limit_oop = NULL; 1665 methodHandle m(THREAD, decode_method(target(), receiver_limit_oop, decode_flags)); 1666 if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "DMH failed to decode"); } 1667 DEBUG_ONLY(int m_vmslots = m->size_of_parameters() - slots_pushed); // pos. of 1st arg. 1668 assert(java_lang_invoke_BoundMethodHandle::vmslots(mh()) == m_vmslots, "type w/ m sig"); 1669 if (argnum == 0 && (decode_flags & _dmf_has_receiver) != 0) { 1670 KlassHandle receiver_limit(THREAD, receiver_limit_oop); 1671 init_BoundMethodHandle_with_receiver(mh, m, 1672 receiver_limit, decode_flags, 1673 CHECK); 1674 return; 1675 } 1676 1677 // Even if it is not a bound receiver, we still might be able 1678 // to bind another argument and still invoke the methodOop directly. 1679 if (!(decode_flags & _dmf_does_dispatch)) { 1680 direct_to_method = true; 1681 java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), m()); 1682 } 1683 } 1684 if (!direct_to_method) 1685 java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), target()); 1686 1687 if (VerifyMethodHandles) { 1688 verify_BoundMethodHandle(mh, target, argnum, direct_to_method, CHECK); 1689 } 1690 1691 // Next question: Is this a ref, int, or long bound value? 1692 MethodHandleEntry* me = NULL; 1693 if (ptype == T_OBJECT) { 1694 if (direct_to_method) me = MethodHandles::entry(_bound_ref_direct_mh); 1695 else me = MethodHandles::entry(_bound_ref_mh); 1696 } else if (slots_pushed == 2) { 1697 if (direct_to_method) me = MethodHandles::entry(_bound_long_direct_mh); 1698 else me = MethodHandles::entry(_bound_long_mh); 1699 } else if (slots_pushed == 1) { 1700 if (direct_to_method) me = MethodHandles::entry(_bound_int_direct_mh); 1701 else me = MethodHandles::entry(_bound_int_mh); 1702 } else { 1703 assert(false, ""); 1704 } 1705 1706 // Done! 1707 java_lang_invoke_MethodHandle::set_vmentry(mh(), me); 1708 } 1709 1710 static void throw_InternalError_for_bad_conversion(int conversion, const char* err, TRAPS) { 1711 char msg[200]; 1712 jio_snprintf(msg, sizeof(msg), "bad adapter (conversion=0x%08x): %s", conversion, err); 1713 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), msg); 1714 } 1715 1716 void MethodHandles::verify_AdapterMethodHandle(Handle mh, int argnum, TRAPS) { 1717 jint conversion = java_lang_invoke_AdapterMethodHandle::conversion(mh()); 1718 int argslot = java_lang_invoke_AdapterMethodHandle::vmargslot(mh()); 1719 1720 verify_vmargslot(mh, argnum, argslot, CHECK); 1721 verify_vmslots(mh, CHECK); 1722 1723 jint conv_op = adapter_conversion_op(conversion); 1724 if (!conv_op_valid(conv_op)) { 1725 throw_InternalError_for_bad_conversion(conversion, "unknown conversion op", THREAD); 1726 return; 1727 } 1728 EntryKind ek = adapter_entry_kind(conv_op); 1729 1730 int stack_move = adapter_conversion_stack_move(conversion); 1731 BasicType src = adapter_conversion_src_type(conversion); 1732 BasicType dest = adapter_conversion_dest_type(conversion); 1733 int vminfo = adapter_conversion_vminfo(conversion); // should be zero 1734 1735 Handle argument(THREAD, java_lang_invoke_AdapterMethodHandle::argument(mh())); 1736 Handle target(THREAD, java_lang_invoke_AdapterMethodHandle::vmtarget(mh())); 1737 Handle src_mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); 1738 Handle dst_mtype(THREAD, java_lang_invoke_MethodHandle::type(target())); 1739 1740 const char* err = NULL; 1741 1742 if (err == NULL) { 1743 // Check that the correct argument is supplied, but only if it is required. 1744 switch (ek) { 1745 case _adapter_check_cast: // target type of cast 1746 case _adapter_ref_to_prim: // wrapper type from which to unbox 1747 case _adapter_prim_to_ref: // wrapper type to box into 1748 case _adapter_collect_args: // array type to collect into 1749 case _adapter_spread_args: // array type to spread from 1750 if (!java_lang_Class::is_instance(argument()) 1751 || java_lang_Class::is_primitive(argument())) 1752 { err = "adapter requires argument of type java.lang.Class"; break; } 1753 if (ek == _adapter_collect_args || 1754 ek == _adapter_spread_args) { 1755 // Make sure it is a suitable collection type. (Array, for now.) 1756 Klass* ak = Klass::cast(java_lang_Class::as_klassOop(argument())); 1757 if (!ak->oop_is_objArray()) { 1758 { err = "adapter requires argument of type java.lang.Class<Object[]>"; break; } 1759 } 1760 } 1761 break; 1762 case _adapter_flyby: 1763 case _adapter_ricochet: 1764 if (!java_lang_invoke_MethodHandle::is_instance(argument())) 1765 { err = "MethodHandle adapter argument required"; break; } 1766 break; 1767 default: 1768 if (argument.not_null()) 1769 { err = "adapter has spurious argument"; break; } 1770 break; 1771 } 1772 } 1773 1774 if (err == NULL) { 1775 // Check that the src/dest types are supplied if needed. 1776 switch (ek) { 1777 case _adapter_check_cast: 1778 if (src != T_OBJECT || dest != T_OBJECT) { 1779 err = "adapter requires object src/dest conversion subfields"; 1780 } 1781 break; 1782 case _adapter_prim_to_prim: 1783 if (!is_java_primitive(src) || !is_java_primitive(dest) || src == dest) { 1784 err = "adapter requires primitive src/dest conversion subfields"; break; 1785 } 1786 if ( (src == T_FLOAT || src == T_DOUBLE) && !(dest == T_FLOAT || dest == T_DOUBLE) || 1787 !(src == T_FLOAT || src == T_DOUBLE) && (dest == T_FLOAT || dest == T_DOUBLE)) { 1788 err = "adapter cannot convert beween floating and fixed-point"; break; 1789 } 1790 break; 1791 case _adapter_ref_to_prim: 1792 if (src != T_OBJECT || !is_java_primitive(dest) 1793 || argument() != Klass::cast(SystemDictionary::box_klass(dest))->java_mirror()) { 1794 err = "adapter requires primitive dest conversion subfield"; break; 1795 } 1796 break; 1797 case _adapter_prim_to_ref: 1798 if (!is_java_primitive(src) || dest != T_OBJECT 1799 || argument() != Klass::cast(SystemDictionary::box_klass(src))->java_mirror()) { 1800 err = "adapter requires primitive src conversion subfield"; break; 1801 } 1802 break; 1803 case _adapter_swap_args: 1804 case _adapter_rot_args: 1805 { 1806 if (!src || src != dest) { 1807 err = "adapter requires src/dest conversion subfields for swap"; break; 1808 } 1809 int swap_size = type2size[src]; 1810 oop src_mtype = java_lang_invoke_AdapterMethodHandle::type(mh()); 1811 oop dest_mtype = java_lang_invoke_AdapterMethodHandle::type(target()); 1812 int slot_limit = java_lang_invoke_AdapterMethodHandle::vmslots(target()); 1813 int src_slot = argslot; 1814 int dest_slot = vminfo; 1815 bool rotate_up = (src_slot > dest_slot); // upward rotation 1816 int src_arg = argnum; 1817 int dest_arg = argument_slot_to_argnum(dest_mtype, dest_slot); 1818 verify_vmargslot(mh, dest_arg, dest_slot, CHECK); 1819 if (!(dest_slot >= src_slot + swap_size) && 1820 !(src_slot >= dest_slot + swap_size)) { 1821 err = "source, destination slots must be distinct"; 1822 } else if (ek == _adapter_swap_args && !(src_slot > dest_slot)) { 1823 err = "source of swap must be deeper in stack"; 1824 } else if (ek == _adapter_swap_args) { 1825 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype, dest_arg), 1826 java_lang_invoke_MethodType::ptype(dest_mtype, src_arg), 1827 dest_arg); 1828 } else if (ek == _adapter_rot_args) { 1829 if (rotate_up) { 1830 assert((src_slot > dest_slot) && (src_arg < dest_arg), ""); 1831 // rotate up: [dest_slot..src_slot-ss] --> [dest_slot+ss..src_slot] 1832 // that is: [src_arg+1..dest_arg] --> [src_arg..dest_arg-1] 1833 for (int i = src_arg+1; i <= dest_arg && err == NULL; i++) { 1834 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype, i), 1835 java_lang_invoke_MethodType::ptype(dest_mtype, i-1), 1836 i); 1837 } 1838 } else { // rotate down 1839 assert((src_slot < dest_slot) && (src_arg > dest_arg), ""); 1840 // rotate down: [src_slot+ss..dest_slot] --> [src_slot..dest_slot-ss] 1841 // that is: [dest_arg..src_arg-1] --> [dst_arg+1..src_arg] 1842 for (int i = dest_arg; i <= src_arg-1 && err == NULL; i++) { 1843 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype, i), 1844 java_lang_invoke_MethodType::ptype(dest_mtype, i+1), 1845 i); 1846 } 1847 } 1848 } 1849 if (err == NULL) 1850 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype, src_arg), 1851 java_lang_invoke_MethodType::ptype(dest_mtype, dest_arg), 1852 src_arg); 1853 } 1854 break; 1855 case _adapter_collect_args: 1856 case _adapter_spread_args: 1857 { 1858 BasicType coll_type = (ek == _adapter_collect_args) ? dest : src; 1859 BasicType elem_type = (ek == _adapter_collect_args) ? src : dest; 1860 if (coll_type != T_OBJECT || elem_type != T_OBJECT) { 1861 err = "adapter requires src/dest subfields"; break; 1862 // later: 1863 // - consider making coll be a primitive array 1864 // - consider making coll be a heterogeneous collection 1865 } 1866 } 1867 break; 1868 default: 1869 if (src != 0 || dest != 0) { 1870 err = "adapter has spurious src/dest conversion subfields"; break; 1871 } 1872 break; 1873 } 1874 } 1875 1876 if (err == NULL) { 1877 // Check the stack_move subfield. 1878 // It must always report the net change in stack size, positive or negative. 1879 int slots_pushed = stack_move / stack_move_unit(); 1880 switch (ek) { 1881 case _adapter_prim_to_prim: 1882 case _adapter_ref_to_prim: 1883 case _adapter_prim_to_ref: 1884 if (slots_pushed != type2size[dest] - type2size[src]) { 1885 err = "wrong stack motion for primitive conversion"; 1886 } 1887 break; 1888 case _adapter_dup_args: 1889 if (slots_pushed <= 0) { 1890 err = "adapter requires conversion subfield slots_pushed > 0"; 1891 } 1892 break; 1893 case _adapter_drop_args: 1894 if (slots_pushed >= 0) { 1895 err = "adapter requires conversion subfield slots_pushed < 0"; 1896 } 1897 break; 1898 case _adapter_collect_args: 1899 if (slots_pushed > 1) { 1900 err = "adapter requires conversion subfield slots_pushed <= 1"; 1901 } 1902 break; 1903 case _adapter_spread_args: 1904 if (slots_pushed < -1) { 1905 err = "adapter requires conversion subfield slots_pushed >= -1"; 1906 } 1907 break; 1908 default: 1909 if (stack_move != 0) { 1910 err = "adapter has spurious stack_move conversion subfield"; 1911 } 1912 break; 1913 } 1914 if (err == NULL && stack_move != slots_pushed * stack_move_unit()) { 1915 err = "stack_move conversion subfield must be multiple of stack_move_unit"; 1916 } 1917 } 1918 1919 if (err == NULL) { 1920 // Make sure this adapter does not push too deeply. 1921 int slots_pushed = stack_move / stack_move_unit(); 1922 int this_vmslots = java_lang_invoke_MethodHandle::vmslots(mh()); 1923 int target_vmslots = java_lang_invoke_MethodHandle::vmslots(target()); 1924 if (slots_pushed != (target_vmslots - this_vmslots)) { 1925 err = "stack_move inconsistent with previous and current MethodType vmslots"; 1926 } else if (slots_pushed > 0) { 1927 // verify stack_move against MethodHandlePushLimit 1928 int target_pushes = decode_MethodHandle_stack_pushes(target()); 1929 // do not blow the stack; use a Java-based adapter if this limit is exceeded 1930 if (slots_pushed + target_pushes > MethodHandlePushLimit) { 1931 err = "adapter pushes too many parameters"; 1932 } 1933 } 1934 1935 // While we're at it, check that the stack motion decoder works: 1936 DEBUG_ONLY(int target_pushes = decode_MethodHandle_stack_pushes(target())); 1937 DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh())); 1938 assert(this_pushes == slots_pushed + target_pushes, "AMH stack motion must be correct"); 1939 } 1940 1941 if (err == NULL && vminfo != 0) { 1942 switch (ek) { 1943 case _adapter_swap_args: 1944 case _adapter_rot_args: 1945 break; // OK 1946 default: 1947 err = "vminfo subfield is reserved to the JVM"; 1948 } 1949 } 1950 1951 // Do additional ad hoc checks. 1952 if (err == NULL) { 1953 switch (ek) { 1954 case _adapter_retype_only: 1955 err = check_method_type_passthrough(src_mtype(), dst_mtype(), false); 1956 break; 1957 1958 case _adapter_retype_raw: 1959 err = check_method_type_passthrough(src_mtype(), dst_mtype(), true); 1960 break; 1961 1962 case _adapter_check_cast: 1963 { 1964 // The actual value being checked must be a reference: 1965 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), argnum), 1966 object_java_mirror(), argnum); 1967 if (err != NULL) break; 1968 1969 // The output of the cast must fit with the destination argument: 1970 Handle cast_class = argument; 1971 err = check_method_type_conversion(src_mtype(), 1972 argnum, cast_class(), 1973 dst_mtype()); 1974 } 1975 break; 1976 1977 // %%% TO DO: continue in remaining cases to verify src/dst_mtype if VerifyMethodHandles 1978 } 1979 } 1980 1981 if (err != NULL) { 1982 throw_InternalError_for_bad_conversion(conversion, err, THREAD); 1983 return; 1984 } 1985 1986 } 1987 1988 void MethodHandles::init_AdapterMethodHandle(Handle mh, Handle target, int argnum, TRAPS) { 1989 oop argument = java_lang_invoke_AdapterMethodHandle::argument(mh()); 1990 int argslot = java_lang_invoke_AdapterMethodHandle::vmargslot(mh()); 1991 jint conversion = java_lang_invoke_AdapterMethodHandle::conversion(mh()); 1992 jint conv_op = adapter_conversion_op(conversion); 1993 1994 // adjust the adapter code to the internal EntryKind enumeration: 1995 EntryKind ek_orig = adapter_entry_kind(conv_op); 1996 EntryKind ek_opt = ek_orig; // may be optimized 1997 1998 // Finalize the vmtarget field (Java initialized it to null). 1999 if (!java_lang_invoke_MethodHandle::is_instance(target())) { 2000 throw_InternalError_for_bad_conversion(conversion, "bad target", THREAD); 2001 return; 2002 } 2003 java_lang_invoke_AdapterMethodHandle::set_vmtarget(mh(), target()); 2004 2005 if (VerifyMethodHandles) { 2006 verify_AdapterMethodHandle(mh, argnum, CHECK); 2007 } 2008 2009 int stack_move = adapter_conversion_stack_move(conversion); 2010 BasicType src = adapter_conversion_src_type(conversion); 2011 BasicType dest = adapter_conversion_dest_type(conversion); 2012 int vminfo = adapter_conversion_vminfo(conversion); // should be zero 2013 2014 const char* err = NULL; 2015 2016 // Now it's time to finish the case analysis and pick a MethodHandleEntry. 2017 switch (ek_orig) { 2018 case _adapter_retype_only: 2019 case _adapter_retype_raw: 2020 case _adapter_check_cast: 2021 case _adapter_dup_args: 2022 case _adapter_drop_args: 2023 // these work fine via general case code 2024 break; 2025 2026 case _adapter_prim_to_prim: 2027 { 2028 // Non-subword cases are {int,float,long,double} -> {int,float,long,double}. 2029 // And, the {float,double} -> {int,long} cases must be handled by Java. 2030 switch (type2size[src] *4+ type2size[dest]) { 2031 case 1 *4+ 1: 2032 assert(src == T_INT || is_subword_type(src), "source is not float"); 2033 // Subword-related cases are int -> {boolean,byte,char,short}. 2034 ek_opt = _adapter_opt_i2i; 2035 vminfo = adapter_prim_to_prim_subword_vminfo(dest); 2036 break; 2037 case 2 *4+ 1: 2038 if (src == T_LONG && (dest == T_INT || is_subword_type(dest))) { 2039 ek_opt = _adapter_opt_l2i; 2040 vminfo = adapter_prim_to_prim_subword_vminfo(dest); 2041 } else if (src == T_DOUBLE && dest == T_FLOAT) { 2042 ek_opt = _adapter_opt_d2f; 2043 } else { 2044 assert(false, ""); 2045 } 2046 break; 2047 case 1 *4+ 2: 2048 if (src == T_INT && dest == T_LONG) { 2049 ek_opt = _adapter_opt_i2l; 2050 } else if (src == T_FLOAT && dest == T_DOUBLE) { 2051 ek_opt = _adapter_opt_f2d; 2052 } else { 2053 assert(false, ""); 2054 } 2055 break; 2056 default: 2057 assert(false, ""); 2058 break; 2059 } 2060 } 2061 break; 2062 2063 case _adapter_ref_to_prim: 2064 { 2065 switch (type2size[dest]) { 2066 case 1: 2067 ek_opt = _adapter_opt_unboxi; 2068 vminfo = adapter_unbox_subword_vminfo(dest); 2069 break; 2070 case 2: 2071 ek_opt = _adapter_opt_unboxl; 2072 break; 2073 default: 2074 assert(false, ""); 2075 break; 2076 } 2077 } 2078 break; 2079 2080 case _adapter_prim_to_ref: 2081 goto throw_not_impl; // allocates, hence could block 2082 2083 case _adapter_swap_args: 2084 case _adapter_rot_args: 2085 { 2086 int swap_slots = type2size[src]; 2087 int slot_limit = java_lang_invoke_AdapterMethodHandle::vmslots(mh()); 2088 int src_slot = argslot; 2089 int dest_slot = vminfo; 2090 int rotate = (ek_orig == _adapter_swap_args) ? 0 : (src_slot > dest_slot) ? 1 : -1; 2091 switch (swap_slots) { 2092 case 1: 2093 ek_opt = (!rotate ? _adapter_opt_swap_1 : 2094 rotate > 0 ? _adapter_opt_rot_1_up : _adapter_opt_rot_1_down); 2095 break; 2096 case 2: 2097 ek_opt = (!rotate ? _adapter_opt_swap_2 : 2098 rotate > 0 ? _adapter_opt_rot_2_up : _adapter_opt_rot_2_down); 2099 break; 2100 default: 2101 assert(false, ""); 2102 break; 2103 } 2104 } 2105 break; 2106 2107 case _adapter_collect_args: 2108 goto throw_not_impl; // allocates, hence could block 2109 2110 case _adapter_spread_args: 2111 { 2112 // vminfo will be the required length of the array 2113 int slots_pushed = stack_move / stack_move_unit(); 2114 int array_size = slots_pushed + 1; 2115 assert(array_size >= 0, ""); 2116 vminfo = array_size; 2117 switch (array_size) { 2118 case 0: ek_opt = _adapter_opt_spread_0; break; 2119 case 1: ek_opt = _adapter_opt_spread_1; break; 2120 default: ek_opt = _adapter_opt_spread_more; break; 2121 } 2122 if ((vminfo & CONV_VMINFO_MASK) != vminfo) 2123 goto throw_not_impl; // overflow 2124 } 2125 break; 2126 2127 case _adapter_flyby: 2128 case _adapter_ricochet: 2129 goto throw_not_impl; // runs Java code, hence could block 2130 2131 default: 2132 // should have failed much earlier; must be a missing case here 2133 assert(false, "incomplete switch"); 2134 // and fall through: 2135 2136 throw_not_impl: 2137 // FIXME: these adapters are NYI 2138 err = "adapter not yet implemented in the JVM"; 2139 break; 2140 } 2141 2142 if (err != NULL) { 2143 throw_InternalError_for_bad_conversion(conversion, err, THREAD); 2144 return; 2145 } 2146 2147 // Rebuild the conversion value; maybe parts of it were changed. 2148 jint new_conversion = adapter_conversion(conv_op, src, dest, stack_move, vminfo); 2149 2150 // Finalize the conversion field. (Note that it is final to Java code.) 2151 java_lang_invoke_AdapterMethodHandle::set_conversion(mh(), new_conversion); 2152 2153 // Done! 2154 java_lang_invoke_MethodHandle::set_vmentry(mh(), entry(ek_opt)); 2155 2156 // There should be enough memory barriers on exit from native methods 2157 // to ensure that the MH is fully initialized to all threads before 2158 // Java code can publish it in global data structures. 2159 } 2160 2161 // 2162 // Here are the native methods on sun.invoke.MethodHandleImpl. 2163 // They are the private interface between this JVM and the HotSpot-specific 2164 // Java code that implements JSR 292 method handles. 2165 // 2166 // Note: We use a JVM_ENTRY macro to define each of these, for this is the way 2167 // that intrinsic (non-JNI) native methods are defined in HotSpot. 2168 // 2169 2170 // direct method handles for invokestatic or invokespecial 2171 // void init(DirectMethodHandle self, MemberName ref, boolean doDispatch, Class<?> caller); 2172 JVM_ENTRY(void, MHN_init_DMH(JNIEnv *env, jobject igcls, jobject mh_jh, 2173 jobject target_jh, jboolean do_dispatch, jobject caller_jh)) { 2174 ResourceMark rm; // for error messages 2175 2176 // This is the guy we are initializing: 2177 if (mh_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 2178 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); 2179 2180 // Early returns out of this method leave the DMH in an unfinished state. 2181 assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); 2182 2183 // which method are we really talking about? 2184 if (target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 2185 oop target_oop = JNIHandles::resolve_non_null(target_jh); 2186 if (java_lang_invoke_MemberName::is_instance(target_oop) && 2187 java_lang_invoke_MemberName::vmindex(target_oop) == VM_INDEX_UNINITIALIZED) { 2188 Handle mname(THREAD, target_oop); 2189 MethodHandles::resolve_MemberName(mname, CHECK); 2190 target_oop = mname(); // in case of GC 2191 } 2192 2193 int decode_flags = 0; klassOop receiver_limit = NULL; 2194 methodHandle m(THREAD, 2195 MethodHandles::decode_method(target_oop, 2196 receiver_limit, decode_flags)); 2197 if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "no such method"); } 2198 2199 // The trusted Java code that calls this method should already have performed 2200 // access checks on behalf of the given caller. But, we can verify this. 2201 if (VerifyMethodHandles && caller_jh != NULL) { 2202 KlassHandle caller(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh))); 2203 // If this were a bytecode, the first access check would be against 2204 // the "reference class" mentioned in the CONSTANT_Methodref. 2205 // We don't know at this point which class that was, and if we 2206 // check against m.method_holder we might get the wrong answer. 2207 // So we just make sure to handle this check when the resolution 2208 // happens, when we call resolve_MemberName. 2209 // 2210 // (A public class can inherit public members from private supers, 2211 // and it would be wrong to check access against the private super 2212 // if the original symbolic reference was against the public class.) 2213 // 2214 // If there were a bytecode, the next step would be to lookup the method 2215 // in the reference class, then then check the method's access bits. 2216 // Emulate LinkResolver::check_method_accessability. 2217 klassOop resolved_klass = m->method_holder(); 2218 if (!Reflection::verify_field_access(caller->as_klassOop(), 2219 resolved_klass, resolved_klass, 2220 m->access_flags(), 2221 true)) { 2222 // %%% following cutout belongs in Reflection::verify_field_access? 2223 bool same_pm = Reflection::is_same_package_member(caller->as_klassOop(), 2224 resolved_klass, THREAD); 2225 if (!same_pm) { 2226 THROW_MSG(vmSymbols::java_lang_InternalError(), m->name_and_sig_as_C_string()); 2227 } 2228 } 2229 } 2230 2231 MethodHandles::init_DirectMethodHandle(mh, m, (do_dispatch != JNI_FALSE), CHECK); 2232 } 2233 JVM_END 2234 2235 // bound method handles 2236 JVM_ENTRY(void, MHN_init_BMH(JNIEnv *env, jobject igcls, jobject mh_jh, 2237 jobject target_jh, int argnum)) { 2238 ResourceMark rm; // for error messages 2239 2240 // This is the guy we are initializing: 2241 if (mh_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 2242 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); 2243 2244 // Early returns out of this method leave the BMH in an unfinished state. 2245 assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); 2246 2247 if (target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 2248 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); 2249 2250 if (!java_lang_invoke_MethodHandle::is_instance(target())) { 2251 // Target object is a reflective method. (%%% Do we need this alternate path?) 2252 Untested("init_BMH of non-MH"); 2253 if (argnum != 0) { THROW(vmSymbols::java_lang_InternalError()); } 2254 int decode_flags = 0; klassOop receiver_limit_oop = NULL; 2255 methodHandle m(THREAD, 2256 MethodHandles::decode_method(target(), 2257 receiver_limit_oop, 2258 decode_flags)); 2259 KlassHandle receiver_limit(THREAD, receiver_limit_oop); 2260 MethodHandles::init_BoundMethodHandle_with_receiver(mh, m, 2261 receiver_limit, 2262 decode_flags, 2263 CHECK); 2264 return; 2265 } 2266 2267 // Build a BMH on top of a DMH or another BMH: 2268 MethodHandles::init_BoundMethodHandle(mh, target, argnum, CHECK); 2269 } 2270 JVM_END 2271 2272 // adapter method handles 2273 JVM_ENTRY(void, MHN_init_AMH(JNIEnv *env, jobject igcls, jobject mh_jh, 2274 jobject target_jh, int argnum)) { 2275 // This is the guy we are initializing: 2276 if (mh_jh == NULL || target_jh == NULL) { 2277 THROW(vmSymbols::java_lang_InternalError()); 2278 } 2279 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); 2280 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); 2281 2282 // Early returns out of this method leave the AMH in an unfinished state. 2283 assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); 2284 2285 MethodHandles::init_AdapterMethodHandle(mh, target, argnum, CHECK); 2286 } 2287 JVM_END 2288 2289 // method type forms 2290 JVM_ENTRY(void, MHN_init_MT(JNIEnv *env, jobject igcls, jobject erased_jh)) { 2291 if (erased_jh == NULL) return; 2292 if (TraceMethodHandles) { 2293 tty->print("creating MethodType form "); 2294 if (WizardMode || Verbose) { // Warning: this calls Java code on the MH! 2295 // call Object.toString() 2296 Symbol* name = vmSymbols::toString_name(); 2297 Symbol* sig = vmSymbols::void_string_signature(); 2298 JavaCallArguments args(Handle(THREAD, JNIHandles::resolve_non_null(erased_jh))); 2299 JavaValue result(T_OBJECT); 2300 JavaCalls::call_virtual(&result, SystemDictionary::Object_klass(), name, sig, 2301 &args, CHECK); 2302 Handle str(THREAD, (oop)result.get_jobject()); 2303 java_lang_String::print(str, tty); 2304 } 2305 tty->cr(); 2306 } 2307 } 2308 JVM_END 2309 2310 // debugging and reflection 2311 JVM_ENTRY(jobject, MHN_getTarget(JNIEnv *env, jobject igcls, jobject mh_jh, jint format)) { 2312 Handle mh(THREAD, JNIHandles::resolve(mh_jh)); 2313 if (!java_lang_invoke_MethodHandle::is_instance(mh())) { 2314 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException()); 2315 } 2316 oop target = MethodHandles::encode_target(mh, format, CHECK_NULL); 2317 return JNIHandles::make_local(THREAD, target); 2318 } 2319 JVM_END 2320 2321 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) { 2322 switch (which) { 2323 case MethodHandles::GC_JVM_PUSH_LIMIT: 2324 guarantee(MethodHandlePushLimit >= 2 && MethodHandlePushLimit <= 0xFF, 2325 "MethodHandlePushLimit parameter must be in valid range"); 2326 return MethodHandlePushLimit; 2327 case MethodHandles::GC_JVM_STACK_MOVE_UNIT: 2328 // return number of words per slot, signed according to stack direction 2329 return MethodHandles::stack_move_unit(); 2330 case MethodHandles::GC_CONV_OP_IMPLEMENTED_MASK: 2331 return MethodHandles::adapter_conversion_ops_supported_mask(); 2332 } 2333 return 0; 2334 } 2335 JVM_END 2336 2337 #ifndef PRODUCT 2338 #define EACH_NAMED_CON(template) \ 2339 template(MethodHandles,GC_JVM_PUSH_LIMIT) \ 2340 template(MethodHandles,GC_JVM_STACK_MOVE_UNIT) \ 2341 template(MethodHandles,ETF_HANDLE_OR_METHOD_NAME) \ 2342 template(MethodHandles,ETF_DIRECT_HANDLE) \ 2343 template(MethodHandles,ETF_METHOD_NAME) \ 2344 template(MethodHandles,ETF_REFLECT_METHOD) \ 2345 template(java_lang_invoke_MemberName,MN_IS_METHOD) \ 2346 template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \ 2347 template(java_lang_invoke_MemberName,MN_IS_FIELD) \ 2348 template(java_lang_invoke_MemberName,MN_IS_TYPE) \ 2349 template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \ 2350 template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \ 2351 template(java_lang_invoke_MemberName,VM_INDEX_UNINITIALIZED) \ 2352 template(java_lang_invoke_AdapterMethodHandle,OP_RETYPE_ONLY) \ 2353 template(java_lang_invoke_AdapterMethodHandle,OP_RETYPE_RAW) \ 2354 template(java_lang_invoke_AdapterMethodHandle,OP_CHECK_CAST) \ 2355 template(java_lang_invoke_AdapterMethodHandle,OP_PRIM_TO_PRIM) \ 2356 template(java_lang_invoke_AdapterMethodHandle,OP_REF_TO_PRIM) \ 2357 template(java_lang_invoke_AdapterMethodHandle,OP_PRIM_TO_REF) \ 2358 template(java_lang_invoke_AdapterMethodHandle,OP_SWAP_ARGS) \ 2359 template(java_lang_invoke_AdapterMethodHandle,OP_ROT_ARGS) \ 2360 template(java_lang_invoke_AdapterMethodHandle,OP_DUP_ARGS) \ 2361 template(java_lang_invoke_AdapterMethodHandle,OP_DROP_ARGS) \ 2362 template(java_lang_invoke_AdapterMethodHandle,OP_COLLECT_ARGS) \ 2363 template(java_lang_invoke_AdapterMethodHandle,OP_SPREAD_ARGS) \ 2364 template(java_lang_invoke_AdapterMethodHandle,OP_FLYBY) \ 2365 template(java_lang_invoke_AdapterMethodHandle,OP_RICOCHET) \ 2366 template(java_lang_invoke_AdapterMethodHandle,CONV_OP_LIMIT) \ 2367 template(java_lang_invoke_AdapterMethodHandle,CONV_OP_MASK) \ 2368 template(java_lang_invoke_AdapterMethodHandle,CONV_VMINFO_MASK) \ 2369 template(java_lang_invoke_AdapterMethodHandle,CONV_VMINFO_SHIFT) \ 2370 template(java_lang_invoke_AdapterMethodHandle,CONV_OP_SHIFT) \ 2371 template(java_lang_invoke_AdapterMethodHandle,CONV_DEST_TYPE_SHIFT) \ 2372 template(java_lang_invoke_AdapterMethodHandle,CONV_SRC_TYPE_SHIFT) \ 2373 template(java_lang_invoke_AdapterMethodHandle,CONV_STACK_MOVE_SHIFT) \ 2374 template(java_lang_invoke_AdapterMethodHandle,CONV_STACK_MOVE_MASK) \ 2375 /*end*/ 2376 2377 #define ONE_PLUS(scope,value) 1+ 2378 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS) 0; 2379 #define VALUE_COMMA(scope,value) scope::value, 2380 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA) 0 }; 2381 #define STRING_NULL(scope,value) #value "\0" 2382 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL) }; 2383 2384 #undef ONE_PLUS 2385 #undef VALUE_COMMA 2386 #undef STRING_NULL 2387 #undef EACH_NAMED_CON 2388 #endif 2389 2390 JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { 2391 #ifndef PRODUCT 2392 if (which >= 0 && which < con_value_count) { 2393 int con = con_values[which]; 2394 objArrayOop box = (objArrayOop) JNIHandles::resolve(box_jh); 2395 if (box != NULL && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) { 2396 const char* str = &con_names[0]; 2397 for (int i = 0; i < which; i++) 2398 str += strlen(str) + 1; // skip name and null 2399 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); 2400 box->obj_at_put(0, name); 2401 } 2402 return con; 2403 } 2404 #endif 2405 return 0; 2406 } 2407 JVM_END 2408 2409 // void init(MemberName self, AccessibleObject ref) 2410 JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) { 2411 if (mname_jh == NULL || target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 2412 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 2413 oop target_oop = JNIHandles::resolve_non_null(target_jh); 2414 MethodHandles::init_MemberName(mname(), target_oop); 2415 } 2416 JVM_END 2417 2418 // void expand(MemberName self) 2419 JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) { 2420 if (mname_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 2421 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 2422 MethodHandles::expand_MemberName(mname, 0, CHECK); 2423 } 2424 JVM_END 2425 2426 // void resolve(MemberName self, Class<?> caller) 2427 JVM_ENTRY(void, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { 2428 if (mname_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 2429 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 2430 2431 // The trusted Java code that calls this method should already have performed 2432 // access checks on behalf of the given caller. But, we can verify this. 2433 if (VerifyMethodHandles && caller_jh != NULL) { 2434 klassOop reference_klass = java_lang_Class::as_klassOop(java_lang_invoke_MemberName::clazz(mname())); 2435 if (reference_klass != NULL) { 2436 // Emulate LinkResolver::check_klass_accessability. 2437 klassOop caller = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh)); 2438 if (!Reflection::verify_class_access(caller, 2439 reference_klass, 2440 true)) { 2441 THROW_MSG(vmSymbols::java_lang_InternalError(), Klass::cast(reference_klass)->external_name()); 2442 } 2443 } 2444 } 2445 2446 MethodHandles::resolve_MemberName(mname, CHECK); 2447 } 2448 JVM_END 2449 2450 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 2451 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 2452 JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls, 2453 jclass clazz_jh, jstring name_jh, jstring sig_jh, 2454 int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) { 2455 if (clazz_jh == NULL || results_jh == NULL) return -1; 2456 klassOop k_oop = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz_jh)); 2457 2458 objArrayOop results = (objArrayOop) JNIHandles::resolve(results_jh); 2459 if (results == NULL || !results->is_objArray()) return -1; 2460 2461 TempNewSymbol name = NULL; 2462 TempNewSymbol sig = NULL; 2463 if (name_jh != NULL) { 2464 name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh)); 2465 if (name == NULL) return 0; // a match is not possible 2466 } 2467 if (sig_jh != NULL) { 2468 sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh)); 2469 if (sig == NULL) return 0; // a match is not possible 2470 } 2471 2472 klassOop caller = NULL; 2473 if (caller_jh != NULL) { 2474 oop caller_oop = JNIHandles::resolve_non_null(caller_jh); 2475 if (!java_lang_Class::is_instance(caller_oop)) return -1; 2476 caller = java_lang_Class::as_klassOop(caller_oop); 2477 } 2478 2479 if (name != NULL && sig != NULL && results != NULL) { 2480 // try a direct resolve 2481 // %%% TO DO 2482 } 2483 2484 int res = MethodHandles::find_MemberNames(k_oop, name, sig, mflags, 2485 caller, skip, results); 2486 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks 2487 return res; 2488 } 2489 JVM_END 2490 2491 JVM_ENTRY(void, MHN_registerBootstrap(JNIEnv *env, jobject igcls, jclass caller_jh, jobject bsm_jh)) { 2492 instanceKlassHandle ik = MethodHandles::resolve_instance_klass(caller_jh, THREAD); 2493 if (!AllowTransitionalJSR292) { 2494 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 2495 "registerBootstrapMethod is only supported in JSR 292 EDR"); 2496 } 2497 ik->link_class(CHECK); 2498 if (!java_lang_invoke_MethodHandle::is_instance(JNIHandles::resolve(bsm_jh))) { 2499 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "method handle"); 2500 } 2501 const char* err = NULL; 2502 if (ik->is_initialized() || ik->is_in_error_state()) { 2503 err = "too late: class is already initialized"; 2504 } else { 2505 ObjectLocker ol(ik, THREAD); // note: this should be a recursive lock 2506 if (ik->is_not_initialized() || 2507 (ik->is_being_initialized() && ik->is_reentrant_initialization(THREAD))) { 2508 if (ik->bootstrap_method() != NULL) { 2509 err = "class is already equipped with a bootstrap method"; 2510 } else { 2511 ik->set_bootstrap_method(JNIHandles::resolve_non_null(bsm_jh)); 2512 err = NULL; 2513 } 2514 } else { 2515 err = "class is already initialized"; 2516 if (ik->is_being_initialized()) 2517 err = "class is already being initialized in a different thread"; 2518 } 2519 } 2520 if (err != NULL) { 2521 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), err); 2522 } 2523 } 2524 JVM_END 2525 2526 JVM_ENTRY(jobject, MHN_getBootstrap(JNIEnv *env, jobject igcls, jclass caller_jh)) { 2527 if (!AllowTransitionalJSR292) 2528 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "getBootstrap: transitional only"); 2529 instanceKlassHandle ik = MethodHandles::resolve_instance_klass(caller_jh, THREAD); 2530 return JNIHandles::make_local(THREAD, ik->bootstrap_method()); 2531 } 2532 JVM_END 2533 2534 JVM_ENTRY(void, MHN_setCallSiteTarget(JNIEnv *env, jobject igcls, jobject site_jh, jobject target_jh)) { 2535 if (!AllowTransitionalJSR292) 2536 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "setCallSite: transitional only"); 2537 } 2538 JVM_END 2539 2540 2541 /// JVM_RegisterMethodHandleMethods 2542 2543 #define LANG "Ljava/lang/" 2544 #define JLINV "Ljava/lang/invoke/" /* standard package */ 2545 #define JDYN "Ljava/dyn/" /* alternative package to JLINV if AllowTransitionalJSR292 */ 2546 #define IDYN "Lsun/dyn/" /* alternative package to JDYN if AllowTransitionalJSR292 */ 2547 // FIXME: After AllowTransitionalJSR292 is removed, replace JDYN and IDYN by JLINV. 2548 2549 #define OBJ LANG"Object;" 2550 #define CLS LANG"Class;" 2551 #define STRG LANG"String;" 2552 #define CST JDYN"CallSite;" 2553 #define MT JDYN"MethodType;" 2554 #define MH JDYN"MethodHandle;" 2555 #define MEM IDYN"MemberName;" 2556 #define AMH IDYN"AdapterMethodHandle;" 2557 #define BMH IDYN"BoundMethodHandle;" 2558 #define DMH IDYN"DirectMethodHandle;" 2559 2560 #define CC (char*) /*cast a literal from (const char*)*/ 2561 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 2562 2563 // These are the native methods on sun.invoke.MethodHandleNatives. 2564 static JNINativeMethod methods[] = { 2565 // void init(MemberName self, AccessibleObject ref) 2566 {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHN_init_AMH)}, 2567 {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHN_init_BMH)}, 2568 {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHN_init_DMH)}, 2569 {CC"init", CC"("MT")V", FN_PTR(MHN_init_MT)}, 2570 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, 2571 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, 2572 {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHN_resolve_Mem)}, 2573 {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHN_getTarget)}, 2574 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, 2575 // static native int getNamedCon(int which, Object[] name) 2576 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, 2577 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 2578 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 2579 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)} 2580 }; 2581 2582 // FIXME: Remove methods2 after AllowTransitionalJSR292 is removed. 2583 static JNINativeMethod methods2[] = { 2584 {CC"registerBootstrap", CC"("CLS MH")V", FN_PTR(MHN_registerBootstrap)}, 2585 {CC"getBootstrap", CC"("CLS")"MH, FN_PTR(MHN_getBootstrap)}, 2586 {CC"setCallSiteTarget", CC"("CST MH")V", FN_PTR(MHN_setCallSiteTarget)} 2587 }; 2588 2589 static void hack_signatures(JNINativeMethod* methods, jint num_methods, const char* from_sig, const char* to_sig) { 2590 for (int i = 0; i < num_methods; i++) { 2591 const char* sig = methods[i].signature; 2592 if (!strstr(sig, from_sig)) continue; 2593 size_t buflen = strlen(sig) + 100; 2594 char* buf = NEW_C_HEAP_ARRAY(char, buflen); 2595 char* bufp = buf; 2596 const char* sigp = sig; 2597 size_t from_len = strlen(from_sig), to_len = strlen(to_sig); 2598 while (*sigp != '\0') { 2599 assert(bufp < buf + buflen - to_len - 1, "oob"); 2600 if (strncmp(sigp, from_sig, from_len) != 0) { 2601 *bufp++ = *sigp++; 2602 } else { 2603 strcpy(bufp, to_sig); 2604 bufp += to_len; 2605 sigp += from_len; 2606 } 2607 } 2608 *bufp = '\0'; 2609 methods[i].signature = buf; // replace with new signature 2610 if (TraceMethodHandles) 2611 tty->print_cr("MethodHandleNatives: %s: change signature %s => %s", methods[i].name, sig, buf); 2612 } 2613 } 2614 2615 // This one function is exported, used by NativeLookup. 2616 2617 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { 2618 assert(MethodHandles::spot_check_entry_names(), "entry enum is OK"); 2619 2620 if (!EnableInvokeDynamic) { 2621 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); 2622 return; // bind nothing 2623 } 2624 2625 if (SystemDictionary::MethodHandleNatives_klass() != NULL && 2626 SystemDictionary::MethodHandleNatives_klass() != java_lang_Class::as_klassOop(JNIHandles::resolve(MHN_class))) { 2627 warning("multiple versions of MethodHandleNatives in boot classpath; consider using -XX:+PreferTransitionalJSR292"); 2628 THROW_MSG(vmSymbols::java_lang_InternalError(), "multiple versions of MethodHandleNatives in boot classpath; consider using -XX:+PreferTransitionalJSR292"); 2629 } 2630 2631 bool enable_MH = true; 2632 2633 // Loop control. FIXME: Replace by dead reckoning after AllowTransitionalJSR292 is removed. 2634 bool registered_natives = false; 2635 bool try_plain = true, try_JDYN = true, try_IDYN = true; 2636 for (;;) { 2637 ThreadToNativeFromVM ttnfv(thread); 2638 2639 if (try_plain) { try_plain = false; } 2640 else if (try_JDYN) { try_JDYN = false; hack_signatures(methods, sizeof(methods)/sizeof(JNINativeMethod), IDYN, JDYN); } 2641 else if (try_IDYN) { try_IDYN = false; hack_signatures(methods, sizeof(methods)/sizeof(JNINativeMethod), JDYN, JLINV); } 2642 else { break; } 2643 int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod)); 2644 if (env->ExceptionOccurred()) { 2645 env->ExceptionClear(); 2646 // and try again... 2647 } else { 2648 registered_natives = true; 2649 break; 2650 } 2651 } 2652 if (!registered_natives) { 2653 MethodHandles::set_enabled(false); 2654 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 2655 enable_MH = false; 2656 } 2657 2658 if (enable_MH) { 2659 bool found_raise_exception = false; 2660 KlassHandle MHN_klass = SystemDictionaryHandles::MethodHandleNatives_klass(); 2661 KlassHandle MHI_klass = SystemDictionaryHandles::MethodHandleImpl_klass(); 2662 // Loop control. FIXME: Replace by dead reckoning after AllowTransitionalJSR292 is removed. 2663 bool try_MHN = true, try_MHI = AllowTransitionalJSR292; 2664 for (;;) { 2665 KlassHandle try_klass; 2666 if (try_MHN) { try_MHN = false; try_klass = MHN_klass; } 2667 else if (try_MHI) { try_MHI = false; try_klass = MHI_klass; } 2668 else { break; } 2669 if (try_klass.is_null()) continue; 2670 TempNewSymbol raiseException_name = SymbolTable::new_symbol("raiseException", CHECK); 2671 TempNewSymbol raiseException_sig = SymbolTable::new_symbol("(ILjava/lang/Object;Ljava/lang/Object;)V", CHECK); 2672 methodOop raiseException_method = instanceKlass::cast(try_klass->as_klassOop()) 2673 ->find_method(raiseException_name, raiseException_sig); 2674 if (raiseException_method != NULL && raiseException_method->is_static()) { 2675 MethodHandles::set_raise_exception_method(raiseException_method); 2676 found_raise_exception = true; 2677 break; 2678 } 2679 } 2680 if (!found_raise_exception) { 2681 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 2682 enable_MH = false; 2683 } 2684 } 2685 2686 if (enable_MH) { 2687 if (AllowTransitionalJSR292) { 2688 // We need to link the MethodHandleImpl klass before we generate 2689 // the method handle adapters as the _raise_exception adapter uses 2690 // one of its methods (and its c2i-adapter). 2691 klassOop k = SystemDictionary::MethodHandleImpl_klass(); 2692 if (k != NULL) { 2693 instanceKlass* ik = instanceKlass::cast(k); 2694 ik->link_class(CHECK); 2695 } 2696 } 2697 2698 MethodHandles::generate_adapters(); 2699 MethodHandles::set_enabled(true); 2700 } 2701 2702 if (AllowTransitionalJSR292) { 2703 ThreadToNativeFromVM ttnfv(thread); 2704 2705 int status = env->RegisterNatives(MHN_class, methods2, sizeof(methods2)/sizeof(JNINativeMethod)); 2706 if (env->ExceptionOccurred()) { 2707 // Don't do this, since it's too late: 2708 // MethodHandles::set_enabled(false) 2709 env->ExceptionClear(); 2710 } 2711 } 2712 } 2713 JVM_END