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