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